Import readline 7.0 (patch 5)
[deliverable/binutils-gdb.git] / readline / util.c
CommitLineData
d60d9f65
SS
1/* util.c -- readline utility functions */
2
775e241e 3/* Copyright (C) 1987-2015 Free Software Foundation, Inc.
d60d9f65 4
cc88a640
JK
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.
d60d9f65 7
cc88a640
JK
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
d60d9f65
SS
11 (at your option) any later version.
12
cc88a640
JK
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
d60d9f65
SS
16 GNU General Public License for more details.
17
cc88a640
JK
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/>.
20*/
21
d60d9f65
SS
22#define READLINE_LIBRARY
23
24#if defined (HAVE_CONFIG_H)
25# include <config.h>
26#endif
27
28#include <sys/types.h>
29#include <fcntl.h>
30#include "posixjmp.h"
31
32#if defined (HAVE_UNISTD_H)
33# include <unistd.h> /* for _POSIX_VERSION */
34#endif /* HAVE_UNISTD_H */
35
36#if defined (HAVE_STDLIB_H)
37# include <stdlib.h>
38#else
39# include "ansi_stdlib.h"
40#endif /* HAVE_STDLIB_H */
41
42#include <stdio.h>
43#include <ctype.h>
44
45/* System-specific feature definitions and include files. */
46#include "rldefs.h"
5bdf8622 47#include "rlmbutil.h"
d60d9f65
SS
48
49#if defined (TIOCSTAT_IN_SYS_IOCTL)
50# include <sys/ioctl.h>
51#endif /* TIOCSTAT_IN_SYS_IOCTL */
52
53/* Some standard library routines. */
54#include "readline.h"
55
1b17e766
EZ
56#include "rlprivate.h"
57#include "xmalloc.h"
7f3c5ec8 58#include "rlshell.h"
d60d9f65 59
d60d9f65
SS
60/* **************************************************************** */
61/* */
62/* Utility Functions */
63/* */
64/* **************************************************************** */
65
66/* Return 0 if C is not a member of the class of characters that belong
67 in words, or 1 if it is. */
68
69int _rl_allow_pathname_alphabetic_chars = 0;
cc88a640 70static const char * const pathname_alphabetic_chars = "/-_=~.#$";
d60d9f65
SS
71
72int
9255ee31 73rl_alphabetic (c)
d60d9f65
SS
74 int c;
75{
76 if (ALPHABETIC (c))
77 return (1);
78
79 return (_rl_allow_pathname_alphabetic_chars &&
80 strchr (pathname_alphabetic_chars, c) != NULL);
81}
82
5bdf8622
DJ
83#if defined (HANDLE_MULTIBYTE)
84int
cc88a640 85_rl_walphabetic (wchar_t wc)
5bdf8622
DJ
86{
87 int c;
88
89 if (iswalnum (wc))
90 return (1);
91
92 c = wc & 0177;
93 return (_rl_allow_pathname_alphabetic_chars &&
94 strchr (pathname_alphabetic_chars, c) != NULL);
95}
96#endif
97
d60d9f65
SS
98/* How to abort things. */
99int
100_rl_abort_internal ()
101{
9255ee31 102 rl_ding ();
d60d9f65 103 rl_clear_message ();
5bdf8622 104 _rl_reset_argument ();
9255ee31 105 rl_clear_pending_input ();
d60d9f65 106
9255ee31
EZ
107 RL_UNSETSTATE (RL_STATE_MACRODEF);
108 while (rl_executing_macro)
d60d9f65
SS
109 _rl_pop_executing_macro ();
110
775e241e
TT
111 RL_UNSETSTATE (RL_STATE_MULTIKEY); /* XXX */
112
9255ee31 113 rl_last_func = (rl_command_func_t *)NULL;
775e241e
TT
114
115 _rl_longjmp (_rl_top_level, 1);
d60d9f65
SS
116 return (0);
117}
118
119int
120rl_abort (count, key)
121 int count, key;
122{
123 return (_rl_abort_internal ());
124}
125
cc88a640
JK
126int
127_rl_null_function (count, key)
128 int count, key;
129{
130 return 0;
131}
132
d60d9f65
SS
133int
134rl_tty_status (count, key)
135 int count, key;
136{
137#if defined (TIOCSTAT)
138 ioctl (1, TIOCSTAT, (char *)0);
c862e87b 139 rl_refresh_line (count, key);
d60d9f65 140#else
9255ee31 141 rl_ding ();
d60d9f65
SS
142#endif
143 return 0;
144}
145
146/* Return a copy of the string between FROM and TO.
147 FROM is inclusive, TO is not. */
148char *
149rl_copy_text (from, to)
150 int from, to;
151{
152 register int length;
153 char *copy;
154
155 /* Fix it if the caller is confused. */
156 if (from > to)
157 SWAP (from, to);
158
159 length = to - from;
9255ee31 160 copy = (char *)xmalloc (1 + length);
d60d9f65
SS
161 strncpy (copy, rl_line_buffer + from, length);
162 copy[length] = '\0';
163 return (copy);
164}
165
166/* Increase the size of RL_LINE_BUFFER until it has enough space to hold
167 LEN characters. */
168void
169rl_extend_line_buffer (len)
170 int len;
171{
172 while (len >= rl_line_buffer_len)
173 {
174 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
9255ee31 175 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
d60d9f65
SS
176 }
177
178 _rl_set_the_line ();
179}
180
181
182/* A function for simple tilde expansion. */
183int
184rl_tilde_expand (ignore, key)
185 int ignore, key;
186{
187 register int start, end;
188 char *homedir, *temp;
189 int len;
190
191 end = rl_point;
192 start = end - 1;
193
194 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
195 {
196 homedir = tilde_expand ("~");
197 _rl_replace_text (homedir, start, end);
cc88a640 198 xfree (homedir);
d60d9f65
SS
199 return (0);
200 }
775e241e 201 else if (start >= 0 && rl_line_buffer[start] != '~')
d60d9f65
SS
202 {
203 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
204 ;
205 start++;
206 }
775e241e
TT
207 else if (start < 0)
208 start = 0;
d60d9f65
SS
209
210 end = start;
211 do
212 end++;
213 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
214
215 if (whitespace (rl_line_buffer[end]) || end >= rl_end)
216 end--;
217
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
220 nothing. */
221 if (rl_line_buffer[start] == '~')
222 {
223 len = end - start + 1;
9255ee31 224 temp = (char *)xmalloc (len + 1);
d60d9f65
SS
225 strncpy (temp, rl_line_buffer + start, len);
226 temp[len] = '\0';
227 homedir = tilde_expand (temp);
cc88a640 228 xfree (temp);
d60d9f65
SS
229
230 _rl_replace_text (homedir, start, end);
cc88a640 231 xfree (homedir);
d60d9f65
SS
232 }
233
234 return (0);
235}
236
cc88a640
JK
237#if defined (USE_VARARGS)
238void
239#if defined (PREFER_STDARG)
240_rl_ttymsg (const char *format, ...)
241#else
242_rl_ttymsg (va_alist)
243 va_dcl
244#endif
245{
246 va_list args;
247#if defined (PREFER_VARARGS)
248 char *format;
249#endif
250
251#if defined (PREFER_STDARG)
252 va_start (args, format);
253#else
254 va_start (args);
255 format = va_arg (args, char *);
256#endif
257
258 fprintf (stderr, "readline: ");
259 vfprintf (stderr, format, args);
260 fprintf (stderr, "\n");
261 fflush (stderr);
262
263 va_end (args);
264
265 rl_forced_update_display ();
266}
267
268void
269#if defined (PREFER_STDARG)
270_rl_errmsg (const char *format, ...)
271#else
272_rl_errmsg (va_alist)
273 va_dcl
274#endif
275{
276 va_list args;
277#if defined (PREFER_VARARGS)
278 char *format;
279#endif
280
281#if defined (PREFER_STDARG)
282 va_start (args, format);
283#else
284 va_start (args);
285 format = va_arg (args, char *);
286#endif
287
288 fprintf (stderr, "readline: ");
289 vfprintf (stderr, format, args);
290 fprintf (stderr, "\n");
291 fflush (stderr);
292
293 va_end (args);
294}
295
296#else /* !USE_VARARGS */
297void
298_rl_ttymsg (format, arg1, arg2)
299 char *format;
300{
301 fprintf (stderr, "readline: ");
302 fprintf (stderr, format, arg1, arg2);
303 fprintf (stderr, "\n");
304
305 rl_forced_update_display ();
306}
307
308void
309_rl_errmsg (format, arg1, arg2)
310 char *format;
311{
312 fprintf (stderr, "readline: ");
313 fprintf (stderr, format, arg1, arg2);
314 fprintf (stderr, "\n");
315}
316#endif /* !USE_VARARGS */
317
d60d9f65
SS
318/* **************************************************************** */
319/* */
320/* String Utility Functions */
321/* */
322/* **************************************************************** */
323
324/* Determine if s2 occurs in s1. If so, return a pointer to the
325 match in s1. The compare is case insensitive. */
326char *
327_rl_strindex (s1, s2)
9255ee31 328 register const char *s1, *s2;
d60d9f65
SS
329{
330 register int i, l, len;
331
332 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
333 if (_rl_strnicmp (s1 + i, s2, l) == 0)
9255ee31 334 return ((char *) (s1 + i));
d60d9f65
SS
335 return ((char *)NULL);
336}
337
9255ee31
EZ
338#ifndef HAVE_STRPBRK
339/* Find the first occurrence in STRING1 of any character from STRING2.
340 Return a pointer to the character in STRING1. */
341char *
342_rl_strpbrk (string1, string2)
343 const char *string1, *string2;
344{
345 register const char *scan;
346#if defined (HANDLE_MULTIBYTE)
347 mbstate_t ps;
348 register int i, v;
349
350 memset (&ps, 0, sizeof (mbstate_t));
351#endif
352
353 for (; *string1; string1++)
354 {
355 for (scan = string2; *scan; scan++)
356 {
357 if (*string1 == *scan)
358 return ((char *)string1);
359 }
360#if defined (HANDLE_MULTIBYTE)
361 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
362 {
363 v = _rl_get_char_len (string1, &ps);
364 if (v > 1)
5bdf8622 365 string1 += v - 1; /* -1 to account for auto-increment in loop */
9255ee31
EZ
366 }
367#endif
368 }
369 return ((char *)NULL);
370}
371#endif
372
d60d9f65
SS
373#if !defined (HAVE_STRCASECMP)
374/* Compare at most COUNT characters from string1 to string2. Case
cc88a640 375 doesn't matter (strncasecmp). */
d60d9f65
SS
376int
377_rl_strnicmp (string1, string2, count)
775e241e
TT
378 const char *string1;
379 const char *string2;
d60d9f65
SS
380 int count;
381{
775e241e
TT
382 register const char *s1;
383 register const char *s2;
384 register int d;
d60d9f65 385
cc88a640
JK
386 if (count <= 0 || (string1 == string2))
387 return 0;
388
389 s1 = string1;
390 s2 = string2;
391 do
d60d9f65 392 {
cc88a640
JK
393 d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */
394 if (d != 0)
395 return d;
396 if (*s1++ == '\0')
d60d9f65 397 break;
cc88a640 398 s2++;
d60d9f65 399 }
ab3a7f8f 400 while (--count != 0);
cc88a640
JK
401
402 return (0);
d60d9f65
SS
403}
404
cc88a640 405/* strcmp (), but caseless (strcasecmp). */
d60d9f65
SS
406int
407_rl_stricmp (string1, string2)
775e241e
TT
408 const char *string1;
409 const char *string2;
d60d9f65 410{
775e241e
TT
411 register const char *s1;
412 register const char *s2;
413 register int d;
cc88a640
JK
414
415 s1 = string1;
416 s2 = string2;
d60d9f65 417
cc88a640
JK
418 if (s1 == s2)
419 return 0;
420
421 while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
d60d9f65 422 {
cc88a640
JK
423 if (*s1++ == '\0')
424 return 0;
425 s2++;
d60d9f65 426 }
cc88a640
JK
427
428 return (d);
d60d9f65
SS
429}
430#endif /* !HAVE_STRCASECMP */
431
432/* Stupid comparison routine for qsort () ing strings. */
433int
434_rl_qsort_string_compare (s1, s2)
435 char **s1, **s2;
436{
437#if defined (HAVE_STRCOLL)
438 return (strcoll (*s1, *s2));
439#else
440 int result;
441
442 result = **s1 - **s2;
443 if (result == 0)
444 result = strcmp (*s1, *s2);
445
446 return result;
447#endif
448}
449
9255ee31
EZ
450/* Function equivalents for the macros defined in chardefs.h. */
451#define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
d60d9f65 452
9255ee31
EZ
453FUNCTION_FOR_MACRO (_rl_digit_p)
454FUNCTION_FOR_MACRO (_rl_digit_value)
455FUNCTION_FOR_MACRO (_rl_lowercase_p)
456FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
457FUNCTION_FOR_MACRO (_rl_to_lower)
458FUNCTION_FOR_MACRO (_rl_to_upper)
459FUNCTION_FOR_MACRO (_rl_uppercase_p)
d60d9f65 460
cc88a640
JK
461/* A convenience function, to force memory deallocation to be performed
462 by readline. DLLs on Windows apparently require this. */
463void
464rl_free (mem)
465 void *mem;
466{
467 if (mem)
468 free (mem);
469}
470
d60d9f65
SS
471/* Backwards compatibility, now that savestring has been removed from
472 all `public' readline header files. */
473#undef _rl_savestring
474char *
475_rl_savestring (s)
9255ee31 476 const char *s;
d60d9f65 477{
9255ee31 478 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
d60d9f65 479}
cc88a640 480
775e241e 481#if defined (DEBUG)
cc88a640
JK
482#if defined (USE_VARARGS)
483static FILE *_rl_tracefp;
484
485void
486#if defined (PREFER_STDARG)
487_rl_trace (const char *format, ...)
488#else
489_rl_trace (va_alist)
490 va_dcl
491#endif
492{
493 va_list args;
494#if defined (PREFER_VARARGS)
495 char *format;
496#endif
497
498#if defined (PREFER_STDARG)
499 va_start (args, format);
500#else
501 va_start (args);
502 format = va_arg (args, char *);
503#endif
504
505 if (_rl_tracefp == 0)
506 _rl_tropen ();
507 vfprintf (_rl_tracefp, format, args);
508 fprintf (_rl_tracefp, "\n");
509 fflush (_rl_tracefp);
510
511 va_end (args);
512}
513
514int
515_rl_tropen ()
516{
775e241e 517 char fnbuf[128], *x;
cc88a640
JK
518
519 if (_rl_tracefp)
520 fclose (_rl_tracefp);
7f3c5ec8
EZ
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")
527 : "."),
16bfc2f9 528 getpid ());
7f3c5ec8 529#else
16bfc2f9 530 sprintf (fnbuf, "/var/tmp/rltrace.%ld", (long) getpid ());
7f3c5ec8 531#endif
16bfc2f9 532 unlink (fnbuf);
cc88a640
JK
533 _rl_tracefp = fopen (fnbuf, "w+");
534 return _rl_tracefp != 0;
535}
536
537int
538_rl_trclose ()
539{
540 int r;
541
542 r = fclose (_rl_tracefp);
543 _rl_tracefp = 0;
544 return r;
545}
546
775e241e
TT
547void
548_rl_settracefp (fp)
549 FILE *fp;
550{
551 _rl_tracefp = fp;
552}
553#endif
554#endif /* DEBUG */
555
556
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>
562
563/* Report STRING to the audit system. */
564void
565_rl_audit_tty (string)
566 char *string;
567{
568 struct audit_message req;
569 struct sockaddr_nl addr;
570 size_t size;
571 int fd;
572
573 fd = socket (PF_NETLINK, SOCK_RAW, NETLINK_AUDIT);
574 if (fd < 0)
575 return;
576 size = strlen (string) + 1;
577
578 if (NLMSG_SPACE (size) > MAX_AUDIT_MESSAGE_LENGTH)
579 return;
580
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;
586 if (size && string)
587 memcpy (NLMSG_DATA(&req.nlh), string, size);
588 memset (&addr, 0, sizeof(addr));
589
590 addr.nl_family = AF_NETLINK;
591 addr.nl_pid = 0;
592 addr.nl_groups = 0;
593
594 sendto (fd, &req, req.nlh.nlmsg_len, 0, (struct sockaddr*)&addr, sizeof(addr));
595 close (fd);
596}
cc88a640 597#endif
This page took 1.042918 seconds and 4 git commands to generate.