cmdline.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. typedef struct {
  40. struct ExpArray arr;
  41. size_t n;
  42. } CmdLine;
  43. static int CmdLine_Init (CmdLine *c);
  44. static void CmdLine_Free (CmdLine *c);
  45. static int CmdLine_Append (CmdLine *c, const char *str);
  46. static int CmdLine_AppendMulti (CmdLine *c, int num, ...);
  47. static int CmdLine_Finish (CmdLine *c);
  48. static char ** CmdLine_Get (CmdLine *c);
  49. static int _CmdLine_finished (CmdLine *c)
  50. {
  51. return (c->n > 0 && ((char **)c->arr.v)[c->n - 1] == NULL);
  52. }
  53. int CmdLine_Init (CmdLine *c)
  54. {
  55. if (!ExpArray_init(&c->arr, sizeof(char *), 16)) {
  56. return 0;
  57. }
  58. c->n = 0;
  59. return 1;
  60. }
  61. void CmdLine_Free (CmdLine *c)
  62. {
  63. for (size_t i = 0; i < c->n; i++) {
  64. free(((char **)c->arr.v)[i]);
  65. }
  66. free(c->arr.v);
  67. }
  68. int CmdLine_Append (CmdLine *c, const char *str)
  69. {
  70. ASSERT(str)
  71. ASSERT(!_CmdLine_finished(c))
  72. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  73. return 0;
  74. }
  75. if (!(((char **)c->arr.v)[c->n] = strdup(str))) {
  76. return 0;
  77. }
  78. c->n++;
  79. return 1;
  80. }
  81. int CmdLine_AppendMulti (CmdLine *c, int num, ...)
  82. {
  83. int res = 1;
  84. va_list vl;
  85. va_start(vl, num);
  86. for (int i = 0; i < num; i++) {
  87. const char *str = va_arg(vl, const char *);
  88. if (!CmdLine_Append(c, str)) {
  89. res = 0;
  90. break;
  91. }
  92. }
  93. va_end(vl);
  94. return res;
  95. }
  96. int CmdLine_Finish (CmdLine *c)
  97. {
  98. ASSERT(!_CmdLine_finished(c))
  99. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  100. return 0;
  101. }
  102. ((char **)c->arr.v)[c->n] = NULL;
  103. c->n++;
  104. return 1;
  105. }
  106. char ** CmdLine_Get (CmdLine *c)
  107. {
  108. ASSERT(_CmdLine_finished(c))
  109. return (char **)c->arr.v;
  110. }
  111. #endif