1 /* $Header: /src/pub/tcsh/sh.lex.c,v 3.56 2002/07/08 20:57:32 christos 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("$Id: sh.lex.c,v 3.56 2002/07/08 20:57:32 christos Exp $")
38 /* #define DEBUG_INP */
39 /* #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
__P((void));
51 static int getC1
__P((int));
52 static void getdol
__P((void));
53 static void getexcl
__P((int));
54 static struct Hist
*findev
__P((Char
*, bool));
55 static void setexclp
__P((Char
*));
56 static int bgetc
__P((void));
57 static void balloc
__P((int));
58 static void bfree
__P((void));
59 static struct wordent
*gethent
__P((int));
60 static int matchs
__P((Char
*, Char
*));
61 static int getsel
__P((int *, int *, int));
62 static struct wordent
*getsub
__P((struct wordent
*));
63 static Char
*subword
__P((Char
*, int, bool *));
64 static struct wordent
*dosub
__P((int, struct wordent
*, bool));
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 static Char labuf
[BUFSIZE
];
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 bool hadhist
= 0;
117 * Avoid alias expansion recursion via \!#
121 Char histline
[BUFSIZE
+ 2]; /* last line input */
123 /* The +2 is to fool hp's optimizer */
124 bool histvalid
= 0; /* is histline valid */
125 static Char
*histlinep
= NULL
; /* current pointer into histline */
129 #define getC(f) (((getCtmp = peekc) != '\0') ? (peekc = 0, getCtmp) : getC1(f))
130 #define ungetC(c) peekc = (Char) c
131 #define ungetD(c) peekd = (Char) c
133 /* Use Htime to store timestamps picked up from history file for enthist()
134 * if reading saved history (sg)
136 time_t Htime
= (time_t)0;
137 static time_t a2time_t
__P((Char
*));
140 * for history event processing
141 * in the command 'echo !?foo?:1 !$' we want the !$ to expand from the line
142 * 'foo' was found instead of the last command
144 static int uselastevent
= 1;
156 histlinep
= histline
;
160 hp
->next
= hp
->prev
= hp
;
165 while (c
== ' ' || c
== '\t');
166 if (c
== HISTSUB
&& intty
)
167 /* ^lef^rit from tty is short !:s^lef^rit */
173 * The following loop is written so that the links needed by freelex will
174 * be ready and rarin to go even if it is interrupted.
179 new = (struct wordent
*) xmalloc((size_t) sizeof(*wdp
));
187 } while (wdp
->word
[0] != '\n');
188 if (histlinep
< histline
+ BUFSIZE
) {
190 if (histlinep
> histline
&& histlinep
[-1] == '\n')
191 histlinep
[-1] = '\0';
195 histline
[BUFSIZE
- 1] = '\0';
205 /* Attempt to distinguish timestamps from other possible entries.
206 * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
212 if (!word
|| *(s
= word
) != '+')
215 for (++s
, ret
= 0, ct
= 0; *s
; ++s
, ++ct
)
217 if (!isdigit((unsigned char)*s
))
219 ret
= ret
* 10 + (time_t)((unsigned char)*s
- '0');
232 struct wordent
*sp
= sp0
->next
;
235 xprintf("%S", sp
->word
);
239 if (sp
->word
[0] != '\n')
256 new = (struct wordent
*) xmalloc((size_t) sizeof(*wdp
));
263 wdp
->word
= Strsave(fp
->word
);
265 } while (wdp
->word
[0] != '\n');
274 while (vp
->next
!= vp
) {
277 if (fp
->word
!= STRNULL
)
278 xfree((ptr_t
) fp
->word
);
294 #if defined(DSPMBYTE)
296 #endif /* DSPMBYTE */
301 while ((c
= getC(DOALL
)) == ' ' || c
== '\t')
303 if (cmap(c
, _META
| _ESC
))
329 Htime
= a2time_t(hbuf
);
357 #if defined(DSPMBYTE)
360 else if (mbytepos
== 1 && Ismbyte1(c
) && 2 <= i
)
363 #endif /* DSPMBYTE */
369 else if (c
== '\\') {
372 * PWP: this is dumb, but how all of the other shells work. If \ quotes
373 * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
374 * following character INSIDE a set of ''s.
376 * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
382 ((c
== '\'') || (c
== '"') ||
389 * if (c1 == '`') c = ' '; else
397 else if (c
== '\n') {
398 seterror(ERR_UNMATCHED
, c1
);
403 else if (cmap(c
, _META
| _QF
| _QB
| _ESC
)) {
415 else if (cmap(c
, _QF
| _QB
)) { /* '"` */
417 dolflg
= c
== '"' ? DOALL
: DOEXCL
;
419 else if (c
!= '#' || !intty
) {
429 seterror(ERR_WTOOLONG
);
436 return (Strsave(wbuf
));
446 if ((c
= peekc
) != 0) {
451 if ((c
= *lap
++) == 0)
454 if (cmap(c
, _META
| _QF
| _QB
))
459 if ((c
= peekd
) != 0) {
464 if ((c
= *exclp
++) != 0)
466 if (exclnxt
&& --exclc
>= 0) {
467 exclnxt
= exclnxt
->next
;
468 setexclp(exclnxt
->word
);
473 /* this will throw away the dummy history entries */
478 exclnxt
= exclnxt
->next
;
482 setexclp(exclnxt
->word
);
486 if (c
== '$' && (flag
& DODOL
)) {
490 if (c
== HIST
&& (flag
& DOEXCL
)) {
503 Char name
[4 * MAXVARLEN
+ 1];
506 bool special
= 0, toolong
;
508 np
= name
, *np
++ = '$';
509 c
= sc
= getC(DOEXCL
);
510 if (any("\t \n", c
)) {
516 *np
++ = (Char
) c
, c
= getC(DOEXCL
);
517 if (c
== '#' || c
== '?' || c
== '%')
518 special
++, *np
++ = (Char
) c
, c
= getC(DOEXCL
);
526 seterror(ERR_SPDOLLT
);
535 seterror(ERR_NEWLINE
);
542 seterror(ERR_SPSTAR
);
551 /* let $?0 pass for now */
559 /* we know that np < &name[4] */
561 while ((c
= getC(DOEXCL
)) != 0) {
570 else if (letter(c
)) {
571 /* we know that np < &name[4] */
574 while ((c
= getC(DOEXCL
)) != 0) {
575 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
576 if (!letter(c
) && !Isdigit(c
))
586 seterror(ERR_VARILL
);
596 seterror(ERR_VARTOOLONG
);
606 * Name up to here is a max of MAXVARLEN + 8.
608 ep
= &np
[2 * MAXVARLEN
+ 8];
611 * Michael Greim: Allow $ expansion to take place in selector
612 * expressions. (limits the number of characters returned)
614 c
= getC(DOEXCL
| DODOL
);
618 seterror(ERR_NLINDEX
);
628 seterror(ERR_SELOVFL
);
635 * Name up to here is a max of 2 * MAXVARLEN + 8.
639 * if the :g modifier is followed by a newline, then error right away!
643 int gmodflag
= 0, amodflag
= 0;
648 *np
++ = (Char
) c
, c
= getC(DOEXCL
);
649 if (c
== 'g' || c
== 'a') {
654 *np
++ = (Char
) c
; c
= getC(DOEXCL
);
656 if ((c
== 'g' && !gmodflag
) || (c
== 'a' && !amodflag
)) {
661 *np
++ = (Char
) c
; c
= getC(DOEXCL
);
664 /* scan s// [eichin:19910926.0512EST] */
668 *np
++ = (Char
) delim
;
670 if (!delim
|| letter(delim
)
671 || Isdigit(delim
) || any(" \t\n", delim
)) {
672 seterror(ERR_BADSUBST
);
675 while ((c
= getC(0)) != (-1)) {
677 if(c
== delim
) delimcnt
--;
681 seterror(ERR_BADSUBST
);
686 if (!any("htrqxesul", c
)) {
687 if ((amodflag
|| gmodflag
) && c
== '\n')
688 stderror(ERR_VARSYN
); /* strike */
689 seterror(ERR_BADMOD
, c
);
696 while ((c
= getC(DOEXCL
)) == ':');
706 seterror(ERR_MISSING
, '}');
724 if (Strlen(cp
) + (lap
? Strlen(lap
) : 0) >=
725 (sizeof(labuf
) - 4) / sizeof(Char
)) {
726 seterror(ERR_EXPOVFL
);
730 (void) Strcpy(buf
, lap
);
731 (void) Strcpy(labuf
, cp
);
733 (void) Strcat(labuf
, buf
);
737 static Char lhsb
[32];
738 static Char slhs
[32];
739 static Char rhsb
[64];
746 struct wordent
*hp
, *ip
;
747 int left
, right
, dol
;
771 for (ip
= hp
->next
->next
; ip
!= alhistt
; ip
= ip
->next
)
774 for (ip
= hp
->next
->next
; ip
!= hp
->prev
; ip
= ip
->next
)
776 left
= 0, right
= dol
;
778 ungetC('s'), unreadc(HISTSUB
), c
= ':';
782 if (!any(":^$*-%", c
))
788 if (letter(c
) || c
== '&') {
790 left
= 0, right
= dol
;
796 if (!getsel(&left
, &right
, dol
))
802 if (!getsel(&left
, &right
, dol
))
807 exclc
= right
- left
+ 1;
810 if (sc
== HISTSUB
|| c
== ':') {
820 seterror(ERR_BADBANG
);
825 static struct wordent
*
834 Char orhsb
[sizeof(rhsb
) / sizeof(Char
)];
842 if (c
== 'g' || c
== 'a') {
843 global
|= (c
== 'g') ? 1 : 2;
846 if (((c
=='g') && !(global
& 1)) || ((c
== 'a') && !(global
& 2))) {
847 global
|= (c
== 'g') ? 1 : 2;
871 seterror(ERR_NOSUBST
);
874 (void) Strcpy(lhsb
, slhs
);
886 if (letter(delim
) || Isdigit(delim
) || any(" \t\n", delim
)) {
889 seterror(ERR_BADSUBST
);
901 if (cp
> &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2]) {
903 seterror(ERR_BADSUBST
);
908 if (c
!= delim
&& c
!= '\\')
915 else if (lhsb
[0] == 0) {
920 (void) Strcpy(orhsb
, cp
);
931 if (&cp
[Strlen(orhsb
)] > &rhsb
[sizeof(rhsb
) /
934 (void) Strcpy(cp
, orhsb
);
939 if (cp
> &rhsb
[sizeof(rhsb
) / sizeof(Char
) - 2]) {
940 seterror(ERR_RHSLONG
);
945 if (c
!= delim
/* && c != '~' */ )
956 seterror(ERR_BADBANGMOD
, c
);
959 (void) Strcpy(slhs
, lhsb
);
961 en
= dosub(sc
, en
, global
);
964 while ((c
= getC(0)) == ':');
972 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
974 * when using history substitution, and the variable
975 * 'history' is set to a value higher than 1000,
976 * the shell might either freeze (hang) or core-dump.
977 * We raise the limit to 50000000
980 #define HIST_PURGE -50000000
981 static struct wordent
*
982 dosub(sc
, en
, global
)
988 bool didsub
= 0, didone
= 0;
989 struct wordent
*hp
= &lexi
;
996 struct wordent
*new =
997 (struct wordent
*) xcalloc(1, sizeof *wdp
);
1006 Char
*tword
, *otword
;
1008 if ((global
& 1) || didsub
== 0) {
1009 tword
= subword(en
->word
, sc
, &didone
);
1013 while (didone
&& tword
!= STRNULL
) {
1015 tword
= subword(otword
, sc
, &didone
);
1016 if (Strcmp(tword
, otword
) == 0) {
1017 xfree((ptr_t
) otword
);
1021 xfree((ptr_t
) otword
);
1026 tword
= Strsave(en
->word
);
1031 seterror(ERR_MODFAIL
);
1034 * ANSI mode HP/UX compiler chokes on
1035 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
1037 hst
= enthist(HIST_PURGE
, &lexi
, 0, 0);
1038 return &(hst
->Hlex
);
1042 subword(cp
, type
, adid
)
1062 wp
= domod(cp
, type
);
1064 return (Strsave(cp
));
1071 for (mp
= cp
; *mp
; mp
++)
1072 if (matchs(mp
, lhsb
)) {
1073 for (np
= cp
; np
< mp
;)
1075 for (np
= rhsb
; *np
; np
++)
1085 seterror(ERR_SUBOVFL
);
1094 seterror(ERR_SUBOVFL
);
1098 (void) Strcat(wp
, lhsb
);
1105 seterror(ERR_SUBOVFL
);
1109 (void) Strcat(wp
, mp
);
1111 return (Strsave(wbuf
));
1113 return (Strsave(cp
));
1130 for (xp
= wp
; (c
= *xp
) != 0; xp
++)
1131 if ((c
!= ' ' && c
!= '\t') || type
== 'q')
1137 for (cp
= wp
; *cp
; cp
++)
1146 for (cp
= wp
; *cp
; cp
++)
1155 if (!any(short2str(cp
), '/'))
1156 return (type
== 't' ? Strsave(cp
) : 0);
1158 while (*--wp
!= '/')
1161 xp
= Strsave(cp
), xp
[wp
- cp
] = 0;
1163 xp
= Strsave(wp
+ 1);
1169 for (wp
--; wp
>= cp
&& *wp
!= '/'; wp
--)
1172 xp
= Strsave(wp
+ 1);
1174 xp
= Strsave(cp
), xp
[wp
- cp
] = 0;
1177 return (Strsave(type
== 'e' ? STRNULL
: cp
));
1188 while (*str
&& *pat
&& *str
== *pat
)
1200 bool first
= *al
< 0;
1205 if (quesarg
== -1) {
1206 seterror(ERR_BADBANGARG
);
1248 while (Isdigit(c
)) {
1249 i
= i
* 10 + c
- '0';
1271 if (*al
> *ar
|| *ar
> dol
) {
1272 seterror(ERR_BADBANGARG
);
1279 static struct wordent
*
1289 c
= sc
== HISTSUB
? HIST
: getC(0);
1304 if (lastev
== eventno
&& alhistp
)
1309 case '#': /* !# is command being typed in (mrh) */
1311 seterror(ERR_HISTLOOP
);
1324 if (any("(=~", c
)) {
1331 while (!cmap(c
, _ESC
| _META
| _QF
| _QB
) && !any("^*-%${}:#", c
)) {
1332 if (event
!= -1 && Isdigit(c
))
1333 event
= event
* 10 + c
- '0';
1336 if (np
< &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2])
1348 * History had only digits
1351 event
= eventno
+ (alhistp
== 0) - (event
? event
: 0);
1355 event
= sizeof(lhsb
) / sizeof(lhsb
[0]);
1356 np
= &lhsb
[--event
];
1358 for (event
--; np
> lhsb
; *np
-- = lhsb
[--event
])
1362 hp
= findev(lhsb
, 0);
1377 if (np
< &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2])
1382 seterror(ERR_NOSEARCH
);
1388 hp
= findev(lhsb
, 1);
1394 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
)
1395 if (hp
->Hnum
== event
) {
1401 seterror(ERR_NOEVENT
, short2str(np
));
1405 static struct Hist
*
1412 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
) {
1415 struct wordent
*lp
= hp
->Hlex
.next
;
1419 * The entries added by alias substitution don't have a newline but do
1420 * have a negative event number. Savehist() trims off these entries,
1421 * but it happens before alias expansion, too early to delete those
1422 * from the previous command.
1426 if (lp
->word
[0] == '\n')
1434 while (*p
++ == *q
++);
1438 for (dp
= lp
->word
; *dp
; dp
++) {
1446 while (*p
++ == *q
++);
1450 } while (lp
->word
[0] != '\n');
1452 seterror(ERR_NOEVENT
, short2str(cp
));
1461 if (cp
&& cp
[0] == '\n')
1470 peekread
= (Char
) c
;
1478 static int sincereal
; /* Number of real EOFs we've seen */
1479 Char
*ptr
; /* For STRignoreeof */
1480 int numeof
= 0; /* Value of STRignoreeof */
1485 if ((c
= peekread
) != 0) {
1490 /* Compute the value of EOFs */
1491 if ((ptr
= varval(STRignoreeof
)) != STRNULL
) {
1493 if (!Isdigit(*ptr
)) {
1497 numeof
= numeof
* 10 + *ptr
++ - '0';
1502 if (numeof
< 0) numeof
= 26; /* Sanity check */
1509 xprintf("alvecp %c\n", *alvecp
& 0xff);
1512 if ((c
= *alvecp
++) != 0)
1514 if (alvec
&& *alvec
) {
1526 if ((alvecp
= *alvec
) != 0) {
1530 /* Infinite source! */
1536 if ((c
= *evalp
++) != 0)
1538 if (evalvec
&& *evalvec
) {
1546 if (evalvec
== INVPPTR
) {
1550 if ((evalp
= *evalvec
) != 0) {
1558 if (arginp
== INVPTR
|| onelflg
== 1) {
1564 if ((c
= *arginp
++) == 0) {
1572 #endif /* BSDJOBS */
1575 #ifndef WINNT_NATIVE
1581 # endif /* TERMIO */
1585 #endif /* !WINNT_NATIVE */
1588 /* was isatty but raw with ignoreeof yields problems */
1589 #ifndef WINNT_NATIVE
1592 if (ioctl(SHIN
, TCGETA
, (ioctl_t
) & tty
) == 0 &&
1593 (tty
.c_lflag
& ICANON
))
1595 if (ioctl(SHIN
, TIOCGETP
, (ioctl_t
) & tty
) == 0 &&
1596 (tty
.sg_flags
& RAW
) == 0)
1597 # endif /* TERMIO */
1599 if (tcgetattr(SHIN
, &tty
) == 0 &&
1600 (tty
.c_lflag
& ICANON
))
1602 #else /* WINNT_NATIVE */
1604 #endif /* !WINNT_NATIVE */
1608 #endif /* BSDJOBS */
1610 if (numeof
!= 0 && ++sincereal
>= numeof
) /* Too many EOFs? Bye! */
1614 (ctpgrp
= tcgetpgrp(FSHTTY
)) != -1 &&
1616 (void) tcsetpgrp(FSHTTY
, tpgrp
);
1619 # endif /* _SEQUENT */
1620 (void) killpg((pid_t
) ctpgrp
, SIGHUP
);
1623 * With the walking process group fix, this message
1624 * is now obsolete. As the foreground process group
1625 * changes, the shell needs to adjust. Well too bad.
1627 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1629 # endif /* notdef */
1632 #endif /* BSDJOBS */
1633 /* What follows is complicated EOF handling -- sterling@netcom.com */
1634 /* First, we check to see if we have ignoreeof set */
1635 if (adrof(STRignoreeof
)) {
1636 /* If so, we check for any stopped jobs only on the first EOF */
1637 if ((sincereal
== 1) && (chkstop
== 0)) {
1641 /* If we don't have ignoreeof set, always check for stopped jobs */
1646 /* At this point, if there were stopped jobs, we would have already
1647 * called reset(). If we got this far, assume we can print an
1648 * exit/logout message if we ignoreeof, or just exit.
1650 if (adrof(STRignoreeof
)) {
1651 /* If so, tell the user to use exit or logout */
1653 xprintf(CGETS(16, 2,
1654 "\nUse \"logout\" to logout.\n"));
1656 xprintf(CGETS(16, 3,
1657 "\nUse \"exit\" to leave %s.\n"),
1662 /* If we don't have ignoreeof set, just fall through */
1671 if (c
== '\n' && onelflg
)
1674 if (histlinep
< histline
+ BUFSIZE
)
1675 *histlinep
++ = (Char
) c
;
1685 while (buf
>= fblocks
) {
1686 nfbuf
= (Char
**) xcalloc((size_t) (fblocks
+ 2),
1689 (void) blkcpy(nfbuf
, fbuf
);
1690 xfree((ptr_t
) fbuf
);
1693 fbuf
[fblocks
] = (Char
*) xcalloc(BUFSIZE
, sizeof(Char
));
1702 int numleft
= 0, roomleft
;
1703 char tbuf
[BUFSIZE
+ 1];
1706 if (fseekp
< fbobp
|| fseekp
> feobp
) {
1707 fbobp
= feobp
= fseekp
;
1708 (void) lseek(SHIN
, fseekp
, L_SET
);
1710 if (fseekp
== feobp
) {
1715 c
= read(SHIN
, tbuf
, BUFSIZE
);
1716 while (c
< 0 && errno
== EINTR
);
1719 stderror(ERR_SYSTEM
, progname
, strerror(errno
));
1723 for (i
= 0; i
< c
; i
++)
1724 fbuf
[0][i
] = (unsigned char) tbuf
[i
];
1727 #ifndef WINNT_NATIVE
1728 c
= fbuf
[0][fseekp
- fbobp
];
1732 c
= fbuf
[0][fseekp
- fbobp
];
1735 #endif /* !WINNT_NATIVE */
1739 while (fseekp
>= feobp
) {
1741 #if defined(FILEC) && defined(TIOCSTI)
1743 #endif /* FILEC && TIOCSTI */
1744 ) && intty
) { /* then use twenex routine */
1745 fseekp
= feobp
; /* where else? */
1746 #if defined(FILEC) && defined(TIOCSTI)
1748 c
= numleft
= tenex(InputBuf
, BUFSIZE
);
1750 #endif /* FILEC && TIOCSTI */
1751 c
= numleft
= Inputl(); /* PWP: get a line */
1752 while (numleft
> 0) {
1753 off
= (int) feobp
% BUFSIZE
;
1754 buf
= (int) feobp
/ BUFSIZE
;
1756 roomleft
= BUFSIZE
- off
;
1757 if (roomleft
> numleft
)
1759 (void) memmove((ptr_t
) (fbuf
[buf
] + off
),
1760 (ptr_t
) (InputBuf
+ c
- numleft
),
1761 (size_t) (roomleft
* sizeof(Char
)));
1762 numleft
-= roomleft
;
1766 off
= (int) feobp
% BUFSIZE
;
1767 buf
= (int) feobp
/ BUFSIZE
;
1769 roomleft
= BUFSIZE
- off
;
1770 c
= read(SHIN
, tbuf
, (size_t) roomleft
);
1773 Char
*ptr
= fbuf
[buf
] + off
;
1775 for (i
= 0; i
< c
; i
++)
1776 ptr
[i
] = (unsigned char) tbuf
[i
];
1780 if (c
== 0 || (c
< 0 && fixio(SHIN
, errno
) == -1))
1783 #ifndef WINNT_NATIVE
1784 c
= fbuf
[(int) fseekp
/ BUFSIZE
][(int) fseekp
% BUFSIZE
];
1788 c
= fbuf
[(int) fseekp
/ BUFSIZE
][(int) fseekp
% BUFSIZE
];
1791 #endif /* !WINNT_NATIVE */
1804 sb
= (int) (fseekp
- 1) / BUFSIZE
;
1806 for (i
= 0; i
< sb
; i
++)
1807 xfree((ptr_t
) fbuf
[i
]);
1808 (void) blkcpy(fbuf
, &fbuf
[sb
]);
1809 fseekp
-= BUFSIZE
* sb
;
1810 feobp
-= BUFSIZE
* sb
;
1819 switch (aret
= l
->type
) {
1821 evalvec
= l
->a_seek
;
1824 xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec
, evalp
);
1831 xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec
, alvecp
);
1836 xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp
);
1841 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret
);
1846 /* any similarity to bell telephone is purely accidental */
1851 switch (l
->type
= aret
) {
1853 l
->a_seek
= evalvec
;
1856 xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec
, evalp
);
1863 xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec
, alvecp
);
1871 xprintf(CGETS(16, 10, "tell file %x\n"), fseekp
);
1875 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret
);
1883 (void) lseek(SHIN
, (off_t
) 0, L_XTND
);
1899 if (arginp
|| onelflg
|| intty
)
1901 if ((x
= lseek(SHIN
, (off_t
) 0, L_INCR
)) == -1)
1903 fbuf
= (Char
**) xcalloc(2, sizeof(Char
**));
1905 fbuf
[0] = (Char
*) xcalloc(BUFSIZE
, sizeof(Char
));
1906 fseekp
= fbobp
= feobp
= x
;