1 /* util.c -- readline utility functions */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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. */
48 #if defined (TIOCSTAT_IN_SYS_IOCTL)
49 # include <sys/ioctl.h>
50 #endif /* TIOCSTAT_IN_SYS_IOCTL */
52 /* Some standard library routines. */
55 #include "rlprivate.h"
58 #define SWAP(s, e) do { int t; t = s; s = e; e = t; } while (0)
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 *pathname_alphabetic_chars
= "/-_=~.#$";
79 return (_rl_allow_pathname_alphabetic_chars
&&
80 strchr (pathname_alphabetic_chars
, c
) != NULL
);
83 /* How to abort things. */
90 rl_clear_pending_input ();
92 _rl_defining_kbd_macro
= 0;
93 while (rl_executing_macro
)
94 _rl_pop_executing_macro ();
96 rl_last_func
= (rl_command_func_t
*)NULL
;
97 longjmp (readline_top_level
, 1);
102 rl_abort (count
, key
)
105 return (_rl_abort_internal ());
109 rl_tty_status (count
, key
)
112 #if defined (TIOCSTAT)
113 ioctl (1, TIOCSTAT
, (char *)0);
114 rl_refresh_line (count
, key
);
121 /* Return a copy of the string between FROM and TO.
122 FROM is inclusive, TO is not. */
124 rl_copy_text (from
, to
)
130 /* Fix it if the caller is confused. */
135 copy
= xmalloc (1 + length
);
136 strncpy (copy
, rl_line_buffer
+ from
, length
);
141 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
144 rl_extend_line_buffer (len
)
147 while (len
>= rl_line_buffer_len
)
149 rl_line_buffer_len
+= DEFAULT_BUFFER_SIZE
;
150 rl_line_buffer
= xrealloc (rl_line_buffer
, rl_line_buffer_len
);
157 /* A function for simple tilde expansion. */
159 rl_tilde_expand (ignore
, key
)
162 register int start
, end
;
163 char *homedir
, *temp
;
169 if (rl_point
== rl_end
&& rl_line_buffer
[rl_point
] == '~')
171 homedir
= tilde_expand ("~");
172 _rl_replace_text (homedir
, start
, end
);
175 else if (rl_line_buffer
[start
] != '~')
177 for (; !whitespace (rl_line_buffer
[start
]) && start
>= 0; start
--)
185 while (whitespace (rl_line_buffer
[end
]) == 0 && end
< rl_end
);
187 if (whitespace (rl_line_buffer
[end
]) || end
>= rl_end
)
190 /* If the first character of the current word is a tilde, perform
191 tilde expansion and insert the result. If not a tilde, do
193 if (rl_line_buffer
[start
] == '~')
195 len
= end
- start
+ 1;
196 temp
= xmalloc (len
+ 1);
197 strncpy (temp
, rl_line_buffer
+ start
, len
);
199 homedir
= tilde_expand (temp
);
202 _rl_replace_text (homedir
, start
, end
);
208 /* **************************************************************** */
210 /* String Utility Functions */
212 /* **************************************************************** */
214 /* Determine if s2 occurs in s1. If so, return a pointer to the
215 match in s1. The compare is case insensitive. */
217 _rl_strindex (s1
, s2
)
218 register const char *s1
, *s2
;
220 register int i
, l
, len
;
222 for (i
= 0, l
= strlen (s2
), len
= strlen (s1
); (len
- i
) >= l
; i
++)
223 if (_rl_strnicmp (s1
+ i
, s2
, l
) == 0)
224 return ((char *) (s1
+ i
));
225 return ((char *)NULL
);
228 /* Find the first occurrence in STRING1 of any character from STRING2.
229 Return a pointer to the character in STRING1. */
231 _rl_strpbrk (string1
, string2
)
232 const char *string1
, *string2
;
234 register const char *scan
;
236 for (; *string1
; string1
++)
238 for (scan
= string2
; *scan
; scan
++)
240 if (*string1
== *scan
)
241 return ((char *)string1
);
244 return ((char *)NULL
);
247 #if !defined (HAVE_STRCASECMP)
248 /* Compare at most COUNT characters from string1 to string2. Case
251 _rl_strnicmp (string1
, string2
, count
)
252 char *string1
, *string2
;
255 register char ch1
, ch2
;
261 if (_rl_to_upper(ch1
) == _rl_to_upper(ch2
))
269 /* strcmp (), but caseless. */
271 _rl_stricmp (string1
, string2
)
272 char *string1
, *string2
;
274 register char ch1
, ch2
;
276 while (*string1
&& *string2
)
280 if (_rl_to_upper(ch1
) != _rl_to_upper(ch2
))
283 return (*string1
- *string2
);
285 #endif /* !HAVE_STRCASECMP */
287 /* Stupid comparison routine for qsort () ing strings. */
289 _rl_qsort_string_compare (s1
, s2
)
292 #if defined (HAVE_STRCOLL)
293 return (strcoll (*s1
, *s2
));
297 result
= **s1
- **s2
;
299 result
= strcmp (*s1
, *s2
);
305 /* Function equivalents for the macros defined in chartypes.h. */
306 #undef _rl_uppercase_p
311 return (isupper (c
));
314 #undef _rl_lowercase_p
319 return (islower (c
));
322 #undef _rl_pure_alphabetic
324 _rl_pure_alphabetic (c
)
327 return (isupper (c
) || islower (c
));
335 return (isdigit (c
));
343 return (isupper (c
) ? tolower (c
) : c
);
351 return (islower (c
) ? toupper (c
) : c
);
354 #undef _rl_digit_value
359 return (isdigit (c
) ? c
- '0' : c
);
362 /* Backwards compatibility, now that savestring has been removed from
363 all `public' readline header files. */
364 #undef _rl_savestring
369 return (strcpy (xmalloc (1 + (int)strlen (s
)), (s
)));