File: compparse.c

package info (click to toggle)
yash 2.60-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,152 kB
  • sloc: ansic: 34,578; makefile: 851; sh: 808; sed: 16
file content (1281 lines) | stat: -rw-r--r-- 38,757 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
/* Yash: yet another shell */
/* compparse.c: simple parser for command line completion */
/* (C) 2007-2024 magicant */

/* 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 "../common.h"
#include "compparse.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
#include "../alias.h"
#include "../expand.h"
#include "../option.h"
#include "../parser.h"
#include "../plist.h"
#include "../strbuf.h"
#include "../util.h"
#include "../variable.h"
#include "../xfnmatch.h"
#include "complete.h"
#include "editing.h"
#include "lineedit.h"


/* This structure contains data used in parsing */
typedef struct cparseinfo_T {
    xwcsbuf_T buf;
    size_t bufindex;
    struct aliaslist_T *aliaslist;
    le_context_T *ctxt;
} cparseinfo_T;
/* The `buf' buffer contains the first `le_main_index' characters of the edit
 * buffer. During parsing, alias substitution may be performed on this buffer.
 * The `bufindex' index indicates the point the parser is currently parsing.
 * The `ctxt' member points to the structure in which the final result is saved.
 */

/* This structure contains data used during parsing */
static cparseinfo_T *pi;

#define BUF   (pi->buf.contents)
#define LEN   (pi->buf.length)
#define INDEX (pi->bufindex)


static void empty_pwords(void);
static void set_pwords(plist_T *pwords)
    __attribute__((nonnull));
static bool cparse_commands(void);
static void skip_blanks(void);
static void skip_blanks_and_newlines(void);
static bool csubstitute_alias(substaliasflags_T flags);
static bool cparse_command(void);
static bool has_token(const wchar_t *token)
    __attribute__((nonnull,pure));
static bool cparse_simple_command(void);
static bool cparse_redirections(void);
static bool ctryparse_assignment(void);
static bool ctryparse_redirect(void);
static bool cparse_for_command(void);
static bool cparse_case_command(void);
static bool cparse_function_definition(void);
static bool ctryparse_word(tildetype_T tilde, le_contexttype_T ctxttype);
static wordunit_T *cparse_word(
        bool testfunc(wchar_t c), tildetype_T tilde, le_contexttype_T ctxttype)
    __attribute__((nonnull,malloc,warn_unused_result));
static bool ctryparse_tilde(void);
static wordunit_T *cparse_special_word_unit(le_contexttype_T ctxttype)
    __attribute__((malloc,warn_unused_result));
static wordunit_T *cparse_paramexp_raw(le_contexttype_T ctxttype)
    __attribute__((malloc,warn_unused_result));
static wordunit_T *cparse_paramexp_in_brace(le_contexttype_T ctxttype)
    __attribute__((malloc,warn_unused_result));
static wordunit_T *cparse_arith(void)
    __attribute__((malloc,warn_unused_result));
static wordunit_T *cparse_cmdsubst_in_paren(void)
    __attribute__((malloc,warn_unused_result));
static wordunit_T *cparse_cmdsubst_in_backquote(void)
    __attribute__((malloc,warn_unused_result));
static bool is_slash_or_closing_brace(wchar_t c)
    __attribute__((const));
static bool is_closing_brace(wchar_t c)
    __attribute__((const));
static bool is_closing_bracket(wchar_t c)
    __attribute__((const));
static bool is_arith_delimiter(wchar_t c)
    __attribute__((pure));
static bool remove_braceexpand(wchar_t *s)
    __attribute__((nonnull));
static bool remove_braceexpand_inner(xwcsbuf_T *buf, size_t *index)
    __attribute__((nonnull));


/* Parses the contents of the edit buffer (`le_main_buffer') from the beginning
 * up to the current cursor position (`le_main_index') and determines the
 * current completion context.
 * The results are returned as a newly malloced `le_context_T' data. */
le_context_T *le_get_context(void)
{
    assert(wcslen(le_main_buffer.contents) == le_main_buffer.length);

    le_context_T *ctxt = xmalloc(sizeof *ctxt);

    cparseinfo_T parseinfo;
    wb_initwithmax(&parseinfo.buf, le_main_index);
    wb_ncat_force(&parseinfo.buf, le_main_buffer.contents, le_main_index);
    parseinfo.bufindex = 0;
    parseinfo.aliaslist = NULL;
    parseinfo.ctxt = ctxt;

    pi = &parseinfo;
    while (!cparse_commands())
        parseinfo.bufindex++;
#ifndef NDEBUG
    pi = NULL;
#endif

    wb_destroy(&parseinfo.buf);
    destroy_aliaslist(parseinfo.aliaslist);

    if (shopt_braceexpand)
        if (remove_braceexpand(ctxt->pattern))
            ctxt->type |= CTXT_EBRACED;
    ctxt->src = unescape(ctxt->pattern);
    if (is_pathname_matching_pattern(ctxt->pattern)) {
        ctxt->substsrc = true;
    } else {
        ctxt->substsrc = false;

        xwcsbuf_T buf;
        wb_initwith(&buf, ctxt->pattern);
        wb_wccat(&buf, L'*');
        ctxt->pattern = wb_towcs(&buf);
    }
    ctxt->origindex = le_main_index;

    return ctxt;
}

