-
Notifications
You must be signed in to change notification settings - Fork 1
/
sh.c
2564 lines (2307 loc) · 62.2 KB
/
sh.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
/*
* sh.c: Main shell routines
*/
/*-
* Copyright (c) 1980, 1991 The Regents of the University of California.
* 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS OR CONTRIBUTORS 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 EXTERN /* Intern */
#include "sh.h"
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#include "tc.h"
#include "ed.h"
#include "tw.h"
extern int MapsAreInited;
extern int NLSMapsAreInited;
/*
* C Shell
*
* Bill Joy, UC Berkeley, California, USA
* October 1978, May 1980
*
* Jim Kulp, IIASA, Laxenburg, Austria
* April 1980
*
* Filename recognition added:
* Ken Greer, Ind. Consultant, Palo Alto CA
* October 1983.
*
* Karl Kleinpaste, Computer Consoles, Inc.
* Added precmd, periodic/tperiod, prompt changes,
* directory stack hack, and login watch.
* Sometime March 1983 - Feb 1984.
*
* Added scheduled commands, including the "sched" command,
* plus the call to sched_run near the precmd et al
* routines.
* Upgraded scheduled events for running events while
* sitting idle at command input.
*
* Paul Placeway, Ohio State
* added stuff for running with twenex/inputl 9 Oct 1984.
*
* ported to Apple Unix (TM) (OREO) 26 -- 29 Jun 1987
*/
jmp_buf_t reslab IZERO_STRUCT;
struct wordent paraml IZERO_STRUCT;
static const char tcshstr[] = "tcsh";
struct sigaction parintr; /* Parents interrupt catch */
struct sigaction parterm; /* Parents terminate catch */
#ifdef TESLA
int do_logout = 0;
#endif /* TESLA */
int use_fork = 0; /* use fork() instead of vfork()? */
/*
* Magic pointer values. Used to specify other invalid conditions aside
* from null.
*/
static Char INVCHAR;
Char *INVPTR = &INVCHAR;
Char **INVPPTR = &INVPTR;
static int fast = 0;
static int mflag = 0;
static int prompt = 1;
int enterhist = 0;
int tellwhat = 0;
time_t t_period;
Char *ffile = NULL;
int dolzero = 0;
int insource = 0;
int exitset = 0;
static time_t chktim; /* Time mail last checked */
char *progname;
int tcsh;
/*
* This preserves the input state of the shell. It is used by
* st_save and st_restore to manupulate shell state.
*/
struct saved_state {
int insource;
int OLDSTD;
int SHIN;
int SHOUT;
int SHDIAG;
int intty;
struct whyle *whyles;
Char *gointr;
Char *arginp;
Char *evalp;
Char **evalvec;
Char *alvecp;
Char **alvec;
int onelflg;
int enterhist;
Char **argv;
Char **av;
Char HIST;
int cantell;
struct Bin B;
int justpr;
};
static int srccat (Char *, Char *);
#ifndef WINNT_NATIVE
static int srcfile (const char *, int, int, Char **);
#else
int srcfile (const char *, int, int, Char **);
#endif /*WINNT_NATIVE*/
static void srcunit (int, int, int, Char **);
static void mailchk (void);
#ifndef _PATH_DEFPATH
static Char **defaultpath (void);
#endif
static void record (void);
static void st_save (struct saved_state *, int, int,
Char **, Char **);
static void st_restore (void *);
int main (int, char **);
#ifndef LOCALEDIR
#define LOCALEDIR "/usr/share/locale"
#endif
#ifdef NLS_CATALOGS
static void
add_localedir_to_nlspath(const char *path)
{
static const char msgs_LOC[] = "/%L/LC_MESSAGES/%N.cat";
static const char msgs_lang[] = "/%l/LC_MESSAGES/%N.cat";
char *old;
char *new, *new_p;
size_t len;
int add_LOC = 1;
int add_lang = 1;
char trypath[MAXPATHLEN];
struct stat st;
if (path == NULL)
return;
(void) xsnprintf(trypath, sizeof(trypath), "%s/C/LC_MESSAGES/tcsh.cat",
path);
if (stat(trypath, &st) == -1)
return;
if ((old = getenv("NLSPATH")) != NULL)
len = strlen(old) + 1; /* don't forget the colon. */
else
len = 0;
len += 2 * strlen(path) +
sizeof(msgs_LOC) + sizeof(msgs_lang); /* includes the extra colon */
new = new_p = xcalloc(len, 1);
if (old != NULL) {
size_t pathlen = strlen(path);
char *old_p;
(void) xsnprintf(new_p, len, "%s", old);
new_p += strlen(new_p);
len -= new_p - new;
/* Check if the paths we try to add are already present in NLSPATH.
If so, note it by setting the appropriate flag to 0. */
for (old_p = old; old_p; old_p = strchr(old_p, ':'),
old_p = old_p ? old_p + 1 : NULL) {
if (strncmp(old_p, path, pathlen) != 0)
continue;
if (strncmp(old_p + pathlen, msgs_LOC, sizeof(msgs_LOC) - 1) == 0)
add_LOC = 0;
else if (strncmp(old_p + pathlen, msgs_lang,
sizeof(msgs_lang) - 1) == 0)
add_lang = 0;
}
}
/* Add the message catalog paths not already present to NLSPATH. */
if (add_LOC || add_lang)
(void) xsnprintf(new_p, len, "%s%s%s%s%s%s",
old ? ":" : "",
add_LOC ? path : "", add_LOC ? msgs_LOC : "",
add_LOC && add_lang ? ":" : "",
add_lang ? path : "", add_lang ? msgs_lang : "");
tsetenv(STRNLSPATH, str2short(new));
free(new);
}
#endif
int
main(int argc, char **argv)
{
int batch = 0;
volatile int nexececho = 0;
int nofile = 0;
volatile int nverbose = 0;
volatile int rdirs = 0;
volatile int exitcode = 0;
int quitit = 0;
Char *cp;
#ifdef AUTOLOGOUT
Char *cp2;
#endif
char *tcp, *ttyn;
int f, reenter;
char **tempv;
static const char *targinp = NULL;
int osetintr;
struct sigaction oparintr;
#ifdef WINNT_NATIVE
nt_init();
#endif /* WINNT_NATIVE */
(void)memset(&reslab, 0, sizeof(reslab));
#if defined(NLS_CATALOGS) && defined(LC_MESSAGES)
(void) setlocale(LC_MESSAGES, "");
#endif /* NLS_CATALOGS && LC_MESSAGES */
#ifdef NLS
# ifdef LC_CTYPE
(void) setlocale(LC_CTYPE, ""); /* for iscntrl */
# endif /* LC_CTYPE */
#endif /* NLS */
STR_environ = blk2short(environ);
environ = short2blk(STR_environ); /* So that we can free it */
#ifdef NLS_CATALOGS
add_localedir_to_nlspath(LOCALEDIR);
#endif
nlsinit();
initlex(¶ml);
#ifdef MALLOC_TRACE
mal_setstatsfile(fdopen(dmove(xopen("/tmp/tcsh.trace",
O_WRONLY|O_CREAT|O_LARGEFILE, 0666), 25), "w"));
mal_trace(1);
#endif /* MALLOC_TRACE */
#if !(defined(BSDTIMES) || defined(_SEQUENT_)) && defined(POSIX)
# ifdef _SC_CLK_TCK
clk_tck = (clock_t) sysconf(_SC_CLK_TCK);
# else /* ! _SC_CLK_TCK */
# ifdef CLK_TCK
clk_tck = CLK_TCK;
# else /* !CLK_TCK */
clk_tck = HZ;
# endif /* CLK_TCK */
# endif /* _SC_CLK_TCK */
#endif /* !BSDTIMES && POSIX */
settimes(); /* Immed. estab. timing base */
#ifdef TESLA
do_logout = 0;
#endif /* TESLA */
/*
* Make sure we have 0, 1, 2 open
* Otherwise `` jobs will not work... (From knaff@poly.polytechnique.fr)
*/
{
do
if ((f = xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE)) == -1 &&
(f = xopen("/", O_RDONLY|O_LARGEFILE)) == -1)
exit(1);
while (f < 3);
xclose(f);
}
osinit(); /* Os dependent initialization */
{
char *t;
t = strrchr(argv[0], '/');
#ifdef WINNT_NATIVE
{
char *s = strrchr(argv[0], '\\');
if (s)
t = s;
}
#endif /* WINNT_NATIVE */
t = t ? t + 1 : argv[0];
if (*t == '-') t++;
progname = strsave((t && *t) ? t : tcshstr); /* never want a null */
tcsh = strncmp(progname, tcshstr, sizeof(tcshstr) - 1) == 0;
}
/*
* Initialize non constant strings
*/
#ifdef _PATH_BSHELL
STR_BSHELL = SAVE(_PATH_BSHELL);
#endif
#ifdef _PATH_TCSHELL
STR_SHELLPATH = SAVE(_PATH_TCSHELL);
#else
# ifdef _PATH_CSHELL
STR_SHELLPATH = SAVE(_PATH_CSHELL);
# endif
#endif
STR_WORD_CHARS = SAVE(WORD_CHARS);
STR_WORD_CHARS_VI = SAVE(WORD_CHARS_VI);
HIST = '!';
HISTSUB = '^';
PRCH = tcsh ? '>' : '%'; /* to replace %# in $prompt for normal users */
PRCHROOT = '#'; /* likewise for root */
word_chars = STR_WORD_CHARS;
bslash_quote = 0; /* PWP: do tcsh-style backslash quoting? */
anyerror = 1; /* for compatibility */
setcopy(STRanyerror, STRNULL, VAR_READWRITE);
/* Default history size to 100 */
setcopy(STRhistory, str2short("100"), VAR_READWRITE);
sethistory(100);
tempv = argv;
ffile = SAVE(tempv[0]);
dolzero = 0;
if (eq(ffile, STRaout)) /* A.out's are quittable */
quitit = 1;
uid = getuid();
gid = getgid();
euid = geteuid();
egid = getegid();
/*
* We are a login shell if: 1. we were invoked as -<something> with
* optional arguments 2. or we were invoked only with the -l flag
*/
loginsh = (**tempv == '-') || (argc == 2 &&
tempv[1][0] == '-' && tempv[1][1] == 'l' &&
tempv[1][2] == '\0');
#ifdef _VMS_POSIX
/* No better way to find if we are a login shell */
if (!loginsh) {
loginsh = (argc == 1 && getppid() == 1);
**tempv = '-'; /* Avoid giving VMS an acidic stomach */
}
#endif /* _VMS_POSIX */
if (loginsh && **tempv != '-') {
char *argv0;
/*
* Mangle the argv space
*/
tempv[1][0] = '\0';
tempv[1][1] = '\0';
tempv[1] = NULL;
argv0 = strspl("-", *tempv);
*tempv = argv0;
argc--;
}
if (loginsh) {
(void) time(&chktim);
setNS(STRloginsh);
}
NoNLSRebind = getenv("NOREBIND") != NULL;
#ifdef NLS
# ifdef SETLOCALEBUG
dont_free = 1;
# endif /* SETLOCALEBUG */
(void) setlocale(LC_ALL, "");
# ifdef LC_COLLATE
(void) setlocale(LC_COLLATE, "");
# endif
# ifdef SETLOCALEBUG
dont_free = 0;
# endif /* SETLOCALEBUG */
# ifdef STRCOLLBUG
fix_strcoll_bug();
# endif /* STRCOLLBUG */
/*
* On solaris ISO8859-1 contains no printable characters in the upper half
* so we need to test only for MB_CUR_MAX == 1, otherwise for multi-byte
* locales we are always AsciiOnly == 0.
*/
if (MB_CUR_MAX == 1) {
int k;
for (k = 0200; k <= 0377 && !isprint(CTL_ESC(k)); k++)
continue;
AsciiOnly = k > 0377;
} else
AsciiOnly = 0;
#else
AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL;
#endif /* NLS */
if (MapsAreInited && !NLSMapsAreInited)
ed_InitNLSMaps();
ResetArrowKeys();
/*
* Initialize for periodic command intervals. Also, initialize the dummy
* tty list for login-watch.
*/
(void) time(&t_period);
#ifndef HAVENOUTMP
initwatch();
#endif /* !HAVENOUTMP */
#if defined(alliant)
/*
* From: Jim Pace <jdp@research.att.com>
* tcsh does not work properly on the alliants through an rlogin session.
* The shell generally hangs. Also, reference to the controlling terminal
* does not work ( ie: echo foo > /dev/tty ).
*
* A security feature was added to rlogind affecting FX/80's Concentrix
* from revision 5.5.xx upwards (through 5.7 where this fix was implemented)
* This security change also affects the FX/2800 series.
* The security change to rlogind requires the process group of an rlogin
* session become disassociated with the tty in rlogind.
*
* The changes needed are:
* 1. set the process group
* 2. reenable the control terminal
*/
if (loginsh && isatty(SHIN)) {
ttyn = ttyname(SHIN);
xclose(SHIN);
SHIN = xopen(ttyn, O_RDWR|O_LARGEFILE);
shpgrp = getpid();
(void) ioctl (SHIN, TIOCSPGRP, (ioctl_t) &shpgrp);
(void) setpgid(0, shpgrp);
}
#endif /* alliant */
/*
* Move the descriptors to safe places. The variable didfds is 0 while we
* have only FSH* to work with. When didfds is true, we have 0,1,2 and
* prefer to use these.
*/
initdesc();
cdtohome = 1;
setv(STRcdtohome, SAVE(""), VAR_READWRITE);
/*
* Get and set the tty now
*/
if ((ttyn = ttyname(SHIN)) != NULL) {
/*
* Could use rindex to get rid of other possible path components, but
* hpux preserves the subdirectory /pty/ when storing the tty name in
* utmp, so we keep it too.
*/
if (strncmp(ttyn, "/dev/", 5) == 0)
setv(STRtty, cp = SAVE(ttyn + 5), VAR_READWRITE);
else
setv(STRtty, cp = SAVE(ttyn), VAR_READWRITE);
}
else
setv(STRtty, cp = SAVE(""), VAR_READWRITE);
/*
* Initialize the shell variables. ARGV and PROMPT are initialized later.
* STATUS is also munged in several places. CHILD is munged when
* forking/waiting
*/
/*
* 7-10-87 Paul Placeway autologout should be set ONLY on login shells and
* on shells running as root. Out of these, autologout should NOT be set
* for any psudo-terminals (this catches most window systems) and not for
* any terminal running X windows.
*
* At Ohio State, we have had problems with a user having his X session
* drop out from under him (on a Sun) because the shell in his master
* xterm timed out and exited.
*
* Really, this should be done with a program external to the shell, that
* watches for no activity (and NO running programs, such as dump) on a
* terminal for a long peroid of time, and then SIGHUPS the shell on that
* terminal.
*
* bugfix by Rich Salz <rsalz@PINEAPPLE.BBN.COM>: For root rsh things
* allways first check to see if loginsh or really root, then do things
* with ttyname()
*
* Also by Jean-Francois Lamy <lamy%ai.toronto.edu@RELAY.CS.NET>: check the
* value of cp before using it! ("root can rsh too")
*
* PWP: keep the nested ifs; the order of the tests matters and a good
* (smart) C compiler might re-arange things wrong.
*/
#ifdef AUTOLOGOUT
# ifdef convex
if (uid == 0)
/* root always has a 15 minute autologout */
setcopy(STRautologout, STRrootdefautologout, VAR_READWRITE);
else
if (loginsh)
/* users get autologout set to 0 */
setcopy(STRautologout, STR0, VAR_READWRITE);
# else /* convex */
if (loginsh || (uid == 0)) {
if (*cp) {
/* only for login shells or root and we must have a tty */
if (((cp2 = Strrchr(cp, (Char) '/')) != NULL) &&
(Strncmp(cp, STRptssl, 3) != 0)) {
cp2 = cp2 + 1;
}
else
cp2 = cp;
if (!(((Strncmp(cp2, STRtty, 3) == 0) && Isalpha(cp2[3])) ||
Strstr(cp, STRptssl) != NULL)) {
if (getenv("DISPLAY") == NULL) {
/* NOT on X window shells */
setcopy(STRautologout, STRdefautologout, VAR_READWRITE);
}
}
}
}
# endif /* convex */
#endif /* AUTOLOGOUT */
sigset_interrupting(SIGALRM, queue_alrmcatch);
setstatus(0);
/*
* get and set machine specific environment variables
*/
getmachine();
/*
* Publish the selected echo style
*/
#if ECHO_STYLE != BSD_ECHO
if (tcsh) {
# if ECHO_STYLE == NONE_ECHO
setcopy(STRecho_style, STRnone, VAR_READWRITE);
# endif /* ECHO_STYLE == NONE_ECHO */
# if ECHO_STYLE == SYSV_ECHO
setcopy(STRecho_style, STRsysv, VAR_READWRITE);
# endif /* ECHO_STYLE == SYSV_ECHO */
# if ECHO_STYLE == BOTH_ECHO
setcopy(STRecho_style, STRboth, VAR_READWRITE);
# endif /* ECHO_STYLE == BOTH_ECHO */
} else
#endif /* ECHO_STYLE != BSD_ECHO */
setcopy(STRecho_style, STRbsd, VAR_READWRITE);
/*
* increment the shell level.
*/
shlvl(1);
#ifdef __ANDROID__
/* On Android, $HOME either isn't set or set to /data, a R/O location.
Check for the environment variable EXTERNAL_STORAGE, which contains
the mount point of the external storage (SD card, mostly). If
EXTERNAL_STORAGE isn't set fall back to "/sdcard". Eventually
override $HOME so the environment is on the same page. */
if (((tcp = getenv("HOME")) != NULL && strcmp (tcp, "/data") != 0)
|| (tcp = getenv("EXTERNAL_STORAGE")) != NULL) {
cp = quote(SAVE(tcp));
} else
cp = quote(SAVE("/sdcard"));
tsetenv(STRKHOME, cp);
#else
if ((tcp = getenv("HOME")) != NULL)
cp = quote(SAVE(tcp));
else
cp = NULL;
#endif
if (cp == NULL)
fast = 1; /* No home -> can't read scripts */
else
setv(STRhome, cp, VAR_READWRITE);
dinit(cp); /* dinit thinks that HOME == cwd in a login
* shell */
/*
* Grab other useful things from the environment. Should we grab
* everything??
*/
{
char *cln, *cus, *cgr;
struct passwd *pw;
struct group *gr;
#ifdef apollo
int oid = getoid();
setv(STRoid, Itoa(oid, 0, 0), VAR_READWRITE);
#endif /* apollo */
setv(STReuid, Itoa(euid, 0, 0), VAR_READWRITE);
if ((pw = xgetpwuid(euid)) == NULL)
setcopy(STReuser, STRunknown, VAR_READWRITE);
else
setcopy(STReuser, str2short(pw->pw_name), VAR_READWRITE);
setv(STRuid, Itoa(uid, 0, 0), VAR_READWRITE);
setv(STRgid, Itoa(gid, 0, 0), VAR_READWRITE);
cln = getenv("LOGNAME");
cus = getenv("USER");
if (cus != NULL)
setv(STRuser, quote(SAVE(cus)), VAR_READWRITE);
else if (cln != NULL)
setv(STRuser, quote(SAVE(cln)), VAR_READWRITE);
else if ((pw = xgetpwuid(uid)) == NULL)
setcopy(STRuser, STRunknown, VAR_READWRITE);
else
setcopy(STRuser, str2short(pw->pw_name), VAR_READWRITE);
if (cln == NULL)
tsetenv(STRLOGNAME, varval(STRuser));
if (cus == NULL)
tsetenv(STRKUSER, varval(STRuser));
cgr = getenv("GROUP");
if (cgr != NULL)
setv(STRgroup, quote(SAVE(cgr)), VAR_READWRITE);
else if ((gr = xgetgrgid(gid)) == NULL)
setcopy(STRgroup, STRunknown, VAR_READWRITE);
else
setcopy(STRgroup, str2short(gr->gr_name), VAR_READWRITE);
if (cgr == NULL)
tsetenv(STRKGROUP, varval(STRgroup));
}
/*
* HOST may be wrong, since rexd transports the entire environment on sun
* 3.x Just set it again
*/
{
char cbuff[MAXHOSTNAMELEN];
if (gethostname(cbuff, sizeof(cbuff)) >= 0) {
cbuff[sizeof(cbuff) - 1] = '\0'; /* just in case */
tsetenv(STRHOST, str2short(cbuff));
}
else
tsetenv(STRHOST, STRunknown);
}
#ifdef REMOTEHOST
/*
* Try to determine the remote host we were logged in from.
*/
remotehost();
#endif /* REMOTEHOST */
#ifdef apollo
if ((tcp = getenv("SYSTYPE")) == NULL)
tcp = "bsd4.3";
tsetenv(STRSYSTYPE, quote(str2short(tcp)));
#endif /* apollo */
/*
* set editing on by default, unless running under Emacs as an inferior
* shell.
* We try to do this intelligently. If $TERM is available, then it
* should determine if we should edit or not. $TERM is preserved
* across rlogin sessions, so we will not get confused if we rlogin
* under an emacs shell. Another advantage is that if we run an
* xterm under an emacs shell, then the $TERM will be set to
* xterm, so we are going to want to edit. Unfortunately emacs
* does not restore all the tty modes, so xterm is not very well
* set up. But this is not the shell's fault.
* Also don't edit if $TERM == wm, for when we're running under an ATK app.
* Finally, emacs compiled under terminfo, sets the terminal to dumb,
* so disable editing for that too.
*
* Unfortunately, in some cases the initial $TERM setting is "unknown",
* "dumb", or "network" which is then changed in the user's startup files.
* We fix this by setting noediting here if $TERM is unknown/dumb and
* if noediting is set, we switch on editing if $TERM is changed.
*/
if ((tcp = getenv("TERM")) != NULL) {
setv(STRterm, quote(SAVE(tcp)), VAR_READWRITE);
noediting = strcmp(tcp, "unknown") == 0 || strcmp(tcp, "dumb") == 0 ||
strcmp(tcp, "network") == 0;
editing = strcmp(tcp, "emacs") != 0 && strcmp(tcp, "wm") != 0 &&
!noediting;
}
else {
noediting = 0;
editing = ((tcp = getenv("EMACS")) == NULL || strcmp(tcp, "t") != 0);
}
/*
* The 'edit' variable is either set or unset. It doesn't
* need a value. Making it 'emacs' might be confusing.
*/
if (editing)
setNS(STRedit);
/*
* still more mutability: make the complete routine automatically add the
* suffix of file names...
*/
setNS(STRaddsuffix);
/*
* Compatibility with tcsh >= 6.12 by default
*/
setNS(STRcsubstnonl);
/*
* Random default kill ring size
*/
setcopy(STRkillring, str2short("30"), VAR_READWRITE);
/*
* Re-initialize path if set in environment
*/
if ((tcp = getenv("PATH")) == NULL)
#ifdef _PATH_DEFPATH
importpath(str2short(_PATH_DEFPATH));
#else /* !_PATH_DEFPATH */
setq(STRpath, defaultpath(), &shvhed, VAR_READWRITE);
#endif /* _PATH_DEFPATH */
else
/* Importpath() allocates memory for the path, and the
* returned pointer from SAVE() was discarded, so
* this was a memory leak.. (sg)
*
* importpath(SAVE(tcp));
*/
importpath(str2short(tcp));
{
/* If the SHELL environment variable ends with "tcsh", set
* STRshell to the same path. This is to facilitate using
* the executable in environments where the compiled-in
* default isn't appropriate (sg).
*/
size_t sh_len = 0;
if ((tcp = getenv("SHELL")) != NULL) {
sh_len = strlen(tcp);
if ((sh_len >= 5 && strcmp(tcp + (sh_len - 5), "/tcsh") == 0) ||
(!tcsh && sh_len >= 4 && strcmp(tcp + (sh_len - 4), "/csh") == 0))
setv(STRshell, quote(SAVE(tcp)), VAR_READWRITE);
else
sh_len = 0;
}
if (sh_len == 0)
setcopy(STRshell, STR_SHELLPATH, VAR_READWRITE);
}
#ifdef _OSD_POSIX /* BS2000 needs this variable set to "SHELL" */
if ((tcp = getenv("PROGRAM_ENVIRONMENT")) == NULL)
tcp = "SHELL";
tsetenv(STRPROGRAM_ENVIRONMENT, quote(str2short(tcp)));
#endif /* _OSD_POSIX */
#ifdef COLOR_LS_F
if ((tcp = getenv("CLICOLOR_FORCE")) != NULL)
parseCLICOLOR_FORCE(TRUE, str2short(tcp));
if ((tcp = getenv("LSCOLORS")) != NULL)
parseLSCOLORS(str2short(tcp), FALSE);
if ((tcp = getenv("LS_COLORS")) != NULL)
parseLS_COLORS(str2short(tcp), FALSE);
#endif /* COLOR_LS_F */
mainpid = getpid();
doldol = putn((tcsh_number_t)mainpid); /* For $$ */
#ifdef WINNT_NATIVE
{
char *tmp;
Char *tmp2;
if ((tmp = getenv("TMP")) != NULL) {
tmp = xasprintf("%s/%s", tmp, "sh");
tmp2 = SAVE(tmp);
xfree(tmp);
}
else {
tmp2 = SAVE("");
}
shtemp = Strspl(tmp2, doldol); /* For << */
xfree(tmp2);
}
#else /* !WINNT_NATIVE */
#ifdef HAVE_MKSTEMP
{
const char *tmpdir = getenv ("TMPDIR");
if (!tmpdir)
tmpdir = "/tmp";
shtemp = Strspl(SAVE(tmpdir), SAVE("/sh" TMP_TEMPLATE)); /* For << */
}
#else /* !HAVE_MKSTEMP */
shtemp = Strspl(STRtmpsh, doldol); /* For << */
#endif /* HAVE_MKSTEMP */
#endif /* WINNT_NATIVE */
/*
* Record the interrupt states from the parent process. If the parent is
* non-interruptible our hand must be forced or we (and our children) won't
* be either. Our children inherit termination from our parent. We catch it
* only if we are the login shell.
*/
sigaction(SIGINT, NULL, &parintr);
sigaction(SIGTERM, NULL, &parterm);
#ifdef TCF
/* Enable process migration on ourselves and our progeny */
(void) signal(SIGMIGRATE, SIG_DFL);
#endif /* TCF */
/*
* dspkanji/dspmbyte autosetting
*/
/* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
#if defined(DSPMBYTE)
#if defined(NLS) && defined(LC_CTYPE)
if (((tcp = setlocale(LC_CTYPE, NULL)) != NULL || (tcp = getenv("LANG")) != NULL) && !adrof(CHECK_MBYTEVAR))
#else
if ((tcp = getenv("LANG")) != NULL && !adrof(CHECK_MBYTEVAR))
#endif
{
autoset_dspmbyte(str2short(tcp));
}
#if defined(WINNT_NATIVE)
else if (!adrof(CHECK_MBYTEVAR))
nt_autoset_dspmbyte();
#endif /* WINNT_NATIVE */
#endif
#if defined(AUTOSET_KANJI)
# if defined(NLS) && defined(LC_CTYPE)
if (setlocale(LC_CTYPE, NULL) != NULL || getenv("LANG") != NULL)
# else
if (getenv("LANG") != NULL)
# endif
autoset_kanji();
#endif /* AUTOSET_KANJI */
fix_version(); /* publish the shell version */
if (argc > 1 && strcmp(argv[1], "--version") == 0) {
xprintf("%" TCSH_S "\n", varval(STRversion));
xexit(0);
}
if (argc > 1 && strcmp(argv[1], "--help") == 0) {
xprintf("%" TCSH_S "\n\n", varval(STRversion));
xprintf("%s", CGETS(11, 8, HELP_STRING));
xexit(0);
}
/*
* Process the arguments.
*
* Note that processing of -v/-x is actually delayed till after script
* processing.
*
* We set the first character of our name to be '-' if we are a shell
* running interruptible commands. Many programs which examine ps'es
* use this to filter such shells out.
*/
argc--, tempv++;
while (argc > 0 && (tcp = tempv[0])[0] == '-' &&
*++tcp != '\0' && !batch) {
do
switch (*tcp++) {
case 0: /* - Interruptible, no prompt */
prompt = 0;
setintr = 1;
nofile = 1;
break;
case 'b': /* -b Next arg is input file */
batch = 1;
break;
case 'c': /* -c Command input from arg */
if (argc == 1)
xexit(0);
argc--, tempv++;
#ifdef M_XENIX
/* Xenix Vi bug:
it relies on a 7 bit environment (/bin/sh), so it
pass ascii arguments with the 8th bit set */
if (!strcmp(argv[0], "sh"))
{
char *p;
for (p = tempv[0]; *p; ++p)
*p &= ASCII;
}
#endif
targinp = tempv[0];
prompt = 0;
nofile = 1;
break;
case 'd': /* -d Load directory stack from file */
rdirs = 1;
break;
#ifdef apollo
case 'D': /* -D Define environment variable */
{
Char *dp;
cp = str2short(tcp);
if (dp = Strchr(cp, '=')) {
*dp++ = '\0';
tsetenv(cp, dp);
}
else
tsetenv(cp, STRNULL);
}
*tcp = '\0'; /* done with this argument */
break;
#endif /* apollo */
case 'e': /* -e Exit on any error */
exiterr = 1;
break;
case 'f': /* -f Fast start */
fast = 1;
break;
case 'i': /* -i Interactive, even if !intty */
intact = 1;
nofile = 1;
break;
case 'm': /* -m read .cshrc (from su) */
mflag = 1;
break;
case 'n': /* -n Don't execute */
noexec = 1;
break;
case 'q': /* -q (Undoc'd) ... die on quit */
quitit = 1;
break;
case 's': /* -s Read from std input */
nofile = 1;
break;
case 't': /* -t Read one line from input */
onelflg = 2;
prompt = 0;
nofile = 1;
break;