alias.con commit t0002: add test for enter_repo(), non-strict mode (3104120)
   1#include "cache.h"
   2
   3char *alias_lookup(const char *alias)
   4{
   5        char *v = NULL;
   6        struct strbuf key = STRBUF_INIT;
   7        strbuf_addf(&key, "alias.%s", alias);
   8        if (git_config_key_is_valid(key.buf))
   9                git_config_get_string(key.buf, &v);
  10        strbuf_release(&key);
  11        return v;
  12}
  13
  14#define SPLIT_CMDLINE_BAD_ENDING 1
  15#define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
  16static const char *split_cmdline_errors[] = {
  17        "cmdline ends with \\",
  18        "unclosed quote"
  19};
  20
  21int split_cmdline(char *cmdline, const char ***argv)
  22{
  23        int src, dst, count = 0, size = 16;
  24        char quoted = 0;
  25
  26        *argv = xmalloc(sizeof(**argv) * size);
  27
  28        /* split alias_string */
  29        (*argv)[count++] = cmdline;
  30        for (src = dst = 0; cmdline[src];) {
  31                char c = cmdline[src];
  32                if (!quoted && isspace(c)) {
  33                        cmdline[dst++] = 0;
  34                        while (cmdline[++src]
  35                                        && isspace(cmdline[src]))
  36                                ; /* skip */
  37                        ALLOC_GROW(*argv, count + 1, size);
  38                        (*argv)[count++] = cmdline + dst;
  39                } else if (!quoted && (c == '\'' || c == '"')) {
  40                        quoted = c;
  41                        src++;
  42                } else if (c == quoted) {
  43                        quoted = 0;
  44                        src++;
  45                } else {
  46                        if (c == '\\' && quoted != '\'') {
  47                                src++;
  48                                c = cmdline[src];
  49                                if (!c) {
  50                                        free(*argv);
  51                                        *argv = NULL;
  52                                        return -SPLIT_CMDLINE_BAD_ENDING;
  53                                }
  54                        }
  55                        cmdline[dst++] = c;
  56                        src++;
  57                }
  58        }
  59
  60        cmdline[dst] = 0;
  61
  62        if (quoted) {
  63                free(*argv);
  64                *argv = NULL;
  65                return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
  66        }
  67
  68        ALLOC_GROW(*argv, count + 1, size);
  69        (*argv)[count] = NULL;
  70
  71        return count;
  72}
  73
  74const char *split_cmdline_strerror(int split_cmdline_errno)
  75{
  76        return split_cmdline_errors[-split_cmdline_errno - 1];
  77}