BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file print.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 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Modules for printing to standard output. |
||
32 | * |
||
33 | * Synopsis: |
||
34 | * print([string str ...]) |
||
35 | * Description: |
||
36 | * On initialization, prints strings to standard output. |
||
37 | * |
||
38 | * Synopsis: |
||
39 | * println([string str ...]) |
||
40 | * Description: |
||
41 | * On initialization, prints strings to standard output, and a newline. |
||
42 | * |
||
43 | * Synopsis: |
||
44 | * rprint([string str ...]) |
||
45 | * Description: |
||
46 | * On deinitialization, prints strings to standard output. |
||
47 | * |
||
48 | * Synopsis: |
||
49 | * rprintln([string str ...]) |
||
50 | * Description: |
||
51 | * On deinitialization, prints strings to standard output, and a newline. |
||
52 | */ |
||
53 | |||
54 | #include <stdlib.h> |
||
55 | #include <stdio.h> |
||
56 | |||
57 | #include <ncd/module_common.h> |
||
58 | |||
59 | #include <generated/blog_channel_ncd_print.h> |
||
60 | |||
61 | struct rprint_instance { |
||
62 | NCDModuleInst *i; |
||
63 | NCDValRef args; |
||
64 | int ln; |
||
65 | }; |
||
66 | |||
67 | static int check_args (NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
68 | { |
||
69 | size_t num_args = NCDVal_ListCount(params->args); |
||
70 | |||
71 | for (size_t j = 0; j < num_args; j++) { |
||
72 | NCDValRef arg = NCDVal_ListGet(params->args, j); |
||
73 | if (!NCDVal_IsString(arg)) { |
||
74 | ModuleLog(i, BLOG_ERROR, "wrong type"); |
||
75 | return 0; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | return 1; |
||
80 | } |
||
81 | |||
82 | static void do_print (NCDModuleInst *i, NCDValRef args, int ln) |
||
83 | { |
||
84 | size_t num_args = NCDVal_ListCount(args); |
||
85 | |||
86 | for (size_t j = 0; j < num_args; j++) { |
||
87 | NCDValRef arg = NCDVal_ListGet(args, j); |
||
88 | ASSERT(NCDVal_IsString(arg)) |
||
89 | |||
90 | MemRef arg_mr = NCDVal_StringMemRef(arg); |
||
91 | |||
92 | size_t pos = 0; |
||
93 | while (pos < arg_mr.len) { |
||
94 | ssize_t res = fwrite(arg_mr.ptr + pos, 1, arg_mr.len - pos, stdout); |
||
95 | if (res <= 0) { |
||
96 | goto out; |
||
97 | } |
||
98 | pos += res; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | out: |
||
103 | if (ln) { |
||
104 | printf("\n"); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | static void rprint_func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int ln) |
||
109 | { |
||
110 | struct rprint_instance *o = vo; |
||
111 | o->i = i; |
||
112 | o->args = params->args; |
||
113 | o->ln = ln; |
||
114 | |||
115 | if (!check_args(i, params)) { |
||
116 | goto fail0; |
||
117 | } |
||
118 | |||
119 | NCDModuleInst_Backend_Up(i); |
||
120 | return; |
||
121 | |||
122 | fail0: |
||
123 | NCDModuleInst_Backend_DeadError(i); |
||
124 | } |
||
125 | |||
126 | static void rprint_func_die (void *vo) |
||
127 | { |
||
128 | struct rprint_instance *o = vo; |
||
129 | |||
130 | do_print(o->i, o->args, o->ln); |
||
131 | |||
132 | NCDModuleInst_Backend_Dead(o->i); |
||
133 | } |
||
134 | |||
135 | static void print_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
136 | { |
||
137 | if (!check_args(i, params)) { |
||
138 | goto fail0; |
||
139 | } |
||
140 | |||
141 | do_print(i, params->args, 0); |
||
142 | |||
143 | NCDModuleInst_Backend_Up(i); |
||
144 | return; |
||
145 | |||
146 | fail0: |
||
147 | NCDModuleInst_Backend_DeadError(i); |
||
148 | } |
||
149 | |||
150 | static void println_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
151 | { |
||
152 | if (!check_args(i, params)) { |
||
153 | goto fail0; |
||
154 | } |
||
155 | |||
156 | do_print(i, params->args, 1); |
||
157 | |||
158 | NCDModuleInst_Backend_Up(i); |
||
159 | return; |
||
160 | |||
161 | fail0: |
||
162 | NCDModuleInst_Backend_DeadError(i); |
||
163 | } |
||
164 | |||
165 | static void rprint_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
166 | { |
||
167 | return rprint_func_new_common(vo, i, params, 0); |
||
168 | } |
||
169 | |||
170 | static void rprintln_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
171 | { |
||
172 | return rprint_func_new_common(vo, i, params, 1); |
||
173 | } |
||
174 | |||
175 | static struct NCDModule modules[] = { |
||
176 | { |
||
177 | .type = "print", |
||
178 | .func_new2 = print_func_new |
||
179 | }, { |
||
180 | .type = "println", |
||
181 | .func_new2 = println_func_new |
||
182 | }, { |
||
183 | .type = "rprint", |
||
184 | .func_new2 = rprint_func_new, |
||
185 | .func_die = rprint_func_die, |
||
186 | .alloc_size = sizeof(struct rprint_instance) |
||
187 | }, { |
||
188 | .type = "rprintln", |
||
189 | .func_new2 = rprintln_func_new, |
||
190 | .func_die = rprint_func_die, |
||
191 | .alloc_size = sizeof(struct rprint_instance) |
||
192 | }, { |
||
193 | .type = NULL |
||
194 | } |
||
195 | }; |
||
196 | |||
197 | const struct NCDModuleGroup ncdmodule_print = { |
||
198 | .modules = modules |
||
199 | }; |