1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987-2017 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"
60 /* **************************************************************** */
62 /* Utility Functions */
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 = "/-_=~.#$";
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. */
99 _rl_abort_internal (void)
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_UNSETSTATE (RL_STATE_MULTIKEY); /* XXX */
112 rl_last_func = (rl_command_func_t *)NULL;
114 _rl_longjmp (_rl_top_level, 1);
119 rl_abort (int count, int key)
121 return (_rl_abort_internal ());
125 _rl_null_function (int count, int key)
131 rl_tty_status (int count, int 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 (int from, int to)
150 /* Fix it if the caller is confused. */
155 copy = (char *)xmalloc (1 + length);
156 strncpy (copy, rl_line_buffer + from, length);
161 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
164 rl_extend_line_buffer (int len)
166 while (len >= rl_line_buffer_len)
168 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
169 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
176 /* A function for simple tilde expansion. */
178 rl_tilde_expand (int ignore, int key)
180 register int start, end;
181 char *homedir, *temp;
187 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
189 homedir = tilde_expand ("~");
190 _rl_replace_text (homedir, start, end);
194 else if (start >= 0 && rl_line_buffer[start] != '~')
196 for (; start >= 0 && !whitespace (rl_line_buffer[start]); start--)
206 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
208 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
211 /* If the first character of the current word is a tilde, perform
212 tilde expansion and insert the result. If not a tilde, do
214 if (rl_line_buffer[start] == '~')
216 len = end - start + 1;
217 temp = (char *)xmalloc (len + 1);
218 strncpy (temp, rl_line_buffer + start, len);
220 homedir = tilde_expand (temp);
223 _rl_replace_text (homedir, start, end);
230 #if defined (USE_VARARGS)
232 #if defined (PREFER_STDARG)
233 _rl_ttymsg (const char *format, ...)
235 _rl_ttymsg (va_alist)
240 #if defined (PREFER_VARARGS)
244 #if defined (PREFER_STDARG)
245 va_start (args, format);
248 format = va_arg (args, char *);
251 fprintf (stderr, "readline: ");
252 vfprintf (stderr, format, args);
253 fprintf (stderr, "\n");
258 rl_forced_update_display ();
262 #if defined (PREFER_STDARG)
263 _rl_errmsg (const char *format, ...)
265 _rl_errmsg (va_alist)
270 #if defined (PREFER_VARARGS)
274 #if defined (PREFER_STDARG)
275 va_start (args, format);
278 format = va_arg (args, char *);
281 fprintf (stderr, "readline: ");
282 vfprintf (stderr, format, args);
283 fprintf (stderr, "\n");
289 #else /* !USE_VARARGS */
291 _rl_ttymsg (format, arg1, arg2)
294 fprintf (stderr, "readline: ");
295 fprintf (stderr, format, arg1, arg2);
296 fprintf (stderr, "\n");
298 rl_forced_update_display ();
302 _rl_errmsg (format, arg1, arg2)
305 fprintf (stderr, "readline: ");
306 fprintf (stderr, format, arg1, arg2);
307 fprintf (stderr, "\n");
309 #endif /* !USE_VARARGS */
311 /* **************************************************************** */
313 /* String Utility Functions */
315 /* **************************************************************** */
317 /* Determine if s2 occurs in s1. If so, return a pointer to the
318 match in s1. The compare is case insensitive. */
320 _rl_strindex (const char *s1, const char *s2)
322 register int i, l, len;
324 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
325 if (_rl_strnicmp (s1 + i, s2, l) == 0)
326 return ((char *) (s1 + i));
327 return ((char *)NULL);
331 /* Find the first occurrence in STRING1 of any character from STRING2.
332 Return a pointer to the character in STRING1. */
334 _rl_strpbrk (const char *string1, const char *string2)
336 register const char *scan;
337 #if defined (HANDLE_MULTIBYTE)
341 memset (&ps, 0, sizeof (mbstate_t));
344 for (; *string1; string1++)
346 for (scan = string2; *scan; scan++)
348 if (*string1 == *scan)
349 return ((char *)string1);
351 #if defined (HANDLE_MULTIBYTE)
352 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
354 v = _rl_get_char_len (string1, &ps);
356 string1 += v - 1; /* -1 to account for auto-increment in loop */
360 return ((char *)NULL);
364 #if !defined (HAVE_STRCASECMP)
365 /* Compare at most COUNT characters from string1 to string2. Case
366 doesn't matter (strncasecmp). */
368 _rl_strnicmp (const char *string1, const char *string2, int count)
370 register const char *s1;
371 register const char *s2;
374 if (count <= 0 || (string1 == string2))
381 d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */
388 while (--count != 0);
393 /* strcmp (), but caseless (strcasecmp). */
395 _rl_stricmp (const char *string1, const char *string2)
397 register const char *s1;
398 register const char *s2;
407 while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
416 #endif /* !HAVE_STRCASECMP */
418 /* Stupid comparison routine for qsort () ing strings. */
420 _rl_qsort_string_compare (char **s1, char **s2)
422 #if defined (HAVE_STRCOLL)
423 return (strcoll (*s1, *s2));
427 result = **s1 - **s2;
429 result = strcmp (*s1, *s2);
435 /* Function equivalents for the macros defined in chardefs.h. */
436 #define FUNCTION_FOR_MACRO(f) int (f) (int c) { return f (c); }
438 FUNCTION_FOR_MACRO (_rl_digit_p)
439 FUNCTION_FOR_MACRO (_rl_digit_value)
440 FUNCTION_FOR_MACRO (_rl_lowercase_p)
441 FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
442 FUNCTION_FOR_MACRO (_rl_to_lower)
443 FUNCTION_FOR_MACRO (_rl_to_upper)
444 FUNCTION_FOR_MACRO (_rl_uppercase_p)
446 /* A convenience function, to force memory deallocation to be performed
447 by readline. DLLs on Windows apparently require this. */
455 /* Backwards compatibility, now that savestring has been removed from
456 all `public' readline header files. */
457 #undef _rl_savestring
459 _rl_savestring (const char *s)
461 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
465 #if defined (USE_VARARGS)
466 static FILE *_rl_tracefp;
469 #if defined (PREFER_STDARG)
470 _rl_trace (const char *format, ...)
477 #if defined (PREFER_VARARGS)
481 #if defined (PREFER_STDARG)
482 va_start (args, format);
485 format = va_arg (args, char *);
488 if (_rl_tracefp == 0)
490 vfprintf (_rl_tracefp, format, args);
491 fprintf (_rl_tracefp, "\n");
492 fflush (_rl_tracefp);
503 fclose (_rl_tracefp);
504 #if defined (_WIN32) && !defined (__CYGWIN__)
505 /* Windows doesn't have /var/tmp, so open the trace file in the
506 user's temporary directory instead. */
507 snprintf (fnbuf, sizeof (fnbuf), "%s/rltrace.%ld",
508 (sh_get_env_value ("TEMP")
509 ? sh_get_env_value ("TEMP")
513 sprintf (fnbuf, "/var/tmp/rltrace.%ld", (long) getpid ());
516 _rl_tracefp = fopen (fnbuf, "w+");
517 return _rl_tracefp != 0;
525 r = fclose (_rl_tracefp);
531 _rl_settracefp (FILE *fp)
539 #if HAVE_DECL_AUDIT_USER_TTY && defined (HAVE_LIBAUDIT_H) && defined (ENABLE_TTY_AUDIT_SUPPORT)
540 #include <sys/socket.h>
541 #include <libaudit.h>
542 #include <linux/audit.h>
543 #include <linux/netlink.h>
545 /* Report STRING to the audit system. */
547 _rl_audit_tty (char *string)
549 struct audit_message req;
550 struct sockaddr_nl addr;
554 fd = socket (PF_NETLINK, SOCK_RAW, NETLINK_AUDIT);
557 size = strlen (string) + 1;
559 if (NLMSG_SPACE (size) > MAX_AUDIT_MESSAGE_LENGTH)
562 memset (&req, 0, sizeof(req));
563 req.nlh.nlmsg_len = NLMSG_SPACE (size);
564 req.nlh.nlmsg_type = AUDIT_USER_TTY;
565 req.nlh.nlmsg_flags = NLM_F_REQUEST;
566 req.nlh.nlmsg_seq = 0;
568 memcpy (NLMSG_DATA(&req.nlh), string, size);
569 memset (&addr, 0, sizeof(addr));
571 addr.nl_family = AF_NETLINK;
575 sendto (fd, &req, req.nlh.nlmsg_len, 0, (struct sockaddr*)&addr, sizeof(addr));