remove unused defines in uip/pick.c
[mmh] / uip / mhfree.c
1 /*
2 ** mhfree.c -- routines to free the data structures used to
3 **          -- represent MIME messages
4 **
5 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12 #include <errno.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15 #include <unistd.h>
16
17 /* The list of top-level contents to display */
18 CT *cts = NULL;
19
20 /*
21 ** prototypes
22 */
23 void free_content(CT);
24 void free_header(CT);
25 void free_ctinfo(CT);
26 void free_encoding(CT, int);
27 void freects_done();
28
29 /*
30 ** static prototypes
31 */
32 static void free_text(CT);
33 static void free_multi(CT);
34 static void free_partial(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                 mh_free0(&(ct->c_partno));
54
55         if (ct->c_vrsn)
56                 mh_free0(&(ct->c_vrsn));
57
58         if (ct->c_ctline)
59                 mh_free0(&(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                 if (ct->c_subtype == MESSAGE_PARTIAL) {
74                         free_partial(ct);
75                 }
76                 break;
77
78         case CT_TEXT:
79                 free_text(ct);
80                 break;
81         }
82
83         if (ct->c_charset)
84                 mh_free0(&(ct->c_charset));
85         if (ct->c_showproc)
86                 mh_free0(&(ct->c_showproc));
87         if (ct->c_storeproc)
88                 mh_free0(&(ct->c_storeproc));
89
90         if (ct->c_celine)
91                 mh_free0(&(ct->c_celine));
92
93         /* free structures for content encodings */
94         free_encoding(ct, 1);
95
96         if (ct->c_id)
97                 mh_free0(&(ct->c_id));
98         if (ct->c_descr)
99                 mh_free0(&(ct->c_descr));
100         if (ct->c_dispo)
101                 mh_free0(&(ct->c_dispo));
102
103         if (ct->c_file) {
104                 if (ct->c_unlink)
105                         unlink(ct->c_file);
106                 mh_free0(&(ct->c_file));
107         }
108         if (ct->c_fp)
109                 fclose(ct->c_fp);
110
111         if (ct->c_storage)
112                 mh_free0(&(ct->c_storage));
113         if (ct->c_folder)
114                 mh_free0(&(ct->c_folder));
115
116         mh_free0(&ct);
117 }
118
119
120 /*
121 ** Free the linked list of header fields
122 ** for this content.
123 */
124
125 void
126 free_header(CT ct)
127 {
128         HF hp1, hp2;
129
130         hp1 = ct->c_first_hf;
131         while (hp1) {
132                 hp2 = hp1->next;
133
134                 mh_free0(&(hp1->name));
135                 mh_free0(&(hp1->value));
136                 mh_free0(&hp1);
137
138                 hp1 = hp2;
139         }
140
141         ct->c_first_hf = NULL;
142         ct->c_last_hf  = NULL;
143 }
144
145
146 void
147 free_ctinfo(CT ct)
148 {
149         char **ap;
150         CI ci;
151
152         ci = &ct->c_ctinfo;
153         if (ci->ci_type) {
154                 mh_free0(&(ci->ci_type));
155         }
156         if (ci->ci_subtype) {
157                 mh_free0(&(ci->ci_subtype));
158         }
159         for (ap = ci->ci_attrs; *ap; ap++) {
160                 mh_free0(ap);
161         }
162         if (ci->ci_comment) {
163                 mh_free0(&(ci->ci_comment));
164         }
165         if (ci->ci_magic) {
166                 mh_free0(&(ci->ci_magic));
167         }
168 }
169
170
171 static void
172 free_text(CT ct)
173 {
174         struct text *t;
175
176         if (!(t = (struct text *) ct->c_ctparams))
177                 return;
178
179         mh_free0(&t);
180 }
181
182
183 static void
184 free_multi(CT ct)
185 {
186         struct multipart *m;
187         struct part *part, *next;
188
189         if (!(m = (struct multipart *) ct->c_ctparams))
190                 return;
191
192         if (m->mp_start)
193                 mh_free0(&(m->mp_start));
194         if (m->mp_stop)
195                 mh_free0(&(m->mp_stop));
196
197         for (part = m->mp_parts; part; part = next) {
198                 next = part->mp_next;
199                 free_content(part->mp_part);
200                 mh_free0(&part);
201         }
202
203         mh_free0(&m);
204 }
205
206
207 static void
208 free_partial(CT ct)
209 {
210         struct partial *p;
211
212         if (!(p = (struct partial *) ct->c_ctparams))
213                 return;
214
215         if (p->pm_partid)
216                 mh_free0(&(p->pm_partid));
217
218         mh_free0(&p);
219 }
220
221
222 /*
223 ** Free data structures related to encoding/decoding
224 ** Content-Transfer-Encodings.
225 */
226
227 void
228 free_encoding(CT ct, int toplevel)
229 {
230         CE ce;
231
232         if (!(ce = ct->c_cefile))
233                 return;
234
235         if (ce->ce_fp) {
236                 fclose(ce->ce_fp);
237                 ce->ce_fp = NULL;
238         }
239
240         if (ce->ce_file) {
241                 if (ce->ce_unlink)
242                         unlink(ce->ce_file);
243                 mh_free0(&(ce->ce_file));
244         }
245
246         if (toplevel) {
247                 mh_free0(&ce);
248         } else {
249                 ct->c_ceopenfnx = NULL;
250         }
251 }
252
253
254 void
255 freects_done()
256 {
257         CT *ctp;
258
259         if ((ctp = cts)) {
260                 for (; *ctp; ctp++){
261                         free_content(*ctp);
262                 }
263         }
264 }