00001 /* util.c -- readline utility functions */ 00002 00003 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc. 00004 00005 This file is part of the GNU Readline Library, a library for 00006 reading lines of text with interactive input and history editing. 00007 00008 The GNU Readline Library is free software; you can redistribute it 00009 and/or modify it under the terms of the GNU General Public License 00010 as published by the Free Software Foundation; either version 2, or 00011 (at your option) any later version. 00012 00013 The GNU Readline Library is distributed in the hope that it will be 00014 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00015 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 The GNU General Public License is often shipped with GNU software, and 00019 is generally kept in a file called COPYING or LICENSE. If you do not 00020 have a copy of the license, write to the Free Software Foundation, 00021 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 00022 #define READLINE_LIBRARY 00023 00024 #include "config_readline.h" 00025 00026 #include <sys/types.h> 00027 #include <fcntl.h> 00028 #include "posixjmp.h" 00029 00030 #if defined (HAVE_UNISTD_H) 00031 # include <unistd.h> /* for _POSIX_VERSION */ 00032 #endif /* HAVE_UNISTD_H */ 00033 00034 #if defined (HAVE_STDLIB_H) 00035 # include <stdlib.h> 00036 #else 00037 # include "ansi_stdlib.h" 00038 #endif /* HAVE_STDLIB_H */ 00039 00040 #include <stdio.h> 00041 #include <ctype.h> 00042 00043 /* System-specific feature definitions and include files. */ 00044 #include "rldefs.h" 00045 00046 #if defined (TIOCSTAT_IN_SYS_IOCTL) 00047 # include <sys/ioctl.h> 00048 #endif /* TIOCSTAT_IN_SYS_IOCTL */ 00049 00050 /* Some standard library routines. */ 00051 #include "readline.h" 00052 00053 #include "rlprivate.h" 00054 #include "xmalloc.h" 00055 00056 /* **************************************************************** */ 00057 /* */ 00058 /* Utility Functions */ 00059 /* */ 00060 /* **************************************************************** */ 00061 00062 /* Return 0 if C is not a member of the class of characters that belong 00063 in words, or 1 if it is. */ 00064 00065 int _rl_allow_pathname_alphabetic_chars = 0; 00066 static const char *pathname_alphabetic_chars = "/-_=~.#$"; 00067 00068 int 00069 rl_alphabetic (c) 00070 int c; 00071 { 00072 if (ALPHABETIC (c)) 00073 return (1); 00074 00075 return (_rl_allow_pathname_alphabetic_chars && 00076 strchr (pathname_alphabetic_chars, c) != NULL); 00077 } 00078 00079 /* How to abort things. */ 00080 int 00081 _rl_abort_internal () 00082 { 00083 rl_ding (); 00084 rl_clear_message (); 00085 _rl_init_argument (); 00086 rl_clear_pending_input (); 00087 00088 RL_UNSETSTATE (RL_STATE_MACRODEF); 00089 while (rl_executing_macro) 00090 _rl_pop_executing_macro (); 00091 00092 rl_last_func = (rl_command_func_t *)NULL; 00093 longjmp (readline_top_level, 1); 00094 return (0); 00095 } 00096 00097 int 00098 rl_abort (count, key) 00099 int count, key; 00100 { 00101 return (_rl_abort_internal ()); 00102 } 00103 00104 int 00105 rl_tty_status (count, key) 00106 int count, key; 00107 { 00108 #if defined (TIOCSTAT) 00109 ioctl (1, TIOCSTAT, (char *)0); 00110 rl_refresh_line (count, key); 00111 #else 00112 rl_ding (); 00113 #endif 00114 return 0; 00115 } 00116 00117 /* Return a copy of the string between FROM and TO. 00118 FROM is inclusive, TO is not. */ 00119 char * 00120 rl_copy_text (from, to) 00121 int from, to; 00122 { 00123 register int length; 00124 char *copy; 00125 00126 /* Fix it if the caller is confused. */ 00127 if (from > to) 00128 SWAP (from, to); 00129 00130 length = to - from; 00131 copy = (char *)xmalloc (1 + length); 00132 strncpy (copy, rl_line_buffer + from, length); 00133 copy[length] = '\0'; 00134 return (copy); 00135 } 00136 00137 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold 00138 LEN characters. */ 00139 void 00140 rl_extend_line_buffer (len) 00141 int len; 00142 { 00143 while (len >= rl_line_buffer_len) 00144 { 00145 rl_line_buffer_len += DEFAULT_BUFFER_SIZE; 00146 rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len); 00147 } 00148 00149 _rl_set_the_line (); 00150 } 00151 00152 00153 /* A function for simple tilde expansion. */ 00154 int 00155 rl_tilde_expand (ignore, key) 00156 int ignore, key; 00157 { 00158 register int start, end; 00159 char *homedir, *temp; 00160 int len; 00161 00162 end = rl_point; 00163 start = end - 1; 00164 00165 if (rl_point == rl_end && rl_line_buffer[rl_point] == '~') 00166 { 00167 homedir = tilde_expand ("~"); 00168 _rl_replace_text (homedir, start, end); 00169 return (0); 00170 } 00171 else if (rl_line_buffer[start] != '~') 00172 { 00173 for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--) 00174 ; 00175 start++; 00176 } 00177 00178 end = start; 00179 do 00180 end++; 00181 while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end); 00182 00183 if (whitespace (rl_line_buffer[end]) || end >= rl_end) 00184 end--; 00185 00186 /* If the first character of the current word is a tilde, perform 00187 tilde expansion and insert the result. If not a tilde, do 00188 nothing. */ 00189 if (rl_line_buffer[start] == '~') 00190 { 00191 len = end - start + 1; 00192 temp = (char *)xmalloc (len + 1); 00193 strncpy (temp, rl_line_buffer + start, len); 00194 temp[len] = '\0'; 00195 homedir = tilde_expand (temp); 00196 free (temp); 00197 00198 _rl_replace_text (homedir, start, end); 00199 } 00200 00201 return (0); 00202 } 00203 00204 /* **************************************************************** */ 00205 /* */ 00206 /* String Utility Functions */ 00207 /* */ 00208 /* **************************************************************** */ 00209 00210 /* Determine if s2 occurs in s1. If so, return a pointer to the 00211 match in s1. The compare is case insensitive. */ 00212 char * 00213 _rl_strindex (s1, s2) 00214 register const char *s1, *s2; 00215 { 00216 register int i, l, len; 00217 00218 for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++) 00219 if (_rl_strnicmp (s1 + i, s2, l) == 0) 00220 return ((char *) (s1 + i)); 00221 return ((char *)NULL); 00222 } 00223 00224 #ifndef HAVE_STRPBRK 00225 /* Find the first occurrence in STRING1 of any character from STRING2. 00226 Return a pointer to the character in STRING1. */ 00227 char * 00228 _rl_strpbrk (string1, string2) 00229 const char *string1, *string2; 00230 { 00231 register const char *scan; 00232 #if defined (HANDLE_MULTIBYTE) 00233 mbstate_t ps; 00234 register int i, v; 00235 00236 memset (&ps, 0, sizeof (mbstate_t)); 00237 #endif 00238 00239 for (; *string1; string1++) 00240 { 00241 for (scan = string2; *scan; scan++) 00242 { 00243 if (*string1 == *scan) 00244 return ((char *)string1); 00245 } 00246 #if defined (HANDLE_MULTIBYTE) 00247 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 00248 { 00249 v = _rl_get_char_len (string1, &ps); 00250 if (v > 1) 00251 string1 += v - 1; /* -1 to account for auto-increment in loop */ 00252 } 00253 #endif 00254 } 00255 return ((char *)NULL); 00256 } 00257 #endif 00258 00259 #if !defined (HAVE_STRCASECMP) 00260 /* Compare at most COUNT characters from string1 to string2. Case 00261 doesn't matter. */ 00262 int 00263 _rl_strnicmp (string1, string2, count) 00264 char *string1, *string2; 00265 int count; 00266 { 00267 register char ch1, ch2; 00268 00269 while (count) 00270 { 00271 ch1 = *string1++; 00272 ch2 = *string2++; 00273 if (_rl_to_upper(ch1) == _rl_to_upper(ch2)) 00274 count--; 00275 else 00276 break; 00277 } 00278 return (count); 00279 } 00280 00281 /* strcmp (), but caseless. */ 00282 int 00283 _rl_stricmp (string1, string2) 00284 char *string1, *string2; 00285 { 00286 register char ch1, ch2; 00287 00288 while (*string1 && *string2) 00289 { 00290 ch1 = *string1++; 00291 ch2 = *string2++; 00292 if (_rl_to_upper(ch1) != _rl_to_upper(ch2)) 00293 return (1); 00294 } 00295 return (*string1 - *string2); 00296 } 00297 #endif /* !HAVE_STRCASECMP */ 00298 00299 /* Stupid comparison routine for qsort () ing strings. */ 00300 int 00301 _rl_qsort_string_compare (s1, s2) 00302 char **s1, **s2; 00303 { 00304 #if defined (HAVE_STRCOLL) 00305 return (strcoll (*s1, *s2)); 00306 #else 00307 int result; 00308 00309 result = **s1 - **s2; 00310 if (result == 0) 00311 result = strcmp (*s1, *s2); 00312 00313 return result; 00314 #endif 00315 } 00316 00317 /* Function equivalents for the macros defined in chardefs.h. */ 00318 #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); } 00319 00320 FUNCTION_FOR_MACRO (_rl_digit_p) 00321 FUNCTION_FOR_MACRO (_rl_digit_value) 00322 FUNCTION_FOR_MACRO (_rl_lowercase_p) 00323 FUNCTION_FOR_MACRO (_rl_pure_alphabetic) 00324 FUNCTION_FOR_MACRO (_rl_to_lower) 00325 FUNCTION_FOR_MACRO (_rl_to_upper) 00326 FUNCTION_FOR_MACRO (_rl_uppercase_p) 00327 00328 /* Backwards compatibility, now that savestring has been removed from 00329 all `public' readline header files. */ 00330 #undef _rl_savestring 00331 char * 00332 _rl_savestring (s) 00333 const char *s; 00334 { 00335 return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s))); 00336 }
1.4.7

