Merge branch 'ical'
[alpine.git] / pith / state.c
blobef361957bbf8459edfb3024fed81e1bfe60e8b24
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: state.c 1074 2008-06-04 00:08:43Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2013-2017 Eduardo Chappa
8 * Copyright 2006-2008 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 /*======================================================================
20 state.c
21 Implements the Pine state management routines
22 ====*/
25 #include "../pith/headers.h"
26 #include "../pith/state.h"
27 #include "../pith/conf.h"
28 #include "../pith/init.h"
29 #include "../pith/sort.h"
30 #include "../pith/atttype.h"
31 #include "../pith/util.h"
32 #include "../pith/mailindx.h"
33 #include "../pith/remote.h"
34 #include "../pith/list.h"
35 #include "../pith/smime.h"
39 * Globals referenced throughout pine...
41 struct pine *ps_global; /* THE global variable! */
43 #ifdef DEBUG
45 * Debug level and output file defined here, referenced globally.
46 * The debug file is opened and initialized below...
48 int debug = DEFAULT_DEBUG;
49 #endif
52 /*----------------------------------------------------------------------
53 General use big buffer. It is used in the following places:
54 compose_mail: while parsing header of postponed message
55 append_message2: while writing header into folder
56 q_status_messageX: while doing printf formatting
57 addr_book: Used to return expanded address in. (Can only use here
58 because mm_log doesn't q_status on PARSE errors !)
59 alpine.c: When address specified on command line
60 init.c: When expanding variable values
61 and many many more...
63 ----*/
64 char tmp_20k_buf[SIZEOF_20KBUF];
68 * new_pine_struct - allocate and fill in with default values a new pine struct
70 struct pine *
71 new_pine_struct(void)
73 struct pine *p;
75 p = (struct pine *)fs_get(sizeof (struct pine));
76 memset((void *) p, 0, sizeof(struct pine));
77 p->def_sort = SortArrival;
78 p->sort_types[0] = SortSubject;
79 p->sort_types[1] = SortArrival;
80 p->sort_types[2] = SortFrom;
81 p->sort_types[3] = SortTo;
82 p->sort_types[4] = SortCc;
83 p->sort_types[5] = SortDate;
84 p->sort_types[6] = SortSize;
85 p->sort_types[7] = SortSubject2;
86 p->sort_types[8] = SortScore;
87 p->sort_types[9] = SortThread;
88 p->sort_types[10] = EndofList;
89 #ifdef SMIME
91 * We need to have access to p->smime even before calling
92 * smime_init() so that we can set do_encrypt and do_sign.
94 p->smime = new_smime_struct();
95 #endif /* SMIME */
96 p->atmts = (ATTACH_S *) fs_get(sizeof(ATTACH_S));
97 p->atmts_allocated = 1;
98 p->atmts->description = NULL;
99 p->low_speed = 1;
100 p->init_context = -1;
101 /* msgno_init(&p->msgmap, 0L, SortArrival, 0);*/
102 init_init_vars(p);
104 return(p);
110 * free_pine_struct -- free up allocated data in pine struct and then the
111 * struct itself
113 void
114 free_pine_struct(struct pine **pps)
116 if(!(pps && (*pps)))
117 return;
119 if((*pps)->id != NULL)
120 mail_free_idlist(&(*pps)->id);
122 if((*pps)->hostname != NULL)
123 fs_give((void **)&(*pps)->hostname);
125 if((*pps)->localdomain != NULL)
126 fs_give((void **)&(*pps)->localdomain);
128 if((*pps)->ttyo != NULL)
129 fs_give((void **)&(*pps)->ttyo);
131 if((*pps)->home_dir != NULL)
132 fs_give((void **)&(*pps)->home_dir);
134 if((*pps)->folders_dir != NULL)
135 fs_give((void **)&(*pps)->folders_dir);
137 if((*pps)->ui.homedir)
138 fs_give((void **)&(*pps)->ui.homedir);
140 if((*pps)->ui.login)
141 fs_give((void **)&(*pps)->ui.login);
143 if((*pps)->ui.fullname)
144 fs_give((void **)&(*pps)->ui.fullname);
146 free_index_format(&(*pps)->index_disp_format);
148 if((*pps)->conv_table){
149 if((*pps)->conv_table->table)
150 fs_give((void **) &(*pps)->conv_table->table);
152 if((*pps)->conv_table->from_charset)
153 fs_give((void **) &(*pps)->conv_table->from_charset);
155 if((*pps)->conv_table->to_charset)
156 fs_give((void **) &(*pps)->conv_table->to_charset);
158 fs_give((void **)&(*pps)->conv_table);
161 if((*pps)->pinerc)
162 fs_give((void **)&(*pps)->pinerc);
164 #if defined(DOS) || defined(OS2)
165 if((*pps)->pine_dir)
166 fs_give((void **)&(*pps)->pine_dir);
168 if((*pps)->aux_files_dir)
169 fs_give((void **)&(*pps)->aux_files_dir);
170 #endif
172 if((*pps)->display_charmap)
173 fs_give((void **)&(*pps)->display_charmap);
175 if((*pps)->keyboard_charmap)
176 fs_give((void **)&(*pps)->keyboard_charmap);
178 if((*pps)->posting_charmap)
179 fs_give((void **)&(*pps)->posting_charmap);
181 #ifdef SMIME
182 if((*pps)->pwdcertdir)
183 fs_give((void **)&(*pps)->pwdcertdir);
185 if((*pps)->pwdcert){
186 PERSONAL_CERT *pc;
188 pc = (PERSONAL_CERT *) (*pps)->pwdcert;
189 free_personal_certs(&pc);
190 (*pps)->pwdcert = NULL;
193 if((*pps)->pwdcertlist){
194 CertList *cert;
196 cert = (CertList *) (*pps)->pwdcertlist;
197 free_certlist(&cert);
198 (*pps)->pwdcertlist = NULL;
201 if((*pps)->backuppassword){
202 CertList *cert;
204 cert = (CertList *) (*pps)->backuppassword;
205 free_certlist(&cert);
206 (*pps)->backuppassword = NULL;
208 #endif /* SMIME */
211 #ifdef PASSFILE
212 if((*pps)->passfile)
213 fs_give((void **)&(*pps)->passfile);
214 #endif /* PASSFILE */
216 if((*pps)->hdr_colors)
217 free_spec_colors(&(*pps)->hdr_colors);
219 if((*pps)->index_token_colors)
220 free_spec_colors(&(*pps)->index_token_colors);
222 if((*pps)->keywords)
223 free_keyword_list(&(*pps)->keywords);
225 if((*pps)->kw_colors)
226 free_spec_colors(&(*pps)->kw_colors);
228 if((*pps)->atmts){
229 int i;
231 for(i = 0; (*pps)->atmts[i].description; i++){
232 fs_give((void **) &(*pps)->atmts[i].description);
233 fs_give((void **) &(*pps)->atmts[i].number);
236 fs_give((void **) &(*pps)->atmts);
239 if((*pps)->msgmap)
240 msgno_give(&(*pps)->msgmap);
242 free_vars(*pps);
244 fs_give((void **) pps);
248 void
249 free_pinerc_strings(struct pine **pps)
251 if((*pps)->prc){
252 if((*pps)->prc->outstanding_pinerc_changes)
253 write_pinerc((*pps), Main, WRP_NONE);
255 if((*pps)->prc->rd)
256 rd_close_remdata(&(*pps)->prc->rd);
258 free_pinerc_s(&(*pps)->prc);
261 if((*pps)->pconf)
262 free_pinerc_s(&(*pps)->pconf);
264 if((*pps)->post_prc){
265 if((*pps)->post_prc->outstanding_pinerc_changes)
266 write_pinerc((*pps), Post, WRP_NONE);
268 if((*pps)->post_prc->rd)
269 rd_close_remdata(&(*pps)->post_prc->rd);
271 free_pinerc_s(&(*pps)->post_prc);
277 * free_vars -- give back resources acquired when we defined the
278 * variables list
280 void
281 free_vars(struct pine *ps)
283 register int i;
285 for(i = 0; ps && i <= V_LAST_VAR; i++)
286 free_variable_values(&ps->vars[i]);
290 void
291 free_variable_values(struct variable *var)
293 if(var){
294 if(var->is_list){
295 free_list_array(&var->current_val.l);
296 free_list_array(&var->main_user_val.l);
297 free_list_array(&var->post_user_val.l);
298 free_list_array(&var->global_val.l);
299 free_list_array(&var->fixed_val.l);
300 free_list_array(&var->cmdline_val.l);
302 else{
303 if(var->current_val.p)
304 fs_give((void **)&var->current_val.p);
305 if(var->main_user_val.p)
306 fs_give((void **)&var->main_user_val.p);
307 if(var->post_user_val.p)
308 fs_give((void **)&var->post_user_val.p);
309 if(var->global_val.p)
310 fs_give((void **)&var->global_val.p);
311 if(var->fixed_val.p)
312 fs_give((void **)&var->fixed_val.p);
313 if(var->cmdline_val.p)
314 fs_give((void **)&var->cmdline_val.p);
320 PINERC_S *
321 new_pinerc_s(char *name)
323 PINERC_S *prc = NULL;
325 if(name){
326 prc = (PINERC_S *)fs_get(sizeof(*prc));
327 memset((void *)prc, 0, sizeof(*prc));
328 prc->name = cpystr(name);
329 if(IS_REMOTE(name))
330 prc->type = RemImap;
331 else
332 prc->type = Loc;
335 return(prc);
339 void
340 free_pinerc_s(PINERC_S **prc)
342 if(prc && *prc){
343 if((*prc)->name)
344 fs_give((void **)&(*prc)->name);
346 if((*prc)->rd)
347 rd_free_remdata(&(*prc)->rd);
349 if((*prc)->pinerc_lines)
350 free_pinerc_lines(&(*prc)->pinerc_lines);
352 fs_give((void **)prc);