commas.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* commas.c - Deal with comma separated lists
  2. *
  3. * Copyright 2018 Rob Landley <rob@landley.net>
  4. */
  5. #include "toys.h"
  6. // Traverse arg_list of csv, calling callback on each value
  7. void comma_args(struct arg_list *al, void *data, char *err,
  8. char *(*callback)(void *data, char *str, int len))
  9. {
  10. char *next, *arg;
  11. int len;
  12. if (CFG_TOYBOX_DEBUG && !err) err = "INTERNAL";
  13. while (al) {
  14. arg = al->arg;
  15. while ((next = comma_iterate(&arg, &len)))
  16. if ((next = callback(data, next, len)))
  17. error_exit("%s '%s'\n%*c", err, al->arg,
  18. (int)(5+strlen(toys.which->name)+strlen(err)+next-al->arg), '^');
  19. al = al->next;
  20. }
  21. }
  22. // Realloc *old with oldstring,newstring
  23. void comma_collate(char **old, char *new)
  24. {
  25. char *temp, *atold = *old;
  26. // Only add a comma if old string didn't end with one
  27. if (atold && *atold) {
  28. char *comma = ",";
  29. if (atold[strlen(atold)-1] == ',') comma = "";
  30. temp = xmprintf("%s%s%s", atold, comma, new);
  31. } else temp = xstrdup(new);
  32. free (atold);
  33. *old = temp;
  34. }
  35. // iterate through strings in a comma separated list.
  36. // returns start of next entry or NULL if none
  37. // sets *len to length of entry (not including comma)
  38. // advances *list to start of next entry
  39. char *comma_iterate(char **list, int *len)
  40. {
  41. char *start = *list, *end;
  42. if (!*list || !**list) return 0;
  43. if (!(end = strchr(*list, ','))) {
  44. *len = strlen(*list);
  45. *list = 0;
  46. } else *list += (*len = end-start)+1;
  47. return start;
  48. }
  49. // Check all instances of opt and "no"opt in optlist, return true if opt
  50. // found and last instance wasn't no. If clean, remove each instance from list.
  51. int comma_scan(char *optlist, char *opt, int clean)
  52. {
  53. int optlen = strlen(opt), len, no, got = 0;
  54. if (optlist) for (;;) {
  55. char *s = comma_iterate(&optlist, &len);
  56. if (!s) break;
  57. no = 2*(*s == 'n' && s[1] == 'o');
  58. if (optlen == len-no && !strncmp(opt, s+no, optlen)) {
  59. got = !no;
  60. if (clean) {
  61. if (optlist) memmove(s, optlist, strlen(optlist)+1);
  62. else *s = 0;
  63. }
  64. }
  65. }
  66. return got;
  67. }
  68. // return true if all scanlist options enabled in optlist
  69. int comma_scanall(char *optlist, char *scanlist)
  70. {
  71. int i = 1;
  72. while (scanlist && *scanlist) {
  73. char *opt = comma_iterate(&scanlist, &i), *s = xstrndup(opt, i);
  74. i = comma_scan(optlist, s, 0);
  75. free(s);
  76. if (!i) break;
  77. }
  78. return i;
  79. }
  80. // Returns true and removes `opt` from `optlist` if present, false otherwise.
  81. // Doesn't have the magic "no" behavior of comma_scan.
  82. int comma_remove(char *optlist, char *opt)
  83. {
  84. int optlen = strlen(opt), len, got = 0;
  85. if (optlist) for (;;) {
  86. char *s = comma_iterate(&optlist, &len);
  87. if (!s) break;
  88. if (optlen == len && !strncmp(opt, s, optlen)) {
  89. got = 1;
  90. if (optlist) memmove(s, optlist, strlen(optlist)+1);
  91. else *s = 0;
  92. }
  93. }
  94. return got;
  95. }