BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file build_cmdline.c
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  
30 #include <stdlib.h>
31  
32 #include <misc/debug.h>
33 #include <ncd/extra/value_utils.h>
34  
35 #include "build_cmdline.h"
36  
37 int ncd_build_cmdline (NCDModuleInst *i, int log_channel, NCDValRef cmd_arg, char **out_exec, CmdLine *out_cl)
38 {
39 ASSERT(!NCDVal_IsInvalid(cmd_arg))
40 ASSERT(out_exec)
41 ASSERT(out_cl)
42  
43 if (!NCDVal_IsList(cmd_arg)) {
44 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "wrong type");
45 goto fail0;
46 }
47  
48 size_t count = NCDVal_ListCount(cmd_arg);
49  
50 // read exec
51 if (count == 0) {
52 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "missing executable name");
53 goto fail0;
54 }
55 NCDValRef exec_arg = NCDVal_ListGet(cmd_arg, 0);
56 if (!NCDVal_IsStringNoNulls(exec_arg)) {
57 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "wrong type");
58 goto fail0;
59 }
60 char *exec = ncd_strdup(exec_arg);
61 if (!exec) {
62 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "ncd_strdup failed");
63 goto fail0;
64 }
65  
66 // start cmdline
67 CmdLine cl;
68 if (!CmdLine_Init(&cl)) {
69 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_Init failed");
70 goto fail1;
71 }
72  
73 // add header
74 if (!CmdLine_Append(&cl, exec)) {
75 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_Append failed");
76 goto fail2;
77 }
78  
79 // add additional arguments
80 for (size_t j = 1; j < count; j++) {
81 NCDValRef arg = NCDVal_ListGet(cmd_arg, j);
82  
83 if (!NCDVal_IsStringNoNulls(arg)) {
84 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "wrong type");
85 goto fail2;
86 }
87  
88 if (!CmdLine_AppendNoNullMr(&cl, NCDVal_StringMemRef(arg))) {
89 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_AppendNoNullMr failed");
90 goto fail2;
91 }
92 }
93  
94 // finish
95 if (!CmdLine_Finish(&cl)) {
96 NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_Finish failed");
97 goto fail2;
98 }
99  
100 *out_exec = exec;
101 *out_cl = cl;
102 return 1;
103  
104 fail2:
105 CmdLine_Free(&cl);
106 fail1:
107 free(exec);
108 fail0:
109 return 0;
110 }