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. 8 * 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. 20 * 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. 32 */ 33 #include "sh.h" 34 35 RCSID("$tcsh: sh.lex.c,v 3.87 2011/01/24 17:48:15 christos Exp $") 36 37 #include "ed.h" 38 39 #include <assert.h> 40 /* #define DEBUG_INP */ 41 /* #define DEBUG_SEEK */ 42 43 /* 44 * C shell 45 */ 46 47 #define FLAG_G 1 48 #define FLAG_A 2 49 /* 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. 53 */ 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); 70 71 /* 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. 84 * 85 * Getdol invokes history substitution, hence the extra peek, peekd, 86 * which it can ungetD to be before history substitutions. 87 */ 88 static Char peekc = 0, peekd = 0; 89 static Char peekread = 0; 90 91 /* (Tail of) current word from ! subst */ 92 static Char *exclp = NULL; 93 94 /* The rest of the ! subst words */ 95 static struct wordent *exclnxt = NULL; 96 97 /* Count of remaining words in ! subst */ 98 static int exclc = 0; 99 100 /* "Globp" for alias resubstitution */ 101 int aret = TCSH_F_SEEK; 102 103 /* 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. 109 */ 110 struct Strbuf labuf; /* = Strbuf_INIT; */ 111 112 /* 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. 118 */ 119 static int hadhist = 0; 120 121 /* 122 * Avoid alias expansion recursion via \!# 123 */ 124 int hleft; 125 126 struct Strbuf histline; /* = Strbuf_INIT; last line input */ 127 128 int histvalid = 0; /* is histline valid */ 129 130 static Char getCtmp; 131 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 135 136 /* Use Htime to store timestamps picked up from history file for enthist() 137 * if reading saved history (sg) 138 */ 139 time_t Htime = (time_t)0; 140 static time_t a2time_t (Char *); 141 142 /* 143 * special parsing rules apply for source -h 144 */ 145 extern int enterhist; 146 147 int 148 lex(struct wordent *hp) 149 { 150 struct wordent *wdp; 151 eChar c; 152 int parsehtime = enterhist; 153 154 histvalid = 0; 155 histline.len = 0; 156 157 btell(&lineloc); 158 hp->next = hp->prev = hp; 159 hp->word = STRNULL; 160 hadhist = 0; 161 do 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; 171 /* 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. 174 */ 175 do { 176 struct wordent *new; 177 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; 194 195 return (hadhist); 196 } 197 198 static time_t 199 a2time_t(Char *wordx) 200 { 201 /* Attempt to distinguish timestamps from other possible entries. 202 * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */ 203 204 time_t ret; 205 Char *s; 206 int ct; 207 208 if (!wordx || *(s = wordx) != '+') 209 return (time_t)0; 210 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'); 215 } 216 217 if (ct != 10) 218 return (time_t)0; 219 220 return ret; 221 } 222 223 void 224 prlex(struct wordent *sp0) 225 { 226 struct wordent *sp = sp0->next; 227 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(' '); 235 } 236 } 237 238 void 239 copylex(struct wordent *hp, struct wordent *fp) 240 { 241 struct wordent *wdp; 242 243 wdp = hp; 244 fp = fp->next; 245 do { 246 struct wordent *new; 247 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'); 258 } 259 260 void 261 freelex(struct wordent *vp) 262 { 263 struct wordent *fp; 264 265 while (vp->next != vp) { 266 fp = vp->next; 267 vp->next = fp->next; 268 xfree(fp->word); 269 xfree(fp); 270 } 271 vp->prev = vp; 272 } 273 274 void 275 lex_cleanup(void *xvp) 276 { 277 struct wordent *vp; 278 279 vp = xvp; 280 freelex(vp); 281 } 282 283 static Char * 284 word(int parsehtime) 285 { 286 eChar c, c1; 287 struct Strbuf wbuf = Strbuf_INIT; 288 Char hbuf[12]; 289 int h; 290 int dolflg; 291 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; 309 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); 324 } 325 if (c1 == '\\') 326 goto loop; 327 /*FALLTHROUGH*/ 328 329 case ';': 330 case '(': 331 case ')': 332 case '\n': 333 Strbuf_append1(&wbuf, c); 334 goto ret; 335 336 case '\\': 337 c = getC(0); 338 if (c == '\n') { 339 if (onelflg == 1) 340 onelflg = 2; 341 goto loop; 342 } 343 if (c != (eChar)HIST) 344 Strbuf_append1(&wbuf, '\\'); 345 c |= QUOTE; 346 default: 347 break; 348 } 349 c1 = 0; 350 dolflg = DOALL; 351 for (;;) { 352 if (c1) { 353 if (c == c1) { 354 c1 = 0; 355 dolflg = DOALL; 356 } 357 else if (c == '\\') { 358 c = getC(0); 359 /* 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. 363 * 364 * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar 365 */ 366 if (c == (eChar)HIST) 367 c |= QUOTE; 368 else { 369 if (bslash_quote && 370 ((c == '\'') || (c == '"') || 371 (c == '\\') || (c == '$'))) { 372 c |= QUOTE; 373 } 374 else { 375 if (c == '\n') 376 /* 377 * if (c1 == '`') c = ' '; else 378 */ 379 c |= QUOTE; 380 ungetC(c); 381 c = '\\'; 382 } 383 } 384 } 385 else if (c == '\n') { 386 seterror(ERR_UNMATCHED, c1); 387 ungetC(c); 388 break; 389 } 390 } 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; 398 } 399 if (c != (eChar)HIST) 400 Strbuf_append1(&wbuf, '\\'); 401 c |= QUOTE; 402 } 403 else if (cmap(c, _QF | _QB)) { /* '"` */ 404 c1 = c; 405 dolflg = c == '"' ? DOALL : DOEXCL; 406 } 407 else if (c != '#' || (!intty && !enterhist)) { 408 ungetC(c); 409 break; 410 } 411 } 412 Strbuf_append1(&wbuf, c); 413 c = getC(dolflg); 414 } 415 ret: 416 cleanup_ignore(&wbuf); 417 cleanup_until(&wbuf); 418 return Strbuf_finish(&wbuf); 419 } 420 421 static eChar 422 getC1(int flag) 423 { 424 eChar c; 425 426 for (;;) { 427 if ((c = peekc) != 0) { 428 peekc = 0; 429 return (c); 430 } 431 if (lap < labuf.len) { 432 c = labuf.s[lap++]; 433 if (cmap(c, _META | _QF | _QB)) 434 c |= QUOTE; 435 return (c); 436 } 437 if ((c = peekd) != 0) { 438 peekd = 0; 439 return (c); 440 } 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 (' '); 448 } 449 exclp = 0; 450 exclnxt = 0; 451 /* this will throw away the dummy history entries */ 452 savehist(NULL, 0); 453 454 } 455 if (exclnxt) { 456 exclnxt = exclnxt->next; 457 if (--exclc < 0) 458 exclnxt = 0; 459 else 460 setexclp(exclnxt->word); 461 continue; 462 } 463 c = readc(1); 464 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'; 469 470 if (c == '$' && (flag & DODOL)) { 471 getdol(); 472 continue; 473 } 474 if (c == (eChar)HIST && (flag & DOEXCL)) { 475 getexcl(0); 476 continue; 477 } 478 break; 479 } 480 return (c); 481 } 482 483 static void 484 getdol(void) 485 { 486 struct Strbuf name = Strbuf_INIT; 487 eChar c; 488 eChar sc; 489 int special = 0; 490 491 c = sc = getC(DOEXCL); 492 if (any("\t \n", c)) { 493 ungetD(c); 494 ungetC('$' | QUOTE); 495 return; 496 } 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) { 505 506 case '<': 507 case '$': 508 case '!': 509 if (special) 510 seterror(ERR_SPDOLLT); 511 goto end; 512 513 case '\n': 514 ungetD(c); 515 name.len--; 516 if (!special) 517 seterror(ERR_NEWLINE); 518 goto end; 519 520 case '*': 521 if (special) 522 seterror(ERR_SPSTAR); 523 goto end; 524 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; 532 } 533 #endif 534 while ((c = getC(DOEXCL)) != 0) { 535 if (!Isdigit(c)) 536 break; 537 Strbuf_append1(&name, c); 538 } 539 } 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); 546 } 547 } 548 else { 549 if (!special) 550 seterror(ERR_VARILL); 551 else { 552 ungetD(c); 553 name.len--; 554 } 555 goto end; 556 } 557 break; 558 } 559 if (c == '[') { 560 Strbuf_append1(&name, c); 561 do { 562 /* 563 * Michael Greim: Allow $ expansion to take place in selector 564 * expressions. (limits the number of characters returned) 565 */ 566 c = getC(DOEXCL | DODOL); 567 if (c == '\n') { 568 ungetD(c); 569 name.len--; 570 seterror(ERR_NLINDEX); 571 goto end; 572 } 573 Strbuf_append1(&name, c); 574 } while (c != ']'); 575 c = getC(DOEXCL); 576 } 577 if (c == ':') { 578 /* 579 * if the :g modifier is followed by a newline, then error right away! 580 * -strike 581 */ 582 583 int gmodflag = 0, amodflag = 0; 584 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); 593 } 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); 600 } 601 Strbuf_append1(&name, c); 602 /* scan s// [eichin:19910926.0512EST] */ 603 if (c == 's') { 604 int delimcnt = 2; 605 eChar delim = getC(0); 606 607 Strbuf_append1(&name, delim); 608 if (!delim || letter(delim) 609 || Isdigit(delim) || any(" \t\n", delim)) { 610 seterror(ERR_BADSUBST); 611 break; 612 } 613 while ((c = getC(0)) != CHAR_ERR) { 614 Strbuf_append1(&name, c); 615 if(c == delim) delimcnt--; 616 if(!delimcnt) break; 617 } 618 if(delimcnt) { 619 seterror(ERR_BADSUBST); 620 break; 621 } 622 c = 's'; 623 } 624 if (!any("htrqxesul", c)) { 625 if ((amodflag || gmodflag) && c == '\n') 626 stderror(ERR_VARSYN); /* strike */ 627 seterror(ERR_BADMOD, c); 628 goto end; 629 } 630 } 631 while ((c = getC(DOEXCL)) == ':'); 632 ungetD(c); 633 } 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; 642 } 643 Strbuf_append1(&name, c); 644 } 645 end: 646 cleanup_ignore(&name); 647 cleanup_until(&name); 648 addla(Strbuf_finish(&name)); 649 } 650 651 /* xfree()'s its argument */ 652 void 653 addla(Char *cp) 654 { 655 static struct Strbuf buf; /* = Strbuf_INIT; */ 656 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; 665 } 666 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; 672 673 static void 674 getexcl(Char sc) 675 { 676 struct wordent *hp, *ip; 677 int left, right, dol; 678 eChar c; 679 680 if (sc == 0) { 681 c = getC(0); 682 if (c == '{') 683 sc = (Char) c; 684 else 685 ungetC(c); 686 } 687 quesarg = -1; 688 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; 705 } 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; 717 } 718 } 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); 730 } 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 == ':'); 740 } 741 unreadc(c); 742 if (sc == '{') { 743 c = getC(0); 744 if (c != '}') 745 seterror(ERR_BADBANG); 746 } 747 exclnxt = hp; 748 } 749 750 static struct wordent * 751 getsub(struct wordent *en) 752 { 753 eChar delim; 754 eChar c; 755 eChar sc; 756 int global; 757 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); 765 } 766 767 switch (c) { 768 case 'p': 769 justpr++; 770 return (en); 771 772 case 'x': 773 case 'q': 774 global |= FLAG_G; 775 /*FALLTHROUGH*/ 776 777 case 'h': 778 case 'r': 779 case 't': 780 case 'e': 781 case 'u': 782 case 'l': 783 break; 784 785 case '&': 786 if (slhs.len == 0) { 787 seterror(ERR_NOSUBST); 788 return (en); 789 } 790 lhsb.len = 0; 791 Strbuf_append(&lhsb, slhs.s); 792 Strbuf_terminate(&lhsb); 793 break; 794 795 #ifdef notdef 796 case '~': 797 if (lhsb.len == 0) 798 goto badlhs; 799 break; 800 #endif 801 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); 809 } 810 Strbuf_terminate(&lhsb); 811 lhsb.len = 0; 812 for (;;) { 813 c = getC(0); 814 if (c == '\n') { 815 unreadc(c); 816 break; 817 } 818 if (c == delim) 819 break; 820 if (c == '\\') { 821 c = getC(0); 822 if (c != delim && c != '\\') 823 Strbuf_append1(&lhsb, '\\'); 824 } 825 Strbuf_append1(&lhsb, c); 826 } 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; 840 } 841 if (c == delim) 842 break; 843 if (c == '\\') { 844 c = getC(0); 845 if (c != delim /* && c != '~' */ ) 846 Strbuf_append1(&rhsb, '\\'); 847 } 848 Strbuf_append1(&rhsb, c); 849 } 850 Strbuf_terminate(&rhsb); 851 break; 852 853 default: 854 if (c == '\n') 855 unreadc(c); 856 seterror(ERR_BADBANGMOD, (int)c); 857 return (en); 858 } 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); 865 } 866 while ((c = getC(0)) == ':'); 867 unreadc(c); 868 return (en); 869 } 870 871 /* 872 * 873 * From Beto Appleton (beto@aixwiz.austin.ibm.com) 874 * 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 879 */ 880 881 #define HIST_PURGE -50000000 882 static struct wordent * 883 dosub(Char sc, struct wordent *en, int global) 884 { 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; 891 892 wdp = hp; 893 while (--i >= 0) { 894 struct wordent *new = xcalloc(1, sizeof *wdp); 895 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; 904 905 if ((global & FLAG_G) || didsub == 0) { 906 size_t pos; 907 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; 919 } 920 else 921 xfree(otword); 922 } 923 } 924 } 925 else 926 tword = Strsave(en->word); 927 wdp->word = tword; 928 } 929 } 930 if (didsub == 0) 931 seterror(ERR_MODFAIL); 932 hp->prev = wdp; 933 /* 934 * ANSI mode HP/UX compiler chokes on 935 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex; 936 */ 937 hst = enthist(HIST_PURGE, &lexi, 0, 0, -1); 938 return &(hst->Hlex); 939 } 940 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) 947 { 948 Char *wp; 949 const Char *mp, *np; 950 951 switch (type) { 952 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)); 965 } 966 *adid = 1; 967 return (wp); 968 969 default: 970 for (mp = cp + *start_pos; *mp; mp++) { 971 if (matchs(mp, lhsb.s)) { 972 struct Strbuf wbuf = Strbuf_INIT; 973 974 Strbuf_appendn(&wbuf, cp, mp - cp); 975 for (np = rhsb.s; *np; np++) 976 switch (*np) { 977 978 case '\\': 979 if (np[1] == '&') 980 np++; 981 /* fall into ... */ 982 983 default: 984 Strbuf_append1(&wbuf, *np); 985 continue; 986 987 case '&': 988 Strbuf_append(&wbuf, lhsb.s); 989 continue; 990 } 991 *start_pos = wbuf.len; 992 Strbuf_append(&wbuf, mp + lhsb.len); 993 *adid = 1; 994 return Strbuf_finish(&wbuf); 995 } 996 } 997 *adid = 0; 998 return (Strsave(cp)); 999 } 1000 } 1001 1002 Char * 1003 domod(Char *cp, Char type) 1004 { 1005 Char *wp, *xp; 1006 int c; 1007 1008 switch (type) { 1009 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); 1017 1018 case 'l': 1019 wp = NLSChangeCase(cp, 1); 1020 return wp ? wp : Strsave(cp); 1021 1022 case 'u': 1023 wp = NLSChangeCase(cp, 0); 1024 return wp ? wp : Strsave(cp); 1025 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); 1036 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); 1047 } 1048 return (Strsave(type == 'e' ? STRNULL : cp)); 1049 default: 1050 break; 1051 } 1052 return (0); 1053 } 1054 1055 static int 1056 matchs(const Char *str, const Char *pat) 1057 { 1058 while (*str && *pat && *str == *pat) 1059 str++, pat++; 1060 return (*pat == 0); 1061 } 1062 1063 static int 1064 getsel(int *al, int *ar, int dol) 1065 { 1066 eChar c = getC(0); 1067 int i; 1068 int first = *al < 0; 1069 1070 switch (c) { 1071 1072 case '%': 1073 if (quesarg == -1) { 1074 seterror(ERR_BADBANGARG); 1075 return (0); 1076 } 1077 if (*al < 0) 1078 *al = quesarg; 1079 *ar = quesarg; 1080 break; 1081 1082 case '-': 1083 if (*al < 0) { 1084 *al = 0; 1085 *ar = dol - 1; 1086 unreadc(c); 1087 } 1088 return (1); 1089 1090 case '^': 1091 if (*al < 0) 1092 *al = 1; 1093 *ar = 1; 1094 break; 1095 1096 case '$': 1097 if (*al < 0) 1098 *al = dol; 1099 *ar = dol; 1100 break; 1101 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); 1110 } 1111 break; 1112 1113 default: 1114 if (Isdigit(c)) { 1115 i = 0; 1116 while (Isdigit(c)) { 1117 i = i * 10 + c - '0'; 1118 c = getC(0); 1119 } 1120 if (i < 0) 1121 i = dol + 1; 1122 if (*al < 0) 1123 *al = i; 1124 *ar = i; 1125 } 1126 else if (*al < 0) 1127 *al = 0, *ar = dol; 1128 else 1129 *ar = dol - 1; 1130 unreadc(c); 1131 break; 1132 } 1133 if (first) { 1134 c = getC(0); 1135 unreadc(c); 1136 if (any("-$*", c)) 1137 return (1); 1138 } 1139 if (*al > *ar || *ar > dol) { 1140 seterror(ERR_BADBANGARG); 1141 return (0); 1142 } 1143 return (1); 1144 1145 } 1146 1147 static struct wordent * 1148 gethent(Char sc) 1149 { 1150 struct Hist *hp; 1151 Char *np; 1152 eChar c; 1153 int event; 1154 int back = 0; 1155 1156 c = (sc == HISTSUB && HISTSUB != '\0') ? (eChar)HIST : getC(0); 1157 if (c == (eChar)HIST) { 1158 if (alhistp) 1159 return (alhistp); 1160 event = eventno; 1161 } 1162 else 1163 switch (c) { 1164 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; 1175 1176 case '#': /* !# is command being typed in (mrh) */ 1177 if (--hleft == 0) { 1178 seterror(ERR_HISTLOOP); 1179 return (0); 1180 } 1181 else 1182 return (¶ml); 1183 /* NOTREACHED */ 1184 1185 case '-': 1186 back = 1; 1187 c = getC(0); 1188 /* FALLSTHROUGH */ 1189 1190 default: 1191 if (any("(=~", c)) { 1192 unreadc(c); 1193 ungetC(HIST); 1194 return (0); 1195 } 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); 1206 } 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); 1212 } 1213 Strbuf_terminate(&lhsb); 1214 if (event != -1) { 1215 /* 1216 * History had only digits 1217 */ 1218 if (back) 1219 event = eventno + (alhistp == 0) - event; 1220 break; 1221 } 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] = '-'; 1227 } 1228 hp = findev(lhsb.s, 0); 1229 if (hp) 1230 lastev = hp->Hnum; 1231 return (&hp->Hlex); 1232 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; 1241 } 1242 if (c == '?') 1243 break; 1244 Strbuf_append1(&lhsb, c); 1245 } 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); 1251 } 1252 } 1253 else 1254 Strbuf_terminate(&lhsb); 1255 hp = findev(lhsb.s, 1); 1256 if (hp) 1257 lastev = hp->Hnum; 1258 return (&hp->Hlex); 1259 } 1260 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); 1266 } 1267 np = putn((tcsh_number_t)event); 1268 seterror(ERR_NOEVENT, short2str(np)); 1269 xfree(np); 1270 return (0); 1271 } 1272 1273 static struct Hist * 1274 findev(Char *cp, int anyarg) 1275 { 1276 struct Hist *hp; 1277 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; 1283 1284 /* 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. 1289 */ 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; 1297 do 1298 if (!*p) 1299 return (hp); 1300 while (*p++ == *q++); 1301 continue; 1302 } 1303 do { 1304 for (dp = lp->word; *dp; dp++) { 1305 p = cp; 1306 q = dp; 1307 do 1308 if (!*p) { 1309 quesarg = argno; 1310 return (hp); 1311 } 1312 while (*p++ == *q++); 1313 } 1314 lp = lp->next; 1315 argno++; 1316 } while (lp->word[0] != '\n'); 1317 } 1318 seterror(ERR_NOEVENT, short2str(cp)); 1319 return (0); 1320 } 1321 1322 1323 static void 1324 setexclp(Char *cp) 1325 { 1326 if (cp && cp[0] == '\n') 1327 return; 1328 exclp = cp; 1329 } 1330 1331 void 1332 unreadc(Char c) 1333 { 1334 peekread = (Char) c; 1335 } 1336 1337 eChar 1338 readc(int wanteof) 1339 { 1340 eChar c; 1341 static int sincereal; /* Number of real EOFs we've seen */ 1342 1343 #ifdef DEBUG_INP 1344 xprintf("readc\n"); 1345 #endif 1346 if ((c = peekread) != 0) { 1347 peekread = 0; 1348 return (c); 1349 } 1350 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 (' '); 1364 } 1365 else { 1366 alvecp = NULL; 1367 aret = TCSH_F_SEEK; 1368 return('\n'); 1369 } 1370 } 1371 if (alvec) { 1372 arun = 1; 1373 if ((alvecp = *alvec) != 0) { 1374 alvec++; 1375 goto top; 1376 } 1377 /* Infinite source! */ 1378 return ('\n'); 1379 } 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 (' '); 1388 } 1389 aret = TCSH_F_SEEK; 1390 evalp = 0; 1391 } 1392 if (evalvec) { 1393 if (evalvec == INVPPTR) { 1394 doneinp = 1; 1395 reset(); 1396 } 1397 if ((evalp = *evalvec) != 0) { 1398 evalvec++; 1399 goto top; 1400 } 1401 evalvec = INVPPTR; 1402 return ('\n'); 1403 } 1404 do { 1405 if (arginp == INVPTR || onelflg == 1) { 1406 if (wanteof) 1407 return CHAR_ERR; 1408 exitstat(); 1409 } 1410 if (arginp) { 1411 if ((c = *arginp++) == 0) { 1412 arginp = INVPTR; 1413 return ('\n'); 1414 } 1415 return (c); 1416 } 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 */ 1452 { 1453 #ifdef BSDJOBS 1454 pid_t ctpgrp; 1455 #endif /* BSDJOBS */ 1456 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 1469 /* 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. 1473 */ 1474 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"), 1475 (int)ctpgrp, (int)tpgrp); 1476 # endif /* notdef */ 1477 goto reread; 1478 } 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); 1486 } 1487 } else { 1488 /* If we don't have ignoreeof set, always check for stopped jobs */ 1489 if (chkstop == 0) { 1490 panystop(1); 1491 } 1492 } 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. 1496 */ 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); 1506 } 1507 reset(); 1508 } else { 1509 /* If we don't have ignoreeof set, just fall through */ 1510 ; /* EMPTY */ 1511 } 1512 } 1513 oops: 1514 doneinp = 1; 1515 reset(); 1516 } 1517 sincereal = 0; 1518 if (c == '\n' && onelflg) 1519 onelflg--; 1520 } while (c == 0); 1521 Strbuf_append1(&histline, c); 1522 return (c); 1523 } 1524 1525 static void 1526 balloc(int buf) 1527 { 1528 Char **nfbuf; 1529 1530 while (buf >= fblocks) { 1531 nfbuf = xcalloc(fblocks + 2, sizeof(Char **)); 1532 if (fbuf) { 1533 (void) blkcpy(nfbuf, fbuf); 1534 xfree(fbuf); 1535 } 1536 fbuf = nfbuf; 1537 fbuf[fblocks] = xcalloc(BUFSIZE, sizeof(Char)); 1538 fblocks++; 1539 } 1540 } 1541 1542 static ssize_t 1543 wide_read(int fildes, Char *buf, size_t nchars, int use_fclens) 1544 { 1545 char cbuf[BUFSIZE + 1]; 1546 ssize_t res, r = 0; 1547 size_t partial; 1548 int err; 1549 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; 1559 1560 if (partial + len >= sizeof(cbuf) / sizeof(*cbuf)) 1561 break; 1562 1563 r = xread(fildes, cbuf + partial, len); 1564 1565 if (partial == 0 && r <= 0) 1566 break; 1567 partial += r; 1568 i = 0; 1569 while (i < partial && nchars != 0) { 1570 int tlen; 1571 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; 1580 } 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--; 1590 } 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; 1601 } 1602 1603 static eChar 1604 bgetc(void) 1605 { 1606 Char ch; 1607 int c, off, buf; 1608 int numleft = 0, roomleft; 1609 1610 if (cantell) { 1611 if (fseekp < fbobp || fseekp > feobp) { 1612 fbobp = feobp = fseekp; 1613 (void) lseek(SHIN, fseekp, L_SET); 1614 } 1615 if (fseekp == feobp) { 1616 #ifdef WIDE_STRINGS 1617 off_t bytes; 1618 size_t i; 1619 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; 1634 } 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); 1645 } 1646 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; 1671 } 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; 1680 } 1681 if (c == 0 || (c < 0 && fixio(SHIN, errno) == -1)) 1682 return CHAR_ERR; 1683 } 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); 1698 } 1699 1700 static void 1701 bfree(void) 1702 { 1703 int sb, i; 1704 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; 1717 } 1718 } 1719 1720 void 1721 bseek(struct Ain *l) 1722 { 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; 1748 1749 o = fbobp; 1750 for (i = 0; i < (size_t)(feobp - fbobp); i++) { 1751 if (fseekp == o) { 1752 fseekp = fbobp + i; 1753 return; 1754 } 1755 o += fclens[i]; 1756 } 1757 if (fseekp == o) { 1758 fseekp = feobp; 1759 return; 1760 } 1761 } 1762 fbobp = feobp = fseekp + 1; /* To force lseek() */ 1763 } 1764 #endif 1765 return; 1766 default: 1767 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret); 1768 abort(); 1769 } 1770 } 1771 1772 /* any similarity to bell telephone is purely accidental */ 1773 void 1774 btell(struct Ain *l) 1775 { 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; 1795 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(); 1811 } 1812 } 1813 1814 void 1815 btoeof(void) 1816 { 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(); 1826 } 1827 1828 void 1829 settell(void) 1830 { 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; 1842 } 1843