/* If `pi->ctxt->pwords' is NULL, assigns a new empty list to it. */
void empty_pwords(void)
{
    if (pi->ctxt->pwords == NULL) {
        pi->ctxt->pwordc = 0;
        pi->ctxt->pwords = xmalloc(1 * sizeof *pi->ctxt->pwords);
        pi->ctxt->pwords[0] = NULL;
    }
}

/* Sets `pi->ctxt->pwords' to the contents of `pwords'. */
void set_pwords(plist_T *pwords)
{
    if (pi->ctxt->pwords == NULL) {
        pi->ctxt->pwordc = pwords->length;
        pi->ctxt->pwords = pl_toary(pwords);
    } else {
        plfree(pl_toary(pwords), free);
    }
}

/* Following parser functions return true iff parsing is finished and the result
 * is saved in the context structure `pi->ctxt'.
 * The result is saved in the following variables of the context structure:
 *     quote, type, pwordc, pwords, pattern, srcindex. */

/* Parses commands from the current position until a right parenthesis (")") is
 * found or the whole line is parsed. */
bool cparse_commands(void)
{
    for (;;) {
        skip_blanks();
        switch (BUF[INDEX]) {
            case L'\n':  case L';':  case L'&':  case L'|':
                INDEX++;
                continue;
            case L')':
                return false;
        }

        if (cparse_command())
            return true;
    }
}

/* Skips blank characters. */
void skip_blanks(void)
{
    while (iswblank(BUF[INDEX]))
        INDEX++;
}

/* Skips blank characters and newlines. */
void skip_blanks_and_newlines(void)
{
    while (BUF[INDEX] == L'\n' || iswblank(BUF[INDEX]))
        INDEX++;
}

/* Performs alias substitution at the current position.
 * See the description of `substitute_alias' for the usage of `flags'.
 * Returns true iff any alias is substituted.
 * If the word at the current INDEX is at the end of BUF, the word is not
 * substituted because it is the word being completed. */
bool csubstitute_alias(substaliasflags_T flags)
{
    return substitute_alias(&pi->buf, INDEX, &pi->aliaslist, flags | AF_NOEOF);
}

/* Parses a command from the current position. */
bool cparse_command(void)
{
    if (BUF[INDEX] == L'(') {
        INDEX++;
        if (cparse_commands())
            return true;
        assert(BUF[INDEX] == L')');
        INDEX++;
        return cparse_redirections();
    } else if (has_token(L"{") || has_token(L"!")) {
        INDEX++;
        return false;
    } else if (has_token(L"if") || has_token(L"do")) {
        INDEX += 2;
        return false;
    } else if (has_token(L"then") || has_token(L"else")
            || has_token(L"elif")) {
        INDEX += 4;
        return false;
    } else if (has_token(L"while") || has_token(L"until")) {
        INDEX += 5;
        return false;
    } else if (has_token(L"}")) {
        INDEX++;
        return cparse_redirections();
    } else if (has_token(L"fi")) {
        INDEX += 2;
        return cparse_redirections();
    } else if (has_token(L"done") || has_token(L"esac")) {
        INDEX += 4;
        return cparse_redirections();
    } else if (has_token(L"for")) {
        return cparse_for_command();
    } else if (has_token(L"case")) {
        return cparse_case_command();
    } else if (!posixly_correct && has_token(L"function")) {
        return cparse_function_definition();
    } else {
        return cparse_simple_command();
    }
}

/* Returns true iff the specified word is at the current position (and it is not
 * at the end of the buffer). */
bool has_token(const wchar_t *token)
{
    const wchar_t *c = matchwcsprefix(BUF + INDEX, token);
    return c != NULL && *c != L'\0' && is_token_delimiter_char(*c);
}

/* Parses a simple command. */
bool cparse_simple_command(void)
{
cparse_simple_command:
    if (csubstitute_alias(AF_NONGLOBAL))
        return false;

    size_t saveindex;
    skip_blanks();
    saveindex = INDEX;
    if (ctryparse_redirect())
        return true;
    if (saveindex != INDEX) {
        skip_blanks();
        goto cparse_simple_command;
    }
    if (ctryparse_assignment())
        return true;
    if (saveindex != INDEX) {
        skip_blanks();
        goto cparse_simple_command;
    }

    plist_T pwords;
    pl_init(&pwords);
    for (;;) {
        switch (BUF[INDEX]) {
            case L'\n':  case L';':  case L'&':  case L'|':
            case L'(':   case L')':
                plfree(pl_toary(&pwords), free);
                return false;
        }

        wordunit_T *w = cparse_word(is_token_delimiter_char, TT_SINGLE,
                pwords.length == 0 ? CTXT_COMMAND : CTXT_ARGUMENT);
        if (w == NULL) {
            set_pwords(&pwords);
            return true;
        } else {
            expand_multiple(w, &pwords);
            wordfree(w);
        }

        do {
            skip_blanks();
            if (ctryparse_redirect()) {
                plfree(pl_toary(&pwords), free);
                return true;
            }
            skip_blanks();
        } while (csubstitute_alias(0));
    }
}

