]>
Commit | Line | Data |
---|---|---|
812666c8 | 1 | #include "cache.h" |
a2fab531 | 2 | #include "refs.h" |
d1df5743 | 3 | |
698a68be JH |
4 | int prefixcmp(const char *str, const char *prefix) |
5 | { | |
6 | for (; ; str++, prefix++) | |
7 | if (!*prefix) | |
8 | return 0; | |
9 | else if (*str != *prefix) | |
10 | return (unsigned char)*prefix - (unsigned char)*str; | |
11 | } | |
12 | ||
8cc5b290 AP |
13 | int suffixcmp(const char *str, const char *suffix) |
14 | { | |
15 | int len = strlen(str), suflen = strlen(suffix); | |
16 | if (len < suflen) | |
17 | return -1; | |
18 | else | |
19 | return strcmp(str + len - suflen, suffix); | |
20 | } | |
21 | ||
b315c5c0 PH |
22 | /* |
23 | * Used as the default ->buf value, so that people can always assume | |
24 | * buf is non NULL and ->buf is NUL terminated even for a freshly | |
25 | * initialized strbuf. | |
26 | */ | |
27 | char strbuf_slopbuf[1]; | |
28 | ||
917c9a71 PH |
29 | void strbuf_init(struct strbuf *sb, size_t hint) |
30 | { | |
b315c5c0 PH |
31 | sb->alloc = sb->len = 0; |
32 | sb->buf = strbuf_slopbuf; | |
8c74ef1e | 33 | if (hint) |
f1696ee3 | 34 | strbuf_grow(sb, hint); |
d1df5743 JH |
35 | } |
36 | ||
917c9a71 PH |
37 | void strbuf_release(struct strbuf *sb) |
38 | { | |
b315c5c0 PH |
39 | if (sb->alloc) { |
40 | free(sb->buf); | |
41 | strbuf_init(sb, 0); | |
42 | } | |
b449f4cf PH |
43 | } |
44 | ||
b315c5c0 | 45 | char *strbuf_detach(struct strbuf *sb, size_t *sz) |
917c9a71 | 46 | { |
08ad56f3 JK |
47 | char *res; |
48 | strbuf_grow(sb, 0); | |
49 | res = sb->buf; | |
b315c5c0 PH |
50 | if (sz) |
51 | *sz = sb->len; | |
f1696ee3 | 52 | strbuf_init(sb, 0); |
b449f4cf PH |
53 | return res; |
54 | } | |
55 | ||
917c9a71 PH |
56 | void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc) |
57 | { | |
58 | strbuf_release(sb); | |
59 | sb->buf = buf; | |
60 | sb->len = len; | |
61 | sb->alloc = alloc; | |
62 | strbuf_grow(sb, 0); | |
63 | sb->buf[sb->len] = '\0'; | |
64 | } | |
65 | ||
66 | void strbuf_grow(struct strbuf *sb, size_t extra) | |
67 | { | |
8c74ef1e | 68 | int new_buf = !sb->alloc; |
1368f650 JN |
69 | if (unsigned_add_overflows(extra, 1) || |
70 | unsigned_add_overflows(sb->len, extra + 1)) | |
b449f4cf | 71 | die("you want to use way too much memory"); |
8c74ef1e | 72 | if (new_buf) |
b315c5c0 | 73 | sb->buf = NULL; |
b449f4cf | 74 | ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); |
8c74ef1e TR |
75 | if (new_buf) |
76 | sb->buf[0] = '\0'; | |
b449f4cf PH |
77 | } |
78 | ||
eacd6dc5 LS |
79 | void strbuf_trim(struct strbuf *sb) |
80 | { | |
81 | char *b = sb->buf; | |
82 | while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1])) | |
83 | sb->len--; | |
84 | while (sb->len > 0 && isspace(*b)) { | |
85 | b++; | |
86 | sb->len--; | |
87 | } | |
88 | memmove(sb->buf, b, sb->len); | |
89 | sb->buf[sb->len] = '\0'; | |
90 | } | |
f1696ee3 PH |
91 | void strbuf_rtrim(struct strbuf *sb) |
92 | { | |
93 | while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1])) | |
94 | sb->len--; | |
95 | sb->buf[sb->len] = '\0'; | |
96 | } | |
97 | ||
eacd6dc5 LS |
98 | void strbuf_ltrim(struct strbuf *sb) |
99 | { | |
100 | char *b = sb->buf; | |
101 | while (sb->len > 0 && isspace(*b)) { | |
102 | b++; | |
103 | sb->len--; | |
104 | } | |
105 | memmove(sb->buf, b, sb->len); | |
106 | sb->buf[sb->len] = '\0'; | |
107 | } | |
108 | ||
2f1d9e2b | 109 | struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max) |
eacd6dc5 | 110 | { |
b8c2c1fa MH |
111 | struct strbuf **ret = NULL; |
112 | size_t nr = 0, alloc = 0; | |
eacd6dc5 LS |
113 | struct strbuf *t; |
114 | ||
1173bb33 MH |
115 | while (slen) { |
116 | int len = slen; | |
117 | if (max <= 0 || nr + 1 < max) { | |
118 | const char *end = memchr(str, delim, slen); | |
119 | if (end) | |
120 | len = end - str + 1; | |
121 | } | |
eacd6dc5 LS |
122 | t = xmalloc(sizeof(struct strbuf)); |
123 | strbuf_init(t, len); | |
1173bb33 | 124 | strbuf_add(t, str, len); |
b8c2c1fa MH |
125 | ALLOC_GROW(ret, nr + 2, alloc); |
126 | ret[nr++] = t; | |
1173bb33 MH |
127 | str += len; |
128 | slen -= len; | |
eacd6dc5 | 129 | } |
b8c2c1fa MH |
130 | ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */ |
131 | ret[nr] = NULL; | |
eacd6dc5 LS |
132 | return ret; |
133 | } | |
134 | ||
135 | void strbuf_list_free(struct strbuf **sbs) | |
136 | { | |
137 | struct strbuf **s = sbs; | |
138 | ||
139 | while (*s) { | |
140 | strbuf_release(*s); | |
141 | free(*s++); | |
142 | } | |
143 | free(sbs); | |
144 | } | |
145 | ||
9b200fd6 | 146 | int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) |
45f66f64 | 147 | { |
8f024655 AR |
148 | int len = a->len < b->len ? a->len: b->len; |
149 | int cmp = memcmp(a->buf, b->buf, len); | |
150 | if (cmp) | |
151 | return cmp; | |
152 | return a->len < b->len ? -1: a->len != b->len; | |
45f66f64 PH |
153 | } |
154 | ||
917c9a71 PH |
155 | void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, |
156 | const void *data, size_t dlen) | |
157 | { | |
1368f650 | 158 | if (unsigned_add_overflows(pos, len)) |
917c9a71 PH |
159 | die("you want to use way too much memory"); |
160 | if (pos > sb->len) | |
161 | die("`pos' is too far after the end of the buffer"); | |
162 | if (pos + len > sb->len) | |
163 | die("`pos + len' is too far after the end of the buffer"); | |
164 | ||
165 | if (dlen >= len) | |
166 | strbuf_grow(sb, dlen - len); | |
167 | memmove(sb->buf + pos + dlen, | |
168 | sb->buf + pos + len, | |
169 | sb->len - pos - len); | |
170 | memcpy(sb->buf + pos, data, dlen); | |
171 | strbuf_setlen(sb, sb->len + dlen - len); | |
172 | } | |
173 | ||
c76689df PH |
174 | void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len) |
175 | { | |
176 | strbuf_splice(sb, pos, 0, data, len); | |
177 | } | |
178 | ||
179 | void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) | |
180 | { | |
181 | strbuf_splice(sb, pos, len, NULL, 0); | |
182 | } | |
183 | ||
917c9a71 PH |
184 | void strbuf_add(struct strbuf *sb, const void *data, size_t len) |
185 | { | |
b449f4cf PH |
186 | strbuf_grow(sb, len); |
187 | memcpy(sb->buf + sb->len, data, len); | |
188 | strbuf_setlen(sb, sb->len + len); | |
189 | } | |
190 | ||
91db267e RS |
191 | void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len) |
192 | { | |
193 | strbuf_grow(sb, len); | |
194 | memcpy(sb->buf + sb->len, sb->buf + pos, len); | |
195 | strbuf_setlen(sb, sb->len + len); | |
196 | } | |
197 | ||
917c9a71 PH |
198 | void strbuf_addf(struct strbuf *sb, const char *fmt, ...) |
199 | { | |
b449f4cf | 200 | va_list ap; |
ebeb6090 JK |
201 | va_start(ap, fmt); |
202 | strbuf_vaddf(sb, fmt, ap); | |
203 | va_end(ap); | |
204 | } | |
205 | ||
206 | void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap) | |
207 | { | |
208 | int len; | |
209 | va_list cp; | |
b449f4cf | 210 | |
f141bd80 SP |
211 | if (!strbuf_avail(sb)) |
212 | strbuf_grow(sb, 64); | |
ebeb6090 JK |
213 | va_copy(cp, ap); |
214 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp); | |
215 | va_end(cp); | |
f141bd80 | 216 | if (len < 0) |
ebeb6090 | 217 | die("BUG: your vsnprintf is broken (returned %d)", len); |
f1696ee3 | 218 | if (len > strbuf_avail(sb)) { |
b449f4cf | 219 | strbuf_grow(sb, len); |
b449f4cf | 220 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); |
ebeb6090 JK |
221 | if (len > strbuf_avail(sb)) |
222 | die("BUG: your vsnprintf is broken (insatiable)"); | |
b449f4cf PH |
223 | } |
224 | strbuf_setlen(sb, sb->len + len); | |
d1df5743 JH |
225 | } |
226 | ||
c3a670de MC |
227 | void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, |
228 | void *context) | |
cde75e59 RS |
229 | { |
230 | for (;;) { | |
c3a670de MC |
231 | const char *percent; |
232 | size_t consumed; | |
cde75e59 RS |
233 | |
234 | percent = strchrnul(format, '%'); | |
235 | strbuf_add(sb, format, percent - format); | |
236 | if (!*percent) | |
237 | break; | |
238 | format = percent + 1; | |
239 | ||
0a0416a3 JK |
240 | if (*format == '%') { |
241 | strbuf_addch(sb, '%'); | |
242 | format++; | |
243 | continue; | |
244 | } | |
245 | ||
c3a670de MC |
246 | consumed = fn(sb, format, context); |
247 | if (consumed) | |
248 | format += consumed; | |
249 | else | |
cde75e59 RS |
250 | strbuf_addch(sb, '%'); |
251 | } | |
252 | } | |
253 | ||
9b864e73 RS |
254 | size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, |
255 | void *context) | |
256 | { | |
257 | struct strbuf_expand_dict_entry *e = context; | |
258 | size_t len; | |
259 | ||
260 | for (; e->placeholder && (len = strlen(e->placeholder)); e++) { | |
261 | if (!strncmp(placeholder, e->placeholder, len)) { | |
262 | if (e->value) | |
263 | strbuf_addstr(sb, e->value); | |
264 | return len; | |
265 | } | |
266 | } | |
267 | return 0; | |
268 | } | |
269 | ||
361df5df JK |
270 | void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src) |
271 | { | |
272 | int i, len = src->len; | |
273 | ||
274 | for (i = 0; i < len; i++) { | |
275 | if (src->buf[i] == '%') | |
276 | strbuf_addch(dst, '%'); | |
277 | strbuf_addch(dst, src->buf[i]); | |
278 | } | |
279 | } | |
280 | ||
917c9a71 PH |
281 | size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) |
282 | { | |
b449f4cf | 283 | size_t res; |
2fc64700 | 284 | size_t oldalloc = sb->alloc; |
b449f4cf PH |
285 | |
286 | strbuf_grow(sb, size); | |
287 | res = fread(sb->buf + sb->len, 1, size, f); | |
2fc64700 | 288 | if (res > 0) |
b449f4cf | 289 | strbuf_setlen(sb, sb->len + res); |
6651c3f7 | 290 | else if (oldalloc == 0) |
2fc64700 | 291 | strbuf_release(sb); |
b449f4cf | 292 | return res; |
d1df5743 JH |
293 | } |
294 | ||
f1696ee3 | 295 | ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) |
b449f4cf PH |
296 | { |
297 | size_t oldlen = sb->len; | |
2fc64700 | 298 | size_t oldalloc = sb->alloc; |
b449f4cf | 299 | |
f1696ee3 | 300 | strbuf_grow(sb, hint ? hint : 8192); |
b449f4cf PH |
301 | for (;;) { |
302 | ssize_t cnt; | |
303 | ||
b449f4cf PH |
304 | cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); |
305 | if (cnt < 0) { | |
2fc64700 RS |
306 | if (oldalloc == 0) |
307 | strbuf_release(sb); | |
308 | else | |
309 | strbuf_setlen(sb, oldlen); | |
b449f4cf PH |
310 | return -1; |
311 | } | |
312 | if (!cnt) | |
313 | break; | |
314 | sb->len += cnt; | |
f1696ee3 | 315 | strbuf_grow(sb, 8192); |
b449f4cf PH |
316 | } |
317 | ||
318 | sb->buf[sb->len] = '\0'; | |
319 | return sb->len - oldlen; | |
d1df5743 JH |
320 | } |
321 | ||
b11b7e13 LT |
322 | #define STRBUF_MAXLINK (2*PATH_MAX) |
323 | ||
324 | int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) | |
325 | { | |
2fc64700 RS |
326 | size_t oldalloc = sb->alloc; |
327 | ||
b11b7e13 LT |
328 | if (hint < 32) |
329 | hint = 32; | |
330 | ||
331 | while (hint < STRBUF_MAXLINK) { | |
332 | int len; | |
333 | ||
334 | strbuf_grow(sb, hint); | |
335 | len = readlink(path, sb->buf, hint); | |
336 | if (len < 0) { | |
337 | if (errno != ERANGE) | |
338 | break; | |
339 | } else if (len < hint) { | |
340 | strbuf_setlen(sb, len); | |
341 | return 0; | |
342 | } | |
343 | ||
344 | /* .. the buffer was too small - try again */ | |
345 | hint *= 2; | |
346 | } | |
2fc64700 RS |
347 | if (oldalloc == 0) |
348 | strbuf_release(sb); | |
b11b7e13 LT |
349 | return -1; |
350 | } | |
351 | ||
c7e4f0d7 | 352 | int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) |
917c9a71 | 353 | { |
d1df5743 | 354 | int ch; |
e6c019d0 | 355 | |
e6c019d0 PH |
356 | if (feof(fp)) |
357 | return EOF; | |
b449f4cf PH |
358 | |
359 | strbuf_reset(sb); | |
d1df5743 | 360 | while ((ch = fgetc(fp)) != EOF) { |
b449f4cf PH |
361 | strbuf_grow(sb, 1); |
362 | sb->buf[sb->len++] = ch; | |
c7e4f0d7 BC |
363 | if (ch == term) |
364 | break; | |
d1df5743 | 365 | } |
e6c019d0 PH |
366 | if (ch == EOF && sb->len == 0) |
367 | return EOF; | |
b449f4cf | 368 | |
b449f4cf | 369 | sb->buf[sb->len] = '\0'; |
e6c019d0 | 370 | return 0; |
d1df5743 | 371 | } |
a9390b9f | 372 | |
c7e4f0d7 BC |
373 | int strbuf_getline(struct strbuf *sb, FILE *fp, int term) |
374 | { | |
375 | if (strbuf_getwholeline(sb, fp, term)) | |
376 | return EOF; | |
377 | if (sb->buf[sb->len-1] == term) | |
378 | strbuf_setlen(sb, sb->len-1); | |
379 | return 0; | |
380 | } | |
381 | ||
5e8617f5 TR |
382 | int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term) |
383 | { | |
384 | strbuf_reset(sb); | |
385 | ||
386 | while (1) { | |
387 | char ch; | |
388 | ssize_t len = xread(fd, &ch, 1); | |
389 | if (len <= 0) | |
390 | return EOF; | |
391 | strbuf_addch(sb, ch); | |
392 | if (ch == term) | |
393 | break; | |
394 | } | |
395 | return 0; | |
396 | } | |
397 | ||
387e7e19 | 398 | int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) |
a9390b9f KH |
399 | { |
400 | int fd, len; | |
401 | ||
402 | fd = open(path, O_RDONLY); | |
403 | if (fd < 0) | |
404 | return -1; | |
387e7e19 | 405 | len = strbuf_read(sb, fd, hint); |
a9390b9f KH |
406 | close(fd); |
407 | if (len < 0) | |
408 | return -1; | |
409 | ||
410 | return len; | |
411 | } | |
895680f0 JH |
412 | |
413 | void strbuf_add_lines(struct strbuf *out, const char *prefix, | |
414 | const char *buf, size_t size) | |
415 | { | |
416 | while (size) { | |
417 | const char *next = memchr(buf, '\n', size); | |
418 | next = next ? (next + 1) : (buf + size); | |
419 | strbuf_addstr(out, prefix); | |
420 | strbuf_add(out, buf, next - buf); | |
421 | size -= next - buf; | |
422 | buf = next; | |
423 | } | |
424 | strbuf_complete_line(out); | |
425 | } | |
367d20ec | 426 | |
c505116b JK |
427 | static int is_rfc3986_reserved(char ch) |
428 | { | |
429 | switch (ch) { | |
430 | case '!': case '*': case '\'': case '(': case ')': case ';': | |
431 | case ':': case '@': case '&': case '=': case '+': case '$': | |
432 | case ',': case '/': case '?': case '#': case '[': case ']': | |
433 | return 1; | |
434 | } | |
435 | return 0; | |
436 | } | |
437 | ||
438 | static int is_rfc3986_unreserved(char ch) | |
439 | { | |
440 | return isalnum(ch) || | |
441 | ch == '-' || ch == '_' || ch == '.' || ch == '~'; | |
442 | } | |
443 | ||
ea03a8e1 JH |
444 | static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len, |
445 | int reserved) | |
c505116b JK |
446 | { |
447 | strbuf_grow(sb, len); | |
448 | while (len--) { | |
449 | char ch = *s++; | |
450 | if (is_rfc3986_unreserved(ch) || | |
451 | (!reserved && is_rfc3986_reserved(ch))) | |
452 | strbuf_addch(sb, ch); | |
453 | else | |
454 | strbuf_addf(sb, "%%%02x", ch); | |
455 | } | |
456 | } | |
457 | ||
458 | void strbuf_addstr_urlencode(struct strbuf *sb, const char *s, | |
459 | int reserved) | |
460 | { | |
461 | strbuf_add_urlencode(sb, s, strlen(s), reserved); | |
462 | } | |
9a0a30aa | 463 | |
9a0a30aa NTND |
464 | int printf_ln(const char *fmt, ...) |
465 | { | |
466 | int ret; | |
467 | va_list ap; | |
468 | va_start(ap, fmt); | |
469 | ret = vprintf(fmt, ap); | |
470 | va_end(ap); | |
471 | if (ret < 0 || putchar('\n') == EOF) | |
472 | return -1; | |
473 | return ret + 1; | |
474 | } | |
475 | ||
476 | int fprintf_ln(FILE *fp, const char *fmt, ...) | |
477 | { | |
478 | int ret; | |
479 | va_list ap; | |
480 | va_start(ap, fmt); | |
481 | ret = vfprintf(fp, fmt, ap); | |
482 | va_end(ap); | |
483 | if (ret < 0 || putc('\n', fp) == EOF) | |
484 | return -1; | |
485 | return ret + 1; | |
486 | } |