cmdline.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include <misc/memref.h>
  41. typedef struct {
  42. struct ExpArray arr;
  43. size_t n;
  44. } CmdLine;
  45. static int CmdLine_Init (CmdLine *c);
  46. static void CmdLine_Free (CmdLine *c);
  47. static int CmdLine_Append (CmdLine *c, const char *str);
  48. static int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len);
  49. static int CmdLine_AppendNoNullMr (CmdLine *c, MemRef mr);
  50. static int CmdLine_AppendMulti (CmdLine *c, int num, ...);
  51. static int CmdLine_Finish (CmdLine *c);
  52. static char ** CmdLine_Get (CmdLine *c);
  53. static int _CmdLine_finished (CmdLine *c)
  54. {
  55. return (c->n > 0 && ((char **)c->arr.v)[c->n - 1] == NULL);
  56. }
  57. int CmdLine_Init (CmdLine *c)
  58. {
  59. if (!ExpArray_init(&c->arr, sizeof(char *), 16)) {
  60. return 0;
  61. }
  62. c->n = 0;
  63. return 1;
  64. }
  65. void CmdLine_Free (CmdLine *c)
  66. {
  67. for (size_t i = 0; i < c->n; i++) {
  68. free(((char **)c->arr.v)[i]);
  69. }
  70. free(c->arr.v);
  71. }
  72. int CmdLine_Append (CmdLine *c, const char *str)
  73. {
  74. ASSERT(str)
  75. ASSERT(!_CmdLine_finished(c))
  76. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  77. return 0;
  78. }
  79. if (!(((char **)c->arr.v)[c->n] = strdup(str))) {
  80. return 0;
  81. }
  82. c->n++;
  83. return 1;
  84. }
  85. int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len)
  86. {
  87. ASSERT(str)
  88. ASSERT(!memchr(str, '\0', str_len))
  89. ASSERT(!_CmdLine_finished(c))
  90. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  91. return 0;
  92. }
  93. if (!(((char **)c->arr.v)[c->n] = b_strdup_bin(str, str_len))) {
  94. return 0;
  95. }
  96. c->n++;
  97. return 1;
  98. }
  99. int CmdLine_AppendNoNullMr (CmdLine *c, MemRef mr)
  100. {
  101. return CmdLine_AppendNoNull(c, mr.ptr, mr.len);
  102. }
  103. int CmdLine_AppendMulti (CmdLine *c, int num, ...)
  104. {
  105. int res = 1;
  106. va_list vl;
  107. va_start(vl, num);
  108. for (int i = 0; i < num; i++) {
  109. const char *str = va_arg(vl, const char *);
  110. if (!CmdLine_Append(c, str)) {
  111. res = 0;
  112. break;
  113. }
  114. }
  115. va_end(vl);
  116. return res;
  117. }
  118. int CmdLine_Finish (CmdLine *c)
  119. {
  120. ASSERT(!_CmdLine_finished(c))
  121. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  122. return 0;
  123. }
  124. ((char **)c->arr.v)[c->n] = NULL;
  125. c->n++;
  126. return 1;
  127. }
  128. char ** CmdLine_Get (CmdLine *c)
  129. {
  130. ASSERT(_CmdLine_finished(c))
  131. return (char **)c->arr.v;
  132. }
  133. #endif