]> diplodocus.org Git - nmh/blob - uip/mhfree.c
Formatting cleanup.
[nmh] / uip / mhfree.c
1
2 /*
3 * mhfree.c -- routines to free the data structures used to
4 * -- represent MIME messages
5 *
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <errno.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15
16 /* The list of top-level contents to display */
17 CT *cts = NULL;
18
19 /*
20 * prototypes
21 */
22 void free_content (CT);
23 void free_header (CT);
24 void free_ctinfo (CT);
25 void free_encoding (CT, int);
26 void freects_done (int);
27
28 /*
29 * static prototypes
30 */
31 static void free_text (CT);
32 static void free_multi (CT);
33 static void free_partial (CT);
34 static void free_external (CT);
35
36
37 /*
38 * Primary routine to free a MIME content structure
39 */
40
41 void
42 free_content (CT ct)
43 {
44 if (!ct)
45 return;
46
47 /*
48 * free all the header fields
49 */
50 free_header (ct);
51
52 if (ct->c_partno)
53 free (ct->c_partno);
54
55 if (ct->c_vrsn)
56 free (ct->c_vrsn);
57
58 if (ct->c_ctline)
59 free (ct->c_ctline);
60
61 free_ctinfo (ct);
62
63 /*
64 * some of the content types have extra
65 * parts which need to be freed.
66 */
67 switch (ct->c_type) {
68 case CT_MULTIPART:
69 free_multi (ct);
70 break;
71
72 case CT_MESSAGE:
73 switch (ct->c_subtype) {
74 case MESSAGE_PARTIAL:
75 free_partial (ct);
76 break;
77
78 case MESSAGE_EXTERNAL:
79 free_external (ct);
80 break;
81 }
82 break;
83
84 case CT_TEXT:
85 free_text (ct);
86 break;
87 }
88
89 if (ct->c_showproc)
90 free (ct->c_showproc);
91 if (ct->c_termproc)
92 free (ct->c_termproc);
93 if (ct->c_storeproc)
94 free (ct->c_storeproc);
95
96 if (ct->c_celine)
97 free (ct->c_celine);
98
99 /* free structures for content encodings */
100 free_encoding (ct, 1);
101
102 if (ct->c_id)
103 free (ct->c_id);
104 if (ct->c_descr)
105 free (ct->c_descr);
106 if (ct->c_dispo)
107 free (ct->c_dispo);
108
109 if (ct->c_file) {
110 if (ct->c_unlink)
111 unlink (ct->c_file);
112 free (ct->c_file);
113 }
114 if (ct->c_fp)
115 fclose (ct->c_fp);
116
117 if (ct->c_storage)
118 free (ct->c_storage);
119 if (ct->c_folder)
120 free (ct->c_folder);
121
122 free (ct);
123 }
124
125
126 /*
127 * Free the linked list of header fields
128 * for this content.
129 */
130
131 void
132 free_header (CT ct)
133 {
134 HF hp1, hp2;
135
136 hp1 = ct->c_first_hf;
137 while (hp1) {
138 hp2 = hp1->next;
139
140 free (hp1->name);
141 free (hp1->value);
142 free (hp1);
143
144 hp1 = hp2;
145 }
146
147 ct->c_first_hf = NULL;
148 ct->c_last_hf = NULL;
149 }
150
151
152 void
153 free_ctinfo (CT ct)
154 {
155 char **ap;
156 CI ci;
157
158 ci = &ct->c_ctinfo;
159 if (ci->ci_type) {
160 free (ci->ci_type);
161 ci->ci_type = NULL;
162 }
163 if (ci->ci_subtype) {
164 free (ci->ci_subtype);
165 ci->ci_subtype = NULL;
166 }
167 for (ap = ci->ci_attrs; *ap; ap++) {
168 free (*ap);
169 *ap = NULL;
170 }
171 if (ci->ci_comment) {
172 free (ci->ci_comment);
173 ci->ci_comment = NULL;
174 }
175 if (ci->ci_magic) {
176 free (ci->ci_magic);
177 ci->ci_magic = NULL;
178 }
179 }
180
181
182 static void
183 free_text (CT ct)
184 {
185 struct text *t;
186
187 if (!(t = (struct text *) ct->c_ctparams))
188 return;
189
190 free ((char *) t);
191 ct->c_ctparams = NULL;
192 }
193
194
195 static void
196 free_multi (CT ct)
197 {
198 struct multipart *m;
199 struct part *part, *next;
200
201 if (!(m = (struct multipart *) ct->c_ctparams))
202 return;
203
204 if (m->mp_start)
205 free (m->mp_start);
206 if (m->mp_stop)
207 free (m->mp_stop);
208
209 for (part = m->mp_parts; part; part = next) {
210 next = part->mp_next;
211 free_content (part->mp_part);
212 free ((char *) part);
213 }
214 m->mp_parts = NULL;
215
216 free ((char *) m);
217 ct->c_ctparams = NULL;
218 }
219
220
221 static void
222 free_partial (CT ct)
223 {
224 struct partial *p;
225
226 if (!(p = (struct partial *) ct->c_ctparams))
227 return;
228
229 if (p->pm_partid)
230 free (p->pm_partid);
231
232 free ((char *) p);
233 ct->c_ctparams = NULL;
234 }
235
236
237 static void
238 free_external (CT ct)
239 {
240 struct exbody *e;
241
242 if (!(e = (struct exbody *) ct->c_ctparams))
243 return;
244
245 free_content (e->eb_content);
246 if (e->eb_body)
247 free (e->eb_body);
248
249 free ((char *) e);
250 ct->c_ctparams = NULL;
251 }
252
253
254 /*
255 * Free data structures related to encoding/decoding
256 * Content-Transfer-Encodings.
257 */
258
259 void
260 free_encoding (CT ct, int toplevel)
261 {
262 CE ce;
263
264 if (!(ce = ct->c_cefile))
265 return;
266
267 if (ce->ce_fp) {
268 fclose (ce->ce_fp);
269 ce->ce_fp = NULL;
270 }
271
272 if (ce->ce_file) {
273 if (ce->ce_unlink)
274 unlink (ce->ce_file);
275 free (ce->ce_file);
276 ce->ce_file = NULL;
277 }
278
279 if (toplevel) {
280 free ((char *) ce);
281 ct->c_cefile = NULL;
282 } else {
283 ct->c_ceopenfnx = NULL;
284 }
285 }
286
287
288 void
289 freects_done (int status)
290 {
291 CT *ctp;
292
293 if ((ctp = cts))
294 for (; *ctp; ctp++)
295 free_content (*ctp);
296
297 exit (status);
298 }