1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.lex.c,v 3.77 2006/09/27 17:01:06 mitr Exp $ */
3 * sh.lex.c: Lexical analysis into tokens
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: sh.lex.c,v 3.77 2006/09/27 17:01:06 mitr Exp $")
40 /* #define DEBUG_INP */
41 /* #define DEBUG_SEEK */
50 * These lexical routines read input and form lists of words.
51 * There is some involved processing here, because of the complications
52 * of input buffering, and especially because of history substitution.
54 static Char
*word (int);
55 static eChar
getC1 (int);
56 static void getdol (void);
57 static void getexcl (Char
);
58 static struct Hist
*findev (Char
*, int);
59 static void setexclp (Char
*);
60 static eChar
bgetc (void);
61 static void balloc (int);
62 static void bfree (void);
63 static struct wordent
*gethent (Char
);
64 static int matchs (const Char
*, const Char
*);
65 static int getsel (int *, int *, int);
66 static struct wordent
*getsub (struct wordent
*);
67 static Char
*subword (Char
*, Char
, int *, size_t *);
68 static struct wordent
*dosub (Char
, struct wordent
*, int);
69 static ssize_t
wide_read (int, Char
*, size_t, int);
72 * Peekc is a peek character for getC, peekread for readc.
73 * There is a subtlety here in many places... history routines
74 * will read ahead and then insert stuff into the input stream.
75 * If they push back a character then they must push it behind
76 * the text substituted by the history substitution. On the other
77 * hand in several places we need 2 peek characters. To make this
78 * all work, the history routines read with getC, and make use both
79 * of ungetC and unreadc. The key observation is that the state
80 * of getC at the call of a history reference is such that calls
81 * to getC from the history routines will always yield calls of
82 * readc, unless this peeking is involved. That is to say that during
83 * getexcl the variables lap, exclp, and exclnxt are all zero.
85 * Getdol invokes history substitution, hence the extra peek, peekd,
86 * which it can ungetD to be before history substitutions.
88 static Char peekc
= 0, peekd
= 0;
89 static Char peekread
= 0;
91 /* (Tail of) current word from ! subst */
92 static Char
*exclp
= NULL
;
94 /* The rest of the ! subst words */
95 static struct wordent
*exclnxt
= NULL
;
97 /* Count of remaining words in ! subst */
100 /* "Globp" for alias resubstitution */
101 int aret
= TCSH_F_SEEK
;
104 * Labuf implements a general buffer for lookahead during lexical operations.
105 * Text which is to be placed in the input stream can be stuck here.
106 * We stick parsed ahead $ constructs during initial input,
107 * process id's from `$$', and modified variable values (from qualifiers
108 * during expansion in sh.dol.c) here.
110 struct Strbuf labuf
; /* = Strbuf_INIT; */
113 * Lex returns to its caller not only a wordlist (as a "var" parameter)
114 * but also whether a history substitution occurred. This is used in
115 * the main (process) routine to determine whether to echo, and also
116 * when called by the alias routine to determine whether to keep the
119 static int hadhist
= 0;
122 * Avoid alias expansion recursion via \!#
126 struct Strbuf histline
; /* = Strbuf_INIT; last line input */
128 int histvalid
= 0; /* is histline valid */
132 #define getC(f) (((getCtmp = peekc) != '\0') ? (peekc = 0, (eChar)getCtmp) : getC1(f))
133 #define ungetC(c) peekc = (Char) c
134 #define ungetD(c) peekd = (Char) c
136 /* Use Htime to store timestamps picked up from history file for enthist()
137 * if reading saved history (sg)
139 time_t Htime
= (time_t)0;
140 static time_t a2time_t (Char
*);
143 * special parsing rules apply for source -h
145 extern int enterhist
;
148 lex(struct wordent
*hp
)
152 int parsehtime
= enterhist
;
158 hp
->next
= hp
->prev
= hp
;
163 while (c
== ' ' || c
== '\t');
164 if (c
== (eChar
)HISTSUB
&& intty
)
165 /* ^lef^rit from tty is short !:s^lef^rit */
169 cleanup_push(hp
, lex_cleanup
);
172 * The following loop is written so that the links needed by freelex will
173 * be ready and rarin to go even if it is interrupted.
178 new = xmalloc(sizeof(*new));
185 wdp
->word
= word(parsehtime
);
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("%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 freelex(struct wordent
*vp
)
265 while (vp
->next
!= vp
) {
275 lex_cleanup(void *xvp
)
287 struct Strbuf wbuf
= Strbuf_INIT
;
292 cleanup_push(&wbuf
, Strbuf_cleanup
);
294 while ((c
= getC(DOALL
)) == ' ' || c
== '\t')
296 if (cmap(c
, _META
| _ESC
))
302 Strbuf_append1(&wbuf
, c
);
305 Strbuf_append1(&wbuf
, c1
);
311 if (intty
|| (enterhist
&& !parsehtime
))
318 if (h
< 11 && parsehtime
)
323 Htime
= a2time_t(hbuf
);
333 Strbuf_append1(&wbuf
, c
);
343 if (c
!= (eChar
)HIST
)
344 Strbuf_append1(&wbuf
, '\\');
357 else if (c
== '\\') {
360 * PWP: this is dumb, but how all of the other shells work. If \ quotes
361 * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
362 * following character INSIDE a set of ''s.
364 * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
366 if (c
== (eChar
)HIST
)
370 ((c
== '\'') || (c
== '"') ||
377 * if (c1 == '`') c = ' '; else
385 else if (c
== '\n') {
386 seterror(ERR_UNMATCHED
, c1
);
391 else if (cmap(c
, _META
| _QF
| _QB
| _ESC
)) {
399 if (c
!= (eChar
)HIST
)
400 Strbuf_append1(&wbuf
, '\\');
403 else if (cmap(c
, _QF
| _QB
)) { /* '"` */
405 dolflg
= c
== '"' ? DOALL
: DOEXCL
;
407 else if (c
!= '#' || (!intty
&& !enterhist
)) {
412 Strbuf_append1(&wbuf
, c
);
416 cleanup_ignore(&wbuf
);
417 cleanup_until(&wbuf
);
418 return Strbuf_finish(&wbuf
);
427 if ((c
= peekc
) != 0) {
431 if (lap
< labuf
.len
) {
433 if (cmap(c
, _META
| _QF
| _QB
))
437 if ((c
= peekd
) != 0) {
442 if ((c
= *exclp
++) != 0)
444 if (exclnxt
&& --exclc
>= 0) {
445 exclnxt
= exclnxt
->next
;
446 setexclp(exclnxt
->word
);
451 /* this will throw away the dummy history entries */
456 exclnxt
= exclnxt
->next
;
460 setexclp(exclnxt
->word
);
464 if (c
== '$' && (flag
& DODOL
)) {
468 if (c
== (eChar
)HIST
&& (flag
& DOEXCL
)) {
480 struct Strbuf name
= Strbuf_INIT
;
485 c
= sc
= getC(DOEXCL
);
486 if (any("\t \n", c
)) {
491 cleanup_push(&name
, Strbuf_cleanup
);
492 Strbuf_append1(&name
, '$');
494 Strbuf_append1(&name
, c
), c
= getC(DOEXCL
);
495 if (c
== '#' || c
== '?' || c
== '%')
496 special
++, Strbuf_append1(&name
, c
), c
= getC(DOEXCL
);
497 Strbuf_append1(&name
, c
);
504 seterror(ERR_SPDOLLT
);
511 seterror(ERR_NEWLINE
);
516 seterror(ERR_SPSTAR
);
522 /* let $?0 pass for now */
528 while ((c
= getC(DOEXCL
)) != 0) {
531 Strbuf_append1(&name
, c
);
534 else if (letter(c
)) {
535 while ((c
= getC(DOEXCL
)) != 0) {
536 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
537 if (!letter(c
) && !Isdigit(c
))
539 Strbuf_append1(&name
, c
);
544 seterror(ERR_VARILL
);
554 Strbuf_append1(&name
, c
);
557 * Michael Greim: Allow $ expansion to take place in selector
558 * expressions. (limits the number of characters returned)
560 c
= getC(DOEXCL
| DODOL
);
564 seterror(ERR_NLINDEX
);
567 Strbuf_append1(&name
, c
);
573 * if the :g modifier is followed by a newline, then error right away!
577 int gmodflag
= 0, amodflag
= 0;
580 Strbuf_append1(&name
, c
), c
= getC(DOEXCL
);
581 if (c
== 'g' || c
== 'a') {
586 Strbuf_append1(&name
, c
); c
= getC(DOEXCL
);
588 if ((c
== 'g' && !gmodflag
) || (c
== 'a' && !amodflag
)) {
593 Strbuf_append1(&name
, c
); c
= getC(DOEXCL
);
595 Strbuf_append1(&name
, c
);
596 /* scan s// [eichin:19910926.0512EST] */
599 eChar delim
= getC(0);
601 Strbuf_append1(&name
, delim
);
602 if (!delim
|| letter(delim
)
603 || Isdigit(delim
) || any(" \t\n", delim
)) {
604 seterror(ERR_BADSUBST
);
607 while ((c
= getC(0)) != CHAR_ERR
) {
608 Strbuf_append1(&name
, c
);
609 if(c
== delim
) delimcnt
--;
613 seterror(ERR_BADSUBST
);
618 if (!any("htrqxesul", c
)) {
619 if ((amodflag
|| gmodflag
) && c
== '\n')
620 stderror(ERR_VARSYN
); /* strike */
621 seterror(ERR_BADMOD
, c
);
625 while ((c
= getC(DOEXCL
)) == ':');
634 seterror(ERR_MISSING
, '}');
637 Strbuf_append1(&name
, c
);
640 cleanup_ignore(&name
);
641 cleanup_until(&name
);
642 addla(Strbuf_finish(&name
));
645 /* xfree()'s its argument */
649 static struct Strbuf buf
; /* = Strbuf_INIT; */
652 Strbuf_appendn(&buf
, labuf
.s
+ lap
, labuf
.len
- lap
);
654 Strbuf_append(&labuf
, cp
);
655 Strbuf_terminate(&labuf
);
656 Strbuf_appendn(&labuf
, buf
.s
, buf
.len
);
661 /* left-hand side of last :s or search string of last ?event? */
662 static struct Strbuf lhsb
; /* = Strbuf_INIT; */
663 static struct Strbuf slhs
; /* = Strbuf_INIT; left-hand side of last :s */
664 static struct Strbuf rhsb
; /* = Strbuf_INIT; right-hand side of last :s */
670 struct wordent
*hp
, *ip
;
671 int left
, right
, dol
;
690 for (ip
= hp
->next
->next
; ip
!= alhistt
; ip
= ip
->next
)
693 for (ip
= hp
->next
->next
; ip
!= hp
->prev
; ip
= ip
->next
)
695 left
= 0, right
= dol
;
697 ungetC('s'), unreadc(HISTSUB
), c
= ':';
701 if (!any(":^$*-%", c
))
707 if (letter(c
) || c
== '&') {
709 left
= 0, right
= dol
;
715 if (!getsel(&left
, &right
, dol
))
721 if (!getsel(&left
, &right
, dol
))
726 exclc
= right
- left
+ 1;
729 if (sc
== HISTSUB
|| c
== ':') {
739 seterror(ERR_BADBANG
);
744 static struct wordent
*
745 getsub(struct wordent
*en
)
756 while (c
== 'g' || c
== 'a') {
757 global
|= (c
== 'g') ? FLAG_G
: FLAG_A
;
781 seterror(ERR_NOSUBST
);
785 Strbuf_append(&lhsb
, slhs
.s
);
786 Strbuf_terminate(&lhsb
);
798 if (letter(delim
) || Isdigit(delim
) || any(" \t\n", delim
)) {
801 seterror(ERR_BADSUBST
);
804 Strbuf_terminate(&lhsb
);
816 if (c
!= delim
&& c
!= '\\')
817 Strbuf_append1(&lhsb
, '\\');
819 Strbuf_append1(&lhsb
, c
);
822 Strbuf_terminate(&lhsb
);
823 else if (lhsb
.s
[0] == 0) {
827 lhsb
.len
= Strlen(lhsb
.s
); /* lhsb.s wasn't changed */
839 if (c
!= delim
/* && c != '~' */ )
840 Strbuf_append1(&rhsb
, '\\');
842 Strbuf_append1(&rhsb
, c
);
844 Strbuf_terminate(&rhsb
);
850 seterror(ERR_BADBANGMOD
, (int)c
);
854 Strbuf_append(&slhs
, lhsb
.s
);
855 Strbuf_terminate(&slhs
);
857 en
= dosub(sc
, en
, global
);
859 while ((c
= getC(0)) == ':');
866 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
868 * when using history substitution, and the variable
869 * 'history' is set to a value higher than 1000,
870 * the shell might either freeze (hang) or core-dump.
871 * We raise the limit to 50000000
874 #define HIST_PURGE -50000000
875 static struct wordent
*
876 dosub(Char sc
, struct wordent
*en
, int global
)
879 int didsub
= 0, didone
= 0;
880 struct wordent
*hp
= &lexi
;
887 struct wordent
*new = xcalloc(1, sizeof *wdp
);
896 Char
*tword
, *otword
;
898 if ((global
& FLAG_G
) || didsub
== 0) {
902 tword
= subword(en
->word
, sc
, &didone
, &pos
);
905 if (global
& FLAG_A
) {
906 while (didone
&& tword
!= STRNULL
) {
908 tword
= subword(otword
, sc
, &didone
, &pos
);
909 if (Strcmp(tword
, otword
) == 0) {
919 tword
= Strsave(en
->word
);
924 seterror(ERR_MODFAIL
);
927 * ANSI mode HP/UX compiler chokes on
928 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
930 hst
= enthist(HIST_PURGE
, &lexi
, 0, 0);
934 /* Return a newly allocated result of one modification of CP using the
935 operation TYPE. Set ADID to 1 if a modification was performed.
936 If TYPE == 's', perform substitutions only from *START_POS on and set
937 *START_POS to the position of next substitution attempt. */
939 subword(Char
*cp
, Char type
, int *adid
, size_t *start_pos
)
954 wp
= domod(cp
, type
);
957 return (Strsave(cp
));
963 for (mp
= cp
+ *start_pos
; *mp
; mp
++) {
964 if (matchs(mp
, lhsb
.s
)) {
965 struct Strbuf wbuf
= Strbuf_INIT
;
967 Strbuf_appendn(&wbuf
, cp
, mp
- cp
);
968 for (np
= rhsb
.s
; *np
; np
++)
977 Strbuf_append1(&wbuf
, *np
);
981 Strbuf_append(&wbuf
, lhsb
.s
);
984 *start_pos
= wbuf
.len
;
985 Strbuf_append(&wbuf
, mp
+ lhsb
.len
);
987 return Strbuf_finish(&wbuf
);
991 return (Strsave(cp
));
996 domod(Char
*cp
, Char type
)
1006 for (xp
= wp
; (c
= *xp
) != 0; xp
++)
1007 if ((c
!= ' ' && c
!= '\t') || type
== 'q')
1012 wp
= NLSChangeCase(cp
, 1);
1013 return wp
? wp
: Strsave(cp
);
1016 wp
= NLSChangeCase(cp
, 0);
1017 return wp
? wp
: Strsave(cp
);
1021 if (!any(short2str(cp
), '/'))
1022 return (type
== 't' ? Strsave(cp
) : 0);
1023 wp
= Strrchr(cp
, '/');
1025 xp
= Strnsave(cp
, wp
- cp
);
1027 xp
= Strsave(wp
+ 1);
1033 for (wp
--; wp
>= cp
&& *wp
!= '/'; wp
--)
1036 xp
= Strsave(wp
+ 1);
1038 xp
= Strnsave(cp
, wp
- cp
);
1041 return (Strsave(type
== 'e' ? STRNULL
: cp
));
1049 matchs(const Char
*str
, const Char
*pat
)
1051 while (*str
&& *pat
&& *str
== *pat
)
1057 getsel(int *al
, int *ar
, int dol
)
1061 int first
= *al
< 0;
1066 if (quesarg
== -1) {
1067 seterror(ERR_BADBANGARG
);
1109 while (Isdigit(c
)) {
1110 i
= i
* 10 + c
- '0';
1132 if (*al
> *ar
|| *ar
> dol
) {
1133 seterror(ERR_BADBANGARG
);
1140 static struct wordent
*
1149 c
= sc
== HISTSUB
? (eChar
)HIST
: getC(0);
1150 if (c
== (eChar
)HIST
) {
1164 if (lastev
== eventno
&& alhistp
)
1169 case '#': /* !# is command being typed in (mrh) */
1171 seterror(ERR_HISTLOOP
);
1184 if (any("(=~", c
)) {
1189 Strbuf_terminate(&lhsb
);
1192 while (!cmap(c
, _ESC
| _META
| _QF
| _QB
) && !any("^*-%${}:#", c
)) {
1193 if (event
!= -1 && Isdigit(c
))
1194 event
= event
* 10 + c
- '0';
1197 Strbuf_append1(&lhsb
, c
);
1201 if (lhsb
.len
== 0) {
1202 lhsb
.len
= Strlen(lhsb
.s
); /* lhsb.s wasn't changed */
1206 Strbuf_terminate(&lhsb
);
1209 * History had only digits
1212 event
= eventno
+ (alhistp
== 0) - event
;
1216 Strbuf_append1(&lhsb
, '\0'); /* Allocate space */
1217 Strbuf_terminate(&lhsb
);
1218 memmove(lhsb
.s
+ 1, lhsb
.s
, (lhsb
.len
- 1) * sizeof (*lhsb
.s
));
1221 hp
= findev(lhsb
.s
, 0);
1227 Strbuf_terminate(&lhsb
);
1237 Strbuf_append1(&lhsb
, c
);
1239 if (lhsb
.len
== 0) {
1240 lhsb
.len
= Strlen(lhsb
.s
); /* lhsb.s wasn't changed */
1241 if (lhsb
.len
== 0) {
1242 seterror(ERR_NOSEARCH
);
1247 Strbuf_terminate(&lhsb
);
1248 hp
= findev(lhsb
.s
, 1);
1254 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
)
1255 if (hp
->Hnum
== event
) {
1261 seterror(ERR_NOEVENT
, short2str(np
));
1266 static struct Hist
*
1267 findev(Char
*cp
, int anyarg
)
1271 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
) {
1274 struct wordent
*lp
= hp
->Hlex
.next
;
1278 * The entries added by alias substitution don't have a newline but do
1279 * have a negative event number. Savehist() trims off these entries,
1280 * but it happens before alias expansion, too early to delete those
1281 * from the previous command.
1285 if (lp
->word
[0] == '\n')
1293 while (*p
++ == *q
++);
1297 for (dp
= lp
->word
; *dp
; dp
++) {
1305 while (*p
++ == *q
++);
1309 } while (lp
->word
[0] != '\n');
1311 seterror(ERR_NOEVENT
, short2str(cp
));
1319 if (cp
&& cp
[0] == '\n')
1327 peekread
= (Char
) c
;
1334 static int sincereal
; /* Number of real EOFs we've seen */
1339 if ((c
= peekread
) != 0) {
1349 xprintf("alvecp %c\n", *alvecp
& 0xff);
1352 if ((c
= *alvecp
++) != 0)
1354 if (alvec
&& *alvec
) {
1366 if ((alvecp
= *alvec
) != 0) {
1370 /* Infinite source! */
1376 if ((c
= *evalp
++) != 0)
1378 if (evalvec
&& *evalvec
) {
1386 if (evalvec
== INVPPTR
) {
1390 if ((evalp
= *evalvec
) != 0) {
1398 if (arginp
== INVPTR
|| onelflg
== 1) {
1404 if ((c
= *arginp
++) == 0) {
1412 #endif /* BSDJOBS */
1414 if (c
== CHAR_ERR
) {
1415 #ifndef WINNT_NATIVE
1421 # endif /* TERMIO */
1425 #endif /* !WINNT_NATIVE */
1428 /* was isatty but raw with ignoreeof yields problems */
1429 #ifndef WINNT_NATIVE
1432 if (ioctl(SHIN
, TCGETA
, (ioctl_t
) & tty
) == 0 &&
1433 (tty
.c_lflag
& ICANON
))
1435 if (ioctl(SHIN
, TIOCGETP
, (ioctl_t
) & tty
) == 0 &&
1436 (tty
.sg_flags
& RAW
) == 0)
1437 # endif /* TERMIO */
1439 if (tcgetattr(SHIN
, &tty
) == 0 &&
1440 (tty
.c_lflag
& ICANON
))
1442 #else /* WINNT_NATIVE */
1444 #endif /* !WINNT_NATIVE */
1448 #endif /* BSDJOBS */
1450 if (numeof
!= 0 && ++sincereal
>= numeof
) /* Too many EOFs? Bye! */
1454 (ctpgrp
= tcgetpgrp(FSHTTY
)) != -1 &&
1456 (void) tcsetpgrp(FSHTTY
, tpgrp
);
1459 # endif /* _SEQUENT */
1460 (void) killpg(ctpgrp
, SIGHUP
);
1463 * With the walking process group fix, this message
1464 * is now obsolete. As the foreground process group
1465 * changes, the shell needs to adjust. Well too bad.
1467 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1468 (int)ctpgrp
, (int)tpgrp
);
1469 # endif /* notdef */
1472 #endif /* BSDJOBS */
1473 /* What follows is complicated EOF handling -- sterling@netcom.com */
1474 /* First, we check to see if we have ignoreeof set */
1475 if (adrof(STRignoreeof
)) {
1476 /* If so, we check for any stopped jobs only on the first EOF */
1477 if ((sincereal
== 1) && (chkstop
== 0)) {
1481 /* If we don't have ignoreeof set, always check for stopped jobs */
1486 /* At this point, if there were stopped jobs, we would have already
1487 * called reset(). If we got this far, assume we can print an
1488 * exit/logout message if we ignoreeof, or just exit.
1490 if (adrof(STRignoreeof
)) {
1491 /* If so, tell the user to use exit or logout */
1493 xprintf(CGETS(16, 2,
1494 "\nUse \"logout\" to logout.\n"));
1496 xprintf(CGETS(16, 3,
1497 "\nUse \"exit\" to leave %s.\n"),
1502 /* If we don't have ignoreeof set, just fall through */
1511 if (c
== '\n' && onelflg
)
1514 Strbuf_append1(&histline
, c
);
1523 while (buf
>= fblocks
) {
1524 nfbuf
= xcalloc(fblocks
+ 2, sizeof(Char
**));
1526 (void) blkcpy(nfbuf
, fbuf
);
1530 fbuf
[fblocks
] = xcalloc(BUFSIZE
, sizeof(Char
));
1536 wide_read(int fildes
, Char
*buf
, size_t nchars
, int use_fclens
)
1538 char cbuf
[BUFSIZE
+ 1];
1545 assert (nchars
<= sizeof(cbuf
) / sizeof(*cbuf
));
1551 size_t len
= nchars
> partial
? nchars
- partial
: 1;
1553 if (partial
+ len
>= sizeof(cbuf
) / sizeof(*cbuf
))
1556 r
= xread(fildes
, cbuf
+ partial
, len
);
1558 if (partial
== 0 && r
<= 0)
1562 while (i
< partial
&& nchars
!= 0) {
1565 tlen
= normal_mbtowc(buf
+ res
, cbuf
+ i
, partial
- i
);
1568 if ((partial
- i
) < MB_LEN_MAX
&& r
> 0)
1569 /* Maybe a partial character and there is still a chance
1572 buf
[res
] = (unsigned char)cbuf
[i
] | INVALID_BYTE
;
1585 memmove(cbuf
, cbuf
+ i
, partial
- i
);
1587 } while (partial
!= 0 && nchars
> 0);
1588 /* Throwing away possible partial multibyte characters on error if the
1589 stream is not seekable */
1591 lseek(fildes
, -(off_t
)partial
, L_INCR
);
1593 return res
!= 0 ? res
: r
;
1601 int numleft
= 0, roomleft
;
1604 if (fseekp
< fbobp
|| fseekp
> feobp
) {
1605 fbobp
= feobp
= fseekp
;
1606 (void) lseek(SHIN
, fseekp
, L_SET
);
1608 if (fseekp
== feobp
) {
1614 for (i
= 0; i
< (size_t)(feobp
- fbobp
); i
++)
1616 fseekp
= feobp
= bytes
;
1619 c
= wide_read(SHIN
, fbuf
[0], BUFSIZE
, 1);
1622 stderror(ERR_SYSTEM
, progname
, strerror(errno
));
1628 #ifndef WINNT_NATIVE
1629 ch
= fbuf
[0][fseekp
- fbobp
];
1633 ch
= fbuf
[0][fseekp
- fbobp
];
1635 } while(ch
== '\r');
1636 #endif /* !WINNT_NATIVE */
1640 while (fseekp
>= feobp
) {
1642 #if defined(FILEC) && defined(TIOCSTI)
1644 #endif /* FILEC && TIOCSTI */
1645 ) && intty
) { /* then use twenex routine */
1646 fseekp
= feobp
; /* where else? */
1647 #if defined(FILEC) && defined(TIOCSTI)
1649 c
= numleft
= tenex(InputBuf
, BUFSIZE
);
1651 #endif /* FILEC && TIOCSTI */
1652 c
= numleft
= Inputl(); /* PWP: get a line */
1653 while (numleft
> 0) {
1654 off
= (int) feobp
% BUFSIZE
;
1655 buf
= (int) feobp
/ BUFSIZE
;
1657 roomleft
= BUFSIZE
- off
;
1658 if (roomleft
> numleft
)
1660 (void) memcpy(fbuf
[buf
] + off
, InputBuf
+ c
- numleft
,
1661 roomleft
* sizeof(Char
));
1662 numleft
-= roomleft
;
1666 off
= (int) feobp
% BUFSIZE
;
1667 buf
= (int) feobp
/ BUFSIZE
;
1669 roomleft
= BUFSIZE
- off
;
1670 c
= wide_read(SHIN
, fbuf
[buf
] + off
, roomleft
, 0);
1674 if (c
== 0 || (c
< 0 && fixio(SHIN
, errno
) == -1))
1679 (void) check_window_size(0); /* for window systems */
1680 #endif /* SIG_WINDOW */
1681 #ifndef WINNT_NATIVE
1682 ch
= fbuf
[(int) fseekp
/ BUFSIZE
][(int) fseekp
% BUFSIZE
];
1686 ch
= fbuf
[(int) fseekp
/ BUFSIZE
][(int) fseekp
% BUFSIZE
];
1688 } while(ch
== '\r');
1689 #endif /* !WINNT_NATIVE */
1702 sb
= (int) (fseekp
- 1) / BUFSIZE
;
1704 for (i
= 0; i
< sb
; i
++)
1706 (void) blkcpy(fbuf
, &fbuf
[sb
]);
1707 fseekp
-= BUFSIZE
* sb
;
1708 feobp
-= BUFSIZE
* sb
;
1714 bseek(struct Ain
*l
)
1716 switch (aret
= l
->type
) {
1718 evalvec
= l
->a_seek
;
1721 xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec
, evalp
);
1728 xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec
, alvecp
);
1733 xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp
);
1738 if (fseekp
>= fbobp
&& feobp
>= fbobp
) {
1743 for (i
= 0; i
< (size_t)(feobp
- fbobp
); i
++) {
1755 fbobp
= feobp
= fseekp
+ 1; /* To force lseek() */
1760 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret
);
1765 /* any similarity to bell telephone is purely accidental */
1767 btell(struct Ain
*l
)
1769 switch (l
->type
= aret
) {
1771 l
->a_seek
= evalvec
;
1774 xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec
, evalp
);
1781 xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec
, alvecp
);
1786 if (cantell
&& fseekp
>= fbobp
&& fseekp
<= feobp
) {
1790 for (i
= 0; i
< (size_t)(fseekp
- fbobp
); i
++)
1791 l
->f_seek
+= fclens
[i
];
1798 xprintf(CGETS(16, 10, "tell file %x\n"), fseekp
);
1802 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret
);
1810 (void) lseek(SHIN
, (off_t
) 0, L_XTND
);
1826 if (arginp
|| onelflg
|| intty
)
1828 if ((x
= lseek(SHIN
, (off_t
) 0, L_INCR
)) == -1)
1830 fbuf
= xcalloc(2, sizeof(Char
**));
1832 fbuf
[0] = xcalloc(BUFSIZE
, sizeof(Char
));
1833 fseekp
= fbobp
= feobp
= x
;