home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume19 / zsh2.00 / patch02e < prev    next >
Encoding:
Text File  |  1991-05-16  |  50.3 KB  |  2,386 lines

  1. Newsgroups: comp.sources.misc
  2. From: Paul Falstad <pfalstad@phoenix.princeton.edu>
  3. Subject:  v19i071:  zsh2.00 - The Z shell, Patch02e/6
  4. Message-ID: <1991May15.220325.7483@sparky.IMD.Sterling.COM>
  5. X-Md4-Signature: 398ff02e6224d7bd0d1c565838d47c2e
  6. Date: Wed, 15 May 1991 22:03:25 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: Paul Falstad <pfalstad@phoenix.princeton.edu>
  10. Posting-number: Volume 19, Issue 71
  11. Archive-name: zsh2.00/patch02e
  12. Patch-To: zsh2.00: Volume 18, Issue 84-98
  13.  
  14. #!/bin/sh
  15. # this is zpatch.05 (part 5 of a multipart archive)
  16. # do not concatenate these parts, unpack them in order with /bin/sh
  17. # file zsh2.00/src/lex.c continued
  18. #
  19. if test ! -r _shar_seq_.tmp; then
  20.     echo 'Please unpack part 1 first!'
  21.     exit 1
  22. fi
  23. (read Scheck
  24.  if test "$Scheck" != 5; then
  25.     echo Please unpack part "$Scheck" next!
  26.     exit 1
  27.  else
  28.     exit 0
  29.  fi
  30. ) < _shar_seq_.tmp || exit 1
  31. echo 'x - continuing file zsh2.00/src/lex.c'
  32. sed 's/^X//' << 'SHAR_EOF' >> 'zsh2.00/src/lex.c' &&
  33. X        case DOUTANGAMP:case DOUTANGAMPBANG: inredir = 1; break;
  34. X        case FUNC:infunc = 1;break;
  35. X        case DINBRACK: incond = 1; break;
  36. X        case DOUTBRACK: incond = 0; break;
  37. X        case DSEMI: ignl = incase = 1; incmd = 0; break;
  38. X        case CASE: incmd = incase = 1; break;
  39. X        }
  40. X    return x;
  41. }
  42. X
  43. #define LX1_BKSLASH 0
  44. #define LX1_COMMENT 1
  45. #define LX1_NEWLIN 2
  46. #define LX1_SEMI 3
  47. #define LX1_BANG 4
  48. #define LX1_AMPER 5
  49. #define LX1_BAR 6
  50. #define LX1_INPAR 7
  51. #define LX1_OUTPAR 8
  52. #define LX1_INBRACE 9
  53. #define LX1_OUTBRACE 10
  54. #define LX1_INBRACK 11
  55. #define LX1_OUTBRACK 12
  56. #define LX1_INANG 13
  57. #define LX1_OUTANG 14
  58. #define LX1_OTHER 15
  59. X
  60. #define LX2_BREAK 0
  61. #define LX2_OUTPAR 1
  62. #define LX2_BAR 2
  63. #define LX2_STRING 3
  64. #define LX2_INBRACK 4
  65. #define LX2_OUTBRACK 5
  66. #define LX2_TILDE 6
  67. #define LX2_INPAR 7
  68. #define LX2_INBRACE 8
  69. #define LX2_OUTBRACE 9
  70. #define LX2_OUTANG 10
  71. #define LX2_INANG 11
  72. #define LX2_EQUALS 12
  73. #define LX2_BKSLASH 13
  74. #define LX2_QUOTE 14
  75. #define LX2_DQUOTE 15
  76. #define LX2_BQUOTE 16
  77. #define LX2_OTHER 17
  78. X
  79. unsigned char lexact1[256],lexact2[256],lextok2[256];
  80. X
  81. void initlextabs() /**/
  82. {
  83. int t0;
  84. static char *lx1 = "\\q\n;!&|(){}[]<>xx";
  85. static char *lx2 = "x)|$[]~({}><=\\\'\"`x";
  86. X
  87. X    for (t0 = 0; t0 != 256; t0++)
  88. X        {
  89. X        lexact1[t0] = LX1_OTHER;
  90. X        lexact2[t0] = LX2_OTHER;
  91. X        lextok2[t0] = t0;
  92. X        }
  93. X    for (t0 = 0; lx1[t0]; t0++)
  94. X        if (lx1[t0] != 'x')
  95. X            lexact1[lx1[t0]] = t0;
  96. X    for (t0 = 0; lx2[t0]; t0++)
  97. X        if (lx2[t0] != 'x')
  98. X            lexact2[lx2[t0]] = t0;
  99. X    lexact2[';'] = LX2_BREAK;
  100. X    lexact2['&'] = LX2_BREAK;
  101. X    lextok2[','] = Comma;
  102. X    lextok2['*'] = Star;
  103. X    lextok2['?'] = Quest;
  104. X    lextok2['{'] = Inbrace;
  105. X    lextok2['['] = Inbrack;
  106. X    lextok2['$'] = String;
  107. }
  108. X
  109. /* initialize lexical state */
  110. X
  111. void lexinit() /**/
  112. {
  113. X    ignl = lsep = incmd = incond = infunc = inredir = incase =
  114. X        nocorrect = dbparens = alstat = lexstop = 0;
  115. X    if (isset(EXTENDEDGLOB))
  116. X        {
  117. X        lextok2['#'] = Pound;
  118. X        lextok2['^'] = Hat;
  119. X        }
  120. X    else
  121. X        {
  122. X        lextok2['#'] = '#'; 
  123. X        lextok2['^'] = '^';
  124. X        }
  125. }
  126. X
  127. int len = 0,bsiz = 256;
  128. char *bptr;
  129. X
  130. /* add a char to the string buffer */
  131. X
  132. void add(c) /**/
  133. int c;
  134. {
  135. X    *bptr++ = c;
  136. X    if (bsiz == ++len)
  137. X        {
  138. X        int newbsiz;
  139. X
  140. X        newbsiz = bsiz * 8;
  141. X        while (newbsiz < inbufct)
  142. X            newbsiz *= 2;
  143. X        bptr = len+(tokstr = hrealloc(tokstr,bsiz,newbsiz));
  144. X        bsiz = newbsiz;
  145. X        }
  146. }
  147. X
  148. int gettok() /**/
  149. {
  150. int bct = 0,pct = 0,brct = 0;
  151. int c,d,intpos = 1;
  152. int peekfd = -1,peek,incm;
  153. X
  154. beginning:
  155. X    hlastw = NULL;
  156. X    tokstr = NULL;
  157. X    incm = incmd || incond || inredir || incase;
  158. X    while (iblank(c = hgetc()) && !lexstop);
  159. X    isfirstln = 0;
  160. X    wordbeg = inbufct;
  161. X    hwbegin();
  162. X    hwaddc(c);
  163. X    if (dbparens)    /* handle ((...)) */
  164. X        {
  165. X        pct = 2;
  166. X        peek = STRING;
  167. X        len = dbparens = 0;
  168. X        bptr = tokstr = ncalloc(bsiz = 256);
  169. X        for (;;)
  170. X            {
  171. X            if (c == '(')
  172. X                pct++;
  173. X            else if (c == ')')
  174. X                pct--;
  175. X            else if (c == '\n')
  176. X                {
  177. X                zerr("parse error: )) expected",NULL,0);
  178. X                peek = LEXERR;
  179. X                return peek;
  180. X                }
  181. X            else if (c == '$')
  182. X                c = Qstring;
  183. X            if (pct >= 2)
  184. X                add(c);
  185. X            if (pct)
  186. X                c = hgetc();
  187. X            else
  188. X                break;
  189. X            }
  190. X        *bptr = '\0';
  191. X        yylval.str = tokstr;
  192. X        return peek;
  193. X        }
  194. X    if (idigit(c))    /* handle 1< foo */
  195. X        {
  196. X        d = hgetc();
  197. X        hungetc(d);
  198. X        if (d == '>' || d == '<')
  199. X            {
  200. X            peekfd = c-'0';
  201. X            c = hgetc();
  202. X            }
  203. X        }
  204. X
  205. X    /* chars in initial position in word */
  206. X
  207. X    if (c == hashchar && (!interact || unset(SHINSTDIN) || strin ||
  208. X            isset(INTERACTIVECOMMENTS)))
  209. X        {
  210. X        while ((c = hgetch()) != '\n' && !lexstop);
  211. X        if (c == '\n')
  212. X            peek = NEWLIN;
  213. X        else
  214. X            {
  215. X            peek = (errflag) ? LEXERR : ENDINPUT;
  216. X            errflag = 1;
  217. X            }
  218. X        return peek;
  219. X        }
  220. X    if (lexstop)
  221. X        return (errflag) ? LEXERR : ENDINPUT;
  222. X    switch (lexact1[(unsigned char) c])
  223. X        {
  224. X        case LX1_BKSLASH:
  225. X            d = hgetc();
  226. X            if (d == '\n')
  227. X                goto beginning;
  228. X            hungetc(d);
  229. X            break;
  230. X        case LX1_NEWLIN: return NEWLIN;
  231. X        case LX1_SEMI:
  232. X            d = hgetc();
  233. X            if (d != ';')
  234. X                {
  235. X                hungetc(d);
  236. X                return SEMI;
  237. X                }
  238. X            return DSEMI;
  239. X        case LX1_BANG:
  240. X            d = hgetc();
  241. X            hungetc(d);
  242. X            if (!inblank(d))
  243. X                break;
  244. X            if (!incm || incond)
  245. X                return BANG;
  246. X            break;
  247. X        case LX1_AMPER:
  248. X            d = hgetc();
  249. X            if (d != '&')
  250. X                {
  251. X                hungetc(d);
  252. X                return AMPER;
  253. X                }
  254. X            return DAMPER;
  255. X        case LX1_BAR:
  256. X            d = hgetc();
  257. X            if (d == '|')
  258. X                return DBAR;
  259. X            else if (d == '&')
  260. X                return BARAMP;
  261. X            hungetc(d);
  262. X            return BAR;
  263. X        case LX1_INPAR:
  264. X            d = hgetc();
  265. X            if (d == '(' && !incm)
  266. X                {
  267. X                yylval.str = tokstr = strdup("let");
  268. X                dbparens = 1;
  269. X                return STRING;
  270. X                }
  271. X            else if (d == ')')
  272. X                return INOUTPAR;
  273. X            hungetc(d);
  274. X            if (incm && !incond)
  275. X                break;
  276. X            return INPAR;
  277. X        case LX1_OUTPAR: return OUTPAR;
  278. X        case LX1_INBRACE: if (incm) break; return INBRACE;
  279. X        case LX1_OUTBRACE: return OUTBRACE;
  280. X        case LX1_INBRACK:
  281. X            if (incm)
  282. X                break;
  283. X            d = hgetc();
  284. X            if (d == '[')
  285. X                return DINBRACK;
  286. X            hungetc(d);
  287. X            break;
  288. X        case LX1_OUTBRACK:
  289. X            if (!incond)
  290. X                break;
  291. X            incond = 0;
  292. X            d = hgetc();
  293. X            if (d == ']')
  294. X                return DOUTBRACK;
  295. X            hungetc(d);
  296. X            break;
  297. X        case LX1_INANG:
  298. X            d = hgetc();
  299. X            if ((incmd && d == '(') || incase)
  300. X                {
  301. X                hungetc(d);
  302. X                break;
  303. X                }
  304. X            else if (d == '<')
  305. X                {
  306. X                int e = hgetc();
  307. X
  308. X                if (e == '(')
  309. X                    {
  310. X                    hungetc(e);
  311. X                    hungetc(d);
  312. X                    peek = INANG;
  313. X                    }
  314. X                else if (e == '<')
  315. X                    peek = TRINANG;
  316. X                else if (e == '-')
  317. X                    peek = DINANGDASH;
  318. X                else
  319. X                    {
  320. X                    hungetc(e);
  321. X                    peek = DINANG;
  322. X                    }
  323. X                }
  324. X            else if (d == '&')
  325. X                peek = INANGAMP;
  326. X            else
  327. X                {
  328. X                peek = INANG;
  329. X                hungetc(d);
  330. X                }
  331. X            yylval.fds.fd1 = peekfd;
  332. X            return peek;
  333. X        case LX1_OUTANG:
  334. X            d = hgetc();
  335. X            if (d == '(')
  336. X                {
  337. X                hungetc(d);
  338. X                break;
  339. X                }
  340. X            else if (d == '&')
  341. X                {
  342. X                d = hgetc();
  343. X                if (d == '!')
  344. X                    peek = OUTANGAMPBANG;
  345. X                else
  346. X                    {
  347. X                    hungetc(d);
  348. X                    peek = OUTANGAMP;
  349. X                    }
  350. X                }
  351. X            else if (d == '!')
  352. X                peek = OUTANGBANG;
  353. X            else if (d == '>')
  354. X                {
  355. X                d = hgetc();
  356. X                if (d == '&')
  357. X                    {
  358. X                    d = hgetc();
  359. X                    if (d == '!')
  360. X                        peek = DOUTANGAMPBANG;
  361. X                    else
  362. X                        {
  363. X                        hungetc(d);
  364. X                        peek = DOUTANGAMP;
  365. X                        }
  366. X                    }
  367. X                else if (d == '!')
  368. X                    peek = DOUTANGBANG;
  369. X                else if (d == '(')
  370. X                    {
  371. X                    hungetc(d);
  372. X                    hungetc('>');
  373. X                    peek = OUTANG;
  374. X                    }
  375. X                else
  376. X                    {
  377. X                    hungetc(d);
  378. X                    peek = DOUTANG;
  379. X                    }
  380. X                }
  381. X            else
  382. X                {
  383. X                hungetc(d);
  384. X                peek = OUTANG;
  385. X                }
  386. X            yylval.fds.fd1 = peekfd;
  387. X            return peek;
  388. X        }
  389. X
  390. X    /* we've started a string, now get the rest of it, performing
  391. X        tokenization */
  392. X
  393. X    peek = STRING;
  394. X    len = 0;
  395. X    bptr = tokstr = ncalloc(bsiz = 256);
  396. X    for(;;)
  397. X        {
  398. X        int act;
  399. X        int d;
  400. X        
  401. X        if (inblank(c))
  402. X            act = LX2_BREAK;
  403. X        else
  404. X            {
  405. X            act = lexact2[(unsigned char) c];
  406. X            c = lextok2[(unsigned char) c];
  407. X            }
  408. X        switch (act)
  409. X            {
  410. X            case LX2_BREAK: goto brk;
  411. X            case LX2_OUTPAR:
  412. X                if (!pct)
  413. X                    goto brk;
  414. X                c = Outpar;
  415. X                pct--;
  416. X                break;
  417. X            case LX2_BAR:
  418. X                if (!pct && !incase)
  419. X                    goto brk;
  420. X                c = Bar;
  421. X                break;
  422. X            case LX2_STRING:
  423. X                d = hgetc();
  424. X                if (d == '[')
  425. X                    {
  426. X                    add(String);
  427. X                    add(Inbrack);
  428. X                    while ((c = hgetc()) != ']' && !lexstop)
  429. X                        add(c);
  430. X                    c = Outbrack;
  431. X                    }
  432. X                else if (d == '(')
  433. X                    {
  434. X                    add(String);
  435. X                    skipcomm();
  436. X                    c = Outpar;
  437. X                    }
  438. X                else
  439. X                    hungetc(d);
  440. X                break;
  441. X            case LX2_INBRACK: brct++; break;
  442. X            case LX2_OUTBRACK:
  443. X                if (incond && !brct)
  444. X                    goto brk;
  445. X                brct--;
  446. X                c = Outbrack;
  447. X                break;
  448. X            case LX2_TILDE: if (intpos) c = Tilde; break;
  449. X            case LX2_INPAR:
  450. X                d = hgetc();
  451. X                hungetc(d);
  452. X                if (d == ')' || !incm)
  453. X                    goto brk;
  454. X                pct++;
  455. X                c = Inpar;
  456. X                break;
  457. X            case LX2_INBRACE: bct++; break;
  458. X            case LX2_OUTBRACE:
  459. X                if (!bct)
  460. X                    goto brk;
  461. X                bct--;
  462. X                c = Outbrace;
  463. X                break;
  464. X            case LX2_OUTANG:
  465. X                d = hgetc();
  466. X                if (d != '(')
  467. X                    {
  468. X                    hungetc(d);
  469. X                    goto brk;
  470. X                    }
  471. X                add(Outang);
  472. X                skipcomm();
  473. X                c = Outpar;
  474. X                break;
  475. X            case LX2_INANG:
  476. X                d = hgetc();
  477. X                if (!(idigit(d) || d == '-' || d == '>' || d == '(' || d == ')'))
  478. X                    {
  479. X                    hungetc(d);
  480. X                    goto brk;
  481. X                    }
  482. X                c = Inang;
  483. X                if (d == '(')
  484. X                    {
  485. X                    add(c);
  486. X                    skipcomm();
  487. X                    c = Outpar;
  488. X                    }
  489. X                else if (d == ')')
  490. X                    hungetc(d);
  491. X                else
  492. X                    {
  493. X                    add(c);
  494. X                    c = d;
  495. X                    while (c != '>' && !lexstop)
  496. X                        add(c),c = hgetc();
  497. X                    c = Outang;
  498. X                    }
  499. X                break;
  500. X            case LX2_EQUALS:
  501. X                if (intpos)
  502. X                    {
  503. X                    d = hgetc();
  504. X                    if (d != '(')
  505. X                        {
  506. X                        hungetc(d);
  507. X                        c = Equals;
  508. X                        }
  509. X                    else
  510. X                        {
  511. X                        add(Equals);
  512. X                        skipcomm();
  513. X                        c = Outpar;
  514. X                        }
  515. X                    }
  516. X                else if (peek != ENVSTRING && !incm)
  517. X                    {
  518. X                    d = hgetc();
  519. X                    if (d == '(' && !incm)
  520. X                        {
  521. X                        *bptr = '\0';
  522. X                        yylval.str = tokstr;
  523. X                        return ENVARRAY;
  524. X                        }
  525. X                    hungetc(d);
  526. X                    peek = ENVSTRING;
  527. X                    intpos = 2;
  528. X                    }
  529. X                break;
  530. X            case LX2_BKSLASH:
  531. X                c = hgetc();
  532. X                if (c == '\n')
  533. X                    {
  534. X                    c = hgetc();
  535. X                    continue;
  536. X                    }
  537. X                add(c);
  538. X                c = hgetc();
  539. X                continue;
  540. X            case LX2_QUOTE:
  541. X                add(Nularg);
  542. X
  543. X                /* we add the Nularg to prevent this:
  544. X
  545. X                echo $PA'TH'
  546. X
  547. X                from printing the path. */
  548. X
  549. X                while ((c = hgetc()) != '\'' && !lexstop)
  550. X                    add(c);
  551. X                c = Nularg;
  552. X                break;
  553. X            case LX2_DQUOTE:
  554. X                add(Nularg);
  555. X                while ((c = hgetc()) != '\"' && !lexstop)
  556. X                    if (c == '\\')
  557. X                        {
  558. X                        c = hgetc();
  559. X                        if (c != '\n')
  560. X                            {
  561. X                            if (c != '$' && c != '\\' && c != '\"' && c != '`')
  562. X                                add('\\');
  563. X                            add(c);
  564. X                            }
  565. X                        }
  566. X                    else
  567. X                        {
  568. X                        if (c == '$')
  569. X                            {
  570. X                            d = hgetc();
  571. X                            if (d == '(')
  572. X                                {
  573. X                                add(Qstring);
  574. X                                skipcomm();
  575. X                                c = Outpar;
  576. X                                }
  577. X                            else if (d == '[')
  578. X                                {
  579. X                                add(String);
  580. X                                add(Inbrack);
  581. X                                while ((c = hgetc()) != ']' && !lexstop)
  582. X                                    add(c);
  583. X                                c = Outbrack;
  584. X                                }
  585. X                            else
  586. X                                {
  587. X                                c = Qstring;
  588. X                                hungetc(d);
  589. X                                }
  590. X                            }
  591. X                        else if (c == '`')
  592. X                            c = Qtick;
  593. X                        add(c);
  594. X                        }
  595. X                c = Nularg;
  596. X                break;
  597. X            case LX2_BQUOTE:
  598. X                add(Tick);
  599. X                while ((c = hgetc()) != '`' && !lexstop)
  600. X                    if (c == '\\')
  601. X                        {
  602. X                        c = hgetc();
  603. X                        if (c != '\n')
  604. X                            {
  605. X                            if (c != '`' && c != '\\' && c != '$')
  606. X                                add('\\');
  607. X                            add(c);
  608. X                            }
  609. X                        }
  610. X                    else
  611. X                        {
  612. X                        if (c == '$')
  613. X                            c = String;
  614. X                        add(c);
  615. X                        }
  616. X                c = Tick;
  617. X                break;
  618. X            }
  619. X        add(c);
  620. X        c = hgetc();
  621. X        if (intpos)
  622. X            intpos--;
  623. X        if (lexstop)
  624. X            break;
  625. X        }
  626. brk:
  627. X    hungetc(c);
  628. X    *bptr = '\0';
  629. X    yylval.str = tokstr;
  630. X    return peek;
  631. }
  632. X
  633. /* expand aliases, perhaps */
  634. X
  635. int exalias(pk) /**/
  636. int *pk;
  637. {
  638. struct alias *an;
  639. char *s,*t;
  640. int ic;
  641. X
  642. X    s = yytext = hwadd();
  643. X    for (t = s; *t && *t != HISTSPACE; t++);
  644. X    if (!*t)
  645. X        t = NULL;
  646. X    else
  647. X        *t = '\0';
  648. X    ic = incmd || incond || inredir || incase;
  649. X    if (interact && isset(SHINSTDIN) && !strin && !incase && *pk == STRING &&
  650. X        (isset(CORRECTALL) || (isset(CORRECT) && !ic)) && !nocorrect)
  651. X            spckword(&yylval.str,&s,ic,1);
  652. X    if (zleparse && !alstackind)
  653. X        gotword(s);
  654. X    an = gethnode(s,aliastab);
  655. X    if (t)
  656. X        *t = HISTSPACE;
  657. X    if (alstackind != MAXAL && an && !an->inuse)
  658. X        if (!(an->cmd && ic && alstat != ALSTAT_MORE))
  659. X            {
  660. X            if (an->cmd < 0)
  661. X                {
  662. X                *pk = DO-an->cmd-1;
  663. X                return 0;
  664. X                }
  665. X            else
  666. X                {
  667. X                an->inuse = 1;
  668. X                hungets(ALPOPS);
  669. X                hungets((alstack[alstackind++] = an)->text);
  670. X                alstat = 0;
  671. X                return 1;
  672. X                }
  673. X            }
  674. X        else if (incase && DO-an->cmd-1 == ESAC)
  675. X            *pk = ESAC;
  676. X    return 0;
  677. }
  678. X
  679. /* skip (...) */
  680. X
  681. void skipcomm() /**/
  682. {
  683. int pct = 1,c;
  684. X
  685. X    c = Inpar;
  686. X    do
  687. X        {
  688. X        add(c);
  689. X        c = hgetc();
  690. X        if (itok(c) || c == EOF)
  691. X            break;
  692. X        else if (c == '(') pct++;
  693. X        else if (c == ')') pct--;
  694. X        else if (c == '\\')
  695. X            {
  696. X            add(c);
  697. X            c = hgetc();
  698. X            }
  699. X        else if (c == '\'')
  700. X            {
  701. X            add(c);
  702. X            while ((c = hgetc()) != '\'' && !lexstop)
  703. X                add(c);
  704. X            }
  705. X        else if (c == '\"')
  706. X            {
  707. X            add(c);
  708. X            while ((c = hgetc()) != '\"' && !lexstop)
  709. X                if (c == '\\')
  710. X                    {
  711. X                    add(c);
  712. X                    add(hgetc());
  713. X                    }
  714. X                else add(c);
  715. X            }
  716. X        else if (c == '`')
  717. X            {
  718. X            add(c);
  719. X            while ((c = hgetc()) != '`' && !lexstop)
  720. X                if (c == '\\') add(c), add(hgetc());
  721. X                else add(c);
  722. X            }
  723. X        }
  724. X    while(pct);
  725. }
  726. X
  727. SHAR_EOF
  728. echo 'File zsh2.00/src/lex.c is complete' &&
  729. chmod 0644 zsh2.00/src/lex.c ||
  730. echo 'restore of zsh2.00/src/lex.c failed'
  731. Wc_c="`wc -c < 'zsh2.00/src/lex.c'`"
  732. test 15255 -eq "$Wc_c" ||
  733.     echo 'zsh2.00/src/lex.c: original size 15255, current size' "$Wc_c"
  734. # ============= zsh2.00/src/lex.pro ==============
  735. echo 'x - extracting zsh2.00/src/lex.pro (Text)'
  736. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/lex.pro' &&
  737. void lexsave DCLPROTO((void));
  738. void lexrestore DCLPROTO((void));
  739. int yylex DCLPROTO((void));
  740. void initlextabs DCLPROTO((void));
  741. void lexinit DCLPROTO((void));
  742. void add DCLPROTO((int c));
  743. int gettok DCLPROTO((void));
  744. int exalias DCLPROTO((int *pk));
  745. void skipcomm DCLPROTO((void));
  746. SHAR_EOF
  747. chmod 0644 zsh2.00/src/lex.pro ||
  748. echo 'restore of zsh2.00/src/lex.pro failed'
  749. Wc_c="`wc -c < 'zsh2.00/src/lex.pro'`"
  750. test 281 -eq "$Wc_c" ||
  751.     echo 'zsh2.00/src/lex.pro: original size 281, current size' "$Wc_c"
  752. # ============= zsh2.00/src/mem.c ==============
  753. echo 'x - extracting zsh2.00/src/mem.c (Text)'
  754. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/mem.c' &&
  755. /*
  756. X
  757. X    mem.c - memory management
  758. X
  759. X    This file is part of zsh, the Z shell.
  760. X
  761. X    zsh is free software; no one can prevent you from reading the source
  762. X   code, or giving it to someone else.
  763. X
  764. X   This file is copyrighted under the GNU General Public License, which
  765. X   can be found in the file called COPYING.
  766. X
  767. X   Copyright (C) 1990, 1991 Paul Falstad
  768. X
  769. X   zsh is distributed in the hope that it will be useful, but
  770. X   WITHOUT ANY WARRANTY.  No author or distributor accepts
  771. X   responsibility to anyone for the consequences of using it or for
  772. X   whether it serves any particular purpose or works at all, unless he
  773. X   says so in writing.  Refer to the GNU General Public License
  774. X   for full details.
  775. X
  776. X   Everyone is granted permission to copy, modify and redistribute
  777. X   zsh, but only under the conditions described in the GNU General Public
  778. X   License.   A copy of this license is supposed to have been given to you
  779. X   along with zsh so you can know your rights and responsibilities.
  780. X   It should be in a file named COPYING.
  781. X
  782. X   Among other things, the copyright notice and this notice must be
  783. X   preserved on all copies.
  784. X
  785. */
  786. X
  787. #include "zsh.h"
  788. #define HEAPSIZE 8192
  789. X
  790. /*
  791. X
  792. X    There are two ways to allocate memory in zsh.  The first way is
  793. X    to call zalloc/zcalloc, which call malloc/calloc directly.  It
  794. X    is legal to call realloc() or free() on memory allocated this way.
  795. X    The second way is to call halloc/hcalloc, which allocates memory
  796. X    from one of the memory pools on the heap stack.  A pool can be
  797. X    created by calling pushheap(), and destroyed by calling popheap().
  798. X    To free the memory in the pool without destroying it, call
  799. X    freeheap(); this is equivalent to { popheap(); pushheap(); }
  800. X    Memory allocated in this way does not have to be freed explicitly;
  801. X    it will all be freed when the pool is destroyed.  In fact,
  802. X    attempting to free this memory may result in a core dump.
  803. X    The pair of pointers ncalloc and alloc may point to either
  804. X    zalloc & zcalloc or halloc & hcalloc; permalloc() sets them to the
  805. X    former, and heapalloc() sets them to the latter. This can be useful.
  806. X    For example, the dupstruct() routine duplicates a syntax tree,
  807. X    allocating the new memory for the tree using alloc().  If you want
  808. X    to duplicate a structure for a one-time use (i.e. to execute the list
  809. X    in a for loop), call heapalloc(), then dupstruct().  If you want
  810. X    to duplicate a structure in order to preserve it (i.e. a function
  811. X    definition), call permalloc(), then dupstruct().
  812. X
  813. */
  814. X
  815. /* initialize heap stack */
  816. X
  817. void meminit() /**/
  818. {
  819. X    permalloc();
  820. X    heaplist = newlist();
  821. X    pushheap();
  822. }
  823. X
  824. /* set default allocation to heap stack */
  825. X
  826. void heapalloc() /**/
  827. {
  828. X    alloc = hcalloc;
  829. X    ncalloc = halloc;
  830. X    useheap = 1;
  831. }
  832. X
  833. static vptr (*lastcalloc) DCLPROTO((int));
  834. static vptr (*lastncalloc) DCLPROTO((int));
  835. X
  836. /* set default allocation to malloc() */
  837. X
  838. void permalloc() /**/
  839. {
  840. X    lastcalloc = alloc;
  841. X    lastncalloc = ncalloc;
  842. X    alloc = zcalloc;
  843. X    ncalloc = zalloc;
  844. X    useheap = 0;
  845. }
  846. X
  847. /* reset previous default allocation */
  848. X
  849. void lastalloc() /**/
  850. {
  851. X    alloc = lastcalloc;
  852. X    ncalloc = lastncalloc;
  853. }
  854. X
  855. struct heap {
  856. X    char *pool,*ptr;
  857. X    int free;
  858. X    struct heap *next;
  859. X    };
  860. X
  861. /* create a memory pool */
  862. X
  863. void pushheap() /**/
  864. {
  865. Heap h;
  866. X
  867. X    h = (Heap) zalloc(sizeof *h);
  868. X    h->pool = h->ptr = zalloc(HEAPSIZE);
  869. X    h->free = HEAPSIZE;
  870. X    h->next = NULL;
  871. X    permalloc();
  872. X    pushnode(heaplist,h);
  873. X    lastalloc();
  874. }
  875. X
  876. /* reset a memory pool */
  877. X
  878. void freeheap() /**/
  879. {
  880. Heap h = (Heap) peekfirst(heaplist);
  881. X
  882. X    freeh(h->next);
  883. X    h->free += (h->ptr-h->pool);
  884. X    h->ptr = h->pool;
  885. }
  886. X
  887. /* destroy a memory pool */
  888. X
  889. void popheap() /**/
  890. {
  891. Heap h = (Heap) getnode(heaplist);
  892. X
  893. X    freeh(h);
  894. }
  895. X
  896. void freeh(h) /**/
  897. Heap h;
  898. {
  899. X    if (h)
  900. X        {
  901. X        freeh(h->next);
  902. X        free(h->pool);
  903. X        free(h);
  904. X        }
  905. }
  906. X
  907. /* allocate memory from the current memory pool */
  908. X
  909. vptr halloc(size) /**/
  910. int size;
  911. {
  912. Heap h = (Heap) peekfirst(heaplist),h2;
  913. char *ret;
  914. X
  915. X    size = (size|7)+1;
  916. X    while (h && h->free-size < 0)
  917. X        h = h->next;
  918. X    if (!h)
  919. X        {
  920. X        h2 = (Heap) zalloc(sizeof *h2);
  921. X        h2->pool = h2->ptr = zalloc(h2->free = 
  922. X            (size < HEAPSIZE) ? HEAPSIZE : (size|(HEAPSIZE-1))+1);
  923. X        h2->next = h;
  924. X        setdata(firstnode(heaplist),(Heap) h2);
  925. X        h = h2;
  926. X        }
  927. X    h->free -= size;
  928. X    ret = h->ptr;
  929. X    h->ptr += size;
  930. X    return ret;
  931. }
  932. X
  933. /* allocate memory from the current memory pool and clear it */
  934. X
  935. vptr hcalloc(size) /**/
  936. int size;
  937. {
  938. vptr ptr;
  939. X
  940. X    ptr = halloc(size);
  941. X    bzero(ptr,size);
  942. X    return ptr;
  943. }
  944. X
  945. vptr hrealloc(p,old,new) /**/
  946. char *p;int old;int new;
  947. {
  948. char *ptr;
  949. X
  950. X    ptr = halloc(new);
  951. X    memcpy(ptr,p,old);
  952. X    return ptr;
  953. }
  954. X
  955. /* allocate permanent memory */
  956. X
  957. vptr zalloc(l) /**/
  958. int l;
  959. {
  960. vptr z;
  961. X    if (!l)
  962. X        l = 1;
  963. X    if (!(z = malloc(l)))
  964. X        {
  965. X        zerr("fatal error: out of memory",NULL,0);
  966. X        exit(1);
  967. X        }
  968. X    return z;
  969. }
  970. X
  971. vptr zcalloc(size) /**/
  972. int size;
  973. {
  974. vptr ptr;
  975. X
  976. X    ptr = zalloc(size);
  977. X    bzero(ptr,size);
  978. X    return ptr;
  979. }
  980. X
  981. char *strdup(s) /**/
  982. char *s;
  983. {
  984. char *t;
  985. X
  986. X    if (!s)
  987. X        return NULL;
  988. X    t = ncalloc(strlen(s)+1);
  989. X    strcpy(t,s);
  990. X    return t;
  991. }
  992. X
  993. char *ztrdup(s) /**/
  994. char *s;
  995. {
  996. char *t;
  997. X
  998. X    if (!s)
  999. X        return NULL;
  1000. X    t = zalloc(strlen(s)+1);
  1001. X    strcpy(t,s);
  1002. X    return t;
  1003. }
  1004. X
  1005. SHAR_EOF
  1006. chmod 0644 zsh2.00/src/mem.c ||
  1007. echo 'restore of zsh2.00/src/mem.c failed'
  1008. Wc_c="`wc -c < 'zsh2.00/src/mem.c'`"
  1009. test 5001 -eq "$Wc_c" ||
  1010.     echo 'zsh2.00/src/mem.c: original size 5001, current size' "$Wc_c"
  1011. # ============= zsh2.00/src/mem.pro ==============
  1012. echo 'x - extracting zsh2.00/src/mem.pro (Text)'
  1013. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/mem.pro' &&
  1014. void meminit DCLPROTO((void));
  1015. void heapalloc DCLPROTO((void));
  1016. void permalloc DCLPROTO((void));
  1017. void lastalloc DCLPROTO((void));
  1018. void pushheap DCLPROTO((void));
  1019. void freeheap DCLPROTO((void));
  1020. void popheap DCLPROTO((void));
  1021. void freeh DCLPROTO((Heap h));
  1022. vptr halloc DCLPROTO((int size));
  1023. vptr hcalloc DCLPROTO((int size));
  1024. vptr hrealloc DCLPROTO((char *p,int old,int new));
  1025. vptr zalloc DCLPROTO((int l));
  1026. vptr zcalloc DCLPROTO((int size));
  1027. char *strdup DCLPROTO((char *s));
  1028. char *ztrdup DCLPROTO((char *s));
  1029. SHAR_EOF
  1030. chmod 0644 zsh2.00/src/mem.pro ||
  1031. echo 'restore of zsh2.00/src/mem.pro failed'
  1032. Wc_c="`wc -c < 'zsh2.00/src/mem.pro'`"
  1033. test 510 -eq "$Wc_c" ||
  1034.     echo 'zsh2.00/src/mem.pro: original size 510, current size' "$Wc_c"
  1035. # ============= zsh2.00/src/subst.pro ==============
  1036. echo 'x - extracting zsh2.00/src/subst.pro (Text)'
  1037. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/subst.pro' &&
  1038. void prefork DCLPROTO((Lklist list));
  1039. void postfork DCLPROTO((Lklist list,int doglob));
  1040. void singsub DCLPROTO((char **s));
  1041. vptr nstrdup DCLPROTO((vptr s));
  1042. char *dynread DCLPROTO((int stop));
  1043. int filesub DCLPROTO((char **namptr));
  1044. char *gethome DCLPROTO((char *user,int len));
  1045. void commsubst DCLPROTO((Lklist l,Lknode n,char *str3,char *str,int qt));
  1046. void paramsubst DCLPROTO((Lklist l,Lknode n,char *aptr,char *bptr,int qt));
  1047. void arithsubst DCLPROTO((vptr *aptr,char **bptr));
  1048. void modify DCLPROTO((char **str,char **ptr));
  1049. char *dstackent DCLPROTO((int val));
  1050. struct alias *mkanode DCLPROTO((char *txt,int cmflag));
  1051. SHAR_EOF
  1052. chmod 0644 zsh2.00/src/subst.pro ||
  1053. echo 'restore of zsh2.00/src/subst.pro failed'
  1054. Wc_c="`wc -c < 'zsh2.00/src/subst.pro'`"
  1055. test 619 -eq "$Wc_c" ||
  1056.     echo 'zsh2.00/src/subst.pro: original size 619, current size' "$Wc_c"
  1057. # ============= zsh2.00/src/table.pro ==============
  1058. echo 'x - extracting zsh2.00/src/table.pro (Text)'
  1059. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/table.pro' &&
  1060. Lklist newlist DCLPROTO((void));
  1061. Hashtab newhtable DCLPROTO((int size));
  1062. int hasher DCLPROTO((char *s));
  1063. void Addhnode DCLPROTO((char *nam,vptr dat,Hashtab ht,FFunc freefunc,int canfree));
  1064. void expandhtab DCLPROTO((Hashtab ht));
  1065. vptr gethnode DCLPROTO((char *nam,Hashtab ht));
  1066. void freehtab DCLPROTO((Hashtab ht,FFunc freefunc));
  1067. vptr remhnode DCLPROTO((char *nam,Hashtab ht));
  1068. void insnode DCLPROTO((Lklist list,Lknode llast,vptr dat));
  1069. void addnodeinorder DCLPROTO((Lklist x, char *dat));
  1070. vptr remnode DCLPROTO((Lklist list,Lknode nd));
  1071. vptr uremnode DCLPROTO((Lklist list,Lknode nd));
  1072. void chuck DCLPROTO((char *str));
  1073. vptr getnode DCLPROTO((Lklist list));
  1074. vptr ugetnode DCLPROTO((Lklist list));
  1075. void freetable DCLPROTO((Lklist tab,FFunc freefunc));
  1076. char *ztrstr DCLPROTO((char *s,char *t));
  1077. void inslist DCLPROTO((Lklist l,Lknode where,Lklist x));
  1078. int countnodes DCLPROTO((Lklist x));
  1079. SHAR_EOF
  1080. chmod 0644 zsh2.00/src/table.pro ||
  1081. echo 'restore of zsh2.00/src/table.pro failed'
  1082. Wc_c="`wc -c < 'zsh2.00/src/table.pro'`"
  1083. test 889 -eq "$Wc_c" ||
  1084.     echo 'zsh2.00/src/table.pro: original size 889, current size' "$Wc_c"
  1085. # ============= zsh2.00/src/utils.pro ==============
  1086. echo 'x - extracting zsh2.00/src/utils.pro (Text)'
  1087. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/utils.pro' &&
  1088. int source DCLPROTO((char *s));
  1089. void sourcehome DCLPROTO((char *s));
  1090. void zerrnam DCLPROTO((char *cmd, char *fmt, char *str,int num));
  1091. void zerr DCLPROTO((char *fmt, char *str,int num));
  1092. void niceputc DCLPROTO((int c,FILE *f));
  1093. void intr DCLPROTO((void));
  1094. void noholdintr DCLPROTO((void));
  1095. void holdintr DCLPROTO((void));
  1096. char *fgetline DCLPROTO((char *buf,int len,FILE *in));
  1097. char *findcwd DCLPROTO((char *s));
  1098. char *fixcwd DCLPROTO((char *s));
  1099. int ispwd DCLPROTO((char *s));
  1100. char *xsymlink DCLPROTO((char *s));
  1101. char **slashsplit DCLPROTO((char *s));
  1102. int islink DCLPROTO((char *s));
  1103. int xsymlinks DCLPROTO((char *s));
  1104. void printdir DCLPROTO((char *s));
  1105. int finddir DCLPROTO((char *s));
  1106. void adduserdir DCLPROTO((char *s,char *t));
  1107. int dircmp DCLPROTO((char *s,char *t));
  1108. int ddifftime DCLPROTO((time_t t1,time_t t2));
  1109. void scanjobs DCLPROTO((void));
  1110. void preprompt DCLPROTO((void));
  1111. int arrlen DCLPROTO((char **s));
  1112. void checkmailpath DCLPROTO((char **s));
  1113. void saveoldfuncs DCLPROTO((char *x,Cmdnam y));
  1114. void newcmdnamtab DCLPROTO((void));
  1115. void freecmdnam DCLPROTO((vptr a));
  1116. void freestr DCLPROTO((vptr a));
  1117. void freeanode DCLPROTO((vptr a));
  1118. void freepm DCLPROTO((vptr a));
  1119. void restoretty DCLPROTO((void));
  1120. void gettyinfo DCLPROTO((struct ttyinfo *ti));
  1121. void settyinfo DCLPROTO((struct ttyinfo *ti));
  1122. void sanetty DCLPROTO((struct ttyinfo *ti));
  1123. void adjustwinsize DCLPROTO((void));
  1124. int zyztem DCLPROTO((char *s,char *t));
  1125. int waitfork DCLPROTO((void));
  1126. int movefd DCLPROTO((int fd));
  1127. void redup DCLPROTO((int x,int y));
  1128. void settrap DCLPROTO((int t0,List l));
  1129. void unsettrap DCLPROTO((int t0));
  1130. void dotrap DCLPROTO((int sig));
  1131. void strucpy DCLPROTO((char **s,char *t));
  1132. void struncpy DCLPROTO((char **s,char *t,int n));
  1133. void checkrmall DCLPROTO((void));
  1134. int getquery DCLPROTO((void));
  1135. void spscan DCLPROTO((char *s,char *junk));
  1136. void spckword DCLPROTO((char **s,char **s2,int cmd,int ask));
  1137. int ztrftime DCLPROTO((char *buf,int bufsize,char *fmt,struct tm *tm));
  1138. char *join DCLPROTO((char **arr,int delim));
  1139. char *spacejoin DCLPROTO((char **s));
  1140. char *colonjoin DCLPROTO((char **s));
  1141. char **colonsplit DCLPROTO((char *s));
  1142. char **spacesplit DCLPROTO((char *s));
  1143. List getshfunc DCLPROTO((char *nam));
  1144. vptr allocnode DCLPROTO((int type));
  1145. vptr dupstruct DCLPROTO((vptr a));
  1146. void freestruct DCLPROTO((vptr a));
  1147. Lklist duplist DCLPROTO((Lklist l,VFunc func));
  1148. char **mkarray DCLPROTO((char *s));
  1149. void feep DCLPROTO((void));
  1150. void freearray DCLPROTO((char **s));
  1151. int equalsplit DCLPROTO((char *s,char **t));
  1152. void simplifyright DCLPROTO((List l));
  1153. void inittyptab DCLPROTO((void));
  1154. char **arrdup DCLPROTO((char **s));
  1155. char *spname  DCLPROTO((char *oldname));
  1156. int mindist DCLPROTO((char *dir,char *guess,char *best));
  1157. int spdist DCLPROTO((char *s,char *t,int thresh));
  1158. char *zgetenv DCLPROTO((char *s));
  1159. int tulower DCLPROTO((int c));
  1160. int tuupper DCLPROTO((int c));
  1161. SHAR_EOF
  1162. chmod 0644 zsh2.00/src/utils.pro ||
  1163. echo 'restore of zsh2.00/src/utils.pro failed'
  1164. Wc_c="`wc -c < 'zsh2.00/src/utils.pro'`"
  1165. test 2865 -eq "$Wc_c" ||
  1166.     echo 'zsh2.00/src/utils.pro: original size 2865, current size' "$Wc_c"
  1167. # ============= zsh2.00/src/y.tab.h ==============
  1168. echo 'x - extracting zsh2.00/src/y.tab.h (Text)'
  1169. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/y.tab.h' &&
  1170. X
  1171. typedef union  {
  1172. X    Pline Pline;
  1173. X    List List;
  1174. X    Sublist Sublist;
  1175. X    struct cmd *Comm;
  1176. X    struct redir *Fnode;
  1177. X    struct cond *Cond;
  1178. X    struct forcmd *Fornode;
  1179. X    struct casecmd *Casenode;
  1180. X    struct ifcmd *Ifnode;
  1181. X    struct whilecmd *Whilenode;
  1182. X    struct repeatcmd *Repeatnode;
  1183. X    struct varasg *Varnode;
  1184. X    Lklist Table;
  1185. X    struct fdpair fds;
  1186. X    char *str;
  1187. X    int value;
  1188. } YYSTYPE;
  1189. extern YYSTYPE yylval;
  1190. # define DOITNOW 257
  1191. # define EMPTY 258
  1192. # define LEXERR 259
  1193. # define SEPER 260
  1194. # define NEWLIN 261
  1195. # define SEMI 262
  1196. # define DSEMI 263
  1197. # define AMPER 264
  1198. # define INPAR 265
  1199. # define INBRACE 266
  1200. # define OUTPAR 267
  1201. # define DBAR 268
  1202. # define DAMPER 269
  1203. # define BANG 270
  1204. # define OUTBRACE 271
  1205. # define OUTANG 272
  1206. # define OUTANGBANG 273
  1207. # define DOUTANG 274
  1208. # define DOUTANGBANG 275
  1209. # define INANG 276
  1210. # define DINANG 277
  1211. # define INANGAMP 278
  1212. # define OUTANGAMP 279
  1213. # define OUTANGAMPBANG 280
  1214. # define DOUTANGAMP 281
  1215. # define DOUTANGAMPBANG 282
  1216. # define TRINANG 283
  1217. # define DINANGDASH 284
  1218. # define BAR 285
  1219. # define BARAMP 286
  1220. # define DINBRACK 287
  1221. # define DOUTBRACK 288
  1222. # define STRING 289
  1223. # define ENVSTRING 290
  1224. # define ENVARRAY 291
  1225. # define ENDINPUT 292
  1226. # define INOUTPAR 293
  1227. # define DO 294
  1228. # define DONE 295
  1229. # define ESAC 296
  1230. # define THEN 297
  1231. # define ELIF 298
  1232. # define ELSE 299
  1233. # define FI 300
  1234. # define FOR 301
  1235. # define CASE 302
  1236. # define IF 303
  1237. # define WHILE 304
  1238. # define FUNC 305
  1239. # define REPEAT 306
  1240. # define TIME 307
  1241. # define UNTIL 308
  1242. # define EXEC 309
  1243. # define COMMAND 310
  1244. # define SELECT 311
  1245. # define COPROC 312
  1246. # define NOGLOB 313
  1247. # define DASH 314
  1248. # define DOITLATER 315
  1249. SHAR_EOF
  1250. chmod 0644 zsh2.00/src/y.tab.h ||
  1251. echo 'restore of zsh2.00/src/y.tab.h failed'
  1252. Wc_c="`wc -c < 'zsh2.00/src/y.tab.h'`"
  1253. test 1565 -eq "$Wc_c" ||
  1254.     echo 'zsh2.00/src/y.tab.h: original size 1565, current size' "$Wc_c"
  1255. # ============= zsh2.00/src/zle_main.pro ==============
  1256. echo 'x - extracting zsh2.00/src/zle_main.pro (Text)'
  1257. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/zle_main.pro' &&
  1258. void setterm DCLPROTO((void));
  1259. void unsetterm DCLPROTO((void));
  1260. void ungetkey DCLPROTO((int ch));
  1261. void ungetkeys DCLPROTO((char *s,int len));
  1262. unsigned int getkey DCLPROTO((int tmok));
  1263. char *zleread DCLPROTO((char *ppt,char *ppt2,int plen));
  1264. int getkeycmd DCLPROTO((void));
  1265. void sendstring DCLPROTO((void));
  1266. Key makefunckey DCLPROTO((int fun));
  1267. void initxbindtab DCLPROTO((void));
  1268. char *getkeystring DCLPROTO((char *s,int *len));
  1269. void printbind DCLPROTO((char *s,int len));
  1270. void printbinding DCLPROTO((char *str,Key k));
  1271. int bin_bindkey DCLPROTO((char *name,char **argv,char *ops,int junc));
  1272. void freekey DCLPROTO((vptr x));
  1273. SHAR_EOF
  1274. chmod 0644 zsh2.00/src/zle_main.pro ||
  1275. echo 'restore of zsh2.00/src/zle_main.pro failed'
  1276. Wc_c="`wc -c < 'zsh2.00/src/zle_main.pro'`"
  1277. test 624 -eq "$Wc_c" ||
  1278.     echo 'zsh2.00/src/zle_main.pro: original size 624, current size' "$Wc_c"
  1279. # ============= zsh2.00/src/zle_tricky.c ==============
  1280. echo 'x - extracting zsh2.00/src/zle_tricky.c (Text)'
  1281. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/zle_tricky.c' &&
  1282. /*
  1283. X
  1284. X    zle_tricky.c - expansion and completion
  1285. X
  1286. X    This file is part of zsh, the Z shell.
  1287. X
  1288. X    zsh is free software; no one can prevent you from reading the source
  1289. X   code, or giving it to someone else.
  1290. X
  1291. X   This file is copyrighted under the GNU General Public License, which
  1292. X   can be found in the file called COPYING.
  1293. X
  1294. X   Copyright (C) 1990, 1991 Paul Falstad
  1295. X
  1296. X   zsh is distributed in the hope that it will be useful, but
  1297. X   WITHOUT ANY WARRANTY.  No author or distributor accepts
  1298. X   responsibility to anyone for the consequences of using it or for
  1299. X   whether it serves any particular purpose or works at all, unless he
  1300. X   says so in writing.  Refer to the GNU General Public License
  1301. X   for full details.
  1302. X
  1303. X   Everyone is granted permission to copy, modify and redistribute
  1304. X   zsh, but only under the conditions described in the GNU General Public
  1305. X   License.   A copy of this license is supposed to have been given to you
  1306. X   along with zsh so you can know your rights and responsibilities.
  1307. X   It should be in a file named COPYING.
  1308. X
  1309. X   Among other things, the copyright notice and this notice must be
  1310. X   preserved on all copies.
  1311. X
  1312. */
  1313. X
  1314. #define ZLE
  1315. #include "zsh.h"
  1316. #include "y.tab.h"
  1317. #include <sys/dir.h>
  1318. #include    <pwd.h>
  1319. X
  1320. static int we,wb,usemenu,useglob;
  1321. X
  1322. static int menub,menue,menuw;
  1323. static Lklist menulist;
  1324. static Lknode menunode;
  1325. X
  1326. #define INCMD (incmd||incond||inredir||incase)
  1327. X
  1328. #define inststr(X) inststrlen((X),-1)
  1329. X
  1330. /* strncpy() does not null always terminate the string, pem */
  1331. #define STRNCPY(s1, s2, len)    (strncpy(s1, s2, len), s1[len] = '\0')
  1332. X
  1333. int usetab() /**/
  1334. {
  1335. char *s = line+cs-1;
  1336. X
  1337. X    for (; s >= line && *s != '\n'; s--)
  1338. X        if (*s != '\t' && *s != ' ')
  1339. X            return 0;
  1340. X    return 1;
  1341. }
  1342. X
  1343. #define COMP_COMPLETE 0
  1344. #define COMP_LIST_COMPLETE 1
  1345. #define COMP_SPELL 2
  1346. #define COMP_EXPAND 3
  1347. #define COMP_EXPAND_COMPLETE 4
  1348. #define COMP_LIST_EXPAND 5
  1349. #define COMP_ISEXPAND(X) ((X) >= COMP_EXPAND)
  1350. X
  1351. void completeword() /**/
  1352. {
  1353. X    usemenu = isset(MENUCOMPLETE) || (useglob = isset(GLOBCOMPLETE));
  1354. X    if (c == '\t' && usetab())
  1355. X        selfinsert();
  1356. X    else
  1357. X        docomplete(COMP_COMPLETE);
  1358. }
  1359. X
  1360. void menucompleteword() /**/
  1361. {
  1362. X    usemenu = 1; useglob = isset(GLOBCOMPLETE);
  1363. X    if (c == '\t' && usetab())
  1364. X        selfinsert();
  1365. X    else
  1366. X        docomplete(COMP_COMPLETE);
  1367. }
  1368. X
  1369. void listchoices() /**/
  1370. {
  1371. X    usemenu = isset(MENUCOMPLETE) || (useglob = isset(GLOBCOMPLETE));
  1372. X    docomplete(COMP_LIST_COMPLETE);
  1373. }
  1374. X
  1375. void spellword() /**/
  1376. {
  1377. X    usemenu = useglob = 0;
  1378. X    docomplete(COMP_SPELL);
  1379. }
  1380. X
  1381. void deletecharorlist() /**/
  1382. {
  1383. X    usemenu = isset(MENUCOMPLETE) || (useglob = isset(GLOBCOMPLETE));
  1384. X    if (cs != ll)
  1385. X        deletechar();
  1386. X    else
  1387. X        docomplete(COMP_LIST_COMPLETE);
  1388. }
  1389. X
  1390. void expandword() /**/
  1391. {
  1392. X    usemenu = useglob = 0;
  1393. X    if (c == '\t' && usetab())
  1394. X        selfinsert();
  1395. X    else
  1396. X        docomplete(COMP_EXPAND);
  1397. }
  1398. X
  1399. void expandorcomplete() /**/
  1400. {
  1401. X    usemenu = isset(MENUCOMPLETE) || (useglob = isset(GLOBCOMPLETE));
  1402. X    if (c == '\t' && usetab())
  1403. X        selfinsert();
  1404. X    else
  1405. X        docomplete(COMP_EXPAND_COMPLETE);
  1406. }
  1407. X
  1408. void menuexpandorcomplete() /**/
  1409. {
  1410. X    usemenu = 1; useglob = isset(GLOBCOMPLETE);
  1411. X    if (c == '\t' && usetab())
  1412. X        selfinsert();
  1413. X    else
  1414. X        docomplete(COMP_EXPAND_COMPLETE);
  1415. }
  1416. X
  1417. void listexpand() /**/
  1418. {
  1419. X    usemenu = isset(MENUCOMPLETE); useglob = isset(GLOBCOMPLETE);
  1420. X    docomplete(COMP_LIST_EXPAND);
  1421. }
  1422. X
  1423. void reversemenucomplete() /**/
  1424. {
  1425. char *s;
  1426. X
  1427. X    if (!menucmp)
  1428. X        menucompleteword();    /* better than just feep'ing, pem */
  1429. X    if (!menucmp) return;
  1430. X    cs = menub;
  1431. X    foredel(menue-menub);
  1432. X    if (menunode == firstnode(menulist))
  1433. X        menunode = lastnode(menulist);
  1434. X    else
  1435. X        menunode = prevnode(menunode);
  1436. X    inststr(s = menunode->dat);
  1437. X    menue = cs;
  1438. }
  1439. X
  1440. /*
  1441. X * Accepts the current completion and starts a new arg,
  1442. X * with the next completions. This gives you a way to accept
  1443. X * several selections from the list of matches.
  1444. X */
  1445. void acceptandmenucomplete() /**/
  1446. {
  1447. int t0,t1;
  1448. X
  1449. X    if (!menucmp)
  1450. X        {
  1451. X        feep();
  1452. X        return;
  1453. X        }
  1454. X    spaceinline(1);
  1455. X    line[cs++] = ' ';
  1456. X    spaceinline(menub-menuw);
  1457. X    t1 = cs;
  1458. X    for (t0 = menuw; t0 != menub; t0++)
  1459. X        line[cs++] = line[t0];
  1460. X    menue = menub = cs;
  1461. X    menuw = t1;
  1462. X    menucompleteword();
  1463. }
  1464. X
  1465. void docomplete(lst) /**/
  1466. int lst;
  1467. {
  1468. int t0,lincmd = INCMD;
  1469. char *s;
  1470. X
  1471. X    if (menucmp)
  1472. X        {
  1473. X        if (lst == COMP_LIST_COMPLETE)
  1474. X            {
  1475. X            listmatches(menulist, NULL);
  1476. X            return;
  1477. X            }
  1478. X        cs = menub;
  1479. X        foredel(menue-menub);
  1480. X        incnode(menunode);
  1481. X        if (!menunode)
  1482. X            menunode = firstnode(menulist);
  1483. X        s = menunode->dat;
  1484. X        if (*s == '~' || *s == '=')
  1485. X            {
  1486. X            spaceinline(1);
  1487. X            line[cs++] = *s++;
  1488. X            }
  1489. X        inststr(s = menunode->dat);
  1490. X        menue = cs;
  1491. X        return;
  1492. X        }
  1493. X    if (doexpandhist())
  1494. X        return;
  1495. X    zleparse = 1;
  1496. X    eofseen = 0;
  1497. X    lexsave();
  1498. X    hungets(" "); /* KLUDGE! */
  1499. X    hungets(line);
  1500. X    strinbeg();
  1501. X    pushheap();
  1502. X    while (!eofseen && zleparse)
  1503. X        {
  1504. X        lincmd = INCMD;
  1505. X        if ((t0 = yylex()) == ENDINPUT)
  1506. X            break;
  1507. X        }
  1508. X    if (t0 == ENDINPUT)
  1509. X        {
  1510. X        s = ztrdup("");
  1511. X        we = wb = cs;
  1512. X        t0 = STRING;
  1513. X        }
  1514. X    else if (t0 == STRING)
  1515. X        s = ztrdup(yylval.str);
  1516. X    hflush();
  1517. X    strinend();
  1518. X    errflag = zleparse = 0;
  1519. X    if (we > ll)
  1520. X        we = ll;
  1521. X    if (lst == COMP_EXPAND_COMPLETE)
  1522. X        {
  1523. X        char *q = s;
  1524. X
  1525. X        if (*q == Tilde)
  1526. X            lst = COMP_COMPLETE;
  1527. X        else if (*q == Equals)
  1528. X            {
  1529. X            for (q++; *q; q++)
  1530. X                if (*q == '/')
  1531. X                    break;
  1532. X            if (!*q)
  1533. X                lst = COMP_EXPAND;
  1534. X            q = s+1;
  1535. X            }
  1536. X        if (lst == COMP_EXPAND_COMPLETE)
  1537. X            {
  1538. X            for (; *q; q++)
  1539. X                if (itok(*q))
  1540. X                    break;
  1541. X            if (!*q)
  1542. X                lst = COMP_COMPLETE;
  1543. X            }
  1544. X        }
  1545. X    if (t0 != STRING)
  1546. X        feep();
  1547. X    else
  1548. X        {
  1549. X        if (lst == COMP_SPELL)
  1550. X            {
  1551. X            char    **x = &s;
  1552. X            untokenize(s);
  1553. X            cs = wb;
  1554. X            foredel(we-wb);
  1555. X            /* call the real spell checker, ash@aaii.oz.zu */
  1556. X            spckword(x, NULL, lincmd, 0);
  1557. X            inststr(*x);
  1558. X            }
  1559. X        else if (COMP_ISEXPAND(lst))
  1560. X            doexpansion(s,lst,lincmd);
  1561. X        else
  1562. X            {
  1563. X            if (!useglob)
  1564. X                untokenize(s);
  1565. X            docompletion(s,lst,lincmd);
  1566. X            }
  1567. X        free(s);
  1568. X        }
  1569. X    popheap();
  1570. X    lexrestore();
  1571. }
  1572. X
  1573. void doexpansion(s,lst,lincmd) /**/
  1574. char *s;int lst;int lincmd;
  1575. {
  1576. Lklist vl = newlist();
  1577. char *ss;
  1578. X
  1579. X    pushheap();
  1580. X    addnode(vl,s);
  1581. X    prefork(vl);
  1582. X    if (errflag)
  1583. X        goto end;
  1584. X    postfork(vl,1);
  1585. X    if (errflag)
  1586. X        goto end;
  1587. X    if (lst == COMP_LIST_EXPAND)
  1588. X        {
  1589. X        listmatches(vl,NULL);
  1590. X        goto end;
  1591. X        }
  1592. X    else if (peekfirst(vl) == s) 
  1593. X        {
  1594. X        if (lst == COMP_EXPAND_COMPLETE)
  1595. X            {
  1596. X            untokenize(s);
  1597. X            docompletion(s,COMP_COMPLETE,lincmd);
  1598. X            }
  1599. X        else
  1600. X            feep();
  1601. X        goto end;
  1602. X        }
  1603. X    if (!full(vl) || !*(char *) peekfirst(vl))
  1604. X        {
  1605. X        feep();
  1606. X        goto end;
  1607. X        }
  1608. X    cs = wb;
  1609. X    foredel(we-wb);
  1610. X    while (ss = ugetnode(vl))
  1611. X        {
  1612. X        inststr(ss);
  1613. X        if (full(vl))
  1614. X            {
  1615. X            spaceinline(1);
  1616. X            line[cs++] = ' ';
  1617. X            }
  1618. X        }
  1619. end:
  1620. X    popheap();
  1621. X    setterm();
  1622. }
  1623. X
  1624. void gotword(s) /**/
  1625. char *s;
  1626. {
  1627. X    we = ll+1-inbufct;
  1628. X    if (cs <= we)
  1629. X        {
  1630. X        wb = ll-wordbeg;
  1631. X        zleparse = 0;
  1632. X        /* major hack ahead */
  1633. X        if (wb && line[wb] == '!' && line[wb-1] == '\\')
  1634. X            wb--;
  1635. X        }
  1636. }
  1637. X
  1638. void inststrlen(s,l) /**/
  1639. char *s;int l;
  1640. {
  1641. char *t,*u,*v;
  1642. X
  1643. X    t = halloc(strlen(s)*2+2);
  1644. X    u = s;
  1645. X    v = t;
  1646. X    for (; *u; u++)
  1647. X        {
  1648. X        if (l != -1 && !l--)
  1649. X            break;
  1650. X        if (ispecial(*u))
  1651. X            if (*u == '\n')
  1652. X                {
  1653. X                *v++ = '\'';
  1654. X                *v++ = '\n';
  1655. X                *v++ = '\'';
  1656. X                continue;
  1657. X                }
  1658. X            else
  1659. X                *v++ = '\\';
  1660. X        *v++ = *u;
  1661. X        }
  1662. X    *v = '\0';
  1663. X    spaceinline(strlen(t));
  1664. X    strncpy(line+cs,t,strlen(t));
  1665. X    cs += strlen(t);
  1666. }
  1667. X
  1668. static int ambig,haspath,exact;
  1669. static Lklist matches;
  1670. static char *pat;
  1671. X
  1672. void addmatch(s) /**/
  1673. char *s;
  1674. {
  1675. X    if (full(matches))
  1676. X        {
  1677. X        int y = pfxlen(peekfirst(matches),s);
  1678. X
  1679. X        if (y < ambig)
  1680. X            ambig = y;
  1681. X        }
  1682. X    else
  1683. X        ambig = strlen(s);
  1684. X    if (!strcmp(pat,s))
  1685. X        exact = 1;
  1686. X    addnodeinorder(matches,strdup(s));
  1687. }
  1688. X
  1689. X
  1690. void addcmdmatch(s,t) /**/
  1691. char *s;char *t;
  1692. {
  1693. X    if (strpfx(pat,s))
  1694. X        addmatch(s);
  1695. }
  1696. X
  1697. void maketildelist(s) /**/
  1698. char    *s;
  1699. {
  1700. X    struct passwd    *pwd;
  1701. X    int        len;
  1702. X
  1703. X    s++;
  1704. X    len = strlen(s);
  1705. X    if (len < 1) {
  1706. X        addmatch(s);
  1707. X        *s = 0;
  1708. X        return;
  1709. X    }
  1710. X    while ((pwd = getpwent()) != NULL && !errflag)
  1711. X        if (strncmp(pwd->pw_name, s, len) == 0)
  1712. X            addmatch(pwd->pw_name);
  1713. X    endpwent();
  1714. X    *s = 0;
  1715. }
  1716. X
  1717. /*
  1718. X * opendir that handles '~' and '='.
  1719. X * orig. by ash@aaii.oz.au, mod. by pf
  1720. X */
  1721. DIR *OPENDIR(s)
  1722. char    *s;
  1723. {
  1724. X    if (*s != '~' && *s != '=')
  1725. X        return(opendir(s));
  1726. X    s = strdup(s);
  1727. X    *s = (*s == '=') ? Equals : Tilde;
  1728. X    filesub(&s);
  1729. X    return(opendir(s));
  1730. }
  1731. X
  1732. int Isdir(s) /**/
  1733. char *s;
  1734. {
  1735. struct stat sbuf;
  1736. X
  1737. X   if (stat(s,&sbuf) == -1)
  1738. X      return 0;
  1739. X   return S_ISDIR(sbuf.st_mode);
  1740. }
  1741. X
  1742. int isdir(t,s) /**/
  1743. char *t;char *s;
  1744. {
  1745. char buf[MAXPATHLEN];
  1746. X
  1747. X    sprintf(buf,"%s/%s",(s) ? s : ".",t);
  1748. X    s = buf;
  1749. X    if (*s != '~' && *s != '=')
  1750. X        return(Isdir(s));
  1751. X    s = strdup(s);
  1752. X    *s = (*s == '=') ? Equals : Tilde;
  1753. X    filesub(&s);
  1754. X    return(Isdir(s));
  1755. }
  1756. X
  1757. void docompletion(s,lst,incmd) /**/
  1758. char *s;int lst;int incmd;
  1759. {
  1760. DIR *d;
  1761. struct direct *de;
  1762. char *u,*ppfx = NULL;
  1763. int commcomp = 0;
  1764. int tildeexpand = 0;
  1765. char    *ss;
  1766. int addedstar = 0;
  1767. X
  1768. X    heapalloc();
  1769. X    pushheap();
  1770. X    ss = strdup(s);
  1771. X    matches = newlist();
  1772. X    if (useglob)
  1773. X        {
  1774. X        char    *pt;
  1775. X        char    ch;
  1776. X        int    hasp = 0;
  1777. X
  1778. X        /*
  1779. X         * Find the longest prefix string without any
  1780. X         * chars special to glob - ash.
  1781. X         */
  1782. X        for (pt = s; *pt; pt++)
  1783. X            {
  1784. X            if (pt == s && (*pt == Tilde || *pt == Equals))
  1785. X            continue;
  1786. X            if (ispecial(*pt) || itok(*pt))
  1787. X            break;
  1788. X            }
  1789. X        for (; pt > s && *pt != '/'; pt--)
  1790. X            ;
  1791. X        if (*pt == '/')
  1792. X            {
  1793. X            *pt = 0;
  1794. X            u = pt + 1;
  1795. X            wb += strlen(s) + 1;
  1796. X            hasp = 1;
  1797. X            }
  1798. X        else
  1799. X            u = s;
  1800. X        if (!hasp && (*s == Tilde || *s == Equals)) 
  1801. X            {
  1802. X            /* string contains only ~xx, so do tilde expansion */
  1803. X            maketildelist(s);
  1804. X            wb++;
  1805. X            ppfx = s;
  1806. X            tildeexpand = 1;
  1807. X            }
  1808. X        else
  1809. X            {
  1810. X            int     commonprefix = 0;
  1811. X            char    *prefix;
  1812. X            Lknode    n;
  1813. X            int        nonomatch = isset(NONOMATCH);
  1814. X
  1815. X            opts[NONOMATCH] = 1;
  1816. X            if (hasp)
  1817. X            {
  1818. X            /* Find the longest common prefix string
  1819. X             * after globbing the input. All expansions
  1820. X             * ~foo/bar/* will turn into something like
  1821. X             * /tmp_mnt/hosts/somehost/home/foo/...
  1822. X             * We will remove this common prefix from the matches.
  1823. X             * ash, 7 May '91
  1824. X             */
  1825. X            addnode(matches,s);
  1826. X            prefork(matches);
  1827. X            if (!errflag)
  1828. X                postfork(matches,1);
  1829. X            if (!errflag)
  1830. X                {
  1831. X                prefix = peekfirst(matches);
  1832. X                if (prefix)
  1833. X                    commonprefix = strlen(prefix) + 1;
  1834. X                *pt = '/';
  1835. X                }
  1836. X            }
  1837. X            if (s[strlen(s) - 1] == '/') {
  1838. X            /* if strings ends in a '/' always add a '*' */
  1839. X            s = dyncat(s,"x");
  1840. X            s[strlen(s)-1] = Star;
  1841. X            addedstar = 1;
  1842. X            };
  1843. X            matches = newlist();
  1844. X            addnode(matches,s);
  1845. X            prefork(matches);
  1846. X            if (!errflag)
  1847. X                postfork(matches,1);
  1848. X            opts[NONOMATCH] = nonomatch;
  1849. X            if (errflag || !full(matches) || !nextnode(firstnode(matches)))
  1850. X                {
  1851. X                /* if there were no matches (or only one)
  1852. X                   add a trailing * and try again */
  1853. X                s = dyncat(s,"x");
  1854. X                s[strlen(s)-1] = Star;
  1855. X                addedstar = 1;
  1856. X                matches = newlist();
  1857. X                addnode(matches,s);
  1858. X                prefork(matches);
  1859. X                if (errflag)
  1860. X                    goto end;
  1861. X                postfork(matches,1);
  1862. X                if (errflag)
  1863. X                    goto end;
  1864. X                }
  1865. X            if (commonprefix)
  1866. X            /* remove the common prefix from all the matches */
  1867. X            for (n = firstnode(matches); n; incnode(n))
  1868. X                (char *)(n->dat) += commonprefix;
  1869. X            s = pt;
  1870. X            *s = 0;
  1871. X            }
  1872. X          }
  1873. X    else
  1874. X        {
  1875. X        haspath = exact = 0;
  1876. X        for (u = s+strlen(s); u >= s && *u != '/'; u--);
  1877. X        if (u >= s)
  1878. X            {
  1879. X            *u++ = '\0';
  1880. X            haspath = 1;
  1881. X            }
  1882. X        else
  1883. X            u = s;
  1884. X        pat = u;
  1885. X        if (commcomp = !incmd && !haspath)
  1886. X            {
  1887. X            listhtable(aliastab ,addcmdmatch);
  1888. X            listhtable(cmdnamtab,addcmdmatch);
  1889. X            }
  1890. X        else if ((*s == '~' || *s == '=') && !haspath)
  1891. X            {
  1892. X            maketildelist(s);
  1893. X            ppfx = s;
  1894. X            tildeexpand = 1;
  1895. X            }
  1896. X        else if (d = OPENDIR(ppfx =
  1897. X                ((haspath || *s == '~') ? ((*s) ? s : "/") : ".")))
  1898. X            {
  1899. X            char *q;
  1900. X
  1901. X            readdir(d); readdir(d);
  1902. X            while ((de = readdir(d)) && !errflag)
  1903. X                if (strpfx(pat,q = de->d_name) &&
  1904. X                            (*q != '.' || *u == '.' || isset(GLOBDOTS)))
  1905. X                        addmatch(q);
  1906. X            closedir(d);
  1907. X            }
  1908. X        }
  1909. X    if (full(matches) && nextnode(firstnode(matches)))
  1910. X        {
  1911. X        Lknode z,zn;
  1912. X
  1913. X        for (z = firstnode(matches); z; z = zn)
  1914. X            {
  1915. X            char *q = getdata(z);
  1916. X            int namlen = strlen(q);
  1917. X            int    slen = strlen(ss);
  1918. X            char **pt = fignore;
  1919. X    
  1920. X            zn = nextnode(z);
  1921. X            for (; *pt; pt++)
  1922. X                {
  1923. X                /* We try to be smart here and override the
  1924. X                   fignore variable if the user has explicity
  1925. X                   used the ignored prefix, pem, 7 May 1991 */
  1926. X                if (!addedstar && strcmp(ss+slen-strlen(*pt), *pt) == 0)
  1927. X                    continue;
  1928. X                if (strlen(*pt) < namlen && !strcmp(q+namlen-strlen(*pt),*pt))
  1929. X                    {
  1930. X                    uremnode(matches,z);
  1931. X                    break;
  1932. X                    }
  1933. X                }
  1934. X            }
  1935. X        }
  1936. X    if (!full(matches) || errflag)
  1937. X        feep();
  1938. X    else if (lst == COMP_LIST_COMPLETE)
  1939. X        listmatches(matches,
  1940. X                    unset(LISTTYPES) ? NULL : (haspath) ? ppfx : "./");
  1941. X    else if (nextnode(firstnode(matches)))
  1942. X        {
  1943. X        if (usemenu)
  1944. X            {
  1945. X            menucmp = 1;
  1946. X            if (isset(MENUCOMPLETEBEEP))
  1947. X                feep();
  1948. X            cs = wb;
  1949. X            menuw = cs;
  1950. X            foredel(we-wb);
  1951. X            if (*s == '~' || *s == '=')
  1952. X                {
  1953. X                spaceinline(1);
  1954. X                line[cs++] = *s++;
  1955. X                }
  1956. X            if (haspath)
  1957. X                {
  1958. X                inststr(s);
  1959. X                spaceinline(1);
  1960. X                line[cs++] = '/';
  1961. X                }
  1962. X            menub = cs;
  1963. X            s = peekfirst(matches);
  1964. X            if ((*s == '~' || *s == '=') && !haspath)
  1965. X                {
  1966. X                spaceinline(1);
  1967. X                line[cs++] = *s++;
  1968. X                }
  1969. X            inststr(s);
  1970. X            menue = cs;
  1971. X            permalloc();
  1972. X            menulist = duplist(matches,ztrdup);
  1973. X            heapalloc();
  1974. X            menunode = firstnode(menulist);
  1975. X            popheap();
  1976. X            permalloc();
  1977. X            return;
  1978. X            }
  1979. X        else if (useglob)
  1980. X            {
  1981. X            feep();
  1982. X            if (isset(AUTOLIST))
  1983. X                listmatches(matches,
  1984. X                    unset(LISTTYPES) ? NULL : (haspath) ? ppfx : "./");
  1985. X            goto end;
  1986. X            }
  1987. X        cs = wb;
  1988. X        foredel(we-wb);
  1989. X        if (*s == '~' || *s == '=')
  1990. X            {
  1991. X            spaceinline(1);
  1992. X            line[cs++] = *s++;
  1993. X            }
  1994. X        if (haspath)
  1995. X            {
  1996. X            inststr(s);
  1997. X            spaceinline(1);
  1998. X            line[cs++] = '/';
  1999. X            }
  2000. X        if (isset(RECEXACT) && exact)
  2001. X            {
  2002. X            if ((*u == '~' || *u == '=') && !haspath)
  2003. X                {
  2004. X                spaceinline(1);
  2005. X                line[cs++] = *s++;
  2006. X                }
  2007. X            inststr(u);
  2008. X            spaceinline(1);
  2009. X            line[cs++] = (!commcomp && (tildeexpand ||
  2010. X                isdir(u,ppfx)) ? '/' : ' ');
  2011. X            }
  2012. X        else
  2013. X            {
  2014. X            s = peekfirst(matches);
  2015. X            if ((*s == '~' || *s == '=') && !haspath)
  2016. X                {
  2017. X                spaceinline(1);
  2018. X                line[cs++] = *s++;
  2019. X                ambig--;
  2020. X                }
  2021. X            inststrlen(s,ambig);
  2022. X            refresh();
  2023. X            feep();
  2024. X            if (isset(AUTOLIST))
  2025. X                listmatches(matches,
  2026. X                    unset(LISTTYPES) ? NULL : (haspath) ? ppfx : "./");
  2027. X            }
  2028. X        }
  2029. X    else
  2030. X        {
  2031. X        cs = wb;
  2032. X        foredel(we-wb);
  2033. X        if (*s == '~' || *s == '=')
  2034. X            {
  2035. X            spaceinline(1);
  2036. X            line[cs++] = *s++;
  2037. X            }
  2038. X        if (haspath)
  2039. X            {
  2040. X            inststr(s);
  2041. X            spaceinline(1);
  2042. X            line[cs++] = '/';
  2043. X            }
  2044. X        s = peekfirst(matches);
  2045. X        if ((*s == '~' || *s == '=') && !haspath)
  2046. X            {
  2047. X            spaceinline(1);
  2048. X            line[cs++] = *s++;
  2049. X            }
  2050. X        inststr(s);
  2051. X        spaceinline(1);
  2052. X        line[cs++] = (!commcomp && (tildeexpand ||
  2053. X            isdir(peekfirst(matches),ppfx)) ? '/' : ' '); 
  2054. X        }
  2055. X    ll = strlen(line);
  2056. end:
  2057. X    setterm();
  2058. X    popheap();
  2059. X    permalloc();
  2060. }
  2061. X
  2062. int strpfx(s,t) /**/
  2063. char *s;char *t;
  2064. {
  2065. X    while (*s && *s == *t) s++,t++;
  2066. X    return !*s;
  2067. }
  2068. X
  2069. int pfxlen(s,t) /**/
  2070. char *s;char *t;
  2071. {
  2072. int i = 0;
  2073. X
  2074. X    while (*s && *s == *t) s++,t++,i++;
  2075. X    return i;
  2076. }
  2077. X
  2078. void listmatches(l,apps) /**/
  2079. Lklist l;char *apps;
  2080. {
  2081. int longest = 1,fct,fw = 0,colsz,t0,t1,ct;
  2082. Lknode n;
  2083. char **arr,**ap;
  2084. X
  2085. X    trashzle();
  2086. X    ct = countnodes(l);
  2087. X    if (listmax && ct > listmax)
  2088. X        {
  2089. X        fprintf(stdout,"zsh: do you wish to see all %d possibilities? ",ct);
  2090. X        fflush(stdout);
  2091. X        if (getquery() != 'y')
  2092. X            return;
  2093. X        }
  2094. X    ap = arr = alloc((countnodes(l)+1)*sizeof(char **));
  2095. X    for (n = firstnode(l); n; incnode(n))
  2096. X        *ap++ = getdata(n);
  2097. X    *ap = NULL;
  2098. X    for (ap = arr; *ap; ap++)
  2099. X        if (strlen(*ap) > longest)
  2100. X            longest = strlen(*ap);
  2101. X    if (apps)
  2102. X        {
  2103. X        apps = strdup(apps);
  2104. X        if (*apps == '~')
  2105. X            *apps = Tilde;
  2106. X        else if (*apps == '=')
  2107. X            *apps = Equals;
  2108. X        filesub(&apps);
  2109. X        longest++;
  2110. X        }
  2111. X    qsort(arr,ct,sizeof(char *),forstrcmp);
  2112. X    fct = (columns-1)/(longest+2);
  2113. X    if (fct == 0)
  2114. X        fct = 1;
  2115. X    else
  2116. X        fw = (columns-1)/fct;
  2117. X    colsz = (ct+fct-1)/fct;
  2118. X    for (t1 = 0; t1 != colsz; t1++)
  2119. X        {
  2120. X        ap = arr+t1;
  2121. X        if (apps)
  2122. X            do
  2123. X                {
  2124. X                int t2 = strlen(*ap)+1;
  2125. X                char pbuf[MAXPATHLEN];
  2126. X                struct stat buf;
  2127. X
  2128. X                printf("%s",*ap);
  2129. X                sprintf(pbuf,"%s/%s",apps,*ap);
  2130. X                if (lstat(pbuf,&buf))
  2131. X                    putchar(' ');
  2132. X                else switch (buf.st_mode & S_IFMT) /* screw POSIX */
  2133. X                    {
  2134. X                    case S_IFDIR: putchar('/'); break;
  2135. #ifdef S_IFIFO
  2136. X                    case S_IFIFO: putchar('|'); break;
  2137. #endif
  2138. X                    case S_IFCHR: putchar('%'); break;
  2139. X                    case S_IFBLK: putchar('#'); break;
  2140. X                    case S_IFLNK: putchar('@'); break;
  2141. X                    case S_IFSOCK: putchar('='); break;
  2142. X                    default:
  2143. X                        if (buf.st_mode & 0111)
  2144. X                            putchar('*');
  2145. X                        else
  2146. X                            putchar(' ');
  2147. X                        break;
  2148. X                    }
  2149. X                for (; t2 < fw; t2++) putchar(' ');
  2150. X                for (t0 = colsz; t0 && *ap; t0--,ap++);
  2151. X                }
  2152. X            while (*ap);
  2153. X        else
  2154. X            do
  2155. X                {
  2156. X                int t2 = strlen(*ap);
  2157. X
  2158. X                printf("%s",*ap);
  2159. X                for (; t2 < fw; t2++) putchar(' ');
  2160. X                for (t0 = colsz; t0 && *ap; t0--,ap++);
  2161. X                }
  2162. X            while (*ap);
  2163. X        putchar('\n');
  2164. X        }
  2165. X    resetneeded = 1;
  2166. X    fflush(stdout);
  2167. }
  2168. X
  2169. void selectlist(l) /**/
  2170. Lklist l;
  2171. {
  2172. int longest = 1,fct,fw = 0,colsz,t0,t1,ct;
  2173. Lknode n;
  2174. char **arr,**ap;
  2175. X
  2176. X    trashzle();
  2177. X    ct = countnodes(l);
  2178. X    ap = arr = alloc((countnodes(l)+1)*sizeof(char **));
  2179. X    for (n = firstnode(l); n; incnode(n))
  2180. X        *ap++ = getdata(n);
  2181. X    *ap = NULL;
  2182. X    for (ap = arr; *ap; ap++)
  2183. X        if (strlen(*ap) > longest)
  2184. X            longest = strlen(*ap);
  2185. X    t0 = ct;
  2186. X    longest++;
  2187. X    while (t0)
  2188. X        t0 /= 10, longest++;
  2189. X    fct = (columns-1)/(longest+2);
  2190. X    if (fct == 0)
  2191. X        fct = 1;
  2192. X    else
  2193. X        fw = (columns-1)/fct;
  2194. X    colsz = (ct+fct-1)/fct;
  2195. X    for (t1 = 0; t1 != colsz; t1++)
  2196. X        {
  2197. X        ap = arr+t1;
  2198. X        do
  2199. X            {
  2200. X            int t2 = strlen(*ap)+1,t3;
  2201. X
  2202. X            fprintf(stderr,"%d %s",t3 = ap-arr+1,*ap);
  2203. X            while (t3)
  2204. X                t2++,t3 /= 10;
  2205. X            for (; t2 < fw; t2++) fputc(' ',stderr);
  2206. X            for (t0 = colsz; t0 && *ap; t0--,ap++);
  2207. X            }
  2208. X        while (*ap);
  2209. X        fputc('\n',stderr);
  2210. X        }
  2211. X    resetneeded = 1;
  2212. X    fflush(stderr);
  2213. }
  2214. X
  2215. int doexpandhist() /**/
  2216. {
  2217. char *cc,*ce;
  2218. int t0;
  2219. X
  2220. X    for (cc = line, ce = line+ll; cc < ce; cc++)
  2221. X        if (*cc == bangchar || *cc == hatchar)
  2222. X            break;
  2223. X    if (cc == ce)
  2224. X        return 0;
  2225. X    zleparse = 1;
  2226. X    eofseen = 0;
  2227. X    lexsave();
  2228. X    hungets(line);
  2229. X    strinbeg();
  2230. X    pushheap();
  2231. X    ll = cs = 0;
  2232. X    for(;;)
  2233. X        {
  2234. X        t0 = hgetc();
  2235. X        if (lexstop)
  2236. X            break;
  2237. X        spaceinline(1);
  2238. X        line[cs++] = t0;
  2239. X        }
  2240. X    hflush();
  2241. X    popheap();
  2242. X    strinend();
  2243. X    errflag = zleparse = 0;
  2244. X    t0 = histdone;
  2245. X    lexrestore();
  2246. X    line[ll = cs] = '\0';
  2247. X    return t0;
  2248. }
  2249. X
  2250. void magicspace() /**/
  2251. {
  2252. X    doexpandhist();
  2253. X    c = ' ';
  2254. X    selfinsert();
  2255. }
  2256. X
  2257. void expandhistory() /**/
  2258. {
  2259. X    if (!doexpandhist())
  2260. X        feep();
  2261. }
  2262. X
  2263. char *getcurcmd() /**/
  2264. {
  2265. int t0,lincmd = INCMD;
  2266. char *s = NULL;
  2267. X
  2268. X    zleparse = 1;
  2269. X    eofseen = 0;
  2270. X    lexsave();
  2271. X    hungets(" "); /* KLUDGE! */
  2272. X    hungets(line);
  2273. X    strinbeg();
  2274. X    pushheap();
  2275. X    while (!eofseen && zleparse)
  2276. X        {
  2277. X        if ((t0 = yylex()) == ENDINPUT)
  2278. X            break;
  2279. X        else if (t0 == STRING && !lincmd)
  2280. X            {
  2281. X            if (s)
  2282. X                free(s);
  2283. X            s = ztrdup(yylval.str);
  2284. X            }
  2285. X        lincmd = INCMD;
  2286. X        }
  2287. X    hflush();
  2288. X    popheap();
  2289. X    strinend();
  2290. X    errflag = zleparse = 0;
  2291. X    lexrestore();
  2292. X    return s;
  2293. }
  2294. X
  2295. void processcmd() /**/
  2296. {
  2297. char *s;
  2298. X
  2299. X    s = getcurcmd();
  2300. X    if (s)
  2301. X        {
  2302. X        char *t;
  2303. X
  2304. X        t = zlecmds[bindk].name;
  2305. X        mult = 1;
  2306. X        pushline();
  2307. X        sizeline(strlen(s)+strlen(t)+1);
  2308. X        strcpy(line,t);
  2309. X        strcat(line," ");
  2310. X        cs = ll = strlen(line);
  2311. X        inststr(s);
  2312. X        free(s);
  2313. X        done = 1;
  2314. X        }
  2315. X    else
  2316. X        feep();
  2317. }
  2318. X
  2319. void freemenu() /**/
  2320. {
  2321. X    if (menucmp)
  2322. X        {
  2323. X        menucmp = 0;
  2324. X        freetable(menulist,freestr);
  2325. X        }
  2326. }
  2327. X
  2328. SHAR_EOF
  2329. chmod 0644 zsh2.00/src/zle_tricky.c ||
  2330. echo 'restore of zsh2.00/src/zle_tricky.c failed'
  2331. Wc_c="`wc -c < 'zsh2.00/src/zle_tricky.c'`"
  2332. test 18859 -eq "$Wc_c" ||
  2333.     echo 'zsh2.00/src/zle_tricky.c: original size 18859, current size' "$Wc_c"
  2334. # ============= zsh2.00/src/zle_tricky.pro ==============
  2335. echo 'x - extracting zsh2.00/src/zle_tricky.pro (Text)'
  2336. sed 's/^X//' << 'SHAR_EOF' > 'zsh2.00/src/zle_tricky.pro' &&
  2337. int usetab DCLPROTO((void));
  2338. void completeword DCLPROTO((void));
  2339. void globcomplete DCLPROTO((void));
  2340. void menucompleteword DCLPROTO((void));
  2341. void listchoices DCLPROTO((void));
  2342. void spellword DCLPROTO((void));
  2343. void deletecharorlist DCLPROTO((void));
  2344. void expandword DCLPROTO((void));
  2345. void expandorcomplete DCLPROTO((void));
  2346. void menuexpandorcomplete DCLPROTO((void));
  2347. void listexpand DCLPROTO((void));
  2348. void reversemenucomplete DCLPROTO((void));
  2349. void acceptandmenucomplete DCLPROTO((void));
  2350. void docomplete DCLPROTO((int lst));
  2351. void doexpansion DCLPROTO((char *s,int lst,int lincmd));
  2352. void gotword DCLPROTO((char *s));
  2353. void inststrlen DCLPROTO((char *s,int l));
  2354. void addmatch DCLPROTO((char *s));
  2355. void addcmdmatch DCLPROTO((char *s,char *t));
  2356. void maketildelist DCLPROTO((char    *s));
  2357. int Isdir DCLPROTO((char *s));
  2358. int isdir DCLPROTO((char *t,char *s));
  2359. void docompletion DCLPROTO((char *s,int lst,int incmd));
  2360. int strpfx DCLPROTO((char *s,char *t));
  2361. int pfxlen DCLPROTO((char *s,char *t));
  2362. void listmatches DCLPROTO((Lklist l,char *apps));
  2363. void selectlist DCLPROTO((Lklist l));
  2364. int doexpandhist DCLPROTO((void));
  2365. void magicspace DCLPROTO((void));
  2366. void expandhistory DCLPROTO((void));
  2367. char *getcurcmd DCLPROTO((void));
  2368. void processcmd DCLPROTO((void));
  2369. SHAR_EOF
  2370. true || echo 'restore of zsh2.00/src/zle_tricky.pro failed'
  2371. echo 'End of  part 5'
  2372. echo 'File zsh2.00/src/zle_tricky.pro is continued in part 6'
  2373. echo 6 > _shar_seq_.tmp
  2374. exit 0
  2375.               Paul Falstad  pfalstad@phoenix.princeton.edu
  2376.          And on the roads, too, vicious gangs of KEEP LEFT signs!
  2377.      If Princeton knew my opinions, they'd have expelled me long ago.
  2378.  
  2379. exit 0 # Just in case...
  2380. -- 
  2381. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  2382. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  2383. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  2384. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  2385.