]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libreadline/util.c
This commit was generated by cvs2svn to compensate for changes in r130561,
[FreeBSD/FreeBSD.git] / contrib / libreadline / util.c
1 /* $FreeBSD$ */
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 /* **************************************************************** */
60 /*                                                                  */
61 /*                      Utility Functions                           */
62 /*                                                                  */
63 /* **************************************************************** */
64
65 /* Return 0 if C is not a member of the class of characters that belong
66    in words, or 1 if it is. */
67
68 int _rl_allow_pathname_alphabetic_chars = 0;
69 static const char *pathname_alphabetic_chars = "/-_=~.#$";
70
71 int
72 rl_alphabetic (c)
73      int c;
74 {
75   if (ALPHABETIC (c))
76     return (1);
77
78   return (_rl_allow_pathname_alphabetic_chars &&
79             strchr (pathname_alphabetic_chars, c) != NULL);
80 }
81
82 /* How to abort things. */
83 int
84 _rl_abort_internal ()
85 {
86   rl_ding ();
87   rl_clear_message ();
88   _rl_init_argument ();
89   rl_clear_pending_input ();
90
91   RL_UNSETSTATE (RL_STATE_MACRODEF);
92   while (rl_executing_macro)
93     _rl_pop_executing_macro ();
94
95   rl_last_func = (rl_command_func_t *)NULL;
96   longjmp (readline_top_level, 1);
97   return (0);
98 }
99
100 int
101 rl_abort (count, key)
102      int count, key;
103 {
104   return (_rl_abort_internal ());
105 }
106
107 int
108 rl_tty_status (count, key)
109      int count, key;
110 {
111 #if defined (TIOCSTAT)
112   ioctl (1, TIOCSTAT, (char *)0);
113   rl_refresh_line (count, key);
114 #else
115   rl_ding ();
116 #endif
117   return 0;
118 }
119
120 /* Return a copy of the string between FROM and TO.
121    FROM is inclusive, TO is not. */
122 char *
123 rl_copy_text (from, to)
124      int from, to;
125 {
126   register int length;
127   char *copy;
128
129   /* Fix it if the caller is confused. */
130   if (from > to)
131     SWAP (from, to);
132
133   length = to - from;
134   copy = (char *)xmalloc (1 + length);
135   strncpy (copy, rl_line_buffer + from, length);
136   copy[length] = '\0';
137   return (copy);
138 }
139
140 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
141    LEN characters. */
142 void
143 rl_extend_line_buffer (len)
144      int len;
145 {
146   while (len >= rl_line_buffer_len)
147     {
148       rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
149       rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
150     }
151
152   _rl_set_the_line ();
153 }
154
155
156 /* A function for simple tilde expansion. */
157 int
158 rl_tilde_expand (ignore, key)
159      int ignore, key;
160 {
161   register int start, end;
162   char *homedir, *temp;
163   int len;
164
165   end = rl_point;
166   start = end - 1;
167
168   if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
169     {
170       homedir = tilde_expand ("~");
171       _rl_replace_text (homedir, start, end);
172       return (0);
173     }
174   else if (rl_line_buffer[start] != '~')
175     {
176       for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
177         ;
178       start++;
179     }
180
181   end = start;
182   do
183     end++;
184   while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
185
186   if (whitespace (rl_line_buffer[end]) || end >= rl_end)
187     end--;
188
189   /* If the first character of the current word is a tilde, perform
190      tilde expansion and insert the result.  If not a tilde, do
191      nothing. */
192   if (rl_line_buffer[start] == '~')
193     {
194       len = end - start + 1;
195       temp = (char *)xmalloc (len + 1);
196       strncpy (temp, rl_line_buffer + start, len);
197       temp[len] = '\0';
198       homedir = tilde_expand (temp);
199       free (temp);
200
201       _rl_replace_text (homedir, start, end);
202     }
203
204   return (0);
205 }
206
207 /* **************************************************************** */
208 /*                                                                  */
209 /*                      String Utility Functions                    */
210 /*                                                                  */
211 /* **************************************************************** */
212
213 /* Determine if s2 occurs in s1.  If so, return a pointer to the
214    match in s1.  The compare is case insensitive. */
215 char *
216 _rl_strindex (s1, s2)
217      register const char *s1, *s2;
218 {
219   register int i, l, len;
220
221   for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
222     if (_rl_strnicmp (s1 + i, s2, l) == 0)
223       return ((char *) (s1 + i));
224   return ((char *)NULL);
225 }
226
227 #ifndef HAVE_STRPBRK
228 /* Find the first occurrence in STRING1 of any character from STRING2.
229    Return a pointer to the character in STRING1. */
230 char *
231 _rl_strpbrk (string1, string2)
232      const char *string1, *string2;
233 {
234   register const char *scan;
235 #if defined (HANDLE_MULTIBYTE)
236   mbstate_t ps;
237   register int i, v;
238
239   memset (&ps, 0, sizeof (mbstate_t));
240 #endif
241
242   if (string2 == NULL)
243     return ((char *)NULL);
244
245   for (; *string1; string1++)
246     {
247       for (scan = string2; *scan; scan++)
248         {
249           if (*string1 == *scan)
250             return ((char *)string1);
251         }
252 #if defined (HANDLE_MULTIBYTE)
253       if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
254         {
255           v = _rl_get_char_len (string1, &ps);
256           if (v > 1)
257             string += v - 1;    /* -1 to account for auto-increment in loop */
258         }
259 #endif
260     }
261   return ((char *)NULL);
262 }
263 #endif
264
265 #if !defined (HAVE_STRCASECMP)
266 /* Compare at most COUNT characters from string1 to string2.  Case
267    doesn't matter. */
268 int
269 _rl_strnicmp (string1, string2, count)
270      char *string1, *string2;
271      int count;
272 {
273   register char ch1, ch2;
274
275   while (count)
276     {
277       ch1 = *string1++;
278       ch2 = *string2++;
279       if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
280         count--;
281       else
282         break;
283     }
284   return (count);
285 }
286
287 /* strcmp (), but caseless. */
288 int
289 _rl_stricmp (string1, string2)
290      char *string1, *string2;
291 {
292   register char ch1, ch2;
293
294   while (*string1 && *string2)
295     {
296       ch1 = *string1++;
297       ch2 = *string2++;
298       if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
299         return (1);
300     }
301   return (*string1 - *string2);
302 }
303 #endif /* !HAVE_STRCASECMP */
304
305 /* Stupid comparison routine for qsort () ing strings. */
306 int
307 _rl_qsort_string_compare (s1, s2)
308   char **s1, **s2;
309 {
310 #if defined (HAVE_STRCOLL)
311   return (strcoll (*s1, *s2));
312 #else
313   int result;
314
315   result = **s1 - **s2;
316   if (result == 0)
317     result = strcmp (*s1, *s2);
318
319   return result;
320 #endif
321 }
322
323 /* Function equivalents for the macros defined in chardefs.h. */
324 #define FUNCTION_FOR_MACRO(f)   int (f) (c) int c; { return f (c); }
325
326 FUNCTION_FOR_MACRO (_rl_digit_p)
327 FUNCTION_FOR_MACRO (_rl_digit_value)
328 FUNCTION_FOR_MACRO (_rl_lowercase_p)
329 FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
330 FUNCTION_FOR_MACRO (_rl_to_lower)
331 FUNCTION_FOR_MACRO (_rl_to_upper)
332 FUNCTION_FOR_MACRO (_rl_uppercase_p)
333
334 /* Backwards compatibility, now that savestring has been removed from
335    all `public' readline header files. */
336 #undef _rl_savestring
337 char *
338 _rl_savestring (s)
339      const char *s;
340 {
341   return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
342 }