PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/tcsh/tw.comp.c

https://github.com/okuoku/freebsd-head
C | 642 lines | 473 code | 73 blank | 96 comment | 124 complexity | 4bac4f82649d77da2668c9be95115cd1 MD5 | raw file
  1. /* $Header: /p/tcsh/cvsroot/tcsh/tw.comp.c,v 1.42 2007/10/01 21:52:00 christos Exp $ */
  2. /*
  3. * tw.comp.c: File completion builtin
  4. */
  5. /*-
  6. * Copyright (c) 1980, 1991 The Regents of the University of California.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "sh.h"
  34. RCSID("$tcsh: tw.comp.c,v 1.42 2007/10/01 21:52:00 christos Exp $")
  35. #include "tw.h"
  36. #include "ed.h"
  37. #include "tc.h"
  38. /* #define TDEBUG */
  39. struct varent completions;
  40. static int tw_result (const Char *, Char **);
  41. static Char **tw_find (Char *, struct varent *, int);
  42. static Char *tw_tok (Char *);
  43. static int tw_pos (Char *, int);
  44. static void tw_pr (Char **);
  45. static int tw_match (const Char *, const Char *);
  46. static void tw_prlist (struct varent *);
  47. static const Char *tw_dollar (const Char *,Char **, size_t, Char **,
  48. Char, const char *);
  49. /* docomplete():
  50. * Add or list completions in the completion list
  51. */
  52. /*ARGSUSED*/
  53. void
  54. docomplete(Char **v, struct command *t)
  55. {
  56. struct varent *vp;
  57. Char *p;
  58. Char **pp;
  59. USE(t);
  60. v++;
  61. p = *v++;
  62. if (p == 0)
  63. tw_prlist(&completions);
  64. else if (*v == 0) {
  65. vp = adrof1(strip(p), &completions);
  66. if (vp && vp->vec)
  67. tw_pr(vp->vec), xputchar('\n');
  68. else
  69. {
  70. #ifdef TDEBUG
  71. xprintf("tw_find(%s) \n", short2str(strip(p)));
  72. #endif /* TDEBUG */
  73. pp = tw_find(strip(p), &completions, FALSE);
  74. if (pp)
  75. tw_pr(pp), xputchar('\n');
  76. }
  77. }
  78. else
  79. set1(strip(p), saveblk(v), &completions, VAR_READWRITE);
  80. } /* end docomplete */
  81. /* douncomplete():
  82. * Remove completions from the completion list
  83. */
  84. /*ARGSUSED*/
  85. void
  86. douncomplete(Char **v, struct command *t)
  87. {
  88. USE(t);
  89. unset1(v, &completions);
  90. } /* end douncomplete */
  91. /* tw_prlist():
  92. * Pretty print a list of variables
  93. */
  94. static void
  95. tw_prlist(struct varent *p)
  96. {
  97. struct varent *c;
  98. for (;;) {
  99. while (p->v_left)
  100. p = p->v_left;
  101. x:
  102. if (p->v_parent == 0) /* is it the header? */
  103. break;
  104. if (setintr) {
  105. int old_pintr_disabled;
  106. pintr_push_enable(&old_pintr_disabled);
  107. cleanup_until(&old_pintr_disabled);
  108. }
  109. xprintf("%s\t", short2str(p->v_name));
  110. if (p->vec)
  111. tw_pr(p->vec);
  112. xputchar('\n');
  113. if (p->v_right) {
  114. p = p->v_right;
  115. continue;
  116. }
  117. do {
  118. c = p;
  119. p = p->v_parent;
  120. } while (p->v_right == c);
  121. goto x;
  122. }
  123. } /* end tw_prlist */
  124. /* tw_pr():
  125. * Pretty print a completion, adding single quotes around
  126. * a completion argument and collapsing multiple spaces to one.
  127. */
  128. static void
  129. tw_pr(Char **cmp)
  130. {
  131. int sp, osp;
  132. Char *ptr;
  133. for (; *cmp; cmp++) {
  134. xputchar('\'');
  135. for (osp = 0, ptr = *cmp; *ptr; ptr++) {
  136. sp = Isspace(*ptr);
  137. if (sp && osp)
  138. continue;
  139. xputwchar(*ptr);
  140. osp = sp;
  141. }
  142. xputchar('\'');
  143. if (cmp[1])
  144. xputchar(' ');
  145. }
  146. } /* end tw_pr */
  147. /* tw_find():
  148. * Find the first matching completion.
  149. * For commands we only look at names that start with -
  150. */
  151. static Char **
  152. tw_find(Char *nam, struct varent *vp, int cmd)
  153. {
  154. Char **rv;
  155. for (vp = vp->v_left; vp; vp = vp->v_right) {
  156. if (vp->v_left && (rv = tw_find(nam, vp, cmd)) != NULL)
  157. return rv;
  158. if (cmd) {
  159. if (vp->v_name[0] != '-')
  160. continue;
  161. if (Gmatch(nam, &vp->v_name[1]) && vp->vec != NULL)
  162. return vp->vec;
  163. }
  164. else
  165. if (Gmatch(nam, vp->v_name) && vp->vec != NULL)
  166. return vp->vec;
  167. }
  168. return NULL;
  169. } /* end tw_find */
  170. /* tw_pos():
  171. * Return true if the position is within the specified range
  172. */
  173. static int
  174. tw_pos(Char *ran, int wno)
  175. {
  176. Char *p;
  177. if (ran[0] == '*' && ran[1] == '\0')
  178. return 1;
  179. for (p = ran; *p && *p != '-'; p++)
  180. continue;
  181. if (*p == '\0') /* range == <number> */
  182. return wno == getn(ran);
  183. if (ran == p) /* range = - <number> */
  184. return wno <= getn(&ran[1]);
  185. *p++ = '\0';
  186. if (*p == '\0') /* range = <number> - */
  187. return getn(ran) <= wno;
  188. else /* range = <number> - <number> */
  189. return (getn(ran) <= wno) && (wno <= getn(p));
  190. } /* end tw_pos */
  191. /* tw_tok():
  192. * Return the next word from string, unquoteing it.
  193. */
  194. static Char *
  195. tw_tok(Char *str)
  196. {
  197. static Char *bf = NULL;
  198. if (str != NULL)
  199. bf = str;
  200. /* skip leading spaces */
  201. for (; *bf && Isspace(*bf); bf++)
  202. continue;
  203. for (str = bf; *bf && !Isspace(*bf); bf++) {
  204. if (ismetahash(*bf))
  205. return INVPTR;
  206. *bf = *bf & ~QUOTE;
  207. }
  208. if (*bf != '\0')
  209. *bf++ = '\0';
  210. return *str ? str : NULL;
  211. } /* end tw_tok */
  212. /* tw_match():
  213. * Match a string against the pattern given.
  214. * and return the number of matched characters
  215. * in a prefix of the string.
  216. */
  217. static int
  218. tw_match(const Char *str, const Char *pat)
  219. {
  220. const Char *estr;
  221. int rv = Gnmatch(str, pat, &estr);
  222. #ifdef TDEBUG
  223. xprintf("Gnmatch(%s, ", short2str(str));
  224. xprintf("%s, ", short2str(pat));
  225. xprintf("%s) = %d [%d]\n", short2str(estr), rv, estr - str);
  226. #endif /* TDEBUG */
  227. return (int) (rv ? estr - str : -1);
  228. }
  229. /* tw_result():
  230. * Return what the completion action should be depending on the
  231. * string
  232. */
  233. static int
  234. tw_result(const Char *act, Char **pat)
  235. {
  236. int looking;
  237. static Char* res = NULL;
  238. Char *p;
  239. if (res != NULL)
  240. xfree(res), res = NULL;
  241. switch (act[0] & ~QUOTE) {
  242. case 'X':
  243. looking = TW_COMPLETION;
  244. break;
  245. case 'S':
  246. looking = TW_SIGNAL;
  247. break;
  248. case 'a':
  249. looking = TW_ALIAS;
  250. break;
  251. case 'b':
  252. looking = TW_BINDING;
  253. break;
  254. case 'c':
  255. looking = TW_COMMAND;
  256. break;
  257. case 'C':
  258. looking = TW_PATH | TW_COMMAND;
  259. break;
  260. case 'd':
  261. looking = TW_DIRECTORY;
  262. break;
  263. case 'D':
  264. looking = TW_PATH | TW_DIRECTORY;
  265. break;
  266. case 'e':
  267. looking = TW_ENVVAR;
  268. break;
  269. case 'f':
  270. looking = TW_FILE;
  271. break;
  272. #ifdef COMPAT
  273. case 'p':
  274. #endif /* COMPAT */
  275. case 'F':
  276. looking = TW_PATH | TW_FILE;
  277. break;
  278. case 'g':
  279. looking = TW_GRPNAME;
  280. break;
  281. case 'j':
  282. looking = TW_JOB;
  283. break;
  284. case 'l':
  285. looking = TW_LIMIT;
  286. break;
  287. case 'n':
  288. looking = TW_NONE;
  289. break;
  290. case 's':
  291. looking = TW_SHELLVAR;
  292. break;
  293. case 't':
  294. looking = TW_TEXT;
  295. break;
  296. case 'T':
  297. looking = TW_PATH | TW_TEXT;
  298. break;
  299. case 'v':
  300. looking = TW_VARIABLE;
  301. break;
  302. case 'u':
  303. looking = TW_USER;
  304. break;
  305. case 'x':
  306. looking = TW_EXPLAIN;
  307. break;
  308. case '$':
  309. *pat = res = Strsave(&act[1]);
  310. (void) strip(res);
  311. return(TW_VARLIST);
  312. case '(':
  313. *pat = res = Strsave(&act[1]);
  314. if ((p = Strchr(res, ')')) != NULL)
  315. *p = '\0';
  316. (void) strip(res);
  317. return TW_WORDLIST;
  318. case '`':
  319. res = Strsave(act);
  320. if ((p = Strchr(&res[1], '`')) != NULL)
  321. *++p = '\0';
  322. if (didfds == 0) {
  323. /*
  324. * Make sure that we have some file descriptors to
  325. * play with, so that the processes have at least 0, 1, 2
  326. * open
  327. */
  328. (void) dcopy(SHIN, 0);
  329. (void) dcopy(SHOUT, 1);
  330. (void) dcopy(SHDIAG, 2);
  331. }
  332. if ((p = globone(res, G_APPEND)) != NULL) {
  333. xfree(res), res = NULL;
  334. *pat = res = Strsave(p);
  335. xfree(p);
  336. return TW_WORDLIST;
  337. }
  338. return TW_ZERO;
  339. default:
  340. stderror(ERR_COMPCOM, short2str(act));
  341. return TW_ZERO;
  342. }
  343. switch (act[1] & ~QUOTE) {
  344. case '\0':
  345. return looking;
  346. case ':':
  347. *pat = res = Strsave(&act[2]);
  348. (void) strip(res);
  349. return looking;
  350. default:
  351. stderror(ERR_COMPCOM, short2str(act));
  352. return TW_ZERO;
  353. }
  354. } /* end tw_result */
  355. /* tw_dollar():
  356. * Expand $<n> args in buffer
  357. */
  358. static const Char *
  359. tw_dollar(const Char *str, Char **wl, size_t nwl, Char **result, Char sep,
  360. const char *msg)
  361. {
  362. struct Strbuf buf = Strbuf_INIT;
  363. Char *res;
  364. const Char *sp;
  365. for (sp = str; *sp && *sp != sep;)
  366. if (sp[0] == '$' && sp[1] == ':' && Isdigit(sp[sp[2] == '-' ? 3 : 2])) {
  367. int num, neg = 0;
  368. sp += 2;
  369. if (*sp == '-') {
  370. neg = 1;
  371. sp++;
  372. }
  373. for (num = *sp++ - '0'; Isdigit(*sp); num += 10 * num + *sp++ - '0')
  374. continue;
  375. if (neg)
  376. num = nwl - num - 1;
  377. if (num >= 0 && (size_t)num < nwl)
  378. Strbuf_append(&buf, wl[num]);
  379. }
  380. else
  381. Strbuf_append1(&buf, *sp++);
  382. res = Strbuf_finish(&buf);
  383. if (*sp++ == sep) {
  384. *result = res;
  385. return sp;
  386. }
  387. xfree(res);
  388. /* Truncates data if WIDE_STRINGS */
  389. stderror(ERR_COMPMIS, (int)sep, msg, short2str(str));
  390. return --sp;
  391. } /* end tw_dollar */
  392. /* tw_complete():
  393. * Return the appropriate completion for the command
  394. *
  395. * valid completion strings are:
  396. * p/<range>/<completion>/[<suffix>/] positional
  397. * c/<pattern>/<completion>/[<suffix>/] current word ignore pattern
  398. * C/<pattern>/<completion>/[<suffix>/] current word with pattern
  399. * n/<pattern>/<completion>/[<suffix>/] next word
  400. * N/<pattern>/<completion>/[<suffix>/] next-next word
  401. */
  402. int
  403. tw_complete(const Char *line, Char **word, Char **pat, int looking, eChar *suf)
  404. {
  405. Char *buf, **vec, **wl;
  406. static Char nomatch[2] = { (Char) ~0, 0x00 };
  407. const Char *ptr;
  408. size_t wordno;
  409. int n;
  410. buf = Strsave(line);
  411. cleanup_push(buf, xfree);
  412. /* Single-character words, empty current word, terminating NULL */
  413. wl = xmalloc(((Strlen(line) + 1) / 2 + 2) * sizeof (*wl));
  414. cleanup_push(wl, xfree);
  415. /* find the command */
  416. if ((wl[0] = tw_tok(buf)) == NULL || wl[0] == INVPTR) {
  417. cleanup_until(buf);
  418. return TW_ZERO;
  419. }
  420. /*
  421. * look for hardwired command completions using a globbing
  422. * search and for arguments using a normal search.
  423. */
  424. if ((vec = tw_find(wl[0], &completions, (looking == TW_COMMAND)))
  425. == NULL) {
  426. cleanup_until(buf);
  427. return looking;
  428. }
  429. /* tokenize the line one more time :-( */
  430. for (wordno = 1; (wl[wordno] = tw_tok(NULL)) != NULL &&
  431. wl[wordno] != INVPTR; wordno++)
  432. continue;
  433. if (wl[wordno] == INVPTR) { /* Found a meta character */
  434. cleanup_until(buf);
  435. return TW_ZERO; /* de-activate completions */
  436. }
  437. #ifdef TDEBUG
  438. {
  439. size_t i;
  440. for (i = 0; i < wordno; i++)
  441. xprintf("'%s' ", short2str(wl[i]));
  442. xprintf("\n");
  443. }
  444. #endif /* TDEBUG */
  445. /* if the current word is empty move the last word to the next */
  446. if (**word == '\0') {
  447. wl[wordno] = *word;
  448. wordno++;
  449. }
  450. wl[wordno] = NULL;
  451. #ifdef TDEBUG
  452. xprintf("\r\n");
  453. xprintf(" w#: %lu\n", (unsigned long)wordno);
  454. xprintf("line: %s\n", short2str(line));
  455. xprintf(" cmd: %s\n", short2str(wl[0]));
  456. xprintf("word: %s\n", short2str(*word));
  457. xprintf("last: %s\n", wordno >= 2 ? short2str(wl[wordno-2]) : "n/a");
  458. xprintf("this: %s\n", wordno >= 1 ? short2str(wl[wordno-1]) : "n/a");
  459. #endif /* TDEBUG */
  460. for (;vec != NULL && (ptr = vec[0]) != NULL; vec++) {
  461. Char *ran, /* The pattern or range X/<range>/XXXX/ */
  462. *com, /* The completion X/XXXXX/<completion>/ */
  463. *pos = NULL; /* scratch pointer */
  464. int cmd, res;
  465. Char sep; /* the command and separator characters */
  466. if (ptr[0] == '\0')
  467. continue;
  468. #ifdef TDEBUG
  469. xprintf("match %s\n", short2str(ptr));
  470. #endif /* TDEBUG */
  471. switch (cmd = ptr[0]) {
  472. case 'N':
  473. pos = (wordno < 3) ? nomatch : wl[wordno - 3];
  474. break;
  475. case 'n':
  476. pos = (wordno < 2) ? nomatch : wl[wordno - 2];
  477. break;
  478. case 'c':
  479. case 'C':
  480. pos = (wordno < 1) ? nomatch : wl[wordno - 1];
  481. break;
  482. case 'p':
  483. break;
  484. default:
  485. stderror(ERR_COMPINV, CGETS(27, 1, "command"), cmd);
  486. return TW_ZERO;
  487. }
  488. sep = ptr[1];
  489. if (!Ispunct(sep)) {
  490. /* Truncates data if WIDE_STRINGS */
  491. stderror(ERR_COMPINV, CGETS(27, 2, "separator"), (int)sep);
  492. return TW_ZERO;
  493. }
  494. ptr = tw_dollar(&ptr[2], wl, wordno, &ran, sep,
  495. CGETS(27, 3, "pattern"));
  496. cleanup_push(ran, xfree);
  497. if (ran[0] == '\0') /* check for empty pattern (disallowed) */
  498. {
  499. stderror(ERR_COMPINC, cmd == 'p' ? CGETS(27, 4, "range") :
  500. CGETS(27, 3, "pattern"), "");
  501. return TW_ZERO;
  502. }
  503. ptr = tw_dollar(ptr, wl, wordno, &com, sep,
  504. CGETS(27, 5, "completion"));
  505. cleanup_push(com, xfree);
  506. if (*ptr != '\0') {
  507. if (*ptr == sep)
  508. *suf = CHAR_ERR;
  509. else
  510. *suf = *ptr;
  511. }
  512. else
  513. *suf = '\0';
  514. #ifdef TDEBUG
  515. xprintf("command: %c\nseparator: %c\n", cmd, (int)sep);
  516. xprintf("pattern: %s\n", short2str(ran));
  517. xprintf("completion: %s\n", short2str(com));
  518. xprintf("suffix: ");
  519. switch (*suf) {
  520. case 0:
  521. xprintf("*auto suffix*\n");
  522. break;
  523. case CHAR_ERR:
  524. xprintf("*no suffix*\n");
  525. break;
  526. default:
  527. xprintf("%c\n", (int)*suf);
  528. break;
  529. }
  530. #endif /* TDEBUG */
  531. switch (cmd) {
  532. case 'p': /* positional completion */
  533. #ifdef TDEBUG
  534. xprintf("p: tw_pos(%s, %lu) = ", short2str(ran),
  535. (unsigned long)wordno - 1);
  536. xprintf("%d\n", tw_pos(ran, wordno - 1));
  537. #endif /* TDEBUG */
  538. if (!tw_pos(ran, wordno - 1)) {
  539. cleanup_until(ran);
  540. continue;
  541. }
  542. break;
  543. case 'N': /* match with the next-next word */
  544. case 'n': /* match with the next word */
  545. case 'c': /* match with the current word */
  546. case 'C':
  547. #ifdef TDEBUG
  548. xprintf("%c: ", cmd);
  549. #endif /* TDEBUG */
  550. if ((n = tw_match(pos, ran)) < 0) {
  551. cleanup_until(ran);
  552. continue;
  553. }
  554. if (cmd == 'c')
  555. *word += n;
  556. break;
  557. default:
  558. abort(); /* Cannot happen */
  559. }
  560. tsetenv(STRCOMMAND_LINE, line);
  561. res = tw_result(com, pat);
  562. Unsetenv(STRCOMMAND_LINE);
  563. cleanup_until(buf);
  564. return res;
  565. }
  566. cleanup_until(buf);
  567. *suf = '\0';
  568. return TW_ZERO;
  569. } /* end tw_complete */