2 * sh.lex.c: Lexical analysis into tokens
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 /* #define DEBUG_INP */
37 /* #define DEBUG_SEEK */
46 * These lexical routines read input and form lists of words.
47 * There is some involved processing here, because of the complications
48 * of input buffering, and especially because of history substitution.
50 static Char
*word (int);
51 static eChar
getC1 (int);
52 static void getdol (void);
53 static void getexcl (Char
);
54 static struct Hist
*findev (Char
*, int);
55 static void setexclp (Char
*);
56 static eChar
bgetc (void);
57 static void balloc (int);
58 static void bfree (void);
59 static struct wordent
*gethent (Char
);
60 static int matchs (const Char
*, const Char
*);
61 static int getsel (int *, int *, int);
62 static struct wordent
*getsub (struct wordent
*);
63 static Char
*subword (Char
*, Char
, int *, size_t *);
64 static struct wordent
*dosub (Char
, struct wordent
*, int);
67 * Peekc is a peek character for getC, peekread for readc.
68 * There is a subtlety here in many places... history routines
69 * will read ahead and then insert stuff into the input stream.
70 * If they push back a character then they must push it behind
71 * the text substituted by the history substitution. On the other
72 * hand in several places we need 2 peek characters. To make this
73 * all work, the history routines read with getC, and make use both
74 * of ungetC and unreadc. The key observation is that the state
75 * of getC at the call of a history reference is such that calls
76 * to getC from the history routines will always yield calls of
77 * readc, unless this peeking is involved. That is to say that during
78 * getexcl the variables lap, exclp, and exclnxt are all zero.
80 * Getdol invokes history substitution, hence the extra peek, peekd,
81 * which it can ungetD to be before history substitutions.
83 static Char peekc
= 0, peekd
= 0;
84 static Char peekread
= 0;
86 /* (Tail of) current word from ! subst */
87 static Char
*exclp
= NULL
;
89 /* The rest of the ! subst words */
90 static struct wordent
*exclnxt
= NULL
;
92 /* Count of remaining words in ! subst */
95 /* "Globp" for alias resubstitution */
96 int aret
= TCSH_F_SEEK
;
99 * Labuf implements a general buffer for lookahead during lexical operations.
100 * Text which is to be placed in the input stream can be stuck here.
101 * We stick parsed ahead $ constructs during initial input,
102 * process id's from `$$', and modified variable values (from qualifiers
103 * during expansion in sh.dol.c) here.
105 struct Strbuf labuf
; /* = Strbuf_INIT; */
108 * Lex returns to its caller not only a wordlist (as a "var" parameter)
109 * but also whether a history substitution occurred. This is used in
110 * the main (process) routine to determine whether to echo, and also
111 * when called by the alias routine to determine whether to keep the
114 static int hadhist
= 0;
117 * Avoid alias expansion recursion via \!#
121 struct Strbuf histline
; /* = Strbuf_INIT; last line input */
123 int histvalid
= 0; /* is histline valid */
127 #define getC(f) (((getCtmp = peekc) != '\0') ? (peekc = 0, (eChar)getCtmp) : getC1(f))
128 #define ungetC(c) peekc = (Char) c
129 #define ungetD(c) peekd = (Char) c
131 /* Use Htime to store timestamps picked up from history file for enthist()
132 * if reading saved history (sg)
134 time_t Htime
= (time_t)0;
135 static time_t a2time_t (Char
*);
138 * special parsing rules apply for source -h
140 extern int enterhist
;
141 extern int postcmd_active
;
144 lex(struct wordent
*hp
)
148 int parsehtime
= enterhist
;
160 while (c
== ' ' || c
== '\t');
161 if (c
== (eChar
)HISTSUB
&& intty
)
162 /* ^lef^rit from tty is short !:s^lef^rit */
166 cleanup_push(hp
, lex_cleanup
);
169 * The following loop is written so that the links needed by freelex will
170 * be ready and rarin to go even if it is interrupted.
175 new = xmalloc(sizeof(*new));
182 wdp
->word
= word(parsehtime
);
184 if (enterhist
&& toolong
++ > 10 * 1024) {
185 stderror(ERR_LTOOLONG
);
187 } while (wdp
->word
[0] != '\n');
190 Strbuf_terminate(&histline
);
191 if (histline
.len
!= 0 && histline
.s
[histline
.len
- 1] == '\n')
192 histline
.s
[histline
.len
- 1] = '\0';
199 a2time_t(Char
*wordx
)
201 /* Attempt to distinguish timestamps from other possible entries.
202 * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
208 if (!wordx
|| *(s
= wordx
) != '+')
211 for (++s
, ret
= 0, ct
= 0; *s
; ++s
, ++ct
) {
212 if (!isdigit((unsigned char)*s
))
214 ret
= ret
* 10 + (time_t)((unsigned char)*s
- '0');
224 prlex(struct wordent
*sp0
)
226 struct wordent
*sp
= sp0
->next
;
229 xprintf("%" TCSH_S
, sp
->word
);
233 if (sp
->word
[0] != '\n')
239 copylex(struct wordent
*hp
, struct wordent
*fp
)
248 new = xmalloc(sizeof(*new));
255 wdp
->word
= Strsave(fp
->word
);
257 } while (wdp
->word
[0] != '\n');
261 initlex(struct wordent
*vp
)
269 freelex(struct wordent
*vp
)
273 while (vp
->next
!= vp
) {
283 lex_cleanup(void *xvp
)
295 struct Strbuf wbuf
= Strbuf_INIT
;
301 cleanup_push(&wbuf
, Strbuf_cleanup
);
303 if (enterhist
&& toolong
++ > 256 * 1024) {
304 stderror(ERR_WTOOLONG
);
306 while ((c
= getC(DOALL
)) == ' ' || c
== '\t')
308 if (cmap(c
, _META
| _ESC
))
314 Strbuf_append1(&wbuf
, c
);
317 Strbuf_append1(&wbuf
, c1
);
323 if (intty
|| (enterhist
&& !parsehtime
))
330 if (h
< 11 && parsehtime
)
335 Htime
= a2time_t(hbuf
);
345 Strbuf_append1(&wbuf
, c
);
355 if (c
!= (eChar
)HIST
)
356 Strbuf_append1(&wbuf
, '\\');
364 if (enterhist
&& toolong
++ > 256 * 1024) {
365 stderror(ERR_WTOOLONG
);
372 else if (c
== '\\') {
375 * PWP: this is dumb, but how all of the other shells work. If \ quotes
376 * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
377 * following character INSIDE a set of ''s.
379 * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
381 if (c
== (eChar
)HIST
)
385 ((c
== '\'') || (c
== '"') ||
386 (c
== '\\') || (c
== '$'))) {
392 * if (c1 == '`') c = ' '; else
400 else if (c
== '\n') {
401 seterror(ERR_UNMATCHED
, c1
);
406 else if (cmap(c
, _META
| _QF
| _QB
| _ESC
)) {
414 if (c
!= (eChar
)HIST
)
415 Strbuf_append1(&wbuf
, '\\');
418 else if (cmap(c
, _QF
| _QB
)) { /* '"` */
420 dolflg
= c
== '"' ? DOALL
: DOEXCL
;
422 else if (c
!= '#' || (!intty
&& !enterhist
)) {
427 Strbuf_append1(&wbuf
, c
);
431 cleanup_ignore(&wbuf
);
432 cleanup_until(&wbuf
);
433 return Strbuf_finish(&wbuf
);
442 if ((c
= peekc
) != 0) {
446 if (lap
< labuf
.len
) {
448 if (cmap(c
, _META
| _QF
| _QB
))
452 if ((c
= peekd
) != 0) {
457 if ((c
= *exclp
++) != 0)
459 if (exclnxt
&& --exclc
>= 0) {
460 exclnxt
= exclnxt
->next
;
461 setexclp(exclnxt
->word
);
466 /* this will throw away the dummy history entries */
471 exclnxt
= exclnxt
->next
;
475 setexclp(exclnxt
->word
);
480 /* Catch EOF in the middle of a line. (An EOF at the beginning of
481 * a line would have been processed by the readc(0) in lex().) */
485 if (c
== '$' && (flag
& DODOL
)) {
489 if (c
== (eChar
)HIST
&& (flag
& DOEXCL
)) {
501 struct Strbuf name
= Strbuf_INIT
;
506 c
= sc
= getC(DOEXCL
);
507 if (any("\t \n", c
)) {
512 cleanup_push(&name
, Strbuf_cleanup
);
513 Strbuf_append1(&name
, '$');
516 Strbuf_append1(&name
, c
);
518 if (c
== '\'') break;
520 Strbuf_append1(&name
, c
);
525 seterror(ERR_MISSING
, '\'');
529 Strbuf_append1(&name
, c
);
533 Strbuf_append1(&name
, c
), c
= getC(DOEXCL
);
534 if (c
== '#' || c
== '?' || c
== '%')
535 special
++, Strbuf_append1(&name
, c
), c
= getC(DOEXCL
);
536 Strbuf_append1(&name
, c
);
543 seterror(ERR_SPDOLLT
);
550 seterror(ERR_NEWLINE
);
555 seterror(ERR_SPSTAR
);
561 /* let $?0 pass for now */
567 while ((c
= getC(DOEXCL
)) != 0) {
570 Strbuf_append1(&name
, c
);
573 else if (letter(c
)) {
574 while ((c
= getC(DOEXCL
)) != 0) {
575 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
576 if (!letter(c
) && !Isdigit(c
))
578 Strbuf_append1(&name
, c
);
583 seterror(ERR_VARILL
);
593 Strbuf_append1(&name
, c
);
596 * Michael Greim: Allow $ expansion to take place in selector
597 * expressions. (limits the number of characters returned)
599 c
= getC(DOEXCL
| DODOL
);
603 seterror(ERR_NLINDEX
);
606 Strbuf_append1(&name
, c
);
612 * if the :g modifier is followed by a newline, then error right away!
616 int gmodflag
= 0, amodflag
= 0;
619 Strbuf_append1(&name
, c
), c
= getC(DOEXCL
), gmodflag
= 0, amodflag
= 0;
620 if (c
== 'g' || c
== 'a') {
625 Strbuf_append1(&name
, c
); c
= getC(DOEXCL
);
627 if ((c
== 'g' && !gmodflag
) || (c
== 'a' && !amodflag
)) {
632 Strbuf_append1(&name
, c
); c
= getC(DOEXCL
);
634 Strbuf_append1(&name
, c
);
635 /* scan s// [eichin:19910926.0512EST] */
639 eChar delim
= getC(0);
641 Strbuf_append1(&name
, delim
);
642 if (!delim
|| letter(delim
)
643 || Isdigit(delim
) || any(" \t\n", delim
)) {
644 seterror(ERR_BADSUBST
);
647 while ((c
= getC(0)) != CHAR_ERR
) {
648 if (esc
== 0 && c
== '\\') {
650 Strbuf_append1(&name
, c
);
653 Strbuf_append1(&name
, c
);
654 if (!esc
&& c
== delim
) delimcnt
--;
655 if (!delimcnt
) break;
659 seterror(ERR_BADSUBST
);
664 if (!any(TCSH_MODIFIERS
, c
)) {
665 if ((amodflag
|| gmodflag
) && c
== '\n')
666 stderror(ERR_VARSYN
); /* strike */
667 seterror(ERR_BADMOD
, c
);
671 while ((c
= getC(DOEXCL
)) == ':');
680 seterror(ERR_MISSING
, '}');
683 Strbuf_append1(&name
, c
);
686 cleanup_ignore(&name
);
687 cleanup_until(&name
);
688 addla(Strbuf_finish(&name
));
691 /* xfree()'s its argument */
695 static struct Strbuf buf
; /* = Strbuf_INIT; */
698 Strbuf_appendn(&buf
, labuf
.s
+ lap
, labuf
.len
- lap
);
700 Strbuf_append(&labuf
, cp
);
701 Strbuf_terminate(&labuf
);
702 Strbuf_appendn(&labuf
, buf
.s
, buf
.len
);
707 /* left-hand side of last :s or search string of last ?event? */
708 static struct Strbuf lhsb
; /* = Strbuf_INIT; */
709 static struct Strbuf slhs
; /* = Strbuf_INIT; left-hand side of last :s */
710 static struct Strbuf rhsb
; /* = Strbuf_INIT; right-hand side of last :s */
716 struct wordent
*hp
, *ip
;
717 int left
, right
, dol
;
736 for (ip
= hp
->next
->next
; ip
!= alhistt
; ip
= ip
->next
)
739 for (ip
= hp
->next
->next
; ip
!= hp
->prev
; ip
= ip
->next
)
741 left
= 0, right
= dol
;
742 if (sc
== HISTSUB
&& HISTSUB
!= '\0') {
743 ungetC('s'), unreadc(HISTSUB
), c
= ':';
747 if (!any(":^$*-%", c
))
753 if (letter(c
) || c
== '&') {
755 left
= 0, right
= dol
;
761 if (!getsel(&left
, &right
, dol
))
767 if (!getsel(&left
, &right
, dol
))
772 exclc
= right
- left
+ 1;
775 if ((sc
== HISTSUB
&& HISTSUB
!= '\0') || c
== ':') {
785 seterror(ERR_BADBANG
);
790 static struct wordent
*
791 getsub(struct wordent
*en
)
802 while (c
== 'g' || c
== 'a') {
803 global
|= (c
== 'g') ? FLAG_G
: FLAG_A
;
827 seterror(ERR_NOSUBST
);
831 Strbuf_append(&lhsb
, slhs
.s
);
832 Strbuf_terminate(&lhsb
);
844 if (letter(delim
) || Isdigit(delim
) || any(" \t\n", delim
)) {
847 seterror(ERR_BADSUBST
);
850 Strbuf_terminate(&lhsb
);
862 if (c
!= delim
&& c
!= '\\')
863 Strbuf_append1(&lhsb
, '\\');
865 Strbuf_append1(&lhsb
, c
);
868 Strbuf_terminate(&lhsb
);
869 else if (lhsb
.s
[0] == 0) {
873 lhsb
.len
= Strlen(lhsb
.s
); /* lhsb.s wasn't changed */
885 if (c
!= delim
/* && c != '~' */ )
886 Strbuf_append1(&rhsb
, '\\');
888 Strbuf_append1(&rhsb
, c
);
890 Strbuf_terminate(&rhsb
);
896 seterror(ERR_BADBANGMOD
, (int)c
);
900 if (lhsb
.s
!= NULL
&& lhsb
.len
!= 0)
901 Strbuf_append(&slhs
, lhsb
.s
);
902 Strbuf_terminate(&slhs
);
904 en
= dosub(sc
, en
, global
);
906 while ((c
= getC(0)) == ':');
913 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
915 * when using history substitution, and the variable
916 * 'history' is set to a value higher than 1000,
917 * the shell might either freeze (hang) or core-dump.
918 * We raise the limit to 50000000
921 static struct wordent
*
922 dosub(Char sc
, struct wordent
*en
, int global
)
925 int didsub
= 0, didone
= 0;
926 struct wordent
*hp
= &lexi
;
933 struct wordent
*new = xcalloc(1, sizeof *wdp
);
942 Char
*tword
, *otword
;
944 if ((global
& FLAG_G
) || didsub
== 0) {
948 tword
= subword(en
->word
, sc
, &didone
, &pos
);
951 if (global
& FLAG_A
) {
952 while (didone
&& tword
!= STRNULL
) {
954 tword
= subword(otword
, sc
, &didone
, &pos
);
955 if (Strcmp(tword
, otword
) == 0) {
965 tword
= Strsave(en
->word
);
970 seterror(ERR_MODFAIL
);
973 * ANSI mode HP/UX compiler chokes on
974 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
976 hst
= enthist(HIST_PURGE
, &lexi
, 0, 0, -1);
980 /* Return a newly allocated result of one modification of CP using the
981 operation TYPE. Set ADID to 1 if a modification was performed.
982 If TYPE == 's', perform substitutions only from *START_POS on and set
983 *START_POS to the position of next substitution attempt. */
985 subword(Char
*cp
, Char type
, int *adid
, size_t *start_pos
)
1000 wp
= domod(cp
, type
);
1003 return (Strsave(cp
));
1009 for (mp
= cp
+ *start_pos
; *mp
; mp
++) {
1010 if (matchs(mp
, lhsb
.s
)) {
1011 struct Strbuf wbuf
= Strbuf_INIT
;
1013 Strbuf_appendn(&wbuf
, cp
, mp
- cp
);
1014 for (np
= rhsb
.s
; *np
; np
++)
1023 Strbuf_append1(&wbuf
, *np
);
1027 Strbuf_append(&wbuf
, lhsb
.s
);
1030 *start_pos
= wbuf
.len
;
1031 Strbuf_append(&wbuf
, mp
+ lhsb
.len
);
1033 return Strbuf_finish(&wbuf
);
1037 return (Strsave(cp
));
1042 domod(Char
*cp
, Char type
)
1050 return Strsave(STRQNULL
);
1055 for (xp
= wp
; (c
= *xp
) != 0; xp
++)
1056 if ((c
!= ' ' && c
!= '\t') || type
== 'q' || type
== 'Q')
1061 wp
= NLSChangeCase(cp
, 1);
1062 return wp
? wp
: Strsave(cp
);
1065 wp
= NLSChangeCase(cp
, 0);
1066 return wp
? wp
: Strsave(cp
);
1070 wp
= Strrchr(cp
, '/');
1074 xp
= Strsave(wp
+ 1);
1076 xp
= Strnsave(cp
, wp
- cp
);
1082 for (wp
--; wp
>= cp
&& *wp
!= '/'; wp
--)
1085 xp
= Strsave(wp
+ 1);
1087 xp
= Strnsave(cp
, wp
- cp
);
1090 return (Strsave(type
== 'e' ? STRNULL
: cp
));
1099 matchs(const Char
*str
, const Char
*pat
)
1101 while (*str
&& *pat
&& *str
== *pat
)
1107 getsel(int *al
, int *ar
, int dol
)
1111 int first
= *al
< 0;
1116 if (quesarg
== -1) {
1117 seterror(ERR_BADBANGARG
);
1159 while (Isdigit(c
)) {
1160 i
= i
* 10 + c
- '0';
1182 if (*al
> *ar
|| *ar
> dol
) {
1183 seterror(ERR_BADBANGARG
);
1190 static struct wordent
*
1199 c
= (sc
== HISTSUB
&& HISTSUB
!= '\0') ? (eChar
)HIST
: getC(0);
1200 if (c
== (eChar
)HIST
) {
1214 if (lastev
== eventno
&& alhistp
)
1219 case '#': /* !# is command being typed in (mrh) */
1221 seterror(ERR_HISTLOOP
);
1234 if (any("(=~", c
)) {
1239 Strbuf_terminate(&lhsb
);
1242 while (!cmap(c
, _ESC
| _META
| _QF
| _QB
) && !any("^*-%${}:#", c
)) {
1243 if (event
!= -1 && Isdigit(c
))
1244 event
= event
* 10 + c
- '0';
1247 Strbuf_append1(&lhsb
, c
);
1251 if (lhsb
.len
== 0) {
1252 lhsb
.len
= Strlen(lhsb
.s
); /* lhsb.s wasn't changed */
1256 Strbuf_terminate(&lhsb
);
1259 * History had only digits
1262 event
= eventno
+ (alhistp
== 0) - event
;
1266 Strbuf_append1(&lhsb
, '\0'); /* Allocate space */
1267 Strbuf_terminate(&lhsb
);
1268 memmove(lhsb
.s
+ 1, lhsb
.s
, (lhsb
.len
- 1) * sizeof (*lhsb
.s
));
1271 hp
= findev(lhsb
.s
, 0);
1277 Strbuf_terminate(&lhsb
);
1287 Strbuf_append1(&lhsb
, c
);
1289 if (lhsb
.len
== 0) {
1290 lhsb
.len
= Strlen(lhsb
.s
); /* lhsb.s wasn't changed */
1291 if (lhsb
.len
== 0) {
1292 seterror(ERR_NOSEARCH
);
1297 Strbuf_terminate(&lhsb
);
1298 hp
= findev(lhsb
.s
, 1);
1304 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
)
1305 if (hp
->Hnum
== event
) {
1310 np
= putn((tcsh_number_t
)event
);
1311 seterror(ERR_NOEVENT
, short2str(np
));
1316 static struct Hist
*
1317 findev(Char
*cp
, int anyarg
)
1321 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
) {
1324 struct wordent
*lp
= hp
->Hlex
.next
;
1328 * The entries added by alias substitution don't have a newline but do
1329 * have a negative event number. Savehist() trims off these entries,
1330 * but it happens before alias expansion, too early to delete those
1331 * from the previous command.
1335 if (lp
->word
[0] == '\n')
1343 while (*p
++ == *q
++);
1347 for (dp
= lp
->word
; *dp
; dp
++) {
1355 while (*p
++ == *q
++);
1359 } while (lp
->word
[0] != '\n');
1361 seterror(ERR_NOEVENT
, short2str(cp
));
1369 if (cp
&& cp
[0] == '\n')
1377 peekread
= (Char
) c
;
1384 static int sincereal
; /* Number of real EOFs we've seen */
1389 if ((c
= peekread
) != 0) {
1399 xprintf("alvecp %c\n", *alvecp
& 0xff);
1402 if ((c
= *alvecp
++) != 0)
1404 if (alvec
&& *alvec
) {
1416 if ((alvecp
= *alvec
) != 0) {
1420 /* Infinite source! */
1426 if ((c
= *evalp
++) != 0)
1428 if (evalvec
&& *evalvec
) {
1436 if (evalvec
== INVPPTR
) {
1440 if ((evalp
= *evalvec
) != 0) {
1448 if (arginp
== INVPTR
|| onelflg
== 1) {
1454 if ((c
= *arginp
++) == 0) {
1462 #endif /* BSDJOBS */
1464 if (c
== CHAR_ERR
) {
1465 #ifndef WINNT_NATIVE
1471 # endif /* TERMIO */
1475 #endif /* !WINNT_NATIVE */
1478 /* was isatty but raw with ignoreeof yields problems */
1479 #ifndef WINNT_NATIVE
1482 if (ioctl(SHIN
, TCGETA
, (ioctl_t
) & tty
) == 0 &&
1483 (tty
.c_lflag
& ICANON
))
1485 if (ioctl(SHIN
, TIOCGETP
, (ioctl_t
) & tty
) == 0 &&
1486 (tty
.sg_flags
& RAW
) == 0)
1487 # endif /* TERMIO */
1489 if (tcgetattr(SHIN
, &tty
) == 0 &&
1490 (tty
.c_lflag
& ICANON
))
1492 #else /* WINNT_NATIVE */
1494 #endif /* !WINNT_NATIVE */
1498 #endif /* BSDJOBS */
1500 if (numeof
!= 0 && ++sincereal
>= numeof
) /* Too many EOFs? Bye! */
1504 (ctpgrp
= tcgetpgrp(FSHTTY
)) != -1 &&
1506 (void) tcsetpgrp(FSHTTY
, tpgrp
);
1509 # endif /* _SEQUENT */
1510 (void) killpg(ctpgrp
, SIGHUP
);
1513 * With the walking process group fix, this message
1514 * is now obsolete. As the foreground process group
1515 * changes, the shell needs to adjust. Well too bad.
1517 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1518 (int)ctpgrp
, (int)tpgrp
);
1519 # endif /* notdef */
1522 #endif /* BSDJOBS */
1523 /* What follows is complicated EOF handling -- sterling@netcom.com */
1524 /* First, we check to see if we have ignoreeof set */
1525 if (adrof(STRignoreeof
)) {
1526 /* If so, we check for any stopped jobs only on the first EOF */
1527 if ((sincereal
== 1) && (chkstop
== 0)) {
1531 /* If we don't have ignoreeof set, always check for stopped jobs */
1536 /* At this point, if there were stopped jobs, we would have already
1537 * called reset(). If we got this far, assume we can print an
1538 * exit/logout message if we ignoreeof, or just exit.
1540 if (adrof(STRignoreeof
)) {
1541 /* If so, tell the user to use exit or logout */
1543 xprintf("%s", CGETS(16, 2,
1544 "\nUse \"logout\" to logout.\n"));
1546 xprintf(CGETS(16, 3,
1547 "\nUse \"exit\" to leave %s.\n"),
1552 /* If we don't have ignoreeof set, just fall through */
1561 if (c
== '\n' && onelflg
)
1564 Strbuf_append1(&histline
, c
);
1573 while (buf
>= fblocks
) {
1574 nfbuf
= xcalloc(fblocks
+ 2, sizeof(Char
**));
1576 (void) blkcpy(nfbuf
, fbuf
);
1580 fbuf
[fblocks
] = xcalloc(BUFSIZE
, sizeof(Char
));
1586 wide_read(int fildes
, Char
*buf
, size_t nchars
, int use_fclens
)
1588 char cbuf
[BUFSIZE
+ 1];
1595 assert (nchars
<= sizeof(cbuf
) / sizeof(*cbuf
));
1601 size_t len
= nchars
> partial
? nchars
- partial
: 1;
1603 if (partial
+ len
>= sizeof(cbuf
) / sizeof(*cbuf
))
1606 r
= xread(fildes
, cbuf
+ partial
, len
);
1608 if (partial
== 0 && r
<= 0)
1612 while (i
< partial
&& nchars
!= 0) {
1615 tlen
= normal_mbtowc(buf
+ res
, cbuf
+ i
, partial
- i
);
1618 if ((partial
- i
) < MB_LEN_MAX
&& r
> 0)
1619 /* Maybe a partial character and there is still a chance
1622 buf
[res
] = (unsigned char)cbuf
[i
] | INVALID_BYTE
;
1635 memmove(cbuf
, cbuf
+ i
, partial
- i
);
1637 } while (partial
!= 0 && nchars
> 0);
1638 /* Throwing away possible partial multibyte characters on error if the
1639 stream is not seekable */
1641 lseek(fildes
, -(off_t
)partial
, L_INCR
);
1643 return res
!= 0 ? res
: r
;
1651 int numleft
= 0, roomleft
;
1654 if (fseekp
< fbobp
|| fseekp
> feobp
) {
1655 fbobp
= feobp
= fseekp
;
1656 (void) lseek(SHIN
, fseekp
, L_SET
);
1658 if (fseekp
== feobp
) {
1664 for (i
= 0; i
< (size_t)(feobp
- fbobp
); i
++)
1666 fseekp
= feobp
= bytes
;
1669 c
= wide_read(SHIN
, fbuf
[0], BUFSIZE
, 1);
1672 stderror(ERR_SYSTEM
, progname
, strerror(errno
));
1678 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1679 ch
= fbuf
[0][fseekp
- fbobp
];
1683 ch
= fbuf
[0][fseekp
- fbobp
];
1685 } while (ch
== '\r');
1686 #endif /* !WINNT_NATIVE && !__CYGWIN__ */
1690 while (fseekp
>= feobp
) {
1692 #if defined(FILEC) && defined(TIOCSTI)
1694 #endif /* FILEC && TIOCSTI */
1695 ) && intty
) { /* then use twenex routine */
1696 fseekp
= feobp
; /* where else? */
1697 #if defined(FILEC) && defined(TIOCSTI)
1699 c
= numleft
= tenex(InputBuf
, BUFSIZE
);
1701 #endif /* FILEC && TIOCSTI */
1702 c
= numleft
= Inputl(); /* PWP: get a line */
1703 while (numleft
> 0) {
1704 off
= (int) feobp
% BUFSIZE
;
1705 buf
= (int) feobp
/ BUFSIZE
;
1707 roomleft
= BUFSIZE
- off
;
1708 if (roomleft
> numleft
)
1710 (void) memcpy(fbuf
[buf
] + off
, InputBuf
+ c
- numleft
,
1711 roomleft
* sizeof(Char
));
1712 numleft
-= roomleft
;
1716 off
= (int) feobp
% BUFSIZE
;
1717 buf
= (int) feobp
/ BUFSIZE
;
1719 roomleft
= BUFSIZE
- off
;
1720 c
= wide_read(SHIN
, fbuf
[buf
] + off
, roomleft
, 0);
1724 if (c
== 0 || (c
< 0 && fixio(SHIN
, errno
) == -1))
1729 (void) check_window_size(0); /* for window systems */
1730 #endif /* SIG_WINDOW */
1731 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1732 ch
= fbuf
[(int) fseekp
/ BUFSIZE
][(int) fseekp
% BUFSIZE
];
1736 ch
= fbuf
[(int) fseekp
/ BUFSIZE
][(int) fseekp
% BUFSIZE
];
1738 } while (ch
== '\r');
1739 #endif /* !WINNT_NATIVE && !__CYGWIN__ */
1752 sb
= (int) (fseekp
- 1) / BUFSIZE
;
1754 for (i
= 0; i
< sb
; i
++)
1756 (void) blkcpy(fbuf
, &fbuf
[sb
]);
1757 fseekp
-= BUFSIZE
* sb
;
1758 feobp
-= BUFSIZE
* sb
;
1764 bseek(struct Ain
*l
)
1766 switch (aret
= l
->type
) {
1768 evalvec
= l
->a_seek
;
1771 xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec
, evalp
);
1778 xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec
, alvecp
);
1783 xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp
);
1788 if (fseekp
>= fbobp
&& feobp
>= fbobp
) {
1793 for (i
= 0; i
< (size_t)(feobp
- fbobp
); i
++) {
1805 fbobp
= feobp
= fseekp
+ 1; /* To force lseek() */
1810 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret
);
1815 /* any similarity to bell telephone is purely accidental */
1817 btell(struct Ain
*l
)
1819 switch (l
->type
= aret
) {
1821 l
->a_seek
= evalvec
;
1824 xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec
, evalp
);
1831 xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec
, alvecp
);
1836 if (cantell
&& fseekp
>= fbobp
&& fseekp
<= feobp
) {
1840 for (i
= 0; i
< (size_t)(fseekp
- fbobp
); i
++)
1841 l
->f_seek
+= fclens
[i
];
1848 xprintf(CGETS(16, 10, "tell file %x\n"), fseekp
);
1852 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret
);
1860 (void) lseek(SHIN
, (off_t
) 0, L_XTND
);
1876 if (arginp
|| onelflg
|| intty
)
1878 if ((x
= lseek(SHIN
, (off_t
) 0, L_INCR
)) == -1)
1880 fbuf
= xcalloc(2, sizeof(Char
**));
1882 fbuf
[0] = xcalloc(BUFSIZE
, sizeof(Char
));
1883 fseekp
= fbobp
= feobp
= x
;