⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readline.c

📁 这是linux下运行的mysql软件包,可用于linux 下安装 php + mysql + apach 的网络配置
💻 C
📖 第 1 页 / 共 3 页
字号:
/*	$NetBSD: readline.c,v 1.49 2005/03/10 19:34:46 christos Exp $	*//*- * Copyright (c) 1997 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Jaromir Dolecek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the NetBSD *	Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its *    contributors may be used to endorse or promote products derived *    from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. *//* AIX requires this to be the first thing in the file.  */#if defined (_AIX) && !defined (__GNUC__) #pragma alloca#endif#include <config.h>#ifdef __GNUC__# undef alloca# define alloca(n) __builtin_alloca (n)#else# ifdef HAVE_ALLOCA_H#  include <alloca.h># else#  ifndef _AIXextern char *alloca ();#  endif# endif#endif#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#include <dirent.h>#include <string.h>#include <pwd.h>#include <ctype.h>#include <stdlib.h>#include <unistd.h>#include <limits.h>#include <errno.h>#include <fcntl.h>#include <vis.h>#include "readline/readline.h"#include "el.h"#include "fcns.h"		/* for EL_NUM_FCNS */#include "histedit.h"/* for rl_complete() */#define TAB		'\r'/* see comment at the #ifdef for sense of this *//* #define GDB_411_HACK *//* readline compatibility stuff - look at readline sources/documentation *//* to see what these variables mean */const char *rl_library_version = "EditLine wrapper";static char empty[] = { '\0' };static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',    '>', '<', '=', ';', '|', '&', '{', '(', '\0' };char *rl_readline_name = empty;FILE *rl_instream = NULL;FILE *rl_outstream = NULL;int rl_point = 0;int rl_end = 0;char *rl_line_buffer = NULL;VFunction *rl_linefunc = NULL;int rl_done = 0;VFunction *rl_event_hook = NULL;int history_base = 1;		/* probably never subject to change */int history_length = 0;int max_input_history = 0;char history_expansion_char = '!';char history_subst_char = '^';char *history_no_expand_chars = expand_chars;Function *history_inhibit_expansion_function = NULL;char *history_arg_extract(int start, int end, const char *str);int rl_inhibit_completion = 0;int rl_attempted_completion_over = 0;char *rl_basic_word_break_characters = break_chars;char *rl_completer_word_break_characters = NULL;char *rl_completer_quote_characters = NULL;Function *rl_completion_entry_function = NULL;CPPFunction *rl_attempted_completion_function = NULL;Function *rl_pre_input_hook = NULL;Function *rl_startup1_hook = NULL;Function *rl_getc_function = NULL;char *rl_terminal_name = NULL;int rl_already_prompted = 0;int rl_filename_completion_desired = 0;int rl_ignore_completion_duplicates = 0;int rl_catch_signals = 1;VFunction *rl_redisplay_function = NULL;Function *rl_startup_hook = NULL;VFunction *rl_completion_display_matches_hook = NULL;VFunction *rl_prep_term_function = NULL;VFunction *rl_deprep_term_function = NULL;/* * The current prompt string. */char *rl_prompt = NULL;/* * This is set to character indicating type of completion being done by * rl_complete_internal(); this is available for application completion * functions. */int rl_completion_type = 0;/* * If more than this number of items results from query for possible * completions, we ask user if they are sure to really display the list. */int rl_completion_query_items = 100;/* * List of characters which are word break characters, but should be left * in the parsed text when it is passed to the completion function. * Shell uses this to help determine what kind of completing to do. */char *rl_special_prefixes = (char *)NULL;/* * This is the character appended to the completed words if at the end of * the line. Default is ' ' (a space). */int rl_completion_append_character = ' ';/* stuff below is used internally by libedit for readline emulation *//* if not zero, non-unique completions always show list of possible matches */static int _rl_complete_show_all = 0;static History *h = NULL;static EditLine *e = NULL;static Function *map[256];static int el_rl_complete_cmdnum = 0;/* internal functions */static unsigned char	 _el_rl_complete(EditLine *, int);static unsigned char	 _el_rl_tstp(EditLine *, int);static char		*_get_prompt(EditLine *);static HIST_ENTRY	*_move_history(int);static int		 _history_expand_command(const char *, size_t, size_t,    char **);static char		*_rl_compat_sub(const char *, const char *,    const char *, int);static int		 _rl_complete_internal(int);static int		 _rl_qsort_string_compare(const void *, const void *);static int		 _rl_event_read_char(EditLine *, char *);static void		 _rl_update_pos(void);/* ARGSUSED */static char *_get_prompt(EditLine *el __attribute__((__unused__))){	rl_already_prompted = 1;	return (rl_prompt);}/* * generic function for moving around history */static HIST_ENTRY *_move_history(int op){	HistEvent ev;	static HIST_ENTRY rl_he;	if (history(h, &ev, op) != 0)		return (HIST_ENTRY *) NULL;	rl_he.line = ev.str;	rl_he.data = (histdata_t) &(ev.num);	return (&rl_he);}/* * READLINE compatibility stuff *//* * initialize rl compat stuff */intrl_initialize(void){	HistEvent ev;	const LineInfo *li;	int i;	int editmode = 1;	struct termios t;	if (e != NULL)		el_end(e);	if (h != NULL)		history_end(h);	if (!rl_instream)		rl_instream = stdin;	if (!rl_outstream)		rl_outstream = stdout;	/*	 * See if we don't really want to run the editor	 */	if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)		editmode = 0;	e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);	if (!editmode)		el_set(e, EL_EDITMODE, 0);	h = history_init();	if (!e || !h)		return (-1);	history(h, &ev, H_SETSIZE, INT_MAX);	/* unlimited */	history_length = 0;	max_input_history = INT_MAX;	el_set(e, EL_HIST, history, h);	/* for proper prompt printing in readline() */	rl_prompt = strdup("");	if (rl_prompt == NULL) {		history_end(h);		el_end(e);		return -1;	}	el_set(e, EL_PROMPT, _get_prompt);	el_set(e, EL_SIGNAL, rl_catch_signals);	/* set default mode to "emacs"-style and read setting afterwards */	/* so this can be overriden */	el_set(e, EL_EDITOR, "emacs");	if (rl_terminal_name != NULL)		el_set(e, EL_TERMINAL, rl_terminal_name);	else		el_get(e, EL_TERMINAL, &rl_terminal_name);	/*	 * Word completion - this has to go AFTER rebinding keys	 * to emacs-style.	 */	el_set(e, EL_ADDFN, "rl_complete",	    "ReadLine compatible completion function",	    _el_rl_complete);	el_set(e, EL_BIND, "^I", "rl_complete", NULL);	/*	 * Send TSTP when ^Z is pressed.	 */	el_set(e, EL_ADDFN, "rl_tstp",	    "ReadLine compatible suspend function",	    _el_rl_tstp);	el_set(e, EL_BIND, "^Z", "rl_tstp", NULL);	/*	 * Find out where the rl_complete function was added; this is	 * used later to detect that lastcmd was also rl_complete.	 */	for(i=EL_NUM_FCNS; i < e->el_map.nfunc; i++) {		if (e->el_map.func[i] == _el_rl_complete) {			el_rl_complete_cmdnum = i;			break;		}	}			/* read settings from configuration file */	el_source(e, NULL);	/*	 * Unfortunately, some applications really do use rl_point	 * and rl_line_buffer directly.	 */	li = el_line(e);	/* a cheesy way to get rid of const cast. */	rl_line_buffer = memchr(li->buffer, *li->buffer, 1);	_rl_update_pos();	if (rl_startup_hook)		(*rl_startup_hook)(NULL, 0);	return (0);}/* * read one line from input stream and return it, chomping * trailing newline (if there is any) */char *readline(const char *prompt){	HistEvent ev;	int count;	const char *ret;	char *buf;	static int used_event_hook;	if (e == NULL || h == NULL)		rl_initialize();	rl_done = 0;	/* update prompt accordingly to what has been passed */	if (!prompt)		prompt = "";	if (strcmp(rl_prompt, prompt) != 0) {		free(rl_prompt);		rl_prompt = strdup(prompt);		if (rl_prompt == NULL)			return NULL;	}	if (rl_pre_input_hook)		(*rl_pre_input_hook)(NULL, 0);	if (rl_event_hook && !(e->el_flags&NO_TTY)) {		el_set(e, EL_GETCFN, _rl_event_read_char);		used_event_hook = 1;	}	if (!rl_event_hook && used_event_hook) {		el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN);		used_event_hook = 0;	}	rl_already_prompted = 0;	/* get one line from input stream */	ret = el_gets(e, &count);	if (ret && count > 0) {		int lastidx;		buf = strdup(ret);		if (buf == NULL)			return NULL;		lastidx = count - 1;		if (buf[lastidx] == '\n')			buf[lastidx] = '\0';	} else		buf = NULL;	history(h, &ev, H_GETSIZE);	history_length = ev.num;	return buf;}/* * history functions *//* * is normally called before application starts to use * history expansion functions */voidusing_history(void){	if (h == NULL || e == NULL)		rl_initialize();}/* * substitute ``what'' with ``with'', returning resulting string; if * globally == 1, substitutes all occurrences of what, otherwise only the * first one */static char *_rl_compat_sub(const char *str, const char *what, const char *with,    int globally){	const	char	*s;	char	*r, *result;	size_t	len, with_len, what_len;	len = strlen(str);	with_len = strlen(with);	what_len = strlen(what);	/* calculate length we need for result */	s = str;	while (*s) {		if (*s == *what && !strncmp(s, what, what_len)) {			len += with_len - what_len;			if (!globally)				break;			s += what_len;		} else			s++;	}	r = result = malloc(len + 1);	if (result == NULL)		return NULL;	s = str;	while (*s) {		if (*s == *what && !strncmp(s, what, what_len)) {			(void)strncpy(r, with, with_len);			r += with_len;			s += what_len;			if (!globally) {				(void)strcpy(r, s);				return(result);			}		} else			*r++ = *s++;	}	*r = 0;	return(result);}static	char	*last_search_pat;	/* last !?pat[?] search pattern */static	char	*last_search_match;	/* last !?pat[?] that matched */const char *get_history_event(const char *cmd, int *cindex, int qchar){	int idx, sign, sub, num, begin, ret;	size_t len;	char	*pat;	const char *rptr;	HistEvent ev;	idx = *cindex;	if (cmd[idx++] != history_expansion_char)		return(NULL);	/* find out which event to take */	if (cmd[idx] == history_expansion_char || cmd[idx] == 0) {		if (history(h, &ev, H_FIRST) != 0)			return(NULL);		*cindex = cmd[idx]? (idx + 1):idx;		return(ev.str);	}	sign = 0;	if (cmd[idx] == '-') {		sign = 1;		idx++;	}	if ('0' <= cmd[idx] && cmd[idx] <= '9') {		HIST_ENTRY *rl_he;		num = 0;		while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') {			num = num * 10 + cmd[idx] - '0';			idx++;		}		if (sign)			num = history_length - num + 1;		if (!(rl_he = history_get(num)))			return(NULL);		*cindex = idx;		return(rl_he->line);	}	sub = 0;	if (cmd[idx] == '?') {		sub = 1;		idx++;	}	begin = idx;	while (cmd[idx]) {		if (cmd[idx] == '\n')			break;		if (sub && cmd[idx] == '?')			break;		if (!sub && (cmd[idx] == ':' || cmd[idx] == ' '				    || cmd[idx] == '\t' || cmd[idx] == qchar))			break;		idx++;	}	len = idx - begin;	if (sub && cmd[idx] == '?')		idx++;	if (sub && len == 0 && last_search_pat && *last_search_pat)		pat = last_search_pat;	else if (len == 0)		return(NULL);	else {		if ((pat = malloc(len + 1)) == NULL)			return NULL;		(void)strncpy(pat, cmd + begin, len);		pat[len] = '\0';	}	if (history(h, &ev, H_CURR) != 0) {		if (pat != last_search_pat)			free(pat);		return (NULL);	}	num = ev.num;	if (sub) {		if (pat != last_search_pat) {			if (last_search_pat)				free(last_search_pat);			last_search_pat = pat;		}		ret = history_search(pat, -1);	} else		ret = history_search_prefix(pat, -1);	if (ret == -1) {		/* restore to end of list on failed search */		history(h, &ev, H_FIRST);		(void)fprintf(rl_outstream, "%s: Event not found\n", pat);		if (pat != last_search_pat)			free(pat);		return(NULL);	}	if (sub && len) {		if (last_search_match && last_search_match != pat)			free(last_search_match);		last_search_match = pat;	}	if (pat != last_search_pat)		free(pat);	if (history(h, &ev, H_CURR) != 0)		return(NULL);	*cindex = idx;	rptr = ev.str;	/* roll back to original position */	(void)history(h, &ev, H_SET, num);	return rptr;}/* * the real function doing history expansion - takes as argument command * to do and data upon which the command should be executed * does expansion the way I've understood readline documentation * * returns 0 if data was not modified, 1 if it was and 2 if the string * should be only printed and not executed; in case of error, * returns -1 and *result points to NULL * it's callers responsibility to free() string returned in *result */static int_history_expand_command(const char *command, size_t offs, size_t cmdlen,    char **result){	char *tmp, *search = NULL, *aptr;	const char *ptr, *cmd;	static char *from = NULL, *to = NULL;	int start, end, idx, has_mods = 0;	int p_on = 0, g_on = 0;	*result = NULL;	aptr = NULL;	ptr = NULL;	/* First get event specifier */	idx = 0;	if (strchr(":^*$", command[offs + 1])) {		char str[4];		/*		* "!:" is shorthand for "!!:".		* "!^", "!*" and "!$" are shorthand for		* "!!:^", "!!:*" and "!!:$" respectively.		*/		str[0] = str[1] = '!';		str[2] = '0';		ptr = get_history_event(str, &idx, 0);		idx = (command[offs + 1] == ':')? 1:0;		has_mods = 1;	} else {		if (command[offs + 1] == '#') {			/* use command so far */			if ((aptr = malloc(offs + 1)) == NULL)				return -1;			(void)strncpy(aptr, command, offs);			aptr[offs] = '\0';			idx = 1;		} else {			int	qchar;			qchar = (offs > 0 && command[offs - 1] == '"')? '"':0;			ptr = get_history_event(command + offs, &idx, qchar);		}		has_mods = command[offs + idx] == ':';	}	if (ptr == NULL && aptr == NULL)		return(-1);	if (!has_mods) {		*result = strdup(aptr? aptr : ptr);		if (aptr)			free(aptr);		return(1);	}	cmd = command + offs + idx + 1;	/* Now parse any word designators */	if (*cmd == '%')	/* last word matched by ?pat? */		tmp = strdup(last_search_match? last_search_match:"");	else if (strchr("^*$-0123456789", *cmd)) {		start = end = -1;		if (*cmd == '^')			start = end = 1, cmd++;		else if (*cmd == '$')			start = -1, cmd++;		else if (*cmd == '*')			start = 1, cmd++;	       else if (*cmd == '-' || isdigit((unsigned char) *cmd)) {			start = 0;			while (*cmd && '0' <= *cmd && *cmd <= '9')				start = start * 10 + *cmd++ - '0';			if (*cmd == '-') {				if (isdigit((unsigned char) cmd[1])) {					cmd++;					end = 0;					while (*cmd && '0' <= *cmd && *cmd <= '9')						end = end * 10 + *cmd++ - '0';				} else if (cmd[1] == '$') {					cmd += 2;					end = -1;				} else {					cmd++;					end = -2;				}			} else if (*cmd == '*')				end = -1, cmd++;			else				end = start;		}		tmp = history_arg_extract(start, end, aptr? aptr:ptr);		if (tmp == NULL) {			(void)fprintf(rl_outstream, "%s: Bad word specifier",			    command + offs + idx);			if (aptr)				free(aptr);			return(-1);		}	} else		tmp = strdup(aptr? aptr:ptr);	if (aptr)		free(aptr);	if (*cmd == 0 || (cmd - (command + offs) >= cmdlen)) {		*result = tmp;		return(1);	}	for (; *cmd; cmd++) {		if (*cmd == ':')			continue;		else if (*cmd == 'h') {		/* remove trailing path */			if ((aptr = strrchr(tmp, '/')) != NULL)				*aptr = 0;		} else if (*cmd == 't') {	/* remove leading path */			if ((aptr = strrchr(tmp, '/')) != NULL) {				aptr = strdup(aptr + 1);				free(tmp);				tmp = aptr;			}		} else if (*cmd == 'r') {	/* remove trailing suffix */			if ((aptr = strrchr(tmp, '.')) != NULL)				*aptr = 0;		} else if (*cmd == 'e') {	/* remove all but suffix */			if ((aptr = strrchr(tmp, '.')) != NULL) {				aptr = strdup(aptr);				free(tmp);				tmp = aptr;			}		} else if (*cmd == 'p')		/* print only */			p_on = 1;		else if (*cmd == 'g')			g_on = 2;		else if (*cmd == 's' || *cmd == '&') {			char *what, *with, delim;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -