The words of shell text [p, e): split at blanks outside quotes, quotes * and backslash escapes removed (a backslash-newline is a blank). */
| 940 | /* The words of shell text [p, e): split at blanks outside quotes, quotes |
| 941 | * and backslash escapes removed (a backslash-newline is a blank). */ |
| 942 | static int dk_words(dk_t *d, const char *p, const char *e, const char **out, int max) { |
| 943 | int n = 0; |
| 944 | while (p < e && n < max) { |
| 945 | while (p < e && (isspace((unsigned char)*p) || |
| 946 | (*p == '\\' && p + SKIP_ONE < e && p[SKIP_ONE] == '\n'))) { |
| 947 | p += *p == '\\' ? PAIR_LEN : SKIP_ONE; |
| 948 | } |
| 949 | if (p >= e) { |
| 950 | break; |
| 951 | } |
| 952 | char *w = cbm_arena_alloc(d->a, (size_t)(e - p) + SKIP_ONE); |
| 953 | if (!w) { |
| 954 | return n; |
| 955 | } |
| 956 | size_t len = 0; |
| 957 | char quote = 0; |
| 958 | while (p < e && (quote || !isspace((unsigned char)*p))) { |
| 959 | char c = *p++; |
| 960 | if (quote) { |
| 961 | if (c == quote) { |
| 962 | quote = 0; |
| 963 | } else if (c == '\\' && quote == '"' && p < e && |
| 964 | (*p == '"' || *p == '\\' || *p == '$' || *p == '`')) { |
| 965 | w[len++] = *p++; |
| 966 | } else { |
| 967 | w[len++] = c; |
| 968 | } |
| 969 | } else if (c == '\'' || c == '"') { |
| 970 | quote = c; |
| 971 | } else if (c == '\\' && p < e) { |
| 972 | if (*p == '\n') { |
| 973 | p++; |
| 974 | break; |
| 975 | } |
| 976 | w[len++] = *p++; |
| 977 | } else { |
| 978 | w[len++] = c; |
| 979 | } |
| 980 | } |
| 981 | w[len] = '\0'; |
| 982 | out[n++] = w; |
| 983 | } |
| 984 | return n; |
| 985 | } |
| 986 | |
| 987 | /* `go build` flags that take the next word as their value (go help build). */ |
| 988 | static bool dk_go_value_flag(const char *f) { |
no test coverage detected