strbuf.con commit config: die on error in command-line config (1c2c9be)
   1#include "cache.h"
   2#include "refs.h"
   3
   4int prefixcmp(const char *str, const char *prefix)
   5{
   6        for (; ; str++, prefix++)
   7                if (!*prefix)
   8                        return 0;
   9                else if (*str != *prefix)
  10                        return (unsigned char)*prefix - (unsigned char)*str;
  11}
  12
  13int suffixcmp(const char *str, const char *suffix)
  14{
  15        int len = strlen(str), suflen = strlen(suffix);
  16        if (len < suflen)
  17                return -1;
  18        else
  19                return strcmp(str + len - suflen, suffix);
  20}
  21
  22/*
  23 * Used as the default ->buf value, so that people can always assume
  24 * buf is non NULL and ->buf is NUL terminated even for a freshly
  25 * initialized strbuf.
  26 */
  27char strbuf_slopbuf[1];
  28
  29void strbuf_init(struct strbuf *sb, size_t hint)
  30{
  31        sb->alloc = sb->len = 0;
  32        sb->buf = strbuf_slopbuf;
  33        if (hint)
  34                strbuf_grow(sb, hint);
  35}
  36
  37void strbuf_release(struct strbuf *sb)
  38{
  39        if (sb->alloc) {
  40                free(sb->buf);
  41                strbuf_init(sb, 0);
  42        }
  43}
  44
  45char *strbuf_detach(struct strbuf *sb, size_t *sz)
  46{
  47        char *res = sb->alloc ? sb->buf : NULL;
  48        if (sz)
  49                *sz = sb->len;
  50        strbuf_init(sb, 0);
  51        return res;
  52}
  53
  54void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
  55{
  56        strbuf_release(sb);
  57        sb->buf   = buf;
  58        sb->len   = len;
  59        sb->alloc = alloc;
  60        strbuf_grow(sb, 0);
  61        sb->buf[sb->len] = '\0';
  62}
  63
  64void strbuf_grow(struct strbuf *sb, size_t extra)
  65{
  66        if (unsigned_add_overflows(extra, 1) ||
  67            unsigned_add_overflows(sb->len, extra + 1))
  68                die("you want to use way too much memory");
  69        if (!sb->alloc)
  70                sb->buf = NULL;
  71        ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
  72}
  73
  74void strbuf_trim(struct strbuf *sb)
  75{
  76        char *b = sb->buf;
  77        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  78                sb->len--;
  79        while (sb->len > 0 && isspace(*b)) {
  80                b++;
  81                sb->len--;
  82        }
  83        memmove(sb->buf, b, sb->len);
  84        sb->buf[sb->len] = '\0';
  85}
  86void strbuf_rtrim(struct strbuf *sb)
  87{
  88        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  89                sb->len--;
  90        sb->buf[sb->len] = '\0';
  91}
  92
  93void strbuf_ltrim(struct strbuf *sb)
  94{
  95        char *b = sb->buf;
  96        while (sb->len > 0 && isspace(*b)) {
  97                b++;
  98                sb->len--;
  99        }
 100        memmove(sb->buf, b, sb->len);
 101        sb->buf[sb->len] = '\0';
 102}
 103
 104struct strbuf **strbuf_split_max(const struct strbuf *sb, int delim, int max)
 105{
 106        int alloc = 2, pos = 0;
 107        char *n, *p;
 108        struct strbuf **ret;
 109        struct strbuf *t;
 110
 111        ret = xcalloc(alloc, sizeof(struct strbuf *));
 112        p = n = sb->buf;
 113        while (n < sb->buf + sb->len) {
 114                int len;
 115                if (max <= 0 || pos + 1 < max)
 116                        n = memchr(n, delim, sb->len - (n - sb->buf));
 117                else
 118                        n = NULL;
 119                if (pos + 1 >= alloc) {
 120                        alloc = alloc * 2;
 121                        ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
 122                }
 123                if (!n)
 124                        n = sb->buf + sb->len - 1;
 125                len = n - p + 1;
 126                t = xmalloc(sizeof(struct strbuf));
 127                strbuf_init(t, len);
 128                strbuf_add(t, p, len);
 129                ret[pos] = t;
 130                ret[++pos] = NULL;
 131                p = ++n;
 132        }
 133        return ret;
 134}
 135
 136void strbuf_list_free(struct strbuf **sbs)
 137{
 138        struct strbuf **s = sbs;
 139
 140        while (*s) {
 141                strbuf_release(*s);
 142                free(*s++);
 143        }
 144        free(sbs);
 145}
 146
 147int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
 148{
 149        int len = a->len < b->len ? a->len: b->len;
 150        int cmp = memcmp(a->buf, b->buf, len);
 151        if (cmp)
 152                return cmp;
 153        return a->len < b->len ? -1: a->len != b->len;
 154}
 155
 156void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
 157                                   const void *data, size_t dlen)
 158{
 159        if (unsigned_add_overflows(pos, len))
 160                die("you want to use way too much memory");
 161        if (pos > sb->len)
 162                die("`pos' is too far after the end of the buffer");
 163        if (pos + len > sb->len)
 164                die("`pos + len' is too far after the end of the buffer");
 165
 166        if (dlen >= len)
 167                strbuf_grow(sb, dlen - len);
 168        memmove(sb->buf + pos + dlen,
 169                        sb->buf + pos + len,
 170                        sb->len - pos - len);
 171        memcpy(sb->buf + pos, data, dlen);
 172        strbuf_setlen(sb, sb->len + dlen - len);
 173}
 174
 175void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
 176{
 177        strbuf_splice(sb, pos, 0, data, len);
 178}
 179
 180void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
 181{
 182        strbuf_splice(sb, pos, len, NULL, 0);
 183}
 184
 185void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 186{
 187        strbuf_grow(sb, len);
 188        memcpy(sb->buf + sb->len, data, len);
 189        strbuf_setlen(sb, sb->len + len);
 190}
 191
 192void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
 193{
 194        strbuf_grow(sb, len);
 195        memcpy(sb->buf + sb->len, sb->buf + pos, len);
 196        strbuf_setlen(sb, sb->len + len);
 197}
 198
 199void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 200{
 201        va_list ap;
 202        va_start(ap, fmt);
 203        strbuf_vaddf(sb, fmt, ap);
 204        va_end(ap);
 205}
 206
 207void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
 208{
 209        int len;
 210        va_list cp;
 211
 212        if (!strbuf_avail(sb))
 213                strbuf_grow(sb, 64);
 214        va_copy(cp, ap);
 215        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
 216        va_end(cp);
 217        if (len < 0)
 218                die("BUG: your vsnprintf is broken (returned %d)", len);
 219        if (len > strbuf_avail(sb)) {
 220                strbuf_grow(sb, len);
 221                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
 222                if (len > strbuf_avail(sb))
 223                        die("BUG: your vsnprintf is broken (insatiable)");
 224        }
 225        strbuf_setlen(sb, sb->len + len);
 226}
 227
 228void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
 229                   void *context)
 230{
 231        for (;;) {
 232                const char *percent;
 233                size_t consumed;
 234
 235                percent = strchrnul(format, '%');
 236                strbuf_add(sb, format, percent - format);
 237                if (!*percent)
 238                        break;
 239                format = percent + 1;
 240
 241                if (*format == '%') {
 242                        strbuf_addch(sb, '%');
 243                        format++;
 244                        continue;
 245                }
 246
 247                consumed = fn(sb, format, context);
 248                if (consumed)
 249                        format += consumed;
 250                else
 251                        strbuf_addch(sb, '%');
 252        }
 253}
 254
 255size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
 256                void *context)
 257{
 258        struct strbuf_expand_dict_entry *e = context;
 259        size_t len;
 260
 261        for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
 262                if (!strncmp(placeholder, e->placeholder, len)) {
 263                        if (e->value)
 264                                strbuf_addstr(sb, e->value);
 265                        return len;
 266                }
 267        }
 268        return 0;
 269}
 270
 271void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
 272{
 273        int i, len = src->len;
 274
 275        for (i = 0; i < len; i++) {
 276                if (src->buf[i] == '%')
 277                        strbuf_addch(dst, '%');
 278                strbuf_addch(dst, src->buf[i]);
 279        }
 280}
 281
 282size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
 283{
 284        size_t res;
 285        size_t oldalloc = sb->alloc;
 286
 287        strbuf_grow(sb, size);
 288        res = fread(sb->buf + sb->len, 1, size, f);
 289        if (res > 0)
 290                strbuf_setlen(sb, sb->len + res);
 291        else if (oldalloc == 0)
 292                strbuf_release(sb);
 293        return res;
 294}
 295
 296ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 297{
 298        size_t oldlen = sb->len;
 299        size_t oldalloc = sb->alloc;
 300
 301        strbuf_grow(sb, hint ? hint : 8192);
 302        for (;;) {
 303                ssize_t cnt;
 304
 305                cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
 306                if (cnt < 0) {
 307                        if (oldalloc == 0)
 308                                strbuf_release(sb);
 309                        else
 310                                strbuf_setlen(sb, oldlen);
 311                        return -1;
 312                }
 313                if (!cnt)
 314                        break;
 315                sb->len += cnt;
 316                strbuf_grow(sb, 8192);
 317        }
 318
 319        sb->buf[sb->len] = '\0';
 320        return sb->len - oldlen;
 321}
 322
 323#define STRBUF_MAXLINK (2*PATH_MAX)
 324
 325int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
 326{
 327        size_t oldalloc = sb->alloc;
 328
 329        if (hint < 32)
 330                hint = 32;
 331
 332        while (hint < STRBUF_MAXLINK) {
 333                int len;
 334
 335                strbuf_grow(sb, hint);
 336                len = readlink(path, sb->buf, hint);
 337                if (len < 0) {
 338                        if (errno != ERANGE)
 339                                break;
 340                } else if (len < hint) {
 341                        strbuf_setlen(sb, len);
 342                        return 0;
 343                }
 344
 345                /* .. the buffer was too small - try again */
 346                hint *= 2;
 347        }
 348        if (oldalloc == 0)
 349                strbuf_release(sb);
 350        return -1;
 351}
 352
 353int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 354{
 355        int ch;
 356
 357        strbuf_grow(sb, 0);
 358        if (feof(fp))
 359                return EOF;
 360
 361        strbuf_reset(sb);
 362        while ((ch = fgetc(fp)) != EOF) {
 363                strbuf_grow(sb, 1);
 364                sb->buf[sb->len++] = ch;
 365                if (ch == term)
 366                        break;
 367        }
 368        if (ch == EOF && sb->len == 0)
 369                return EOF;
 370
 371        sb->buf[sb->len] = '\0';
 372        return 0;
 373}
 374
 375int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 376{
 377        if (strbuf_getwholeline(sb, fp, term))
 378                return EOF;
 379        if (sb->buf[sb->len-1] == term)
 380                strbuf_setlen(sb, sb->len-1);
 381        return 0;
 382}
 383
 384int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 385{
 386        int fd, len;
 387
 388        fd = open(path, O_RDONLY);
 389        if (fd < 0)
 390                return -1;
 391        len = strbuf_read(sb, fd, hint);
 392        close(fd);
 393        if (len < 0)
 394                return -1;
 395
 396        return len;
 397}