1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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. */
48 #if defined (TIOCSTAT_IN_SYS_IOCTL)
49 # include <sys/ioctl.h>
50 #endif /* TIOCSTAT_IN_SYS_IOCTL */
52 /* Some standard library routines. */
55 #include "rlprivate.h"
58 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
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 char *pathname_alphabetic_chars = "/-_=~.#$";
79 return (_rl_allow_pathname_alphabetic_chars &&
80 strchr (pathname_alphabetic_chars, c) != NULL);
83 /* How to abort things. */
92 _rl_defining_kbd_macro = 0;
93 while (_rl_executing_macro)
94 _rl_pop_executing_macro ();
96 rl_last_func = (Function *)NULL;
97 longjmp (readline_top_level, 1);
102 rl_abort (count, key)
105 return (_rl_abort_internal ());
109 rl_tty_status (count, key)
112 #if defined (TIOCSTAT)
113 ioctl (1, TIOCSTAT, (char *)0);
114 rl_refresh_line (count, key);
121 /* Return a copy of the string between FROM and TO.
122 FROM is inclusive, TO is not. */
124 rl_copy_text (from, to)
130 /* Fix it if the caller is confused. */
135 copy = xmalloc (1 + length);
136 strncpy (copy, rl_line_buffer + from, length);
141 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
144 rl_extend_line_buffer (len)
147 while (len >= rl_line_buffer_len)
149 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
150 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
157 /* A function for simple tilde expansion. */
159 rl_tilde_expand (ignore, key)
162 register int start, end;
163 char *homedir, *temp;
169 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
171 homedir = tilde_expand ("~");
172 _rl_replace_text (homedir, start, end);
175 else if (rl_line_buffer[start] != '~')
177 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
185 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
187 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
190 /* If the first character of the current word is a tilde, perform
191 tilde expansion and insert the result. If not a tilde, do
193 if (rl_line_buffer[start] == '~')
195 len = end - start + 1;
196 temp = xmalloc (len + 1);
197 strncpy (temp, rl_line_buffer + start, len);
199 homedir = tilde_expand (temp);
202 _rl_replace_text (homedir, start, end);
208 /* **************************************************************** */
210 /* String Utility Functions */
212 /* **************************************************************** */
214 /* Determine if s2 occurs in s1. If so, return a pointer to the
215 match in s1. The compare is case insensitive. */
217 _rl_strindex (s1, s2)
218 register char *s1, *s2;
220 register int i, l, len;
222 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
223 if (_rl_strnicmp (s1 + i, s2, l) == 0)
225 return ((char *)NULL);
228 #if !defined (HAVE_STRCASECMP)
229 /* Compare at most COUNT characters from string1 to string2. Case
232 _rl_strnicmp (string1, string2, count)
233 char *string1, *string2;
236 register char ch1, ch2;
242 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
250 /* strcmp (), but caseless. */
252 _rl_stricmp (string1, string2)
253 char *string1, *string2;
255 register char ch1, ch2;
257 while (*string1 && *string2)
261 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
264 return (*string1 - *string2);
266 #endif /* !HAVE_STRCASECMP */
268 /* Stupid comparison routine for qsort () ing strings. */
270 _rl_qsort_string_compare (s1, s2)
273 #if defined (HAVE_STRCOLL)
274 return (strcoll (*s1, *s2));
278 result = **s1 - **s2;
280 result = strcmp (*s1, *s2);
286 /* Function equivalents for the macros defined in chartypes.h. */
287 #undef _rl_uppercase_p
292 return (isupper (c));
295 #undef _rl_lowercase_p
300 return (islower (c));
303 #undef _rl_pure_alphabetic
305 _rl_pure_alphabetic (c)
308 return (isupper (c) || islower (c));
316 return (isdigit (c));
324 return (isupper (c) ? tolower (c) : c);
332 return (islower (c) ? toupper (c) : c);
335 #undef _rl_digit_value
340 return (isdigit (c) ? c - '0' : c);
343 /* Backwards compatibility, now that savestring has been removed from
344 all `public' readline header files. */
345 #undef _rl_savestring
350 return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));