Imported from ../bash-2.0.tar.gz.
[platform/upstream/bash.git] / lib / readline / util.c
1 /* util.c -- readline utility functions */
2
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
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.
12
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.
17
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
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <setjmp.h>
32 #include <ctype.h>
33
34 #if defined (HAVE_UNISTD_H)
35 #  include <unistd.h>           /* for _POSIX_VERSION */
36 #endif /* HAVE_UNISTD_H */
37
38 #if defined (HAVE_STDLIB_H)
39 #  include <stdlib.h>
40 #else
41 #  include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
43
44 /* System-specific feature definitions and include files. */
45 #include "rldefs.h"
46
47 #if defined (TIOCSTAT_IN_SYS_IOCTL)
48 #  include <sys/ioctl.h>
49 #endif /* TIOCSTAT_IN_SYS_IOCTL */
50
51 /* Some standard library routines. */
52 #include "readline.h"
53
54 #define SWAP(s, e)  do { int t; t = s; s = e; e = t; } while (0)
55
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;
61
62 extern int _rl_defining_kbd_macro;
63 extern char *_rl_executing_macro;
64
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 ();
69
70 extern char *xmalloc (), *xrealloc ();
71
72 /* **************************************************************** */
73 /*                                                                  */
74 /*                      Utility Functions                           */
75 /*                                                                  */
76 /* **************************************************************** */
77
78 /* Return 0 if C is not a member of the class of characters that belong
79    in words, or 1 if it is. */
80
81 int _rl_allow_pathname_alphabetic_chars = 0;
82 static char *pathname_alphabetic_chars = "/-_=~.#$";
83
84 int
85 alphabetic (c)
86      int c;
87 {
88   if (ALPHABETIC (c))
89     return (1);
90
91   return (_rl_allow_pathname_alphabetic_chars &&
92             strchr (pathname_alphabetic_chars, c) != NULL);
93 }
94
95 /* How to abort things. */
96 int
97 _rl_abort_internal ()
98 {
99   ding ();
100   rl_clear_message ();
101   _rl_init_argument ();
102   rl_pending_input = 0;
103
104   _rl_defining_kbd_macro = 0;
105   while (_rl_executing_macro)
106     _rl_pop_executing_macro ();
107
108   rl_last_func = (Function *)NULL;
109   longjmp (readline_top_level, 1);
110   return (0);
111 }
112
113 int
114 rl_abort (count, key)
115      int count, key;
116 {
117   return (_rl_abort_internal ());
118 }
119
120 int
121 rl_tty_status (count, key)
122      int count, key;
123 {
124 #if defined (TIOCSTAT)
125   ioctl (1, TIOCSTAT, (char *)0);
126   rl_refresh_line ();
127 #else
128   ding ();
129 #endif
130   return 0;
131 }
132
133 /* Return a copy of the string between FROM and TO.
134    FROM is inclusive, TO is not. */
135 char *
136 rl_copy_text (from, to)
137      int from, to;
138 {
139   register int length;
140   char *copy;
141
142   /* Fix it if the caller is confused. */
143   if (from > to)
144     SWAP (from, to);
145
146   length = to - from;
147   copy = xmalloc (1 + length);
148   strncpy (copy, rl_line_buffer + from, length);
149   copy[length] = '\0';
150   return (copy);
151 }
152
153 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
154    LEN characters. */
155 void
156 rl_extend_line_buffer (len)
157      int len;
158 {
159   while (len >= rl_line_buffer_len)
160     {
161       rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
162       rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
163     }
164
165   _rl_set_the_line ();
166 }
167
168 /* **************************************************************** */
169 /*                                                                  */
170 /*                      String Utility Functions                    */
171 /*                                                                  */
172 /* **************************************************************** */
173
174 /* Determine if s2 occurs in s1.  If so, return a pointer to the
175    match in s1.  The compare is case insensitive. */
176 char *
177 _rl_strindex (s1, s2)
178      register char *s1, *s2;
179 {
180   register int i, l, len;
181
182   for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
183     if (_rl_strnicmp (s1 + i, s2, l) == 0)
184       return (s1 + i);
185   return ((char *)NULL);
186 }
187
188 #if !defined (HAVE_STRCASECMP)
189 /* Compare at most COUNT characters from string1 to string2.  Case
190    doesn't matter. */
191 int
192 _rl_strnicmp (string1, string2, count)
193      char *string1, *string2;
194      int count;
195 {
196   register char ch1, ch2;
197
198   while (count)
199     {
200       ch1 = *string1++;
201       ch2 = *string2++;
202       if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
203         count--;
204       else
205         break;
206     }
207   return (count);
208 }
209
210 /* strcmp (), but caseless. */
211 int
212 _rl_stricmp (string1, string2)
213      char *string1, *string2;
214 {
215   register char ch1, ch2;
216
217   while (*string1 && *string2)
218     {
219       ch1 = *string1++;
220       ch2 = *string2++;
221       if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
222         return (1);
223     }
224   return (*string1 - *string2);
225 }
226 #endif /* !HAVE_STRCASECMP */
227
228 /* Stupid comparison routine for qsort () ing strings. */
229 int
230 _rl_qsort_string_compare (s1, s2)
231   char **s1, **s2;
232 {
233 #if defined (HAVE_STRCOLL)
234   return (strcoll (*s1, *s2));
235 #else
236   int result;
237
238   result = **s1 - **s2;
239   if (result == 0)
240     result = strcmp (*s1, *s2);
241
242   return result;
243 #endif
244 }
245
246 #if !defined (SHELL)
247 #ifdef savestring
248 #undef savestring
249 #endif
250 /* Backwards compatibility, now that savestring has been removed from
251    all `public' readline header files. */
252 char *
253 savestring (s)
254      char *s;
255 {
256   return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
257 }
258 #endif /* !SHELL */
259
260 /* Function equivalents for the macros defined in chartypes.h. */
261 #undef _rl_uppercase_p
262 int
263 _rl_uppercase_p (c)
264      int c;
265 {
266   return (isupper (c));
267 }
268
269 #undef _rl_lowercase_p
270 int
271 _rl_lowercase_p (c)
272      int c;
273 {
274   return (islower (c));
275 }
276
277 #undef _rl_pure_alphabetic
278 int
279 _rl_pure_alphabetic (c)
280      int c;
281 {
282   return (isupper (c) || islower (c));
283 }
284
285 #undef _rl_digit_p
286 int
287 _rl_digit_p (c)
288      int c;
289 {
290   return (isdigit (c));
291 }
292
293 #undef _rl_to_lower
294 int
295 _rl_to_lower (c)
296      int c;
297 {
298   return (isupper (c) ? tolower (c) : c);
299 }
300
301 #undef _rl_to_upper
302 int
303 _rl_to_upper (c)
304      int c;
305 {
306   return (islower (c) ? toupper (c) : c);
307 }
308
309 #undef _rl_digit_value
310 int
311 _rl_digit_value (c)
312      int c;
313 {
314   return (isdigit (c) ? c - '0' : c);
315 }