bfd/
[deliverable/binutils-gdb.git] / readline / util.c
CommitLineData
d60d9f65
SS
1/* util.c -- readline utility functions */
2
cc88a640 3/* Copyright (C) 1987-2010 Free Software Foundation, Inc.
d60d9f65 4
cc88a640
JK
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
d60d9f65 7
cc88a640
JK
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
d60d9f65
SS
11 (at your option) any later version.
12
cc88a640
JK
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d60d9f65
SS
16 GNU General Public License for more details.
17
cc88a640
JK
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
d60d9f65
SS
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25# include <config.h>
26#endif
27
28#include <sys/types.h>
29#include <fcntl.h>
30#include "posixjmp.h"
31
32#if defined (HAVE_UNISTD_H)
33# include <unistd.h> /* for _POSIX_VERSION */
34#endif /* HAVE_UNISTD_H */
35
36#if defined (HAVE_STDLIB_H)
37# include <stdlib.h>
38#else
39# include "ansi_stdlib.h"
40#endif /* HAVE_STDLIB_H */
41
42#include <stdio.h>
43#include <ctype.h>
44
45/* System-specific feature definitions and include files. */
46#include "rldefs.h"
5bdf8622 47#include "rlmbutil.h"
d60d9f65
SS
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
1b17e766
EZ
56#include "rlprivate.h"
57#include "xmalloc.h"
d60d9f65 58
d60d9f65
SS
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
68int _rl_allow_pathname_alphabetic_chars = 0;
cc88a640 69static const char * const pathname_alphabetic_chars = "/-_=~.#$";
d60d9f65
SS
70
71int
9255ee31 72rl_alphabetic (c)
d60d9f65
SS
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
5bdf8622
DJ
82#if defined (HANDLE_MULTIBYTE)
83int
cc88a640 84_rl_walphabetic (wchar_t wc)
5bdf8622
DJ
85{
86 int c;
87
88 if (iswalnum (wc))
89 return (1);
90
91 c = wc & 0177;
92 return (_rl_allow_pathname_alphabetic_chars &&
93 strchr (pathname_alphabetic_chars, c) != NULL);
94}
95#endif
96
d60d9f65
SS
97/* How to abort things. */
98int
99_rl_abort_internal ()
100{
9255ee31 101 rl_ding ();
d60d9f65 102 rl_clear_message ();
5bdf8622 103 _rl_reset_argument ();
9255ee31 104 rl_clear_pending_input ();
d60d9f65 105
9255ee31
EZ
106 RL_UNSETSTATE (RL_STATE_MACRODEF);
107 while (rl_executing_macro)
d60d9f65
SS
108 _rl_pop_executing_macro ();
109
9255ee31 110 rl_last_func = (rl_command_func_t *)NULL;
cc88a640 111 longjmp (_rl_top_level, 1);
d60d9f65
SS
112 return (0);
113}
114
115int
116rl_abort (count, key)
117 int count, key;
118{
119 return (_rl_abort_internal ());
120}
121
cc88a640
JK
122int
123_rl_null_function (count, key)
124 int count, key;
125{
126 return 0;
127}
128
d60d9f65
SS
129int
130rl_tty_status (count, key)
131 int count, key;
132{
133#if defined (TIOCSTAT)
134 ioctl (1, TIOCSTAT, (char *)0);
c862e87b 135 rl_refresh_line (count, key);
d60d9f65 136#else
9255ee31 137 rl_ding ();
d60d9f65
SS
138#endif
139 return 0;
140}
141
142/* Return a copy of the string between FROM and TO.
143 FROM is inclusive, TO is not. */
144char *
145rl_copy_text (from, to)
146 int from, to;
147{
148 register int length;
149 char *copy;
150
151 /* Fix it if the caller is confused. */
152 if (from > to)
153 SWAP (from, to);
154
155 length = to - from;
9255ee31 156 copy = (char *)xmalloc (1 + length);
d60d9f65
SS
157 strncpy (copy, rl_line_buffer + from, length);
158 copy[length] = '\0';
159 return (copy);
160}
161
162/* Increase the size of RL_LINE_BUFFER until it has enough space to hold
163 LEN characters. */
164void
165rl_extend_line_buffer (len)
166 int len;
167{
168 while (len >= rl_line_buffer_len)
169 {
170 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
9255ee31 171 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
d60d9f65
SS
172 }
173
174 _rl_set_the_line ();
175}
176
177
178/* A function for simple tilde expansion. */
179int
180rl_tilde_expand (ignore, key)
181 int ignore, key;
182{
183 register int start, end;
184 char *homedir, *temp;
185 int len;
186
187 end = rl_point;
188 start = end - 1;
189
190 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
191 {
192 homedir = tilde_expand ("~");
193 _rl_replace_text (homedir, start, end);
cc88a640 194 xfree (homedir);
d60d9f65
SS
195 return (0);
196 }
197 else if (rl_line_buffer[start] != '~')
198 {
199 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
200 ;
201 start++;
202 }
203
204 end = start;
205 do
206 end++;
207 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
208
209 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
210 end--;
211
212 /* If the first character of the current word is a tilde, perform
213 tilde expansion and insert the result. If not a tilde, do
214 nothing. */
215 if (rl_line_buffer[start] == '~')
216 {
217 len = end - start + 1;
9255ee31 218 temp = (char *)xmalloc (len + 1);
d60d9f65
SS
219 strncpy (temp, rl_line_buffer + start, len);
220 temp[len] = '\0';
221 homedir = tilde_expand (temp);
cc88a640 222 xfree (temp);
d60d9f65
SS
223
224 _rl_replace_text (homedir, start, end);
cc88a640 225 xfree (homedir);
d60d9f65
SS
226 }
227
228 return (0);
229}
230
cc88a640
JK
231#if defined (USE_VARARGS)
232void
233#if defined (PREFER_STDARG)
234_rl_ttymsg (const char *format, ...)
235#else
236_rl_ttymsg (va_alist)
237 va_dcl
238#endif
239{
240 va_list args;
241#if defined (PREFER_VARARGS)
242 char *format;
243#endif
244
245#if defined (PREFER_STDARG)
246 va_start (args, format);
247#else
248 va_start (args);
249 format = va_arg (args, char *);
250#endif
251
252 fprintf (stderr, "readline: ");
253 vfprintf (stderr, format, args);
254 fprintf (stderr, "\n");
255 fflush (stderr);
256
257 va_end (args);
258
259 rl_forced_update_display ();
260}
261
262void
263#if defined (PREFER_STDARG)
264_rl_errmsg (const char *format, ...)
265#else
266_rl_errmsg (va_alist)
267 va_dcl
268#endif
269{
270 va_list args;
271#if defined (PREFER_VARARGS)
272 char *format;
273#endif
274
275#if defined (PREFER_STDARG)
276 va_start (args, format);
277#else
278 va_start (args);
279 format = va_arg (args, char *);
280#endif
281
282 fprintf (stderr, "readline: ");
283 vfprintf (stderr, format, args);
284 fprintf (stderr, "\n");
285 fflush (stderr);
286
287 va_end (args);
288}
289
290#else /* !USE_VARARGS */
291void
292_rl_ttymsg (format, arg1, arg2)
293 char *format;
294{
295 fprintf (stderr, "readline: ");
296 fprintf (stderr, format, arg1, arg2);
297 fprintf (stderr, "\n");
298
299 rl_forced_update_display ();
300}
301
302void
303_rl_errmsg (format, arg1, arg2)
304 char *format;
305{
306 fprintf (stderr, "readline: ");
307 fprintf (stderr, format, arg1, arg2);
308 fprintf (stderr, "\n");
309}
310#endif /* !USE_VARARGS */
311
d60d9f65
SS
312/* **************************************************************** */
313/* */
314/* String Utility Functions */
315/* */
316/* **************************************************************** */
317
318/* Determine if s2 occurs in s1. If so, return a pointer to the
319 match in s1. The compare is case insensitive. */
320char *
321_rl_strindex (s1, s2)
9255ee31 322 register const char *s1, *s2;
d60d9f65
SS
323{
324 register int i, l, len;
325
326 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
327 if (_rl_strnicmp (s1 + i, s2, l) == 0)
9255ee31 328 return ((char *) (s1 + i));
d60d9f65
SS
329 return ((char *)NULL);
330}
331
9255ee31
EZ
332#ifndef HAVE_STRPBRK
333/* Find the first occurrence in STRING1 of any character from STRING2.
334 Return a pointer to the character in STRING1. */
335char *
336_rl_strpbrk (string1, string2)
337 const char *string1, *string2;
338{
339 register const char *scan;
340#if defined (HANDLE_MULTIBYTE)
341 mbstate_t ps;
342 register int i, v;
343
344 memset (&ps, 0, sizeof (mbstate_t));
345#endif
346
347 for (; *string1; string1++)
348 {
349 for (scan = string2; *scan; scan++)
350 {
351 if (*string1 == *scan)
352 return ((char *)string1);
353 }
354#if defined (HANDLE_MULTIBYTE)
355 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
356 {
357 v = _rl_get_char_len (string1, &ps);
358 if (v > 1)
5bdf8622 359 string1 += v - 1; /* -1 to account for auto-increment in loop */
9255ee31
EZ
360 }
361#endif
362 }
363 return ((char *)NULL);
364}
365#endif
366
d60d9f65
SS
367#if !defined (HAVE_STRCASECMP)
368/* Compare at most COUNT characters from string1 to string2. Case
cc88a640 369 doesn't matter (strncasecmp). */
d60d9f65
SS
370int
371_rl_strnicmp (string1, string2, count)
372 char *string1, *string2;
373 int count;
374{
cc88a640
JK
375 register char *s1, *s2;
376 int d;
d60d9f65 377
cc88a640
JK
378 if (count <= 0 || (string1 == string2))
379 return 0;
380
381 s1 = string1;
382 s2 = string2;
383 do
d60d9f65 384 {
cc88a640
JK
385 d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */
386 if (d != 0)
387 return d;
388 if (*s1++ == '\0')
d60d9f65 389 break;
cc88a640 390 s2++;
d60d9f65 391 }
cc88a640
JK
392 while (--count != 0)
393
394 return (0);
d60d9f65
SS
395}
396
cc88a640 397/* strcmp (), but caseless (strcasecmp). */
d60d9f65
SS
398int
399_rl_stricmp (string1, string2)
400 char *string1, *string2;
401{
cc88a640
JK
402 register char *s1, *s2;
403 int d;
404
405 s1 = string1;
406 s2 = string2;
d60d9f65 407
cc88a640
JK
408 if (s1 == s2)
409 return 0;
410
411 while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
d60d9f65 412 {
cc88a640
JK
413 if (*s1++ == '\0')
414 return 0;
415 s2++;
d60d9f65 416 }
cc88a640
JK
417
418 return (d);
d60d9f65
SS
419}
420#endif /* !HAVE_STRCASECMP */
421
422/* Stupid comparison routine for qsort () ing strings. */
423int
424_rl_qsort_string_compare (s1, s2)
425 char **s1, **s2;
426{
427#if defined (HAVE_STRCOLL)
428 return (strcoll (*s1, *s2));
429#else
430 int result;
431
432 result = **s1 - **s2;
433 if (result == 0)
434 result = strcmp (*s1, *s2);
435
436 return result;
437#endif
438}
439
9255ee31
EZ
440/* Function equivalents for the macros defined in chardefs.h. */
441#define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
d60d9f65 442
9255ee31
EZ
443FUNCTION_FOR_MACRO (_rl_digit_p)
444FUNCTION_FOR_MACRO (_rl_digit_value)
445FUNCTION_FOR_MACRO (_rl_lowercase_p)
446FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
447FUNCTION_FOR_MACRO (_rl_to_lower)
448FUNCTION_FOR_MACRO (_rl_to_upper)
449FUNCTION_FOR_MACRO (_rl_uppercase_p)
d60d9f65 450
cc88a640
JK
451/* A convenience function, to force memory deallocation to be performed
452 by readline. DLLs on Windows apparently require this. */
453void
454rl_free (mem)
455 void *mem;
456{
457 if (mem)
458 free (mem);
459}
460
d60d9f65
SS
461/* Backwards compatibility, now that savestring has been removed from
462 all `public' readline header files. */
463#undef _rl_savestring
464char *
465_rl_savestring (s)
9255ee31 466 const char *s;
d60d9f65 467{
9255ee31 468 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
d60d9f65 469}
cc88a640
JK
470
471#if defined (USE_VARARGS)
472static FILE *_rl_tracefp;
473
474void
475#if defined (PREFER_STDARG)
476_rl_trace (const char *format, ...)
477#else
478_rl_trace (va_alist)
479 va_dcl
480#endif
481{
482 va_list args;
483#if defined (PREFER_VARARGS)
484 char *format;
485#endif
486
487#if defined (PREFER_STDARG)
488 va_start (args, format);
489#else
490 va_start (args);
491 format = va_arg (args, char *);
492#endif
493
494 if (_rl_tracefp == 0)
495 _rl_tropen ();
496 vfprintf (_rl_tracefp, format, args);
497 fprintf (_rl_tracefp, "\n");
498 fflush (_rl_tracefp);
499
500 va_end (args);
501}
502
503int
504_rl_tropen ()
505{
506 char fnbuf[128];
507
508 if (_rl_tracefp)
509 fclose (_rl_tracefp);
510 sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid());
511 unlink(fnbuf);
512 _rl_tracefp = fopen (fnbuf, "w+");
513 return _rl_tracefp != 0;
514}
515
516int
517_rl_trclose ()
518{
519 int r;
520
521 r = fclose (_rl_tracefp);
522 _rl_tracefp = 0;
523 return r;
524}
525
526#endif
This page took 0.619646 seconds and 4 git commands to generate.