diff options
Diffstat (limited to 'sway/stringop.c')
-rw-r--r-- | sway/stringop.c | 111 |
1 files changed, 79 insertions, 32 deletions
diff --git a/sway/stringop.c b/sway/stringop.c index 191e40c8..90f963d6 100644 --- a/sway/stringop.c +++ b/sway/stringop.c | |||
@@ -105,40 +105,40 @@ char **split_args(const char *start, int *argc) { | |||
105 | bool in_char = false; | 105 | bool in_char = false; |
106 | bool escaped = false; | 106 | bool escaped = false; |
107 | const char *end = start; | 107 | const char *end = start; |
108 | while (*start) { | 108 | if (start) { |
109 | if (!in_token) { | 109 | while (*start) { |
110 | start = (end += strspn(end, whitespace)); | 110 | if (!in_token) { |
111 | in_token = true; | 111 | start = (end += strspn(end, whitespace)); |
112 | } | 112 | in_token = true; |
113 | if (*end == '"' && !in_char && !escaped) { | 113 | } |
114 | in_string = !in_string; | 114 | if (*end == '"' && !in_char && !escaped) { |
115 | } else if (*end == '\'' && !in_string && !escaped) { | 115 | in_string = !in_string; |
116 | in_char = !in_char; | 116 | } else if (*end == '\'' && !in_string && !escaped) { |
117 | } else if (*end == '\\') { | 117 | in_char = !in_char; |
118 | escaped = !escaped; | 118 | } else if (*end == '\\') { |
119 | } else if (*end == '\0' || (!in_string && !in_char && !escaped | 119 | escaped = !escaped; |
120 | && strchr(whitespace, *end))) { | 120 | } else if (*end == '\0' || (!in_string && !in_char && !escaped |
121 | goto add_part; | 121 | && strchr(whitespace, *end))) { |
122 | } | 122 | goto add_token; |
123 | if (*end != '\\') { | 123 | } |
124 | escaped = false; | 124 | if (*end != '\\') { |
125 | } | 125 | escaped = false; |
126 | ++end; | 126 | } |
127 | continue; | 127 | ++end; |
128 | add_part: | 128 | continue; |
129 | if (end - start > 0) { | 129 | add_token: |
130 | char *token = malloc(end - start + 1); | 130 | if (end - start > 0) { |
131 | strncpy(token, start, end - start + 1); | 131 | char *token = malloc(end - start + 1); |
132 | token[end - start] = '\0'; | 132 | strncpy(token, start, end - start + 1); |
133 | strip_quotes(token); | 133 | token[end - start] = '\0'; |
134 | unescape_string(token); | 134 | argv[*argc] = token; |
135 | argv[*argc] = token; | 135 | if (++*argc + 1 == alloc) { |
136 | if (++*argc + 1 == alloc) { | 136 | argv = realloc(argv, (alloc *= 2) * sizeof(char *)); |
137 | argv = realloc(argv, (alloc *= 2) * sizeof(char *)); | 137 | } |
138 | } | 138 | } |
139 | in_token = false; | ||
140 | escaped = false; | ||
139 | } | 141 | } |
140 | in_token = false; | ||
141 | escaped = false; | ||
142 | } | 142 | } |
143 | argv[*argc] = NULL; | 143 | argv[*argc] = NULL; |
144 | return argv; | 144 | return argv; |
@@ -311,3 +311,50 @@ char *join_list(list_t *list, char *separator) { | |||
311 | 311 | ||
312 | return res; | 312 | return res; |
313 | } | 313 | } |
314 | |||
315 | char *cmdsep(char **stringp, const char *delim) { | ||
316 | char *head = strsep(stringp, delim); | ||
317 | // But skip over trailing delims. '3 tokens here' -> '3' 'tokens here' | ||
318 | if (*stringp) { | ||
319 | *stringp += strspn(*stringp, delim); | ||
320 | // If skiping over delims brings us to the end of string, set to NULL | ||
321 | if (!**stringp) *stringp = NULL; | ||
322 | } | ||
323 | return head; | ||
324 | } | ||
325 | |||
326 | char *argsep(char **stringp, const char *delim) { | ||
327 | char *start = *stringp; | ||
328 | char *end = start; | ||
329 | bool in_string = false; | ||
330 | bool in_char = false; | ||
331 | bool escaped = false; | ||
332 | while (1) { | ||
333 | if (*end == '"' && !in_char && !escaped) { | ||
334 | in_string = !in_string; | ||
335 | } else if (*end == '\'' && !in_string && !escaped) { | ||
336 | in_char = !in_char; | ||
337 | } else if (*end == '\\') { | ||
338 | escaped = !escaped; | ||
339 | } else if (*end == '\0') { | ||
340 | *stringp = NULL; | ||
341 | goto found; | ||
342 | } else if (!in_string && !in_char && !escaped && strchr(delim, *end)) { | ||
343 | if (end - start) { | ||
344 | *(end++) = 0; | ||
345 | *stringp = end + strspn(end, delim);; | ||
346 | if (!**stringp) *stringp = NULL; | ||
347 | goto found; | ||
348 | } else { | ||
349 | ++start; | ||
350 | end = start; | ||
351 | } | ||
352 | } | ||
353 | if (*end != '\\') { | ||
354 | escaped = false; | ||
355 | } | ||
356 | ++end; | ||
357 | } | ||
358 | found: | ||
359 | return start; | ||
360 | } | ||