1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987-2010 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"
60 /* **************************************************************** */
62 /* Utility Functions */
64 /* **************************************************************** */
66 /* Return 0 if C is not a member of the class of characters that belong
67 in words, or 1 if it is. */
69 int _rl_allow_pathname_alphabetic_chars = 0;
70 static const char * const pathname_alphabetic_chars = "/-_=~.#$";
79 return (_rl_allow_pathname_alphabetic_chars &&
80 strchr (pathname_alphabetic_chars, c) != NULL);
83 #if defined (HANDLE_MULTIBYTE)
85 _rl_walphabetic (wchar_t wc)
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_null_function (count, key)
131 rl_tty_status (count, key)
134 #if defined (TIOCSTAT)
135 ioctl (1, TIOCSTAT, (char *)0);
136 rl_refresh_line (count, key);
143 /* Return a copy of the string between FROM and TO.
144 FROM is inclusive, TO is not. */
146 rl_copy_text (from, to)
152 /* Fix it if the caller is confused. */
157 copy = (char *)xmalloc (1 + length);
158 strncpy (copy, rl_line_buffer + from, length);
163 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
166 rl_extend_line_buffer (len)
169 while (len >= rl_line_buffer_len)
171 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
172 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
179 /* A function for simple tilde expansion. */
181 rl_tilde_expand (ignore, key)
184 register int start, end;
185 char *homedir, *temp;
191 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
193 homedir = tilde_expand ("~");
194 _rl_replace_text (homedir, start, end);
198 else if (rl_line_buffer[start] != '~')
200 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
208 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
210 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
213 /* If the first character of the current word is a tilde, perform
214 tilde expansion and insert the result. If not a tilde, do
216 if (rl_line_buffer[start] == '~')
218 len = end - start + 1;
219 temp = (char *)xmalloc (len + 1);
220 strncpy (temp, rl_line_buffer + start, len);
222 homedir = tilde_expand (temp);
225 _rl_replace_text (homedir, start, end);
232 #if defined (USE_VARARGS)
234 #if defined (PREFER_STDARG)
235 _rl_ttymsg (const char *format, ...)
237 _rl_ttymsg (va_alist)
242 #if defined (PREFER_VARARGS)
246 #if defined (PREFER_STDARG)
247 va_start (args, format);
250 format = va_arg (args, char *);
253 fprintf (stderr, "readline: ");
254 vfprintf (stderr, format, args);
255 fprintf (stderr, "\n");
260 rl_forced_update_display ();
264 #if defined (PREFER_STDARG)
265 _rl_errmsg (const char *format, ...)
267 _rl_errmsg (va_alist)
272 #if defined (PREFER_VARARGS)
276 #if defined (PREFER_STDARG)
277 va_start (args, format);
280 format = va_arg (args, char *);
283 fprintf (stderr, "readline: ");
284 vfprintf (stderr, format, args);
285 fprintf (stderr, "\n");
291 #else /* !USE_VARARGS */
293 _rl_ttymsg (format, arg1, arg2)
296 fprintf (stderr, "readline: ");
297 fprintf (stderr, format, arg1, arg2);
298 fprintf (stderr, "\n");
300 rl_forced_update_display ();
304 _rl_errmsg (format, arg1, arg2)
307 fprintf (stderr, "readline: ");
308 fprintf (stderr, format, arg1, arg2);
309 fprintf (stderr, "\n");
311 #endif /* !USE_VARARGS */
313 /* **************************************************************** */
315 /* String Utility Functions */
317 /* **************************************************************** */
319 /* Determine if s2 occurs in s1. If so, return a pointer to the
320 match in s1. The compare is case insensitive. */
322 _rl_strindex (s1, s2)
323 register const char *s1, *s2;
325 register int i, l, len;
327 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
328 if (_rl_strnicmp (s1 + i, s2, l) == 0)
329 return ((char *) (s1 + i));
330 return ((char *)NULL);
334 /* Find the first occurrence in STRING1 of any character from STRING2.
335 Return a pointer to the character in STRING1. */
337 _rl_strpbrk (string1, string2)
338 const char *string1, *string2;
340 register const char *scan;
341 #if defined (HANDLE_MULTIBYTE)
345 memset (&ps, 0, sizeof (mbstate_t));
348 for (; *string1; string1++)
350 for (scan = string2; *scan; scan++)
352 if (*string1 == *scan)
353 return ((char *)string1);
355 #if defined (HANDLE_MULTIBYTE)
356 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
358 v = _rl_get_char_len (string1, &ps);
360 string1 += v - 1; /* -1 to account for auto-increment in loop */
364 return ((char *)NULL);
368 #if !defined (HAVE_STRCASECMP)
369 /* Compare at most COUNT characters from string1 to string2. Case
370 doesn't matter (strncasecmp). */
372 _rl_strnicmp (string1, string2, count)
373 char *string1, *string2;
376 register char *s1, *s2;
379 if (count <= 0 || (string1 == string2))
386 d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */
393 while (--count != 0);
398 /* strcmp (), but caseless (strcasecmp). */
400 _rl_stricmp (string1, string2)
401 char *string1, *string2;
403 register char *s1, *s2;
412 while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
421 #endif /* !HAVE_STRCASECMP */
423 /* Stupid comparison routine for qsort () ing strings. */
425 _rl_qsort_string_compare (s1, s2)
428 #if defined (HAVE_STRCOLL)
429 return (strcoll (*s1, *s2));
433 result = **s1 - **s2;
435 result = strcmp (*s1, *s2);
441 /* Function equivalents for the macros defined in chardefs.h. */
442 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
444 FUNCTION_FOR_MACRO (_rl_digit_p)
445 FUNCTION_FOR_MACRO (_rl_digit_value)
446 FUNCTION_FOR_MACRO (_rl_lowercase_p)
447 FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
448 FUNCTION_FOR_MACRO (_rl_to_lower)
449 FUNCTION_FOR_MACRO (_rl_to_upper)
450 FUNCTION_FOR_MACRO (_rl_uppercase_p)
452 /* A convenience function, to force memory deallocation to be performed
453 by readline. DLLs on Windows apparently require this. */
462 /* Backwards compatibility, now that savestring has been removed from
463 all `public' readline header files. */
464 #undef _rl_savestring
469 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
472 #if defined (USE_VARARGS)
473 static FILE *_rl_tracefp;
476 #if defined (PREFER_STDARG)
477 _rl_trace (const char *format, ...)
484 #if defined (PREFER_VARARGS)
488 #if defined (PREFER_STDARG)
489 va_start (args, format);
492 format = va_arg (args, char *);
495 if (_rl_tracefp == 0)
497 vfprintf (_rl_tracefp, format, args);
498 fprintf (_rl_tracefp, "\n");
499 fflush (_rl_tracefp);
510 fclose (_rl_tracefp);
511 #if defined (_WIN32) && !defined (__CYGWIN__)
512 /* Windows doesn't have /var/tmp, so open the trace file in the
513 user's temporary directory instead. */
514 sprintf (fnbuf, "%s/rltrace.%ld",
515 (sh_get_env_value ("TEMP")
516 ? sh_get_env_value ("TEMP")
520 sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid());
523 _rl_tracefp = fopen (fnbuf, "w+");
524 return _rl_tracefp != 0;
532 r = fclose (_rl_tracefp);