forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
4658 lines (4079 loc) · 114 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* Config/command parsing
*
* @authors
* Copyright (C) 1996-2002,2010,2013,2016 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <libgen.h>
#include <limits.h>
#include <pwd.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <wchar.h>
#include "mutt.h"
#include "init.h"
#include "address.h"
#include "alias.h"
#include "charset.h"
#include "context.h"
#include "envelope.h"
#include "filter.h"
#include "group.h"
#include "hcache/hcache.h"
#include "header.h"
#include "history.h"
#include "keymap.h"
#include "lib/lib.h"
#include "list.h"
#include "mailbox.h"
#include "mapping.h"
#include "mbyte.h"
#include "mbyte_table.h"
#include "mutt.h"
#include "mutt_curses.h"
#include "mutt_idna.h"
#include "mutt_menu.h"
#include "mutt_regex.h"
#include "mx.h"
#include "myvar.h"
#include "ncrypt/ncrypt.h"
#include "options.h"
#include "pattern.h"
#include "rfc822.h"
#include "sidebar.h"
#include "version.h"
#ifdef USE_NOTMUCH
#include "mutt_notmuch.h"
#endif
#define CHECK_PAGER \
if ((CurrentMenu == MENU_PAGER) && (idx >= 0) && (MuttVars[idx].flags & R_RESORT)) \
{ \
snprintf(err->data, err->dsize, "%s", _("Not available in this menu.")); \
return -1; \
}
/**
* struct MyVar - A user-set variable
*/
struct MyVar
{
char *name;
char *value;
struct MyVar *next;
};
static struct MyVar *MyVars;
static void myvar_set(const char *var, const char *val)
{
struct MyVar **cur = NULL;
for (cur = &MyVars; *cur; cur = &((*cur)->next))
if (mutt_strcmp((*cur)->name, var) == 0)
break;
if (!*cur)
*cur = safe_calloc(1, sizeof(struct MyVar));
if (!(*cur)->name)
(*cur)->name = safe_strdup(var);
mutt_str_replace(&(*cur)->value, val);
}
static void myvar_del(const char *var)
{
struct MyVar **cur = NULL;
struct MyVar *tmp = NULL;
for (cur = &MyVars; *cur; cur = &((*cur)->next))
if (mutt_strcmp((*cur)->name, var) == 0)
break;
if (*cur)
{
tmp = (*cur)->next;
FREE(&(*cur)->name);
FREE(&(*cur)->value);
FREE(cur);
*cur = tmp;
}
}
#ifdef USE_NOTMUCH
/* List of tags found in last call to mutt_nm_query_complete(). */
static char **nm_tags;
#endif
extern char **envlist;
static void toggle_quadoption(int opt)
{
int n = opt / 4;
int b = (opt % 4) * 2;
QuadOptions[n] ^= (1 << b);
}
static int parse_regex(int idx, struct Buffer *tmp, struct Buffer *err)
{
int e, flags = 0;
const char *p = NULL;
regex_t *rx = NULL;
struct Regex *ptr = (struct Regex *) MuttVars[idx].data;
if (!ptr->pattern || (mutt_strcmp(ptr->pattern, tmp->data) != 0))
{
bool not = false;
/* $mask is case-sensitive */
if (mutt_strcmp(MuttVars[idx].option, "mask") != 0)
flags |= mutt_which_case(tmp->data);
p = tmp->data;
if (mutt_strcmp(MuttVars[idx].option, "mask") == 0)
{
if (*p == '!')
{
not = true;
p++;
}
}
rx = safe_malloc(sizeof(regex_t));
if ((e = REGCOMP(rx, p, flags)) != 0)
{
regerror(e, rx, err->data, err->dsize);
FREE(&rx);
return 0;
}
/* get here only if everything went smoothly */
if (ptr->pattern)
{
FREE(&ptr->pattern);
regfree((regex_t *) ptr->rx);
FREE(&ptr->rx);
}
ptr->pattern = safe_strdup(tmp->data);
ptr->rx = rx;
ptr->not = not;
return 1;
}
return 0;
}
void set_quadoption(int opt, int flag)
{
int n = opt / 4;
int b = (opt % 4) * 2;
QuadOptions[n] &= ~(0x3 << b);
QuadOptions[n] |= (flag & 0x3) << b;
}
int quadoption(int opt)
{
int n = opt / 4;
int b = (opt % 4) * 2;
return (QuadOptions[n] >> b) & 0x3;
}
int query_quadoption(int opt, const char *prompt)
{
int v = quadoption(opt);
switch (v)
{
case MUTT_YES:
case MUTT_NO:
return v;
default:
v = mutt_yesorno(prompt, (v == MUTT_ASKYES));
mutt_window_clearline(MuttMessageWindow, 0);
return v;
}
/* not reached */
}
/**
* mutt_option_index - Find the index (in rc_vars) of a variable name
* @param s Variable name to search for
* @retval -1 on error
* @retval >0 on success
*/
int mutt_option_index(const char *s)
{
for (int i = 0; MuttVars[i].option; i++)
if (mutt_strcmp(s, MuttVars[i].option) == 0)
return (MuttVars[i].type == DT_SYN ?
mutt_option_index((char *) MuttVars[i].data) :
i);
return -1;
}
#ifdef USE_LUA
int mutt_option_to_string(const struct Option *opt, char *val, size_t len)
{
mutt_debug(2, " * mutt_option_to_string(%s)\n", NONULL((char *) opt->data));
int idx = mutt_option_index((const char *) opt->option);
if (idx != -1)
return var_to_string(idx, val, len);
return 0;
}
bool mutt_option_get(const char *s, struct Option *opt)
{
mutt_debug(2, " * mutt_option_get(%s)\n", s);
int idx = mutt_option_index(s);
if (idx != -1)
{
if (opt)
*opt = MuttVars[idx];
return true;
}
if (mutt_strncmp("my_", s, 3) == 0)
{
const char *mv = myvar_get(s);
if (!mv)
return false;
if (opt)
{
memset(opt, 0, sizeof(*opt));
opt->option = s;
opt->type = DT_STR;
}
return true;
}
return false;
}
#endif
static void free_mbchar_table(struct MbCharTable **t)
{
if (!t || !*t)
return;
FREE(&(*t)->chars);
FREE(&(*t)->segmented_str);
FREE(&(*t)->orig_str);
FREE(t);
}
static struct MbCharTable *parse_mbchar_table(const char *s)
{
struct MbCharTable *t = NULL;
size_t slen, k;
mbstate_t mbstate;
char *d = NULL;
t = safe_calloc(1, sizeof(struct MbCharTable));
slen = mutt_strlen(s);
if (!slen)
return t;
t->orig_str = safe_strdup(s);
/* This could be more space efficient. However, being used on tiny
* strings (Tochars and StChars), the overhead is not great. */
t->chars = safe_calloc(slen, sizeof(char *));
d = t->segmented_str = safe_calloc(slen * 2, sizeof(char));
memset(&mbstate, 0, sizeof(mbstate));
while (slen && (k = mbrtowc(NULL, s, slen, &mbstate)))
{
if (k == (size_t)(-1) || k == (size_t)(-2))
{
mutt_debug(
1, "parse_mbchar_table: mbrtowc returned %d converting %s in %s\n",
(k == (size_t)(-1)) ? -1 : -2, s, t->orig_str);
if (k == (size_t)(-1))
memset(&mbstate, 0, sizeof(mbstate));
k = (k == (size_t)(-1)) ? 1 : slen;
}
slen -= k;
t->chars[t->len++] = d;
while (k--)
*d++ = *s++;
*d++ = '\0';
}
return t;
}
static int parse_sort(short *val, const char *s, const struct Mapping *map, struct Buffer *err)
{
int i, flags = 0;
if (mutt_strncmp("reverse-", s, 8) == 0)
{
s += 8;
flags = SORT_REVERSE;
}
if (mutt_strncmp("last-", s, 5) == 0)
{
s += 5;
flags |= SORT_LAST;
}
if ((i = mutt_getvaluebyname(s, map)) == -1)
{
snprintf(err->data, err->dsize, _("%s: unknown sorting method"), s);
return -1;
}
*val = i | flags;
return 0;
}
#ifdef USE_LUA
int mutt_option_set(const struct Option *val, struct Buffer *err)
{
mutt_debug(2, " * mutt_option_set()\n");
int idx = mutt_option_index(val->option);
if (idx != -1)
{
switch (DTYPE(MuttVars[idx].type))
{
case DT_RX:
{
char err_str[LONG_STRING] = "";
struct Buffer err2;
err2.data = err_str;
err2.dsize = sizeof(err_str);
struct Buffer tmp;
tmp.data = (char *) val->data;
tmp.dsize = strlen((char *) val->data);
if (parse_regex(idx, &tmp, &err2))
{
/* $reply_regexp and $alternates require special treatment */
if (Context && Context->msgcount &&
(mutt_strcmp(MuttVars[idx].option, "reply_regexp") == 0))
{
regmatch_t pmatch[1];
#define CUR_ENV Context->hdrs[i]->env
for (int i = 0; i < Context->msgcount; i++)
{
if (CUR_ENV && CUR_ENV->subject)
{
CUR_ENV->real_subj =
(regexec(ReplyRegexp.rx, CUR_ENV->subject, 1, pmatch, 0)) ?
CUR_ENV->subject :
CUR_ENV->subject + pmatch[0].rm_eo;
}
}
#undef CUR_ENV
}
}
else
{
snprintf(err->data, err->dsize, _("%s: Unknown type."), MuttVars[idx].option);
return -1;
}
break;
}
case DT_SORT:
{
const struct Mapping *map = NULL;
switch (MuttVars[idx].type & DT_SUBTYPE_MASK)
{
case DT_SORT_ALIAS:
map = SortAliasMethods;
break;
case DT_SORT_BROWSER:
map = SortBrowserMethods;
break;
case DT_SORT_KEYS:
if ((WithCrypto & APPLICATION_PGP))
map = SortKeyMethods;
break;
case DT_SORT_AUX:
map = SortAuxMethods;
break;
case DT_SORT_SIDEBAR:
map = SortSidebarMethods;
break;
default:
map = SortMethods;
break;
}
if (!map)
{
snprintf(err->data, err->dsize, _("%s: Unknown type."), MuttVars[idx].option);
return -1;
}
if (parse_sort((short *) MuttVars[idx].data, (const char *) val->data, map, err) == -1)
{
return -1;
}
}
break;
case DT_MBCHARTBL:
{
struct MbCharTable **tbl = (struct MbCharTable **) MuttVars[idx].data;
free_mbchar_table(tbl);
*tbl = parse_mbchar_table((const char *) val->data);
}
break;
case DT_ADDR:
rfc822_free_address((struct Address **) MuttVars[idx].data);
*((struct Address **) MuttVars[idx].data) =
rfc822_parse_adrlist(NULL, (const char *) val->data);
break;
case DT_PATH:
{
char scratch[LONG_STRING];
strfcpy(scratch, NONULL((const char *) val->data), sizeof(scratch));
mutt_expand_path(scratch, sizeof(scratch));
/* MuttVars[idx].data is already 'char**' (or some 'void**') or...
* so cast to 'void*' is okay */
FREE((void *) MuttVars[idx].data);
*((char **) MuttVars[idx].data) = safe_strdup(scratch);
break;
}
case DT_STR:
{
/* MuttVars[idx].data is already 'char**' (or some 'void**') or...
* so cast to 'void*' is okay */
FREE((void *) MuttVars[idx].data);
*((char **) MuttVars[idx].data) = safe_strdup((char *) val->data);
}
break;
case DT_BOOL:
if (val->data)
set_option(MuttVars[idx].data);
else
unset_option(MuttVars[idx].data);
break;
case DT_QUAD:
set_quadoption(MuttVars[idx].data, val->data);
break;
case DT_NUM:
*((short *) MuttVars[idx].data) = val->data;
break;
default:
return -1;
}
}
/* set the string as a myvar if it's one */
if (mutt_strncmp("my_", val->option, 3) == 0)
{
myvar_set(val->option, (const char *) val->data);
}
return 0;
}
#endif
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, int flags)
{
if (!dest || !tok)
return -1;
char ch;
char qc = 0; /* quote char */
char *pc = NULL;
/* reset the destination pointer to the beginning of the buffer */
dest->dptr = dest->data;
SKIPWS(tok->dptr);
while ((ch = *tok->dptr))
{
if (!qc)
{
if ((ISSPACE(ch) && !(flags & MUTT_TOKEN_SPACE)) ||
(ch == '#' && !(flags & MUTT_TOKEN_COMMENT)) ||
(ch == '=' && (flags & MUTT_TOKEN_EQUAL)) ||
(ch == ';' && !(flags & MUTT_TOKEN_SEMICOLON)) ||
((flags & MUTT_TOKEN_PATTERN) && strchr("~%=!|", ch)))
break;
}
tok->dptr++;
if (ch == qc)
qc = 0; /* end of quote */
else if (!qc && (ch == '\'' || ch == '"') && !(flags & MUTT_TOKEN_QUOTE))
qc = ch;
else if (ch == '\\' && qc != '\'')
{
if (!*tok->dptr)
return -1; /* premature end of token */
switch (ch = *tok->dptr++)
{
case 'c':
case 'C':
if (!*tok->dptr)
return -1; /* premature end of token */
mutt_buffer_addch(dest, (toupper((unsigned char) *tok->dptr) - '@') & 0x7f);
tok->dptr++;
break;
case 'r':
mutt_buffer_addch(dest, '\r');
break;
case 'n':
mutt_buffer_addch(dest, '\n');
break;
case 't':
mutt_buffer_addch(dest, '\t');
break;
case 'f':
mutt_buffer_addch(dest, '\f');
break;
case 'e':
mutt_buffer_addch(dest, '\033');
break;
default:
if (isdigit((unsigned char) ch) && isdigit((unsigned char) *tok->dptr) &&
isdigit((unsigned char) *(tok->dptr + 1)))
{
mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
tok->dptr += 2;
}
else
mutt_buffer_addch(dest, ch);
}
}
else if (ch == '^' && (flags & MUTT_TOKEN_CONDENSE))
{
if (!*tok->dptr)
return -1; /* premature end of token */
ch = *tok->dptr++;
if (ch == '^')
mutt_buffer_addch(dest, ch);
else if (ch == '[')
mutt_buffer_addch(dest, '\033');
else if (isalpha((unsigned char) ch))
mutt_buffer_addch(dest, toupper((unsigned char) ch) - '@');
else
{
mutt_buffer_addch(dest, '^');
mutt_buffer_addch(dest, ch);
}
}
else if (ch == '`' && (!qc || qc == '"'))
{
FILE *fp = NULL;
pid_t pid;
char *cmd = NULL, *ptr = NULL;
size_t expnlen;
struct Buffer expn;
int line = 0;
pc = tok->dptr;
do
{
if ((pc = strpbrk(pc, "\\`")))
{
/* skip any quoted chars */
if (*pc == '\\')
pc += 2;
}
} while (pc && *pc != '`');
if (!pc)
{
mutt_debug(1, "mutt_get_token: mismatched backticks\n");
return -1;
}
cmd = mutt_substrdup(tok->dptr, pc);
if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0)
{
mutt_debug(1, "mutt_get_token: unable to fork command: %s\n", cmd);
FREE(&cmd);
return -1;
}
FREE(&cmd);
tok->dptr = pc + 1;
/* read line */
mutt_buffer_init(&expn);
expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line, 0);
safe_fclose(&fp);
mutt_wait_filter(pid);
/* if we got output, make a new string consisting of the shell output
plus whatever else was left on the original line */
/* BUT: If this is inside a quoted string, directly add output to
* the token */
if (expn.data && qc)
{
mutt_buffer_addstr(dest, expn.data);
FREE(&expn.data);
}
else if (expn.data)
{
expnlen = mutt_strlen(expn.data);
tok->dsize = expnlen + mutt_strlen(tok->dptr) + 1;
ptr = safe_malloc(tok->dsize);
memcpy(ptr, expn.data, expnlen);
strcpy(ptr + expnlen, tok->dptr);
if (tok->destroy)
FREE(&tok->data);
tok->data = ptr;
tok->dptr = ptr;
tok->destroy = 1; /* mark that the caller should destroy this data */
ptr = NULL;
FREE(&expn.data);
}
}
else if (ch == '$' && (!qc || qc == '"') &&
(*tok->dptr == '{' || isalpha((unsigned char) *tok->dptr)))
{
const char *env = NULL;
char *var = NULL;
int idx;
if (*tok->dptr == '{')
{
tok->dptr++;
if ((pc = strchr(tok->dptr, '}')))
{
var = mutt_substrdup(tok->dptr, pc);
tok->dptr = pc + 1;
}
}
else
{
for (pc = tok->dptr; isalnum((unsigned char) *pc) || *pc == '_'; pc++)
;
var = mutt_substrdup(tok->dptr, pc);
tok->dptr = pc;
}
if (var)
{
if ((env = getenv(var)) || (env = myvar_get(var)))
mutt_buffer_addstr(dest, env);
else if ((idx = mutt_option_index(var)) != -1)
{
/* expand settable mutt variables */
char val[LONG_STRING];
if (var_to_string(idx, val, sizeof(val)))
mutt_buffer_addstr(dest, val);
}
FREE(&var);
}
}
else
mutt_buffer_addch(dest, ch);
}
mutt_buffer_addch(dest, 0); /* terminate the string */
SKIPWS(tok->dptr);
return 0;
}
static void free_opt(struct Option *p)
{
struct Regex *pp = NULL;
switch (DTYPE(p->type))
{
case DT_ADDR:
rfc822_free_address((struct Address **) p->data);
break;
case DT_RX:
pp = (struct Regex *) p->data;
FREE(&pp->pattern);
if (pp->rx)
{
regfree(pp->rx);
FREE(&pp->rx);
}
break;
case DT_PATH:
case DT_STR:
FREE((char **) p->data);
break;
}
}
/**
* mutt_free_opts - clean up before quitting
*/
void mutt_free_opts(void)
{
for (int i = 0; MuttVars[i].option; i++)
free_opt(MuttVars + i);
mutt_free_rx_list(&Alternates);
mutt_free_rx_list(&UnAlternates);
mutt_free_rx_list(&MailLists);
mutt_free_rx_list(&UnMailLists);
mutt_free_rx_list(&SubscribedLists);
mutt_free_rx_list(&UnSubscribedLists);
mutt_free_rx_list(&NoSpamList);
}
static void add_to_stailq(struct ListHead *head, const char *str)
{
/* don't add a NULL or empty string to the list */
if (!str || *str == '\0')
return;
/* check to make sure the item is not already on this list */
struct ListNode *np;
STAILQ_FOREACH(np, head, entries)
{
if (mutt_strcasecmp(str, np->data) == 0)
{
return;
}
}
mutt_list_insert_tail(head, safe_strdup(str));
}
static struct RxList *new_rx_list(void)
{
return safe_calloc(1, sizeof(struct RxList));
}
int mutt_add_to_rx_list(struct RxList **list, const char *s, int flags, struct Buffer *err)
{
struct RxList *t = NULL, *last = NULL;
struct Regex *rx = NULL;
if (!s || !*s)
return 0;
if (!(rx = mutt_compile_regexp(s, flags)))
{
snprintf(err->data, err->dsize, "Bad regexp: %s\n", s);
return -1;
}
/* check to make sure the item is not already on this list */
for (last = *list; last; last = last->next)
{
if (mutt_strcasecmp(rx->pattern, last->rx->pattern) == 0)
{
/* already on the list, so just ignore it */
last = NULL;
break;
}
if (!last->next)
break;
}
if (!*list || last)
{
t = new_rx_list();
t->rx = rx;
if (last)
{
last->next = t;
last = last->next;
}
else
*list = last = t;
}
else /* duplicate */
mutt_free_regexp(&rx);
return 0;
}
static int remove_from_replace_list(struct ReplaceList **list, const char *pat)
{
struct ReplaceList *cur = NULL, *prev = NULL;
int nremoved = 0;
/* Being first is a special case. */
cur = *list;
if (!cur)
return 0;
if (cur->rx && (mutt_strcmp(cur->rx->pattern, pat) == 0))
{
*list = cur->next;
mutt_free_regexp(&cur->rx);
FREE(&cur->template);
FREE(&cur);
return 1;
}
prev = cur;
for (cur = prev->next; cur;)
{
if (mutt_strcmp(cur->rx->pattern, pat) == 0)
{
prev->next = cur->next;
mutt_free_regexp(&cur->rx);
FREE(&cur->template);
FREE(&cur);
cur = prev->next;
nremoved++;
}
else
cur = cur->next;
}
return nremoved;
}
static struct ReplaceList *new_replace_list(void)
{
return safe_calloc(1, sizeof(struct ReplaceList));
}
static int add_to_replace_list(struct ReplaceList **list, const char *pat,
const char *templ, struct Buffer *err)
{
struct ReplaceList *t = NULL, *last = NULL;
struct Regex *rx = NULL;
int n;
const char *p = NULL;
if (!pat || !*pat || !templ)
return 0;
if (!(rx = mutt_compile_regexp(pat, REG_ICASE)))
{
snprintf(err->data, err->dsize, _("Bad regexp: %s"), pat);
return -1;
}
/* check to make sure the item is not already on this list */
for (last = *list; last; last = last->next)
{
if (mutt_strcasecmp(rx->pattern, last->rx->pattern) == 0)
{
/* Already on the list. Formerly we just skipped this case, but
* now we're supporting removals, which means we're supporting
* re-adds conceptually. So we probably want this to imply a
* removal, then do an add. We can achieve the removal by freeing
* the template, and leaving t pointed at the current item.
*/
t = last;
FREE(&t->template);
break;
}
if (!last->next)
break;
}
/* If t is set, it's pointing into an extant ReplaceList* that we want to
* update. Otherwise we want to make a new one to link at the list's end.
*/
if (!t)
{
t = new_replace_list();
t->rx = rx;
rx = NULL;
if (last)
last->next = t;
else
*list = t;
}
else
mutt_free_regexp(&rx);
/* Now t is the ReplaceList* that we want to modify. It is prepared. */
t->template = safe_strdup(templ);
/* Find highest match number in template string */
t->nmatch = 0;
for (p = templ; *p;)
{
if (*p == '%')
{
n = atoi(++p);
if (n > t->nmatch)
t->nmatch = n;
while (*p && isdigit((int) *p))
p++;
}
else
p++;
}
if (t->nmatch > t->rx->rx->re_nsub)
{
snprintf(err->data, err->dsize, "%s", _("Not enough subexpressions for "
"template"));
remove_from_replace_list(list, pat);
return -1;
}
t->nmatch++; /* match 0 is always the whole expr */
return 0;
}
/**
* finish_source - 'finish' command: stop processing current config file
* @param tmp Temporary space shared by all command handlers
* @param s Current line of the config file
* @param data data field from init.h:struct Command
* @param err Buffer for any error message
* @retval 1 Stop processing the current file
* @retval -1 Failed
*
* If the 'finish' command is found, we should stop reading the current file.
*/
static int finish_source(struct Buffer *tmp, struct Buffer *s,
unsigned long data, struct Buffer *err)
{
if (MoreArgs(s))
{
snprintf(err->data, err->dsize, _("finish: too many arguments"));
return -1;
}
return 1;
}
/**
* parse_ifdef - 'ifdef' command: conditional config
* @param tmp Temporary space shared by all command handlers
* @param s Current line of the config file
* @param data data field from init.h:struct Command
* @param err Buffer for any error message
* @retval 0 Success
* @retval -1 Failed
*
* The 'ifdef' command allows conditional elements in the config file.
* If a given variable, function, command or compile-time symbol exists, then
* read the rest of the line of config commands.
* e.g.
* ifdef sidebar source ~/.mutt/sidebar.rc
*
* If (data == 1) then it means use the 'ifndef' (if-not-defined) command.
* e.g.
* ifndef imap finish
*/
static int parse_ifdef(struct Buffer *tmp, struct Buffer *s, unsigned long data,
struct Buffer *err)
{
int i, j;
bool res = 0;
struct Buffer token;
memset(&token, 0, sizeof(token));
mutt_extract_token(tmp, s, 0);
/* is the item defined as a variable? */
res = (mutt_option_index(tmp->data) != -1);
/* is the item a compiled-in feature? */