Deleted Added
full compact
util.c (58314) util.c (75409)
1/* $FreeBSD: head/contrib/libreadline/util.c 58314 2000-03-19 22:00:57Z ache $ */
1/* $FreeBSD: head/contrib/libreadline/util.c 75409 2001-04-11 03:15:56Z ache $ */
2/* util.c -- readline utility functions */
3
4/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
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.
13
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.
18
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
24
25#if defined (HAVE_CONFIG_H)
26# include <config.h>
27#endif
28
29#include <sys/types.h>
30#include <fcntl.h>
31#include "posixjmp.h"
32
33#if defined (HAVE_UNISTD_H)
34# include <unistd.h> /* for _POSIX_VERSION */
35#endif /* HAVE_UNISTD_H */
36
37#if defined (HAVE_STDLIB_H)
38# include <stdlib.h>
39#else
40# include "ansi_stdlib.h"
41#endif /* HAVE_STDLIB_H */
42
43#include <stdio.h>
44#include <ctype.h>
45
46/* System-specific feature definitions and include files. */
47#include "rldefs.h"
48
49#if defined (TIOCSTAT_IN_SYS_IOCTL)
50# include <sys/ioctl.h>
51#endif /* TIOCSTAT_IN_SYS_IOCTL */
52
53/* Some standard library routines. */
54#include "readline.h"
55
56#include "rlprivate.h"
57#include "xmalloc.h"
58
59#define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
60
61/* **************************************************************** */
62/* */
63/* Utility Functions */
64/* */
65/* **************************************************************** */
66
67/* Return 0 if C is not a member of the class of characters that belong
68 in words, or 1 if it is. */
69
70int _rl_allow_pathname_alphabetic_chars = 0;
2/* util.c -- readline utility functions */
3
4/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6 This file is part of the GNU Readline Library, a library for
7 reading lines of text with interactive input and history editing.
8
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.
13
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.
18
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
24
25#if defined (HAVE_CONFIG_H)
26# include <config.h>
27#endif
28
29#include <sys/types.h>
30#include <fcntl.h>
31#include "posixjmp.h"
32
33#if defined (HAVE_UNISTD_H)
34# include <unistd.h> /* for _POSIX_VERSION */
35#endif /* HAVE_UNISTD_H */
36
37#if defined (HAVE_STDLIB_H)
38# include <stdlib.h>
39#else
40# include "ansi_stdlib.h"
41#endif /* HAVE_STDLIB_H */
42
43#include <stdio.h>
44#include <ctype.h>
45
46/* System-specific feature definitions and include files. */
47#include "rldefs.h"
48
49#if defined (TIOCSTAT_IN_SYS_IOCTL)
50# include <sys/ioctl.h>
51#endif /* TIOCSTAT_IN_SYS_IOCTL */
52
53/* Some standard library routines. */
54#include "readline.h"
55
56#include "rlprivate.h"
57#include "xmalloc.h"
58
59#define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
60
61/* **************************************************************** */
62/* */
63/* Utility Functions */
64/* */
65/* **************************************************************** */
66
67/* Return 0 if C is not a member of the class of characters that belong
68 in words, or 1 if it is. */
69
70int _rl_allow_pathname_alphabetic_chars = 0;
71static char *pathname_alphabetic_chars = "/-_=~.#$";
71static const char *pathname_alphabetic_chars = "/-_=~.#$";
72
73int
72
73int
74alphabetic (c)
74rl_alphabetic (c)
75 int c;
76{
77 if (ALPHABETIC (c))
78 return (1);
79
80 return (_rl_allow_pathname_alphabetic_chars &&
81 strchr (pathname_alphabetic_chars, c) != NULL);
82}
83
84/* How to abort things. */
85int
86_rl_abort_internal ()
87{
75 int c;
76{
77 if (ALPHABETIC (c))
78 return (1);
79
80 return (_rl_allow_pathname_alphabetic_chars &&
81 strchr (pathname_alphabetic_chars, c) != NULL);
82}
83
84/* How to abort things. */
85int
86_rl_abort_internal ()
87{
88 ding ();
88 rl_ding ();
89 rl_clear_message ();
90 _rl_init_argument ();
89 rl_clear_message ();
90 _rl_init_argument ();
91 rl_pending_input = 0;
91 rl_clear_pending_input ();
92
93 _rl_defining_kbd_macro = 0;
92
93 _rl_defining_kbd_macro = 0;
94 while (_rl_executing_macro)
94 while (rl_executing_macro)
95 _rl_pop_executing_macro ();
96
95 _rl_pop_executing_macro ();
96
97 rl_last_func = (Function *)NULL;
97 rl_last_func = (rl_command_func_t *)NULL;
98 longjmp (readline_top_level, 1);
99 return (0);
100}
101
102int
103rl_abort (count, key)
104 int count, key;
105{
106 return (_rl_abort_internal ());
107}
108
109int
110rl_tty_status (count, key)
111 int count, key;
112{
113#if defined (TIOCSTAT)
114 ioctl (1, TIOCSTAT, (char *)0);
115 rl_refresh_line (count, key);
116#else
98 longjmp (readline_top_level, 1);
99 return (0);
100}
101
102int
103rl_abort (count, key)
104 int count, key;
105{
106 return (_rl_abort_internal ());
107}
108
109int
110rl_tty_status (count, key)
111 int count, key;
112{
113#if defined (TIOCSTAT)
114 ioctl (1, TIOCSTAT, (char *)0);
115 rl_refresh_line (count, key);
116#else
117 ding ();
117 rl_ding ();
118#endif
119 return 0;
120}
121
122/* Return a copy of the string between FROM and TO.
123 FROM is inclusive, TO is not. */
124char *
125rl_copy_text (from, to)
126 int from, to;
127{
128 register int length;
129 char *copy;
130
131 /* Fix it if the caller is confused. */
132 if (from > to)
133 SWAP (from, to);
134
135 length = to - from;
136 copy = xmalloc (1 + length);
137 strncpy (copy, rl_line_buffer + from, length);
138 copy[length] = '\0';
139 return (copy);
140}
141
142/* Increase the size of RL_LINE_BUFFER until it has enough space to hold
143 LEN characters. */
144void
145rl_extend_line_buffer (len)
146 int len;
147{
148 while (len >= rl_line_buffer_len)
149 {
150 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
151 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
152 }
153
154 _rl_set_the_line ();
155}
156
157
158/* A function for simple tilde expansion. */
159int
160rl_tilde_expand (ignore, key)
161 int ignore, key;
162{
163 register int start, end;
164 char *homedir, *temp;
165 int len;
166
167 end = rl_point;
168 start = end - 1;
169
170 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
171 {
172 homedir = tilde_expand ("~");
173 _rl_replace_text (homedir, start, end);
174 return (0);
175 }
176 else if (rl_line_buffer[start] != '~')
177 {
178 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
179 ;
180 start++;
181 }
182
183 end = start;
184 do
185 end++;
186 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
187
188 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
189 end--;
190
191 /* If the first character of the current word is a tilde, perform
192 tilde expansion and insert the result. If not a tilde, do
193 nothing. */
194 if (rl_line_buffer[start] == '~')
195 {
196 len = end - start + 1;
197 temp = xmalloc (len + 1);
198 strncpy (temp, rl_line_buffer + start, len);
199 temp[len] = '\0';
200 homedir = tilde_expand (temp);
201 free (temp);
202
203 _rl_replace_text (homedir, start, end);
204 }
205
206 return (0);
207}
208
209/* **************************************************************** */
210/* */
211/* String Utility Functions */
212/* */
213/* **************************************************************** */
214
215/* Determine if s2 occurs in s1. If so, return a pointer to the
216 match in s1. The compare is case insensitive. */
217char *
218_rl_strindex (s1, s2)
118#endif
119 return 0;
120}
121
122/* Return a copy of the string between FROM and TO.
123 FROM is inclusive, TO is not. */
124char *
125rl_copy_text (from, to)
126 int from, to;
127{
128 register int length;
129 char *copy;
130
131 /* Fix it if the caller is confused. */
132 if (from > to)
133 SWAP (from, to);
134
135 length = to - from;
136 copy = xmalloc (1 + length);
137 strncpy (copy, rl_line_buffer + from, length);
138 copy[length] = '\0';
139 return (copy);
140}
141
142/* Increase the size of RL_LINE_BUFFER until it has enough space to hold
143 LEN characters. */
144void
145rl_extend_line_buffer (len)
146 int len;
147{
148 while (len >= rl_line_buffer_len)
149 {
150 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
151 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
152 }
153
154 _rl_set_the_line ();
155}
156
157
158/* A function for simple tilde expansion. */
159int
160rl_tilde_expand (ignore, key)
161 int ignore, key;
162{
163 register int start, end;
164 char *homedir, *temp;
165 int len;
166
167 end = rl_point;
168 start = end - 1;
169
170 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
171 {
172 homedir = tilde_expand ("~");
173 _rl_replace_text (homedir, start, end);
174 return (0);
175 }
176 else if (rl_line_buffer[start] != '~')
177 {
178 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
179 ;
180 start++;
181 }
182
183 end = start;
184 do
185 end++;
186 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
187
188 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
189 end--;
190
191 /* If the first character of the current word is a tilde, perform
192 tilde expansion and insert the result. If not a tilde, do
193 nothing. */
194 if (rl_line_buffer[start] == '~')
195 {
196 len = end - start + 1;
197 temp = xmalloc (len + 1);
198 strncpy (temp, rl_line_buffer + start, len);
199 temp[len] = '\0';
200 homedir = tilde_expand (temp);
201 free (temp);
202
203 _rl_replace_text (homedir, start, end);
204 }
205
206 return (0);
207}
208
209/* **************************************************************** */
210/* */
211/* String Utility Functions */
212/* */
213/* **************************************************************** */
214
215/* Determine if s2 occurs in s1. If so, return a pointer to the
216 match in s1. The compare is case insensitive. */
217char *
218_rl_strindex (s1, s2)
219 register char *s1, *s2;
219 register const char *s1, *s2;
220{
221 register int i, l, len;
222
223 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
224 if (_rl_strnicmp (s1 + i, s2, l) == 0)
220{
221 register int i, l, len;
222
223 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
224 if (_rl_strnicmp (s1 + i, s2, l) == 0)
225 return (s1 + i);
225 return ((char *) (s1 + i));
226 return ((char *)NULL);
227}
228
226 return ((char *)NULL);
227}
228
229/* Find the first occurrence in STRING1 of any character from STRING2.
230 Return a pointer to the character in STRING1. */
231char *
232_rl_strpbrk (string1, string2)
233 const char *string1, *string2;
234{
235 register const char *scan;
236
237 if (string2 == NULL)
238 return ((char *)NULL);
239
240 for (; *string1; string1++)
241 {
242 for (scan = string2; *scan; scan++)
243 {
244 if (*string1 == *scan)
245 return ((char *)string1);
246 }
247 }
248 return ((char *)NULL);
249}
250
229#if !defined (HAVE_STRCASECMP)
230/* Compare at most COUNT characters from string1 to string2. Case
231 doesn't matter. */
232int
233_rl_strnicmp (string1, string2, count)
234 char *string1, *string2;
235 int count;
236{
237 register char ch1, ch2;
238
239 while (count)
240 {
241 ch1 = *string1++;
242 ch2 = *string2++;
243 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
244 count--;
245 else
246 break;
247 }
248 return (count);
249}
250
251/* strcmp (), but caseless. */
252int
253_rl_stricmp (string1, string2)
254 char *string1, *string2;
255{
256 register char ch1, ch2;
257
258 while (*string1 && *string2)
259 {
260 ch1 = *string1++;
261 ch2 = *string2++;
262 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
263 return (1);
264 }
265 return (*string1 - *string2);
266}
267#endif /* !HAVE_STRCASECMP */
268
269/* Stupid comparison routine for qsort () ing strings. */
270int
271_rl_qsort_string_compare (s1, s2)
272 char **s1, **s2;
273{
274#if defined (HAVE_STRCOLL)
275 return (strcoll (*s1, *s2));
276#else
277 int result;
278
279 result = **s1 - **s2;
280 if (result == 0)
281 result = strcmp (*s1, *s2);
282
283 return result;
284#endif
285}
286
287/* Function equivalents for the macros defined in chartypes.h. */
288#undef _rl_uppercase_p
289int
290_rl_uppercase_p (c)
291 int c;
292{
293 return (isupper (c));
294}
295
296#undef _rl_lowercase_p
297int
298_rl_lowercase_p (c)
299 int c;
300{
301 return (islower (c));
302}
303
304#undef _rl_pure_alphabetic
305int
306_rl_pure_alphabetic (c)
307 int c;
308{
309 return (isupper (c) || islower (c));
310}
311
312#undef _rl_digit_p
313int
314_rl_digit_p (c)
315 int c;
316{
317 return (isdigit (c));
318}
319
320#undef _rl_to_lower
321int
322_rl_to_lower (c)
323 int c;
324{
325 return (isupper (c) ? tolower (c) : c);
326}
327
328#undef _rl_to_upper
329int
330_rl_to_upper (c)
331 int c;
332{
333 return (islower (c) ? toupper (c) : c);
334}
335
336#undef _rl_digit_value
337int
338_rl_digit_value (c)
339 int c;
340{
341 return (isdigit (c) ? c - '0' : c);
342}
343
344/* Backwards compatibility, now that savestring has been removed from
345 all `public' readline header files. */
346#undef _rl_savestring
347char *
348_rl_savestring (s)
251#if !defined (HAVE_STRCASECMP)
252/* Compare at most COUNT characters from string1 to string2. Case
253 doesn't matter. */
254int
255_rl_strnicmp (string1, string2, count)
256 char *string1, *string2;
257 int count;
258{
259 register char ch1, ch2;
260
261 while (count)
262 {
263 ch1 = *string1++;
264 ch2 = *string2++;
265 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
266 count--;
267 else
268 break;
269 }
270 return (count);
271}
272
273/* strcmp (), but caseless. */
274int
275_rl_stricmp (string1, string2)
276 char *string1, *string2;
277{
278 register char ch1, ch2;
279
280 while (*string1 && *string2)
281 {
282 ch1 = *string1++;
283 ch2 = *string2++;
284 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
285 return (1);
286 }
287 return (*string1 - *string2);
288}
289#endif /* !HAVE_STRCASECMP */
290
291/* Stupid comparison routine for qsort () ing strings. */
292int
293_rl_qsort_string_compare (s1, s2)
294 char **s1, **s2;
295{
296#if defined (HAVE_STRCOLL)
297 return (strcoll (*s1, *s2));
298#else
299 int result;
300
301 result = **s1 - **s2;
302 if (result == 0)
303 result = strcmp (*s1, *s2);
304
305 return result;
306#endif
307}
308
309/* Function equivalents for the macros defined in chartypes.h. */
310#undef _rl_uppercase_p
311int
312_rl_uppercase_p (c)
313 int c;
314{
315 return (isupper (c));
316}
317
318#undef _rl_lowercase_p
319int
320_rl_lowercase_p (c)
321 int c;
322{
323 return (islower (c));
324}
325
326#undef _rl_pure_alphabetic
327int
328_rl_pure_alphabetic (c)
329 int c;
330{
331 return (isupper (c) || islower (c));
332}
333
334#undef _rl_digit_p
335int
336_rl_digit_p (c)
337 int c;
338{
339 return (isdigit (c));
340}
341
342#undef _rl_to_lower
343int
344_rl_to_lower (c)
345 int c;
346{
347 return (isupper (c) ? tolower (c) : c);
348}
349
350#undef _rl_to_upper
351int
352_rl_to_upper (c)
353 int c;
354{
355 return (islower (c) ? toupper (c) : c);
356}
357
358#undef _rl_digit_value
359int
360_rl_digit_value (c)
361 int c;
362{
363 return (isdigit (c) ? c - '0' : c);
364}
365
366/* Backwards compatibility, now that savestring has been removed from
367 all `public' readline header files. */
368#undef _rl_savestring
369char *
370_rl_savestring (s)
349 char *s;
371 const char *s;
350{
372{
351 return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
373 return (strcpy (xmalloc (1 + (int)strlen (s)), (s)));
352}
374}