include $PATH in generating list of commands for "help -a"
[git/git.git] / shell.c
CommitLineData
35eb2d36
LT
1#include "cache.h"
2#include "quote.h"
77cb17e9 3#include "exec_cmd.h"
0c696fe7 4#include "strbuf.h"
35eb2d36
LT
5
6static int do_generic_cmd(const char *me, char *arg)
7{
8 const char *my_argv[4];
9
ab5f8627 10 if (!arg || !(arg = sq_dequote(arg)))
35eb2d36 11 die("bad argument");
cc44c765 12 if (prefixcmp(me, "git-"))
77cb17e9 13 die("bad command");
35eb2d36 14
77cb17e9 15 my_argv[0] = me + 4;
35eb2d36
LT
16 my_argv[1] = arg;
17 my_argv[2] = NULL;
18
9201c707 19 return execv_git_cmd(my_argv);
35eb2d36
LT
20}
21
0c696fe7
JS
22static int do_cvs_cmd(const char *me, char *arg)
23{
24 const char *cvsserver_argv[3] = {
25 "cvsserver", "server", NULL
26 };
27 const char *oldpath = getenv("PATH");
28 struct strbuf newpath = STRBUF_INIT;
29
30 if (!arg || strcmp(arg, "server"))
31 die("git-cvsserver only handles server: %s", arg);
32
33 strbuf_addstr(&newpath, git_exec_path());
34 strbuf_addch(&newpath, ':');
35 strbuf_addstr(&newpath, oldpath);
36
37 setenv("PATH", strbuf_detach(&newpath, NULL), 1);
38
39 return execv_git_cmd(cvsserver_argv);
40}
41
42
35eb2d36
LT
43static 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 },
0c696fe7 49 { "cvs", do_cvs_cmd },
35eb2d36
LT
50 { NULL },
51};
52
53int main(int argc, char **argv)
54{
55 char *prog;
56 struct commands *cmd;
57
0c696fe7
JS
58 if (argc == 2 && !strcmp(argv[1], "cvs server"))
59 argv--;
35eb2d36 60 /* We want to see "-c cmd args", and nothing else */
0c696fe7 61 else if (argc != 3 || strcmp(argv[1], "-c"))
35eb2d36
LT
62 die("What do you think I am? A shell?");
63
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;
82 }
83 exit(cmd->exec(cmd->name, arg));
84 }
85 die("unrecognized command '%s'", prog);
86}