shell.con commit pathspec: small readability changes (4f1bf4d)
   1#include "cache.h"
   2#include "quote.h"
   3#include "exec_cmd.h"
   4#include "strbuf.h"
   5#include "run-command.h"
   6
   7#define COMMAND_DIR "git-shell-commands"
   8#define HELP_COMMAND COMMAND_DIR "/help"
   9#define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
  10
  11static int do_generic_cmd(const char *me, char *arg)
  12{
  13        const char *my_argv[4];
  14
  15        setup_path();
  16        if (!arg || !(arg = sq_dequote(arg)))
  17                die("bad argument");
  18        if (!starts_with(me, "git-"))
  19                die("bad command");
  20
  21        my_argv[0] = me + 4;
  22        my_argv[1] = arg;
  23        my_argv[2] = NULL;
  24
  25        return execv_git_cmd(my_argv);
  26}
  27
  28static int do_cvs_cmd(const char *me, char *arg)
  29{
  30        const char *cvsserver_argv[3] = {
  31                "cvsserver", "server", NULL
  32        };
  33
  34        if (!arg || strcmp(arg, "server"))
  35                die("git-cvsserver only handles server: %s", arg);
  36
  37        setup_path();
  38        return execv_git_cmd(cvsserver_argv);
  39}
  40
  41static int is_valid_cmd_name(const char *cmd)
  42{
  43        /* Test command contains no . or / characters */
  44        return cmd[strcspn(cmd, "./")] == '\0';
  45}
  46
  47static char *make_cmd(const char *prog)
  48{
  49        return xstrfmt("%s/%s", COMMAND_DIR, prog);
  50}
  51
  52static void cd_to_homedir(void)
  53{
  54        const char *home = getenv("HOME");
  55        if (!home)
  56                die("could not determine user's home directory; HOME is unset");
  57        if (chdir(home) == -1)
  58                die("could not chdir to user's home directory");
  59}
  60
  61static void run_shell(void)
  62{
  63        int done = 0;
  64        static const char *help_argv[] = { HELP_COMMAND, NULL };
  65
  66        if (!access(NOLOGIN_COMMAND, F_OK)) {
  67                /* Interactive login disabled. */
  68                const char *argv[] = { NOLOGIN_COMMAND, NULL };
  69                int status;
  70
  71                status = run_command_v_opt(argv, 0);
  72                if (status < 0)
  73                        exit(127);
  74                exit(status);
  75        }
  76
  77        /* Print help if enabled */
  78        run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
  79
  80        do {
  81                struct strbuf line = STRBUF_INIT;
  82                const char *prog;
  83                char *full_cmd;
  84                char *rawargs;
  85                char *split_args;
  86                const char **argv;
  87                int code;
  88                int count;
  89
  90                fprintf(stderr, "git> ");
  91                if (strbuf_getline_lf(&line, stdin) == EOF) {
  92                        fprintf(stderr, "\n");
  93                        strbuf_release(&line);
  94                        break;
  95                }
  96                strbuf_trim(&line);
  97                rawargs = strbuf_detach(&line, NULL);
  98                split_args = xstrdup(rawargs);
  99                count = split_cmdline(split_args, &argv);
 100                if (count < 0) {
 101                        fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
 102                                split_cmdline_strerror(count));
 103                        free(split_args);
 104                        free(rawargs);
 105                        continue;
 106                }
 107
 108                prog = argv[0];
 109                if (!strcmp(prog, "")) {
 110                } else if (!strcmp(prog, "quit") || !strcmp(prog, "logout") ||
 111                           !strcmp(prog, "exit") || !strcmp(prog, "bye")) {
 112                        done = 1;
 113                } else if (is_valid_cmd_name(prog)) {
 114                        full_cmd = make_cmd(prog);
 115                        argv[0] = full_cmd;
 116                        code = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
 117                        if (code == -1 && errno == ENOENT) {
 118                                fprintf(stderr, "unrecognized command '%s'\n", prog);
 119                        }
 120                        free(full_cmd);
 121                } else {
 122                        fprintf(stderr, "invalid command format '%s'\n", prog);
 123                }
 124
 125                free(argv);
 126                free(rawargs);
 127        } while (!done);
 128}
 129
 130static struct commands {
 131        const char *name;
 132        int (*exec)(const char *me, char *arg);
 133} cmd_list[] = {
 134        { "git-receive-pack", do_generic_cmd },
 135        { "git-upload-pack", do_generic_cmd },
 136        { "git-upload-archive", do_generic_cmd },
 137        { "cvs", do_cvs_cmd },
 138        { NULL },
 139};
 140
 141int cmd_main(int argc, const char **argv)
 142{
 143        char *prog;
 144        const char **user_argv;
 145        struct commands *cmd;
 146        int count;
 147
 148        /*
 149         * Special hack to pretend to be a CVS server
 150         */
 151        if (argc == 2 && !strcmp(argv[1], "cvs server")) {
 152                argv--;
 153        } else if (argc == 1) {
 154                /* Allow the user to run an interactive shell */
 155                cd_to_homedir();
 156                if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
 157                        die("Interactive git shell is not enabled.\n"
 158                            "hint: ~/" COMMAND_DIR " should exist "
 159                            "and have read and execute access.");
 160                }
 161                run_shell();
 162                exit(0);
 163        } else if (argc != 3 || strcmp(argv[1], "-c")) {
 164                /*
 165                 * We do not accept any other modes except "-c" followed by
 166                 * "cmd arg", where "cmd" is a very limited subset of git
 167                 * commands or a command in the COMMAND_DIR
 168                 */
 169                die("Run with no arguments or with -c cmd");
 170        }
 171
 172        prog = xstrdup(argv[2]);
 173        if (!strncmp(prog, "git", 3) && isspace(prog[3]))
 174                /* Accept "git foo" as if the caller said "git-foo". */
 175                prog[3] = '-';
 176
 177        for (cmd = cmd_list ; cmd->name ; cmd++) {
 178                int len = strlen(cmd->name);
 179                char *arg;
 180                if (strncmp(cmd->name, prog, len))
 181                        continue;
 182                arg = NULL;
 183                switch (prog[len]) {
 184                case '\0':
 185                        arg = NULL;
 186                        break;
 187                case ' ':
 188                        arg = prog + len + 1;
 189                        break;
 190                default:
 191                        continue;
 192                }
 193                exit(cmd->exec(cmd->name, arg));
 194        }
 195
 196        cd_to_homedir();
 197        count = split_cmdline(prog, &user_argv);
 198        if (count >= 0) {
 199                if (is_valid_cmd_name(user_argv[0])) {
 200                        prog = make_cmd(user_argv[0]);
 201                        user_argv[0] = prog;
 202                        execv(user_argv[0], (char *const *) user_argv);
 203                }
 204                free(prog);
 205                free(user_argv);
 206                die("unrecognized command '%s'", argv[2]);
 207        } else {
 208                free(prog);
 209                die("invalid command format '%s': %s", argv[2],
 210                    split_cmdline_strerror(count));
 211        }
 212}