2 /* util.c -- readline utility functions */
4 /* Copyright (C) 1987-2005 Free Software Foundation, Inc.
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
9 The GNU Readline Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2, or
12 (at your option) any later version.
14 The GNU Readline Library is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 The GNU General Public License is often shipped with GNU software, and
20 is generally kept in a file called COPYING or LICENSE. If you do not
21 have a copy of the license, write to the Free Software Foundation,
22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #define READLINE_LIBRARY
25 #if defined (HAVE_CONFIG_H)
29 #include <sys/types.h>
33 #if defined (HAVE_UNISTD_H)
34 # include <unistd.h> /* for _POSIX_VERSION */
35 #endif /* HAVE_UNISTD_H */
37 #if defined (HAVE_STDLIB_H)
40 # include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
46 /* System-specific feature definitions and include files. */
50 #if defined (TIOCSTAT_IN_SYS_IOCTL)
51 # include <sys/ioctl.h>
52 #endif /* TIOCSTAT_IN_SYS_IOCTL */
54 /* Some standard library routines. */
57 #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 *pathname_alphabetic_chars = "/-_=~.#$";
79 return (_rl_allow_pathname_alphabetic_chars &&
80 strchr (pathname_alphabetic_chars, c) != NULL);
83 #if defined (HANDLE_MULTIBYTE)
94 return (_rl_allow_pathname_alphabetic_chars &&
95 strchr (pathname_alphabetic_chars, c) != NULL);
99 /* How to abort things. */
101 _rl_abort_internal ()
105 _rl_reset_argument ();
106 rl_clear_pending_input ();
108 RL_UNSETSTATE (RL_STATE_MACRODEF);
109 while (rl_executing_macro)
110 _rl_pop_executing_macro ();
112 rl_last_func = (rl_command_func_t *)NULL;
113 longjmp (readline_top_level, 1);
118 rl_abort (count, key)
121 return (_rl_abort_internal ());
125 rl_tty_status (count, key)
128 #if defined (TIOCSTAT)
129 ioctl (1, TIOCSTAT, (char *)0);
130 rl_refresh_line (count, key);
137 /* Return a copy of the string between FROM and TO.
138 FROM is inclusive, TO is not. */
140 rl_copy_text (from, to)
146 /* Fix it if the caller is confused. */
151 copy = (char *)xmalloc (1 + length);
152 strncpy (copy, rl_line_buffer + from, length);
157 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
160 rl_extend_line_buffer (len)
163 while (len >= rl_line_buffer_len)
165 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
166 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
173 /* A function for simple tilde expansion. */
175 rl_tilde_expand (ignore, key)
178 register int start, end;
179 char *homedir, *temp;
185 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
187 homedir = tilde_expand ("~");
188 _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);
224 /* **************************************************************** */
226 /* String Utility Functions */
228 /* **************************************************************** */
230 /* Determine if s2 occurs in s1. If so, return a pointer to the
231 match in s1. The compare is case insensitive. */
233 _rl_strindex (s1, s2)
234 register const char *s1, *s2;
236 register int i, l, len;
238 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
239 if (_rl_strnicmp (s1 + i, s2, l) == 0)
240 return ((char *) (s1 + i));
241 return ((char *)NULL);
245 /* Find the first occurrence in STRING1 of any character from STRING2.
246 Return a pointer to the character in STRING1. */
248 _rl_strpbrk (string1, string2)
249 const char *string1, *string2;
251 register const char *scan;
252 #if defined (HANDLE_MULTIBYTE)
256 memset (&ps, 0, sizeof (mbstate_t));
259 for (; *string1; string1++)
261 for (scan = string2; *scan; scan++)
263 if (*string1 == *scan)
264 return ((char *)string1);
266 #if defined (HANDLE_MULTIBYTE)
267 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
269 v = _rl_get_char_len (string1, &ps);
271 string1 += v - 1; /* -1 to account for auto-increment in loop */
275 return ((char *)NULL);
279 #if !defined (HAVE_STRCASECMP)
280 /* Compare at most COUNT characters from string1 to string2. Case
283 _rl_strnicmp (string1, string2, count)
284 char *string1, *string2;
287 register char ch1, ch2;
293 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
301 /* strcmp (), but caseless. */
303 _rl_stricmp (string1, string2)
304 char *string1, *string2;
306 register char ch1, ch2;
308 while (*string1 && *string2)
312 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
315 return (*string1 - *string2);
317 #endif /* !HAVE_STRCASECMP */
319 /* Stupid comparison routine for qsort () ing strings. */
321 _rl_qsort_string_compare (s1, s2)
324 #if defined (HAVE_STRCOLL)
325 return (strcoll (*s1, *s2));
329 result = **s1 - **s2;
331 result = strcmp (*s1, *s2);
337 /* Function equivalents for the macros defined in chardefs.h. */
338 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
340 FUNCTION_FOR_MACRO (_rl_digit_p)
341 FUNCTION_FOR_MACRO (_rl_digit_value)
342 FUNCTION_FOR_MACRO (_rl_lowercase_p)
343 FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
344 FUNCTION_FOR_MACRO (_rl_to_lower)
345 FUNCTION_FOR_MACRO (_rl_to_upper)
346 FUNCTION_FOR_MACRO (_rl_uppercase_p)
348 /* Backwards compatibility, now that savestring has been removed from
349 all `public' readline header files. */
350 #undef _rl_savestring
355 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));