/* Parses redirections as many as possible.
 * Global aliases are substituted if necessary. */
bool cparse_redirections(void)
{
    for (;;) {
        skip_blanks();
        size_t saveindex = INDEX;
        if (ctryparse_redirect())
            return true;
        if (saveindex == INDEX) {
            skip_blanks();
            if (!csubstitute_alias(0))
                return false;
        }
    }
}

/* Parses an assignment if any.
 * `skip_blanks' should be called before this function is called. */
bool ctryparse_assignment(void)
{
    size_t index = INDEX;

    if (BUF[index] == L'=' || iswdigit(BUF[index]))
        return false;
    while (is_name_char(BUF[index]))
        index++;
    if (BUF[index] != L'=')
        return false;
    INDEX = index + 1;

    if (BUF[INDEX] != L'(') {
        /* scalar variable */
        if (ctryparse_word(TT_MULTI, CTXT_ASSIGN)) {
            empty_pwords();
            return true;
        } else {
            return false;
        }
    } else {
        /* parse array contents */
        plist_T pwords;
        pl_init(&pwords);
        INDEX++;
        for (;;) {
            do
                skip_blanks();
            while (csubstitute_alias(0));

            if (BUF[INDEX] != L'\0' && is_token_delimiter_char(BUF[INDEX])) {
                if (BUF[INDEX] == L')')
                    INDEX++;
                plfree(pl_toary(&pwords), free);
                return false;
            }

            wordunit_T *w = cparse_word(
                    is_token_delimiter_char, TT_SINGLE, CTXT_ASSIGN);
            if (w == NULL) {
                set_pwords(&pwords);
                return true;
            } else {
                expand_multiple(w, &pwords);
                wordfree(w);
            }
        }
    }
}

/* Parses a redirection if any.
 * `skip_blanks' should be called before this function is called. */
bool ctryparse_redirect(void)
{
    size_t index = INDEX;
    le_contexttype_T type;
    bool result;

    while (iswdigit(BUF[index]))
        index++;
    switch (BUF[index]) {
    case L'<':
        switch (BUF[index + 1]) {
        case L'<':
            type = CTXT_REDIR;
            switch (BUF[index + 2]) {
            case L'<':  /* here-string */
            case L'-':  /* here-document */
                INDEX = index + 3;  break;
            default:    /* here-document */
                INDEX = index + 2;  break;
            }
            break;
        case L'>':  INDEX = index + 2;  type = CTXT_REDIR;     break;
        case L'&':  INDEX = index + 2;  type = CTXT_REDIR_FD;  break;
        case L'(':  INDEX = index + 2;  goto parse_inner;
        default:    INDEX = index + 1;  type = CTXT_REDIR;     break;
        }
        break;
    case L'>':
        switch (BUF[index + 1]) {
        case L'>':
        case L'|':  INDEX = index + 2;  type = CTXT_REDIR;  break;
        case L'(':  INDEX = index + 2;  goto parse_inner;
        case L'&':  INDEX = index + 2;  type = CTXT_REDIR_FD;  break;
        default:    INDEX = index + 1;  type = CTXT_REDIR;  break;
        }
        break;
    default:  /* not a redirection */
        return false;
    }

    skip_blanks();
    if (ctryparse_word(TT_SINGLE, type)) {
        empty_pwords();
        return true;
    } else {
        return false;
    }

parse_inner:
    result = cparse_commands();
    if (!result) {
        assert(BUF[INDEX] == L')');
        INDEX++;
    }
    return result;
}

/* Parses a for command.
 * There must be the "for" keyword at the current position. */
bool cparse_for_command(void)
{
    assert(wcsncmp(&BUF[INDEX], L"for", 3) == 0);
    INDEX += 3;
    do
        skip_blanks();
    while (csubstitute_alias(0));

    /* parse variable name */
    wordunit_T *w = cparse_word(is_token_delimiter_char, TT_NONE, CTXT_VAR);
    if (w == NULL) {
        empty_pwords();
        return true;
    }
    wordfree(w);
    skip_blanks_and_newlines();

    /* parse "in" ... */
    bool in = has_token(L"in");
    if (in) {
        plist_T pwords;
        pl_init(&pwords);
        INDEX += 2;
        for (;;) {
            do
                skip_blanks();
            while (csubstitute_alias(0));

            if (BUF[INDEX] == L';' || BUF[INDEX] == L'\n') {
                plfree(pl_toary(&pwords), free);
                INDEX++;
                break;
            }
            if (BUF[INDEX] != L'\0' && is_token_delimiter_char(BUF[INDEX])) {
                plfree(pl_toary(&pwords), free);
                return false;
            }

            w = cparse_word(is_token_delimiter_char, TT_SINGLE, CTXT_NORMAL);
            if (w == NULL) {
                set_pwords(&pwords);
                return true;
            } else {
                expand_multiple(w, &pwords);
                wordfree(w);
            }
        }
        skip_blanks_and_newlines();
    }

    /* parse "do" ... */
    if (has_token(L"do")) {
        INDEX += 2;
        return false;
    }

    /* found no "in" or "do", so the next word should be one of them */
    w = cparse_word(is_token_delimiter_char, TT_NONE,
            in ? CTXT_FOR_DO : CTXT_FOR_IN);
    if (w == NULL) {
        empty_pwords();
        return true;
    }
    wordfree(w);

    /* syntax error */
    return false;
}

