2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
16 #include "Conditions.h"
20 BaseJob::BaseJob(const char* name
)
38 return Title().String();
43 BaseJob::Condition() const
57 BaseJob::SetCondition(::Condition
* condition
)
59 fCondition
= condition
;
64 BaseJob::CheckCondition(ConditionContext
& context
) const
66 if (fCondition
!= NULL
)
67 return fCondition
->Test(context
);
74 BaseJob::Event() const
88 BaseJob::SetEvent(::Event
* event
)
92 event
->SetOwner(this);
96 /*! Determines whether the events of this job has been triggered
98 Note, if this job does not have any events, this method returns
102 BaseJob::EventHasTriggered() const
104 return Event() == NULL
|| Event()->Triggered();
109 BaseJob::Environment() const
116 BaseJob::Environment()
123 BaseJob::EnvironmentSourceFiles() const
130 BaseJob::EnvironmentSourceFiles()
137 BaseJob::SetEnvironment(const BMessage
& message
)
142 for (int32 index
= 0; message
.GetInfo(B_STRING_TYPE
, index
, &name
, &type
,
143 &count
) == B_OK
; index
++) {
144 if (strcmp(name
, "from_script") == 0) {
145 const char* fromScript
;
146 for (int32 scriptIndex
= 0; message
.FindString(name
, scriptIndex
,
147 &fromScript
) == B_OK
; scriptIndex
++) {
148 fSourceFiles
.Add(fromScript
);
153 BString variable
= name
;
156 const char* argument
;
157 for (int32 argumentIndex
= 0; message
.FindString(name
, argumentIndex
,
158 &argument
) == B_OK
; argumentIndex
++) {
159 if (argumentIndex
> 0)
161 variable
+= argument
;
164 fEnvironment
.Add(variable
);
170 BaseJob::GetSourceFilesEnvironment(BStringList
& environment
)
172 int32 count
= fSourceFiles
.CountStrings();
173 for (int32 index
= 0; index
< count
; index
++) {
174 _GetSourceFileEnvironment(fSourceFiles
.StringAt(index
), environment
);
179 /*! Gets the environment by evaluating the source files, and move that
180 environment to the static environment.
182 When this method returns, the source files list will be empty.
185 BaseJob::ResolveSourceFiles()
187 if (fSourceFiles
.IsEmpty())
190 GetSourceFilesEnvironment(fEnvironment
);
191 fSourceFiles
.MakeEmpty();
196 BaseJob::_GetSourceFileEnvironment(const char* script
, BStringList
& environment
)
199 if (pipe(&pipes
[0]) != 0) {
204 pid_t child
= fork();
207 debug_printf("could not fork: %s\n", strerror(errno
));
208 } else if (child
== 0) {
209 // We're the child, redirect stdout
210 close(STDOUT_FILENO
);
211 close(STDERR_FILENO
);
212 dup2(pipes
[1], STDOUT_FILENO
);
213 dup2(pipes
[1], STDERR_FILENO
);
215 for (int32 i
= 0; i
< 2; i
++)
219 command
.SetToFormat(". \"%s\"; export -p", script
);
220 execl("/bin/sh", "/bin/sh", "-c", command
.String(), NULL
);
223 // Retrieve environment from child
230 ssize_t bytesRead
= read(pipes
[0], buffer
, sizeof(buffer
) - 1);
234 // Make sure the buffer is null terminated
235 buffer
[bytesRead
] = 0;
237 const char* chunk
= buffer
;
239 const char* separator
= strchr(chunk
, '\n');
240 if (separator
== NULL
) {
241 line
.Append(chunk
, bytesRead
);
244 line
.Append(chunk
, separator
- chunk
);
245 chunk
= separator
+ 1;
247 _ParseExportVariable(environment
, line
);
252 _ParseExportVariable(environment
, line
);
260 BaseJob::_ParseExportVariable(BStringList
& environment
, const BString
& line
)
262 if (!line
.StartsWith("export "))
265 int separator
= line
.FindFirst("=\"");
270 line
.CopyInto(variable
, 7, separator
- 7);
273 line
.CopyInto(value
, separator
+ 2, line
.Length() - separator
- 3);
275 variable
<< "=" << value
;
276 environment
.Add(variable
);