pexecute.c
来自「GCC编译器源代码」· C语言 代码 · 共 655 行 · 第 1/2 页
C
655 行
/* ??? Does OS2 have process.h? */extern int spawnv ();extern int spawnvp ();intpexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) const char *program; char * const *argv; const char *this_pname; const char *temp_base; char **errmsg_fmt, **errmsg_arg; int flags;{ int pid; if ((flags & PEXECUTE_ONE) != PEXECUTE_ONE) abort (); /* ??? Presumably 1 == _P_NOWAIT. */ pid = (flags & PEXECUTE_SEARCH ? spawnvp : spawnv) (1, program, argv); if (pid == -1) { *errmsg_fmt = install_error_msg; *errmsg_arg = program; return -1; } return pid;}intpwait (pid, status, flags) int pid; int *status; int flags;{ /* ??? Here's an opportunity to canonicalize the values in STATUS. Needed? */ int pid = wait (status); return pid;}#endif /* OS2 */#ifdef MPW/* MPW pexecute doesn't actually run anything; instead, it writes out script commands that, when run, will do the actual executing. For example, in GCC's case, GCC will write out several script commands: cpp ... cc1 ... as ... ld ... and then exit. None of the above programs will have run yet. The task that called GCC will then execute the script and cause cpp,etc. to run. The caller must invoke pfinish before calling exit. This adds the finishing touches to the generated script. */static int first_time = 1;intpexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) const char *program; char * const *argv; const char *this_pname; const char *temp_base; char **errmsg_fmt, **errmsg_arg; int flags;{ char tmpprogram[255]; char *cp, *tmpname; int i; mpwify_filename (program, tmpprogram); if (first_time) { printf ("Set Failed 0\n"); first_time = 0; } fputs ("If {Failed} == 0\n", stdout); /* If being verbose, output a copy of the command. It should be accurate enough and escaped enough to be "clickable". */ if (flags & PEXECUTE_VERBOSE) { fputs ("\tEcho ", stdout); fputc ('\'', stdout); fputs (tmpprogram, stdout); fputc ('\'', stdout); fputc (' ', stdout); for (i=1; argv[i]; i++) { fputc ('\'', stdout); /* See if we have an argument that needs fixing. */ if (strchr(argv[i], '/')) { tmpname = (char *) xmalloc (256); mpwify_filename (argv[i], tmpname); argv[i] = tmpname; } for (cp = argv[i]; *cp; cp++) { /* Write an Option-d escape char in front of special chars. */ if (strchr("'+", *cp)) fputc ('\266', stdout); fputc (*cp, stdout); } fputc ('\'', stdout); fputc (' ', stdout); } fputs ("\n", stdout); } fputs ("\t", stdout); fputs (tmpprogram, stdout); fputc (' ', stdout); for (i=1; argv[i]; i++) { /* See if we have an argument that needs fixing. */ if (strchr(argv[i], '/')) { tmpname = (char *) xmalloc (256); mpwify_filename (argv[i], tmpname); argv[i] = tmpname; } if (strchr (argv[i], ' ')) fputc ('\'', stdout); for (cp = argv[i]; *cp; cp++) { /* Write an Option-d escape char in front of special chars. */ if (strchr("'+", *cp)) fputc ('\266', stdout); fputc (*cp, stdout); } if (strchr (argv[i], ' ')) fputc ('\'', stdout); fputc (' ', stdout); } fputs ("\n", stdout); /* Output commands that arrange to clean up and exit if a failure occurs. We have to be careful to collect the status from the program that was run, rather than some other script command. Also, we don't exit immediately, since necessary cleanups are at the end of the script. */ fputs ("\tSet TmpStatus {Status}\n", stdout); fputs ("\tIf {TmpStatus} != 0\n", stdout); fputs ("\t\tSet Failed {TmpStatus}\n", stdout); fputs ("\tEnd\n", stdout); fputs ("End\n", stdout); /* We're just composing a script, can't fail here. */ return 0;}intpwait (pid, status, flags) int pid; int *status; int flags;{ *status = 0; return 0;}/* Write out commands that will exit with the correct error code if something in the script failed. */voidpfinish (){ printf ("\tExit \"{Failed}\"\n");}#endif /* MPW *//* include for Unix-like environments but not for Dos-like environments */#if ! defined (__MSDOS__) && ! defined (OS2) && ! defined (MPW) \ && ! defined (_WIN32)#ifdef VMS#define vfork() (decc$$alloc_vfork_blocks() >= 0 ? \ lib$get_current_invo_context(decc$$get_vfork_jmpbuf()) : -1)#else#ifdef USG#define vfork fork#endif#endifextern int execv ();extern int execvp ();intpexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags) const char *program; char * const *argv; const char *this_pname; const char *temp_base; char **errmsg_fmt, **errmsg_arg; int flags;{ int (*func)() = (flags & PEXECUTE_SEARCH ? execvp : execv); int pid; int pdes[2]; int input_desc, output_desc; int retries, sleep_interval; /* Pipe waiting from last process, to be used as input for the next one. Value is STDIN_FILE_NO if no pipe is waiting (i.e. the next command is the first of a group). */ static int last_pipe_input; /* If this is the first process, initialize. */ if (flags & PEXECUTE_FIRST) last_pipe_input = STDIN_FILE_NO; input_desc = last_pipe_input; /* If this isn't the last process, make a pipe for its output, and record it as waiting to be the input to the next process. */ if (! (flags & PEXECUTE_LAST)) { if (pipe (pdes) < 0) { *errmsg_fmt = "pipe"; *errmsg_arg = NULL; return -1; } output_desc = pdes[WRITE_PORT]; last_pipe_input = pdes[READ_PORT]; } else { /* Last process. */ output_desc = STDOUT_FILE_NO; last_pipe_input = STDIN_FILE_NO; } /* Fork a subprocess; wait and retry if it fails. */ sleep_interval = 1; for (retries = 0; retries < 4; retries++) { pid = vfork (); if (pid >= 0) break; sleep (sleep_interval); sleep_interval *= 2; } switch (pid) { case -1: {#ifdef vfork *errmsg_fmt = "fork";#else *errmsg_fmt = "vfork";#endif *errmsg_arg = NULL; return -1; } case 0: /* child */ /* Move the input and output pipes into place, if necessary. */ if (input_desc != STDIN_FILE_NO) { close (STDIN_FILE_NO); dup (input_desc); close (input_desc); } if (output_desc != STDOUT_FILE_NO) { close (STDOUT_FILE_NO); dup (output_desc); close (output_desc); } /* Close the parent's descs that aren't wanted here. */ if (last_pipe_input != STDIN_FILE_NO) close (last_pipe_input); /* Exec the program. */ (*func) (program, argv); /* Note: Calling fprintf and exit here doesn't seem right for vfork. */ fprintf (stderr, "%s: ", this_pname); fprintf (stderr, install_error_msg, program);#ifdef IN_GCC fprintf (stderr, ": %s\n", my_strerror (errno));#else fprintf (stderr, ": %s\n", xstrerror (errno));#endif exit (-1); /* NOTREACHED */ return 0; default: /* In the parent, after forking. Close the descriptors that we made for this child. */ if (input_desc != STDIN_FILE_NO) close (input_desc); if (output_desc != STDOUT_FILE_NO) close (output_desc); /* Return child's process number. */ return pid; }}intpwait (pid, status, flags) int pid; int *status; int flags;{ /* ??? Here's an opportunity to canonicalize the values in STATUS. Needed? */#ifdef VMS pid = waitpid (-1, status, 0);#else pid = wait (status);#endif return pid;}#endif /* ! __MSDOS__ && ! OS2 && ! MPW && ! _WIN32 */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?