28a9d9d1045ba6f491e47b434b4594cff3ad90b2
[deliverable/binutils-gdb.git] / readline / util.c
1 /* util.c -- readline utility functions */
2
3 /* Copyright (C) 1987-2015 Free Software Foundation, Inc.
4
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.
7
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.
12
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.
17
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
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"
47 #include "rlmbutil.h"
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
56 #include "rlprivate.h"
57 #include "xmalloc.h"
58 #include "rlshell.h"
59
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
69 int _rl_allow_pathname_alphabetic_chars = 0;
70 static const char * const pathname_alphabetic_chars = "/-_=~.#$";
71
72 int
73 rl_alphabetic (c)
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
83 #if defined (HANDLE_MULTIBYTE)
84 int
85 _rl_walphabetic (wchar_t wc)
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
98 /* How to abort things. */
99 int
100 _rl_abort_internal ()
101 {
102 rl_ding ();
103 rl_clear_message ();
104 _rl_reset_argument ();
105 rl_clear_pending_input ();
106
107 RL_UNSETSTATE (RL_STATE_MACRODEF);
108 while (rl_executing_macro)
109 _rl_pop_executing_macro ();
110
111 RL_UNSETSTATE (RL_STATE_MULTIKEY); /* XXX */
112
113 rl_last_func = (rl_command_func_t *)NULL;
114
115 _rl_longjmp (_rl_top_level, 1);
116 return (0);
117 }
118
119 int
120 rl_abort (count, key)
121 int count, key;
122 {
123 return (_rl_abort_internal ());
124 }
125
126 int
127 _rl_null_function (count, key)
128 int count, key;
129 {
130 return 0;
131 }
132
133 int
134 rl_tty_status (count, key)
135 int count, key;
136 {
137 #if defined (TIOCSTAT)
138 ioctl (1, TIOCSTAT, (char *)0);
139 rl_refresh_line (count, key);
140 #else
141 rl_ding ();
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. */
148 char *
149 rl_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;
160 copy = (char *)xmalloc (1 + length);
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. */
168 void
169 rl_extend_line_buffer (len)
170 int len;
171 {
172 while (len >= rl_line_buffer_len)
173 {
174 rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
175 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
176 }
177
178 _rl_set_the_line ();
179 }
180
181
182 /* A function for simple tilde expansion. */
183 int
184 rl_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);
198 xfree (homedir);
199 return (0);
200 }
201 else if (start >= 0 && rl_line_buffer[start] != '~')
202 {
203 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
204 ;
205 start++;
206 }
207 else if (start < 0)
208 start = 0;
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;
224 temp = (char *)xmalloc (len + 1);
225 strncpy (temp, rl_line_buffer + start, len);
226 temp[len] = '\0';
227 homedir = tilde_expand (temp);
228 xfree (temp);
229
230 _rl_replace_text (homedir, start, end);
231 xfree (homedir);
232 }
233
234 return (0);
235 }
236
237 #if defined (USE_VARARGS)
238 void
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
268 void
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 */
297 void
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
308 void
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
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. */
326 char *
327 _rl_strindex (s1, s2)
328 register const char *s1, *s2;
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)
334 return ((char *) (s1 + i));
335 return ((char *)NULL);
336 }
337
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. */
341 char *
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)
365 string1 += v - 1; /* -1 to account for auto-increment in loop */
366 }
367 #endif
368 }
369 return ((char *)NULL);
370 }
371 #endif
372
373 #if !defined (HAVE_STRCASECMP)
374 /* Compare at most COUNT characters from string1 to string2. Case
375 doesn't matter (strncasecmp). */
376 int
377 _rl_strnicmp (string1, string2, count)
378 const char *string1;
379 const char *string2;
380 int count;
381 {
382 register const char *s1;
383 register const char *s2;
384 register int d;
385
386 if (count <= 0 || (string1 == string2))
387 return 0;
388
389 s1 = string1;
390 s2 = string2;
391 do
392 {
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')
397 break;
398 s2++;
399 }
400 while (--count != 0);
401
402 return (0);
403 }
404
405 /* strcmp (), but caseless (strcasecmp). */
406 int
407 _rl_stricmp (string1, string2)
408 const char *string1;
409 const char *string2;
410 {
411 register const char *s1;
412 register const char *s2;
413 register int d;
414
415 s1 = string1;
416 s2 = string2;
417
418 if (s1 == s2)
419 return 0;
420
421 while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
422 {
423 if (*s1++ == '\0')
424 return 0;
425 s2++;
426 }
427
428 return (d);
429 }
430 #endif /* !HAVE_STRCASECMP */
431
432 /* Stupid comparison routine for qsort () ing strings. */
433 int
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
450 /* Function equivalents for the macros defined in chardefs.h. */
451 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
452
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)
460
461 /* A convenience function, to force memory deallocation to be performed
462 by readline. DLLs on Windows apparently require this. */
463 void
464 rl_free (mem)
465 void *mem;
466 {
467 if (mem)
468 free (mem);
469 }
470
471 /* Backwards compatibility, now that savestring has been removed from
472 all `public' readline header files. */
473 #undef _rl_savestring
474 char *
475 _rl_savestring (s)
476 const char *s;
477 {
478 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
479 }
480
481 #if defined (DEBUG)
482 #if defined (USE_VARARGS)
483 static FILE *_rl_tracefp;
484
485 void
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
514 int
515 _rl_tropen ()
516 {
517 char fnbuf[128], *x;
518
519 if (_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")
527 : "."),
528 getpid ());
529 #else
530 sprintf (fnbuf, "/var/tmp/rltrace.%ld", (long) getpid ());
531 #endif
532 unlink (fnbuf);
533 _rl_tracefp = fopen (fnbuf, "w+");
534 return _rl_tracefp != 0;
535 }
536
537 int
538 _rl_trclose ()
539 {
540 int r;
541
542 r = fclose (_rl_tracefp);
543 _rl_tracefp = 0;
544 return r;
545 }
546
547 void
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. */
564 void
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 }
597 #endif
This page took 0.043304 seconds and 3 git commands to generate.