1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987-2015 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
= "/-_=~.#$";
79 return (_rl_allow_pathname_alphabetic_chars
&&
80 strchr (pathname_alphabetic_chars
, c
) != NULL
);
83 #if defined (HANDLE_MULTIBYTE)
85 _rl_walphabetic (wchar_t wc
)
93 return (_rl_allow_pathname_alphabetic_chars
&&
94 strchr (pathname_alphabetic_chars
, c
) != NULL
);
98 /* How to abort things. */
100 _rl_abort_internal ()
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_UNSETSTATE (RL_STATE_MULTIKEY
); /* XXX */
113 rl_last_func
= (rl_command_func_t
*)NULL
;
115 _rl_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 (start
>= 0 && rl_line_buffer
[start
] != '~')
203 for (; !whitespace (rl_line_buffer
[start
]) && start
>= 0; start
--)
213 while (whitespace (rl_line_buffer
[end
]) == 0 && end
< rl_end
);
215 if (whitespace (rl_line_buffer
[end
]) || end
>= rl_end
)
218 /* If the first character of the current word is a tilde, perform
219 tilde expansion and insert the result. If not a tilde, do
221 if (rl_line_buffer
[start
] == '~')
223 len
= end
- start
+ 1;
224 temp
= (char *)xmalloc (len
+ 1);
225 strncpy (temp
, rl_line_buffer
+ start
, len
);
227 homedir
= tilde_expand (temp
);
230 _rl_replace_text (homedir
, start
, end
);
237 #if defined (USE_VARARGS)
239 #if defined (PREFER_STDARG)
240 _rl_ttymsg (const char *format
, ...)
242 _rl_ttymsg (va_alist
)
247 #if defined (PREFER_VARARGS)
251 #if defined (PREFER_STDARG)
252 va_start (args
, format
);
255 format
= va_arg (args
, char *);
258 fprintf (stderr
, "readline: ");
259 vfprintf (stderr
, format
, args
);
260 fprintf (stderr
, "\n");
265 rl_forced_update_display ();
269 #if defined (PREFER_STDARG)
270 _rl_errmsg (const char *format
, ...)
272 _rl_errmsg (va_alist
)
277 #if defined (PREFER_VARARGS)
281 #if defined (PREFER_STDARG)
282 va_start (args
, format
);
285 format
= va_arg (args
, char *);
288 fprintf (stderr
, "readline: ");
289 vfprintf (stderr
, format
, args
);
290 fprintf (stderr
, "\n");
296 #else /* !USE_VARARGS */
298 _rl_ttymsg (format
, arg1
, arg2
)
301 fprintf (stderr
, "readline: ");
302 fprintf (stderr
, format
, arg1
, arg2
);
303 fprintf (stderr
, "\n");
305 rl_forced_update_display ();
309 _rl_errmsg (format
, arg1
, arg2
)
312 fprintf (stderr
, "readline: ");
313 fprintf (stderr
, format
, arg1
, arg2
);
314 fprintf (stderr
, "\n");
316 #endif /* !USE_VARARGS */
318 /* **************************************************************** */
320 /* String Utility Functions */
322 /* **************************************************************** */
324 /* Determine if s2 occurs in s1. If so, return a pointer to the
325 match in s1. The compare is case insensitive. */
327 _rl_strindex (s1
, s2
)
328 register const char *s1
, *s2
;
330 register int i
, l
, len
;
332 for (i
= 0, l
= strlen (s2
), len
= strlen (s1
); (len
- i
) >= l
; i
++)
333 if (_rl_strnicmp (s1
+ i
, s2
, l
) == 0)
334 return ((char *) (s1
+ i
));
335 return ((char *)NULL
);
339 /* Find the first occurrence in STRING1 of any character from STRING2.
340 Return a pointer to the character in STRING1. */
342 _rl_strpbrk (string1
, string2
)
343 const char *string1
, *string2
;
345 register const char *scan
;
346 #if defined (HANDLE_MULTIBYTE)
350 memset (&ps
, 0, sizeof (mbstate_t));
353 for (; *string1
; string1
++)
355 for (scan
= string2
; *scan
; scan
++)
357 if (*string1
== *scan
)
358 return ((char *)string1
);
360 #if defined (HANDLE_MULTIBYTE)
361 if (MB_CUR_MAX
> 1 && rl_byte_oriented
== 0)
363 v
= _rl_get_char_len (string1
, &ps
);
365 string1
+= v
- 1; /* -1 to account for auto-increment in loop */
369 return ((char *)NULL
);
373 #if !defined (HAVE_STRCASECMP)
374 /* Compare at most COUNT characters from string1 to string2. Case
375 doesn't matter (strncasecmp). */
377 _rl_strnicmp (string1
, string2
, count
)
382 register const char *s1
;
383 register const char *s2
;
386 if (count
<= 0 || (string1
== string2
))
393 d
= _rl_to_lower (*s1
) - _rl_to_lower (*s2
); /* XXX - cast to unsigned char? */
400 while (--count
!= 0);
405 /* strcmp (), but caseless (strcasecmp). */
407 _rl_stricmp (string1
, string2
)
411 register const char *s1
;
412 register const char *s2
;
421 while ((d
= _rl_to_lower (*s1
) - _rl_to_lower (*s2
)) == 0)
430 #endif /* !HAVE_STRCASECMP */
432 /* Stupid comparison routine for qsort () ing strings. */
434 _rl_qsort_string_compare (s1
, s2
)
437 #if defined (HAVE_STRCOLL)
438 return (strcoll (*s1
, *s2
));
442 result
= **s1
- **s2
;
444 result
= strcmp (*s1
, *s2
);
450 /* Function equivalents for the macros defined in chardefs.h. */
451 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
453 FUNCTION_FOR_MACRO (_rl_digit_p
)
454 FUNCTION_FOR_MACRO (_rl_digit_value
)
455 FUNCTION_FOR_MACRO (_rl_lowercase_p
)
456 FUNCTION_FOR_MACRO (_rl_pure_alphabetic
)
457 FUNCTION_FOR_MACRO (_rl_to_lower
)
458 FUNCTION_FOR_MACRO (_rl_to_upper
)
459 FUNCTION_FOR_MACRO (_rl_uppercase_p
)
461 /* A convenience function, to force memory deallocation to be performed
462 by readline. DLLs on Windows apparently require this. */
471 /* Backwards compatibility, now that savestring has been removed from
472 all `public' readline header files. */
473 #undef _rl_savestring
478 return (strcpy ((char *)xmalloc (1 + (int)strlen (s
)), (s
)));
482 #if defined (USE_VARARGS)
483 static FILE *_rl_tracefp
;
486 #if defined (PREFER_STDARG)
487 _rl_trace (const char *format
, ...)
494 #if defined (PREFER_VARARGS)
498 #if defined (PREFER_STDARG)
499 va_start (args
, format
);
502 format
= va_arg (args
, char *);
505 if (_rl_tracefp
== 0)
507 vfprintf (_rl_tracefp
, format
, args
);
508 fprintf (_rl_tracefp
, "\n");
509 fflush (_rl_tracefp
);
520 fclose (_rl_tracefp
);
521 #if defined (_WIN32) && !defined (__CYGWIN__)
522 /* Windows doesn't have /var/tmp, so open the trace file in the
523 user's temporary directory instead. */
524 sprintf (fnbuf
, "%s/rltrace.%ld",
525 (sh_get_env_value ("TEMP")
526 ? sh_get_env_value ("TEMP")
530 sprintf (fnbuf
, "/var/tmp/rltrace.%ld", (long) getpid ());
533 _rl_tracefp
= fopen (fnbuf
, "w+");
534 return _rl_tracefp
!= 0;
542 r
= fclose (_rl_tracefp
);
557 #if HAVE_DECL_AUDIT_USER_TTY && defined (HAVE_LIBAUDIT_H) && defined (ENABLE_TTY_AUDIT_SUPPORT)
558 #include <sys/socket.h>
559 #include <libaudit.h>
560 #include <linux/audit.h>
561 #include <linux/netlink.h>
563 /* Report STRING to the audit system. */
565 _rl_audit_tty (string
)
568 struct audit_message req
;
569 struct sockaddr_nl addr
;
573 fd
= socket (PF_NETLINK
, SOCK_RAW
, NETLINK_AUDIT
);
576 size
= strlen (string
) + 1;
578 if (NLMSG_SPACE (size
) > MAX_AUDIT_MESSAGE_LENGTH
)
581 memset (&req
, 0, sizeof(req
));
582 req
.nlh
.nlmsg_len
= NLMSG_SPACE (size
);
583 req
.nlh
.nlmsg_type
= AUDIT_USER_TTY
;
584 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
;
585 req
.nlh
.nlmsg_seq
= 0;
587 memcpy (NLMSG_DATA(&req
.nlh
), string
, size
);
588 memset (&addr
, 0, sizeof(addr
));
590 addr
.nl_family
= AF_NETLINK
;
594 sendto (fd
, &req
, req
.nlh
.nlmsg_len
, 0, (struct sockaddr
*)&addr
, sizeof(addr
));