forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_parse.c
1743 lines (1538 loc) · 45 KB
/
command_parse.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
* Functions to parse commands in a config file
*
* @authors
* Copyright (C) 1996-2002,2007,2010,2012-2013,2016 Michael R. Elkins <[email protected]>
* Copyright (C) 2004 g10 Code GmbH
* Copyright (C) 2020 R Primus <[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/>.
*/
/**
* @page neo_command_parse Functions to parse commands in a config file
*
* Functions to parse commands in a config file
*/
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "command_parse.h"
#include "imap/lib.h"
#include "menu/lib.h"
#include "init.h"
#include "keymap.h"
#include "mutt_commands.h"
#include "mutt_globals.h"
#include "muttlib.h"
#include "mx.h"
#include "myvar.h"
#include "options.h"
#include "version.h"
#ifdef USE_INOTIFY
#include "monitor.h"
#endif
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
/* LIFO designed to contain the list of config files that have been sourced and
* avoid cyclic sourcing */
static struct ListHead MuttrcStack = STAILQ_HEAD_INITIALIZER(MuttrcStack);
#define MAX_ERRS 128
/**
* enum GroupState - Type of email address group
*/
enum GroupState
{
GS_NONE, ///< Group is missing an argument
GS_RX, ///< Entry is a regular expression
GS_ADDR, ///< Entry is an address
};
/**
* is_function - Is the argument a neomutt function?
* @param name Command name to be searched for
* @retval true Function found
* @retval false Function not found
*/
static bool is_function(const char *name)
{
for (size_t i = 0; MenuNames[i].name; i++)
{
const struct MenuFuncOp *fns = km_get_table(MenuNames[i].value);
if (!fns)
continue;
for (int j = 0; fns[j].name; j++)
if (mutt_str_equal(name, fns[j].name))
return true;
}
return false;
}
/**
* parse_grouplist - Parse a group context
* @param gl GroupList to add to
* @param buf Temporary Buffer space
* @param s Buffer containing string to be parsed
* @param err Buffer for error messages
* @retval 0 Success
* @retval -1 Error
*/
int parse_grouplist(struct GroupList *gl, struct Buffer *buf, struct Buffer *s,
struct Buffer *err)
{
while (mutt_istr_equal(buf->data, "-group"))
{
if (!MoreArgs(s))
{
mutt_buffer_strcpy(err, _("-group: no group name"));
return -1;
}
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
mutt_grouplist_add(gl, mutt_pattern_group(buf->data));
if (!MoreArgs(s))
{
mutt_buffer_strcpy(err, _("out of arguments"));
return -1;
}
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
}
return 0;
}
/**
* mutt_parse_rc_line_cwd - Parse and run a muttrc line in a relative directory
* @param line Line to be parsed
* @param cwd File relative where to run the line
* @param err Where to write error messages
* @retval #CommandResult Result e.g. #MUTT_CMD_SUCCESS
*/
enum CommandResult mutt_parse_rc_line_cwd(const char *line, char *cwd, struct Buffer *err)
{
mutt_list_insert_head(&MuttrcStack, mutt_str_dup(NONULL(cwd)));
enum CommandResult ret = mutt_parse_rc_line(line, err);
struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
STAILQ_REMOVE_HEAD(&MuttrcStack, entries);
FREE(&np->data);
FREE(&np);
return ret;
}
/**
* mutt_get_sourced_cwd - Get the current file path that is being parsed
* @retval ptr File path that is being parsed or cwd at runtime
*
* @note Caller is responsible for freeing returned string
*/
char *mutt_get_sourced_cwd(void)
{
struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
if (np && np->data)
return mutt_str_dup(np->data);
// stack is empty, return our own dummy file relative to cwd
struct Buffer *cwd = mutt_buffer_pool_get();
mutt_path_getcwd(cwd);
mutt_buffer_addstr(cwd, "/dummy.rc");
char *ret = mutt_buffer_strdup(cwd);
mutt_buffer_pool_release(&cwd);
return ret;
}
/**
* source_rc - Read an initialization file
* @param rcfile_path Path to initialization file
* @param err Buffer for error messages
* @retval <0 NeoMutt should pause to let the user know
*/
int source_rc(const char *rcfile_path, struct Buffer *err)
{
int lineno = 0, rc = 0, warnings = 0;
enum CommandResult line_rc;
struct Buffer *token = NULL, *linebuf = NULL;
char *line = NULL;
char *currentline = NULL;
char rcfile[PATH_MAX] = { 0 };
size_t linelen = 0;
pid_t pid;
mutt_str_copy(rcfile, rcfile_path, sizeof(rcfile));
size_t rcfilelen = mutt_str_len(rcfile);
if (rcfilelen == 0)
return -1;
bool ispipe = rcfile[rcfilelen - 1] == '|';
if (!ispipe)
{
struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
if (!mutt_path_to_absolute(rcfile, np ? NONULL(np->data) : ""))
{
mutt_error(_("Error: Can't build path of '%s'"), rcfile_path);
return -1;
}
STAILQ_FOREACH(np, &MuttrcStack, entries)
{
if (mutt_str_equal(np->data, rcfile))
{
break;
}
}
if (np)
{
mutt_error(_("Error: Cyclic sourcing of configuration file '%s'"), rcfile);
return -1;
}
mutt_list_insert_head(&MuttrcStack, mutt_str_dup(rcfile));
}
mutt_debug(LL_DEBUG2, "Reading configuration file '%s'\n", rcfile);
FILE *fp = mutt_open_read(rcfile, &pid);
if (!fp)
{
mutt_buffer_printf(err, "%s: %s", rcfile, strerror(errno));
return -1;
}
token = mutt_buffer_pool_get();
linebuf = mutt_buffer_pool_get();
while ((line = mutt_file_read_line(line, &linelen, fp, &lineno, MUTT_RL_CONT)) != NULL)
{
const char *const c_config_charset = cs_subset_string(NeoMutt->sub, "config_charset");
const char *const c_charset = cs_subset_string(NeoMutt->sub, "charset");
const bool conv = c_config_charset && c_charset;
if (conv)
{
currentline = mutt_str_dup(line);
if (!currentline)
continue;
mutt_ch_convert_string(¤tline, c_config_charset, c_charset, MUTT_ICONV_NO_FLAGS);
}
else
currentline = line;
mutt_buffer_strcpy(linebuf, currentline);
mutt_buffer_reset(err);
line_rc = mutt_parse_rc_buffer(linebuf, token, err);
if (line_rc == MUTT_CMD_ERROR)
{
mutt_error(_("Error in %s, line %d: %s"), rcfile, lineno, err->data);
if (--rc < -MAX_ERRS)
{
if (conv)
FREE(¤tline);
break;
}
}
else if (line_rc == MUTT_CMD_WARNING)
{
/* Warning */
mutt_warning(_("Warning in %s, line %d: %s"), rcfile, lineno, err->data);
warnings++;
}
else if (line_rc == MUTT_CMD_FINISH)
{
if (conv)
FREE(¤tline);
break; /* Found "finish" command */
}
else
{
if (rc < 0)
rc = -1;
}
if (conv)
FREE(¤tline);
}
FREE(&line);
mutt_file_fclose(&fp);
if (pid != -1)
filter_wait(pid);
if (rc)
{
/* the neomuttrc source keyword */
mutt_buffer_reset(err);
mutt_buffer_printf(err,
(rc >= -MAX_ERRS) ?
_("source: errors in %s") :
_("source: reading aborted due to too many errors in %s"),
rcfile);
rc = -1;
}
else
{
/* Don't alias errors with warnings */
if (warnings > 0)
{
mutt_buffer_printf(err, ngettext("source: %d warning in %s", "source: %d warnings in %s", warnings),
warnings, rcfile);
rc = -2;
}
}
if (!ispipe && !STAILQ_EMPTY(&MuttrcStack))
{
struct ListNode *np = STAILQ_FIRST(&MuttrcStack);
STAILQ_REMOVE_HEAD(&MuttrcStack, entries);
FREE(&np->data);
FREE(&np);
}
mutt_buffer_pool_release(&token);
mutt_buffer_pool_release(&linebuf);
return rc;
}
/**
* parse_cd - Parse the 'cd' command - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult parse_cd(struct Buffer *buf, struct Buffer *s, intptr_t data,
struct Buffer *err)
{
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
mutt_buffer_expand_path(buf);
if (mutt_buffer_len(buf) == 0)
{
if (HomeDir)
mutt_buffer_strcpy(buf, HomeDir);
else
{
mutt_buffer_printf(err, _("%s: too few arguments"), "cd");
return MUTT_CMD_ERROR;
}
}
if (chdir(mutt_buffer_string(buf)) != 0)
{
mutt_buffer_printf(err, "cd: %s", strerror(errno));
return MUTT_CMD_ERROR;
}
return MUTT_CMD_SUCCESS;
}
/**
* parse_echo - Parse the 'echo' command - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult parse_echo(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
if (!MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too few arguments"), "echo");
return MUTT_CMD_WARNING;
}
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
OptForceRefresh = true;
mutt_message("%s", buf->data);
OptForceRefresh = false;
mutt_sleep(0);
return MUTT_CMD_SUCCESS;
}
/**
* parse_finish - Parse the 'finish' command - Implements Command::parse() - @ingroup command_parse
* @retval #MUTT_CMD_FINISH Stop processing the current file
* @retval #MUTT_CMD_WARNING Failed
*
* If the 'finish' command is found, we should stop reading the current file.
*/
enum CommandResult parse_finish(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
if (MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too many arguments"), "finish");
return MUTT_CMD_WARNING;
}
return MUTT_CMD_FINISH;
}
/**
* parse_group - Parse the 'group' and 'ungroup' commands - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult parse_group(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
struct GroupList gl = STAILQ_HEAD_INITIALIZER(gl);
enum GroupState state = GS_NONE;
do
{
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
if (parse_grouplist(&gl, buf, s, err) == -1)
goto bail;
if ((data == MUTT_UNGROUP) && mutt_istr_equal(buf->data, "*"))
{
mutt_grouplist_clear(&gl);
goto out;
}
if (mutt_istr_equal(buf->data, "-rx"))
state = GS_RX;
else if (mutt_istr_equal(buf->data, "-addr"))
state = GS_ADDR;
else
{
switch (state)
{
case GS_NONE:
mutt_buffer_printf(err, _("%sgroup: missing -rx or -addr"),
(data == MUTT_UNGROUP) ? "un" : "");
goto warn;
case GS_RX:
if ((data == MUTT_GROUP) &&
(mutt_grouplist_add_regex(&gl, buf->data, REG_ICASE, err) != 0))
{
goto bail;
}
else if ((data == MUTT_UNGROUP) &&
(mutt_grouplist_remove_regex(&gl, buf->data) < 0))
{
goto bail;
}
break;
case GS_ADDR:
{
char *estr = NULL;
struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
mutt_addrlist_parse2(&al, buf->data);
if (TAILQ_EMPTY(&al))
goto bail;
if (mutt_addrlist_to_intl(&al, &estr))
{
mutt_buffer_printf(err, _("%sgroup: warning: bad IDN '%s'"),
(data == 1) ? "un" : "", estr);
mutt_addrlist_clear(&al);
FREE(&estr);
goto bail;
}
if (data == MUTT_GROUP)
mutt_grouplist_add_addrlist(&gl, &al);
else if (data == MUTT_UNGROUP)
mutt_grouplist_remove_addrlist(&gl, &al);
mutt_addrlist_clear(&al);
break;
}
}
}
} while (MoreArgs(s));
out:
mutt_grouplist_destroy(&gl);
return MUTT_CMD_SUCCESS;
bail:
mutt_grouplist_destroy(&gl);
return MUTT_CMD_ERROR;
warn:
mutt_grouplist_destroy(&gl);
return MUTT_CMD_WARNING;
}
/**
* parse_ifdef - Parse the 'ifdef' and 'ifndef' commands - Implements Command::parse() - @ingroup command_parse
*
* 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 ~/.neomutt/sidebar.rc
*
* If (data == 1) then it means use the 'ifndef' (if-not-defined) command.
* e.g.
* ifndef imap finish
*/
enum CommandResult parse_ifdef(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
// is the item defined as:
bool res = cs_subset_lookup(NeoMutt->sub, buf->data) // a variable?
|| feature_enabled(buf->data) // a compiled-in feature?
|| is_function(buf->data) // a function?
|| mutt_command_get(buf->data) // a command?
|| myvar_get(buf->data) // a my_ variable?
|| mutt_str_getenv(buf->data); // an environment variable?
if (!MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too few arguments"), (data ? "ifndef" : "ifdef"));
return MUTT_CMD_WARNING;
}
mutt_extract_token(buf, s, MUTT_TOKEN_SPACE);
/* ifdef KNOWN_SYMBOL or ifndef UNKNOWN_SYMBOL */
if ((res && (data == 0)) || (!res && (data == 1)))
{
enum CommandResult rc = mutt_parse_rc_line(buf->data, err);
if (rc == MUTT_CMD_ERROR)
{
mutt_error(_("Error: %s"), err->data);
return MUTT_CMD_ERROR;
}
return rc;
}
return MUTT_CMD_SUCCESS;
}
/**
* parse_ignore - Parse the 'ignore' command - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult parse_ignore(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
do
{
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
remove_from_stailq(&UnIgnore, buf->data);
add_to_stailq(&Ignore, buf->data);
} while (MoreArgs(s));
return MUTT_CMD_SUCCESS;
}
/**
* parse_lists - Parse the 'lists' command - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult parse_lists(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
struct GroupList gl = STAILQ_HEAD_INITIALIZER(gl);
do
{
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
if (parse_grouplist(&gl, buf, s, err) == -1)
goto bail;
mutt_regexlist_remove(&UnMailLists, buf->data);
if (mutt_regexlist_add(&MailLists, buf->data, REG_ICASE, err) != 0)
goto bail;
if (mutt_grouplist_add_regex(&gl, buf->data, REG_ICASE, err) != 0)
goto bail;
} while (MoreArgs(s));
mutt_grouplist_destroy(&gl);
return MUTT_CMD_SUCCESS;
bail:
mutt_grouplist_destroy(&gl);
return MUTT_CMD_ERROR;
}
/**
* parse_mailboxes - Parse the 'mailboxes' command - Implements Command::parse() - @ingroup command_parse
*
* This is also used by 'virtual-mailboxes'.
*/
enum CommandResult parse_mailboxes(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
while (MoreArgs(s))
{
struct Mailbox *m = mailbox_new();
if (data & MUTT_NAMED)
{
// This may be empty, e.g. `named-mailboxes "" +inbox`
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
m->name = mutt_buffer_strdup(buf);
}
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
if (mutt_buffer_is_empty(buf))
{
/* Skip empty tokens. */
mailbox_free(&m);
continue;
}
mutt_buffer_strcpy(&m->pathbuf, buf->data);
const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
/* int rc = */ mx_path_canon2(m, c_folder);
if (m->type <= MUTT_UNKNOWN)
{
mutt_error("Unknown Mailbox: %s", m->realpath);
mailbox_free(&m);
return MUTT_CMD_ERROR;
}
bool new_account = false;
struct Account *a = mx_ac_find(m);
if (!a)
{
a = account_new(NULL, NeoMutt->sub);
a->type = m->type;
new_account = true;
}
if (!new_account)
{
struct Mailbox *m_old = mx_mbox_find(a, m->realpath);
if (m_old)
{
if (!m_old->visible)
{
m_old->visible = true;
m_old->gen = mailbox_gen();
}
const bool rename = (data & MUTT_NAMED) && !mutt_str_equal(m_old->name, m->name);
if (rename)
{
mutt_str_replace(&m_old->name, m->name);
}
mailbox_free(&m);
continue;
}
}
if (!mx_ac_add(a, m))
{
mailbox_free(&m);
if (new_account)
{
cs_subset_free(&a->sub);
FREE(&a->name);
notify_free(&a->notify);
FREE(&a);
}
continue;
}
if (new_account)
{
neomutt_account_add(NeoMutt, a);
}
// this is finally a visible mailbox in the sidebar and mailboxes list
m->visible = true;
#ifdef USE_INOTIFY
mutt_monitor_add(m);
#endif
}
return MUTT_CMD_SUCCESS;
}
/**
* parse_my_hdr - Parse the 'my_hdr' command - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult parse_my_hdr(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
mutt_extract_token(buf, s, MUTT_TOKEN_SPACE | MUTT_TOKEN_QUOTE);
char *p = strpbrk(buf->data, ": \t");
if (!p || (*p != ':'))
{
mutt_buffer_strcpy(err, _("invalid header field"));
return MUTT_CMD_WARNING;
}
struct EventHeader ev_h = { buf->data };
struct ListNode *n = header_find(&UserHeader, buf->data);
if (n)
{
header_update(n, buf->data);
mutt_debug(LL_NOTIFY, "NT_HEADER_CHANGE: %s\n", buf->data);
notify_send(NeoMutt->notify, NT_HEADER, NT_HEADER_CHANGE, &ev_h);
}
else
{
header_add(&UserHeader, buf->data);
mutt_debug(LL_NOTIFY, "NT_HEADER_ADD: %s\n", buf->data);
notify_send(NeoMutt->notify, NT_HEADER, NT_HEADER_ADD, &ev_h);
}
return MUTT_CMD_SUCCESS;
}
/**
* parse_set - Parse the 'set' family of commands - Implements Command::parse() - @ingroup command_parse
*
* This is used by 'reset', 'set', 'toggle' and 'unset'.
*/
enum CommandResult parse_set(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
/* The order must match `enum MuttSetCommand` */
static const char *set_commands[] = { "set", "toggle", "unset", "reset" };
int rc = 0;
while (MoreArgs(s))
{
bool prefix = false;
bool query = false;
bool inv = (data == MUTT_SET_INV);
bool reset = (data == MUTT_SET_RESET);
bool unset = (data == MUTT_SET_UNSET);
if (*s->dptr == '?')
{
prefix = true;
query = true;
s->dptr++;
}
else if (mutt_str_startswith(s->dptr, "no"))
{
prefix = true;
unset = !unset;
s->dptr += 2;
}
else if (mutt_str_startswith(s->dptr, "inv"))
{
prefix = true;
inv = !inv;
s->dptr += 3;
}
else if (*s->dptr == '&')
{
prefix = true;
reset = true;
s->dptr++;
}
if (prefix && (data != MUTT_SET_SET))
{
mutt_buffer_printf(err, _("Can't use 'inv', 'no', '&' or '?' with the '%s' command"),
set_commands[data]);
return MUTT_CMD_WARNING;
}
/* get the variable name */
mutt_extract_token(buf, s, MUTT_TOKEN_EQUAL | MUTT_TOKEN_QUESTION | MUTT_TOKEN_PLUS | MUTT_TOKEN_MINUS);
bool bq = false;
bool equals = false;
bool increment = false;
bool decrement = false;
struct HashElem *he = NULL;
bool my = mutt_str_startswith(buf->data, "my_");
if (!my)
{
he = cs_subset_lookup(NeoMutt->sub, buf->data);
if (!he)
{
if (reset && mutt_str_equal(buf->data, "all"))
{
struct HashElem **list = get_elem_list(NeoMutt->sub->cs);
if (!list)
return MUTT_CMD_ERROR;
for (size_t i = 0; list[i]; i++)
cs_subset_he_reset(NeoMutt->sub, list[i], NULL);
FREE(&list);
break;
}
else
{
mutt_buffer_printf(err, _("%s: unknown variable"), buf->data);
return MUTT_CMD_ERROR;
}
}
// Use the correct name if a synonym is used
mutt_buffer_strcpy(buf, he->key.strkey);
bq = ((DTYPE(he->type) == DT_BOOL) || (DTYPE(he->type) == DT_QUAD));
}
if (*s->dptr == '?')
{
if (prefix)
{
mutt_buffer_printf(err, _("Can't use a prefix when querying a variable"));
return MUTT_CMD_WARNING;
}
if (reset || unset || inv)
{
mutt_buffer_printf(err, _("Can't query a variable with the '%s' command"),
set_commands[data]);
return MUTT_CMD_WARNING;
}
query = true;
s->dptr++;
}
else if (*s->dptr == '+' || *s->dptr == '-')
{
if (prefix)
{
mutt_buffer_printf(err, _("Can't use prefix when incrementing or decrementing a variable"));
return MUTT_CMD_WARNING;
}
if (reset || unset || inv)
{
mutt_buffer_printf(err, _("Can't set a variable with the '%s' command"),
set_commands[data]);
return MUTT_CMD_WARNING;
}
if (*s->dptr == '+')
increment = true;
else
decrement = true;
if (my && decrement)
{
mutt_buffer_printf(err, _("Can't decrement a my_ variable"), set_commands[data]);
return MUTT_CMD_WARNING;
}
s->dptr++;
if (*s->dptr == '=')
{
equals = true;
s->dptr++;
}
}
else if (*s->dptr == '=')
{
if (prefix)
{
mutt_buffer_printf(err, _("Can't use prefix when setting a variable"));
return MUTT_CMD_WARNING;
}
if (reset || unset || inv)
{
mutt_buffer_printf(err, _("Can't set a variable with the '%s' command"),
set_commands[data]);
return MUTT_CMD_WARNING;
}
equals = true;
s->dptr++;
}
if (!bq && (inv || (unset && prefix)))
{
if (data == MUTT_SET_SET)
{
mutt_buffer_printf(err, _("Prefixes 'no' and 'inv' may only be used with bool/quad variables"));
}
else
{
mutt_buffer_printf(err, _("Command '%s' can only be used with bool/quad variables"),
set_commands[data]);
}
return MUTT_CMD_WARNING;
}
if (reset)
{
// mutt_buffer_printf(err, "ACT24 reset variable %s", buf->data);
if (he)
{
rc = cs_subset_he_reset(NeoMutt->sub, he, err);
if (CSR_RESULT(rc) != CSR_SUCCESS)
return MUTT_CMD_ERROR;
}
else
{
myvar_del(buf->data);
}
continue;
}
if ((data == MUTT_SET_SET) && !inv && !unset)
{
if (query)
{
// mutt_buffer_printf(err, "ACT08 query variable %s", buf->data);
if (he)
{
mutt_buffer_addstr(err, buf->data);
mutt_buffer_addch(err, '=');
mutt_buffer_reset(buf);
rc = cs_subset_he_string_get(NeoMutt->sub, he, buf);
if (CSR_RESULT(rc) != CSR_SUCCESS)
{
mutt_buffer_addstr(err, buf->data);
return MUTT_CMD_ERROR;
}
if (DTYPE(he->type) == DT_PATH)
mutt_pretty_mailbox(buf->data, buf->dsize);
pretty_var(buf->data, err);
}
else
{
const char *val = myvar_get(buf->data);
if (val)
{
mutt_buffer_addstr(err, buf->data);
mutt_buffer_addch(err, '=');
pretty_var(val, err);
}
else
{
mutt_buffer_printf(err, _("%s: unknown variable"), buf->data);
return MUTT_CMD_ERROR;
}
}
break;
}
else if (equals)
{
// mutt_buffer_printf(err, "ACT11 set variable %s to ", buf->data);
const char *name = NULL;
if (my)
{
name = mutt_str_dup(buf->data);
}
mutt_extract_token(buf, s, MUTT_TOKEN_BACKTICK_VARS);
if (my)
{
assert(!decrement);
if (increment)
{
myvar_append(name, buf->data);
}
else
{
myvar_set(name, buf->data);
}
FREE(&name);
}
else
{
if (DTYPE(he->type) == DT_PATH)
{
if (he->type & (DT_PATH_DIR | DT_PATH_FILE))
mutt_buffer_expand_path(buf);
else
mutt_path_tilde(buf->data, buf->dsize, HomeDir);
}
else if (IS_MAILBOX(he))
{
mutt_buffer_expand_path(buf);
}
else if (IS_COMMAND(he))
{
struct Buffer scratch = mutt_buffer_make(1024);
mutt_buffer_copy(&scratch, buf);
if (!mutt_str_equal(buf->data, "builtin"))
{
mutt_buffer_expand_path(&scratch);
}
mutt_buffer_reset(buf);
mutt_buffer_addstr(buf, mutt_buffer_string(&scratch));
mutt_buffer_dealloc(&scratch);
}
if (increment)
{
rc = cs_subset_he_string_plus_equals(NeoMutt->sub, he, buf->data, err);
}
else if (decrement)
{
rc = cs_subset_he_string_minus_equals(NeoMutt->sub, he, buf->data, err);
}
else
{
rc = cs_subset_he_string_set(NeoMutt->sub, he, buf->data, err);
}
if (CSR_RESULT(rc) != CSR_SUCCESS)
return MUTT_CMD_ERROR;
}
continue;