-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathosh.c
3193 lines (2938 loc) · 68.6 KB
/
osh.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
/*
* osh.c - an enhanced port of the Sixth Edition (V6) UNIX Thompson shell
*/
/*-
* Copyright (c) 2004-2011
* Jeffrey Allen Neitzel <jan (at) v6shell (dot) org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JEFFREY ALLEN NEITZEL ``AS IS'', AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JEFFREY ALLEN NEITZEL BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @(#)$Id$
*/
/*
* Derived from: osh-20061230
* osh.c (1.1 (jneitzel) 2006/12/26)
* sh6.c (1.3 (jneitzel) 2006/09/15)
* glob6.c (1.3 (jneitzel) 2006/09/23)
*/
/*-
* Copyright (C) Caldera International Inc. 2001-2002. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code and documentation must retain the above
* copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed or owned by Caldera
* International, Inc.
* 4. Neither the name of Caldera International, Inc. nor the names of other
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
* INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define OSH_SHELL
#include "defs.h"
#include "err.h"
#include "pexec.h"
#include "sasignal.h"
#include "sh.h"
#ifdef __GNUC__
#define IS_UNUSED __attribute__((__unused__))
#else
#define IS_UNUSED /* nothing */
#endif
/*
* The following file descriptors are reserved for special use by osh.
*/
#define DUPFD0 10 /* used for input redirection w/ `<-' */
#define HWFD 11 /* used for history write fildes */
#define PWD 12 /* used in do_chdir() */
#define SAVFD0 13 /* used in do_source() */
/*
* These are the rc (init and logout) files used by osh.
* The `FILE_DOT_*' files are in user's HOME directory.
*/
#define DO_SYSTEM_LOGIN 1
#define PATH_SYSTEM_LOGIN SYSCONFDIR/**/"/osh.login"
#define DO_SYSTEM_OSHRC 2
#define PATH_SYSTEM_OSHRC SYSCONFDIR/**/"/osh.oshrc"
#define DO_DOT_LOGIN 3
#define FILE_DOT_LOGIN ".osh.login"
#define DO_DOT_OSHRC 4
#define FILE_DOT_OSHRC ".oshrc"
#define DO_INIT_DONE 5
#define DO_SYSTEM_LOGOUT 1
#define PATH_SYSTEM_LOGOUT SYSCONFDIR/**/"/osh.logout"
#define DO_DOT_LOGOUT 2
#define FILE_DOT_LOGOUT ".osh.logout"
#define DO_LOGOUT_DONE 3
/*
* This is the history file (in user's HOME directory) used by osh.
*/
#define FILE_DOT_HISTORY ".osh.history"
/*
* These are the symbolic names for the types checked by fd_type().
*/
#define FD_TMASK S_IFMT /* file descriptor (FD) type mask */
#define FD_ISOPEN 01 /* Does FD refer to an open file? */
#define FD_ISDIROK 02 /* Does FD refer to an existent directory? */
#define FD_ISREGOK 04 /* Does FD refer to an existent regular file? */
#define FD_ISDIR S_IFDIR /* Does FD refer to a directory? */
#define FD_ISREG S_IFREG /* Does FD refer to a regular file? */
/*
* (NSIG - 1) is the maximum signal number value accepted by `sigign'.
*/
#ifndef NSIG
#define NSIG 32
#endif
#define HALT true
#define PROMPT ((shtype & ST_MASK) == ST_INTERACTIVE)
#define SHTYPE(f) ((shtype & (f)) != 0)
#define DO_TRIM(k) ((k) != SBI_CD && (k) != SBI_CHDIR)
/*
* shell type flags
*/
enum stflags {
ST_ONELINE = 001,
ST_COMMANDFILE = 002,
ST_INTERACTIVE = 004,
ST_RCFILE = 010,
ST_SOURCE = 020,
ST_MASK = 037
};
/*
* **** Global Variables ****
*/
uid_t sheuid; /* effective shell user ID */
/*
* shell sbi command structure array
*/
static const struct sbicmd {
const char *sbi_command;
const enum sbikey sbi_key;
} sbi[] = {
{ ":", SBI_NULL },
{ "alias", SBI_ALIAS },
{ "cd", SBI_CD },
{ "chdir", SBI_CHDIR },
{ "echo", SBI_ECHO },
{ "exec", SBI_EXEC },
{ "exit", SBI_EXIT },
{ "fd2", SBI_FD2 },
{ "goto", SBI_GOTO },
{ "if", SBI_IF },
{ "login", SBI_LOGIN },
{ "newgrp", SBI_NEWGRP },
{ "setenv", SBI_SETENV },
{ "shift", SBI_SHIFT },
{ "sigign", SBI_SIGIGN },
{ "source", SBI_SOURCE },
{ "umask", SBI_UMASK },
{ "unalias", SBI_UNALIAS },
{ "unsetenv", SBI_UNSETENV },
{ "wait", SBI_WAIT }
};
#define NSBICMD ((int)(sizeof(sbi) / sizeof(sbi[0])))
/*
* shell signal messages
*/
static const char *const sigmsg[] = {
" -- Core dumped",
"Hangup",
"",
"Quit",
"Illegal instruction",
"Trace/BPT trap",
"IOT trap",
"EMT trap",
"Floating exception",
"Killed",
"Bus error",
"Memory fault",
"Bad system call",
"Broken pipe"
};
#define NSIGMSG ((int)(sizeof(sigmsg) / sizeof(sigmsg[0])))
/*@null@*/
static const char *argv2p; /* string for `-c' option */
static int dolac; /* $* dollar-argument count */
static char dolbuf[DOLMAX]; /* dollar buffer for $$, $n, $s, $v */
static int dolc; /* $N dollar-argument count */
/*@null@*/ /*@only@*/
static const char *dolp; /* $ dollar-value pointer */
static char *const *dolv; /* $N dollar-argument value array */
static int dupfd0; /* duplicate of the standard input */
static bool error; /* error flag for read/parse errors */
/*@observer@*/
static const char *error_message; /* error msg for read/parse errors */
static bool error_source; /* error flag for `source' command */
static int hwfd = -1; /* history write file descriptor */
static bool is_login; /* login shell flag (true if login) */
static char line[LINEMAX]; /* command-line buffer */
static char aline[LINEMAX]; /* alias-line buffer */
static char *linep;
static char *elinep;
static volatile sig_atomic_t
logout_now; /* SIGHUP caught flag (1 if caught) */
static const char *name; /* $0 - shell command name */
static int nul_count; /* `\0'-character count (per line) */
static int peekc; /* just-read, pushed-back character */
static enum stflags shtype; /* shell type (determines behavior) */
static enum sigflags sig_child; /* SIG(INT|QUIT|TERM) child flags */
static enum sigflags sig_state; /* SIG(INT|QUIT|TERM) state flags */
static int status; /* shell exit status */
static char *word[WORDMAX]; /* arg/word pointer array */
static char *aword[WORDMAX];/* alias arg/word pointer array */
static char **wordp;
static char **ewordp;
static struct anode *anp; /* shell alias node pointer */
/*@null@*/
static const char *asp; /* shell alias string pointer */
/*@only@*/
static struct tnode *tnp; /* shell command tree node pointer */
/*@null@*/ /*@only@*/
static char *tty; /* $t - terminal name */
/*@null@*/ /*@only@*/
static char *user; /* $u - effective user name */
static bool verbose_flag; /* verbose flag for `-v' option */
/*
* **** Function Prototypes ****
*/
static void cmd_loop(bool);
static void cmd_verbose(void);
static int rpx_line(void);
static const char **rp_alias(const char *);
static int get_word(void);
static int xgetc(bool);
static int readc(void);
/*@null@*/ /*@only@*/
static const char *get_dolp(int);
static void aalloc(const char *, const char *);
static struct anode *aalloc1(const char *, const char *);
static void afree(/*@null@*/ const char *);
static const char *asget(const char *);
static struct tnode *talloc(void);
static void tfree(/*@null@*/ /*@only@*/ struct tnode *);
/*@null@*/
static struct tnode *syntax(char **, char **);
/*@null@*/
static struct tnode *syn1(char **, char **);
/*@null@*/
static struct tnode *syn2(char **, char **);
/*@null@*/
static struct tnode *syn3(char **, char **);
static bool any(int, const char *);
static bool vtglob(char **);
static void vtrim(char **);
static int vacount(const char **);
static void execute(/*@null@*/ struct tnode *,
/*@null@*/ int *, /*@null@*/ int *);
static void execute1(struct tnode *);
static void execute2(struct tnode *,
/*@null@*/ int *, /*@null@*/ int *);
static void do_chdir(char **);
static void do_sigign(char **, enum tnflags);
static void set_ss_flags(int, action_type);
static void do_source(char **);
static void pwait(pid_t);
static int prsig(int, pid_t, pid_t);
/*@maynotreturn@*/
static void sh_errexit(int);
static void sh_init(/*@null@*/ const char *);
static void sh_magic(void);
static bool sh_on_tty(void);
static void sighup(/*@unused@*/ int IS_UNUSED);
static void rc_init(int *);
static void rc_logout(int *);
/*@maynotreturn@*/
static bool rc_open(/*@null@*/ const char *);
static char *pn_build(/*@out@*/ /*@returned@*/ char *,
const char *, size_t);
static void hist_write(bool);
static void hist_open(void);
static void fd_free(void);
static bool fd_type(int, mode_t);
/*@maynotreturn@*/ /*@null@*/
static char *atrim(/*@returned@*/ UChar *);
/*@null@*/
static char *gtrim(/*@returned@*/ UChar *);
/*@null@*/
static char *gchar(/*@returned@*/ const char *);
static void vfree(/*@null@*/ char **);
static void xfree(/*@null@*/ /*@only@*/ void *);
/*@out@*/
static void *xmalloc(size_t);
static void *xrealloc(/*@only@*/ void *, size_t);
static char *xstrdup(const char *);
/*@maynotreturn@*/ /*@null@*/
static const char **glob(enum sbikey, char **);
/*
* NAME
* osh - old shell (command interpreter)
*
* SYNOPSIS
* osh [-v] [- | -c [string] | -i | -l | -t | file [arg1 ...]]
*
* DESCRIPTION
* See the osh(1) manual page for full details.
*/
int
main(int argc, char **argv)
{
char *av0p;
int rcflag = 0;
bool dosigs = false;
sh_init(argv[0]);
if (argv[0] == NULL || *argv[0] == EOS)
err(SH_ERR, FMT2S, getmyname(), ERR_ALINVAL);
if (fd_type(FD0, FD_ISDIR))
goto done;
if (argc > 1 && *argv[1] == HYPHEN && argv[1][1] == 'v') {
verbose_flag = true;
av0p = argv[0], argv = &argv[1], argv[0] = av0p;
argc--;
}
if (argc > 1) {
name = argv[1];
dolv = &argv[1];
dolc = argc - 1;
if (*argv[1] == HYPHEN) {
dosigs = true;
if (argv[1][1] == 'c' && argc > 2) {
shtype = ST_ONELINE;
dolv += 1;
dolc -= 1;
argv2p = argv[2];
} else if (argv[1][1] == 'i') {
rcflag = DO_SYSTEM_OSHRC;
shtype = ST_INTERACTIVE;
if (!sh_on_tty())
err(SH_ERR, FMT3S,
getmyname(), argv[1], ERR_NOTTY);
} else if (argv[1][1] == 'l') {
is_login = true;
rcflag = DO_SYSTEM_LOGIN;
shtype = ST_INTERACTIVE;
if (!sh_on_tty())
err(SH_ERR, FMT3S,
getmyname(), argv[1], ERR_NOTTY);
} else if (argv[1][1] == 't')
shtype = ST_ONELINE;
} else {
shtype = ST_COMMANDFILE;
(void)close(FD0);
if (open(argv[1], O_RDONLY) != FD0)
err(SH_ERR,FMT3S,getmyname(),argv[1],ERR_OPEN);
if (fd_type(FD0, FD_ISDIR))
goto done;
}
fd_free();
} else {
dosigs = true;
fd_free();
if (sh_on_tty())
shtype = ST_INTERACTIVE;
}
if (dosigs) {
if (sasignal(SIGINT, SIG_IGN) == SIG_DFL)
sig_child |= S_SIGINT;
if (sasignal(SIGQUIT, SIG_IGN) == SIG_DFL)
sig_child |= S_SIGQUIT;
if (PROMPT) {
if (sasignal(SIGTERM, SIG_IGN) == SIG_DFL)
sig_child |= S_SIGTERM;
if (rcflag == 0) {
if (*argv[0] == HYPHEN) {
is_login = true;
rcflag = DO_SYSTEM_LOGIN;
} else
rcflag = DO_SYSTEM_OSHRC;
}
if (is_login)
if (sasignal(SIGHUP, sighup) == SIG_IGN)
(void)sasignal(SIGHUP, SIG_IGN);
rc_init(&rcflag);
hist_open();
}
}
if (SHTYPE(ST_ONELINE))
(void)rpx_line();
else {
/* Read and execute any rc init files if needed. */
while (SHTYPE(ST_RCFILE)) {
cmd_loop(!HALT);
if (logout_now != 0) {
logout_now = 0;
if (is_login)
goto logout;
goto done;
}
rc_init(&rcflag);
}
/* Read and execute the shell's input. */
cmd_loop(!HALT);
if (logout_now != 0)
logout_now = 0;
logout:
/* Read and execute any rc logout files if needed. */
rcflag = DO_SYSTEM_LOGOUT;
rc_logout(&rcflag);
while (SHTYPE(ST_RCFILE)) {
cmd_loop(!HALT);
if (logout_now != 0)
logout_now = 0;
rc_logout(&rcflag);
}
}
done:
xfree(tty);
xfree(user);
afree(NULL);
return status;
}
/*
* Determine whether or not the string pointed to by cmd
* is a special built-in command. Return the key value.
*/
enum sbikey
cmd_lookup(const char *cmd)
{
const struct sbicmd *lp, *mp, *rp;
int d;
for (lp = sbi, rp = &sbi[NSBICMD]; lp < rp; /* nothing */) {
mp = lp + (rp - lp) / 2;
if ((d = strcmp(cmd, mp->sbi_command)) == 0)
return mp->sbi_key;
if (d > 0)
lp = mp + 1;
else
rp = mp;
}
return SBI_UNKNOWN;
}
/*
* Read and execute command lines until EOF, or until one of
* error_source or logout_now != 0 is true according to halt
* or is_login.
*/
static void
cmd_loop(bool halt)
{
bool gz;
sh_magic();
for (error_source = gz = false; ; ) {
if (PROMPT)
fd_print(FD2, "%s", (sheuid != 0) ? "% " : "# ");
if (rpx_line() == EOF) {
if (!gz)
status = SH_TRUE;
break;
}
if (halt && error_source)
break;
if (is_login && logout_now != 0)
break;
gz = true;
}
}
/*
* If verbose_flag is true, print each argument/word
* in the word pointer array to the standard error.
* Otherwise, do nothing.
*/
static void
cmd_verbose(void)
{
char **vp;
if (!verbose_flag)
return;
for (vp = word; **vp != EOL; vp++)
fd_print(FD2, "%s%s", *vp, (**(vp + 1) != EOL) ? " " : "");
fd_print(FD2, FMT1S, "");
}
/*
* Read, parse, and execute a command line.
*/
static int
rpx_line(void)
{
struct tnode *t;
sigset_t nmask, omask;
char *wp;
linep = line;
elinep = &line[LINEMAX - 5];
wordp = word;
ewordp = &word[WORDMAX - 5];
error = false;
error_message = NULL;
nul_count = 0;
do {
wp = linep;
if (get_word() == EOF)
return EOF;
} while (*wp != EOL);
*wordp = NULL;
cmd_verbose();
hist_write(PROMPT && wordp - word > 1);
if (error) {
err(-1, FMT2S, getmyname(), error_message);
return 1;
}
if (wordp - word > 1) {
(void)sigfillset(&nmask);
(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
t = tnp;
tnp = NULL;
tnp = syntax(word, wordp);
(void)sigprocmask(SIG_SETMASK, &omask, NULL);
if (error)
err(-1, FMT2S, getmyname(), error_message);
else
execute(tnp, NULL, NULL);
tfree(tnp);
tnp = NULL;
tnp = t;
}
return 1;
}
/*
* Read and parse the alias string specified by string.
* Return a pointer to aword on success.
* Return a pointer to NULL on error.
*/
static const char **
rp_alias(const char *string)
{
struct tnode *t;
sigset_t nmask, omask;
char *wp;
asp = string;
linep = aline;
elinep = &aline[LINEMAX - 5];
wordp = aword;
ewordp = &aword[WORDMAX - 5];
error = false;
error_message = NULL;
nul_count = 0;
do {
wp = linep;
if (get_word() == EOF) {
error_message = ERR_SYNTAX;/* same as: ([...] \) */
asp = NULL;
return NULL;
}
} while (*wp != EOL);
*wordp = NULL;
if (error) {
asp = NULL;
return NULL;
}
if (wordp - aword > 1) {
if (any(**aword, ";&")) {
error_message = ERR_SYNTAX;/* same as: (;) or (&) */
asp = NULL;
return NULL;
}
(void)sigfillset(&nmask);
(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
t = NULL;
t = syntax(aword, wordp);
(void)sigprocmask(SIG_SETMASK, &omask, NULL);
if (error) {
tfree(t);
t = NULL;
asp = NULL;
return NULL;
}
tfree(t);
t = NULL;
asp = NULL;
return (const char **)aword;
}
error_message = ERR_SYNTAX;/* same as: () */
asp = NULL;
return NULL;
}
/*
* Copy a word from the standard input into the [a]line buffer,
* and point to it w/ *wordp. Each copied word is represented
* in [a]line as an individual `\0'-terminated string.
*/
static int
get_word(void)
{
int c, c1;
*wordp++ = linep;
for (;;) {
switch (c = xgetc(DOLSUB)) {
case EOF:
return EOF;
case SPACE:
case TAB:
continue;
case DQUOT:
case SQUOT:
c1 = c;
*linep++ = c;
while ((c = xgetc(!DOLSUB)) != c1) {
if (c == EOF)
return EOF;
if (c == EOL) {
if (!error)
error_message = ERR_SYNTAX;
peekc = c;
*linep++ = EOS;
error = true;
return 1;
}
if (c == BQUOT) {
if ((c = xgetc(!DOLSUB)) == EOF)
return EOF;
if (c == EOL)
c = SPACE;
else {
peekc = c;
c = BQUOT;
}
}
*linep++ = c;
}
*linep++ = c;
break;
case BQUOT:
if ((c = xgetc(!DOLSUB)) == EOF)
return EOF;
if (c == EOL)
continue;
*linep++ = BQUOT;
*linep++ = c;
break;
case LPARENTHESIS: case RPARENTHESIS:
case SEMICOLON: case AMPERSAND:
case VERTICALBAR: case CARET:
case LESSTHAN: case GREATERTHAN:
case EOL:
*linep++ = c;
*linep++ = EOS;
return 1;
default:
peekc = c;
}
for (;;) {
if ((c = xgetc(DOLSUB)) == EOF)
return EOF;
if (c == BQUOT) {
if ((c = xgetc(!DOLSUB)) == EOF)
return EOF;
if (c == EOL)
c = SPACE;
else {
*linep++ = BQUOT;
*linep++ = c;
continue;
}
}
if (any(c, WORDPACK)) {
peekc = c;
if (any(c, QUOTPACK))
break;
*linep++ = EOS;
return 1;
}
*linep++ = c;
}
}
/*NOTREACHED*/
}
/*
* If dolsub is true, get either the next literal character from the
* standard input or substitute the current $ dollar w/ the next
* character of its value, which is pointed to by dolp. Otherwise,
* get only the next literal character from the standard input.
*/
static int
xgetc(bool dolsub)
{
int c;
if (peekc != EOS) {
c = peekc;
peekc = EOS;
return c;
}
if (wordp >= ewordp) {
wordp -= 10;
while ((c = xgetc(!DOLSUB)) != EOF && c != EOL)
; /* nothing */
wordp += 10;
error_message = ERR_TMARGS;
goto geterr;
}
if (linep >= elinep) {
linep -= 10;
while ((c = xgetc(!DOLSUB)) != EOF && c != EOL)
; /* nothing */
linep += 10;
error_message = ERR_TMCHARS;
goto geterr;
}
getd:
if (dolp != NULL) {
c = *dolp++;
if (c != EOS)
return c;
if (dolac > 0 && ++dolac < dolc) {
dolp = dolv[dolac];
return SPACE;
}
dolac = 0;
dolp = NULL;
}
c = readc();
if (c == DOLLAR && dolsub) {
c = readc();
if (c == ASTERISK) {
if (dolc > 1) {
dolac = 1;
dolp = dolv[dolac];
}
goto getd;
}
if ((dolp = get_dolp(c)) != NULL)
goto getd;
}
/* Ignore all EOS/NUL characters. */
if (c == EOS) do {
if (++nul_count >= LINEMAX) {
error_message = ERR_TMCHARS;
goto geterr;
}
c = readc();
} while (c == EOS);
return c;
geterr:
error = true;
return EOL;
}
/*
* Read and return a character from the string pointed to by asp
* or argv2p or from the standard input. When reading from asp
* or argv2p, return the character, `\n', or EOF. When reading
* from the standard input, return the character or EOF.
*/
static int
readc(void)
{
UChar c;
if (asp != NULL) {
if (asp == (char *)-1)
return EOF;
if ((c = UCHAR(*asp++)) == EOS) {
asp = (char *)-1;
c = UCHAR(EOL);
}
return c;
}
if (argv2p != NULL) {
if (argv2p == (char *)-1)
return EOF;
if ((c = UCHAR(*argv2p++)) == EOS) {
argv2p = (char *)-1;
c = UCHAR(EOL);
}
return c;
}
if (read(FD0, &c, (size_t)1) != 1)
return EOF;
return c;
}
/*
* Return a pointer to the appropriate $ dollar value on success.
* Return a NULL pointer on error.
*/
static const char *
get_dolp(int c)
{
int n, r;
const char *v;
*dolbuf = EOS;
switch (c) {
case DOLLAR:
r = snprintf(dolbuf,sizeof(dolbuf),"%05u",(unsigned)getmypid());
v = (r < 0 || r >= (int)sizeof(dolbuf)) ? NULL : dolbuf;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
n = c - '0';
if (IS_DIGIT(n, c) && n < dolc)
v = (n > 0) ? dolv[n] : name;
else
v = dolbuf;
break;
case 'd':
if ((v = getenv("OSHDIR")) == NULL)
v = dolbuf;
break;
case 'e':
if ((v = getenv("EXECSHELL")) == NULL)
v = dolbuf;
break;
case 'h':
if ((v = getenv("HOME")) == NULL)
v = dolbuf;
break;
case 'm':
if ((v = getenv("MANPATH")) == NULL)
v = dolbuf;
break;
case HASH:
case 'n':
n = (dolc > 1) ? dolc - 1 : 0;
r = snprintf(dolbuf, sizeof(dolbuf), "%u", (unsigned)n);
v = (r < 0 || r >= (int)sizeof(dolbuf)) ? NULL : dolbuf;
break;
case 'p':
if ((v = getenv("PATH")) == NULL)
v = dolbuf;
break;
case 's':
r = snprintf(dolbuf, sizeof(dolbuf), "%u", (unsigned)status);
v = (r < 0 || r >= (int)sizeof(dolbuf)) ? NULL : dolbuf;
break;
case 't':
v = tty;
break;
case 'u':
v = user;
break;
case 'v':
r = snprintf(dolbuf, sizeof(dolbuf), "%s", OSH_VERSION);
v = (r < 0 || r >= (int)sizeof(dolbuf)) ? NULL : dolbuf;
break;
default:
v = NULL;
}
return v;
}
/*
* Allocate memory for new alias node (specified by name and string)
* if needed, and link it to alias node list. Insert new alias, or
* replace old alias string w/ new alias string if needed.
*/
static void
aalloc(const char *name, const char *string)
{
struct anode *a, *n, *p;
int d;
if (name == NULL || *name == EOS || string == NULL || *string == EOS)
return;
a = anp;
if (a == NULL) { /* First */
anp = aalloc1(name, string);
return;
}
p = a;
/* ascending ASCII sort */
while (a != NULL) {
if ((d = strcmp(name, a->name)) == 0) {
if (!EQUAL(string, a->string)) {
/* Replace old string w/ new string. */
xfree(a->string);
a->string = xstrdup(string);
}
return;
}
if (d < 0)
break;
p = a;
a = a->next;
}
if (a == NULL) { /* Last */
p->next = aalloc1(name, string);
return;
}
/* Insert new alias between p and a (ascending ASCII insert). */
if (a == anp) { /* New Head */
n = aalloc1(name, string);
n->next = anp;
anp = n;
return;
}
n = aalloc1(name, string);
n->next = p->next;
p->next = n;
}
/*
* Allocate and initialize memory for new alias node
* specified by name and string. Return pointer to new
* node on success. Do not return on error (ENOMEM).
*/
static struct anode *
aalloc1(const char *name, const char *string)
{
struct anode *a;
a = xmalloc(sizeof(struct anode));
a->next = NULL;
a->name = xstrdup(name);
a->string = xstrdup(string);
return a;
}
/*
* If name is specified (is not NULL), free its alias.
* If name is not specified (is NULL), free all aliases.
*/
static void
afree(const char *name)
{
struct anode *a, *p;
if (name != NULL) {
a = anp;
p = a;
while (a != NULL) {
if (EQUAL(name, a->name)) {
if (a == anp)
anp = a->next;
else
p->next = a->next;
xfree(a->name);
xfree(a->string);
xfree(a);