2 * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 static char * token_unescape(char *start, char *end)
28 s = n = malloc(end - start);
31 for (r = start + 1; r < end; r++) {
36 } else if (*r == '\\') {
47 static char * token_next(char *line, char **end)
54 while (isspace(*start))
63 else if (cend == '\\')
70 while (*lend && !isspace(*lend))
77 char ** tokens_new(char *line)
92 nargs = realloc(args, alloc * sizeof(char *));
94 for (i = 0; i < alloc - inc; i++)
100 for (i = alloc - inc; i < alloc; i++)
104 start = token_next(end, &end);
108 args[next] = token_unescape(start, end - 1);
110 args[next] = strndup(start, end - start);
113 for (i = 0; i < alloc; i++)
124 void tokens_free(char **args)
127 for (arg = args; *arg; arg++)