📄 readline.c
字号:
* abs(pos); continue backward, if pos<0, forward otherwise *//* ARGSUSED */inthistory_search_pos(const char *str, int direction, int pos){ HistEvent ev; int curr_num, off; off = (pos > 0) ? pos : -pos; pos = (pos > 0) ? 1 : -1; if (history(h, &ev, H_CURR) != 0) return (-1); curr_num = ev.num; if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0) return (-1); for (;;) { if (strstr(ev.str, str)) return (off); if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0) break; } /* set "current" pointer back to previous state */ history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num); return (-1);}/********************************//* completition functions *//* * does tilde expansion of strings of type ``~user/foo'' * if ``user'' isn't valid user name or ``txt'' doesn't start * w/ '~', returns pointer to strdup()ed copy of ``txt'' * * it's callers's responsibility to free() returned string */char *tilde_expand(char *txt){ struct passwd *pass; char *temp; size_t len = 0; if (txt[0] != '~') return (strdup(txt)); temp = strchr(txt + 1, '/'); if (temp == NULL) temp = strdup(txt + 1); else { len = temp - txt + 1; /* text until string after slash */ temp = malloc(len); (void) strncpy(temp, txt + 1, len - 2); temp[len - 2] = '\0'; } pass = getpwnam(temp); free(temp); /* value no more needed */ if (pass == NULL) return (strdup(txt)); /* update pointer txt to point at string immedially following */ /* first slash */ txt += len; temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1); (void) sprintf(temp, "%s/%s", pass->pw_dir, txt); return (temp);}/* * return first found file name starting by the ``text'' or NULL if no * such file can be found. * The first ``state'' matches are ignored. * * it's caller's responsibility to free returned string */char *filename_completion_function(const char *text, int state){ DIR *dir = NULL; char *filename = NULL, *dirname = NULL; size_t filename_len = 0; struct dirent *entry; char *temp; size_t len; temp = strrchr(text, '/'); if (temp) { temp++; filename = realloc(filename, strlen(temp) + 1); (void) strcpy(filename, temp); len = temp - text; /* including last slash */ dirname = realloc(dirname, len + 1); (void) strncpy(dirname, text, len); dirname[len] = '\0'; } else { filename = strdup(text); dirname = NULL; } /* support for ``~user'' syntax */ if (dirname && *dirname == '~') { temp = tilde_expand(dirname); dirname = realloc(dirname, strlen(temp) + 1); (void) strcpy(dirname, temp); /* safe */ free(temp); /* no longer needed */ } /* will be used in cycle */ filename_len = strlen(filename); dir = opendir(dirname ? dirname : "."); if (!dir) return (NULL); /* cannot open the directory */ /* find the match */ while ((entry = readdir(dir)) != NULL) { /* otherwise, get first entry where first */ /* filename_len characters are equal */ if (#if defined(__SVR4) || defined(__linux__) strlen(entry->d_name) >= filename_len#else entry->d_namlen >= filename_len#endif && strncmp(entry->d_name, filename, filename_len) == 0 && (state-- == 0)) break; } if (entry) { /* match found */ struct stat stbuf;#if defined(__SVR4) || defined(__linux__) len = strlen(entry->d_name) +#else len = entry->d_namlen +#endif ((dirname) ? strlen(dirname) : 0) + 1 + 1; temp = malloc(len); (void) sprintf(temp, "%s%s", dirname ? dirname : "", entry->d_name); /* safe */ /* test, if it's directory */ if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode)) strcat(temp, "/"); /* safe */ } else temp = NULL; closedir(dir); return (temp);}/* * a completion generator for usernames; returns _first_ username * which starts with supplied text * text contains a partial username preceded by random character * (usually '~'); state is ignored * it's callers responsibility to free returned value */char *username_completion_function(const char *text, int state){ struct passwd *pwd; if (text[0] == '\0') return (NULL); if (*text == '~') text++; if (state == 0) setpwent(); while ((pwd = getpwent()) && text[0] == pwd->pw_name[0] && strcmp(text, pwd->pw_name) == 0); if (pwd == NULL) { endpwent(); return (NULL); } return (strdup(pwd->pw_name));}/* * el-compatible wrapper around rl_complete; needed for key binding *//* ARGSUSED */static unsigned char_el_rl_complete(EditLine *el, int ch){ return (unsigned char) rl_complete(0, ch);}/* * returns list of completitions for text given */char **completion_matches(const char *text, CPFunction *genfunc){ char **match_list = NULL, *retstr, *prevstr; size_t match_list_len, max_equal, which, i; int matches; if (h == NULL || e == NULL) rl_initialize(); matches = 0; match_list_len = 1; while ((retstr = (*genfunc) (text, matches)) != NULL) { if (matches + 1 >= match_list_len) { match_list_len <<= 1; match_list = realloc(match_list, match_list_len * sizeof(char *)); } match_list[++matches] = retstr; } if (!match_list) return (char **) NULL; /* nothing found */ /* find least denominator and insert it to match_list[0] */ which = 2; prevstr = match_list[1]; max_equal = strlen(prevstr); for (; which <= matches; which++) { for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++) continue; max_equal = i; } retstr = malloc(max_equal + 1); (void) strncpy(retstr, match_list[1], max_equal); retstr[max_equal] = '\0'; match_list[0] = retstr; /* add NULL as last pointer to the array */ if (matches + 1 >= match_list_len) match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *)); match_list[matches + 1] = (char *) NULL; return (match_list);}/* * Sort function for qsort(). Just wrapper around strcasecmp(). */static int_rl_qsort_string_compare(i1, i2) const void *i1, *i2;{ /* LINTED const castaway */ const char *s1 = ((const char **)i1)[0]; /* LINTED const castaway */ const char *s2 = ((const char **)i2)[0]; return strcasecmp(s1, s2);}/* * Display list of strings in columnar format on readline's output stream. * 'matches' is list of strings, 'len' is number of strings in 'matches', * 'max' is maximum length of string in 'matches'. */voidrl_display_match_list (matches, len, max) char **matches; int len, max;{ int i, idx, limit, count; int screenwidth = e->el_term.t_size.h; /* * Find out how many entries can be put on one line, count * with two spaces between strings. */ limit = screenwidth / (max + 2); if (limit == 0) limit = 1; /* how many lines of output */ count = len / limit; if (count * limit < len) count++; /* Sort the items if they are not already sorted. */ qsort(&matches[1], (size_t)(len - 1), sizeof(char *), _rl_qsort_string_compare); idx = 1; for(; count > 0; count--) { for(i=0; i < limit && matches[idx]; i++, idx++) fprintf(e->el_outfile, "%-*s ", max, matches[idx]); fprintf(e->el_outfile, "\n"); }}/* * Complete the word at or before point, called by rl_complete() * 'what_to_do' says what to do with the completion. * `?' means list the possible completions. * TAB means do standard completion. * `*' means insert all of the possible completions. * `!' means to do standard completion, and list all possible completions if * there is more than one. * * Note: '*' support is not implemented */static intrl_complete_internal(int what_to_do){ CPFunction *complet_func; const LineInfo *li; char *temp, **matches; const char *ctemp; size_t len; rl_completion_type = what_to_do; if (h == NULL || e == NULL) rl_initialize(); complet_func = rl_completion_entry_function; if (!complet_func) complet_func = filename_completion_function; /* We now look backwards for the start of a filename/variable word */ li = el_line(e); ctemp = (const char *) li->cursor; while (ctemp > li->buffer && !strchr(rl_basic_word_break_characters, ctemp[-1]) && (!rl_special_prefixes || !strchr(rl_special_prefixes, ctemp[-1]) ) ) ctemp--; len = li->cursor - ctemp; temp = alloca(len + 1); (void) strncpy(temp, ctemp, len); temp[len] = '\0'; /* these can be used by function called in completion_matches() */ /* or (*rl_attempted_completion_function)() */ rl_point = li->cursor - li->buffer; rl_end = li->lastchar - li->buffer; if (!rl_attempted_completion_function) matches = completion_matches(temp, complet_func); else { int end = li->cursor - li->buffer; matches = (*rl_attempted_completion_function) (temp, (int) (end - len), end); } if (matches) { int i, retval = CC_REFRESH; int matches_num, maxlen, match_len, match_display=1; /* * Only replace the completed string with common part of * possible matches if there is possible completion. */ if (matches[0][0] != '\0') { el_deletestr(e, (int) len); el_insertstr(e, matches[0]); } if (what_to_do == '?') goto display_matches; if (matches[2] == NULL && strcmp(matches[0], matches[1]) == 0) { /* * We found exact match. Add a space after * it, unless we do filename completition and the * object is a directory. */ size_t alen = strlen(matches[0]); if ((complet_func != filename_completion_function || (alen > 0 && (matches[0])[alen - 1] != '/')) && rl_completion_append_character) { char buf[2]; buf[0] = rl_completion_append_character; buf[1] = '\0'; el_insertstr(e, buf); } } else if (what_to_do == '!') { display_matches: /* * More than one match and requested to list possible * matches. */ for(i=1, maxlen=0; matches[i]; i++) { match_len = strlen(matches[i]); if (match_len > maxlen) maxlen = match_len; } matches_num = i - 1; /* newline to get on next line from command line */ fprintf(e->el_outfile, "\n"); /* * If there are too many items, ask user for display * confirmation. */ if (matches_num > rl_completion_query_items) { fprintf(e->el_outfile, "Display all %d possibilities? (y or n) ", matches_num); fflush(e->el_outfile); if (getc(stdin) != 'y') match_display = 0; fprintf(e->el_outfile, "\n"); } if (match_display) rl_display_match_list(matches, matches_num, maxlen); retval = CC_REDISPLAY; } else if (matches[0][0]) { /* * There was some common match, but the name was * not complete enough. Next tab will print possible * completions. */ el_beep(e); } else { /* lcd is not a valid object - further specification */ /* is needed */ el_beep(e); retval = CC_NORM; } /* free elements of array and the array itself */ for (i = 0; matches[i]; i++) free(matches[i]); free(matches), matches = NULL; return (retval); } return (CC_NORM);}/* * complete word at current point */intrl_complete(int ignore, int invoking_key){ if (h == NULL || e == NULL) rl_initialize(); if (rl_inhibit_completion) { rl_insert(ignore, invoking_key); return (CC_REFRESH); } else if (e->el_state.lastcmd == el_rl_complete_cmdnum) return rl_complete_internal('?'); else if (_rl_complete_show_all) return rl_complete_internal('!'); else return (rl_complete_internal(TAB));}/* * misc other functions *//* * bind key c to readline-type function func */intrl_bind_key(int c, int func(int, int)){ int retval = -1; if (h == NULL || e == NULL) rl_initialize(); if (func == rl_insert) { /* XXX notice there is no range checking of ``c'' */ e->el_map.key[c] = ED_INSERT; retval = 0; } return (retval);}/* * read one key from input - handles chars pushed back * to input stream also */intrl_read_key(void){ char fooarr[2 * sizeof(int)]; if (e == NULL || h == NULL) rl_initialize(); return (el_getc(e, fooarr));}/* * reset the terminal *//* ARGSUSED */voidrl_reset_terminal(const char *p){ if (h == NULL || e == NULL) rl_initialize(); el_reset(e);}/* * insert character ``c'' back into input stream, ``count'' times */intrl_insert(int count, int c){ char arr[2]; if (h == NULL || e == NULL) rl_initialize(); /* XXX - int -> char conversion can lose on multichars */ arr[0] = c; arr[1] = '\0'; for (; count > 0; count--) el_push(e, arr); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -