cmdline.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @file cmdline.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Command line construction functions.
  25. */
  26. #ifndef BADVPN_MISC_CMDLINE_H
  27. #define BADVPN_MISC_CMDLINE_H
  28. #include <stddef.h>
  29. #include <stdarg.h>
  30. #include <misc/debug.h>
  31. #include <misc/exparray.h>
  32. typedef struct {
  33. struct ExpArray arr;
  34. size_t n;
  35. } CmdLine;
  36. static int CmdLine_Init (CmdLine *c);
  37. static void CmdLine_Free (CmdLine *c);
  38. static int CmdLine_Append (CmdLine *c, const char *str);
  39. static int CmdLine_AppendMulti (CmdLine *c, int num, ...);
  40. static int CmdLine_Finish (CmdLine *c);
  41. static char ** CmdLine_Get (CmdLine *c);
  42. static int _CmdLine_finished (CmdLine *c)
  43. {
  44. return (c->n > 0 && ((char **)c->arr.v)[c->n - 1] == NULL);
  45. }
  46. int CmdLine_Init (CmdLine *c)
  47. {
  48. if (!ExpArray_init(&c->arr, sizeof(char *), 16)) {
  49. return 0;
  50. }
  51. c->n = 0;
  52. return 1;
  53. }
  54. void CmdLine_Free (CmdLine *c)
  55. {
  56. for (size_t i = 0; i < c->n; i++) {
  57. free(((char **)c->arr.v)[i]);
  58. }
  59. free(c->arr.v);
  60. }
  61. int CmdLine_Append (CmdLine *c, const char *str)
  62. {
  63. ASSERT(str)
  64. ASSERT(!_CmdLine_finished(c))
  65. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  66. return 0;
  67. }
  68. if (!(((char **)c->arr.v)[c->n] = strdup(str))) {
  69. return 0;
  70. }
  71. c->n++;
  72. return 1;
  73. }
  74. int CmdLine_AppendMulti (CmdLine *c, int num, ...)
  75. {
  76. int res = 1;
  77. va_list vl;
  78. va_start(vl, num);
  79. for (int i = 0; i < num; i++) {
  80. const char *str = va_arg(vl, const char *);
  81. if (!CmdLine_Append(c, str)) {
  82. res = 0;
  83. break;
  84. }
  85. }
  86. va_end(vl);
  87. return res;
  88. }
  89. int CmdLine_Finish (CmdLine *c)
  90. {
  91. ASSERT(!_CmdLine_finished(c))
  92. if (!ExpArray_resize(&c->arr, c->n + 1)) {
  93. return 0;
  94. }
  95. ((char **)c->arr.v)[c->n] = NULL;
  96. c->n++;
  97. return 1;
  98. }
  99. char ** CmdLine_Get (CmdLine *c)
  100. {
  101. ASSERT(_CmdLine_finished(c))
  102. return (char **)c->arr.v;
  103. }
  104. #endif