forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand.cpp
2132 lines (1847 loc) · 61.1 KB
/
expand.cpp
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 expand.c
String expansion functions. These functions perform several kinds of
parameter expansion.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <wctype.h>
#include <errno.h>
#include <pwd.h>
#include <unistd.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/types.h>
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#include <termios.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <algorithm>
#include <assert.h>
#include <vector>
#ifdef SunOS
#include <procfs.h>
#endif
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "wutil.h"
#include "env.h"
#include "proc.h"
#include "parser.h"
#include "expand.h"
#include "wildcard.h"
#include "exec.h"
#include "signal.h"
#include "tokenizer.h"
#include "complete.h"
#include "iothread.h"
#include "parse_util.h"
/**
Description for child process
*/
#define COMPLETE_CHILD_PROCESS_DESC _( L"Child process")
/**
Description for non-child process
*/
#define COMPLETE_PROCESS_DESC _( L"Process")
/**
Description for long job
*/
#define COMPLETE_JOB_DESC _( L"Job")
/**
Description for short job. The job command is concatenated
*/
#define COMPLETE_JOB_DESC_VAL _( L"Job: %ls")
/**
Description for the shells own pid
*/
#define COMPLETE_SELF_DESC _( L"Shell process")
/**
Description for the shells own pid
*/
#define COMPLETE_LAST_DESC _( L"Last background job")
/**
String in process expansion denoting ourself
*/
#define SELF_STR L"self"
/**
String in process expansion denoting last background job
*/
#define LAST_STR L"last"
/**
Characters which make a string unclean if they are the first
character of the string. See \c expand_is_clean().
*/
#define UNCLEAN_FIRST L"~%"
/**
Unclean characters. See \c expand_is_clean().
*/
#define UNCLEAN L"$*?\\\"'({})"
static void remove_internal_separator(wcstring &s, bool conv);
int expand_is_clean(const wchar_t *in)
{
const wchar_t * str = in;
CHECK(in, 1);
/*
Test characters that have a special meaning in the first character position
*/
if (wcschr(UNCLEAN_FIRST, *str))
return 0;
/*
Test characters that have a special meaning in any character position
*/
while (*str)
{
if (wcschr(UNCLEAN, *str))
return 0;
str++;
}
return 1;
}
/* Append a syntax error to the given error list */
static void append_syntax_error(parse_error_list_t *errors, size_t source_start, const wchar_t *fmt, ...)
{
if (errors != NULL)
{
parse_error_t error;
error.source_start = source_start;
error.source_length = 0;
error.code = parse_error_syntax;
va_list va;
va_start(va, fmt);
error.text = vformat_string(fmt, va);
va_end(va);
errors->push_back(error);
}
}
/* Append a cmdsub error to the given error list */
static void append_cmdsub_error(parse_error_list_t *errors, size_t source_start, const wchar_t *fmt, ...)
{
if (errors != NULL)
{
parse_error_t error;
error.source_start = source_start;
error.source_length = 0;
error.code = parse_error_cmdsubst;
va_list va;
va_start(va, fmt);
error.text = vformat_string(fmt, va);
va_end(va);
errors->push_back(error);
}
}
/**
Return the environment variable value for the string starting at \c in.
*/
static env_var_t expand_var(const wchar_t *in)
{
if (!in)
return env_var_t::missing_var();
return env_get_string(in);
}
/**
Test if the specified string does not contain character which can
not be used inside a quoted string.
*/
static int is_quotable(const wchar_t *str)
{
switch (*str)
{
case 0:
return 1;
case L'\n':
case L'\t':
case L'\r':
case L'\b':
case L'\x1b':
return 0;
default:
return is_quotable(str+1);
}
return 0;
}
static int is_quotable(const wcstring &str)
{
return is_quotable(str.c_str());
}
wcstring expand_escape_variable(const wcstring &in)
{
wcstring_list_t lst;
wcstring buff;
tokenize_variable_array(in, lst);
switch (lst.size())
{
case 0:
buff.append(L"''");
break;
case 1:
{
const wcstring &el = lst.at(0);
if (el.find(L' ') != wcstring::npos && is_quotable(el))
{
buff.append(L"'");
buff.append(el);
buff.append(L"'");
}
else
{
buff.append(escape_string(el, 1));
}
break;
}
default:
{
for (size_t j=0; j<lst.size(); j++)
{
const wcstring &el = lst.at(j);
if (j)
buff.append(L" ");
if (is_quotable(el))
{
buff.append(L"'");
buff.append(el);
buff.append(L"'");
}
else
{
buff.append(escape_string(el, 1));
}
}
}
}
return buff;
}
/**
Tests if all characters in the wide string are numeric
*/
static int iswnumeric(const wchar_t *n)
{
for (; *n; n++)
{
if (*n < L'0' || *n > L'9')
{
return 0;
}
}
return 1;
}
/**
See if the process described by \c proc matches the commandline \c
cmd
*/
static bool match_pid(const wcstring &cmd,
const wchar_t *proc,
int flags,
size_t *offset)
{
/* Test for a direct match. If the proc string is empty (e.g. the user tries to complete against %), then return an offset pointing at the base command. That ensures that you don't see a bunch of dumb paths when completing against all processes. */
if (proc[0] != L'\0' && wcsncmp(cmd.c_str(), proc, wcslen(proc)) == 0)
{
if (offset)
*offset = 0;
return true;
}
/* Get the command to match against. We're only interested in the last path component. */
const wcstring base_cmd = wbasename(cmd);
bool result = string_prefixes_string(proc, base_cmd);
if (result)
{
/* It's a match. Return the offset within the full command. */
if (offset)
*offset = cmd.size() - base_cmd.size();
}
return result;
}
/** Helper class for iterating over processes. The names returned have been unescaped (e.g. may include spaces) */
#ifdef KERN_PROCARGS2
/* BSD / OS X process completions */
class process_iterator_t
{
std::vector<pid_t> pids;
size_t idx;
wcstring name_for_pid(pid_t pid);
public:
process_iterator_t();
bool next_process(wcstring *str, pid_t *pid);
};
wcstring process_iterator_t::name_for_pid(pid_t pid)
{
wcstring result;
int mib[4], maxarg = 0, numArgs = 0;
size_t size = 0;
char *args = NULL, *stringPtr = NULL;
mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX;
size = sizeof(maxarg);
if (sysctl(mib, 2, &maxarg, &size, NULL, 0) == -1)
{
return result;
}
args = (char *)malloc(maxarg);
if (args == NULL)
{
return result;
}
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2;
mib[2] = pid;
size = (size_t)maxarg;
if (sysctl(mib, 3, args, &size, NULL, 0) == -1)
{
free(args);
return result;;
}
memcpy(&numArgs, args, sizeof(numArgs));
stringPtr = args + sizeof(numArgs);
result = str2wcstring(stringPtr);
free(args);
return result;
}
bool process_iterator_t::next_process(wcstring *out_str, pid_t *out_pid)
{
wcstring name;
pid_t pid = 0;
bool result = false;
while (idx < pids.size())
{
pid = pids.at(idx++);
name = name_for_pid(pid);
if (! name.empty())
{
result = true;
break;
}
}
if (result)
{
*out_str = name;
*out_pid = pid;
}
return result;
}
process_iterator_t::process_iterator_t() : idx(0)
{
int err;
struct kinfo_proc * result;
bool done;
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
// Declaring name as const requires us to cast it when passing it to
// sysctl because the prototype doesn't include the const modifier.
size_t length;
// We start by calling sysctl with result == NULL and length == 0.
// That will succeed, and set length to the appropriate length.
// We then allocate a buffer of that size and call sysctl again
// with that buffer. If that succeeds, we're done. If that fails
// with ENOMEM, we have to throw away our buffer and loop. Note
// that the loop causes use to call sysctl with NULL again; this
// is necessary because the ENOMEM failure case sets length to
// the amount of data returned, not the amount of data that
// could have been returned.
result = NULL;
done = false;
do
{
assert(result == NULL);
// Call sysctl with a NULL buffer.
length = 0;
err = sysctl((int *) name, (sizeof(name) / sizeof(*name)) - 1,
NULL, &length,
NULL, 0);
if (err == -1)
{
err = errno;
}
// Allocate an appropriately sized buffer based on the results
// from the previous call.
if (err == 0)
{
result = (struct kinfo_proc *)malloc(length);
if (result == NULL)
{
err = ENOMEM;
}
}
// Call sysctl again with the new buffer. If we get an ENOMEM
// error, toss away our buffer and start again.
if (err == 0)
{
err = sysctl((int *) name, (sizeof(name) / sizeof(*name)) - 1,
result, &length,
NULL, 0);
if (err == -1)
{
err = errno;
}
if (err == 0)
{
done = true;
}
else if (err == ENOMEM)
{
assert(result != NULL);
free(result);
result = NULL;
err = 0;
}
}
}
while (err == 0 && ! done);
// Clean up and establish post conditions.
if (err == 0 && result != NULL)
{
for (size_t idx = 0; idx < length / sizeof(struct kinfo_proc); idx++)
pids.push_back(result[idx].kp_proc.p_pid);
}
if (result)
free(result);
}
#else
/* /proc style process completions */
class process_iterator_t
{
DIR *dir;
public:
process_iterator_t();
~process_iterator_t();
bool next_process(wcstring *out_str, pid_t *out_pid);
};
process_iterator_t::process_iterator_t(void)
{
dir = opendir("/proc");
}
process_iterator_t::~process_iterator_t(void)
{
if (dir)
closedir(dir);
}
bool process_iterator_t::next_process(wcstring *out_str, pid_t *out_pid)
{
wcstring cmd;
pid_t pid = 0;
while (cmd.empty())
{
wcstring name;
if (! dir || ! wreaddir(dir, name))
break;
if (!iswnumeric(name.c_str()))
continue;
wcstring path = wcstring(L"/proc/") + name;
struct stat buf;
if (wstat(path, &buf))
continue;
if (buf.st_uid != getuid())
continue;
/* remember the pid */
pid = fish_wcstoi(name.c_str(), NULL, 10);
/* the 'cmdline' file exists, it should contain the commandline */
FILE *cmdfile;
if ((cmdfile=wfopen(path + L"/cmdline", "r")))
{
wcstring full_command_line;
fgetws2(&full_command_line, cmdfile);
/* The command line needs to be escaped */
cmd = tok_first(full_command_line.c_str());
}
#ifdef SunOS
else if ((cmdfile=wfopen(path + L"/psinfo", "r")))
{
psinfo_t info;
if (fread(&info, sizeof(info), 1, cmdfile))
{
/* The filename is unescaped */
cmd = str2wcstring(info.pr_fname);
}
}
#endif
if (cmdfile)
fclose(cmdfile);
}
bool result = ! cmd.empty();
if (result)
{
*out_str = cmd;
*out_pid = pid;
}
return result;
}
#endif
std::vector<wcstring> expand_get_all_process_names(void)
{
wcstring name;
pid_t pid;
process_iterator_t iterator;
std::vector<wcstring> result;
while (iterator.next_process(&name, &pid))
{
result.push_back(name);
}
return result;
}
/* Helper function to do a job search. */
struct find_job_data_t
{
const wchar_t *proc; /* The process to search for - possibly numeric, possibly a name */
expand_flags_t flags;
std::vector<completion_t> *completions;
};
/* The following function is invoked on the main thread, because the job list is not thread safe. It should search the job list for something matching the given proc, and then return 1 to stop the search, 0 to continue it */
static int find_job(const struct find_job_data_t *info)
{
ASSERT_IS_MAIN_THREAD();
const wchar_t * const proc = info->proc;
const expand_flags_t flags = info->flags;
std::vector<completion_t> &completions = *info->completions;
const job_t *j;
int found = 0;
// do the empty param check first, because an empty string passes our 'numeric' check
if (wcslen(proc)==0)
{
/*
This is an empty job expansion: '%'
It expands to the last job backgrounded.
*/
job_iterator_t jobs;
while ((j = jobs.next()))
{
if (!j->command_is_empty())
{
append_completion(completions, to_string<long>(j->pgid));
break;
}
}
/*
You don't *really* want to flip a coin between killing
the last process backgrounded and all processes, do you?
Let's not try other match methods with the solo '%' syntax.
*/
found = 1;
}
else if (iswnumeric(proc))
{
/*
This is a numeric job string, like '%2'
*/
if (flags & ACCEPT_INCOMPLETE)
{
job_iterator_t jobs;
while ((j = jobs.next()))
{
wchar_t jid[16];
if (j->command_is_empty())
continue;
swprintf(jid, 16, L"%d", j->job_id);
if (wcsncmp(proc, jid, wcslen(proc))==0)
{
wcstring desc_buff = format_string(COMPLETE_JOB_DESC_VAL, j->command_wcstr());
append_completion(completions,
jid+wcslen(proc),
desc_buff,
0);
}
}
}
else
{
int jid;
wchar_t *end;
errno = 0;
jid = fish_wcstoi(proc, &end, 10);
if (jid > 0 && !errno && !*end)
{
j = job_get(jid);
if ((j != 0) && (j->command_wcstr() != 0) && (!j->command_is_empty()))
{
append_completion(completions, to_string<long>(j->pgid));
}
}
}
/*
Stop here so you can't match a random process name
when you're just trying to use job control.
*/
found = 1;
}
if (! found)
{
job_iterator_t jobs;
while ((j = jobs.next()))
{
if (j->command_is_empty())
continue;
size_t offset;
if (match_pid(j->command(), proc, flags, &offset))
{
if (flags & ACCEPT_INCOMPLETE)
{
append_completion(completions,
j->command_wcstr() + offset + wcslen(proc),
COMPLETE_JOB_DESC,
0);
}
else
{
append_completion(completions, to_string<long>(j->pgid));
found = 1;
}
}
}
if (! found)
{
jobs.reset();
while ((j = jobs.next()))
{
process_t *p;
if (j->command_is_empty())
continue;
for (p=j->first_process; p; p=p->next)
{
if (p->actual_cmd.empty())
continue;
size_t offset;
if (match_pid(p->actual_cmd, proc, flags, &offset))
{
if (flags & ACCEPT_INCOMPLETE)
{
append_completion(completions,
wcstring(p->actual_cmd, offset + wcslen(proc)),
COMPLETE_CHILD_PROCESS_DESC,
0);
}
else
{
append_completion(completions,
to_string<long>(p->pid),
L"",
0);
found = 1;
}
}
}
}
}
}
return found;
}
/**
Searches for a job with the specified job id, or a job or process
which has the string \c proc as a prefix of its commandline. Appends
the name of the process as a completion in 'out'.
If the ACCEPT_INCOMPLETE flag is set, the remaining string for any matches
are inserted.
Otherwise, any job matching the specified string is matched, and
the job pgid is returned. If no job matches, all child processes
are searched. If no child processes match, and <tt>fish</tt> can
understand the contents of the /proc filesystem, all the users
processes are searched for matches.
*/
static void find_process(const wchar_t *proc, expand_flags_t flags, std::vector<completion_t> &out)
{
if (!(flags & EXPAND_SKIP_JOBS))
{
const struct find_job_data_t data = {proc, flags, &out};
int found = iothread_perform_on_main(find_job, &data);
if (found)
{
return;
}
}
/* Iterate over all processes */
wcstring process_name;
pid_t process_pid;
process_iterator_t iterator;
while (iterator.next_process(&process_name, &process_pid))
{
size_t offset;
if (match_pid(process_name, proc, flags, &offset))
{
if (flags & ACCEPT_INCOMPLETE)
{
append_completion(out,
process_name.c_str() + offset + wcslen(proc),
COMPLETE_PROCESS_DESC,
0);
}
else
{
append_completion(out, to_string<long>(process_pid));
}
}
}
}
/**
Process id expansion
*/
static bool expand_pid(const wcstring &instr_with_sep, expand_flags_t flags, std::vector<completion_t> &out, parse_error_list_t *errors)
{
/* Hack. If there's no INTERNAL_SEP and no PROCESS_EXPAND, then there's nothing to do. Check out this "null terminated string." */
const wchar_t some_chars[] = {INTERNAL_SEPARATOR, PROCESS_EXPAND, L'\0'};
if (instr_with_sep.find_first_of(some_chars) == wcstring::npos)
{
/* Nothing to do */
append_completion(out, instr_with_sep);
return true;
}
/* expand_string calls us with internal separators in instr...sigh */
wcstring instr = instr_with_sep;
remove_internal_separator(instr, false);
if (instr.empty() || instr.at(0) != PROCESS_EXPAND)
{
/* Not a process expansion */
append_completion(out, instr);
return true;
}
const wchar_t * const in = instr.c_str();
/* We know we are a process expansion now */
assert(in[0] == PROCESS_EXPAND);
if (flags & ACCEPT_INCOMPLETE)
{
if (wcsncmp(in+1, SELF_STR, wcslen(in+1))==0)
{
append_completion(out,
&SELF_STR[wcslen(in+1)],
COMPLETE_SELF_DESC,
0);
}
else if (wcsncmp(in+1, LAST_STR, wcslen(in+1))==0)
{
append_completion(out,
&LAST_STR[wcslen(in+1)],
COMPLETE_LAST_DESC,
0);
}
}
else
{
if (wcscmp((in+1), SELF_STR)==0)
{
append_completion(out, to_string<long>(getpid()));
return true;
}
if (wcscmp((in+1), LAST_STR)==0)
{
if (proc_last_bg_pid > 0)
{
append_completion(out, to_string<long>(proc_last_bg_pid));
}
return true;
}
}
/* This is sort of crummy - find_process doesn't return any indication of success, so instead we check to see if it inserted any completions */
const size_t prev_count = out.size();
find_process(in+1, flags, out);
if (prev_count == out.size())
{
if (!(flags & ACCEPT_INCOMPLETE))
{
/* We failed to find anything */
append_syntax_error(errors, 1, FAILED_EXPANSION_PROCESS_ERR_MSG, in+1);
return false;
}
}
return true;
}
void expand_variable_error(parser_t &parser, const wcstring &token, size_t token_pos, int error_pos, parse_error_list_t *errors)
{
size_t stop_pos = token_pos+1;
switch (token[stop_pos])
{
case BRACKET_BEGIN:
{
wchar_t *cpy = wcsdup(token.c_str());
*(cpy+token_pos)=0;
wchar_t *name = &cpy[stop_pos+1];
wchar_t *end = wcschr(name, BRACKET_END);
wchar_t *post = NULL;
int is_var=0;
if (end)
{
post = end+1;
*end = 0;
if (!wcsvarname(name))
{
is_var = 1;
}
}
if (is_var)
{
append_syntax_error(errors,
error_pos,
COMPLETE_VAR_BRACKET_DESC,
cpy,
name,
post);
}
else
{
append_syntax_error(errors,
error_pos,
COMPLETE_VAR_BRACKET_DESC,
L"",
L"VARIABLE",
L"");
}
free(cpy);
break;
}
case INTERNAL_SEPARATOR:
{
append_syntax_error(errors,
error_pos,
COMPLETE_VAR_PARAN_DESC);
break;
}
case 0:
{
append_syntax_error(errors,
error_pos,
COMPLETE_VAR_NULL_DESC);
break;
}
default:
{
wchar_t token_stop_char = token[stop_pos];
// Unescape (see http://github.com/fish-shell/fish-shell/issues/50)
if (token_stop_char == ANY_CHAR)
token_stop_char = L'?';
else if (token_stop_char == ANY_STRING || token_stop_char == ANY_STRING_RECURSIVE)
token_stop_char = L'*';
append_syntax_error(errors,
error_pos,
(token_stop_char == L'?' ? COMPLETE_YOU_WANT_STATUS : COMPLETE_VAR_DESC),
token_stop_char);
break;
}
}
}
/**
Parse an array slicing specification
Returns 0 on success.
If a parse error occurs, returns the index of the bad token.
Note that 0 can never be a bad index because the string always starts with [.
*/
static size_t parse_slice(const wchar_t *in, wchar_t **end_ptr, std::vector<long> &idx, std::vector<size_t> &source_positions, size_t array_size)
{
wchar_t *end;
const long size = (long)array_size;
size_t pos = 1; //skip past the opening square bracket
// debug( 0, L"parse_slice on '%ls'", in );
while (1)
{
long tmp;
while (iswspace(in[pos]) || (in[pos]==INTERNAL_SEPARATOR))
pos++;
if (in[pos] == L']')
{
pos++;
break;
}
errno=0;
const size_t i1_src_pos = pos;
tmp = wcstol(&in[pos], &end, 10);
if ((errno) || (end == &in[pos]))
{
return pos;
}
// debug( 0, L"Push idx %d", tmp );
long i1 = tmp>-1 ? tmp : (long)array_size+tmp+1;
pos = end-in;
while (in[pos]==INTERNAL_SEPARATOR)
pos++;
if (in[pos]==L'.' && in[pos+1]==L'.')
{
pos+=2;
while (in[pos]==INTERNAL_SEPARATOR)
pos++;