home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / readline.zip / readline-2.1 / util.c < prev    next >
C/C++ Source or Header  |  1997-03-26  |  6KB  |  303 lines

  1. /* util.c -- readline utility functions */
  2.  
  3. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the GNU Readline Library, a library for
  6.    reading lines of text with interactive input and history editing.
  7.  
  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 1, or
  11.    (at your option) any later version.
  12.  
  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.
  17.  
  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.    675 Mass Ave, Cambridge, MA 02139, USA. */
  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.  
  48. #if defined (TIOCSTAT_IN_SYS_IOCTL)
  49. #  include <sys/ioctl.h>
  50. #endif /* TIOCSTAT_IN_SYS_IOCTL */
  51.  
  52. /* Some standard library routines. */
  53. #include "readline.h"
  54.  
  55. #define SWAP(s, e)  do { int t; t = s; s = e; e = t; } while (0)
  56.  
  57. /* Pseudo-globals imported from readline.c */
  58. extern int readline_echoing_p;
  59. extern procenv_t readline_top_level;
  60. extern int rl_line_buffer_len;
  61. extern Function *rl_last_func;
  62.  
  63. extern int _rl_defining_kbd_macro;
  64. extern char *_rl_executing_macro;
  65.  
  66. /* Pseudo-global functions imported from other library files. */
  67. extern void _rl_pop_executing_macro ();
  68. extern void _rl_set_the_line ();
  69. extern void _rl_init_argument ();
  70.  
  71. extern char *xmalloc (), *xrealloc ();
  72.  
  73. /* **************************************************************** */
  74. /*                                    */
  75. /*            Utility Functions                */
  76. /*                                    */
  77. /* **************************************************************** */
  78.  
  79. /* Return 0 if C is not a member of the class of characters that belong
  80.    in words, or 1 if it is. */
  81.  
  82. int _rl_allow_pathname_alphabetic_chars = 0;
  83. static char *pathname_alphabetic_chars = "/-_=~.#$";
  84.  
  85. int
  86. alphabetic (c)
  87.      int c;
  88. {
  89.   if (ALPHABETIC (c))
  90.     return (1);
  91.  
  92.   return (_rl_allow_pathname_alphabetic_chars &&
  93.         strchr (pathname_alphabetic_chars, c) != NULL);
  94. }
  95.  
  96. /* How to abort things. */
  97. int
  98. _rl_abort_internal ()
  99. {
  100.   ding ();
  101.   rl_clear_message ();
  102.   _rl_init_argument ();
  103.   rl_pending_input = 0;
  104.  
  105.   _rl_defining_kbd_macro = 0;
  106.   while (_rl_executing_macro)
  107.     _rl_pop_executing_macro ();
  108.  
  109.   rl_last_func = (Function *)NULL;
  110.   longjmp (readline_top_level, 1);
  111.   return (0);
  112. }
  113.  
  114. int
  115. rl_abort (count, key)
  116.      int count, key;
  117. {
  118.   return (_rl_abort_internal ());
  119. }
  120.  
  121. int
  122. rl_tty_status (count, key)
  123.      int count, key;
  124. {
  125. #if defined (TIOCSTAT)
  126.   ioctl (1, TIOCSTAT, (char *)0);
  127.   rl_refresh_line ();
  128. #else
  129.   ding ();
  130. #endif
  131.   return 0;
  132. }
  133.  
  134. /* Return a copy of the string between FROM and TO.
  135.    FROM is inclusive, TO is not. */
  136. char *
  137. rl_copy_text (from, to)
  138.      int from, to;
  139. {
  140.   register int length;
  141.   char *copy;
  142.  
  143.   /* Fix it if the caller is confused. */
  144.   if (from > to)
  145.     SWAP (from, to);
  146.  
  147.   length = to - from;
  148.   copy = xmalloc (1 + length);
  149.   strncpy (copy, rl_line_buffer + from, length);
  150.   copy[length] = '\0';
  151.   return (copy);
  152. }
  153.  
  154. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  155.    LEN characters. */
  156. void
  157. rl_extend_line_buffer (len)
  158.      int len;
  159. {
  160.   while (len >= rl_line_buffer_len)
  161.     {
  162.       rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
  163.       rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
  164.     }
  165.  
  166.   _rl_set_the_line ();
  167. }
  168.  
  169. /* **************************************************************** */
  170. /*                                    */
  171. /*            String Utility Functions            */
  172. /*                                    */
  173. /* **************************************************************** */
  174.  
  175. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  176.    match in s1.  The compare is case insensitive. */
  177. char *
  178. _rl_strindex (s1, s2)
  179.      register char *s1, *s2;
  180. {
  181.   register int i, l, len;
  182.  
  183.   for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
  184.     if (_rl_strnicmp (s1 + i, s2, l) == 0)
  185.       return (s1 + i);
  186.   return ((char *)NULL);
  187. }
  188.  
  189. #if !defined (HAVE_STRCASECMP)
  190. /* Compare at most COUNT characters from string1 to string2.  Case
  191.    doesn't matter. */
  192. int
  193. _rl_strnicmp (string1, string2, count)
  194.      char *string1, *string2;
  195.      int count;
  196. {
  197.   register char ch1, ch2;
  198.  
  199.   while (count)
  200.     {
  201.       ch1 = *string1++;
  202.       ch2 = *string2++;
  203.       if (_rl_to_upper(ch1) == _rl_to_upper(ch2))
  204.     count--;
  205.       else
  206.         break;
  207.     }
  208.   return (count);
  209. }
  210.  
  211. /* strcmp (), but caseless. */
  212. int
  213. _rl_stricmp (string1, string2)
  214.      char *string1, *string2;
  215. {
  216.   register char ch1, ch2;
  217.  
  218.   while (*string1 && *string2)
  219.     {
  220.       ch1 = *string1++;
  221.       ch2 = *string2++;
  222.       if (_rl_to_upper(ch1) != _rl_to_upper(ch2))
  223.     return (1);
  224.     }
  225.   return (*string1 - *string2);
  226. }
  227. #endif /* !HAVE_STRCASECMP */
  228.  
  229. /* Stupid comparison routine for qsort () ing strings. */
  230. int
  231. _rl_qsort_string_compare (s1, s2)
  232.   char **s1, **s2;
  233. {
  234. #if defined (HAVE_STRCOLL)
  235.   return (strcoll (*s1, *s2));
  236. #else
  237.   int result;
  238.  
  239.   result = **s1 - **s2;
  240.   if (result == 0)
  241.     result = strcmp (*s1, *s2);
  242.  
  243.   return result;
  244. #endif
  245. }
  246.  
  247. /* Function equivalents for the macros defined in chartypes.h. */
  248. #undef _rl_uppercase_p
  249. int
  250. _rl_uppercase_p (c)
  251.      int c;
  252. {
  253.   return (isupper (c));
  254. }
  255.  
  256. #undef _rl_lowercase_p
  257. int
  258. _rl_lowercase_p (c)
  259.      int c;
  260. {
  261.   return (islower (c));
  262. }
  263.  
  264. #undef _rl_pure_alphabetic
  265. int
  266. _rl_pure_alphabetic (c)
  267.      int c;
  268. {
  269.   return (isupper (c) || islower (c));
  270. }
  271.  
  272. #undef _rl_digit_p
  273. int
  274. _rl_digit_p (c)
  275.      int c;
  276. {
  277.   return (isdigit (c));
  278. }
  279.  
  280. #undef _rl_to_lower
  281. int
  282. _rl_to_lower (c)
  283.      int c;
  284. {
  285.   return (isupper (c) ? tolower (c) : c);
  286. }
  287.  
  288. #undef _rl_to_upper
  289. int
  290. _rl_to_upper (c)
  291.      int c;
  292. {
  293.   return (islower (c) ? toupper (c) : c);
  294. }
  295.  
  296. #undef _rl_digit_value
  297. int
  298. _rl_digit_value (c)
  299.      int c;
  300. {
  301.   return (isdigit (c) ? c - '0' : c);
  302. }
  303.