1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987-2012 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 #if defined (HAVE_POSIX_SIGSETJMP)
112 siglongjmp (_rl_top_level, 1);
114 longjmp (_rl_top_level, 1);
120 rl_abort (count, key)
123 return (_rl_abort_internal ());
127 _rl_null_function (count, key)
134 rl_tty_status (count, key)
137 #if defined (TIOCSTAT)
138 ioctl (1, TIOCSTAT, (char *)0);
139 rl_refresh_line (count, key);
146 /* Return a copy of the string between FROM and TO.
147 FROM is inclusive, TO is not. */
149 rl_copy_text (from, to)
155 /* Fix it if the caller is confused. */
160 copy = (char *)xmalloc (1 + length);
161 strncpy (copy, rl_line_buffer + from, length);
166 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
169 rl_extend_line_buffer (len)
172 while (len >= rl_line_buffer_len)
174 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
175 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
182 /* A function for simple tilde expansion. */
184 rl_tilde_expand (ignore, key)
187 register int start, end;
188 char *homedir, *temp;
194 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
196 homedir = tilde_expand ("~");
197 _rl_replace_text (homedir, start, end);
201 else if (rl_line_buffer[start] != '~')
203 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
211 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
213 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
216 /* If the first character of the current word is a tilde, perform
217 tilde expansion and insert the result. If not a tilde, do
219 if (rl_line_buffer[start] == '~')
221 len = end - start + 1;
222 temp = (char *)xmalloc (len + 1);
223 strncpy (temp, rl_line_buffer + start, len);
225 homedir = tilde_expand (temp);
228 _rl_replace_text (homedir, start, end);
235 #if defined (USE_VARARGS)
237 #if defined (PREFER_STDARG)
238 _rl_ttymsg (const char *format, ...)
240 _rl_ttymsg (va_alist)
245 #if defined (PREFER_VARARGS)
249 #if defined (PREFER_STDARG)
250 va_start (args, format);
253 format = va_arg (args, char *);
256 fprintf (stderr, "readline: ");
257 vfprintf (stderr, format, args);
258 fprintf (stderr, "\n");
263 rl_forced_update_display ();
267 #if defined (PREFER_STDARG)
268 _rl_errmsg (const char *format, ...)
270 _rl_errmsg (va_alist)
275 #if defined (PREFER_VARARGS)
279 #if defined (PREFER_STDARG)
280 va_start (args, format);
283 format = va_arg (args, char *);
286 fprintf (stderr, "readline: ");
287 vfprintf (stderr, format, args);
288 fprintf (stderr, "\n");
294 #else /* !USE_VARARGS */
296 _rl_ttymsg (format, arg1, arg2)
299 fprintf (stderr, "readline: ");
300 fprintf (stderr, format, arg1, arg2);
301 fprintf (stderr, "\n");
303 rl_forced_update_display ();
307 _rl_errmsg (format, arg1, arg2)
310 fprintf (stderr, "readline: ");
311 fprintf (stderr, format, arg1, arg2);
312 fprintf (stderr, "\n");
314 #endif /* !USE_VARARGS */
316 /* **************************************************************** */
318 /* String Utility Functions */
320 /* **************************************************************** */
322 /* Determine if s2 occurs in s1. If so, return a pointer to the
323 match in s1. The compare is case insensitive. */
325 _rl_strindex (s1, s2)
326 register const char *s1, *s2;
328 register int i, l, len;
330 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
331 if (_rl_strnicmp (s1 + i, s2, l) == 0)
332 return ((char *) (s1 + i));
333 return ((char *)NULL);
337 /* Find the first occurrence in STRING1 of any character from STRING2.
338 Return a pointer to the character in STRING1. */
340 _rl_strpbrk (string1, string2)
341 const char *string1, *string2;
343 register const char *scan;
344 #if defined (HANDLE_MULTIBYTE)
348 memset (&ps, 0, sizeof (mbstate_t));
351 for (; *string1; string1++)
353 for (scan = string2; *scan; scan++)
355 if (*string1 == *scan)
356 return ((char *)string1);
358 #if defined (HANDLE_MULTIBYTE)
359 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
361 v = _rl_get_char_len (string1, &ps);
363 string1 += v - 1; /* -1 to account for auto-increment in loop */
367 return ((char *)NULL);
371 #if !defined (HAVE_STRCASECMP)
372 /* Compare at most COUNT characters from string1 to string2. Case
373 doesn't matter (strncasecmp). */
375 _rl_strnicmp (string1, string2, count)
380 register const char *s1;
381 register const char *s2;
384 if (count <= 0 || (string1 == string2))
391 d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */
398 while (--count != 0);
403 /* strcmp (), but caseless (strcasecmp). */
405 _rl_stricmp (string1, string2)
409 register const char *s1;
410 register const char *s2;
419 while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
428 #endif /* !HAVE_STRCASECMP */
430 /* Stupid comparison routine for qsort () ing strings. */
432 _rl_qsort_string_compare (s1, s2)
435 #if defined (HAVE_STRCOLL)
436 return (strcoll (*s1, *s2));
440 result = **s1 - **s2;
442 result = strcmp (*s1, *s2);
448 /* Function equivalents for the macros defined in chardefs.h. */
449 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
451 FUNCTION_FOR_MACRO (_rl_digit_p)
452 FUNCTION_FOR_MACRO (_rl_digit_value)
453 FUNCTION_FOR_MACRO (_rl_lowercase_p)
454 FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
455 FUNCTION_FOR_MACRO (_rl_to_lower)
456 FUNCTION_FOR_MACRO (_rl_to_upper)
457 FUNCTION_FOR_MACRO (_rl_uppercase_p)
459 /* A convenience function, to force memory deallocation to be performed
460 by readline. DLLs on Windows apparently require this. */
469 /* Backwards compatibility, now that savestring has been removed from
470 all `public' readline header files. */
471 #undef _rl_savestring
476 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
479 #if defined (USE_VARARGS)
480 static FILE *_rl_tracefp;
483 #if defined (PREFER_STDARG)
484 _rl_trace (const char *format, ...)
491 #if defined (PREFER_VARARGS)
495 #if defined (PREFER_STDARG)
496 va_start (args, format);
499 format = va_arg (args, char *);
502 if (_rl_tracefp == 0)
504 vfprintf (_rl_tracefp, format, args);
505 fprintf (_rl_tracefp, "\n");
506 fflush (_rl_tracefp);
517 fclose (_rl_tracefp);
518 sprintf (fnbuf, "/var/tmp/rltrace.%ld", (long)getpid());
520 _rl_tracefp = fopen (fnbuf, "w+");
521 return _rl_tracefp != 0;
529 r = fclose (_rl_tracefp);
543 #if HAVE_DECL_AUDIT_USER_TTY && defined (ENABLE_TTY_AUDIT_SUPPORT)
544 #include <sys/socket.h>
545 #include <linux/audit.h>
546 #include <linux/netlink.h>
548 /* Report STRING to the audit system. */
550 _rl_audit_tty (string)
553 struct sockaddr_nl addr;
560 fd = socket (AF_NETLINK, SOCK_RAW, NETLINK_AUDIT);
563 size = strlen (string) + 1;
565 nlm.nlmsg_len = NLMSG_LENGTH (size);
566 nlm.nlmsg_type = AUDIT_USER_TTY;
567 nlm.nlmsg_flags = NLM_F_REQUEST;
571 iov[0].iov_base = &nlm;
572 iov[0].iov_len = sizeof (nlm);
573 iov[1].iov_base = string;
574 iov[1].iov_len = size;
576 addr.nl_family = AF_NETLINK;
580 msg.msg_name = &addr;
581 msg.msg_namelen = sizeof (addr);
584 msg.msg_control = NULL;
585 msg.msg_controllen = 0;
588 (void)sendmsg (fd, &msg, 0);