1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
33 # include <unistd.h> /* for _POSIX_VERSION */
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
45 /* System-specific feature definitions and include files. */
49 #if defined (TIOCSTAT_IN_SYS_IOCTL)
50 # include <sys/ioctl.h>
51 #endif /* TIOCSTAT_IN_SYS_IOCTL */
53 /* Some standard library routines. */
56 #include "rlprivate.h"
59 /* **************************************************************** */
61 /* Utility Functions */
63 /* **************************************************************** */
65 /* Return 0 if C is not a member of the class of characters that belong
66 in words, or 1 if it is. */
68 int _rl_allow_pathname_alphabetic_chars = 0;
69 static const char * const pathname_alphabetic_chars = "/-_=~.#$";
78 return (_rl_allow_pathname_alphabetic_chars &&
79 strchr (pathname_alphabetic_chars, c) != NULL);
82 #if defined (HANDLE_MULTIBYTE)
93 return (_rl_allow_pathname_alphabetic_chars &&
94 strchr (pathname_alphabetic_chars, c) != NULL);
98 /* How to abort things. */
100 _rl_abort_internal ()
104 _rl_reset_argument ();
105 rl_clear_pending_input ();
107 RL_UNSETSTATE (RL_STATE_MACRODEF);
108 while (rl_executing_macro)
109 _rl_pop_executing_macro ();
111 rl_last_func = (rl_command_func_t *)NULL;
112 longjmp (_rl_top_level, 1);
117 rl_abort (count, key)
120 return (_rl_abort_internal ());
124 rl_tty_status (count, key)
127 #if defined (TIOCSTAT)
128 ioctl (1, TIOCSTAT, (char *)0);
129 rl_refresh_line (count, key);
136 /* Return a copy of the string between FROM and TO.
137 FROM is inclusive, TO is not. */
139 rl_copy_text (from, to)
145 /* Fix it if the caller is confused. */
150 copy = (char *)xmalloc (1 + length);
151 strncpy (copy, rl_line_buffer + from, length);
156 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
159 rl_extend_line_buffer (len)
162 while (len >= rl_line_buffer_len)
164 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
165 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
172 /* A function for simple tilde expansion. */
174 rl_tilde_expand (ignore, key)
177 register int start, end;
178 char *homedir, *temp;
184 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
186 homedir = tilde_expand ("~");
187 _rl_replace_text (homedir, start, end);
191 else if (rl_line_buffer[start] != '~')
193 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
201 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
203 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
206 /* If the first character of the current word is a tilde, perform
207 tilde expansion and insert the result. If not a tilde, do
209 if (rl_line_buffer[start] == '~')
211 len = end - start + 1;
212 temp = (char *)xmalloc (len + 1);
213 strncpy (temp, rl_line_buffer + start, len);
215 homedir = tilde_expand (temp);
218 _rl_replace_text (homedir, start, end);
225 #if defined (USE_VARARGS)
227 #if defined (PREFER_STDARG)
228 _rl_ttymsg (const char *format, ...)
230 _rl_ttymsg (va_alist)
235 #if defined (PREFER_VARARGS)
239 #if defined (PREFER_STDARG)
240 va_start (args, format);
243 format = va_arg (args, char *);
246 fprintf (stderr, "readline: ");
247 vfprintf (stderr, format, args);
248 fprintf (stderr, "\n");
253 rl_forced_update_display ();
257 #if defined (PREFER_STDARG)
258 _rl_errmsg (const char *format, ...)
260 _rl_errmsg (va_alist)
265 #if defined (PREFER_VARARGS)
269 #if defined (PREFER_STDARG)
270 va_start (args, format);
273 format = va_arg (args, char *);
276 fprintf (stderr, "readline: ");
277 vfprintf (stderr, format, args);
278 fprintf (stderr, "\n");
284 #else /* !USE_VARARGS */
286 _rl_ttymsg (format, arg1, arg2)
289 fprintf (stderr, "readline: ");
290 fprintf (stderr, format, arg1, arg2);
291 fprintf (stderr, "\n");
293 rl_forced_update_display ();
297 _rl_errmsg (format, arg1, arg2)
300 fprintf (stderr, "readline: ");
301 fprintf (stderr, format, arg1, arg2);
302 fprintf (stderr, "\n");
304 #endif /* !USE_VARARGS */
306 /* **************************************************************** */
308 /* String Utility Functions */
310 /* **************************************************************** */
312 /* Determine if s2 occurs in s1. If so, return a pointer to the
313 match in s1. The compare is case insensitive. */
315 _rl_strindex (s1, s2)
316 register const char *s1, *s2;
318 register int i, l, len;
320 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
321 if (_rl_strnicmp (s1 + i, s2, l) == 0)
322 return ((char *) (s1 + i));
323 return ((char *)NULL);
327 /* Find the first occurrence in STRING1 of any character from STRING2.
328 Return a pointer to the character in STRING1. */
330 _rl_strpbrk (string1, string2)
331 const char *string1, *string2;
333 register const char *scan;
334 #if defined (HANDLE_MULTIBYTE)
338 memset (&ps, 0, sizeof (mbstate_t));
341 for (; *string1; string1++)
343 for (scan = string2; *scan; scan++)
345 if (*string1 == *scan)
346 return ((char *)string1);
348 #if defined (HANDLE_MULTIBYTE)
349 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
351 v = _rl_get_char_len (string1, &ps);
353 string1 += v - 1; /* -1 to account for auto-increment in loop */
357 return ((char *)NULL);
361 #if !defined (HAVE_STRCASECMP)
362 /* Compare at most COUNT characters from string1 to string2. Case
365 _rl_strnicmp (string1, string2, count)
366 char *string1, *string2;
369 register char ch1, ch2;
375 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
383 /* strcmp (), but caseless. */
385 _rl_stricmp (string1, string2)
386 char *string1, *string2;
388 register char ch1, ch2;
390 while (*string1 && *string2)
394 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
397 return (*string1 - *string2);
399 #endif /* !HAVE_STRCASECMP */
401 /* Stupid comparison routine for qsort () ing strings. */
403 _rl_qsort_string_compare (s1, s2)
406 #if defined (HAVE_STRCOLL)
407 return (strcoll (*s1, *s2));
411 result = **s1 - **s2;
413 result = strcmp (*s1, *s2);
419 /* Function equivalents for the macros defined in chardefs.h. */
420 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
422 FUNCTION_FOR_MACRO (_rl_digit_p)
423 FUNCTION_FOR_MACRO (_rl_digit_value)
424 FUNCTION_FOR_MACRO (_rl_lowercase_p)
425 FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
426 FUNCTION_FOR_MACRO (_rl_to_lower)
427 FUNCTION_FOR_MACRO (_rl_to_upper)
428 FUNCTION_FOR_MACRO (_rl_uppercase_p)
430 /* A convenience function, to force memory deallocation to be performed
431 by readline. DLLs on Windows apparently require this. */
440 /* Backwards compatibility, now that savestring has been removed from
441 all `public' readline header files. */
442 #undef _rl_savestring
447 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
450 #if defined (USE_VARARGS)
451 static FILE *_rl_tracefp;
454 #if defined (PREFER_STDARG)
455 _rl_trace (const char *format, ...)
462 #if defined (PREFER_VARARGS)
466 #if defined (PREFER_STDARG)
467 va_start (args, format);
470 format = va_arg (args, char *);
473 if (_rl_tracefp == 0)
475 vfprintf (_rl_tracefp, format, args);
476 fprintf (_rl_tracefp, "\n");
477 fflush (_rl_tracefp);
488 fclose (_rl_tracefp);
489 sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid());
491 _rl_tracefp = fopen (fnbuf, "w+");
492 return _rl_tracefp != 0;
500 r = fclose (_rl_tracefp);