/* Parses a case command.
 * There must be the "case" keyword at the current position. */
bool cparse_case_command(void)
{
    assert(wcsncmp(&BUF[INDEX], L"case", 4) == 0);
    INDEX += 4;
    do
        skip_blanks();
    while (csubstitute_alias(0));

    /* parse matched word */
    wordunit_T *w =
        cparse_word(is_token_delimiter_char, TT_SINGLE, CTXT_NORMAL);
    if (w == NULL) {
        empty_pwords();
        return true;
    }
    wordfree(w);
    skip_blanks_and_newlines();

    /* parse "in" */
    if (has_token(L"in")) {
        INDEX += 2;
        return false;
    }

    /* there is no "in", so the next word should be "in". */
    w = cparse_word(is_token_delimiter_char, TT_NONE, CTXT_CASE_IN);
    if (w == NULL) {
        empty_pwords();
        return true;
    }
    wordfree(w);

    /* syntax error */
    return false;
}

/* Parses a function definition command.
 * There must be the "function" keyword at the current position. */
bool cparse_function_definition(void)
{
    assert(wcsncmp(&BUF[INDEX], L"function", 8) == 0);
    INDEX += 8;
    skip_blanks();

    /* parse the function name */
    wordunit_T *w =
        cparse_word(is_token_delimiter_char, TT_NONE, CTXT_FUNCTION);
    if (w == NULL) {
        empty_pwords();
        return true;
    }
    wordfree(w);

    return false;
}

/* Parses the word at the current position.
 * `skip_blanks' should be called before this function is called.
 * The `ctxttype' parameter is the context type of the word parsed.
 * If the word was completely parsed, the return value is false.
 * If the parser reached the end of the input string, the return value is true
 * and the result is saved in `pi->ctxt'. However, `pi->ctxt->pwords' is NULL
 * when the preceding words need to be determined by the caller. In this case,
 * the caller must update the `pwordc' and `pwords' member. */
bool ctryparse_word(tildetype_T tilde, le_contexttype_T ctxttype)
{
    wordunit_T *w = cparse_word(is_token_delimiter_char, tilde, ctxttype);
    if (w == NULL)
        return true;
    wordfree(w);
    return false;
}

/* Parses the word at the current position.
 * `skip_blanks' should be called before this function is called.
 * `testfunc' is a function that determines if a character is a word delimiter.
 * The `ctxttype' parameter is the context type of the word parsed.
 * If the word was completely parsed, the word is returned.
 * If the parser reached the end of the input string, the return value is NULL
 * and the result is saved in `pi->ctxt'. However, `pi->ctxt->pwords' is NULL
 * when the preceding words need to be determined by the caller. In this case,
 * the caller must update the `pwordc' and `pwords' member. */
