readline.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /* $NetBSD: readline.c,v 1.21 2002/03/18 16:20:36 christos Exp $ */
  2. /*-
  3. * Copyright (c) 1997 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Jaromir Dolecek.
  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. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the NetBSD
  20. * Foundation, Inc. and its contributors.
  21. * 4. Neither the name of The NetBSD Foundation nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  26. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  27. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  29. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "config.h"
  38. #if !defined(lint) && !defined(SCCSID)
  39. __RCSID("$NetBSD: readline.c,v 1.21 2002/03/18 16:20:36 christos Exp $");
  40. #endif /* not lint && not SCCSID */
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <stdio.h>
  44. #include <dirent.h>
  45. #include <string.h>
  46. #include <pwd.h>
  47. #include <ctype.h>
  48. #include <stdlib.h>
  49. #include <unistd.h>
  50. #include <limits.h>
  51. #ifdef SOLARIS
  52. #include <alloca.h>
  53. #endif
  54. #include "histedit.h"
  55. #include "readline.h"
  56. #include "el.h"
  57. #include "fcns.h" /* for EL_NUM_FCNS */
  58. /* for rl_complete() */
  59. #define TAB '\r'
  60. /* see comment at the #ifdef for sense of this */
  61. #define GDB_411_HACK
  62. /* readline compatibility stuff - look at readline sources/documentation */
  63. /* to see what these variables mean */
  64. const char *rl_library_version = "EditLine wrapper";
  65. static char empty[] = { '\0' };
  66. static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
  67. static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
  68. '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
  69. char *rl_readline_name = empty;
  70. FILE *rl_instream = NULL;
  71. FILE *rl_outstream = NULL;
  72. int rl_point = 0;
  73. int rl_end = 0;
  74. char *rl_line_buffer = NULL;
  75. int history_base = 1; /* probably never subject to change */
  76. int history_length = 0;
  77. int max_input_history = 0;
  78. char history_expansion_char = '!';
  79. char history_subst_char = '^';
  80. char *history_no_expand_chars = expand_chars;
  81. Function *history_inhibit_expansion_function = NULL;
  82. int rl_inhibit_completion = 0;
  83. int rl_attempted_completion_over = 0;
  84. char *rl_basic_word_break_characters = break_chars;
  85. char *rl_completer_word_break_characters = NULL;
  86. char *rl_completer_quote_characters = NULL;
  87. CPFunction *rl_completion_entry_function = NULL;
  88. CPPFunction *rl_attempted_completion_function = NULL;
  89. /*
  90. * This is set to character indicating type of completion being done by
  91. * rl_complete_internal(); this is available for application completion
  92. * functions.
  93. */
  94. int rl_completion_type = 0;
  95. /*
  96. * If more than this number of items results from query for possible
  97. * completions, we ask user if they are sure to really display the list.
  98. */
  99. int rl_completion_query_items = 100;
  100. /*
  101. * List of characters which are word break characters, but should be left
  102. * in the parsed text when it is passed to the completion function.
  103. * Shell uses this to help determine what kind of completing to do.
  104. */
  105. char *rl_special_prefixes = (char *)NULL;
  106. /*
  107. * This is the character appended to the completed words if at the end of
  108. * the line. Default is ' ' (a space).
  109. */
  110. int rl_completion_append_character = ' ';
  111. /* stuff below is used internally by libedit for readline emulation */
  112. /* if not zero, non-unique completions always show list of possible matches */
  113. static int _rl_complete_show_all = 0;
  114. static History *h = NULL;
  115. static EditLine *e = NULL;
  116. static int el_rl_complete_cmdnum = 0;
  117. /* internal functions */
  118. static unsigned char _el_rl_complete(EditLine *, int);
  119. static char *_get_prompt(EditLine *);
  120. static HIST_ENTRY *_move_history(int);
  121. static int _history_search_gen(const char *, int, int);
  122. static int _history_expand_command(const char *, size_t, char **);
  123. static char *_rl_compat_sub(const char *, const char *,
  124. const char *, int);
  125. static int rl_complete_internal(int);
  126. static int _rl_qsort_string_compare(const void *, const void *);
  127. /*
  128. * needed for prompt switching in readline()
  129. */
  130. static char *el_rl_prompt = NULL;
  131. /* ARGSUSED */
  132. static char *
  133. _get_prompt(EditLine *el)
  134. {
  135. return (el_rl_prompt);
  136. }
  137. /*
  138. * generic function for moving around history
  139. */
  140. static HIST_ENTRY *
  141. _move_history(int op)
  142. {
  143. HistEvent ev;
  144. static HIST_ENTRY rl_he;
  145. if (history(h, &ev, op) != 0)
  146. return (HIST_ENTRY *) NULL;
  147. rl_he.line = ev.str;
  148. rl_he.data = "";
  149. return (&rl_he);
  150. }
  151. /*
  152. * READLINE compatibility stuff
  153. */
  154. /*
  155. * initialize rl compat stuff
  156. */
  157. int
  158. rl_initialize(void)
  159. {
  160. HistEvent ev;
  161. const LineInfo *li;
  162. int i;
  163. int editmode = 1;
  164. struct termios t;
  165. if (e != NULL)
  166. el_end(e);
  167. if (h != NULL)
  168. history_end(h);
  169. if (!rl_instream)
  170. rl_instream = stdin;
  171. if (!rl_outstream)
  172. rl_outstream = stdout;
  173. /*
  174. * See if we don't really want to run the editor
  175. */
  176. if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)
  177. editmode = 0;
  178. e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
  179. if (!editmode)
  180. el_set(e, EL_EDITMODE, 0);
  181. h = history_init();
  182. if (!e || !h)
  183. return (-1);
  184. history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */
  185. history_length = 0;
  186. max_input_history = INT_MAX;
  187. el_set(e, EL_HIST, history, h);
  188. /* for proper prompt printing in readline() */
  189. el_rl_prompt = strdup("");
  190. el_set(e, EL_PROMPT, _get_prompt);
  191. el_set(e, EL_SIGNAL, 1);
  192. /* set default mode to "emacs"-style and read setting afterwards */
  193. /* so this can be overriden */
  194. el_set(e, EL_EDITOR, "emacs");
  195. /*
  196. * Word completition - this has to go AFTER rebinding keys
  197. * to emacs-style.
  198. */
  199. el_set(e, EL_ADDFN, "rl_complete",
  200. "ReadLine compatible completition function",
  201. _el_rl_complete);
  202. el_set(e, EL_BIND, "^I", "rl_complete", NULL);
  203. /*
  204. * Find out where the rl_complete function was added; this is
  205. * used later to detect that lastcmd was also rl_complete.
  206. */
  207. for(i=EL_NUM_FCNS; i < e->el_map.nfunc; i++) {
  208. if (e->el_map.func[i] == _el_rl_complete) {
  209. el_rl_complete_cmdnum = i;
  210. break;
  211. }
  212. }
  213. /* read settings from configuration file */
  214. el_source(e, NULL);
  215. /*
  216. * Unfortunately, some applications really do use rl_point
  217. * and rl_line_buffer directly.
  218. */
  219. li = el_line(e);
  220. /* a cheesy way to get rid of const cast. */
  221. rl_line_buffer = memchr(li->buffer, *li->buffer, 1);
  222. rl_point = rl_end = 0;
  223. return (0);
  224. }
  225. /*
  226. * read one line from input stream and return it, chomping
  227. * trailing newline (if there is any)
  228. */
  229. char *
  230. readline(const char *prompt)
  231. {
  232. HistEvent ev;
  233. int count;
  234. const char *ret;
  235. char *buf;
  236. if (e == NULL || h == NULL)
  237. rl_initialize();
  238. /* update prompt accordingly to what has been passed */
  239. if (!prompt)
  240. prompt = "";
  241. if (strcmp(el_rl_prompt, prompt) != 0) {
  242. free(el_rl_prompt);
  243. el_rl_prompt = strdup(prompt);
  244. }
  245. /* get one line from input stream */
  246. ret = el_gets(e, &count);
  247. if (ret && count > 0) {
  248. int lastidx;
  249. buf = strdup(ret);
  250. lastidx = count - 1;
  251. if (buf[lastidx] == '\n')
  252. buf[lastidx] = '\0';
  253. } else
  254. buf = NULL;
  255. history(h, &ev, H_GETSIZE);
  256. history_length = ev.num;
  257. return buf;
  258. }
  259. /*
  260. * history functions
  261. */
  262. /*
  263. * is normally called before application starts to use
  264. * history expansion functions
  265. */
  266. void
  267. using_history(void)
  268. {
  269. if (h == NULL || e == NULL)
  270. rl_initialize();
  271. }
  272. /*
  273. * substitute ``what'' with ``with'', returning resulting string; if
  274. * globally == 1, substitutes all occurences of what, otherwise only the
  275. * first one
  276. */
  277. static char *
  278. _rl_compat_sub(const char *str, const char *what, const char *with,
  279. int globally)
  280. {
  281. char *result;
  282. const char *temp, *new;
  283. int len, with_len, what_len, add;
  284. size_t size, i;
  285. result = malloc((size = 16));
  286. temp = str;
  287. with_len = strlen(with);
  288. what_len = strlen(what);
  289. len = 0;
  290. do {
  291. new = strstr(temp, what);
  292. if (new) {
  293. i = new - temp;
  294. add = i + with_len;
  295. if (i + add + 1 >= size) {
  296. size += add + 1;
  297. result = realloc(result, size);
  298. }
  299. (void) strncpy(&result[len], temp, i);
  300. len += i;
  301. (void) strcpy(&result[len], with); /* safe */
  302. len += with_len;
  303. temp = new + what_len;
  304. } else {
  305. add = strlen(temp);
  306. if (len + add + 1 >= size) {
  307. size += add + 1;
  308. result = realloc(result, size);
  309. }
  310. (void) strcpy(&result[len], temp); /* safe */
  311. len += add;
  312. temp = NULL;
  313. }
  314. } while (temp && globally);
  315. result[len] = '\0';
  316. return (result);
  317. }
  318. /*
  319. * the real function doing history expansion - takes as argument command
  320. * to do and data upon which the command should be executed
  321. * does expansion the way I've understood readline documentation
  322. * word designator ``%'' isn't supported (yet ?)
  323. *
  324. * returns 0 if data was not modified, 1 if it was and 2 if the string
  325. * should be only printed and not executed; in case of error,
  326. * returns -1 and *result points to NULL
  327. * it's callers responsibility to free() string returned in *result
  328. */
  329. static int
  330. _history_expand_command(const char *command, size_t cmdlen, char **result)
  331. {
  332. char **arr, *tempcmd, *line, *search = NULL, *cmd;
  333. const char *event_data = NULL;
  334. static char *from = NULL, *to = NULL;
  335. int start = -1, end = -1, max, i, idx;
  336. int h_on = 0, t_on = 0, r_on = 0, e_on = 0, p_on = 0, g_on = 0;
  337. int event_num = 0, retval;
  338. size_t cmdsize;
  339. *result = NULL;
  340. cmd = alloca(cmdlen + 1);
  341. (void) strncpy(cmd, command, cmdlen);
  342. cmd[cmdlen] = 0;
  343. idx = 1;
  344. /* find out which event to take */
  345. if (cmd[idx] == history_expansion_char) {
  346. event_num = history_length;
  347. idx++;
  348. } else {
  349. int off, num;
  350. size_t len;
  351. off = idx;
  352. while (cmd[off] && !strchr(":^$*-%", cmd[off]))
  353. off++;
  354. num = atoi(&cmd[idx]);
  355. if (num != 0) {
  356. event_num = num;
  357. if (num < 0)
  358. event_num += history_length + 1;
  359. } else {
  360. int prefix = 1, curr_num;
  361. HistEvent ev;
  362. len = off - idx;
  363. if (cmd[idx] == '?') {
  364. idx++, len--;
  365. if (cmd[off - 1] == '?')
  366. len--;
  367. else if (cmd[off] != '\n' && cmd[off] != '\0')
  368. return (-1);
  369. prefix = 0;
  370. }
  371. search = alloca(len + 1);
  372. (void) strncpy(search, &cmd[idx], len);
  373. search[len] = '\0';
  374. if (history(h, &ev, H_CURR) != 0)
  375. return (-1);
  376. curr_num = ev.num;
  377. if (prefix)
  378. retval = history_search_prefix(search, -1);
  379. else
  380. retval = history_search(search, -1);
  381. if (retval == -1) {
  382. fprintf(rl_outstream, "%s: Event not found\n",
  383. search);
  384. return (-1);
  385. }
  386. if (history(h, &ev, H_CURR) != 0)
  387. return (-1);
  388. event_data = ev.str;
  389. /* roll back to original position */
  390. history(h, &ev, H_NEXT_EVENT, curr_num);
  391. }
  392. idx = off;
  393. }
  394. if (!event_data && event_num >= 0) {
  395. HIST_ENTRY *rl_he;
  396. rl_he = history_get(event_num);
  397. if (!rl_he)
  398. return (0);
  399. event_data = rl_he->line;
  400. } else
  401. return (-1);
  402. if (cmd[idx] != ':')
  403. return (-1);
  404. cmd += idx + 1;
  405. /* recognize cmd */
  406. if (*cmd == '^')
  407. start = end = 1, cmd++;
  408. else if (*cmd == '$')
  409. start = end = -1, cmd++;
  410. else if (*cmd == '*')
  411. start = 1, end = -1, cmd++;
  412. else if (isdigit((unsigned char) *cmd)) {
  413. const char *temp;
  414. int shifted = 0;
  415. start = atoi(cmd);
  416. temp = cmd;
  417. for (; isdigit((unsigned char) *cmd); cmd++);
  418. if (temp != cmd)
  419. shifted = 1;
  420. if (shifted && *cmd == '-') {
  421. if (!isdigit((unsigned char) *(cmd + 1)))
  422. end = -2;
  423. else {
  424. end = atoi(cmd + 1);
  425. for (; isdigit((unsigned char) *cmd); cmd++);
  426. }
  427. } else if (shifted && *cmd == '*')
  428. end = -1, cmd++;
  429. else if (shifted)
  430. end = start;
  431. }
  432. if (*cmd == ':')
  433. cmd++;
  434. line = strdup(event_data);
  435. for (; *cmd; cmd++) {
  436. if (*cmd == ':')
  437. continue;
  438. else if (*cmd == 'h')
  439. h_on = 1 | g_on, g_on = 0;
  440. else if (*cmd == 't')
  441. t_on = 1 | g_on, g_on = 0;
  442. else if (*cmd == 'r')
  443. r_on = 1 | g_on, g_on = 0;
  444. else if (*cmd == 'e')
  445. e_on = 1 | g_on, g_on = 0;
  446. else if (*cmd == 'p')
  447. p_on = 1 | g_on, g_on = 0;
  448. else if (*cmd == 'g')
  449. g_on = 2;
  450. else if (*cmd == 's' || *cmd == '&') {
  451. char *what, *with, delim;
  452. int len, from_len;
  453. size_t size;
  454. if (*cmd == '&' && (from == NULL || to == NULL))
  455. continue;
  456. else if (*cmd == 's') {
  457. delim = *(++cmd), cmd++;
  458. size = 16;
  459. what = realloc(from, size);
  460. len = 0;
  461. for (; *cmd && *cmd != delim; cmd++) {
  462. if (*cmd == '\\'
  463. && *(cmd + 1) == delim)
  464. cmd++;
  465. if (len >= size)
  466. what = realloc(what,
  467. (size <<= 1));
  468. what[len++] = *cmd;
  469. }
  470. what[len] = '\0';
  471. from = what;
  472. if (*what == '\0') {
  473. free(what);
  474. if (search)
  475. from = strdup(search);
  476. else {
  477. from = NULL;
  478. free(line);
  479. return (-1);
  480. }
  481. }
  482. cmd++; /* shift after delim */
  483. if (!*cmd)
  484. continue;
  485. size = 16;
  486. with = realloc(to, size);
  487. len = 0;
  488. from_len = strlen(from);
  489. for (; *cmd && *cmd != delim; cmd++) {
  490. if (len + from_len + 1 >= size) {
  491. size += from_len + 1;
  492. with = realloc(with, size);
  493. }
  494. if (*cmd == '&') {
  495. /* safe */
  496. (void) strcpy(&with[len], from);
  497. len += from_len;
  498. continue;
  499. }
  500. if (*cmd == '\\'
  501. && (*(cmd + 1) == delim
  502. || *(cmd + 1) == '&'))
  503. cmd++;
  504. with[len++] = *cmd;
  505. }
  506. with[len] = '\0';
  507. to = with;
  508. tempcmd = _rl_compat_sub(line, from, to,
  509. (g_on) ? 1 : 0);
  510. free(line);
  511. line = tempcmd;
  512. g_on = 0;
  513. }
  514. }
  515. }
  516. arr = history_tokenize(line);
  517. free(line); /* no more needed */
  518. if (arr && *arr == NULL)
  519. free(arr), arr = NULL;
  520. if (!arr)
  521. return (-1);
  522. /* find out max valid idx to array of array */
  523. max = 0;
  524. for (i = 0; arr[i]; i++)
  525. max++;
  526. max--;
  527. /* set boundaries to something relevant */
  528. if (start < 0)
  529. start = 1;
  530. if (end < 0)
  531. end = max - ((end < -1) ? 1 : 0);
  532. /* check boundaries ... */
  533. if (start > max || end > max || start > end) {
  534. for (i = 0; i <= max; i++) {
  535. free(arr[i]);
  536. }
  537. free(arr), arr = (char **) NULL;
  538. return (-1);
  539. }
  540. for (i = 0; i <= max; i++) {
  541. char *temp;
  542. if (h_on && (i == 1 || h_on > 1) &&
  543. (temp = strrchr(arr[i], '/')))
  544. *(temp + 1) = '\0';
  545. if (t_on && (i == 1 || t_on > 1) &&
  546. (temp = strrchr(arr[i], '/')))
  547. (void) strcpy(arr[i], temp + 1);
  548. if (r_on && (i == 1 || r_on > 1) &&
  549. (temp = strrchr(arr[i], '.')))
  550. *temp = '\0';
  551. if (e_on && (i == 1 || e_on > 1) &&
  552. (temp = strrchr(arr[i], '.')))
  553. (void) strcpy(arr[i], temp);
  554. }
  555. cmdsize = 1, cmdlen = 0;
  556. tempcmd = malloc(cmdsize);
  557. for (i = start; start <= i && i <= end; i++) {
  558. int arr_len;
  559. arr_len = strlen(arr[i]);
  560. if (cmdlen + arr_len + 1 >= cmdsize) {
  561. cmdsize += arr_len + 1;
  562. tempcmd = realloc(tempcmd, cmdsize);
  563. }
  564. (void) strcpy(&tempcmd[cmdlen], arr[i]); /* safe */
  565. cmdlen += arr_len;
  566. tempcmd[cmdlen++] = ' '; /* add a space */
  567. }
  568. while (cmdlen > 0 && isspace((unsigned char) tempcmd[cmdlen - 1]))
  569. cmdlen--;
  570. tempcmd[cmdlen] = '\0';
  571. *result = tempcmd;
  572. for (i = 0; i <= max; i++)
  573. free(arr[i]);
  574. free(arr), arr = (char **) NULL;
  575. return (p_on) ? 2 : 1;
  576. }
  577. /*
  578. * csh-style history expansion
  579. */
  580. int
  581. history_expand(char *str, char **output)
  582. {
  583. int i, retval = 0, idx;
  584. size_t size;
  585. char *temp, *result;
  586. if (h == NULL || e == NULL)
  587. rl_initialize();
  588. *output = strdup(str); /* do it early */
  589. if (str[0] == history_subst_char) {
  590. /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
  591. temp = alloca(4 + strlen(str) + 1);
  592. temp[0] = temp[1] = history_expansion_char;
  593. temp[2] = ':';
  594. temp[3] = 's';
  595. (void) strcpy(temp + 4, str);
  596. str = temp;
  597. }
  598. #define ADD_STRING(what, len) \
  599. { \
  600. if (idx + len + 1 > size) \
  601. result = realloc(result, (size += len + 1)); \
  602. (void)strncpy(&result[idx], what, len); \
  603. idx += len; \
  604. result[idx] = '\0'; \
  605. }
  606. result = NULL;
  607. size = idx = 0;
  608. for (i = 0; str[i];) {
  609. int start, j, loop_again;
  610. size_t len;
  611. loop_again = 1;
  612. start = j = i;
  613. loop:
  614. for (; str[j]; j++) {
  615. if (str[j] == '\\' &&
  616. str[j + 1] == history_expansion_char) {
  617. (void) strcpy(&str[j], &str[j + 1]);
  618. continue;
  619. }
  620. if (!loop_again) {
  621. if (str[j] == '?') {
  622. while (str[j] && str[++j] != '?');
  623. if (str[j] == '?')
  624. j++;
  625. } else if (isspace((unsigned char) str[j]))
  626. break;
  627. }
  628. if (str[j] == history_expansion_char
  629. && !strchr(history_no_expand_chars, str[j + 1])
  630. && (!history_inhibit_expansion_function ||
  631. (*history_inhibit_expansion_function)(str, j) == 0))
  632. break;
  633. }
  634. if (str[j] && str[j + 1] != '#' && loop_again) {
  635. i = j;
  636. j++;
  637. if (str[j] == history_expansion_char)
  638. j++;
  639. loop_again = 0;
  640. goto loop;
  641. }
  642. len = i - start;
  643. temp = &str[start];
  644. ADD_STRING(temp, len);
  645. if (str[i] == '\0' || str[i] != history_expansion_char
  646. || str[i + 1] == '#') {
  647. len = j - i;
  648. temp = &str[i];
  649. ADD_STRING(temp, len);
  650. if (start == 0)
  651. retval = 0;
  652. else
  653. retval = 1;
  654. break;
  655. }
  656. retval = _history_expand_command(&str[i], (size_t) (j - i),
  657. &temp);
  658. if (retval != -1) {
  659. len = strlen(temp);
  660. ADD_STRING(temp, len);
  661. }
  662. i = j;
  663. } /* for(i ...) */
  664. if (retval == 2) {
  665. add_history(temp);
  666. #ifdef GDB_411_HACK
  667. /* gdb 4.11 has been shipped with readline, where */
  668. /* history_expand() returned -1 when the line */
  669. /* should not be executed; in readline 2.1+ */
  670. /* it should return 2 in such a case */
  671. retval = -1;
  672. #endif
  673. }
  674. free(*output);
  675. *output = result;
  676. return (retval);
  677. }
  678. /*
  679. * Parse the string into individual tokens, similarily to how shell would do it.
  680. */
  681. char **
  682. history_tokenize(const char *str)
  683. {
  684. int size = 1, result_idx = 0, i, start;
  685. size_t len;
  686. char **result = NULL, *temp, delim = '\0';
  687. for (i = 0; str[i]; i++) {
  688. while (isspace((unsigned char) str[i]))
  689. i++;
  690. start = i;
  691. for (; str[i]; i++) {
  692. if (str[i] == '\\') {
  693. if (str[i+1] != '\0')
  694. i++;
  695. } else if (str[i] == delim)
  696. delim = '\0';
  697. else if (!delim &&
  698. (isspace((unsigned char) str[i]) ||
  699. strchr("()<>;&|$", str[i])))
  700. break;
  701. else if (!delim && strchr("'`\"", str[i]))
  702. delim = str[i];
  703. }
  704. if (result_idx + 2 >= size) {
  705. size <<= 1;
  706. result = realloc(result, size * sizeof(char *));
  707. }
  708. len = i - start;
  709. temp = malloc(len + 1);
  710. (void) strncpy(temp, &str[start], len);
  711. temp[len] = '\0';
  712. result[result_idx++] = temp;
  713. result[result_idx] = NULL;
  714. }
  715. return (result);
  716. }
  717. /*
  718. * limit size of history record to ``max'' events
  719. */
  720. void
  721. stifle_history(int max)
  722. {
  723. HistEvent ev;
  724. if (h == NULL || e == NULL)
  725. rl_initialize();
  726. if (history(h, &ev, H_SETSIZE, max) == 0)
  727. max_input_history = max;
  728. }
  729. /*
  730. * "unlimit" size of history - set the limit to maximum allowed int value
  731. */
  732. int
  733. unstifle_history(void)
  734. {
  735. HistEvent ev;
  736. int omax;
  737. history(h, &ev, H_SETSIZE, INT_MAX);
  738. omax = max_input_history;
  739. max_input_history = INT_MAX;
  740. return (omax); /* some value _must_ be returned */
  741. }
  742. int
  743. history_is_stifled(void)
  744. {
  745. /* cannot return true answer */
  746. return (max_input_history != INT_MAX);
  747. }
  748. /*
  749. * read history from a file given
  750. */
  751. int
  752. read_history(const char *filename)
  753. {
  754. HistEvent ev;
  755. if (h == NULL || e == NULL)
  756. rl_initialize();
  757. return (history(h, &ev, H_LOAD, filename));
  758. }
  759. /*
  760. * write history to a file given
  761. */
  762. int
  763. write_history(const char *filename)
  764. {
  765. HistEvent ev;
  766. if (h == NULL || e == NULL)
  767. rl_initialize();
  768. return (history(h, &ev, H_SAVE, filename));
  769. }
  770. /*
  771. * returns history ``num''th event
  772. *
  773. * returned pointer points to static variable
  774. */
  775. HIST_ENTRY *
  776. history_get(int num)
  777. {
  778. static HIST_ENTRY she;
  779. HistEvent ev;
  780. int i = 1, curr_num;
  781. if (h == NULL || e == NULL)
  782. rl_initialize();
  783. /* rewind to beginning */
  784. if (history(h, &ev, H_CURR) != 0)
  785. return (NULL);
  786. curr_num = ev.num;
  787. if (history(h, &ev, H_LAST) != 0)
  788. return (NULL); /* error */
  789. while (i < num && history(h, &ev, H_PREV) == 0)
  790. i++;
  791. if (i != num)
  792. return (NULL); /* not so many entries */
  793. she.line = ev.str;
  794. she.data = NULL;
  795. /* rewind history to the same event it was before */
  796. (void) history(h, &ev, H_FIRST);
  797. (void) history(h, &ev, H_NEXT_EVENT, curr_num);
  798. return (&she);
  799. }
  800. /*
  801. * add the line to history table
  802. */
  803. int
  804. add_history(const char *line)
  805. {
  806. HistEvent ev;
  807. if (h == NULL || e == NULL)
  808. rl_initialize();
  809. (void) history(h, &ev, H_ENTER, line);
  810. if (history(h, &ev, H_GETSIZE) == 0)
  811. history_length = ev.num;
  812. return (!(history_length > 0)); /* return 0 if all is okay */
  813. }
  814. /*
  815. * clear the history list - delete all entries
  816. */
  817. void
  818. clear_history(void)
  819. {
  820. HistEvent ev;
  821. history(h, &ev, H_CLEAR);
  822. }
  823. /*
  824. * returns offset of the current history event
  825. */
  826. int
  827. where_history(void)
  828. {
  829. HistEvent ev;
  830. int curr_num, off;
  831. if (history(h, &ev, H_CURR) != 0)
  832. return (0);
  833. curr_num = ev.num;
  834. history(h, &ev, H_FIRST);
  835. off = 1;
  836. while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
  837. off++;
  838. return (off);
  839. }
  840. /*
  841. * returns current history event or NULL if there is no such event
  842. */
  843. HIST_ENTRY *
  844. current_history(void)
  845. {
  846. return (_move_history(H_CURR));
  847. }
  848. /*
  849. * returns total number of bytes history events' data are using
  850. */
  851. int
  852. history_total_bytes(void)
  853. {
  854. HistEvent ev;
  855. int curr_num, size;
  856. if (history(h, &ev, H_CURR) != 0)
  857. return (-1);
  858. curr_num = ev.num;
  859. history(h, &ev, H_FIRST);
  860. size = 0;
  861. do
  862. size += strlen(ev.str);
  863. while (history(h, &ev, H_NEXT) == 0);
  864. /* get to the same position as before */
  865. history(h, &ev, H_PREV_EVENT, curr_num);
  866. return (size);
  867. }
  868. /*
  869. * sets the position in the history list to ``pos''
  870. */
  871. int
  872. history_set_pos(int pos)
  873. {
  874. HistEvent ev;
  875. int off, curr_num;
  876. if (pos > history_length || pos < 0)
  877. return (-1);
  878. history(h, &ev, H_CURR);
  879. curr_num = ev.num;
  880. history(h, &ev, H_FIRST);
  881. off = 0;
  882. while (off < pos && history(h, &ev, H_NEXT) == 0)
  883. off++;
  884. if (off != pos) { /* do a rollback in case of error */
  885. history(h, &ev, H_FIRST);
  886. history(h, &ev, H_NEXT_EVENT, curr_num);
  887. return (-1);
  888. }
  889. return (0);
  890. }
  891. /*
  892. * returns previous event in history and shifts pointer accordingly
  893. */
  894. HIST_ENTRY *
  895. previous_history(void)
  896. {
  897. return (_move_history(H_PREV));
  898. }
  899. /*
  900. * returns next event in history and shifts pointer accordingly
  901. */
  902. HIST_ENTRY *
  903. next_history(void)
  904. {
  905. return (_move_history(H_NEXT));
  906. }
  907. /*
  908. * generic history search function
  909. */
  910. static int
  911. _history_search_gen(const char *str, int direction, int pos)
  912. {
  913. HistEvent ev;
  914. const char *strp;
  915. int curr_num;
  916. if (history(h, &ev, H_CURR) != 0)
  917. return (-1);
  918. curr_num = ev.num;
  919. for (;;) {
  920. strp = strstr(ev.str, str);
  921. if (strp && (pos < 0 || &ev.str[pos] == strp))
  922. return (int) (strp - ev.str);
  923. if (history(h, &ev, direction < 0 ? H_PREV : H_NEXT) != 0)
  924. break;
  925. }
  926. history(h, &ev, direction < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
  927. return (-1);
  928. }
  929. /*
  930. * searches for first history event containing the str
  931. */
  932. int
  933. history_search(const char *str, int direction)
  934. {
  935. return (_history_search_gen(str, direction, -1));
  936. }
  937. /*
  938. * searches for first history event beginning with str
  939. */
  940. int
  941. history_search_prefix(const char *str, int direction)
  942. {
  943. return (_history_search_gen(str, direction, 0));
  944. }
  945. /*
  946. * search for event in history containing str, starting at offset
  947. * abs(pos); continue backward, if pos<0, forward otherwise
  948. */
  949. /* ARGSUSED */
  950. int
  951. history_search_pos(const char *str, int direction, int pos)
  952. {
  953. HistEvent ev;
  954. int curr_num, off;
  955. off = (pos > 0) ? pos : -pos;
  956. pos = (pos > 0) ? 1 : -1;
  957. if (history(h, &ev, H_CURR) != 0)
  958. return (-1);
  959. curr_num = ev.num;
  960. if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
  961. return (-1);
  962. for (;;) {
  963. if (strstr(ev.str, str))
  964. return (off);
  965. if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
  966. break;
  967. }
  968. /* set "current" pointer back to previous state */
  969. history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
  970. return (-1);
  971. }
  972. /********************************/
  973. /* completition functions */
  974. /*
  975. * does tilde expansion of strings of type ``~user/foo''
  976. * if ``user'' isn't valid user name or ``txt'' doesn't start
  977. * w/ '~', returns pointer to strdup()ed copy of ``txt''
  978. *
  979. * it's callers's responsibility to free() returned string
  980. */
  981. char *
  982. tilde_expand(char *txt)
  983. {
  984. struct passwd *pass;
  985. char *temp;
  986. size_t len = 0;
  987. if (txt[0] != '~')
  988. return (strdup(txt));
  989. temp = strchr(txt + 1, '/');
  990. if (temp == NULL)
  991. temp = strdup(txt + 1);
  992. else {
  993. len = temp - txt + 1; /* text until string after slash */
  994. temp = malloc(len);
  995. (void) strncpy(temp, txt + 1, len - 2);
  996. temp[len - 2] = '\0';
  997. }
  998. pass = getpwnam(temp);
  999. free(temp); /* value no more needed */
  1000. if (pass == NULL)
  1001. return (strdup(txt));
  1002. /* update pointer txt to point at string immedially following */
  1003. /* first slash */
  1004. txt += len;
  1005. temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);
  1006. (void) sprintf(temp, "%s/%s", pass->pw_dir, txt);
  1007. return (temp);
  1008. }
  1009. /*
  1010. * return first found file name starting by the ``text'' or NULL if no
  1011. * such file can be found.
  1012. * The first ``state'' matches are ignored.
  1013. *
  1014. * it's caller's responsibility to free returned string
  1015. */
  1016. char *
  1017. filename_completion_function(const char *text, int state)
  1018. {
  1019. DIR *dir = NULL;
  1020. char *filename = NULL, *dirname = NULL;
  1021. size_t filename_len = 0;
  1022. struct dirent *entry;
  1023. char *temp;
  1024. size_t len;
  1025. temp = strrchr(text, '/');
  1026. if (temp) {
  1027. temp++;
  1028. filename = realloc(filename, strlen(temp) + 1);
  1029. (void) strcpy(filename, temp);
  1030. len = temp - text; /* including last slash */
  1031. dirname = realloc(dirname, len + 1);
  1032. (void) strncpy(dirname, text, len);
  1033. dirname[len] = '\0';
  1034. } else {
  1035. filename = strdup(text);
  1036. dirname = NULL;
  1037. }
  1038. /* support for ``~user'' syntax */
  1039. if (dirname && *dirname == '~') {
  1040. temp = tilde_expand(dirname);
  1041. dirname = realloc(dirname, strlen(temp) + 1);
  1042. (void) strcpy(dirname, temp); /* safe */
  1043. free(temp); /* no longer needed */
  1044. }
  1045. /* will be used in cycle */
  1046. filename_len = strlen(filename);
  1047. dir = opendir(dirname ? dirname : ".");
  1048. if (!dir) {
  1049. free(filename);
  1050. free(dirname);
  1051. return (NULL); /* cannot open the directory */
  1052. }
  1053. /* find the match */
  1054. while ((entry = readdir(dir)) != NULL) {
  1055. /* otherwise, get first entry where first */
  1056. /* filename_len characters are equal */
  1057. if (
  1058. #if defined(__SVR4) || defined(__linux__)
  1059. strlen(entry->d_name) >= filename_len
  1060. #else
  1061. entry->d_namlen >= filename_len
  1062. #endif
  1063. && strncmp(entry->d_name, filename,
  1064. filename_len) == 0
  1065. && (state-- == 0))
  1066. break;
  1067. }
  1068. if (entry) { /* match found */
  1069. struct stat stbuf;
  1070. #if defined(__SVR4) || defined(__linux__)
  1071. len = strlen(entry->d_name) +
  1072. #else
  1073. len = entry->d_namlen +
  1074. #endif
  1075. ((dirname) ? strlen(dirname) : 0) + 1 + 1;
  1076. temp = malloc(len);
  1077. (void) sprintf(temp, "%s%s",
  1078. dirname ? dirname : "", entry->d_name); /* safe */
  1079. /* test, if it's directory */
  1080. if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
  1081. strcat(temp, "/"); /* safe */
  1082. } else
  1083. temp = NULL;
  1084. closedir(dir);
  1085. free(filename);
  1086. free(dirname);
  1087. return (temp);
  1088. }
  1089. /*
  1090. * a completion generator for usernames; returns _first_ username
  1091. * which starts with supplied text
  1092. * text contains a partial username preceded by random character
  1093. * (usually '~'); state is ignored
  1094. * it's callers responsibility to free returned value
  1095. */
  1096. char *
  1097. username_completion_function(const char *text, int state)
  1098. {
  1099. struct passwd *pwd;
  1100. if (text[0] == '\0')
  1101. return (NULL);
  1102. if (*text == '~')
  1103. text++;
  1104. if (state == 0)
  1105. setpwent();
  1106. while ((pwd = getpwent()) && text[0] == pwd->pw_name[0]
  1107. && strcmp(text, pwd->pw_name) == 0);
  1108. if (pwd == NULL) {
  1109. endpwent();
  1110. return (NULL);
  1111. }
  1112. return (strdup(pwd->pw_name));
  1113. }
  1114. /*
  1115. * el-compatible wrapper around rl_complete; needed for key binding
  1116. */
  1117. /* ARGSUSED */
  1118. static unsigned char
  1119. _el_rl_complete(EditLine *el, int ch)
  1120. {
  1121. return (unsigned char) rl_complete(0, ch);
  1122. }
  1123. /*
  1124. * returns list of completitions for text given
  1125. */
  1126. char **
  1127. completion_matches(const char *text, CPFunction *genfunc)
  1128. {
  1129. char **match_list = NULL, *retstr, *prevstr;
  1130. size_t match_list_len, max_equal, which, i;
  1131. int matches;
  1132. if (h == NULL || e == NULL)
  1133. rl_initialize();
  1134. matches = 0;
  1135. match_list_len = 1;
  1136. while ((retstr = (*genfunc) (text, matches)) != NULL) {
  1137. if (matches + 1 >= match_list_len) {
  1138. match_list_len <<= 1;
  1139. match_list = realloc(match_list,
  1140. match_list_len * sizeof(char *));
  1141. }
  1142. match_list[++matches] = retstr;
  1143. }
  1144. if (!match_list)
  1145. return (char **) NULL; /* nothing found */
  1146. /* find least denominator and insert it to match_list[0] */
  1147. which = 2;
  1148. prevstr = match_list[1];
  1149. max_equal = strlen(prevstr);
  1150. for (; which <= matches; which++) {
  1151. for (i = 0; i < max_equal &&
  1152. prevstr[i] == match_list[which][i]; i++)
  1153. continue;
  1154. max_equal = i;
  1155. }
  1156. retstr = malloc(max_equal + 1);
  1157. (void) strncpy(retstr, match_list[1], max_equal);
  1158. retstr[max_equal] = '\0';
  1159. match_list[0] = retstr;
  1160. /* add NULL as last pointer to the array */
  1161. if (matches + 1 >= match_list_len)
  1162. match_list = realloc(match_list,
  1163. (match_list_len + 1) * sizeof(char *));
  1164. match_list[matches + 1] = (char *) NULL;
  1165. return (match_list);
  1166. }
  1167. /*
  1168. * Sort function for qsort(). Just wrapper around strcasecmp().
  1169. */
  1170. static int
  1171. _rl_qsort_string_compare(i1, i2)
  1172. const void *i1, *i2;
  1173. {
  1174. /* LINTED const castaway */
  1175. const char *s1 = ((const char **)i1)[0];
  1176. /* LINTED const castaway */
  1177. const char *s2 = ((const char **)i2)[0];
  1178. return strcasecmp(s1, s2);
  1179. }
  1180. /*
  1181. * Display list of strings in columnar format on readline's output stream.
  1182. * 'matches' is list of strings, 'len' is number of strings in 'matches',
  1183. * 'max' is maximum length of string in 'matches'.
  1184. */
  1185. void
  1186. rl_display_match_list (matches, len, max)
  1187. char **matches;
  1188. int len, max;
  1189. {
  1190. int i, idx, limit, count;
  1191. int screenwidth = e->el_term.t_size.h;
  1192. /*
  1193. * Find out how many entries can be put on one line, count
  1194. * with two spaces between strings.
  1195. */
  1196. limit = screenwidth / (max + 2);
  1197. if (limit == 0)
  1198. limit = 1;
  1199. /* how many lines of output */
  1200. count = len / limit;
  1201. if (count * limit < len)
  1202. count++;
  1203. /* Sort the items if they are not already sorted. */
  1204. qsort(&matches[1], (size_t)(len - 1), sizeof(char *),
  1205. _rl_qsort_string_compare);
  1206. idx = 1;
  1207. for(; count > 0; count--) {
  1208. for(i=0; i < limit && matches[idx]; i++, idx++)
  1209. fprintf(e->el_outfile, "%-*s ", max, matches[idx]);
  1210. fprintf(e->el_outfile, "\n");
  1211. }
  1212. }
  1213. /*
  1214. * Complete the word at or before point, called by rl_complete()
  1215. * 'what_to_do' says what to do with the completion.
  1216. * `?' means list the possible completions.
  1217. * TAB means do standard completion.
  1218. * `*' means insert all of the possible completions.
  1219. * `!' means to do standard completion, and list all possible completions if
  1220. * there is more than one.
  1221. *
  1222. * Note: '*' support is not implemented
  1223. */
  1224. static int
  1225. rl_complete_internal(int what_to_do)
  1226. {
  1227. CPFunction *complet_func;
  1228. const LineInfo *li;
  1229. char *temp, **matches;
  1230. const char *ctemp;
  1231. size_t len;
  1232. rl_completion_type = what_to_do;
  1233. if (h == NULL || e == NULL)
  1234. rl_initialize();
  1235. complet_func = rl_completion_entry_function;
  1236. if (!complet_func)
  1237. complet_func = filename_completion_function;
  1238. /* We now look backwards for the start of a filename/variable word */
  1239. li = el_line(e);
  1240. ctemp = (const char *) li->cursor;
  1241. while (ctemp > li->buffer
  1242. && !strchr(rl_basic_word_break_characters, ctemp[-1])
  1243. && (!rl_special_prefixes
  1244. || !strchr(rl_special_prefixes, ctemp[-1]) ) )
  1245. ctemp--;
  1246. len = li->cursor - ctemp;
  1247. temp = alloca(len + 1);
  1248. (void) strncpy(temp, ctemp, len);
  1249. temp[len] = '\0';
  1250. /* these can be used by function called in completion_matches() */
  1251. /* or (*rl_attempted_completion_function)() */
  1252. rl_point = li->cursor - li->buffer;
  1253. rl_end = li->lastchar - li->buffer;
  1254. if (!rl_attempted_completion_function)
  1255. matches = completion_matches(temp, complet_func);
  1256. else {
  1257. int end = li->cursor - li->buffer;
  1258. matches = (*rl_attempted_completion_function) (temp, (int)
  1259. (end - len), end);
  1260. }
  1261. if (matches) {
  1262. int i, retval = CC_REFRESH;
  1263. int matches_num, maxlen, match_len, match_display=1;
  1264. /*
  1265. * Only replace the completed string with common part of
  1266. * possible matches if there is possible completion.
  1267. */
  1268. if (matches[0][0] != '\0') {
  1269. el_deletestr(e, (int) len);
  1270. el_insertstr(e, matches[0]);
  1271. }
  1272. if (what_to_do == '?')
  1273. goto display_matches;
  1274. if (matches[2] == NULL && strcmp(matches[0], matches[1]) == 0) {
  1275. /*
  1276. * We found exact match. Add a space after
  1277. * it, unless we do filename completition and the
  1278. * object is a directory.
  1279. */
  1280. size_t alen = strlen(matches[0]);
  1281. if ((complet_func != filename_completion_function
  1282. || (alen > 0 && (matches[0])[alen - 1] != '/'))
  1283. && rl_completion_append_character) {
  1284. char buf[2];
  1285. buf[0] = rl_completion_append_character;
  1286. buf[1] = '\0';
  1287. el_insertstr(e, buf);
  1288. }
  1289. } else if (what_to_do == '!') {
  1290. display_matches:
  1291. /*
  1292. * More than one match and requested to list possible
  1293. * matches.
  1294. */
  1295. for(i=1, maxlen=0; matches[i]; i++) {
  1296. match_len = strlen(matches[i]);
  1297. if (match_len > maxlen)
  1298. maxlen = match_len;
  1299. }
  1300. matches_num = i - 1;
  1301. /* newline to get on next line from command line */
  1302. fprintf(e->el_outfile, "\n");
  1303. /*
  1304. * If there are too many items, ask user for display
  1305. * confirmation.
  1306. */
  1307. if (matches_num > rl_completion_query_items) {
  1308. fprintf(e->el_outfile,
  1309. "Display all %d possibilities? (y or n) ",
  1310. matches_num);
  1311. fflush(e->el_outfile);
  1312. if (getc(stdin) != 'y')
  1313. match_display = 0;
  1314. fprintf(e->el_outfile, "\n");
  1315. }
  1316. if (match_display)
  1317. rl_display_match_list(matches, matches_num,
  1318. maxlen);
  1319. retval = CC_REDISPLAY;
  1320. } else if (matches[0][0]) {
  1321. /*
  1322. * There was some common match, but the name was
  1323. * not complete enough. Next tab will print possible
  1324. * completions.
  1325. */
  1326. el_beep(e);
  1327. } else {
  1328. /* lcd is not a valid object - further specification */
  1329. /* is needed */
  1330. el_beep(e);
  1331. retval = CC_NORM;
  1332. }
  1333. /* free elements of array and the array itself */
  1334. for (i = 0; matches[i]; i++)
  1335. free(matches[i]);
  1336. free(matches), matches = NULL;
  1337. return (retval);
  1338. }
  1339. return (CC_NORM);
  1340. }
  1341. /*
  1342. * complete word at current point
  1343. */
  1344. int
  1345. rl_complete(int ignore, int invoking_key)
  1346. {
  1347. if (h == NULL || e == NULL)
  1348. rl_initialize();
  1349. if (rl_inhibit_completion) {
  1350. rl_insert(ignore, invoking_key);
  1351. return (CC_REFRESH);
  1352. } else if (e->el_state.lastcmd == el_rl_complete_cmdnum)
  1353. return rl_complete_internal('?');
  1354. else if (_rl_complete_show_all)
  1355. return rl_complete_internal('!');
  1356. else
  1357. return (rl_complete_internal(TAB));
  1358. }
  1359. /*
  1360. * misc other functions
  1361. */
  1362. /*
  1363. * bind key c to readline-type function func
  1364. */
  1365. int
  1366. rl_bind_key(int c, int func(int, int))
  1367. {
  1368. int retval = -1;
  1369. if (h == NULL || e == NULL)
  1370. rl_initialize();
  1371. if (func == rl_insert) {
  1372. /* XXX notice there is no range checking of ``c'' */
  1373. e->el_map.key[c] = ED_INSERT;
  1374. retval = 0;
  1375. }
  1376. return (retval);
  1377. }
  1378. /*
  1379. * read one key from input - handles chars pushed back
  1380. * to input stream also
  1381. */
  1382. int
  1383. rl_read_key(void)
  1384. {
  1385. char fooarr[2 * sizeof(int)];
  1386. if (e == NULL || h == NULL)
  1387. rl_initialize();
  1388. return (el_getc(e, fooarr));
  1389. }
  1390. /*
  1391. * reset the terminal
  1392. */
  1393. /* ARGSUSED */
  1394. void
  1395. rl_reset_terminal(const char *p)
  1396. {
  1397. if (h == NULL || e == NULL)
  1398. rl_initialize();
  1399. el_reset(e);
  1400. }
  1401. /*
  1402. * insert character ``c'' back into input stream, ``count'' times
  1403. */
  1404. int
  1405. rl_insert(int count, int c)
  1406. {
  1407. char arr[2];
  1408. if (h == NULL || e == NULL)
  1409. rl_initialize();
  1410. /* XXX - int -> char conversion can lose on multichars */
  1411. arr[0] = c;
  1412. arr[1] = '\0';
  1413. for (; count > 0; count--)
  1414. el_push(e, arr);
  1415. return (0);
  1416. }