cmdline.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @file cmdline.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Command line construction functions.
  32. */
  33. #ifndef BADVPN_MISC_CMDLINE_H
  34. #define BADVPN_MISC_CMDLINE_H
  35. #include <stddef.h>
  36. #include <stdarg.h>
  37. #include <misc/debug.h>
  38. #include <misc/exparray.h>
  39. #include <misc/strdup.h>
  40. typedef struct {
  41. struct ExpArray arr;
  42. size_t n;
  43. } CmdLine;
  44. static int CmdLine_Init (CmdLine *c);
  45. static void CmdLine_Free (CmdLine *c);
  46. static int CmdLine_Append (CmdLine *c, const char *str);
  47. static int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len);
  48. static int CmdLine_AppendMulti (CmdLine *c, int num, ...);
  49. static int CmdLine_Finish (CmdLine *c);
  50. static char ** CmdLine_Get (CmdLine *c);
  51. static int _CmdLine_finished (CmdLine *c)
  52. {
  53. return (c->n > 0 && ((char **)c->arr.v)[c->n - 1] == NULL);
  54. }
  55. int CmdLine_Init (CmdLine *c)
  56. {
  57. if (!ExpArray_init(&c->arr, sizeof(char *), 16)) {
  58. return 0;
  59. }
  60. c->n = 0;
  61. return 1;
  62. }
  63. void CmdLine_Free (CmdLine *c)
  64. {
  65. for (size_t i = 0; i < c->n; i++) {
  66. free(((char **)c->arr.v)[i]);
  67. }
  68. free(c->arr.v);
  69. }
  70. int CmdLine_Append (CmdLine *c, const char *str)
  71. {
  72. ASSERT(str)
  73. ASSERT(!_CmdLine_finished(c))
  74. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  75. return 0;
  76. }
  77. if (!(((char **)c->arr.v)[c->n] = strdup(str))) {
  78. return 0;
  79. }
  80. c->n++;
  81. return 1;
  82. }
  83. int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len)
  84. {
  85. ASSERT(str)
  86. ASSERT(!memchr(str, '\0', str_len))
  87. ASSERT(!_CmdLine_finished(c))
  88. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  89. return 0;
  90. }
  91. if (!(((char **)c->arr.v)[c->n] = b_strdup_bin(str, str_len))) {
  92. return 0;
  93. }
  94. c->n++;
  95. return 1;
  96. }
  97. int CmdLine_AppendMulti (CmdLine *c, int num, ...)
  98. {
  99. int res = 1;
  100. va_list vl;
  101. va_start(vl, num);
  102. for (int i = 0; i < num; i++) {
  103. const char *str = va_arg(vl, const char *);
  104. if (!CmdLine_Append(c, str)) {
  105. res = 0;
  106. break;
  107. }
  108. }
  109. va_end(vl);
  110. return res;
  111. }
  112. int CmdLine_Finish (CmdLine *c)
  113. {
  114. ASSERT(!_CmdLine_finished(c))
  115. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  116. return 0;
  117. }
  118. ((char **)c->arr.v)[c->n] = NULL;
  119. c->n++;
  120. return 1;
  121. }
  122. char ** CmdLine_Get (CmdLine *c)
  123. {
  124. ASSERT(_CmdLine_finished(c))
  125. return (char **)c->arr.v;
  126. }
  127. #endif