GC old libarchive/bsdtar.
[dragonfly/port-amd64.git] / contrib / libreadline / util.c
blob383f4488062221a2038bda30b143018eb9f39188
1 /* $FreeBSD: src/contrib/libreadline/util.c,v 1.5.2.2 2000/07/06 23:04:25 ache Exp $ */
2 /* $DragonFly: src/contrib/libreadline/Attic/util.c,v 1.2 2003/06/17 04:24:03 dillon Exp $ */
3 /* util.c -- readline utility functions */
5 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
7 This file is part of the GNU Readline Library, a library for
8 reading lines of text with interactive input and history editing.
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.
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.
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
26 #if defined (HAVE_CONFIG_H)
27 # include <config.h>
28 #endif
30 #include <sys/types.h>
31 #include <fcntl.h>
32 #include "posixjmp.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)
39 # include <stdlib.h>
40 #else
41 # include "ansi_stdlib.h"
42 #endif /* HAVE_STDLIB_H */
44 #include <stdio.h>
45 #include <ctype.h>
47 /* System-specific feature definitions and include files. */
48 #include "rldefs.h"
50 #if defined (TIOCSTAT_IN_SYS_IOCTL)
51 # include <sys/ioctl.h>
52 #endif /* TIOCSTAT_IN_SYS_IOCTL */
54 /* Some standard library routines. */
55 #include "readline.h"
57 #include "rlprivate.h"
58 #include "xmalloc.h"
60 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
62 /* **************************************************************** */
63 /* */
64 /* Utility Functions */
65 /* */
66 /* **************************************************************** */
68 /* Return 0 if C is not a member of the class of characters that belong
69 in words, or 1 if it is. */
71 int _rl_allow_pathname_alphabetic_chars = 0;
72 static char *pathname_alphabetic_chars = "/-_=~.#$";
74 int
75 alphabetic (c)
76 int c;
78 if (ALPHABETIC (c))
79 return (1);
81 return (_rl_allow_pathname_alphabetic_chars &&
82 strchr (pathname_alphabetic_chars, c) != NULL);
85 /* How to abort things. */
86 int
87 _rl_abort_internal ()
89 ding ();
90 rl_clear_message ();
91 _rl_init_argument ();
92 rl_pending_input = 0;
94 _rl_defining_kbd_macro = 0;
95 while (_rl_executing_macro)
96 _rl_pop_executing_macro ();
98 rl_last_func = (Function *)NULL;
99 longjmp (readline_top_level, 1);
100 return (0);
104 rl_abort (count, key)
105 int count, key;
107 return (_rl_abort_internal ());
111 rl_tty_status (count, key)
112 int count, key;
114 #if defined (TIOCSTAT)
115 ioctl (1, TIOCSTAT, (char *)0);
116 rl_refresh_line (count, key);
117 #else
118 ding ();
119 #endif
120 return 0;
123 /* Return a copy of the string between FROM and TO.
124 FROM is inclusive, TO is not. */
125 char *
126 rl_copy_text (from, to)
127 int from, to;
129 register int length;
130 char *copy;
132 /* Fix it if the caller is confused. */
133 if (from > to)
134 SWAP (from, to);
136 length = to - from;
137 copy = xmalloc (1 + length);
138 strncpy (copy, rl_line_buffer + from, length);
139 copy[length] = '\0';
140 return (copy);
143 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
144 LEN characters. */
145 void
146 rl_extend_line_buffer (len)
147 int len;
149 while (len >= rl_line_buffer_len)
151 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
152 rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
155 _rl_set_the_line ();
159 /* A function for simple tilde expansion. */
161 rl_tilde_expand (ignore, key)
162 int ignore, key;
164 register int start, end;
165 char *homedir, *temp;
166 int len;
168 end = rl_point;
169 start = end - 1;
171 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
173 homedir = tilde_expand ("~");
174 _rl_replace_text (homedir, start, end);
175 return (0);
177 else if (rl_line_buffer[start] != '~')
179 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
181 start++;
184 end = start;
186 end++;
187 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
189 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
190 end--;
192 /* If the first character of the current word is a tilde, perform
193 tilde expansion and insert the result. If not a tilde, do
194 nothing. */
195 if (rl_line_buffer[start] == '~')
197 len = end - start + 1;
198 temp = xmalloc (len + 1);
199 strncpy (temp, rl_line_buffer + start, len);
200 temp[len] = '\0';
201 homedir = tilde_expand (temp);
202 free (temp);
204 _rl_replace_text (homedir, start, end);
207 return (0);
210 /* **************************************************************** */
211 /* */
212 /* String Utility Functions */
213 /* */
214 /* **************************************************************** */
216 /* Determine if s2 occurs in s1. If so, return a pointer to the
217 match in s1. The compare is case insensitive. */
218 char *
219 _rl_strindex (s1, s2)
220 register char *s1, *s2;
222 register int i, l, len;
224 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
225 if (_rl_strnicmp (s1 + i, s2, l) == 0)
226 return (s1 + i);
227 return ((char *)NULL);
230 #if !defined (HAVE_STRCASECMP)
231 /* Compare at most COUNT characters from string1 to string2. Case
232 doesn't matter. */
234 _rl_strnicmp (string1, string2, count)
235 char *string1, *string2;
236 int count;
238 register char ch1, ch2;
240 while (count)
242 ch1 = *string1++;
243 ch2 = *string2++;
244 if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
245 count--;
246 else
247 break;
249 return (count);
252 /* strcmp (), but caseless. */
254 _rl_stricmp (string1, string2)
255 char *string1, *string2;
257 register char ch1, ch2;
259 while (*string1 && *string2)
261 ch1 = *string1++;
262 ch2 = *string2++;
263 if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
264 return (1);
266 return (*string1 - *string2);
268 #endif /* !HAVE_STRCASECMP */
270 /* Stupid comparison routine for qsort () ing strings. */
272 _rl_qsort_string_compare (s1, s2)
273 char **s1, **s2;
275 #if defined (HAVE_STRCOLL)
276 return (strcoll (*s1, *s2));
277 #else
278 int result;
280 result = **s1 - **s2;
281 if (result == 0)
282 result = strcmp (*s1, *s2);
284 return result;
285 #endif
288 /* Function equivalents for the macros defined in chartypes.h. */
289 #undef _rl_uppercase_p
291 _rl_uppercase_p (c)
292 int c;
294 return (isupper (c));
297 #undef _rl_lowercase_p
299 _rl_lowercase_p (c)
300 int c;
302 return (islower (c));
305 #undef _rl_pure_alphabetic
307 _rl_pure_alphabetic (c)
308 int c;
310 return (isupper (c) || islower (c));
313 #undef _rl_digit_p
315 _rl_digit_p (c)
316 int c;
318 return (isdigit (c));
321 #undef _rl_to_lower
323 _rl_to_lower (c)
324 int c;
326 return (isupper (c) ? tolower (c) : c);
329 #undef _rl_to_upper
331 _rl_to_upper (c)
332 int c;
334 return (islower (c) ? toupper (c) : c);
337 #undef _rl_digit_value
339 _rl_digit_value (c)
340 int c;
342 return (isdigit (c) ? c - '0' : c);
345 /* Backwards compatibility, now that savestring has been removed from
346 all `public' readline header files. */
347 #undef _rl_savestring
348 char *
349 _rl_savestring (s)
350 char *s;
352 return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));