* Memory management
*/
-# ifdef __APPLE__
-/* under darwin, the environment gets added to the process at startup time. So, it's not defined at library link time, forcing us to extra tricks */
-# include <crt_externs.h>
-# define environ (*_NSGetEnviron())
-# else
-/* the environment, as specified by the opengroup, used to initialize the process properties */
-extern char **environ;
-# endif
-
void rctx_empty(rctx_t rc) {
int i;
char **env_it=environ;
if (rctx->cmd) {
if (!rctx->is_empty) {
ERROR2("[%s] More than one command in this chunk of lines (previous: %s).\n"
- " Dunno which input/output belongs to which command.",
+ " Cannot guess which input/output belongs to which command.",
filepos,rctx->cmd);
ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
rctx_armageddon(rctx,1);
VERB1("[%s] (ignore output of next command)", filepos);
} else if (!strncmp(line,"setenv ",strlen("setenv "))) {
+ int len = strlen("setenv ");
+ char *eq = strchr(line+len,'=');
+ char *key = bprintf("%.*s",(int)(eq-line-len),line+len);
+ xbt_dict_set(env,key,xbt_strdup(eq+1),xbt_free_f);
+
rctx->env = realloc(rctx->env,++(rctx->env_size)*sizeof(char*));
- rctx->env[rctx->env_size-2] = xbt_strdup(line+strlen("setenv "));
+ rctx->env[rctx->env_size-2] = xbt_strdup(line+len);
rctx->env[rctx->env_size-1] = NULL;
- VERB1("[%s] (ignore output of next command)", filepos);
+ VERB2("[%s] setenv %s", filepos,line+len);
} else {
ERROR2("%s: Malformed metacommand: %s",filepos,line);
return NULL;
}
+/* Special command: mkfile is a building creating a file with the input data as content */
+static void rctx_mkfile(void) {
+ char *filename = xbt_strdup(rctx->cmd + strlen("mkfile "));
+ FILE*OUT;
+ xbt_str_trim(filename,NULL);
+ OUT=fopen(filename,"w");
+ if (!OUT) {
+ free(filename);
+ THROW3(system_error,errno,"%s: Cannot create file %s: %s",rctx->filepos,filename,strerror(errno));
+ }
+ fprintf(OUT,"%s",rctx->input->data);
+ fclose(OUT);
+}
+
+/* function to be called from the child to start the actual process */
+static void start_command(rctx_t rctx){
+ xbt_dynar_t cmd = xbt_str_split_quoted(rctx->cmd);
+ char *binary_name = NULL;
+ unsigned int it;
+ char *str;
+ xbt_dynar_get_cpy(cmd,0,&binary_name);
+ char **args = xbt_new(char*,xbt_dynar_length(cmd)+1);
+ int errcode;
+
+ if (!strncmp(rctx->cmd,"mkfile ",strlen("mkfile "))) {
+ rctx_mkfile();
+ exit(0); /* end the working child */
+ }
+
+ xbt_dynar_foreach(cmd,it,str) {
+ args[it] = xbt_strdup(str);
+ }
+ args[it] = NULL;
+
+ /* To search for the right executable path when not trivial */
+ struct stat stat_buf;
+
+ /* build the command line */
+ if (stat(binary_name, &stat_buf)) {
+ /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
+ int i;
+
+ for (i = 0; environ[i]; i++) {
+ if (!strncmp("PATH=", environ[i], 5)) {
+ xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
+
+ xbt_dynar_foreach(path, it, str) {
+ if (binary_name)
+ free(binary_name);
+ binary_name = bprintf("%s/%s", str, args[0]);
+ if (!stat(binary_name, &stat_buf)) {
+ /* Found. */
+ DEBUG1("Looked in the PATH for the binary. Found %s",
+ binary_name);
+ xbt_dynar_free(&path);
+ break;
+ }
+ }
+ xbt_dynar_free(&path);
+ if (stat(binary_name, &stat_buf)) {
+ /* not found */
+ printf("TESH_ERROR Command %s not found\n",args[0]);
+ exit(127);
+ }
+ break;
+ }
+ }
+ } else {
+ binary_name = xbt_strdup(args[0]);
+ }
+
+ errcode = execve(binary_name, args, rctx->env);
+ printf("TESH_ERROR %s: Cannot start %s: %s\n",rctx->filepos,rctx->cmd, strerror(errcode));
+ exit(127);
+}
+
/* Start a new child, plug the pipes as expected and fire up the
helping threads. Is also waits for the child to end if this is a
foreground job, or fire up a thread to wait otherwise. */
-
void rctx_start(void) {
int child_in[2];
int child_out[2];
+ DEBUG1("Cmd before rewriting %s",rctx->cmd);
+ rctx->cmd = xbt_str_varsubst(rctx->cmd,env);
VERB2("Start %s %s",rctx->cmd,(rctx->is_background?"(background job)":""));
if (pipe(child_in) || pipe(child_out)) {
perror("Cannot open the pipes");
dup2(child_out[1],2);
close(child_out[1]);
- xbt_dynar_t cmd = xbt_str_split_quoted(rctx->cmd);
- char *file;
- unsigned int it;
- char *str;
- char *long_cmd=xbt_strdup("");
- xbt_dynar_get_cpy(cmd,0,&file);
- char **args = xbt_new(char*,xbt_dynar_length(cmd)+1);
- xbt_dynar_foreach(cmd,it,str) {
- args[it] = xbt_strdup(str);
- long_cmd = bprintf("%s %s",long_cmd,str);
- }
- args[it] = NULL;
-
- /* To search for the right executable path when not trivial */
- struct stat stat_buf;
- char *binary_name = NULL;
-
- /* build the command line */
- if (stat(file, &stat_buf)) {
- /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
- int i;
-
- for (i = 0; environ[i]; i++) {
- if (!strncmp("PATH=", environ[i], 5)) {
- xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
-
- xbt_dynar_foreach(path, it, str) {
- if (binary_name)
- free(binary_name);
- binary_name = bprintf("%s/%s", str, file);
- if (!stat(binary_name, &stat_buf)) {
- /* Found. */
- DEBUG1("Looked in the PATH for the binary. Found %s",
- binary_name);
- xbt_dynar_free(&path);
- break;
- }
- }
- xbt_dynar_free(&path);
- if (stat(binary_name, &stat_buf)) {
- /* not found */
- ERROR1("Command %s not found",file);
- return;
- }
- break;
- }
- }
- } else {
- binary_name = xbt_strdup(file);
- }
-
-
- DEBUG2("execve %s %s env",binary_name,long_cmd);
- execve(binary_name, args, rctx->env);
+ start_command(rctx);
}
rctx->is_stoppable = 1;
rctx->expected_signal = NULL;
}
}
+ while (rctx->output_got->used && !strncmp(rctx->output_got->data,"TESH_ERROR ", strlen("TESH_ERROR "))) {
+ int marklen = strlen("TESH_ERROR ");
+ char *endline = strchr(rctx->output_got->data,'\n');
+
+ CRITICAL2("%.*s",endline -rctx->output_got->data-marklen, rctx->output_got->data+marklen);
+ memmove(rctx->output_got->data, rctx->output_got->data+marklen, rctx->output_got->used - marklen);
+ rctx->output_got->used -= endline -rctx->output_got->data+1;
+ rctx->output_got->data[rctx->output_got->used] = '\0';
+ errcode=1;
+ }
if ( rctx->output == e_output_check
&& ( rctx->output_got->used != rctx->output_wanted->used
xbt_dynar_free(&a);
INFO1("Here is the (ignored) command output: \n||%s",out);
free(out);
- } else if (errcode || rctx->interrupted) {
+ } else if ( (errcode&&errcode!=1) || rctx->interrupted) {
/* checking output, and matching */
xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
char *out = xbt_str_join(a,"\n||");