close another possibility for propagating pack corruption
[git/spearce.git] / shell.c
blobe3393690dd3b79af80e6b95710583e744fd574d4
1 #include "cache.h"
2 #include "quote.h"
3 #include "exec_cmd.h"
4 #include "strbuf.h"
6 static int do_generic_cmd(const char *me, char *arg)
8 const char *my_argv[4];
10 setup_path();
11 if (!arg || !(arg = sq_dequote(arg)))
12 die("bad argument");
13 if (prefixcmp(me, "git-"))
14 die("bad command");
16 my_argv[0] = me + 4;
17 my_argv[1] = arg;
18 my_argv[2] = NULL;
20 return execv_git_cmd(my_argv);
23 static int do_cvs_cmd(const char *me, char *arg)
25 const char *cvsserver_argv[3] = {
26 "cvsserver", "server", NULL
29 if (!arg || strcmp(arg, "server"))
30 die("git-cvsserver only handles server: %s", arg);
32 setup_path();
33 return execv_git_cmd(cvsserver_argv);
37 static struct commands {
38 const char *name;
39 int (*exec)(const char *me, char *arg);
40 } cmd_list[] = {
41 { "git-receive-pack", do_generic_cmd },
42 { "git-upload-pack", do_generic_cmd },
43 { "cvs", do_cvs_cmd },
44 { NULL },
47 int main(int argc, char **argv)
49 char *prog;
50 struct commands *cmd;
51 int devnull_fd;
54 * Always open file descriptors 0/1/2 to avoid clobbering files
55 * in die(). It also avoids not messing up when the pipes are
56 * dup'ed onto stdin/stdout/stderr in the child processes we spawn.
58 devnull_fd = open("/dev/null", O_RDWR);
59 while (devnull_fd >= 0 && devnull_fd <= 2)
60 devnull_fd = dup(devnull_fd);
61 if (devnull_fd == -1)
62 die("opening /dev/null failed (%s)", strerror(errno));
63 close (devnull_fd);
66 * Special hack to pretend to be a CVS server
68 if (argc == 2 && !strcmp(argv[1], "cvs server"))
69 argv--;
72 * We do not accept anything but "-c" followed by "cmd arg",
73 * where "cmd" is a very limited subset of git commands.
75 else if (argc != 3 || strcmp(argv[1], "-c"))
76 die("What do you think I am? A shell?");
78 prog = argv[2];
79 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
80 /* Accept "git foo" as if the caller said "git-foo". */
81 prog[3] = '-';
83 for (cmd = cmd_list ; cmd->name ; cmd++) {
84 int len = strlen(cmd->name);
85 char *arg;
86 if (strncmp(cmd->name, prog, len))
87 continue;
88 arg = NULL;
89 switch (prog[len]) {
90 case '\0':
91 arg = NULL;
92 break;
93 case ' ':
94 arg = prog + len + 1;
95 break;
96 default:
97 continue;
99 exit(cmd->exec(cmd->name, arg));
101 die("unrecognized command '%s'", prog);