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)
28 #include <sys/types.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)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
45 /* System-specific feature definitions and include files. */
49 #if defined (TIOCSTAT_IN_SYS_IOCTL)
50 # include <sys/ioctl.h>
51 #endif /* TIOCSTAT_IN_SYS_IOCTL */
53 /* Some standard library routines. */
56 #include "rlprivate.h"
59 /* **************************************************************** */
61 /* Utility Functions */
63 /* **************************************************************** */
65 /* Return 0 if C is not a member of the class of characters that belong
66 in words, or 1 if it is. */
68 int _rl_allow_pathname_alphabetic_chars
= 0;
69 static const char * const pathname_alphabetic_chars
= "/-_=~.#$";
78 return (_rl_allow_pathname_alphabetic_chars
&&
79 strchr (pathname_alphabetic_chars
, c
) != NULL
);
82 #if defined (HANDLE_MULTIBYTE)
84 _rl_walphabetic (wchar_t wc
)
92 return (_rl_allow_pathname_alphabetic_chars
&&
93 strchr (pathname_alphabetic_chars
, c
) != NULL
);
97 /* How to abort things. */
103 _rl_reset_argument ();
104 rl_clear_pending_input ();
106 RL_UNSETSTATE (RL_STATE_MACRODEF
);
107 while (rl_executing_macro
)
108 _rl_pop_executing_macro ();
110 rl_last_func
= (rl_command_func_t
*)NULL
;
111 longjmp (_rl_top_level
, 1);
116 rl_abort (count
, key
)
119 return (_rl_abort_internal ());
123 _rl_null_function (count
, key
)
130 rl_tty_status (count
, key
)
133 #if defined (TIOCSTAT)
134 ioctl (1, TIOCSTAT
, (char *)0);
135 rl_refresh_line (count
, key
);
142 /* Return a copy of the string between FROM and TO.
143 FROM is inclusive, TO is not. */
145 rl_copy_text (from
, to
)
151 /* Fix it if the caller is confused. */
156 copy
= (char *)xmalloc (1 + length
);
157 strncpy (copy
, rl_line_buffer
+ from
, length
);
162 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
165 rl_extend_line_buffer (len
)
168 while (len
>= rl_line_buffer_len
)
170 rl_line_buffer_len
+= DEFAULT_BUFFER_SIZE
;
171 rl_line_buffer
= (char *)xrealloc (rl_line_buffer
, rl_line_buffer_len
);
178 /* A function for simple tilde expansion. */
180 rl_tilde_expand (ignore
, key
)
183 register int start
, end
;
184 char *homedir
, *temp
;
190 if (rl_point
== rl_end
&& rl_line_buffer
[rl_point
] == '~')
192 homedir
= tilde_expand ("~");
193 _rl_replace_text (homedir
, start
, end
);
197 else if (rl_line_buffer
[start
] != '~')
199 for (; !whitespace (rl_line_buffer
[start
]) && start
>= 0; start
--)
207 while (whitespace (rl_line_buffer
[end
]) == 0 && end
< rl_end
);
209 if (whitespace (rl_line_buffer
[end
]) || end
>= rl_end
)
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
215 if (rl_line_buffer
[start
] == '~')
217 len
= end
- start
+ 1;
218 temp
= (char *)xmalloc (len
+ 1);
219 strncpy (temp
, rl_line_buffer
+ start
, len
);
221 homedir
= tilde_expand (temp
);
224 _rl_replace_text (homedir
, start
, end
);
231 #if defined (USE_VARARGS)
233 #if defined (PREFER_STDARG)
234 _rl_ttymsg (const char *format
, ...)
236 _rl_ttymsg (va_alist
)
241 #if defined (PREFER_VARARGS)
245 #if defined (PREFER_STDARG)
246 va_start (args
, format
);
249 format
= va_arg (args
, char *);
252 fprintf (stderr
, "readline: ");
253 vfprintf (stderr
, format
, args
);
254 fprintf (stderr
, "\n");
259 rl_forced_update_display ();
263 #if defined (PREFER_STDARG)
264 _rl_errmsg (const char *format
, ...)
266 _rl_errmsg (va_alist
)
271 #if defined (PREFER_VARARGS)
275 #if defined (PREFER_STDARG)
276 va_start (args
, format
);
279 format
= va_arg (args
, char *);
282 fprintf (stderr
, "readline: ");
283 vfprintf (stderr
, format
, args
);
284 fprintf (stderr
, "\n");
290 #else /* !USE_VARARGS */
292 _rl_ttymsg (format
, arg1
, arg2
)
295 fprintf (stderr
, "readline: ");
296 fprintf (stderr
, format
, arg1
, arg2
);
297 fprintf (stderr
, "\n");
299 rl_forced_update_display ();
303 _rl_errmsg (format
, arg1
, arg2
)
306 fprintf (stderr
, "readline: ");
307 fprintf (stderr
, format
, arg1
, arg2
);
308 fprintf (stderr
, "\n");
310 #endif /* !USE_VARARGS */
312 /* **************************************************************** */
314 /* String Utility Functions */
316 /* **************************************************************** */
318 /* Determine if s2 occurs in s1. If so, return a pointer to the
319 match in s1. The compare is case insensitive. */
321 _rl_strindex (s1
, s2
)
322 register const char *s1
, *s2
;
324 register int i
, l
, len
;
326 for (i
= 0, l
= strlen (s2
), len
= strlen (s1
); (len
- i
) >= l
; i
++)
327 if (_rl_strnicmp (s1
+ i
, s2
, l
) == 0)
328 return ((char *) (s1
+ i
));
329 return ((char *)NULL
);
333 /* Find the first occurrence in STRING1 of any character from STRING2.
334 Return a pointer to the character in STRING1. */
336 _rl_strpbrk (string1
, string2
)
337 const char *string1
, *string2
;
339 register const char *scan
;
340 #if defined (HANDLE_MULTIBYTE)
344 memset (&ps
, 0, sizeof (mbstate_t));
347 for (; *string1
; string1
++)
349 for (scan
= string2
; *scan
; scan
++)
351 if (*string1
== *scan
)
352 return ((char *)string1
);
354 #if defined (HANDLE_MULTIBYTE)
355 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
357 v
= _rl_get_char_len (string1
, &ps
);
359 string1
+= v
- 1; /* -1 to account for auto-increment in loop */
363 return ((char *)NULL
);
367 #if !defined (HAVE_STRCASECMP)
368 /* Compare at most COUNT characters from string1 to string2. Case
369 doesn't matter (strncasecmp). */
371 _rl_strnicmp (string1
, string2
, count
)
372 char *string1
, *string2
;
375 register char *s1
, *s2
;
378 if (count
<= 0 || (string1
== string2
))
385 d
= _rl_to_lower (*s1
) - _rl_to_lower (*s2
); /* XXX - cast to unsigned char? */
397 /* strcmp (), but caseless (strcasecmp). */
399 _rl_stricmp (string1
, string2
)
400 char *string1
, *string2
;
402 register char *s1
, *s2
;
411 while ((d
= _rl_to_lower (*s1
) - _rl_to_lower (*s2
)) == 0)
420 #endif /* !HAVE_STRCASECMP */
422 /* Stupid comparison routine for qsort () ing strings. */
424 _rl_qsort_string_compare (s1
, s2
)
427 #if defined (HAVE_STRCOLL)
428 return (strcoll (*s1
, *s2
));
432 result
= **s1
- **s2
;
434 result
= strcmp (*s1
, *s2
);
440 /* Function equivalents for the macros defined in chardefs.h. */
441 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
443 FUNCTION_FOR_MACRO (_rl_digit_p
)
444 FUNCTION_FOR_MACRO (_rl_digit_value
)
445 FUNCTION_FOR_MACRO (_rl_lowercase_p
)
446 FUNCTION_FOR_MACRO (_rl_pure_alphabetic
)
447 FUNCTION_FOR_MACRO (_rl_to_lower
)
448 FUNCTION_FOR_MACRO (_rl_to_upper
)
449 FUNCTION_FOR_MACRO (_rl_uppercase_p
)
451 /* A convenience function, to force memory deallocation to be performed
452 by readline. DLLs on Windows apparently require this. */
461 /* Backwards compatibility, now that savestring has been removed from
462 all `public' readline header files. */
463 #undef _rl_savestring
468 return (strcpy ((char *)xmalloc (1 + (int)strlen (s
)), (s
)));
471 #if defined (USE_VARARGS)
472 static FILE *_rl_tracefp
;
475 #if defined (PREFER_STDARG)
476 _rl_trace (const char *format
, ...)
483 #if defined (PREFER_VARARGS)
487 #if defined (PREFER_STDARG)
488 va_start (args
, format
);
491 format
= va_arg (args
, char *);
494 if (_rl_tracefp
== 0)
496 vfprintf (_rl_tracefp
, format
, args
);
497 fprintf (_rl_tracefp
, "\n");
498 fflush (_rl_tracefp
);
509 fclose (_rl_tracefp
);
510 sprintf (fnbuf
, "/var/tmp/rltrace.%ld", getpid());
512 _rl_tracefp
= fopen (fnbuf
, "w+");
513 return _rl_tracefp
!= 0;
521 r
= fclose (_rl_tracefp
);