forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.cpp
4506 lines (3829 loc) · 118 KB
/
builtin.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 builtin.c
Functions for executing builtin functions.
How to add a new builtin function:
1). Create a function in builtin.c with the following signature:
<tt>static int builtin_NAME( parser_t &parser, wchar_t ** args )</tt>
where NAME is the name of the builtin, and args is a zero-terminated list of arguments.
2). Add a line like { L"NAME", &builtin_NAME, N_(L"Bla bla bla") }, to the builtin_data_t variable. The description is used by the completion system. Note that this array is sorted!
3). Create a file doc_src/NAME.txt, containing the manual for the builtin in Doxygen-format. Check the other builtin manuals for proper syntax.
4). Use 'git add doc_src/NAME.txt' to start tracking changes to the documentation file.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <signal.h>
#include <wctype.h>
#include <sys/time.h>
#include <time.h>
#include <stack>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "builtin.h"
#include "function.h"
#include "complete.h"
#include "proc.h"
#include "parser.h"
#include "reader.h"
#include "env.h"
#include "common.h"
#include "wgetopt.h"
#include "sanity.h"
#include "tokenizer.h"
#include "wildcard.h"
#include "input_common.h"
#include "input.h"
#include "intern.h"
#include "event.h"
#include "signal.h"
#include "exec.h"
#include "highlight.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "expand.h"
#include "path.h"
#include "history.h"
#include "parse_tree.h"
/**
The default prompt for the read command
*/
#define DEFAULT_READ_PROMPT L"set_color green; echo -n read; set_color normal; echo -n \"> \""
/**
The mode name to pass to history and input
*/
#define READ_MODE_NAME L"fish_read"
/**
The send stuff to foreground message
*/
#define FG_MSG _( L"Send job %d, '%ls' to foreground\n" )
/**
Datastructure to describe a builtin.
*/
struct builtin_data_t
{
/**
Name of the builtin
*/
const wchar_t *name;
/**
Function pointer tothe builtin implementation
*/
int (*func)(parser_t &parser, wchar_t **argv);
/**
Description of what the builtin does
*/
const wchar_t *desc;
bool operator<(const wcstring &) const;
bool operator<(const builtin_data_t *) const;
};
bool builtin_data_t::operator<(const wcstring &other) const
{
return wcscmp(this->name, other.c_str()) < 0;
}
bool builtin_data_t::operator<(const builtin_data_t *other) const
{
return wcscmp(this->name, other->name) < 0;
}
int builtin_out_redirect;
int builtin_err_redirect;
/* Buffers for storing the output of builtin functions */
wcstring stdout_buffer, stderr_buffer;
const wcstring &get_stdout_buffer()
{
ASSERT_IS_MAIN_THREAD();
return stdout_buffer;
}
const wcstring &get_stderr_buffer()
{
ASSERT_IS_MAIN_THREAD();
return stderr_buffer;
}
void builtin_show_error(const wcstring &err)
{
ASSERT_IS_MAIN_THREAD();
stderr_buffer.append(err);
}
/**
Stack containing builtin I/O for recursive builtin calls.
*/
struct io_stack_elem_t
{
int in;
wcstring out;
wcstring err;
};
static std::stack<io_stack_elem_t, std::vector<io_stack_elem_t> > io_stack;
/**
The file from which builtin functions should attempt to read, use
instead of stdin.
*/
static int builtin_stdin;
/**
The underlying IO redirections behind the current builtin. This
should normally not be used - sb_out and friends are already
configured to handle everything.
*/
static const io_chain_t *real_io;
/**
Counts the number of non null pointers in the specified array
*/
static int builtin_count_args(const wchar_t * const * argv)
{
int argc = 1;
while (argv[argc] != NULL)
{
argc++;
}
return argc;
}
/**
This function works like wperror, but it prints its result into
the sb_err string instead of to stderr. Used by the builtin
commands.
*/
static void builtin_wperror(const wchar_t *s)
{
if (s != 0)
{
stderr_buffer.append(s);
stderr_buffer.append(L": ");
}
char *err = strerror(errno);
if (err)
{
const wcstring werr = str2wcstring(err);
stderr_buffer.append(werr);
stderr_buffer.push_back(L'\n');
}
}
/**
Count the number of times the specified character occurs in the specified string
*/
static int count_char(const wchar_t *str, wchar_t c)
{
int res = 0;
for (; *str; str++)
{
res += (*str==c);
}
return res;
}
wcstring builtin_help_get(parser_t &parser, const wchar_t *name)
{
/* This won't ever work if no_exec is set */
if (no_exec)
return wcstring();
wcstring_list_t lst;
wcstring out;
const wcstring name_esc = escape_string(name, 1);
const wcstring cmd = format_string(L"__fish_print_help %ls", name_esc.c_str());
if (exec_subshell(cmd, lst, false /* don't apply exit status */) >= 0)
{
for (size_t i=0; i<lst.size(); i++)
{
out.append(lst.at(i));
out.push_back(L'\n');
}
}
return out;
}
/**
Print help for the specified builtin. If \c b is sb_err, also print
the line information
If \c b is the buffer representing standard error, and the help
message is about to be printed to an interactive screen, it may be
shortened to fit the screen.
*/
static void builtin_print_help(parser_t &parser, const wchar_t *cmd, wcstring &b)
{
if (&b == &stderr_buffer)
{
stderr_buffer.append(parser.current_line());
}
const wcstring h = builtin_help_get(parser, cmd);
if (!h.size())
return;
wchar_t *str = wcsdup(h.c_str());
if (str)
{
bool is_short = false;
if (&b == &stderr_buffer)
{
/*
Interactive mode help to screen - only print synopsis if
the rest won't fit
*/
int screen_height, lines;
screen_height = common_get_height();
lines = count_char(str, L'\n');
if (!get_is_interactive() || (lines > 2*screen_height/3))
{
wchar_t *pos;
int cut=0;
int i;
is_short = true;
/*
First move down 4 lines
*/
pos = str;
for (i=0; (i<4) && pos && *pos; i++)
{
pos = wcschr(pos+1, L'\n');
}
if (pos && *pos)
{
/*
Then find the next empty line
*/
for (; *pos; pos++)
{
if (*pos == L'\n')
{
wchar_t *pos2;
int is_empty = 1;
for (pos2 = pos+1; *pos2; pos2++)
{
if (*pos2 == L'\n')
break;
if (*pos2 != L'\t' && *pos2 !=L' ')
{
is_empty = 0;
break;
}
}
if (is_empty)
{
/*
And cut it
*/
*(pos2+1)=L'\0';
cut = 1;
break;
}
}
}
}
/*
We did not find a good place to cut message to
shorten it - so we make sure we don't print
anything.
*/
if (!cut)
{
*str = 0;
}
}
}
b.append(str);
if (is_short)
{
append_format(b, _(L"%ls: Type 'help %ls' for related documentation\n\n"), cmd, cmd);
}
free(str);
}
}
/**
Perform error reporting for encounter with unknown option
*/
static void builtin_unknown_option(parser_t &parser, const wchar_t *cmd, const wchar_t *opt)
{
append_format(stderr_buffer, BUILTIN_ERR_UNKNOWN, cmd, opt);
builtin_print_help(parser, cmd, stderr_buffer);
}
/**
Perform error reporting for encounter with missing argument
*/
static void builtin_missing_argument(parser_t &parser, const wchar_t *cmd, const wchar_t *opt)
{
append_format(stderr_buffer, BUILTIN_ERR_MISSING, cmd, opt);
builtin_print_help(parser, cmd, stderr_buffer);
}
/*
Here follows the definition of all builtin commands. The function
names are all on the form builtin_NAME where NAME is the name of the
builtin. so the function name for the builtin 'fg' is
'builtin_fg'.
A few builtins, including 'while', 'command' and 'builtin' are not
defined here as they are handled directly by the parser. (They are
not parsed as commands, instead they only alter the parser state)
The builtins 'break' and 'continue' are so closely related that they
share the same implementation, namely 'builtin_break_continue.
Several other builtins, including jobs, ulimit and set are so big
that they have been given their own file. These files are all named
'builtin_NAME.c', where NAME is the name of the builtin. These files
are included directly below.
*/
#include "builtin_set.cpp"
#include "builtin_commandline.cpp"
#include "builtin_complete.cpp"
#include "builtin_ulimit.cpp"
#include "builtin_jobs.cpp"
#include "builtin_set_color.cpp"
#include "builtin_printf.cpp"
/* builtin_test lives in builtin_test.cpp */
int builtin_test(parser_t &parser, wchar_t **argv);
/**
List all current key bindings
*/
static void builtin_bind_list()
{
size_t i;
wcstring_list_t lst;
input_mapping_get_names(lst);
for (i=0; i<lst.size(); i++)
{
wcstring seq = lst.at(i);
wcstring ecmd;
input_mapping_get(seq, ecmd);
ecmd = escape_string(ecmd, 1);
wcstring tname;
if (input_terminfo_get_name(seq, tname))
{
append_format(stdout_buffer, L"bind -k %ls %ls\n", tname.c_str(), ecmd.c_str());
}
else
{
const wcstring eseq = escape_string(seq, 1);
append_format(stdout_buffer, L"bind %ls %ls\n", eseq.c_str(), ecmd.c_str());
}
}
}
/**
Print terminfo key binding names to string buffer used for standard output.
\param all if set, all terminfo key binding names will be
printed. If not set, only ones that are defined for this terminal
are printed.
*/
static void builtin_bind_key_names(int all)
{
const wcstring_list_t names = input_terminfo_get_names(!all);
for (size_t i=0; i<names.size(); i++)
{
const wcstring &name = names.at(i);
append_format(stdout_buffer, L"%ls\n", name.c_str());
}
}
/**
Print all the special key binding functions to string buffer used for standard output.
*/
static void builtin_bind_function_names()
{
wcstring_list_t names = input_function_get_names();
for (size_t i=0; i<names.size(); i++)
{
const wchar_t *seq = names.at(i).c_str();
append_format(stdout_buffer, L"%ls\n", seq);
}
}
/**
Add specified key binding.
*/
static int builtin_bind_add(const wchar_t *seq, const wchar_t *cmd, int terminfo)
{
if (terminfo)
{
wcstring seq2;
if (input_terminfo_get_sequence(seq, &seq2))
{
input_mapping_add(seq2.c_str(), cmd);
}
else
{
switch (errno)
{
case ENOENT:
{
append_format(stderr_buffer, _(L"%ls: No key with name '%ls' found\n"), L"bind", seq);
break;
}
case EILSEQ:
{
append_format(stderr_buffer, _(L"%ls: Key with name '%ls' does not have any mapping\n"), L"bind", seq);
break;
}
default:
{
append_format(stderr_buffer, _(L"%ls: Unknown error trying to bind to key named '%ls'\n"), L"bind", seq);
break;
}
}
return 1;
}
}
else
{
input_mapping_add(seq, cmd);
}
return 0;
}
/**
Erase specified key bindings
\param seq an array of all key bindings to erase
\param all if specified, _all_ key bindings will be erased
*/
static void builtin_bind_erase(wchar_t **seq, int all)
{
if (all)
{
size_t i;
wcstring_list_t lst;
input_mapping_get_names(lst);
for (i=0; i<lst.size(); i++)
{
input_mapping_erase(lst.at(i).c_str());
}
}
else
{
while (*seq)
{
input_mapping_erase(*seq++);
}
}
}
/**
The bind builtin, used for setting character sequences
*/
static int builtin_bind(parser_t &parser, wchar_t **argv)
{
enum
{
BIND_INSERT,
BIND_ERASE,
BIND_KEY_NAMES,
BIND_FUNCTION_NAMES
}
;
int argc=builtin_count_args(argv);
int mode = BIND_INSERT;
int res = STATUS_BUILTIN_OK;
int all = 0;
int use_terminfo = 0;
woptind=0;
static const struct woption
long_options[] =
{
{ L"all", no_argument, 0, 'a' },
{ L"erase", no_argument, 0, 'e' },
{ L"function-names", no_argument, 0, 'f' },
{ L"help", no_argument, 0, 'h' },
{ L"key", no_argument, 0, 'k' },
{ L"key-names", no_argument, 0, 'K' },
{ 0, 0, 0, 0 }
};
while (1)
{
int opt_index = 0;
int opt = wgetopt_long(argc,
argv,
L"aehkKf",
long_options,
&opt_index);
if (opt == -1)
break;
switch (opt)
{
case 0:
if (long_options[opt_index].flag != 0)
break;
append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN,
argv[0],
long_options[opt_index].name);
builtin_print_help(parser, argv[0], stderr_buffer);
return STATUS_BUILTIN_ERROR;
case 'a':
all = 1;
break;
case 'e':
mode = BIND_ERASE;
break;
case 'h':
builtin_print_help(parser, argv[0], stdout_buffer);
return STATUS_BUILTIN_OK;
case 'k':
use_terminfo = 1;
break;
case 'K':
mode = BIND_KEY_NAMES;
break;
case 'f':
mode = BIND_FUNCTION_NAMES;
break;
case '?':
builtin_unknown_option(parser, argv[0], argv[woptind-1]);
return STATUS_BUILTIN_ERROR;
}
}
switch (mode)
{
case BIND_ERASE:
{
builtin_bind_erase(&argv[woptind], all);
break;
}
case BIND_INSERT:
{
switch (argc-woptind)
{
case 0:
{
builtin_bind_list();
break;
}
case 2:
{
builtin_bind_add(argv[woptind], argv[woptind+1], use_terminfo);
break;
}
default:
{
res = STATUS_BUILTIN_ERROR;
append_format(stderr_buffer, _(L"%ls: Expected zero or two parameters, got %d\n"), argv[0], argc-woptind);
break;
}
}
break;
}
case BIND_KEY_NAMES:
{
builtin_bind_key_names(all);
break;
}
case BIND_FUNCTION_NAMES:
{
builtin_bind_function_names();
break;
}
default:
{
res = STATUS_BUILTIN_ERROR;
append_format(stderr_buffer, _(L"%ls: Invalid state\n"), argv[0]);
break;
}
}
return res;
}
/**
The block builtin, used for temporarily blocking events
*/
static int builtin_block(parser_t &parser, wchar_t **argv)
{
enum
{
UNSET,
GLOBAL,
LOCAL,
}
;
int scope=UNSET;
int erase = 0;
int argc=builtin_count_args(argv);
woptind=0;
static const struct woption
long_options[] =
{
{
L"erase", no_argument, 0, 'e'
}
,
{
L"local", no_argument, 0, 'l'
}
,
{
L"global", no_argument, 0, 'g'
}
,
{
L"help", no_argument, 0, 'h'
}
,
{
0, 0, 0, 0
}
}
;
while (1)
{
int opt_index = 0;
int opt = wgetopt_long(argc,
argv,
L"elgh",
long_options,
&opt_index);
if (opt == -1)
break;
switch (opt)
{
case 0:
if (long_options[opt_index].flag != 0)
break;
append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN,
argv[0],
long_options[opt_index].name);
builtin_print_help(parser, argv[0], stderr_buffer);
return STATUS_BUILTIN_ERROR;
case 'h':
builtin_print_help(parser, argv[0], stdout_buffer);
return STATUS_BUILTIN_OK;
case 'g':
scope = GLOBAL;
break;
case 'l':
scope = LOCAL;
break;
case 'e':
erase = 1;
break;
case '?':
builtin_unknown_option(parser, argv[0], argv[woptind-1]);
return STATUS_BUILTIN_ERROR;
}
}
if (erase)
{
if (scope != UNSET)
{
append_format(stderr_buffer, _(L"%ls: Can not specify scope when removing block\n"), argv[0]);
return STATUS_BUILTIN_ERROR;
}
if (parser.global_event_blocks.empty())
{
append_format(stderr_buffer, _(L"%ls: No blocks defined\n"), argv[0]);
return STATUS_BUILTIN_ERROR;
}
parser.global_event_blocks.pop_front();
}
else
{
size_t block_idx = 0;
block_t *block = parser.block_at_index(block_idx);
event_blockage_t eb = {};
eb.typemask = (1<<EVENT_ANY);
switch (scope)
{
case LOCAL:
{
// If this is the outermost block, then we're global
if (block_idx + 1 >= parser.block_count())
{
block = NULL;
}
break;
}
case GLOBAL:
{
block=NULL;
}
case UNSET:
{
while (block != NULL && block->type() != FUNCTION_CALL && block->type() != FUNCTION_CALL_NO_SHADOW)
{
// Set it in function scope
block = parser.block_at_index(++block_idx);
}
}
}
if (block)
{
block->event_blocks.push_front(eb);
}
else
{
parser.global_event_blocks.push_front(eb);
}
}
return STATUS_BUILTIN_OK;
}
/**
The builtin builtin, used for giving builtins precedence over
functions. Mostly handled by the parser. All this code does is some
additional operational modes, such as printing a list of all
builtins, printing help, etc.
*/
static int builtin_builtin(parser_t &parser, wchar_t **argv)
{
int argc=builtin_count_args(argv);
int list=0;
woptind=0;
static const struct woption
long_options[] =
{
{
L"names", no_argument, 0, 'n'
}
,
{
L"help", no_argument, 0, 'h'
}
,
{
0, 0, 0, 0
}
}
;
while (1)
{
int opt_index = 0;
int opt = wgetopt_long(argc,
argv,
L"nh",
long_options,
&opt_index);
if (opt == -1)
break;
switch (opt)
{
case 0:
if (long_options[opt_index].flag != 0)
break;
append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN,
argv[0],
long_options[opt_index].name);
builtin_print_help(parser, argv[0], stderr_buffer);
return STATUS_BUILTIN_ERROR;
case 'h':
builtin_print_help(parser, argv[0], stdout_buffer);
return STATUS_BUILTIN_OK;
case 'n':
list=1;
break;
case '?':
builtin_unknown_option(parser, argv[0], argv[woptind-1]);
return STATUS_BUILTIN_ERROR;
}
}
if (list)
{
wcstring_list_t names = builtin_get_names();
sort(names.begin(), names.end());
for (size_t i=0; i<names.size(); i++)
{
const wchar_t *el = names.at(i).c_str();
stdout_buffer.append(el);
stdout_buffer.append(L"\n");
}
}
return STATUS_BUILTIN_OK;
}
/**
Implementation of the builtin emit command, used to create events.
*/
static int builtin_emit(parser_t &parser, wchar_t **argv)
{
int argc=builtin_count_args(argv);
woptind=0;
static const struct woption
long_options[] =
{
{
L"help", no_argument, 0, 'h'
}
,
{
0, 0, 0, 0
}
}
;
while (1)
{
int opt_index = 0;
int opt = wgetopt_long(argc,
argv,
L"h",
long_options,
&opt_index);
if (opt == -1)
break;
switch (opt)
{
case 0:
if (long_options[opt_index].flag != 0)
break;
append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN,
argv[0],
long_options[opt_index].name);
builtin_print_help(parser, argv[0], stderr_buffer);
return STATUS_BUILTIN_ERROR;
case 'h':
builtin_print_help(parser, argv[0], stdout_buffer);
return STATUS_BUILTIN_OK;
case '?':
builtin_unknown_option(parser, argv[0], argv[woptind-1]);
return STATUS_BUILTIN_ERROR;
}
}
if (!argv[woptind])
{