histexpand.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /* histexpand.c -- history expansion. */
  2. /* Copyright (C) 1989-2010 Free Software Foundation, Inc.
  3. This file contains the GNU History Library (History), a set of
  4. routines for managing the text of previously typed lines.
  5. History 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. History 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 History. 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 <stdio.h>
  21. #if defined (HAVE_STDLIB_H)
  22. # include <stdlib.h>
  23. #else
  24. # include "ansi_stdlib.h"
  25. #endif /* HAVE_STDLIB_H */
  26. #if defined (HAVE_UNISTD_H)
  27. # ifndef _MINIX
  28. # include <sys/types.h>
  29. # endif
  30. # include <unistd.h>
  31. #endif
  32. #include "rlmbutil.h"
  33. #include "history.h"
  34. #include "histlib.h"
  35. #include "rlshell.h"
  36. #include "xmalloc.h"
  37. #define HISTORY_WORD_DELIMITERS " \t\n;&()|<>"
  38. #define HISTORY_QUOTE_CHARACTERS "\"'`"
  39. #define slashify_in_quotes "\\`\"$"
  40. typedef int _hist_search_func_t PARAMS((const char *, int));
  41. static char error_pointer;
  42. static char *subst_lhs;
  43. static char *subst_rhs;
  44. static int subst_lhs_len;
  45. static int subst_rhs_len;
  46. static char *get_history_word_specifier PARAMS((char *, char *, int *));
  47. static int history_tokenize_word PARAMS((const char *, int));
  48. static char **history_tokenize_internal PARAMS((const char *, int, int *));
  49. static char *history_substring PARAMS((const char *, int, int));
  50. static void freewords PARAMS((char **, int));
  51. static char *history_find_word PARAMS((char *, int));
  52. static char *quote_breaks PARAMS((char *));
  53. /* Variables exported by this file. */
  54. /* The character that represents the start of a history expansion
  55. request. This is usually `!'. */
  56. char history_expansion_char = '!';
  57. /* The character that invokes word substitution if found at the start of
  58. a line. This is usually `^'. */
  59. char history_subst_char = '^';
  60. /* During tokenization, if this character is seen as the first character
  61. of a word, then it, and all subsequent characters upto a newline are
  62. ignored. For a Bourne shell, this should be '#'. Bash special cases
  63. the interactive comment character to not be a comment delimiter. */
  64. char history_comment_char = '\0';
  65. /* The list of characters which inhibit the expansion of text if found
  66. immediately following history_expansion_char. */
  67. char *history_no_expand_chars = " \t\n\r=";
  68. /* If set to a non-zero value, single quotes inhibit history expansion.
  69. The default is 0. */
  70. int history_quotes_inhibit_expansion = 0;
  71. /* Used to split words by history_tokenize_internal. */
  72. char *history_word_delimiters = HISTORY_WORD_DELIMITERS;
  73. /* If set, this points to a function that is called to verify that a
  74. particular history expansion should be performed. */
  75. rl_linebuf_func_t *history_inhibit_expansion_function;
  76. /* **************************************************************** */
  77. /* */
  78. /* History Expansion */
  79. /* */
  80. /* **************************************************************** */
  81. /* Hairy history expansion on text, not tokens. This is of general
  82. use, and thus belongs in this library. */
  83. /* The last string searched for by a !?string? search. */
  84. static char *search_string;
  85. /* The last string matched by a !?string? search. */
  86. static char *search_match;
  87. /* Return the event specified at TEXT + OFFSET modifying OFFSET to
  88. point to after the event specifier. Just a pointer to the history
  89. line is returned; NULL is returned in the event of a bad specifier.
  90. You pass STRING with *INDEX equal to the history_expansion_char that
  91. begins this specification.
  92. DELIMITING_QUOTE is a character that is allowed to end the string
  93. specification for what to search for in addition to the normal
  94. characters `:', ` ', `\t', `\n', and sometimes `?'.
  95. So you might call this function like:
  96. line = get_history_event ("!echo:p", &index, 0); */
  97. char *
  98. get_history_event (string, caller_index, delimiting_quote)
  99. const char *string;
  100. int *caller_index;
  101. int delimiting_quote;
  102. {
  103. register int i;
  104. register char c;
  105. HIST_ENTRY *entry;
  106. int which, sign, local_index, substring_okay;
  107. _hist_search_func_t *search_func;
  108. char *temp;
  109. /* The event can be specified in a number of ways.
  110. !! the previous command
  111. !n command line N
  112. !-n current command-line minus N
  113. !str the most recent command starting with STR
  114. !?str[?]
  115. the most recent command containing STR
  116. All values N are determined via HISTORY_BASE. */
  117. i = *caller_index;
  118. if (string[i] != history_expansion_char)
  119. return ((char *)NULL);
  120. /* Move on to the specification. */
  121. i++;
  122. sign = 1;
  123. substring_okay = 0;
  124. #define RETURN_ENTRY(e, w) \
  125. return ((e = history_get (w)) ? e->line : (char *)NULL)
  126. /* Handle !! case. */
  127. if (string[i] == history_expansion_char)
  128. {
  129. i++;
  130. which = history_base + (history_length - 1);
  131. *caller_index = i;
  132. RETURN_ENTRY (entry, which);
  133. }
  134. /* Hack case of numeric line specification. */
  135. if (string[i] == '-')
  136. {
  137. sign = -1;
  138. i++;
  139. }
  140. if (_rl_digit_p (string[i]))
  141. {
  142. /* Get the extent of the digits and compute the value. */
  143. for (which = 0; _rl_digit_p (string[i]); i++)
  144. which = (which * 10) + _rl_digit_value (string[i]);
  145. *caller_index = i;
  146. if (sign < 0)
  147. which = (history_length + history_base) - which;
  148. RETURN_ENTRY (entry, which);
  149. }
  150. /* This must be something to search for. If the spec begins with
  151. a '?', then the string may be anywhere on the line. Otherwise,
  152. the string must be found at the start of a line. */
  153. if (string[i] == '?')
  154. {
  155. substring_okay++;
  156. i++;
  157. }
  158. /* Only a closing `?' or a newline delimit a substring search string. */
  159. for (local_index = i; c = string[i]; i++)
  160. {
  161. #if defined (HANDLE_MULTIBYTE)
  162. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  163. {
  164. int v;
  165. mbstate_t ps;
  166. memset (&ps, 0, sizeof (mbstate_t));
  167. /* These produce warnings because we're passing a const string to a
  168. function that takes a non-const string. */
  169. _rl_adjust_point ((char *)string, i, &ps);
  170. if ((v = _rl_get_char_len ((char *)string + i, &ps)) > 1)
  171. {
  172. i += v - 1;
  173. continue;
  174. }
  175. }
  176. #endif /* HANDLE_MULTIBYTE */
  177. if ((!substring_okay && (whitespace (c) || c == ':' ||
  178. (history_search_delimiter_chars && member (c, history_search_delimiter_chars)) ||
  179. string[i] == delimiting_quote)) ||
  180. string[i] == '\n' ||
  181. (substring_okay && string[i] == '?'))
  182. break;
  183. }
  184. which = i - local_index;
  185. temp = (char *)xmalloc (1 + which);
  186. if (which)
  187. strncpy (temp, string + local_index, which);
  188. temp[which] = '\0';
  189. if (substring_okay && string[i] == '?')
  190. i++;
  191. *caller_index = i;
  192. #define FAIL_SEARCH() \
  193. do { \
  194. history_offset = history_length; xfree (temp) ; return (char *)NULL; \
  195. } while (0)
  196. /* If there is no search string, try to use the previous search string,
  197. if one exists. If not, fail immediately. */
  198. if (*temp == '\0' && substring_okay)
  199. {
  200. if (search_string)
  201. {
  202. xfree (temp);
  203. temp = savestring (search_string);
  204. }
  205. else
  206. FAIL_SEARCH ();
  207. }
  208. search_func = substring_okay ? history_search : history_search_prefix;
  209. while (1)
  210. {
  211. local_index = (*search_func) (temp, -1);
  212. if (local_index < 0)
  213. FAIL_SEARCH ();
  214. if (local_index == 0 || substring_okay)
  215. {
  216. entry = current_history ();
  217. history_offset = history_length;
  218. /* If this was a substring search, then remember the
  219. string that we matched for word substitution. */
  220. if (substring_okay)
  221. {
  222. FREE (search_string);
  223. search_string = temp;
  224. FREE (search_match);
  225. search_match = history_find_word (entry->line, local_index);
  226. }
  227. else
  228. xfree (temp);
  229. return (entry->line);
  230. }
  231. if (history_offset)
  232. history_offset--;
  233. else
  234. FAIL_SEARCH ();
  235. }
  236. #undef FAIL_SEARCH
  237. #undef RETURN_ENTRY
  238. }
  239. /* Function for extracting single-quoted strings. Used for inhibiting
  240. history expansion within single quotes. */
  241. /* Extract the contents of STRING as if it is enclosed in single quotes.
  242. SINDEX, when passed in, is the offset of the character immediately
  243. following the opening single quote; on exit, SINDEX is left pointing
  244. to the closing single quote. FLAGS currently used to allow backslash
  245. to escape a single quote (e.g., for bash $'...'). */
  246. static void
  247. hist_string_extract_single_quoted (string, sindex, flags)
  248. char *string;
  249. int *sindex, flags;
  250. {
  251. register int i;
  252. for (i = *sindex; string[i] && string[i] != '\''; i++)
  253. {
  254. if ((flags & 1) && string[i] == '\\' && string[i+1])
  255. i++;
  256. }
  257. *sindex = i;
  258. }
  259. static char *
  260. quote_breaks (s)
  261. char *s;
  262. {
  263. register char *p, *r;
  264. char *ret;
  265. int len = 3;
  266. for (p = s; p && *p; p++, len++)
  267. {
  268. if (*p == '\'')
  269. len += 3;
  270. else if (whitespace (*p) || *p == '\n')
  271. len += 2;
  272. }
  273. r = ret = (char *)xmalloc (len);
  274. *r++ = '\'';
  275. for (p = s; p && *p; )
  276. {
  277. if (*p == '\'')
  278. {
  279. *r++ = '\'';
  280. *r++ = '\\';
  281. *r++ = '\'';
  282. *r++ = '\'';
  283. p++;
  284. }
  285. else if (whitespace (*p) || *p == '\n')
  286. {
  287. *r++ = '\'';
  288. *r++ = *p++;
  289. *r++ = '\'';
  290. }
  291. else
  292. *r++ = *p++;
  293. }
  294. *r++ = '\'';
  295. *r = '\0';
  296. return ret;
  297. }
  298. static char *
  299. hist_error(s, start, current, errtype)
  300. char *s;
  301. int start, current, errtype;
  302. {
  303. char *temp;
  304. const char *emsg;
  305. int ll, elen;
  306. ll = current - start;
  307. switch (errtype)
  308. {
  309. case EVENT_NOT_FOUND:
  310. emsg = "event not found";
  311. elen = 15;
  312. break;
  313. case BAD_WORD_SPEC:
  314. emsg = "bad word specifier";
  315. elen = 18;
  316. break;
  317. case SUBST_FAILED:
  318. emsg = "substitution failed";
  319. elen = 19;
  320. break;
  321. case BAD_MODIFIER:
  322. emsg = "unrecognized history modifier";
  323. elen = 29;
  324. break;
  325. case NO_PREV_SUBST:
  326. emsg = "no previous substitution";
  327. elen = 24;
  328. break;
  329. default:
  330. emsg = "unknown expansion error";
  331. elen = 23;
  332. break;
  333. }
  334. temp = (char *)xmalloc (ll + elen + 3);
  335. strncpy (temp, s + start, ll);
  336. temp[ll] = ':';
  337. temp[ll + 1] = ' ';
  338. strcpy (temp + ll + 2, emsg);
  339. return (temp);
  340. }
  341. /* Get a history substitution string from STR starting at *IPTR
  342. and return it. The length is returned in LENPTR.
  343. A backslash can quote the delimiter. If the string is the
  344. empty string, the previous pattern is used. If there is
  345. no previous pattern for the lhs, the last history search
  346. string is used.
  347. If IS_RHS is 1, we ignore empty strings and set the pattern
  348. to "" anyway. subst_lhs is not changed if the lhs is empty;
  349. subst_rhs is allowed to be set to the empty string. */
  350. static char *
  351. get_subst_pattern (str, iptr, delimiter, is_rhs, lenptr)
  352. char *str;
  353. int *iptr, delimiter, is_rhs, *lenptr;
  354. {
  355. register int si, i, j, k;
  356. char *s;
  357. #if defined (HANDLE_MULTIBYTE)
  358. mbstate_t ps;
  359. #endif
  360. s = (char *)NULL;
  361. i = *iptr;
  362. #if defined (HANDLE_MULTIBYTE)
  363. memset (&ps, 0, sizeof (mbstate_t));
  364. _rl_adjust_point (str, i, &ps);
  365. #endif
  366. for (si = i; str[si] && str[si] != delimiter; si++)
  367. #if defined (HANDLE_MULTIBYTE)
  368. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  369. {
  370. int v;
  371. if ((v = _rl_get_char_len (str + si, &ps)) > 1)
  372. si += v - 1;
  373. else if (str[si] == '\\' && str[si + 1] == delimiter)
  374. si++;
  375. }
  376. else
  377. #endif /* HANDLE_MULTIBYTE */
  378. if (str[si] == '\\' && str[si + 1] == delimiter)
  379. si++;
  380. if (si > i || is_rhs)
  381. {
  382. s = (char *)xmalloc (si - i + 1);
  383. for (j = 0, k = i; k < si; j++, k++)
  384. {
  385. /* Remove a backslash quoting the search string delimiter. */
  386. if (str[k] == '\\' && str[k + 1] == delimiter)
  387. k++;
  388. s[j] = str[k];
  389. }
  390. s[j] = '\0';
  391. if (lenptr)
  392. *lenptr = j;
  393. }
  394. i = si;
  395. if (str[i])
  396. i++;
  397. *iptr = i;
  398. return s;
  399. }
  400. static void
  401. postproc_subst_rhs ()
  402. {
  403. char *new;
  404. int i, j, new_size;
  405. new = (char *)xmalloc (new_size = subst_rhs_len + subst_lhs_len);
  406. for (i = j = 0; i < subst_rhs_len; i++)
  407. {
  408. if (subst_rhs[i] == '&')
  409. {
  410. if (j + subst_lhs_len >= new_size)
  411. new = (char *)xrealloc (new, (new_size = new_size * 2 + subst_lhs_len));
  412. strcpy (new + j, subst_lhs);
  413. j += subst_lhs_len;
  414. }
  415. else
  416. {
  417. /* a single backslash protects the `&' from lhs interpolation */
  418. if (subst_rhs[i] == '\\' && subst_rhs[i + 1] == '&')
  419. i++;
  420. if (j >= new_size)
  421. new = (char *)xrealloc (new, new_size *= 2);
  422. new[j++] = subst_rhs[i];
  423. }
  424. }
  425. new[j] = '\0';
  426. xfree (subst_rhs);
  427. subst_rhs = new;
  428. subst_rhs_len = j;
  429. }
  430. /* Expand the bulk of a history specifier starting at STRING[START].
  431. Returns 0 if everything is OK, -1 if an error occurred, and 1
  432. if the `p' modifier was supplied and the caller should just print
  433. the returned string. Returns the new index into string in
  434. *END_INDEX_PTR, and the expanded specifier in *RET_STRING. */
  435. static int
  436. history_expand_internal (string, start, end_index_ptr, ret_string, current_line)
  437. char *string;
  438. int start, *end_index_ptr;
  439. char **ret_string;
  440. char *current_line; /* for !# */
  441. {
  442. int i, n, starting_index;
  443. int substitute_globally, subst_bywords, want_quotes, print_only;
  444. char *event, *temp, *result, *tstr, *t, c, *word_spec;
  445. int result_len;
  446. #if defined (HANDLE_MULTIBYTE)
  447. mbstate_t ps;
  448. memset (&ps, 0, sizeof (mbstate_t));
  449. #endif
  450. result = (char *)xmalloc (result_len = 128);
  451. i = start;
  452. /* If it is followed by something that starts a word specifier,
  453. then !! is implied as the event specifier. */
  454. if (member (string[i + 1], ":$*%^"))
  455. {
  456. char fake_s[3];
  457. int fake_i = 0;
  458. i++;
  459. fake_s[0] = fake_s[1] = history_expansion_char;
  460. fake_s[2] = '\0';
  461. event = get_history_event (fake_s, &fake_i, 0);
  462. }
  463. else if (string[i + 1] == '#')
  464. {
  465. i += 2;
  466. event = current_line;
  467. }
  468. else
  469. {
  470. int quoted_search_delimiter = 0;
  471. /* If the character before this `!' is a double or single
  472. quote, then this expansion takes place inside of the
  473. quoted string. If we have to search for some text ("!foo"),
  474. allow the delimiter to end the search string. */
  475. #if defined (HANDLE_MULTIBYTE)
  476. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  477. {
  478. int ch, l;
  479. l = _rl_find_prev_mbchar (string, i, MB_FIND_ANY);
  480. ch = string[l];
  481. /* XXX - original patch had i - 1 ??? If i == 0 it would fail. */
  482. if (i && (ch == '\'' || ch == '"'))
  483. quoted_search_delimiter = ch;
  484. }
  485. else
  486. #endif /* HANDLE_MULTIBYTE */
  487. if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
  488. quoted_search_delimiter = string[i - 1];
  489. event = get_history_event (string, &i, quoted_search_delimiter);
  490. }
  491. if (event == 0)
  492. {
  493. *ret_string = hist_error (string, start, i, EVENT_NOT_FOUND);
  494. xfree (result);
  495. return (-1);
  496. }
  497. /* If a word specifier is found, then do what that requires. */
  498. starting_index = i;
  499. word_spec = get_history_word_specifier (string, event, &i);
  500. /* There is no such thing as a `malformed word specifier'. However,
  501. it is possible for a specifier that has no match. In that case,
  502. we complain. */
  503. if (word_spec == (char *)&error_pointer)
  504. {
  505. *ret_string = hist_error (string, starting_index, i, BAD_WORD_SPEC);
  506. xfree (result);
  507. return (-1);
  508. }
  509. /* If no word specifier, than the thing of interest was the event. */
  510. temp = word_spec ? savestring (word_spec) : savestring (event);
  511. FREE (word_spec);
  512. /* Perhaps there are other modifiers involved. Do what they say. */
  513. want_quotes = substitute_globally = subst_bywords = print_only = 0;
  514. starting_index = i;
  515. while (string[i] == ':')
  516. {
  517. c = string[i + 1];
  518. if (c == 'g' || c == 'a')
  519. {
  520. substitute_globally = 1;
  521. i++;
  522. c = string[i + 1];
  523. }
  524. else if (c == 'G')
  525. {
  526. subst_bywords = 1;
  527. i++;
  528. c = string[i + 1];
  529. }
  530. switch (c)
  531. {
  532. default:
  533. *ret_string = hist_error (string, i+1, i+2, BAD_MODIFIER);
  534. xfree (result);
  535. xfree (temp);
  536. return -1;
  537. case 'q':
  538. want_quotes = 'q';
  539. break;
  540. case 'x':
  541. want_quotes = 'x';
  542. break;
  543. /* :p means make this the last executed line. So we
  544. return an error state after adding this line to the
  545. history. */
  546. case 'p':
  547. print_only++;
  548. break;
  549. /* :t discards all but the last part of the pathname. */
  550. case 't':
  551. tstr = strrchr (temp, '/');
  552. if (tstr)
  553. {
  554. tstr++;
  555. t = savestring (tstr);
  556. xfree (temp);
  557. temp = t;
  558. }
  559. break;
  560. /* :h discards the last part of a pathname. */
  561. case 'h':
  562. tstr = strrchr (temp, '/');
  563. if (tstr)
  564. *tstr = '\0';
  565. break;
  566. /* :r discards the suffix. */
  567. case 'r':
  568. tstr = strrchr (temp, '.');
  569. if (tstr)
  570. *tstr = '\0';
  571. break;
  572. /* :e discards everything but the suffix. */
  573. case 'e':
  574. tstr = strrchr (temp, '.');
  575. if (tstr)
  576. {
  577. t = savestring (tstr);
  578. xfree (temp);
  579. temp = t;
  580. }
  581. break;
  582. /* :s/this/that substitutes `that' for the first
  583. occurrence of `this'. :gs/this/that substitutes `that'
  584. for each occurrence of `this'. :& repeats the last
  585. substitution. :g& repeats the last substitution
  586. globally. */
  587. case '&':
  588. case 's':
  589. {
  590. char *new_event;
  591. int delimiter, failed, si, l_temp, ws, we;
  592. if (c == 's')
  593. {
  594. if (i + 2 < (int)strlen (string))
  595. {
  596. #if defined (HANDLE_MULTIBYTE)
  597. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  598. {
  599. _rl_adjust_point (string, i + 2, &ps);
  600. if (_rl_get_char_len (string + i + 2, &ps) > 1)
  601. delimiter = 0;
  602. else
  603. delimiter = string[i + 2];
  604. }
  605. else
  606. #endif /* HANDLE_MULTIBYTE */
  607. delimiter = string[i + 2];
  608. }
  609. else
  610. break; /* no search delimiter */
  611. i += 3;
  612. t = get_subst_pattern (string, &i, delimiter, 0, &subst_lhs_len);
  613. /* An empty substitution lhs with no previous substitution
  614. uses the last search string as the lhs. */
  615. if (t)
  616. {
  617. FREE (subst_lhs);
  618. subst_lhs = t;
  619. }
  620. else if (!subst_lhs)
  621. {
  622. if (search_string && *search_string)
  623. {
  624. subst_lhs = savestring (search_string);
  625. subst_lhs_len = strlen (subst_lhs);
  626. }
  627. else
  628. {
  629. subst_lhs = (char *) NULL;
  630. subst_lhs_len = 0;
  631. }
  632. }
  633. FREE (subst_rhs);
  634. subst_rhs = get_subst_pattern (string, &i, delimiter, 1, &subst_rhs_len);
  635. /* If `&' appears in the rhs, it's supposed to be replaced
  636. with the lhs. */
  637. if (member ('&', subst_rhs))
  638. postproc_subst_rhs ();
  639. }
  640. else
  641. i += 2;
  642. /* If there is no lhs, the substitution can't succeed. */
  643. if (subst_lhs_len == 0)
  644. {
  645. *ret_string = hist_error (string, starting_index, i, NO_PREV_SUBST);
  646. xfree (result);
  647. xfree (temp);
  648. return -1;
  649. }
  650. l_temp = strlen (temp);
  651. /* Ignore impossible cases. */
  652. if (subst_lhs_len > l_temp)
  653. {
  654. *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
  655. xfree (result);
  656. xfree (temp);
  657. return (-1);
  658. }
  659. /* Find the first occurrence of THIS in TEMP. */
  660. /* Substitute SUBST_RHS for SUBST_LHS in TEMP. There are three
  661. cases to consider:
  662. 1. substitute_globally == subst_bywords == 0
  663. 2. substitute_globally == 1 && subst_bywords == 0
  664. 3. substitute_globally == 0 && subst_bywords == 1
  665. In the first case, we substitute for the first occurrence only.
  666. In the second case, we substitute for every occurrence.
  667. In the third case, we tokenize into words and substitute the
  668. first occurrence of each word. */
  669. si = we = 0;
  670. for (failed = 1; (si + subst_lhs_len) <= l_temp; si++)
  671. {
  672. /* First skip whitespace and find word boundaries if
  673. we're past the end of the word boundary we found
  674. the last time. */
  675. if (subst_bywords && si > we)
  676. {
  677. for (; temp[si] && whitespace (temp[si]); si++)
  678. ;
  679. ws = si;
  680. we = history_tokenize_word (temp, si);
  681. }
  682. if (STREQN (temp+si, subst_lhs, subst_lhs_len))
  683. {
  684. int len = subst_rhs_len - subst_lhs_len + l_temp;
  685. new_event = (char *)xmalloc (1 + len);
  686. strncpy (new_event, temp, si);
  687. strncpy (new_event + si, subst_rhs, subst_rhs_len);
  688. strncpy (new_event + si + subst_rhs_len,
  689. temp + si + subst_lhs_len,
  690. l_temp - (si + subst_lhs_len));
  691. new_event[len] = '\0';
  692. xfree (temp);
  693. temp = new_event;
  694. failed = 0;
  695. if (substitute_globally)
  696. {
  697. /* Reported to fix a bug that causes it to skip every
  698. other match when matching a single character. Was
  699. si += subst_rhs_len previously. */
  700. si += subst_rhs_len - 1;
  701. l_temp = strlen (temp);
  702. substitute_globally++;
  703. continue;
  704. }
  705. else if (subst_bywords)
  706. {
  707. si = we;
  708. l_temp = strlen (temp);
  709. continue;
  710. }
  711. else
  712. break;
  713. }
  714. }
  715. if (substitute_globally > 1)
  716. {
  717. substitute_globally = 0;
  718. continue; /* don't want to increment i */
  719. }
  720. if (failed == 0)
  721. continue; /* don't want to increment i */
  722. *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
  723. xfree (result);
  724. xfree (temp);
  725. return (-1);
  726. }
  727. }
  728. i += 2;
  729. }
  730. /* Done with modfiers. */
  731. /* Believe it or not, we have to back the pointer up by one. */
  732. --i;
  733. if (want_quotes)
  734. {
  735. char *x;
  736. if (want_quotes == 'q')
  737. x = sh_single_quote (temp);
  738. else if (want_quotes == 'x')
  739. x = quote_breaks (temp);
  740. else
  741. x = savestring (temp);
  742. xfree (temp);
  743. temp = x;
  744. }
  745. n = strlen (temp);
  746. if (n >= result_len)
  747. result = (char *)xrealloc (result, n + 2);
  748. strcpy (result, temp);
  749. xfree (temp);
  750. *end_index_ptr = i;
  751. *ret_string = result;
  752. return (print_only);
  753. }
  754. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  755. to a string. Returns:
  756. -1) If there was an error in expansion.
  757. 0) If no expansions took place (or, if the only change in
  758. the text was the de-slashifying of the history expansion
  759. character)
  760. 1) If expansions did take place
  761. 2) If the `p' modifier was given and the caller should print the result
  762. If an error ocurred in expansion, then OUTPUT contains a descriptive
  763. error message. */
  764. #define ADD_STRING(s) \
  765. do \
  766. { \
  767. int sl = strlen (s); \
  768. j += sl; \
  769. if (j >= result_len) \
  770. { \
  771. while (j >= result_len) \
  772. result_len += 128; \
  773. result = (char *)xrealloc (result, result_len); \
  774. } \
  775. strcpy (result + j - sl, s); \
  776. } \
  777. while (0)
  778. #define ADD_CHAR(c) \
  779. do \
  780. { \
  781. if (j >= result_len - 1) \
  782. result = (char *)xrealloc (result, result_len += 64); \
  783. result[j++] = c; \
  784. result[j] = '\0'; \
  785. } \
  786. while (0)
  787. int
  788. history_expand (hstring, output)
  789. char *hstring;
  790. char **output;
  791. {
  792. register int j;
  793. int i, r, l, passc, cc, modified, eindex, only_printing, dquote, flag;
  794. char *string;
  795. /* The output string, and its length. */
  796. int result_len;
  797. char *result;
  798. #if defined (HANDLE_MULTIBYTE)
  799. char mb[MB_LEN_MAX];
  800. mbstate_t ps;
  801. #endif
  802. /* Used when adding the string. */
  803. char *temp;
  804. if (output == 0)
  805. return 0;
  806. /* Setting the history expansion character to 0 inhibits all
  807. history expansion. */
  808. if (history_expansion_char == 0)
  809. {
  810. *output = savestring (hstring);
  811. return (0);
  812. }
  813. /* Prepare the buffer for printing error messages. */
  814. result = (char *)xmalloc (result_len = 256);
  815. result[0] = '\0';
  816. only_printing = modified = 0;
  817. l = strlen (hstring);
  818. /* Grovel the string. Only backslash and single quotes can quote the
  819. history escape character. We also handle arg specifiers. */
  820. /* Before we grovel forever, see if the history_expansion_char appears
  821. anywhere within the text. */
  822. /* The quick substitution character is a history expansion all right. That
  823. is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
  824. that is the substitution that we do. */
  825. if (hstring[0] == history_subst_char)
  826. {
  827. string = (char *)xmalloc (l + 5);
  828. string[0] = string[1] = history_expansion_char;
  829. string[2] = ':';
  830. string[3] = 's';
  831. strcpy (string + 4, hstring);
  832. l += 4;
  833. }
  834. else
  835. {
  836. #if defined (HANDLE_MULTIBYTE)
  837. memset (&ps, 0, sizeof (mbstate_t));
  838. #endif
  839. string = hstring;
  840. /* If not quick substitution, still maybe have to do expansion. */
  841. /* `!' followed by one of the characters in history_no_expand_chars
  842. is NOT an expansion. */
  843. for (i = dquote = 0; string[i]; i++)
  844. {
  845. #if defined (HANDLE_MULTIBYTE)
  846. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  847. {
  848. int v;
  849. v = _rl_get_char_len (string + i, &ps);
  850. if (v > 1)
  851. {
  852. i += v - 1;
  853. continue;
  854. }
  855. }
  856. #endif /* HANDLE_MULTIBYTE */
  857. cc = string[i + 1];
  858. /* The history_comment_char, if set, appearing at the beginning
  859. of a word signifies that the rest of the line should not have
  860. history expansion performed on it.
  861. Skip the rest of the line and break out of the loop. */
  862. if (history_comment_char && string[i] == history_comment_char &&
  863. (i == 0 || member (string[i - 1], history_word_delimiters)))
  864. {
  865. while (string[i])
  866. i++;
  867. break;
  868. }
  869. else if (string[i] == history_expansion_char)
  870. {
  871. if (cc == 0 || member (cc, history_no_expand_chars))
  872. continue;
  873. /* If the calling application has set
  874. history_inhibit_expansion_function to a function that checks
  875. for special cases that should not be history expanded,
  876. call the function and skip the expansion if it returns a
  877. non-zero value. */
  878. else if (history_inhibit_expansion_function &&
  879. (*history_inhibit_expansion_function) (string, i))
  880. continue;
  881. else
  882. break;
  883. }
  884. /* Shell-like quoting: allow backslashes to quote double quotes
  885. inside a double-quoted string. */
  886. else if (dquote && string[i] == '\\' && cc == '"')
  887. i++;
  888. /* More shell-like quoting: if we're paying attention to single
  889. quotes and letting them quote the history expansion character,
  890. then we need to pay attention to double quotes, because single
  891. quotes are not special inside double-quoted strings. */
  892. else if (history_quotes_inhibit_expansion && string[i] == '"')
  893. {
  894. dquote = 1 - dquote;
  895. }
  896. else if (dquote == 0 && history_quotes_inhibit_expansion && string[i] == '\'')
  897. {
  898. /* If this is bash, single quotes inhibit history expansion. */
  899. flag = (i > 0 && string[i - 1] == '$');
  900. i++;
  901. hist_string_extract_single_quoted (string, &i, flag);
  902. }
  903. else if (history_quotes_inhibit_expansion && string[i] == '\\')
  904. {
  905. /* If this is bash, allow backslashes to quote single
  906. quotes and the history expansion character. */
  907. if (cc == '\'' || cc == history_expansion_char)
  908. i++;
  909. }
  910. }
  911. if (string[i] != history_expansion_char)
  912. {
  913. xfree (result);
  914. *output = savestring (string);
  915. return (0);
  916. }
  917. }
  918. /* Extract and perform the substitution. */
  919. for (passc = dquote = i = j = 0; i < l; i++)
  920. {
  921. int tchar = string[i];
  922. if (passc)
  923. {
  924. passc = 0;
  925. ADD_CHAR (tchar);
  926. continue;
  927. }
  928. #if defined (HANDLE_MULTIBYTE)
  929. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  930. {
  931. int k, c;
  932. c = tchar;
  933. memset (mb, 0, sizeof (mb));
  934. for (k = 0; k < MB_LEN_MAX; k++)
  935. {
  936. mb[k] = (char)c;
  937. memset (&ps, 0, sizeof (mbstate_t));
  938. if (_rl_get_char_len (mb, &ps) == -2)
  939. c = string[++i];
  940. else
  941. break;
  942. }
  943. if (strlen (mb) > 1)
  944. {
  945. ADD_STRING (mb);
  946. continue;
  947. }
  948. }
  949. #endif /* HANDLE_MULTIBYTE */
  950. if (tchar == history_expansion_char)
  951. tchar = -3;
  952. else if (tchar == history_comment_char)
  953. tchar = -2;
  954. switch (tchar)
  955. {
  956. default:
  957. ADD_CHAR (string[i]);
  958. break;
  959. case '\\':
  960. passc++;
  961. ADD_CHAR (tchar);
  962. break;
  963. case '"':
  964. dquote = 1 - dquote;
  965. ADD_CHAR (tchar);
  966. break;
  967. case '\'':
  968. {
  969. /* If history_quotes_inhibit_expansion is set, single quotes
  970. inhibit history expansion. */
  971. if (dquote == 0 && history_quotes_inhibit_expansion)
  972. {
  973. int quote, slen;
  974. flag = (i > 0 && string[i - 1] == '$');
  975. quote = i++;
  976. hist_string_extract_single_quoted (string, &i, flag);
  977. slen = i - quote + 2;
  978. temp = (char *)xmalloc (slen);
  979. strncpy (temp, string + quote, slen);
  980. temp[slen - 1] = '\0';
  981. ADD_STRING (temp);
  982. xfree (temp);
  983. }
  984. else
  985. ADD_CHAR (string[i]);
  986. break;
  987. }
  988. case -2: /* history_comment_char */
  989. if (i == 0 || member (string[i - 1], history_word_delimiters))
  990. {
  991. temp = (char *)xmalloc (l - i + 1);
  992. strcpy (temp, string + i);
  993. ADD_STRING (temp);
  994. xfree (temp);
  995. i = l;
  996. }
  997. else
  998. ADD_CHAR (string[i]);
  999. break;
  1000. case -3: /* history_expansion_char */
  1001. cc = string[i + 1];
  1002. /* If the history_expansion_char is followed by one of the
  1003. characters in history_no_expand_chars, then it is not a
  1004. candidate for expansion of any kind. */
  1005. if (cc == 0 || member (cc, history_no_expand_chars) ||
  1006. (history_inhibit_expansion_function && (*history_inhibit_expansion_function) (string, i)))
  1007. {
  1008. ADD_CHAR (string[i]);
  1009. break;
  1010. }
  1011. #if defined (NO_BANG_HASH_MODIFIERS)
  1012. /* There is something that is listed as a `word specifier' in csh
  1013. documentation which means `the expanded text to this point'.
  1014. That is not a word specifier, it is an event specifier. If we
  1015. don't want to allow modifiers with `!#', just stick the current
  1016. output line in again. */
  1017. if (cc == '#')
  1018. {
  1019. if (result)
  1020. {
  1021. temp = (char *)xmalloc (1 + strlen (result));
  1022. strcpy (temp, result);
  1023. ADD_STRING (temp);
  1024. xfree (temp);
  1025. }
  1026. i++;
  1027. break;
  1028. }
  1029. #endif
  1030. r = history_expand_internal (string, i, &eindex, &temp, result);
  1031. if (r < 0)
  1032. {
  1033. *output = temp;
  1034. xfree (result);
  1035. if (string != hstring)
  1036. xfree (string);
  1037. return -1;
  1038. }
  1039. else
  1040. {
  1041. if (temp)
  1042. {
  1043. modified++;
  1044. if (*temp)
  1045. ADD_STRING (temp);
  1046. xfree (temp);
  1047. }
  1048. only_printing = r == 1;
  1049. i = eindex;
  1050. }
  1051. break;
  1052. }
  1053. }
  1054. *output = result;
  1055. if (string != hstring)
  1056. xfree (string);
  1057. if (only_printing)
  1058. {
  1059. #if 0
  1060. add_history (result);
  1061. #endif
  1062. return (2);
  1063. }
  1064. return (modified != 0);
  1065. }
  1066. /* Return a consed string which is the word specified in SPEC, and found
  1067. in FROM. NULL is returned if there is no spec. The address of
  1068. ERROR_POINTER is returned if the word specified cannot be found.
  1069. CALLER_INDEX is the offset in SPEC to start looking; it is updated
  1070. to point to just after the last character parsed. */
  1071. static char *
  1072. get_history_word_specifier (spec, from, caller_index)
  1073. char *spec, *from;
  1074. int *caller_index;
  1075. {
  1076. register int i = *caller_index;
  1077. int first, last;
  1078. int expecting_word_spec = 0;
  1079. char *result;
  1080. /* The range of words to return doesn't exist yet. */
  1081. first = last = 0;
  1082. result = (char *)NULL;
  1083. /* If we found a colon, then this *must* be a word specification. If
  1084. it isn't, then it is an error. */
  1085. if (spec[i] == ':')
  1086. {
  1087. i++;
  1088. expecting_word_spec++;
  1089. }
  1090. /* Handle special cases first. */
  1091. /* `%' is the word last searched for. */
  1092. if (spec[i] == '%')
  1093. {
  1094. *caller_index = i + 1;
  1095. return (search_match ? savestring (search_match) : savestring (""));
  1096. }
  1097. /* `*' matches all of the arguments, but not the command. */
  1098. if (spec[i] == '*')
  1099. {
  1100. *caller_index = i + 1;
  1101. result = history_arg_extract (1, '$', from);
  1102. return (result ? result : savestring (""));
  1103. }
  1104. /* `$' is last arg. */
  1105. if (spec[i] == '$')
  1106. {
  1107. *caller_index = i + 1;
  1108. return (history_arg_extract ('$', '$', from));
  1109. }
  1110. /* Try to get FIRST and LAST figured out. */
  1111. if (spec[i] == '-')
  1112. first = 0;
  1113. else if (spec[i] == '^')
  1114. {
  1115. first = 1;
  1116. i++;
  1117. }
  1118. else if (_rl_digit_p (spec[i]) && expecting_word_spec)
  1119. {
  1120. for (first = 0; _rl_digit_p (spec[i]); i++)
  1121. first = (first * 10) + _rl_digit_value (spec[i]);
  1122. }
  1123. else
  1124. return ((char *)NULL); /* no valid `first' for word specifier */
  1125. if (spec[i] == '^' || spec[i] == '*')
  1126. {
  1127. last = (spec[i] == '^') ? 1 : '$'; /* x* abbreviates x-$ */
  1128. i++;
  1129. }
  1130. else if (spec[i] != '-')
  1131. last = first;
  1132. else
  1133. {
  1134. i++;
  1135. if (_rl_digit_p (spec[i]))
  1136. {
  1137. for (last = 0; _rl_digit_p (spec[i]); i++)
  1138. last = (last * 10) + _rl_digit_value (spec[i]);
  1139. }
  1140. else if (spec[i] == '$')
  1141. {
  1142. i++;
  1143. last = '$';
  1144. }
  1145. #if 0
  1146. else if (!spec[i] || spec[i] == ':')
  1147. /* check against `:' because there could be a modifier separator */
  1148. #else
  1149. else
  1150. /* csh seems to allow anything to terminate the word spec here,
  1151. leaving it as an abbreviation. */
  1152. #endif
  1153. last = -1; /* x- abbreviates x-$ omitting word `$' */
  1154. }
  1155. *caller_index = i;
  1156. if (last >= first || last == '$' || last < 0)
  1157. result = history_arg_extract (first, last, from);
  1158. return (result ? result : (char *)&error_pointer);
  1159. }
  1160. /* Extract the args specified, starting at FIRST, and ending at LAST.
  1161. The args are taken from STRING. If either FIRST or LAST is < 0,
  1162. then make that arg count from the right (subtract from the number of
  1163. tokens, so that FIRST = -1 means the next to last token on the line).
  1164. If LAST is `$' the last arg from STRING is used. */
  1165. char *
  1166. history_arg_extract (first, last, string)
  1167. int first, last;
  1168. const char *string;
  1169. {
  1170. register int i, len;
  1171. char *result;
  1172. int size, offset;
  1173. char **list;
  1174. /* XXX - think about making history_tokenize return a struct array,
  1175. each struct in array being a string and a length to avoid the
  1176. calls to strlen below. */
  1177. if ((list = history_tokenize (string)) == NULL)
  1178. return ((char *)NULL);
  1179. for (len = 0; list[len]; len++)
  1180. ;
  1181. if (last < 0)
  1182. last = len + last - 1;
  1183. if (first < 0)
  1184. first = len + first - 1;
  1185. if (last == '$')
  1186. last = len - 1;
  1187. if (first == '$')
  1188. first = len - 1;
  1189. last++;
  1190. if (first >= len || last > len || first < 0 || last < 0 || first > last)
  1191. result = ((char *)NULL);
  1192. else
  1193. {
  1194. for (size = 0, i = first; i < last; i++)
  1195. size += strlen (list[i]) + 1;
  1196. result = (char *)xmalloc (size + 1);
  1197. result[0] = '\0';
  1198. for (i = first, offset = 0; i < last; i++)
  1199. {
  1200. strcpy (result + offset, list[i]);
  1201. offset += strlen (list[i]);
  1202. if (i + 1 < last)
  1203. {
  1204. result[offset++] = ' ';
  1205. result[offset] = 0;
  1206. }
  1207. }
  1208. }
  1209. for (i = 0; i < len; i++)
  1210. xfree (list[i]);
  1211. xfree (list);
  1212. return (result);
  1213. }
  1214. static int
  1215. history_tokenize_word (string, ind)
  1216. const char *string;
  1217. int ind;
  1218. {
  1219. register int i;
  1220. int delimiter, nestdelim, delimopen;
  1221. i = ind;
  1222. delimiter = nestdelim = 0;
  1223. if (member (string[i], "()\n"))
  1224. {
  1225. i++;
  1226. return i;
  1227. }
  1228. if (member (string[i], "<>;&|$"))
  1229. {
  1230. int peek = string[i + 1];
  1231. if (peek == string[i] && peek != '$')
  1232. {
  1233. if (peek == '<' && string[i + 2] == '-')
  1234. i++;
  1235. else if (peek == '<' && string[i + 2] == '<')
  1236. i++;
  1237. i += 2;
  1238. return i;
  1239. }
  1240. else if ((peek == '&' && (string[i] == '>' || string[i] == '<')) ||
  1241. (peek == '>' && string[i] == '&'))
  1242. {
  1243. i += 2;
  1244. return i;
  1245. }
  1246. /* XXX - separated out for later -- bash-4.2 */
  1247. else if ((peek == '(' && (string[i] == '>' || string[i] == '<')) || /* ) */
  1248. (peek == '(' && string[i] == '$')) /*)*/
  1249. {
  1250. i += 2;
  1251. delimopen = '(';
  1252. delimiter = ')';
  1253. nestdelim = 1;
  1254. goto get_word;
  1255. }
  1256. #if 0
  1257. else if (peek == '\'' && string[i] == '$')
  1258. {
  1259. i += 2; /* XXX */
  1260. return i;
  1261. }
  1262. #endif
  1263. if (string[i] != '$')
  1264. {
  1265. i++;
  1266. return i;
  1267. }
  1268. }
  1269. /* same code also used for $(...)/<(...)/>(...) above */
  1270. if (member (string[i], "!@?+*"))
  1271. {
  1272. int peek = string[i + 1];
  1273. if (peek == '(') /*)*/
  1274. {
  1275. /* Shell extended globbing patterns */
  1276. i += 2;
  1277. delimopen = '(';
  1278. delimiter = ')'; /* XXX - not perfect */
  1279. nestdelim = 1;
  1280. }
  1281. }
  1282. get_word:
  1283. /* Get word from string + i; */
  1284. if (delimiter == 0 && member (string[i], HISTORY_QUOTE_CHARACTERS))
  1285. delimiter = string[i++];
  1286. for (; string[i]; i++)
  1287. {
  1288. if (string[i] == '\\' && string[i + 1] == '\n')
  1289. {
  1290. i++;
  1291. continue;
  1292. }
  1293. if (string[i] == '\\' && delimiter != '\'' &&
  1294. (delimiter != '"' || member (string[i], slashify_in_quotes)))
  1295. {
  1296. i++;
  1297. continue;
  1298. }
  1299. /* delimiter must be set and set to something other than a quote if
  1300. nestdelim is set, so these tests are safe. */
  1301. if (nestdelim && string[i] == delimopen)
  1302. {
  1303. nestdelim++;
  1304. continue;
  1305. }
  1306. if (nestdelim && string[i] == delimiter)
  1307. {
  1308. nestdelim--;
  1309. if (nestdelim == 0)
  1310. delimiter = 0;
  1311. continue;
  1312. }
  1313. if (delimiter && string[i] == delimiter)
  1314. {
  1315. delimiter = 0;
  1316. continue;
  1317. }
  1318. if (delimiter == 0 && (member (string[i], history_word_delimiters)))
  1319. break;
  1320. if (delimiter == 0 && member (string[i], HISTORY_QUOTE_CHARACTERS))
  1321. delimiter = string[i];
  1322. }
  1323. return i;
  1324. }
  1325. static char *
  1326. history_substring (string, start, end)
  1327. const char *string;
  1328. int start, end;
  1329. {
  1330. register int len;
  1331. register char *result;
  1332. len = end - start;
  1333. result = (char *)xmalloc (len + 1);
  1334. strncpy (result, string + start, len);
  1335. result[len] = '\0';
  1336. return result;
  1337. }
  1338. /* Parse STRING into tokens and return an array of strings. If WIND is
  1339. not -1 and INDP is not null, we also want the word surrounding index
  1340. WIND. The position in the returned array of strings is returned in
  1341. *INDP. */
  1342. static char **
  1343. history_tokenize_internal (string, wind, indp)
  1344. const char *string;
  1345. int wind, *indp;
  1346. {
  1347. char **result;
  1348. register int i, start, result_index, size;
  1349. /* If we're searching for a string that's not part of a word (e.g., " "),
  1350. make sure we set *INDP to a reasonable value. */
  1351. if (indp && wind != -1)
  1352. *indp = -1;
  1353. /* Get a token, and stuff it into RESULT. The tokens are split
  1354. exactly where the shell would split them. */
  1355. for (i = result_index = size = 0, result = (char **)NULL; string[i]; )
  1356. {
  1357. /* Skip leading whitespace. */
  1358. for (; string[i] && whitespace (string[i]); i++)
  1359. ;
  1360. if (string[i] == 0 || string[i] == history_comment_char)
  1361. return (result);
  1362. start = i;
  1363. i = history_tokenize_word (string, start);
  1364. /* If we have a non-whitespace delimiter character (which would not be
  1365. skipped by the loop above), use it and any adjacent delimiters to
  1366. make a separate field. Any adjacent white space will be skipped the
  1367. next time through the loop. */
  1368. if (i == start && history_word_delimiters)
  1369. {
  1370. i++;
  1371. while (string[i] && member (string[i], history_word_delimiters))
  1372. i++;
  1373. }
  1374. /* If we are looking for the word in which the character at a
  1375. particular index falls, remember it. */
  1376. if (indp && wind != -1 && wind >= start && wind < i)
  1377. *indp = result_index;
  1378. if (result_index + 2 >= size)
  1379. result = (char **)xrealloc (result, ((size += 10) * sizeof (char *)));
  1380. result[result_index++] = history_substring (string, start, i);
  1381. result[result_index] = (char *)NULL;
  1382. }
  1383. return (result);
  1384. }
  1385. /* Return an array of tokens, much as the shell might. The tokens are
  1386. parsed out of STRING. */
  1387. char **
  1388. history_tokenize (string)
  1389. const char *string;
  1390. {
  1391. return (history_tokenize_internal (string, -1, (int *)NULL));
  1392. }
  1393. /* Free members of WORDS from START to an empty string */
  1394. static void
  1395. freewords (words, start)
  1396. char **words;
  1397. int start;
  1398. {
  1399. register int i;
  1400. for (i = start; words[i]; i++)
  1401. xfree (words[i]);
  1402. }
  1403. /* Find and return the word which contains the character at index IND
  1404. in the history line LINE. Used to save the word matched by the
  1405. last history !?string? search. */
  1406. static char *
  1407. history_find_word (line, ind)
  1408. char *line;
  1409. int ind;
  1410. {
  1411. char **words, *s;
  1412. int i, wind;
  1413. words = history_tokenize_internal (line, ind, &wind);
  1414. if (wind == -1 || words == 0)
  1415. {
  1416. if (words)
  1417. freewords (words, 0);
  1418. FREE (words);
  1419. return ((char *)NULL);
  1420. }
  1421. s = words[wind];
  1422. for (i = 0; i < wind; i++)
  1423. xfree (words[i]);
  1424. freewords (words, wind + 1);
  1425. xfree (words);
  1426. return s;
  1427. }