cat-file: split --batch input lines on whitespace
[gitweb.git] / shell.c
diff --git a/shell.c b/shell.c
index e07aef4bf2331765deba3a344a2d1641becb4068..1429870a8f29d3d8b8e4d1d7ba3e228d15a73759 100644 (file)
--- a/shell.c
+++ b/shell.c
@@ -6,6 +6,7 @@
 
 #define COMMAND_DIR "git-shell-commands"
 #define HELP_COMMAND COMMAND_DIR "/help"
+#define NOLOGIN_COMMAND COMMAND_DIR "/no-interactive-login"
 
 static int do_generic_cmd(const char *me, char *arg)
 {
@@ -65,6 +66,18 @@ static void run_shell(void)
 {
        int done = 0;
        static const char *help_argv[] = { HELP_COMMAND, NULL };
+
+       if (!access(NOLOGIN_COMMAND, F_OK)) {
+               /* Interactive login disabled. */
+               const char *argv[] = { NOLOGIN_COMMAND, NULL };
+               int status;
+
+               status = run_command_v_opt(argv, 0);
+               if (status < 0)
+                       exit(127);
+               exit(status);
+       }
+
        /* Print help if enabled */
        run_command_v_opt(help_argv, RUN_SILENT_EXEC_FAILURE);
 
@@ -73,8 +86,10 @@ static void run_shell(void)
                const char *prog;
                char *full_cmd;
                char *rawargs;
+               char *split_args;
                const char **argv;
                int code;
+               int count;
 
                fprintf(stderr, "git> ");
                if (strbuf_getline(&line, stdin, '\n') == EOF) {
@@ -84,7 +99,12 @@ static void run_shell(void)
                }
                strbuf_trim(&line);
                rawargs = strbuf_detach(&line, NULL);
-               if (split_cmdline(rawargs, &argv) == -1) {
+               split_args = xstrdup(rawargs);
+               count = split_cmdline(split_args, &argv);
+               if (count < 0) {
+                       fprintf(stderr, "invalid command format '%s': %s\n", rawargs,
+                               split_cmdline_strerror(count));
+                       free(split_args);
                        free(rawargs);
                        continue;
                }
@@ -128,6 +148,11 @@ int main(int argc, char **argv)
        const char **user_argv;
        struct commands *cmd;
        int devnull_fd;
+       int count;
+
+       git_setup_gettext();
+
+       git_extract_argv0_path(argv[0]);
 
        /*
         * Always open file descriptors 0/1/2 to avoid clobbering files
@@ -149,8 +174,11 @@ int main(int argc, char **argv)
        } else if (argc == 1) {
                /* Allow the user to run an interactive shell */
                cd_to_homedir();
-               if (access(COMMAND_DIR, R_OK | X_OK) == -1)
-                       die("Sorry, the interactive git-shell is not enabled");
+               if (access(COMMAND_DIR, R_OK | X_OK) == -1) {
+                       die("Interactive git shell is not enabled.\n"
+                           "hint: ~/" COMMAND_DIR " should exist "
+                           "and have read and execute access.");
+               }
                run_shell();
                exit(0);
        } else if (argc != 3 || strcmp(argv[1], "-c")) {
@@ -187,7 +215,8 @@ int main(int argc, char **argv)
        }
 
        cd_to_homedir();
-       if (split_cmdline(prog, &user_argv) != -1) {
+       count = split_cmdline(prog, &user_argv);
+       if (count >= 0) {
                if (is_valid_cmd_name(user_argv[0])) {
                        prog = make_cmd(user_argv[0]);
                        user_argv[0] = prog;
@@ -198,6 +227,7 @@ int main(int argc, char **argv)
                die("unrecognized command '%s'", argv[2]);
        } else {
                free(prog);
-               die("invalid command format '%s'", argv[2]);
+               die("invalid command format '%s': %s", argv[2],
+                   split_cmdline_strerror(count));
        }
 }