util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* util.c -- readline utility functions */
  2. /* Copyright (C) 1987-2010 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library
  4. for reading lines of text with interactive input and history editing.
  5. Readline is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Readline is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (HAVE_CONFIG_H)
  18. # include <config.h>
  19. #endif
  20. #include <sys/types.h>
  21. #include <fcntl.h>
  22. #include "posixjmp.h"
  23. #if defined (HAVE_UNISTD_H)
  24. # include <unistd.h> /* for _POSIX_VERSION */
  25. #endif /* HAVE_UNISTD_H */
  26. #if defined (HAVE_STDLIB_H)
  27. # include <stdlib.h>
  28. #else
  29. # include "ansi_stdlib.h"
  30. #endif /* HAVE_STDLIB_H */
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. /* System-specific feature definitions and include files. */
  34. #include "rldefs.h"
  35. #include "rlmbutil.h"
  36. #if defined (TIOCSTAT_IN_SYS_IOCTL)
  37. # include <sys/ioctl.h>
  38. #endif /* TIOCSTAT_IN_SYS_IOCTL */
  39. /* Some standard library routines. */
  40. #include "readline.h"
  41. #include "rlprivate.h"
  42. #include "xmalloc.h"
  43. /* **************************************************************** */
  44. /* */
  45. /* Utility Functions */
  46. /* */
  47. /* **************************************************************** */
  48. /* Return 0 if C is not a member of the class of characters that belong
  49. in words, or 1 if it is. */
  50. int _rl_allow_pathname_alphabetic_chars = 0;
  51. static const char * const pathname_alphabetic_chars = "/-_=~.#$";
  52. int
  53. rl_alphabetic (c)
  54. int c;
  55. {
  56. if (ALPHABETIC (c))
  57. return (1);
  58. return (_rl_allow_pathname_alphabetic_chars &&
  59. strchr (pathname_alphabetic_chars, c) != NULL);
  60. }
  61. #if defined (HANDLE_MULTIBYTE)
  62. int
  63. _rl_walphabetic (wchar_t wc)
  64. {
  65. int c;
  66. if (iswalnum (wc))
  67. return (1);
  68. c = wc & 0177;
  69. return (_rl_allow_pathname_alphabetic_chars &&
  70. strchr (pathname_alphabetic_chars, c) != NULL);
  71. }
  72. #endif
  73. /* How to abort things. */
  74. int
  75. _rl_abort_internal ()
  76. {
  77. rl_ding ();
  78. rl_clear_message ();
  79. _rl_reset_argument ();
  80. rl_clear_pending_input ();
  81. RL_UNSETSTATE (RL_STATE_MACRODEF);
  82. while (rl_executing_macro)
  83. _rl_pop_executing_macro ();
  84. rl_last_func = (rl_command_func_t *)NULL;
  85. longjmp (_rl_top_level, 1);
  86. return (0);
  87. }
  88. int
  89. rl_abort (count, key)
  90. int count, key;
  91. {
  92. return (_rl_abort_internal ());
  93. }
  94. int
  95. _rl_null_function (count, key)
  96. int count, key;
  97. {
  98. return 0;
  99. }
  100. int
  101. rl_tty_status (count, key)
  102. int count, key;
  103. {
  104. #if defined (TIOCSTAT)
  105. ioctl (1, TIOCSTAT, (char *)0);
  106. rl_refresh_line (count, key);
  107. #else
  108. rl_ding ();
  109. #endif
  110. return 0;
  111. }
  112. /* Return a copy of the string between FROM and TO.
  113. FROM is inclusive, TO is not. */
  114. char *
  115. rl_copy_text (from, to)
  116. int from, to;
  117. {
  118. register int length;
  119. char *copy;
  120. /* Fix it if the caller is confused. */
  121. if (from > to)
  122. SWAP (from, to);
  123. length = to - from;
  124. copy = (char *)xmalloc (1 + length);
  125. strncpy (copy, rl_line_buffer + from, length);
  126. copy[length] = '\0';
  127. return (copy);
  128. }
  129. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  130. LEN characters. */
  131. void
  132. rl_extend_line_buffer (len)
  133. int len;
  134. {
  135. while (len >= rl_line_buffer_len)
  136. {
  137. rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
  138. rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len);
  139. }
  140. _rl_set_the_line ();
  141. }
  142. /* A function for simple tilde expansion. */
  143. int
  144. rl_tilde_expand (ignore, key)
  145. int ignore, key;
  146. {
  147. register int start, end;
  148. char *homedir, *temp;
  149. int len;
  150. end = rl_point;
  151. start = end - 1;
  152. if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
  153. {
  154. homedir = tilde_expand ("~");
  155. _rl_replace_text (homedir, start, end);
  156. xfree (homedir);
  157. return (0);
  158. }
  159. else if (rl_line_buffer[start] != '~')
  160. {
  161. for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--)
  162. ;
  163. start++;
  164. }
  165. end = start;
  166. do
  167. end++;
  168. while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end);
  169. if (whitespace (rl_line_buffer[end]) || end >= rl_end)
  170. end--;
  171. /* If the first character of the current word is a tilde, perform
  172. tilde expansion and insert the result. If not a tilde, do
  173. nothing. */
  174. if (rl_line_buffer[start] == '~')
  175. {
  176. len = end - start + 1;
  177. temp = (char *)xmalloc (len + 1);
  178. strncpy (temp, rl_line_buffer + start, len);
  179. temp[len] = '\0';
  180. homedir = tilde_expand (temp);
  181. xfree (temp);
  182. _rl_replace_text (homedir, start, end);
  183. xfree (homedir);
  184. }
  185. return (0);
  186. }
  187. #if defined (USE_VARARGS)
  188. void
  189. #if defined (PREFER_STDARG)
  190. _rl_ttymsg (const char *format, ...)
  191. #else
  192. _rl_ttymsg (va_alist)
  193. va_dcl
  194. #endif
  195. {
  196. va_list args;
  197. #if defined (PREFER_VARARGS)
  198. char *format;
  199. #endif
  200. #if defined (PREFER_STDARG)
  201. va_start (args, format);
  202. #else
  203. va_start (args);
  204. format = va_arg (args, char *);
  205. #endif
  206. fprintf (stderr, "readline: ");
  207. vfprintf (stderr, format, args);
  208. fprintf (stderr, "\n");
  209. fflush (stderr);
  210. va_end (args);
  211. rl_forced_update_display ();
  212. }
  213. void
  214. #if defined (PREFER_STDARG)
  215. _rl_errmsg (const char *format, ...)
  216. #else
  217. _rl_errmsg (va_alist)
  218. va_dcl
  219. #endif
  220. {
  221. va_list args;
  222. #if defined (PREFER_VARARGS)
  223. char *format;
  224. #endif
  225. #if defined (PREFER_STDARG)
  226. va_start (args, format);
  227. #else
  228. va_start (args);
  229. format = va_arg (args, char *);
  230. #endif
  231. fprintf (stderr, "readline: ");
  232. vfprintf (stderr, format, args);
  233. fprintf (stderr, "\n");
  234. fflush (stderr);
  235. va_end (args);
  236. }
  237. #else /* !USE_VARARGS */
  238. void
  239. _rl_ttymsg (format, arg1, arg2)
  240. char *format;
  241. {
  242. fprintf (stderr, "readline: ");
  243. fprintf (stderr, format, arg1, arg2);
  244. fprintf (stderr, "\n");
  245. rl_forced_update_display ();
  246. }
  247. void
  248. _rl_errmsg (format, arg1, arg2)
  249. char *format;
  250. {
  251. fprintf (stderr, "readline: ");
  252. fprintf (stderr, format, arg1, arg2);
  253. fprintf (stderr, "\n");
  254. }
  255. #endif /* !USE_VARARGS */
  256. /* **************************************************************** */
  257. /* */
  258. /* String Utility Functions */
  259. /* */
  260. /* **************************************************************** */
  261. /* Determine if s2 occurs in s1. If so, return a pointer to the
  262. match in s1. The compare is case insensitive. */
  263. char *
  264. _rl_strindex (s1, s2)
  265. register const char *s1, *s2;
  266. {
  267. register int i, l, len;
  268. for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++)
  269. if (_rl_strnicmp (s1 + i, s2, l) == 0)
  270. return ((char *) (s1 + i));
  271. return ((char *)NULL);
  272. }
  273. #ifndef HAVE_STRPBRK
  274. /* Find the first occurrence in STRING1 of any character from STRING2.
  275. Return a pointer to the character in STRING1. */
  276. char *
  277. _rl_strpbrk (string1, string2)
  278. const char *string1, *string2;
  279. {
  280. register const char *scan;
  281. #if defined (HANDLE_MULTIBYTE)
  282. mbstate_t ps;
  283. register int i, v;
  284. memset (&ps, 0, sizeof (mbstate_t));
  285. #endif
  286. for (; *string1; string1++)
  287. {
  288. for (scan = string2; *scan; scan++)
  289. {
  290. if (*string1 == *scan)
  291. return ((char *)string1);
  292. }
  293. #if defined (HANDLE_MULTIBYTE)
  294. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  295. {
  296. v = _rl_get_char_len (string1, &ps);
  297. if (v > 1)
  298. string1 += v - 1; /* -1 to account for auto-increment in loop */
  299. }
  300. #endif
  301. }
  302. return ((char *)NULL);
  303. }
  304. #endif
  305. #if !defined (HAVE_STRCASECMP)
  306. /* Compare at most COUNT characters from string1 to string2. Case
  307. doesn't matter (strncasecmp). */
  308. int
  309. _rl_strnicmp (string1, string2, count)
  310. char *string1, *string2;
  311. int count;
  312. {
  313. register char *s1, *s2;
  314. int d;
  315. if (count <= 0 || (string1 == string2))
  316. return 0;
  317. s1 = string1;
  318. s2 = string2;
  319. do
  320. {
  321. d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */
  322. if (d != 0)
  323. return d;
  324. if (*s1++ == '\0')
  325. break;
  326. s2++;
  327. }
  328. while (--count != 0);
  329. return (0);
  330. }
  331. /* strcmp (), but caseless (strcasecmp). */
  332. int
  333. _rl_stricmp (string1, string2)
  334. char *string1, *string2;
  335. {
  336. register char *s1, *s2;
  337. int d;
  338. s1 = string1;
  339. s2 = string2;
  340. if (s1 == s2)
  341. return 0;
  342. while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0)
  343. {
  344. if (*s1++ == '\0')
  345. return 0;
  346. s2++;
  347. }
  348. return (d);
  349. }
  350. #endif /* !HAVE_STRCASECMP */
  351. /* Stupid comparison routine for qsort () ing strings. */
  352. int
  353. _rl_qsort_string_compare (s1, s2)
  354. char **s1, **s2;
  355. {
  356. #if defined (HAVE_STRCOLL)
  357. return (strcoll (*s1, *s2));
  358. #else
  359. int result;
  360. result = **s1 - **s2;
  361. if (result == 0)
  362. result = strcmp (*s1, *s2);
  363. return result;
  364. #endif
  365. }
  366. /* Function equivalents for the macros defined in chardefs.h. */
  367. #define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); }
  368. FUNCTION_FOR_MACRO (_rl_digit_p)
  369. FUNCTION_FOR_MACRO (_rl_digit_value)
  370. FUNCTION_FOR_MACRO (_rl_lowercase_p)
  371. FUNCTION_FOR_MACRO (_rl_pure_alphabetic)
  372. FUNCTION_FOR_MACRO (_rl_to_lower)
  373. FUNCTION_FOR_MACRO (_rl_to_upper)
  374. FUNCTION_FOR_MACRO (_rl_uppercase_p)
  375. /* A convenience function, to force memory deallocation to be performed
  376. by readline. DLLs on Windows apparently require this. */
  377. void
  378. rl_free (mem)
  379. void *mem;
  380. {
  381. if (mem)
  382. free (mem);
  383. }
  384. /* Backwards compatibility, now that savestring has been removed from
  385. all `public' readline header files. */
  386. #undef _rl_savestring
  387. char *
  388. _rl_savestring (s)
  389. const char *s;
  390. {
  391. return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s)));
  392. }
  393. #if defined (USE_VARARGS)
  394. static FILE *_rl_tracefp;
  395. void
  396. #if defined (PREFER_STDARG)
  397. _rl_trace (const char *format, ...)
  398. #else
  399. _rl_trace (va_alist)
  400. va_dcl
  401. #endif
  402. {
  403. va_list args;
  404. #if defined (PREFER_VARARGS)
  405. char *format;
  406. #endif
  407. #if defined (PREFER_STDARG)
  408. va_start (args, format);
  409. #else
  410. va_start (args);
  411. format = va_arg (args, char *);
  412. #endif
  413. if (_rl_tracefp == 0)
  414. _rl_tropen ();
  415. vfprintf (_rl_tracefp, format, args);
  416. fprintf (_rl_tracefp, "\n");
  417. fflush (_rl_tracefp);
  418. va_end (args);
  419. }
  420. int
  421. _rl_tropen ()
  422. {
  423. char fnbuf[128];
  424. if (_rl_tracefp)
  425. fclose (_rl_tracefp);
  426. sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid());
  427. unlink(fnbuf);
  428. _rl_tracefp = fopen (fnbuf, "w+");
  429. return _rl_tracefp != 0;
  430. }
  431. int
  432. _rl_trclose ()
  433. {
  434. int r;
  435. r = fclose (_rl_tracefp);
  436. _rl_tracefp = 0;
  437. return r;
  438. }
  439. #endif