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