kernel - Fix excessive ipiq recursion (4)
[dragonfly.git] / contrib / tcsh-6 / sh.lex.c
blob80643f7b1731302b9f5a730a28df419e56ce2a9e
1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.lex.c,v 3.87 2011/01/24 17:48:15 christos Exp $ */
2 /*
3 * sh.lex.c: Lexical analysis into tokens
4 */
5 /*-
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
31 * SUCH DAMAGE.
33 #include "sh.h"
35 RCSID("$tcsh: sh.lex.c,v 3.87 2011/01/24 17:48:15 christos Exp $")
37 #include "ed.h"
39 #include <assert.h>
40 /* #define DEBUG_INP */
41 /* #define DEBUG_SEEK */
44 * C shell
47 #define FLAG_G 1
48 #define FLAG_A 2
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 */
98 static int exclc = 0;
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
117 * argument list.
119 static int hadhist = 0;
122 * Avoid alias expansion recursion via \!#
124 int hleft;
126 struct Strbuf histline; /* = Strbuf_INIT; last line input */
128 int histvalid = 0; /* is histline valid */
130 static Char getCtmp;
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)
150 struct wordent *wdp;
151 eChar c;
152 int parsehtime = enterhist;
154 histvalid = 0;
155 histline.len = 0;
157 btell(&lineloc);
158 hp->next = hp->prev = hp;
159 hp->word = STRNULL;
160 hadhist = 0;
162 c = readc(0);
163 while (c == ' ' || c == '\t');
164 if (c == (eChar)HISTSUB && intty)
165 /* ^lef^rit from tty is short !:s^lef^rit */
166 getexcl(c);
167 else
168 unreadc(c);
169 cleanup_push(hp, lex_cleanup);
170 wdp = hp;
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.
175 do {
176 struct wordent *new;
178 new = xmalloc(sizeof(*new));
179 new->word = NULL;
180 new->prev = wdp;
181 new->next = hp;
182 wdp->next = new;
183 hp->prev = new;
184 wdp = new;
185 wdp->word = word(parsehtime);
186 parsehtime = 0;
187 } while (wdp->word[0] != '\n');
188 cleanup_ignore(hp);
189 cleanup_until(hp);
190 Strbuf_terminate(&histline);
191 if (histline.len != 0 && histline.s[histline.len - 1] == '\n')
192 histline.s[histline.len - 1] = '\0';
193 histvalid = 1;
195 return (hadhist);
198 static time_t
199 a2time_t(Char *wordx)
201 /* Attempt to distinguish timestamps from other possible entries.
202 * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
204 time_t ret;
205 Char *s;
206 int ct;
208 if (!wordx || *(s = wordx) != '+')
209 return (time_t)0;
211 for (++s, ret = 0, ct = 0; *s; ++s, ++ct) {
212 if (!isdigit((unsigned char)*s))
213 return (time_t)0;
214 ret = ret * 10 + (time_t)((unsigned char)*s - '0');
217 if (ct != 10)
218 return (time_t)0;
220 return ret;
223 void
224 prlex(struct wordent *sp0)
226 struct wordent *sp = sp0->next;
228 for (;;) {
229 xprintf("%S", sp->word);
230 sp = sp->next;
231 if (sp == sp0)
232 break;
233 if (sp->word[0] != '\n')
234 xputchar(' ');
238 void
239 copylex(struct wordent *hp, struct wordent *fp)
241 struct wordent *wdp;
243 wdp = hp;
244 fp = fp->next;
245 do {
246 struct wordent *new;
248 new = xmalloc(sizeof(*new));
249 new->word = NULL;
250 new->prev = wdp;
251 new->next = hp;
252 wdp->next = new;
253 hp->prev = new;
254 wdp = new;
255 wdp->word = Strsave(fp->word);
256 fp = fp->next;
257 } while (wdp->word[0] != '\n');
260 void
261 freelex(struct wordent *vp)
263 struct wordent *fp;
265 while (vp->next != vp) {
266 fp = vp->next;
267 vp->next = fp->next;
268 xfree(fp->word);
269 xfree(fp);
271 vp->prev = vp;
274 void
275 lex_cleanup(void *xvp)
277 struct wordent *vp;
279 vp = xvp;
280 freelex(vp);
283 static Char *
284 word(int parsehtime)
286 eChar c, c1;
287 struct Strbuf wbuf = Strbuf_INIT;
288 Char hbuf[12];
289 int h;
290 int dolflg;
292 cleanup_push(&wbuf, Strbuf_cleanup);
293 loop:
294 while ((c = getC(DOALL)) == ' ' || c == '\t')
295 continue;
296 if (cmap(c, _META | _ESC))
297 switch (c) {
298 case '&':
299 case '|':
300 case '<':
301 case '>':
302 Strbuf_append1(&wbuf, c);
303 c1 = getC(DOALL);
304 if (c1 == c)
305 Strbuf_append1(&wbuf, c1);
306 else
307 ungetC(c1);
308 goto ret;
310 case '#':
311 if (intty || (enterhist && !parsehtime))
312 break;
313 c = 0;
314 h = 0;
315 do {
316 c1 = c;
317 c = getC(0);
318 if (h < 11 && parsehtime)
319 hbuf[h++] = c;
320 } while (c != '\n');
321 if (parsehtime) {
322 hbuf[11] = '\0';
323 Htime = a2time_t(hbuf);
325 if (c1 == '\\')
326 goto loop;
327 /*FALLTHROUGH*/
329 case ';':
330 case '(':
331 case ')':
332 case '\n':
333 Strbuf_append1(&wbuf, c);
334 goto ret;
336 case '\\':
337 c = getC(0);
338 if (c == '\n') {
339 if (onelflg == 1)
340 onelflg = 2;
341 goto loop;
343 if (c != (eChar)HIST)
344 Strbuf_append1(&wbuf, '\\');
345 c |= QUOTE;
346 default:
347 break;
349 c1 = 0;
350 dolflg = DOALL;
351 for (;;) {
352 if (c1) {
353 if (c == c1) {
354 c1 = 0;
355 dolflg = DOALL;
357 else if (c == '\\') {
358 c = getC(0);
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)
367 c |= QUOTE;
368 else {
369 if (bslash_quote &&
370 ((c == '\'') || (c == '"') ||
371 (c == '\\') || (c == '$'))) {
372 c |= QUOTE;
374 else {
375 if (c == '\n')
377 * if (c1 == '`') c = ' '; else
379 c |= QUOTE;
380 ungetC(c);
381 c = '\\';
385 else if (c == '\n') {
386 seterror(ERR_UNMATCHED, c1);
387 ungetC(c);
388 break;
391 else if (cmap(c, _META | _QF | _QB | _ESC)) {
392 if (c == '\\') {
393 c = getC(0);
394 if (c == '\n') {
395 if (onelflg == 1)
396 onelflg = 2;
397 break;
399 if (c != (eChar)HIST)
400 Strbuf_append1(&wbuf, '\\');
401 c |= QUOTE;
403 else if (cmap(c, _QF | _QB)) { /* '"` */
404 c1 = c;
405 dolflg = c == '"' ? DOALL : DOEXCL;
407 else if (c != '#' || (!intty && !enterhist)) {
408 ungetC(c);
409 break;
412 Strbuf_append1(&wbuf, c);
413 c = getC(dolflg);
415 ret:
416 cleanup_ignore(&wbuf);
417 cleanup_until(&wbuf);
418 return Strbuf_finish(&wbuf);
421 static eChar
422 getC1(int flag)
424 eChar c;
426 for (;;) {
427 if ((c = peekc) != 0) {
428 peekc = 0;
429 return (c);
431 if (lap < labuf.len) {
432 c = labuf.s[lap++];
433 if (cmap(c, _META | _QF | _QB))
434 c |= QUOTE;
435 return (c);
437 if ((c = peekd) != 0) {
438 peekd = 0;
439 return (c);
441 if (exclp) {
442 if ((c = *exclp++) != 0)
443 return (c);
444 if (exclnxt && --exclc >= 0) {
445 exclnxt = exclnxt->next;
446 setexclp(exclnxt->word);
447 return (' ');
449 exclp = 0;
450 exclnxt = 0;
451 /* this will throw away the dummy history entries */
452 savehist(NULL, 0);
455 if (exclnxt) {
456 exclnxt = exclnxt->next;
457 if (--exclc < 0)
458 exclnxt = 0;
459 else
460 setexclp(exclnxt->word);
461 continue;
463 c = readc(1);
465 /* Catch EOF in the middle of a line. (An EOF at the beginning of
466 * a line would have been processed by the readc(0) in lex().) */
467 if (c == CHAR_ERR)
468 c = '\n';
470 if (c == '$' && (flag & DODOL)) {
471 getdol();
472 continue;
474 if (c == (eChar)HIST && (flag & DOEXCL)) {
475 getexcl(0);
476 continue;
478 break;
480 return (c);
483 static void
484 getdol(void)
486 struct Strbuf name = Strbuf_INIT;
487 eChar c;
488 eChar sc;
489 int special = 0;
491 c = sc = getC(DOEXCL);
492 if (any("\t \n", c)) {
493 ungetD(c);
494 ungetC('$' | QUOTE);
495 return;
497 cleanup_push(&name, Strbuf_cleanup);
498 Strbuf_append1(&name, '$');
499 if (c == '{')
500 Strbuf_append1(&name, c), c = getC(DOEXCL);
501 if (c == '#' || c == '?' || c == '%')
502 special++, Strbuf_append1(&name, c), c = getC(DOEXCL);
503 Strbuf_append1(&name, c);
504 switch (c) {
506 case '<':
507 case '$':
508 case '!':
509 if (special)
510 seterror(ERR_SPDOLLT);
511 goto end;
513 case '\n':
514 ungetD(c);
515 name.len--;
516 if (!special)
517 seterror(ERR_NEWLINE);
518 goto end;
520 case '*':
521 if (special)
522 seterror(ERR_SPSTAR);
523 goto end;
525 default:
526 if (Isdigit(c)) {
527 #ifdef notdef
528 /* let $?0 pass for now */
529 if (special) {
530 seterror(ERR_DIGIT);
531 goto end;
533 #endif
534 while ((c = getC(DOEXCL)) != 0) {
535 if (!Isdigit(c))
536 break;
537 Strbuf_append1(&name, c);
540 else if (letter(c)) {
541 while ((c = getC(DOEXCL)) != 0) {
542 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
543 if (!letter(c) && !Isdigit(c))
544 break;
545 Strbuf_append1(&name, c);
548 else {
549 if (!special)
550 seterror(ERR_VARILL);
551 else {
552 ungetD(c);
553 name.len--;
555 goto end;
557 break;
559 if (c == '[') {
560 Strbuf_append1(&name, c);
561 do {
563 * Michael Greim: Allow $ expansion to take place in selector
564 * expressions. (limits the number of characters returned)
566 c = getC(DOEXCL | DODOL);
567 if (c == '\n') {
568 ungetD(c);
569 name.len--;
570 seterror(ERR_NLINDEX);
571 goto end;
573 Strbuf_append1(&name, c);
574 } while (c != ']');
575 c = getC(DOEXCL);
577 if (c == ':') {
579 * if the :g modifier is followed by a newline, then error right away!
580 * -strike
583 int gmodflag = 0, amodflag = 0;
585 do {
586 Strbuf_append1(&name, c), c = getC(DOEXCL);
587 if (c == 'g' || c == 'a') {
588 if (c == 'g')
589 gmodflag++;
590 else
591 amodflag++;
592 Strbuf_append1(&name, c); c = getC(DOEXCL);
594 if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
595 if (c == 'g')
596 gmodflag++;
597 else
598 amodflag++;
599 Strbuf_append1(&name, c); c = getC(DOEXCL);
601 Strbuf_append1(&name, c);
602 /* scan s// [eichin:19910926.0512EST] */
603 if (c == 's') {
604 int delimcnt = 2;
605 eChar delim = getC(0);
607 Strbuf_append1(&name, delim);
608 if (!delim || letter(delim)
609 || Isdigit(delim) || any(" \t\n", delim)) {
610 seterror(ERR_BADSUBST);
611 break;
613 while ((c = getC(0)) != CHAR_ERR) {
614 Strbuf_append1(&name, c);
615 if(c == delim) delimcnt--;
616 if(!delimcnt) break;
618 if(delimcnt) {
619 seterror(ERR_BADSUBST);
620 break;
622 c = 's';
624 if (!any("htrqxesul", c)) {
625 if ((amodflag || gmodflag) && c == '\n')
626 stderror(ERR_VARSYN); /* strike */
627 seterror(ERR_BADMOD, c);
628 goto end;
631 while ((c = getC(DOEXCL)) == ':');
632 ungetD(c);
634 else
635 ungetD(c);
636 if (sc == '{') {
637 c = getC(DOEXCL);
638 if (c != '}') {
639 ungetD(c);
640 seterror(ERR_MISSING, '}');
641 goto end;
643 Strbuf_append1(&name, c);
645 end:
646 cleanup_ignore(&name);
647 cleanup_until(&name);
648 addla(Strbuf_finish(&name));
651 /* xfree()'s its argument */
652 void
653 addla(Char *cp)
655 static struct Strbuf buf; /* = Strbuf_INIT; */
657 buf.len = 0;
658 Strbuf_appendn(&buf, labuf.s + lap, labuf.len - lap);
659 labuf.len = 0;
660 Strbuf_append(&labuf, cp);
661 Strbuf_terminate(&labuf);
662 Strbuf_appendn(&labuf, buf.s, buf.len);
663 xfree(cp);
664 lap = 0;
667 /* left-hand side of last :s or search string of last ?event? */
668 static struct Strbuf lhsb; /* = Strbuf_INIT; */
669 static struct Strbuf slhs; /* = Strbuf_INIT; left-hand side of last :s */
670 static struct Strbuf rhsb; /* = Strbuf_INIT; right-hand side of last :s */
671 static int quesarg;
673 static void
674 getexcl(Char sc)
676 struct wordent *hp, *ip;
677 int left, right, dol;
678 eChar c;
680 if (sc == 0) {
681 c = getC(0);
682 if (c == '{')
683 sc = (Char) c;
684 else
685 ungetC(c);
687 quesarg = -1;
689 lastev = eventno;
690 hp = gethent(sc);
691 if (hp == 0)
692 return;
693 hadhist = 1;
694 dol = 0;
695 if (hp == alhistp)
696 for (ip = hp->next->next; ip != alhistt; ip = ip->next)
697 dol++;
698 else
699 for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
700 dol++;
701 left = 0, right = dol;
702 if (sc == HISTSUB && HISTSUB != '\0') {
703 ungetC('s'), unreadc(HISTSUB), c = ':';
704 goto subst;
706 c = getC(0);
707 if (!any(":^$*-%", c))
708 goto subst;
709 left = right = -1;
710 if (c == ':') {
711 c = getC(0);
712 unreadc(c);
713 if (letter(c) || c == '&') {
714 c = ':';
715 left = 0, right = dol;
716 goto subst;
719 else
720 ungetC(c);
721 if (!getsel(&left, &right, dol))
722 return;
723 c = getC(0);
724 if (c == '*')
725 ungetC(c), c = '-';
726 if (c == '-') {
727 if (!getsel(&left, &right, dol))
728 return;
729 c = getC(0);
731 subst:
732 exclc = right - left + 1;
733 while (--left >= 0)
734 hp = hp->next;
735 if ((sc == HISTSUB && HISTSUB != '\0') || c == ':') {
736 do {
737 hp = getsub(hp);
738 c = getC(0);
739 } while (c == ':');
741 unreadc(c);
742 if (sc == '{') {
743 c = getC(0);
744 if (c != '}')
745 seterror(ERR_BADBANG);
747 exclnxt = hp;
750 static struct wordent *
751 getsub(struct wordent *en)
753 eChar delim;
754 eChar c;
755 eChar sc;
756 int global;
758 do {
759 exclnxt = 0;
760 global = 0;
761 sc = c = getC(0);
762 while (c == 'g' || c == 'a') {
763 global |= (c == 'g') ? FLAG_G : FLAG_A;
764 sc = c = getC(0);
767 switch (c) {
768 case 'p':
769 justpr++;
770 return (en);
772 case 'x':
773 case 'q':
774 global |= FLAG_G;
775 /*FALLTHROUGH*/
777 case 'h':
778 case 'r':
779 case 't':
780 case 'e':
781 case 'u':
782 case 'l':
783 break;
785 case '&':
786 if (slhs.len == 0) {
787 seterror(ERR_NOSUBST);
788 return (en);
790 lhsb.len = 0;
791 Strbuf_append(&lhsb, slhs.s);
792 Strbuf_terminate(&lhsb);
793 break;
795 #ifdef notdef
796 case '~':
797 if (lhsb.len == 0)
798 goto badlhs;
799 break;
800 #endif
802 case 's':
803 delim = getC(0);
804 if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
805 unreadc(delim);
806 lhsb.len = 0;
807 seterror(ERR_BADSUBST);
808 return (en);
810 Strbuf_terminate(&lhsb);
811 lhsb.len = 0;
812 for (;;) {
813 c = getC(0);
814 if (c == '\n') {
815 unreadc(c);
816 break;
818 if (c == delim)
819 break;
820 if (c == '\\') {
821 c = getC(0);
822 if (c != delim && c != '\\')
823 Strbuf_append1(&lhsb, '\\');
825 Strbuf_append1(&lhsb, c);
827 if (lhsb.len != 0)
828 Strbuf_terminate(&lhsb);
829 else if (lhsb.s[0] == 0) {
830 seterror(ERR_LHS);
831 return (en);
832 } else
833 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
834 rhsb.len = 0;
835 for (;;) {
836 c = getC(0);
837 if (c == '\n') {
838 unreadc(c);
839 break;
841 if (c == delim)
842 break;
843 if (c == '\\') {
844 c = getC(0);
845 if (c != delim /* && c != '~' */ )
846 Strbuf_append1(&rhsb, '\\');
848 Strbuf_append1(&rhsb, c);
850 Strbuf_terminate(&rhsb);
851 break;
853 default:
854 if (c == '\n')
855 unreadc(c);
856 seterror(ERR_BADBANGMOD, (int)c);
857 return (en);
859 slhs.len = 0;
860 if (lhsb.s != NULL && lhsb.len != 0)
861 Strbuf_append(&slhs, lhsb.s);
862 Strbuf_terminate(&slhs);
863 if (exclc)
864 en = dosub(sc, en, global);
866 while ((c = getC(0)) == ':');
867 unreadc(c);
868 return (en);
873 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
875 * when using history substitution, and the variable
876 * 'history' is set to a value higher than 1000,
877 * the shell might either freeze (hang) or core-dump.
878 * We raise the limit to 50000000
881 #define HIST_PURGE -50000000
882 static struct wordent *
883 dosub(Char sc, struct wordent *en, int global)
885 struct wordent lexi;
886 int didsub = 0, didone = 0;
887 struct wordent *hp = &lexi;
888 struct wordent *wdp;
889 int i = exclc;
890 struct Hist *hst;
892 wdp = hp;
893 while (--i >= 0) {
894 struct wordent *new = xcalloc(1, sizeof *wdp);
896 new->word = 0;
897 new->prev = wdp;
898 new->next = hp;
899 wdp->next = new;
900 wdp = new;
901 en = en->next;
902 if (en->word) {
903 Char *tword, *otword;
905 if ((global & FLAG_G) || didsub == 0) {
906 size_t pos;
908 pos = 0;
909 tword = subword(en->word, sc, &didone, &pos);
910 if (didone)
911 didsub = 1;
912 if (global & FLAG_A) {
913 while (didone && tword != STRNULL) {
914 otword = tword;
915 tword = subword(otword, sc, &didone, &pos);
916 if (Strcmp(tword, otword) == 0) {
917 xfree(otword);
918 break;
920 else
921 xfree(otword);
925 else
926 tword = Strsave(en->word);
927 wdp->word = tword;
930 if (didsub == 0)
931 seterror(ERR_MODFAIL);
932 hp->prev = wdp;
934 * ANSI mode HP/UX compiler chokes on
935 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
937 hst = enthist(HIST_PURGE, &lexi, 0, 0, -1);
938 return &(hst->Hlex);
941 /* Return a newly allocated result of one modification of CP using the
942 operation TYPE. Set ADID to 1 if a modification was performed.
943 If TYPE == 's', perform substitutions only from *START_POS on and set
944 *START_POS to the position of next substitution attempt. */
945 static Char *
946 subword(Char *cp, Char type, int *adid, size_t *start_pos)
948 Char *wp;
949 const Char *mp, *np;
951 switch (type) {
953 case 'r':
954 case 'e':
955 case 'h':
956 case 't':
957 case 'q':
958 case 'x':
959 case 'u':
960 case 'l':
961 wp = domod(cp, type);
962 if (wp == 0) {
963 *adid = 0;
964 return (Strsave(cp));
966 *adid = 1;
967 return (wp);
969 default:
970 for (mp = cp + *start_pos; *mp; mp++) {
971 if (matchs(mp, lhsb.s)) {
972 struct Strbuf wbuf = Strbuf_INIT;
974 Strbuf_appendn(&wbuf, cp, mp - cp);
975 for (np = rhsb.s; *np; np++)
976 switch (*np) {
978 case '\\':
979 if (np[1] == '&')
980 np++;
981 /* fall into ... */
983 default:
984 Strbuf_append1(&wbuf, *np);
985 continue;
987 case '&':
988 Strbuf_append(&wbuf, lhsb.s);
989 continue;
991 *start_pos = wbuf.len;
992 Strbuf_append(&wbuf, mp + lhsb.len);
993 *adid = 1;
994 return Strbuf_finish(&wbuf);
997 *adid = 0;
998 return (Strsave(cp));
1002 Char *
1003 domod(Char *cp, Char type)
1005 Char *wp, *xp;
1006 int c;
1008 switch (type) {
1010 case 'x':
1011 case 'q':
1012 wp = Strsave(cp);
1013 for (xp = wp; (c = *xp) != 0; xp++)
1014 if ((c != ' ' && c != '\t') || type == 'q')
1015 *xp |= QUOTE;
1016 return (wp);
1018 case 'l':
1019 wp = NLSChangeCase(cp, 1);
1020 return wp ? wp : Strsave(cp);
1022 case 'u':
1023 wp = NLSChangeCase(cp, 0);
1024 return wp ? wp : Strsave(cp);
1026 case 'h':
1027 case 't':
1028 if (!any(short2str(cp), '/'))
1029 return (type == 't' ? Strsave(cp) : 0);
1030 wp = Strrchr(cp, '/');
1031 if (type == 'h')
1032 xp = Strnsave(cp, wp - cp);
1033 else
1034 xp = Strsave(wp + 1);
1035 return (xp);
1037 case 'e':
1038 case 'r':
1039 wp = Strend(cp);
1040 for (wp--; wp >= cp && *wp != '/'; wp--)
1041 if (*wp == '.') {
1042 if (type == 'e')
1043 xp = Strsave(wp + 1);
1044 else
1045 xp = Strnsave(cp, wp - cp);
1046 return (xp);
1048 return (Strsave(type == 'e' ? STRNULL : cp));
1049 default:
1050 break;
1052 return (0);
1055 static int
1056 matchs(const Char *str, const Char *pat)
1058 while (*str && *pat && *str == *pat)
1059 str++, pat++;
1060 return (*pat == 0);
1063 static int
1064 getsel(int *al, int *ar, int dol)
1066 eChar c = getC(0);
1067 int i;
1068 int first = *al < 0;
1070 switch (c) {
1072 case '%':
1073 if (quesarg == -1) {
1074 seterror(ERR_BADBANGARG);
1075 return (0);
1077 if (*al < 0)
1078 *al = quesarg;
1079 *ar = quesarg;
1080 break;
1082 case '-':
1083 if (*al < 0) {
1084 *al = 0;
1085 *ar = dol - 1;
1086 unreadc(c);
1088 return (1);
1090 case '^':
1091 if (*al < 0)
1092 *al = 1;
1093 *ar = 1;
1094 break;
1096 case '$':
1097 if (*al < 0)
1098 *al = dol;
1099 *ar = dol;
1100 break;
1102 case '*':
1103 if (*al < 0)
1104 *al = 1;
1105 *ar = dol;
1106 if (*ar < *al) {
1107 *ar = 0;
1108 *al = 1;
1109 return (1);
1111 break;
1113 default:
1114 if (Isdigit(c)) {
1115 i = 0;
1116 while (Isdigit(c)) {
1117 i = i * 10 + c - '0';
1118 c = getC(0);
1120 if (i < 0)
1121 i = dol + 1;
1122 if (*al < 0)
1123 *al = i;
1124 *ar = i;
1126 else if (*al < 0)
1127 *al = 0, *ar = dol;
1128 else
1129 *ar = dol - 1;
1130 unreadc(c);
1131 break;
1133 if (first) {
1134 c = getC(0);
1135 unreadc(c);
1136 if (any("-$*", c))
1137 return (1);
1139 if (*al > *ar || *ar > dol) {
1140 seterror(ERR_BADBANGARG);
1141 return (0);
1143 return (1);
1147 static struct wordent *
1148 gethent(Char sc)
1150 struct Hist *hp;
1151 Char *np;
1152 eChar c;
1153 int event;
1154 int back = 0;
1156 c = (sc == HISTSUB && HISTSUB != '\0') ? (eChar)HIST : getC(0);
1157 if (c == (eChar)HIST) {
1158 if (alhistp)
1159 return (alhistp);
1160 event = eventno;
1162 else
1163 switch (c) {
1165 case ':':
1166 case '^':
1167 case '$':
1168 case '*':
1169 case '%':
1170 ungetC(c);
1171 if (lastev == eventno && alhistp)
1172 return (alhistp);
1173 event = lastev;
1174 break;
1176 case '#': /* !# is command being typed in (mrh) */
1177 if (--hleft == 0) {
1178 seterror(ERR_HISTLOOP);
1179 return (0);
1181 else
1182 return (&paraml);
1183 /* NOTREACHED */
1185 case '-':
1186 back = 1;
1187 c = getC(0);
1188 /* FALLSTHROUGH */
1190 default:
1191 if (any("(=~", c)) {
1192 unreadc(c);
1193 ungetC(HIST);
1194 return (0);
1196 Strbuf_terminate(&lhsb);
1197 lhsb.len = 0;
1198 event = 0;
1199 while (!cmap(c, _ESC | _META | _QF | _QB) && !any("^*-%${}:#", c)) {
1200 if (event != -1 && Isdigit(c))
1201 event = event * 10 + c - '0';
1202 else
1203 event = -1;
1204 Strbuf_append1(&lhsb, c);
1205 c = getC(0);
1207 unreadc(c);
1208 if (lhsb.len == 0) {
1209 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
1210 ungetC(HIST);
1211 return (0);
1213 Strbuf_terminate(&lhsb);
1214 if (event != -1) {
1216 * History had only digits
1218 if (back)
1219 event = eventno + (alhistp == 0) - event;
1220 break;
1222 if (back) {
1223 Strbuf_append1(&lhsb, '\0'); /* Allocate space */
1224 Strbuf_terminate(&lhsb);
1225 memmove(lhsb.s + 1, lhsb.s, (lhsb.len - 1) * sizeof (*lhsb.s));
1226 lhsb.s[0] = '-';
1228 hp = findev(lhsb.s, 0);
1229 if (hp)
1230 lastev = hp->Hnum;
1231 return (&hp->Hlex);
1233 case '?':
1234 Strbuf_terminate(&lhsb);
1235 lhsb.len = 0;
1236 for (;;) {
1237 c = getC(0);
1238 if (c == '\n') {
1239 unreadc(c);
1240 break;
1242 if (c == '?')
1243 break;
1244 Strbuf_append1(&lhsb, c);
1246 if (lhsb.len == 0) {
1247 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
1248 if (lhsb.len == 0) {
1249 seterror(ERR_NOSEARCH);
1250 return (0);
1253 else
1254 Strbuf_terminate(&lhsb);
1255 hp = findev(lhsb.s, 1);
1256 if (hp)
1257 lastev = hp->Hnum;
1258 return (&hp->Hlex);
1261 for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
1262 if (hp->Hnum == event) {
1263 hp->Href = eventno;
1264 lastev = hp->Hnum;
1265 return (&hp->Hlex);
1267 np = putn((tcsh_number_t)event);
1268 seterror(ERR_NOEVENT, short2str(np));
1269 xfree(np);
1270 return (0);
1273 static struct Hist *
1274 findev(Char *cp, int anyarg)
1276 struct Hist *hp;
1278 for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
1279 Char *dp;
1280 Char *p, *q;
1281 struct wordent *lp = hp->Hlex.next;
1282 int argno = 0;
1285 * The entries added by alias substitution don't have a newline but do
1286 * have a negative event number. Savehist() trims off these entries,
1287 * but it happens before alias expansion, too early to delete those
1288 * from the previous command.
1290 if (hp->Hnum < 0)
1291 continue;
1292 if (lp->word[0] == '\n')
1293 continue;
1294 if (!anyarg) {
1295 p = cp;
1296 q = lp->word;
1298 if (!*p)
1299 return (hp);
1300 while (*p++ == *q++);
1301 continue;
1303 do {
1304 for (dp = lp->word; *dp; dp++) {
1305 p = cp;
1306 q = dp;
1308 if (!*p) {
1309 quesarg = argno;
1310 return (hp);
1312 while (*p++ == *q++);
1314 lp = lp->next;
1315 argno++;
1316 } while (lp->word[0] != '\n');
1318 seterror(ERR_NOEVENT, short2str(cp));
1319 return (0);
1323 static void
1324 setexclp(Char *cp)
1326 if (cp && cp[0] == '\n')
1327 return;
1328 exclp = cp;
1331 void
1332 unreadc(Char c)
1334 peekread = (Char) c;
1337 eChar
1338 readc(int wanteof)
1340 eChar c;
1341 static int sincereal; /* Number of real EOFs we've seen */
1343 #ifdef DEBUG_INP
1344 xprintf("readc\n");
1345 #endif
1346 if ((c = peekread) != 0) {
1347 peekread = 0;
1348 return (c);
1351 top:
1352 aret = TCSH_F_SEEK;
1353 if (alvecp) {
1354 arun = 1;
1355 #ifdef DEBUG_INP
1356 xprintf("alvecp %c\n", *alvecp & 0xff);
1357 #endif
1358 aret = TCSH_A_SEEK;
1359 if ((c = *alvecp++) != 0)
1360 return (c);
1361 if (alvec && *alvec) {
1362 alvecp = *alvec++;
1363 return (' ');
1365 else {
1366 alvecp = NULL;
1367 aret = TCSH_F_SEEK;
1368 return('\n');
1371 if (alvec) {
1372 arun = 1;
1373 if ((alvecp = *alvec) != 0) {
1374 alvec++;
1375 goto top;
1377 /* Infinite source! */
1378 return ('\n');
1380 arun = 0;
1381 if (evalp) {
1382 aret = TCSH_E_SEEK;
1383 if ((c = *evalp++) != 0)
1384 return (c);
1385 if (evalvec && *evalvec) {
1386 evalp = *evalvec++;
1387 return (' ');
1389 aret = TCSH_F_SEEK;
1390 evalp = 0;
1392 if (evalvec) {
1393 if (evalvec == INVPPTR) {
1394 doneinp = 1;
1395 reset();
1397 if ((evalp = *evalvec) != 0) {
1398 evalvec++;
1399 goto top;
1401 evalvec = INVPPTR;
1402 return ('\n');
1404 do {
1405 if (arginp == INVPTR || onelflg == 1) {
1406 if (wanteof)
1407 return CHAR_ERR;
1408 exitstat();
1410 if (arginp) {
1411 if ((c = *arginp++) == 0) {
1412 arginp = INVPTR;
1413 return ('\n');
1415 return (c);
1417 #ifdef BSDJOBS
1418 reread:
1419 #endif /* BSDJOBS */
1420 c = bgetc();
1421 if (c == CHAR_ERR) {
1422 #ifndef WINNT_NATIVE
1423 # ifndef POSIX
1424 # ifdef TERMIO
1425 struct termio tty;
1426 # else /* SGTTYB */
1427 struct sgttyb tty;
1428 # endif /* TERMIO */
1429 # else /* POSIX */
1430 struct termios tty;
1431 # endif /* POSIX */
1432 #endif /* !WINNT_NATIVE */
1433 if (wanteof)
1434 return CHAR_ERR;
1435 /* was isatty but raw with ignoreeof yields problems */
1436 #ifndef WINNT_NATIVE
1437 # ifndef POSIX
1438 # ifdef TERMIO
1439 if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
1440 (tty.c_lflag & ICANON))
1441 # else /* GSTTYB */
1442 if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
1443 (tty.sg_flags & RAW) == 0)
1444 # endif /* TERMIO */
1445 # else /* POSIX */
1446 if (tcgetattr(SHIN, &tty) == 0 &&
1447 (tty.c_lflag & ICANON))
1448 # endif /* POSIX */
1449 #else /* WINNT_NATIVE */
1450 if (isatty(SHIN))
1451 #endif /* !WINNT_NATIVE */
1453 #ifdef BSDJOBS
1454 pid_t ctpgrp;
1455 #endif /* BSDJOBS */
1457 if (numeof != 0 && ++sincereal >= numeof) /* Too many EOFs? Bye! */
1458 goto oops;
1459 #ifdef BSDJOBS
1460 if (tpgrp != -1 &&
1461 (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
1462 tpgrp != ctpgrp) {
1463 (void) tcsetpgrp(FSHTTY, tpgrp);
1464 # ifdef _SEQUENT_
1465 if (ctpgrp)
1466 # endif /* _SEQUENT */
1467 (void) killpg(ctpgrp, SIGHUP);
1468 # ifdef notdef
1470 * With the walking process group fix, this message
1471 * is now obsolete. As the foreground process group
1472 * changes, the shell needs to adjust. Well too bad.
1474 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1475 (int)ctpgrp, (int)tpgrp);
1476 # endif /* notdef */
1477 goto reread;
1479 #endif /* BSDJOBS */
1480 /* What follows is complicated EOF handling -- sterling@netcom.com */
1481 /* First, we check to see if we have ignoreeof set */
1482 if (adrof(STRignoreeof)) {
1483 /* If so, we check for any stopped jobs only on the first EOF */
1484 if ((sincereal == 1) && (chkstop == 0)) {
1485 panystop(1);
1487 } else {
1488 /* If we don't have ignoreeof set, always check for stopped jobs */
1489 if (chkstop == 0) {
1490 panystop(1);
1493 /* At this point, if there were stopped jobs, we would have already
1494 * called reset(). If we got this far, assume we can print an
1495 * exit/logout message if we ignoreeof, or just exit.
1497 if (adrof(STRignoreeof)) {
1498 /* If so, tell the user to use exit or logout */
1499 if (loginsh) {
1500 xprintf("%s", CGETS(16, 2,
1501 "\nUse \"logout\" to logout.\n"));
1502 } else {
1503 xprintf(CGETS(16, 3,
1504 "\nUse \"exit\" to leave %s.\n"),
1505 progname);
1507 reset();
1508 } else {
1509 /* If we don't have ignoreeof set, just fall through */
1510 ; /* EMPTY */
1513 oops:
1514 doneinp = 1;
1515 reset();
1517 sincereal = 0;
1518 if (c == '\n' && onelflg)
1519 onelflg--;
1520 } while (c == 0);
1521 Strbuf_append1(&histline, c);
1522 return (c);
1525 static void
1526 balloc(int buf)
1528 Char **nfbuf;
1530 while (buf >= fblocks) {
1531 nfbuf = xcalloc(fblocks + 2, sizeof(Char **));
1532 if (fbuf) {
1533 (void) blkcpy(nfbuf, fbuf);
1534 xfree(fbuf);
1536 fbuf = nfbuf;
1537 fbuf[fblocks] = xcalloc(BUFSIZE, sizeof(Char));
1538 fblocks++;
1542 static ssize_t
1543 wide_read(int fildes, Char *buf, size_t nchars, int use_fclens)
1545 char cbuf[BUFSIZE + 1];
1546 ssize_t res, r = 0;
1547 size_t partial;
1548 int err;
1550 if (nchars == 0)
1551 return 0;
1552 assert (nchars <= sizeof(cbuf) / sizeof(*cbuf));
1553 USE(use_fclens);
1554 res = 0;
1555 partial = 0;
1556 do {
1557 size_t i;
1558 size_t len = nchars > partial ? nchars - partial : 1;
1560 if (partial + len >= sizeof(cbuf) / sizeof(*cbuf))
1561 break;
1563 r = xread(fildes, cbuf + partial, len);
1565 if (partial == 0 && r <= 0)
1566 break;
1567 partial += r;
1568 i = 0;
1569 while (i < partial && nchars != 0) {
1570 int tlen;
1572 tlen = normal_mbtowc(buf + res, cbuf + i, partial - i);
1573 if (tlen == -1) {
1574 reset_mbtowc();
1575 if ((partial - i) < MB_LEN_MAX && r > 0)
1576 /* Maybe a partial character and there is still a chance
1577 to read more */
1578 break;
1579 buf[res] = (unsigned char)cbuf[i] | INVALID_BYTE;
1581 if (tlen <= 0)
1582 tlen = 1;
1583 #ifdef WIDE_STRINGS
1584 if (use_fclens)
1585 fclens[res] = tlen;
1586 #endif
1587 i += tlen;
1588 res++;
1589 nchars--;
1591 if (i != partial)
1592 memmove(cbuf, cbuf + i, partial - i);
1593 partial -= i;
1594 } while (partial != 0 && nchars > 0);
1595 /* Throwing away possible partial multibyte characters on error if the
1596 stream is not seekable */
1597 err = errno;
1598 lseek(fildes, -(off_t)partial, L_INCR);
1599 errno = err;
1600 return res != 0 ? res : r;
1603 static eChar
1604 bgetc(void)
1606 Char ch;
1607 int c, off, buf;
1608 int numleft = 0, roomleft;
1610 if (cantell) {
1611 if (fseekp < fbobp || fseekp > feobp) {
1612 fbobp = feobp = fseekp;
1613 (void) lseek(SHIN, fseekp, L_SET);
1615 if (fseekp == feobp) {
1616 #ifdef WIDE_STRINGS
1617 off_t bytes;
1618 size_t i;
1620 bytes = fbobp;
1621 for (i = 0; i < (size_t)(feobp - fbobp); i++)
1622 bytes += fclens[i];
1623 fseekp = feobp = bytes;
1624 #endif
1625 fbobp = feobp;
1626 c = wide_read(SHIN, fbuf[0], BUFSIZE, 1);
1627 #ifdef convex
1628 if (c < 0)
1629 stderror(ERR_SYSTEM, progname, strerror(errno));
1630 #endif /* convex */
1631 if (c <= 0)
1632 return CHAR_ERR;
1633 feobp += c;
1635 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1636 ch = fbuf[0][fseekp - fbobp];
1637 fseekp++;
1638 #else
1639 do {
1640 ch = fbuf[0][fseekp - fbobp];
1641 fseekp++;
1642 } while(ch == '\r');
1643 #endif /* !WINNT_NATIVE && !__CYGWIN__ */
1644 return (ch);
1647 while (fseekp >= feobp) {
1648 if ((editing
1649 #if defined(FILEC) && defined(TIOCSTI)
1650 || filec
1651 #endif /* FILEC && TIOCSTI */
1652 ) && intty) { /* then use twenex routine */
1653 fseekp = feobp; /* where else? */
1654 #if defined(FILEC) && defined(TIOCSTI)
1655 if (!editing)
1656 c = numleft = tenex(InputBuf, BUFSIZE);
1657 else
1658 #endif /* FILEC && TIOCSTI */
1659 c = numleft = Inputl(); /* PWP: get a line */
1660 while (numleft > 0) {
1661 off = (int) feobp % BUFSIZE;
1662 buf = (int) feobp / BUFSIZE;
1663 balloc(buf);
1664 roomleft = BUFSIZE - off;
1665 if (roomleft > numleft)
1666 roomleft = numleft;
1667 (void) memcpy(fbuf[buf] + off, InputBuf + c - numleft,
1668 roomleft * sizeof(Char));
1669 numleft -= roomleft;
1670 feobp += roomleft;
1672 } else {
1673 off = (int) feobp % BUFSIZE;
1674 buf = (int) feobp / BUFSIZE;
1675 balloc(buf);
1676 roomleft = BUFSIZE - off;
1677 c = wide_read(SHIN, fbuf[buf] + off, roomleft, 0);
1678 if (c > 0)
1679 feobp += c;
1681 if (c == 0 || (c < 0 && fixio(SHIN, errno) == -1))
1682 return CHAR_ERR;
1684 #ifdef SIG_WINDOW
1685 if (windowchg)
1686 (void) check_window_size(0); /* for window systems */
1687 #endif /* SIG_WINDOW */
1688 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1689 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1690 fseekp++;
1691 #else
1692 do {
1693 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1694 fseekp++;
1695 } while(ch == '\r');
1696 #endif /* !WINNT_NATIVE && !__CYGWIN__ */
1697 return (ch);
1700 static void
1701 bfree(void)
1703 int sb, i;
1705 if (cantell)
1706 return;
1707 if (whyles)
1708 return;
1709 sb = (int) (fseekp - 1) / BUFSIZE;
1710 if (sb > 0) {
1711 for (i = 0; i < sb; i++)
1712 xfree(fbuf[i]);
1713 (void) blkcpy(fbuf, &fbuf[sb]);
1714 fseekp -= BUFSIZE * sb;
1715 feobp -= BUFSIZE * sb;
1716 fblocks -= sb;
1720 void
1721 bseek(struct Ain *l)
1723 switch (aret = l->type) {
1724 case TCSH_E_SEEK:
1725 evalvec = l->a_seek;
1726 evalp = l->c_seek;
1727 #ifdef DEBUG_SEEK
1728 xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec, evalp);
1729 #endif
1730 return;
1731 case TCSH_A_SEEK:
1732 alvec = l->a_seek;
1733 alvecp = l->c_seek;
1734 #ifdef DEBUG_SEEK
1735 xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec, alvecp);
1736 #endif
1737 return;
1738 case TCSH_F_SEEK:
1739 #ifdef DEBUG_SEEK
1740 xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp);
1741 #endif
1742 fseekp = l->f_seek;
1743 #ifdef WIDE_STRINGS
1744 if (cantell) {
1745 if (fseekp >= fbobp && feobp >= fbobp) {
1746 size_t i;
1747 off_t o;
1749 o = fbobp;
1750 for (i = 0; i < (size_t)(feobp - fbobp); i++) {
1751 if (fseekp == o) {
1752 fseekp = fbobp + i;
1753 return;
1755 o += fclens[i];
1757 if (fseekp == o) {
1758 fseekp = feobp;
1759 return;
1762 fbobp = feobp = fseekp + 1; /* To force lseek() */
1764 #endif
1765 return;
1766 default:
1767 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1768 abort();
1772 /* any similarity to bell telephone is purely accidental */
1773 void
1774 btell(struct Ain *l)
1776 switch (l->type = aret) {
1777 case TCSH_E_SEEK:
1778 l->a_seek = evalvec;
1779 l->c_seek = evalp;
1780 #ifdef DEBUG_SEEK
1781 xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec, evalp);
1782 #endif
1783 return;
1784 case TCSH_A_SEEK:
1785 l->a_seek = alvec;
1786 l->c_seek = alvecp;
1787 #ifdef DEBUG_SEEK
1788 xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec, alvecp);
1789 #endif
1790 return;
1791 case TCSH_F_SEEK:
1792 #ifdef WIDE_STRINGS
1793 if (cantell && fseekp >= fbobp && fseekp <= feobp) {
1794 size_t i;
1796 l->f_seek = fbobp;
1797 for (i = 0; i < (size_t)(fseekp - fbobp); i++)
1798 l->f_seek += fclens[i];
1799 } else
1800 #endif
1801 /*SUPPRESS 112*/
1802 l->f_seek = fseekp;
1803 l->a_seek = NULL;
1804 #ifdef DEBUG_SEEK
1805 xprintf(CGETS(16, 10, "tell file %x\n"), fseekp);
1806 #endif
1807 return;
1808 default:
1809 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1810 abort();
1814 void
1815 btoeof(void)
1817 (void) lseek(SHIN, (off_t) 0, L_XTND);
1818 aret = TCSH_F_SEEK;
1819 fseekp = feobp;
1820 alvec = NULL;
1821 alvecp = NULL;
1822 evalvec = NULL;
1823 evalp = NULL;
1824 wfree();
1825 bfree();
1828 void
1829 settell(void)
1831 off_t x;
1832 cantell = 0;
1833 if (arginp || onelflg || intty)
1834 return;
1835 if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1)
1836 return;
1837 fbuf = xcalloc(2, sizeof(Char **));
1838 fblocks = 1;
1839 fbuf[0] = xcalloc(BUFSIZE, sizeof(Char));
1840 fseekp = fbobp = feobp = x;
1841 cantell = 1;