int main(int argc, char *argv[]) { char buf[MAX] = {0}; int count = 0; while ((count = get_cmds(buf, MAX)) > 0) { #if DEBUG == 1 print_cmds(count); #endif run_cmds(buf); init_pipes(); } }
int main(int argc, char *argv[]) { const char *progname; char **cmdv, *default_cmdv[] = { "caps" }; int c, result, cmdc; int print_level = LOG_INFO, verbose = 1, use_syslog = 1; clockid_t clkid; install_handler(SIGALRM, handle_alarm); /* Process the command line arguments. */ progname = strrchr(argv[0], '/'); progname = progname ? 1+progname : argv[0]; while (EOF != (c = getopt(argc, argv, "l:qQvh"))) { switch (c) { case 'l': if (get_arg_val_i(c, optarg, &print_level, PRINT_LEVEL_MIN, PRINT_LEVEL_MAX)) return -1; break; case 'q': use_syslog = 0; break; case 'Q': verbose = 0; break; case 'v': version_show(stdout); return 0; case 'h': usage(progname); return 0; default: usage(progname); return -1; } } print_set_progname(progname); print_set_verbose(verbose); print_set_syslog(use_syslog); print_set_level(print_level); if ((argc - optind) < 1) { usage(progname); return -1; } if ((argc - optind) == 1) { cmdv = default_cmdv; cmdc = 1; } else { cmdv = &argv[optind+1]; cmdc = argc - optind - 1; } clkid = clock_open(argv[optind]); if (clkid == CLOCK_INVALID) return -1; /* pass the remaining arguments to the run_cmds loop */ result = run_cmds(clkid, cmdc, cmdv); if (result < -1) { /* show usage when command fails */ usage(progname); return result; } return 0; }
static void parse_argv(ChildProcs *child_procs, Opts *opts, int argc, char *argv[]) { child_procs->n_cmds = 1; char **args_end = argv + argc; for (char **i = argv + 1; i < args_end; ++i) { if (! strcmp(*i, SEP)) ++child_procs->n_cmds; } child_procs->cmds = calloc(child_procs->n_cmds, sizeof(Cmd)); *opts = (Opts) { .signal_everything = false }; parse_cmd_args(opts, child_procs->cmds, argv + 1, args_end); char **arg_it = child_procs->cmds->args; int cmd_idx = 0; for (; arg_it < args_end; ++arg_it) { if (! strcmp(*arg_it, SEP)) { *arg_it = 0; // replace with null to terminate when passed to execvp if (arg_it + 1 == args_end) { fprintf(stderr, "command must follow `---'\n"); exit(1); } parse_cmd_args(opts, child_procs->cmds + (++cmd_idx), arg_it + 1, args_end); Cmd *cmd = child_procs->cmds + cmd_idx; arg_it = cmd->args - 1; } } } int main(int argc, char *argv[]) { if (argc == 1) { fprintf(stderr, "please supply at least one command to run\n"); return 1; } ChildProcs child_procs; Opts opts; parse_argv(&child_procs, &opts, argc, argv); int n_watch_cmds = 0; for (int i = 0; i < child_procs.n_cmds; ++i) { if (child_procs.cmds[i].watch) ++n_watch_cmds; } // if -f hasn't been used then watch every command if (0 == n_watch_cmds) { for (int i = 0; i < child_procs.n_cmds; ++i) { if (! child_procs.cmds[i].configuring) { ++n_watch_cmds; child_procs.cmds[i].watch = true; } } } Cmd **watch_cmds = calloc(n_watch_cmds, sizeof(Cmd *)); { int watch_cmd_end = 0; for (int i = 0; i < child_procs.n_cmds; ++i) { if (child_procs.cmds[i].watch) watch_cmds[watch_cmd_end++] = child_procs.cmds + i; } } install_term_and_int_handlers(); int error_code; run_configure_cmds(child_procs.n_cmds, child_procs.cmds); if (running) { run_cmds(child_procs.n_cmds, child_procs.cmds); error_code = wait_for_requested_commands_to_exit(n_watch_cmds, watch_cmds); remove_term_and_int_handlers(); alarm(WAIT_FOR_PROC_DEATH_TIMEOUT); kill(opts.signal_everything ? -1 : 0, SIGTERM); wait_for_all_processes_to_exit(error_code); DPRINTF("all processes exited cleanly"); } free(watch_cmds); free(child_procs.cmds); return error_code; }