[PATCH 25/57][Arm][GAS] Add support for MVE instruction: vmvn, vqabs and vqneg
[binutils-gdb.git] / readline / util.c
blob13bd00c09c9c234f9cb157a3f58830830093c376
1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987-2010 Free Software Foundation, Inc.
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.
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
11 (at your option) any later version.
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
16 GNU General Public License for more details.
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/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
25 # include <config.h>
26 #endif
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include "posixjmp.h"
32 #if defined (HAVE_UNISTD_H)
33 # include <unistd.h> /* for _POSIX_VERSION */
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
37 # include <stdlib.h>
38 #else
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
42 #include <stdio.h>
43 #include <ctype.h>
45 /* System-specific feature definitions and include files. */
46 #include "rldefs.h"
47 #include "rlmbutil.h"
49 #if defined (TIOCSTAT_IN_SYS_IOCTL)
50 # include <sys/ioctl.h>
51 #endif /* TIOCSTAT_IN_SYS_IOCTL */
53 /* Some standard library routines. */
54 #include "readline.h"
56 #include "rlprivate.h"
57 #include "xmalloc.h"
58 #include "rlshell.h"
60 /* **************************************************************** */
61 /* */
62 /* Utility Functions */
63 /* */
64 /* **************************************************************** */
66 /* Return 0 if C is not a member of the class of characters that belong
67 in words, or 1 if it is. */
69 int _rl_allow_pathname_alphabetic_chars = 0;
70 static const char * const pathname_alphabetic_chars = "/-_=~.#$";
72 int
73 rl_alphabetic (c)
74 int c;
76 if (ALPHABETIC (c))
77 return (1);
79 return (_rl_allow_pathname_alphabetic_chars &&
80 strchr (pathname_alphabetic_chars, c) != NULL);
83 #if defined (HANDLE_MULTIBYTE)
84 int
85 _rl_walphabetic (wchar_t wc)
87 int c;
89 if (iswalnum (wc))
90 return (1);
92 c = wc & 0177;
93 return (_rl_allow_pathname_alphabetic_chars &&
94 strchr (pathname_alphabetic_chars, c) != NULL);
96 #endif
98 /* How to abort things. */
99 int
100 _rl_abort_internal ()
102 rl_ding ();
103 rl_clear_message ();
104 _rl_reset_argument ();
105 rl_clear_pending_input ();
107 RL_UNSETSTATE (RL_STATE_MACRODEF);
108 while (rl_executing_macro)
109 _rl_pop_executing_macro ();
111 rl_last_func = (rl_command_func_t *)NULL;
112 longjmp (_rl_top_level, 1);
113 return (0);
117 rl_abort (count, key)
118 int count, key;
120 return (_rl_abort_internal ());
124 _rl_null_function (count, key)
125 int count, key;
127 return 0;
131 rl_tty_status (count, key)
132 int count, key;
134 #if defined (TIOCSTAT)
135 ioctl (1, TIOCSTAT, (char *)0);
136 rl_refresh_line (count, key);
137 #else
138 rl_ding ();
139 #endif
140 return 0;
143 /* Return a copy of the string between FROM and TO.
144 FROM is inclusive, TO is not. */
145 char *
146 rl_copy_text (from, to)
147 int from, to;
149 register int length;
150 char *copy;
152 /* Fix it if the caller is confused. */
153 if (from > to)
154 SWAP (from, to);
156 length = to - from;
157 copy = (char *)xmalloc (1 + length);
158 strncpy (copy, rl_line_buffer + from, length);
159 copy[length] = '\0';
160 return (copy);
163 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
164 LEN characters. */
165 void
166 rl_extend_line_buffer (len)
167 int len;
169 while (len >= rl_line_buffer_len)
171 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
172 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
175 _rl_set_the_line ();
179 /* A function for simple tilde expansion. */
181 rl_tilde_expand (ignore, key)
182 int ignore, key;
184 register int start, end;
185 char *homedir, *temp;
186 int len;
188 end = rl_point;
189 start = end - 1;
191 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
193 homedir = tilde_expand ("~");
194 _rl_replace_text (homedir, start, end);
195 xfree (homedir);
196 return (0);
198 else if (rl_line_buffer[start] != '~')
200 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
202 start++;
205 end = start;
207 end++;
208 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
210 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
211 end--;
213 /* If the first character of the current word is a tilde, perform
214 tilde expansion and insert the result. If not a tilde, do
215 nothing. */
216 if (rl_line_buffer[start] == '~')
218 len = end - start + 1;
219 temp = (char *)xmalloc (len + 1);
220 strncpy (temp, rl_line_buffer + start, len);
221 temp[len] = '\0';
222 homedir = tilde_expand (temp);
223 xfree (temp);
225 _rl_replace_text (homedir, start, end);
226 xfree (homedir);
229 return (0);
232 #if defined (USE_VARARGS)
233 void
234 #if defined (PREFER_STDARG)
235 _rl_ttymsg (const char *format, ...)
236 #else
237 _rl_ttymsg (va_alist)
238 va_dcl
239 #endif
241 va_list args;
242 #if defined (PREFER_VARARGS)
243 char *format;
244 #endif
246 #if defined (PREFER_STDARG)
247 va_start (args, format);
248 #else
249 va_start (args);
250 format = va_arg (args, char *);
251 #endif
253 fprintf (stderr, "readline: ");
254 vfprintf (stderr, format, args);
255 fprintf (stderr, "\n");
256 fflush (stderr);
258 va_end (args);
260 rl_forced_update_display ();
263 void
264 #if defined (PREFER_STDARG)
265 _rl_errmsg (const char *format, ...)
266 #else
267 _rl_errmsg (va_alist)
268 va_dcl
269 #endif
271 va_list args;
272 #if defined (PREFER_VARARGS)
273 char *format;
274 #endif
276 #if defined (PREFER_STDARG)
277 va_start (args, format);
278 #else
279 va_start (args);
280 format = va_arg (args, char *);
281 #endif
283 fprintf (stderr, "readline: ");
284 vfprintf (stderr, format, args);
285 fprintf (stderr, "\n");
286 fflush (stderr);
288 va_end (args);
291 #else /* !USE_VARARGS */
292 void
293 _rl_ttymsg (format, arg1, arg2)
294 char *format;
296 fprintf (stderr, "readline: ");
297 fprintf (stderr, format, arg1, arg2);
298 fprintf (stderr, "\n");
300 rl_forced_update_display ();
303 void
304 _rl_errmsg (format, arg1, arg2)
305 char *format;
307 fprintf (stderr, "readline: ");
308 fprintf (stderr, format, arg1, arg2);
309 fprintf (stderr, "\n");
311 #endif /* !USE_VARARGS */
313 /* **************************************************************** */
314 /* */
315 /* String Utility Functions */
316 /* */
317 /* **************************************************************** */
319 /* Determine if s2 occurs in s1. If so, return a pointer to the
320 match in s1. The compare is case insensitive. */
321 char *
322 _rl_strindex (s1, s2)
323 register const char *s1, *s2;
325 register int i, l, len;
327 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
328 if (_rl_strnicmp (s1 + i, s2, l) == 0)
329 return ((char *) (s1 + i));
330 return ((char *)NULL);
333 #ifndef HAVE_STRPBRK
334 /* Find the first occurrence in STRING1 of any character from STRING2.
335 Return a pointer to the character in STRING1. */
336 char *
337 _rl_strpbrk (string1, string2)
338 const char *string1, *string2;
340 register const char *scan;
341 #if defined (HANDLE_MULTIBYTE)
342 mbstate_t ps;
343 register int i, v;
345 memset (&ps, 0, sizeof (mbstate_t));
346 #endif
348 for (; *string1; string1++)
350 for (scan = string2; *scan; scan++)
352 if (*string1 == *scan)
353 return ((char *)string1);
355 #if defined (HANDLE_MULTIBYTE)
356 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
358 v = _rl_get_char_len (string1, &ps);
359 if (v > 1)
360 string1 += v - 1; /* -1 to account for auto-increment in loop */
362 #endif
364 return ((char *)NULL);
366 #endif
368 #if !defined (HAVE_STRCASECMP)
369 /* Compare at most COUNT characters from string1 to string2. Case
370 doesn't matter (strncasecmp). */
372 _rl_strnicmp (string1, string2, count)
373 char *string1, *string2;
374 int count;
376 register char *s1, *s2;
377 int d;
379 if (count <= 0 || (string1 == string2))
380 return 0;
382 s1 = string1;
383 s2 = string2;
386 d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */
387 if (d != 0)
388 return d;
389 if (*s1++ == '\0')
390 break;
391 s2++;
393 while (--count != 0);
395 return (0);
398 /* strcmp (), but caseless (strcasecmp). */
400 _rl_stricmp (string1, string2)
401 char *string1, *string2;
403 register char *s1, *s2;
404 int d;
406 s1 = string1;
407 s2 = string2;
409 if (s1 == s2)
410 return 0;
412 while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
414 if (*s1++ == '\0')
415 return 0;
416 s2++;
419 return (d);
421 #endif /* !HAVE_STRCASECMP */
423 /* Stupid comparison routine for qsort () ing strings. */
425 _rl_qsort_string_compare (s1, s2)
426 char **s1, **s2;
428 #if defined (HAVE_STRCOLL)
429 return (strcoll (*s1, *s2));
430 #else
431 int result;
433 result = **s1 - **s2;
434 if (result == 0)
435 result = strcmp (*s1, *s2);
437 return result;
438 #endif
441 /* Function equivalents for the macros defined in chardefs.h. */
442 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
444 FUNCTION_FOR_MACRO (_rl_digit_p)
445 FUNCTION_FOR_MACRO (_rl_digit_value)
446 FUNCTION_FOR_MACRO (_rl_lowercase_p)
447 FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
448 FUNCTION_FOR_MACRO (_rl_to_lower)
449 FUNCTION_FOR_MACRO (_rl_to_upper)
450 FUNCTION_FOR_MACRO (_rl_uppercase_p)
452 /* A convenience function, to force memory deallocation to be performed
453 by readline. DLLs on Windows apparently require this. */
454 void
455 rl_free (mem)
456 void *mem;
458 if (mem)
459 free (mem);
462 /* Backwards compatibility, now that savestring has been removed from
463 all `public' readline header files. */
464 #undef _rl_savestring
465 char *
466 _rl_savestring (s)
467 const char *s;
469 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
472 #if defined (USE_VARARGS)
473 static FILE *_rl_tracefp;
475 void
476 #if defined (PREFER_STDARG)
477 _rl_trace (const char *format, ...)
478 #else
479 _rl_trace (va_alist)
480 va_dcl
481 #endif
483 va_list args;
484 #if defined (PREFER_VARARGS)
485 char *format;
486 #endif
488 #if defined (PREFER_STDARG)
489 va_start (args, format);
490 #else
491 va_start (args);
492 format = va_arg (args, char *);
493 #endif
495 if (_rl_tracefp == 0)
496 _rl_tropen ();
497 vfprintf (_rl_tracefp, format, args);
498 fprintf (_rl_tracefp, "\n");
499 fflush (_rl_tracefp);
501 va_end (args);
505 _rl_tropen ()
507 char fnbuf[128];
509 if (_rl_tracefp)
510 fclose (_rl_tracefp);
511 #if defined (_WIN32) && !defined (__CYGWIN__)
512 /* Windows doesn't have /var/tmp, so open the trace file in the
513 user's temporary directory instead. */
514 sprintf (fnbuf, "%s/rltrace.%ld",
515 (sh_get_env_value ("TEMP")
516 ? sh_get_env_value ("TEMP")
517 : "."),
518 getpid ());
519 #else
520 sprintf (fnbuf, "/var/tmp/rltrace.%ld", (long) getpid ());
521 #endif
522 unlink (fnbuf);
523 _rl_tracefp = fopen (fnbuf, "w+");
524 return _rl_tracefp != 0;
528 _rl_trclose ()
530 int r;
532 r = fclose (_rl_tracefp);
533 _rl_tracefp = 0;
534 return r;
537 #endif