| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
#include "strlist.h" |
| 34 |
#include "xalloc.h" |
| 35 |
|
| 36 |
void |
| 37 |
strlist_init(struct strlist *l) |
| 38 |
{ |
| 39 |
l->first = l->last = NULL; |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
strlist_free(struct strlist *l) |
| 44 |
{ |
| 45 |
struct string *s1, *s2; |
| 46 |
|
| 47 |
STRLIST_FOREACH_MUTABLE(s1, l, s2) { |
| 48 |
free(s1->value); |
| 49 |
free(s1); |
| 50 |
} |
| 51 |
l->first = l->last = NULL; |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
strlist_make_array(const struct strlist *l, char ***a, size_t *len) |
| 56 |
{ |
| 57 |
const struct string *s; |
| 58 |
char **i; |
| 59 |
|
| 60 |
*len = 0; |
| 61 |
|
| 62 |
STRLIST_FOREACH(s, l) |
| 63 |
++*len; |
| 64 |
|
| 65 |
*a = xcalloc(*len + 1, sizeof(*i)); |
| 66 |
i = *a; |
| 67 |
|
| 68 |
STRLIST_FOREACH(s, l) |
| 69 |
*i++ = xstrdup(s->value); |
| 70 |
*i = NULL; |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
strlist_print(const struct strlist *l, FILE *f, int esc, const char *sep) |
| 75 |
{ |
| 76 |
const struct string *s; |
| 77 |
int quote, first = 1; |
| 78 |
const char *p; |
| 79 |
|
| 80 |
STRLIST_FOREACH(s, l) { |
| 81 |
if (!first) |
| 82 |
fputs(sep, f); |
| 83 |
quote = 0; |
| 84 |
if (esc) { |
| 85 |
for (p = s->value; *p; p++) { |
| 86 |
if ((*p >= '0' && *p <= '9') |
| 87 |
|| (*p >= 'a' && *p <= 'z') |
| 88 |
|| (*p >= 'A' && *p <= 'Z') |
| 89 |
|| *p == '.' || *p == '/' |
| 90 |
|| *p == '-' || *p == '_') |
| 91 |
continue; |
| 92 |
quote = 1; |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
if (quote) |
| 97 |
putc('"', f); |
| 98 |
for (p = s->value; *p; p++) { |
| 99 |
if (quote && (*p == '"' || *p == '$' |
| 100 |
|| *p == '\\' || *p == '`')) |
| 101 |
putc('\\', f); |
| 102 |
putc(*p, f); |
| 103 |
} |
| 104 |
if (quote) |
| 105 |
putc('"', f); |
| 106 |
first = 0; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
strlist_append_nocopy(struct strlist *l, char *val) |
| 112 |
{ |
| 113 |
struct string *s; |
| 114 |
|
| 115 |
s = xmalloc(sizeof(*s)); |
| 116 |
s->next = NULL; |
| 117 |
s->value = val; |
| 118 |
if (l->last != NULL) { |
| 119 |
l->last->next = s; |
| 120 |
l->last = s; |
| 121 |
} else { |
| 122 |
l->last = s; |
| 123 |
l->first = s; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
strlist_append(struct strlist *l, const char *val) |
| 129 |
{ |
| 130 |
strlist_append_nocopy(l, xstrdup(val)); |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
strlist_append_list(struct strlist *l, const struct strlist *l2) |
| 135 |
{ |
| 136 |
struct string *s; |
| 137 |
|
| 138 |
STRLIST_FOREACH(s, l2) |
| 139 |
strlist_append(l, s->value); |
| 140 |
} |
| 141 |
|
| 142 |
void |
| 143 |
strlist_append_array(struct strlist *l, const char * const *strings) |
| 144 |
{ |
| 145 |
for (; *strings != NULL; ++strings) |
| 146 |
strlist_append(l, *strings); |
| 147 |
} |
| 148 |
|
| 149 |
void |
| 150 |
strlist_prepend_nocopy(struct strlist *l, char *val) |
| 151 |
{ |
| 152 |
struct string *s; |
| 153 |
|
| 154 |
s = xmalloc(sizeof(*s)); |
| 155 |
s->next = l->first; |
| 156 |
s->value = val; |
| 157 |
l->first = s; |
| 158 |
if (l->last == NULL) { |
| 159 |
l->last = s; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void |
| 164 |
strlist_prepend(struct strlist *l, const char *val) |
| 165 |
{ |
| 166 |
strlist_prepend_nocopy(l, xstrdup(val)); |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
strlist_prepend_list(struct strlist *l, const struct strlist *l2) |
| 171 |
{ |
| 172 |
struct string *s, *s2, *s3, *s4; |
| 173 |
|
| 174 |
if (STRLIST_EMPTY(l2)) |
| 175 |
return; |
| 176 |
|
| 177 |
if (STRLIST_EMPTY(l)) { |
| 178 |
strlist_append_list(l, l2); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
s2 = NULL; |
| 183 |
s4 = l->first; |
| 184 |
STRLIST_FOREACH(s, l2) { |
| 185 |
s3 = xmalloc(sizeof(*s3)); |
| 186 |
s3->value = xstrdup(s->value); |
| 187 |
s3->next = s4; |
| 188 |
if (s2 == NULL) |
| 189 |
l->first = s3; |
| 190 |
else |
| 191 |
s2->next = s3; |
| 192 |
} |
| 193 |
} |