wordunit_T *cparse_word(
        bool testfunc(wchar_t c), tildetype_T tilde, le_contexttype_T ctxttype)
{
cparse_word:
    if (tilde != TT_NONE)
        if (ctryparse_tilde())
            return NULL;

    wordunit_T *first = NULL, **lastp = &first;
    bool indq = false;     /* in double quotes? */
    bool escaped = false;  /* after a backslash? */
    size_t startindex = INDEX;
    size_t srcindex = le_main_index - (LEN - INDEX);

#define MAKE_WORDUNIT_STRING                                           \
    do {                                                               \
        wordunit_T *w = xmalloc(sizeof *w);                            \
        w->next = NULL;                                                \
        w->wu_type = WT_STRING;                                        \
        w->wu_string = xwcsndup(&BUF[startindex], INDEX - startindex); \
        *lastp = w, lastp = &w->next;                                  \
    } while (0)

    while (indq || !testfunc(BUF[INDEX])) {
        wordunit_T *wu;

        switch (BUF[INDEX]) {
        case L'\0':
            goto done;
        case L'\\':
            if (BUF[INDEX + 1] == L'\0') {
                escaped = true;
                INDEX += 1;
                goto done;
            } else {
                INDEX += 2;
                continue;
            }
        case L'$':
            MAKE_WORDUNIT_STRING;
            wu = cparse_special_word_unit(
                    (ctxttype & CTXT_MASK) | (indq ? CTXT_QUOTED : 0));
            if (wu == NULL) {
                if (pi->ctxt->pwords == NULL
                        && (pi->ctxt->type & CTXT_VBRACED)) {
                    xwcsbuf_T buf;
                    wchar_t *prefix =
                        expand_single(first, tilde, Q_WORD, ES_QUOTED_HARD);
                    assert(prefix != NULL);
                    pi->ctxt->pattern = wb_towcs(wb_catfree(
                                wb_initwith(&buf, prefix), pi->ctxt->pattern));
                    pi->ctxt->srcindex = srcindex;
                }
                wordfree(first);
                return NULL;
            }
            *lastp = wu, lastp = &wu->next;
            startindex = INDEX;
            continue;
        case L'`':
            wu = cparse_cmdsubst_in_backquote();
            if (wu == NULL) {
                wordfree(first);
                return NULL;
            }
            *lastp = wu, lastp = &wu->next;
            startindex = INDEX;
            continue;
        case L'\'':
            if (!indq) {
                const wchar_t *end = wcschr(&BUF[INDEX + 1], L'\'');
                if (end == NULL)
                    goto end_single_quote;
                INDEX = end - BUF;
            }
            break;
        case L'"':
            indq = !indq;
            break;
        case L':':
            if (!indq && tilde == TT_MULTI) {
                INDEX++;
                wordfree(first);
                goto cparse_word;
            }
            break;
        }
        INDEX++;
    }
done:
    MAKE_WORDUNIT_STRING;

    if (BUF[INDEX] != L'\0') {
        assert(first != NULL);
        return first;
    } else {
        pi->ctxt->quote = indq ?
            escaped ? QUOTE_DOUBLE_ESCAPED : QUOTE_DOUBLE :
            escaped ? QUOTE_NORMAL_ESCAPED : QUOTE_NORMAL;
        goto finish;
    }

    /* if the word ends without a closing quote, add one */
end_single_quote:;
    wordunit_T *w = xmalloc(sizeof *w);
    w->next = NULL;
    w->wu_type = WT_STRING;
    w->wu_string = malloc_wprintf(L"%ls'", &BUF[startindex]);
    *lastp = w, lastp = &w->next;

    pi->ctxt->quote = QUOTE_SINGLE;
finish:
    pi->ctxt->type = ctxttype;
    pi->ctxt->pwordc = 0;
    pi->ctxt->pwords = NULL;
    pi->ctxt->pattern = expand_single(first, tilde, Q_WORD, ES_QUOTED_HARD);
    pi->ctxt->srcindex = srcindex;
    wordfree(first);
    return NULL;
}

/* Parses a tilde expansion at the current position if any.
 * If there is a tilde expansion to complete, this function returns true after
 * setting the members of `pi->ctxt'. (However, `pi->ctxt->pwords' is NULL
 * when the preceding words need to be determined by the caller. In this case,
 * the caller must update the `pwordc' and `pwords' member.)
 * Otherwise, this function simply returns false. */
bool ctryparse_tilde(void)
{
    if (BUF[INDEX] != L'~')
        return false;

    size_t index = INDEX;
    do {
        index++;
        switch (BUF[index]) {
            case L' ':   case L'\t':  case L'\n':
            case L';':   case L'&':   case L'|':
            case L'<':   case L'>':   case L'(':  case L')':
            case L'$':   case L'`':   case L'/':  case L':':
            case L'\\':  case L'\'':  case L'"':
            case L'?':   case L'*':   case L'[':
                return false;
        }
        if (iswblank(BUF[index]))
            return false;
    } while (BUF[index] != L'\0');

    pi->ctxt->quote = QUOTE_NONE;
    pi->ctxt->type = CTXT_TILDE;
    pi->ctxt->pwordc = 0;
    pi->ctxt->pwords = NULL;
    pi->ctxt->pattern = xwcsdup(&BUF[INDEX + 1]);
    pi->ctxt->srcindex = le_main_index - (LEN - (INDEX + 1));
    return true;
}

/* Parses a parameter expansion or command substitution that starts with '$'.
 * The `ctxttype' parameter is the context type of the word containing this
 * expansion.
 * If the parser reached the end of the input string, the return value is NULL
 * and the result is saved in `pi->ctxt'. However, `pi->ctxt->pwords' is NULL
 * when the preceding words need to be determined by the caller. In this case,
 * the caller must update the `pwordc' and `pwords' member. */
wordunit_T *cparse_special_word_unit(le_contexttype_T ctxttype)
{
    assert(BUF[INDEX] == L'$');
    switch (BUF[INDEX + 1]) {
        case L'{':
            INDEX++;
            return cparse_paramexp_in_brace(ctxttype);
        case L'(':
            if (BUF[INDEX + 2] == L'(')
                return cparse_arith();
            else
                return cparse_cmdsubst_in_paren();
        default:
            return cparse_paramexp_raw(ctxttype);
    }
}

/* Parses a parameter that is not enclosed by { }.
 * The `ctxttype' parameter is the context type of the word containing this
 * parameter expansion. Only the CTXT_QUOTED flag in `ctxttype' affect the
 * result.
 * If the parser reached the end of the input string, the return value is NULL
 * and the result is saved in `pi->ctxt'. However, `pi->ctxt->pwords' is NULL
 * when the preceding words need to be determined by the caller. In this case,
 * the caller must update the `pwordc' and `pwords' member. */
