THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1128 lines
24KB

  1. #include "cache.h"
  2. #include "refs.h"
  3. #include "string-list.h"
  4. #include "utf8.h"
  5. int starts_with(const char *str, const char *prefix)
  6. {
  7. for (; ; str++, prefix++)
  8. if (!*prefix)
  9. return 1;
  10. else if (*str != *prefix)
  11. return 0;
  12. }
  13. int istarts_with(const char *str, const char *prefix)
  14. {
  15. for (; ; str++, prefix++)
  16. if (!*prefix)
  17. return 1;
  18. else if (tolower(*str) != tolower(*prefix))
  19. return 0;
  20. }
  21. int skip_to_optional_arg_default(const char *str, const char *prefix,
  22. const char **arg, const char *def)
  23. {
  24. const char *p;
  25. if (!skip_prefix(str, prefix, &p))
  26. return 0;
  27. if (!*p) {
  28. if (arg)
  29. *arg = def;
  30. return 1;
  31. }
  32. if (*p != '=')
  33. return 0;
  34. if (arg)
  35. *arg = p + 1;
  36. return 1;
  37. }
  38. /*
  39. * Used as the default ->buf value, so that people can always assume
  40. * buf is non NULL and ->buf is NUL terminated even for a freshly
  41. * initialized strbuf.
  42. */
  43. char strbuf_slopbuf[1];
  44. void strbuf_init(struct strbuf *sb, size_t hint)
  45. {
  46. sb->alloc = sb->len = 0;
  47. sb->buf = strbuf_slopbuf;
  48. if (hint)
  49. strbuf_grow(sb, hint);
  50. }
  51. void strbuf_release(struct strbuf *sb)
  52. {
  53. if (sb->alloc) {
  54. free(sb->buf);
  55. strbuf_init(sb, 0);
  56. }
  57. }
  58. char *strbuf_detach(struct strbuf *sb, size_t *sz)
  59. {
  60. char *res;
  61. strbuf_grow(sb, 0);
  62. res = sb->buf;
  63. if (sz)
  64. *sz = sb->len;
  65. strbuf_init(sb, 0);
  66. return res;
  67. }
  68. void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
  69. {
  70. strbuf_release(sb);
  71. sb->buf = buf;
  72. sb->len = len;
  73. sb->alloc = alloc;
  74. strbuf_grow(sb, 0);
  75. sb->buf[sb->len] = '\0';
  76. }
  77. void strbuf_grow(struct strbuf *sb, size_t extra)
  78. {
  79. int new_buf = !sb->alloc;
  80. if (unsigned_add_overflows(extra, 1) ||
  81. unsigned_add_overflows(sb->len, extra + 1))
  82. die("you want to use way too much memory");
  83. if (new_buf)
  84. sb->buf = NULL;
  85. ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
  86. if (new_buf)
  87. sb->buf[0] = '\0';
  88. }
  89. void strbuf_trim(struct strbuf *sb)
  90. {
  91. strbuf_rtrim(sb);
  92. strbuf_ltrim(sb);
  93. }
  94. void strbuf_rtrim(struct strbuf *sb)
  95. {
  96. while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  97. sb->len--;
  98. sb->buf[sb->len] = '\0';
  99. }
  100. void strbuf_trim_trailing_dir_sep(struct strbuf *sb)
  101. {
  102. while (sb->len > 0 && is_dir_sep((unsigned char)sb->buf[sb->len - 1]))
  103. sb->len--;
  104. sb->buf[sb->len] = '\0';
  105. }
  106. void strbuf_trim_trailing_newline(struct strbuf *sb)
  107. {
  108. if (sb->len > 0 && sb->buf[sb->len - 1] == '\n') {
  109. if (--sb->len > 0 && sb->buf[sb->len - 1] == '\r')
  110. --sb->len;
  111. sb->buf[sb->len] = '\0';
  112. }
  113. }
  114. void strbuf_ltrim(struct strbuf *sb)
  115. {
  116. char *b = sb->buf;
  117. while (sb->len > 0 && isspace(*b)) {
  118. b++;
  119. sb->len--;
  120. }
  121. memmove(sb->buf, b, sb->len);
  122. sb->buf[sb->len] = '\0';
  123. }
  124. int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
  125. {
  126. char *out;
  127. size_t len;
  128. if (same_encoding(from, to))
  129. return 0;
  130. out = reencode_string_len(sb->buf, sb->len, to, from, &len);
  131. if (!out)
  132. return -1;
  133. strbuf_attach(sb, out, len, len);
  134. return 0;
  135. }
  136. void strbuf_tolower(struct strbuf *sb)
  137. {
  138. char *p = sb->buf, *end = sb->buf + sb->len;
  139. for (; p < end; p++)
  140. *p = tolower(*p);
  141. }
  142. struct strbuf **strbuf_split_buf(const char *str, size_t slen,
  143. int terminator, int max)
  144. {
  145. struct strbuf **ret = NULL;
  146. size_t nr = 0, alloc = 0;
  147. struct strbuf *t;
  148. while (slen) {
  149. int len = slen;
  150. if (max <= 0 || nr + 1 < max) {
  151. const char *end = memchr(str, terminator, slen);
  152. if (end)
  153. len = end - str + 1;
  154. }
  155. t = xmalloc(sizeof(struct strbuf));
  156. strbuf_init(t, len);
  157. strbuf_add(t, str, len);
  158. ALLOC_GROW(ret, nr + 2, alloc);
  159. ret[nr++] = t;
  160. str += len;
  161. slen -= len;
  162. }
  163. ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
  164. ret[nr] = NULL;
  165. return ret;
  166. }
  167. void strbuf_add_separated_string_list(struct strbuf *str,
  168. const char *sep,
  169. struct string_list *slist)
  170. {
  171. struct string_list_item *item;
  172. int sep_needed = 0;
  173. for_each_string_list_item(item, slist) {
  174. if (sep_needed)
  175. strbuf_addstr(str, sep);
  176. strbuf_addstr(str, item->string);
  177. sep_needed = 1;
  178. }
  179. }
  180. void strbuf_list_free(struct strbuf **sbs)
  181. {
  182. struct strbuf **s = sbs;
  183. while (*s) {
  184. strbuf_release(*s);
  185. free(*s++);
  186. }
  187. free(sbs);
  188. }
  189. int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
  190. {
  191. size_t len = a->len < b->len ? a->len: b->len;
  192. int cmp = memcmp(a->buf, b->buf, len);
  193. if (cmp)
  194. return cmp;
  195. return a->len < b->len ? -1: a->len != b->len;
  196. }
  197. void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  198. const void *data, size_t dlen)
  199. {
  200. if (unsigned_add_overflows(pos, len))
  201. die("you want to use way too much memory");
  202. if (pos > sb->len)
  203. die("`pos' is too far after the end of the buffer");
  204. if (pos + len > sb->len)
  205. die("`pos + len' is too far after the end of the buffer");
  206. if (dlen >= len)
  207. strbuf_grow(sb, dlen - len);
  208. memmove(sb->buf + pos + dlen,
  209. sb->buf + pos + len,
  210. sb->len - pos - len);
  211. memcpy(sb->buf + pos, data, dlen);
  212. strbuf_setlen(sb, sb->len + dlen - len);
  213. }
  214. void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
  215. {
  216. strbuf_splice(sb, pos, 0, data, len);
  217. }
  218. void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)
  219. {
  220. int len, len2;
  221. char save;
  222. va_list cp;
  223. if (pos > sb->len)
  224. die("`pos' is too far after the end of the buffer");
  225. va_copy(cp, ap);
  226. len = vsnprintf(sb->buf + sb->len, 0, fmt, cp);
  227. va_end(cp);
  228. if (len < 0)
  229. BUG("your vsnprintf is broken (returned %d)", len);
  230. if (!len)
  231. return; /* nothing to do */
  232. if (unsigned_add_overflows(sb->len, len))
  233. die("you want to use way too much memory");
  234. strbuf_grow(sb, len);
  235. memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
  236. /* vsnprintf() will append a NUL, overwriting one of our characters */
  237. save = sb->buf[pos + len];
  238. len2 = vsnprintf(sb->buf + pos, len + 1, fmt, ap);
  239. sb->buf[pos + len] = save;
  240. if (len2 != len)
  241. BUG("your vsnprintf is broken (returns inconsistent lengths)");
  242. strbuf_setlen(sb, sb->len + len);
  243. }
  244. void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...)
  245. {
  246. va_list ap;
  247. va_start(ap, fmt);
  248. strbuf_vinsertf(sb, pos, fmt, ap);
  249. va_end(ap);
  250. }
  251. void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
  252. {
  253. strbuf_splice(sb, pos, len, "", 0);
  254. }
  255. void strbuf_add(struct strbuf *sb, const void *data, size_t len)
  256. {
  257. strbuf_grow(sb, len);
  258. memcpy(sb->buf + sb->len, data, len);
  259. strbuf_setlen(sb, sb->len + len);
  260. }
  261. void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
  262. {
  263. strbuf_grow(sb, sb2->len);
  264. memcpy(sb->buf + sb->len, sb2->buf, sb2->len);
  265. strbuf_setlen(sb, sb->len + sb2->len);
  266. }
  267. const char *strbuf_join_argv(struct strbuf *buf,
  268. int argc, const char **argv, char delim)
  269. {
  270. if (!argc)
  271. return buf->buf;
  272. strbuf_addstr(buf, *argv);
  273. while (--argc) {
  274. strbuf_addch(buf, delim);
  275. strbuf_addstr(buf, *(++argv));
  276. }
  277. return buf->buf;
  278. }
  279. void strbuf_addchars(struct strbuf *sb, int c, size_t n)
  280. {
  281. strbuf_grow(sb, n);
  282. memset(sb->buf + sb->len, c, n);
  283. strbuf_setlen(sb, sb->len + n);
  284. }
  285. void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
  286. {
  287. va_list ap;
  288. va_start(ap, fmt);
  289. strbuf_vaddf(sb, fmt, ap);
  290. va_end(ap);
  291. }
  292. static void add_lines(struct strbuf *out,
  293. const char *prefix1,
  294. const char *prefix2,
  295. const char *buf, size_t size)
  296. {
  297. while (size) {
  298. const char *prefix;
  299. const char *next = memchr(buf, '\n', size);
  300. next = next ? (next + 1) : (buf + size);
  301. prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
  302. ? prefix2 : prefix1);
  303. strbuf_addstr(out, prefix);
  304. strbuf_add(out, buf, next - buf);
  305. size -= next - buf;
  306. buf = next;
  307. }
  308. strbuf_complete_line(out);
  309. }
  310. void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
  311. {
  312. static char prefix1[3];
  313. static char prefix2[2];
  314. if (prefix1[0] != comment_line_char) {
  315. xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char);
  316. xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char);
  317. }
  318. add_lines(out, prefix1, prefix2, buf, size);
  319. }
  320. void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
  321. {
  322. va_list params;
  323. struct strbuf buf = STRBUF_INIT;
  324. int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
  325. va_start(params, fmt);
  326. strbuf_vaddf(&buf, fmt, params);
  327. va_end(params);
  328. strbuf_add_commented_lines(sb, buf.buf, buf.len);
  329. if (incomplete_line)
  330. sb->buf[--sb->len] = '\0';
  331. strbuf_release(&buf);
  332. }
  333. void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
  334. {
  335. int len;
  336. va_list cp;
  337. if (!strbuf_avail(sb))
  338. strbuf_grow(sb, 64);
  339. va_copy(cp, ap);
  340. len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
  341. va_end(cp);
  342. if (len < 0)
  343. BUG("your vsnprintf is broken (returned %d)", len);
  344. if (len > strbuf_avail(sb)) {
  345. strbuf_grow(sb, len);
  346. len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
  347. if (len > strbuf_avail(sb))
  348. BUG("your vsnprintf is broken (insatiable)");
  349. }
  350. strbuf_setlen(sb, sb->len + len);
  351. }
  352. void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
  353. void *context)
  354. {
  355. for (;;) {
  356. const char *percent;
  357. size_t consumed;
  358. percent = strchrnul(format, '%');
  359. strbuf_add(sb, format, percent - format);
  360. if (!*percent)
  361. break;
  362. format = percent + 1;
  363. if (*format == '%') {
  364. strbuf_addch(sb, '%');
  365. format++;
  366. continue;
  367. }
  368. consumed = fn(sb, format, context);
  369. if (consumed)
  370. format += consumed;
  371. else
  372. strbuf_addch(sb, '%');
  373. }
  374. }
  375. size_t strbuf_expand_literal_cb(struct strbuf *sb,
  376. const char *placeholder,
  377. void *context)
  378. {
  379. int ch;
  380. switch (placeholder[0]) {
  381. case 'n': /* newline */
  382. strbuf_addch(sb, '\n');
  383. return 1;
  384. case 'x':
  385. /* %x00 == NUL, %x0a == LF, etc. */
  386. ch = hex2chr(placeholder + 1);
  387. if (ch < 0)
  388. return 0;
  389. strbuf_addch(sb, ch);
  390. return 3;
  391. }
  392. return 0;
  393. }
  394. size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
  395. void *context)
  396. {
  397. struct strbuf_expand_dict_entry *e = context;
  398. size_t len;
  399. for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
  400. if (!strncmp(placeholder, e->placeholder, len)) {
  401. if (e->value)
  402. strbuf_addstr(sb, e->value);
  403. return len;
  404. }
  405. }
  406. return 0;
  407. }
  408. void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
  409. {
  410. size_t i, len = src->len;
  411. for (i = 0; i < len; i++) {
  412. if (src->buf[i] == '%')
  413. strbuf_addch(dst, '%');
  414. strbuf_addch(dst, src->buf[i]);
  415. }
  416. }
  417. size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
  418. {
  419. size_t res;
  420. size_t oldalloc = sb->alloc;
  421. strbuf_grow(sb, size);
  422. res = fread(sb->buf + sb->len, 1, size, f);
  423. if (res > 0)
  424. strbuf_setlen(sb, sb->len + res);
  425. else if (oldalloc == 0)
  426. strbuf_release(sb);
  427. return res;
  428. }
  429. ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
  430. {
  431. size_t oldlen = sb->len;
  432. size_t oldalloc = sb->alloc;
  433. strbuf_grow(sb, hint ? hint : 8192);
  434. for (;;) {
  435. ssize_t want = sb->alloc - sb->len - 1;
  436. ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
  437. if (got < 0) {
  438. if (oldalloc == 0)
  439. strbuf_release(sb);
  440. else
  441. strbuf_setlen(sb, oldlen);
  442. return -1;
  443. }
  444. sb->len += got;
  445. if (got < want)
  446. break;
  447. strbuf_grow(sb, 8192);
  448. }
  449. sb->buf[sb->len] = '\0';
  450. return sb->len - oldlen;
  451. }
  452. ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
  453. {
  454. size_t oldalloc = sb->alloc;
  455. ssize_t cnt;
  456. strbuf_grow(sb, hint ? hint : 8192);
  457. cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
  458. if (cnt > 0)
  459. strbuf_setlen(sb, sb->len + cnt);
  460. else if (oldalloc == 0)
  461. strbuf_release(sb);
  462. return cnt;
  463. }
  464. ssize_t strbuf_write(struct strbuf *sb, FILE *f)
  465. {
  466. return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
  467. }
  468. #define STRBUF_MAXLINK (2*PATH_MAX)
  469. int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
  470. {
  471. size_t oldalloc = sb->alloc;
  472. if (hint < 32)
  473. hint = 32;
  474. while (hint < STRBUF_MAXLINK) {
  475. ssize_t len;
  476. strbuf_grow(sb, hint);
  477. len = readlink(path, sb->buf, hint);
  478. if (len < 0) {
  479. if (errno != ERANGE)
  480. break;
  481. } else if (len < hint) {
  482. strbuf_setlen(sb, len);
  483. return 0;
  484. }
  485. /* .. the buffer was too small - try again */
  486. hint *= 2;
  487. }
  488. if (oldalloc == 0)
  489. strbuf_release(sb);
  490. return -1;
  491. }
  492. int strbuf_getcwd(struct strbuf *sb)
  493. {
  494. size_t oldalloc = sb->alloc;
  495. size_t guessed_len = 128;
  496. for (;; guessed_len *= 2) {
  497. strbuf_grow(sb, guessed_len);
  498. if (getcwd(sb->buf, sb->alloc)) {
  499. strbuf_setlen(sb, strlen(sb->buf));
  500. return 0;
  501. }
  502. /*
  503. * If getcwd(3) is implemented as a syscall that falls
  504. * back to a regular lookup using readdir(3) etc. then
  505. * we may be able to avoid EACCES by providing enough
  506. * space to the syscall as it's not necessarily bound
  507. * to the same restrictions as the fallback.
  508. */
  509. if (errno == EACCES && guessed_len < PATH_MAX)
  510. continue;
  511. if (errno != ERANGE)
  512. break;
  513. }
  514. if (oldalloc == 0)
  515. strbuf_release(sb);
  516. else
  517. strbuf_reset(sb);
  518. return -1;
  519. }
  520. #ifdef HAVE_GETDELIM
  521. int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
  522. {
  523. ssize_t r;
  524. if (feof(fp))
  525. return EOF;
  526. strbuf_reset(sb);
  527. /* Translate slopbuf to NULL, as we cannot call realloc on it */
  528. if (!sb->alloc)
  529. sb->buf = NULL;
  530. errno = 0;
  531. r = getdelim(&sb->buf, &sb->alloc, term, fp);
  532. if (r > 0) {
  533. sb->len = r;
  534. return 0;
  535. }
  536. assert(r == -1);
  537. /*
  538. * Normally we would have called xrealloc, which will try to free
  539. * memory and recover. But we have no way to tell getdelim() to do so.
  540. * Worse, we cannot try to recover ENOMEM ourselves, because we have
  541. * no idea how many bytes were read by getdelim.
  542. *
  543. * Dying here is reasonable. It mirrors what xrealloc would do on
  544. * catastrophic memory failure. We skip the opportunity to free pack
  545. * memory and retry, but that's unlikely to help for a malloc small
  546. * enough to hold a single line of input, anyway.
  547. */
  548. if (errno == ENOMEM)
  549. die("Out of memory, getdelim failed");
  550. /*
  551. * Restore strbuf invariants; if getdelim left us with a NULL pointer,
  552. * we can just re-init, but otherwise we should make sure that our
  553. * length is empty, and that the result is NUL-terminated.
  554. */
  555. if (!sb->buf)
  556. strbuf_init(sb, 0);
  557. else
  558. strbuf_reset(sb);
  559. return EOF;
  560. }
  561. #else
  562. int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
  563. {
  564. int ch;
  565. if (feof(fp))
  566. return EOF;
  567. strbuf_reset(sb);
  568. flockfile(fp);
  569. while ((ch = getc_unlocked(fp)) != EOF) {
  570. if (!strbuf_avail(sb))
  571. strbuf_grow(sb, 1);
  572. sb->buf[sb->len++] = ch;
  573. if (ch == term)
  574. break;
  575. }
  576. funlockfile(fp);
  577. if (ch == EOF && sb->len == 0)
  578. return EOF;
  579. sb->buf[sb->len] = '\0';
  580. return 0;
  581. }
  582. #endif
  583. static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
  584. {
  585. if (strbuf_getwholeline(sb, fp, term))
  586. return EOF;
  587. if (sb->buf[sb->len - 1] == term)
  588. strbuf_setlen(sb, sb->len - 1);
  589. return 0;
  590. }
  591. int strbuf_getline(struct strbuf *sb, FILE *fp)
  592. {
  593. if (strbuf_getwholeline(sb, fp, '\n'))
  594. return EOF;
  595. if (sb->buf[sb->len - 1] == '\n') {
  596. strbuf_setlen(sb, sb->len - 1);
  597. if (sb->len && sb->buf[sb->len - 1] == '\r')
  598. strbuf_setlen(sb, sb->len - 1);
  599. }
  600. return 0;
  601. }
  602. int strbuf_getline_lf(struct strbuf *sb, FILE *fp)
  603. {
  604. return strbuf_getdelim(sb, fp, '\n');
  605. }
  606. int strbuf_getline_nul(struct strbuf *sb, FILE *fp)
  607. {
  608. return strbuf_getdelim(sb, fp, '\0');
  609. }
  610. int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
  611. {
  612. strbuf_reset(sb);
  613. while (1) {
  614. char ch;
  615. ssize_t len = xread(fd, &ch, 1);
  616. if (len <= 0)
  617. return EOF;
  618. strbuf_addch(sb, ch);
  619. if (ch == term)
  620. break;
  621. }
  622. return 0;
  623. }
  624. ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
  625. {
  626. int fd;
  627. ssize_t len;
  628. int saved_errno;
  629. fd = open(path, O_RDONLY);
  630. if (fd < 0)
  631. return -1;
  632. len = strbuf_read(sb, fd, hint);
  633. saved_errno = errno;
  634. close(fd);
  635. if (len < 0) {
  636. errno = saved_errno;
  637. return -1;
  638. }
  639. return len;
  640. }
  641. void strbuf_add_lines(struct strbuf *out, const char *prefix,
  642. const char *buf, size_t size)
  643. {
  644. add_lines(out, prefix, NULL, buf, size);
  645. }
  646. void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
  647. {
  648. while (*s) {
  649. size_t len = strcspn(s, "\"<>&");
  650. strbuf_add(buf, s, len);
  651. s += len;
  652. switch (*s) {
  653. case '"':
  654. strbuf_addstr(buf, "&quot;");
  655. break;
  656. case '<':
  657. strbuf_addstr(buf, "&lt;");
  658. break;
  659. case '>':
  660. strbuf_addstr(buf, "&gt;");
  661. break;
  662. case '&':
  663. strbuf_addstr(buf, "&amp;");
  664. break;
  665. case 0:
  666. return;
  667. }
  668. s++;
  669. }
  670. }
  671. int is_rfc3986_reserved_or_unreserved(char ch)
  672. {
  673. if (is_rfc3986_unreserved(ch))
  674. return 1;
  675. switch (ch) {
  676. case '!': case '*': case '\'': case '(': case ')': case ';':
  677. case ':': case '@': case '&': case '=': case '+': case '$':
  678. case ',': case '/': case '?': case '#': case '[': case ']':
  679. return 1;
  680. }
  681. return 0;
  682. }
  683. int is_rfc3986_unreserved(char ch)
  684. {
  685. return isalnum(ch) ||
  686. ch == '-' || ch == '_' || ch == '.' || ch == '~';
  687. }
  688. static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
  689. char_predicate allow_unencoded_fn)
  690. {
  691. strbuf_grow(sb, len);
  692. while (len--) {
  693. char ch = *s++;
  694. if (allow_unencoded_fn(ch))
  695. strbuf_addch(sb, ch);
  696. else
  697. strbuf_addf(sb, "%%%02x", (unsigned char)ch);
  698. }
  699. }
  700. void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
  701. char_predicate allow_unencoded_fn)
  702. {
  703. strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn);
  704. }
  705. static void strbuf_humanise(struct strbuf *buf, off_t bytes,
  706. int humanise_rate)
  707. {
  708. if (bytes > 1 << 30) {
  709. strbuf_addf(buf,
  710. humanise_rate == 0 ?
  711. /* TRANSLATORS: IEC 80000-13:2008 gibibyte */
  712. _("%u.%2.2u GiB") :
  713. /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */
  714. _("%u.%2.2u GiB/s"),
  715. (unsigned)(bytes >> 30),
  716. (unsigned)(bytes & ((1 << 30) - 1)) / 10737419);
  717. } else if (bytes > 1 << 20) {
  718. unsigned x = bytes + 5243; /* for rounding */
  719. strbuf_addf(buf,
  720. humanise_rate == 0 ?
  721. /* TRANSLATORS: IEC 80000-13:2008 mebibyte */
  722. _("%u.%2.2u MiB") :
  723. /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */
  724. _("%u.%2.2u MiB/s"),
  725. x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
  726. } else if (bytes > 1 << 10) {
  727. unsigned x = bytes + 5; /* for rounding */
  728. strbuf_addf(buf,
  729. humanise_rate == 0 ?
  730. /* TRANSLATORS: IEC 80000-13:2008 kibibyte */
  731. _("%u.%2.2u KiB") :
  732. /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */
  733. _("%u.%2.2u KiB/s"),
  734. x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
  735. } else {
  736. strbuf_addf(buf,
  737. humanise_rate == 0 ?
  738. /* TRANSLATORS: IEC 80000-13:2008 byte */
  739. Q_("%u byte", "%u bytes", (unsigned)bytes) :
  740. /* TRANSLATORS: IEC 80000-13:2008 byte/second */
  741. Q_("%u byte/s", "%u bytes/s", (unsigned)bytes),
  742. (unsigned)bytes);
  743. }
  744. }
  745. void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
  746. {
  747. strbuf_humanise(buf, bytes, 0);
  748. }
  749. void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
  750. {
  751. strbuf_humanise(buf, bytes, 1);
  752. }
  753. void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
  754. {
  755. if (!*path)
  756. die("The empty string is not a valid path");
  757. if (!is_absolute_path(path)) {
  758. struct stat cwd_stat, pwd_stat;
  759. size_t orig_len = sb->len;
  760. char *cwd = xgetcwd();
  761. char *pwd = getenv("PWD");
  762. if (pwd && strcmp(pwd, cwd) &&
  763. !stat(cwd, &cwd_stat) &&
  764. (cwd_stat.st_dev || cwd_stat.st_ino) &&
  765. !stat(pwd, &pwd_stat) &&
  766. pwd_stat.st_dev == cwd_stat.st_dev &&
  767. pwd_stat.st_ino == cwd_stat.st_ino)
  768. strbuf_addstr(sb, pwd);
  769. else
  770. strbuf_addstr(sb, cwd);
  771. if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
  772. strbuf_addch(sb, '/');
  773. free(cwd);
  774. }
  775. strbuf_addstr(sb, path);
  776. }
  777. void strbuf_add_real_path(struct strbuf *sb, const char *path)
  778. {
  779. if (sb->len) {
  780. struct strbuf resolved = STRBUF_INIT;
  781. strbuf_realpath(&resolved, path, 1);
  782. strbuf_addbuf(sb, &resolved);
  783. strbuf_release(&resolved);
  784. } else
  785. strbuf_realpath(sb, path, 1);
  786. }
  787. int printf_ln(const char *fmt, ...)
  788. {
  789. int ret;
  790. va_list ap;
  791. va_start(ap, fmt);
  792. ret = vprintf(fmt, ap);
  793. va_end(ap);
  794. if (ret < 0 || putchar('\n') == EOF)
  795. return -1;
  796. return ret + 1;
  797. }
  798. int fprintf_ln(FILE *fp, const char *fmt, ...)
  799. {
  800. int ret;
  801. va_list ap;
  802. va_start(ap, fmt);
  803. ret = vfprintf(fp, fmt, ap);
  804. va_end(ap);
  805. if (ret < 0 || putc('\n', fp) == EOF)
  806. return -1;
  807. return ret + 1;
  808. }
  809. char *xstrdup_tolower(const char *string)
  810. {
  811. char *result;
  812. size_t len, i;
  813. len = strlen(string);
  814. result = xmallocz(len);
  815. for (i = 0; i < len; i++)
  816. result[i] = tolower(string[i]);
  817. return result;
  818. }
  819. char *xstrdup_toupper(const char *string)
  820. {
  821. char *result;
  822. size_t len, i;
  823. len = strlen(string);
  824. result = xmallocz(len);
  825. for (i = 0; i < len; i++)
  826. result[i] = toupper(string[i]);
  827. return result;
  828. }
  829. char *xstrvfmt(const char *fmt, va_list ap)
  830. {
  831. struct strbuf buf = STRBUF_INIT;
  832. strbuf_vaddf(&buf, fmt, ap);
  833. return strbuf_detach(&buf, NULL);
  834. }
  835. char *xstrfmt(const char *fmt, ...)
  836. {
  837. va_list ap;
  838. char *ret;
  839. va_start(ap, fmt);
  840. ret = xstrvfmt(fmt, ap);
  841. va_end(ap);
  842. return ret;
  843. }
  844. void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
  845. int tz_offset, int suppress_tz_name)
  846. {
  847. struct strbuf munged_fmt = STRBUF_INIT;
  848. size_t hint = 128;
  849. size_t len;
  850. if (!*fmt)
  851. return;
  852. /*
  853. * There is no portable way to pass timezone information to
  854. * strftime, so we handle %z and %Z here.
  855. */
  856. for (;;) {
  857. const char *percent = strchrnul(fmt, '%');
  858. strbuf_add(&munged_fmt, fmt, percent - fmt);
  859. if (!*percent)
  860. break;
  861. fmt = percent + 1;
  862. switch (*fmt) {
  863. case '%':
  864. strbuf_addstr(&munged_fmt, "%%");
  865. fmt++;
  866. break;
  867. case 'z':
  868. strbuf_addf(&munged_fmt, "%+05d", tz_offset);
  869. fmt++;
  870. break;
  871. case 'Z':
  872. if (suppress_tz_name) {
  873. fmt++;
  874. break;
  875. }
  876. /* FALLTHROUGH */
  877. default:
  878. strbuf_addch(&munged_fmt, '%');
  879. }
  880. }
  881. fmt = munged_fmt.buf;
  882. strbuf_grow(sb, hint);
  883. len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
  884. if (!len) {
  885. /*
  886. * strftime reports "0" if it could not fit the result in the buffer.
  887. * Unfortunately, it also reports "0" if the requested time string
  888. * takes 0 bytes. So our strategy is to munge the format so that the
  889. * output contains at least one character, and then drop the extra
  890. * character before returning.
  891. */
  892. strbuf_addch(&munged_fmt, ' ');
  893. while (!len) {
  894. hint *= 2;
  895. strbuf_grow(sb, hint);
  896. len = strftime(sb->buf + sb->len, sb->alloc - sb->len,
  897. munged_fmt.buf, tm);
  898. }
  899. len--; /* drop munged space */
  900. }
  901. strbuf_release(&munged_fmt);
  902. strbuf_setlen(sb, sb->len + len);
  903. }
  904. void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
  905. int abbrev_len)
  906. {
  907. int r;
  908. strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
  909. r = find_unique_abbrev_r(sb->buf + sb->len, oid, abbrev_len);
  910. strbuf_setlen(sb, sb->len + r);
  911. }
  912. /*
  913. * Returns the length of a line, without trailing spaces.
  914. *
  915. * If the line ends with newline, it will be removed too.
  916. */
  917. static size_t cleanup(char *line, size_t len)
  918. {
  919. while (len) {
  920. unsigned char c = line[len - 1];
  921. if (!isspace(c))
  922. break;
  923. len--;
  924. }
  925. return len;
  926. }
  927. /*
  928. * Remove empty lines from the beginning and end
  929. * and also trailing spaces from every line.
  930. *
  931. * Turn multiple consecutive empty lines between paragraphs
  932. * into just one empty line.
  933. *
  934. * If the input has only empty lines and spaces,
  935. * no output will be produced.
  936. *
  937. * If last line does not have a newline at the end, one is added.
  938. *
  939. * Enable skip_comments to skip every line starting with comment
  940. * character.
  941. */
  942. void strbuf_stripspace(struct strbuf *sb, int skip_comments)
  943. {
  944. size_t empties = 0;
  945. size_t i, j, len, newlen;
  946. char *eol;
  947. /* We may have to add a newline. */
  948. strbuf_grow(sb, 1);
  949. for (i = j = 0; i < sb->len; i += len, j += newlen) {
  950. eol = memchr(sb->buf + i, '\n', sb->len - i);
  951. len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
  952. if (skip_comments && len && sb->buf[i] == comment_line_char) {
  953. newlen = 0;
  954. continue;
  955. }
  956. newlen = cleanup(sb->buf + i, len);
  957. /* Not just an empty line? */
  958. if (newlen) {
  959. if (empties > 0 && j > 0)
  960. sb->buf[j++] = '\n';
  961. empties = 0;
  962. memmove(sb->buf + j, sb->buf + i, newlen);
  963. sb->buf[newlen + j++] = '\n';
  964. } else {
  965. empties++;
  966. }
  967. }
  968. strbuf_setlen(sb, j);
  969. }
  970. int strbuf_normalize_path(struct strbuf *src)
  971. {
  972. struct strbuf dst = STRBUF_INIT;
  973. strbuf_grow(&dst, src->len);
  974. if (normalize_path_copy(dst.buf, src->buf) < 0) {
  975. strbuf_release(&dst);
  976. return -1;
  977. }
  978. /*
  979. * normalize_path does not tell us the new length, so we have to
  980. * compute it by looking for the new NUL it placed
  981. */
  982. strbuf_setlen(&dst, strlen(dst.buf));
  983. strbuf_swap(src, &dst);
  984. strbuf_release(&dst);
  985. return 0;
  986. }