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)
29 #include <sys/types.h>
34 #if defined (HAVE_UNISTD_H)
35 # include <unistd.h> /* for _POSIX_VERSION */
36 #endif /* HAVE_UNISTD_H */
38 #if defined (HAVE_STDLIB_H)
41 # include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
44 /* System-specific feature definitions and include files. */
47 #if defined (TIOCSTAT_IN_SYS_IOCTL)
48 # include <sys/ioctl.h>
49 #endif /* TIOCSTAT_IN_SYS_IOCTL */
51 /* Some standard library routines. */
54 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
56 /* Pseudo-globals imported from readline.c */
57 extern int readline_echoing_p;
58 extern jmp_buf readline_top_level;
59 extern int rl_line_buffer_len;
60 extern Function *rl_last_func;
62 extern int _rl_defining_kbd_macro;
63 extern char *_rl_executing_macro;
65 /* Pseudo-global functions imported from other library files. */
66 extern void _rl_pop_executing_macro ();
67 extern void _rl_set_the_line ();
68 extern void _rl_init_argument ();
70 extern char *xmalloc (), *xrealloc ();
72 /* **************************************************************** */
74 /* Utility Functions */
76 /* **************************************************************** */
78 /* Return 0 if C is not a member of the class of characters that belong
79 in words, or 1 if it is. */
81 int _rl_allow_pathname_alphabetic_chars = 0;
82 static char *pathname_alphabetic_chars = "/-_=~.#$";
91 return (_rl_allow_pathname_alphabetic_chars &&
92 strchr (pathname_alphabetic_chars, c) != NULL);
95 /* How to abort things. */
101 _rl_init_argument ();
102 rl_pending_input = 0;
104 _rl_defining_kbd_macro = 0;
105 while (_rl_executing_macro)
106 _rl_pop_executing_macro ();
108 rl_last_func = (Function *)NULL;
109 longjmp (readline_top_level, 1);
114 rl_abort (count, key)
117 return (_rl_abort_internal ());
121 rl_tty_status (count, key)
124 #if defined (TIOCSTAT)
125 ioctl (1, TIOCSTAT, (char *)0);
133 /* Return a copy of the string between FROM and TO.
134 FROM is inclusive, TO is not. */
136 rl_copy_text (from, to)
142 /* Fix it if the caller is confused. */
147 copy = xmalloc (1 + length);
148 strncpy (copy, rl_line_buffer + from, length);
153 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
156 rl_extend_line_buffer (len)
159 while (len >= rl_line_buffer_len)
161 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
162 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
168 /* **************************************************************** */
170 /* String Utility Functions */
172 /* **************************************************************** */
174 /* Determine if s2 occurs in s1. If so, return a pointer to the
175 match in s1. The compare is case insensitive. */
177 _rl_strindex (s1, s2)
178 register char *s1, *s2;
180 register int i, l, len;
182 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
183 if (_rl_strnicmp (s1 + i, s2, l) == 0)
185 return ((char *)NULL);
188 #if !defined (HAVE_STRCASECMP)
189 /* Compare at most COUNT characters from string1 to string2. Case
192 _rl_strnicmp (string1, string2, count)
193 char *string1, *string2;
196 register char ch1, ch2;
202 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
210 /* strcmp (), but caseless. */
212 _rl_stricmp (string1, string2)
213 char *string1, *string2;
215 register char ch1, ch2;
217 while (*string1 && *string2)
221 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
224 return (*string1 - *string2);
226 #endif /* !HAVE_STRCASECMP */
228 /* Stupid comparison routine for qsort () ing strings. */
230 _rl_qsort_string_compare (s1, s2)
233 #if defined (HAVE_STRCOLL)
234 return (strcoll (*s1, *s2));
238 result = **s1 - **s2;
240 result = strcmp (*s1, *s2);
250 /* Backwards compatibility, now that savestring has been removed from
251 all `public' readline header files. */
256 return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
260 /* Function equivalents for the macros defined in chartypes.h. */
261 #undef _rl_uppercase_p
266 return (isupper (c));
269 #undef _rl_lowercase_p
274 return (islower (c));
277 #undef _rl_pure_alphabetic
279 _rl_pure_alphabetic (c)
282 return (isupper (c) || islower (c));
290 return (isdigit (c));
298 return (isupper (c) ? tolower (c) : c);
306 return (islower (c) ? toupper (c) : c);
309 #undef _rl_digit_value
314 return (isdigit (c) ? c - '0' : c);