wordunit_T *cparse_paramexp_raw(le_contexttype_T ctxttype)
{
    assert(BUF[INDEX] == L'$');

    size_t namelen;
    switch (BUF[INDEX + 1]) {
        case L'@':  case L'*':  case L'#':  case L'?':
        case L'-':  case L'$':  case L'!':
            namelen = 1;
            break;
        default:
            if (iswdigit(BUF[INDEX + 1])) {
                namelen = 1;
            } else {
                namelen = 0;
                while (is_portable_name_char(BUF[INDEX + 1 + namelen]))
                    namelen++;
                if (BUF[INDEX + 1 + namelen] == L'\0') {
                    /* complete variable name */
                    pi->ctxt->quote = QUOTE_NONE;
                    pi->ctxt->type = CTXT_VAR | (ctxttype & CTXT_QUOTED);
                    pi->ctxt->pwordc = 0;
                    pi->ctxt->pwords = xmalloc(1 * sizeof *pi->ctxt->pwords);
                    pi->ctxt->pwords[0] = NULL;
                    pi->ctxt->pattern = xwcsndup(&BUF[INDEX + 1], namelen);
                    pi->ctxt->srcindex = le_main_index - namelen;
                    return NULL;
                }
            }
            break;
    }

    wordunit_T *wu = xmalloc(sizeof *wu);
    wu->next = NULL;
    if (namelen == 0) {
        wu->wu_type = WT_STRING;
        wu->wu_string = xwcsdup(L"$");
    } else {
        wu->wu_type = WT_PARAM;
        wu->wu_param = xmalloc(sizeof *wu->wu_param);
        wu->wu_param->pe_type = PT_MINUS;
        wu->wu_param->pe_name = xwcsndup(&BUF[INDEX + 1], namelen);
        wu->wu_param->pe_start = wu->wu_param->pe_end =
        wu->wu_param->pe_match = wu->wu_param->pe_subst = NULL;
    }

    INDEX += namelen + 1;
    return wu;
}

/* Parses a parameter expansion enclosed by { }.
 * The `ctxttype' parameter is the context type of the word containing this
 * parameter expansion.
 * If the parser reached the end of the input string, the return value is NULL
 * and the result is saved in `pi->ctxt'. However, `pi->ctxt->pwords' is NULL
 * when the preceding words need to be determined by the caller. In this case,
 * the caller must update the `pwordc' and `pwords' member. */
