]> diplodocus.org Git - nmh/blob - uip/mhfree.c
Another pass at cleaning up (some of) the manpages.
[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 <h/mime.h>
13 #include <h/mhparse.h>
14
15 /* The list of top-level contents to display */
16 CT *cts = NULL;
17
18 /*
19 * prototypes
20 */
21 void free_header (CT);
22 void free_ctinfo (CT);
23 void free_encoding (CT, int);
24 void freects_done (int);
25
26 /*
27 * static prototypes
28 */
29 static void free_text (CT);
30 static void free_multi (CT);
31 static void free_partial (CT);
32 static void free_external (CT);
33 static void free_pmlist (PM *);
34
35
36 /*
37 * Primary routine to free a MIME content structure
38 */
39
40 void
41 free_content (CT ct)
42 {
43 if (!ct)
44 return;
45
46 /*
47 * free all the header fields
48 */
49 free_header (ct);
50
51 if (ct->c_partno) {
52 free (ct->c_partno);
53 ct->c_partno = NULL;
54 }
55
56 if (ct->c_vrsn) {
57 free (ct->c_vrsn);
58 ct->c_vrsn = NULL;
59 }
60
61 if (ct->c_ctline) {
62 free (ct->c_ctline);
63 ct->c_ctline = NULL;
64 }
65
66 free_ctinfo (ct);
67
68 /*
69 * some of the content types have extra
70 * parts which need to be freed.
71 */
72 switch (ct->c_type) {
73 case CT_MULTIPART:
74 free_multi (ct);
75 break;
76
77 case CT_MESSAGE:
78 switch (ct->c_subtype) {
79 case MESSAGE_PARTIAL:
80 free_partial (ct);
81 break;
82
83 case MESSAGE_EXTERNAL:
84 free_external (ct);
85 break;
86 }
87 break;
88
89 case CT_TEXT:
90 free_text (ct);
91 break;
92 }
93
94 if (ct->c_showproc) {
95 free (ct->c_showproc);
96 ct->c_showproc = NULL;
97 }
98 if (ct->c_termproc) {
99 free (ct->c_termproc);
100 ct->c_termproc = NULL;
101 }
102 if (ct->c_storeproc) {
103 free (ct->c_storeproc);
104 ct->c_storeproc = NULL;
105 }
106
107 if (ct->c_celine) {
108 free (ct->c_celine);
109 ct->c_celine = NULL;
110 }
111
112 /* free structures for content encodings */
113 free_encoding (ct, 1);
114
115 if (ct->c_id) {
116 free (ct->c_id);
117 ct->c_id = NULL;
118 }
119 if (ct->c_descr) {
120 free (ct->c_descr);
121 ct->c_descr = NULL;
122 }
123 if (ct->c_dispo) {
124 free (ct->c_dispo);
125 ct->c_dispo = NULL;
126 }
127 if (ct->c_dispo_type) {
128 free (ct->c_dispo_type);
129 ct->c_dispo_type = NULL;
130 }
131 free_pmlist (&ct->c_dispo_first);
132
133 if (ct->c_file) {
134 if (ct->c_unlink)
135 (void) m_unlink (ct->c_file);
136 free (ct->c_file);
137 ct->c_file = NULL;
138 }
139 if (ct->c_fp) {
140 fclose (ct->c_fp);
141 ct->c_fp = NULL;
142 }
143
144 if (ct->c_storage) {
145 free (ct->c_storage);
146 ct->c_storage = NULL;
147 }
148 if (ct->c_folder) {
149 free (ct->c_folder);
150 ct->c_folder = NULL;
151 }
152
153 free (ct);
154 }
155
156
157 /*
158 * Free the linked list of header fields
159 * for this content.
160 */
161
162 void
163 free_header (CT ct)
164 {
165 HF hp1, hp2;
166
167 hp1 = ct->c_first_hf;
168 while (hp1) {
169 hp2 = hp1->next;
170
171 free (hp1->name);
172 free (hp1->value);
173 free (hp1);
174
175 hp1 = hp2;
176 }
177
178 ct->c_first_hf = NULL;
179 ct->c_last_hf = NULL;
180 }
181
182
183 void
184 free_ctinfo (CT ct)
185 {
186 CI ci;
187
188 ci = &ct->c_ctinfo;
189 if (ci->ci_type) {
190 free (ci->ci_type);
191 ci->ci_type = NULL;
192 }
193 if (ci->ci_subtype) {
194 free (ci->ci_subtype);
195 ci->ci_subtype = NULL;
196 }
197 free_pmlist(&ci->ci_first_pm);
198 if (ci->ci_comment) {
199 free (ci->ci_comment);
200 ci->ci_comment = NULL;
201 }
202 if (ci->ci_magic) {
203 free (ci->ci_magic);
204 ci->ci_magic = NULL;
205 }
206 }
207
208
209 static void
210 free_text (CT ct)
211 {
212 struct text *t;
213
214 if (!(t = (struct text *) ct->c_ctparams))
215 return;
216
217 free ((char *) t);
218 ct->c_ctparams = NULL;
219 }
220
221
222 static void
223 free_multi (CT ct)
224 {
225 struct multipart *m;
226 struct part *part, *next;
227
228 if (!(m = (struct multipart *) ct->c_ctparams))
229 return;
230
231 if (m->mp_start)
232 free (m->mp_start);
233 if (m->mp_stop)
234 free (m->mp_stop);
235 free (m->mp_content_before);
236 free (m->mp_content_after);
237
238 for (part = m->mp_parts; part; part = next) {
239 next = part->mp_next;
240 free_content (part->mp_part);
241 free ((char *) part);
242 }
243 m->mp_parts = NULL;
244
245 free ((char *) m);
246 ct->c_ctparams = NULL;
247 }
248
249
250 static void
251 free_partial (CT ct)
252 {
253 struct partial *p;
254
255 if (!(p = (struct partial *) ct->c_ctparams))
256 return;
257
258 if (p->pm_partid)
259 free (p->pm_partid);
260
261 free ((char *) p);
262 ct->c_ctparams = NULL;
263 }
264
265
266 static void
267 free_external (CT ct)
268 {
269 struct exbody *e;
270
271 if (!(e = (struct exbody *) ct->c_ctparams))
272 return;
273
274 free_content (e->eb_content);
275 if (e->eb_body)
276 free (e->eb_body);
277 if (e->eb_url)
278 free (e->eb_url);
279
280 free ((char *) e);
281 ct->c_ctparams = NULL;
282 }
283
284
285 static void
286 free_pmlist (PM *p)
287 {
288 PM pm = *p, pm2;
289
290 while (pm != NULL) {
291 if (pm->pm_name)
292 free (pm->pm_name);
293 if (pm->pm_value)
294 free (pm->pm_value);
295 if (pm->pm_charset)
296 free (pm->pm_charset);
297 if (pm->pm_lang)
298 free (pm->pm_lang);
299 pm2 = pm->pm_next;
300 free(pm);
301 pm = pm2;
302 }
303
304 if (*p)
305 *p = NULL;
306 }
307
308
309 /*
310 * Free data structures related to encoding/decoding
311 * Content-Transfer-Encodings.
312 */
313
314 void
315 free_encoding (CT ct, int toplevel)
316 {
317 CE ce = &ct->c_cefile;
318
319 if (ce->ce_fp) {
320 fclose (ce->ce_fp);
321 ce->ce_fp = NULL;
322 }
323
324 if (ce->ce_file) {
325 if (ce->ce_unlink)
326 (void) m_unlink (ce->ce_file);
327 free (ce->ce_file);
328 ce->ce_file = NULL;
329 }
330
331 if (! toplevel) {
332 ct->c_ceopenfnx = NULL;
333 }
334 }
335
336
337 void
338 freects_done (int status)
339 {
340 CT *ctp;
341
342 for (ctp = cts; ctp && *ctp; ctp++)
343 free_content (*ctp);
344
345 free (cts);
346
347 exit (status);
348 }