Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | a2fab53 | 2009-03-21 14:35:51 -0700 | [diff] [blame] | 2 | #include "refs.h" |
Elijah Newren | f6f7755 | 2018-04-19 10:58:08 -0700 | [diff] [blame] | 3 | #include "string-list.h" |
Jeff King | d4241f5 | 2014-05-22 05:30:14 -0400 | [diff] [blame] | 4 | #include "utf8.h" |
Ævar Arnfjörð Bjarmason | 88c7b4c | 2022-02-16 09:14:02 +0100 | [diff] [blame] | 5 | #include "date.h" |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 6 | |
Christian Couder | 9566231 | 2013-12-01 08:49:16 +0100 | [diff] [blame] | 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 | |
Lars Schneider | 66b8af3 | 2018-03-09 18:35:29 +0100 | [diff] [blame] | 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 | |
Christian Couder | afaef55 | 2017-12-09 21:40:07 +0100 | [diff] [blame] | 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 | |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 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 | |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 54 | void strbuf_init(struct strbuf *sb, size_t hint) |
| 55 | { |
Ævar Arnfjörð Bjarmason | 5726a6b | 2021-07-01 12:51:26 +0200 | [diff] [blame] | 56 | struct strbuf blank = STRBUF_INIT; |
| 57 | memcpy(sb, &blank, sizeof(*sb)); |
Thomas Rast | 8c74ef1 | 2011-08-29 23:16:12 +0200 | [diff] [blame] | 58 | if (hint) |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 59 | strbuf_grow(sb, hint); |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 62 | void strbuf_release(struct strbuf *sb) |
| 63 | { |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 64 | if (sb->alloc) { |
| 65 | free(sb->buf); |
| 66 | strbuf_init(sb, 0); |
| 67 | } |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 70 | char *strbuf_detach(struct strbuf *sb, size_t *sz) |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 71 | { |
Jeff King | 08ad56f | 2012-10-18 06:00:12 -0400 | [diff] [blame] | 72 | char *res; |
| 73 | strbuf_grow(sb, 0); |
| 74 | res = sb->buf; |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 75 | if (sz) |
| 76 | *sz = sb->len; |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 77 | strbuf_init(sb, 0); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 78 | return res; |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 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 | { |
Thomas Rast | 8c74ef1 | 2011-08-29 23:16:12 +0200 | [diff] [blame] | 93 | int new_buf = !sb->alloc; |
Jonathan Nieder | 1368f65 | 2010-10-10 21:59:26 -0500 | [diff] [blame] | 94 | if (unsigned_add_overflows(extra, 1) || |
| 95 | unsigned_add_overflows(sb->len, extra + 1)) |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 96 | die("you want to use way too much memory"); |
Thomas Rast | 8c74ef1 | 2011-08-29 23:16:12 +0200 | [diff] [blame] | 97 | if (new_buf) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 98 | sb->buf = NULL; |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 99 | ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); |
Thomas Rast | 8c74ef1 | 2011-08-29 23:16:12 +0200 | [diff] [blame] | 100 | if (new_buf) |
| 101 | sb->buf[0] = '\0'; |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 104 | void strbuf_trim(struct strbuf *sb) |
| 105 | { |
Brian Gesiak | 3bb55e8 | 2014-04-30 17:58:06 +0900 | [diff] [blame] | 106 | strbuf_rtrim(sb); |
| 107 | strbuf_ltrim(sb); |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 108 | } |
Nguyễn Thái Ngọc Duy | c64a8d2 | 2018-02-12 16:49:37 +0700 | [diff] [blame] | 109 | |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 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 | |
Nguyễn Thái Ngọc Duy | c64a8d2 | 2018-02-12 16:49:37 +0700 | [diff] [blame] | 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 | |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 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 | |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 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 | |
Jeff King | d4241f5 | 2014-05-22 05:30:14 -0400 | [diff] [blame] | 144 | int strbuf_reencode(struct strbuf *sb, const char *from, const char *to) |
| 145 | { |
| 146 | char *out; |
Jeff King | c7d017d | 2018-07-24 06:50:33 -0400 | [diff] [blame] | 147 | size_t len; |
Jeff King | d4241f5 | 2014-05-22 05:30:14 -0400 | [diff] [blame] | 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 | |
Jeff King | ffb20ce | 2014-05-23 16:03:47 -0400 | [diff] [blame] | 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 | |
Michael Haggerty | 17b73dc | 2012-11-04 07:46:53 +0100 | [diff] [blame] | 167 | struct strbuf **strbuf_split_buf(const char *str, size_t slen, |
| 168 | int terminator, int max) |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 169 | { |
Michael Haggerty | b8c2c1f | 2012-11-04 07:46:51 +0100 | [diff] [blame] | 170 | struct strbuf **ret = NULL; |
| 171 | size_t nr = 0, alloc = 0; |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 172 | struct strbuf *t; |
| 173 | |
Michael Haggerty | 1173bb3 | 2012-11-04 07:46:52 +0100 | [diff] [blame] | 174 | while (slen) { |
| 175 | int len = slen; |
| 176 | if (max <= 0 || nr + 1 < max) { |
Michael Haggerty | 17b73dc | 2012-11-04 07:46:53 +0100 | [diff] [blame] | 177 | const char *end = memchr(str, terminator, slen); |
Michael Haggerty | 1173bb3 | 2012-11-04 07:46:52 +0100 | [diff] [blame] | 178 | if (end) |
| 179 | len = end - str + 1; |
| 180 | } |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 181 | t = xmalloc(sizeof(struct strbuf)); |
| 182 | strbuf_init(t, len); |
Michael Haggerty | 1173bb3 | 2012-11-04 07:46:52 +0100 | [diff] [blame] | 183 | strbuf_add(t, str, len); |
Michael Haggerty | b8c2c1f | 2012-11-04 07:46:51 +0100 | [diff] [blame] | 184 | ALLOC_GROW(ret, nr + 2, alloc); |
| 185 | ret[nr++] = t; |
Michael Haggerty | 1173bb3 | 2012-11-04 07:46:52 +0100 | [diff] [blame] | 186 | str += len; |
| 187 | slen -= len; |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 188 | } |
Michael Haggerty | b8c2c1f | 2012-11-04 07:46:51 +0100 | [diff] [blame] | 189 | ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */ |
| 190 | ret[nr] = NULL; |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 191 | return ret; |
| 192 | } |
| 193 | |
Elijah Newren | f6f7755 | 2018-04-19 10:58:08 -0700 | [diff] [blame] | 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 | |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 209 | void strbuf_list_free(struct strbuf **sbs) |
| 210 | { |
| 211 | struct strbuf **s = sbs; |
| 212 | |
Andrzej Hunt | f3a9680 | 2021-04-25 14:16:16 +0000 | [diff] [blame] | 213 | if (!s) |
| 214 | return; |
Lukas Sandström | eacd6dc | 2008-07-13 20:29:18 +0200 | [diff] [blame] | 215 | while (*s) { |
| 216 | strbuf_release(*s); |
| 217 | free(*s++); |
| 218 | } |
| 219 | free(sbs); |
| 220 | } |
| 221 | |
Lukas Sandström | 9b200fd | 2008-07-13 20:28:24 +0200 | [diff] [blame] | 222 | int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) |
Pierre Habouzit | 45f66f6 | 2007-09-24 11:25:03 +0200 | [diff] [blame] | 223 | { |
Jeff King | 26114c0 | 2018-07-24 06:51:08 -0400 | [diff] [blame] | 224 | size_t len = a->len < b->len ? a->len: b->len; |
Alex Riesen | 8f02465 | 2009-03-19 23:27:32 +0100 | [diff] [blame] | 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; |
Pierre Habouzit | 45f66f6 | 2007-09-24 11:25:03 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 231 | void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, |
| 232 | const void *data, size_t dlen) |
| 233 | { |
Jonathan Nieder | 1368f65 | 2010-10-10 21:59:26 -0500 | [diff] [blame] | 234 | if (unsigned_add_overflows(pos, len)) |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 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 | |
Pierre Habouzit | c76689d | 2007-09-20 00:42:12 +0200 | [diff] [blame] | 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 | |
Paul-Sebastian Ungureanu | 5ef264d | 2019-02-25 23:16:07 +0000 | [diff] [blame] | 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 | |
Pierre Habouzit | c76689d | 2007-09-20 00:42:12 +0200 | [diff] [blame] | 291 | void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) |
| 292 | { |
René Scharfe | a8342a4 | 2016-09-13 18:40:22 +0200 | [diff] [blame] | 293 | strbuf_splice(sb, pos, len, "", 0); |
Pierre Habouzit | c76689d | 2007-09-20 00:42:12 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 296 | void strbuf_add(struct strbuf *sb, const void *data, size_t len) |
| 297 | { |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 298 | strbuf_grow(sb, len); |
| 299 | memcpy(sb->buf + sb->len, data, len); |
| 300 | strbuf_setlen(sb, sb->len + len); |
| 301 | } |
| 302 | |
René Scharfe | 31471ba | 2016-07-21 18:46:44 +0200 | [diff] [blame] | 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 | |
Paul-Sebastian Ungureanu | e71c4a8 | 2019-02-25 23:16:06 +0000 | [diff] [blame] | 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 | |
René Scharfe | d07235a | 2014-09-07 09:03:32 +0200 | [diff] [blame] | 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 | |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 332 | void strbuf_addf(struct strbuf *sb, const char *fmt, ...) |
| 333 | { |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 334 | va_list ap; |
Jeff King | ebeb609 | 2011-02-25 23:08:53 -0600 | [diff] [blame] | 335 | va_start(ap, fmt); |
| 336 | strbuf_vaddf(sb, fmt, ap); |
| 337 | va_end(ap); |
| 338 | } |
| 339 | |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 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 | |
Junio C Hamano | d55aeb7 | 2014-10-27 14:13:15 -0700 | [diff] [blame] | 350 | prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t')) |
| 351 | ? prefix2 : prefix1); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 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) { |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 366 | xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char); |
| 367 | xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 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 | |
Jeff King | ebeb609 | 2011-02-25 23:08:53 -0600 | [diff] [blame] | 389 | void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap) |
| 390 | { |
| 391 | int len; |
| 392 | va_list cp; |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 393 | |
Shawn O. Pearce | f141bd8 | 2007-11-13 03:22:44 -0500 | [diff] [blame] | 394 | if (!strbuf_avail(sb)) |
| 395 | strbuf_grow(sb, 64); |
Jeff King | ebeb609 | 2011-02-25 23:08:53 -0600 | [diff] [blame] | 396 | va_copy(cp, ap); |
| 397 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp); |
| 398 | va_end(cp); |
Shawn O. Pearce | f141bd8 | 2007-11-13 03:22:44 -0500 | [diff] [blame] | 399 | if (len < 0) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 400 | BUG("your vsnprintf is broken (returned %d)", len); |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 401 | if (len > strbuf_avail(sb)) { |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 402 | strbuf_grow(sb, len); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 403 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); |
Jeff King | ebeb609 | 2011-02-25 23:08:53 -0600 | [diff] [blame] | 404 | if (len > strbuf_avail(sb)) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 405 | BUG("your vsnprintf is broken (insatiable)"); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 406 | } |
| 407 | strbuf_setlen(sb, sb->len + len); |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 410 | void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, |
| 411 | void *context) |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 412 | { |
| 413 | for (;;) { |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 414 | const char *percent; |
| 415 | size_t consumed; |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 416 | |
| 417 | percent = strchrnul(format, '%'); |
| 418 | strbuf_add(sb, format, percent - format); |
| 419 | if (!*percent) |
| 420 | break; |
| 421 | format = percent + 1; |
| 422 | |
Jeff King | 0a0416a | 2010-01-13 12:35:31 -0500 | [diff] [blame] | 423 | if (*format == '%') { |
| 424 | strbuf_addch(sb, '%'); |
| 425 | format++; |
| 426 | continue; |
| 427 | } |
| 428 | |
Marco Costalba | c3a670d | 2008-02-09 15:40:19 +0100 | [diff] [blame] | 429 | consumed = fn(sb, format, context); |
| 430 | if (consumed) |
| 431 | format += consumed; |
| 432 | else |
René Scharfe | cde75e5 | 2007-11-09 01:49:42 +0100 | [diff] [blame] | 433 | strbuf_addch(sb, '%'); |
| 434 | } |
| 435 | } |
| 436 | |
Anders Waldenborg | fd2015b | 2019-01-28 22:33:36 +0100 | [diff] [blame] | 437 | size_t strbuf_expand_literal_cb(struct strbuf *sb, |
| 438 | const char *placeholder, |
| 439 | void *context) |
| 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 | |
René Scharfe | 9b864e7 | 2008-11-23 00:09:30 +0100 | [diff] [blame] | 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 | |
Jeff King | 361df5d | 2010-01-13 12:36:42 -0500 | [diff] [blame] | 474 | void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src) |
| 475 | { |
Jeff King | 26114c0 | 2018-07-24 06:51:08 -0400 | [diff] [blame] | 476 | size_t i, len = src->len; |
Jeff King | 361df5d | 2010-01-13 12:36:42 -0500 | [diff] [blame] | 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 | |
brian m. carlson | b44d011 | 2020-04-27 01:18:08 +0000 | [diff] [blame] | 485 | #define URL_UNSAFE_CHARS " <>\"%{}|\\^`:?#[]@!$&'()*+,;=" |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 486 | |
brian m. carlson | b44d011 | 2020-04-27 01:18:08 +0000 | [diff] [blame] | 487 | void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags) |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 488 | { |
| 489 | size_t i, len = strlen(src); |
| 490 | |
| 491 | for (i = 0; i < len; i++) { |
| 492 | unsigned char ch = src[i]; |
brian m. carlson | b44d011 | 2020-04-27 01:18:08 +0000 | [diff] [blame] | 493 | if (ch <= 0x1F || ch >= 0x7F || |
| 494 | (ch == '/' && (flags & STRBUF_ENCODE_SLASH)) || |
| 495 | strchr(URL_UNSAFE_CHARS, ch)) |
brian m. carlson | 46fd7b3 | 2020-02-20 02:24:13 +0000 | [diff] [blame] | 496 | strbuf_addf(dst, "%%%02X", (unsigned char)ch); |
| 497 | else |
| 498 | strbuf_addch(dst, ch); |
| 499 | } |
| 500 | } |
| 501 | |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 502 | size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) |
| 503 | { |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 504 | size_t res; |
René Scharfe | 2fc6470 | 2009-01-06 21:41:14 +0100 | [diff] [blame] | 505 | size_t oldalloc = sb->alloc; |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 506 | |
| 507 | strbuf_grow(sb, size); |
| 508 | res = fread(sb->buf + sb->len, 1, size, f); |
René Scharfe | 2fc6470 | 2009-01-06 21:41:14 +0100 | [diff] [blame] | 509 | if (res > 0) |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 510 | strbuf_setlen(sb, sb->len + res); |
Roel Kluin | 6651c3f | 2009-06-22 18:42:33 +0200 | [diff] [blame] | 511 | else if (oldalloc == 0) |
René Scharfe | 2fc6470 | 2009-01-06 21:41:14 +0100 | [diff] [blame] | 512 | strbuf_release(sb); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 513 | return res; |
| 514 | } |
| 515 | |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 516 | ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 517 | { |
| 518 | size_t oldlen = sb->len; |
René Scharfe | 2fc6470 | 2009-01-06 21:41:14 +0100 | [diff] [blame] | 519 | size_t oldalloc = sb->alloc; |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 520 | |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 521 | strbuf_grow(sb, hint ? hint : 8192); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 522 | for (;;) { |
Jim Hill | 3ebbd00 | 2015-05-31 11:16:45 -0700 | [diff] [blame] | 523 | ssize_t want = sb->alloc - sb->len - 1; |
| 524 | ssize_t got = read_in_full(fd, sb->buf + sb->len, want); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 525 | |
Jim Hill | 3ebbd00 | 2015-05-31 11:16:45 -0700 | [diff] [blame] | 526 | if (got < 0) { |
René Scharfe | 2fc6470 | 2009-01-06 21:41:14 +0100 | [diff] [blame] | 527 | if (oldalloc == 0) |
| 528 | strbuf_release(sb); |
| 529 | else |
| 530 | strbuf_setlen(sb, oldlen); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 531 | return -1; |
| 532 | } |
Jim Hill | 3ebbd00 | 2015-05-31 11:16:45 -0700 | [diff] [blame] | 533 | sb->len += got; |
| 534 | if (got < want) |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 535 | break; |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 536 | strbuf_grow(sb, 8192); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | sb->buf[sb->len] = '\0'; |
| 540 | return sb->len - oldlen; |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Stefan Beller | b4e04fb | 2015-12-15 16:04:08 -0800 | [diff] [blame] | 543 | ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint) |
| 544 | { |
René Scharfe | c3ff8f6 | 2017-12-07 21:51:26 +0100 | [diff] [blame] | 545 | size_t oldalloc = sb->alloc; |
Stefan Beller | b4e04fb | 2015-12-15 16:04:08 -0800 | [diff] [blame] | 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); |
René Scharfe | c3ff8f6 | 2017-12-07 21:51:26 +0100 | [diff] [blame] | 552 | else if (oldalloc == 0) |
| 553 | strbuf_release(sb); |
Stefan Beller | b4e04fb | 2015-12-15 16:04:08 -0800 | [diff] [blame] | 554 | return cnt; |
| 555 | } |
| 556 | |
Stefan Beller | 2dac9b5 | 2016-02-29 18:07:15 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | b11b7e1 | 2008-12-17 09:36:40 -0800 | [diff] [blame] | 562 | #define STRBUF_MAXLINK (2*PATH_MAX) |
| 563 | |
| 564 | int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) |
| 565 | { |
René Scharfe | 2fc6470 | 2009-01-06 21:41:14 +0100 | [diff] [blame] | 566 | size_t oldalloc = sb->alloc; |
| 567 | |
Linus Torvalds | b11b7e1 | 2008-12-17 09:36:40 -0800 | [diff] [blame] | 568 | if (hint < 32) |
| 569 | hint = 32; |
| 570 | |
| 571 | while (hint < STRBUF_MAXLINK) { |
Jeff King | f3e76ed | 2018-07-24 06:51:25 -0400 | [diff] [blame] | 572 | ssize_t len; |
Linus Torvalds | b11b7e1 | 2008-12-17 09:36:40 -0800 | [diff] [blame] | 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 | } |
René Scharfe | 2fc6470 | 2009-01-06 21:41:14 +0100 | [diff] [blame] | 587 | if (oldalloc == 0) |
| 588 | strbuf_release(sb); |
Linus Torvalds | b11b7e1 | 2008-12-17 09:36:40 -0800 | [diff] [blame] | 589 | return -1; |
| 590 | } |
| 591 | |
René Scharfe | f22a76e | 2014-07-28 20:24:29 +0200 | [diff] [blame] | 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 | } |
René Scharfe | a54e938 | 2017-03-26 15:43:50 +0200 | [diff] [blame] | 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 | |
René Scharfe | f22a76e | 2014-07-28 20:24:29 +0200 | [diff] [blame] | 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 | |
Jeff King | 0cc30e0 | 2015-04-16 05:01:38 -0400 | [diff] [blame] | 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; |
René Scharfe | 642956c | 2017-08-10 22:56:40 +0200 | [diff] [blame] | 637 | errno = 0; |
Jeff King | 0cc30e0 | 2015-04-16 05:01:38 -0400 | [diff] [blame] | 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 | |
Jeff King | b709043 | 2016-03-05 13:43:30 -0500 | [diff] [blame] | 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 | */ |
Jeff King | 0cc30e0 | 2015-04-16 05:01:38 -0400 | [diff] [blame] | 665 | if (!sb->buf) |
| 666 | strbuf_init(sb, 0); |
Jeff King | b709043 | 2016-03-05 13:43:30 -0500 | [diff] [blame] | 667 | else |
| 668 | strbuf_reset(sb); |
Jeff King | 0cc30e0 | 2015-04-16 05:01:38 -0400 | [diff] [blame] | 669 | return EOF; |
| 670 | } |
| 671 | #else |
Brandon Casey | c7e4f0d | 2009-08-04 22:31:56 -0500 | [diff] [blame] | 672 | int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) |
Pierre Habouzit | 917c9a7 | 2007-09-15 15:56:50 +0200 | [diff] [blame] | 673 | { |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 674 | int ch; |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 675 | |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 676 | if (feof(fp)) |
| 677 | return EOF; |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 678 | |
| 679 | strbuf_reset(sb); |
Jeff King | 82912d1 | 2015-04-16 04:49:06 -0400 | [diff] [blame] | 680 | flockfile(fp); |
| 681 | while ((ch = getc_unlocked(fp)) != EOF) { |
Jeff King | f80c153 | 2015-04-16 04:58:54 -0400 | [diff] [blame] | 682 | if (!strbuf_avail(sb)) |
| 683 | strbuf_grow(sb, 1); |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 684 | sb->buf[sb->len++] = ch; |
Brandon Casey | c7e4f0d | 2009-08-04 22:31:56 -0500 | [diff] [blame] | 685 | if (ch == term) |
| 686 | break; |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 687 | } |
Jeff King | 82912d1 | 2015-04-16 04:49:06 -0400 | [diff] [blame] | 688 | funlockfile(fp); |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 689 | if (ch == EOF && sb->len == 0) |
| 690 | return EOF; |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 691 | |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 692 | sb->buf[sb->len] = '\0'; |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 693 | return 0; |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 694 | } |
Jeff King | 0cc30e0 | 2015-04-16 05:01:38 -0400 | [diff] [blame] | 695 | #endif |
Kristian Høgsberg | a9390b9 | 2007-09-17 20:06:46 -0400 | [diff] [blame] | 696 | |
Patrick Steinhardt | bd021f3 | 2020-03-30 15:46:27 +0200 | [diff] [blame] | 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 | |
Junio C Hamano | 1a0c8df | 2016-01-13 18:32:23 -0800 | [diff] [blame] | 707 | static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term) |
Brandon Casey | c7e4f0d | 2009-08-04 22:31:56 -0500 | [diff] [blame] | 708 | { |
| 709 | if (strbuf_getwholeline(sb, fp, term)) |
| 710 | return EOF; |
Junio C Hamano | dce80bd | 2016-01-13 15:10:45 -0800 | [diff] [blame] | 711 | if (sb->buf[sb->len - 1] == term) |
| 712 | strbuf_setlen(sb, sb->len - 1); |
Brandon Casey | c7e4f0d | 2009-08-04 22:31:56 -0500 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
Junio C Hamano | 1a0c8df | 2016-01-13 18:32:23 -0800 | [diff] [blame] | 716 | int strbuf_getline(struct strbuf *sb, FILE *fp) |
Junio C Hamano | c8aa9fd | 2015-10-28 13:17:29 -0700 | [diff] [blame] | 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 | |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 728 | int strbuf_getline_lf(struct strbuf *sb, FILE *fp) |
| 729 | { |
Junio C Hamano | 1a0c8df | 2016-01-13 18:32:23 -0800 | [diff] [blame] | 730 | return strbuf_getdelim(sb, fp, '\n'); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | int strbuf_getline_nul(struct strbuf *sb, FILE *fp) |
| 734 | { |
Junio C Hamano | 1a0c8df | 2016-01-13 18:32:23 -0800 | [diff] [blame] | 735 | return strbuf_getdelim(sb, fp, '\0'); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Thomas Rast | 5e8617f | 2012-02-22 20:34:22 +0100 | [diff] [blame] | 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 | |
Michael Haggerty | 6c8afe4 | 2015-07-03 15:59:32 +0200 | [diff] [blame] | 754 | ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) |
Kristian Høgsberg | a9390b9 | 2007-09-17 20:06:46 -0400 | [diff] [blame] | 755 | { |
Michael Haggerty | 6c8afe4 | 2015-07-03 15:59:32 +0200 | [diff] [blame] | 756 | int fd; |
| 757 | ssize_t len; |
Jeff King | 79f0ba1 | 2018-02-23 02:00:54 -0500 | [diff] [blame] | 758 | int saved_errno; |
Kristian Høgsberg | a9390b9 | 2007-09-17 20:06:46 -0400 | [diff] [blame] | 759 | |
| 760 | fd = open(path, O_RDONLY); |
| 761 | if (fd < 0) |
| 762 | return -1; |
Pierre Habouzit | 387e7e1 | 2007-09-27 15:25:55 +0200 | [diff] [blame] | 763 | len = strbuf_read(sb, fd, hint); |
Jeff King | 79f0ba1 | 2018-02-23 02:00:54 -0500 | [diff] [blame] | 764 | saved_errno = errno; |
Kristian Høgsberg | a9390b9 | 2007-09-17 20:06:46 -0400 | [diff] [blame] | 765 | close(fd); |
Jeff King | 79f0ba1 | 2018-02-23 02:00:54 -0500 | [diff] [blame] | 766 | if (len < 0) { |
| 767 | errno = saved_errno; |
Kristian Høgsberg | a9390b9 | 2007-09-17 20:06:46 -0400 | [diff] [blame] | 768 | return -1; |
Jeff King | 79f0ba1 | 2018-02-23 02:00:54 -0500 | [diff] [blame] | 769 | } |
Kristian Høgsberg | a9390b9 | 2007-09-17 20:06:46 -0400 | [diff] [blame] | 770 | |
| 771 | return len; |
| 772 | } |
Junio C Hamano | 895680f | 2011-11-04 21:06:30 -0700 | [diff] [blame] | 773 | |
| 774 | void strbuf_add_lines(struct strbuf *out, const char *prefix, |
| 775 | const char *buf, size_t size) |
| 776 | { |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 777 | add_lines(out, prefix, NULL, buf, size); |
Junio C Hamano | 895680f | 2011-11-04 21:06:30 -0700 | [diff] [blame] | 778 | } |
Junio C Hamano | 367d20e | 2011-12-19 16:05:16 -0800 | [diff] [blame] | 779 | |
Michael Haggerty | 5963c03 | 2012-11-25 12:08:34 +0100 | [diff] [blame] | 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 | |
Matthew DeVore | c269495 | 2019-06-27 15:54:11 -0700 | [diff] [blame] | 806 | int is_rfc3986_reserved_or_unreserved(char ch) |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 807 | { |
Matthew DeVore | c269495 | 2019-06-27 15:54:11 -0700 | [diff] [blame] | 808 | if (is_rfc3986_unreserved(ch)) |
| 809 | return 1; |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 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 | |
Matthew DeVore | c269495 | 2019-06-27 15:54:11 -0700 | [diff] [blame] | 819 | int is_rfc3986_unreserved(char ch) |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 820 | { |
| 821 | return isalnum(ch) || |
| 822 | ch == '-' || ch == '_' || ch == '.' || ch == '~'; |
| 823 | } |
| 824 | |
Junio C Hamano | ea03a8e | 2012-09-15 22:43:15 -0700 | [diff] [blame] | 825 | static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len, |
Matthew DeVore | c269495 | 2019-06-27 15:54:11 -0700 | [diff] [blame] | 826 | char_predicate allow_unencoded_fn) |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 827 | { |
| 828 | strbuf_grow(sb, len); |
| 829 | while (len--) { |
| 830 | char ch = *s++; |
Matthew DeVore | c269495 | 2019-06-27 15:54:11 -0700 | [diff] [blame] | 831 | if (allow_unencoded_fn(ch)) |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 832 | strbuf_addch(sb, ch); |
| 833 | else |
Julien Dusser | 4c267f2 | 2017-12-22 18:24:37 +0100 | [diff] [blame] | 834 | strbuf_addf(sb, "%%%02x", (unsigned char)ch); |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | void strbuf_addstr_urlencode(struct strbuf *sb, const char *s, |
Matthew DeVore | c269495 | 2019-06-27 15:54:11 -0700 | [diff] [blame] | 839 | char_predicate allow_unencoded_fn) |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 840 | { |
Matthew DeVore | c269495 | 2019-06-27 15:54:11 -0700 | [diff] [blame] | 841 | strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn); |
Jeff King | c505116 | 2011-12-10 05:34:20 -0500 | [diff] [blame] | 842 | } |
Nguyễn Thái Ngọc Duy | 9a0a30a | 2012-04-23 19:30:22 +0700 | [diff] [blame] | 843 | |
Dimitriy Ryazantcev | 8f354a1 | 2019-07-02 21:22:48 +0300 | [diff] [blame] | 844 | static void strbuf_humanise(struct strbuf *buf, off_t bytes, |
| 845 | int humanise_rate) |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 846 | { |
| 847 | if (bytes > 1 << 30) { |
Dimitriy Ryazantcev | 8f354a1 | 2019-07-02 21:22:48 +0300 | [diff] [blame] | 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"), |
Jeff King | 7726d36 | 2018-07-24 06:52:29 -0400 | [diff] [blame] | 854 | (unsigned)(bytes >> 30), |
| 855 | (unsigned)(bytes & ((1 << 30) - 1)) / 10737419); |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 856 | } else if (bytes > 1 << 20) { |
Jeff King | 7726d36 | 2018-07-24 06:52:29 -0400 | [diff] [blame] | 857 | unsigned x = bytes + 5243; /* for rounding */ |
Dimitriy Ryazantcev | 8f354a1 | 2019-07-02 21:22:48 +0300 | [diff] [blame] | 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"), |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 864 | x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20); |
| 865 | } else if (bytes > 1 << 10) { |
Jeff King | 7726d36 | 2018-07-24 06:52:29 -0400 | [diff] [blame] | 866 | unsigned x = bytes + 5; /* for rounding */ |
Dimitriy Ryazantcev | 8f354a1 | 2019-07-02 21:22:48 +0300 | [diff] [blame] | 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"), |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 873 | x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10); |
| 874 | } else { |
Dimitriy Ryazantcev | 8f354a1 | 2019-07-02 21:22:48 +0300 | [diff] [blame] | 875 | strbuf_addf(buf, |
| 876 | humanise_rate == 0 ? |
| 877 | /* TRANSLATORS: IEC 80000-13:2008 byte */ |
| 878 | Q_("%u byte", "%u bytes", (unsigned)bytes) : |
| 879 | /* TRANSLATORS: IEC 80000-13:2008 byte/second */ |
| 880 | Q_("%u byte/s", "%u bytes/s", (unsigned)bytes), |
| 881 | (unsigned)bytes); |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
Dimitriy Ryazantcev | 8f354a1 | 2019-07-02 21:22:48 +0300 | [diff] [blame] | 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 | |
René Scharfe | 679eebe | 2014-07-28 20:33:55 +0200 | [diff] [blame] | 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 | |
René Scharfe | 33ad9dd | 2017-02-25 17:00:33 +0100 | [diff] [blame] | 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 | |
Nguyễn Thái Ngọc Duy | 9a0a30a | 2012-04-23 19:30:22 +0700 | [diff] [blame] | 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 | } |
Jeff King | 88d5a6f | 2014-05-22 05:44:09 -0400 | [diff] [blame] | 954 | |
| 955 | char *xstrdup_tolower(const char *string) |
| 956 | { |
| 957 | char *result; |
| 958 | size_t len, i; |
| 959 | |
| 960 | len = strlen(string); |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 961 | result = xmallocz(len); |
Jeff King | 88d5a6f | 2014-05-22 05:44:09 -0400 | [diff] [blame] | 962 | for (i = 0; i < len; i++) |
| 963 | result[i] = tolower(string[i]); |
Jeff King | 88d5a6f | 2014-05-22 05:44:09 -0400 | [diff] [blame] | 964 | return result; |
| 965 | } |
Jeff King | 30a0ddb | 2014-06-18 16:01:34 -0400 | [diff] [blame] | 966 | |
Lars Schneider | 13ecb463 | 2018-02-15 16:27:06 +0100 | [diff] [blame] | 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]); |
Junio C Hamano | d1df574 | 2005-04-25 18:26:45 -0700 | [diff] [blame] | 976 | return result; |
| 977 | } |
Jeff King | 30a0ddb | 2014-06-18 16:01:34 -0400 | [diff] [blame] | 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 | } |
Jeff King | aa1462c | 2015-06-25 12:55:45 -0400 | [diff] [blame] | 997 | |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 998 | void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm, |
Ævar Arnfjörð Bjarmason | 3b70223 | 2017-07-01 13:15:47 +0000 | [diff] [blame] | 999 | int tz_offset, int suppress_tz_name) |
Jeff King | aa1462c | 2015-06-25 12:55:45 -0400 | [diff] [blame] | 1000 | { |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 1001 | struct strbuf munged_fmt = STRBUF_INIT; |
Jeff King | e4f031e | 2015-06-30 09:26:53 -0400 | [diff] [blame] | 1002 | size_t hint = 128; |
Jeff King | aa1462c | 2015-06-25 12:55:45 -0400 | [diff] [blame] | 1003 | size_t len; |
| 1004 | |
Jeff King | e4f031e | 2015-06-30 09:26:53 -0400 | [diff] [blame] | 1005 | if (!*fmt) |
| 1006 | return; |
| 1007 | |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 1008 | /* |
| 1009 | * There is no portable way to pass timezone information to |
Jeff King | 9b591b9 | 2021-11-02 07:35:34 -0400 | [diff] [blame] | 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. |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 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; |
Jeff King | 9b591b9 | 2021-11-02 07:35:34 -0400 | [diff] [blame] | 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; |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 1035 | case 'z': |
| 1036 | strbuf_addf(&munged_fmt, "%+05d", tz_offset); |
| 1037 | fmt++; |
| 1038 | break; |
| 1039 | case 'Z': |
Ævar Arnfjörð Bjarmason | 3b70223 | 2017-07-01 13:15:47 +0000 | [diff] [blame] | 1040 | if (suppress_tz_name) { |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 1041 | fmt++; |
| 1042 | break; |
| 1043 | } |
| 1044 | /* FALLTHROUGH */ |
| 1045 | default: |
| 1046 | strbuf_addch(&munged_fmt, '%'); |
| 1047 | } |
| 1048 | } |
| 1049 | fmt = munged_fmt.buf; |
| 1050 | |
Jeff King | e4f031e | 2015-06-30 09:26:53 -0400 | [diff] [blame] | 1051 | strbuf_grow(sb, hint); |
Jeff King | aa1462c | 2015-06-25 12:55:45 -0400 | [diff] [blame] | 1052 | len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm); |
| 1053 | |
| 1054 | if (!len) { |
| 1055 | /* |
Jeff King | e4f031e | 2015-06-30 09:26:53 -0400 | [diff] [blame] | 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. |
Jeff King | aa1462c | 2015-06-25 12:55:45 -0400 | [diff] [blame] | 1061 | */ |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 1062 | strbuf_addch(&munged_fmt, ' '); |
Jeff King | e4f031e | 2015-06-30 09:26:53 -0400 | [diff] [blame] | 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 | } |
Jeff King | e4f031e | 2015-06-30 09:26:53 -0400 | [diff] [blame] | 1069 | len--; /* drop munged space */ |
Jeff King | aa1462c | 2015-06-25 12:55:45 -0400 | [diff] [blame] | 1070 | } |
René Scharfe | c3fbf81 | 2017-06-15 14:29:53 +0200 | [diff] [blame] | 1071 | strbuf_release(&munged_fmt); |
Jeff King | e4f031e | 2015-06-30 09:26:53 -0400 | [diff] [blame] | 1072 | strbuf_setlen(sb, sb->len + len); |
Jeff King | aa1462c | 2015-06-25 12:55:45 -0400 | [diff] [blame] | 1073 | } |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 1074 | |
Jonathan Tan | 155b517 | 2021-10-08 14:08:17 -0700 | [diff] [blame] | 1075 | void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, |
| 1076 | const struct object_id *oid, int abbrev_len) |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 1077 | { |
| 1078 | int r; |
brian m. carlson | 4b048c9 | 2018-07-16 01:27:57 +0000 | [diff] [blame] | 1079 | strbuf_grow(sb, GIT_MAX_HEXSZ + 1); |
Jonathan Tan | 155b517 | 2021-10-08 14:08:17 -0700 | [diff] [blame] | 1080 | r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len); |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 1081 | strbuf_setlen(sb, sb->len + r); |
| 1082 | } |
Junio C Hamano | 1ad7c0f | 2015-10-26 15:55:20 -0700 | [diff] [blame] | 1083 | |
Jonathan Tan | 155b517 | 2021-10-08 14:08:17 -0700 | [diff] [blame] | 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 | |
Tobias Klauser | 63af4a8 | 2015-10-16 17:16:42 +0200 | [diff] [blame] | 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 | { |
Jeff King | 26114c0 | 2018-07-24 06:51:08 -0400 | [diff] [blame] | 1124 | size_t empties = 0; |
Tobias Klauser | 63af4a8 | 2015-10-16 17:16:42 +0200 | [diff] [blame] | 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 | } |
Jeff King | 670c359 | 2016-10-03 16:34:17 -0400 | [diff] [blame] | 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 | } |
Johannes Schindelin | b38dd9e | 2019-12-13 08:08:00 +0000 | [diff] [blame] | 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 | } |