2001-01-10 11:10:45 +03:00
|
|
|
/* $NetBSD: readline.c,v 1.19 2001/01/10 08:10:45 jdolecek Exp $ */
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
2001-01-10 11:10:45 +03:00
|
|
|
__RCSID("$NetBSD: readline.c,v 1.19 2001/01/10 08:10:45 jdolecek Exp $");
|
1997-10-24 02:24:09 +04:00
|
|
|
#endif /* not lint && not SCCSID */
|
|
|
|
|
|
|
|
#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 "histedit.h"
|
2001-01-06 00:15:49 +03:00
|
|
|
#include "readline/readline.h"
|
1997-10-24 02:24:09 +04:00
|
|
|
#include "sys.h"
|
|
|
|
#include "el.h"
|
2000-12-24 01:02:20 +03:00
|
|
|
#include "fcns.h" /* for EL_NUM_FCNS */
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* for rl_complete() */
|
2000-09-05 02:06:28 +04:00
|
|
|
#define TAB '\r'
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* see comment at the #ifdef for sense of this */
|
2000-09-05 02:06:28 +04:00
|
|
|
#define GDB_411_HACK
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* readline compatibility stuff - look at readline sources/documentation */
|
|
|
|
/* to see what these variables mean */
|
2000-09-05 02:06:28 +04:00
|
|
|
const char *rl_library_version = "EditLine wrapper";
|
|
|
|
char *rl_readline_name = "";
|
|
|
|
FILE *rl_instream = NULL;
|
|
|
|
FILE *rl_outstream = NULL;
|
|
|
|
int rl_point = 0;
|
|
|
|
int rl_end = 0;
|
|
|
|
char *rl_line_buffer = 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 = " \t\n=(";
|
|
|
|
Function *history_inhibit_expansion_function = NULL;
|
|
|
|
|
|
|
|
int rl_inhibit_completion = 0;
|
|
|
|
int rl_attempted_completion_over = 0;
|
|
|
|
char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
|
|
|
|
char *rl_completer_word_break_characters = NULL;
|
|
|
|
char *rl_completer_quote_characters = NULL;
|
|
|
|
CPFunction *rl_completion_entry_function = NULL;
|
|
|
|
CPPFunction *rl_attempted_completion_function = NULL;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/*
|
2001-01-01 18:52:25 +03:00
|
|
|
* 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.
|
2000-12-24 01:02:20 +03:00
|
|
|
*/
|
2001-01-01 18:52:25 +03:00
|
|
|
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 */
|
2000-12-24 01:02:20 +03:00
|
|
|
static int _rl_complete_show_all = 0;
|
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
static History *h = NULL;
|
|
|
|
static EditLine *e = NULL;
|
2000-12-24 01:02:20 +03:00
|
|
|
static int el_rl_complete_cmdnum = 0;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* internal functions */
|
2000-09-05 02:06:28 +04:00
|
|
|
static unsigned char _el_rl_complete(EditLine *, int);
|
|
|
|
static char *_get_prompt(EditLine *);
|
|
|
|
static HIST_ENTRY *_move_history(int);
|
|
|
|
static int _history_search_gen(const char *, int, int);
|
|
|
|
static int _history_expand_command(const char *, size_t, char **);
|
|
|
|
static char *_rl_compat_sub(const char *, const char *,
|
|
|
|
const char *, int);
|
|
|
|
static int rl_complete_internal(int);
|
2000-12-24 01:02:20 +03:00
|
|
|
static int _rl_qsort_string_compare(const void *, const void *);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/*
|
2000-03-10 15:55:15 +03:00
|
|
|
* needed for prompt switching in readline()
|
1997-10-24 02:24:09 +04:00
|
|
|
*/
|
2000-09-05 02:06:28 +04:00
|
|
|
static char *el_rl_prompt = NULL;
|
|
|
|
|
1998-12-12 22:54:16 +03:00
|
|
|
|
|
|
|
/* ARGSUSED */
|
1997-10-24 02:24:09 +04:00
|
|
|
static char *
|
2000-09-05 02:06:28 +04:00
|
|
|
_get_prompt(EditLine *el)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
return (el_rl_prompt);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* generic function for moving around history
|
|
|
|
*/
|
1997-10-24 02:51:59 +04:00
|
|
|
static HIST_ENTRY *
|
2000-09-05 02:06:28 +04:00
|
|
|
_move_history(int op)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
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 = "";
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (&rl_he);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-02 19:14:07 +04:00
|
|
|
/*
|
|
|
|
* READLINE compatibility stuff
|
1997-10-24 02:24:09 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize rl compat stuff
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
rl_initialize(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
const LineInfo *li;
|
2000-12-24 01:02:20 +03:00
|
|
|
int i;
|
2001-01-06 01:45:30 +03:00
|
|
|
int editmode = 1;
|
|
|
|
struct termios t;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (e != NULL)
|
|
|
|
el_end(e);
|
|
|
|
if (h != NULL)
|
|
|
|
history_end(h);
|
|
|
|
|
|
|
|
if (!rl_instream)
|
|
|
|
rl_instream = stdin;
|
|
|
|
if (!rl_outstream)
|
|
|
|
rl_outstream = stdout;
|
2001-01-06 01:45:30 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
1998-05-20 05:03:06 +04:00
|
|
|
e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
2001-01-06 01:45:30 +03:00
|
|
|
if (!editmode)
|
|
|
|
el_set(e, EL_EDITMODE, 0);
|
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
h = history_init();
|
|
|
|
if (!e || !h)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
1998-05-20 05:03:06 +04:00
|
|
|
history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */
|
1997-10-24 02:24:09 +04:00
|
|
|
history_length = 0;
|
|
|
|
max_input_history = INT_MAX;
|
|
|
|
el_set(e, EL_HIST, history, h);
|
|
|
|
|
|
|
|
/* for proper prompt printing in readline() */
|
|
|
|
el_rl_prompt = strdup("");
|
|
|
|
el_set(e, EL_PROMPT, _get_prompt);
|
|
|
|
el_set(e, EL_SIGNAL, 1);
|
|
|
|
|
|
|
|
/* set default mode to "emacs"-style and read setting afterwards */
|
|
|
|
/* so this can be overriden */
|
|
|
|
el_set(e, EL_EDITOR, "emacs");
|
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
/*
|
|
|
|
* Word completition - this has to go AFTER rebinding keys
|
|
|
|
* to emacs-style.
|
|
|
|
*/
|
1997-10-24 02:24:09 +04:00
|
|
|
el_set(e, EL_ADDFN, "rl_complete",
|
2000-09-05 02:06:28 +04:00
|
|
|
"ReadLine compatible completition function",
|
|
|
|
_el_rl_complete);
|
1997-10-24 02:24:09 +04:00
|
|
|
el_set(e, EL_BIND, "^I", "rl_complete", NULL);
|
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/* read settings from configuration file */
|
|
|
|
el_source(e, NULL);
|
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
/*
|
|
|
|
* Unfortunately, some applications really do use rl_point
|
|
|
|
* and rl_line_buffer directly.
|
|
|
|
*/
|
1997-10-24 02:24:09 +04:00
|
|
|
li = el_line(e);
|
1998-12-12 22:54:16 +03:00
|
|
|
/* LINTED const cast */
|
1997-10-24 02:24:09 +04:00
|
|
|
rl_line_buffer = (char *) li->buffer;
|
|
|
|
rl_point = rl_end = 0;
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (0);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* read one line from input stream and return it, chomping
|
|
|
|
* trailing newline (if there is any)
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
readline(const char *prompt)
|
|
|
|
{
|
|
|
|
HistEvent ev;
|
1997-11-13 00:56:05 +03:00
|
|
|
int count;
|
1997-10-24 02:24:09 +04:00
|
|
|
const char *ret;
|
|
|
|
|
|
|
|
if (e == NULL || h == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
2000-03-10 15:55:15 +03:00
|
|
|
/* update prompt accordingly to what has been passed */
|
2000-09-05 02:06:28 +04:00
|
|
|
if (!prompt)
|
|
|
|
prompt = "";
|
1997-10-24 02:24:09 +04:00
|
|
|
if (strcmp(el_rl_prompt, prompt) != 0) {
|
|
|
|
free(el_rl_prompt);
|
|
|
|
el_rl_prompt = strdup(prompt);
|
|
|
|
}
|
|
|
|
/* get one line from input stream */
|
|
|
|
ret = el_gets(e, &count);
|
|
|
|
|
|
|
|
if (ret && count > 0) {
|
|
|
|
char *foo;
|
1998-12-12 22:54:16 +03:00
|
|
|
int lastidx;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
foo = strdup(ret);
|
|
|
|
lastidx = count - 1;
|
|
|
|
if (foo[lastidx] == '\n')
|
|
|
|
foo[lastidx] = '\0';
|
|
|
|
|
|
|
|
ret = foo;
|
|
|
|
} else
|
|
|
|
ret = NULL;
|
|
|
|
|
|
|
|
history(h, &ev, H_GETSIZE);
|
|
|
|
history_length = ev.num;
|
|
|
|
|
1998-12-12 22:54:16 +03:00
|
|
|
/* LINTED const cast */
|
1997-10-24 02:24:09 +04:00
|
|
|
return (char *) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* history functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is normally called before application starts to use
|
|
|
|
* history expansion functions
|
|
|
|
*/
|
|
|
|
void
|
2000-09-05 02:06:28 +04:00
|
|
|
using_history(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* substitute ``what'' with ``with'', returning resulting string; if
|
|
|
|
* globally == 1, substitutes all occurences of what, otherwise only the
|
|
|
|
* first one
|
|
|
|
*/
|
2000-09-05 02:06:28 +04:00
|
|
|
static char *
|
|
|
|
_rl_compat_sub(const char *str, const char *what, const char *with,
|
|
|
|
int globally)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char *result;
|
|
|
|
const char *temp, *new;
|
|
|
|
int len, with_len, what_len, add;
|
|
|
|
size_t size, i;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
result = malloc((size = 16));
|
|
|
|
temp = str;
|
|
|
|
with_len = strlen(with);
|
|
|
|
what_len = strlen(what);
|
|
|
|
len = 0;
|
|
|
|
do {
|
|
|
|
new = strstr(temp, what);
|
|
|
|
if (new) {
|
|
|
|
i = new - temp;
|
|
|
|
add = i + with_len;
|
|
|
|
if (i + add + 1 >= size) {
|
|
|
|
size += add + 1;
|
|
|
|
result = realloc(result, size);
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strncpy(&result[len], temp, i);
|
1997-10-24 02:24:09 +04:00
|
|
|
len += i;
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(&result[len], with); /* safe */
|
1997-10-24 02:24:09 +04:00
|
|
|
len += with_len;
|
|
|
|
temp = new + what_len;
|
|
|
|
} else {
|
|
|
|
add = strlen(temp);
|
|
|
|
if (len + add + 1 >= size) {
|
|
|
|
size += add + 1;
|
|
|
|
result = realloc(result, size);
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(&result[len], temp); /* safe */
|
1997-10-24 02:24:09 +04:00
|
|
|
len += add;
|
|
|
|
temp = NULL;
|
|
|
|
}
|
2000-03-10 16:06:43 +03:00
|
|
|
} while (temp && globally);
|
1997-10-24 02:24:09 +04:00
|
|
|
result[len] = '\0';
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (result);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* word designator ``%'' isn't supported (yet ?)
|
|
|
|
*
|
|
|
|
* 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
|
2000-09-05 02:06:28 +04:00
|
|
|
_history_expand_command(const char *command, size_t cmdlen, char **result)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char **arr, *tempcmd, *line, *search = NULL, *cmd;
|
|
|
|
const char *event_data = NULL;
|
|
|
|
static char *from = NULL, *to = NULL;
|
|
|
|
int start = -1, end = -1, max, i, idx;
|
|
|
|
int h_on = 0, t_on = 0, r_on = 0, e_on = 0, p_on = 0, g_on = 0;
|
|
|
|
int event_num = 0, retval;
|
|
|
|
size_t cmdsize;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
1998-12-12 22:54:16 +03:00
|
|
|
cmd = alloca(cmdlen + 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strncpy(cmd, command, cmdlen);
|
1998-12-12 22:54:16 +03:00
|
|
|
cmd[cmdlen] = 0;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
idx = 1;
|
|
|
|
/* find out which event to take */
|
|
|
|
if (cmd[idx] == history_expansion_char) {
|
|
|
|
event_num = history_length;
|
|
|
|
idx++;
|
|
|
|
} else {
|
2000-09-05 02:06:28 +04:00
|
|
|
int off, num;
|
1998-12-12 22:54:16 +03:00
|
|
|
size_t len;
|
1997-10-24 02:24:09 +04:00
|
|
|
off = idx;
|
|
|
|
while (cmd[off] && !strchr(":^$*-%", cmd[off]))
|
|
|
|
off++;
|
|
|
|
num = atoi(&cmd[idx]);
|
|
|
|
if (num != 0) {
|
|
|
|
event_num = num;
|
|
|
|
if (num < 0)
|
|
|
|
event_num += history_length + 1;
|
|
|
|
} else {
|
|
|
|
int prefix = 1, curr_num;
|
|
|
|
HistEvent ev;
|
|
|
|
|
|
|
|
len = off - idx;
|
|
|
|
if (cmd[idx] == '?') {
|
|
|
|
idx++, len--;
|
|
|
|
if (cmd[off - 1] == '?')
|
|
|
|
len--;
|
|
|
|
else if (cmd[off] != '\n' && cmd[off] != '\0')
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
prefix = 0;
|
|
|
|
}
|
|
|
|
search = alloca(len + 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strncpy(search, &cmd[idx], len);
|
1997-10-24 02:24:09 +04:00
|
|
|
search[len] = '\0';
|
|
|
|
|
|
|
|
if (history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
curr_num = ev.num;
|
|
|
|
|
|
|
|
if (prefix)
|
|
|
|
retval = history_search_prefix(search, -1);
|
|
|
|
else
|
|
|
|
retval = history_search(search, -1);
|
|
|
|
|
|
|
|
if (retval == -1) {
|
|
|
|
fprintf(rl_outstream, "%s: Event not found\n",
|
2000-09-05 02:06:28 +04:00
|
|
|
search);
|
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
if (history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
event_data = ev.str;
|
|
|
|
|
|
|
|
/* roll back to original position */
|
|
|
|
history(h, &ev, H_NEXT_EVENT, curr_num);
|
|
|
|
}
|
|
|
|
idx = off;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!event_data && event_num >= 0) {
|
1997-10-24 02:51:59 +04:00
|
|
|
HIST_ENTRY *rl_he;
|
1997-10-24 02:24:09 +04:00
|
|
|
rl_he = history_get(event_num);
|
|
|
|
if (!rl_he)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (0);
|
1997-10-24 02:24:09 +04:00
|
|
|
event_data = rl_he->line;
|
|
|
|
} else
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (cmd[idx] != ':')
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
cmd += idx + 1;
|
|
|
|
|
|
|
|
/* recognize cmd */
|
|
|
|
if (*cmd == '^')
|
|
|
|
start = end = 1, cmd++;
|
|
|
|
else if (*cmd == '$')
|
|
|
|
start = end = -1, cmd++;
|
|
|
|
else if (*cmd == '*')
|
|
|
|
start = 1, end = -1, cmd++;
|
1998-05-20 05:03:06 +04:00
|
|
|
else if (isdigit((unsigned char) *cmd)) {
|
1997-10-24 02:24:09 +04:00
|
|
|
const char *temp;
|
|
|
|
int shifted = 0;
|
|
|
|
|
|
|
|
start = atoi(cmd);
|
|
|
|
temp = cmd;
|
1998-05-20 05:03:06 +04:00
|
|
|
for (; isdigit((unsigned char) *cmd); cmd++);
|
1997-10-24 02:24:09 +04:00
|
|
|
if (temp != cmd)
|
|
|
|
shifted = 1;
|
|
|
|
if (shifted && *cmd == '-') {
|
1998-05-20 05:03:06 +04:00
|
|
|
if (!isdigit((unsigned char) *(cmd + 1)))
|
1997-10-24 02:24:09 +04:00
|
|
|
end = -2;
|
|
|
|
else {
|
|
|
|
end = atoi(cmd + 1);
|
1998-05-20 05:03:06 +04:00
|
|
|
for (; isdigit((unsigned char) *cmd); cmd++);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
} else if (shifted && *cmd == '*')
|
|
|
|
end = -1, cmd++;
|
|
|
|
else if (shifted)
|
|
|
|
end = start;
|
|
|
|
}
|
|
|
|
if (*cmd == ':')
|
|
|
|
cmd++;
|
|
|
|
|
|
|
|
line = strdup(event_data);
|
|
|
|
for (; *cmd; cmd++) {
|
|
|
|
if (*cmd == ':')
|
|
|
|
continue;
|
|
|
|
else if (*cmd == 'h')
|
|
|
|
h_on = 1 | g_on, g_on = 0;
|
|
|
|
else if (*cmd == 't')
|
|
|
|
t_on = 1 | g_on, g_on = 0;
|
|
|
|
else if (*cmd == 'r')
|
|
|
|
r_on = 1 | g_on, g_on = 0;
|
|
|
|
else if (*cmd == 'e')
|
|
|
|
e_on = 1 | g_on, g_on = 0;
|
|
|
|
else if (*cmd == 'p')
|
|
|
|
p_on = 1 | g_on, g_on = 0;
|
|
|
|
else if (*cmd == 'g')
|
|
|
|
g_on = 2;
|
|
|
|
else if (*cmd == 's' || *cmd == '&') {
|
2000-09-05 02:06:28 +04:00
|
|
|
char *what, *with, delim;
|
|
|
|
int len, from_len;
|
1998-12-12 22:54:16 +03:00
|
|
|
size_t size;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (*cmd == '&' && (from == NULL || to == NULL))
|
|
|
|
continue;
|
|
|
|
else if (*cmd == 's') {
|
|
|
|
delim = *(++cmd), cmd++;
|
|
|
|
size = 16;
|
1998-12-12 22:54:16 +03:00
|
|
|
what = realloc(from, size);
|
1997-10-24 02:24:09 +04:00
|
|
|
len = 0;
|
|
|
|
for (; *cmd && *cmd != delim; cmd++) {
|
|
|
|
if (*cmd == '\\'
|
|
|
|
&& *(cmd + 1) == delim)
|
|
|
|
cmd++;
|
|
|
|
if (len >= size)
|
|
|
|
what = realloc(what,
|
|
|
|
(size <<= 1));
|
|
|
|
what[len++] = *cmd;
|
|
|
|
}
|
|
|
|
what[len] = '\0';
|
|
|
|
from = what;
|
|
|
|
if (*what == '\0') {
|
|
|
|
free(what);
|
|
|
|
if (search)
|
|
|
|
from = strdup(search);
|
|
|
|
else {
|
|
|
|
from = NULL;
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd++; /* shift after delim */
|
|
|
|
if (!*cmd)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
size = 16;
|
1998-12-12 22:54:16 +03:00
|
|
|
with = realloc(to, size);
|
1997-10-24 02:24:09 +04:00
|
|
|
len = 0;
|
|
|
|
from_len = strlen(from);
|
|
|
|
for (; *cmd && *cmd != delim; cmd++) {
|
|
|
|
if (len + from_len + 1 >= size) {
|
|
|
|
size += from_len + 1;
|
|
|
|
with = realloc(with, size);
|
|
|
|
}
|
|
|
|
if (*cmd == '&') {
|
|
|
|
/* safe */
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(&with[len], from);
|
1997-10-24 02:24:09 +04:00
|
|
|
len += from_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*cmd == '\\'
|
|
|
|
&& (*(cmd + 1) == delim
|
|
|
|
|| *(cmd + 1) == '&'))
|
|
|
|
cmd++;
|
|
|
|
with[len++] = *cmd;
|
|
|
|
}
|
|
|
|
with[len] = '\0';
|
|
|
|
to = with;
|
|
|
|
|
1999-07-02 19:14:07 +04:00
|
|
|
tempcmd = _rl_compat_sub(line, from, to,
|
1997-10-24 02:24:09 +04:00
|
|
|
(g_on) ? 1 : 0);
|
|
|
|
free(line);
|
1998-12-12 22:54:16 +03:00
|
|
|
line = tempcmd;
|
1997-10-24 02:24:09 +04:00
|
|
|
g_on = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
arr = history_tokenize(line);
|
|
|
|
free(line); /* no more needed */
|
|
|
|
if (arr && *arr == NULL)
|
|
|
|
free(arr), arr = NULL;
|
|
|
|
if (!arr)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* find out max valid idx to array of array */
|
|
|
|
max = 0;
|
|
|
|
for (i = 0; arr[i]; i++)
|
|
|
|
max++;
|
|
|
|
max--;
|
|
|
|
|
|
|
|
/* set boundaries to something relevant */
|
|
|
|
if (start < 0)
|
|
|
|
start = 1;
|
|
|
|
if (end < 0)
|
|
|
|
end = max - ((end < -1) ? 1 : 0);
|
|
|
|
|
|
|
|
/* check boundaries ... */
|
|
|
|
if (start > max || end > max || start > end)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
for (i = 0; i <= max; i++) {
|
2000-09-05 02:06:28 +04:00
|
|
|
char *temp;
|
1997-10-24 02:24:09 +04:00
|
|
|
if (h_on && (i == 1 || h_on > 1) &&
|
|
|
|
(temp = strrchr(arr[i], '/')))
|
|
|
|
*(temp + 1) = '\0';
|
|
|
|
if (t_on && (i == 1 || t_on > 1) &&
|
|
|
|
(temp = strrchr(arr[i], '/')))
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(arr[i], temp + 1);
|
1997-10-24 02:24:09 +04:00
|
|
|
if (r_on && (i == 1 || r_on > 1) &&
|
|
|
|
(temp = strrchr(arr[i], '.')))
|
|
|
|
*temp = '\0';
|
|
|
|
if (e_on && (i == 1 || e_on > 1) &&
|
|
|
|
(temp = strrchr(arr[i], '.')))
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(arr[i], temp);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
1998-12-12 22:54:16 +03:00
|
|
|
cmdsize = 1, cmdlen = 0;
|
|
|
|
tempcmd = malloc(cmdsize);
|
1997-10-24 02:24:09 +04:00
|
|
|
for (i = start; start <= i && i <= end; i++) {
|
2000-09-05 02:06:28 +04:00
|
|
|
int arr_len;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
arr_len = strlen(arr[i]);
|
1998-12-12 22:54:16 +03:00
|
|
|
if (cmdlen + arr_len + 1 >= cmdsize) {
|
|
|
|
cmdsize += arr_len + 1;
|
|
|
|
tempcmd = realloc(tempcmd, cmdsize);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(&tempcmd[cmdlen], arr[i]); /* safe */
|
1998-12-12 22:54:16 +03:00
|
|
|
cmdlen += arr_len;
|
|
|
|
tempcmd[cmdlen++] = ' '; /* add a space */
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
1998-12-12 22:54:16 +03:00
|
|
|
while (cmdlen > 0 && isspace((unsigned char) tempcmd[cmdlen - 1]))
|
|
|
|
cmdlen--;
|
|
|
|
tempcmd[cmdlen] = '\0';
|
1997-10-24 02:24:09 +04:00
|
|
|
|
1998-12-12 22:54:16 +03:00
|
|
|
*result = tempcmd;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
for (i = 0; i <= max; i++)
|
|
|
|
free(arr[i]);
|
|
|
|
free(arr), arr = (char **) NULL;
|
|
|
|
return (p_on) ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* csh-style history expansion
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
history_expand(char *str, char **output)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int i, retval = 0, idx;
|
|
|
|
size_t size;
|
|
|
|
char *temp, *result;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
|
|
|
*output = strdup(str); /* do it early */
|
|
|
|
|
|
|
|
if (str[0] == history_subst_char) {
|
|
|
|
/* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
|
|
|
|
temp = alloca(4 + strlen(str) + 1);
|
|
|
|
temp[0] = temp[1] = history_expansion_char;
|
|
|
|
temp[2] = ':';
|
|
|
|
temp[3] = 's';
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(temp + 4, str);
|
1997-10-24 02:24:09 +04:00
|
|
|
str = temp;
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
#define ADD_STRING(what, len) \
|
1997-10-24 02:24:09 +04:00
|
|
|
{ \
|
|
|
|
if (idx + len + 1 > size) \
|
|
|
|
result = realloc(result, (size += len + 1)); \
|
|
|
|
(void)strncpy(&result[idx], what, len); \
|
|
|
|
idx += len; \
|
|
|
|
result[idx] = '\0'; \
|
|
|
|
}
|
|
|
|
|
|
|
|
result = NULL;
|
|
|
|
size = idx = 0;
|
|
|
|
for (i = 0; str[i];) {
|
1998-12-12 22:54:16 +03:00
|
|
|
int start, j, loop_again;
|
|
|
|
size_t len;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
loop_again = 1;
|
|
|
|
start = j = i;
|
|
|
|
loop:
|
|
|
|
for (; str[j]; j++) {
|
|
|
|
if (str[j] == '\\' &&
|
|
|
|
str[j + 1] == history_expansion_char) {
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(&str[j], &str[j + 1]);
|
1997-10-24 02:24:09 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!loop_again) {
|
|
|
|
if (str[j] == '?') {
|
|
|
|
while (str[j] && str[++j] != '?');
|
|
|
|
if (str[j] == '?')
|
|
|
|
j++;
|
1998-05-20 05:03:06 +04:00
|
|
|
} else if (isspace((unsigned char) str[j]))
|
1997-10-24 02:24:09 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (str[j] == history_expansion_char
|
|
|
|
&& !strchr(history_no_expand_chars, str[j + 1])
|
|
|
|
&& (!history_inhibit_expansion_function ||
|
2000-09-05 02:06:28 +04:00
|
|
|
(*history_inhibit_expansion_function)(str, j) == 0))
|
1997-10-24 02:24:09 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str[j] && str[j + 1] != '#' && loop_again) {
|
|
|
|
i = j;
|
|
|
|
j++;
|
|
|
|
if (str[j] == history_expansion_char)
|
|
|
|
j++;
|
|
|
|
loop_again = 0;
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
len = i - start;
|
|
|
|
temp = &str[start];
|
|
|
|
ADD_STRING(temp, len);
|
|
|
|
|
|
|
|
if (str[i] == '\0' || str[i] != history_expansion_char
|
|
|
|
|| str[i + 1] == '#') {
|
|
|
|
len = j - i;
|
|
|
|
temp = &str[i];
|
|
|
|
ADD_STRING(temp, len);
|
|
|
|
if (start == 0)
|
|
|
|
retval = 0;
|
|
|
|
else
|
|
|
|
retval = 1;
|
|
|
|
break;
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
retval = _history_expand_command(&str[i], (size_t) (j - i),
|
|
|
|
&temp);
|
1997-10-24 02:24:09 +04:00
|
|
|
if (retval != -1) {
|
|
|
|
len = strlen(temp);
|
|
|
|
ADD_STRING(temp, len);
|
|
|
|
}
|
|
|
|
i = j;
|
|
|
|
} /* for(i ...) */
|
|
|
|
|
|
|
|
if (retval == 2) {
|
|
|
|
add_history(temp);
|
|
|
|
#ifdef GDB_411_HACK
|
|
|
|
/* gdb 4.11 has been shipped with readline, where */
|
|
|
|
/* history_expand() returned -1 when the line */
|
|
|
|
/* should not be executed; in readline 2.1+ */
|
|
|
|
/* it should return 2 in such a case */
|
|
|
|
retval = -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
free(*output);
|
|
|
|
*output = result;
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (retval);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
2000-03-10 15:55:15 +03:00
|
|
|
* Parse the string into individual tokens, similarily to how shell would do it.
|
1997-10-24 02:24:09 +04:00
|
|
|
*/
|
|
|
|
char **
|
2000-09-05 02:06:28 +04:00
|
|
|
history_tokenize(const char *str)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int size = 1, result_idx = 0, i, start;
|
1998-12-12 22:54:16 +03:00
|
|
|
size_t len;
|
1997-10-24 02:24:09 +04:00
|
|
|
char **result = NULL, *temp, delim = '\0';
|
|
|
|
|
|
|
|
for (i = 0; str[i]; i++) {
|
1998-05-20 05:03:06 +04:00
|
|
|
while (isspace((unsigned char) str[i]))
|
1997-10-24 02:24:09 +04:00
|
|
|
i++;
|
|
|
|
start = i;
|
|
|
|
for (; str[i]; i++) {
|
2000-03-10 15:55:15 +03:00
|
|
|
if (str[i] == '\\') {
|
2001-01-01 14:03:16 +03:00
|
|
|
if (str[i+1] != '\0')
|
2000-03-10 15:55:15 +03:00
|
|
|
i++;
|
|
|
|
} else if (str[i] == delim)
|
1997-10-24 02:24:09 +04:00
|
|
|
delim = '\0';
|
|
|
|
else if (!delim &&
|
2000-09-05 02:06:28 +04:00
|
|
|
(isspace((unsigned char) str[i]) ||
|
|
|
|
strchr("()<>;&|$", str[i])))
|
1997-10-24 02:24:09 +04:00
|
|
|
break;
|
|
|
|
else if (!delim && strchr("'`\"", str[i]))
|
|
|
|
delim = str[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result_idx + 2 >= size) {
|
|
|
|
size <<= 1;
|
|
|
|
result = realloc(result, size * sizeof(char *));
|
|
|
|
}
|
|
|
|
len = i - start;
|
|
|
|
temp = malloc(len + 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strncpy(temp, &str[start], len);
|
1997-10-24 02:24:09 +04:00
|
|
|
temp[len] = '\0';
|
|
|
|
result[result_idx++] = temp;
|
|
|
|
result[result_idx] = NULL;
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (result);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* limit size of history record to ``max'' events
|
|
|
|
*/
|
|
|
|
void
|
2000-09-05 02:06:28 +04:00
|
|
|
stifle_history(int max)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
1998-05-20 05:03:06 +04:00
|
|
|
if (history(h, &ev, H_SETSIZE, max) == 0)
|
1997-10-24 02:24:09 +04:00
|
|
|
max_input_history = max;
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* "unlimit" size of history - set the limit to maximum allowed int value
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
unstifle_history(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
int omax;
|
|
|
|
|
1998-05-20 05:03:06 +04:00
|
|
|
history(h, &ev, H_SETSIZE, INT_MAX);
|
1997-10-24 02:24:09 +04:00
|
|
|
omax = max_input_history;
|
|
|
|
max_input_history = INT_MAX;
|
2000-09-05 02:06:28 +04:00
|
|
|
return (omax); /* some value _must_ be returned */
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
history_is_stifled(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/* cannot return true answer */
|
|
|
|
return (max_input_history != INT_MAX);
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* read history from a file given
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
read_history(const char *filename)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
2000-09-05 02:06:28 +04:00
|
|
|
return (history(h, &ev, H_LOAD, filename));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* write history to a file given
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
write_history(const char *filename)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
2000-09-05 02:06:28 +04:00
|
|
|
return (history(h, &ev, H_SAVE, filename));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* returns history ``num''th event
|
|
|
|
*
|
|
|
|
* returned pointer points to static variable
|
|
|
|
*/
|
1997-10-24 02:51:59 +04:00
|
|
|
HIST_ENTRY *
|
2000-09-05 02:06:28 +04:00
|
|
|
history_get(int num)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
static HIST_ENTRY she;
|
|
|
|
HistEvent ev;
|
|
|
|
int i = 1, curr_num;
|
|
|
|
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
|
|
|
/* rewind to beginning */
|
|
|
|
if (history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (NULL);
|
1997-10-24 02:24:09 +04:00
|
|
|
curr_num = ev.num;
|
|
|
|
if (history(h, &ev, H_LAST) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (NULL); /* error */
|
1997-10-24 02:24:09 +04:00
|
|
|
while (i < num && history(h, &ev, H_PREV) == 0)
|
|
|
|
i++;
|
|
|
|
if (i != num)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (NULL); /* not so many entries */
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
she.line = ev.str;
|
|
|
|
she.data = NULL;
|
|
|
|
|
|
|
|
/* rewind history to the same event it was before */
|
|
|
|
(void) history(h, &ev, H_FIRST);
|
|
|
|
(void) history(h, &ev, H_NEXT_EVENT, curr_num);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (&she);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* add the line to history table
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
add_history(const char *line)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
|
|
|
(void) history(h, &ev, H_ENTER, line);
|
|
|
|
if (history(h, &ev, H_GETSIZE) == 0)
|
|
|
|
history_length = ev.num;
|
|
|
|
|
|
|
|
return (!(history_length > 0)); /* return 0 if all is okay */
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* clear the history list - delete all entries
|
|
|
|
*/
|
|
|
|
void
|
2000-09-05 02:06:28 +04:00
|
|
|
clear_history(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
history(h, &ev, H_CLEAR);
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* returns offset of the current history event
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
where_history(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
int curr_num, off;
|
|
|
|
|
|
|
|
if (history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (0);
|
1997-10-24 02:24:09 +04:00
|
|
|
curr_num = ev.num;
|
|
|
|
|
|
|
|
history(h, &ev, H_FIRST);
|
|
|
|
off = 1;
|
|
|
|
while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
|
|
|
|
off++;
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (off);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* returns current history event or NULL if there is no such event
|
|
|
|
*/
|
1997-10-24 02:51:59 +04:00
|
|
|
HIST_ENTRY *
|
2000-09-05 02:06:28 +04:00
|
|
|
current_history(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
return (_move_history(H_CURR));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* returns total number of bytes history events' data are using
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
history_total_bytes(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
int curr_num, size;
|
|
|
|
|
|
|
|
if (history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
curr_num = ev.num;
|
|
|
|
|
|
|
|
history(h, &ev, H_FIRST);
|
|
|
|
size = 0;
|
|
|
|
do
|
|
|
|
size += strlen(ev.str);
|
|
|
|
while (history(h, &ev, H_NEXT) == 0);
|
|
|
|
|
|
|
|
/* get to the same position as before */
|
|
|
|
history(h, &ev, H_PREV_EVENT, curr_num);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (size);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* sets the position in the history list to ``pos''
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
history_set_pos(int pos)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
HistEvent ev;
|
|
|
|
int off, curr_num;
|
|
|
|
|
|
|
|
if (pos > history_length || pos < 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
history(h, &ev, H_CURR);
|
|
|
|
curr_num = ev.num;
|
|
|
|
history(h, &ev, H_FIRST);
|
|
|
|
off = 0;
|
|
|
|
while (off < pos && history(h, &ev, H_NEXT) == 0)
|
|
|
|
off++;
|
|
|
|
|
|
|
|
if (off != pos) { /* do a rollback in case of error */
|
|
|
|
history(h, &ev, H_FIRST);
|
|
|
|
history(h, &ev, H_NEXT_EVENT, curr_num);
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
return (0);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* returns previous event in history and shifts pointer accordingly
|
|
|
|
*/
|
1997-10-24 02:51:59 +04:00
|
|
|
HIST_ENTRY *
|
2000-09-05 02:06:28 +04:00
|
|
|
previous_history(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
return (_move_history(H_PREV));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* returns next event in history and shifts pointer accordingly
|
|
|
|
*/
|
1997-10-24 02:51:59 +04:00
|
|
|
HIST_ENTRY *
|
2000-09-05 02:06:28 +04:00
|
|
|
next_history(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
return (_move_history(H_NEXT));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* generic history search function
|
|
|
|
*/
|
|
|
|
static int
|
2000-09-05 02:06:28 +04:00
|
|
|
_history_search_gen(const char *str, int direction, int pos)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
HistEvent ev;
|
|
|
|
const char *strp;
|
|
|
|
int curr_num;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
curr_num = ev.num;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
strp = strstr(ev.str, str);
|
|
|
|
if (strp && (pos < 0 || &ev.str[pos] == strp))
|
|
|
|
return (int) (strp - ev.str);
|
|
|
|
if (history(h, &ev, direction < 0 ? H_PREV : H_NEXT) != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
history(h, &ev, direction < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* searches for first history event containing the str
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
history_search(const char *str, int direction)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
return (_history_search_gen(str, direction, -1));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* searches for first history event beginning with str
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
history_search_prefix(const char *str, int direction)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
return (_history_search_gen(str, direction, 0));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* search for event in history containing str, starting at offset
|
|
|
|
* abs(pos); continue backward, if pos<0, forward otherwise
|
|
|
|
*/
|
1998-12-12 22:54:16 +03:00
|
|
|
/* ARGSUSED */
|
1997-10-24 02:24:09 +04:00
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
history_search_pos(const char *str, int direction, int pos)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
HistEvent ev;
|
|
|
|
int curr_num, off;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
off = (pos > 0) ? pos : -pos;
|
|
|
|
pos = (pos > 0) ? 1 : -1;
|
|
|
|
|
|
|
|
if (history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
curr_num = ev.num;
|
|
|
|
|
|
|
|
if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (strstr(ev.str, str))
|
2000-09-05 02:06:28 +04:00
|
|
|
return (off);
|
1997-10-24 02:24:09 +04:00
|
|
|
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);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (-1);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************/
|
2000-09-05 02:06:28 +04:00
|
|
|
/* completition functions */
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
1997-10-24 02:51:59 +04:00
|
|
|
char *
|
2000-09-05 02:06:28 +04:00
|
|
|
tilde_expand(char *txt)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
struct passwd *pass;
|
|
|
|
char *temp;
|
|
|
|
size_t len = 0;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (txt[0] != '~')
|
2000-09-05 02:06:28 +04:00
|
|
|
return (strdup(txt));
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
temp = strchr(txt + 1, '/');
|
|
|
|
if (temp == NULL)
|
|
|
|
temp = strdup(txt + 1);
|
|
|
|
else {
|
|
|
|
len = temp - txt + 1; /* text until string after slash */
|
|
|
|
temp = malloc(len);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strncpy(temp, txt + 1, len - 2);
|
1997-10-24 02:24:09 +04:00
|
|
|
temp[len - 2] = '\0';
|
|
|
|
}
|
|
|
|
pass = getpwnam(temp);
|
|
|
|
free(temp); /* value no more needed */
|
|
|
|
if (pass == NULL)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (strdup(txt));
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* update pointer txt to point at string immedially following */
|
|
|
|
/* first slash */
|
|
|
|
txt += len;
|
|
|
|
|
|
|
|
temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) sprintf(temp, "%s/%s", pass->pw_dir, txt);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (temp);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* return first found file name starting by the ``text'' or NULL if no
|
|
|
|
* such file can be found
|
|
|
|
* value of ``state'' is ignored
|
|
|
|
*
|
|
|
|
* it's caller's responsibility to free returned string
|
|
|
|
*/
|
2000-09-05 02:06:28 +04:00
|
|
|
char *
|
|
|
|
filename_completion_function(const char *text, int state)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
static DIR *dir = NULL;
|
|
|
|
static char *filename = NULL, *dirname = NULL;
|
|
|
|
static size_t filename_len = 0;
|
|
|
|
struct dirent *entry;
|
|
|
|
char *temp;
|
|
|
|
size_t len;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (state == 0 || dir == NULL) {
|
|
|
|
if (dir != NULL) {
|
|
|
|
closedir(dir);
|
|
|
|
dir = NULL;
|
|
|
|
}
|
|
|
|
temp = strrchr(text, '/');
|
|
|
|
if (temp) {
|
|
|
|
temp++;
|
|
|
|
filename = realloc(filename, strlen(temp) + 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(filename, temp);
|
1997-10-24 02:24:09 +04:00
|
|
|
len = temp - text; /* including last slash */
|
|
|
|
dirname = realloc(dirname, len + 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strncpy(dirname, text, len);
|
1997-10-24 02:24:09 +04:00
|
|
|
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);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strcpy(dirname, temp); /* safe */
|
2000-12-24 01:02:20 +03:00
|
|
|
free(temp); /* no longer needed */
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
/* will be used in cycle */
|
|
|
|
filename_len = strlen(filename);
|
|
|
|
if (filename_len == 0)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (NULL); /* no expansion possible */
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
dir = opendir(dirname ? dirname : ".");
|
|
|
|
if (!dir)
|
2000-09-05 02:06:28 +04:00
|
|
|
return (NULL); /* cannot open the directory */
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
/* find the match */
|
1998-12-12 22:54:16 +03:00
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
1997-10-24 02:24:09 +04:00
|
|
|
/* otherwise, get first entry where first */
|
|
|
|
/* filename_len characters are equal */
|
|
|
|
if (entry->d_name[0] == filename[0]
|
1999-06-12 22:58:05 +04:00
|
|
|
#if defined(__SVR4) || defined(__linux__)
|
1998-05-20 05:03:06 +04:00
|
|
|
&& strlen(entry->d_name) >= filename_len
|
1999-06-12 22:58:05 +04:00
|
|
|
#else
|
|
|
|
&& entry->d_namlen >= filename_len
|
1998-05-20 05:03:06 +04:00
|
|
|
#endif
|
1997-10-24 02:24:09 +04:00
|
|
|
&& strncmp(entry->d_name, filename,
|
2000-09-05 02:06:28 +04:00
|
|
|
filename_len) == 0)
|
1997-10-24 02:24:09 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry) { /* match found */
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
struct stat stbuf;
|
1999-06-12 22:58:05 +04:00
|
|
|
#if defined(__SVR4) || defined(__linux__)
|
1998-05-20 05:03:06 +04:00
|
|
|
len = strlen(entry->d_name) +
|
1999-06-12 22:58:05 +04:00
|
|
|
#else
|
|
|
|
len = entry->d_namlen +
|
1998-05-20 05:03:06 +04:00
|
|
|
#endif
|
2000-09-05 02:06:28 +04:00
|
|
|
((dirname) ? strlen(dirname) : 0) + 1 + 1;
|
1997-10-24 02:24:09 +04:00
|
|
|
temp = malloc(len);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) sprintf(temp, "%s%s",
|
|
|
|
dirname ? dirname : "", entry->d_name); /* safe */
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* test, if it's directory */
|
|
|
|
if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
|
|
|
|
strcat(temp, "/"); /* safe */
|
|
|
|
} else
|
|
|
|
temp = NULL;
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (temp);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2000-09-05 02:06:28 +04:00
|
|
|
char *
|
|
|
|
username_completion_function(const char *text, int state)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
struct passwd *pwd;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (text[0] == '\0')
|
2000-09-05 02:06:28 +04:00
|
|
|
return (NULL);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (*text == '~')
|
|
|
|
text++;
|
|
|
|
|
|
|
|
if (state == 0)
|
|
|
|
setpwent();
|
|
|
|
|
|
|
|
while ((pwd = getpwent()) && text[0] == pwd->pw_name[0]
|
2000-09-05 02:06:28 +04:00
|
|
|
&& strcmp(text, pwd->pw_name) == 0);
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (pwd == NULL) {
|
|
|
|
endpwent();
|
2000-09-05 02:06:28 +04:00
|
|
|
return (NULL);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
return (strdup(pwd->pw_name));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* el-compatible wrapper around rl_complete; needed for key binding
|
|
|
|
*/
|
1998-12-12 22:54:16 +03:00
|
|
|
/* ARGSUSED */
|
1997-10-24 02:24:09 +04:00
|
|
|
static unsigned char
|
2000-09-05 02:06:28 +04:00
|
|
|
_el_rl_complete(EditLine *el, int ch)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
return (unsigned char) rl_complete(0, ch);
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* returns list of completitions for text given
|
|
|
|
*/
|
2000-09-05 02:06:28 +04:00
|
|
|
char **
|
|
|
|
completion_matches(const char *text, CPFunction *genfunc)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char **match_list = NULL, *retstr, *prevstr;
|
2000-12-24 01:02:20 +03:00
|
|
|
size_t match_list_len, max_equal, which, i;
|
2000-09-05 02:06:28 +04:00
|
|
|
int matches;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
|
|
|
matches = 0;
|
2000-12-24 01:02:20 +03:00
|
|
|
match_list_len = 1;
|
1998-12-12 22:54:16 +03:00
|
|
|
while ((retstr = (*genfunc) (text, matches)) != NULL) {
|
2000-12-24 01:02:20 +03:00
|
|
|
if (matches + 1 >= match_list_len) {
|
|
|
|
match_list_len <<= 1;
|
1997-10-24 02:24:09 +04:00
|
|
|
match_list = realloc(match_list,
|
2000-12-24 01:02:20 +03:00
|
|
|
match_list_len * sizeof(char *));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
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];
|
1998-12-12 22:54:16 +03:00
|
|
|
max_equal = strlen(prevstr);
|
2000-12-24 01:02:20 +03:00
|
|
|
for (; which <= matches; which++) {
|
1997-10-24 02:24:09 +04:00
|
|
|
for (i = 0; i < max_equal &&
|
|
|
|
prevstr[i] == match_list[which][i]; i++)
|
|
|
|
continue;
|
|
|
|
max_equal = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
retstr = malloc(max_equal + 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) strncpy(retstr, match_list[1], max_equal);
|
1997-10-24 02:24:09 +04:00
|
|
|
retstr[max_equal] = '\0';
|
|
|
|
match_list[0] = retstr;
|
|
|
|
|
|
|
|
/* add NULL as last pointer to the array */
|
2000-12-24 01:02:20 +03:00
|
|
|
if (matches + 1 >= match_list_len)
|
1997-10-24 02:24:09 +04:00
|
|
|
match_list = realloc(match_list,
|
2000-12-24 01:02:20 +03:00
|
|
|
(match_list_len + 1) * sizeof(char *));
|
1997-10-24 02:24:09 +04:00
|
|
|
match_list[matches + 1] = (char *) NULL;
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (match_list);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
/*
|
|
|
|
* Sort function for qsort(). Just wrapper around strcasecmp().
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
_rl_qsort_string_compare(i1, i2)
|
|
|
|
const void *i1, *i2;
|
|
|
|
{
|
2001-01-04 18:55:53 +03:00
|
|
|
/*LINTED const castaway*/
|
2000-12-24 01:02:20 +03:00
|
|
|
const char *s1 = ((const char **)i1)[0];
|
2001-01-04 18:55:53 +03:00
|
|
|
/*LINTED const castaway*/
|
2000-12-31 12:50:32 +03:00
|
|
|
const char *s2 = ((const char **)i2)[0];
|
2000-12-24 01:02:20 +03:00
|
|
|
|
|
|
|
return strcasecmp(s1, s2);
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
2000-12-24 01:02:20 +03:00
|
|
|
* 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'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rl_display_match_list (matches, len, max)
|
|
|
|
char **matches;
|
|
|
|
int len, max;
|
|
|
|
{
|
|
|
|
int i, idx, limit, count;
|
|
|
|
int screenwidth = e->el_term.t_size.h;
|
|
|
|
|
2000-12-31 12:50:32 +03:00
|
|
|
/*
|
|
|
|
* Find out how many entries can be put on one line, count
|
|
|
|
* with two spaces between strings.
|
|
|
|
*/
|
|
|
|
limit = screenwidth / (max + 2);
|
2000-12-24 01:02:20 +03:00
|
|
|
if (limit == 0)
|
|
|
|
limit = 1;
|
|
|
|
|
|
|
|
/* how many lines of output */
|
|
|
|
count = len / limit;
|
2000-12-31 12:50:32 +03:00
|
|
|
if (count * limit < len)
|
|
|
|
count++;
|
2000-12-24 01:02:20 +03:00
|
|
|
|
|
|
|
/* Sort the items if they are not already sorted. */
|
2001-01-04 18:55:53 +03:00
|
|
|
qsort(&matches[1], (size_t)(len - 1), sizeof(char *),
|
|
|
|
_rl_qsort_string_compare);
|
2000-12-24 01:02:20 +03:00
|
|
|
|
|
|
|
idx = 1;
|
2000-12-31 12:50:32 +03:00
|
|
|
for(; count > 0; count--) {
|
2000-12-24 01:02:20 +03:00
|
|
|
for(i=0; i < limit && matches[idx]; i++, idx++)
|
2000-12-31 12:50:32 +03:00
|
|
|
fprintf(e->el_outfile, "%-*s ", max, matches[idx]);
|
2000-12-24 01:02:20 +03:00
|
|
|
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
|
1997-10-24 02:24:09 +04:00
|
|
|
*/
|
|
|
|
static int
|
2000-09-05 02:06:28 +04:00
|
|
|
rl_complete_internal(int what_to_do)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
CPFunction *complet_func;
|
1997-10-24 02:24:09 +04:00
|
|
|
const LineInfo *li;
|
2000-12-24 01:02:20 +03:00
|
|
|
char *temp, **matches;
|
|
|
|
const char *ctemp;
|
2000-09-05 02:06:28 +04:00
|
|
|
size_t len;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
rl_completion_type = what_to_do;
|
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
|
|
|
complet_func = rl_completion_entry_function;
|
|
|
|
if (!complet_func)
|
|
|
|
complet_func = filename_completion_function;
|
|
|
|
|
2001-01-01 18:52:25 +03:00
|
|
|
/* We now look backwards for the start of a filename/variable word */
|
1997-10-24 02:24:09 +04:00
|
|
|
li = el_line(e);
|
2001-01-04 18:55:53 +03:00
|
|
|
ctemp = (const char *) li->cursor;
|
2001-01-01 18:52:25 +03:00
|
|
|
while (ctemp > li->buffer
|
|
|
|
&& !strchr(rl_basic_word_break_characters, ctemp[-1])
|
|
|
|
&& (!rl_special_prefixes
|
|
|
|
|| !strchr(rl_special_prefixes, ctemp[-1]) ) )
|
2000-12-24 01:02:20 +03:00
|
|
|
ctemp--;
|
|
|
|
|
|
|
|
len = li->cursor - ctemp;
|
|
|
|
temp = alloca(len + 1);
|
|
|
|
(void) strncpy(temp, ctemp, len);
|
1997-10-24 02:24:09 +04:00
|
|
|
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)
|
2000-12-24 01:02:20 +03:00
|
|
|
matches = completion_matches(temp, complet_func);
|
1997-10-24 02:24:09 +04:00
|
|
|
else {
|
2000-09-05 02:06:28 +04:00
|
|
|
int end = li->cursor - li->buffer;
|
2000-12-24 01:02:20 +03:00
|
|
|
matches = (*rl_attempted_completion_function) (temp, (int)
|
2000-09-05 02:06:28 +04:00
|
|
|
(end - len), end);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
if (matches) {
|
|
|
|
int i, retval = CC_REFRESH;
|
|
|
|
int matches_num, maxlen, match_len, match_display=1;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
2001-01-01 14:03:16 +03:00
|
|
|
/*
|
|
|
|
* 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]);
|
|
|
|
}
|
2000-12-24 01:02:20 +03:00
|
|
|
|
|
|
|
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]);
|
2001-01-01 18:52:25 +03:00
|
|
|
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);
|
|
|
|
}
|
2000-12-24 01:02:20 +03:00
|
|
|
} 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;
|
2001-01-10 11:10:45 +03:00
|
|
|
} 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);
|
2000-12-24 01:02:20 +03:00
|
|
|
} else {
|
1997-10-24 02:24:09 +04:00
|
|
|
/* lcd is not a valid object - further specification */
|
|
|
|
/* is needed */
|
1998-05-20 05:03:06 +04:00
|
|
|
el_beep(e);
|
2000-12-24 01:02:20 +03:00
|
|
|
retval = CC_NORM;
|
|
|
|
}
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
/* free elements of array and the array itself */
|
2000-12-24 01:02:20 +03:00
|
|
|
for (i = 0; matches[i]; i++)
|
|
|
|
free(matches[i]);
|
|
|
|
free(matches), matches = NULL;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
2000-12-24 01:02:20 +03:00
|
|
|
return (retval);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
return (CC_NORM);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* complete word at current point
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
rl_complete(int ignore, int invoking_key)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
|
|
|
if (rl_inhibit_completion) {
|
|
|
|
rl_insert(ignore, invoking_key);
|
2000-09-05 02:06:28 +04:00
|
|
|
return (CC_REFRESH);
|
2000-12-24 01:02:20 +03:00
|
|
|
} 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));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
1999-07-02 19:14:07 +04:00
|
|
|
* misc other functions
|
1997-10-24 02:24:09 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bind key c to readline-type function func
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
rl_bind_key(int c, int func(int, int))
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int retval = -1;
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
return (retval);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* read one key from input - handles chars pushed back
|
|
|
|
* to input stream also
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
rl_read_key(void)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char fooarr[2 * sizeof(int)];
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
if (e == NULL || h == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (el_getc(e, fooarr));
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* reset the terminal
|
|
|
|
*/
|
1998-12-12 22:54:16 +03:00
|
|
|
/* ARGSUSED */
|
1997-10-24 02:24:09 +04:00
|
|
|
void
|
2000-09-05 02:06:28 +04:00
|
|
|
rl_reset_terminal(const char *p)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
if (h == NULL || e == NULL)
|
|
|
|
rl_initialize();
|
|
|
|
el_reset(e);
|
|
|
|
}
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
|
1997-10-24 02:24:09 +04:00
|
|
|
/*
|
|
|
|
* insert character ``c'' back into input stream, ``count'' times
|
|
|
|
*/
|
|
|
|
int
|
2000-09-05 02:06:28 +04:00
|
|
|
rl_insert(int count, int c)
|
1997-10-24 02:24:09 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char arr[2];
|
1997-10-24 02:24:09 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
return (0);
|
1997-10-24 02:24:09 +04:00
|
|
|
}
|