sbin/hammer: Make info command print a proper error message
[dragonfly.git] / usr.sbin / installer / libdfui / encode.c
blob7467d6b8b296c64ca1c3c822eb2bc1a277b65084
1 /*
2 * Copyright (c)2004 Cat's Eye Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
35 * encode.c
36 * $Id: encode.c,v 1.12 2005/02/07 06:40:00 cpressey Exp $
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
43 #include <libaura/buffer.h>
45 #define NEEDS_DFUI_STRUCTURE_DEFINITIONS
46 #include "dfui.h"
47 #undef NEEDS_DFUI_STRUCTURE_DEFINITIONS
48 #include "encoding.h"
50 /*** BASIC TYPES ***/
52 void
53 dfui_encode_string(struct aura_buffer *e, const char *str)
55 char fmt[16];
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);
67 void
68 dfui_encode_int(struct aura_buffer *e, int i)
70 char fmt[16];
72 snprintf(fmt, 16, "%d", i);
73 aura_buffer_cat(e, fmt);
74 aura_buffer_cat(e, " ");
77 void
78 dfui_encode_bool(struct aura_buffer *e, int b)
80 if (b)
81 aura_buffer_cat(e, "Y");
82 else
83 aura_buffer_cat(e, "N");
86 /*** FORM TYPES ***/
88 void
89 dfui_encode_info(struct aura_buffer *e, struct dfui_info *i)
91 dfui_encode_string(e, i->name);
92 dfui_encode_string(e, i->short_desc);
93 dfui_encode_string(e, i->long_desc);
96 void
97 dfui_encode_form(struct aura_buffer *e, struct dfui_form *f)
99 aura_buffer_cat(e, "F{");
100 dfui_encode_string(e, f->id);
101 dfui_encode_info(e, f->info);
103 dfui_encode_bool(e, f->multiple);
104 dfui_encode_bool(e, f->extensible);
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);
111 aura_buffer_cat(e, "}");
114 void
115 dfui_encode_fields(struct aura_buffer *e, struct dfui_field *head)
117 struct dfui_field *fi;
119 aura_buffer_cat(e, "f{");
120 for (fi = head; fi != NULL; fi = fi->next) {
121 dfui_encode_field(e, fi);
123 aura_buffer_cat(e, "}");
126 void
127 dfui_encode_field(struct aura_buffer *e, struct dfui_field *fi)
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);
135 void
136 dfui_encode_options(struct aura_buffer *e, struct dfui_option *head)
138 struct dfui_option *o;
140 aura_buffer_cat(e, "O{");
141 for (o = head; o != NULL; o = o->next) {
142 dfui_encode_option(e, o);
144 aura_buffer_cat(e, "}");
147 void
148 dfui_encode_option(struct aura_buffer *e, struct dfui_option *o)
150 dfui_encode_string(e, o->value);
153 void
154 dfui_encode_actions(struct aura_buffer *e, struct dfui_action *head)
156 struct dfui_action *a;
158 aura_buffer_cat(e, "a{");
159 for (a = head; a != NULL; a = a->next) {
160 dfui_encode_action(e, a);
162 aura_buffer_cat(e, "}");
165 void
166 dfui_encode_action(struct aura_buffer *e, struct dfui_action *a)
168 dfui_encode_string(e, a->id);
169 dfui_encode_info(e, a->info);
170 dfui_encode_properties(e, a->property_head);
173 void
174 dfui_encode_datasets(struct aura_buffer *e, struct dfui_dataset *head)
176 struct dfui_dataset *ds;
178 aura_buffer_cat(e, "D{");
179 for (ds = head; ds != NULL; ds = ds->next) {
180 dfui_encode_dataset(e, ds);
182 aura_buffer_cat(e, "}");
185 void
186 dfui_encode_dataset(struct aura_buffer *e, struct dfui_dataset *ds)
188 dfui_encode_celldatas(e, ds->celldata_head);
191 void
192 dfui_encode_celldatas(struct aura_buffer *e, struct dfui_celldata *c)
194 aura_buffer_cat(e, "d{");
195 while (c != NULL) {
196 dfui_encode_celldata(e, c);
197 c = c->next;
199 aura_buffer_cat(e, "}");
202 void
203 dfui_encode_celldata(struct aura_buffer *e, struct dfui_celldata *c)
205 dfui_encode_string(e, c->field_id);
206 dfui_encode_string(e, c->value);
209 void
210 dfui_encode_properties(struct aura_buffer *e, struct dfui_property *h)
212 aura_buffer_cat(e, "p{");
213 while (h != NULL) {
214 dfui_encode_property(e, h);
215 h = h->next;
217 aura_buffer_cat(e, "}");
220 void
221 dfui_encode_property(struct aura_buffer *e, struct dfui_property *h)
223 dfui_encode_string(e, h->name);
224 dfui_encode_string(e, h->value);
227 void
228 dfui_encode_response(struct aura_buffer *e, struct dfui_response *r)
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, "}");
239 void
240 dfui_encode_progress(struct aura_buffer *e, struct dfui_progress *pr)
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);