wordunit_T *cparse_paramexp_in_brace(le_contexttype_T ctxttype)
{
    paramexp_T *pe = xmalloc(sizeof *pe);
    pe->pe_type = 0;
    pe->pe_name = NULL;
    pe->pe_start = pe->pe_end = pe->pe_match = pe->pe_subst = NULL;

    const size_t origindex = INDEX;
    assert(BUF[INDEX] == L'{');
    INDEX++;

    /* parse PT_NUMBER */
    if (BUF[INDEX] == L'#') {
        switch (BUF[INDEX + 1]) {
            case L'}':  case L'+':  case L'=':
            case L':':  case L'/':  case L'%':
                break;
            case L'-':  case L'?':  case L'#':
                if (BUF[INDEX + 2] != L'}')
                    break;
                /* falls thru! */
            case L'\0':  default:
                pe->pe_type |= PT_NUMBER;
                INDEX++;
                break;
        }
    }

    /* parse nested expansion */
    if (BUF[INDEX] == L'{') {
        pe->pe_type |= PT_NEST;
        pe->pe_nest = cparse_paramexp_in_brace(ctxttype & CTXT_MASK);
        if (pe->pe_nest == NULL)
            goto return_null;
    } else if (BUF[INDEX] == L'`'
                || (BUF[INDEX] == L'$'
                    && (BUF[INDEX + 1] == L'{' || BUF[INDEX + 1] == L'('))) {
        pe->pe_type |= PT_NEST;
        pe->pe_nest = cparse_special_word_unit(ctxttype & CTXT_MASK);
        if (pe->pe_nest == NULL)
            goto return_null;
    } else {
        /* no nesting: parse parameter name */
        size_t namelen;
        switch (BUF[INDEX]) {
            case L'@':  case L'*':  case L'#':  case L'?':
            case L'-':  case L'$':  case L'!':
                namelen = 1;
                break;
            default:
                namelen = 0;
                while (is_name_char(BUF[INDEX + namelen]))
                    namelen++;
                break;
        }
        if (BUF[INDEX + namelen] == L'\0') {
            pi->ctxt->quote = QUOTE_NORMAL;
            pi->ctxt->type = CTXT_VAR | CTXT_VBRACED | (ctxttype & CTXT_QUOTED);
            pi->ctxt->pwordc = 0;
            pi->ctxt->pwords = xmalloc(1 * sizeof *pi->ctxt->pwords);
            pi->ctxt->pwords[0] = NULL;
            pi->ctxt->pattern = xwcsndup(&BUF[INDEX], namelen);
            pi->ctxt->srcindex = le_main_index - namelen;
            goto return_null;
        }
        pe->pe_name = xwcsndup(&BUF[INDEX], namelen);
        INDEX += namelen;
    }

    /* parse index */
    if (BUF[INDEX] == L'[') {
        INDEX++;
        wordunit_T *w = cparse_word(is_closing_bracket, TT_NONE, CTXT_ARITH);
        if (w == NULL)
            goto return_null;
        wordfree(w);
        /* don't assign the result to `pe->pe_start/end' since it may cause an
         * arithmetic expansion error. */
        if (BUF[INDEX] == L']')
            INDEX++;
    }

    /* parse PT_COLON */
    if (BUF[INDEX] == L':') {
        pe->pe_type |= PT_COLON;
        INDEX++;
    }

    /* parse '-', '+', '#', etc. */
    switch (BUF[INDEX]) {
        case L'+':
            pe->pe_type |= PT_PLUS;
            goto parse_subst;
        case L'-':  case L'=':  case L'?':
            pe->pe_type |= PT_MINUS;
            goto parse_subst;
        case L'#':
            pe->pe_type |= PT_MATCH | PT_MATCHHEAD;
            goto parse_match;
        case L'%':
            pe->pe_type |= PT_MATCH | PT_MATCHTAIL;
            goto parse_match;
        case L'/':
            pe->pe_type |= PT_SUBST | PT_MATCHLONGEST;
            goto parse_match;
        case L'}':
            pe->pe_type |= PT_NONE;
            goto check_closing_paren;
        default:
            goto syntax_error;
    }

parse_match:
    if (pe->pe_type & PT_COLON) {
        if ((pe->pe_type & PT_MASK) == PT_SUBST)
            pe->pe_type |= PT_MATCHHEAD | PT_MATCHTAIL;
        else
            goto syntax_error;
        INDEX++;
    } else if (BUF[INDEX] == BUF[INDEX + 1]) {
        if ((pe->pe_type & PT_MASK) == PT_MATCH)
            pe->pe_type |= PT_MATCHLONGEST;
        else
            pe->pe_type |= PT_SUBSTALL;
        INDEX += 2;
    } else if (BUF[INDEX] == L'/') {
        switch (BUF[INDEX + 1]) {
            case L'#':
                pe->pe_type |= PT_MATCHHEAD;
                INDEX += 2;
                break;
            case L'%':
                pe->pe_type |= PT_MATCHTAIL;
                INDEX += 2;
                break;
            default:
                INDEX += 1;
                break;
        }
    } else {
        INDEX++;
    }
    if ((pe->pe_type & PT_MASK) == PT_MATCH) {
        pe->pe_match = cparse_word(
                is_closing_brace, TT_NONE, ctxttype | CTXT_VBRACED);
        if (pe->pe_match == NULL)
            goto return_null;
        goto check_closing_paren;
    } else {
        pe->pe_match = cparse_word(
                is_slash_or_closing_brace, TT_NONE, ctxttype | CTXT_VBRACED);
        if (pe->pe_match == NULL)
            goto return_null;
        if (BUF[INDEX] != L'/')
            goto check_closing_paren;
    }

parse_subst:
    INDEX++;
    pe->pe_subst = cparse_word(
            is_closing_brace, TT_NONE, ctxttype | CTXT_VBRACED);
    if (pe->pe_subst == NULL)
        goto return_null;

check_closing_paren:
    if (BUF[INDEX] == L'}')
        INDEX++;

    switch (pe->pe_type & PT_MASK) {
        case PT_MINUS:
        case PT_PLUS:
            break;
        case PT_ASSIGN:
        case PT_ERROR:
            assert(false);
        default:
            if (pe->pe_type & PT_NEST)
                break;

            /* make the variable expansion nested with the PT_MINUS flag
             * to avoid a possible "nounset" error. */
            paramexp_T *pe2 = xmalloc(sizeof *pe2);
            pe2->pe_type = PT_MINUS;
            pe2->pe_name = pe->pe_name;
            pe2->pe_start = pe2->pe_end = pe2->pe_match = pe2->pe_subst = NULL;

            wordunit_T *nest = xmalloc(sizeof *nest);
            nest->next = NULL;
            nest->wu_type = WT_PARAM;
            nest->wu_param = pe2;
            pe->pe_type |= PT_NEST;
            pe->pe_nest = nest;
            break;
    }

    wordunit_T *result = xmalloc(sizeof *result);
    result->next = NULL;
    result->wu_type = WT_PARAM;
    result->wu_param = pe;
    return result;

syntax_error:
    paramfree(pe);
    result = xmalloc(sizeof *result);
    result->next = NULL;
    result->wu_type = WT_STRING;
    result->wu_string = escapefree(
            xwcsndup(&BUF[origindex], INDEX - origindex), NULL);
    return result;

return_null:
    paramfree(pe);
    return NULL;
}

/* Parses an arithmetic expansion.
 * If the parser reached the end of the input string, the return value is NULL
 * and the result is saved in `pi->ctxt'. */
