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