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 1, 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 675 Mass Ave, Cambridge, MA 02139, 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 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
57 /* Pseudo-globals imported from readline.c */
58 extern int readline_echoing_p;
59 extern procenv_t readline_top_level;
60 extern int rl_line_buffer_len;
61 extern Function *rl_last_func;
63 extern int _rl_defining_kbd_macro;
64 extern char *_rl_executing_macro;
66 /* Pseudo-global functions imported from other library files. */
67 extern void _rl_pop_executing_macro ();
68 extern void _rl_set_the_line ();
69 extern void _rl_init_argument ();
71 extern char *xmalloc (), *xrealloc ();
73 /* **************************************************************** */
75 /* Utility Functions */
77 /* **************************************************************** */
79 /* Return 0 if C is not a member of the class of characters that belong
80 in words, or 1 if it is. */
82 int _rl_allow_pathname_alphabetic_chars = 0;
83 static char *pathname_alphabetic_chars = "/-_=~.#$";
92 return (_rl_allow_pathname_alphabetic_chars &&
93 strchr (pathname_alphabetic_chars, c) != NULL);
96 /* How to abort things. */
102 _rl_init_argument ();
103 rl_pending_input = 0;
105 _rl_defining_kbd_macro = 0;
106 while (_rl_executing_macro)
107 _rl_pop_executing_macro ();
109 rl_last_func = (Function *)NULL;
110 longjmp (readline_top_level, 1);
115 rl_abort (count, key)
118 return (_rl_abort_internal ());
122 rl_tty_status (count, key)
125 #if defined (TIOCSTAT)
126 ioctl (1, TIOCSTAT, (char *)0);
134 /* Return a copy of the string between FROM and TO.
135 FROM is inclusive, TO is not. */
137 rl_copy_text (from, to)
143 /* Fix it if the caller is confused. */
148 copy = xmalloc (1 + length);
149 strncpy (copy, rl_line_buffer + from, length);
154 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
157 rl_extend_line_buffer (len)
160 while (len >= rl_line_buffer_len)
162 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
163 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
170 /* A function for simple tilde expansion. */
172 rl_tilde_expand (ignore, key)
175 register int start, end;
176 char *homedir, *temp;
182 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
184 homedir = tilde_expand ("~");
185 _rl_replace_text (homedir, start, end);
188 else if (rl_line_buffer[start] != '~')
190 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
198 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
200 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
203 /* If the first character of the current word is a tilde, perform
204 tilde expansion and insert the result. If not a tilde, do
206 if (rl_line_buffer[start] == '~')
208 len = end - start + 1;
209 temp = xmalloc (len + 1);
210 strncpy (temp, rl_line_buffer + start, len);
212 homedir = tilde_expand (temp);
215 _rl_replace_text (homedir, start, end);
221 /* **************************************************************** */
223 /* String Utility Functions */
225 /* **************************************************************** */
227 /* Determine if s2 occurs in s1. If so, return a pointer to the
228 match in s1. The compare is case insensitive. */
230 _rl_strindex (s1, s2)
231 register char *s1, *s2;
233 register int i, l, len;
235 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
236 if (_rl_strnicmp (s1 + i, s2, l) == 0)
238 return ((char *)NULL);
241 #if !defined (HAVE_STRCASECMP)
242 /* Compare at most COUNT characters from string1 to string2. Case
245 _rl_strnicmp (string1, string2, count)
246 char *string1, *string2;
249 register char ch1, ch2;
255 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
263 /* strcmp (), but caseless. */
265 _rl_stricmp (string1, string2)
266 char *string1, *string2;
268 register char ch1, ch2;
270 while (*string1 && *string2)
274 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
277 return (*string1 - *string2);
279 #endif /* !HAVE_STRCASECMP */
281 /* Stupid comparison routine for qsort () ing strings. */
283 _rl_qsort_string_compare (s1, s2)
286 #if defined (HAVE_STRCOLL)
287 return (strcoll (*s1, *s2));
291 result = **s1 - **s2;
293 result = strcmp (*s1, *s2);
299 /* Function equivalents for the macros defined in chartypes.h. */
300 #undef _rl_uppercase_p
305 return (isupper (c));
308 #undef _rl_lowercase_p
313 return (islower (c));
316 #undef _rl_pure_alphabetic
318 _rl_pure_alphabetic (c)
321 return (isupper (c) || islower (c));
329 return (isdigit (c));
337 return (isupper (c) ? tolower (c) : c);
345 return (islower (c) ? toupper (c) : c);
348 #undef _rl_digit_value
353 return (isdigit (c) ? c - '0' : c);
356 /* Backwards compatibility, now that savestring has been removed from
357 all `public' readline header files. */
358 #undef _rl_savestring
363 return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));