readline.c (255891) | readline.c (276881) |
---|---|
1/* $NetBSD: readline.c,v 1.90 2010/08/04 20:29:18 christos Exp $ */ | 1/* $NetBSD: readline.c,v 1.113 2014/10/18 08:33:23 snj Exp $ */ |
2 3/*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jaromir Dolecek. 9 * --- 14 unchanged lines hidden (view full) --- 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 | 2 3/*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jaromir Dolecek. 9 * --- 14 unchanged lines hidden (view full) --- 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 |
32#include "config.h" 33#if !defined(lint) && !defined(SCCSID) 34__RCSID("$NetBSD: readline.c,v 1.113 2014/10/18 08:33:23 snj Exp $"); 35#endif /* not lint && not SCCSID */ |
|
32#include <sys/cdefs.h> | 36#include <sys/cdefs.h> |
33__RCSID("$NetBSD: readline.c,v 1.90 2010/08/04 20:29:18 christos Exp $"); 34__FBSDID("$FreeBSD: head/lib/libedit/readline.c 255891 2013-09-26 17:54:58Z delphij $"); | 37__FBSDID("$FreeBSD: head/lib/libedit/readline.c 276881 2015-01-09 07:40:56Z bapt $"); |
35 36#include <sys/types.h> 37#include <sys/stat.h> 38#include <stdio.h> 39#include <dirent.h> 40#include <string.h> 41#include <pwd.h> 42#include <ctype.h> 43#include <stdlib.h> 44#include <unistd.h> 45#include <limits.h> 46#include <errno.h> 47#include <fcntl.h> 48#include <setjmp.h> 49#include <vis.h> | 38 39#include <sys/types.h> 40#include <sys/stat.h> 41#include <stdio.h> 42#include <dirent.h> 43#include <string.h> 44#include <pwd.h> 45#include <ctype.h> 46#include <stdlib.h> 47#include <unistd.h> 48#include <limits.h> 49#include <errno.h> 50#include <fcntl.h> 51#include <setjmp.h> 52#include <vis.h> |
50#include "sys.h" | 53 |
51#include "readline/readline.h" | 54#include "readline/readline.h" |
52#include "chartype.h" | |
53#include "el.h" 54#include "fcns.h" /* for EL_NUM_FCNS */ 55#include "histedit.h" 56#include "filecomplete.h" 57 58void rl_prep_terminal(int); 59void rl_deprep_terminal(void); 60 --- 18 unchanged lines hidden (view full) --- 79int rl_end = 0; 80char *rl_line_buffer = NULL; 81VCPFunction *rl_linefunc = NULL; 82int rl_done = 0; 83VFunction *rl_event_hook = NULL; 84KEYMAP_ENTRY_ARRAY emacs_standard_keymap, 85 emacs_meta_keymap, 86 emacs_ctlx_keymap; | 55#include "el.h" 56#include "fcns.h" /* for EL_NUM_FCNS */ 57#include "histedit.h" 58#include "filecomplete.h" 59 60void rl_prep_terminal(int); 61void rl_deprep_terminal(void); 62 --- 18 unchanged lines hidden (view full) --- 81int rl_end = 0; 82char *rl_line_buffer = NULL; 83VCPFunction *rl_linefunc = NULL; 84int rl_done = 0; 85VFunction *rl_event_hook = NULL; 86KEYMAP_ENTRY_ARRAY emacs_standard_keymap, 87 emacs_meta_keymap, 88 emacs_ctlx_keymap; |
89/* 90 * The following is not implemented; we always catch signals in the 91 * libedit fashion: set handlers on entry to el_gets() and clear them 92 * on the way out. This simplistic approach works for most cases; if 93 * it does not work for your application, please let us know. 94 */ 95int rl_catch_signals = 1; 96int rl_catch_sigwinch = 1; |
|
87 88int history_base = 1; /* probably never subject to change */ 89int history_length = 0; 90int max_input_history = 0; 91char history_expansion_char = '!'; 92char history_subst_char = '^'; 93char *history_no_expand_chars = expand_chars; 94Function *history_inhibit_expansion_function = NULL; 95char *history_arg_extract(int start, int end, const char *str); 96 97int rl_inhibit_completion = 0; 98int rl_attempted_completion_over = 0; 99char *rl_basic_word_break_characters = break_chars; 100char *rl_completer_word_break_characters = NULL; 101char *rl_completer_quote_characters = NULL; 102Function *rl_completion_entry_function = NULL; | 97 98int history_base = 1; /* probably never subject to change */ 99int history_length = 0; 100int max_input_history = 0; 101char history_expansion_char = '!'; 102char history_subst_char = '^'; 103char *history_no_expand_chars = expand_chars; 104Function *history_inhibit_expansion_function = NULL; 105char *history_arg_extract(int start, int end, const char *str); 106 107int rl_inhibit_completion = 0; 108int rl_attempted_completion_over = 0; 109char *rl_basic_word_break_characters = break_chars; 110char *rl_completer_word_break_characters = NULL; 111char *rl_completer_quote_characters = NULL; 112Function *rl_completion_entry_function = NULL; |
113char *(*rl_completion_word_break_hook)(void) = NULL; |
|
103CPPFunction *rl_attempted_completion_function = NULL; 104Function *rl_pre_input_hook = NULL; 105Function *rl_startup1_hook = NULL; 106int (*rl_getc_function)(FILE *) = NULL; 107char *rl_terminal_name = NULL; 108int rl_already_prompted = 0; 109int rl_filename_completion_desired = 0; 110int rl_ignore_completion_duplicates = 0; | 114CPPFunction *rl_attempted_completion_function = NULL; 115Function *rl_pre_input_hook = NULL; 116Function *rl_startup1_hook = NULL; 117int (*rl_getc_function)(FILE *) = NULL; 118char *rl_terminal_name = NULL; 119int rl_already_prompted = 0; 120int rl_filename_completion_desired = 0; 121int rl_ignore_completion_duplicates = 0; |
111int rl_catch_signals = 1; | |
112int readline_echoing_p = 1; 113int _rl_print_completions_horizontally = 0; 114VFunction *rl_redisplay_function = NULL; 115Function *rl_startup_hook = NULL; 116VFunction *rl_completion_display_matches_hook = NULL; 117VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal; 118VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal; 119KEYMAP_ENTRY_ARRAY emacs_meta_keymap; 120 | 122int readline_echoing_p = 1; 123int _rl_print_completions_horizontally = 0; 124VFunction *rl_redisplay_function = NULL; 125Function *rl_startup_hook = NULL; 126VFunction *rl_completion_display_matches_hook = NULL; 127VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal; 128VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal; 129KEYMAP_ENTRY_ARRAY emacs_meta_keymap; 130 |
121#ifdef WIDECHAR 122static ct_buffer_t conv; 123#endif 124 | |
125/* 126 * The current prompt string. 127 */ 128char *rl_prompt = NULL; 129/* 130 * This is set to character indicating type of completion being done by 131 * rl_complete_internal(); this is available for application completion 132 * functions. --- 16 unchanged lines hidden (view full) --- 149/* 150 * This is the character appended to the completed words if at the end of 151 * the line. Default is ' ' (a space). 152 */ 153int rl_completion_append_character = ' '; 154 155/* stuff below is used internally by libedit for readline emulation */ 156 | 131/* 132 * The current prompt string. 133 */ 134char *rl_prompt = NULL; 135/* 136 * This is set to character indicating type of completion being done by 137 * rl_complete_internal(); this is available for application completion 138 * functions. --- 16 unchanged lines hidden (view full) --- 155/* 156 * This is the character appended to the completed words if at the end of 157 * the line. Default is ' ' (a space). 158 */ 159int rl_completion_append_character = ' '; 160 161/* stuff below is used internally by libedit for readline emulation */ 162 |
157static TYPE(History) *h = NULL; | 163static History *h = NULL; |
158static EditLine *e = NULL; 159static Function *map[256]; 160static jmp_buf topbuf; 161 162/* internal functions */ 163static unsigned char _el_rl_complete(EditLine *, int); 164static unsigned char _el_rl_tstp(EditLine *, int); 165static char *_get_prompt(EditLine *); --- 7 unchanged lines hidden (view full) --- 173static void _rl_update_pos(void); 174 175 176/* ARGSUSED */ 177static char * 178_get_prompt(EditLine *el __attribute__((__unused__))) 179{ 180 rl_already_prompted = 1; | 164static EditLine *e = NULL; 165static Function *map[256]; 166static jmp_buf topbuf; 167 168/* internal functions */ 169static unsigned char _el_rl_complete(EditLine *, int); 170static unsigned char _el_rl_tstp(EditLine *, int); 171static char *_get_prompt(EditLine *); --- 7 unchanged lines hidden (view full) --- 179static void _rl_update_pos(void); 180 181 182/* ARGSUSED */ 183static char * 184_get_prompt(EditLine *el __attribute__((__unused__))) 185{ 186 rl_already_prompted = 1; |
181 return (rl_prompt); | 187 return rl_prompt; |
182} 183 184 185/* 186 * generic function for moving around history 187 */ 188static HIST_ENTRY * 189_move_history(int op) 190{ | 188} 189 190 191/* 192 * generic function for moving around history 193 */ 194static HIST_ENTRY * 195_move_history(int op) 196{ |
191 TYPE(HistEvent) ev; | 197 HistEvent ev; |
192 static HIST_ENTRY rl_he; 193 | 198 static HIST_ENTRY rl_he; 199 |
194 if (FUNW(history)(h, &ev, op) != 0) 195 return (HIST_ENTRY *) NULL; | 200 if (history(h, &ev, op) != 0) 201 return NULL; |
196 | 202 |
197 rl_he.line = ct_encode_string(ev.str, &conv); | 203 rl_he.line = ev.str; |
198 rl_he.data = NULL; 199 | 204 rl_he.data = NULL; 205 |
200 return (&rl_he); | 206 return &rl_he; |
201} 202 203 204/* 205 * read one key from user defined input function 206 */ 207static int 208/*ARGSUSED*/ | 207} 208 209 210/* 211 * read one key from user defined input function 212 */ 213static int 214/*ARGSUSED*/ |
209_getc_function(EditLine *el, char *c) | 215_getc_function(EditLine *el __attribute__((__unused__)), char *c) |
210{ 211 int i; 212 213 i = (*rl_getc_function)(NULL); 214 if (i == -1) 215 return 0; | 216{ 217 int i; 218 219 i = (*rl_getc_function)(NULL); 220 if (i == -1) 221 return 0; |
216 *c = i; | 222 *c = (char)i; |
217 return 1; 218} 219 | 223 return 1; 224} 225 |
220static const char _dothistory[] = "/.history"; | 226static void 227_resize_fun(EditLine *el, void *a) 228{ 229 const LineInfo *li; 230 char **ap = a; |
221 | 231 |
232 li = el_line(el); 233 /* a cheesy way to get rid of const cast. */ 234 *ap = memchr(li->buffer, *li->buffer, (size_t)1); 235} 236 |
|
222static const char * 223_default_history_file(void) 224{ 225 struct passwd *p; | 237static const char * 238_default_history_file(void) 239{ 240 struct passwd *p; |
226 static char path[PATH_MAX]; | 241 static char *path; 242 size_t len; |
227 | 243 |
228 if (*path) | 244 if (path) |
229 return path; | 245 return path; |
246 |
|
230 if ((p = getpwuid(getuid())) == NULL) 231 return NULL; | 247 if ((p = getpwuid(getuid())) == NULL) 248 return NULL; |
232 strlcpy(path, p->pw_dir, PATH_MAX); 233 strlcat(path, _dothistory, PATH_MAX); | 249 250 len = strlen(p->pw_dir) + sizeof("/.history"); 251 if ((path = malloc(len)) == NULL) 252 return NULL; 253 254 (void)snprintf(path, len, "%s/.history", p->pw_dir); |
234 return path; 235} 236 237/* 238 * READLINE compatibility stuff 239 */ 240 241/* --- 4 unchanged lines hidden (view full) --- 246{ 247 char *p; 248 249 if (!prompt) 250 prompt = ""; 251 if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0) 252 return 0; 253 if (rl_prompt) | 255 return path; 256} 257 258/* 259 * READLINE compatibility stuff 260 */ 261 262/* --- 4 unchanged lines hidden (view full) --- 267{ 268 char *p; 269 270 if (!prompt) 271 prompt = ""; 272 if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0) 273 return 0; 274 if (rl_prompt) |
254 free(rl_prompt); | 275 el_free(rl_prompt); |
255 rl_prompt = strdup(prompt); 256 if (rl_prompt == NULL) 257 return -1; 258 259 while ((p = strchr(rl_prompt, RL_PROMPT_END_IGNORE)) != NULL) 260 *p = RL_PROMPT_START_IGNORE; 261 262 return 0; 263} 264 265/* 266 * initialize rl compat stuff 267 */ 268int 269rl_initialize(void) 270{ | 276 rl_prompt = strdup(prompt); 277 if (rl_prompt == NULL) 278 return -1; 279 280 while ((p = strchr(rl_prompt, RL_PROMPT_END_IGNORE)) != NULL) 281 *p = RL_PROMPT_START_IGNORE; 282 283 return 0; 284} 285 286/* 287 * initialize rl compat stuff 288 */ 289int 290rl_initialize(void) 291{ |
271 TYPE(HistEvent) ev; 272 const LineInfo *li; | 292 HistEvent ev; |
273 int editmode = 1; 274 struct termios t; 275 276 if (e != NULL) 277 el_end(e); 278 if (h != NULL) | 293 int editmode = 1; 294 struct termios t; 295 296 if (e != NULL) 297 el_end(e); 298 if (h != NULL) |
279 FUN(history,end)(h); | 299 history_end(h); |
280 281 if (!rl_instream) 282 rl_instream = stdin; 283 if (!rl_outstream) 284 rl_outstream = stdout; 285 286 /* 287 * See if we don't really want to run the editor 288 */ 289 if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0) 290 editmode = 0; 291 292 e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr); 293 294 if (!editmode) | 300 301 if (!rl_instream) 302 rl_instream = stdin; 303 if (!rl_outstream) 304 rl_outstream = stdout; 305 306 /* 307 * See if we don't really want to run the editor 308 */ 309 if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0) 310 editmode = 0; 311 312 e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr); 313 314 if (!editmode) |
295 FUN(el,set)(e, EL_EDITMODE, 0); | 315 el_set(e, EL_EDITMODE, 0); |
296 | 316 |
297 h = FUN(history,init)(); | 317 h = history_init(); |
298 if (!e || !h) | 318 if (!e || !h) |
299 return (-1); | 319 return -1; |
300 | 320 |
301 FUNW(history)(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */ | 321 history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */ |
302 history_length = 0; 303 max_input_history = INT_MAX; 304 el_set(e, EL_HIST, history, h); 305 | 322 history_length = 0; 323 max_input_history = INT_MAX; 324 el_set(e, EL_HIST, history, h); 325 |
326 /* Setup resize function */ 327 el_set(e, EL_RESIZE, _resize_fun, &rl_line_buffer); 328 |
|
306 /* setup getc function if valid */ 307 if (rl_getc_function) 308 el_set(e, EL_GETCFN, _getc_function); 309 310 /* for proper prompt printing in readline() */ 311 if (rl_set_prompt("") == -1) { | 329 /* setup getc function if valid */ 330 if (rl_getc_function) 331 el_set(e, EL_GETCFN, _getc_function); 332 333 /* for proper prompt printing in readline() */ 334 if (rl_set_prompt("") == -1) { |
312 FUN(history,end)(h); | 335 history_end(h); |
313 el_end(e); 314 return -1; 315 } 316 el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE); 317 el_set(e, EL_SIGNAL, rl_catch_signals); 318 319 /* set default mode to "emacs"-style and read setting afterwards */ | 336 el_end(e); 337 return -1; 338 } 339 el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE); 340 el_set(e, EL_SIGNAL, rl_catch_signals); 341 342 /* set default mode to "emacs"-style and read setting afterwards */ |
320 /* so this can be overriden */ | 343 /* so this can be overridden */ |
321 el_set(e, EL_EDITOR, "emacs"); 322 if (rl_terminal_name != NULL) 323 el_set(e, EL_TERMINAL, rl_terminal_name); 324 else 325 el_get(e, EL_TERMINAL, &rl_terminal_name); 326 327 /* 328 * Word completion - this has to go AFTER rebinding keys --- 14 unchanged lines hidden (view full) --- 343 344 /* read settings from configuration file */ 345 el_source(e, NULL); 346 347 /* 348 * Unfortunately, some applications really do use rl_point 349 * and rl_line_buffer directly. 350 */ | 344 el_set(e, EL_EDITOR, "emacs"); 345 if (rl_terminal_name != NULL) 346 el_set(e, EL_TERMINAL, rl_terminal_name); 347 else 348 el_get(e, EL_TERMINAL, &rl_terminal_name); 349 350 /* 351 * Word completion - this has to go AFTER rebinding keys --- 14 unchanged lines hidden (view full) --- 366 367 /* read settings from configuration file */ 368 el_source(e, NULL); 369 370 /* 371 * Unfortunately, some applications really do use rl_point 372 * and rl_line_buffer directly. 373 */ |
351 li = el_line(e); 352 /* a cheesy way to get rid of const cast. */ 353 rl_line_buffer = memchr(li->buffer, *li->buffer, 1); | 374 _resize_fun(e, &rl_line_buffer); |
354 _rl_update_pos(); 355 356 if (rl_startup_hook) 357 (*rl_startup_hook)(NULL, 0); 358 | 375 _rl_update_pos(); 376 377 if (rl_startup_hook) 378 (*rl_startup_hook)(NULL, 0); 379 |
359 return (0); | 380 return 0; |
360} 361 362 363/* 364 * read one line from input stream and return it, chomping 365 * trailing newline (if there is any) 366 */ 367char * 368readline(const char *p) 369{ | 381} 382 383 384/* 385 * read one line from input stream and return it, chomping 386 * trailing newline (if there is any) 387 */ 388char * 389readline(const char *p) 390{ |
370 TYPE(HistEvent) ev; | 391 HistEvent ev; |
371 const char * volatile prompt = p; 372 int count; 373 const char *ret; 374 char *buf; 375 static int used_event_hook; 376 377 if (e == NULL || h == NULL) 378 rl_initialize(); --- 31 unchanged lines hidden (view full) --- 410 if (buf == NULL) 411 return NULL; 412 lastidx = count - 1; 413 if (buf[lastidx] == '\n') 414 buf[lastidx] = '\0'; 415 } else 416 buf = NULL; 417 | 392 const char * volatile prompt = p; 393 int count; 394 const char *ret; 395 char *buf; 396 static int used_event_hook; 397 398 if (e == NULL || h == NULL) 399 rl_initialize(); --- 31 unchanged lines hidden (view full) --- 431 if (buf == NULL) 432 return NULL; 433 lastidx = count - 1; 434 if (buf[lastidx] == '\n') 435 buf[lastidx] = '\0'; 436 } else 437 buf = NULL; 438 |
418 FUNW(history)(h, &ev, H_GETSIZE); | 439 history(h, &ev, H_GETSIZE); |
419 history_length = ev.num; 420 421 return buf; 422} 423 424/* 425 * history functions 426 */ --- 33 unchanged lines hidden (view full) --- 460 if (*s == *what && !strncmp(s, what, what_len)) { 461 len += with_len - what_len; 462 if (!globally) 463 break; 464 s += what_len; 465 } else 466 s++; 467 } | 440 history_length = ev.num; 441 442 return buf; 443} 444 445/* 446 * history functions 447 */ --- 33 unchanged lines hidden (view full) --- 481 if (*s == *what && !strncmp(s, what, what_len)) { 482 len += with_len - what_len; 483 if (!globally) 484 break; 485 s += what_len; 486 } else 487 s++; 488 } |
468 r = result = malloc(len + 1); | 489 r = result = el_malloc((len + 1) * sizeof(*r)); |
469 if (result == NULL) 470 return NULL; 471 s = str; 472 while (*s) { 473 if (*s == *what && !strncmp(s, what, what_len)) { 474 (void)strncpy(r, with, with_len); 475 r += with_len; 476 s += what_len; 477 if (!globally) { 478 (void)strcpy(r, s); | 490 if (result == NULL) 491 return NULL; 492 s = str; 493 while (*s) { 494 if (*s == *what && !strncmp(s, what, what_len)) { 495 (void)strncpy(r, with, with_len); 496 r += with_len; 497 s += what_len; 498 if (!globally) { 499 (void)strcpy(r, s); |
479 return(result); | 500 return result; |
480 } 481 } else 482 *r++ = *s++; 483 } 484 *r = '\0'; | 501 } 502 } else 503 *r++ = *s++; 504 } 505 *r = '\0'; |
485 return(result); | 506 return result; |
486} 487 488static char *last_search_pat; /* last !?pat[?] search pattern */ 489static char *last_search_match; /* last !?pat[?] that matched */ 490 491const char * 492get_history_event(const char *cmd, int *cindex, int qchar) 493{ 494 int idx, sign, sub, num, begin, ret; 495 size_t len; 496 char *pat; 497 const char *rptr; | 507} 508 509static char *last_search_pat; /* last !?pat[?] search pattern */ 510static char *last_search_match; /* last !?pat[?] that matched */ 511 512const char * 513get_history_event(const char *cmd, int *cindex, int qchar) 514{ 515 int idx, sign, sub, num, begin, ret; 516 size_t len; 517 char *pat; 518 const char *rptr; |
498 TYPE(HistEvent) ev; | 519 HistEvent ev; |
499 500 idx = *cindex; 501 if (cmd[idx++] != history_expansion_char) | 520 521 idx = *cindex; 522 if (cmd[idx++] != history_expansion_char) |
502 return(NULL); | 523 return NULL; |
503 504 /* find out which event to take */ 505 if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') { | 524 525 /* find out which event to take */ 526 if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') { |
506 if (FUNW(history)(h, &ev, H_FIRST) != 0) 507 return(NULL); | 527 if (history(h, &ev, H_FIRST) != 0) 528 return NULL; |
508 *cindex = cmd[idx]? (idx + 1):idx; | 529 *cindex = cmd[idx]? (idx + 1):idx; |
509 return ct_encode_string(ev.str, &conv); | 530 return ev.str; |
510 } 511 sign = 0; 512 if (cmd[idx] == '-') { 513 sign = 1; 514 idx++; 515 } 516 517 if ('0' <= cmd[idx] && cmd[idx] <= '9') { 518 HIST_ENTRY *rl_he; 519 520 num = 0; 521 while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') { 522 num = num * 10 + cmd[idx] - '0'; 523 idx++; 524 } 525 if (sign) 526 num = history_length - num + 1; 527 528 if (!(rl_he = history_get(num))) | 531 } 532 sign = 0; 533 if (cmd[idx] == '-') { 534 sign = 1; 535 idx++; 536 } 537 538 if ('0' <= cmd[idx] && cmd[idx] <= '9') { 539 HIST_ENTRY *rl_he; 540 541 num = 0; 542 while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') { 543 num = num * 10 + cmd[idx] - '0'; 544 idx++; 545 } 546 if (sign) 547 num = history_length - num + 1; 548 549 if (!(rl_he = history_get(num))) |
529 return(NULL); | 550 return NULL; |
530 531 *cindex = idx; | 551 552 *cindex = idx; |
532 return(rl_he->line); | 553 return rl_he->line; |
533 } 534 sub = 0; 535 if (cmd[idx] == '?') { 536 sub = 1; 537 idx++; 538 } 539 begin = idx; 540 while (cmd[idx]) { 541 if (cmd[idx] == '\n') 542 break; 543 if (sub && cmd[idx] == '?') 544 break; 545 if (!sub && (cmd[idx] == ':' || cmd[idx] == ' ' 546 || cmd[idx] == '\t' || cmd[idx] == qchar)) 547 break; 548 idx++; 549 } | 554 } 555 sub = 0; 556 if (cmd[idx] == '?') { 557 sub = 1; 558 idx++; 559 } 560 begin = idx; 561 while (cmd[idx]) { 562 if (cmd[idx] == '\n') 563 break; 564 if (sub && cmd[idx] == '?') 565 break; 566 if (!sub && (cmd[idx] == ':' || cmd[idx] == ' ' 567 || cmd[idx] == '\t' || cmd[idx] == qchar)) 568 break; 569 idx++; 570 } |
550 len = idx - begin; | 571 len = (size_t)idx - (size_t)begin; |
551 if (sub && cmd[idx] == '?') 552 idx++; 553 if (sub && len == 0 && last_search_pat && *last_search_pat) 554 pat = last_search_pat; 555 else if (len == 0) | 572 if (sub && cmd[idx] == '?') 573 idx++; 574 if (sub && len == 0 && last_search_pat && *last_search_pat) 575 pat = last_search_pat; 576 else if (len == 0) |
556 return(NULL); | 577 return NULL; |
557 else { | 578 else { |
558 if ((pat = malloc(len + 1)) == NULL) | 579 if ((pat = el_malloc((len + 1) * sizeof(*pat))) == NULL) |
559 return NULL; 560 (void)strncpy(pat, cmd + begin, len); 561 pat[len] = '\0'; 562 } 563 | 580 return NULL; 581 (void)strncpy(pat, cmd + begin, len); 582 pat[len] = '\0'; 583 } 584 |
564 if (FUNW(history)(h, &ev, H_CURR) != 0) { | 585 if (history(h, &ev, H_CURR) != 0) { |
565 if (pat != last_search_pat) | 586 if (pat != last_search_pat) |
566 free(pat); 567 return (NULL); | 587 el_free(pat); 588 return NULL; |
568 } 569 num = ev.num; 570 571 if (sub) { 572 if (pat != last_search_pat) { 573 if (last_search_pat) | 589 } 590 num = ev.num; 591 592 if (sub) { 593 if (pat != last_search_pat) { 594 if (last_search_pat) |
574 free(last_search_pat); | 595 el_free(last_search_pat); |
575 last_search_pat = pat; 576 } 577 ret = history_search(pat, -1); 578 } else 579 ret = history_search_prefix(pat, -1); 580 581 if (ret == -1) { 582 /* restore to end of list on failed search */ | 596 last_search_pat = pat; 597 } 598 ret = history_search(pat, -1); 599 } else 600 ret = history_search_prefix(pat, -1); 601 602 if (ret == -1) { 603 /* restore to end of list on failed search */ |
583 FUNW(history)(h, &ev, H_FIRST); | 604 history(h, &ev, H_FIRST); |
584 (void)fprintf(rl_outstream, "%s: Event not found\n", pat); 585 if (pat != last_search_pat) | 605 (void)fprintf(rl_outstream, "%s: Event not found\n", pat); 606 if (pat != last_search_pat) |
586 free(pat); 587 return(NULL); | 607 el_free(pat); 608 return NULL; |
588 } 589 590 if (sub && len) { 591 if (last_search_match && last_search_match != pat) | 609 } 610 611 if (sub && len) { 612 if (last_search_match && last_search_match != pat) |
592 free(last_search_match); | 613 el_free(last_search_match); |
593 last_search_match = pat; 594 } 595 596 if (pat != last_search_pat) | 614 last_search_match = pat; 615 } 616 617 if (pat != last_search_pat) |
597 free(pat); | 618 el_free(pat); |
598 | 619 |
599 if (FUNW(history)(h, &ev, H_CURR) != 0) 600 return(NULL); | 620 if (history(h, &ev, H_CURR) != 0) 621 return NULL; |
601 *cindex = idx; | 622 *cindex = idx; |
602 rptr = ct_encode_string(ev.str, &conv); | 623 rptr = ev.str; |
603 604 /* roll back to original position */ | 624 625 /* roll back to original position */ |
605 (void)FUNW(history)(h, &ev, H_SET, num); | 626 (void)history(h, &ev, H_SET, num); |
606 607 return rptr; 608} 609 610/* 611 * the real function doing history expansion - takes as argument command 612 * to do and data upon which the command should be executed 613 * does expansion the way I've understood readline documentation 614 * 615 * returns 0 if data was not modified, 1 if it was and 2 if the string 616 * should be only printed and not executed; in case of error, 617 * returns -1 and *result points to NULL | 627 628 return rptr; 629} 630 631/* 632 * the real function doing history expansion - takes as argument command 633 * to do and data upon which the command should be executed 634 * does expansion the way I've understood readline documentation 635 * 636 * returns 0 if data was not modified, 1 if it was and 2 if the string 637 * should be only printed and not executed; in case of error, 638 * returns -1 and *result points to NULL |
618 * it's callers responsibility to free() string returned in *result | 639 * it's the caller's responsibility to free() the string returned in *result |
619 */ 620static int 621_history_expand_command(const char *command, size_t offs, size_t cmdlen, 622 char **result) 623{ 624 char *tmp, *search = NULL, *aptr; 625 const char *ptr, *cmd; 626 static char *from = NULL, *to = NULL; --- 17 unchanged lines hidden (view full) --- 644 str[0] = str[1] = '!'; 645 str[2] = '0'; 646 ptr = get_history_event(str, &idx, 0); 647 idx = (command[offs + 1] == ':')? 1:0; 648 has_mods = 1; 649 } else { 650 if (command[offs + 1] == '#') { 651 /* use command so far */ | 640 */ 641static int 642_history_expand_command(const char *command, size_t offs, size_t cmdlen, 643 char **result) 644{ 645 char *tmp, *search = NULL, *aptr; 646 const char *ptr, *cmd; 647 static char *from = NULL, *to = NULL; --- 17 unchanged lines hidden (view full) --- 665 str[0] = str[1] = '!'; 666 str[2] = '0'; 667 ptr = get_history_event(str, &idx, 0); 668 idx = (command[offs + 1] == ':')? 1:0; 669 has_mods = 1; 670 } else { 671 if (command[offs + 1] == '#') { 672 /* use command so far */ |
652 if ((aptr = malloc(offs + 1)) == NULL) | 673 if ((aptr = el_malloc((offs + 1) * sizeof(*aptr))) 674 == NULL) |
653 return -1; 654 (void)strncpy(aptr, command, offs); 655 aptr[offs] = '\0'; 656 idx = 1; 657 } else { 658 int qchar; 659 660 qchar = (offs > 0 && command[offs - 1] == '"')? '"':0; 661 ptr = get_history_event(command + offs, &idx, qchar); 662 } | 675 return -1; 676 (void)strncpy(aptr, command, offs); 677 aptr[offs] = '\0'; 678 idx = 1; 679 } else { 680 int qchar; 681 682 qchar = (offs > 0 && command[offs - 1] == '"')? '"':0; 683 ptr = get_history_event(command + offs, &idx, qchar); 684 } |
663 has_mods = command[offs + idx] == ':'; | 685 has_mods = command[offs + (size_t)idx] == ':'; |
664 } 665 666 if (ptr == NULL && aptr == NULL) | 686 } 687 688 if (ptr == NULL && aptr == NULL) |
667 return(-1); | 689 return -1; |
668 669 if (!has_mods) { 670 *result = strdup(aptr ? aptr : ptr); 671 if (aptr) | 690 691 if (!has_mods) { 692 *result = strdup(aptr ? aptr : ptr); 693 if (aptr) |
672 free(aptr); | 694 el_free(aptr); |
673 if (*result == NULL) 674 return -1; | 695 if (*result == NULL) 696 return -1; |
675 return(1); | 697 return 1; |
676 } 677 678 cmd = command + offs + idx + 1; 679 680 /* Now parse any word designators */ 681 682 if (*cmd == '%') /* last word matched by ?pat? */ 683 tmp = strdup(last_search_match? last_search_match:""); --- 28 unchanged lines hidden (view full) --- 712 else 713 end = start; 714 } 715 tmp = history_arg_extract(start, end, aptr? aptr:ptr); 716 if (tmp == NULL) { 717 (void)fprintf(rl_outstream, "%s: Bad word specifier", 718 command + offs + idx); 719 if (aptr) | 698 } 699 700 cmd = command + offs + idx + 1; 701 702 /* Now parse any word designators */ 703 704 if (*cmd == '%') /* last word matched by ?pat? */ 705 tmp = strdup(last_search_match? last_search_match:""); --- 28 unchanged lines hidden (view full) --- 734 else 735 end = start; 736 } 737 tmp = history_arg_extract(start, end, aptr? aptr:ptr); 738 if (tmp == NULL) { 739 (void)fprintf(rl_outstream, "%s: Bad word specifier", 740 command + offs + idx); 741 if (aptr) |
720 free(aptr); 721 return(-1); | 742 el_free(aptr); 743 return -1; |
722 } 723 } else 724 tmp = strdup(aptr? aptr:ptr); 725 726 if (aptr) | 744 } 745 } else 746 tmp = strdup(aptr? aptr:ptr); 747 748 if (aptr) |
727 free(aptr); | 749 el_free(aptr); |
728 729 if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) { 730 *result = tmp; | 750 751 if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) { 752 *result = tmp; |
731 return(1); | 753 return 1; |
732 } 733 734 for (; *cmd; cmd++) { 735 if (*cmd == ':') 736 continue; 737 else if (*cmd == 'h') { /* remove trailing path */ 738 if ((aptr = strrchr(tmp, '/')) != NULL) 739 *aptr = '\0'; 740 } else if (*cmd == 't') { /* remove leading path */ 741 if ((aptr = strrchr(tmp, '/')) != NULL) { 742 aptr = strdup(aptr + 1); | 754 } 755 756 for (; *cmd; cmd++) { 757 if (*cmd == ':') 758 continue; 759 else if (*cmd == 'h') { /* remove trailing path */ 760 if ((aptr = strrchr(tmp, '/')) != NULL) 761 *aptr = '\0'; 762 } else if (*cmd == 't') { /* remove leading path */ 763 if ((aptr = strrchr(tmp, '/')) != NULL) { 764 aptr = strdup(aptr + 1); |
743 free(tmp); | 765 el_free(tmp); |
744 tmp = aptr; 745 } 746 } else if (*cmd == 'r') { /* remove trailing suffix */ 747 if ((aptr = strrchr(tmp, '.')) != NULL) 748 *aptr = '\0'; 749 } else if (*cmd == 'e') { /* remove all but suffix */ 750 if ((aptr = strrchr(tmp, '.')) != NULL) { 751 aptr = strdup(aptr); | 766 tmp = aptr; 767 } 768 } else if (*cmd == 'r') { /* remove trailing suffix */ 769 if ((aptr = strrchr(tmp, '.')) != NULL) 770 *aptr = '\0'; 771 } else if (*cmd == 'e') { /* remove all but suffix */ 772 if ((aptr = strrchr(tmp, '.')) != NULL) { 773 aptr = strdup(aptr); |
752 free(tmp); | 774 el_free(tmp); |
753 tmp = aptr; 754 } 755 } else if (*cmd == 'p') /* print only */ 756 p_on = 1; 757 else if (*cmd == 'g') 758 g_on = 2; 759 else if (*cmd == 's' || *cmd == '&') { 760 char *what, *with, delim; 761 size_t len, from_len; 762 size_t size; 763 764 if (*cmd == '&' && (from == NULL || to == NULL)) 765 continue; 766 else if (*cmd == 's') { 767 delim = *(++cmd), cmd++; 768 size = 16; | 775 tmp = aptr; 776 } 777 } else if (*cmd == 'p') /* print only */ 778 p_on = 1; 779 else if (*cmd == 'g') 780 g_on = 2; 781 else if (*cmd == 's' || *cmd == '&') { 782 char *what, *with, delim; 783 size_t len, from_len; 784 size_t size; 785 786 if (*cmd == '&' && (from == NULL || to == NULL)) 787 continue; 788 else if (*cmd == 's') { 789 delim = *(++cmd), cmd++; 790 size = 16; |
769 what = realloc(from, size); | 791 what = el_realloc(from, size * sizeof(*what)); |
770 if (what == NULL) { | 792 if (what == NULL) { |
771 free(from); 772 free(tmp); | 793 el_free(from); 794 el_free(tmp); |
773 return 0; 774 } 775 len = 0; 776 for (; *cmd && *cmd != delim; cmd++) { 777 if (*cmd == '\\' && cmd[1] == delim) 778 cmd++; 779 if (len >= size) { 780 char *nwhat; | 795 return 0; 796 } 797 len = 0; 798 for (; *cmd && *cmd != delim; cmd++) { 799 if (*cmd == '\\' && cmd[1] == delim) 800 cmd++; 801 if (len >= size) { 802 char *nwhat; |
781 nwhat = realloc(what, 782 (size <<= 1)); | 803 nwhat = el_realloc(what, 804 (size <<= 1) * 805 sizeof(*nwhat)); |
783 if (nwhat == NULL) { | 806 if (nwhat == NULL) { |
784 free(what); 785 free(tmp); | 807 el_free(what); 808 el_free(tmp); |
786 return 0; 787 } 788 what = nwhat; 789 } 790 what[len++] = *cmd; 791 } 792 what[len] = '\0'; 793 from = what; 794 if (*what == '\0') { | 809 return 0; 810 } 811 what = nwhat; 812 } 813 what[len++] = *cmd; 814 } 815 what[len] = '\0'; 816 from = what; 817 if (*what == '\0') { |
795 free(what); | 818 el_free(what); |
796 if (search) { 797 from = strdup(search); 798 if (from == NULL) { | 819 if (search) { 820 from = strdup(search); 821 if (from == NULL) { |
799 free(tmp); | 822 el_free(tmp); |
800 return 0; 801 } 802 } else { 803 from = NULL; | 823 return 0; 824 } 825 } else { 826 from = NULL; |
804 free(tmp); 805 return (-1); | 827 el_free(tmp); 828 return -1; |
806 } 807 } 808 cmd++; /* shift after delim */ 809 if (!*cmd) 810 continue; 811 812 size = 16; | 829 } 830 } 831 cmd++; /* shift after delim */ 832 if (!*cmd) 833 continue; 834 835 size = 16; |
813 with = realloc(to, size); | 836 with = el_realloc(to, size * sizeof(*with)); |
814 if (with == NULL) { | 837 if (with == NULL) { |
815 free(to); 816 free(tmp); | 838 el_free(to); 839 el_free(tmp); |
817 return -1; 818 } 819 len = 0; 820 from_len = strlen(from); 821 for (; *cmd && *cmd != delim; cmd++) { 822 if (len + from_len + 1 >= size) { 823 char *nwith; 824 size += from_len + 1; | 840 return -1; 841 } 842 len = 0; 843 from_len = strlen(from); 844 for (; *cmd && *cmd != delim; cmd++) { 845 if (len + from_len + 1 >= size) { 846 char *nwith; 847 size += from_len + 1; |
825 nwith = realloc(with, size); | 848 nwith = el_realloc(with, 849 size * sizeof(*nwith)); |
826 if (nwith == NULL) { | 850 if (nwith == NULL) { |
827 free(with); 828 free(tmp); | 851 el_free(with); 852 el_free(tmp); |
829 return -1; 830 } 831 with = nwith; 832 } 833 if (*cmd == '&') { 834 /* safe */ 835 (void)strcpy(&with[len], from); 836 len += from_len; --- 6 unchanged lines hidden (view full) --- 843 with[len++] = *cmd; 844 } 845 with[len] = '\0'; 846 to = with; 847 } 848 849 aptr = _rl_compat_sub(tmp, from, to, g_on); 850 if (aptr) { | 853 return -1; 854 } 855 with = nwith; 856 } 857 if (*cmd == '&') { 858 /* safe */ 859 (void)strcpy(&with[len], from); 860 len += from_len; --- 6 unchanged lines hidden (view full) --- 867 with[len++] = *cmd; 868 } 869 with[len] = '\0'; 870 to = with; 871 } 872 873 aptr = _rl_compat_sub(tmp, from, to, g_on); 874 if (aptr) { |
851 free(tmp); | 875 el_free(tmp); |
852 tmp = aptr; 853 } 854 g_on = 0; 855 } 856 } 857 *result = tmp; | 876 tmp = aptr; 877 } 878 g_on = 0; 879 } 880 } 881 *result = tmp; |
858 return (p_on? 2:1); | 882 return p_on? 2:1; |
859} 860 861 862/* 863 * csh-style history expansion 864 */ 865int 866history_expand(char *str, char **output) 867{ 868 int ret = 0; 869 size_t idx, i, size; 870 char *tmp, *result; 871 872 if (h == NULL || e == NULL) 873 rl_initialize(); 874 875 if (history_expansion_char == 0) { 876 *output = strdup(str); | 883} 884 885 886/* 887 * csh-style history expansion 888 */ 889int 890history_expand(char *str, char **output) 891{ 892 int ret = 0; 893 size_t idx, i, size; 894 char *tmp, *result; 895 896 if (h == NULL || e == NULL) 897 rl_initialize(); 898 899 if (history_expansion_char == 0) { 900 *output = strdup(str); |
877 return(0); | 901 return 0; |
878 } 879 880 *output = NULL; 881 if (str[0] == history_subst_char) { 882 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */ | 902 } 903 904 *output = NULL; 905 if (str[0] == history_subst_char) { 906 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */ |
883 *output = malloc(strlen(str) + 4 + 1); | 907 *output = el_malloc((strlen(str) + 4 + 1) * sizeof(**output)); |
884 if (*output == NULL) 885 return 0; 886 (*output)[0] = (*output)[1] = history_expansion_char; 887 (*output)[2] = ':'; 888 (*output)[3] = 's'; 889 (void)strcpy((*output) + 4, str); 890 str = *output; 891 } else { 892 *output = strdup(str); 893 if (*output == NULL) 894 return 0; 895 } 896 897#define ADD_STRING(what, len, fr) \ 898 { \ 899 if (idx + len + 1 > size) { \ | 908 if (*output == NULL) 909 return 0; 910 (*output)[0] = (*output)[1] = history_expansion_char; 911 (*output)[2] = ':'; 912 (*output)[3] = 's'; 913 (void)strcpy((*output) + 4, str); 914 str = *output; 915 } else { 916 *output = strdup(str); 917 if (*output == NULL) 918 return 0; 919 } 920 921#define ADD_STRING(what, len, fr) \ 922 { \ 923 if (idx + len + 1 > size) { \ |
900 char *nresult = realloc(result, (size += len + 1));\ | 924 char *nresult = el_realloc(result, \ 925 (size += len + 1) * sizeof(*nresult)); \ |
901 if (nresult == NULL) { \ | 926 if (nresult == NULL) { \ |
902 free(*output); \ | 927 el_free(*output); \ |
903 if (/*CONSTCOND*/fr) \ | 928 if (/*CONSTCOND*/fr) \ |
904 free(tmp); \ | 929 el_free(tmp); \ |
905 return 0; \ 906 } \ 907 result = nresult; \ 908 } \ 909 (void)strncpy(&result[idx], what, len); \ 910 idx += len; \ 911 result[idx] = '\0'; \ 912 } --- 50 unchanged lines hidden (view full) --- 963 break; 964 } 965 ret = _history_expand_command (str, i, (j - i), &tmp); 966 if (ret > 0 && tmp) { 967 len = strlen(tmp); 968 ADD_STRING(tmp, len, 1); 969 } 970 if (tmp) { | 930 return 0; \ 931 } \ 932 result = nresult; \ 933 } \ 934 (void)strncpy(&result[idx], what, len); \ 935 idx += len; \ 936 result[idx] = '\0'; \ 937 } --- 50 unchanged lines hidden (view full) --- 988 break; 989 } 990 ret = _history_expand_command (str, i, (j - i), &tmp); 991 if (ret > 0 && tmp) { 992 len = strlen(tmp); 993 ADD_STRING(tmp, len, 1); 994 } 995 if (tmp) { |
971 free(tmp); | 996 el_free(tmp); |
972 tmp = NULL; 973 } 974 i = j; 975 } 976 977 /* ret is 2 for "print only" option */ 978 if (ret == 2) { 979 add_history(result); 980#ifdef GDB_411_HACK 981 /* gdb 4.11 has been shipped with readline, where */ 982 /* history_expand() returned -1 when the line */ 983 /* should not be executed; in readline 2.1+ */ 984 /* it should return 2 in such a case */ 985 ret = -1; 986#endif 987 } | 997 tmp = NULL; 998 } 999 i = j; 1000 } 1001 1002 /* ret is 2 for "print only" option */ 1003 if (ret == 2) { 1004 add_history(result); 1005#ifdef GDB_411_HACK 1006 /* gdb 4.11 has been shipped with readline, where */ 1007 /* history_expand() returned -1 when the line */ 1008 /* should not be executed; in readline 2.1+ */ 1009 /* it should return 2 in such a case */ 1010 ret = -1; 1011#endif 1012 } |
988 free(*output); | 1013 el_free(*output); |
989 *output = result; 990 | 1014 *output = result; 1015 |
991 return (ret); | 1016 return ret; |
992} 993 994/* 995* Return a string consisting of arguments of "str" from "start" to "end". 996*/ 997char * 998history_arg_extract(int start, int end, const char *str) 999{ --- 18 unchanged lines hidden (view full) --- 1018 end = (int)max + end + 1; 1019 if (start < 0) 1020 start = end; 1021 1022 if (start < 0 || end < 0 || (size_t)start > max || 1023 (size_t)end > max || start > end) 1024 goto out; 1025 | 1017} 1018 1019/* 1020* Return a string consisting of arguments of "str" from "start" to "end". 1021*/ 1022char * 1023history_arg_extract(int start, int end, const char *str) 1024{ --- 18 unchanged lines hidden (view full) --- 1043 end = (int)max + end + 1; 1044 if (start < 0) 1045 start = end; 1046 1047 if (start < 0 || end < 0 || (size_t)start > max || 1048 (size_t)end > max || start > end) 1049 goto out; 1050 |
1026 for (i = start, len = 0; i <= (size_t)end; i++) | 1051 for (i = (size_t)start, len = 0; i <= (size_t)end; i++) |
1027 len += strlen(arr[i]) + 1; 1028 len++; | 1052 len += strlen(arr[i]) + 1; 1053 len++; |
1029 result = malloc(len); | 1054 result = el_malloc(len * sizeof(*result)); |
1030 if (result == NULL) 1031 goto out; 1032 | 1055 if (result == NULL) 1056 goto out; 1057 |
1033 for (i = start, len = 0; i <= (size_t)end; i++) { | 1058 for (i = (size_t)start, len = 0; i <= (size_t)end; i++) { |
1034 (void)strcpy(result + len, arr[i]); 1035 len += strlen(arr[i]); 1036 if (i < (size_t)end) 1037 result[len++] = ' '; 1038 } 1039 result[len] = '\0'; 1040 1041out: 1042 for (i = 0; arr[i]; i++) | 1059 (void)strcpy(result + len, arr[i]); 1060 len += strlen(arr[i]); 1061 if (i < (size_t)end) 1062 result[len++] = ' '; 1063 } 1064 result[len] = '\0'; 1065 1066out: 1067 for (i = 0; arr[i]; i++) |
1043 free(arr[i]); 1044 free(arr); | 1068 el_free(arr[i]); 1069 el_free(arr); |
1045 1046 return result; 1047} 1048 1049/* 1050 * Parse the string into individual tokens, 1051 * similar to how shell would do it. 1052 */ --- 22 unchanged lines hidden (view full) --- 1075 delim = str[i]; 1076 if (str[i]) 1077 i++; 1078 } 1079 1080 if (idx + 2 >= size) { 1081 char **nresult; 1082 size <<= 1; | 1070 1071 return result; 1072} 1073 1074/* 1075 * Parse the string into individual tokens, 1076 * similar to how shell would do it. 1077 */ --- 22 unchanged lines hidden (view full) --- 1100 delim = str[i]; 1101 if (str[i]) 1102 i++; 1103 } 1104 1105 if (idx + 2 >= size) { 1106 char **nresult; 1107 size <<= 1; |
1083 nresult = realloc(result, size * sizeof(char *)); | 1108 nresult = el_realloc(result, (size_t)size * sizeof(*nresult)); |
1084 if (nresult == NULL) { | 1109 if (nresult == NULL) { |
1085 free(result); | 1110 el_free(result); |
1086 return NULL; 1087 } 1088 result = nresult; 1089 } | 1111 return NULL; 1112 } 1113 result = nresult; 1114 } |
1090 len = i - start; 1091 temp = malloc(len + 1); | 1115 len = (size_t)i - (size_t)start; 1116 temp = el_malloc((size_t)(len + 1) * sizeof(*temp)); |
1092 if (temp == NULL) { 1093 for (i = 0; i < idx; i++) | 1117 if (temp == NULL) { 1118 for (i = 0; i < idx; i++) |
1094 free(result[i]); 1095 free(result); | 1119 el_free(result[i]); 1120 el_free(result); |
1096 return NULL; 1097 } 1098 (void)strncpy(temp, &str[start], len); 1099 temp[len] = '\0'; 1100 result[idx++] = temp; 1101 result[idx] = NULL; 1102 if (str[i]) 1103 i++; 1104 } | 1121 return NULL; 1122 } 1123 (void)strncpy(temp, &str[start], len); 1124 temp[len] = '\0'; 1125 result[idx++] = temp; 1126 result[idx] = NULL; 1127 if (str[i]) 1128 i++; 1129 } |
1105 return (result); | 1130 return result; |
1106} 1107 1108 1109/* 1110 * limit size of history record to ``max'' events 1111 */ 1112void 1113stifle_history(int max) 1114{ | 1131} 1132 1133 1134/* 1135 * limit size of history record to ``max'' events 1136 */ 1137void 1138stifle_history(int max) 1139{ |
1115 TYPE(HistEvent) ev; | 1140 HistEvent ev; |
1116 1117 if (h == NULL || e == NULL) 1118 rl_initialize(); 1119 | 1141 1142 if (h == NULL || e == NULL) 1143 rl_initialize(); 1144 |
1120 if (FUNW(history)(h, &ev, H_SETSIZE, max) == 0) | 1145 if (history(h, &ev, H_SETSIZE, max) == 0) |
1121 max_input_history = max; 1122} 1123 1124 1125/* 1126 * "unlimit" size of history - set the limit to maximum allowed int value 1127 */ 1128int 1129unstifle_history(void) 1130{ | 1146 max_input_history = max; 1147} 1148 1149 1150/* 1151 * "unlimit" size of history - set the limit to maximum allowed int value 1152 */ 1153int 1154unstifle_history(void) 1155{ |
1131 TYPE(HistEvent) ev; | 1156 HistEvent ev; |
1132 int omax; 1133 | 1157 int omax; 1158 |
1134 FUNW(history)(h, &ev, H_SETSIZE, INT_MAX); | 1159 history(h, &ev, H_SETSIZE, INT_MAX); |
1135 omax = max_input_history; 1136 max_input_history = INT_MAX; | 1160 omax = max_input_history; 1161 max_input_history = INT_MAX; |
1137 return (omax); /* some value _must_ be returned */ | 1162 return omax; /* some value _must_ be returned */ |
1138} 1139 1140 1141int 1142history_is_stifled(void) 1143{ 1144 1145 /* cannot return true answer */ | 1163} 1164 1165 1166int 1167history_is_stifled(void) 1168{ 1169 1170 /* cannot return true answer */ |
1146 return (max_input_history != INT_MAX); | 1171 return max_input_history != INT_MAX; |
1147} 1148 1149static const char _history_tmp_template[] = "/tmp/.historyXXXXXX"; 1150 1151int 1152history_truncate_file (const char *filename, int nlines) 1153{ 1154 int ret = 0; --- 18 unchanged lines hidden (view full) --- 1173 1174 if ((tp = fdopen(fd, "r+")) == NULL) { 1175 close(fd); 1176 ret = errno; 1177 goto out2; 1178 } 1179 1180 for(;;) { | 1172} 1173 1174static const char _history_tmp_template[] = "/tmp/.historyXXXXXX"; 1175 1176int 1177history_truncate_file (const char *filename, int nlines) 1178{ 1179 int ret = 0; --- 18 unchanged lines hidden (view full) --- 1198 1199 if ((tp = fdopen(fd, "r+")) == NULL) { 1200 close(fd); 1201 ret = errno; 1202 goto out2; 1203 } 1204 1205 for(;;) { |
1181 if (fread(buf, sizeof(buf), 1, fp) != 1) { | 1206 if (fread(buf, sizeof(buf), (size_t)1, fp) != 1) { |
1182 if (ferror(fp)) { 1183 ret = errno; 1184 break; 1185 } 1186 if (fseeko(fp, (off_t)sizeof(buf) * count, SEEK_SET) == 1187 (off_t)-1) { 1188 ret = errno; 1189 break; 1190 } | 1207 if (ferror(fp)) { 1208 ret = errno; 1209 break; 1210 } 1211 if (fseeko(fp, (off_t)sizeof(buf) * count, SEEK_SET) == 1212 (off_t)-1) { 1213 ret = errno; 1214 break; 1215 } |
1191 left = fread(buf, 1, sizeof(buf), fp); | 1216 left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), fp); |
1192 if (ferror(fp)) { 1193 ret = errno; 1194 break; 1195 } 1196 if (left == 0) { 1197 count--; 1198 left = sizeof(buf); | 1217 if (ferror(fp)) { 1218 ret = errno; 1219 break; 1220 } 1221 if (left == 0) { 1222 count--; 1223 left = sizeof(buf); |
1199 } else if (fwrite(buf, (size_t)left, 1, tp) != 1) { | 1224 } else if (fwrite(buf, (size_t)left, (size_t)1, tp) 1225 != 1) { |
1200 ret = errno; 1201 break; 1202 } 1203 fflush(tp); 1204 break; 1205 } | 1226 ret = errno; 1227 break; 1228 } 1229 fflush(tp); 1230 break; 1231 } |
1206 if (fwrite(buf, sizeof(buf), 1, tp) != 1) { | 1232 if (fwrite(buf, sizeof(buf), (size_t)1, tp) != 1) { |
1207 ret = errno; 1208 break; 1209 } 1210 count++; 1211 } 1212 if (ret) 1213 goto out3; 1214 cp = buf + left - 1; --- 13 unchanged lines hidden (view full) --- 1228 } 1229 if (nlines <= 0 || count == 0) 1230 break; 1231 count--; 1232 if (fseeko(tp, (off_t)sizeof(buf) * count, SEEK_SET) < 0) { 1233 ret = errno; 1234 break; 1235 } | 1233 ret = errno; 1234 break; 1235 } 1236 count++; 1237 } 1238 if (ret) 1239 goto out3; 1240 cp = buf + left - 1; --- 13 unchanged lines hidden (view full) --- 1254 } 1255 if (nlines <= 0 || count == 0) 1256 break; 1257 count--; 1258 if (fseeko(tp, (off_t)sizeof(buf) * count, SEEK_SET) < 0) { 1259 ret = errno; 1260 break; 1261 } |
1236 if (fread(buf, sizeof(buf), 1, tp) != 1) { | 1262 if (fread(buf, sizeof(buf), (size_t)1, tp) != 1) { |
1237 if (ferror(tp)) { 1238 ret = errno; 1239 break; 1240 } 1241 ret = EAGAIN; 1242 break; 1243 } 1244 cp = buf + sizeof(buf); 1245 } 1246 1247 if (ret || nlines > 0) 1248 goto out3; 1249 | 1263 if (ferror(tp)) { 1264 ret = errno; 1265 break; 1266 } 1267 ret = EAGAIN; 1268 break; 1269 } 1270 cp = buf + sizeof(buf); 1271 } 1272 1273 if (ret || nlines > 0) 1274 goto out3; 1275 |
1250 if (fseeko(fp, 0, SEEK_SET) == (off_t)-1) { | 1276 if (fseeko(fp, (off_t)0, SEEK_SET) == (off_t)-1) { |
1251 ret = errno; 1252 goto out3; 1253 } 1254 1255 if (fseeko(tp, (off_t)sizeof(buf) * count + (cp - buf), SEEK_SET) == 1256 (off_t)-1) { 1257 ret = errno; 1258 goto out3; 1259 } 1260 1261 for(;;) { | 1277 ret = errno; 1278 goto out3; 1279 } 1280 1281 if (fseeko(tp, (off_t)sizeof(buf) * count + (cp - buf), SEEK_SET) == 1282 (off_t)-1) { 1283 ret = errno; 1284 goto out3; 1285 } 1286 1287 for(;;) { |
1262 if ((left = fread(buf, 1, sizeof(buf), tp)) == 0) { | 1288 if ((left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), tp)) == 0) { |
1263 if (ferror(fp)) 1264 ret = errno; 1265 break; 1266 } | 1289 if (ferror(fp)) 1290 ret = errno; 1291 break; 1292 } |
1267 if (fwrite(buf, (size_t)left, 1, fp) != 1) { | 1293 if (fwrite(buf, (size_t)left, (size_t)1, fp) != 1) { |
1268 ret = errno; 1269 break; 1270 } 1271 } 1272 fflush(fp); 1273 if((off = ftello(fp)) > 0) 1274 (void)ftruncate(fileno(fp), off); 1275out3: --- 8 unchanged lines hidden (view full) --- 1284 1285 1286/* 1287 * read history from a file given 1288 */ 1289int 1290read_history(const char *filename) 1291{ | 1294 ret = errno; 1295 break; 1296 } 1297 } 1298 fflush(fp); 1299 if((off = ftello(fp)) > 0) 1300 (void)ftruncate(fileno(fp), off); 1301out3: --- 8 unchanged lines hidden (view full) --- 1310 1311 1312/* 1313 * read history from a file given 1314 */ 1315int 1316read_history(const char *filename) 1317{ |
1292 TYPE(HistEvent) ev; | 1318 HistEvent ev; |
1293 1294 if (h == NULL || e == NULL) 1295 rl_initialize(); 1296 if (filename == NULL && (filename = _default_history_file()) == NULL) 1297 return errno; | 1319 1320 if (h == NULL || e == NULL) 1321 rl_initialize(); 1322 if (filename == NULL && (filename = _default_history_file()) == NULL) 1323 return errno; |
1298 return (FUNW(history)(h, &ev, H_LOAD, filename) == -1 ? 1299 (errno ? errno : EINVAL) : 0); | 1324 return history(h, &ev, H_LOAD, filename) == -1 ? 1325 (errno ? errno : EINVAL) : 0; |
1300} 1301 1302 1303/* 1304 * write history to a file given 1305 */ 1306int 1307write_history(const char *filename) 1308{ | 1326} 1327 1328 1329/* 1330 * write history to a file given 1331 */ 1332int 1333write_history(const char *filename) 1334{ |
1309 TYPE(HistEvent) ev; | 1335 HistEvent ev; |
1310 1311 if (h == NULL || e == NULL) 1312 rl_initialize(); 1313 if (filename == NULL && (filename = _default_history_file()) == NULL) 1314 return errno; | 1336 1337 if (h == NULL || e == NULL) 1338 rl_initialize(); 1339 if (filename == NULL && (filename = _default_history_file()) == NULL) 1340 return errno; |
1315 return (FUNW(history)(h, &ev, H_SAVE, filename) == -1 ? 1316 (errno ? errno : EINVAL) : 0); | 1341 return history(h, &ev, H_SAVE, filename) == -1 ? 1342 (errno ? errno : EINVAL) : 0; |
1317} 1318 1319 1320/* 1321 * returns history ``num''th event 1322 * 1323 * returned pointer points to static variable 1324 */ 1325HIST_ENTRY * 1326history_get(int num) 1327{ 1328 static HIST_ENTRY she; | 1343} 1344 1345 1346/* 1347 * returns history ``num''th event 1348 * 1349 * returned pointer points to static variable 1350 */ 1351HIST_ENTRY * 1352history_get(int num) 1353{ 1354 static HIST_ENTRY she; |
1329 TYPE(HistEvent) ev; | 1355 HistEvent ev; |
1330 int curr_num; 1331 1332 if (h == NULL || e == NULL) 1333 rl_initialize(); 1334 1335 /* save current position */ | 1356 int curr_num; 1357 1358 if (h == NULL || e == NULL) 1359 rl_initialize(); 1360 1361 /* save current position */ |
1336 if (FUNW(history)(h, &ev, H_CURR) != 0) 1337 return (NULL); | 1362 if (history(h, &ev, H_CURR) != 0) 1363 return NULL; |
1338 curr_num = ev.num; 1339 1340 /* start from the oldest */ | 1364 curr_num = ev.num; 1365 1366 /* start from the oldest */ |
1341 if (FUNW(history)(h, &ev, H_LAST) != 0) 1342 return (NULL); /* error */ | 1367 if (history(h, &ev, H_LAST) != 0) 1368 return NULL; /* error */ |
1343 1344 /* look forwards for event matching specified offset */ | 1369 1370 /* look forwards for event matching specified offset */ |
1345 if (FUNW(history)(h, &ev, H_NEXT_EVDATA, num, &she.data)) 1346 return (NULL); | 1371 if (history(h, &ev, H_NEXT_EVDATA, num, &she.data)) 1372 return NULL; |
1347 | 1373 |
1348 she.line = ct_encode_string(ev.str, &conv); | 1374 she.line = ev.str; |
1349 1350 /* restore pointer to where it was */ | 1375 1376 /* restore pointer to where it was */ |
1351 (void)FUNW(history)(h, &ev, H_SET, curr_num); | 1377 (void)history(h, &ev, H_SET, curr_num); |
1352 | 1378 |
1353 return (&she); | 1379 return &she; |
1354} 1355 1356 1357/* 1358 * add the line to history table 1359 */ 1360int 1361add_history(const char *line) 1362{ | 1380} 1381 1382 1383/* 1384 * add the line to history table 1385 */ 1386int 1387add_history(const char *line) 1388{ |
1363 TYPE(HistEvent) ev; 1364 const Char *wline; | 1389 HistEvent ev; |
1365 1366 if (line == NULL) 1367 return 0; 1368 1369 if (h == NULL || e == NULL) 1370 rl_initialize(); 1371 | 1390 1391 if (line == NULL) 1392 return 0; 1393 1394 if (h == NULL || e == NULL) 1395 rl_initialize(); 1396 |
1372 wline = ct_decode_string(line, &conv); 1373 1374 (void)FUNW(history)(h, &ev, H_ENTER, wline); 1375 if (FUNW(history)(h, &ev, H_GETSIZE) == 0) | 1397 (void)history(h, &ev, H_ENTER, line); 1398 if (history(h, &ev, H_GETSIZE) == 0) |
1376 history_length = ev.num; 1377 | 1399 history_length = ev.num; 1400 |
1378 return (!(history_length > 0)); /* return 0 if all is okay */ | 1401 return !(history_length > 0); /* return 0 if all is okay */ |
1379} 1380 1381 1382/* 1383 * remove the specified entry from the history list and return it. 1384 */ 1385HIST_ENTRY * 1386remove_history(int num) 1387{ 1388 HIST_ENTRY *he; | 1402} 1403 1404 1405/* 1406 * remove the specified entry from the history list and return it. 1407 */ 1408HIST_ENTRY * 1409remove_history(int num) 1410{ 1411 HIST_ENTRY *he; |
1389 TYPE(HistEvent) ev; | 1412 HistEvent ev; |
1390 1391 if (h == NULL || e == NULL) 1392 rl_initialize(); 1393 | 1413 1414 if (h == NULL || e == NULL) 1415 rl_initialize(); 1416 |
1394 if ((he = malloc(sizeof(*he))) == NULL) | 1417 if ((he = el_malloc(sizeof(*he))) == NULL) |
1395 return NULL; 1396 | 1418 return NULL; 1419 |
1397 if (FUNW(history)(h, &ev, H_DELDATA, num, &he->data) != 0) { 1398 free(he); | 1420 if (history(h, &ev, H_DELDATA, num, &he->data) != 0) { 1421 el_free(he); |
1399 return NULL; 1400 } 1401 | 1422 return NULL; 1423 } 1424 |
1402 he->line = ct_encode_string(ev.str, &conv); 1403 if (FUNW(history)(h, &ev, H_GETSIZE) == 0) | 1425 he->line = ev.str; 1426 if (history(h, &ev, H_GETSIZE) == 0) |
1404 history_length = ev.num; 1405 1406 return he; 1407} 1408 1409 1410/* 1411 * replace the line and data of the num-th entry 1412 */ 1413HIST_ENTRY * 1414replace_history_entry(int num, const char *line, histdata_t data) 1415{ 1416 HIST_ENTRY *he; | 1427 history_length = ev.num; 1428 1429 return he; 1430} 1431 1432 1433/* 1434 * replace the line and data of the num-th entry 1435 */ 1436HIST_ENTRY * 1437replace_history_entry(int num, const char *line, histdata_t data) 1438{ 1439 HIST_ENTRY *he; |
1417 TYPE(HistEvent) ev; | 1440 HistEvent ev; |
1418 int curr_num; 1419 1420 if (h == NULL || e == NULL) 1421 rl_initialize(); 1422 1423 /* save current position */ | 1441 int curr_num; 1442 1443 if (h == NULL || e == NULL) 1444 rl_initialize(); 1445 1446 /* save current position */ |
1424 if (FUNW(history)(h, &ev, H_CURR) != 0) | 1447 if (history(h, &ev, H_CURR) != 0) |
1425 return NULL; 1426 curr_num = ev.num; 1427 1428 /* start from the oldest */ | 1448 return NULL; 1449 curr_num = ev.num; 1450 1451 /* start from the oldest */ |
1429 if (FUNW(history)(h, &ev, H_LAST) != 0) | 1452 if (history(h, &ev, H_LAST) != 0) |
1430 return NULL; /* error */ 1431 | 1453 return NULL; /* error */ 1454 |
1432 if ((he = malloc(sizeof(*he))) == NULL) | 1455 if ((he = el_malloc(sizeof(*he))) == NULL) |
1433 return NULL; 1434 1435 /* look forwards for event matching specified offset */ | 1456 return NULL; 1457 1458 /* look forwards for event matching specified offset */ |
1436 if (FUNW(history)(h, &ev, H_NEXT_EVDATA, num, &he->data)) | 1459 if (history(h, &ev, H_NEXT_EVDATA, num, &he->data)) |
1437 goto out; 1438 | 1460 goto out; 1461 |
1439 he->line = strdup(ct_encode_string(ev.str, &e->el_scratch)); | 1462 he->line = strdup(ev.str); |
1440 if (he->line == NULL) 1441 goto out; 1442 | 1463 if (he->line == NULL) 1464 goto out; 1465 |
1443 if (FUNW(history)(h, &ev, H_REPLACE, line, data)) | 1466 if (history(h, &ev, H_REPLACE, line, data)) |
1444 goto out; 1445 1446 /* restore pointer to where it was */ | 1467 goto out; 1468 1469 /* restore pointer to where it was */ |
1447 if (FUNW(history)(h, &ev, H_SET, curr_num)) | 1470 if (history(h, &ev, H_SET, curr_num)) |
1448 goto out; 1449 1450 return he; 1451out: | 1471 goto out; 1472 1473 return he; 1474out: |
1452 free(he); | 1475 el_free(he); |
1453 return NULL; 1454} 1455 1456/* 1457 * clear the history list - delete all entries 1458 */ 1459void 1460clear_history(void) 1461{ | 1476 return NULL; 1477} 1478 1479/* 1480 * clear the history list - delete all entries 1481 */ 1482void 1483clear_history(void) 1484{ |
1462 TYPE(HistEvent) ev; | 1485 HistEvent ev; |
1463 | 1486 |
1464 (void)FUNW(history)(h, &ev, H_CLEAR); | 1487 if (h == NULL || e == NULL) 1488 rl_initialize(); 1489 1490 (void)history(h, &ev, H_CLEAR); |
1465 history_length = 0; 1466} 1467 1468 1469/* 1470 * returns offset of the current history event 1471 */ 1472int 1473where_history(void) 1474{ | 1491 history_length = 0; 1492} 1493 1494 1495/* 1496 * returns offset of the current history event 1497 */ 1498int 1499where_history(void) 1500{ |
1475 TYPE(HistEvent) ev; | 1501 HistEvent ev; |
1476 int curr_num, off; 1477 | 1502 int curr_num, off; 1503 |
1478 if (FUNW(history)(h, &ev, H_CURR) != 0) 1479 return (0); | 1504 if (history(h, &ev, H_CURR) != 0) 1505 return 0; |
1480 curr_num = ev.num; 1481 | 1506 curr_num = ev.num; 1507 |
1482 (void)FUNW(history)(h, &ev, H_FIRST); | 1508 (void)history(h, &ev, H_FIRST); |
1483 off = 1; | 1509 off = 1; |
1484 while (ev.num != curr_num && FUNW(history)(h, &ev, H_NEXT) == 0) | 1510 while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0) |
1485 off++; 1486 | 1511 off++; 1512 |
1487 return (off); | 1513 return off; |
1488} 1489 1490 1491/* 1492 * returns current history event or NULL if there is no such event 1493 */ 1494HIST_ENTRY * 1495current_history(void) 1496{ 1497 | 1514} 1515 1516 1517/* 1518 * returns current history event or NULL if there is no such event 1519 */ 1520HIST_ENTRY * 1521current_history(void) 1522{ 1523 |
1498 return (_move_history(H_CURR)); | 1524 return _move_history(H_CURR); |
1499} 1500 1501 1502/* 1503 * returns total number of bytes history events' data are using 1504 */ 1505int 1506history_total_bytes(void) 1507{ | 1525} 1526 1527 1528/* 1529 * returns total number of bytes history events' data are using 1530 */ 1531int 1532history_total_bytes(void) 1533{ |
1508 TYPE(HistEvent) ev; | 1534 HistEvent ev; |
1509 int curr_num; 1510 size_t size; 1511 | 1535 int curr_num; 1536 size_t size; 1537 |
1512 if (FUNW(history)(h, &ev, H_CURR) != 0) 1513 return (-1); | 1538 if (history(h, &ev, H_CURR) != 0) 1539 return -1; |
1514 curr_num = ev.num; 1515 | 1540 curr_num = ev.num; 1541 |
1516 (void)FUNW(history)(h, &ev, H_FIRST); | 1542 (void)history(h, &ev, H_FIRST); |
1517 size = 0; 1518 do | 1543 size = 0; 1544 do |
1519 size += Strlen(ev.str) * sizeof(*ev.str); 1520 while (FUNW(history)(h, &ev, H_NEXT) == 0); | 1545 size += strlen(ev.str) * sizeof(*ev.str); 1546 while (history(h, &ev, H_NEXT) == 0); |
1521 1522 /* get to the same position as before */ | 1547 1548 /* get to the same position as before */ |
1523 FUNW(history)(h, &ev, H_PREV_EVENT, curr_num); | 1549 history(h, &ev, H_PREV_EVENT, curr_num); |
1524 | 1550 |
1525 return (int)(size); | 1551 return (int)size; |
1526} 1527 1528 1529/* 1530 * sets the position in the history list to ``pos'' 1531 */ 1532int 1533history_set_pos(int pos) 1534{ | 1552} 1553 1554 1555/* 1556 * sets the position in the history list to ``pos'' 1557 */ 1558int 1559history_set_pos(int pos) 1560{ |
1535 TYPE(HistEvent) ev; | 1561 HistEvent ev; |
1536 int curr_num; 1537 1538 if (pos >= history_length || pos < 0) | 1562 int curr_num; 1563 1564 if (pos >= history_length || pos < 0) |
1539 return (-1); | 1565 return -1; |
1540 | 1566 |
1541 (void)FUNW(history)(h, &ev, H_CURR); | 1567 (void)history(h, &ev, H_CURR); |
1542 curr_num = ev.num; 1543 1544 /* 1545 * use H_DELDATA to set to nth history (without delete) by passing 1546 * (void **)-1 1547 */ | 1568 curr_num = ev.num; 1569 1570 /* 1571 * use H_DELDATA to set to nth history (without delete) by passing 1572 * (void **)-1 1573 */ |
1548 if (FUNW(history)(h, &ev, H_DELDATA, pos, (void **)-1)) { 1549 (void)FUNW(history)(h, &ev, H_SET, curr_num); 1550 return(-1); | 1574 if (history(h, &ev, H_DELDATA, pos, (void **)-1)) { 1575 (void)history(h, &ev, H_SET, curr_num); 1576 return -1; |
1551 } | 1577 } |
1552 return (0); | 1578 return 0; |
1553} 1554 1555 1556/* 1557 * returns previous event in history and shifts pointer accordingly 1558 */ 1559HIST_ENTRY * 1560previous_history(void) 1561{ 1562 | 1579} 1580 1581 1582/* 1583 * returns previous event in history and shifts pointer accordingly 1584 */ 1585HIST_ENTRY * 1586previous_history(void) 1587{ 1588 |
1563 return (_move_history(H_PREV)); | 1589 return _move_history(H_PREV); |
1564} 1565 1566 1567/* 1568 * returns next event in history and shifts pointer accordingly 1569 */ 1570HIST_ENTRY * 1571next_history(void) 1572{ 1573 | 1590} 1591 1592 1593/* 1594 * returns next event in history and shifts pointer accordingly 1595 */ 1596HIST_ENTRY * 1597next_history(void) 1598{ 1599 |
1574 return (_move_history(H_NEXT)); | 1600 return _move_history(H_NEXT); |
1575} 1576 1577 1578/* 1579 * searches for first history event containing the str 1580 */ 1581int 1582history_search(const char *str, int direction) 1583{ | 1601} 1602 1603 1604/* 1605 * searches for first history event containing the str 1606 */ 1607int 1608history_search(const char *str, int direction) 1609{ |
1584 TYPE(HistEvent) ev; 1585 const Char *strp; 1586 const Char *wstr; | 1610 HistEvent ev; 1611 const char *strp; |
1587 int curr_num; 1588 | 1612 int curr_num; 1613 |
1589 if (FUNW(history)(h, &ev, H_CURR) != 0) 1590 return (-1); | 1614 if (history(h, &ev, H_CURR) != 0) 1615 return -1; |
1591 curr_num = ev.num; 1592 | 1616 curr_num = ev.num; 1617 |
1593 wstr = ct_decode_string(str, &conv); | |
1594 for (;;) { | 1618 for (;;) { |
1595 if ((strp = Strstr(ev.str, wstr)) != NULL) 1596 return (int) (strp - ev.str); 1597 if (FUNW(history)(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0) | 1619 if ((strp = strstr(ev.str, str)) != NULL) 1620 return (int)(strp - ev.str); 1621 if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0) |
1598 break; 1599 } | 1622 break; 1623 } |
1600 (void)FUNW(history)(h, &ev, H_SET, curr_num); 1601 return (-1); | 1624 (void)history(h, &ev, H_SET, curr_num); 1625 return -1; |
1602} 1603 1604 1605/* 1606 * searches for first history event beginning with str 1607 */ 1608int 1609history_search_prefix(const char *str, int direction) 1610{ | 1626} 1627 1628 1629/* 1630 * searches for first history event beginning with str 1631 */ 1632int 1633history_search_prefix(const char *str, int direction) 1634{ |
1611 TYPE(HistEvent) ev; | 1635 HistEvent ev; |
1612 | 1636 |
1613 return (FUNW(history)(h, &ev, direction < 0 ? | 1637 return (history(h, &ev, direction < 0 ? |
1614 H_PREV_STR : H_NEXT_STR, str)); 1615} 1616 1617 1618/* 1619 * search for event in history containing str, starting at offset 1620 * abs(pos); continue backward, if pos<0, forward otherwise 1621 */ 1622/* ARGSUSED */ 1623int 1624history_search_pos(const char *str, 1625 int direction __attribute__((__unused__)), int pos) 1626{ | 1638 H_PREV_STR : H_NEXT_STR, str)); 1639} 1640 1641 1642/* 1643 * search for event in history containing str, starting at offset 1644 * abs(pos); continue backward, if pos<0, forward otherwise 1645 */ 1646/* ARGSUSED */ 1647int 1648history_search_pos(const char *str, 1649 int direction __attribute__((__unused__)), int pos) 1650{ |
1627 TYPE(HistEvent) ev; | 1651 HistEvent ev; |
1628 int curr_num, off; | 1652 int curr_num, off; |
1629 const Char *wstr; | |
1630 1631 off = (pos > 0) ? pos : -pos; 1632 pos = (pos > 0) ? 1 : -1; 1633 | 1653 1654 off = (pos > 0) ? pos : -pos; 1655 pos = (pos > 0) ? 1 : -1; 1656 |
1634 if (FUNW(history)(h, &ev, H_CURR) != 0) 1635 return (-1); | 1657 if (history(h, &ev, H_CURR) != 0) 1658 return -1; |
1636 curr_num = ev.num; 1637 | 1659 curr_num = ev.num; 1660 |
1638 if (history_set_pos(off) != 0 || FUNW(history)(h, &ev, H_CURR) != 0) 1639 return (-1); | 1661 if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0) 1662 return -1; |
1640 | 1663 |
1641 wstr = ct_decode_string(str, &conv); | |
1642 for (;;) { | 1664 for (;;) { |
1643 if (Strstr(ev.str, wstr)) 1644 return (off); 1645 if (FUNW(history)(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0) | 1665 if (strstr(ev.str, str)) 1666 return off; 1667 if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0) |
1646 break; 1647 } 1648 1649 /* set "current" pointer back to previous state */ | 1668 break; 1669 } 1670 1671 /* set "current" pointer back to previous state */ |
1650 (void)FUNW(history)(h, &ev, | 1672 (void)history(h, &ev, |
1651 pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num); 1652 | 1673 pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num); 1674 |
1653 return (-1); | 1675 return -1; |
1654} 1655 1656 1657/********************************/ 1658/* completion functions */ 1659 1660char * 1661tilde_expand(char *name) --- 6 unchanged lines hidden (view full) --- 1668{ 1669 return fn_filename_completion_function(name, state); 1670} 1671 1672/* 1673 * a completion generator for usernames; returns _first_ username 1674 * which starts with supplied text 1675 * text contains a partial username preceded by random character | 1676} 1677 1678 1679/********************************/ 1680/* completion functions */ 1681 1682char * 1683tilde_expand(char *name) --- 6 unchanged lines hidden (view full) --- 1690{ 1691 return fn_filename_completion_function(name, state); 1692} 1693 1694/* 1695 * a completion generator for usernames; returns _first_ username 1696 * which starts with supplied text 1697 * text contains a partial username preceded by random character |
1676 * (usually '~'); state is ignored 1677 * it's callers responsibility to free returned value | 1698 * (usually '~'); state resets search from start (??? should we do that anyway) 1699 * it's the caller's responsibility to free the returned value |
1678 */ 1679char * 1680username_completion_function(const char *text, int state) 1681{ | 1700 */ 1701char * 1702username_completion_function(const char *text, int state) 1703{ |
1682 struct passwd *pwd, pwres; | 1704#if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT) 1705 struct passwd pwres; |
1683 char pwbuf[1024]; | 1706 char pwbuf[1024]; |
1707#endif 1708 struct passwd *pass = NULL; |
|
1684 1685 if (text[0] == '\0') | 1709 1710 if (text[0] == '\0') |
1686 return (NULL); | 1711 return NULL; |
1687 1688 if (*text == '~') 1689 text++; 1690 1691 if (state == 0) 1692 setpwent(); 1693 | 1712 1713 if (*text == '~') 1714 text++; 1715 1716 if (state == 0) 1717 setpwent(); 1718 |
1694 while (getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pwd) == 0 1695 && pwd != NULL && text[0] == pwd->pw_name[0] 1696 && strcmp(text, pwd->pw_name) == 0); | 1719 while ( 1720#if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT) 1721 getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pass) == 0 && pass != NULL 1722#else 1723 (pass = getpwent()) != NULL 1724#endif 1725 && text[0] == pass->pw_name[0] 1726 && strcmp(text, pass->pw_name) == 0) 1727 continue; |
1697 | 1728 |
1698 if (pwd == NULL) { | 1729 if (pass == NULL) { |
1699 endpwent(); 1700 return NULL; 1701 } | 1730 endpwent(); 1731 return NULL; 1732 } |
1702 return strdup(pwd->pw_name); | 1733 return strdup(pass->pw_name); |
1703} 1704 1705 1706/* 1707 * el-compatible wrapper to send TSTP on ^Z 1708 */ 1709/* ARGSUSED */ 1710static unsigned char --- 16 unchanged lines hidden (view full) --- 1727} 1728 1729static const char * 1730/*ARGSUSED*/ 1731_rl_completion_append_character_function(const char *dummy 1732 __attribute__((__unused__))) 1733{ 1734 static char buf[2]; | 1734} 1735 1736 1737/* 1738 * el-compatible wrapper to send TSTP on ^Z 1739 */ 1740/* ARGSUSED */ 1741static unsigned char --- 16 unchanged lines hidden (view full) --- 1758} 1759 1760static const char * 1761/*ARGSUSED*/ 1762_rl_completion_append_character_function(const char *dummy 1763 __attribute__((__unused__))) 1764{ 1765 static char buf[2]; |
1735 buf[0] = rl_completion_append_character; | 1766 buf[0] = (char)rl_completion_append_character; |
1736 buf[1] = '\0'; 1737 return buf; 1738} 1739 1740 1741/* 1742 * complete word at current point 1743 */ 1744/* ARGSUSED */ 1745int 1746rl_complete(int ignore __attribute__((__unused__)), int invoking_key) 1747{ 1748#ifdef WIDECHAR 1749 static ct_buffer_t wbreak_conv, sprefix_conv; 1750#endif | 1767 buf[1] = '\0'; 1768 return buf; 1769} 1770 1771 1772/* 1773 * complete word at current point 1774 */ 1775/* ARGSUSED */ 1776int 1777rl_complete(int ignore __attribute__((__unused__)), int invoking_key) 1778{ 1779#ifdef WIDECHAR 1780 static ct_buffer_t wbreak_conv, sprefix_conv; 1781#endif |
1782 char *breakchars; |
|
1751 1752 if (h == NULL || e == NULL) 1753 rl_initialize(); 1754 1755 if (rl_inhibit_completion) { 1756 char arr[2]; 1757 arr[0] = (char)invoking_key; 1758 arr[1] = '\0'; 1759 el_insertstr(e, arr); | 1783 1784 if (h == NULL || e == NULL) 1785 rl_initialize(); 1786 1787 if (rl_inhibit_completion) { 1788 char arr[2]; 1789 arr[0] = (char)invoking_key; 1790 arr[1] = '\0'; 1791 el_insertstr(e, arr); |
1760 return (CC_REFRESH); | 1792 return CC_REFRESH; |
1761 } 1762 | 1793 } 1794 |
1795 if (rl_completion_word_break_hook != NULL) 1796 breakchars = (*rl_completion_word_break_hook)(); 1797 else 1798 breakchars = rl_basic_word_break_characters; 1799 |
|
1763 /* Just look at how many global variables modify this operation! */ 1764 return fn_complete(e, 1765 (CPFunction *)rl_completion_entry_function, 1766 rl_attempted_completion_function, 1767 ct_decode_string(rl_basic_word_break_characters, &wbreak_conv), | 1800 /* Just look at how many global variables modify this operation! */ 1801 return fn_complete(e, 1802 (CPFunction *)rl_completion_entry_function, 1803 rl_attempted_completion_function, 1804 ct_decode_string(rl_basic_word_break_characters, &wbreak_conv), |
1768 ct_decode_string(rl_special_prefixes, &sprefix_conv), | 1805 ct_decode_string(breakchars, &sprefix_conv), |
1769 _rl_completion_append_character_function, 1770 (size_t)rl_completion_query_items, 1771 &rl_completion_type, &rl_attempted_completion_over, 1772 &rl_point, &rl_end, NULL, NULL, NULL); | 1806 _rl_completion_append_character_function, 1807 (size_t)rl_completion_query_items, 1808 &rl_completion_type, &rl_attempted_completion_over, 1809 &rl_point, &rl_end, NULL, NULL, NULL); |
1810 1811 |
|
1773} 1774 1775 1776/* ARGSUSED */ 1777static unsigned char 1778_el_rl_complete(EditLine *el __attribute__((__unused__)), int ch) 1779{ 1780 return (unsigned char)rl_complete(0, ch); --- 14 unchanged lines hidden (view full) --- 1795 if (h == NULL || e == NULL) 1796 rl_initialize(); 1797 1798 if (func == rl_insert) { 1799 /* XXX notice there is no range checking of ``c'' */ 1800 e->el_map.key[c] = ED_INSERT; 1801 retval = 0; 1802 } | 1812} 1813 1814 1815/* ARGSUSED */ 1816static unsigned char 1817_el_rl_complete(EditLine *el __attribute__((__unused__)), int ch) 1818{ 1819 return (unsigned char)rl_complete(0, ch); --- 14 unchanged lines hidden (view full) --- 1834 if (h == NULL || e == NULL) 1835 rl_initialize(); 1836 1837 if (func == rl_insert) { 1838 /* XXX notice there is no range checking of ``c'' */ 1839 e->el_map.key[c] = ED_INSERT; 1840 retval = 0; 1841 } |
1803 return (retval); | 1842 return retval; |
1804} 1805 1806 1807/* 1808 * read one key from input - handles chars pushed back 1809 * to input stream also 1810 */ 1811int 1812rl_read_key(void) 1813{ 1814 char fooarr[2 * sizeof(int)]; 1815 1816 if (e == NULL || h == NULL) 1817 rl_initialize(); 1818 | 1843} 1844 1845 1846/* 1847 * read one key from input - handles chars pushed back 1848 * to input stream also 1849 */ 1850int 1851rl_read_key(void) 1852{ 1853 char fooarr[2 * sizeof(int)]; 1854 1855 if (e == NULL || h == NULL) 1856 rl_initialize(); 1857 |
1819 return (el_getc(e, fooarr)); | 1858 return el_getc(e, fooarr); |
1820} 1821 1822 1823/* 1824 * reset the terminal 1825 */ 1826/* ARGSUSED */ 1827void --- 13 unchanged lines hidden (view full) --- 1841rl_insert(int count, int c) 1842{ 1843 char arr[2]; 1844 1845 if (h == NULL || e == NULL) 1846 rl_initialize(); 1847 1848 /* XXX - int -> char conversion can lose on multichars */ | 1859} 1860 1861 1862/* 1863 * reset the terminal 1864 */ 1865/* ARGSUSED */ 1866void --- 13 unchanged lines hidden (view full) --- 1880rl_insert(int count, int c) 1881{ 1882 char arr[2]; 1883 1884 if (h == NULL || e == NULL) 1885 rl_initialize(); 1886 1887 /* XXX - int -> char conversion can lose on multichars */ |
1849 arr[0] = c; | 1888 arr[0] = (char)c; |
1850 arr[1] = '\0'; 1851 1852 for (; count > 0; count--) 1853 el_push(e, arr); 1854 | 1889 arr[1] = '\0'; 1890 1891 for (; count > 0; count--) 1892 el_push(e, arr); 1893 |
1855 return (0); | 1894 return 0; |
1856} 1857 1858int 1859rl_insert_text(const char *text) 1860{ 1861 if (!text || *text == 0) | 1895} 1896 1897int 1898rl_insert_text(const char *text) 1899{ 1900 if (!text || *text == 0) |
1862 return (0); | 1901 return 0; |
1863 1864 if (h == NULL || e == NULL) 1865 rl_initialize(); 1866 1867 if (el_insertstr(e, text) < 0) | 1902 1903 if (h == NULL || e == NULL) 1904 rl_initialize(); 1905 1906 if (el_insertstr(e, text) < 0) |
1868 return (0); | 1907 return 0; |
1869 return (int)strlen(text); 1870} 1871 1872/*ARGSUSED*/ 1873int | 1908 return (int)strlen(text); 1909} 1910 1911/*ARGSUSED*/ 1912int |
1874rl_newline(int count, int c) | 1913rl_newline(int count __attribute__((__unused__)), 1914 int c __attribute__((__unused__))) |
1875{ 1876 /* 1877 * Readline-4.0 appears to ignore the args. 1878 */ 1879 return rl_insert(1, '\n'); 1880} 1881 1882/*ARGSUSED*/ 1883static unsigned char | 1915{ 1916 /* 1917 * Readline-4.0 appears to ignore the args. 1918 */ 1919 return rl_insert(1, '\n'); 1920} 1921 1922/*ARGSUSED*/ 1923static unsigned char |
1884rl_bind_wrapper(EditLine *el, unsigned char c) | 1924rl_bind_wrapper(EditLine *el __attribute__((__unused__)), unsigned char c) |
1885{ 1886 if (map[c] == NULL) 1887 return CC_ERROR; 1888 1889 _rl_update_pos(); 1890 1891 (*map[c])(NULL, c); 1892 --- 8 unchanged lines hidden (view full) --- 1901rl_add_defun(const char *name, Function *fun, int c) 1902{ 1903 char dest[8]; 1904 if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0) 1905 return -1; 1906 map[(unsigned char)c] = fun; 1907 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper); 1908 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0); | 1925{ 1926 if (map[c] == NULL) 1927 return CC_ERROR; 1928 1929 _rl_update_pos(); 1930 1931 (*map[c])(NULL, c); 1932 --- 8 unchanged lines hidden (view full) --- 1941rl_add_defun(const char *name, Function *fun, int c) 1942{ 1943 char dest[8]; 1944 if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0) 1945 return -1; 1946 map[(unsigned char)c] = fun; 1947 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper); 1948 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0); |
1909 el_set(e, EL_BIND, dest, name); | 1949 el_set(e, EL_BIND, dest, name, NULL); |
1910 return 0; 1911} 1912 1913void | 1950 return 0; 1951} 1952 1953void |
1914rl_callback_read_char() | 1954rl_callback_read_char(void) |
1915{ 1916 int count = 0, done = 0; 1917 const char *buf = el_gets(e, &count); 1918 char *wbuf; 1919 1920 if (buf == NULL || count-- <= 0) 1921 return; 1922 if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF]) --- 4 unchanged lines hidden (view full) --- 1927 if (done && rl_linefunc != NULL) { 1928 el_set(e, EL_UNBUFFERED, 0); 1929 if (done == 2) { 1930 if ((wbuf = strdup(buf)) != NULL) 1931 wbuf[count] = '\0'; 1932 } else 1933 wbuf = NULL; 1934 (*(void (*)(const char *))rl_linefunc)(wbuf); | 1955{ 1956 int count = 0, done = 0; 1957 const char *buf = el_gets(e, &count); 1958 char *wbuf; 1959 1960 if (buf == NULL || count-- <= 0) 1961 return; 1962 if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF]) --- 4 unchanged lines hidden (view full) --- 1967 if (done && rl_linefunc != NULL) { 1968 el_set(e, EL_UNBUFFERED, 0); 1969 if (done == 2) { 1970 if ((wbuf = strdup(buf)) != NULL) 1971 wbuf[count] = '\0'; 1972 } else 1973 wbuf = NULL; 1974 (*(void (*)(const char *))rl_linefunc)(wbuf); |
1935 //el_set(e, EL_UNBUFFERED, 1); | 1975 el_set(e, EL_UNBUFFERED, 1); |
1936 } 1937} 1938 1939void 1940rl_callback_handler_install(const char *prompt, VCPFunction *linefunc) 1941{ 1942 if (e == NULL) { 1943 rl_initialize(); --- 9 unchanged lines hidden (view full) --- 1953 el_set(e, EL_UNBUFFERED, 0); 1954 rl_linefunc = NULL; 1955} 1956 1957void 1958rl_redisplay(void) 1959{ 1960 char a[2]; | 1976 } 1977} 1978 1979void 1980rl_callback_handler_install(const char *prompt, VCPFunction *linefunc) 1981{ 1982 if (e == NULL) { 1983 rl_initialize(); --- 9 unchanged lines hidden (view full) --- 1993 el_set(e, EL_UNBUFFERED, 0); 1994 rl_linefunc = NULL; 1995} 1996 1997void 1998rl_redisplay(void) 1999{ 2000 char a[2]; |
1961 a[0] = e->el_tty.t_c[TS_IO][C_REPRINT]; | 2001 a[0] = (char)e->el_tty.t_c[TS_IO][C_REPRINT]; |
1962 a[1] = '\0'; 1963 el_push(e, a); 1964} 1965 1966int 1967rl_get_previous_history(int count, int key) 1968{ 1969 char a[2]; | 2002 a[1] = '\0'; 2003 el_push(e, a); 2004} 2005 2006int 2007rl_get_previous_history(int count, int key) 2008{ 2009 char a[2]; |
1970 a[0] = key; | 2010 a[0] = (char)key; |
1971 a[1] = '\0'; 1972 while (count--) 1973 el_push(e, a); 1974 return 0; 1975} 1976 1977void 1978/*ARGSUSED*/ | 2011 a[1] = '\0'; 2012 while (count--) 2013 el_push(e, a); 2014 return 0; 2015} 2016 2017void 2018/*ARGSUSED*/ |
1979rl_prep_terminal(int meta_flag) | 2019rl_prep_terminal(int meta_flag __attribute__((__unused__))) |
1980{ 1981 el_set(e, EL_PREP_TERM, 1); 1982} 1983 1984void 1985rl_deprep_terminal(void) 1986{ 1987 el_set(e, EL_PREP_TERM, 0); 1988} 1989 1990int 1991rl_read_init_file(const char *s) 1992{ | 2020{ 2021 el_set(e, EL_PREP_TERM, 1); 2022} 2023 2024void 2025rl_deprep_terminal(void) 2026{ 2027 el_set(e, EL_PREP_TERM, 0); 2028} 2029 2030int 2031rl_read_init_file(const char *s) 2032{ |
1993 return(el_source(e, s)); | 2033 return el_source(e, s); |
1994} 1995 1996int 1997rl_parse_and_bind(const char *line) 1998{ 1999 const char **argv; 2000 int argc; 2001 Tokenizer *tok; 2002 2003 tok = tok_init(NULL); 2004 tok_str(tok, line, &argc, &argv); 2005 argc = el_parse(e, argc, argv); 2006 tok_end(tok); | 2034} 2035 2036int 2037rl_parse_and_bind(const char *line) 2038{ 2039 const char **argv; 2040 int argc; 2041 Tokenizer *tok; 2042 2043 tok = tok_init(NULL); 2044 tok_str(tok, line, &argc, &argv); 2045 argc = el_parse(e, argc, argv); 2046 tok_end(tok); |
2007 return (argc ? 1 : 0); | 2047 return argc ? 1 : 0; |
2008} 2009 2010int 2011rl_variable_bind(const char *var, const char *value) 2012{ 2013 /* 2014 * The proper return value is undocument, but this is what the 2015 * readline source seems to do. 2016 */ | 2048} 2049 2050int 2051rl_variable_bind(const char *var, const char *value) 2052{ 2053 /* 2054 * The proper return value is undocument, but this is what the 2055 * readline source seems to do. 2056 */ |
2017 return ((el_set(e, EL_BIND, "", var, value) == -1) ? 1 : 0); | 2057 return el_set(e, EL_BIND, "", var, value, NULL) == -1 ? 1 : 0; |
2018} 2019 2020void 2021rl_stuff_char(int c) 2022{ 2023 char buf[2]; 2024 | 2058} 2059 2060void 2061rl_stuff_char(int c) 2062{ 2063 char buf[2]; 2064 |
2025 buf[0] = c; | 2065 buf[0] = (char)c; |
2026 buf[1] = '\0'; 2027 el_insertstr(e, buf); 2028} 2029 2030static int 2031_rl_event_read_char(EditLine *el, char *cp) 2032{ 2033 int n; 2034 ssize_t num_read = 0; 2035 2036 *cp = '\0'; 2037 while (rl_event_hook) { 2038 2039 (*rl_event_hook)(); 2040 2041#if defined(FIONREAD) 2042 if (ioctl(el->el_infd, FIONREAD, &n) < 0) | 2066 buf[1] = '\0'; 2067 el_insertstr(e, buf); 2068} 2069 2070static int 2071_rl_event_read_char(EditLine *el, char *cp) 2072{ 2073 int n; 2074 ssize_t num_read = 0; 2075 2076 *cp = '\0'; 2077 while (rl_event_hook) { 2078 2079 (*rl_event_hook)(); 2080 2081#if defined(FIONREAD) 2082 if (ioctl(el->el_infd, FIONREAD, &n) < 0) |
2043 return(-1); | 2083 return -1; |
2044 if (n) | 2084 if (n) |
2045 num_read = read(el->el_infd, cp, 1); | 2085 num_read = read(el->el_infd, cp, (size_t)1); |
2046 else 2047 num_read = 0; 2048#elif defined(F_SETFL) && defined(O_NDELAY) 2049 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0) | 2086 else 2087 num_read = 0; 2088#elif defined(F_SETFL) && defined(O_NDELAY) 2089 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0) |
2050 return(-1); | 2090 return -1; |
2051 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0) | 2091 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0) |
2052 return(-1); | 2092 return -1; |
2053 num_read = read(el->el_infd, cp, 1); 2054 if (fcntl(el->el_infd, F_SETFL, n)) | 2093 num_read = read(el->el_infd, cp, 1); 2094 if (fcntl(el->el_infd, F_SETFL, n)) |
2055 return(-1); | 2095 return -1; |
2056#else 2057 /* not non-blocking, but what you gonna do? */ 2058 num_read = read(el->el_infd, cp, 1); | 2096#else 2097 /* not non-blocking, but what you gonna do? */ 2098 num_read = read(el->el_infd, cp, 1); |
2059 return(-1); | 2099 return -1; |
2060#endif 2061 2062 if (num_read < 0 && errno == EAGAIN) 2063 continue; 2064 if (num_read == 0) 2065 continue; 2066 break; 2067 } --- 10 unchanged lines hidden (view full) --- 2078 rl_point = (int)(li->cursor - li->buffer); 2079 rl_end = (int)(li->lastchar - li->buffer); 2080} 2081 2082void 2083rl_get_screen_size(int *rows, int *cols) 2084{ 2085 if (rows) | 2100#endif 2101 2102 if (num_read < 0 && errno == EAGAIN) 2103 continue; 2104 if (num_read == 0) 2105 continue; 2106 break; 2107 } --- 10 unchanged lines hidden (view full) --- 2118 rl_point = (int)(li->cursor - li->buffer); 2119 rl_end = (int)(li->lastchar - li->buffer); 2120} 2121 2122void 2123rl_get_screen_size(int *rows, int *cols) 2124{ 2125 if (rows) |
2086 el_get(e, EL_GETTC, "li", rows); | 2126 el_get(e, EL_GETTC, "li", rows, (void *)0); |
2087 if (cols) | 2127 if (cols) |
2088 el_get(e, EL_GETTC, "co", cols); | 2128 el_get(e, EL_GETTC, "co", cols, (void *)0); |
2089} 2090 2091void 2092rl_set_screen_size(int rows, int cols) 2093{ 2094 char buf[64]; 2095 (void)snprintf(buf, sizeof(buf), "%d", rows); | 2129} 2130 2131void 2132rl_set_screen_size(int rows, int cols) 2133{ 2134 char buf[64]; 2135 (void)snprintf(buf, sizeof(buf), "%d", rows); |
2096 el_set(e, EL_SETTC, "li", buf); | 2136 el_set(e, EL_SETTC, "li", buf, NULL); |
2097 (void)snprintf(buf, sizeof(buf), "%d", cols); | 2137 (void)snprintf(buf, sizeof(buf), "%d", cols); |
2098 el_set(e, EL_SETTC, "co", buf); | 2138 el_set(e, EL_SETTC, "co", buf, NULL); |
2099} 2100 2101char ** 2102rl_completion_matches(const char *str, rl_compentry_func_t *fun) 2103{ 2104 size_t len, max, i, j, min; 2105 char **list, *match, *a, *b; 2106 2107 len = 1; 2108 max = 10; | 2139} 2140 2141char ** 2142rl_completion_matches(const char *str, rl_compentry_func_t *fun) 2143{ 2144 size_t len, max, i, j, min; 2145 char **list, *match, *a, *b; 2146 2147 len = 1; 2148 max = 10; |
2109 if ((list = malloc(max * sizeof(*list))) == NULL) | 2149 if ((list = el_malloc(max * sizeof(*list))) == NULL) |
2110 return NULL; 2111 2112 while ((match = (*fun)(str, (int)(len - 1))) != NULL) { 2113 list[len++] = match; 2114 if (len == max) { 2115 char **nl; 2116 max += 10; | 2150 return NULL; 2151 2152 while ((match = (*fun)(str, (int)(len - 1))) != NULL) { 2153 list[len++] = match; 2154 if (len == max) { 2155 char **nl; 2156 max += 10; |
2117 if ((nl = realloc(list, max * sizeof(*nl))) == NULL) | 2157 if ((nl = el_realloc(list, max * sizeof(*nl))) == NULL) |
2118 goto out; 2119 list = nl; 2120 } 2121 } 2122 if (len == 1) 2123 goto out; 2124 list[len] = NULL; 2125 if (len == 2) { --- 10 unchanged lines hidden (view full) --- 2136 continue; 2137 if (min > j) 2138 min = j; 2139 } 2140 if (min == 0 && *str) { 2141 if ((list[0] = strdup(str)) == NULL) 2142 goto out; 2143 } else { | 2158 goto out; 2159 list = nl; 2160 } 2161 } 2162 if (len == 1) 2163 goto out; 2164 list[len] = NULL; 2165 if (len == 2) { --- 10 unchanged lines hidden (view full) --- 2176 continue; 2177 if (min > j) 2178 min = j; 2179 } 2180 if (min == 0 && *str) { 2181 if ((list[0] = strdup(str)) == NULL) 2182 goto out; 2183 } else { |
2144 if ((list[0] = malloc(min + 1)) == NULL) | 2184 if ((list[0] = el_malloc((min + 1) * sizeof(*list[0]))) == NULL) |
2145 goto out; 2146 (void)memcpy(list[0], list[1], min); 2147 list[0][min] = '\0'; 2148 } 2149 return list; 2150 2151out: | 2185 goto out; 2186 (void)memcpy(list[0], list[1], min); 2187 list[0][min] = '\0'; 2188 } 2189 return list; 2190 2191out: |
2152 free(list); | 2192 el_free(list); |
2153 return NULL; 2154} 2155 2156char * 2157rl_filename_completion_function (const char *text, int state) 2158{ 2159 return fn_filename_completion_function(text, state); 2160} --- 18 unchanged lines hidden (view full) --- 2179 return strcoll(*s1, *s2); 2180} 2181 2182HISTORY_STATE * 2183history_get_history_state(void) 2184{ 2185 HISTORY_STATE *hs; 2186 | 2193 return NULL; 2194} 2195 2196char * 2197rl_filename_completion_function (const char *text, int state) 2198{ 2199 return fn_filename_completion_function(text, state); 2200} --- 18 unchanged lines hidden (view full) --- 2219 return strcoll(*s1, *s2); 2220} 2221 2222HISTORY_STATE * 2223history_get_history_state(void) 2224{ 2225 HISTORY_STATE *hs; 2226 |
2187 if ((hs = malloc(sizeof(HISTORY_STATE))) == NULL) 2188 return (NULL); | 2227 if ((hs = el_malloc(sizeof(*hs))) == NULL) 2228 return NULL; |
2189 hs->length = history_length; | 2229 hs->length = history_length; |
2190 return (hs); | 2230 return hs; |
2191} 2192 2193int 2194/*ARGSUSED*/ | 2231} 2232 2233int 2234/*ARGSUSED*/ |
2195rl_kill_text(int from, int to) | 2235rl_kill_text(int from __attribute__((__unused__)), 2236 int to __attribute__((__unused__))) |
2196{ 2197 return 0; 2198} 2199 2200Keymap 2201rl_make_bare_keymap(void) 2202{ 2203 return NULL; 2204} 2205 2206Keymap 2207rl_get_keymap(void) 2208{ 2209 return NULL; 2210} 2211 2212void 2213/*ARGSUSED*/ | 2237{ 2238 return 0; 2239} 2240 2241Keymap 2242rl_make_bare_keymap(void) 2243{ 2244 return NULL; 2245} 2246 2247Keymap 2248rl_get_keymap(void) 2249{ 2250 return NULL; 2251} 2252 2253void 2254/*ARGSUSED*/ |
2214rl_set_keymap(Keymap k) | 2255rl_set_keymap(Keymap k __attribute__((__unused__))) |
2215{ 2216} 2217 2218int 2219/*ARGSUSED*/ | 2256{ 2257} 2258 2259int 2260/*ARGSUSED*/ |
2220rl_generic_bind(int type, const char * keyseq, const char * data, Keymap k) | 2261rl_generic_bind(int type __attribute__((__unused__)), 2262 const char * keyseq __attribute__((__unused__)), 2263 const char * data __attribute__((__unused__)), 2264 Keymap k __attribute__((__unused__))) |
2221{ 2222 return 0; 2223} 2224 2225int 2226/*ARGSUSED*/ | 2265{ 2266 return 0; 2267} 2268 2269int 2270/*ARGSUSED*/ |
2227rl_bind_key_in_map(int key, rl_command_func_t *fun, Keymap k) | 2271rl_bind_key_in_map(int key __attribute__((__unused__)), 2272 rl_command_func_t *fun __attribute__((__unused__)), 2273 Keymap k __attribute__((__unused__))) |
2228{ 2229 return 0; 2230} 2231 2232/* unsupported, but needed by python */ 2233void 2234rl_cleanup_after_signal(void) 2235{ 2236} 2237 2238int 2239rl_on_new_line(void) 2240{ 2241 return 0; 2242} | 2274{ 2275 return 0; 2276} 2277 2278/* unsupported, but needed by python */ 2279void 2280rl_cleanup_after_signal(void) 2281{ 2282} 2283 2284int 2285rl_on_new_line(void) 2286{ 2287 return 0; 2288} |
2289 2290void 2291rl_free_line_state(void) 2292{ 2293} |
|