BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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  
34 #ifndef BADVPN_MISC_CMDLINE_H
35 #define BADVPN_MISC_CMDLINE_H
36  
37 #include <stddef.h>
38 #include <stdarg.h>
39  
40 #include <misc/debug.h>
41 #include <misc/exparray.h>
42 #include <misc/strdup.h>
43 #include <misc/memref.h>
44  
45 typedef struct {
46 struct ExpArray arr;
47 size_t n;
48 } CmdLine;
49  
50 static int CmdLine_Init (CmdLine *c);
51 static void CmdLine_Free (CmdLine *c);
52 static int CmdLine_Append (CmdLine *c, const char *str);
53 static int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len);
54 static int CmdLine_AppendNoNullMr (CmdLine *c, MemRef mr);
55 static int CmdLine_AppendMulti (CmdLine *c, int num, ...);
56 static int CmdLine_Finish (CmdLine *c);
57 static char ** CmdLine_Get (CmdLine *c);
58  
59 static int _CmdLine_finished (CmdLine *c)
60 {
61 return (c->n > 0 && ((char **)c->arr.v)[c->n - 1] == NULL);
62 }
63  
64 int CmdLine_Init (CmdLine *c)
65 {
66 if (!ExpArray_init(&c->arr, sizeof(char *), 16)) {
67 return 0;
68 }
69  
70 c->n = 0;
71  
72 return 1;
73 }
74  
75 void CmdLine_Free (CmdLine *c)
76 {
77 for (size_t i = 0; i < c->n; i++) {
78 free(((char **)c->arr.v)[i]);
79 }
80  
81 free(c->arr.v);
82 }
83  
84 int CmdLine_Append (CmdLine *c, const char *str)
85 {
86 ASSERT(str)
87 ASSERT(!_CmdLine_finished(c))
88  
89 if (!ExpArray_resize(&c->arr, c->n + 1)) {
90 return 0;
91 }
92  
93 if (!(((char **)c->arr.v)[c->n] = strdup(str))) {
94 return 0;
95 }
96  
97 c->n++;
98  
99 return 1;
100 }
101  
102 int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len)
103 {
104 ASSERT(str)
105 ASSERT(!memchr(str, '\0', str_len))
106 ASSERT(!_CmdLine_finished(c))
107  
108 if (!ExpArray_resize(&c->arr, c->n + 1)) {
109 return 0;
110 }
111  
112 if (!(((char **)c->arr.v)[c->n] = b_strdup_bin(str, str_len))) {
113 return 0;
114 }
115  
116 c->n++;
117  
118 return 1;
119 }
120  
121 int CmdLine_AppendNoNullMr (CmdLine *c, MemRef mr)
122 {
123 return CmdLine_AppendNoNull(c, mr.ptr, mr.len);
124 }
125  
126 int CmdLine_AppendMulti (CmdLine *c, int num, ...)
127 {
128 int res = 1;
129  
130 va_list vl;
131 va_start(vl, num);
132  
133 for (int i = 0; i < num; i++) {
134 const char *str = va_arg(vl, const char *);
135 if (!CmdLine_Append(c, str)) {
136 res = 0;
137 break;
138 }
139 }
140  
141 va_end(vl);
142  
143 return res;
144 }
145  
146 int CmdLine_Finish (CmdLine *c)
147 {
148 ASSERT(!_CmdLine_finished(c))
149  
150 if (!ExpArray_resize(&c->arr, c->n + 1)) {
151 return 0;
152 }
153  
154 ((char **)c->arr.v)[c->n] = NULL;
155  
156 c->n++;
157  
158 return 1;
159 }
160  
161 char ** CmdLine_Get (CmdLine *c)
162 {
163 ASSERT(_CmdLine_finished(c))
164  
165 return (char **)c->arr.v;
166 }
167  
168 #endif