wordunit_T *cparse_arith(void)
{
    size_t startindex = INDEX;

    assert(BUF[INDEX    ] == L'$');
    assert(BUF[INDEX + 1] == L'(');

    unsigned nestparen = 0;
    INDEX += 2;
    for (;;) {
        if (BUF[INDEX] != L'\0' && is_arith_delimiter(BUF[INDEX])) {
            switch (BUF[INDEX++]) {
                case L'(':
                    nestparen++;
                    break;
                case L')':
                    if (nestparen == 0)
                        goto return_content;
                    nestparen--;
                    break;
            }
        } else {
            wordunit_T *w =
                cparse_word(is_arith_delimiter, TT_NONE, CTXT_ARITH);
            if (w == NULL) {
                empty_pwords();
                return NULL;
            }
            wordfree(w);
        }
    }

return_content:;
    wordunit_T *result = xmalloc(sizeof *result);
    result->next = NULL;
    result->wu_type = WT_STRING;
    result->wu_string = xwcsndup(&BUF[startindex], INDEX - startindex);
    return result;
}

/* Parses a command substitution enclosed by "$( )".
 * If the parser reached the end of the input string, the return value is NULL
 * and the result is saved in `pi->ctxt'. */
wordunit_T *cparse_cmdsubst_in_paren(void)
{
    size_t startindex = INDEX;

    assert(BUF[INDEX    ] == L'$');
    assert(BUF[INDEX + 1] == L'(');

    INDEX += 2;
    if (cparse_commands()) {
        return NULL;
    } else {
        assert(BUF[INDEX] == L')');
        INDEX++;

        wordunit_T *result = xmalloc(sizeof *result);
        result->next = NULL;
        result->wu_type = WT_STRING;
        result->wu_string = xwcsndup(&BUF[startindex], INDEX - startindex);
        return result;
    }
}

/* Parses a command substitution enclosed by backquotes.
 * If the parser reached the end of the input string, the return value is NULL
 * and the result is saved in `pi->ctxt'. */
wordunit_T *cparse_cmdsubst_in_backquote(void)
{
    size_t startindex = INDEX;
    size_t endindex;

    assert(BUF[INDEX] == L'`');
    INDEX++;
    endindex = INDEX;
    for (;;) {
        switch (BUF[endindex++]) {
            case L'\0':
                goto parse_inside;
            case L'`':
                goto return_content;
            case L'\\':
                if (BUF[endindex] != L'\0')
                    endindex++;
                break;
        }
    }

parse_inside:
    while (!cparse_commands())
        INDEX++;
    return NULL;

return_content:
    INDEX = endindex;
    wordunit_T *result = xmalloc(sizeof *result);
    result->next = NULL;
    result->wu_type = WT_STRING;
    result->wu_string =
        escapefree(xwcsndup(&BUF[startindex], endindex - startindex), NULL);
    return result;
}

bool is_slash_or_closing_brace(wchar_t c)
{
    return c == L'/' || c == L'}' || c == L'\0';
}

bool is_closing_brace(wchar_t c)
{
    return c == L'}' || c == L'\0';
}
bool is_closing_bracket(wchar_t c)
{
    return c == L']' || c == L'\0';
}

bool is_arith_delimiter(wchar_t c)
{
    switch (c) {
        case L'\0':
            return true;
        case L'$':  case L'`':  case L'"':  case L'\'':  case L'\\':  case L'_':
            return false;
        default:
            return !iswalnum(c);
    }
}

/* Remove brace expansion in the specified string and returns the last result of
 * expansion.
 * For example, remove_braceexpand("a{1,2{b,c}2{d,e") = "a2c2e".
 * The string is modified in place.
 * Returns true iff expansion is unclosed. */
bool remove_braceexpand(wchar_t *s)
{
    bool unclosed = false;
    xwcsbuf_T buf;

    wb_initwithmax(&buf, wcslen(s));
    wb_cat(&buf, s);

    for (size_t i = 0; i < buf.length; ) {
        switch (buf.contents[i]) {
            case L'{':
                unclosed = remove_braceexpand_inner(&buf, &i);
                break;
            case L'\\':
                i += 2;
                break;
            default:
                i++;
                break;
        }
    }

    assert(buf.length <= wcslen(s));
    wmemcpy(s, buf.contents, buf.length + 1);
    wb_destroy(&buf);

    return unclosed;
}

bool remove_braceexpand_inner(xwcsbuf_T *buf, size_t *index)
{
    bool foundcomma = false;
    size_t i = *index;
    size_t leftbraceindex = i;
    assert(buf->contents[leftbraceindex] == L'{');

    i++;
    while (i < buf->length) {
        switch (buf->contents[i]) {
        case L'{':
            remove_braceexpand_inner(buf, &i);
            break;
        case L',':
            foundcomma = true;
            wb_remove(buf, leftbraceindex, i - leftbraceindex + 1);
            i = leftbraceindex;
            break;
        case L'}':
            if (foundcomma)  /* remove right brace */
                wb_remove(buf, i, 1);
            else
                i++;
            *index = i;
            return false;
        case L'\\':
            i += 2;
            break;
        default:
            i++;
            break;
        }
    }

    if (!foundcomma)  /* remove left brace */
        wb_remove(buf, leftbraceindex, 1);
    *index = buf->length;
    return true;
}


/* vim: set ts=8 sts=4 sw=4 et tw=80: */