shell.con commit Makefile: fix shell quoting (bfce508)
   1#include "cache.h"
   2#include "quote.h"
   3#include "exec_cmd.h"
   4#include "strbuf.h"
   5
   6static int do_generic_cmd(const char *me, char *arg)
   7{
   8        const char *my_argv[4];
   9
  10        setup_path(NULL);
  11        if (!arg || !(arg = sq_dequote(arg)))
  12                die("bad argument");
  13        if (prefixcmp(me, "git-"))
  14                die("bad command");
  15
  16        my_argv[0] = me + 4;
  17        my_argv[1] = arg;
  18        my_argv[2] = NULL;
  19
  20        return execv_git_cmd(my_argv);
  21}
  22
  23static int do_cvs_cmd(const char *me, char *arg)
  24{
  25        const char *cvsserver_argv[3] = {
  26                "cvsserver", "server", NULL
  27        };
  28
  29        if (!arg || strcmp(arg, "server"))
  30                die("git-cvsserver only handles server: %s", arg);
  31
  32        setup_path(NULL);
  33        return execv_git_cmd(cvsserver_argv);
  34}
  35
  36
  37static 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 },
  45};
  46
  47int main(int argc, char **argv)
  48{
  49        char *prog;
  50        struct commands *cmd;
  51
  52        /*
  53         * Special hack to pretend to be a CVS server
  54         */
  55        if (argc == 2 && !strcmp(argv[1], "cvs server"))
  56                argv--;
  57
  58        /*
  59         * We do not accept anything but "-c" followed by "cmd arg",
  60         * where "cmd" is a very limited subset of git commands.
  61         */
  62        else if (argc != 3 || strcmp(argv[1], "-c"))
  63                die("What do you think I am? A shell?");
  64
  65        prog = argv[2];
  66        if (!strncmp(prog, "git", 3) && isspace(prog[3]))
  67                /* Accept "git foo" as if the caller said "git-foo". */
  68                prog[3] = '-';
  69
  70        for (cmd = cmd_list ; cmd->name ; cmd++) {
  71                int len = strlen(cmd->name);
  72                char *arg;
  73                if (strncmp(cmd->name, prog, len))
  74                        continue;
  75                arg = NULL;
  76                switch (prog[len]) {
  77                case '\0':
  78                        arg = NULL;
  79                        break;
  80                case ' ':
  81                        arg = prog + len + 1;
  82                        break;
  83                default:
  84                        continue;
  85                }
  86                exit(cmd->exec(cmd->name, arg));
  87        }
  88        die("unrecognized command '%s'", prog);
  89}