xref: /dragonfly/usr.sbin/installer/libdfui/encode.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1 /*
2  * Copyright (c)2004 Cat's Eye Technologies.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  *   Neither the name of Cat's Eye Technologies nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * encode.c
36  * $Id: encode.c,v 1.12 2005/02/07 06:40:00 cpressey Exp $
37  */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <libaura/buffer.h>
44 
45 #define   NEEDS_DFUI_STRUCTURE_DEFINITIONS
46 #include "dfui.h"
47 #undef    NEEDS_DFUI_STRUCTURE_DEFINITIONS
48 #include "encoding.h"
49 
50 /*** BASIC TYPES ***/
51 
52 void
dfui_encode_string(struct aura_buffer * e,const char * str)53 dfui_encode_string(struct aura_buffer *e, const char *str)
54 {
55           char fmt[16];
56 
57           if (str == NULL) {
58                     aura_buffer_cat(e, "0:");
59           } else {
60                     snprintf(fmt, 16, "%zu", strlen(str));
61                     aura_buffer_cat(e, fmt);
62                     aura_buffer_cat(e, ":");
63                     aura_buffer_cat(e, str);
64           }
65 }
66 
67 void
dfui_encode_int(struct aura_buffer * e,int i)68 dfui_encode_int(struct aura_buffer *e, int i)
69 {
70           char fmt[16];
71 
72           snprintf(fmt, 16, "%d", i);
73           aura_buffer_cat(e, fmt);
74           aura_buffer_cat(e, " ");
75 }
76 
77 void
dfui_encode_bool(struct aura_buffer * e,int b)78 dfui_encode_bool(struct aura_buffer *e, int b)
79 {
80           if (b)
81                     aura_buffer_cat(e, "Y");
82           else
83                     aura_buffer_cat(e, "N");
84 }
85 
86 /*** FORM TYPES ***/
87 
88 void
dfui_encode_info(struct aura_buffer * e,struct dfui_info * i)89 dfui_encode_info(struct aura_buffer *e, struct dfui_info *i)
90 {
91           dfui_encode_string(e, i->name);
92           dfui_encode_string(e, i->short_desc);
93           dfui_encode_string(e, i->long_desc);
94 }
95 
96 void
dfui_encode_form(struct aura_buffer * e,struct dfui_form * f)97 dfui_encode_form(struct aura_buffer *e, struct dfui_form *f)
98 {
99           aura_buffer_cat(e, "F{");
100           dfui_encode_string(e, f->id);
101           dfui_encode_info(e, f->info);
102 
103           dfui_encode_bool(e, f->multiple);
104           dfui_encode_bool(e, f->extensible);
105 
106           dfui_encode_fields(e, f->field_head);
107           dfui_encode_actions(e, f->action_head);
108           dfui_encode_datasets(e, f->dataset_head);
109           dfui_encode_properties(e, f->property_head);
110 
111           aura_buffer_cat(e, "}");
112 }
113 
114 void
dfui_encode_fields(struct aura_buffer * e,struct dfui_field * head)115 dfui_encode_fields(struct aura_buffer *e, struct dfui_field *head)
116 {
117           struct dfui_field *fi;
118 
119           aura_buffer_cat(e, "f{");
120           for (fi = head; fi != NULL; fi = fi->next) {
121                     dfui_encode_field(e, fi);
122           }
123           aura_buffer_cat(e, "}");
124 }
125 
126 void
dfui_encode_field(struct aura_buffer * e,struct dfui_field * fi)127 dfui_encode_field(struct aura_buffer *e, struct dfui_field *fi)
128 {
129           dfui_encode_string(e, fi->id);
130           dfui_encode_info(e, fi->info);
131           dfui_encode_options(e, fi->option_head);
132           dfui_encode_properties(e, fi->property_head);
133 }
134 
135 void
dfui_encode_options(struct aura_buffer * e,struct dfui_option * head)136 dfui_encode_options(struct aura_buffer *e, struct dfui_option *head)
137 {
138           struct dfui_option *o;
139 
140           aura_buffer_cat(e, "O{");
141           for (o = head; o != NULL; o = o->next) {
142                     dfui_encode_option(e, o);
143           }
144           aura_buffer_cat(e, "}");
145 }
146 
147 void
dfui_encode_option(struct aura_buffer * e,struct dfui_option * o)148 dfui_encode_option(struct aura_buffer *e, struct dfui_option *o)
149 {
150           dfui_encode_string(e, o->value);
151 }
152 
153 void
dfui_encode_actions(struct aura_buffer * e,struct dfui_action * head)154 dfui_encode_actions(struct aura_buffer *e, struct dfui_action *head)
155 {
156           struct dfui_action *a;
157 
158           aura_buffer_cat(e, "a{");
159           for (a = head; a != NULL; a = a->next) {
160                     dfui_encode_action(e, a);
161           }
162           aura_buffer_cat(e, "}");
163 }
164 
165 void
dfui_encode_action(struct aura_buffer * e,struct dfui_action * a)166 dfui_encode_action(struct aura_buffer *e, struct dfui_action *a)
167 {
168           dfui_encode_string(e, a->id);
169           dfui_encode_info(e, a->info);
170           dfui_encode_properties(e, a->property_head);
171 }
172 
173 void
dfui_encode_datasets(struct aura_buffer * e,struct dfui_dataset * head)174 dfui_encode_datasets(struct aura_buffer *e, struct dfui_dataset *head)
175 {
176           struct dfui_dataset *ds;
177 
178           aura_buffer_cat(e, "D{");
179           for (ds = head; ds != NULL; ds = ds->next) {
180                     dfui_encode_dataset(e, ds);
181           }
182           aura_buffer_cat(e, "}");
183 }
184 
185 void
dfui_encode_dataset(struct aura_buffer * e,struct dfui_dataset * ds)186 dfui_encode_dataset(struct aura_buffer *e, struct dfui_dataset *ds)
187 {
188           dfui_encode_celldatas(e, ds->celldata_head);
189 }
190 
191 void
dfui_encode_celldatas(struct aura_buffer * e,struct dfui_celldata * c)192 dfui_encode_celldatas(struct aura_buffer *e, struct dfui_celldata *c)
193 {
194           aura_buffer_cat(e, "d{");
195           while (c != NULL) {
196                     dfui_encode_celldata(e, c);
197                     c = c->next;
198           }
199           aura_buffer_cat(e, "}");
200 }
201 
202 void
dfui_encode_celldata(struct aura_buffer * e,struct dfui_celldata * c)203 dfui_encode_celldata(struct aura_buffer *e, struct dfui_celldata *c)
204 {
205           dfui_encode_string(e, c->field_id);
206           dfui_encode_string(e, c->value);
207 }
208 
209 void
dfui_encode_properties(struct aura_buffer * e,struct dfui_property * h)210 dfui_encode_properties(struct aura_buffer *e, struct dfui_property *h)
211 {
212           aura_buffer_cat(e, "p{");
213           while (h != NULL) {
214                     dfui_encode_property(e, h);
215                     h = h->next;
216           }
217           aura_buffer_cat(e, "}");
218 }
219 
220 void
dfui_encode_property(struct aura_buffer * e,struct dfui_property * h)221 dfui_encode_property(struct aura_buffer *e, struct dfui_property *h)
222 {
223           dfui_encode_string(e, h->name);
224           dfui_encode_string(e, h->value);
225 }
226 
227 void
dfui_encode_response(struct aura_buffer * e,struct dfui_response * r)228 dfui_encode_response(struct aura_buffer *e, struct dfui_response *r)
229 {
230           if (r) {
231                     aura_buffer_cat(e, "R{");
232                     dfui_encode_string(e, r->form_id);
233                     dfui_encode_string(e, r->action_id);
234                     dfui_encode_datasets(e, r->dataset_head);
235                     aura_buffer_cat(e, "}");
236           }
237 }
238 
239 void
dfui_encode_progress(struct aura_buffer * e,struct dfui_progress * pr)240 dfui_encode_progress(struct aura_buffer *e, struct dfui_progress *pr)
241 {
242           dfui_encode_info(e, pr->info);
243           dfui_encode_int(e, pr->amount);
244           dfui_encode_int(e, pr->streaming);
245           dfui_encode_string(e, pr->msg_line);
246 }
247