Add tests for parse-options.c
[git/mjg.git] / shell.c
blobcfe372b21379486ea0c7e4d5686c08064c20c279
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 if (!arg || !(arg = sq_dequote(arg)))
11 die("bad argument");
12 if (prefixcmp(me, "git-"))
13 die("bad command");
15 my_argv[0] = me + 4;
16 my_argv[1] = arg;
17 my_argv[2] = NULL;
19 return execv_git_cmd(my_argv);
22 static int do_cvs_cmd(const char *me, char *arg)
24 const char *cvsserver_argv[3] = {
25 "cvsserver", "server", NULL
27 const char *oldpath = getenv("PATH");
28 struct strbuf newpath = STRBUF_INIT;
30 if (!arg || strcmp(arg, "server"))
31 die("git-cvsserver only handles server: %s", arg);
33 strbuf_addstr(&newpath, git_exec_path());
34 strbuf_addch(&newpath, ':');
35 strbuf_addstr(&newpath, oldpath);
37 setenv("PATH", strbuf_detach(&newpath, NULL), 1);
39 return execv_git_cmd(cvsserver_argv);
43 static struct commands {
44 const char *name;
45 int (*exec)(const char *me, char *arg);
46 } cmd_list[] = {
47 { "git-receive-pack", do_generic_cmd },
48 { "git-upload-pack", do_generic_cmd },
49 { "cvs", do_cvs_cmd },
50 { NULL },
53 int main(int argc, char **argv)
55 char *prog;
56 struct commands *cmd;
58 if (argc == 2 && !strcmp(argv[1], "cvs server"))
59 argv--;
60 /* We want to see "-c cmd args", and nothing else */
61 else if (argc != 3 || strcmp(argv[1], "-c"))
62 die("What do you think I am? A shell?");
64 prog = argv[2];
65 argv += 2;
66 argc -= 2;
67 for (cmd = cmd_list ; cmd->name ; cmd++) {
68 int len = strlen(cmd->name);
69 char *arg;
70 if (strncmp(cmd->name, prog, len))
71 continue;
72 arg = NULL;
73 switch (prog[len]) {
74 case '\0':
75 arg = NULL;
76 break;
77 case ' ':
78 arg = prog + len + 1;
79 break;
80 default:
81 continue;
83 exit(cmd->exec(cmd->name, arg));
85 die("unrecognized command '%s'", prog);