Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* util.c -- readline utility functions */ |
| 2 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | GNU General Public License for more details. |
| 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 22 | #define READLINE_LIBRARY |
| 23 | |
| 24 | #if defined (HAVE_CONFIG_H) |
| 25 | # include <config.h> |
| 26 | #endif |
| 27 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <fcntl.h> |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 30 | #include "posixjmp.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 42 | #include <stdio.h> |
| 43 | #include <ctype.h> |
| 44 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 45 | /* System-specific feature definitions and include files. */ |
| 46 | #include "rldefs.h" |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 47 | #include "rlmbutil.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 56 | #include "rlprivate.h" |
| 57 | #include "xmalloc.h" |
| 58 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | /* **************************************************************** */ |
| 60 | /* */ |
| 61 | /* Utility Functions */ |
| 62 | /* */ |
| 63 | /* **************************************************************** */ |
| 64 | |
| 65 | /* Return 0 if C is not a member of the class of characters that belong |
| 66 | in words, or 1 if it is. */ |
| 67 | |
| 68 | int _rl_allow_pathname_alphabetic_chars = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 69 | static const char * const pathname_alphabetic_chars = "/-_=~.#$"; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | |
| 71 | int |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 72 | rl_alphabetic (c) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 73 | int c; |
| 74 | { |
| 75 | if (ALPHABETIC (c)) |
| 76 | return (1); |
| 77 | |
| 78 | return (_rl_allow_pathname_alphabetic_chars && |
| 79 | strchr (pathname_alphabetic_chars, c) != NULL); |
| 80 | } |
| 81 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 82 | #if defined (HANDLE_MULTIBYTE) |
| 83 | int |
| 84 | _rl_walphabetic (wc) |
| 85 | 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 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 98 | /* How to abort things. */ |
| 99 | int |
| 100 | _rl_abort_internal () |
| 101 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 102 | rl_ding (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 103 | rl_clear_message (); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 104 | _rl_reset_argument (); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 105 | rl_clear_pending_input (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 106 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 107 | RL_UNSETSTATE (RL_STATE_MACRODEF); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 108 | while (rl_executing_macro) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 109 | _rl_pop_executing_macro (); |
| 110 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 111 | rl_last_func = (rl_command_func_t *)NULL; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 112 | longjmp (_rl_top_level, 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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_tty_status (count, key) |
| 125 | int count, key; |
| 126 | { |
| 127 | #if defined (TIOCSTAT) |
| 128 | ioctl (1, TIOCSTAT, (char *)0); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 129 | rl_refresh_line (count, key); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 130 | #else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 131 | rl_ding (); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 132 | #endif |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* Return a copy of the string between FROM and TO. |
| 137 | FROM is inclusive, TO is not. */ |
| 138 | char * |
| 139 | rl_copy_text (from, to) |
| 140 | int from, to; |
| 141 | { |
| 142 | register int length; |
| 143 | char *copy; |
| 144 | |
| 145 | /* Fix it if the caller is confused. */ |
| 146 | if (from > to) |
| 147 | SWAP (from, to); |
| 148 | |
| 149 | length = to - from; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 150 | copy = (char *)xmalloc (1 + length); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 151 | strncpy (copy, rl_line_buffer + from, length); |
| 152 | copy[length] = '\0'; |
| 153 | return (copy); |
| 154 | } |
| 155 | |
| 156 | /* Increase the size of RL_LINE_BUFFER until it has enough space to hold |
| 157 | LEN characters. */ |
| 158 | void |
| 159 | rl_extend_line_buffer (len) |
| 160 | int len; |
| 161 | { |
| 162 | while (len >= rl_line_buffer_len) |
| 163 | { |
| 164 | rl_line_buffer_len += DEFAULT_BUFFER_SIZE; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 165 | rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | _rl_set_the_line (); |
| 169 | } |
| 170 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 171 | |
| 172 | /* A function for simple tilde expansion. */ |
| 173 | int |
| 174 | rl_tilde_expand (ignore, key) |
| 175 | int ignore, key; |
| 176 | { |
| 177 | register int start, end; |
| 178 | char *homedir, *temp; |
| 179 | int len; |
| 180 | |
| 181 | end = rl_point; |
| 182 | start = end - 1; |
| 183 | |
| 184 | if (rl_point == rl_end && rl_line_buffer[rl_point] == '~') |
| 185 | { |
| 186 | homedir = tilde_expand ("~"); |
| 187 | _rl_replace_text (homedir, start, end); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 188 | xfree (homedir); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 189 | return (0); |
| 190 | } |
| 191 | else if (rl_line_buffer[start] != '~') |
| 192 | { |
| 193 | for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--) |
| 194 | ; |
| 195 | start++; |
| 196 | } |
| 197 | |
| 198 | end = start; |
| 199 | do |
| 200 | end++; |
| 201 | while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end); |
| 202 | |
| 203 | if (whitespace (rl_line_buffer[end]) || end >= rl_end) |
| 204 | end--; |
| 205 | |
| 206 | /* If the first character of the current word is a tilde, perform |
| 207 | tilde expansion and insert the result. If not a tilde, do |
| 208 | nothing. */ |
| 209 | if (rl_line_buffer[start] == '~') |
| 210 | { |
| 211 | len = end - start + 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 212 | temp = (char *)xmalloc (len + 1); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 213 | strncpy (temp, rl_line_buffer + start, len); |
| 214 | temp[len] = '\0'; |
| 215 | homedir = tilde_expand (temp); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 216 | xfree (temp); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 217 | |
| 218 | _rl_replace_text (homedir, start, end); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 219 | xfree (homedir); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return (0); |
| 223 | } |
| 224 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 225 | #if defined (USE_VARARGS) |
| 226 | void |
| 227 | #if defined (PREFER_STDARG) |
| 228 | _rl_ttymsg (const char *format, ...) |
| 229 | #else |
| 230 | _rl_ttymsg (va_alist) |
| 231 | va_dcl |
| 232 | #endif |
| 233 | { |
| 234 | va_list args; |
| 235 | #if defined (PREFER_VARARGS) |
| 236 | char *format; |
| 237 | #endif |
| 238 | |
| 239 | #if defined (PREFER_STDARG) |
| 240 | va_start (args, format); |
| 241 | #else |
| 242 | va_start (args); |
| 243 | format = va_arg (args, char *); |
| 244 | #endif |
| 245 | |
| 246 | fprintf (stderr, "readline: "); |
| 247 | vfprintf (stderr, format, args); |
| 248 | fprintf (stderr, "\n"); |
| 249 | fflush (stderr); |
| 250 | |
| 251 | va_end (args); |
| 252 | |
| 253 | rl_forced_update_display (); |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | #if defined (PREFER_STDARG) |
| 258 | _rl_errmsg (const char *format, ...) |
| 259 | #else |
| 260 | _rl_errmsg (va_alist) |
| 261 | va_dcl |
| 262 | #endif |
| 263 | { |
| 264 | va_list args; |
| 265 | #if defined (PREFER_VARARGS) |
| 266 | char *format; |
| 267 | #endif |
| 268 | |
| 269 | #if defined (PREFER_STDARG) |
| 270 | va_start (args, format); |
| 271 | #else |
| 272 | va_start (args); |
| 273 | format = va_arg (args, char *); |
| 274 | #endif |
| 275 | |
| 276 | fprintf (stderr, "readline: "); |
| 277 | vfprintf (stderr, format, args); |
| 278 | fprintf (stderr, "\n"); |
| 279 | fflush (stderr); |
| 280 | |
| 281 | va_end (args); |
| 282 | } |
| 283 | |
| 284 | #else /* !USE_VARARGS */ |
| 285 | void |
| 286 | _rl_ttymsg (format, arg1, arg2) |
| 287 | char *format; |
| 288 | { |
| 289 | fprintf (stderr, "readline: "); |
| 290 | fprintf (stderr, format, arg1, arg2); |
| 291 | fprintf (stderr, "\n"); |
| 292 | |
| 293 | rl_forced_update_display (); |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | _rl_errmsg (format, arg1, arg2) |
| 298 | char *format; |
| 299 | { |
| 300 | fprintf (stderr, "readline: "); |
| 301 | fprintf (stderr, format, arg1, arg2); |
| 302 | fprintf (stderr, "\n"); |
| 303 | } |
| 304 | #endif /* !USE_VARARGS */ |
| 305 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 306 | /* **************************************************************** */ |
| 307 | /* */ |
| 308 | /* String Utility Functions */ |
| 309 | /* */ |
| 310 | /* **************************************************************** */ |
| 311 | |
| 312 | /* Determine if s2 occurs in s1. If so, return a pointer to the |
| 313 | match in s1. The compare is case insensitive. */ |
| 314 | char * |
| 315 | _rl_strindex (s1, s2) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 316 | register const char *s1, *s2; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 317 | { |
| 318 | register int i, l, len; |
| 319 | |
| 320 | for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++) |
| 321 | if (_rl_strnicmp (s1 + i, s2, l) == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 322 | return ((char *) (s1 + i)); |
| 323 | return ((char *)NULL); |
| 324 | } |
| 325 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 326 | #ifndef HAVE_STRPBRK |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 327 | /* Find the first occurrence in STRING1 of any character from STRING2. |
| 328 | Return a pointer to the character in STRING1. */ |
| 329 | char * |
| 330 | _rl_strpbrk (string1, string2) |
| 331 | const char *string1, *string2; |
| 332 | { |
| 333 | register const char *scan; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 334 | #if defined (HANDLE_MULTIBYTE) |
| 335 | mbstate_t ps; |
| 336 | register int i, v; |
| 337 | |
| 338 | memset (&ps, 0, sizeof (mbstate_t)); |
| 339 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 340 | |
| 341 | for (; *string1; string1++) |
| 342 | { |
| 343 | for (scan = string2; *scan; scan++) |
| 344 | { |
| 345 | if (*string1 == *scan) |
| 346 | return ((char *)string1); |
| 347 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 348 | #if defined (HANDLE_MULTIBYTE) |
| 349 | if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) |
| 350 | { |
| 351 | v = _rl_get_char_len (string1, &ps); |
| 352 | if (v > 1) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 353 | string1 += v - 1; /* -1 to account for auto-increment in loop */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 354 | } |
| 355 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 356 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 357 | return ((char *)NULL); |
| 358 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 359 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 360 | |
| 361 | #if !defined (HAVE_STRCASECMP) |
| 362 | /* Compare at most COUNT characters from string1 to string2. Case |
| 363 | doesn't matter. */ |
| 364 | int |
| 365 | _rl_strnicmp (string1, string2, count) |
| 366 | char *string1, *string2; |
| 367 | int count; |
| 368 | { |
| 369 | register char ch1, ch2; |
| 370 | |
| 371 | while (count) |
| 372 | { |
| 373 | ch1 = *string1++; |
| 374 | ch2 = *string2++; |
| 375 | if (_rl_to_upper(ch1) == _rl_to_upper(ch2)) |
| 376 | count--; |
| 377 | else |
| 378 | break; |
| 379 | } |
| 380 | return (count); |
| 381 | } |
| 382 | |
| 383 | /* strcmp (), but caseless. */ |
| 384 | int |
| 385 | _rl_stricmp (string1, string2) |
| 386 | char *string1, *string2; |
| 387 | { |
| 388 | register char ch1, ch2; |
| 389 | |
| 390 | while (*string1 && *string2) |
| 391 | { |
| 392 | ch1 = *string1++; |
| 393 | ch2 = *string2++; |
| 394 | if (_rl_to_upper(ch1) != _rl_to_upper(ch2)) |
| 395 | return (1); |
| 396 | } |
| 397 | return (*string1 - *string2); |
| 398 | } |
| 399 | #endif /* !HAVE_STRCASECMP */ |
| 400 | |
| 401 | /* Stupid comparison routine for qsort () ing strings. */ |
| 402 | int |
| 403 | _rl_qsort_string_compare (s1, s2) |
| 404 | char **s1, **s2; |
| 405 | { |
| 406 | #if defined (HAVE_STRCOLL) |
| 407 | return (strcoll (*s1, *s2)); |
| 408 | #else |
| 409 | int result; |
| 410 | |
| 411 | result = **s1 - **s2; |
| 412 | if (result == 0) |
| 413 | result = strcmp (*s1, *s2); |
| 414 | |
| 415 | return result; |
| 416 | #endif |
| 417 | } |
| 418 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 419 | /* Function equivalents for the macros defined in chardefs.h. */ |
| 420 | #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 421 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 422 | FUNCTION_FOR_MACRO (_rl_digit_p) |
| 423 | FUNCTION_FOR_MACRO (_rl_digit_value) |
| 424 | FUNCTION_FOR_MACRO (_rl_lowercase_p) |
| 425 | FUNCTION_FOR_MACRO (_rl_pure_alphabetic) |
| 426 | FUNCTION_FOR_MACRO (_rl_to_lower) |
| 427 | FUNCTION_FOR_MACRO (_rl_to_upper) |
| 428 | FUNCTION_FOR_MACRO (_rl_uppercase_p) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 429 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 430 | /* A convenience function, to force memory deallocation to be performed |
| 431 | by readline. DLLs on Windows apparently require this. */ |
| 432 | void |
| 433 | rl_free (mem) |
| 434 | void *mem; |
| 435 | { |
| 436 | if (mem) |
| 437 | free (mem); |
| 438 | } |
| 439 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 440 | /* Backwards compatibility, now that savestring has been removed from |
| 441 | all `public' readline header files. */ |
| 442 | #undef _rl_savestring |
| 443 | char * |
| 444 | _rl_savestring (s) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 445 | const char *s; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 446 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 447 | return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s))); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 448 | } |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 449 | |
| 450 | #if defined (USE_VARARGS) |
| 451 | static FILE *_rl_tracefp; |
| 452 | |
| 453 | void |
| 454 | #if defined (PREFER_STDARG) |
| 455 | _rl_trace (const char *format, ...) |
| 456 | #else |
| 457 | _rl_trace (va_alist) |
| 458 | va_dcl |
| 459 | #endif |
| 460 | { |
| 461 | va_list args; |
| 462 | #if defined (PREFER_VARARGS) |
| 463 | char *format; |
| 464 | #endif |
| 465 | |
| 466 | #if defined (PREFER_STDARG) |
| 467 | va_start (args, format); |
| 468 | #else |
| 469 | va_start (args); |
| 470 | format = va_arg (args, char *); |
| 471 | #endif |
| 472 | |
| 473 | if (_rl_tracefp == 0) |
| 474 | _rl_tropen (); |
| 475 | vfprintf (_rl_tracefp, format, args); |
| 476 | fprintf (_rl_tracefp, "\n"); |
| 477 | fflush (_rl_tracefp); |
| 478 | |
| 479 | va_end (args); |
| 480 | } |
| 481 | |
| 482 | int |
| 483 | _rl_tropen () |
| 484 | { |
| 485 | char fnbuf[128]; |
| 486 | |
| 487 | if (_rl_tracefp) |
| 488 | fclose (_rl_tracefp); |
| 489 | sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid()); |
| 490 | unlink(fnbuf); |
| 491 | _rl_tracefp = fopen (fnbuf, "w+"); |
| 492 | return _rl_tracefp != 0; |
| 493 | } |
| 494 | |
| 495 | int |
| 496 | _rl_trclose () |
| 497 | { |
| 498 | int r; |
| 499 | |
| 500 | r = fclose (_rl_tracefp); |
| 501 | _rl_tracefp = 0; |
| 502 | return r; |
| 503 | } |
| 504 | |
| 505 | #endif |