00001 /* $NetBSD: readline.c,v 1.49 2005/03/10 19:34:46 christos Exp $ */ 00002 00003 /*- 00004 * Copyright (c) 1997 The NetBSD Foundation, Inc. 00005 * All rights reserved. 00006 * 00007 * This code is derived from software contributed to The NetBSD Foundation 00008 * by Jaromir Dolecek. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions 00012 * are met: 00013 * 1. Redistributions of source code must retain the above copyright 00014 * notice, this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in the 00017 * documentation and/or other materials provided with the distribution. 00018 * 3. All advertising materials mentioning features or use of this software 00019 * must display the following acknowledgement: 00020 * This product includes software developed by the NetBSD 00021 * Foundation, Inc. and its contributors. 00022 * 4. Neither the name of The NetBSD Foundation nor the names of its 00023 * contributors may be used to endorse or promote products derived 00024 * from this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 00027 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00028 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00029 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 00030 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 */ 00038 00039 /* AIX requires this to be the first thing in the file. */ 00040 #if defined (_AIX) && !defined (__GNUC__) 00041 #pragma alloca 00042 #endif 00043 00044 #include <config.h> 00045 00046 #ifdef __GNUC__ 00047 # undef alloca 00048 # define alloca(n) __builtin_alloca (n) 00049 #else 00050 # ifdef HAVE_ALLOCA_H 00051 # include <alloca.h> 00052 # else 00053 # ifndef _AIX 00054 extern char *alloca (); 00055 # endif 00056 # endif 00057 #endif 00058 00059 #include <sys/types.h> 00060 #include <sys/stat.h> 00061 #include <stdio.h> 00062 #include <dirent.h> 00063 #include <string.h> 00064 #include <pwd.h> 00065 #include <ctype.h> 00066 #include <stdlib.h> 00067 #include <unistd.h> 00068 #include <limits.h> 00069 #include <errno.h> 00070 #include <fcntl.h> 00071 #include <vis.h> 00072 00073 #include "readline/readline.h" 00074 #include "el.h" 00075 #include "fcns.h" /* for EL_NUM_FCNS */ 00076 #include "histedit.h" 00077 00078 /* for rl_complete() */ 00079 #define TAB '\r' 00080 00081 /* see comment at the #ifdef for sense of this */ 00082 /* #define GDB_411_HACK */ 00083 00084 /* readline compatibility stuff - look at readline sources/documentation */ 00085 /* to see what these variables mean */ 00086 const char *rl_library_version = "EditLine wrapper"; 00087 static char empty[] = { '\0' }; 00088 static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' }; 00089 static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$', 00090 '>', '<', '=', ';', '|', '&', '{', '(', '\0' }; 00091 char *rl_readline_name = empty; 00092 FILE *rl_instream = NULL; 00093 FILE *rl_outstream = NULL; 00094 int rl_point = 0; 00095 int rl_end = 0; 00096 char *rl_line_buffer = NULL; 00097 VFunction *rl_linefunc = NULL; 00098 int rl_done = 0; 00099 VFunction *rl_event_hook = NULL; 00100 00101 int history_base = 1; /* probably never subject to change */ 00102 int history_length = 0; 00103 int max_input_history = 0; 00104 char history_expansion_char = '!'; 00105 char history_subst_char = '^'; 00106 char *history_no_expand_chars = expand_chars; 00107 Function *history_inhibit_expansion_function = NULL; 00108 char *history_arg_extract(int start, int end, const char *str); 00109 00110 int rl_inhibit_completion = 0; 00111 int rl_attempted_completion_over = 0; 00112 char *rl_basic_word_break_characters = break_chars; 00113 char *rl_completer_word_break_characters = NULL; 00114 char *rl_completer_quote_characters = NULL; 00115 Function *rl_completion_entry_function = NULL; 00116 CPPFunction *rl_attempted_completion_function = NULL; 00117 Function *rl_pre_input_hook = NULL; 00118 Function *rl_startup1_hook = NULL; 00119 Function *rl_getc_function = NULL; 00120 char *rl_terminal_name = NULL; 00121 int rl_already_prompted = 0; 00122 int rl_filename_completion_desired = 0; 00123 int rl_ignore_completion_duplicates = 0; 00124 int rl_catch_signals = 1; 00125 VFunction *rl_redisplay_function = NULL; 00126 Function *rl_startup_hook = NULL; 00127 VFunction *rl_completion_display_matches_hook = NULL; 00128 VFunction *rl_prep_term_function = NULL; 00129 VFunction *rl_deprep_term_function = NULL; 00130 00131 /* 00132 * The current prompt string. 00133 */ 00134 char *rl_prompt = NULL; 00135 /* 00136 * This is set to character indicating type of completion being done by 00137 * rl_complete_internal(); this is available for application completion 00138 * functions. 00139 */ 00140 int rl_completion_type = 0; 00141 00142 /* 00143 * If more than this number of items results from query for possible 00144 * completions, we ask user if they are sure to really display the list. 00145 */ 00146 int rl_completion_query_items = 100; 00147 00148 /* 00149 * List of characters which are word break characters, but should be left 00150 * in the parsed text when it is passed to the completion function. 00151 * Shell uses this to help determine what kind of completing to do. 00152 */ 00153 char *rl_special_prefixes = (char *)NULL; 00154 00155 /* 00156 * This is the character appended to the completed words if at the end of 00157 * the line. Default is ' ' (a space). 00158 */ 00159 int rl_completion_append_character = ' '; 00160 00161 /* stuff below is used internally by libedit for readline emulation */ 00162 00163 /* if not zero, non-unique completions always show list of possible matches */ 00164 static int _rl_complete_show_all = 0; 00165 00166 static History *h = NULL; 00167 static EditLine *e = NULL; 00168 static Function *map[256]; 00169 static int el_rl_complete_cmdnum = 0; 00170 00171 /* internal functions */ 00172 static unsigned char _el_rl_complete(EditLine *, int); 00173 static unsigned char _el_rl_tstp(EditLine *, int); 00174 static char *_get_prompt(EditLine *); 00175 static HIST_ENTRY *_move_history(int); 00176 static int _history_expand_command(const char *, size_t, size_t, 00177 char **); 00178 static char *_rl_compat_sub(const char *, const char *, 00179 const char *, int); 00180 static int _rl_complete_internal(int); 00181 static int _rl_qsort_string_compare(const void *, const void *); 00182 static int _rl_event_read_char(EditLine *, char *); 00183 static void _rl_update_pos(void); 00184 00185 00186 /* ARGSUSED */ 00187 static char * 00188 _get_prompt(EditLine *el __attribute__((__unused__))) 00189 { 00190 rl_already_prompted = 1; 00191 return (rl_prompt); 00192 } 00193 00194 00195 /* 00196 * generic function for moving around history 00197 */ 00198 static HIST_ENTRY * 00199 _move_history(int op) 00200 { 00201 HistEvent ev; 00202 static HIST_ENTRY rl_he; 00203 00204 if (history(h, &ev, op) != 0) 00205 return (HIST_ENTRY *) NULL; 00206 00207 rl_he.line = ev.str; 00208 rl_he.data = (histdata_t) &(ev.num); 00209 00210 return (&rl_he); 00211 } 00212 00213 00214 /* 00215 * READLINE compatibility stuff 00216 */ 00217 00218 /* 00219 * initialize rl compat stuff 00220 */ 00221 int 00222 rl_initialize(void) 00223 { 00224 HistEvent ev; 00225 const LineInfo *li; 00226 int i; 00227 int editmode = 1; 00228 struct termios t; 00229 00230 if (e != NULL) 00231 el_end(e); 00232 if (h != NULL) 00233 history_end(h); 00234 00235 if (!rl_instream) 00236 rl_instream = stdin; 00237 if (!rl_outstream) 00238 rl_outstream = stdout; 00239 00240 /* 00241 * See if we don't really want to run the editor 00242 */ 00243 if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0) 00244 editmode = 0; 00245 00246 e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr); 00247 00248 if (!editmode) 00249 el_set(e, EL_EDITMODE, 0); 00250 00251 h = history_init(); 00252 if (!e || !h) 00253 return (-1); 00254 00255 history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */ 00256 history_length = 0; 00257 max_input_history = INT_MAX; 00258 el_set(e, EL_HIST, history, h); 00259 00260 /* for proper prompt printing in readline() */ 00261 rl_prompt = strdup(""); 00262 if (rl_prompt == NULL) { 00263 history_end(h); 00264 el_end(e); 00265 return -1; 00266 } 00267 el_set(e, EL_PROMPT, _get_prompt); 00268 el_set(e, EL_SIGNAL, rl_catch_signals); 00269 00270 /* set default mode to "emacs"-style and read setting afterwards */ 00271 /* so this can be overriden */ 00272 el_set(e, EL_EDITOR, "emacs"); 00273 if (rl_terminal_name != NULL) 00274 el_set(e, EL_TERMINAL, rl_terminal_name); 00275 else 00276 el_get(e, EL_TERMINAL, &rl_terminal_name); 00277 00278 /* 00279 * Word completion - this has to go AFTER rebinding keys 00280 * to emacs-style. 00281 */ 00282 el_set(e, EL_ADDFN, "rl_complete", 00283 "ReadLine compatible completion function", 00284 _el_rl_complete); 00285 el_set(e, EL_BIND, "^I", "rl_complete", NULL); 00286 00287 /* 00288 * Send TSTP when ^Z is pressed. 00289 */ 00290 el_set(e, EL_ADDFN, "rl_tstp", 00291 "ReadLine compatible suspend function", 00292 _el_rl_tstp); 00293 el_set(e, EL_BIND, "^Z", "rl_tstp", NULL); 00294 00295 /* 00296 * Find out where the rl_complete function was added; this is 00297 * used later to detect that lastcmd was also rl_complete. 00298 */ 00299 for(i=EL_NUM_FCNS; i < e->el_map.nfunc; i++) { 00300 if (e->el_map.func[i] == _el_rl_complete) { 00301 el_rl_complete_cmdnum = i; 00302 break; 00303 } 00304 } 00305 00306 /* read settings from configuration file */ 00307 el_source(e, NULL); 00308 00309 /* 00310 * Unfortunately, some applications really do use rl_point 00311 * and rl_line_buffer directly. 00312 */ 00313 li = el_line(e); 00314 /* a cheesy way to get rid of const cast. */ 00315 rl_line_buffer = memchr(li->buffer, *li->buffer, 1); 00316 _rl_update_pos(); 00317 00318 if (rl_startup_hook) 00319 (*rl_startup_hook)(NULL, 0); 00320 00321 return (0); 00322 } 00323 00324 00325 /* 00326 * read one line from input stream and return it, chomping 00327 * trailing newline (if there is any) 00328 */ 00329 char * 00330 readline(const char *prompt) 00331 { 00332 HistEvent ev; 00333 int count; 00334 const char *ret; 00335 char *buf; 00336 static int used_event_hook; 00337 00338 if (e == NULL || h == NULL) 00339 rl_initialize(); 00340 00341 rl_done = 0; 00342 00343 /* update prompt accordingly to what has been passed */ 00344 if (!prompt) 00345 prompt = ""; 00346 if (strcmp(rl_prompt, prompt) != 0) { 00347 free(rl_prompt); 00348 rl_prompt = strdup(prompt); 00349 if (rl_prompt == NULL) 00350 return NULL; 00351 } 00352 00353 if (rl_pre_input_hook) 00354 (*rl_pre_input_hook)(NULL, 0); 00355 00356 if (rl_event_hook && !(e->el_flags&NO_TTY)) { 00357 el_set(e, EL_GETCFN, _rl_event_read_char); 00358 used_event_hook = 1; 00359 } 00360 00361 if (!rl_event_hook && used_event_hook) { 00362 el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN); 00363 used_event_hook = 0; 00364 } 00365 00366 rl_already_prompted = 0; 00367 00368 /* get one line from input stream */ 00369 ret = el_gets(e, &count); 00370 00371 if (ret && count > 0) { 00372 int lastidx; 00373 00374 buf = strdup(ret); 00375 if (buf == NULL) 00376 return NULL; 00377 lastidx = count - 1; 00378 if (buf[lastidx] == '\n') 00379 buf[lastidx] = '\0'; 00380 } else 00381 buf = NULL; 00382 00383 history(h, &ev, H_GETSIZE); 00384 history_length = ev.num; 00385 00386 return buf; 00387 } 00388 00389 /* 00390 * history functions 00391 */ 00392 00393 /* 00394 * is normally called before application starts to use 00395 * history expansion functions 00396 */ 00397 void 00398 using_history(void) 00399 { 00400 if (h == NULL || e == NULL) 00401 rl_initialize(); 00402 } 00403 00404 00405 /* 00406 * substitute ``what'' with ``with'', returning resulting string; if 00407 * globally == 1, substitutes all occurrences of what, otherwise only the 00408 * first one 00409 */ 00410 static char * 00411 _rl_compat_sub(const char *str, const char *what, const char *with, 00412 int globally) 00413 { 00414 const char *s; 00415 char *r, *result; 00416 size_t len, with_len, what_len; 00417 00418 len = strlen(str); 00419 with_len = strlen(with); 00420 what_len = strlen(what); 00421 00422 /* calculate length we need for result */ 00423 s = str; 00424 while (*s) { 00425 if (*s == *what && !strncmp(s, what, what_len)) { 00426 len += with_len - what_len; 00427 if (!globally) 00428 break; 00429 s += what_len; 00430 } else 00431 s++; 00432 } 00433 r = result = malloc(len + 1); 00434 if (result == NULL) 00435 return NULL; 00436 s = str; 00437 while (*s) { 00438 if (*s == *what && !strncmp(s, what, what_len)) { 00439 (void)strncpy(r, with, with_len); 00440 r += with_len; 00441 s += what_len; 00442 if (!globally) { 00443 (void)strcpy(r, s); 00444 return(result); 00445 } 00446 } else 00447 *r++ = *s++; 00448 } 00449 *r = 0; 00450 return(result); 00451 } 00452 00453 static char *last_search_pat; /* last !?pat[?] search pattern */ 00454 static char *last_search_match; /* last !?pat[?] that matched */ 00455 00456 const char * 00457 get_history_event(const char *cmd, int *cindex, int qchar) 00458 { 00459 int idx, sign, sub, num, begin, ret; 00460 size_t len; 00461 char *pat; 00462 const char *rptr; 00463 HistEvent ev; 00464 00465 idx = *cindex; 00466 if (cmd[idx++] != history_expansion_char) 00467 return(NULL); 00468 00469 /* find out which event to take */ 00470 if (cmd[idx] == history_expansion_char || cmd[idx] == 0) { 00471 if (history(h, &ev, H_FIRST) != 0) 00472 return(NULL); 00473 *cindex = cmd[idx]? (idx + 1):idx; 00474 return(ev.str); 00475 } 00476 sign = 0; 00477 if (cmd[idx] == '-') { 00478 sign = 1; 00479 idx++; 00480 } 00481 00482 if ('0' <= cmd[idx] && cmd[idx] <= '9') { 00483 HIST_ENTRY *rl_he; 00484 00485 num = 0; 00486 while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') { 00487 num = num * 10 + cmd[idx] - '0'; 00488 idx++; 00489 } 00490 if (sign) 00491 num = history_length - num + 1; 00492 00493 if (!(rl_he = history_get(num))) 00494 return(NULL); 00495 00496 *cindex = idx; 00497 return(rl_he->line); 00498 } 00499 sub = 0; 00500 if (cmd[idx] == '?') { 00501 sub = 1; 00502 idx++; 00503 } 00504 begin = idx; 00505 while (cmd[idx]) { 00506 if (cmd[idx] == '\n') 00507 break; 00508 if (sub && cmd[idx] == '?') 00509 break; 00510 if (!sub && (cmd[idx] == ':' || cmd[idx] == ' ' 00511 || cmd[idx] == '\t' || cmd[idx] == qchar)) 00512 break; 00513 idx++; 00514 } 00515 len = idx - begin; 00516 if (sub && cmd[idx] == '?') 00517 idx++; 00518 if (sub && len == 0 && last_search_pat && *last_search_pat) 00519 pat = last_search_pat; 00520 else if (len == 0) 00521 return(NULL); 00522 else { 00523 if ((pat = malloc(len + 1)) == NULL) 00524 return NULL; 00525 (void)strncpy(pat, cmd + begin, len); 00526 pat[len] = '\0'; 00527 } 00528 00529 if (history(h, &ev, H_CURR) != 0) { 00530 if (pat != last_search_pat) 00531 free(pat); 00532 return (NULL); 00533 } 00534 num = ev.num; 00535 00536 if (sub) { 00537 if (pat != last_search_pat) { 00538 if (last_search_pat) 00539 free(last_search_pat); 00540 last_search_pat = pat; 00541 } 00542 ret = history_search(pat, -1); 00543 } else 00544 ret = history_search_prefix(pat, -1); 00545 00546 if (ret == -1) { 00547 /* restore to end of list on failed search */ 00548 history(h, &ev, H_FIRST); 00549 (void)fprintf(rl_outstream, "%s: Event not found\n", pat); 00550 if (pat != last_search_pat) 00551 free(pat); 00552 return(NULL); 00553 } 00554 00555 if (sub && len) { 00556 if (last_search_match && last_search_match != pat) 00557 free(last_search_match); 00558 last_search_match = pat; 00559 } 00560 00561 if (pat != last_search_pat) 00562 free(pat); 00563 00564 if (history(h, &ev, H_CURR) != 0) 00565 return(NULL); 00566 *cindex = idx; 00567 rptr = ev.str; 00568 00569 /* roll back to original position */ 00570 (void)history(h, &ev, H_SET, num); 00571 00572 return rptr; 00573 } 00574 00575 /* 00576 * the real function doing history expansion - takes as argument command 00577 * to do and data upon which the command should be executed 00578 * does expansion the way I've understood readline documentation 00579 * 00580 * returns 0 if data was not modified, 1 if it was and 2 if the string 00581 * should be only printed and not executed; in case of error, 00582 * returns -1 and *result points to NULL 00583 * it's callers responsibility to free() string returned in *result 00584 */ 00585 static int 00586 _history_expand_command(const char *command, size_t offs, size_t cmdlen, 00587 char **result) 00588 { 00589 char *tmp, *search = NULL, *aptr; 00590 const char *ptr, *cmd; 00591 static char *from = NULL, *to = NULL; 00592 int start, end, idx, has_mods = 0; 00593 int p_on = 0, g_on = 0; 00594 00595 *result = NULL; 00596 aptr = NULL; 00597 ptr = NULL; 00598 00599 /* First get event specifier */ 00600 idx = 0; 00601 00602 if (strchr(":^*$", command[offs + 1])) { 00603 char str[4]; 00604 /* 00605 * "!:" is shorthand for "!!:". 00606 * "!^", "!*" and "!$" are shorthand for 00607 * "!!:^", "!!:*" and "!!:$" respectively. 00608 */ 00609 str[0] = str[1] = '!'; 00610 str[2] = '0'; 00611 ptr = get_history_event(str, &idx, 0); 00612 idx = (command[offs + 1] == ':')? 1:0; 00613 has_mods = 1; 00614 } else { 00615 if (command[offs + 1] == '#') { 00616 /* use command so far */ 00617 if ((aptr = malloc(offs + 1)) == NULL) 00618 return -1; 00619 (void)strncpy(aptr, command, offs); 00620 aptr[offs] = '\0'; 00621 idx = 1; 00622 } else { 00623 int qchar; 00624 00625 qchar = (offs > 0 && command[offs - 1] == '"')? '"':0; 00626 ptr = get_history_event(command + offs, &idx, qchar); 00627 } 00628 has_mods = command[offs + idx] == ':'; 00629 } 00630 00631 if (ptr == NULL && aptr == NULL) 00632 return(-1); 00633 00634 if (!has_mods) { 00635 *result = strdup(aptr? aptr : ptr); 00636 if (aptr) 00637 free(aptr); 00638 return(1); 00639 } 00640 00641 cmd = command + offs + idx + 1; 00642 00643 /* Now parse any word designators */ 00644 00645 if (*cmd == '%') /* last word matched by ?pat? */ 00646 tmp = strdup(last_search_match? last_search_match:""); 00647 else if (strchr("^*$-0123456789", *cmd)) { 00648 start = end = -1; 00649 if (*cmd == '^') 00650 start = end = 1, cmd++; 00651 else if (*cmd == '$') 00652 start = -1, cmd++; 00653 else if (*cmd == '*') 00654 start = 1, cmd++; 00655 else if (*cmd == '-' || isdigit((unsigned char) *cmd)) { 00656 start = 0; 00657 while (*cmd && '0' <= *cmd && *cmd <= '9') 00658 start = start * 10 + *cmd++ - '0'; 00659 00660 if (*cmd == '-') { 00661 if (isdigit((unsigned char) cmd[1])) { 00662 cmd++; 00663 end = 0; 00664 while (*cmd && '0' <= *cmd && *cmd <= '9') 00665 end = end * 10 + *cmd++ - '0'; 00666 } else if (cmd[1] == '$') { 00667 cmd += 2; 00668 end = -1; 00669 } else { 00670 cmd++; 00671 end = -2; 00672 } 00673 } else if (*cmd == '*') 00674 end = -1, cmd++; 00675 else 00676 end = start; 00677 } 00678 tmp = history_arg_extract(start, end, aptr? aptr:ptr); 00679 if (tmp == NULL) { 00680 (void)fprintf(rl_outstream, "%s: Bad word specifier", 00681 command + offs + idx); 00682 if (aptr) 00683 free(aptr); 00684 return(-1); 00685 } 00686 } else 00687 tmp = strdup(aptr? aptr:ptr); 00688 00689 if (aptr) 00690 free(aptr); 00691 00692 if (*cmd == 0 || (cmd - (command + offs) >= cmdlen)) { 00693 *result = tmp; 00694 return(1); 00695 } 00696 00697 for (; *cmd; cmd++) { 00698 if (*cmd == ':') 00699 continue; 00700 else if (*cmd == 'h') { /* remove trailing path */ 00701 if ((aptr = strrchr(tmp, '/')) != NULL) 00702 *aptr = 0; 00703 } else if (*cmd == 't') { /* remove leading path */ 00704 if ((aptr = strrchr(tmp, '/')) != NULL) { 00705 aptr = strdup(aptr + 1); 00706 free(tmp); 00707 tmp = aptr; 00708 } 00709 } else if (*cmd == 'r') { /* remove trailing suffix */ 00710 if ((aptr = strrchr(tmp, '.')) != NULL) 00711 *aptr = 0; 00712 } else if (*cmd == 'e') { /* remove all but suffix */ 00713 if ((aptr = strrchr(tmp, '.')) != NULL) { 00714 aptr = strdup(aptr); 00715 free(tmp); 00716 tmp = aptr; 00717 } 00718 } else if (*cmd == 'p') /* print only */ 00719 p_on = 1; 00720 else if (*cmd == 'g') 00721 g_on = 2; 00722 else if (*cmd == 's' || *cmd == '&') { 00723 char *what, *with, delim; 00724 size_t len, from_len; 00725 size_t size; 00726 00727 if (*cmd == '&' && (from == NULL || to == NULL)) 00728 continue; 00729 else if (*cmd == 's') { 00730 delim = *(++cmd), cmd++; 00731 size = 16; 00732 what = realloc(from, size); 00733 if (what == NULL) { 00734 free(from); 00735 return 0; 00736 } 00737 len = 0; 00738 for (; *cmd && *cmd != delim; cmd++) { 00739 if (*cmd == '\\' && cmd[1] == delim) 00740 cmd++; 00741 if (len >= size) { 00742 char *nwhat; 00743 nwhat = realloc(what, 00744 (size <<= 1)); 00745 if (nwhat == NULL) { 00746 free(what); 00747 return 0; 00748 } 00749 what = nwhat; 00750 } 00751 what[len++] = *cmd; 00752 } 00753 what[len] = '\0'; 00754 from = what; 00755 if (*what == '\0') { 00756 free(what); 00757 if (search) { 00758 from = strdup(search); 00759 if (from == NULL) 00760 return 0; 00761 } else { 00762 from = NULL; 00763 return (-1); 00764 } 00765 } 00766 cmd++; /* shift after delim */ 00767 if (!*cmd) 00768 continue; 00769 00770 size = 16; 00771 with = realloc(to, size); 00772 if (with == NULL) { 00773 free(to); 00774 return -1; 00775 } 00776 len = 0; 00777 from_len = strlen(from); 00778 for (; *cmd && *cmd != delim; cmd++) { 00779 if (len + from_len + 1 >= size) { 00780 char *nwith; 00781 size += from_len + 1; 00782 nwith = realloc(with, size); 00783 if (nwith == NULL) { 00784 free(with); 00785 return -1; 00786 } 00787 with = nwith; 00788 } 00789 if (*cmd == '&') { 00790 /* safe */ 00791 (void)strcpy(&with[len], from); 00792 len += from_len; 00793 continue; 00794 } 00795 if (*cmd == '\\' 00796 && (*(cmd + 1) == delim 00797 || *(cmd + 1) == '&')) 00798 cmd++; 00799 with[len++] = *cmd; 00800 } 00801 with[len] = '\0'; 00802 to = with; 00803 } 00804 00805 aptr = _rl_compat_sub(tmp, from, to, g_on); 00806 if (aptr) { 00807 free(tmp); 00808 tmp = aptr; 00809 } 00810 g_on = 0; 00811 } 00812 } 00813 *result = tmp; 00814 return (p_on? 2:1); 00815 } 00816 00817 00818 /* 00819 * csh-style history expansion 00820 */ 00821 int 00822 history_expand(char *str, char **output) 00823 { 00824 int ret = 0; 00825 size_t idx, i, size; 00826 char *tmp, *result; 00827 00828 if (h == NULL || e == NULL) 00829 rl_initialize(); 00830 00831 if (history_expansion_char == 0) { 00832 *output = strdup(str); 00833 return(0); 00834 } 00835 00836 *output = NULL; 00837 if (str[0] == history_subst_char) { 00838 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */ 00839 *output = malloc(strlen(str) + 4 + 1); 00840 if (*output == NULL) 00841 return 0; 00842 (*output)[0] = (*output)[1] = history_expansion_char; 00843 (*output)[2] = ':'; 00844 (*output)[3] = 's'; 00845 (void)strcpy((*output) + 4, str); 00846 str = *output; 00847 } else { 00848 *output = strdup(str); 00849 if (*output == NULL) 00850 return 0; 00851 } 00852 00853 #define ADD_STRING(what, len) \ 00854 { \ 00855 if (idx + len + 1 > size) { \ 00856 char *nresult = realloc(result, (size += len + 1));\ 00857 if (nresult == NULL) { \ 00858 free(*output); \ 00859 return 0; \ 00860 } \ 00861 result = nresult; \ 00862 } \ 00863 (void)strncpy(&result[idx], what, len); \ 00864 idx += len; \ 00865 result[idx] = '\0'; \ 00866 } 00867 00868 result = NULL; 00869 size = idx = 0; 00870 for (i = 0; str[i];) { 00871 int qchar, loop_again; 00872 size_t len, start, j; 00873 00874 qchar = 0; 00875 loop_again = 1; 00876 start = j = i; 00877 loop: 00878 for (; str[j]; j++) { 00879 if (str[j] == '\\' && 00880 str[j + 1] == history_expansion_char) { 00881 (void)strcpy(&str[j], &str[j + 1]); 00882 continue; 00883 } 00884 if (!loop_again) { 00885 if (isspace((unsigned char) str[j]) 00886 || str[j] == qchar) 00887 break; 00888 } 00889 if (str[j] == history_expansion_char 00890 && !strchr(history_no_expand_chars, str[j + 1]) 00891 && (!history_inhibit_expansion_function || 00892 (*history_inhibit_expansion_function)(str, 00893 (int)j) == 0)) 00894 break; 00895 } 00896 00897 if (str[j] && loop_again) { 00898 i = j; 00899 qchar = (j > 0 && str[j - 1] == '"' )? '"':0; 00900 j++; 00901 if (str[j] == history_expansion_char) 00902 j++; 00903 loop_again = 0; 00904 goto loop; 00905 } 00906 len = i - start; 00907 tmp = &str[start]; 00908 ADD_STRING(tmp, len); 00909 00910 if (str[i] == '\0' || str[i] != history_expansion_char) { 00911 len = j - i; 00912 tmp = &str[i]; 00913 ADD_STRING(tmp, len); 00914 if (start == 0) 00915 ret = 0; 00916 else 00917 ret = 1; 00918 break; 00919 } 00920 ret = _history_expand_command (str, i, (j - i), &tmp); 00921 if (ret > 0 && tmp) { 00922 len = strlen(tmp); 00923 ADD_STRING(tmp, len); 00924 free(tmp); 00925 } 00926 i = j; 00927 } 00928 00929 /* ret is 2 for "print only" option */ 00930 if (ret == 2) { 00931 add_history(result); 00932 #ifdef GDB_411_HACK 00933 /* gdb 4.11 has been shipped with readline, where */ 00934 /* history_expand() returned -1 when the line */ 00935 /* should not be executed; in readline 2.1+ */ 00936 /* it should return 2 in such a case */ 00937 ret = -1; 00938 #endif 00939 } 00940 free(*output); 00941 *output = result; 00942 00943 return (ret); 00944 } 00945 00946 /* 00947 * Return a string consisting of arguments of "str" from "start" to "end". 00948 */ 00949 char * 00950 history_arg_extract(int start, int end, const char *str) 00951 { 00952 size_t i, len, max; 00953 char **arr, *result; 00954 00955 arr = history_tokenize(str); 00956 if (!arr) 00957 return(NULL); 00958 if (arr && *arr == NULL) { 00959 free(arr); 00960 return(NULL); 00961 } 00962 00963 for (max = 0; arr[max]; max++) 00964 continue; 00965 max--; 00966 00967 if (start == '$') 00968 start = max; 00969 if (end == '$') 00970 end = max; 00971 if (end < 0) 00972 end = max + end + 1; 00973 if (start < 0) 00974 start = end; 00975 00976 if (start < 0 || end < 0 || start > max || end > max || start > end) 00977 return(NULL); 00978 00979 for (i = start, len = 0; i <= end; i++) 00980 len += strlen(arr[i]) + 1; 00981 len++; 00982 result = malloc(len); 00983 if (result == NULL) 00984 return NULL; 00985 00986 for (i = start, len = 0; i <= end; i++) { 00987 (void)strcpy(result + len, arr[i]); 00988 len += strlen(arr[i]); 00989 if (i < end) 00990 result[len++] = ' '; 00991 } 00992 result[len] = 0; 00993 00994 for (i = 0; arr[i]; i++) 00995 free(arr[i]); 00996 free(arr); 00997 00998 return(result); 00999 } 01000 01001 /* 01002 * Parse the string into individual tokens, 01003 * similar to how shell would do it. 01004 */ 01005 char ** 01006 history_tokenize(const char *str) 01007 { 01008 int size = 1, idx = 0, i, start; 01009 size_t len; 01010 char **result = NULL, *temp, delim = '\0'; 01011 01012 for (i = 0; str[i];) { 01013 while (isspace((unsigned char) str[i])) 01014 i++; 01015 start = i; 01016 for (; str[i];) { 01017 if (str[i] == '\\') { 01018 if (str[i+1] != '\0') 01019 i++; 01020 } else if (str[i] == delim) 01021 delim = '\0'; 01022 else if (!delim && 01023 (isspace((unsigned char) str[i]) || 01024 strchr("()<>;&|$", str[i]))) 01025 break; 01026 else if (!delim && strchr("'`\"", str[i])) 01027 delim = str[i]; 01028 if (str[i]) 01029 i++; 01030 } 01031 01032 if (idx + 2 >= size) { 01033 char **nresult; 01034 size <<= 1; 01035 nresult = realloc(result, size * sizeof(char *)); 01036 if (nresult == NULL) { 01037 free(result); 01038 return NULL; 01039 } 01040 result = nresult; 01041 } 01042 len = i - start; 01043 temp = malloc(len + 1); 01044 if (temp == NULL) { 01045 for (i = 0; i < idx; i++) 01046 free(result[i]); 01047 free(result); 01048 return NULL; 01049 } 01050 (void)strncpy(temp, &str[start], len); 01051 temp[len] = '\0'; 01052 result[idx++] = temp; 01053 result[idx] = NULL; 01054 if (str[i]) 01055 i++; 01056 } 01057 return (result); 01058 } 01059 01060 01061 /* 01062 * limit size of history record to ``max'' events 01063 */ 01064 void 01065 stifle_history(int max) 01066 { 01067 HistEvent ev; 01068 01069 if (h == NULL || e == NULL) 01070 rl_initialize(); 01071 01072 if (history(h, &ev, H_SETSIZE, max) == 0) 01073 max_input_history = max; 01074 } 01075 01076 01077 /* 01078 * "unlimit" size of history - set the limit to maximum allowed int value 01079 */ 01080 int 01081 unstifle_history(void) 01082 { 01083 HistEvent ev; 01084 int omax; 01085 01086 history(h, &ev, H_SETSIZE, INT_MAX); 01087 omax = max_input_history; 01088 max_input_history = INT_MAX; 01089 return (omax); /* some value _must_ be returned */ 01090 } 01091 01092 01093 int 01094 history_is_stifled(void) 01095 { 01096 01097 /* cannot return true answer */ 01098 return (max_input_history != INT_MAX); 01099 } 01100 01101 01102 /* 01103 * read history from a file given 01104 */ 01105 int 01106 read_history(const char *filename) 01107 { 01108 HistEvent ev; 01109 01110 if (h == NULL || e == NULL) 01111 rl_initialize(); 01112 return (history(h, &ev, H_LOAD, filename) == -1); 01113 } 01114 01115 01116 /* 01117 * write history to a file given 01118 */ 01119 int 01120 write_history(const char *filename) 01121 { 01122 HistEvent ev; 01123 01124 if (h == NULL || e == NULL) 01125 rl_initialize(); 01126 return (history(h, &ev, H_SAVE, filename) == -1); 01127 } 01128 01129 01130 /* 01131 * returns history ``num''th event 01132 * 01133 * returned pointer points to static variable 01134 */ 01135 HIST_ENTRY * 01136 history_get(int num) 01137 { 01138 static HIST_ENTRY she; 01139 HistEvent ev; 01140 int curr_num; 01141 01142 if (h == NULL || e == NULL) 01143 rl_initialize(); 01144 01145 /* save current position */ 01146 if (history(h, &ev, H_CURR) != 0) 01147 return (NULL); 01148 curr_num = ev.num; 01149 01150 /* start from most recent */ 01151 if (history(h, &ev, H_FIRST) != 0) 01152 return (NULL); /* error */ 01153 01154 /* look backwards for event matching specified offset */ 01155 if (history(h, &ev, H_NEXT_EVENT, num)) 01156 return (NULL); 01157 01158 she.line = ev.str; 01159 she.data = NULL; 01160 01161 /* restore pointer to where it was */ 01162 (void)history(h, &ev, H_SET, curr_num); 01163 01164 return (&she); 01165 } 01166 01167 01168 /* 01169 * add the line to history table 01170 */ 01171 int 01172 add_history(const char *line) 01173 { 01174 HistEvent ev; 01175 01176 if (h == NULL || e == NULL) 01177 rl_initialize(); 01178 01179 (void)history(h, &ev, H_ENTER, line); 01180 if (history(h, &ev, H_GETSIZE) == 0) 01181 history_length = ev.num; 01182 01183 return (!(history_length > 0)); /* return 0 if all is okay */ 01184 } 01185 01186 01187 /* 01188 * clear the history list - delete all entries 01189 */ 01190 void 01191 clear_history(void) 01192 { 01193 HistEvent ev; 01194 01195 history(h, &ev, H_CLEAR); 01196 } 01197 01198 01199 /* 01200 * returns offset of the current history event 01201 */ 01202 int 01203 where_history(void) 01204 { 01205 HistEvent ev; 01206 int curr_num, off; 01207 01208 if (history(h, &ev, H_CURR) != 0) 01209 return (0); 01210 curr_num = ev.num; 01211 01212 history(h, &ev, H_FIRST); 01213 off = 1; 01214 while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0) 01215 off++; 01216 01217 return (off); 01218 } 01219 01220 01221 /* 01222 * returns current history event or NULL if there is no such event 01223 */ 01224 HIST_ENTRY * 01225 current_history(void) 01226 { 01227 01228 return (_move_history(H_CURR)); 01229 } 01230 01231 01232 /* 01233 * returns total number of bytes history events' data are using 01234 */ 01235 int 01236 history_total_bytes(void) 01237 { 01238 HistEvent ev; 01239 int curr_num, size; 01240 01241 if (history(h, &ev, H_CURR) != 0) 01242 return (-1); 01243 curr_num = ev.num; 01244 01245 history(h, &ev, H_FIRST); 01246 size = 0; 01247 do 01248 size += strlen(ev.str); 01249 while (history(h, &ev, H_NEXT) == 0); 01250 01251 /* get to the same position as before */ 01252 history(h, &ev, H_PREV_EVENT, curr_num); 01253 01254 return (size); 01255 } 01256 01257 01258 /* 01259 * sets the position in the history list to ``pos'' 01260 */ 01261 int 01262 history_set_pos(int pos) 01263 { 01264 HistEvent ev; 01265 int curr_num; 01266 01267 if (pos > history_length || pos < 0) 01268 return (-1); 01269 01270 history(h, &ev, H_CURR); 01271 curr_num = ev.num; 01272 01273 if (history(h, &ev, H_SET, pos)) { 01274 history(h, &ev, H_SET, curr_num); 01275 return(-1); 01276 } 01277 return (0); 01278 } 01279 01280 01281 /* 01282 * returns previous event in history and shifts pointer accordingly 01283 */ 01284 HIST_ENTRY * 01285 previous_history(void) 01286 { 01287 01288 return (_move_history(H_PREV)); 01289 } 01290 01291 01292 /* 01293 * returns next event in history and shifts pointer accordingly 01294 */ 01295 HIST_ENTRY * 01296 next_history(void) 01297 { 01298 01299 return (_move_history(H_NEXT)); 01300 } 01301 01302 01303 /* 01304 * searches for first history event containing the str 01305 */ 01306 int 01307 history_search(const char *str, int direction) 01308 { 01309 HistEvent ev; 01310 const char *strp; 01311 int curr_num; 01312 01313 if (history(h, &ev, H_CURR) != 0) 01314 return (-1); 01315 curr_num = ev.num; 01316 01317 for (;;) { 01318 if ((strp = strstr(ev.str, str)) != NULL) 01319 return (int) (strp - ev.str); 01320 if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0) 01321 break; 01322 } 01323 history(h, &ev, H_SET, curr_num); 01324 return (-1); 01325 } 01326 01327 01328 /* 01329 * searches for first history event beginning with str 01330 */ 01331 int 01332 history_search_prefix(const char *str, int direction) 01333 { 01334 HistEvent ev; 01335 01336 return (history(h, &ev, direction < 0? H_PREV_STR:H_NEXT_STR, str)); 01337 } 01338 01339 01340 /* 01341 * search for event in history containing str, starting at offset 01342 * abs(pos); continue backward, if pos<0, forward otherwise 01343 */ 01344 /* ARGSUSED */ 01345 int 01346 history_search_pos(const char *str, 01347 int direction __attribute__((__unused__)), int pos) 01348 { 01349 HistEvent ev; 01350 int curr_num, off; 01351 01352 off = (pos > 0) ? pos : -pos; 01353 pos = (pos > 0) ? 1 : -1; 01354 01355 if (history(h, &ev, H_CURR) != 0) 01356 return (-1); 01357 curr_num = ev.num; 01358 01359 if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0) 01360 return (-1); 01361 01362 01363 for (;;) { 01364 if (strstr(ev.str, str)) 01365 return (off); 01366 if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0) 01367 break; 01368 } 01369 01370 /* set "current" pointer back to previous state */ 01371 history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num); 01372 01373 return (-1); 01374 } 01375 01376 01377 /********************************/ 01378 /* completion functions */ 01379 01380 /* 01381 * does tilde expansion of strings of type ``~user/foo'' 01382 * if ``user'' isn't valid user name or ``txt'' doesn't start 01383 * w/ '~', returns pointer to strdup()ed copy of ``txt'' 01384 * 01385 * it's callers's responsibility to free() returned string 01386 */ 01387 char * 01388 tilde_expand(char *txt) 01389 { 01390 struct passwd *pass; 01391 char *temp; 01392 size_t len = 0; 01393 01394 if (txt[0] != '~') 01395 return (strdup(txt)); 01396 01397 temp = strchr(txt + 1, '/'); 01398 if (temp == NULL) { 01399 temp = strdup(txt + 1); 01400 if (temp == NULL) 01401 return NULL; 01402 } else { 01403 len = temp - txt + 1; /* text until string after slash */ 01404 temp = malloc(len); 01405 if (temp == NULL) 01406 return NULL; 01407 (void)strncpy(temp, txt + 1, len - 2); 01408 temp[len - 2] = '\0'; 01409 } 01410 pass = getpwnam(temp); 01411 free(temp); /* value no more needed */ 01412 if (pass == NULL) 01413 return (strdup(txt)); 01414 01415 /* update pointer txt to point at string immedially following */ 01416 /* first slash */ 01417 txt += len; 01418 01419 temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1); 01420 if (temp == NULL) 01421 return NULL; 01422 (void)sprintf(temp, "%s/%s", pass->pw_dir, txt); 01423 01424 return (temp); 01425 } 01426 01427 01428 /* 01429 * return first found file name starting by the ``text'' or NULL if no 01430 * such file can be found 01431 * value of ``state'' is ignored 01432 * 01433 * it's caller's responsibility to free returned string 01434 */ 01435 char * 01436 filename_completion_function(const char *text, int state) 01437 { 01438 static DIR *dir = NULL; 01439 static char *filename = NULL, *dirname = NULL; 01440 static size_t filename_len = 0; 01441 struct dirent *entry; 01442 char *temp; 01443 size_t len; 01444 01445 if (state == 0 || dir == NULL) { 01446 temp = strrchr(text, '/'); 01447 if (temp) { 01448 char *nptr; 01449 temp++; 01450 nptr = realloc(filename, strlen(temp) + 1); 01451 if (nptr == NULL) { 01452 free(filename); 01453 return NULL; 01454 } 01455 filename = nptr; 01456 (void)strcpy(filename, temp); 01457 len = temp - text; /* including last slash */ 01458 nptr = realloc(dirname, len + 1); 01459 if (nptr == NULL) { 01460 free(filename); 01461 return NULL; 01462 } 01463 dirname = nptr; 01464 (void)strncpy(dirname, text, len); 01465 dirname[len] = '\0'; 01466 } else { 01467 if (*text == 0) 01468 filename = NULL; 01469 else { 01470 filename = strdup(text); 01471 if (filename == NULL) 01472 return NULL; 01473 } 01474 dirname = NULL; 01475 } 01476 01477 /* support for ``~user'' syntax */ 01478 if (dirname && *dirname == '~') { 01479 char *nptr; 01480 temp = tilde_expand(dirname); 01481 if (temp == NULL) 01482 return NULL; 01483 nptr = realloc(dirname, strlen(temp) + 1); 01484 if (nptr == NULL) { 01485 free(dirname); 01486 return NULL; 01487 } 01488 dirname = nptr; 01489 (void)strcpy(dirname, temp); /* safe */ 01490 free(temp); /* no longer needed */ 01491 } 01492 /* will be used in cycle */ 01493 filename_len = filename ? strlen(filename) : 0; 01494 01495 if (dir != NULL) { 01496 (void)closedir(dir); 01497 dir = NULL; 01498 } 01499 dir = opendir(dirname ? dirname : "."); 01500 if (!dir) 01501 return (NULL); /* cannot open the directory */ 01502 } 01503 /* find the match */ 01504 while ((entry = readdir(dir)) != NULL) { 01505 /* skip . and .. */ 01506 if (entry->d_name[0] == '.' && (!entry->d_name[1] 01507 || (entry->d_name[1] == '.' && !entry->d_name[2]))) 01508 continue; 01509 if (filename_len == 0) 01510 break; 01511 /* otherwise, get first entry where first */ 01512 /* filename_len characters are equal */ 01513 if (entry->d_name[0] == filename[0] 01514 /* Some dirents have d_namlen, but it is not portable. */ 01515 && strlen(entry->d_name) >= filename_len 01516 && strncmp(entry->d_name, filename, 01517 filename_len) == 0) 01518 break; 01519 } 01520 01521 if (entry) { /* match found */ 01522 01523 struct stat stbuf; 01524 /* Some dirents have d_namlen, but it is not portable. */ 01525 len = strlen(entry->d_name) + 01526 ((dirname) ? strlen(dirname) : 0) + 1 + 1; 01527 temp = malloc(len); 01528 if (temp == NULL) 01529 return NULL; 01530 (void)sprintf(temp, "%s%s", 01531 dirname ? dirname : "", entry->d_name); /* safe */ 01532 01533 /* test, if it's directory */ 01534 if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode)) 01535 strcat(temp, "/"); /* safe */ 01536 } else { 01537 (void)closedir(dir); 01538 dir = NULL; 01539 temp = NULL; 01540 } 01541 01542 return (temp); 01543 } 01544 01545 01546 /* 01547 * a completion generator for usernames; returns _first_ username 01548 * which starts with supplied text 01549 * text contains a partial username preceded by random character 01550 * (usually '~'); state is ignored 01551 * it's callers responsibility to free returned value 01552 */ 01553 char * 01554 username_completion_function(const char *text, int state) 01555 { 01556 struct passwd *pwd; 01557 01558 if (text[0] == '\0') 01559 return (NULL); 01560 01561 if (*text == '~') 01562 text++; 01563 01564 if (state == 0) 01565 setpwent(); 01566 01567 while ((pwd = getpwent()) && text[0] == pwd->pw_name[0] 01568 && strcmp(text, pwd->pw_name) == 0); 01569 01570 if (pwd == NULL) { 01571 endpwent(); 01572 return (NULL); 01573 } 01574 return (strdup(pwd->pw_name)); 01575 } 01576 01577 01578 /* 01579 * el-compatible wrapper around rl_complete; needed for key binding 01580 */ 01581 /* ARGSUSED */ 01582 static unsigned char 01583 _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch) 01584 { 01585 return (unsigned char) rl_complete(0, ch); 01586 } 01587 01588 /* 01589 * el-compatible wrapper to send TSTP on ^Z 01590 */ 01591 /* ARGSUSED */ 01592 static unsigned char 01593 _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__))) 01594 { 01595 (void)kill(0, SIGTSTP); 01596 return CC_NORM; 01597 } 01598 01599 /* 01600 * returns list of completions for text given 01601 */ 01602 char ** 01603 completion_matches(const char *text, CPFunction *genfunc) 01604 { 01605 char **match_list = NULL, *retstr, *prevstr; 01606 size_t match_list_len, max_equal, which, i; 01607 size_t matches; 01608 01609 if (h == NULL || e == NULL) 01610 rl_initialize(); 01611 01612 matches = 0; 01613 match_list_len = 1; 01614 while ((retstr = (*genfunc) (text, (int)matches)) != NULL) { 01615 /* allow for list terminator here */ 01616 if (matches + 3 >= match_list_len) { 01617 char **nmatch_list; 01618 while (matches + 3 >= match_list_len) 01619 match_list_len <<= 1; 01620 nmatch_list = realloc(match_list, 01621 match_list_len * sizeof(char *)); 01622 if (nmatch_list == NULL) { 01623 free(match_list); 01624 return NULL; 01625 } 01626 match_list = nmatch_list; 01627 01628 } 01629 match_list[++matches] = retstr; 01630 } 01631 01632 if (!match_list) 01633 return NULL; /* nothing found */ 01634 01635 /* find least denominator and insert it to match_list[0] */ 01636 which = 2; 01637 prevstr = match_list[1]; 01638 max_equal = strlen(prevstr); 01639 for (; which <= matches; which++) { 01640 for (i = 0; i < max_equal && 01641 prevstr[i] == match_list[which][i]; i++) 01642 continue; 01643 max_equal = i; 01644 } 01645 01646 retstr = malloc(max_equal + 1); 01647 if (retstr == NULL) { 01648 free(match_list); 01649 return NULL; 01650 } 01651 (void)strncpy(retstr, match_list[1], max_equal); 01652 retstr[max_equal] = '\0'; 01653 match_list[0] = retstr; 01654 01655 /* add NULL as last pointer to the array */ 01656 match_list[matches + 1] = (char *) NULL; 01657 01658 return (match_list); 01659 } 01660 01661 /* 01662 * Sort function for qsort(). Just wrapper around strcasecmp(). 01663 */ 01664 static int 01665 _rl_qsort_string_compare(i1, i2) 01666 const void *i1, *i2; 01667 { 01668 const char *s1 = ((const char * const *)i1)[0]; 01669 const char *s2 = ((const char * const *)i2)[0]; 01670 01671 return strcasecmp(s1, s2); 01672 } 01673 01674 /* 01675 * Display list of strings in columnar format on readline's output stream. 01676 * 'matches' is list of strings, 'len' is number of strings in 'matches', 01677 * 'max' is maximum length of string in 'matches'. 01678 */ 01679 void 01680 rl_display_match_list (matches, len, max) 01681 char **matches; 01682 int len, max; 01683 { 01684 int i, idx, limit, count; 01685 int screenwidth = e->el_term.t_size.h; 01686 01687 /* 01688 * Find out how many entries can be put on one line, count 01689 * with two spaces between strings. 01690 */ 01691 limit = screenwidth / (max + 2); 01692 if (limit == 0) 01693 limit = 1; 01694 01695 /* how many lines of output */ 01696 count = len / limit; 01697 if (count * limit < len) 01698 count++; 01699 01700 /* Sort the items if they are not already sorted. */ 01701 qsort(&matches[1], (size_t)(len - 1), sizeof(char *), 01702 _rl_qsort_string_compare); 01703 01704 idx = 1; 01705 for(; count > 0; count--) { 01706 for(i = 0; i < limit && matches[idx]; i++, idx++) 01707 (void)fprintf(e->el_outfile, "%-*s ", max, 01708 matches[idx]); 01709 (void)fprintf(e->el_outfile, "\n"); 01710 } 01711 } 01712 01713 /* 01714 * Complete the word at or before point, called by rl_complete() 01715 * 'what_to_do' says what to do with the completion. 01716 * `?' means list the possible completions. 01717 * TAB means do standard completion. 01718 * `*' means insert all of the possible completions. 01719 * `!' means to do standard completion, and list all possible completions if 01720 * there is more than one. 01721 * 01722 * Note: '*' support is not implemented 01723 */ 01724 static int 01725 _rl_complete_internal(int what_to_do) 01726 { 01727 Function *complet_func; 01728 const LineInfo *li; 01729 char *temp, **matches; 01730 const char *ctemp; 01731 size_t len; 01732 01733 rl_completion_type = what_to_do; 01734 01735 if (h == NULL || e == NULL) 01736 rl_initialize(); 01737 01738 complet_func = rl_completion_entry_function; 01739 if (!complet_func) 01740 complet_func = (Function *)(void *)filename_completion_function; 01741 01742 /* We now look backwards for the start of a filename/variable word */ 01743 li = el_line(e); 01744 ctemp = (const char *) li->cursor; 01745 while (ctemp > li->buffer 01746 && !strchr(rl_basic_word_break_characters, ctemp[-1]) 01747 && (!rl_special_prefixes 01748 || !strchr(rl_special_prefixes, ctemp[-1]) ) ) 01749 ctemp--; 01750 01751 len = li->cursor - ctemp; 01752 temp = alloca(len + 1); 01753 (void)strncpy(temp, ctemp, len); 01754 temp[len] = '\0'; 01755 01756 /* these can be used by function called in completion_matches() */ 01757 /* or (*rl_attempted_completion_function)() */ 01758 _rl_update_pos(); 01759 01760 if (rl_attempted_completion_function) { 01761 int end = li->cursor - li->buffer; 01762 matches = (*rl_attempted_completion_function) (temp, (int) 01763 (end - len), end); 01764 } else 01765 matches = 0; 01766 if (!rl_attempted_completion_function || !matches) 01767 matches = completion_matches(temp, (CPFunction *)complet_func); 01768 01769 if (matches) { 01770 int i, retval = CC_REFRESH; 01771 int matches_num, maxlen, match_len, match_display=1; 01772 01773 /* 01774 * Only replace the completed string with common part of 01775 * possible matches if there is possible completion. 01776 */ 01777 if (matches[0][0] != '\0') { 01778 el_deletestr(e, (int) len); 01779 el_insertstr(e, matches[0]); 01780 } 01781 01782 if (what_to_do == '?') 01783 goto display_matches; 01784 01785 if (matches[2] == NULL && strcmp(matches[0], matches[1]) == 0) { 01786 /* 01787 * We found exact match. Add a space after 01788 * it, unless we do filename completion and the 01789 * object is a directory. 01790 */ 01791 size_t alen = strlen(matches[0]); 01792 if ((complet_func != 01793 (Function *)filename_completion_function 01794 || (alen > 0 && (matches[0])[alen - 1] != '/')) 01795 && rl_completion_append_character) { 01796 char buf[2]; 01797 buf[0] = rl_completion_append_character; 01798 buf[1] = '\0'; 01799 el_insertstr(e, buf); 01800 } 01801 } else if (what_to_do == '!') { 01802 display_matches: 01803 /* 01804 * More than one match and requested to list possible 01805 * matches. 01806 */ 01807 01808 for(i=1, maxlen=0; matches[i]; i++) { 01809 match_len = strlen(matches[i]); 01810 if (match_len > maxlen) 01811 maxlen = match_len; 01812 } 01813 matches_num = i - 1; 01814 01815 /* newline to get on next line from command line */ 01816 (void)fprintf(e->el_outfile, "\n"); 01817 01818 /* 01819 * If there are too many items, ask user for display 01820 * confirmation. 01821 */ 01822 if (matches_num > rl_completion_query_items) { 01823 (void)fprintf(e->el_outfile, 01824 "Display all %d possibilities? (y or n) ", 01825 matches_num); 01826 (void)fflush(e->el_outfile); 01827 if (getc(stdin) != 'y') 01828 match_display = 0; 01829 (void)fprintf(e->el_outfile, "\n"); 01830 } 01831 01832 if (match_display) 01833 rl_display_match_list(matches, matches_num, 01834 maxlen); 01835 retval = CC_REDISPLAY; 01836 } else if (matches[0][0]) { 01837 /* 01838 * There was some common match, but the name was 01839 * not complete enough. Next tab will print possible 01840 * completions. 01841 */ 01842 el_beep(e); 01843 } else { 01844 /* lcd is not a valid object - further specification */ 01845 /* is needed */ 01846 el_beep(e); 01847 retval = CC_NORM; 01848 } 01849 01850 /* free elements of array and the array itself */ 01851 for (i = 0; matches[i]; i++) 01852 free(matches[i]); 01853 free(matches), matches = NULL; 01854 01855 return (retval); 01856 } 01857 return (CC_NORM); 01858 } 01859 01860 01861 /* 01862 * complete word at current point 01863 */ 01864 int 01865 /*ARGSUSED*/ 01866 rl_complete(int ignore, int invoking_key) 01867 { 01868 if (h == NULL || e == NULL) 01869 rl_initialize(); 01870 01871 if (rl_inhibit_completion) { 01872 char arr[2]; 01873 arr[0] = (char)invoking_key; 01874 arr[1] = '\0'; 01875 el_insertstr(e, arr); 01876 return (CC_REFRESH); 01877 } else if (e->el_state.lastcmd == el_rl_complete_cmdnum) 01878 return _rl_complete_internal('?'); 01879 else if (_rl_complete_show_all) 01880 return _rl_complete_internal('!'); 01881 else 01882 return _rl_complete_internal(TAB); 01883 } 01884 01885 01886 /* 01887 * misc other functions 01888 */ 01889 01890 /* 01891 * bind key c to readline-type function func 01892 */ 01893 int 01894 rl_bind_key(int c, int func(int, int)) 01895 { 01896 int retval = -1; 01897 01898 if (h == NULL || e == NULL) 01899 rl_initialize(); 01900 01901 if (func == rl_insert) { 01902 /* XXX notice there is no range checking of ``c'' */ 01903 e->el_map.key[c] = ED_INSERT; 01904 retval = 0; 01905 } 01906 return (retval); 01907 } 01908 01909 01910 /* 01911 * read one key from input - handles chars pushed back 01912 * to input stream also 01913 */ 01914 int 01915 rl_read_key(void) 01916 { 01917 char fooarr[2 * sizeof(int)]; 01918 01919 if (e == NULL || h == NULL) 01920 rl_initialize(); 01921 01922 return (el_getc(e, fooarr)); 01923 } 01924 01925 01926 /* 01927 * reset the terminal 01928 */ 01929 /* ARGSUSED */ 01930 void 01931 rl_reset_terminal(const char *p __attribute__((__unused__))) 01932 { 01933 01934 if (h == NULL || e == NULL) 01935 rl_initialize(); 01936 el_reset(e); 01937 } 01938 01939 01940 /* 01941 * insert character ``c'' back into input stream, ``count'' times 01942 */ 01943 int 01944 rl_insert(int count, int c) 01945 { 01946 char arr[2]; 01947 01948 if (h == NULL || e == NULL) 01949 rl_initialize(); 01950 01951 /* XXX - int -> char conversion can lose on multichars */ 01952 arr[0] = c; 01953 arr[1] = '\0'; 01954 01955 for (; count > 0; count--) 01956 el_push(e, arr); 01957 01958 return (0); 01959 } 01960 01961 /*ARGSUSED*/ 01962 int 01963 rl_newline(int count, int c) 01964 { 01965 /* 01966 * Readline-4.0 appears to ignore the args. 01967 */ 01968 return rl_insert(1, '\n'); 01969 } 01970 01971 /*ARGSUSED*/ 01972 static unsigned char 01973 rl_bind_wrapper(EditLine *el, unsigned char c) 01974 { 01975 if (map[c] == NULL) 01976 return CC_ERROR; 01977 01978 _rl_update_pos(); 01979 01980 (*map[c])(NULL, c); 01981 01982 /* If rl_done was set by the above call, deal with it here */ 01983 if (rl_done) 01984 return CC_EOF; 01985 01986 return CC_NORM; 01987 } 01988 01989 int 01990 rl_add_defun(const char *name, Function *fun, int c) 01991 { 01992 char dest[8]; 01993 if (c >= sizeof(map) / sizeof(map[0]) || c < 0) 01994 return -1; 01995 map[(unsigned char)c] = fun; 01996 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper); 01997 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0); 01998 el_set(e, EL_BIND, dest, name); 01999 return 0; 02000 } 02001 02002 void 02003 rl_callback_read_char() 02004 { 02005 int count = 0, done = 0; 02006 const char *buf = el_gets(e, &count); 02007 char *wbuf; 02008 02009 if (buf == NULL || count-- <= 0) 02010 return; 02011 #ifdef CTRL2 /* _AIX */ 02012 if (count == 0 && buf[0] == CTRL2('d')) 02013 #else 02014 if (count == 0 && buf[0] == CTRL('d')) 02015 #endif 02016 done = 1; 02017 if (buf[count] == '\n' || buf[count] == '\r') 02018 done = 2; 02019 02020 if (done && rl_linefunc != NULL) { 02021 el_set(e, EL_UNBUFFERED, 0); 02022 if (done == 2) { 02023 if ((wbuf = strdup(buf)) != NULL) 02024 wbuf[count] = '\0'; 02025 } else 02026 wbuf = NULL; 02027 (*(void (*)(const char *))rl_linefunc)(wbuf); 02028 el_set(e, EL_UNBUFFERED, 1); 02029 } 02030 } 02031 02032 void 02033 rl_callback_handler_install (const char *prompt, VFunction *linefunc) 02034 { 02035 if (e == NULL) { 02036 rl_initialize(); 02037 } 02038 if (rl_prompt) 02039 free(rl_prompt); 02040 rl_prompt = prompt ? strdup(strchr(prompt, *prompt)) : NULL; 02041 rl_linefunc = linefunc; 02042 el_set(e, EL_UNBUFFERED, 1); 02043 } 02044 02045 void 02046 rl_callback_handler_remove(void) 02047 { 02048 el_set(e, EL_UNBUFFERED, 0); 02049 } 02050 02051 void 02052 rl_redisplay(void) 02053 { 02054 char a[2]; 02055 #ifdef CTRL2 /* _AIX */ 02056 a[0] = CTRL2('r'); 02057 #else 02058 a[0] = CTRL('r'); 02059 #endif 02060 a[1] = '\0'; 02061 el_push(e, a); 02062 } 02063 02064 int 02065 rl_get_previous_history(int count, int key) 02066 { 02067 char a[2]; 02068 a[0] = key; 02069 a[1] = '\0'; 02070 while (count--) 02071 el_push(e, a); 02072 return 0; 02073 } 02074 02075 void 02076 /*ARGSUSED*/ 02077 rl_prep_terminal(int meta_flag) 02078 { 02079 el_set(e, EL_PREP_TERM, 1); 02080 } 02081 02082 void 02083 rl_deprep_terminal() 02084 { 02085 el_set(e, EL_PREP_TERM, 0); 02086 } 02087 02088 int 02089 rl_read_init_file(const char *s) 02090 { 02091 return(el_source(e, s)); 02092 } 02093 02094 int 02095 rl_parse_and_bind(const char *line) 02096 { 02097 const char **argv; 02098 int argc; 02099 Tokenizer *tok; 02100 02101 tok = tok_init(NULL); 02102 tok_str(tok, line, &argc, &argv); 02103 argc = el_parse(e, argc, argv); 02104 tok_end(tok); 02105 return (argc ? 1 : 0); 02106 } 02107 02108 void 02109 rl_stuff_char(int c) 02110 { 02111 char buf[2]; 02112 02113 buf[0] = c; 02114 buf[1] = '\0'; 02115 el_insertstr(e, buf); 02116 } 02117 02118 static int 02119 _rl_event_read_char(EditLine *el, char *cp) 02120 { 02121 int n, num_read = 0; 02122 02123 *cp = 0; 02124 while (rl_event_hook) { 02125 02126 (*rl_event_hook)(); 02127 02128 #if defined(FIONREAD) 02129 if (ioctl(el->el_infd, FIONREAD, &n) < 0) 02130 return(-1); 02131 if (n) 02132 num_read = read(el->el_infd, cp, 1); 02133 else 02134 num_read = 0; 02135 #elif defined(F_SETFL) && defined(O_NDELAY) 02136 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0) 02137 return(-1); 02138 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0) 02139 return(-1); 02140 num_read = read(el->el_infd, cp, 1); 02141 if (fcntl(el->el_infd, F_SETFL, n)) 02142 return(-1); 02143 #else 02144 /* not non-blocking, but what you gonna do? */ 02145 num_read = read(el->el_infd, cp, 1); 02146 return(-1); 02147 #endif 02148 02149 if (num_read < 0 && errno == EAGAIN) 02150 continue; 02151 if (num_read == 0) 02152 continue; 02153 break; 02154 } 02155 if (!rl_event_hook) 02156 el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN); 02157 return(num_read); 02158 } 02159 02160 static void 02161 _rl_update_pos(void) 02162 { 02163 const LineInfo *li = el_line(e); 02164 02165 rl_point = li->cursor - li->buffer; 02166 rl_end = li->lastchar - li->buffer; 02167 }
1.4.7

