1 /* 2 * sh.lex.c: Lexical analysis into tokens 3 */ 4 /*- 5 * Copyright (c) 1980, 1991 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 #include "sh.h" 33 #include "ed.h" 34 35 #include <assert.h> 36 /* #define DEBUG_INP */ 37 /* #define DEBUG_SEEK */ 38 39 /* 40 * C shell 41 */ 42 43 #define FLAG_G 1 44 #define FLAG_A 2 45 /* 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. 49 */ 50 static Char *word (int); 51 static eChar getC1 (int); 52 static void getdol (void); 53 static void getexcl (Char); 54 static struct Hist *findev (Char *, int); 55 static void setexclp (Char *); 56 static eChar bgetc (void); 57 static void balloc (int); 58 static void bfree (void); 59 static struct wordent *gethent (Char); 60 static int matchs (const Char *, const Char *); 61 static int getsel (int *, int *, int); 62 static struct wordent *getsub (struct wordent *); 63 static Char *subword (Char *, Char, int *, size_t *); 64 static struct wordent *dosub (Char, struct wordent *, int); 65 66 /* 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. 79 * 80 * Getdol invokes history substitution, hence the extra peek, peekd, 81 * which it can ungetD to be before history substitutions. 82 */ 83 static Char peekc = 0, peekd = 0; 84 static Char peekread = 0; 85 86 /* (Tail of) current word from ! subst */ 87 static Char *exclp = NULL; 88 89 /* The rest of the ! subst words */ 90 static struct wordent *exclnxt = NULL; 91 92 /* Count of remaining words in ! subst */ 93 static int exclc = 0; 94 95 /* "Globp" for alias resubstitution */ 96 int aret = TCSH_F_SEEK; 97 98 /* 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. 104 */ 105 struct Strbuf labuf; /* = Strbuf_INIT; */ 106 107 /* 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 112 * argument list. 113 */ 114 static int hadhist = 0; 115 116 /* 117 * Avoid alias expansion recursion via \!# 118 */ 119 int hleft; 120 121 struct Strbuf histline; /* = Strbuf_INIT; last line input */ 122 123 int histvalid = 0; /* is histline valid */ 124 125 static Char getCtmp; 126 127 #define getC(f) (((getCtmp = peekc) != '\0') ? (peekc = 0, (eChar)getCtmp) : getC1(f)) 128 #define ungetC(c) peekc = (Char) c 129 #define ungetD(c) peekd = (Char) c 130 131 /* Use Htime to store timestamps picked up from history file for enthist() 132 * if reading saved history (sg) 133 */ 134 time_t Htime = (time_t)0; 135 static time_t a2time_t (Char *); 136 137 /* 138 * special parsing rules apply for source -h 139 */ 140 extern int enterhist; 141 extern int postcmd_active; 142 143 int 144 lex(struct wordent *hp) 145 { 146 struct wordent *wdp; 147 eChar c; 148 int parsehtime = enterhist; 149 int toolong = 0; 150 151 histvalid = 0; 152 histline.len = 0; 153 154 if (!postcmd_active) 155 btell(&lineloc); 156 hp->next = hp->prev = hp; 157 hp->word = STRNULL; 158 hadhist = 0; 159 do 160 c = readc(0); 161 while (c == ' ' || c == '\t'); 162 if (c == (eChar)HISTSUB && intty) 163 /* ^lef^rit from tty is short !:s^lef^rit */ 164 getexcl(c); 165 else 166 unreadc(c); 167 cleanup_push(hp, lex_cleanup); 168 wdp = hp; 169 /* 170 * The following loop is written so that the links needed by freelex will 171 * be ready and rarin to go even if it is interrupted. 172 */ 173 do { 174 struct wordent *new; 175 176 new = xmalloc(sizeof(*new)); 177 new->word = NULL; 178 new->prev = wdp; 179 new->next = hp; 180 wdp->next = new; 181 hp->prev = new; 182 wdp = new; 183 wdp->word = word(parsehtime); 184 parsehtime = 0; 185 if (enterhist && toolong++ > 10 * 1024) 186 stderror(ERR_LTOOLONG); 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 initlex(struct wordent *vp) 262 { 263 vp->word = STRNULL; 264 vp->prev = vp; 265 vp->next = vp; 266 } 267 268 void 269 freelex(struct wordent *vp) 270 { 271 struct wordent *fp; 272 273 while (vp->next != vp) { 274 fp = vp->next; 275 vp->next = fp->next; 276 xfree(fp->word); 277 xfree(fp); 278 } 279 vp->prev = vp; 280 } 281 282 void 283 lex_cleanup(void *xvp) 284 { 285 struct wordent *vp; 286 287 vp = xvp; 288 freelex(vp); 289 } 290 291 static Char * 292 word(int parsehtime) 293 { 294 eChar c, c1; 295 struct Strbuf wbuf = Strbuf_INIT; 296 Char hbuf[12]; 297 int h; 298 int dolflg; 299 int toolong = 0; 300 301 cleanup_push(&wbuf, Strbuf_cleanup); 302 loop: 303 if (enterhist && toolong++ > 256 * 1024) 304 seterror(ERR_WTOOLONG); 305 while ((c = getC(DOALL)) == ' ' || c == '\t') 306 continue; 307 if (cmap(c, _META | _ESC)) 308 switch (c) { 309 case '&': 310 case '|': 311 case '<': 312 case '>': 313 Strbuf_append1(&wbuf, c); 314 c1 = getC(DOALL); 315 if (c1 == c) 316 Strbuf_append1(&wbuf, c1); 317 else 318 ungetC(c1); 319 goto ret; 320 321 case '#': 322 if (intty || (enterhist && !parsehtime)) 323 break; 324 c = 0; 325 h = 0; 326 do { 327 c1 = c; 328 c = getC(0); 329 if (h < 11 && parsehtime) 330 hbuf[h++] = c; 331 } while (c != '\n'); 332 if (parsehtime) { 333 hbuf[11] = '\0'; 334 Htime = a2time_t(hbuf); 335 } 336 if (c1 == '\\') 337 goto loop; 338 /*FALLTHROUGH*/ 339 340 case ';': 341 case '(': 342 case ')': 343 case '\n': 344 Strbuf_append1(&wbuf, c); 345 goto ret; 346 347 case '\\': 348 c = getC(0); 349 if (c == '\n') { 350 if (onelflg == 1) 351 onelflg = 2; 352 goto loop; 353 } 354 if (c != (eChar)HIST) 355 Strbuf_append1(&wbuf, '\\'); 356 c |= QUOTE; 357 default: 358 break; 359 } 360 c1 = 0; 361 dolflg = DOALL; 362 for (;;) { 363 if (enterhist && toolong++ > 256 * 1024) 364 seterror(ERR_WTOOLONG); 365 if (c1) { 366 if (c == c1) { 367 c1 = 0; 368 dolflg = DOALL; 369 } 370 else if (c == '\\') { 371 c = getC(0); 372 /* 373 * PWP: this is dumb, but how all of the other shells work. If \ quotes 374 * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY 375 * following character INSIDE a set of ''s. 376 * 377 * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar 378 */ 379 if (c == (eChar)HIST) 380 c |= QUOTE; 381 else { 382 if (bslash_quote && 383 ((c == '\'') || (c == '"') || 384 (c == '\\') || (c == '$'))) { 385 c |= QUOTE; 386 } 387 else { 388 if (c == '\n') 389 /* 390 * if (c1 == '`') c = ' '; else 391 */ 392 c |= QUOTE; 393 ungetC(c); 394 c = '\\' | QUOTE; 395 } 396 } 397 } 398 else if (c == '\n') { 399 seterror(ERR_UNMATCHED, c1); 400 ungetC(c); 401 break; 402 } 403 } 404 else if (cmap(c, _META | _QF | _QB | _ESC)) { 405 if (c == '\\') { 406 c = getC(0); 407 if (c == '\n') { 408 if (onelflg == 1) 409 onelflg = 2; 410 break; 411 } 412 if (c != (eChar)HIST) 413 Strbuf_append1(&wbuf, '\\'); 414 c |= QUOTE; 415 } 416 else if (cmap(c, _QF | _QB)) { /* '"` */ 417 c1 = c; 418 dolflg = c == '"' ? DOALL : DOEXCL; 419 } 420 else if (c != '#' || (!intty && !enterhist)) { 421 ungetC(c); 422 break; 423 } 424 } 425 Strbuf_append1(&wbuf, c); 426 c = getC(dolflg); 427 } 428 ret: 429 cleanup_ignore(&wbuf); 430 cleanup_until(&wbuf); 431 return Strbuf_finish(&wbuf); 432 } 433 434 static eChar 435 getC1(int flag) 436 { 437 eChar c; 438 439 for (;;) { 440 if ((c = peekc) != 0) { 441 peekc = 0; 442 return (c); 443 } 444 if (lap < labuf.len) { 445 c = labuf.s[lap++]; 446 if (cmap(c, _META | _QF | _QB)) 447 c |= QUOTE; 448 return (c); 449 } 450 if ((c = peekd) != 0) { 451 peekd = 0; 452 return (c); 453 } 454 if (exclp) { 455 if ((c = *exclp++) != 0) 456 return (c); 457 if (exclnxt && --exclc >= 0) { 458 exclnxt = exclnxt->next; 459 setexclp(exclnxt->word); 460 return (' '); 461 } 462 exclp = 0; 463 exclnxt = 0; 464 /* this will throw away the dummy history entries */ 465 savehist(NULL, 0); 466 467 } 468 if (exclnxt) { 469 exclnxt = exclnxt->next; 470 if (--exclc < 0) 471 exclnxt = 0; 472 else 473 setexclp(exclnxt->word); 474 continue; 475 } 476 c = readc(1); 477 478 /* Catch EOF in the middle of a line. (An EOF at the beginning of 479 * a line would have been processed by the readc(0) in lex().) */ 480 if (c == CHAR_ERR) 481 c = '\n'; 482 483 if (c == '$' && (flag & DODOL)) { 484 getdol(); 485 continue; 486 } 487 if (c == (eChar)HIST && (flag & DOEXCL)) { 488 getexcl(0); 489 continue; 490 } 491 break; 492 } 493 return (c); 494 } 495 496 static void 497 getdol(void) 498 { 499 struct Strbuf name = Strbuf_INIT; 500 eChar c; 501 eChar sc; 502 int special = 0; 503 504 c = sc = getC(DOEXCL); 505 if (any("\t \n", c)) { 506 ungetD(c); 507 ungetC('$' | QUOTE); 508 return; 509 } 510 cleanup_push(&name, Strbuf_cleanup); 511 Strbuf_append1(&name, '$'); 512 if (c == '{') 513 Strbuf_append1(&name, c), c = getC(DOEXCL); 514 if (c == '#' || c == '?' || c == '%') 515 special++, Strbuf_append1(&name, c), c = getC(DOEXCL); 516 Strbuf_append1(&name, c); 517 switch (c) { 518 519 case '<': 520 case '$': 521 case '!': 522 if (special) 523 seterror(ERR_SPDOLLT); 524 goto end; 525 526 case '\n': 527 ungetD(c); 528 name.len--; 529 if (!special) 530 seterror(ERR_NEWLINE); 531 goto end; 532 533 case '*': 534 if (special) 535 seterror(ERR_SPSTAR); 536 goto end; 537 538 default: 539 if (Isdigit(c)) { 540 #ifdef notdef 541 /* let $?0 pass for now */ 542 if (special) { 543 seterror(ERR_DIGIT); 544 goto end; 545 } 546 #endif 547 while ((c = getC(DOEXCL)) != 0) { 548 if (!Isdigit(c)) 549 break; 550 Strbuf_append1(&name, c); 551 } 552 } 553 else if (letter(c)) { 554 while ((c = getC(DOEXCL)) != 0) { 555 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */ 556 if (!letter(c) && !Isdigit(c)) 557 break; 558 Strbuf_append1(&name, c); 559 } 560 } 561 else { 562 if (!special) 563 seterror(ERR_VARILL); 564 else { 565 ungetD(c); 566 name.len--; 567 } 568 goto end; 569 } 570 break; 571 } 572 if (c == '[') { 573 Strbuf_append1(&name, c); 574 do { 575 /* 576 * Michael Greim: Allow $ expansion to take place in selector 577 * expressions. (limits the number of characters returned) 578 */ 579 c = getC(DOEXCL | DODOL); 580 if (c == '\n') { 581 ungetD(c); 582 name.len--; 583 seterror(ERR_NLINDEX); 584 goto end; 585 } 586 Strbuf_append1(&name, c); 587 } while (c != ']'); 588 c = getC(DOEXCL); 589 } 590 if (c == ':') { 591 /* 592 * if the :g modifier is followed by a newline, then error right away! 593 * -strike 594 */ 595 596 int gmodflag = 0, amodflag = 0; 597 598 do { 599 Strbuf_append1(&name, c), c = getC(DOEXCL); 600 if (c == 'g' || c == 'a') { 601 if (c == 'g') 602 gmodflag++; 603 else 604 amodflag++; 605 Strbuf_append1(&name, c); c = getC(DOEXCL); 606 } 607 if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) { 608 if (c == 'g') 609 gmodflag++; 610 else 611 amodflag++; 612 Strbuf_append1(&name, c); c = getC(DOEXCL); 613 } 614 Strbuf_append1(&name, c); 615 /* scan s// [eichin:19910926.0512EST] */ 616 if (c == 's') { 617 int delimcnt = 2; 618 eChar delim = getC(0); 619 620 Strbuf_append1(&name, delim); 621 if (!delim || letter(delim) 622 || Isdigit(delim) || any(" \t\n", delim)) { 623 seterror(ERR_BADSUBST); 624 break; 625 } 626 while ((c = getC(0)) != CHAR_ERR) { 627 Strbuf_append1(&name, c); 628 if(c == delim) delimcnt--; 629 if(!delimcnt) break; 630 } 631 if(delimcnt) { 632 seterror(ERR_BADSUBST); 633 break; 634 } 635 c = 's'; 636 } 637 if (!any("htrqxesul", c)) { 638 if ((amodflag || gmodflag) && c == '\n') 639 stderror(ERR_VARSYN); /* strike */ 640 seterror(ERR_BADMOD, c); 641 goto end; 642 } 643 } 644 while ((c = getC(DOEXCL)) == ':'); 645 ungetD(c); 646 } 647 else 648 ungetD(c); 649 if (sc == '{') { 650 c = getC(DOEXCL); 651 if (c != '}') { 652 ungetD(c); 653 seterror(ERR_MISSING, '}'); 654 goto end; 655 } 656 Strbuf_append1(&name, c); 657 } 658 end: 659 cleanup_ignore(&name); 660 cleanup_until(&name); 661 addla(Strbuf_finish(&name)); 662 } 663 664 /* xfree()'s its argument */ 665 void 666 addla(Char *cp) 667 { 668 static struct Strbuf buf; /* = Strbuf_INIT; */ 669 670 buf.len = 0; 671 Strbuf_appendn(&buf, labuf.s + lap, labuf.len - lap); 672 labuf.len = 0; 673 Strbuf_append(&labuf, cp); 674 Strbuf_terminate(&labuf); 675 Strbuf_appendn(&labuf, buf.s, buf.len); 676 xfree(cp); 677 lap = 0; 678 } 679 680 /* left-hand side of last :s or search string of last ?event? */ 681 static struct Strbuf lhsb; /* = Strbuf_INIT; */ 682 static struct Strbuf slhs; /* = Strbuf_INIT; left-hand side of last :s */ 683 static struct Strbuf rhsb; /* = Strbuf_INIT; right-hand side of last :s */ 684 static int quesarg; 685 686 static void 687 getexcl(Char sc) 688 { 689 struct wordent *hp, *ip; 690 int left, right, dol; 691 eChar c; 692 693 if (sc == 0) { 694 c = getC(0); 695 if (c == '{') 696 sc = (Char) c; 697 else 698 ungetC(c); 699 } 700 quesarg = -1; 701 702 lastev = eventno; 703 hp = gethent(sc); 704 if (hp == 0) 705 return; 706 hadhist = 1; 707 dol = 0; 708 if (hp == alhistp) 709 for (ip = hp->next->next; ip != alhistt; ip = ip->next) 710 dol++; 711 else 712 for (ip = hp->next->next; ip != hp->prev; ip = ip->next) 713 dol++; 714 left = 0, right = dol; 715 if (sc == HISTSUB && HISTSUB != '\0') { 716 ungetC('s'), unreadc(HISTSUB), c = ':'; 717 goto subst; 718 } 719 c = getC(0); 720 if (!any(":^$*-%", c)) 721 goto subst; 722 left = right = -1; 723 if (c == ':') { 724 c = getC(0); 725 unreadc(c); 726 if (letter(c) || c == '&') { 727 c = ':'; 728 left = 0, right = dol; 729 goto subst; 730 } 731 } 732 else 733 ungetC(c); 734 if (!getsel(&left, &right, dol)) 735 return; 736 c = getC(0); 737 if (c == '*') 738 ungetC(c), c = '-'; 739 if (c == '-') { 740 if (!getsel(&left, &right, dol)) 741 return; 742 c = getC(0); 743 } 744 subst: 745 exclc = right - left + 1; 746 while (--left >= 0) 747 hp = hp->next; 748 if ((sc == HISTSUB && HISTSUB != '\0') || c == ':') { 749 do { 750 hp = getsub(hp); 751 c = getC(0); 752 } while (c == ':'); 753 } 754 unreadc(c); 755 if (sc == '{') { 756 c = getC(0); 757 if (c != '}') 758 seterror(ERR_BADBANG); 759 } 760 exclnxt = hp; 761 } 762 763 static struct wordent * 764 getsub(struct wordent *en) 765 { 766 eChar delim; 767 eChar c; 768 eChar sc; 769 int global; 770 771 do { 772 exclnxt = 0; 773 global = 0; 774 sc = c = getC(0); 775 while (c == 'g' || c == 'a') { 776 global |= (c == 'g') ? FLAG_G : FLAG_A; 777 sc = c = getC(0); 778 } 779 780 switch (c) { 781 case 'p': 782 justpr++; 783 return (en); 784 785 case 'x': 786 case 'q': 787 global |= FLAG_G; 788 /*FALLTHROUGH*/ 789 790 case 'h': 791 case 'r': 792 case 't': 793 case 'e': 794 case 'u': 795 case 'l': 796 break; 797 798 case '&': 799 if (slhs.len == 0) { 800 seterror(ERR_NOSUBST); 801 return (en); 802 } 803 lhsb.len = 0; 804 Strbuf_append(&lhsb, slhs.s); 805 Strbuf_terminate(&lhsb); 806 break; 807 808 #ifdef notdef 809 case '~': 810 if (lhsb.len == 0) 811 goto badlhs; 812 break; 813 #endif 814 815 case 's': 816 delim = getC(0); 817 if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) { 818 unreadc(delim); 819 lhsb.len = 0; 820 seterror(ERR_BADSUBST); 821 return (en); 822 } 823 Strbuf_terminate(&lhsb); 824 lhsb.len = 0; 825 for (;;) { 826 c = getC(0); 827 if (c == '\n') { 828 unreadc(c); 829 break; 830 } 831 if (c == delim) 832 break; 833 if (c == '\\') { 834 c = getC(0); 835 if (c != delim && c != '\\') 836 Strbuf_append1(&lhsb, '\\'); 837 } 838 Strbuf_append1(&lhsb, c); 839 } 840 if (lhsb.len != 0) 841 Strbuf_terminate(&lhsb); 842 else if (lhsb.s[0] == 0) { 843 seterror(ERR_LHS); 844 return (en); 845 } else 846 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */ 847 rhsb.len = 0; 848 for (;;) { 849 c = getC(0); 850 if (c == '\n') { 851 unreadc(c); 852 break; 853 } 854 if (c == delim) 855 break; 856 if (c == '\\') { 857 c = getC(0); 858 if (c != delim /* && c != '~' */ ) 859 Strbuf_append1(&rhsb, '\\'); 860 } 861 Strbuf_append1(&rhsb, c); 862 } 863 Strbuf_terminate(&rhsb); 864 break; 865 866 default: 867 if (c == '\n') 868 unreadc(c); 869 seterror(ERR_BADBANGMOD, (int)c); 870 return (en); 871 } 872 slhs.len = 0; 873 if (lhsb.s != NULL && lhsb.len != 0) 874 Strbuf_append(&slhs, lhsb.s); 875 Strbuf_terminate(&slhs); 876 if (exclc) 877 en = dosub(sc, en, global); 878 } 879 while ((c = getC(0)) == ':'); 880 unreadc(c); 881 return (en); 882 } 883 884 /* 885 * 886 * From Beto Appleton (beto@aixwiz.austin.ibm.com) 887 * 888 * when using history substitution, and the variable 889 * 'history' is set to a value higher than 1000, 890 * the shell might either freeze (hang) or core-dump. 891 * We raise the limit to 50000000 892 */ 893 894 #define HIST_PURGE -50000000 895 static struct wordent * 896 dosub(Char sc, struct wordent *en, int global) 897 { 898 struct wordent lexi; 899 int didsub = 0, didone = 0; 900 struct wordent *hp = &lexi; 901 struct wordent *wdp; 902 int i = exclc; 903 struct Hist *hst; 904 905 wdp = hp; 906 while (--i >= 0) { 907 struct wordent *new = xcalloc(1, sizeof *wdp); 908 909 new->word = 0; 910 new->prev = wdp; 911 new->next = hp; 912 wdp->next = new; 913 wdp = new; 914 en = en->next; 915 if (en->word) { 916 Char *tword, *otword; 917 918 if ((global & FLAG_G) || didsub == 0) { 919 size_t pos; 920 921 pos = 0; 922 tword = subword(en->word, sc, &didone, &pos); 923 if (didone) 924 didsub = 1; 925 if (global & FLAG_A) { 926 while (didone && tword != STRNULL) { 927 otword = tword; 928 tword = subword(otword, sc, &didone, &pos); 929 if (Strcmp(tword, otword) == 0) { 930 xfree(otword); 931 break; 932 } 933 else 934 xfree(otword); 935 } 936 } 937 } 938 else 939 tword = Strsave(en->word); 940 wdp->word = tword; 941 } 942 } 943 if (didsub == 0) 944 seterror(ERR_MODFAIL); 945 hp->prev = wdp; 946 /* 947 * ANSI mode HP/UX compiler chokes on 948 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex; 949 */ 950 hst = enthist(HIST_PURGE, &lexi, 0, 0, -1); 951 return &(hst->Hlex); 952 } 953 954 /* Return a newly allocated result of one modification of CP using the 955 operation TYPE. Set ADID to 1 if a modification was performed. 956 If TYPE == 's', perform substitutions only from *START_POS on and set 957 *START_POS to the position of next substitution attempt. */ 958 static Char * 959 subword(Char *cp, Char type, int *adid, size_t *start_pos) 960 { 961 Char *wp; 962 const Char *mp, *np; 963 964 switch (type) { 965 966 case 'r': 967 case 'e': 968 case 'h': 969 case 't': 970 case 'q': 971 case 'x': 972 case 'u': 973 case 'l': 974 wp = domod(cp, type); 975 if (wp == 0) { 976 *adid = 0; 977 return (Strsave(cp)); 978 } 979 *adid = 1; 980 return (wp); 981 982 default: 983 for (mp = cp + *start_pos; *mp; mp++) { 984 if (matchs(mp, lhsb.s)) { 985 struct Strbuf wbuf = Strbuf_INIT; 986 987 Strbuf_appendn(&wbuf, cp, mp - cp); 988 for (np = rhsb.s; *np; np++) 989 switch (*np) { 990 991 case '\\': 992 if (np[1] == '&') 993 np++; 994 /* fall into ... */ 995 996 default: 997 Strbuf_append1(&wbuf, *np); 998 continue; 999 1000 case '&': 1001 Strbuf_append(&wbuf, lhsb.s); 1002 continue; 1003 } 1004 *start_pos = wbuf.len; 1005 Strbuf_append(&wbuf, mp + lhsb.len); 1006 *adid = 1; 1007 return Strbuf_finish(&wbuf); 1008 } 1009 } 1010 *adid = 0; 1011 return (Strsave(cp)); 1012 } 1013 } 1014 1015 Char * 1016 domod(Char *cp, Char type) 1017 { 1018 Char *wp, *xp; 1019 int c; 1020 1021 switch (type) { 1022 1023 case 'x': 1024 case 'q': 1025 wp = Strsave(cp); 1026 for (xp = wp; (c = *xp) != 0; xp++) 1027 if ((c != ' ' && c != '\t') || type == 'q') 1028 *xp |= QUOTE; 1029 return (wp); 1030 1031 case 'l': 1032 wp = NLSChangeCase(cp, 1); 1033 return wp ? wp : Strsave(cp); 1034 1035 case 'u': 1036 wp = NLSChangeCase(cp, 0); 1037 return wp ? wp : Strsave(cp); 1038 1039 case 'h': 1040 case 't': 1041 if (!any(short2str(cp), '/')) 1042 return (type == 't' ? Strsave(cp) : 0); 1043 wp = Strrchr(cp, '/'); 1044 if (type == 'h') 1045 xp = Strnsave(cp, wp - cp); 1046 else 1047 xp = Strsave(wp + 1); 1048 return (xp); 1049 1050 case 'e': 1051 case 'r': 1052 wp = Strend(cp); 1053 for (wp--; wp >= cp && *wp != '/'; wp--) 1054 if (*wp == '.') { 1055 if (type == 'e') 1056 xp = Strsave(wp + 1); 1057 else 1058 xp = Strnsave(cp, wp - cp); 1059 return (xp); 1060 } 1061 return (Strsave(type == 'e' ? STRNULL : cp)); 1062 default: 1063 break; 1064 } 1065 return (0); 1066 } 1067 1068 static int 1069 matchs(const Char *str, const Char *pat) 1070 { 1071 while (*str && *pat && *str == *pat) 1072 str++, pat++; 1073 return (*pat == 0); 1074 } 1075 1076 static int 1077 getsel(int *al, int *ar, int dol) 1078 { 1079 eChar c = getC(0); 1080 int i; 1081 int first = *al < 0; 1082 1083 switch (c) { 1084 1085 case '%': 1086 if (quesarg == -1) { 1087 seterror(ERR_BADBANGARG); 1088 return (0); 1089 } 1090 if (*al < 0) 1091 *al = quesarg; 1092 *ar = quesarg; 1093 break; 1094 1095 case '-': 1096 if (*al < 0) { 1097 *al = 0; 1098 *ar = dol - 1; 1099 unreadc(c); 1100 } 1101 return (1); 1102 1103 case '^': 1104 if (*al < 0) 1105 *al = 1; 1106 *ar = 1; 1107 break; 1108 1109 case '$': 1110 if (*al < 0) 1111 *al = dol; 1112 *ar = dol; 1113 break; 1114 1115 case '*': 1116 if (*al < 0) 1117 *al = 1; 1118 *ar = dol; 1119 if (*ar < *al) { 1120 *ar = 0; 1121 *al = 1; 1122 return (1); 1123 } 1124 break; 1125 1126 default: 1127 if (Isdigit(c)) { 1128 i = 0; 1129 while (Isdigit(c)) { 1130 i = i * 10 + c - '0'; 1131 c = getC(0); 1132 } 1133 if (i < 0) 1134 i = dol + 1; 1135 if (*al < 0) 1136 *al = i; 1137 *ar = i; 1138 } 1139 else if (*al < 0) 1140 *al = 0, *ar = dol; 1141 else 1142 *ar = dol - 1; 1143 unreadc(c); 1144 break; 1145 } 1146 if (first) { 1147 c = getC(0); 1148 unreadc(c); 1149 if (any("-$*", c)) 1150 return (1); 1151 } 1152 if (*al > *ar || *ar > dol) { 1153 seterror(ERR_BADBANGARG); 1154 return (0); 1155 } 1156 return (1); 1157 1158 } 1159 1160 static struct wordent * 1161 gethent(Char sc) 1162 { 1163 struct Hist *hp; 1164 Char *np; 1165 eChar c; 1166 int event; 1167 int back = 0; 1168 1169 c = (sc == HISTSUB && HISTSUB != '\0') ? (eChar)HIST : getC(0); 1170 if (c == (eChar)HIST) { 1171 if (alhistp) 1172 return (alhistp); 1173 event = eventno; 1174 } 1175 else 1176 switch (c) { 1177 1178 case ':': 1179 case '^': 1180 case '$': 1181 case '*': 1182 case '%': 1183 ungetC(c); 1184 if (lastev == eventno && alhistp) 1185 return (alhistp); 1186 event = lastev; 1187 break; 1188 1189 case '#': /* !# is command being typed in (mrh) */ 1190 if (--hleft == 0) { 1191 seterror(ERR_HISTLOOP); 1192 return (0); 1193 } 1194 else 1195 return (¶ml); 1196 /* NOTREACHED */ 1197 1198 case '-': 1199 back = 1; 1200 c = getC(0); 1201 /* FALLSTHROUGH */ 1202 1203 default: 1204 if (any("(=~", c)) { 1205 unreadc(c); 1206 ungetC(HIST); 1207 return (0); 1208 } 1209 Strbuf_terminate(&lhsb); 1210 lhsb.len = 0; 1211 event = 0; 1212 while (!cmap(c, _ESC | _META | _QF | _QB) && !any("^*-%${}:#", c)) { 1213 if (event != -1 && Isdigit(c)) 1214 event = event * 10 + c - '0'; 1215 else 1216 event = -1; 1217 Strbuf_append1(&lhsb, c); 1218 c = getC(0); 1219 } 1220 unreadc(c); 1221 if (lhsb.len == 0) { 1222 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */ 1223 ungetC(HIST); 1224 return (0); 1225 } 1226 Strbuf_terminate(&lhsb); 1227 if (event != -1) { 1228 /* 1229 * History had only digits 1230 */ 1231 if (back) 1232 event = eventno + (alhistp == 0) - event; 1233 break; 1234 } 1235 if (back) { 1236 Strbuf_append1(&lhsb, '\0'); /* Allocate space */ 1237 Strbuf_terminate(&lhsb); 1238 memmove(lhsb.s + 1, lhsb.s, (lhsb.len - 1) * sizeof (*lhsb.s)); 1239 lhsb.s[0] = '-'; 1240 } 1241 hp = findev(lhsb.s, 0); 1242 if (hp) 1243 lastev = hp->Hnum; 1244 return (&hp->Hlex); 1245 1246 case '?': 1247 Strbuf_terminate(&lhsb); 1248 lhsb.len = 0; 1249 for (;;) { 1250 c = getC(0); 1251 if (c == '\n') { 1252 unreadc(c); 1253 break; 1254 } 1255 if (c == '?') 1256 break; 1257 Strbuf_append1(&lhsb, c); 1258 } 1259 if (lhsb.len == 0) { 1260 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */ 1261 if (lhsb.len == 0) { 1262 seterror(ERR_NOSEARCH); 1263 return (0); 1264 } 1265 } 1266 else 1267 Strbuf_terminate(&lhsb); 1268 hp = findev(lhsb.s, 1); 1269 if (hp) 1270 lastev = hp->Hnum; 1271 return (&hp->Hlex); 1272 } 1273 1274 for (hp = Histlist.Hnext; hp; hp = hp->Hnext) 1275 if (hp->Hnum == event) { 1276 hp->Href = eventno; 1277 lastev = hp->Hnum; 1278 return (&hp->Hlex); 1279 } 1280 np = putn((tcsh_number_t)event); 1281 seterror(ERR_NOEVENT, short2str(np)); 1282 xfree(np); 1283 return (0); 1284 } 1285 1286 static struct Hist * 1287 findev(Char *cp, int anyarg) 1288 { 1289 struct Hist *hp; 1290 1291 for (hp = Histlist.Hnext; hp; hp = hp->Hnext) { 1292 Char *dp; 1293 Char *p, *q; 1294 struct wordent *lp = hp->Hlex.next; 1295 int argno = 0; 1296 1297 /* 1298 * The entries added by alias substitution don't have a newline but do 1299 * have a negative event number. Savehist() trims off these entries, 1300 * but it happens before alias expansion, too early to delete those 1301 * from the previous command. 1302 */ 1303 if (hp->Hnum < 0) 1304 continue; 1305 if (lp->word[0] == '\n') 1306 continue; 1307 if (!anyarg) { 1308 p = cp; 1309 q = lp->word; 1310 do 1311 if (!*p) 1312 return (hp); 1313 while (*p++ == *q++); 1314 continue; 1315 } 1316 do { 1317 for (dp = lp->word; *dp; dp++) { 1318 p = cp; 1319 q = dp; 1320 do 1321 if (!*p) { 1322 quesarg = argno; 1323 return (hp); 1324 } 1325 while (*p++ == *q++); 1326 } 1327 lp = lp->next; 1328 argno++; 1329 } while (lp->word[0] != '\n'); 1330 } 1331 seterror(ERR_NOEVENT, short2str(cp)); 1332 return (0); 1333 } 1334 1335 1336 static void 1337 setexclp(Char *cp) 1338 { 1339 if (cp && cp[0] == '\n') 1340 return; 1341 exclp = cp; 1342 } 1343 1344 void 1345 unreadc(Char c) 1346 { 1347 peekread = (Char) c; 1348 } 1349 1350 eChar 1351 readc(int wanteof) 1352 { 1353 eChar c; 1354 static int sincereal; /* Number of real EOFs we've seen */ 1355 1356 #ifdef DEBUG_INP 1357 xprintf("readc\n"); 1358 #endif 1359 if ((c = peekread) != 0) { 1360 peekread = 0; 1361 return (c); 1362 } 1363 1364 top: 1365 aret = TCSH_F_SEEK; 1366 if (alvecp) { 1367 arun = 1; 1368 #ifdef DEBUG_INP 1369 xprintf("alvecp %c\n", *alvecp & 0xff); 1370 #endif 1371 aret = TCSH_A_SEEK; 1372 if ((c = *alvecp++) != 0) 1373 return (c); 1374 if (alvec && *alvec) { 1375 alvecp = *alvec++; 1376 return (' '); 1377 } 1378 else { 1379 alvecp = NULL; 1380 aret = TCSH_F_SEEK; 1381 return('\n'); 1382 } 1383 } 1384 if (alvec) { 1385 arun = 1; 1386 if ((alvecp = *alvec) != 0) { 1387 alvec++; 1388 goto top; 1389 } 1390 /* Infinite source! */ 1391 return ('\n'); 1392 } 1393 arun = 0; 1394 if (evalp) { 1395 aret = TCSH_E_SEEK; 1396 if ((c = *evalp++) != 0) 1397 return (c); 1398 if (evalvec && *evalvec) { 1399 evalp = *evalvec++; 1400 return (' '); 1401 } 1402 aret = TCSH_F_SEEK; 1403 evalp = 0; 1404 } 1405 if (evalvec) { 1406 if (evalvec == INVPPTR) { 1407 doneinp = 1; 1408 reset(); 1409 } 1410 if ((evalp = *evalvec) != 0) { 1411 evalvec++; 1412 goto top; 1413 } 1414 evalvec = INVPPTR; 1415 return ('\n'); 1416 } 1417 do { 1418 if (arginp == INVPTR || onelflg == 1) { 1419 if (wanteof) 1420 return CHAR_ERR; 1421 exitstat(); 1422 } 1423 if (arginp) { 1424 if ((c = *arginp++) == 0) { 1425 arginp = INVPTR; 1426 return ('\n'); 1427 } 1428 return (c); 1429 } 1430 #ifdef BSDJOBS 1431 reread: 1432 #endif /* BSDJOBS */ 1433 c = bgetc(); 1434 if (c == CHAR_ERR) { 1435 #ifndef WINNT_NATIVE 1436 # ifndef POSIX 1437 # ifdef TERMIO 1438 struct termio tty; 1439 # else /* SGTTYB */ 1440 struct sgttyb tty; 1441 # endif /* TERMIO */ 1442 # else /* POSIX */ 1443 struct termios tty; 1444 # endif /* POSIX */ 1445 #endif /* !WINNT_NATIVE */ 1446 if (wanteof) 1447 return CHAR_ERR; 1448 /* was isatty but raw with ignoreeof yields problems */ 1449 #ifndef WINNT_NATIVE 1450 # ifndef POSIX 1451 # ifdef TERMIO 1452 if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 && 1453 (tty.c_lflag & ICANON)) 1454 # else /* GSTTYB */ 1455 if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 && 1456 (tty.sg_flags & RAW) == 0) 1457 # endif /* TERMIO */ 1458 # else /* POSIX */ 1459 if (tcgetattr(SHIN, &tty) == 0 && 1460 (tty.c_lflag & ICANON)) 1461 # endif /* POSIX */ 1462 #else /* WINNT_NATIVE */ 1463 if (isatty(SHIN)) 1464 #endif /* !WINNT_NATIVE */ 1465 { 1466 #ifdef BSDJOBS 1467 pid_t ctpgrp; 1468 #endif /* BSDJOBS */ 1469 1470 if (numeof != 0 && ++sincereal >= numeof) /* Too many EOFs? Bye! */ 1471 goto oops; 1472 #ifdef BSDJOBS 1473 if (tpgrp != -1 && 1474 (ctpgrp = tcgetpgrp(FSHTTY)) != -1 && 1475 tpgrp != ctpgrp) { 1476 (void) tcsetpgrp(FSHTTY, tpgrp); 1477 # ifdef _SEQUENT_ 1478 if (ctpgrp) 1479 # endif /* _SEQUENT */ 1480 (void) killpg(ctpgrp, SIGHUP); 1481 # ifdef notdef 1482 /* 1483 * With the walking process group fix, this message 1484 * is now obsolete. As the foreground process group 1485 * changes, the shell needs to adjust. Well too bad. 1486 */ 1487 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"), 1488 (int)ctpgrp, (int)tpgrp); 1489 # endif /* notdef */ 1490 goto reread; 1491 } 1492 #endif /* BSDJOBS */ 1493 /* What follows is complicated EOF handling -- sterling@netcom.com */ 1494 /* First, we check to see if we have ignoreeof set */ 1495 if (adrof(STRignoreeof)) { 1496 /* If so, we check for any stopped jobs only on the first EOF */ 1497 if ((sincereal == 1) && (chkstop == 0)) { 1498 panystop(1); 1499 } 1500 } else { 1501 /* If we don't have ignoreeof set, always check for stopped jobs */ 1502 if (chkstop == 0) { 1503 panystop(1); 1504 } 1505 } 1506 /* At this point, if there were stopped jobs, we would have already 1507 * called reset(). If we got this far, assume we can print an 1508 * exit/logout message if we ignoreeof, or just exit. 1509 */ 1510 if (adrof(STRignoreeof)) { 1511 /* If so, tell the user to use exit or logout */ 1512 if (loginsh) { 1513 xprintf("%s", CGETS(16, 2, 1514 "\nUse \"logout\" to logout.\n")); 1515 } else { 1516 xprintf(CGETS(16, 3, 1517 "\nUse \"exit\" to leave %s.\n"), 1518 progname); 1519 } 1520 reset(); 1521 } else { 1522 /* If we don't have ignoreeof set, just fall through */ 1523 ; /* EMPTY */ 1524 } 1525 } 1526 oops: 1527 doneinp = 1; 1528 reset(); 1529 } 1530 sincereal = 0; 1531 if (c == '\n' && onelflg) 1532 onelflg--; 1533 } while (c == 0); 1534 Strbuf_append1(&histline, c); 1535 return (c); 1536 } 1537 1538 static void 1539 balloc(int buf) 1540 { 1541 Char **nfbuf; 1542 1543 while (buf >= fblocks) { 1544 nfbuf = xcalloc(fblocks + 2, sizeof(Char **)); 1545 if (fbuf) { 1546 (void) blkcpy(nfbuf, fbuf); 1547 xfree(fbuf); 1548 } 1549 fbuf = nfbuf; 1550 fbuf[fblocks] = xcalloc(BUFSIZE, sizeof(Char)); 1551 fblocks++; 1552 } 1553 } 1554 1555 ssize_t 1556 wide_read(int fildes, Char *buf, size_t nchars, int use_fclens) 1557 { 1558 char cbuf[BUFSIZE + 1]; 1559 ssize_t res, r = 0; 1560 size_t partial; 1561 int err; 1562 1563 if (nchars == 0) 1564 return 0; 1565 assert (nchars <= sizeof(cbuf) / sizeof(*cbuf)); 1566 USE(use_fclens); 1567 res = 0; 1568 partial = 0; 1569 do { 1570 size_t i; 1571 size_t len = nchars > partial ? nchars - partial : 1; 1572 1573 if (partial + len >= sizeof(cbuf) / sizeof(*cbuf)) 1574 break; 1575 1576 r = xread(fildes, cbuf + partial, len); 1577 1578 if (partial == 0 && r <= 0) 1579 break; 1580 partial += r; 1581 i = 0; 1582 while (i < partial && nchars != 0) { 1583 int tlen; 1584 1585 tlen = normal_mbtowc(buf + res, cbuf + i, partial - i); 1586 if (tlen == -1) { 1587 reset_mbtowc(); 1588 if ((partial - i) < MB_LEN_MAX && r > 0) 1589 /* Maybe a partial character and there is still a chance 1590 to read more */ 1591 break; 1592 buf[res] = (unsigned char)cbuf[i] | INVALID_BYTE; 1593 } 1594 if (tlen <= 0) 1595 tlen = 1; 1596 #ifdef WIDE_STRINGS 1597 if (use_fclens) 1598 fclens[res] = tlen; 1599 #endif 1600 i += tlen; 1601 res++; 1602 nchars--; 1603 } 1604 if (i != partial) 1605 memmove(cbuf, cbuf + i, partial - i); 1606 partial -= i; 1607 } while (partial != 0 && nchars > 0); 1608 /* Throwing away possible partial multibyte characters on error if the 1609 stream is not seekable */ 1610 err = errno; 1611 lseek(fildes, -(off_t)partial, L_INCR); 1612 errno = err; 1613 return res != 0 ? res : r; 1614 } 1615 1616 static eChar 1617 bgetc(void) 1618 { 1619 Char ch; 1620 int c, off, buf; 1621 int numleft = 0, roomleft; 1622 1623 if (cantell) { 1624 if (fseekp < fbobp || fseekp > feobp) { 1625 fbobp = feobp = fseekp; 1626 (void) lseek(SHIN, fseekp, L_SET); 1627 } 1628 if (fseekp == feobp) { 1629 #ifdef WIDE_STRINGS 1630 off_t bytes; 1631 size_t i; 1632 1633 bytes = fbobp; 1634 for (i = 0; i < (size_t)(feobp - fbobp); i++) 1635 bytes += fclens[i]; 1636 fseekp = feobp = bytes; 1637 #endif 1638 fbobp = feobp; 1639 c = wide_read(SHIN, fbuf[0], BUFSIZE, 1); 1640 #ifdef convex 1641 if (c < 0) 1642 stderror(ERR_SYSTEM, progname, strerror(errno)); 1643 #endif /* convex */ 1644 if (c <= 0) 1645 return CHAR_ERR; 1646 feobp += c; 1647 } 1648 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__) 1649 ch = fbuf[0][fseekp - fbobp]; 1650 fseekp++; 1651 #else 1652 do { 1653 ch = fbuf[0][fseekp - fbobp]; 1654 fseekp++; 1655 } while(ch == '\r'); 1656 #endif /* !WINNT_NATIVE && !__CYGWIN__ */ 1657 return (ch); 1658 } 1659 1660 while (fseekp >= feobp) { 1661 if ((editing 1662 #if defined(FILEC) && defined(TIOCSTI) 1663 || filec 1664 #endif /* FILEC && TIOCSTI */ 1665 ) && intty) { /* then use twenex routine */ 1666 fseekp = feobp; /* where else? */ 1667 #if defined(FILEC) && defined(TIOCSTI) 1668 if (!editing) 1669 c = numleft = tenex(InputBuf, BUFSIZE); 1670 else 1671 #endif /* FILEC && TIOCSTI */ 1672 c = numleft = Inputl(); /* PWP: get a line */ 1673 while (numleft > 0) { 1674 off = (int) feobp % BUFSIZE; 1675 buf = (int) feobp / BUFSIZE; 1676 balloc(buf); 1677 roomleft = BUFSIZE - off; 1678 if (roomleft > numleft) 1679 roomleft = numleft; 1680 (void) memcpy(fbuf[buf] + off, InputBuf + c - numleft, 1681 roomleft * sizeof(Char)); 1682 numleft -= roomleft; 1683 feobp += roomleft; 1684 } 1685 } else { 1686 off = (int) feobp % BUFSIZE; 1687 buf = (int) feobp / BUFSIZE; 1688 balloc(buf); 1689 roomleft = BUFSIZE - off; 1690 c = wide_read(SHIN, fbuf[buf] + off, roomleft, 0); 1691 if (c > 0) 1692 feobp += c; 1693 } 1694 if (c == 0 || (c < 0 && fixio(SHIN, errno) == -1)) 1695 return CHAR_ERR; 1696 } 1697 #ifdef SIG_WINDOW 1698 if (windowchg) 1699 (void) check_window_size(0); /* for window systems */ 1700 #endif /* SIG_WINDOW */ 1701 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__) 1702 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE]; 1703 fseekp++; 1704 #else 1705 do { 1706 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE]; 1707 fseekp++; 1708 } while(ch == '\r'); 1709 #endif /* !WINNT_NATIVE && !__CYGWIN__ */ 1710 return (ch); 1711 } 1712 1713 static void 1714 bfree(void) 1715 { 1716 int sb, i; 1717 1718 if (cantell) 1719 return; 1720 if (whyles) 1721 return; 1722 sb = (int) (fseekp - 1) / BUFSIZE; 1723 if (sb > 0) { 1724 for (i = 0; i < sb; i++) 1725 xfree(fbuf[i]); 1726 (void) blkcpy(fbuf, &fbuf[sb]); 1727 fseekp -= BUFSIZE * sb; 1728 feobp -= BUFSIZE * sb; 1729 fblocks -= sb; 1730 } 1731 } 1732 1733 void 1734 bseek(struct Ain *l) 1735 { 1736 switch (aret = l->type) { 1737 case TCSH_E_SEEK: 1738 evalvec = l->a_seek; 1739 evalp = l->c_seek; 1740 #ifdef DEBUG_SEEK 1741 xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec, evalp); 1742 #endif 1743 return; 1744 case TCSH_A_SEEK: 1745 alvec = l->a_seek; 1746 alvecp = l->c_seek; 1747 #ifdef DEBUG_SEEK 1748 xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec, alvecp); 1749 #endif 1750 return; 1751 case TCSH_F_SEEK: 1752 #ifdef DEBUG_SEEK 1753 xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp); 1754 #endif 1755 fseekp = l->f_seek; 1756 #ifdef WIDE_STRINGS 1757 if (cantell) { 1758 if (fseekp >= fbobp && feobp >= fbobp) { 1759 size_t i; 1760 off_t o; 1761 1762 o = fbobp; 1763 for (i = 0; i < (size_t)(feobp - fbobp); i++) { 1764 if (fseekp == o) { 1765 fseekp = fbobp + i; 1766 return; 1767 } 1768 o += fclens[i]; 1769 } 1770 if (fseekp == o) { 1771 fseekp = feobp; 1772 return; 1773 } 1774 } 1775 fbobp = feobp = fseekp + 1; /* To force lseek() */ 1776 } 1777 #endif 1778 return; 1779 default: 1780 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret); 1781 abort(); 1782 } 1783 } 1784 1785 /* any similarity to bell telephone is purely accidental */ 1786 void 1787 btell(struct Ain *l) 1788 { 1789 switch (l->type = aret) { 1790 case TCSH_E_SEEK: 1791 l->a_seek = evalvec; 1792 l->c_seek = evalp; 1793 #ifdef DEBUG_SEEK 1794 xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec, evalp); 1795 #endif 1796 return; 1797 case TCSH_A_SEEK: 1798 l->a_seek = alvec; 1799 l->c_seek = alvecp; 1800 #ifdef DEBUG_SEEK 1801 xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec, alvecp); 1802 #endif 1803 return; 1804 case TCSH_F_SEEK: 1805 #ifdef WIDE_STRINGS 1806 if (cantell && fseekp >= fbobp && fseekp <= feobp) { 1807 size_t i; 1808 1809 l->f_seek = fbobp; 1810 for (i = 0; i < (size_t)(fseekp - fbobp); i++) 1811 l->f_seek += fclens[i]; 1812 } else 1813 #endif 1814 /*SUPPRESS 112*/ 1815 l->f_seek = fseekp; 1816 l->a_seek = NULL; 1817 #ifdef DEBUG_SEEK 1818 xprintf(CGETS(16, 10, "tell file %x\n"), fseekp); 1819 #endif 1820 return; 1821 default: 1822 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret); 1823 abort(); 1824 } 1825 } 1826 1827 void 1828 btoeof(void) 1829 { 1830 (void) lseek(SHIN, (off_t) 0, L_XTND); 1831 aret = TCSH_F_SEEK; 1832 fseekp = feobp; 1833 alvec = NULL; 1834 alvecp = NULL; 1835 evalvec = NULL; 1836 evalp = NULL; 1837 wfree(); 1838 bfree(); 1839 } 1840 1841 void 1842 settell(void) 1843 { 1844 off_t x; 1845 cantell = 0; 1846 if (arginp || onelflg || intty) 1847 return; 1848 if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1) 1849 return; 1850 fbuf = xcalloc(2, sizeof(Char **)); 1851 fblocks = 1; 1852 fbuf[0] = xcalloc(BUFSIZE, sizeof(Char)); 1853 fseekp = fbobp = feobp = x; 1854 cantell = 1; 1855 } 1856