static int run_argv(int *argcp, const char ***argv) { int done_alias = 0; while (1) { /* See if it's a builtin */ handle_builtin(*argcp, *argv); /* .. then try the external ones */ execv_dashed_external(*argv); /* It could be an alias -- this works around the insanity * of overriding "git log" with "git show" by having * alias.log = show */ if (done_alias || !handle_alias(argcp, argv)) break; done_alias = 1; } return done_alias; }
int cmd_main(int argc, const char **argv) { const char *cmd; int done_help = 0; cmd = argv[0]; if (!cmd) cmd = "git-help"; else { const char *slash = find_last_dir_sep(cmd); if (slash) cmd = slash + 1; } /* * wait_for_pager_atexit will close stdout/stderr so it needs to be * registered first so that it will execute last and not close the * handles until all other atexit handlers have finished */ atexit(wait_for_pager_atexit); trace_command_performance(argv); atexit(post_command_hook_atexit); /* * "git-xxxx" is the same as "git xxxx", but we obviously: * * - cannot take flags in between the "git" and the "xxxx". * - cannot execute it externally (since it would just do * the same thing over again) * * So we just directly call the builtin handler, and die if * that one cannot handle it. */ if (skip_prefix(cmd, "git-", &cmd)) { argv[0] = cmd; handle_builtin(argc, argv); die("cannot handle %s as a builtin", cmd); } /* Look for flags.. */ argv++; argc--; handle_options(&argv, &argc, NULL); if (argc > 0) { /* translate --help and --version into commands */ skip_prefix(argv[0], "--", &argv[0]); } else { /* The user didn't specify a command; give them help */ commit_pager_choice(); if (run_pre_command_hook(argv)) die("pre-command hook aborted command"); printf("usage: %s\n\n", git_usage_string); list_common_cmds_help(); printf("\n%s\n", _(git_more_info_string)); exit_code = 1; run_post_command_hook(); exit(exit_code); } cmd = argv[0]; /* * We use PATH to find git commands, but we prepend some higher * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH * environment, and the $(gitexecdir) from the Makefile at build * time. */ setup_path(); while (1) { int was_alias = run_argv(&argc, &argv); if (errno != ENOENT) break; if (was_alias) { fprintf(stderr, _("expansion of alias '%s' failed; " "'%s' is not a git command\n"), cmd, argv[0]); exit(1); } if (!done_help) { cmd = argv[0] = help_unknown_cmd(cmd); done_help = 1; } else break; } fprintf(stderr, _("failed to run command '%s': %s\n"), cmd, strerror(errno)); return 1; }
int main(int argc, char **av) { const char **argv = (const char **) av; const char *cmd; startup_info = &git_startup_info; cmd = git_extract_argv0_path(argv[0]); if (!cmd) cmd = "git-help"; /* * Always open file descriptors 0/1/2 to avoid clobbering files * in die(). It also avoids messing up when the pipes are dup'ed * onto stdin/stdout/stderr in the child processes we spawn. */ sanitize_stdfds(); git_setup_gettext(); /* * "git-xxxx" is the same as "git xxxx", but we obviously: * * - cannot take flags in between the "git" and the "xxxx". * - cannot execute it externally (since it would just do * the same thing over again) * * So we just directly call the builtin handler, and die if * that one cannot handle it. */ if (starts_with(cmd, "git-")) { cmd += 4; argv[0] = cmd; handle_builtin(argc, argv); die("cannot handle %s as a builtin", cmd); } /* Look for flags.. */ argv++; argc--; handle_options(&argv, &argc, NULL); if (argc > 0) { if (starts_with(argv[0], "--")) argv[0] += 2; } else { /* The user didn't specify a command; give them help */ commit_pager_choice(); printf("usage: %s\n\n", git_usage_string); list_common_cmds_help(); printf("\n%s\n", _(git_more_info_string)); exit(1); } cmd = argv[0]; /* * We use PATH to find git commands, but we prepend some higher * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH * environment, and the $(gitexecdir) from the Makefile at build * time. */ setup_path(); while (1) { static int done_help = 0; static int was_alias = 0; was_alias = run_argv(&argc, &argv); if (errno != ENOENT) break; if (was_alias) { fprintf(stderr, "Expansion of alias '%s' failed; " "'%s' is not a git command\n", cmd, argv[0]); exit(1); } if (!done_help) { cmd = argv[0] = help_unknown_cmd(cmd); done_help = 1; } else break; } fprintf(stderr, "Failed to run command '%s': %s\n", cmd, strerror(errno)); return 1; }
void execute(char *cmdline, char **argv) { char *cmd = cmdline; if(strlen(cmd) > 4) { if(strncmp(cmd, "res=", 4)==0) { point5(cmdline); return; } } if(strlen(cmdline)<=0) return; // 0 for stdout; 1 for redirect; 2 for pipeline int out_type = 0; char *childcmd[2]; childcmd[0] = cmdline; while(*cmdline != '>' && *cmdline != '|' && *cmdline != '$' && *cmdline != '\0' && *cmdline != '\n') { cmdline++; } if(*cmdline=='>') { out_type = 1; *cmdline++ = '\0'; childcmd[1] = cmdline; } else if(*cmdline=='|') { out_type = 2; *cmdline++ = '\0'; childcmd[1] = cmdline; } else if(*cmdline=='$') { if(strncmp(cmdline, "$(", 2)==0) { out_type = 3; *cmdline++ = '\0'; childcmd[1] = cmdline; } } int n = strlen(childcmd[0]) - 1; while(n>0) { if(childcmd[0][n]==' ') { childcmd[0][n]='\0'; } else break; n--; } while(*childcmd[1] != '\0') { if(*childcmd[1]==' ') childcmd[1]++; else break; } parse_cmdline(childcmd[0], argv); int ret = handle_builtin(argv); if(ret==0) return; pid_t childpid; int status, options = 0; childpid = fork(); if (childpid == -1) { perror("Cannot proceed. fork() error"); exit(1); } else if (childpid == 0) { // This is child's process. if(out_type==0) { childexec(argv); } else if(out_type==1) {// output redirection here int fd; close(STDOUT); fd = open(childcmd[1], O_APPEND); if(fd<0) { fd = creat(childcmd[1], 0666); if(fd<0) { perror("create file failed"); exit(-1); } } dup(fd); childexec(argv); close(fd); } else if(out_type==2) {// pipeline here int fd[2]; pipe(&fd[0]); if(fork()!=0) { close(fd[0]); close(STDOUT); dup(fd[1]); childexec(argv); close(fd[1]); } else { close(fd[1]); close(STDIN); dup(fd[0]); parse_cmdline(childcmd[1], argv); childexec(argv); close(fd[0]); } puts("\n"); } else if(out_type==3) { childcmd[1]++; size_t len = strlen(childcmd[1]); childcmd[1][len-1] = '\0'; printf("childcmd[0]:%s\n", childcmd[0]); printf("childcmd[1]:%s\n", childcmd[1]); int fd[2]; pipe(fd); if(fork()!=0) { close(fd[1]); close(STDIN); dup(fd[0]); childexecline(childcmd[0]); close(fd[0]); } else exec_pipelines(childcmd[1], fd,argv); /* parse_cmdline(childcmd[1], argv); int fd[2]; pipe(&fd[0]); if(fork()!=0) { close(fd[0]); close(STDOUT); dup(fd[1]); childexec(argv); close(fd[1]); } else { close(fd[1]); close(STDIN); dup(fd[0]); parse_cmdline(childcmd[0], argv); childexec(argv); close(fd[0]); } */ puts("\n"); } } else { waitpid(childpid, &status, options); if (!WIFEXITED(status)) printf("Parent: child has not terminated normally.\n"); } }