| 1 | /* util.c -- readline utility functions */ |
| 2 | |
| 3 | /* Copyright (C) 1987-2010 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_last_func = (rl_command_func_t *)NULL; |
| 112 | longjmp (_rl_top_level, 1); |
| 113 | return (0); |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | rl_abort (count, key) |
| 118 | int count, key; |
| 119 | { |
| 120 | return (_rl_abort_internal ()); |
| 121 | } |
| 122 | |
| 123 | int |
| 124 | _rl_null_function (count, key) |
| 125 | int count, key; |
| 126 | { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int |
| 131 | rl_tty_status (count, key) |
| 132 | int count, key; |
| 133 | { |
| 134 | #if defined (TIOCSTAT) |
| 135 | ioctl (1, TIOCSTAT, (char *)0); |
| 136 | rl_refresh_line (count, key); |
| 137 | #else |
| 138 | rl_ding (); |
| 139 | #endif |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* Return a copy of the string between FROM and TO. |
| 144 | FROM is inclusive, TO is not. */ |
| 145 | char * |
| 146 | rl_copy_text (from, to) |
| 147 | int from, to; |
| 148 | { |
| 149 | register int length; |
| 150 | char *copy; |
| 151 | |
| 152 | /* Fix it if the caller is confused. */ |
| 153 | if (from > to) |
| 154 | SWAP (from, to); |
| 155 | |
| 156 | length = to - from; |
| 157 | copy = (char *)xmalloc (1 + length); |
| 158 | strncpy (copy, rl_line_buffer + from, length); |
| 159 | copy[length] = '\0'; |
| 160 | return (copy); |
| 161 | } |
| 162 | |
| 163 | /* Increase the size of RL_LINE_BUFFER until it has enough space to hold |
| 164 | LEN characters. */ |
| 165 | void |
| 166 | rl_extend_line_buffer (len) |
| 167 | int len; |
| 168 | { |
| 169 | while (len >= rl_line_buffer_len) |
| 170 | { |
| 171 | rl_line_buffer_len += DEFAULT_BUFFER_SIZE; |
| 172 | rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len); |
| 173 | } |
| 174 | |
| 175 | _rl_set_the_line (); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* A function for simple tilde expansion. */ |
| 180 | int |
| 181 | rl_tilde_expand (ignore, key) |
| 182 | int ignore, key; |
| 183 | { |
| 184 | register int start, end; |
| 185 | char *homedir, *temp; |
| 186 | int len; |
| 187 | |
| 188 | end = rl_point; |
| 189 | start = end - 1; |
| 190 | |
| 191 | if (rl_point == rl_end && rl_line_buffer[rl_point] == '~') |
| 192 | { |
| 193 | homedir = tilde_expand ("~"); |
| 194 | _rl_replace_text (homedir, start, end); |
| 195 | xfree (homedir); |
| 196 | return (0); |
| 197 | } |
| 198 | else if (rl_line_buffer[start] != '~') |
| 199 | { |
| 200 | for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--) |
| 201 | ; |
| 202 | start++; |
| 203 | } |
| 204 | |
| 205 | end = start; |
| 206 | do |
| 207 | end++; |
| 208 | while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end); |
| 209 | |
| 210 | if (whitespace (rl_line_buffer[end]) || end >= rl_end) |
| 211 | end--; |
| 212 | |
| 213 | /* If the first character of the current word is a tilde, perform |
| 214 | tilde expansion and insert the result. If not a tilde, do |
| 215 | nothing. */ |
| 216 | if (rl_line_buffer[start] == '~') |
| 217 | { |
| 218 | len = end - start + 1; |
| 219 | temp = (char *)xmalloc (len + 1); |
| 220 | strncpy (temp, rl_line_buffer + start, len); |
| 221 | temp[len] = '\0'; |
| 222 | homedir = tilde_expand (temp); |
| 223 | xfree (temp); |
| 224 | |
| 225 | _rl_replace_text (homedir, start, end); |
| 226 | xfree (homedir); |
| 227 | } |
| 228 | |
| 229 | return (0); |
| 230 | } |
| 231 | |
| 232 | #if defined (USE_VARARGS) |
| 233 | void |
| 234 | #if defined (PREFER_STDARG) |
| 235 | _rl_ttymsg (const char *format, ...) |
| 236 | #else |
| 237 | _rl_ttymsg (va_alist) |
| 238 | va_dcl |
| 239 | #endif |
| 240 | { |
| 241 | va_list args; |
| 242 | #if defined (PREFER_VARARGS) |
| 243 | char *format; |
| 244 | #endif |
| 245 | |
| 246 | #if defined (PREFER_STDARG) |
| 247 | va_start (args, format); |
| 248 | #else |
| 249 | va_start (args); |
| 250 | format = va_arg (args, char *); |
| 251 | #endif |
| 252 | |
| 253 | fprintf (stderr, "readline: "); |
| 254 | vfprintf (stderr, format, args); |
| 255 | fprintf (stderr, "\n"); |
| 256 | fflush (stderr); |
| 257 | |
| 258 | va_end (args); |
| 259 | |
| 260 | rl_forced_update_display (); |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | #if defined (PREFER_STDARG) |
| 265 | _rl_errmsg (const char *format, ...) |
| 266 | #else |
| 267 | _rl_errmsg (va_alist) |
| 268 | va_dcl |
| 269 | #endif |
| 270 | { |
| 271 | va_list args; |
| 272 | #if defined (PREFER_VARARGS) |
| 273 | char *format; |
| 274 | #endif |
| 275 | |
| 276 | #if defined (PREFER_STDARG) |
| 277 | va_start (args, format); |
| 278 | #else |
| 279 | va_start (args); |
| 280 | format = va_arg (args, char *); |
| 281 | #endif |
| 282 | |
| 283 | fprintf (stderr, "readline: "); |
| 284 | vfprintf (stderr, format, args); |
| 285 | fprintf (stderr, "\n"); |
| 286 | fflush (stderr); |
| 287 | |
| 288 | va_end (args); |
| 289 | } |
| 290 | |
| 291 | #else /* !USE_VARARGS */ |
| 292 | void |
| 293 | _rl_ttymsg (format, arg1, arg2) |
| 294 | char *format; |
| 295 | { |
| 296 | fprintf (stderr, "readline: "); |
| 297 | fprintf (stderr, format, arg1, arg2); |
| 298 | fprintf (stderr, "\n"); |
| 299 | |
| 300 | rl_forced_update_display (); |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | _rl_errmsg (format, arg1, arg2) |
| 305 | char *format; |
| 306 | { |
| 307 | fprintf (stderr, "readline: "); |
| 308 | fprintf (stderr, format, arg1, arg2); |
| 309 | fprintf (stderr, "\n"); |
| 310 | } |
| 311 | #endif /* !USE_VARARGS */ |
| 312 | |
| 313 | /* **************************************************************** */ |
| 314 | /* */ |
| 315 | /* String Utility Functions */ |
| 316 | /* */ |
| 317 | /* **************************************************************** */ |
| 318 | |
| 319 | /* Determine if s2 occurs in s1. If so, return a pointer to the |
| 320 | match in s1. The compare is case insensitive. */ |
| 321 | char * |
| 322 | _rl_strindex (s1, s2) |
| 323 | register const char *s1, *s2; |
| 324 | { |
| 325 | register int i, l, len; |
| 326 | |
| 327 | for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++) |
| 328 | if (_rl_strnicmp (s1 + i, s2, l) == 0) |
| 329 | return ((char *) (s1 + i)); |
| 330 | return ((char *)NULL); |
| 331 | } |
| 332 | |
| 333 | #ifndef HAVE_STRPBRK |
| 334 | /* Find the first occurrence in STRING1 of any character from STRING2. |
| 335 | Return a pointer to the character in STRING1. */ |
| 336 | char * |
| 337 | _rl_strpbrk (string1, string2) |
| 338 | const char *string1, *string2; |
| 339 | { |
| 340 | register const char *scan; |
| 341 | #if defined (HANDLE_MULTIBYTE) |
| 342 | mbstate_t ps; |
| 343 | register int i, v; |
| 344 | |
| 345 | memset (&ps, 0, sizeof (mbstate_t)); |
| 346 | #endif |
| 347 | |
| 348 | for (; *string1; string1++) |
| 349 | { |
| 350 | for (scan = string2; *scan; scan++) |
| 351 | { |
| 352 | if (*string1 == *scan) |
| 353 | return ((char *)string1); |
| 354 | } |
| 355 | #if defined (HANDLE_MULTIBYTE) |
| 356 | if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
| 357 | { |
| 358 | v = _rl_get_char_len (string1, &ps); |
| 359 | if (v > 1) |
| 360 | string1 += v - 1; /* -1 to account for auto-increment in loop */ |
| 361 | } |
| 362 | #endif |
| 363 | } |
| 364 | return ((char *)NULL); |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | #if !defined (HAVE_STRCASECMP) |
| 369 | /* Compare at most COUNT characters from string1 to string2. Case |
| 370 | doesn't matter (strncasecmp). */ |
| 371 | int |
| 372 | _rl_strnicmp (string1, string2, count) |
| 373 | char *string1, *string2; |
| 374 | int count; |
| 375 | { |
| 376 | register char *s1, *s2; |
| 377 | int d; |
| 378 | |
| 379 | if (count <= 0 || (string1 == string2)) |
| 380 | return 0; |
| 381 | |
| 382 | s1 = string1; |
| 383 | s2 = string2; |
| 384 | do |
| 385 | { |
| 386 | d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */ |
| 387 | if (d != 0) |
| 388 | return d; |
| 389 | if (*s1++ == '\0') |
| 390 | break; |
| 391 | s2++; |
| 392 | } |
| 393 | while (--count != 0); |
| 394 | |
| 395 | return (0); |
| 396 | } |
| 397 | |
| 398 | /* strcmp (), but caseless (strcasecmp). */ |
| 399 | int |
| 400 | _rl_stricmp (string1, string2) |
| 401 | char *string1, *string2; |
| 402 | { |
| 403 | register char *s1, *s2; |
| 404 | int d; |
| 405 | |
| 406 | s1 = string1; |
| 407 | s2 = string2; |
| 408 | |
| 409 | if (s1 == s2) |
| 410 | return 0; |
| 411 | |
| 412 | while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0) |
| 413 | { |
| 414 | if (*s1++ == '\0') |
| 415 | return 0; |
| 416 | s2++; |
| 417 | } |
| 418 | |
| 419 | return (d); |
| 420 | } |
| 421 | #endif /* !HAVE_STRCASECMP */ |
| 422 | |
| 423 | /* Stupid comparison routine for qsort () ing strings. */ |
| 424 | int |
| 425 | _rl_qsort_string_compare (s1, s2) |
| 426 | char **s1, **s2; |
| 427 | { |
| 428 | #if defined (HAVE_STRCOLL) |
| 429 | return (strcoll (*s1, *s2)); |
| 430 | #else |
| 431 | int result; |
| 432 | |
| 433 | result = **s1 - **s2; |
| 434 | if (result == 0) |
| 435 | result = strcmp (*s1, *s2); |
| 436 | |
| 437 | return result; |
| 438 | #endif |
| 439 | } |
| 440 | |
| 441 | /* Function equivalents for the macros defined in chardefs.h. */ |
| 442 | #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); } |
| 443 | |
| 444 | FUNCTION_FOR_MACRO (_rl_digit_p) |
| 445 | FUNCTION_FOR_MACRO (_rl_digit_value) |
| 446 | FUNCTION_FOR_MACRO (_rl_lowercase_p) |
| 447 | FUNCTION_FOR_MACRO (_rl_pure_alphabetic) |
| 448 | FUNCTION_FOR_MACRO (_rl_to_lower) |
| 449 | FUNCTION_FOR_MACRO (_rl_to_upper) |
| 450 | FUNCTION_FOR_MACRO (_rl_uppercase_p) |
| 451 | |
| 452 | /* A convenience function, to force memory deallocation to be performed |
| 453 | by readline. DLLs on Windows apparently require this. */ |
| 454 | void |
| 455 | rl_free (mem) |
| 456 | void *mem; |
| 457 | { |
| 458 | if (mem) |
| 459 | free (mem); |
| 460 | } |
| 461 | |
| 462 | /* Backwards compatibility, now that savestring has been removed from |
| 463 | all `public' readline header files. */ |
| 464 | #undef _rl_savestring |
| 465 | char * |
| 466 | _rl_savestring (s) |
| 467 | const char *s; |
| 468 | { |
| 469 | return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s))); |
| 470 | } |
| 471 | |
| 472 | #if defined (USE_VARARGS) |
| 473 | static FILE *_rl_tracefp; |
| 474 | |
| 475 | void |
| 476 | #if defined (PREFER_STDARG) |
| 477 | _rl_trace (const char *format, ...) |
| 478 | #else |
| 479 | _rl_trace (va_alist) |
| 480 | va_dcl |
| 481 | #endif |
| 482 | { |
| 483 | va_list args; |
| 484 | #if defined (PREFER_VARARGS) |
| 485 | char *format; |
| 486 | #endif |
| 487 | |
| 488 | #if defined (PREFER_STDARG) |
| 489 | va_start (args, format); |
| 490 | #else |
| 491 | va_start (args); |
| 492 | format = va_arg (args, char *); |
| 493 | #endif |
| 494 | |
| 495 | if (_rl_tracefp == 0) |
| 496 | _rl_tropen (); |
| 497 | vfprintf (_rl_tracefp, format, args); |
| 498 | fprintf (_rl_tracefp, "\n"); |
| 499 | fflush (_rl_tracefp); |
| 500 | |
| 501 | va_end (args); |
| 502 | } |
| 503 | |
| 504 | int |
| 505 | _rl_tropen () |
| 506 | { |
| 507 | char fnbuf[128]; |
| 508 | |
| 509 | if (_rl_tracefp) |
| 510 | fclose (_rl_tracefp); |
| 511 | #if defined (_WIN32) && !defined (__CYGWIN__) |
| 512 | /* Windows doesn't have /var/tmp, so open the trace file in the |
| 513 | user's temporary directory instead. */ |
| 514 | sprintf (fnbuf, "%s/rltrace.%ld", |
| 515 | (sh_get_env_value ("TEMP") |
| 516 | ? sh_get_env_value ("TEMP") |
| 517 | : "."), |
| 518 | getpid()); |
| 519 | #else |
| 520 | sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid()); |
| 521 | #endif |
| 522 | unlink(fnbuf); |
| 523 | _rl_tracefp = fopen (fnbuf, "w+"); |
| 524 | return _rl_tracefp != 0; |
| 525 | } |
| 526 | |
| 527 | int |
| 528 | _rl_trclose () |
| 529 | { |
| 530 | int r; |
| 531 | |
| 532 | r = fclose (_rl_tracefp); |
| 533 | _rl_tracefp = 0; |
| 534 | return r; |
| 535 | } |
| 536 | |
| 537 | #endif |