]>
Commit | Line | Data |
---|---|---|
812666c8 | 1 | #include "cache.h" |
a2fab531 | 2 | #include "refs.h" |
f6f77559 | 3 | #include "string-list.h" |
d4241f52 | 4 | #include "utf8.h" |
88c7b4c3 | 5 | #include "date.h" |
d1df5743 | 6 | |
95662315 CC |
7 | int starts_with(const char *str, const char *prefix) |
8 | { | |
9 | for (; ; str++, prefix++) | |
10 | if (!*prefix) | |
11 | return 1; | |
12 | else if (*str != *prefix) | |
13 | return 0; | |
14 | } | |
15 | ||
66b8af3e LS |
16 | int istarts_with(const char *str, const char *prefix) |
17 | { | |
18 | for (; ; str++, prefix++) | |
19 | if (!*prefix) | |
20 | return 1; | |
21 | else if (tolower(*str) != tolower(*prefix)) | |
22 | return 0; | |
23 | } | |
24 | ||
afaef55e CC |
25 | int skip_to_optional_arg_default(const char *str, const char *prefix, |
26 | const char **arg, const char *def) | |
27 | { | |
28 | const char *p; | |
29 | ||
30 | if (!skip_prefix(str, prefix, &p)) | |
31 | return 0; | |
32 | ||
33 | if (!*p) { | |
34 | if (arg) | |
35 | *arg = def; | |
36 | return 1; | |
37 | } | |
38 | ||
39 | if (*p != '=') | |
40 | return 0; | |
41 | ||
42 | if (arg) | |
43 | *arg = p + 1; | |
44 | return 1; | |
45 | } | |
46 | ||
b315c5c0 PH |
47 | /* |
48 | * Used as the default ->buf value, so that people can always assume | |
49 | * buf is non NULL and ->buf is NUL terminated even for a freshly | |
50 | * initialized strbuf. | |
51 | */ | |
52 | char strbuf_slopbuf[1]; | |
53 | ||
917c9a71 PH |
54 | void strbuf_init(struct strbuf *sb, size_t hint) |
55 | { | |
5726a6b4 ÆAB |
56 | struct strbuf blank = STRBUF_INIT; |
57 | memcpy(sb, &blank, sizeof(*sb)); | |
8c74ef1e | 58 | if (hint) |
f1696ee3 | 59 | strbuf_grow(sb, hint); |
d1df5743 JH |
60 | } |
61 | ||
917c9a71 PH |
62 | void strbuf_release(struct strbuf *sb) |
63 | { | |
b315c5c0 PH |
64 | if (sb->alloc) { |
65 | free(sb->buf); | |
66 | strbuf_init(sb, 0); | |
67 | } | |
b449f4cf PH |
68 | } |
69 | ||
b315c5c0 | 70 | char *strbuf_detach(struct strbuf *sb, size_t *sz) |
917c9a71 | 71 | { |
08ad56f3 JK |
72 | char *res; |
73 | strbuf_grow(sb, 0); | |
74 | res = sb->buf; | |
b315c5c0 PH |
75 | if (sz) |
76 | *sz = sb->len; | |
f1696ee3 | 77 | strbuf_init(sb, 0); |
b449f4cf PH |
78 | return res; |
79 | } | |
80 | ||
917c9a71 PH |
81 | void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc) |
82 | { | |
83 | strbuf_release(sb); | |
84 | sb->buf = buf; | |
85 | sb->len = len; | |
86 | sb->alloc = alloc; | |
87 | strbuf_grow(sb, 0); | |
88 | sb->buf[sb->len] = '\0'; | |
89 | } | |
90 | ||
91 | void strbuf_grow(struct strbuf *sb, size_t extra) | |
92 | { | |
8c74ef1e | 93 | int new_buf = !sb->alloc; |
1368f650 JN |
94 | if (unsigned_add_overflows(extra, 1) || |
95 | unsigned_add_overflows(sb->len, extra + 1)) | |
b449f4cf | 96 | die("you want to use way too much memory"); |
8c74ef1e | 97 | if (new_buf) |
b315c5c0 | 98 | sb->buf = NULL; |
b449f4cf | 99 | ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); |
8c74ef1e TR |
100 | if (new_buf) |
101 | sb->buf[0] = '\0'; | |
b449f4cf PH |
102 | } |
103 | ||
eacd6dc5 LS |
104 | void strbuf_trim(struct strbuf *sb) |
105 | { | |
3bb55e8a BG |
106 | strbuf_rtrim(sb); |
107 | strbuf_ltrim(sb); | |
eacd6dc5 | 108 | } |
c64a8d20 | 109 | |
f1696ee3 PH |
110 | void strbuf_rtrim(struct strbuf *sb) |
111 | { | |
112 | while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1])) | |
113 | sb->len--; | |
114 | sb->buf[sb->len] = '\0'; | |
115 | } | |
116 | ||
c64a8d20 NTND |
117 | void strbuf_trim_trailing_dir_sep(struct strbuf *sb) |
118 | { | |
119 | while (sb->len > 0 && is_dir_sep((unsigned char)sb->buf[sb->len - 1])) | |
120 | sb->len--; | |
121 | sb->buf[sb->len] = '\0'; | |
122 | } | |
123 | ||
f9573628 PK |
124 | void strbuf_trim_trailing_newline(struct strbuf *sb) |
125 | { | |
126 | if (sb->len > 0 && sb->buf[sb->len - 1] == '\n') { | |
127 | if (--sb->len > 0 && sb->buf[sb->len - 1] == '\r') | |
128 | --sb->len; | |
129 | sb->buf[sb->len] = '\0'; | |
130 | } | |
131 | } | |
132 | ||
eacd6dc5 LS |
133 | void strbuf_ltrim(struct strbuf *sb) |
134 | { | |
135 | char *b = sb->buf; | |
136 | while (sb->len > 0 && isspace(*b)) { | |
137 | b++; | |
138 | sb->len--; | |
139 | } | |
140 | memmove(sb->buf, b, sb->len); | |
141 | sb->buf[sb->len] = '\0'; | |
142 | } | |
143 | ||
d4241f52 JK |
144 | int strbuf_reencode(struct strbuf *sb, const char *from, const char *to) |
145 | { | |
146 | char *out; | |
c7d017d7 | 147 | size_t len; |
d4241f52 JK |
148 | |
149 | if (same_encoding(from, to)) | |
150 | return 0; | |
151 | ||
152 | out = reencode_string_len(sb->buf, sb->len, to, from, &len); | |
153 | if (!out) | |
154 | return -1; | |
155 | ||
156 | strbuf_attach(sb, out, len, len); | |
157 | return 0; | |
158 | } | |
159 | ||
ffb20ce1 JK |
160 | void strbuf_tolower(struct strbuf *sb) |
161 | { | |
162 | char *p = sb->buf, *end = sb->buf + sb->len; | |
163 | for (; p < end; p++) | |
164 | *p = tolower(*p); | |
165 | } | |
166 | ||
17b73dc6 MH |
167 | struct strbuf **strbuf_split_buf(const char *str, size_t slen, |
168 | int terminator, int max) | |
eacd6dc5 | 169 | { |
b8c2c1fa MH |
170 | struct strbuf **ret = NULL; |
171 | size_t nr = 0, alloc = 0; | |
eacd6dc5 LS |
172 | struct strbuf *t; |
173 | ||
1173bb33 MH |
174 | while (slen) { |
175 | int len = slen; | |
176 | if (max <= 0 || nr + 1 < max) { | |
17b73dc6 | 177 | const char *end = memchr(str, terminator, slen); |
1173bb33 MH |
178 | if (end) |
179 | len = end - str + 1; | |
180 | } | |
eacd6dc5 LS |
181 | t = xmalloc(sizeof(struct strbuf)); |
182 | strbuf_init(t, len); | |
1173bb33 | 183 | strbuf_add(t, str, len); |
b8c2c1fa MH |
184 | ALLOC_GROW(ret, nr + 2, alloc); |
185 | ret[nr++] = t; | |
1173bb33 MH |
186 | str += len; |
187 | slen -= len; | |
eacd6dc5 | 188 | } |
b8c2c1fa MH |
189 | ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */ |
190 | ret[nr] = NULL; | |
eacd6dc5 LS |
191 | return ret; |
192 | } | |
193 | ||
f6f77559 EN |
194 | void strbuf_add_separated_string_list(struct strbuf *str, |
195 | const char *sep, | |
196 | struct string_list *slist) | |
197 | { | |
198 | struct string_list_item *item; | |
199 | int sep_needed = 0; | |
200 | ||
201 | for_each_string_list_item(item, slist) { | |
202 | if (sep_needed) | |
203 | strbuf_addstr(str, sep); | |
204 | strbuf_addstr(str, item->string); | |
205 | sep_needed = 1; | |
206 | } | |
207 | } | |
208 | ||
eacd6dc5 LS |
209 | void strbuf_list_free(struct strbuf **sbs) |
210 | { | |
211 | struct strbuf **s = sbs; | |
212 | ||
f3a96807 AH |
213 | if (!s) |
214 | return; | |
eacd6dc5 LS |
215 | while (*s) { |
216 | strbuf_release(*s); | |
217 | free(*s++); | |
218 | } | |
219 | free(sbs); | |
220 | } | |
221 | ||
9b200fd6 | 222 | int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) |
45f66f64 | 223 | { |
26114c00 | 224 | size_t len = a->len < b->len ? a->len: b->len; |
8f024655 AR |
225 | int cmp = memcmp(a->buf, b->buf, len); |
226 | if (cmp) | |
227 | return cmp; | |
228 | return a->len < b->len ? -1: a->len != b->len; | |
45f66f64 PH |
229 | } |
230 | ||
917c9a71 PH |
231 | void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, |
232 | const void *data, size_t dlen) | |
233 | { | |
1368f650 | 234 | if (unsigned_add_overflows(pos, len)) |
917c9a71 PH |
235 | die("you want to use way too much memory"); |
236 | if (pos > sb->len) | |
237 | die("`pos' is too far after the end of the buffer"); | |
238 | if (pos + len > sb->len) | |
239 | die("`pos + len' is too far after the end of the buffer"); | |
240 | ||
241 | if (dlen >= len) | |
242 | strbuf_grow(sb, dlen - len); | |
243 | memmove(sb->buf + pos + dlen, | |
244 | sb->buf + pos + len, | |
245 | sb->len - pos - len); | |
246 | memcpy(sb->buf + pos, data, dlen); | |
247 | strbuf_setlen(sb, sb->len + dlen - len); | |
248 | } | |
249 | ||
c76689df PH |
250 | void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len) |
251 | { | |
252 | strbuf_splice(sb, pos, 0, data, len); | |
253 | } | |
254 | ||
5ef264db PSU |
255 | void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap) |
256 | { | |
257 | int len, len2; | |
258 | char save; | |
259 | va_list cp; | |
260 | ||
261 | if (pos > sb->len) | |
262 | die("`pos' is too far after the end of the buffer"); | |
263 | va_copy(cp, ap); | |
264 | len = vsnprintf(sb->buf + sb->len, 0, fmt, cp); | |
265 | va_end(cp); | |
266 | if (len < 0) | |
267 | BUG("your vsnprintf is broken (returned %d)", len); | |
268 | if (!len) | |
269 | return; /* nothing to do */ | |
270 | if (unsigned_add_overflows(sb->len, len)) | |
271 | die("you want to use way too much memory"); | |
272 | strbuf_grow(sb, len); | |
273 | memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos); | |
274 | /* vsnprintf() will append a NUL, overwriting one of our characters */ | |
275 | save = sb->buf[pos + len]; | |
276 | len2 = vsnprintf(sb->buf + pos, len + 1, fmt, ap); | |
277 | sb->buf[pos + len] = save; | |
278 | if (len2 != len) | |
279 | BUG("your vsnprintf is broken (returns inconsistent lengths)"); | |
280 | strbuf_setlen(sb, sb->len + len); | |
281 | } | |
282 | ||
283 | void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...) | |
284 | { | |
285 | va_list ap; | |
286 | va_start(ap, fmt); | |
287 | strbuf_vinsertf(sb, pos, fmt, ap); | |
288 | va_end(ap); | |
289 | } | |
290 | ||
c76689df PH |
291 | void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) |
292 | { | |
a8342a41 | 293 | strbuf_splice(sb, pos, len, "", 0); |
c76689df PH |
294 | } |
295 | ||
917c9a71 PH |
296 | void strbuf_add(struct strbuf *sb, const void *data, size_t len) |
297 | { | |
b449f4cf PH |
298 | strbuf_grow(sb, len); |
299 | memcpy(sb->buf + sb->len, data, len); | |
300 | strbuf_setlen(sb, sb->len + len); | |
301 | } | |
302 | ||
31471ba2 RS |
303 | void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) |
304 | { | |
305 | strbuf_grow(sb, sb2->len); | |
306 | memcpy(sb->buf + sb->len, sb2->buf, sb2->len); | |
307 | strbuf_setlen(sb, sb->len + sb2->len); | |
308 | } | |
309 | ||
e71c4a88 PSU |
310 | const char *strbuf_join_argv(struct strbuf *buf, |
311 | int argc, const char **argv, char delim) | |
312 | { | |
313 | if (!argc) | |
314 | return buf->buf; | |
315 | ||
316 | strbuf_addstr(buf, *argv); | |
317 | while (--argc) { | |
318 | strbuf_addch(buf, delim); | |
319 | strbuf_addstr(buf, *(++argv)); | |
320 | } | |
321 | ||
322 | return buf->buf; | |
323 | } | |
324 | ||
d07235a0 RS |
325 | void strbuf_addchars(struct strbuf *sb, int c, size_t n) |
326 | { | |
327 | strbuf_grow(sb, n); | |
328 | memset(sb->buf + sb->len, c, n); | |
329 | strbuf_setlen(sb, sb->len + n); | |
330 | } | |
331 | ||
917c9a71 PH |
332 | void strbuf_addf(struct strbuf *sb, const char *fmt, ...) |
333 | { | |
b449f4cf | 334 | va_list ap; |
ebeb6090 JK |
335 | va_start(ap, fmt); |
336 | strbuf_vaddf(sb, fmt, ap); | |
337 | va_end(ap); | |
338 | } | |
339 | ||
eff80a9f JH |
340 | static void add_lines(struct strbuf *out, |
341 | const char *prefix1, | |
342 | const char *prefix2, | |
343 | const char *buf, size_t size) | |
344 | { | |
345 | while (size) { | |
346 | const char *prefix; | |
347 | const char *next = memchr(buf, '\n', size); | |
348 | next = next ? (next + 1) : (buf + size); | |
349 | ||
d55aeb76 JH |
350 | prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t')) |
351 | ? prefix2 : prefix1); | |
eff80a9f JH |
352 | strbuf_addstr(out, prefix); |
353 | strbuf_add(out, buf, next - buf); | |
354 | size -= next - buf; | |
355 | buf = next; | |
356 | } | |
357 | strbuf_complete_line(out); | |
358 | } | |
359 | ||
360 | void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size) | |
361 | { | |
362 | static char prefix1[3]; | |
363 | static char prefix2[2]; | |
364 | ||
365 | if (prefix1[0] != comment_line_char) { | |
5096d490 JK |
366 | xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char); |
367 | xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char); | |
eff80a9f JH |
368 | } |
369 | add_lines(out, prefix1, prefix2, buf, size); | |
370 | } | |
371 | ||
372 | void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...) | |
373 | { | |
374 | va_list params; | |
375 | struct strbuf buf = STRBUF_INIT; | |
376 | int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n'; | |
377 | ||
378 | va_start(params, fmt); | |
379 | strbuf_vaddf(&buf, fmt, params); | |
380 | va_end(params); | |
381 | ||
382 | strbuf_add_commented_lines(sb, buf.buf, buf.len); | |
383 | if (incomplete_line) | |
384 | sb->buf[--sb->len] = '\0'; | |
385 | ||
386 | strbuf_release(&buf); | |
387 | } | |
388 | ||
ebeb6090 JK |
389 | void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap) |
390 | { | |
391 | int len; | |
392 | va_list cp; | |
b449f4cf | 393 | |
f141bd80 SP |
394 | if (!strbuf_avail(sb)) |
395 | strbuf_grow(sb, 64); | |
ebeb6090 JK |
396 | va_copy(cp, ap); |
397 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp); | |
398 | va_end(cp); | |
f141bd80 | 399 | if (len < 0) |
033abf97 | 400 | BUG("your vsnprintf is broken (returned %d)", len); |
f1696ee3 | 401 | if (len > strbuf_avail(sb)) { |
b449f4cf | 402 | strbuf_grow(sb, len); |
b449f4cf | 403 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); |
ebeb6090 | 404 | if (len > strbuf_avail(sb)) |
033abf97 | 405 | BUG("your vsnprintf is broken (insatiable)"); |
b449f4cf PH |
406 | } |
407 | strbuf_setlen(sb, sb->len + len); | |
d1df5743 JH |
408 | } |
409 | ||
c3a670de MC |
410 | void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, |
411 | void *context) | |
cde75e59 RS |
412 | { |
413 | for (;;) { | |
c3a670de MC |
414 | const char *percent; |
415 | size_t consumed; | |
cde75e59 RS |
416 | |
417 | percent = strchrnul(format, '%'); | |
418 | strbuf_add(sb, format, percent - format); | |
419 | if (!*percent) | |
420 | break; | |
421 | format = percent + 1; | |
422 | ||
0a0416a3 JK |
423 | if (*format == '%') { |
424 | strbuf_addch(sb, '%'); | |
425 | format++; | |
426 | continue; | |
427 | } | |
428 | ||
c3a670de MC |
429 | consumed = fn(sb, format, context); |
430 | if (consumed) | |
431 | format += consumed; | |
432 | else | |
cde75e59 RS |
433 | strbuf_addch(sb, '%'); |
434 | } | |
435 | } | |
436 | ||
fd2015b3 AW |
437 | size_t strbuf_expand_literal_cb(struct strbuf *sb, |
438 | const char *placeholder, | |
5cf88fd8 | 439 | void *context UNUSED) |
fd2015b3 AW |
440 | { |
441 | int ch; | |
442 | ||
443 | switch (placeholder[0]) { | |
444 | case 'n': /* newline */ | |
445 | strbuf_addch(sb, '\n'); | |
446 | return 1; | |
447 | case 'x': | |
448 | /* %x00 == NUL, %x0a == LF, etc. */ | |
449 | ch = hex2chr(placeholder + 1); | |
450 | if (ch < 0) | |
451 | return 0; | |
452 | strbuf_addch(sb, ch); | |
453 | return 3; | |
454 | } | |
455 | return 0; | |
456 | } | |
457 | ||
9b864e73 RS |
458 | size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, |
459 | void *context) | |
460 | { | |
461 | struct strbuf_expand_dict_entry *e = context; | |
462 | size_t len; | |
463 | ||
464 | for (; e->placeholder && (len = strlen(e->placeholder)); e++) { | |
465 | if (!strncmp(placeholder, e->placeholder, len)) { | |
466 | if (e->value) | |
467 | strbuf_addstr(sb, e->value); | |
468 | return len; | |
469 | } | |
470 | } | |
471 | return 0; | |
472 | } | |
473 | ||
361df5df JK |
474 | void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src) |
475 | { | |
26114c00 | 476 | size_t i, len = src->len; |
361df5df JK |
477 | |
478 | for (i = 0; i < len; i++) { | |
479 | if (src->buf[i] == '%') | |
480 | strbuf_addch(dst, '%'); | |
481 | strbuf_addch(dst, src->buf[i]); | |
482 | } | |
483 | } | |
484 | ||
b44d0118 | 485 | #define URL_UNSAFE_CHARS " <>\"%{}|\\^`:?#[]@!$&'()*+,;=" |
46fd7b39 | 486 | |
b44d0118 | 487 | void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags) |
46fd7b39 | 488 | { |
489 | size_t i, len = strlen(src); | |
490 | ||
491 | for (i = 0; i < len; i++) { | |
492 | unsigned char ch = src[i]; | |
b44d0118 | 493 | if (ch <= 0x1F || ch >= 0x7F || |
494 | (ch == '/' && (flags & STRBUF_ENCODE_SLASH)) || | |
495 | strchr(URL_UNSAFE_CHARS, ch)) | |
46fd7b39 | 496 | strbuf_addf(dst, "%%%02X", (unsigned char)ch); |
497 | else | |
498 | strbuf_addch(dst, ch); | |
499 | } | |
500 | } | |
501 | ||
917c9a71 PH |
502 | size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) |
503 | { | |
b449f4cf | 504 | size_t res; |
2fc64700 | 505 | size_t oldalloc = sb->alloc; |
b449f4cf PH |
506 | |
507 | strbuf_grow(sb, size); | |
508 | res = fread(sb->buf + sb->len, 1, size, f); | |
2fc64700 | 509 | if (res > 0) |
b449f4cf | 510 | strbuf_setlen(sb, sb->len + res); |
6651c3f7 | 511 | else if (oldalloc == 0) |
2fc64700 | 512 | strbuf_release(sb); |
b449f4cf | 513 | return res; |
d1df5743 JH |
514 | } |
515 | ||
f1696ee3 | 516 | ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) |
b449f4cf PH |
517 | { |
518 | size_t oldlen = sb->len; | |
2fc64700 | 519 | size_t oldalloc = sb->alloc; |
b449f4cf | 520 | |
f1696ee3 | 521 | strbuf_grow(sb, hint ? hint : 8192); |
b449f4cf | 522 | for (;;) { |
3ebbd00c JH |
523 | ssize_t want = sb->alloc - sb->len - 1; |
524 | ssize_t got = read_in_full(fd, sb->buf + sb->len, want); | |
b449f4cf | 525 | |
3ebbd00c | 526 | if (got < 0) { |
2fc64700 RS |
527 | if (oldalloc == 0) |
528 | strbuf_release(sb); | |
529 | else | |
530 | strbuf_setlen(sb, oldlen); | |
b449f4cf PH |
531 | return -1; |
532 | } | |
3ebbd00c JH |
533 | sb->len += got; |
534 | if (got < want) | |
b449f4cf | 535 | break; |
f1696ee3 | 536 | strbuf_grow(sb, 8192); |
b449f4cf PH |
537 | } |
538 | ||
539 | sb->buf[sb->len] = '\0'; | |
540 | return sb->len - oldlen; | |
d1df5743 JH |
541 | } |
542 | ||
b4e04fb6 SB |
543 | ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint) |
544 | { | |
c3ff8f6c | 545 | size_t oldalloc = sb->alloc; |
b4e04fb6 SB |
546 | ssize_t cnt; |
547 | ||
548 | strbuf_grow(sb, hint ? hint : 8192); | |
549 | cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); | |
550 | if (cnt > 0) | |
551 | strbuf_setlen(sb, sb->len + cnt); | |
c3ff8f6c RS |
552 | else if (oldalloc == 0) |
553 | strbuf_release(sb); | |
b4e04fb6 SB |
554 | return cnt; |
555 | } | |
556 | ||
2dac9b56 SB |
557 | ssize_t strbuf_write(struct strbuf *sb, FILE *f) |
558 | { | |
559 | return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0; | |
560 | } | |
561 | ||
b11b7e13 LT |
562 | #define STRBUF_MAXLINK (2*PATH_MAX) |
563 | ||
564 | int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) | |
565 | { | |
2fc64700 RS |
566 | size_t oldalloc = sb->alloc; |
567 | ||
b11b7e13 LT |
568 | if (hint < 32) |
569 | hint = 32; | |
570 | ||
571 | while (hint < STRBUF_MAXLINK) { | |
f3e76ed2 | 572 | ssize_t len; |
b11b7e13 LT |
573 | |
574 | strbuf_grow(sb, hint); | |
575 | len = readlink(path, sb->buf, hint); | |
576 | if (len < 0) { | |
577 | if (errno != ERANGE) | |
578 | break; | |
579 | } else if (len < hint) { | |
580 | strbuf_setlen(sb, len); | |
581 | return 0; | |
582 | } | |
583 | ||
584 | /* .. the buffer was too small - try again */ | |
585 | hint *= 2; | |
586 | } | |
2fc64700 RS |
587 | if (oldalloc == 0) |
588 | strbuf_release(sb); | |
b11b7e13 LT |
589 | return -1; |
590 | } | |
591 | ||
f22a76e9 RS |
592 | int strbuf_getcwd(struct strbuf *sb) |
593 | { | |
594 | size_t oldalloc = sb->alloc; | |
595 | size_t guessed_len = 128; | |
596 | ||
597 | for (;; guessed_len *= 2) { | |
598 | strbuf_grow(sb, guessed_len); | |
599 | if (getcwd(sb->buf, sb->alloc)) { | |
600 | strbuf_setlen(sb, strlen(sb->buf)); | |
601 | return 0; | |
602 | } | |
a54e938e RS |
603 | |
604 | /* | |
605 | * If getcwd(3) is implemented as a syscall that falls | |
606 | * back to a regular lookup using readdir(3) etc. then | |
607 | * we may be able to avoid EACCES by providing enough | |
608 | * space to the syscall as it's not necessarily bound | |
609 | * to the same restrictions as the fallback. | |
610 | */ | |
611 | if (errno == EACCES && guessed_len < PATH_MAX) | |
612 | continue; | |
613 | ||
f22a76e9 RS |
614 | if (errno != ERANGE) |
615 | break; | |
616 | } | |
617 | if (oldalloc == 0) | |
618 | strbuf_release(sb); | |
619 | else | |
620 | strbuf_reset(sb); | |
621 | return -1; | |
622 | } | |
623 | ||
0cc30e0e JK |
624 | #ifdef HAVE_GETDELIM |
625 | int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) | |
626 | { | |
627 | ssize_t r; | |
628 | ||
629 | if (feof(fp)) | |
630 | return EOF; | |
631 | ||
632 | strbuf_reset(sb); | |
633 | ||
634 | /* Translate slopbuf to NULL, as we cannot call realloc on it */ | |
635 | if (!sb->alloc) | |
636 | sb->buf = NULL; | |
642956cf | 637 | errno = 0; |
0cc30e0e JK |
638 | r = getdelim(&sb->buf, &sb->alloc, term, fp); |
639 | ||
640 | if (r > 0) { | |
641 | sb->len = r; | |
642 | return 0; | |
643 | } | |
644 | assert(r == -1); | |
645 | ||
646 | /* | |
647 | * Normally we would have called xrealloc, which will try to free | |
648 | * memory and recover. But we have no way to tell getdelim() to do so. | |
649 | * Worse, we cannot try to recover ENOMEM ourselves, because we have | |
650 | * no idea how many bytes were read by getdelim. | |
651 | * | |
652 | * Dying here is reasonable. It mirrors what xrealloc would do on | |
653 | * catastrophic memory failure. We skip the opportunity to free pack | |
654 | * memory and retry, but that's unlikely to help for a malloc small | |
655 | * enough to hold a single line of input, anyway. | |
656 | */ | |
657 | if (errno == ENOMEM) | |
658 | die("Out of memory, getdelim failed"); | |
659 | ||
b7090430 JK |
660 | /* |
661 | * Restore strbuf invariants; if getdelim left us with a NULL pointer, | |
662 | * we can just re-init, but otherwise we should make sure that our | |
663 | * length is empty, and that the result is NUL-terminated. | |
664 | */ | |
0cc30e0e JK |
665 | if (!sb->buf) |
666 | strbuf_init(sb, 0); | |
b7090430 JK |
667 | else |
668 | strbuf_reset(sb); | |
0cc30e0e JK |
669 | return EOF; |
670 | } | |
671 | #else | |
c7e4f0d7 | 672 | int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) |
917c9a71 | 673 | { |
d1df5743 | 674 | int ch; |
e6c019d0 | 675 | |
e6c019d0 PH |
676 | if (feof(fp)) |
677 | return EOF; | |
b449f4cf PH |
678 | |
679 | strbuf_reset(sb); | |
82912d1d JK |
680 | flockfile(fp); |
681 | while ((ch = getc_unlocked(fp)) != EOF) { | |
f80c153b JK |
682 | if (!strbuf_avail(sb)) |
683 | strbuf_grow(sb, 1); | |
b449f4cf | 684 | sb->buf[sb->len++] = ch; |
c7e4f0d7 BC |
685 | if (ch == term) |
686 | break; | |
d1df5743 | 687 | } |
82912d1d | 688 | funlockfile(fp); |
e6c019d0 PH |
689 | if (ch == EOF && sb->len == 0) |
690 | return EOF; | |
b449f4cf | 691 | |
b449f4cf | 692 | sb->buf[sb->len] = '\0'; |
e6c019d0 | 693 | return 0; |
d1df5743 | 694 | } |
0cc30e0e | 695 | #endif |
a9390b9f | 696 | |
bd021f39 PS |
697 | int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term) |
698 | { | |
699 | struct strbuf line = STRBUF_INIT; | |
700 | if (strbuf_getwholeline(&line, fp, term)) | |
701 | return EOF; | |
702 | strbuf_addbuf(sb, &line); | |
703 | strbuf_release(&line); | |
704 | return 0; | |
705 | } | |
706 | ||
1a0c8dfd | 707 | static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term) |
c7e4f0d7 BC |
708 | { |
709 | if (strbuf_getwholeline(sb, fp, term)) | |
710 | return EOF; | |
dce80bd1 JH |
711 | if (sb->buf[sb->len - 1] == term) |
712 | strbuf_setlen(sb, sb->len - 1); | |
c7e4f0d7 BC |
713 | return 0; |
714 | } | |
715 | ||
1a0c8dfd | 716 | int strbuf_getline(struct strbuf *sb, FILE *fp) |
c8aa9fdf JH |
717 | { |
718 | if (strbuf_getwholeline(sb, fp, '\n')) | |
719 | return EOF; | |
720 | if (sb->buf[sb->len - 1] == '\n') { | |
721 | strbuf_setlen(sb, sb->len - 1); | |
722 | if (sb->len && sb->buf[sb->len - 1] == '\r') | |
723 | strbuf_setlen(sb, sb->len - 1); | |
724 | } | |
725 | return 0; | |
726 | } | |
727 | ||
8f309aeb JH |
728 | int strbuf_getline_lf(struct strbuf *sb, FILE *fp) |
729 | { | |
1a0c8dfd | 730 | return strbuf_getdelim(sb, fp, '\n'); |
8f309aeb JH |
731 | } |
732 | ||
733 | int strbuf_getline_nul(struct strbuf *sb, FILE *fp) | |
734 | { | |
1a0c8dfd | 735 | return strbuf_getdelim(sb, fp, '\0'); |
8f309aeb JH |
736 | } |
737 | ||
5e8617f5 TR |
738 | int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term) |
739 | { | |
740 | strbuf_reset(sb); | |
741 | ||
742 | while (1) { | |
743 | char ch; | |
744 | ssize_t len = xread(fd, &ch, 1); | |
745 | if (len <= 0) | |
746 | return EOF; | |
747 | strbuf_addch(sb, ch); | |
748 | if (ch == term) | |
749 | break; | |
750 | } | |
751 | return 0; | |
752 | } | |
753 | ||
6c8afe49 | 754 | ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) |
a9390b9f | 755 | { |
6c8afe49 MH |
756 | int fd; |
757 | ssize_t len; | |
79f0ba15 | 758 | int saved_errno; |
a9390b9f KH |
759 | |
760 | fd = open(path, O_RDONLY); | |
761 | if (fd < 0) | |
762 | return -1; | |
387e7e19 | 763 | len = strbuf_read(sb, fd, hint); |
79f0ba15 | 764 | saved_errno = errno; |
a9390b9f | 765 | close(fd); |
79f0ba15 JK |
766 | if (len < 0) { |
767 | errno = saved_errno; | |
a9390b9f | 768 | return -1; |
79f0ba15 | 769 | } |
a9390b9f KH |
770 | |
771 | return len; | |
772 | } | |
895680f0 JH |
773 | |
774 | void strbuf_add_lines(struct strbuf *out, const char *prefix, | |
775 | const char *buf, size_t size) | |
776 | { | |
eff80a9f | 777 | add_lines(out, prefix, NULL, buf, size); |
895680f0 | 778 | } |
367d20ec | 779 | |
5963c036 MH |
780 | void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s) |
781 | { | |
782 | while (*s) { | |
783 | size_t len = strcspn(s, "\"<>&"); | |
784 | strbuf_add(buf, s, len); | |
785 | s += len; | |
786 | switch (*s) { | |
787 | case '"': | |
788 | strbuf_addstr(buf, """); | |
789 | break; | |
790 | case '<': | |
791 | strbuf_addstr(buf, "<"); | |
792 | break; | |
793 | case '>': | |
794 | strbuf_addstr(buf, ">"); | |
795 | break; | |
796 | case '&': | |
797 | strbuf_addstr(buf, "&"); | |
798 | break; | |
799 | case 0: | |
800 | return; | |
801 | } | |
802 | s++; | |
803 | } | |
804 | } | |
805 | ||
c2694952 | 806 | int is_rfc3986_reserved_or_unreserved(char ch) |
c505116b | 807 | { |
c2694952 MD |
808 | if (is_rfc3986_unreserved(ch)) |
809 | return 1; | |
c505116b JK |
810 | switch (ch) { |
811 | case '!': case '*': case '\'': case '(': case ')': case ';': | |
812 | case ':': case '@': case '&': case '=': case '+': case '$': | |
813 | case ',': case '/': case '?': case '#': case '[': case ']': | |
814 | return 1; | |
815 | } | |
816 | return 0; | |
817 | } | |
818 | ||
c2694952 | 819 | int is_rfc3986_unreserved(char ch) |
c505116b JK |
820 | { |
821 | return isalnum(ch) || | |
822 | ch == '-' || ch == '_' || ch == '.' || ch == '~'; | |
823 | } | |
824 | ||
ea03a8e1 | 825 | static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len, |
c2694952 | 826 | char_predicate allow_unencoded_fn) |
c505116b JK |
827 | { |
828 | strbuf_grow(sb, len); | |
829 | while (len--) { | |
830 | char ch = *s++; | |
c2694952 | 831 | if (allow_unencoded_fn(ch)) |
c505116b JK |
832 | strbuf_addch(sb, ch); |
833 | else | |
4c267f2a | 834 | strbuf_addf(sb, "%%%02x", (unsigned char)ch); |
c505116b JK |
835 | } |
836 | } | |
837 | ||
838 | void strbuf_addstr_urlencode(struct strbuf *sb, const char *s, | |
c2694952 | 839 | char_predicate allow_unencoded_fn) |
c505116b | 840 | { |
c2694952 | 841 | strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn); |
c505116b | 842 | } |
9a0a30aa | 843 | |
8f354a1f DR |
844 | static void strbuf_humanise(struct strbuf *buf, off_t bytes, |
845 | int humanise_rate) | |
079b546a AP |
846 | { |
847 | if (bytes > 1 << 30) { | |
8f354a1f DR |
848 | strbuf_addf(buf, |
849 | humanise_rate == 0 ? | |
850 | /* TRANSLATORS: IEC 80000-13:2008 gibibyte */ | |
851 | _("%u.%2.2u GiB") : | |
852 | /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */ | |
853 | _("%u.%2.2u GiB/s"), | |
7726d360 JK |
854 | (unsigned)(bytes >> 30), |
855 | (unsigned)(bytes & ((1 << 30) - 1)) / 10737419); | |
079b546a | 856 | } else if (bytes > 1 << 20) { |
7726d360 | 857 | unsigned x = bytes + 5243; /* for rounding */ |
8f354a1f DR |
858 | strbuf_addf(buf, |
859 | humanise_rate == 0 ? | |
860 | /* TRANSLATORS: IEC 80000-13:2008 mebibyte */ | |
861 | _("%u.%2.2u MiB") : | |
862 | /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */ | |
863 | _("%u.%2.2u MiB/s"), | |
079b546a AP |
864 | x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20); |
865 | } else if (bytes > 1 << 10) { | |
7726d360 | 866 | unsigned x = bytes + 5; /* for rounding */ |
8f354a1f DR |
867 | strbuf_addf(buf, |
868 | humanise_rate == 0 ? | |
869 | /* TRANSLATORS: IEC 80000-13:2008 kibibyte */ | |
870 | _("%u.%2.2u KiB") : | |
871 | /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */ | |
872 | _("%u.%2.2u KiB/s"), | |
079b546a AP |
873 | x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10); |
874 | } else { | |
8f354a1f DR |
875 | strbuf_addf(buf, |
876 | humanise_rate == 0 ? | |
877 | /* TRANSLATORS: IEC 80000-13:2008 byte */ | |
6f693252 | 878 | Q_("%u byte", "%u bytes", bytes) : |
8f354a1f | 879 | /* TRANSLATORS: IEC 80000-13:2008 byte/second */ |
6f693252 | 880 | Q_("%u byte/s", "%u bytes/s", bytes), |
8f354a1f | 881 | (unsigned)bytes); |
079b546a AP |
882 | } |
883 | } | |
884 | ||
8f354a1f DR |
885 | void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes) |
886 | { | |
887 | strbuf_humanise(buf, bytes, 0); | |
888 | } | |
889 | ||
890 | void strbuf_humanise_rate(struct strbuf *buf, off_t bytes) | |
891 | { | |
892 | strbuf_humanise(buf, bytes, 1); | |
893 | } | |
894 | ||
679eebe2 RS |
895 | void strbuf_add_absolute_path(struct strbuf *sb, const char *path) |
896 | { | |
897 | if (!*path) | |
898 | die("The empty string is not a valid path"); | |
899 | if (!is_absolute_path(path)) { | |
900 | struct stat cwd_stat, pwd_stat; | |
901 | size_t orig_len = sb->len; | |
902 | char *cwd = xgetcwd(); | |
903 | char *pwd = getenv("PWD"); | |
904 | if (pwd && strcmp(pwd, cwd) && | |
905 | !stat(cwd, &cwd_stat) && | |
906 | (cwd_stat.st_dev || cwd_stat.st_ino) && | |
907 | !stat(pwd, &pwd_stat) && | |
908 | pwd_stat.st_dev == cwd_stat.st_dev && | |
909 | pwd_stat.st_ino == cwd_stat.st_ino) | |
910 | strbuf_addstr(sb, pwd); | |
911 | else | |
912 | strbuf_addstr(sb, cwd); | |
913 | if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1])) | |
914 | strbuf_addch(sb, '/'); | |
915 | free(cwd); | |
916 | } | |
917 | strbuf_addstr(sb, path); | |
918 | } | |
919 | ||
33ad9ddd RS |
920 | void strbuf_add_real_path(struct strbuf *sb, const char *path) |
921 | { | |
922 | if (sb->len) { | |
923 | struct strbuf resolved = STRBUF_INIT; | |
924 | strbuf_realpath(&resolved, path, 1); | |
925 | strbuf_addbuf(sb, &resolved); | |
926 | strbuf_release(&resolved); | |
927 | } else | |
928 | strbuf_realpath(sb, path, 1); | |
929 | } | |
930 | ||
9a0a30aa NTND |
931 | int printf_ln(const char *fmt, ...) |
932 | { | |
933 | int ret; | |
934 | va_list ap; | |
935 | va_start(ap, fmt); | |
936 | ret = vprintf(fmt, ap); | |
937 | va_end(ap); | |
938 | if (ret < 0 || putchar('\n') == EOF) | |
939 | return -1; | |
940 | return ret + 1; | |
941 | } | |
942 | ||
943 | int fprintf_ln(FILE *fp, const char *fmt, ...) | |
944 | { | |
945 | int ret; | |
946 | va_list ap; | |
947 | va_start(ap, fmt); | |
948 | ret = vfprintf(fp, fmt, ap); | |
949 | va_end(ap); | |
950 | if (ret < 0 || putc('\n', fp) == EOF) | |
951 | return -1; | |
952 | return ret + 1; | |
953 | } | |
88d5a6f6 JK |
954 | |
955 | char *xstrdup_tolower(const char *string) | |
956 | { | |
957 | char *result; | |
958 | size_t len, i; | |
959 | ||
960 | len = strlen(string); | |
3733e694 | 961 | result = xmallocz(len); |
88d5a6f6 JK |
962 | for (i = 0; i < len; i++) |
963 | result[i] = tolower(string[i]); | |
88d5a6f6 JK |
964 | return result; |
965 | } | |
30a0ddb7 | 966 | |
13ecb463 LS |
967 | char *xstrdup_toupper(const char *string) |
968 | { | |
969 | char *result; | |
970 | size_t len, i; | |
971 | ||
972 | len = strlen(string); | |
973 | result = xmallocz(len); | |
974 | for (i = 0; i < len; i++) | |
975 | result[i] = toupper(string[i]); | |
88d5a6f6 JK |
976 | return result; |
977 | } | |
30a0ddb7 JK |
978 | |
979 | char *xstrvfmt(const char *fmt, va_list ap) | |
980 | { | |
981 | struct strbuf buf = STRBUF_INIT; | |
982 | strbuf_vaddf(&buf, fmt, ap); | |
983 | return strbuf_detach(&buf, NULL); | |
984 | } | |
985 | ||
986 | char *xstrfmt(const char *fmt, ...) | |
987 | { | |
988 | va_list ap; | |
989 | char *ret; | |
990 | ||
991 | va_start(ap, fmt); | |
992 | ret = xstrvfmt(fmt, ap); | |
993 | va_end(ap); | |
994 | ||
995 | return ret; | |
996 | } | |
aa1462cc | 997 | |
c3fbf81a | 998 | void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm, |
3b702239 | 999 | int tz_offset, int suppress_tz_name) |
aa1462cc | 1000 | { |
c3fbf81a | 1001 | struct strbuf munged_fmt = STRBUF_INIT; |
e4f031e3 | 1002 | size_t hint = 128; |
aa1462cc JK |
1003 | size_t len; |
1004 | ||
e4f031e3 JK |
1005 | if (!*fmt) |
1006 | return; | |
1007 | ||
c3fbf81a RS |
1008 | /* |
1009 | * There is no portable way to pass timezone information to | |
9b591b94 JK |
1010 | * strftime, so we handle %z and %Z here. Likewise '%s', because |
1011 | * going back to an epoch time requires knowing the zone. | |
1012 | * | |
1013 | * Note that tz_offset is in the "[-+]HHMM" decimal form; this is what | |
1014 | * we want for %z, but the computation for %s has to convert to number | |
1015 | * of seconds. | |
c3fbf81a RS |
1016 | */ |
1017 | for (;;) { | |
1018 | const char *percent = strchrnul(fmt, '%'); | |
1019 | strbuf_add(&munged_fmt, fmt, percent - fmt); | |
1020 | if (!*percent) | |
1021 | break; | |
1022 | fmt = percent + 1; | |
1023 | switch (*fmt) { | |
1024 | case '%': | |
1025 | strbuf_addstr(&munged_fmt, "%%"); | |
1026 | fmt++; | |
1027 | break; | |
9b591b94 JK |
1028 | case 's': |
1029 | strbuf_addf(&munged_fmt, "%"PRItime, | |
1030 | (timestamp_t)tm_to_time_t(tm) - | |
1031 | 3600 * (tz_offset / 100) - | |
1032 | 60 * (tz_offset % 100)); | |
1033 | fmt++; | |
1034 | break; | |
c3fbf81a RS |
1035 | case 'z': |
1036 | strbuf_addf(&munged_fmt, "%+05d", tz_offset); | |
1037 | fmt++; | |
1038 | break; | |
1039 | case 'Z': | |
3b702239 | 1040 | if (suppress_tz_name) { |
c3fbf81a RS |
1041 | fmt++; |
1042 | break; | |
1043 | } | |
1044 | /* FALLTHROUGH */ | |
1045 | default: | |
1046 | strbuf_addch(&munged_fmt, '%'); | |
1047 | } | |
1048 | } | |
1049 | fmt = munged_fmt.buf; | |
1050 | ||
e4f031e3 | 1051 | strbuf_grow(sb, hint); |
aa1462cc JK |
1052 | len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm); |
1053 | ||
1054 | if (!len) { | |
1055 | /* | |
e4f031e3 JK |
1056 | * strftime reports "0" if it could not fit the result in the buffer. |
1057 | * Unfortunately, it also reports "0" if the requested time string | |
1058 | * takes 0 bytes. So our strategy is to munge the format so that the | |
1059 | * output contains at least one character, and then drop the extra | |
1060 | * character before returning. | |
aa1462cc | 1061 | */ |
c3fbf81a | 1062 | strbuf_addch(&munged_fmt, ' '); |
e4f031e3 JK |
1063 | while (!len) { |
1064 | hint *= 2; | |
1065 | strbuf_grow(sb, hint); | |
1066 | len = strftime(sb->buf + sb->len, sb->alloc - sb->len, | |
1067 | munged_fmt.buf, tm); | |
1068 | } | |
e4f031e3 | 1069 | len--; /* drop munged space */ |
aa1462cc | 1070 | } |
c3fbf81a | 1071 | strbuf_release(&munged_fmt); |
e4f031e3 | 1072 | strbuf_setlen(sb, sb->len + len); |
aa1462cc | 1073 | } |
af49c6d0 | 1074 | |
155b517d JT |
1075 | void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, |
1076 | const struct object_id *oid, int abbrev_len) | |
af49c6d0 JK |
1077 | { |
1078 | int r; | |
4b048c91 | 1079 | strbuf_grow(sb, GIT_MAX_HEXSZ + 1); |
155b517d | 1080 | r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len); |
af49c6d0 JK |
1081 | strbuf_setlen(sb, sb->len + r); |
1082 | } | |
1ad7c0f6 | 1083 | |
155b517d JT |
1084 | void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, |
1085 | int abbrev_len) | |
1086 | { | |
1087 | strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len); | |
1088 | } | |
1089 | ||
63af4a84 TK |
1090 | /* |
1091 | * Returns the length of a line, without trailing spaces. | |
1092 | * | |
1093 | * If the line ends with newline, it will be removed too. | |
1094 | */ | |
1095 | static size_t cleanup(char *line, size_t len) | |
1096 | { | |
1097 | while (len) { | |
1098 | unsigned char c = line[len - 1]; | |
1099 | if (!isspace(c)) | |
1100 | break; | |
1101 | len--; | |
1102 | } | |
1103 | ||
1104 | return len; | |
1105 | } | |
1106 | ||
1107 | /* | |
1108 | * Remove empty lines from the beginning and end | |
1109 | * and also trailing spaces from every line. | |
1110 | * | |
1111 | * Turn multiple consecutive empty lines between paragraphs | |
1112 | * into just one empty line. | |
1113 | * | |
1114 | * If the input has only empty lines and spaces, | |
1115 | * no output will be produced. | |
1116 | * | |
1117 | * If last line does not have a newline at the end, one is added. | |
1118 | * | |
1119 | * Enable skip_comments to skip every line starting with comment | |
1120 | * character. | |
1121 | */ | |
1122 | void strbuf_stripspace(struct strbuf *sb, int skip_comments) | |
1123 | { | |
26114c00 | 1124 | size_t empties = 0; |
63af4a84 TK |
1125 | size_t i, j, len, newlen; |
1126 | char *eol; | |
1127 | ||
1128 | /* We may have to add a newline. */ | |
1129 | strbuf_grow(sb, 1); | |
1130 | ||
1131 | for (i = j = 0; i < sb->len; i += len, j += newlen) { | |
1132 | eol = memchr(sb->buf + i, '\n', sb->len - i); | |
1133 | len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; | |
1134 | ||
1135 | if (skip_comments && len && sb->buf[i] == comment_line_char) { | |
1136 | newlen = 0; | |
1137 | continue; | |
1138 | } | |
1139 | newlen = cleanup(sb->buf + i, len); | |
1140 | ||
1141 | /* Not just an empty line? */ | |
1142 | if (newlen) { | |
1143 | if (empties > 0 && j > 0) | |
1144 | sb->buf[j++] = '\n'; | |
1145 | empties = 0; | |
1146 | memmove(sb->buf + j, sb->buf + i, newlen); | |
1147 | sb->buf[newlen + j++] = '\n'; | |
1148 | } else { | |
1149 | empties++; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | strbuf_setlen(sb, j); | |
1154 | } | |
670c359d JK |
1155 | |
1156 | int strbuf_normalize_path(struct strbuf *src) | |
1157 | { | |
1158 | struct strbuf dst = STRBUF_INIT; | |
1159 | ||
1160 | strbuf_grow(&dst, src->len); | |
1161 | if (normalize_path_copy(dst.buf, src->buf) < 0) { | |
1162 | strbuf_release(&dst); | |
1163 | return -1; | |
1164 | } | |
1165 | ||
1166 | /* | |
1167 | * normalize_path does not tell us the new length, so we have to | |
1168 | * compute it by looking for the new NUL it placed | |
1169 | */ | |
1170 | strbuf_setlen(&dst, strlen(dst.buf)); | |
1171 | strbuf_swap(src, &dst); | |
1172 | strbuf_release(&dst); | |
1173 | return 0; | |
1174 | } | |
b38dd9e7 JS |
1175 | |
1176 | int strbuf_edit_interactively(struct strbuf *buffer, const char *path, | |
1177 | const char *const *env) | |
1178 | { | |
1179 | char *path2 = NULL; | |
1180 | int fd, res = 0; | |
1181 | ||
1182 | if (!is_absolute_path(path)) | |
1183 | path = path2 = xstrdup(git_path("%s", path)); | |
1184 | ||
1185 | fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
1186 | if (fd < 0) | |
1187 | res = error_errno(_("could not open '%s' for writing"), path); | |
1188 | else if (write_in_full(fd, buffer->buf, buffer->len) < 0) { | |
1189 | res = error_errno(_("could not write to '%s'"), path); | |
1190 | close(fd); | |
1191 | } else if (close(fd) < 0) | |
1192 | res = error_errno(_("could not close '%s'"), path); | |
1193 | else { | |
1194 | strbuf_reset(buffer); | |
1195 | if (launch_editor(path, buffer, env) < 0) | |
1196 | res = error_errno(_("could not edit '%s'"), path); | |
1197 | unlink(path); | |
1198 | } | |
1199 | ||
1200 | free(path2); | |
1201 | return res; | |
1202 | } |