PageRenderTime 266ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 1ms

/subst.c

https://github.com/SnkBitten/android_external_bash
C | 9071 lines | 7503 code | 704 blank | 864 comment | 1375 complexity | 27d13ea50af0f630524507c4f96fee2d MD5 | raw file
Possible License(s): GPL-3.0
  1. /* subst.c -- The part of the shell that does parameter, command, arithmetic,
  2. and globbing substitutions. */
  3. /* ``Have a little faith, there's magic in the night. You ain't a
  4. beauty, but, hey, you're alright.'' */
  5. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7. Bash is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. Bash is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with Bash. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include "bashtypes.h"
  20. #include <stdio.h>
  21. #include "chartypes.h"
  22. #if defined (HAVE_PWD_H)
  23. # include <pwd.h>
  24. #endif
  25. #include <signal.h>
  26. #include <errno.h>
  27. #if defined (HAVE_UNISTD_H)
  28. # include <unistd.h>
  29. #endif
  30. #include "bashansi.h"
  31. #include "posixstat.h"
  32. #include "bashintl.h"
  33. #include "shell.h"
  34. #include "flags.h"
  35. #include "jobs.h"
  36. #include "execute_cmd.h"
  37. #include "filecntl.h"
  38. #include "trap.h"
  39. #include "pathexp.h"
  40. #include "mailcheck.h"
  41. #include "shmbutil.h"
  42. #include "builtins/getopt.h"
  43. #include "builtins/common.h"
  44. #include "builtins/builtext.h"
  45. #include <tilde/tilde.h>
  46. #include <glob/strmatch.h>
  47. #if !defined (errno)
  48. extern int errno;
  49. #endif /* !errno */
  50. /* The size that strings change by. */
  51. #define DEFAULT_INITIAL_ARRAY_SIZE 112
  52. #define DEFAULT_ARRAY_SIZE 128
  53. /* Variable types. */
  54. #define VT_VARIABLE 0
  55. #define VT_POSPARMS 1
  56. #define VT_ARRAYVAR 2
  57. #define VT_ARRAYMEMBER 3
  58. #define VT_ASSOCVAR 4
  59. #define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
  60. /* Flags for quoted_strchr */
  61. #define ST_BACKSL 0x01
  62. #define ST_CTLESC 0x02
  63. #define ST_SQUOTE 0x04 /* unused yet */
  64. #define ST_DQUOTE 0x08 /* unused yet */
  65. /* Flags for the `pflags' argument to param_expand() */
  66. #define PF_NOCOMSUB 0x01 /* Do not perform command substitution */
  67. #define PF_IGNUNBOUND 0x02 /* ignore unbound vars even if -u set */
  68. #define PF_NOSPLIT2 0x04 /* same as W_NOSPLIT2 */
  69. /* These defs make it easier to use the editor. */
  70. #define LBRACE '{'
  71. #define RBRACE '}'
  72. #define LPAREN '('
  73. #define RPAREN ')'
  74. #if defined (HANDLE_MULTIBYTE)
  75. #define WLPAREN L'('
  76. #define WRPAREN L')'
  77. #endif
  78. /* Evaluates to 1 if C is one of the shell's special parameters whose length
  79. can be taken, but is also one of the special expansion characters. */
  80. #define VALID_SPECIAL_LENGTH_PARAM(c) \
  81. ((c) == '-' || (c) == '?' || (c) == '#')
  82. /* Evaluates to 1 if C is one of the shell's special parameters for which an
  83. indirect variable reference may be made. */
  84. #define VALID_INDIR_PARAM(c) \
  85. ((c) == '#' || (c) == '?' || (c) == '@' || (c) == '*')
  86. /* Evaluates to 1 if C is one of the OP characters that follows the parameter
  87. in ${parameter[:]OPword}. */
  88. #define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
  89. /* Evaluates to 1 if this is one of the shell's special variables. */
  90. #define SPECIAL_VAR(name, wi) \
  91. ((DIGIT (*name) && all_digits (name)) || \
  92. (name[1] == '\0' && (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) || \
  93. (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1])))
  94. /* An expansion function that takes a string and a quoted flag and returns
  95. a WORD_LIST *. Used as the type of the third argument to
  96. expand_string_if_necessary(). */
  97. typedef WORD_LIST *EXPFUNC __P((char *, int));
  98. /* Process ID of the last command executed within command substitution. */
  99. pid_t last_command_subst_pid = NO_PID;
  100. pid_t current_command_subst_pid = NO_PID;
  101. /* Variables used to keep track of the characters in IFS. */
  102. SHELL_VAR *ifs_var;
  103. char *ifs_value;
  104. unsigned char ifs_cmap[UCHAR_MAX + 1];
  105. #if defined (HANDLE_MULTIBYTE)
  106. unsigned char ifs_firstc[MB_LEN_MAX];
  107. size_t ifs_firstc_len;
  108. #else
  109. unsigned char ifs_firstc;
  110. #endif
  111. /* Sentinel to tell when we are performing variable assignments preceding a
  112. command name and putting them into the environment. Used to make sure
  113. we use the temporary environment when looking up variable values. */
  114. int assigning_in_environment;
  115. /* Used to hold a list of variable assignments preceding a command. Global
  116. so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
  117. SIGCHLD trap and so it can be saved and restored by the trap handlers. */
  118. WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
  119. /* Extern functions and variables from different files. */
  120. extern int last_command_exit_value, last_command_exit_signal;
  121. extern int subshell_environment, line_number;
  122. extern int subshell_level, parse_and_execute_level, sourcelevel;
  123. extern int eof_encountered;
  124. extern int return_catch_flag, return_catch_value;
  125. extern pid_t dollar_dollar_pid;
  126. extern int posixly_correct;
  127. extern char *this_command_name;
  128. extern struct fd_bitmap *current_fds_to_close;
  129. extern int wordexp_only;
  130. extern int expanding_redir;
  131. extern int tempenv_assign_error;
  132. #if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
  133. extern wchar_t *wcsdup __P((const wchar_t *));
  134. #endif
  135. /* Non-zero means to allow unmatched globbed filenames to expand to
  136. a null file. */
  137. int allow_null_glob_expansion;
  138. /* Non-zero means to throw an error when globbing fails to match anything. */
  139. int fail_glob_expansion;
  140. #if 0
  141. /* Variables to keep track of which words in an expanded word list (the
  142. output of expand_word_list_internal) are the result of globbing
  143. expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
  144. (CURRENTLY UNUSED). */
  145. char *glob_argv_flags;
  146. static int glob_argv_flags_size;
  147. #endif
  148. static WORD_LIST expand_word_error, expand_word_fatal;
  149. static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
  150. static char expand_param_error, expand_param_fatal;
  151. static char extract_string_error, extract_string_fatal;
  152. /* Tell the expansion functions to not longjmp back to top_level on fatal
  153. errors. Enabled when doing completion and prompt string expansion. */
  154. static int no_longjmp_on_fatal_error = 0;
  155. /* Set by expand_word_unsplit; used to inhibit splitting and re-joining
  156. $* on $IFS, primarily when doing assignment statements. */
  157. static int expand_no_split_dollar_star = 0;
  158. /* A WORD_LIST of words to be expanded by expand_word_list_internal,
  159. without any leading variable assignments. */
  160. static WORD_LIST *garglist = (WORD_LIST *)NULL;
  161. static char *quoted_substring __P((char *, int, int));
  162. static int quoted_strlen __P((char *));
  163. static char *quoted_strchr __P((char *, int, int));
  164. static char *expand_string_if_necessary __P((char *, int, EXPFUNC *));
  165. static inline char *expand_string_to_string_internal __P((char *, int, EXPFUNC *));
  166. static WORD_LIST *call_expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
  167. static WORD_LIST *expand_string_internal __P((char *, int));
  168. static WORD_LIST *expand_string_leave_quoted __P((char *, int));
  169. static WORD_LIST *expand_string_for_rhs __P((char *, int, int *, int *));
  170. static WORD_LIST *list_quote_escapes __P((WORD_LIST *));
  171. static char *make_quoted_char __P((int));
  172. static WORD_LIST *quote_list __P((WORD_LIST *));
  173. static int unquoted_substring __P((char *, char *));
  174. static int unquoted_member __P((int, char *));
  175. #if defined (ARRAY_VARS)
  176. static SHELL_VAR *do_compound_assignment __P((char *, char *, int));
  177. #endif
  178. static int do_assignment_internal __P((const WORD_DESC *, int));
  179. static char *string_extract_verbatim __P((char *, size_t, int *, char *, int));
  180. static char *string_extract __P((char *, int *, char *, int));
  181. static char *string_extract_double_quoted __P((char *, int *, int));
  182. static inline char *string_extract_single_quoted __P((char *, int *));
  183. static inline int skip_single_quoted __P((const char *, size_t, int));
  184. static int skip_double_quoted __P((char *, size_t, int));
  185. static char *extract_delimited_string __P((char *, int *, char *, char *, char *, int));
  186. static char *extract_dollar_brace_string __P((char *, int *, int, int));
  187. static int skip_matched_pair __P((const char *, int, int, int, int));
  188. static char *pos_params __P((char *, int, int, int));
  189. static unsigned char *mb_getcharlens __P((char *, int));
  190. static char *remove_upattern __P((char *, char *, int));
  191. #if defined (HANDLE_MULTIBYTE)
  192. static wchar_t *remove_wpattern __P((wchar_t *, size_t, wchar_t *, int));
  193. #endif
  194. static char *remove_pattern __P((char *, char *, int));
  195. static int match_pattern_char __P((char *, char *));
  196. static int match_upattern __P((char *, char *, int, char **, char **));
  197. #if defined (HANDLE_MULTIBYTE)
  198. static int match_pattern_wchar __P((wchar_t *, wchar_t *));
  199. static int match_wpattern __P((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
  200. #endif
  201. static int match_pattern __P((char *, char *, int, char **, char **));
  202. static int getpatspec __P((int, char *));
  203. static char *getpattern __P((char *, int, int));
  204. static char *variable_remove_pattern __P((char *, char *, int, int));
  205. static char *list_remove_pattern __P((WORD_LIST *, char *, int, int, int));
  206. static char *parameter_list_remove_pattern __P((int, char *, int, int));
  207. #ifdef ARRAY_VARS
  208. static char *array_remove_pattern __P((SHELL_VAR *, char *, int, char *, int));
  209. #endif
  210. static char *parameter_brace_remove_pattern __P((char *, char *, char *, int, int));
  211. static char *process_substitute __P((char *, int));
  212. static char *read_comsub __P((int, int, int *));
  213. #ifdef ARRAY_VARS
  214. static arrayind_t array_length_reference __P((char *));
  215. #endif
  216. static int valid_brace_expansion_word __P((char *, int));
  217. static int chk_atstar __P((char *, int, int *, int *));
  218. static int chk_arithsub __P((const char *, int));
  219. static WORD_DESC *parameter_brace_expand_word __P((char *, int, int, int));
  220. static WORD_DESC *parameter_brace_expand_indir __P((char *, int, int, int *, int *));
  221. static WORD_DESC *parameter_brace_expand_rhs __P((char *, char *, int, int, int *, int *));
  222. static void parameter_brace_expand_error __P((char *, char *));
  223. static int valid_length_expression __P((char *));
  224. static intmax_t parameter_brace_expand_length __P((char *));
  225. static char *skiparith __P((char *, int));
  226. static int verify_substring_values __P((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *));
  227. static int get_var_and_type __P((char *, char *, int, SHELL_VAR **, char **));
  228. static char *mb_substring __P((char *, int, int));
  229. static char *parameter_brace_substring __P((char *, char *, char *, int));
  230. static char *pos_params_pat_subst __P((char *, char *, char *, int));
  231. static char *parameter_brace_patsub __P((char *, char *, char *, int));
  232. static char *pos_params_casemod __P((char *, char *, int, int));
  233. static char *parameter_brace_casemod __P((char *, char *, int, char *, int));
  234. static WORD_DESC *parameter_brace_expand __P((char *, int *, int, int, int *, int *));
  235. static WORD_DESC *param_expand __P((char *, int *, int, int *, int *, int *, int *, int));
  236. static WORD_LIST *expand_word_internal __P((WORD_DESC *, int, int, int *, int *));
  237. static WORD_LIST *word_list_split __P((WORD_LIST *));
  238. static void exp_jump_to_top_level __P((int));
  239. static WORD_LIST *separate_out_assignments __P((WORD_LIST *));
  240. static WORD_LIST *glob_expand_word_list __P((WORD_LIST *, int));
  241. #ifdef BRACE_EXPANSION
  242. static WORD_LIST *brace_expand_word_list __P((WORD_LIST *, int));
  243. #endif
  244. #if defined (ARRAY_VARS)
  245. static int make_internal_declare __P((char *, char *));
  246. #endif
  247. static WORD_LIST *shell_expand_word_list __P((WORD_LIST *, int));
  248. static WORD_LIST *expand_word_list_internal __P((WORD_LIST *, int));
  249. /* **************************************************************** */
  250. /* */
  251. /* Utility Functions */
  252. /* */
  253. /* **************************************************************** */
  254. #if defined (DEBUG)
  255. void
  256. dump_word_flags (flags)
  257. int flags;
  258. {
  259. int f;
  260. f = flags;
  261. fprintf (stderr, "%d -> ", f);
  262. if (f & W_ASSIGNASSOC)
  263. {
  264. f &= ~W_ASSIGNASSOC;
  265. fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
  266. }
  267. if (f & W_HASCTLESC)
  268. {
  269. f &= ~W_HASCTLESC;
  270. fprintf (stderr, "W_HASCTLESC%s", f ? "|" : "");
  271. }
  272. if (f & W_NOPROCSUB)
  273. {
  274. f &= ~W_NOPROCSUB;
  275. fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : "");
  276. }
  277. if (f & W_DQUOTE)
  278. {
  279. f &= ~W_DQUOTE;
  280. fprintf (stderr, "W_DQUOTE%s", f ? "|" : "");
  281. }
  282. if (f & W_HASQUOTEDNULL)
  283. {
  284. f &= ~W_HASQUOTEDNULL;
  285. fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : "");
  286. }
  287. if (f & W_ASSIGNARG)
  288. {
  289. f &= ~W_ASSIGNARG;
  290. fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : "");
  291. }
  292. if (f & W_ASSNBLTIN)
  293. {
  294. f &= ~W_ASSNBLTIN;
  295. fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : "");
  296. }
  297. if (f & W_COMPASSIGN)
  298. {
  299. f &= ~W_COMPASSIGN;
  300. fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
  301. }
  302. if (f & W_NOEXPAND)
  303. {
  304. f &= ~W_NOEXPAND;
  305. fprintf (stderr, "W_NOEXPAND%s", f ? "|" : "");
  306. }
  307. if (f & W_ITILDE)
  308. {
  309. f &= ~W_ITILDE;
  310. fprintf (stderr, "W_ITILDE%s", f ? "|" : "");
  311. }
  312. if (f & W_NOTILDE)
  313. {
  314. f &= ~W_NOTILDE;
  315. fprintf (stderr, "W_NOTILDE%s", f ? "|" : "");
  316. }
  317. if (f & W_ASSIGNRHS)
  318. {
  319. f &= ~W_ASSIGNRHS;
  320. fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : "");
  321. }
  322. if (f & W_NOCOMSUB)
  323. {
  324. f &= ~W_NOCOMSUB;
  325. fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : "");
  326. }
  327. if (f & W_DOLLARSTAR)
  328. {
  329. f &= ~W_DOLLARSTAR;
  330. fprintf (stderr, "W_DOLLARSTAR%s", f ? "|" : "");
  331. }
  332. if (f & W_DOLLARAT)
  333. {
  334. f &= ~W_DOLLARAT;
  335. fprintf (stderr, "W_DOLLARAT%s", f ? "|" : "");
  336. }
  337. if (f & W_TILDEEXP)
  338. {
  339. f &= ~W_TILDEEXP;
  340. fprintf (stderr, "W_TILDEEXP%s", f ? "|" : "");
  341. }
  342. if (f & W_NOSPLIT2)
  343. {
  344. f &= ~W_NOSPLIT2;
  345. fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : "");
  346. }
  347. if (f & W_NOGLOB)
  348. {
  349. f &= ~W_NOGLOB;
  350. fprintf (stderr, "W_NOGLOB%s", f ? "|" : "");
  351. }
  352. if (f & W_NOSPLIT)
  353. {
  354. f &= ~W_NOSPLIT;
  355. fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
  356. }
  357. if (f & W_GLOBEXP)
  358. {
  359. f &= ~W_GLOBEXP;
  360. fprintf (stderr, "W_GLOBEXP%s", f ? "|" : "");
  361. }
  362. if (f & W_ASSIGNMENT)
  363. {
  364. f &= ~W_ASSIGNMENT;
  365. fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : "");
  366. }
  367. if (f & W_QUOTED)
  368. {
  369. f &= ~W_QUOTED;
  370. fprintf (stderr, "W_QUOTED%s", f ? "|" : "");
  371. }
  372. if (f & W_HASDOLLAR)
  373. {
  374. f &= ~W_HASDOLLAR;
  375. fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : "");
  376. }
  377. fprintf (stderr, "\n");
  378. fflush (stderr);
  379. }
  380. #endif
  381. #ifdef INCLUDE_UNUSED
  382. static char *
  383. quoted_substring (string, start, end)
  384. char *string;
  385. int start, end;
  386. {
  387. register int len, l;
  388. register char *result, *s, *r;
  389. len = end - start;
  390. /* Move to string[start], skipping quoted characters. */
  391. for (s = string, l = 0; *s && l < start; )
  392. {
  393. if (*s == CTLESC)
  394. {
  395. s++;
  396. continue;
  397. }
  398. l++;
  399. if (*s == 0)
  400. break;
  401. }
  402. r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
  403. /* Copy LEN characters, including quote characters. */
  404. s = string + l;
  405. for (l = 0; l < len; s++)
  406. {
  407. if (*s == CTLESC)
  408. *r++ = *s++;
  409. *r++ = *s;
  410. l++;
  411. if (*s == 0)
  412. break;
  413. }
  414. *r = '\0';
  415. return result;
  416. }
  417. #endif
  418. #ifdef INCLUDE_UNUSED
  419. /* Return the length of S, skipping over quoted characters */
  420. static int
  421. quoted_strlen (s)
  422. char *s;
  423. {
  424. register char *p;
  425. int i;
  426. i = 0;
  427. for (p = s; *p; p++)
  428. {
  429. if (*p == CTLESC)
  430. {
  431. p++;
  432. if (*p == 0)
  433. return (i + 1);
  434. }
  435. i++;
  436. }
  437. return i;
  438. }
  439. #endif
  440. /* Find the first occurrence of character C in string S, obeying shell
  441. quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
  442. characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
  443. escaped with CTLESC are skipped. */
  444. static char *
  445. quoted_strchr (s, c, flags)
  446. char *s;
  447. int c, flags;
  448. {
  449. register char *p;
  450. for (p = s; *p; p++)
  451. {
  452. if (((flags & ST_BACKSL) && *p == '\\')
  453. || ((flags & ST_CTLESC) && *p == CTLESC))
  454. {
  455. p++;
  456. if (*p == '\0')
  457. return ((char *)NULL);
  458. continue;
  459. }
  460. else if (*p == c)
  461. return p;
  462. }
  463. return ((char *)NULL);
  464. }
  465. /* Return 1 if CHARACTER appears in an unquoted portion of
  466. STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
  467. static int
  468. unquoted_member (character, string)
  469. int character;
  470. char *string;
  471. {
  472. size_t slen;
  473. int sindex, c;
  474. DECLARE_MBSTATE;
  475. slen = strlen (string);
  476. sindex = 0;
  477. while (c = string[sindex])
  478. {
  479. if (c == character)
  480. return (1);
  481. switch (c)
  482. {
  483. default:
  484. ADVANCE_CHAR (string, slen, sindex);
  485. break;
  486. case '\\':
  487. sindex++;
  488. if (string[sindex])
  489. ADVANCE_CHAR (string, slen, sindex);
  490. break;
  491. case '\'':
  492. sindex = skip_single_quoted (string, slen, ++sindex);
  493. break;
  494. case '"':
  495. sindex = skip_double_quoted (string, slen, ++sindex);
  496. break;
  497. }
  498. }
  499. return (0);
  500. }
  501. /* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
  502. static int
  503. unquoted_substring (substr, string)
  504. char *substr, *string;
  505. {
  506. size_t slen;
  507. int sindex, c, sublen;
  508. DECLARE_MBSTATE;
  509. if (substr == 0 || *substr == '\0')
  510. return (0);
  511. slen = strlen (string);
  512. sublen = strlen (substr);
  513. for (sindex = 0; c = string[sindex]; )
  514. {
  515. if (STREQN (string + sindex, substr, sublen))
  516. return (1);
  517. switch (c)
  518. {
  519. case '\\':
  520. sindex++;
  521. if (string[sindex])
  522. ADVANCE_CHAR (string, slen, sindex);
  523. break;
  524. case '\'':
  525. sindex = skip_single_quoted (string, slen, ++sindex);
  526. break;
  527. case '"':
  528. sindex = skip_double_quoted (string, slen, ++sindex);
  529. break;
  530. default:
  531. ADVANCE_CHAR (string, slen, sindex);
  532. break;
  533. }
  534. }
  535. return (0);
  536. }
  537. /* Most of the substitutions must be done in parallel. In order
  538. to avoid using tons of unclear goto's, I have some functions
  539. for manipulating malloc'ed strings. They all take INDX, a
  540. pointer to an integer which is the offset into the string
  541. where manipulation is taking place. They also take SIZE, a
  542. pointer to an integer which is the current length of the
  543. character array for this string. */
  544. /* Append SOURCE to TARGET at INDEX. SIZE is the current amount
  545. of space allocated to TARGET. SOURCE can be NULL, in which
  546. case nothing happens. Gets rid of SOURCE by freeing it.
  547. Returns TARGET in case the location has changed. */
  548. INLINE char *
  549. sub_append_string (source, target, indx, size)
  550. char *source, *target;
  551. int *indx, *size;
  552. {
  553. if (source)
  554. {
  555. int srclen, n;
  556. srclen = STRLEN (source);
  557. if (srclen >= (int)(*size - *indx))
  558. {
  559. n = srclen + *indx;
  560. n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
  561. target = (char *)xrealloc (target, (*size = n));
  562. }
  563. FASTCOPY (source, target + *indx, srclen);
  564. *indx += srclen;
  565. target[*indx] = '\0';
  566. free (source);
  567. }
  568. return (target);
  569. }
  570. #if 0
  571. /* UNUSED */
  572. /* Append the textual representation of NUMBER to TARGET.
  573. INDX and SIZE are as in SUB_APPEND_STRING. */
  574. char *
  575. sub_append_number (number, target, indx, size)
  576. intmax_t number;
  577. int *indx, *size;
  578. char *target;
  579. {
  580. char *temp;
  581. temp = itos (number);
  582. return (sub_append_string (temp, target, indx, size));
  583. }
  584. #endif
  585. /* Extract a substring from STRING, starting at SINDEX and ending with
  586. one of the characters in CHARLIST. Don't make the ending character
  587. part of the string. Leave SINDEX pointing at the ending character.
  588. Understand about backslashes in the string. If (flags & SX_VARNAME)
  589. is non-zero, and array variables have been compiled into the shell,
  590. everything between a `[' and a corresponding `]' is skipped over.
  591. If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
  592. update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
  593. contain a closing character from CHARLIST. */
  594. static char *
  595. string_extract (string, sindex, charlist, flags)
  596. char *string;
  597. int *sindex;
  598. char *charlist;
  599. int flags;
  600. {
  601. register int c, i;
  602. int found;
  603. size_t slen;
  604. char *temp;
  605. DECLARE_MBSTATE;
  606. slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
  607. i = *sindex;
  608. found = 0;
  609. while (c = string[i])
  610. {
  611. if (c == '\\')
  612. {
  613. if (string[i + 1])
  614. i++;
  615. else
  616. break;
  617. }
  618. #if defined (ARRAY_VARS)
  619. else if ((flags & SX_VARNAME) && c == '[')
  620. {
  621. int ni;
  622. /* If this is an array subscript, skip over it and continue. */
  623. ni = skipsubscript (string, i, 0);
  624. if (string[ni] == ']')
  625. i = ni;
  626. }
  627. #endif
  628. else if (MEMBER (c, charlist))
  629. {
  630. found = 1;
  631. break;
  632. }
  633. ADVANCE_CHAR (string, slen, i);
  634. }
  635. /* If we had to have a matching delimiter and didn't find one, return an
  636. error and let the caller deal with it. */
  637. if ((flags & SX_REQMATCH) && found == 0)
  638. {
  639. *sindex = i;
  640. return (&extract_string_error);
  641. }
  642. temp = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
  643. *sindex = i;
  644. return (temp);
  645. }
  646. /* Extract the contents of STRING as if it is enclosed in double quotes.
  647. SINDEX, when passed in, is the offset of the character immediately
  648. following the opening double quote; on exit, SINDEX is left pointing after
  649. the closing double quote. If STRIPDQ is non-zero, unquoted double
  650. quotes are stripped and the string is terminated by a null byte.
  651. Backslashes between the embedded double quotes are processed. If STRIPDQ
  652. is zero, an unquoted `"' terminates the string. */
  653. static char *
  654. string_extract_double_quoted (string, sindex, stripdq)
  655. char *string;
  656. int *sindex, stripdq;
  657. {
  658. size_t slen;
  659. char *send;
  660. int j, i, t;
  661. unsigned char c;
  662. char *temp, *ret; /* The new string we return. */
  663. int pass_next, backquote, si; /* State variables for the machine. */
  664. int dquote;
  665. DECLARE_MBSTATE;
  666. slen = strlen (string + *sindex) + *sindex;
  667. send = string + slen;
  668. pass_next = backquote = dquote = 0;
  669. temp = (char *)xmalloc (1 + slen - *sindex);
  670. j = 0;
  671. i = *sindex;
  672. while (c = string[i])
  673. {
  674. /* Process a character that was quoted by a backslash. */
  675. if (pass_next)
  676. {
  677. /* Posix.2 sez:
  678. ``The backslash shall retain its special meaning as an escape
  679. character only when followed by one of the characters:
  680. $ ` " \ <newline>''.
  681. If STRIPDQ is zero, we handle the double quotes here and let
  682. expand_word_internal handle the rest. If STRIPDQ is non-zero,
  683. we have already been through one round of backslash stripping,
  684. and want to strip these backslashes only if DQUOTE is non-zero,
  685. indicating that we are inside an embedded double-quoted string. */
  686. /* If we are in an embedded quoted string, then don't strip
  687. backslashes before characters for which the backslash
  688. retains its special meaning, but remove backslashes in
  689. front of other characters. If we are not in an
  690. embedded quoted string, don't strip backslashes at all.
  691. This mess is necessary because the string was already
  692. surrounded by double quotes (and sh has some really weird
  693. quoting rules).
  694. The returned string will be run through expansion as if
  695. it were double-quoted. */
  696. if ((stripdq == 0 && c != '"') ||
  697. (stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
  698. temp[j++] = '\\';
  699. pass_next = 0;
  700. add_one_character:
  701. COPY_CHAR_I (temp, j, string, send, i);
  702. continue;
  703. }
  704. /* A backslash protects the next character. The code just above
  705. handles preserving the backslash in front of any character but
  706. a double quote. */
  707. if (c == '\\')
  708. {
  709. pass_next++;
  710. i++;
  711. continue;
  712. }
  713. /* Inside backquotes, ``the portion of the quoted string from the
  714. initial backquote and the characters up to the next backquote
  715. that is not preceded by a backslash, having escape characters
  716. removed, defines that command''. */
  717. if (backquote)
  718. {
  719. if (c == '`')
  720. backquote = 0;
  721. temp[j++] = c;
  722. i++;
  723. continue;
  724. }
  725. if (c == '`')
  726. {
  727. temp[j++] = c;
  728. backquote++;
  729. i++;
  730. continue;
  731. }
  732. /* Pass everything between `$(' and the matching `)' or a quoted
  733. ${ ... } pair through according to the Posix.2 specification. */
  734. if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
  735. {
  736. int free_ret = 1;
  737. si = i + 2;
  738. if (string[i + 1] == LPAREN)
  739. ret = extract_command_subst (string, &si, 0);
  740. else
  741. ret = extract_dollar_brace_string (string, &si, 1, 0);
  742. temp[j++] = '$';
  743. temp[j++] = string[i + 1];
  744. /* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
  745. is set. */
  746. if (ret == 0 && no_longjmp_on_fatal_error)
  747. {
  748. free_ret = 0;
  749. ret = string + i + 2;
  750. }
  751. for (t = 0; ret[t]; t++, j++)
  752. temp[j] = ret[t];
  753. temp[j] = string[si];
  754. if (string[si])
  755. {
  756. j++;
  757. i = si + 1;
  758. }
  759. else
  760. i = si;
  761. if (free_ret)
  762. free (ret);
  763. continue;
  764. }
  765. /* Add any character but a double quote to the quoted string we're
  766. accumulating. */
  767. if (c != '"')
  768. goto add_one_character;
  769. /* c == '"' */
  770. if (stripdq)
  771. {
  772. dquote ^= 1;
  773. i++;
  774. continue;
  775. }
  776. break;
  777. }
  778. temp[j] = '\0';
  779. /* Point to after the closing quote. */
  780. if (c)
  781. i++;
  782. *sindex = i;
  783. return (temp);
  784. }
  785. /* This should really be another option to string_extract_double_quoted. */
  786. static int
  787. skip_double_quoted (string, slen, sind)
  788. char *string;
  789. size_t slen;
  790. int sind;
  791. {
  792. int c, i;
  793. char *ret;
  794. int pass_next, backquote, si;
  795. DECLARE_MBSTATE;
  796. pass_next = backquote = 0;
  797. i = sind;
  798. while (c = string[i])
  799. {
  800. if (pass_next)
  801. {
  802. pass_next = 0;
  803. ADVANCE_CHAR (string, slen, i);
  804. continue;
  805. }
  806. else if (c == '\\')
  807. {
  808. pass_next++;
  809. i++;
  810. continue;
  811. }
  812. else if (backquote)
  813. {
  814. if (c == '`')
  815. backquote = 0;
  816. ADVANCE_CHAR (string, slen, i);
  817. continue;
  818. }
  819. else if (c == '`')
  820. {
  821. backquote++;
  822. i++;
  823. continue;
  824. }
  825. else if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
  826. {
  827. si = i + 2;
  828. if (string[i + 1] == LPAREN)
  829. ret = extract_command_subst (string, &si, SX_NOALLOC);
  830. else
  831. ret = extract_dollar_brace_string (string, &si, 1, SX_NOALLOC);
  832. i = si + 1;
  833. continue;
  834. }
  835. else if (c != '"')
  836. {
  837. ADVANCE_CHAR (string, slen, i);
  838. continue;
  839. }
  840. else
  841. break;
  842. }
  843. if (c)
  844. i++;
  845. return (i);
  846. }
  847. /* Extract the contents of STRING as if it is enclosed in single quotes.
  848. SINDEX, when passed in, is the offset of the character immediately
  849. following the opening single quote; on exit, SINDEX is left pointing after
  850. the closing single quote. */
  851. static inline char *
  852. string_extract_single_quoted (string, sindex)
  853. char *string;
  854. int *sindex;
  855. {
  856. register int i;
  857. size_t slen;
  858. char *t;
  859. DECLARE_MBSTATE;
  860. /* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
  861. slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
  862. i = *sindex;
  863. while (string[i] && string[i] != '\'')
  864. ADVANCE_CHAR (string, slen, i);
  865. t = substring (string, *sindex, i);
  866. if (string[i])
  867. i++;
  868. *sindex = i;
  869. return (t);
  870. }
  871. static inline int
  872. skip_single_quoted (string, slen, sind)
  873. const char *string;
  874. size_t slen;
  875. int sind;
  876. {
  877. register int c;
  878. DECLARE_MBSTATE;
  879. c = sind;
  880. while (string[c] && string[c] != '\'')
  881. ADVANCE_CHAR (string, slen, c);
  882. if (string[c])
  883. c++;
  884. return c;
  885. }
  886. /* Just like string_extract, but doesn't hack backslashes or any of
  887. that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
  888. static char *
  889. string_extract_verbatim (string, slen, sindex, charlist, flags)
  890. char *string;
  891. size_t slen;
  892. int *sindex;
  893. char *charlist;
  894. int flags;
  895. {
  896. register int i;
  897. #if defined (HANDLE_MULTIBYTE)
  898. size_t clen;
  899. wchar_t *wcharlist;
  900. #endif
  901. int c;
  902. char *temp;
  903. DECLARE_MBSTATE;
  904. if (charlist[0] == '\'' && charlist[1] == '\0')
  905. {
  906. temp = string_extract_single_quoted (string, sindex);
  907. --*sindex; /* leave *sindex at separator character */
  908. return temp;
  909. }
  910. i = *sindex;
  911. #if 0
  912. /* See how the MBLEN and ADVANCE_CHAR macros work to understand why we need
  913. this only if MB_CUR_MAX > 1. */
  914. slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 1;
  915. #endif
  916. #if defined (HANDLE_MULTIBYTE)
  917. clen = strlen (charlist);
  918. wcharlist = 0;
  919. #endif
  920. while (c = string[i])
  921. {
  922. #if defined (HANDLE_MULTIBYTE)
  923. size_t mblength;
  924. #endif
  925. if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
  926. {
  927. i += 2;
  928. continue;
  929. }
  930. /* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
  931. through, to protect the CTLNULs from later calls to
  932. remove_quoted_nulls. */
  933. else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC && string[i+1] == CTLNUL)
  934. {
  935. i += 2;
  936. continue;
  937. }
  938. #if defined (HANDLE_MULTIBYTE)
  939. mblength = MBLEN (string + i, slen - i);
  940. if (mblength > 1)
  941. {
  942. wchar_t wc;
  943. mblength = mbtowc (&wc, string + i, slen - i);
  944. if (MB_INVALIDCH (mblength))
  945. {
  946. if (MEMBER (c, charlist))
  947. break;
  948. }
  949. else
  950. {
  951. if (wcharlist == 0)
  952. {
  953. size_t len;
  954. len = mbstowcs (wcharlist, charlist, 0);
  955. if (len == -1)
  956. len = 0;
  957. wcharlist = (wchar_t *)xmalloc (sizeof (wchar_t) * (len + 1));
  958. mbstowcs (wcharlist, charlist, len + 1);
  959. }
  960. if (wcschr (wcharlist, wc))
  961. break;
  962. }
  963. }
  964. else
  965. #endif
  966. if (MEMBER (c, charlist))
  967. break;
  968. ADVANCE_CHAR (string, slen, i);
  969. }
  970. #if defined (HANDLE_MULTIBYTE)
  971. FREE (wcharlist);
  972. #endif
  973. temp = substring (string, *sindex, i);
  974. *sindex = i;
  975. return (temp);
  976. }
  977. /* Extract the $( construct in STRING, and return a new string.
  978. Start extracting at (SINDEX) as if we had just seen "$(".
  979. Make (SINDEX) get the position of the matching ")". )
  980. XFLAGS is additional flags to pass to other extraction functions. */
  981. char *
  982. extract_command_subst (string, sindex, xflags)
  983. char *string;
  984. int *sindex;
  985. int xflags;
  986. {
  987. if (string[*sindex] == LPAREN)
  988. return (extract_delimited_string (string, sindex, "$(", "(", ")", xflags|SX_COMMAND)); /*)*/
  989. else
  990. {
  991. xflags |= (no_longjmp_on_fatal_error ? SX_NOLONGJMP : 0);
  992. return (xparse_dolparen (string, string+*sindex, sindex, xflags));
  993. }
  994. }
  995. /* Extract the $[ construct in STRING, and return a new string. (])
  996. Start extracting at (SINDEX) as if we had just seen "$[".
  997. Make (SINDEX) get the position of the matching "]". */
  998. char *
  999. extract_arithmetic_subst (string, sindex)
  1000. char *string;
  1001. int *sindex;
  1002. {
  1003. return (extract_delimited_string (string, sindex, "$[", "[", "]", 0)); /*]*/
  1004. }
  1005. #if defined (PROCESS_SUBSTITUTION)
  1006. /* Extract the <( or >( construct in STRING, and return a new string.
  1007. Start extracting at (SINDEX) as if we had just seen "<(".
  1008. Make (SINDEX) get the position of the matching ")". */ /*))*/
  1009. char *
  1010. extract_process_subst (string, starter, sindex)
  1011. char *string;
  1012. char *starter;
  1013. int *sindex;
  1014. {
  1015. return (extract_delimited_string (string, sindex, starter, "(", ")", 0));
  1016. }
  1017. #endif /* PROCESS_SUBSTITUTION */
  1018. #if defined (ARRAY_VARS)
  1019. /* This can be fooled by unquoted right parens in the passed string. If
  1020. each caller verifies that the last character in STRING is a right paren,
  1021. we don't even need to call extract_delimited_string. */
  1022. char *
  1023. extract_array_assignment_list (string, sindex)
  1024. char *string;
  1025. int *sindex;
  1026. {
  1027. int slen;
  1028. char *ret;
  1029. slen = strlen (string); /* ( */
  1030. if (string[slen - 1] == ')')
  1031. {
  1032. ret = substring (string, *sindex, slen - 1);
  1033. *sindex = slen - 1;
  1034. return ret;
  1035. }
  1036. return 0;
  1037. }
  1038. #endif
  1039. /* Extract and create a new string from the contents of STRING, a
  1040. character string delimited with OPENER and CLOSER. SINDEX is
  1041. the address of an int describing the current offset in STRING;
  1042. it should point to just after the first OPENER found. On exit,
  1043. SINDEX gets the position of the last character of the matching CLOSER.
  1044. If OPENER is more than a single character, ALT_OPENER, if non-null,
  1045. contains a character string that can also match CLOSER and thus
  1046. needs to be skipped. */
  1047. static char *
  1048. extract_delimited_string (string, sindex, opener, alt_opener, closer, flags)
  1049. char *string;
  1050. int *sindex;
  1051. char *opener, *alt_opener, *closer;
  1052. int flags;
  1053. {
  1054. int i, c, si;
  1055. size_t slen;
  1056. char *t, *result;
  1057. int pass_character, nesting_level, in_comment;
  1058. int len_closer, len_opener, len_alt_opener;
  1059. DECLARE_MBSTATE;
  1060. slen = strlen (string + *sindex) + *sindex;
  1061. len_opener = STRLEN (opener);
  1062. len_alt_opener = STRLEN (alt_opener);
  1063. len_closer = STRLEN (closer);
  1064. pass_character = in_comment = 0;
  1065. nesting_level = 1;
  1066. i = *sindex;
  1067. while (nesting_level)
  1068. {
  1069. c = string[i];
  1070. if (c == 0)
  1071. break;
  1072. if (in_comment)
  1073. {
  1074. if (c == '\n')
  1075. in_comment = 0;
  1076. ADVANCE_CHAR (string, slen, i);
  1077. continue;
  1078. }
  1079. if (pass_character) /* previous char was backslash */
  1080. {
  1081. pass_character = 0;
  1082. ADVANCE_CHAR (string, slen, i);
  1083. continue;
  1084. }
  1085. /* Not exactly right yet; should handle shell metacharacters and
  1086. multibyte characters, too. See COMMENT_BEGIN define in parse.y */
  1087. if ((flags & SX_COMMAND) && c == '#' && (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
  1088. {
  1089. in_comment = 1;
  1090. ADVANCE_CHAR (string, slen, i);
  1091. continue;
  1092. }
  1093. if (c == CTLESC || c == '\\')
  1094. {
  1095. pass_character++;
  1096. i++;
  1097. continue;
  1098. }
  1099. #if 0
  1100. /* Process a nested command substitution, but only if we're parsing a
  1101. command substitution. XXX - for bash-4.2 */
  1102. if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN)
  1103. {
  1104. si = i + 2;
  1105. t = extract_command_subst (string, &si, flags);
  1106. i = si + 1;
  1107. continue;
  1108. }
  1109. #endif
  1110. /* Process a nested OPENER. */
  1111. if (STREQN (string + i, opener, len_opener))
  1112. {
  1113. si = i + len_opener;
  1114. t = extract_delimited_string (string, &si, opener, alt_opener, closer, flags|SX_NOALLOC);
  1115. i = si + 1;
  1116. continue;
  1117. }
  1118. /* Process a nested ALT_OPENER */
  1119. if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
  1120. {
  1121. si = i + len_alt_opener;
  1122. t = extract_delimited_string (string, &si, alt_opener, alt_opener, closer, flags|SX_NOALLOC);
  1123. i = si + 1;
  1124. continue;
  1125. }
  1126. /* If the current substring terminates the delimited string, decrement
  1127. the nesting level. */
  1128. if (STREQN (string + i, closer, len_closer))
  1129. {
  1130. i += len_closer - 1; /* move to last byte of the closer */
  1131. nesting_level--;
  1132. if (nesting_level == 0)
  1133. break;
  1134. }
  1135. /* Pass old-style command substitution through verbatim. */
  1136. if (c == '`')
  1137. {
  1138. si = i + 1;
  1139. t = string_extract (string, &si, "`", flags|SX_NOALLOC);
  1140. i = si + 1;
  1141. continue;
  1142. }
  1143. /* Pass single-quoted and double-quoted strings through verbatim. */
  1144. if (c == '\'' || c == '"')
  1145. {
  1146. si = i + 1;
  1147. i = (c == '\'') ? skip_single_quoted (string, slen, si)
  1148. : skip_double_quoted (string, slen, si);
  1149. continue;
  1150. }
  1151. /* move past this character, which was not special. */
  1152. ADVANCE_CHAR (string, slen, i);
  1153. }
  1154. if (c == 0 && nesting_level)
  1155. {
  1156. if (no_longjmp_on_fatal_error == 0)
  1157. {
  1158. report_error (_("bad substitution: no closing `%s' in %s"), closer, string);
  1159. last_command_exit_value = EXECUTION_FAILURE;
  1160. exp_jump_to_top_level (DISCARD);
  1161. }
  1162. else
  1163. {
  1164. *sindex = i;
  1165. return (char *)NULL;
  1166. }
  1167. }
  1168. si = i - *sindex - len_closer + 1;
  1169. if (flags & SX_NOALLOC)
  1170. result = (char *)NULL;
  1171. else
  1172. {
  1173. result = (char *)xmalloc (1 + si);
  1174. strncpy (result, string + *sindex, si);
  1175. result[si] = '\0';
  1176. }
  1177. *sindex = i;
  1178. return (result);
  1179. }
  1180. /* Extract a parameter expansion expression within ${ and } from STRING.
  1181. Obey the Posix.2 rules for finding the ending `}': count braces while
  1182. skipping over enclosed quoted strings and command substitutions.
  1183. SINDEX is the address of an int describing the current offset in STRING;
  1184. it should point to just after the first `{' found. On exit, SINDEX
  1185. gets the position of the matching `}'. QUOTED is non-zero if this
  1186. occurs inside double quotes. */
  1187. /* XXX -- this is very similar to extract_delimited_string -- XXX */
  1188. static char *
  1189. extract_dollar_brace_string (string, sindex, quoted, flags)
  1190. char *string;
  1191. int *sindex, quoted, flags;
  1192. {
  1193. register int i, c;
  1194. size_t slen;
  1195. int pass_character, nesting_level, si;
  1196. char *result, *t;
  1197. DECLARE_MBSTATE;
  1198. pass_character = 0;
  1199. nesting_level = 1;
  1200. slen = strlen (string + *sindex) + *sindex;
  1201. i = *sindex;
  1202. while (c = string[i])
  1203. {
  1204. if (pass_character)
  1205. {
  1206. pass_character = 0;
  1207. ADVANCE_CHAR (string, slen, i);
  1208. continue;
  1209. }
  1210. /* CTLESCs and backslashes quote the next character. */
  1211. if (c == CTLESC || c == '\\')
  1212. {
  1213. pass_character++;
  1214. i++;
  1215. continue;
  1216. }
  1217. if (string[i] == '$' && string[i+1] == LBRACE)
  1218. {
  1219. nesting_level++;
  1220. i += 2;
  1221. continue;
  1222. }
  1223. if (c == RBRACE)
  1224. {
  1225. nesting_level--;
  1226. if (nesting_level == 0)
  1227. break;
  1228. i++;
  1229. continue;
  1230. }
  1231. /* Pass the contents of old-style command substitutions through
  1232. verbatim. */
  1233. if (c == '`')
  1234. {
  1235. si = i + 1;
  1236. t = string_extract (string, &si, "`", flags|SX_NOALLOC);
  1237. i = si + 1;
  1238. continue;
  1239. }
  1240. /* Pass the contents of new-style command substitutions and
  1241. arithmetic substitutions through verbatim. */
  1242. if (string[i] == '$' && string[i+1] == LPAREN)
  1243. {
  1244. si = i + 2;
  1245. t = extract_command_subst (string, &si, flags|SX_NOALLOC);
  1246. i = si + 1;
  1247. continue;
  1248. }
  1249. /* Pass the contents of single-quoted and double-quoted strings
  1250. through verbatim. */
  1251. if (c == '\'' || c == '"')
  1252. {
  1253. si = i + 1;
  1254. i = (c == '\'') ? skip_single_quoted (string, slen, si)
  1255. : skip_double_quoted (string, slen, si);
  1256. /* skip_XXX_quoted leaves index one past close quote */
  1257. continue;
  1258. }
  1259. /* move past this character, which was not special. */
  1260. ADVANCE_CHAR (string, slen, i);
  1261. }
  1262. if (c == 0 && nesting_level)
  1263. {
  1264. if (no_longjmp_on_fatal_error == 0)
  1265. { /* { */
  1266. report_error (_("bad substitution: no closing `%s' in %s"), "}", string);
  1267. last_command_exit_value = EXECUTION_FAILURE;
  1268. exp_jump_to_top_level (DISCARD);
  1269. }
  1270. else
  1271. {
  1272. *sindex = i;
  1273. return ((char *)NULL);
  1274. }
  1275. }
  1276. result = (flags & SX_NOALLOC) ? (char *)NULL : substring (string, *sindex, i);
  1277. *sindex = i;
  1278. return (result);
  1279. }
  1280. /* Remove backslashes which are quoting backquotes from STRING. Modifies
  1281. STRING, and returns a pointer to it. */
  1282. char *
  1283. de_backslash (string)
  1284. char *string;
  1285. {
  1286. register size_t slen;
  1287. register int i, j, prev_i;
  1288. DECLARE_MBSTATE;
  1289. slen = strlen (string);
  1290. i = j = 0;
  1291. /* Loop copying string[i] to string[j], i >= j. */
  1292. while (i < slen)
  1293. {
  1294. if (string[i] == '\\' && (string[i + 1] == '`' || string[i + 1] == '\\' ||
  1295. string[i + 1] == '$'))
  1296. i++;
  1297. prev_i = i;
  1298. ADVANCE_CHAR (string, slen, i);
  1299. if (j < prev_i)
  1300. do string[j++] = string[prev_i++]; while (prev_i < i);
  1301. else
  1302. j = i;
  1303. }
  1304. string[j] = '\0';
  1305. return (string);
  1306. }
  1307. #if 0
  1308. /*UNUSED*/
  1309. /* Replace instances of \! in a string with !. */
  1310. void
  1311. unquote_bang (string)
  1312. char *string;
  1313. {
  1314. register int i, j;
  1315. register char *temp;
  1316. temp = (char *)xmalloc (1 + strlen (string));
  1317. for (i = 0, j = 0; (temp[j] = string[i]); i++, j++)
  1318. {
  1319. if (string[i] == '\\' && string[i + 1] == '!')
  1320. {
  1321. temp[j] = '!';
  1322. i++;
  1323. }
  1324. }
  1325. strcpy (string, temp);
  1326. free (temp);
  1327. }
  1328. #endif
  1329. #define CQ_RETURN(x) do { no_longjmp_on_fatal_error = 0; return (x); } while (0)
  1330. /* This function assumes s[i] == open; returns with s[ret] == close; used to
  1331. parse array subscripts. FLAGS & 1 means to not attempt to skip over
  1332. matched pairs of quotes or backquotes, or skip word expansions; it is
  1333. intended to be used after expansion has been performed and during final
  1334. assignment parsing (see arrayfunc.c:assign_compound_array_list()). */
  1335. static int
  1336. skip_matched_pair (string, start, open, close, flags)
  1337. const char *string;
  1338. int start, open, close, flags;
  1339. {
  1340. int i, pass_next, backq, si, c, count;
  1341. size_t slen;
  1342. char *temp, *ss;
  1343. DECLARE_MBSTATE;
  1344. slen = strlen (string + start) + start;
  1345. no_longjmp_on_fatal_error = 1;
  1346. i = start + 1; /* skip over leading bracket */
  1347. count = 1;
  1348. pass_next = backq = 0;
  1349. ss = (char *)string;
  1350. while (c = string[i])
  1351. {
  1352. if (pass_next)
  1353. {
  1354. pass_next = 0;
  1355. if (c == 0)
  1356. CQ_RETURN(i);
  1357. ADVANCE_CHAR (string, slen, i);
  1358. continue;
  1359. }
  1360. else if (c == '\\')
  1361. {
  1362. pass_next = 1;
  1363. i++;
  1364. continue;
  1365. }
  1366. else if (backq)
  1367. {
  1368. if (c == '`')
  1369. backq = 0;
  1370. ADVANCE_CHAR (string, slen, i);
  1371. continue;
  1372. }
  1373. else if ((flags & 1) == 0 && c == '`')
  1374. {
  1375. backq = 1;
  1376. i++;
  1377. continue;
  1378. }
  1379. else if ((flags & 1) == 0 && c == open)
  1380. {
  1381. count++;
  1382. i++;
  1383. continue;
  1384. }
  1385. else if (c == close)
  1386. {
  1387. count--;
  1388. if (count == 0)
  1389. break;
  1390. i++;
  1391. continue;
  1392. }
  1393. else if ((flags & 1) == 0 && (c == '\'' || c == '"'))
  1394. {
  1395. i = (c == '\'') ? skip_single_quoted (ss, slen, ++i)
  1396. : skip_double_quoted (ss, slen, ++i);
  1397. /* no increment, the skip functions increment past the closing quote. */
  1398. }
  1399. else if ((flags&1) == 0 && c == '$' && (string[i+1] == LPAREN || string[i+1] == LBRACE))
  1400. {
  1401. si = i + 2;
  1402. if (string[si] == '\0')
  1403. CQ_RETURN(si);
  1404. if (string[i+1] == LPAREN)
  1405. temp = extract_delimited_string (ss, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
  1406. else
  1407. temp = extract_dollar_brace_string (ss, &si, 0, SX_NOALLOC);
  1408. i = si;
  1409. if (string[i] == '\0') /* don't increment i past EOS in loop */
  1410. break;
  1411. i++;
  1412. continue;
  1413. }
  1414. else
  1415. ADVANCE_CHAR (string, slen, i);
  1416. }
  1417. CQ_RETURN(i);
  1418. }
  1419. #if defined (ARRAY_VARS)
  1420. int
  1421. skipsubscript (string, start, flags)
  1422. const char *string;
  1423. int start, flags;
  1424. {
  1425. return (skip_matched_pair (string, start, '[', ']', flags));
  1426. }
  1427. #endif
  1428. /* Skip characters in STRING until we find a character in DELIMS, and return
  1429. the index of that character. START is the index into string at which we
  1430. begin. This is similar in spirit to strpbrk, but it returns an index into
  1431. STRING and takes a starting index. This little piece of code knows quite
  1432. a lot of shell syntax. It's very similar to skip_double_quoted and other
  1433. functions of that ilk. */
  1434. int
  1435. skip_to_delim (string, start, delims, flags)
  1436. char *string;
  1437. int start;
  1438. char *delims;
  1439. int flags;
  1440. {
  1441. int i, pass_next, backq, si, c, invert, skipquote, skipcmd;
  1442. size_t slen;
  1443. char *temp;
  1444. DECLARE_MBSTATE;
  1445. slen = strlen (string + start) + start;
  1446. if (flags & SD_NOJMP)
  1447. no_longjmp_on_fatal_error = 1;
  1448. invert = (flags & SD_INVERT);
  1449. skipcmd = (flags & SD_NOSKIPCMD) == 0;
  1450. i = start;
  1451. pass_next = backq = 0;
  1452. while (c = string[i])
  1453. {
  1454. /* If this is non-zero, we should not let quote characters be delimiters
  1455. and the current character is a single or double quote. We should not
  1456. test whether or not it's a delimiter until after we skip single- or
  1457. double-quoted strings. */
  1458. skipquote = ((flags & SD_NOQUOTEDELIM) && (c == '\'' || c =='"'));
  1459. if (pass_next)
  1460. {
  1461. pass_next = 0;
  1462. if (c == 0)
  1463. CQ_RETURN(i);
  1464. ADVANCE_CHAR (string, slen, i);
  1465. continue;
  1466. }
  1467. else if (c == '\\')
  1468. {
  1469. pass_next = 1;
  1470. i++;
  1471. continue;
  1472. }
  1473. else if (backq)
  1474. {
  1475. if (c == '`')
  1476. backq = 0;
  1477. ADVANCE_CHAR (string, slen, i);
  1478. continue;
  1479. }
  1480. else if (c == '`')
  1481. {
  1482. backq = 1;
  1483. i++;
  1484. continue;
  1485. }
  1486. else if (skipquote == 0 && invert == 0 && member (c, delims))
  1487. break;
  1488. else if (c == '\'' || c == '"')
  1489. {
  1490. i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
  1491. : skip_double_quoted (string, slen, ++i);
  1492. /* no increment, the skip functions increment past the closing quote. */
  1493. }
  1494. else if (c == '$' && ((skipcmd && string[i+1] == LPAREN) || string[i+1] == LBRACE))
  1495. {
  1496. si = i + 2;
  1497. if (string[si] == '\0')
  1498. CQ_RETURN(si);
  1499. if (string[i+1] == LPAREN)
  1500. temp = extract_delimited_string (string, &si, "$(", "(", ")", SX_NOALLOC|SX_COMMAND); /* ) */
  1501. else
  1502. temp = extract_dollar_brace_string (string, &si, 0, SX_NOALLOC);
  1503. i = si;
  1504. if (string[i] == '\0') /* don't increment i past EOS in loop */
  1505. break;
  1506. i++;
  1507. continue;
  1508. }
  1509. #if defined (PROCESS_SUBSTITUTION)
  1510. else if (skipcmd && (c == '<' || c == '>') && string[i+1] == LPAREN)
  1511. {
  1512. si = i + 2;
  1513. if (string[si] == '\0')
  1514. CQ_RETURN(si);
  1515. temp = extract_process_subst (string, (c == '<') ? "<(" : ">(", &si);
  1516. i = si;
  1517. if (string[i] == '\0')
  1518. break;
  1519. i++;
  1520. continue;
  1521. }
  1522. #endif /* PROCESS_SUBSTITUTION */
  1523. else if ((skipquote || invert) && (member (c, delims) == 0))
  1524. break;
  1525. else
  1526. ADVANCE_CHAR (string, slen, i);
  1527. }
  1528. CQ_RETURN(i);
  1529. }
  1530. #if defined (READLINE)
  1531. /* Return 1 if the portion of STRING ending at EINDEX is quoted (there is
  1532. an unclosed quoted string), or if the character at EINDEX is quoted
  1533. by a backslash. NO_LONGJMP_ON_FATAL_ERROR is used to flag that the various
  1534. single and double-quoted string parsing functions should not return an
  1535. error if there are unclosed quotes or braces. The characters that this
  1536. recognizes need to be the same as the contents of
  1537. rl_completer_quote_characters. */
  1538. int
  1539. char_is_quoted (string, eindex)
  1540. char *string;
  1541. int eindex;
  1542. {
  1543. int i, pass_next, c;
  1544. size_t slen;
  1545. DECLARE_MBSTATE;
  1546. slen = strlen (string);
  1547. no_longjmp_on_fatal_error = 1;
  1548. i = pass_next = 0;
  1549. while (i <= eindex)
  1550. {
  1551. c = string[i];
  1552. if (pass_next)
  1553. {
  1554. pass_next = 0;
  1555. if (i >= eindex) /* XXX was if (i >= eindex - 1) */
  1556. CQ_RETURN(1);
  1557. ADVANCE_CHAR (string, slen, i);
  1558. continue;
  1559. }
  1560. else if (c == '\\')
  1561. {
  1562. pass_next = 1;
  1563. i++;
  1564. continue;
  1565. }
  1566. else if (c == '\'' || c == '"')
  1567. {
  1568. i = (c == '\'') ? skip_single_quoted (string, slen, ++i)
  1569. : skip_double_quoted (string, slen, ++i);
  1570. if (i > eindex)
  1571. CQ_RETURN(1);
  1572. /* no increment, the skip_xxx functions go one past end */
  1573. }
  1574. else
  1575. ADVANCE_CHAR (string, slen, i);
  1576. }
  1577. CQ_RETURN(0);
  1578. }
  1579. int
  1580. unclosed_pair (string, eindex, openstr)
  1581. char *string;
  1582. int eindex;
  1583. char *openstr;
  1584. {
  1585. int i, pass_next, openc, olen;
  1586. size_t slen;
  1587. DECLARE_MBSTATE;
  1588. slen = strlen (string);
  1589. olen = strlen (openstr);
  1590. i = pass_next = openc = 0;
  1591. while (i <= eindex)
  1592. {
  1593. if (pass_next)
  1594. {
  1595. pass_next = 0;
  1596. if (i >= eindex) /* XXX was if (i >= eindex - 1) */
  1597. return 0;
  1598. ADVANCE_CHAR (string, slen, i);
  1599. continue;
  1600. }
  1601. else if (string[i] == '\\')
  1602. {
  1603. pass_next = 1;
  1604. i++;
  1605. continue;
  1606. }
  1607. else if (STREQN (string + i, openstr, olen))
  1608. {
  1609. openc = 1 - openc;
  1610. i += olen;
  1611. }
  1612. else if (string[i] == '\'' || string[i] == '"')
  1613. {
  1614. i = (string[i] == '\'') ? skip_single_quoted (string, slen, i)
  1615. : skip_double_quoted (string, slen, i);
  1616. if (i > eindex)
  1617. return 0;
  1618. }
  1619. else
  1620. ADVANCE_CHAR (string, slen, i);
  1621. }
  1622. return (openc);
  1623. }
  1624. /* Split STRING (length SLEN) at DELIMS, and return a WORD_LIST with the
  1625. individual words. If DELIMS is NULL, the current value of $IFS is used
  1626. to split the string, and the function follows the shell field splitting
  1627. rules. SENTINEL is an index to look for. NWP, if non-NULL,
  1628. gets the number of words in the returned list. CWP, if non-NULL, gets
  1629. the index of the word containing SENTINEL. Non-whitespace chars in
  1630. DELIMS delimit separate fields. */
  1631. WORD_LIST *
  1632. split_at_delims (string, slen, delims, sentinel, flags, nwp, cwp)
  1633. char *string;
  1634. int slen;
  1635. char *delims;
  1636. int sentinel, flags;
  1637. int *nwp, *cwp;
  1638. {
  1639. int ts, te, i, nw, cw, ifs_split, dflags;
  1640. char *token, *d, *d2;
  1641. WORD_LIST *ret, *tl;
  1642. if (string == 0 || *string == '\0')
  1643. {
  1644. if (nwp)
  1645. *nwp = 0;
  1646. if (cwp)
  1647. *cwp = 0;
  1648. return ((WORD_LIST *)NULL);
  1649. }
  1650. d = (delims == 0) ? ifs_value : delims;
  1651. ifs_split = delims == 0;
  1652. /* Make d2 the non-whitespace characters in delims */
  1653. d2 = 0;
  1654. if (delims)
  1655. {
  1656. size_t slength;
  1657. #if defined (HANDLE_MULTIBYTE)
  1658. size_t mblength = 1;
  1659. #endif
  1660. DECLARE_MBSTATE;
  1661. slength = strlen (delims);
  1662. d2 = (char *)xmalloc (slength + 1);
  1663. i = ts = 0;
  1664. while (delims[i])
  1665. {
  1666. #if defined (HANDLE_MULTIBYTE)
  1667. mbstate_t state_bak;
  1668. state_bak = state;
  1669. mblength = MBRLEN (delims + i, slength, &state);
  1670. if (MB_INVALIDCH (mblength))
  1671. state = state_bak;
  1672. else if (mblength > 1)
  1673. {
  1674. memcpy (d2 + ts, delims + i, mblength);
  1675. ts += mblength;
  1676. i += mblength;
  1677. slength -= mblength;
  1678. continue;
  1679. }
  1680. #endif
  1681. if (whitespace (delims[i]) == 0)
  1682. d2[ts++] = delims[i];
  1683. i++;
  1684. slength--;
  1685. }
  1686. d2[ts] = '\0';
  1687. }
  1688. ret = (WORD_LIST *)NULL;
  1689. /* Remove sequences of whitespace characters at the start of the string, as
  1690. long as those characters are delimiters. */
  1691. for (i = 0; member (string[i], d) && spctabnl (string[i]); i++)
  1692. ;
  1693. if (string[i] == '\0')
  1694. return (ret);
  1695. ts = i;
  1696. nw = 0;
  1697. cw = -1;
  1698. dflags = flags|SD_NOJMP;
  1699. while (1)
  1700. {
  1701. te = skip_to_delim (string, ts, d, dflags);
  1702. /* If we have a non-whitespace delimiter character, use it to make a
  1703. separate field. This is just about what $IFS splitting does and
  1704. is closer to the behavior of the shell parser. */
  1705. if (ts == te && d2 && member (string[ts], d2))
  1706. {
  1707. te = ts + 1;
  1708. /* If we're using IFS splitting, the non-whitespace delimiter char
  1709. and any additional IFS whitespace delimits a field. */
  1710. if (ifs_split)
  1711. while (member (string[te], d) && spctabnl (string[te]))
  1712. te++;
  1713. else
  1714. while (member (string[te], d2))
  1715. te++;
  1716. }
  1717. token = substring (string, ts, te);
  1718. ret = add_string_to_list (token, ret);
  1719. free (token);
  1720. nw++;
  1721. if (sentinel >= ts && sentinel <= te)
  1722. cw = nw;
  1723. /* If the cursor is at whitespace just before word start, set the
  1724. sentinel word to the current word. */
  1725. if (cwp && cw == -1 && sentinel == ts-1)
  1726. cw = nw;
  1727. /* If the cursor is at whitespace between two words, make a new, empty
  1728. word, add it before (well, after, since the list is in reverse order)
  1729. the word we just added, and set the current word to that one. */
  1730. if (cwp && cw == -1 && sentinel < ts)
  1731. {
  1732. tl = make_word_list (make_word (""), ret->next);
  1733. ret->next = tl;
  1734. cw = nw;
  1735. nw++;
  1736. }
  1737. if (string[te] == 0)
  1738. break;
  1739. i = te;
  1740. while (member (string[i], d) && (ifs_split || spctabnl(string[i])))
  1741. i++;
  1742. if (string[i])
  1743. ts = i;
  1744. else
  1745. break;
  1746. }
  1747. /* Special case for SENTINEL at the end of STRING. If we haven't found
  1748. the word containing SENTINEL yet, and the index we're looking for is at
  1749. the end of STRING (or past the end of the previously-found token,
  1750. possible if the end of the line is composed solely of IFS whitespace)
  1751. add an additional null argument and set the current word pointer to that. */
  1752. if (cwp && cw == -1 && (sentinel >= slen || sentinel >= te))
  1753. {
  1754. if (whitespace (string[sentinel - 1]))
  1755. {
  1756. token = "";
  1757. ret = add_string_to_list (token, ret);
  1758. nw++;
  1759. }
  1760. cw = nw;
  1761. }
  1762. if (nwp)
  1763. *nwp = nw;
  1764. if (cwp)
  1765. *cwp = cw;
  1766. return (REVERSE_LIST (ret, WORD_LIST *));
  1767. }
  1768. #endif /* READLINE */
  1769. #if 0
  1770. /* UNUSED */
  1771. /* Extract the name of the variable to bind to from the assignment string. */
  1772. char *
  1773. assignment_name (string)
  1774. char *string;
  1775. {
  1776. int offset;
  1777. char *temp;
  1778. offset = assignment (string, 0);
  1779. if (offset == 0)
  1780. return (char *)NULL;
  1781. temp = substring (string, 0, offset);
  1782. return (temp);
  1783. }
  1784. #endif
  1785. /* **************************************************************** */
  1786. /* */
  1787. /* Functions to convert strings to WORD_LISTs and vice versa */
  1788. /* */
  1789. /* **************************************************************** */
  1790. /* Return a single string of all the words in LIST. SEP is the separator
  1791. to put between individual elements of LIST in the output string. */
  1792. char *
  1793. string_list_internal (list, sep)
  1794. WORD_LIST *list;
  1795. char *sep;
  1796. {
  1797. register WORD_LIST *t;
  1798. char *result, *r;
  1799. int word_len, sep_len, result_size;
  1800. if (list == 0)
  1801. return ((char *)NULL);
  1802. /* Short-circuit quickly if we don't need to separate anything. */
  1803. if (list->next == 0)
  1804. return (savestring (list->word->word));
  1805. /* This is nearly always called with either sep[0] == 0 or sep[1] == 0. */
  1806. sep_len = STRLEN (sep);
  1807. result_size = 0;
  1808. for (t = list; t; t = t->next)
  1809. {
  1810. if (t != list)
  1811. result_size += sep_len;
  1812. result_size += strlen (t->word->word);
  1813. }
  1814. r = result = (char *)xmalloc (result_size + 1);
  1815. for (t = list; t; t = t->next)
  1816. {
  1817. if (t != list && sep_len)
  1818. {
  1819. if (sep_len > 1)
  1820. {
  1821. FASTCOPY (sep, r, sep_len);
  1822. r += sep_len;
  1823. }
  1824. else
  1825. *r++ = sep[0];
  1826. }
  1827. word_len = strlen (t->word->word);
  1828. FASTCOPY (t->word->word, r, word_len);
  1829. r += word_len;
  1830. }
  1831. *r = '\0';
  1832. return (result);
  1833. }
  1834. /* Return a single string of all the words present in LIST, separating
  1835. each word with a space. */
  1836. char *
  1837. string_list (list)
  1838. WORD_LIST *list;
  1839. {
  1840. return (string_list_internal (list, " "));
  1841. }
  1842. /* An external interface that can be used by the rest of the shell to
  1843. obtain a string containing the first character in $IFS. Handles all
  1844. the multibyte complications. If LENP is non-null, it is set to the
  1845. length of the returned string. */
  1846. char *
  1847. ifs_firstchar (lenp)
  1848. int *lenp;
  1849. {
  1850. char *ret;
  1851. int len;
  1852. ret = xmalloc (MB_LEN_MAX + 1);
  1853. #if defined (HANDLE_MULTIBYTE)
  1854. if (ifs_firstc_len == 1)
  1855. {
  1856. ret[0] = ifs_firstc[0];
  1857. ret[1] = '\0';
  1858. len = ret[0] ? 1 : 0;
  1859. }
  1860. else
  1861. {
  1862. memcpy (ret, ifs_firstc, ifs_firstc_len);
  1863. ret[len = ifs_firstc_len] = '\0';
  1864. }
  1865. #else
  1866. ret[0] = ifs_firstc;
  1867. ret[1] = '\0';
  1868. len = ret[0] ? 0 : 1;
  1869. #endif
  1870. if (lenp)
  1871. *lenp = len;
  1872. return ret;
  1873. }
  1874. /* Return a single string of all the words present in LIST, obeying the
  1875. quoting rules for "$*", to wit: (P1003.2, draft 11, 3.5.2) "If the
  1876. expansion [of $*] appears within a double quoted string, it expands
  1877. to a single field with the value of each parameter separated by the
  1878. first character of the IFS variable, or by a <space> if IFS is unset." */
  1879. char *
  1880. string_list_dollar_star (list)
  1881. WORD_LIST *list;
  1882. {
  1883. char *ret;
  1884. #if defined (HANDLE_MULTIBYTE)
  1885. # if defined (__GNUC__)
  1886. char sep[MB_CUR_MAX + 1];
  1887. # else
  1888. char *sep = 0;
  1889. # endif
  1890. #else
  1891. char sep[2];
  1892. #endif
  1893. #if defined (HANDLE_MULTIBYTE)
  1894. # if !defined (__GNUC__)
  1895. sep = (char *)xmalloc (MB_CUR_MAX + 1);
  1896. # endif /* !__GNUC__ */
  1897. if (ifs_firstc_len == 1)
  1898. {
  1899. sep[0] = ifs_firstc[0];
  1900. sep[1] = '\0';
  1901. }
  1902. else
  1903. {
  1904. memcpy (sep, ifs_firstc, ifs_firstc_len);
  1905. sep[ifs_firstc_len] = '\0';
  1906. }
  1907. #else
  1908. sep[0] = ifs_firstc;
  1909. sep[1] = '\0';
  1910. #endif
  1911. ret = string_list_internal (list, sep);
  1912. #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
  1913. free (sep);
  1914. #endif
  1915. return ret;
  1916. }
  1917. /* Turn $@ into a string. If (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  1918. is non-zero, the $@ appears within double quotes, and we should quote
  1919. the list before converting it into a string. If IFS is unset, and the
  1920. word is not quoted, we just need to quote CTLESC and CTLNUL characters
  1921. in the words in the list, because the default value of $IFS is
  1922. <space><tab><newline>, IFS characters in the words in the list should
  1923. also be split. If IFS is null, and the word is not quoted, we need
  1924. to quote the words in the list to preserve the positional parameters
  1925. exactly. */
  1926. char *
  1927. string_list_dollar_at (list, quoted)
  1928. WORD_LIST *list;
  1929. int quoted;
  1930. {
  1931. char *ifs, *ret;
  1932. #if defined (HANDLE_MULTIBYTE)
  1933. # if defined (__GNUC__)
  1934. char sep[MB_CUR_MAX + 1];
  1935. # else
  1936. char *sep = 0;
  1937. # endif /* !__GNUC__ */
  1938. #else
  1939. char sep[2];
  1940. #endif
  1941. WORD_LIST *tlist;
  1942. /* XXX this could just be ifs = ifs_value; */
  1943. ifs = ifs_var ? value_cell (ifs_var) : (char *)0;
  1944. #if defined (HANDLE_MULTIBYTE)
  1945. # if !defined (__GNUC__)
  1946. sep = (char *)xmalloc (MB_CUR_MAX + 1);
  1947. # endif /* !__GNUC__ */
  1948. if (ifs && *ifs)
  1949. {
  1950. if (ifs_firstc_len == 1)
  1951. {
  1952. sep[0] = ifs_firstc[0];
  1953. sep[1] = '\0';
  1954. }
  1955. else
  1956. {
  1957. memcpy (sep, ifs_firstc, ifs_firstc_len);
  1958. sep[ifs_firstc_len] = '\0';
  1959. }
  1960. }
  1961. else
  1962. {
  1963. sep[0] = ' ';
  1964. sep[1] = '\0';
  1965. }
  1966. #else
  1967. sep[0] = (ifs == 0 || *ifs == 0) ? ' ' : *ifs;
  1968. sep[1] = '\0';
  1969. #endif
  1970. /* XXX -- why call quote_list if ifs == 0? we can get away without doing
  1971. it now that quote_escapes quotes spaces */
  1972. #if 0
  1973. tlist = ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (ifs && *ifs == 0))
  1974. #else
  1975. tlist = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
  1976. #endif
  1977. ? quote_list (list)
  1978. : list_quote_escapes (list);
  1979. ret = string_list_internal (tlist, sep);
  1980. #if defined (HANDLE_MULTIBYTE) && !defined (__GNUC__)
  1981. free (sep);
  1982. #endif
  1983. return ret;
  1984. }
  1985. /* Turn the positional paramters into a string, understanding quoting and
  1986. the various subtleties of using the first character of $IFS as the
  1987. separator. Calls string_list_dollar_at, string_list_dollar_star, and
  1988. string_list as appropriate. */
  1989. char *
  1990. string_list_pos_params (pchar, list, quoted)
  1991. int pchar;
  1992. WORD_LIST *list;
  1993. int quoted;
  1994. {
  1995. char *ret;
  1996. WORD_LIST *tlist;
  1997. if (pchar == '*' && (quoted & Q_DOUBLE_QUOTES))
  1998. {
  1999. tlist = quote_list (list);
  2000. word_list_remove_quoted_nulls (tlist);
  2001. ret = string_list_dollar_star (tlist);
  2002. }
  2003. else if (pchar == '*' && (quoted & Q_HERE_DOCUMENT))
  2004. {
  2005. tlist = quote_list (list);
  2006. word_list_remove_quoted_nulls (tlist);
  2007. ret = string_list (tlist);
  2008. }
  2009. else if (pchar == '*')
  2010. {
  2011. /* Even when unquoted, string_list_dollar_star does the right thing
  2012. making sure that the first character of $IFS is used as the
  2013. separator. */
  2014. ret = string_list_dollar_star (list);
  2015. }
  2016. else if (pchar == '@' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  2017. /* We use string_list_dollar_at, but only if the string is quoted, since
  2018. that quotes the escapes if it's not, which we don't want. We could
  2019. use string_list (the old code did), but that doesn't do the right
  2020. thing if the first character of $IFS is not a space. We use
  2021. string_list_dollar_star if the string is unquoted so we make sure that
  2022. the elements of $@ are separated by the first character of $IFS for
  2023. later splitting. */
  2024. ret = string_list_dollar_at (list, quoted);
  2025. else if (pchar == '@')
  2026. ret = string_list_dollar_star (list);
  2027. else
  2028. ret = string_list ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? quote_list (list) : list);
  2029. return ret;
  2030. }
  2031. /* Return the list of words present in STRING. Separate the string into
  2032. words at any of the characters found in SEPARATORS. If QUOTED is
  2033. non-zero then word in the list will have its quoted flag set, otherwise
  2034. the quoted flag is left as make_word () deemed fit.
  2035. This obeys the P1003.2 word splitting semantics. If `separators' is
  2036. exactly <space><tab><newline>, then the splitting algorithm is that of
  2037. the Bourne shell, which treats any sequence of characters from `separators'
  2038. as a delimiter. If IFS is unset, which results in `separators' being set
  2039. to "", no splitting occurs. If separators has some other value, the
  2040. following rules are applied (`IFS white space' means zero or more
  2041. occurrences of <space>, <tab>, or <newline>, as long as those characters
  2042. are in `separators'):
  2043. 1) IFS white space is ignored at the start and the end of the
  2044. string.
  2045. 2) Each occurrence of a character in `separators' that is not
  2046. IFS white space, along with any adjacent occurrences of
  2047. IFS white space delimits a field.
  2048. 3) Any nonzero-length sequence of IFS white space delimits a field.
  2049. */
  2050. /* BEWARE! list_string strips null arguments. Don't call it twice and
  2051. expect to have "" preserved! */
  2052. /* This performs word splitting and quoted null character removal on
  2053. STRING. */
  2054. #define issep(c) \
  2055. (((separators)[0]) ? ((separators)[1] ? isifs(c) \
  2056. : (c) == (separators)[0]) \
  2057. : 0)
  2058. WORD_LIST *
  2059. list_string (string, separators, quoted)
  2060. register char *string, *separators;
  2061. int quoted;
  2062. {
  2063. WORD_LIST *result;
  2064. WORD_DESC *t;
  2065. char *current_word, *s;
  2066. int sindex, sh_style_split, whitesep, xflags;
  2067. size_t slen;
  2068. if (!string || !*string)
  2069. return ((WORD_LIST *)NULL);
  2070. sh_style_split = separators && separators[0] == ' ' &&
  2071. separators[1] == '\t' &&
  2072. separators[2] == '\n' &&
  2073. separators[3] == '\0';
  2074. for (xflags = 0, s = ifs_value; s && *s; s++)
  2075. {
  2076. if (*s == CTLESC) xflags |= SX_NOCTLESC;
  2077. else if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
  2078. }
  2079. slen = 0;
  2080. /* Remove sequences of whitespace at the beginning of STRING, as
  2081. long as those characters appear in IFS. Do not do this if
  2082. STRING is quoted or if there are no separator characters. */
  2083. if (!quoted || !separators || !*separators)
  2084. {
  2085. for (s = string; *s && spctabnl (*s) && issep (*s); s++);
  2086. if (!*s)
  2087. return ((WORD_LIST *)NULL);
  2088. string = s;
  2089. }
  2090. /* OK, now STRING points to a word that does not begin with white space.
  2091. The splitting algorithm is:
  2092. extract a word, stopping at a separator
  2093. skip sequences of spc, tab, or nl as long as they are separators
  2094. This obeys the field splitting rules in Posix.2. */
  2095. slen = (MB_CUR_MAX > 1) ? strlen (string) : 1;
  2096. for (result = (WORD_LIST *)NULL, sindex = 0; string[sindex]; )
  2097. {
  2098. /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
  2099. unless multibyte chars are possible. */
  2100. current_word = string_extract_verbatim (string, slen, &sindex, separators, xflags);
  2101. if (current_word == 0)
  2102. break;
  2103. /* If we have a quoted empty string, add a quoted null argument. We
  2104. want to preserve the quoted null character iff this is a quoted
  2105. empty string; otherwise the quoted null characters are removed
  2106. below. */
  2107. if (QUOTED_NULL (current_word))
  2108. {
  2109. t = alloc_word_desc ();
  2110. t->word = make_quoted_char ('\0');
  2111. t->flags |= W_QUOTED|W_HASQUOTEDNULL;
  2112. result = make_word_list (t, result);
  2113. }
  2114. else if (current_word[0] != '\0')
  2115. {
  2116. /* If we have something, then add it regardless. However,
  2117. perform quoted null character removal on the current word. */
  2118. remove_quoted_nulls (current_word);
  2119. result = add_string_to_list (current_word, result);
  2120. result->word->flags &= ~W_HASQUOTEDNULL; /* just to be sure */
  2121. if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
  2122. result->word->flags |= W_QUOTED;
  2123. }
  2124. /* If we're not doing sequences of separators in the traditional
  2125. Bourne shell style, then add a quoted null argument. */
  2126. else if (!sh_style_split && !spctabnl (string[sindex]))
  2127. {
  2128. t = alloc_word_desc ();
  2129. t->word = make_quoted_char ('\0');
  2130. t->flags |= W_QUOTED|W_HASQUOTEDNULL;
  2131. result = make_word_list (t, result);
  2132. }
  2133. free (current_word);
  2134. /* Note whether or not the separator is IFS whitespace, used later. */
  2135. whitesep = string[sindex] && spctabnl (string[sindex]);
  2136. /* Move past the current separator character. */
  2137. if (string[sindex])
  2138. {
  2139. DECLARE_MBSTATE;
  2140. ADVANCE_CHAR (string, slen, sindex);
  2141. }
  2142. /* Now skip sequences of space, tab, or newline characters if they are
  2143. in the list of separators. */
  2144. while (string[sindex] && spctabnl (string[sindex]) && issep (string[sindex]))
  2145. sindex++;
  2146. /* If the first separator was IFS whitespace and the current character
  2147. is a non-whitespace IFS character, it should be part of the current
  2148. field delimiter, not a separate delimiter that would result in an
  2149. empty field. Look at POSIX.2, 3.6.5, (3)(b). */
  2150. if (string[sindex] && whitesep && issep (string[sindex]) && !spctabnl (string[sindex]))
  2151. {
  2152. sindex++;
  2153. /* An IFS character that is not IFS white space, along with any
  2154. adjacent IFS white space, shall delimit a field. (SUSv3) */
  2155. while (string[sindex] && spctabnl (string[sindex]) && isifs (string[sindex]))
  2156. sindex++;
  2157. }
  2158. }
  2159. return (REVERSE_LIST (result, WORD_LIST *));
  2160. }
  2161. /* Parse a single word from STRING, using SEPARATORS to separate fields.
  2162. ENDPTR is set to the first character after the word. This is used by
  2163. the `read' builtin. This is never called with SEPARATORS != $IFS;
  2164. it should be simplified.
  2165. XXX - this function is very similar to list_string; they should be
  2166. combined - XXX */
  2167. char *
  2168. get_word_from_string (stringp, separators, endptr)
  2169. char **stringp, *separators, **endptr;
  2170. {
  2171. register char *s;
  2172. char *current_word;
  2173. int sindex, sh_style_split, whitesep, xflags;
  2174. size_t slen;
  2175. if (!stringp || !*stringp || !**stringp)
  2176. return ((char *)NULL);
  2177. sh_style_split = separators && separators[0] == ' ' &&
  2178. separators[1] == '\t' &&
  2179. separators[2] == '\n' &&
  2180. separators[3] == '\0';
  2181. for (xflags = 0, s = ifs_value; s && *s; s++)
  2182. {
  2183. if (*s == CTLESC) xflags |= SX_NOCTLESC;
  2184. if (*s == CTLNUL) xflags |= SX_NOESCCTLNUL;
  2185. }
  2186. s = *stringp;
  2187. slen = 0;
  2188. /* Remove sequences of whitespace at the beginning of STRING, as
  2189. long as those characters appear in IFS. */
  2190. if (sh_style_split || !separators || !*separators)
  2191. {
  2192. for (; *s && spctabnl (*s) && isifs (*s); s++);
  2193. /* If the string is nothing but whitespace, update it and return. */
  2194. if (!*s)
  2195. {
  2196. *stringp = s;
  2197. if (endptr)
  2198. *endptr = s;
  2199. return ((char *)NULL);
  2200. }
  2201. }
  2202. /* OK, S points to a word that does not begin with white space.
  2203. Now extract a word, stopping at a separator, save a pointer to
  2204. the first character after the word, then skip sequences of spc,
  2205. tab, or nl as long as they are separators.
  2206. This obeys the field splitting rules in Posix.2. */
  2207. sindex = 0;
  2208. /* Don't need string length in ADVANCE_CHAR or string_extract_verbatim
  2209. unless multibyte chars are possible. */
  2210. slen = (MB_CUR_MAX > 1) ? strlen (s) : 1;
  2211. current_word = string_extract_verbatim (s, slen, &sindex, separators, xflags);
  2212. /* Set ENDPTR to the first character after the end of the word. */
  2213. if (endptr)
  2214. *endptr = s + sindex;
  2215. /* Note whether or not the separator is IFS whitespace, used later. */
  2216. whitesep = s[sindex] && spctabnl (s[sindex]);
  2217. /* Move past the current separator character. */
  2218. if (s[sindex])
  2219. {
  2220. DECLARE_MBSTATE;
  2221. ADVANCE_CHAR (s, slen, sindex);
  2222. }
  2223. /* Now skip sequences of space, tab, or newline characters if they are
  2224. in the list of separators. */
  2225. while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
  2226. sindex++;
  2227. /* If the first separator was IFS whitespace and the current character is
  2228. a non-whitespace IFS character, it should be part of the current field
  2229. delimiter, not a separate delimiter that would result in an empty field.
  2230. Look at POSIX.2, 3.6.5, (3)(b). */
  2231. if (s[sindex] && whitesep && isifs (s[sindex]) && !spctabnl (s[sindex]))
  2232. {
  2233. sindex++;
  2234. /* An IFS character that is not IFS white space, along with any adjacent
  2235. IFS white space, shall delimit a field. */
  2236. while (s[sindex] && spctabnl (s[sindex]) && isifs (s[sindex]))
  2237. sindex++;
  2238. }
  2239. /* Update STRING to point to the next field. */
  2240. *stringp = s + sindex;
  2241. return (current_word);
  2242. }
  2243. /* Remove IFS white space at the end of STRING. Start at the end
  2244. of the string and walk backwards until the beginning of the string
  2245. or we find a character that's not IFS white space and not CTLESC.
  2246. Only let CTLESC escape a white space character if SAW_ESCAPE is
  2247. non-zero. */
  2248. char *
  2249. strip_trailing_ifs_whitespace (string, separators, saw_escape)
  2250. char *string, *separators;
  2251. int saw_escape;
  2252. {
  2253. char *s;
  2254. s = string + STRLEN (string) - 1;
  2255. while (s > string && ((spctabnl (*s) && isifs (*s)) ||
  2256. (saw_escape && *s == CTLESC && spctabnl (s[1]))))
  2257. s--;
  2258. *++s = '\0';
  2259. return string;
  2260. }
  2261. #if 0
  2262. /* UNUSED */
  2263. /* Split STRING into words at whitespace. Obeys shell-style quoting with
  2264. backslashes, single and double quotes. */
  2265. WORD_LIST *
  2266. list_string_with_quotes (string)
  2267. char *string;
  2268. {
  2269. WORD_LIST *list;
  2270. char *token, *s;
  2271. size_t s_len;
  2272. int c, i, tokstart, len;
  2273. for (s = string; s && *s && spctabnl (*s); s++)
  2274. ;
  2275. if (s == 0 || *s == 0)
  2276. return ((WORD_LIST *)NULL);
  2277. s_len = strlen (s);
  2278. tokstart = i = 0;
  2279. list = (WORD_LIST *)NULL;
  2280. while (1)
  2281. {
  2282. c = s[i];
  2283. if (c == '\\')
  2284. {
  2285. i++;
  2286. if (s[i])
  2287. i++;
  2288. }
  2289. else if (c == '\'')
  2290. i = skip_single_quoted (s, s_len, ++i);
  2291. else if (c == '"')
  2292. i = skip_double_quoted (s, s_len, ++i);
  2293. else if (c == 0 || spctabnl (c))
  2294. {
  2295. /* We have found the end of a token. Make a word out of it and
  2296. add it to the word list. */
  2297. token = substring (s, tokstart, i);
  2298. list = add_string_to_list (token, list);
  2299. free (token);
  2300. while (spctabnl (s[i]))
  2301. i++;
  2302. if (s[i])
  2303. tokstart = i;
  2304. else
  2305. break;
  2306. }
  2307. else
  2308. i++; /* normal character */
  2309. }
  2310. return (REVERSE_LIST (list, WORD_LIST *));
  2311. }
  2312. #endif
  2313. /********************************************************/
  2314. /* */
  2315. /* Functions to perform assignment statements */
  2316. /* */
  2317. /********************************************************/
  2318. #if defined (ARRAY_VARS)
  2319. static SHELL_VAR *
  2320. do_compound_assignment (name, value, flags)
  2321. char *name, *value;
  2322. int flags;
  2323. {
  2324. SHELL_VAR *v;
  2325. int mklocal, mkassoc;
  2326. WORD_LIST *list;
  2327. mklocal = flags & ASS_MKLOCAL;
  2328. mkassoc = flags & ASS_MKASSOC;
  2329. if (mklocal && variable_context)
  2330. {
  2331. v = find_variable (name);
  2332. list = expand_compound_array_assignment (v, value, flags);
  2333. if (mkassoc)
  2334. v = make_local_assoc_variable (name);
  2335. else if (v == 0 || (array_p (v) == 0 && assoc_p (v) == 0) || v->context != variable_context)
  2336. v = make_local_array_variable (name);
  2337. assign_compound_array_list (v, list, flags);
  2338. }
  2339. else
  2340. v = assign_array_from_string (name, value, flags);
  2341. return (v);
  2342. }
  2343. #endif
  2344. /* Given STRING, an assignment string, get the value of the right side
  2345. of the `=', and bind it to the left side. If EXPAND is true, then
  2346. perform parameter expansion, command substitution, and arithmetic
  2347. expansion on the right-hand side. Perform tilde expansion in any
  2348. case. Do not perform word splitting on the result of expansion. */
  2349. static int
  2350. do_assignment_internal (word, expand)
  2351. const WORD_DESC *word;
  2352. int expand;
  2353. {
  2354. int offset, tlen, appendop, assign_list, aflags, retval;
  2355. char *name, *value;
  2356. SHELL_VAR *entry;
  2357. #if defined (ARRAY_VARS)
  2358. char *t;
  2359. int ni;
  2360. #endif
  2361. const char *string;
  2362. if (word == 0 || word->word == 0)
  2363. return 0;
  2364. appendop = assign_list = aflags = 0;
  2365. string = word->word;
  2366. offset = assignment (string, 0);
  2367. name = savestring (string);
  2368. value = (char *)NULL;
  2369. if (name[offset] == '=')
  2370. {
  2371. char *temp;
  2372. if (name[offset - 1] == '+')
  2373. {
  2374. appendop = 1;
  2375. name[offset - 1] = '\0';
  2376. }
  2377. name[offset] = 0; /* might need this set later */
  2378. temp = name + offset + 1;
  2379. tlen = STRLEN (temp);
  2380. #if defined (ARRAY_VARS)
  2381. if (expand && (word->flags & W_COMPASSIGN))
  2382. {
  2383. assign_list = ni = 1;
  2384. value = extract_array_assignment_list (temp, &ni);
  2385. }
  2386. else
  2387. #endif
  2388. if (expand && temp[0])
  2389. value = expand_string_if_necessary (temp, 0, expand_string_assignment);
  2390. else
  2391. value = savestring (temp);
  2392. }
  2393. if (value == 0)
  2394. {
  2395. value = (char *)xmalloc (1);
  2396. value[0] = '\0';
  2397. }
  2398. if (echo_command_at_execute)
  2399. {
  2400. if (appendop)
  2401. name[offset - 1] = '+';
  2402. xtrace_print_assignment (name, value, assign_list, 1);
  2403. if (appendop)
  2404. name[offset - 1] = '\0';
  2405. }
  2406. #define ASSIGN_RETURN(r) do { FREE (value); free (name); return (r); } while (0)
  2407. if (appendop)
  2408. aflags |= ASS_APPEND;
  2409. #if defined (ARRAY_VARS)
  2410. if (t = mbschr (name, '[')) /*]*/
  2411. {
  2412. if (assign_list)
  2413. {
  2414. report_error (_("%s: cannot assign list to array member"), name);
  2415. ASSIGN_RETURN (0);
  2416. }
  2417. entry = assign_array_element (name, value, aflags);
  2418. if (entry == 0)
  2419. ASSIGN_RETURN (0);
  2420. }
  2421. else if (assign_list)
  2422. {
  2423. if (word->flags & W_ASSIGNARG)
  2424. aflags |= ASS_MKLOCAL;
  2425. if (word->flags & W_ASSIGNASSOC)
  2426. aflags |= ASS_MKASSOC;
  2427. entry = do_compound_assignment (name, value, aflags);
  2428. }
  2429. else
  2430. #endif /* ARRAY_VARS */
  2431. entry = bind_variable (name, value, aflags);
  2432. stupidly_hack_special_variables (name);
  2433. #if 1
  2434. /* Return 1 if the assignment seems to have been performed correctly. */
  2435. if (entry == 0 || readonly_p (entry))
  2436. retval = 0; /* assignment failure */
  2437. else if (noassign_p (entry))
  2438. {
  2439. last_command_exit_value = EXECUTION_FAILURE;
  2440. retval = 1; /* error status, but not assignment failure */
  2441. }
  2442. else
  2443. retval = 1;
  2444. if (entry && retval != 0 && noassign_p (entry) == 0)
  2445. VUNSETATTR (entry, att_invisible);
  2446. ASSIGN_RETURN (retval);
  2447. #else
  2448. if (entry)
  2449. VUNSETATTR (entry, att_invisible);
  2450. ASSIGN_RETURN (entry ? ((readonly_p (entry) == 0) && noassign_p (entry) == 0) : 0);
  2451. #endif
  2452. }
  2453. /* Perform the assignment statement in STRING, and expand the
  2454. right side by doing tilde, command and parameter expansion. */
  2455. int
  2456. do_assignment (string)
  2457. char *string;
  2458. {
  2459. WORD_DESC td;
  2460. td.flags = W_ASSIGNMENT;
  2461. td.word = string;
  2462. return do_assignment_internal (&td, 1);
  2463. }
  2464. int
  2465. do_word_assignment (word)
  2466. WORD_DESC *word;
  2467. {
  2468. return do_assignment_internal (word, 1);
  2469. }
  2470. /* Given STRING, an assignment string, get the value of the right side
  2471. of the `=', and bind it to the left side. Do not perform any word
  2472. expansions on the right hand side. */
  2473. int
  2474. do_assignment_no_expand (string)
  2475. char *string;
  2476. {
  2477. WORD_DESC td;
  2478. td.flags = W_ASSIGNMENT;
  2479. td.word = string;
  2480. return (do_assignment_internal (&td, 0));
  2481. }
  2482. /***************************************************
  2483. * *
  2484. * Functions to manage the positional parameters *
  2485. * *
  2486. ***************************************************/
  2487. /* Return the word list that corresponds to `$*'. */
  2488. WORD_LIST *
  2489. list_rest_of_args ()
  2490. {
  2491. register WORD_LIST *list, *args;
  2492. int i;
  2493. /* Break out of the loop as soon as one of the dollar variables is null. */
  2494. for (i = 1, list = (WORD_LIST *)NULL; i < 10 && dollar_vars[i]; i++)
  2495. list = make_word_list (make_bare_word (dollar_vars[i]), list);
  2496. for (args = rest_of_args; args; args = args->next)
  2497. list = make_word_list (make_bare_word (args->word->word), list);
  2498. return (REVERSE_LIST (list, WORD_LIST *));
  2499. }
  2500. int
  2501. number_of_args ()
  2502. {
  2503. register WORD_LIST *list;
  2504. int n;
  2505. for (n = 0; n < 9 && dollar_vars[n+1]; n++)
  2506. ;
  2507. for (list = rest_of_args; list; list = list->next)
  2508. n++;
  2509. return n;
  2510. }
  2511. /* Return the value of a positional parameter. This handles values > 10. */
  2512. char *
  2513. get_dollar_var_value (ind)
  2514. intmax_t ind;
  2515. {
  2516. char *temp;
  2517. WORD_LIST *p;
  2518. if (ind < 10)
  2519. temp = dollar_vars[ind] ? savestring (dollar_vars[ind]) : (char *)NULL;
  2520. else /* We want something like ${11} */
  2521. {
  2522. ind -= 10;
  2523. for (p = rest_of_args; p && ind--; p = p->next)
  2524. ;
  2525. temp = p ? savestring (p->word->word) : (char *)NULL;
  2526. }
  2527. return (temp);
  2528. }
  2529. /* Make a single large string out of the dollar digit variables,
  2530. and the rest_of_args. If DOLLAR_STAR is 1, then obey the special
  2531. case of "$*" with respect to IFS. */
  2532. char *
  2533. string_rest_of_args (dollar_star)
  2534. int dollar_star;
  2535. {
  2536. register WORD_LIST *list;
  2537. char *string;
  2538. list = list_rest_of_args ();
  2539. string = dollar_star ? string_list_dollar_star (list) : string_list (list);
  2540. dispose_words (list);
  2541. return (string);
  2542. }
  2543. /* Return a string containing the positional parameters from START to
  2544. END, inclusive. If STRING[0] == '*', we obey the rules for $*,
  2545. which only makes a difference if QUOTED is non-zero. If QUOTED includes
  2546. Q_HERE_DOCUMENT or Q_DOUBLE_QUOTES, this returns a quoted list, otherwise
  2547. no quoting chars are added. */
  2548. static char *
  2549. pos_params (string, start, end, quoted)
  2550. char *string;
  2551. int start, end, quoted;
  2552. {
  2553. WORD_LIST *save, *params, *h, *t;
  2554. char *ret;
  2555. int i;
  2556. /* see if we can short-circuit. if start == end, we want 0 parameters. */
  2557. if (start == end)
  2558. return ((char *)NULL);
  2559. save = params = list_rest_of_args ();
  2560. if (save == 0)
  2561. return ((char *)NULL);
  2562. if (start == 0) /* handle ${@:0[:x]} specially */
  2563. {
  2564. t = make_word_list (make_word (dollar_vars[0]), params);
  2565. save = params = t;
  2566. }
  2567. for (i = start ? 1 : 0; params && i < start; i++)
  2568. params = params->next;
  2569. if (params == 0)
  2570. return ((char *)NULL);
  2571. for (h = t = params; params && i < end; i++)
  2572. {
  2573. t = params;
  2574. params = params->next;
  2575. }
  2576. t->next = (WORD_LIST *)NULL;
  2577. ret = string_list_pos_params (string[0], h, quoted);
  2578. if (t != params)
  2579. t->next = params;
  2580. dispose_words (save);
  2581. return (ret);
  2582. }
  2583. /******************************************************************/
  2584. /* */
  2585. /* Functions to expand strings to strings or WORD_LISTs */
  2586. /* */
  2587. /******************************************************************/
  2588. #if defined (PROCESS_SUBSTITUTION)
  2589. #define EXP_CHAR(s) (s == '$' || s == '`' || s == '<' || s == '>' || s == CTLESC || s == '~')
  2590. #else
  2591. #define EXP_CHAR(s) (s == '$' || s == '`' || s == CTLESC || s == '~')
  2592. #endif
  2593. /* If there are any characters in STRING that require full expansion,
  2594. then call FUNC to expand STRING; otherwise just perform quote
  2595. removal if necessary. This returns a new string. */
  2596. static char *
  2597. expand_string_if_necessary (string, quoted, func)
  2598. char *string;
  2599. int quoted;
  2600. EXPFUNC *func;
  2601. {
  2602. WORD_LIST *list;
  2603. size_t slen;
  2604. int i, saw_quote;
  2605. char *ret;
  2606. DECLARE_MBSTATE;
  2607. /* Don't need string length for ADVANCE_CHAR unless multibyte chars possible. */
  2608. slen = (MB_CUR_MAX > 1) ? strlen (string) : 0;
  2609. i = saw_quote = 0;
  2610. while (string[i])
  2611. {
  2612. if (EXP_CHAR (string[i]))
  2613. break;
  2614. else if (string[i] == '\'' || string[i] == '\\' || string[i] == '"')
  2615. saw_quote = 1;
  2616. ADVANCE_CHAR (string, slen, i);
  2617. }
  2618. if (string[i])
  2619. {
  2620. list = (*func) (string, quoted);
  2621. if (list)
  2622. {
  2623. ret = string_list (list);
  2624. dispose_words (list);
  2625. }
  2626. else
  2627. ret = (char *)NULL;
  2628. }
  2629. else if (saw_quote && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
  2630. ret = string_quote_removal (string, quoted);
  2631. else
  2632. ret = savestring (string);
  2633. return ret;
  2634. }
  2635. static inline char *
  2636. expand_string_to_string_internal (string, quoted, func)
  2637. char *string;
  2638. int quoted;
  2639. EXPFUNC *func;
  2640. {
  2641. WORD_LIST *list;
  2642. char *ret;
  2643. if (string == 0 || *string == '\0')
  2644. return ((char *)NULL);
  2645. list = (*func) (string, quoted);
  2646. if (list)
  2647. {
  2648. ret = string_list (list);
  2649. dispose_words (list);
  2650. }
  2651. else
  2652. ret = (char *)NULL;
  2653. return (ret);
  2654. }
  2655. char *
  2656. expand_string_to_string (string, quoted)
  2657. char *string;
  2658. int quoted;
  2659. {
  2660. return (expand_string_to_string_internal (string, quoted, expand_string));
  2661. }
  2662. char *
  2663. expand_string_unsplit_to_string (string, quoted)
  2664. char *string;
  2665. int quoted;
  2666. {
  2667. return (expand_string_to_string_internal (string, quoted, expand_string_unsplit));
  2668. }
  2669. char *
  2670. expand_assignment_string_to_string (string, quoted)
  2671. char *string;
  2672. int quoted;
  2673. {
  2674. return (expand_string_to_string_internal (string, quoted, expand_string_assignment));
  2675. }
  2676. char *
  2677. expand_arith_string (string, quoted)
  2678. char *string;
  2679. int quoted;
  2680. {
  2681. return (expand_string_if_necessary (string, quoted, expand_string));
  2682. }
  2683. #if defined (COND_COMMAND)
  2684. /* Just remove backslashes in STRING. Returns a new string. */
  2685. char *
  2686. remove_backslashes (string)
  2687. char *string;
  2688. {
  2689. char *r, *ret, *s;
  2690. r = ret = (char *)xmalloc (strlen (string) + 1);
  2691. for (s = string; s && *s; )
  2692. {
  2693. if (*s == '\\')
  2694. s++;
  2695. if (*s == 0)
  2696. break;
  2697. *r++ = *s++;
  2698. }
  2699. *r = '\0';
  2700. return ret;
  2701. }
  2702. /* This needs better error handling. */
  2703. /* Expand W for use as an argument to a unary or binary operator in a
  2704. [[...]] expression. If SPECIAL is 1, this is the rhs argument
  2705. to the != or == operator, and should be treated as a pattern. In
  2706. this case, we quote the string specially for the globbing code. If
  2707. SPECIAL is 2, this is an rhs argument for the =~ operator, and should
  2708. be quoted appropriately for regcomp/regexec. The caller is responsible
  2709. for removing the backslashes if the unquoted word is needed later. */
  2710. char *
  2711. cond_expand_word (w, special)
  2712. WORD_DESC *w;
  2713. int special;
  2714. {
  2715. char *r, *p;
  2716. WORD_LIST *l;
  2717. int qflags;
  2718. if (w->word == 0 || w->word[0] == '\0')
  2719. return ((char *)NULL);
  2720. w->flags |= W_NOSPLIT2;
  2721. l = call_expand_word_internal (w, 0, 0, (int *)0, (int *)0);
  2722. if (l)
  2723. {
  2724. if (special == 0)
  2725. {
  2726. dequote_list (l);
  2727. r = string_list (l);
  2728. }
  2729. else
  2730. {
  2731. qflags = QGLOB_CVTNULL;
  2732. if (special == 2)
  2733. qflags |= QGLOB_REGEXP;
  2734. p = string_list (l);
  2735. r = quote_string_for_globbing (p, qflags);
  2736. free (p);
  2737. }
  2738. dispose_words (l);
  2739. }
  2740. else
  2741. r = (char *)NULL;
  2742. return r;
  2743. }
  2744. #endif
  2745. /* Call expand_word_internal to expand W and handle error returns.
  2746. A convenience function for functions that don't want to handle
  2747. any errors or free any memory before aborting. */
  2748. static WORD_LIST *
  2749. call_expand_word_internal (w, q, i, c, e)
  2750. WORD_DESC *w;
  2751. int q, i, *c, *e;
  2752. {
  2753. WORD_LIST *result;
  2754. result = expand_word_internal (w, q, i, c, e);
  2755. if (result == &expand_word_error || result == &expand_word_fatal)
  2756. {
  2757. /* By convention, each time this error is returned, w->word has
  2758. already been freed (it sometimes may not be in the fatal case,
  2759. but that doesn't result in a memory leak because we're going
  2760. to exit in most cases). */
  2761. w->word = (char *)NULL;
  2762. last_command_exit_value = EXECUTION_FAILURE;
  2763. exp_jump_to_top_level ((result == &expand_word_error) ? DISCARD : FORCE_EOF);
  2764. /* NOTREACHED */
  2765. return (NULL);
  2766. }
  2767. else
  2768. return (result);
  2769. }
  2770. /* Perform parameter expansion, command substitution, and arithmetic
  2771. expansion on STRING, as if it were a word. Leave the result quoted. */
  2772. static WORD_LIST *
  2773. expand_string_internal (string, quoted)
  2774. char *string;
  2775. int quoted;
  2776. {
  2777. WORD_DESC td;
  2778. WORD_LIST *tresult;
  2779. if (string == 0 || *string == 0)
  2780. return ((WORD_LIST *)NULL);
  2781. td.flags = 0;
  2782. td.word = savestring (string);
  2783. tresult = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
  2784. FREE (td.word);
  2785. return (tresult);
  2786. }
  2787. /* Expand STRING by performing parameter expansion, command substitution,
  2788. and arithmetic expansion. Dequote the resulting WORD_LIST before
  2789. returning it, but do not perform word splitting. The call to
  2790. remove_quoted_nulls () is in here because word splitting normally
  2791. takes care of quote removal. */
  2792. WORD_LIST *
  2793. expand_string_unsplit (string, quoted)
  2794. char *string;
  2795. int quoted;
  2796. {
  2797. WORD_LIST *value;
  2798. if (string == 0 || *string == '\0')
  2799. return ((WORD_LIST *)NULL);
  2800. expand_no_split_dollar_star = 1;
  2801. value = expand_string_internal (string, quoted);
  2802. expand_no_split_dollar_star = 0;
  2803. if (value)
  2804. {
  2805. if (value->word)
  2806. {
  2807. remove_quoted_nulls (value->word->word);
  2808. value->word->flags &= ~W_HASQUOTEDNULL;
  2809. }
  2810. dequote_list (value);
  2811. }
  2812. return (value);
  2813. }
  2814. /* Expand the rhs of an assignment statement */
  2815. WORD_LIST *
  2816. expand_string_assignment (string, quoted)
  2817. char *string;
  2818. int quoted;
  2819. {
  2820. WORD_DESC td;
  2821. WORD_LIST *value;
  2822. if (string == 0 || *string == '\0')
  2823. return ((WORD_LIST *)NULL);
  2824. expand_no_split_dollar_star = 1;
  2825. td.flags = W_ASSIGNRHS;
  2826. td.word = savestring (string);
  2827. value = call_expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
  2828. FREE (td.word);
  2829. expand_no_split_dollar_star = 0;
  2830. if (value)
  2831. {
  2832. if (value->word)
  2833. {
  2834. remove_quoted_nulls (value->word->word);
  2835. value->word->flags &= ~W_HASQUOTEDNULL;
  2836. }
  2837. dequote_list (value);
  2838. }
  2839. return (value);
  2840. }
  2841. /* Expand one of the PS? prompt strings. This is a sort of combination of
  2842. expand_string_unsplit and expand_string_internal, but returns the
  2843. passed string when an error occurs. Might want to trap other calls
  2844. to jump_to_top_level here so we don't endlessly loop. */
  2845. WORD_LIST *
  2846. expand_prompt_string (string, quoted, wflags)
  2847. char *string;
  2848. int quoted;
  2849. int wflags;
  2850. {
  2851. WORD_LIST *value;
  2852. WORD_DESC td;
  2853. if (string == 0 || *string == 0)
  2854. return ((WORD_LIST *)NULL);
  2855. td.flags = wflags;
  2856. td.word = savestring (string);
  2857. no_longjmp_on_fatal_error = 1;
  2858. value = expand_word_internal (&td, quoted, 0, (int *)NULL, (int *)NULL);
  2859. no_longjmp_on_fatal_error = 0;
  2860. if (value == &expand_word_error || value == &expand_word_fatal)
  2861. {
  2862. value = make_word_list (make_bare_word (string), (WORD_LIST *)NULL);
  2863. return value;
  2864. }
  2865. FREE (td.word);
  2866. if (value)
  2867. {
  2868. if (value->word)
  2869. {
  2870. remove_quoted_nulls (value->word->word);
  2871. value->word->flags &= ~W_HASQUOTEDNULL;
  2872. }
  2873. dequote_list (value);
  2874. }
  2875. return (value);
  2876. }
  2877. /* Expand STRING just as if you were expanding a word, but do not dequote
  2878. the resultant WORD_LIST. This is called only from within this file,
  2879. and is used to correctly preserve quoted characters when expanding
  2880. things like ${1+"$@"}. This does parameter expansion, command
  2881. substitution, arithmetic expansion, and word splitting. */
  2882. static WORD_LIST *
  2883. expand_string_leave_quoted (string, quoted)
  2884. char *string;
  2885. int quoted;
  2886. {
  2887. WORD_LIST *tlist;
  2888. WORD_LIST *tresult;
  2889. if (string == 0 || *string == '\0')
  2890. return ((WORD_LIST *)NULL);
  2891. tlist = expand_string_internal (string, quoted);
  2892. if (tlist)
  2893. {
  2894. tresult = word_list_split (tlist);
  2895. dispose_words (tlist);
  2896. return (tresult);
  2897. }
  2898. return ((WORD_LIST *)NULL);
  2899. }
  2900. /* This does not perform word splitting or dequote the WORD_LIST
  2901. it returns. */
  2902. static WORD_LIST *
  2903. expand_string_for_rhs (string, quoted, dollar_at_p, has_dollar_at)
  2904. char *string;
  2905. int quoted, *dollar_at_p, *has_dollar_at;
  2906. {
  2907. WORD_DESC td;
  2908. WORD_LIST *tresult;
  2909. if (string == 0 || *string == '\0')
  2910. return (WORD_LIST *)NULL;
  2911. td.flags = 0;
  2912. td.word = string;
  2913. tresult = call_expand_word_internal (&td, quoted, 1, dollar_at_p, has_dollar_at);
  2914. return (tresult);
  2915. }
  2916. /* Expand STRING just as if you were expanding a word. This also returns
  2917. a list of words. Note that filename globbing is *NOT* done for word
  2918. or string expansion, just when the shell is expanding a command. This
  2919. does parameter expansion, command substitution, arithmetic expansion,
  2920. and word splitting. Dequote the resultant WORD_LIST before returning. */
  2921. WORD_LIST *
  2922. expand_string (string, quoted)
  2923. char *string;
  2924. int quoted;
  2925. {
  2926. WORD_LIST *result;
  2927. if (string == 0 || *string == '\0')
  2928. return ((WORD_LIST *)NULL);
  2929. result = expand_string_leave_quoted (string, quoted);
  2930. return (result ? dequote_list (result) : result);
  2931. }
  2932. /***************************************************
  2933. * *
  2934. * Functions to handle quoting chars *
  2935. * *
  2936. ***************************************************/
  2937. /* Conventions:
  2938. A string with s[0] == CTLNUL && s[1] == 0 is a quoted null string.
  2939. The parser passes CTLNUL as CTLESC CTLNUL. */
  2940. /* Quote escape characters in string s, but no other characters. This is
  2941. used to protect CTLESC and CTLNUL in variable values from the rest of
  2942. the word expansion process after the variable is expanded (word splitting
  2943. and filename generation). If IFS is null, we quote spaces as well, just
  2944. in case we split on spaces later (in the case of unquoted $@, we will
  2945. eventually attempt to split the entire word on spaces). Corresponding
  2946. code exists in dequote_escapes. Even if we don't end up splitting on
  2947. spaces, quoting spaces is not a problem. This should never be called on
  2948. a string that is quoted with single or double quotes or part of a here
  2949. document (effectively double-quoted). */
  2950. char *
  2951. quote_escapes (string)
  2952. char *string;
  2953. {
  2954. register char *s, *t;
  2955. size_t slen;
  2956. char *result, *send;
  2957. int quote_spaces, skip_ctlesc, skip_ctlnul;
  2958. DECLARE_MBSTATE;
  2959. slen = strlen (string);
  2960. send = string + slen;
  2961. quote_spaces = (ifs_value && *ifs_value == 0);
  2962. for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
  2963. skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
  2964. t = result = (char *)xmalloc ((slen * 2) + 1);
  2965. s = string;
  2966. while (*s)
  2967. {
  2968. if ((skip_ctlesc == 0 && *s == CTLESC) || (skip_ctlnul == 0 && *s == CTLNUL) || (quote_spaces && *s == ' '))
  2969. *t++ = CTLESC;
  2970. COPY_CHAR_P (t, s, send);
  2971. }
  2972. *t = '\0';
  2973. return (result);
  2974. }
  2975. static WORD_LIST *
  2976. list_quote_escapes (list)
  2977. WORD_LIST *list;
  2978. {
  2979. register WORD_LIST *w;
  2980. char *t;
  2981. for (w = list; w; w = w->next)
  2982. {
  2983. t = w->word->word;
  2984. w->word->word = quote_escapes (t);
  2985. free (t);
  2986. }
  2987. return list;
  2988. }
  2989. /* Inverse of quote_escapes; remove CTLESC protecting CTLESC or CTLNUL.
  2990. The parser passes us CTLESC as CTLESC CTLESC and CTLNUL as CTLESC CTLNUL.
  2991. This is necessary to make unquoted CTLESC and CTLNUL characters in the
  2992. data stream pass through properly.
  2993. We need to remove doubled CTLESC characters inside quoted strings before
  2994. quoting the entire string, so we do not double the number of CTLESC
  2995. characters.
  2996. Also used by parts of the pattern substitution code. */
  2997. char *
  2998. dequote_escapes (string)
  2999. char *string;
  3000. {
  3001. register char *s, *t, *s1;
  3002. size_t slen;
  3003. char *result, *send;
  3004. int quote_spaces;
  3005. DECLARE_MBSTATE;
  3006. if (string == 0)
  3007. return string;
  3008. slen = strlen (string);
  3009. send = string + slen;
  3010. t = result = (char *)xmalloc (slen + 1);
  3011. if (strchr (string, CTLESC) == 0)
  3012. return (strcpy (result, string));
  3013. quote_spaces = (ifs_value && *ifs_value == 0);
  3014. s = string;
  3015. while (*s)
  3016. {
  3017. if (*s == CTLESC && (s[1] == CTLESC || s[1] == CTLNUL || (quote_spaces && s[1] == ' ')))
  3018. {
  3019. s++;
  3020. if (*s == '\0')
  3021. break;
  3022. }
  3023. COPY_CHAR_P (t, s, send);
  3024. }
  3025. *t = '\0';
  3026. return result;
  3027. }
  3028. /* Return a new string with the quoted representation of character C.
  3029. This turns "" into QUOTED_NULL, so the W_HASQUOTEDNULL flag needs to be
  3030. set in any resultant WORD_DESC where this value is the word. */
  3031. static char *
  3032. make_quoted_char (c)
  3033. int c;
  3034. {
  3035. char *temp;
  3036. temp = (char *)xmalloc (3);
  3037. if (c == 0)
  3038. {
  3039. temp[0] = CTLNUL;
  3040. temp[1] = '\0';
  3041. }
  3042. else
  3043. {
  3044. temp[0] = CTLESC;
  3045. temp[1] = c;
  3046. temp[2] = '\0';
  3047. }
  3048. return (temp);
  3049. }
  3050. /* Quote STRING, returning a new string. This turns "" into QUOTED_NULL, so
  3051. the W_HASQUOTEDNULL flag needs to be set in any resultant WORD_DESC where
  3052. this value is the word. */
  3053. char *
  3054. quote_string (string)
  3055. char *string;
  3056. {
  3057. register char *t;
  3058. size_t slen;
  3059. char *result, *send;
  3060. if (*string == 0)
  3061. {
  3062. result = (char *)xmalloc (2);
  3063. result[0] = CTLNUL;
  3064. result[1] = '\0';
  3065. }
  3066. else
  3067. {
  3068. DECLARE_MBSTATE;
  3069. slen = strlen (string);
  3070. send = string + slen;
  3071. result = (char *)xmalloc ((slen * 2) + 1);
  3072. for (t = result; string < send; )
  3073. {
  3074. *t++ = CTLESC;
  3075. COPY_CHAR_P (t, string, send);
  3076. }
  3077. *t = '\0';
  3078. }
  3079. return (result);
  3080. }
  3081. /* De-quote quoted characters in STRING. */
  3082. char *
  3083. dequote_string (string)
  3084. char *string;
  3085. {
  3086. register char *s, *t;
  3087. size_t slen;
  3088. char *result, *send;
  3089. DECLARE_MBSTATE;
  3090. slen = strlen (string);
  3091. t = result = (char *)xmalloc (slen + 1);
  3092. if (QUOTED_NULL (string))
  3093. {
  3094. result[0] = '\0';
  3095. return (result);
  3096. }
  3097. /* If no character in the string can be quoted, don't bother examining
  3098. each character. Just return a copy of the string passed to us. */
  3099. if (strchr (string, CTLESC) == NULL)
  3100. return (strcpy (result, string));
  3101. send = string + slen;
  3102. s = string;
  3103. while (*s)
  3104. {
  3105. if (*s == CTLESC)
  3106. {
  3107. s++;
  3108. if (*s == '\0')
  3109. break;
  3110. }
  3111. COPY_CHAR_P (t, s, send);
  3112. }
  3113. *t = '\0';
  3114. return (result);
  3115. }
  3116. /* Quote the entire WORD_LIST list. */
  3117. static WORD_LIST *
  3118. quote_list (list)
  3119. WORD_LIST *list;
  3120. {
  3121. register WORD_LIST *w;
  3122. char *t;
  3123. for (w = list; w; w = w->next)
  3124. {
  3125. t = w->word->word;
  3126. w->word->word = quote_string (t);
  3127. if (*t == 0)
  3128. w->word->flags |= W_HASQUOTEDNULL; /* XXX - turn on W_HASQUOTEDNULL here? */
  3129. w->word->flags |= W_QUOTED;
  3130. free (t);
  3131. }
  3132. return list;
  3133. }
  3134. /* De-quote quoted characters in each word in LIST. */
  3135. WORD_LIST *
  3136. dequote_list (list)
  3137. WORD_LIST *list;
  3138. {
  3139. register char *s;
  3140. register WORD_LIST *tlist;
  3141. for (tlist = list; tlist; tlist = tlist->next)
  3142. {
  3143. s = dequote_string (tlist->word->word);
  3144. if (QUOTED_NULL (tlist->word->word))
  3145. tlist->word->flags &= ~W_HASQUOTEDNULL;
  3146. free (tlist->word->word);
  3147. tlist->word->word = s;
  3148. }
  3149. return list;
  3150. }
  3151. /* Remove CTLESC protecting a CTLESC or CTLNUL in place. Return the passed
  3152. string. */
  3153. char *
  3154. remove_quoted_escapes (string)
  3155. char *string;
  3156. {
  3157. char *t;
  3158. if (string)
  3159. {
  3160. t = dequote_escapes (string);
  3161. strcpy (string, t);
  3162. free (t);
  3163. }
  3164. return (string);
  3165. }
  3166. /* Perform quoted null character removal on STRING. We don't allow any
  3167. quoted null characters in the middle or at the ends of strings because
  3168. of how expand_word_internal works. remove_quoted_nulls () turns
  3169. STRING into an empty string iff it only consists of a quoted null,
  3170. and removes all unquoted CTLNUL characters. */
  3171. char *
  3172. remove_quoted_nulls (string)
  3173. char *string;
  3174. {
  3175. register size_t slen;
  3176. register int i, j, prev_i;
  3177. DECLARE_MBSTATE;
  3178. if (strchr (string, CTLNUL) == 0) /* XXX */
  3179. return string; /* XXX */
  3180. slen = strlen (string);
  3181. i = j = 0;
  3182. while (i < slen)
  3183. {
  3184. if (string[i] == CTLESC)
  3185. {
  3186. /* Old code had j++, but we cannot assume that i == j at this
  3187. point -- what if a CTLNUL has already been removed from the
  3188. string? We don't want to drop the CTLESC or recopy characters
  3189. that we've already copied down. */
  3190. i++; string[j++] = CTLESC;
  3191. if (i == slen)
  3192. break;
  3193. }
  3194. else if (string[i] == CTLNUL)
  3195. i++;
  3196. prev_i = i;
  3197. ADVANCE_CHAR (string, slen, i);
  3198. if (j < prev_i)
  3199. {
  3200. do string[j++] = string[prev_i++]; while (prev_i < i);
  3201. }
  3202. else
  3203. j = i;
  3204. }
  3205. string[j] = '\0';
  3206. return (string);
  3207. }
  3208. /* Perform quoted null character removal on each element of LIST.
  3209. This modifies LIST. */
  3210. void
  3211. word_list_remove_quoted_nulls (list)
  3212. WORD_LIST *list;
  3213. {
  3214. register WORD_LIST *t;
  3215. for (t = list; t; t = t->next)
  3216. {
  3217. remove_quoted_nulls (t->word->word);
  3218. t->word->flags &= ~W_HASQUOTEDNULL;
  3219. }
  3220. }
  3221. /* **************************************************************** */
  3222. /* */
  3223. /* Functions for Matching and Removing Patterns */
  3224. /* */
  3225. /* **************************************************************** */
  3226. #if defined (HANDLE_MULTIBYTE)
  3227. #if 0 /* Currently unused */
  3228. static unsigned char *
  3229. mb_getcharlens (string, len)
  3230. char *string;
  3231. int len;
  3232. {
  3233. int i, offset, last;
  3234. unsigned char *ret;
  3235. char *p;
  3236. DECLARE_MBSTATE;
  3237. i = offset = 0;
  3238. last = 0;
  3239. ret = (unsigned char *)xmalloc (len);
  3240. memset (ret, 0, len);
  3241. while (string[last])
  3242. {
  3243. ADVANCE_CHAR (string, len, offset);
  3244. ret[last] = offset - last;
  3245. last = offset;
  3246. }
  3247. return ret;
  3248. }
  3249. #endif
  3250. #endif
  3251. /* Remove the portion of PARAM matched by PATTERN according to OP, where OP
  3252. can have one of 4 values:
  3253. RP_LONG_LEFT remove longest matching portion at start of PARAM
  3254. RP_SHORT_LEFT remove shortest matching portion at start of PARAM
  3255. RP_LONG_RIGHT remove longest matching portion at end of PARAM
  3256. RP_SHORT_RIGHT remove shortest matching portion at end of PARAM
  3257. */
  3258. #define RP_LONG_LEFT 1
  3259. #define RP_SHORT_LEFT 2
  3260. #define RP_LONG_RIGHT 3
  3261. #define RP_SHORT_RIGHT 4
  3262. static char *
  3263. remove_upattern (param, pattern, op)
  3264. char *param, *pattern;
  3265. int op;
  3266. {
  3267. register int len;
  3268. register char *end;
  3269. register char *p, *ret, c;
  3270. len = STRLEN (param);
  3271. end = param + len;
  3272. switch (op)
  3273. {
  3274. case RP_LONG_LEFT: /* remove longest match at start */
  3275. for (p = end; p >= param; p--)
  3276. {
  3277. c = *p; *p = '\0';
  3278. if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3279. {
  3280. *p = c;
  3281. return (savestring (p));
  3282. }
  3283. *p = c;
  3284. }
  3285. break;
  3286. case RP_SHORT_LEFT: /* remove shortest match at start */
  3287. for (p = param; p <= end; p++)
  3288. {
  3289. c = *p; *p = '\0';
  3290. if (strmatch (pattern, param, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3291. {
  3292. *p = c;
  3293. return (savestring (p));
  3294. }
  3295. *p = c;
  3296. }
  3297. break;
  3298. case RP_LONG_RIGHT: /* remove longest match at end */
  3299. for (p = param; p <= end; p++)
  3300. {
  3301. if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3302. {
  3303. c = *p; *p = '\0';
  3304. ret = savestring (param);
  3305. *p = c;
  3306. return (ret);
  3307. }
  3308. }
  3309. break;
  3310. case RP_SHORT_RIGHT: /* remove shortest match at end */
  3311. for (p = end; p >= param; p--)
  3312. {
  3313. if (strmatch (pattern, p, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3314. {
  3315. c = *p; *p = '\0';
  3316. ret = savestring (param);
  3317. *p = c;
  3318. return (ret);
  3319. }
  3320. }
  3321. break;
  3322. }
  3323. return (savestring (param)); /* no match, return original string */
  3324. }
  3325. #if defined (HANDLE_MULTIBYTE)
  3326. static wchar_t *
  3327. remove_wpattern (wparam, wstrlen, wpattern, op)
  3328. wchar_t *wparam;
  3329. size_t wstrlen;
  3330. wchar_t *wpattern;
  3331. int op;
  3332. {
  3333. wchar_t wc, *ret;
  3334. int n;
  3335. switch (op)
  3336. {
  3337. case RP_LONG_LEFT: /* remove longest match at start */
  3338. for (n = wstrlen; n >= 0; n--)
  3339. {
  3340. wc = wparam[n]; wparam[n] = L'\0';
  3341. if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3342. {
  3343. wparam[n] = wc;
  3344. return (wcsdup (wparam + n));
  3345. }
  3346. wparam[n] = wc;
  3347. }
  3348. break;
  3349. case RP_SHORT_LEFT: /* remove shortest match at start */
  3350. for (n = 0; n <= wstrlen; n++)
  3351. {
  3352. wc = wparam[n]; wparam[n] = L'\0';
  3353. if (wcsmatch (wpattern, wparam, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3354. {
  3355. wparam[n] = wc;
  3356. return (wcsdup (wparam + n));
  3357. }
  3358. wparam[n] = wc;
  3359. }
  3360. break;
  3361. case RP_LONG_RIGHT: /* remove longest match at end */
  3362. for (n = 0; n <= wstrlen; n++)
  3363. {
  3364. if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3365. {
  3366. wc = wparam[n]; wparam[n] = L'\0';
  3367. ret = wcsdup (wparam);
  3368. wparam[n] = wc;
  3369. return (ret);
  3370. }
  3371. }
  3372. break;
  3373. case RP_SHORT_RIGHT: /* remove shortest match at end */
  3374. for (n = wstrlen; n >= 0; n--)
  3375. {
  3376. if (wcsmatch (wpattern, wparam + n, FNMATCH_EXTFLAG) != FNM_NOMATCH)
  3377. {
  3378. wc = wparam[n]; wparam[n] = L'\0';
  3379. ret = wcsdup (wparam);
  3380. wparam[n] = wc;
  3381. return (ret);
  3382. }
  3383. }
  3384. break;
  3385. }
  3386. return (wcsdup (wparam)); /* no match, return original string */
  3387. }
  3388. #endif /* HANDLE_MULTIBYTE */
  3389. static char *
  3390. remove_pattern (param, pattern, op)
  3391. char *param, *pattern;
  3392. int op;
  3393. {
  3394. if (param == NULL)
  3395. return (param);
  3396. if (*param == '\0' || pattern == NULL || *pattern == '\0') /* minor optimization */
  3397. return (savestring (param));
  3398. #if defined (HANDLE_MULTIBYTE)
  3399. if (MB_CUR_MAX > 1)
  3400. {
  3401. wchar_t *ret, *oret;
  3402. size_t n;
  3403. wchar_t *wparam, *wpattern;
  3404. mbstate_t ps;
  3405. char *xret;
  3406. n = xdupmbstowcs (&wpattern, NULL, pattern);
  3407. if (n == (size_t)-1)
  3408. return (remove_upattern (param, pattern, op));
  3409. n = xdupmbstowcs (&wparam, NULL, param);
  3410. if (n == (size_t)-1)
  3411. {
  3412. free (wpattern);
  3413. return (remove_upattern (param, pattern, op));
  3414. }
  3415. oret = ret = remove_wpattern (wparam, n, wpattern, op);
  3416. free (wparam);
  3417. free (wpattern);
  3418. n = strlen (param);
  3419. xret = (char *)xmalloc (n + 1);
  3420. memset (&ps, '\0', sizeof (mbstate_t));
  3421. n = wcsrtombs (xret, (const wchar_t **)&ret, n, &ps);
  3422. xret[n] = '\0'; /* just to make sure */
  3423. free (oret);
  3424. return xret;
  3425. }
  3426. else
  3427. #endif
  3428. return (remove_upattern (param, pattern, op));
  3429. }
  3430. /* Return 1 of the first character of STRING could match the first
  3431. character of pattern PAT. Used to avoid n2 calls to strmatch(). */
  3432. static int
  3433. match_pattern_char (pat, string)
  3434. char *pat, *string;
  3435. {
  3436. char c;
  3437. if (*string == 0)
  3438. return (0);
  3439. switch (c = *pat++)
  3440. {
  3441. default:
  3442. return (*string == c);
  3443. case '\\':
  3444. return (*string == *pat);
  3445. case '?':
  3446. return (*pat == LPAREN ? 1 : (*string != '\0'));
  3447. case '*':
  3448. return (1);
  3449. case '+':
  3450. case '!':
  3451. case '@':
  3452. return (*pat == LPAREN ? 1 : (*string == c));
  3453. case '[':
  3454. return (*string != '\0');
  3455. }
  3456. }
  3457. /* Match PAT anywhere in STRING and return the match boundaries.
  3458. This returns 1 in case of a successful match, 0 otherwise. SP
  3459. and EP are pointers into the string where the match begins and
  3460. ends, respectively. MTYPE controls what kind of match is attempted.
  3461. MATCH_BEG and MATCH_END anchor the match at the beginning and end
  3462. of the string, respectively. The longest match is returned. */
  3463. static int
  3464. match_upattern (string, pat, mtype, sp, ep)
  3465. char *string, *pat;
  3466. int mtype;
  3467. char **sp, **ep;
  3468. {
  3469. int c, len;
  3470. register char *p, *p1, *npat;
  3471. char *end;
  3472. /* If the pattern doesn't match anywhere in the string, go ahead and
  3473. short-circuit right away. A minor optimization, saves a bunch of
  3474. unnecessary calls to strmatch (up to N calls for a string of N
  3475. characters) if the match is unsuccessful. To preserve the semantics
  3476. of the substring matches below, we make sure that the pattern has
  3477. `*' as first and last character, making a new pattern if necessary. */
  3478. /* XXX - check this later if I ever implement `**' with special meaning,
  3479. since this will potentially result in `**' at the beginning or end */
  3480. len = STRLEN (pat);
  3481. if (pat[0] != '*' || (pat[0] == '*' && pat[1] == LPAREN && extended_glob) || pat[len - 1] != '*')
  3482. {
  3483. p = npat = (char *)xmalloc (len + 3);
  3484. p1 = pat;
  3485. if (*p1 != '*' || (*p1 == '*' && p1[1] == LPAREN && extended_glob))
  3486. *p++ = '*';
  3487. while (*p1)
  3488. *p++ = *p1++;
  3489. if (p1[-1] != '*' || p[-2] == '\\')
  3490. *p++ = '*';
  3491. *p = '\0';
  3492. }
  3493. else
  3494. npat = pat;
  3495. c = strmatch (npat, string, FNMATCH_EXTFLAG);
  3496. if (npat != pat)
  3497. free (npat);
  3498. if (c == FNM_NOMATCH)
  3499. return (0);
  3500. len = STRLEN (string);
  3501. end = string + len;
  3502. switch (mtype)
  3503. {
  3504. case MATCH_ANY:
  3505. for (p = string; p <= end; p++)
  3506. {
  3507. if (match_pattern_char (pat, p))
  3508. {
  3509. for (p1 = end; p1 >= p; p1--)
  3510. {
  3511. c = *p1; *p1 = '\0';
  3512. if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
  3513. {
  3514. *p1 = c;
  3515. *sp = p;
  3516. *ep = p1;
  3517. return 1;
  3518. }
  3519. *p1 = c;
  3520. }
  3521. }
  3522. }
  3523. return (0);
  3524. case MATCH_BEG:
  3525. if (match_pattern_char (pat, string) == 0)
  3526. return (0);
  3527. for (p = end; p >= string; p--)
  3528. {
  3529. c = *p; *p = '\0';
  3530. if (strmatch (pat, string, FNMATCH_EXTFLAG) == 0)
  3531. {
  3532. *p = c;
  3533. *sp = string;
  3534. *ep = p;
  3535. return 1;
  3536. }
  3537. *p = c;
  3538. }
  3539. return (0);
  3540. case MATCH_END:
  3541. for (p = string; p <= end; p++)
  3542. {
  3543. if (strmatch (pat, p, FNMATCH_EXTFLAG) == 0)
  3544. {
  3545. *sp = p;
  3546. *ep = end;
  3547. return 1;
  3548. }
  3549. }
  3550. return (0);
  3551. }
  3552. return (0);
  3553. }
  3554. #if defined (HANDLE_MULTIBYTE)
  3555. /* Return 1 of the first character of WSTRING could match the first
  3556. character of pattern WPAT. Wide character version. */
  3557. static int
  3558. match_pattern_wchar (wpat, wstring)
  3559. wchar_t *wpat, *wstring;
  3560. {
  3561. wchar_t wc;
  3562. if (*wstring == 0)
  3563. return (0);
  3564. switch (wc = *wpat++)
  3565. {
  3566. default:
  3567. return (*wstring == wc);
  3568. case L'\\':
  3569. return (*wstring == *wpat);
  3570. case L'?':
  3571. return (*wpat == LPAREN ? 1 : (*wstring != L'\0'));
  3572. case L'*':
  3573. return (1);
  3574. case L'+':
  3575. case L'!':
  3576. case L'@':
  3577. return (*wpat == LPAREN ? 1 : (*wstring == wc));
  3578. case L'[':
  3579. return (*wstring != L'\0');
  3580. }
  3581. }
  3582. /* Match WPAT anywhere in WSTRING and return the match boundaries.
  3583. This returns 1 in case of a successful match, 0 otherwise. Wide
  3584. character version. */
  3585. static int
  3586. match_wpattern (wstring, indices, wstrlen, wpat, mtype, sp, ep)
  3587. wchar_t *wstring;
  3588. char **indices;
  3589. size_t wstrlen;
  3590. wchar_t *wpat;
  3591. int mtype;
  3592. char **sp, **ep;
  3593. {
  3594. wchar_t wc, *wp, *nwpat, *wp1;
  3595. int len;
  3596. #if 0
  3597. size_t n, n1; /* Apple's gcc seems to miscompile this badly */
  3598. #else
  3599. int n, n1;
  3600. #endif
  3601. /* If the pattern doesn't match anywhere in the string, go ahead and
  3602. short-circuit right away. A minor optimization, saves a bunch of
  3603. unnecessary calls to strmatch (up to N calls for a string of N
  3604. characters) if the match is unsuccessful. To preserve the semantics
  3605. of the substring matches below, we make sure that the pattern has
  3606. `*' as first and last character, making a new pattern if necessary. */
  3607. /* XXX - check this later if I ever implement `**' with special meaning,
  3608. since this will potentially result in `**' at the beginning or end */
  3609. len = wcslen (wpat);
  3610. if (wpat[0] != L'*' || (wpat[0] == L'*' && wpat[1] == WLPAREN && extended_glob) || wpat[len - 1] != L'*')
  3611. {
  3612. wp = nwpat = (wchar_t *)xmalloc ((len + 3) * sizeof (wchar_t));
  3613. wp1 = wpat;
  3614. if (*wp1 != L'*' || (*wp1 == '*' && wp1[1] == WLPAREN && extended_glob))
  3615. *wp++ = L'*';
  3616. while (*wp1 != L'\0')
  3617. *wp++ = *wp1++;
  3618. if (wp1[-1] != L'*' || wp1[-2] == L'\\')
  3619. *wp++ = L'*';
  3620. *wp = '\0';
  3621. }
  3622. else
  3623. nwpat = wpat;
  3624. len = wcsmatch (nwpat, wstring, FNMATCH_EXTFLAG);
  3625. if (nwpat != wpat)
  3626. free (nwpat);
  3627. if (len == FNM_NOMATCH)
  3628. return (0);
  3629. switch (mtype)
  3630. {
  3631. case MATCH_ANY:
  3632. for (n = 0; n <= wstrlen; n++)
  3633. {
  3634. if (match_pattern_wchar (wpat, wstring + n))
  3635. {
  3636. for (n1 = wstrlen; n1 >= n; n1--)
  3637. {
  3638. wc = wstring[n1]; wstring[n1] = L'\0';
  3639. if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
  3640. {
  3641. wstring[n1] = wc;
  3642. *sp = indices[n];
  3643. *ep = indices[n1];
  3644. return 1;
  3645. }
  3646. wstring[n1] = wc;
  3647. }
  3648. }
  3649. }
  3650. return (0);
  3651. case MATCH_BEG:
  3652. if (match_pattern_wchar (wpat, wstring) == 0)
  3653. return (0);
  3654. for (n = wstrlen; n >= 0; n--)
  3655. {
  3656. wc = wstring[n]; wstring[n] = L'\0';
  3657. if (wcsmatch (wpat, wstring, FNMATCH_EXTFLAG) == 0)
  3658. {
  3659. wstring[n] = wc;
  3660. *sp = indices[0];
  3661. *ep = indices[n];
  3662. return 1;
  3663. }
  3664. wstring[n] = wc;
  3665. }
  3666. return (0);
  3667. case MATCH_END:
  3668. for (n = 0; n <= wstrlen; n++)
  3669. {
  3670. if (wcsmatch (wpat, wstring + n, FNMATCH_EXTFLAG) == 0)
  3671. {
  3672. *sp = indices[n];
  3673. *ep = indices[wstrlen];
  3674. return 1;
  3675. }
  3676. }
  3677. return (0);
  3678. }
  3679. return (0);
  3680. }
  3681. #endif /* HANDLE_MULTIBYTE */
  3682. static int
  3683. match_pattern (string, pat, mtype, sp, ep)
  3684. char *string, *pat;
  3685. int mtype;
  3686. char **sp, **ep;
  3687. {
  3688. #if defined (HANDLE_MULTIBYTE)
  3689. int ret;
  3690. size_t n;
  3691. wchar_t *wstring, *wpat;
  3692. char **indices;
  3693. #endif
  3694. if (string == 0 || *string == 0 || pat == 0 || *pat == 0)
  3695. return (0);
  3696. #if defined (HANDLE_MULTIBYTE)
  3697. if (MB_CUR_MAX > 1)
  3698. {
  3699. n = xdupmbstowcs (&wpat, NULL, pat);
  3700. if (n == (size_t)-1)
  3701. return (match_upattern (string, pat, mtype, sp, ep));
  3702. n = xdupmbstowcs (&wstring, &indices, string);
  3703. if (n == (size_t)-1)
  3704. {
  3705. free (wpat);
  3706. return (match_upattern (string, pat, mtype, sp, ep));
  3707. }
  3708. ret = match_wpattern (wstring, indices, n, wpat, mtype, sp, ep);
  3709. free (wpat);
  3710. free (wstring);
  3711. free (indices);
  3712. return (ret);
  3713. }
  3714. else
  3715. #endif
  3716. return (match_upattern (string, pat, mtype, sp, ep));
  3717. }
  3718. static int
  3719. getpatspec (c, value)
  3720. int c;
  3721. char *value;
  3722. {
  3723. if (c == '#')
  3724. return ((*value == '#') ? RP_LONG_LEFT : RP_SHORT_LEFT);
  3725. else /* c == '%' */
  3726. return ((*value == '%') ? RP_LONG_RIGHT : RP_SHORT_RIGHT);
  3727. }
  3728. /* Posix.2 says that the WORD should be run through tilde expansion,
  3729. parameter expansion, command substitution and arithmetic expansion.
  3730. This leaves the result quoted, so quote_string_for_globbing () has
  3731. to be called to fix it up for strmatch (). If QUOTED is non-zero,
  3732. it means that the entire expression was enclosed in double quotes.
  3733. This means that quoting characters in the pattern do not make any
  3734. special pattern characters quoted. For example, the `*' in the
  3735. following retains its special meaning: "${foo#'*'}". */
  3736. static char *
  3737. getpattern (value, quoted, expandpat)
  3738. char *value;
  3739. int quoted, expandpat;
  3740. {
  3741. char *pat, *tword;
  3742. WORD_LIST *l;
  3743. #if 0
  3744. int i;
  3745. #endif
  3746. /* There is a problem here: how to handle single or double quotes in the
  3747. pattern string when the whole expression is between double quotes?
  3748. POSIX.2 says that enclosing double quotes do not cause the pattern to
  3749. be quoted, but does that leave us a problem with @ and array[@] and their
  3750. expansions inside a pattern? */
  3751. #if 0
  3752. if (expandpat && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *tword)
  3753. {
  3754. i = 0;
  3755. pat = string_extract_double_quoted (tword, &i, 1);
  3756. free (tword);
  3757. tword = pat;
  3758. }
  3759. #endif
  3760. /* expand_string_for_rhs () leaves WORD quoted and does not perform
  3761. word splitting. */
  3762. l = *value ? expand_string_for_rhs (value,
  3763. (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) ? Q_PATQUOTE : quoted,
  3764. (int *)NULL, (int *)NULL)
  3765. : (WORD_LIST *)0;
  3766. pat = string_list (l);
  3767. dispose_words (l);
  3768. if (pat)
  3769. {
  3770. tword = quote_string_for_globbing (pat, QGLOB_CVTNULL);
  3771. free (pat);
  3772. pat = tword;
  3773. }
  3774. return (pat);
  3775. }
  3776. #if 0
  3777. /* Handle removing a pattern from a string as a result of ${name%[%]value}
  3778. or ${name#[#]value}. */
  3779. static char *
  3780. variable_remove_pattern (value, pattern, patspec, quoted)
  3781. char *value, *pattern;
  3782. int patspec, quoted;
  3783. {
  3784. char *tword;
  3785. tword = remove_pattern (value, pattern, patspec);
  3786. return (tword);
  3787. }
  3788. #endif
  3789. static char *
  3790. list_remove_pattern (list, pattern, patspec, itype, quoted)
  3791. WORD_LIST *list;
  3792. char *pattern;
  3793. int patspec, itype, quoted;
  3794. {
  3795. WORD_LIST *new, *l;
  3796. WORD_DESC *w;
  3797. char *tword;
  3798. for (new = (WORD_LIST *)NULL, l = list; l; l = l->next)
  3799. {
  3800. tword = remove_pattern (l->word->word, pattern, patspec);
  3801. w = alloc_word_desc ();
  3802. w->word = tword ? tword : savestring ("");
  3803. new = make_word_list (w, new);
  3804. }
  3805. l = REVERSE_LIST (new, WORD_LIST *);
  3806. tword = string_list_pos_params (itype, l, quoted);
  3807. dispose_words (l);
  3808. return (tword);
  3809. }
  3810. static char *
  3811. parameter_list_remove_pattern (itype, pattern, patspec, quoted)
  3812. int itype;
  3813. char *pattern;
  3814. int patspec, quoted;
  3815. {
  3816. char *ret;
  3817. WORD_LIST *list;
  3818. list = list_rest_of_args ();
  3819. if (list == 0)
  3820. return ((char *)NULL);
  3821. ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
  3822. dispose_words (list);
  3823. return (ret);
  3824. }
  3825. #if defined (ARRAY_VARS)
  3826. static char *
  3827. array_remove_pattern (var, pattern, patspec, varname, quoted)
  3828. SHELL_VAR *var;
  3829. char *pattern;
  3830. int patspec;
  3831. char *varname; /* so we can figure out how it's indexed */
  3832. int quoted;
  3833. {
  3834. ARRAY *a;
  3835. HASH_TABLE *h;
  3836. int itype;
  3837. char *ret;
  3838. WORD_LIST *list;
  3839. SHELL_VAR *v;
  3840. /* compute itype from varname here */
  3841. v = array_variable_part (varname, &ret, 0);
  3842. itype = ret[0];
  3843. a = (v && array_p (v)) ? array_cell (v) : 0;
  3844. h = (v && assoc_p (v)) ? assoc_cell (v) : 0;
  3845. list = a ? array_to_word_list (a) : (h ? assoc_to_word_list (h) : 0);
  3846. if (list == 0)
  3847. return ((char *)NULL);
  3848. ret = list_remove_pattern (list, pattern, patspec, itype, quoted);
  3849. dispose_words (list);
  3850. return ret;
  3851. }
  3852. #endif /* ARRAY_VARS */
  3853. static char *
  3854. parameter_brace_remove_pattern (varname, value, patstr, rtype, quoted)
  3855. char *varname, *value, *patstr;
  3856. int rtype, quoted;
  3857. {
  3858. int vtype, patspec, starsub;
  3859. char *temp1, *val, *pattern;
  3860. SHELL_VAR *v;
  3861. if (value == 0)
  3862. return ((char *)NULL);
  3863. this_command_name = varname;
  3864. vtype = get_var_and_type (varname, value, quoted, &v, &val);
  3865. if (vtype == -1)
  3866. return ((char *)NULL);
  3867. starsub = vtype & VT_STARSUB;
  3868. vtype &= ~VT_STARSUB;
  3869. patspec = getpatspec (rtype, patstr);
  3870. if (patspec == RP_LONG_LEFT || patspec == RP_LONG_RIGHT)
  3871. patstr++;
  3872. /* Need to pass getpattern newly-allocated memory in case of expansion --
  3873. the expansion code will free the passed string on an error. */
  3874. temp1 = savestring (patstr);
  3875. pattern = getpattern (temp1, quoted, 1);
  3876. free (temp1);
  3877. temp1 = (char *)NULL; /* shut up gcc */
  3878. switch (vtype)
  3879. {
  3880. case VT_VARIABLE:
  3881. case VT_ARRAYMEMBER:
  3882. temp1 = remove_pattern (val, pattern, patspec);
  3883. if (vtype == VT_VARIABLE)
  3884. FREE (val);
  3885. if (temp1)
  3886. {
  3887. val = (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  3888. ? quote_string (temp1)
  3889. : quote_escapes (temp1);
  3890. free (temp1);
  3891. temp1 = val;
  3892. }
  3893. break;
  3894. #if defined (ARRAY_VARS)
  3895. case VT_ARRAYVAR:
  3896. temp1 = array_remove_pattern (v, pattern, patspec, varname, quoted);
  3897. if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
  3898. {
  3899. val = quote_escapes (temp1);
  3900. free (temp1);
  3901. temp1 = val;
  3902. }
  3903. break;
  3904. #endif
  3905. case VT_POSPARMS:
  3906. temp1 = parameter_list_remove_pattern (varname[0], pattern, patspec, quoted);
  3907. if (temp1 && ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) == 0))
  3908. {
  3909. val = quote_escapes (temp1);
  3910. free (temp1);
  3911. temp1 = val;
  3912. }
  3913. break;
  3914. }
  3915. FREE (pattern);
  3916. return temp1;
  3917. }
  3918. /*******************************************
  3919. * *
  3920. * Functions to expand WORD_DESCs *
  3921. * *
  3922. *******************************************/
  3923. /* Expand WORD, performing word splitting on the result. This does
  3924. parameter expansion, command substitution, arithmetic expansion,
  3925. word splitting, and quote removal. */
  3926. WORD_LIST *
  3927. expand_word (word, quoted)
  3928. WORD_DESC *word;
  3929. int quoted;
  3930. {
  3931. WORD_LIST *result, *tresult;
  3932. tresult = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
  3933. result = word_list_split (tresult);
  3934. dispose_words (tresult);
  3935. return (result ? dequote_list (result) : result);
  3936. }
  3937. /* Expand WORD, but do not perform word splitting on the result. This
  3938. does parameter expansion, command substitution, arithmetic expansion,
  3939. and quote removal. */
  3940. WORD_LIST *
  3941. expand_word_unsplit (word, quoted)
  3942. WORD_DESC *word;
  3943. int quoted;
  3944. {
  3945. WORD_LIST *result;
  3946. expand_no_split_dollar_star = 1;
  3947. #if defined (HANDLE_MULTIBYTE)
  3948. if (ifs_firstc[0] == 0)
  3949. #else
  3950. if (ifs_firstc == 0)
  3951. #endif
  3952. word->flags |= W_NOSPLIT;
  3953. result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
  3954. expand_no_split_dollar_star = 0;
  3955. return (result ? dequote_list (result) : result);
  3956. }
  3957. /* Perform shell expansions on WORD, but do not perform word splitting or
  3958. quote removal on the result. Virtually identical to expand_word_unsplit;
  3959. could be combined if implementations don't diverge. */
  3960. WORD_LIST *
  3961. expand_word_leave_quoted (word, quoted)
  3962. WORD_DESC *word;
  3963. int quoted;
  3964. {
  3965. WORD_LIST *result;
  3966. expand_no_split_dollar_star = 1;
  3967. #if defined (HANDLE_MULTIBYTE)
  3968. if (ifs_firstc[0] == 0)
  3969. #else
  3970. if (ifs_firstc == 0)
  3971. #endif
  3972. word->flags |= W_NOSPLIT;
  3973. word->flags |= W_NOSPLIT2;
  3974. result = call_expand_word_internal (word, quoted, 0, (int *)NULL, (int *)NULL);
  3975. expand_no_split_dollar_star = 0;
  3976. return result;
  3977. }
  3978. #if defined (PROCESS_SUBSTITUTION)
  3979. /*****************************************************************/
  3980. /* */
  3981. /* Hacking Process Substitution */
  3982. /* */
  3983. /*****************************************************************/
  3984. #if !defined (HAVE_DEV_FD)
  3985. /* Named pipes must be removed explicitly with `unlink'. This keeps a list
  3986. of FIFOs the shell has open. unlink_fifo_list will walk the list and
  3987. unlink all of them. add_fifo_list adds the name of an open FIFO to the
  3988. list. NFIFO is a count of the number of FIFOs in the list. */
  3989. #define FIFO_INCR 20
  3990. struct temp_fifo {
  3991. char *file;
  3992. pid_t proc;
  3993. };
  3994. static struct temp_fifo *fifo_list = (struct temp_fifo *)NULL;
  3995. static int nfifo;
  3996. static int fifo_list_size;
  3997. static void
  3998. add_fifo_list (pathname)
  3999. char *pathname;
  4000. {
  4001. if (nfifo >= fifo_list_size - 1)
  4002. {
  4003. fifo_list_size += FIFO_INCR;
  4004. fifo_list = (struct temp_fifo *)xrealloc (fifo_list,
  4005. fifo_list_size * sizeof (struct temp_fifo));
  4006. }
  4007. fifo_list[nfifo].file = savestring (pathname);
  4008. nfifo++;
  4009. }
  4010. void
  4011. unlink_fifo_list ()
  4012. {
  4013. int saved, i, j;
  4014. if (nfifo == 0)
  4015. return;
  4016. for (i = saved = 0; i < nfifo; i++)
  4017. {
  4018. if ((fifo_list[i].proc == -1) || (kill(fifo_list[i].proc, 0) == -1))
  4019. {
  4020. unlink (fifo_list[i].file);
  4021. free (fifo_list[i].file);
  4022. fifo_list[i].file = (char *)NULL;
  4023. fifo_list[i].proc = -1;
  4024. }
  4025. else
  4026. saved++;
  4027. }
  4028. /* If we didn't remove some of the FIFOs, compact the list. */
  4029. if (saved)
  4030. {
  4031. for (i = j = 0; i < nfifo; i++)
  4032. if (fifo_list[i].file)
  4033. {
  4034. fifo_list[j].file = fifo_list[i].file;
  4035. fifo_list[j].proc = fifo_list[i].proc;
  4036. j++;
  4037. }
  4038. nfifo = j;
  4039. }
  4040. else
  4041. nfifo = 0;
  4042. }
  4043. int
  4044. fifos_pending ()
  4045. {
  4046. return nfifo;
  4047. }
  4048. static char *
  4049. make_named_pipe ()
  4050. {
  4051. char *tname;
  4052. tname = sh_mktmpname ("sh-np", MT_USERANDOM|MT_USETMPDIR);
  4053. if (mkfifo (tname, 0600) < 0)
  4054. {
  4055. free (tname);
  4056. return ((char *)NULL);
  4057. }
  4058. add_fifo_list (tname);
  4059. return (tname);
  4060. }
  4061. #else /* HAVE_DEV_FD */
  4062. /* DEV_FD_LIST is a bitmap of file descriptors attached to pipes the shell
  4063. has open to children. NFDS is a count of the number of bits currently
  4064. set in DEV_FD_LIST. TOTFDS is a count of the highest possible number
  4065. of open files. */
  4066. static char *dev_fd_list = (char *)NULL;
  4067. static int nfds;
  4068. static int totfds; /* The highest possible number of open files. */
  4069. static void
  4070. add_fifo_list (fd)
  4071. int fd;
  4072. {
  4073. if (!dev_fd_list || fd >= totfds)
  4074. {
  4075. int ofds;
  4076. ofds = totfds;
  4077. totfds = getdtablesize ();
  4078. if (totfds < 0 || totfds > 256)
  4079. totfds = 256;
  4080. if (fd >= totfds)
  4081. totfds = fd + 2;
  4082. dev_fd_list = (char *)xrealloc (dev_fd_list, totfds);
  4083. memset (dev_fd_list + ofds, '\0', totfds - ofds);
  4084. }
  4085. dev_fd_list[fd] = 1;
  4086. nfds++;
  4087. }
  4088. int
  4089. fifos_pending ()
  4090. {
  4091. return 0; /* used for cleanup; not needed with /dev/fd */
  4092. }
  4093. void
  4094. unlink_fifo_list ()
  4095. {
  4096. register int i;
  4097. if (nfds == 0)
  4098. return;
  4099. for (i = 0; nfds && i < totfds; i++)
  4100. if (dev_fd_list[i])
  4101. {
  4102. close (i);
  4103. dev_fd_list[i] = 0;
  4104. nfds--;
  4105. }
  4106. nfds = 0;
  4107. }
  4108. #if defined (NOTDEF)
  4109. print_dev_fd_list ()
  4110. {
  4111. register int i;
  4112. fprintf (stderr, "pid %ld: dev_fd_list:", (long)getpid ());
  4113. fflush (stderr);
  4114. for (i = 0; i < totfds; i++)
  4115. {
  4116. if (dev_fd_list[i])
  4117. fprintf (stderr, " %d", i);
  4118. }
  4119. fprintf (stderr, "\n");
  4120. }
  4121. #endif /* NOTDEF */
  4122. static char *
  4123. make_dev_fd_filename (fd)
  4124. int fd;
  4125. {
  4126. char *ret, intbuf[INT_STRLEN_BOUND (int) + 1], *p;
  4127. ret = (char *)xmalloc (sizeof (DEV_FD_PREFIX) + 8);
  4128. strcpy (ret, DEV_FD_PREFIX);
  4129. p = inttostr (fd, intbuf, sizeof (intbuf));
  4130. strcpy (ret + sizeof (DEV_FD_PREFIX) - 1, p);
  4131. add_fifo_list (fd);
  4132. return (ret);
  4133. }
  4134. #endif /* HAVE_DEV_FD */
  4135. /* Return a filename that will open a connection to the process defined by
  4136. executing STRING. HAVE_DEV_FD, if defined, means open a pipe and return
  4137. a filename in /dev/fd corresponding to a descriptor that is one of the
  4138. ends of the pipe. If not defined, we use named pipes on systems that have
  4139. them. Systems without /dev/fd and named pipes are out of luck.
  4140. OPEN_FOR_READ_IN_CHILD, if 1, means open the named pipe for reading or
  4141. use the read end of the pipe and dup that file descriptor to fd 0 in
  4142. the child. If OPEN_FOR_READ_IN_CHILD is 0, we open the named pipe for
  4143. writing or use the write end of the pipe in the child, and dup that
  4144. file descriptor to fd 1 in the child. The parent does the opposite. */
  4145. static char *
  4146. process_substitute (string, open_for_read_in_child)
  4147. char *string;
  4148. int open_for_read_in_child;
  4149. {
  4150. char *pathname;
  4151. int fd, result;
  4152. pid_t old_pid, pid;
  4153. #if defined (HAVE_DEV_FD)
  4154. int parent_pipe_fd, child_pipe_fd;
  4155. int fildes[2];
  4156. #endif /* HAVE_DEV_FD */
  4157. #if defined (JOB_CONTROL)
  4158. pid_t old_pipeline_pgrp;
  4159. #endif
  4160. if (!string || !*string || wordexp_only)
  4161. return ((char *)NULL);
  4162. #if !defined (HAVE_DEV_FD)
  4163. pathname = make_named_pipe ();
  4164. #else /* HAVE_DEV_FD */
  4165. if (pipe (fildes) < 0)
  4166. {
  4167. sys_error (_("cannot make pipe for process substitution"));
  4168. return ((char *)NULL);
  4169. }
  4170. /* If OPEN_FOR_READ_IN_CHILD == 1, we want to use the write end of
  4171. the pipe in the parent, otherwise the read end. */
  4172. parent_pipe_fd = fildes[open_for_read_in_child];
  4173. child_pipe_fd = fildes[1 - open_for_read_in_child];
  4174. /* Move the parent end of the pipe to some high file descriptor, to
  4175. avoid clashes with FDs used by the script. */
  4176. parent_pipe_fd = move_to_high_fd (parent_pipe_fd, 1, 64);
  4177. pathname = make_dev_fd_filename (parent_pipe_fd);
  4178. #endif /* HAVE_DEV_FD */
  4179. if (pathname == 0)
  4180. {
  4181. sys_error (_("cannot make pipe for process substitution"));
  4182. return ((char *)NULL);
  4183. }
  4184. old_pid = last_made_pid;
  4185. #if defined (JOB_CONTROL)
  4186. old_pipeline_pgrp = pipeline_pgrp;
  4187. pipeline_pgrp = shell_pgrp;
  4188. save_pipeline (1);
  4189. #endif /* JOB_CONTROL */
  4190. pid = make_child ((char *)NULL, 1);
  4191. if (pid == 0)
  4192. {
  4193. reset_terminating_signals (); /* XXX */
  4194. free_pushed_string_input ();
  4195. /* Cancel traps, in trap.c. */
  4196. restore_original_signals ();
  4197. setup_async_signals ();
  4198. subshell_environment |= SUBSHELL_COMSUB|SUBSHELL_PROCSUB;
  4199. }
  4200. #if defined (JOB_CONTROL)
  4201. set_sigchld_handler ();
  4202. stop_making_children ();
  4203. /* XXX - should we only do this in the parent? (as in command subst) */
  4204. pipeline_pgrp = old_pipeline_pgrp;
  4205. #endif /* JOB_CONTROL */
  4206. if (pid < 0)
  4207. {
  4208. sys_error (_("cannot make child for process substitution"));
  4209. free (pathname);
  4210. #if defined (HAVE_DEV_FD)
  4211. close (parent_pipe_fd);
  4212. close (child_pipe_fd);
  4213. #endif /* HAVE_DEV_FD */
  4214. return ((char *)NULL);
  4215. }
  4216. if (pid > 0)
  4217. {
  4218. #if defined (JOB_CONTROL)
  4219. restore_pipeline (1);
  4220. #endif
  4221. #if !defined (HAVE_DEV_FD)
  4222. fifo_list[nfifo-1].proc = pid;
  4223. #endif
  4224. last_made_pid = old_pid;
  4225. #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
  4226. close_pgrp_pipe ();
  4227. #endif /* JOB_CONTROL && PGRP_PIPE */
  4228. #if defined (HAVE_DEV_FD)
  4229. close (child_pipe_fd);
  4230. #endif /* HAVE_DEV_FD */
  4231. return (pathname);
  4232. }
  4233. set_sigint_handler ();
  4234. #if defined (JOB_CONTROL)
  4235. set_job_control (0);
  4236. #endif /* JOB_CONTROL */
  4237. #if !defined (HAVE_DEV_FD)
  4238. /* Open the named pipe in the child. */
  4239. fd = open (pathname, open_for_read_in_child ? O_RDONLY|O_NONBLOCK : O_WRONLY);
  4240. if (fd < 0)
  4241. {
  4242. /* Two separate strings for ease of translation. */
  4243. if (open_for_read_in_child)
  4244. sys_error (_("cannot open named pipe %s for reading"), pathname);
  4245. else
  4246. sys_error (_("cannot open named pipe %s for writing"), pathname);
  4247. exit (127);
  4248. }
  4249. if (open_for_read_in_child)
  4250. {
  4251. if (sh_unset_nodelay_mode (fd) < 0)
  4252. {
  4253. sys_error (_("cannot reset nodelay mode for fd %d"), fd);
  4254. exit (127);
  4255. }
  4256. }
  4257. #else /* HAVE_DEV_FD */
  4258. fd = child_pipe_fd;
  4259. #endif /* HAVE_DEV_FD */
  4260. if (dup2 (fd, open_for_read_in_child ? 0 : 1) < 0)
  4261. {
  4262. sys_error (_("cannot duplicate named pipe %s as fd %d"), pathname,
  4263. open_for_read_in_child ? 0 : 1);
  4264. exit (127);
  4265. }
  4266. if (fd != (open_for_read_in_child ? 0 : 1))
  4267. close (fd);
  4268. /* Need to close any files that this process has open to pipes inherited
  4269. from its parent. */
  4270. if (current_fds_to_close)
  4271. {
  4272. close_fd_bitmap (current_fds_to_close);
  4273. current_fds_to_close = (struct fd_bitmap *)NULL;
  4274. }
  4275. #if defined (HAVE_DEV_FD)
  4276. /* Make sure we close the parent's end of the pipe and clear the slot
  4277. in the fd list so it is not closed later, if reallocated by, for
  4278. instance, pipe(2). */
  4279. close (parent_pipe_fd);
  4280. dev_fd_list[parent_pipe_fd] = 0;
  4281. #endif /* HAVE_DEV_FD */
  4282. result = parse_and_execute (string, "process substitution", (SEVAL_NONINT|SEVAL_NOHIST));
  4283. #if !defined (HAVE_DEV_FD)
  4284. /* Make sure we close the named pipe in the child before we exit. */
  4285. close (open_for_read_in_child ? 0 : 1);
  4286. #endif /* !HAVE_DEV_FD */
  4287. exit (result);
  4288. /*NOTREACHED*/
  4289. }
  4290. #endif /* PROCESS_SUBSTITUTION */
  4291. /***********************************/
  4292. /* */
  4293. /* Command Substitution */
  4294. /* */
  4295. /***********************************/
  4296. static char *
  4297. read_comsub (fd, quoted, rflag)
  4298. int fd, quoted;
  4299. int *rflag;
  4300. {
  4301. char *istring, buf[128], *bufp, *s;
  4302. int istring_index, istring_size, c, tflag, skip_ctlesc, skip_ctlnul;
  4303. ssize_t bufn;
  4304. istring = (char *)NULL;
  4305. istring_index = istring_size = bufn = tflag = 0;
  4306. for (skip_ctlesc = skip_ctlnul = 0, s = ifs_value; s && *s; s++)
  4307. skip_ctlesc |= *s == CTLESC, skip_ctlnul |= *s == CTLNUL;
  4308. #ifdef __CYGWIN__
  4309. setmode (fd, O_TEXT); /* we don't want CR/LF, we want Unix-style */
  4310. #endif
  4311. /* Read the output of the command through the pipe. This may need to be
  4312. changed to understand multibyte characters in the future. */
  4313. while (1)
  4314. {
  4315. if (fd < 0)
  4316. break;
  4317. if (--bufn <= 0)
  4318. {
  4319. bufn = zread (fd, buf, sizeof (buf));
  4320. if (bufn <= 0)
  4321. break;
  4322. bufp = buf;
  4323. }
  4324. c = *bufp++;
  4325. if (c == 0)
  4326. {
  4327. #if 0
  4328. internal_warning ("read_comsub: ignored null byte in input");
  4329. #endif
  4330. continue;
  4331. }
  4332. /* Add the character to ISTRING, possibly after resizing it. */
  4333. RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size, DEFAULT_ARRAY_SIZE);
  4334. /* This is essentially quote_string inline */
  4335. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) /* || c == CTLESC || c == CTLNUL */)
  4336. istring[istring_index++] = CTLESC;
  4337. /* Escape CTLESC and CTLNUL in the output to protect those characters
  4338. from the rest of the word expansions (word splitting and globbing.)
  4339. This is essentially quote_escapes inline. */
  4340. else if (skip_ctlesc == 0 && c == CTLESC)
  4341. {
  4342. tflag |= W_HASCTLESC;
  4343. istring[istring_index++] = CTLESC;
  4344. }
  4345. else if ((skip_ctlnul == 0 && c == CTLNUL) || (c == ' ' && (ifs_value && *ifs_value == 0)))
  4346. istring[istring_index++] = CTLESC;
  4347. istring[istring_index++] = c;
  4348. #if 0
  4349. #if defined (__CYGWIN__)
  4350. if (c == '\n' && istring_index > 1 && istring[istring_index - 2] == '\r')
  4351. {
  4352. istring_index--;
  4353. istring[istring_index - 1] = '\n';
  4354. }
  4355. #endif
  4356. #endif
  4357. }
  4358. if (istring)
  4359. istring[istring_index] = '\0';
  4360. /* If we read no output, just return now and save ourselves some
  4361. trouble. */
  4362. if (istring_index == 0)
  4363. {
  4364. FREE (istring);
  4365. if (rflag)
  4366. *rflag = tflag;
  4367. return (char *)NULL;
  4368. }
  4369. /* Strip trailing newlines from the output of the command. */
  4370. if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  4371. {
  4372. while (istring_index > 0)
  4373. {
  4374. if (istring[istring_index - 1] == '\n')
  4375. {
  4376. --istring_index;
  4377. /* If the newline was quoted, remove the quoting char. */
  4378. if (istring[istring_index - 1] == CTLESC)
  4379. --istring_index;
  4380. }
  4381. else
  4382. break;
  4383. }
  4384. istring[istring_index] = '\0';
  4385. }
  4386. else
  4387. strip_trailing (istring, istring_index - 1, 1);
  4388. if (rflag)
  4389. *rflag = tflag;
  4390. return istring;
  4391. }
  4392. /* Perform command substitution on STRING. This returns a WORD_DESC * with the
  4393. contained string possibly quoted. */
  4394. WORD_DESC *
  4395. command_substitute (string, quoted)
  4396. char *string;
  4397. int quoted;
  4398. {
  4399. pid_t pid, old_pid, old_pipeline_pgrp, old_async_pid;
  4400. char *istring;
  4401. int result, fildes[2], function_value, pflags, rc, tflag;
  4402. WORD_DESC *ret;
  4403. istring = (char *)NULL;
  4404. /* Don't fork () if there is no need to. In the case of no command to
  4405. run, just return NULL. */
  4406. if (!string || !*string || (string[0] == '\n' && !string[1]))
  4407. return ((WORD_DESC *)NULL);
  4408. if (wordexp_only && read_but_dont_execute)
  4409. {
  4410. last_command_exit_value = EX_WEXPCOMSUB;
  4411. jump_to_top_level (EXITPROG);
  4412. }
  4413. /* We're making the assumption here that the command substitution will
  4414. eventually run a command from the file system. Since we'll run
  4415. maybe_make_export_env in this subshell before executing that command,
  4416. the parent shell and any other shells it starts will have to remake
  4417. the environment. If we make it before we fork, other shells won't
  4418. have to. Don't bother if we have any temporary variable assignments,
  4419. though, because the export environment will be remade after this
  4420. command completes anyway, but do it if all the words to be expanded
  4421. are variable assignments. */
  4422. if (subst_assign_varlist == 0 || garglist == 0)
  4423. maybe_make_export_env (); /* XXX */
  4424. /* Flags to pass to parse_and_execute() */
  4425. pflags = (interactive && sourcelevel == 0) ? SEVAL_RESETLINE : 0;
  4426. /* Pipe the output of executing STRING into the current shell. */
  4427. if (pipe (fildes) < 0)
  4428. {
  4429. sys_error (_("cannot make pipe for command substitution"));
  4430. goto error_exit;
  4431. }
  4432. old_pid = last_made_pid;
  4433. #if defined (JOB_CONTROL)
  4434. old_pipeline_pgrp = pipeline_pgrp;
  4435. /* Don't reset the pipeline pgrp if we're already a subshell in a pipeline. */
  4436. if ((subshell_environment & SUBSHELL_PIPE) == 0)
  4437. pipeline_pgrp = shell_pgrp;
  4438. cleanup_the_pipeline ();
  4439. #endif /* JOB_CONTROL */
  4440. old_async_pid = last_asynchronous_pid;
  4441. pid = make_child ((char *)NULL, subshell_environment&SUBSHELL_ASYNC);
  4442. last_asynchronous_pid = old_async_pid;
  4443. if (pid == 0)
  4444. /* Reset the signal handlers in the child, but don't free the
  4445. trap strings. */
  4446. reset_signal_handlers ();
  4447. #if defined (JOB_CONTROL)
  4448. /* XXX DO THIS ONLY IN PARENT ? XXX */
  4449. set_sigchld_handler ();
  4450. stop_making_children ();
  4451. if (pid != 0)
  4452. pipeline_pgrp = old_pipeline_pgrp;
  4453. #else
  4454. stop_making_children ();
  4455. #endif /* JOB_CONTROL */
  4456. if (pid < 0)
  4457. {
  4458. sys_error (_("cannot make child for command substitution"));
  4459. error_exit:
  4460. FREE (istring);
  4461. close (fildes[0]);
  4462. close (fildes[1]);
  4463. return ((WORD_DESC *)NULL);
  4464. }
  4465. if (pid == 0)
  4466. {
  4467. set_sigint_handler (); /* XXX */
  4468. free_pushed_string_input ();
  4469. if (dup2 (fildes[1], 1) < 0)
  4470. {
  4471. sys_error (_("command_substitute: cannot duplicate pipe as fd 1"));
  4472. exit (EXECUTION_FAILURE);
  4473. }
  4474. /* If standard output is closed in the parent shell
  4475. (such as after `exec >&-'), file descriptor 1 will be
  4476. the lowest available file descriptor, and end up in
  4477. fildes[0]. This can happen for stdin and stderr as well,
  4478. but stdout is more important -- it will cause no output
  4479. to be generated from this command. */
  4480. if ((fildes[1] != fileno (stdin)) &&
  4481. (fildes[1] != fileno (stdout)) &&
  4482. (fildes[1] != fileno (stderr)))
  4483. close (fildes[1]);
  4484. if ((fildes[0] != fileno (stdin)) &&
  4485. (fildes[0] != fileno (stdout)) &&
  4486. (fildes[0] != fileno (stderr)))
  4487. close (fildes[0]);
  4488. /* The currently executing shell is not interactive. */
  4489. interactive = 0;
  4490. /* This is a subshell environment. */
  4491. subshell_environment |= SUBSHELL_COMSUB;
  4492. /* When not in POSIX mode, command substitution does not inherit
  4493. the -e flag. */
  4494. if (posixly_correct == 0)
  4495. exit_immediately_on_error = 0;
  4496. remove_quoted_escapes (string);
  4497. startup_state = 2; /* see if we can avoid a fork */
  4498. /* Give command substitution a place to jump back to on failure,
  4499. so we don't go back up to main (). */
  4500. result = setjmp (top_level);
  4501. /* If we're running a command substitution inside a shell function,
  4502. trap `return' so we don't return from the function in the subshell
  4503. and go off to never-never land. */
  4504. if (result == 0 && return_catch_flag)
  4505. function_value = setjmp (return_catch);
  4506. else
  4507. function_value = 0;
  4508. if (result == ERREXIT)
  4509. rc = last_command_exit_value;
  4510. else if (result == EXITPROG)
  4511. rc = last_command_exit_value;
  4512. else if (result)
  4513. rc = EXECUTION_FAILURE;
  4514. else if (function_value)
  4515. rc = return_catch_value;
  4516. else
  4517. {
  4518. subshell_level++;
  4519. rc = parse_and_execute (string, "command substitution", pflags|SEVAL_NOHIST);
  4520. subshell_level--;
  4521. }
  4522. last_command_exit_value = rc;
  4523. rc = run_exit_trap ();
  4524. #if defined (PROCESS_SUBSTITUTION)
  4525. unlink_fifo_list ();
  4526. #endif
  4527. exit (rc);
  4528. }
  4529. else
  4530. {
  4531. #if defined (JOB_CONTROL) && defined (PGRP_PIPE)
  4532. close_pgrp_pipe ();
  4533. #endif /* JOB_CONTROL && PGRP_PIPE */
  4534. close (fildes[1]);
  4535. tflag = 0;
  4536. istring = read_comsub (fildes[0], quoted, &tflag);
  4537. close (fildes[0]);
  4538. current_command_subst_pid = pid;
  4539. last_command_exit_value = wait_for (pid);
  4540. last_command_subst_pid = pid;
  4541. last_made_pid = old_pid;
  4542. #if defined (JOB_CONTROL)
  4543. /* If last_command_exit_value > 128, then the substituted command
  4544. was terminated by a signal. If that signal was SIGINT, then send
  4545. SIGINT to ourselves. This will break out of loops, for instance. */
  4546. if (last_command_exit_value == (128 + SIGINT) && last_command_exit_signal == SIGINT)
  4547. kill (getpid (), SIGINT);
  4548. /* wait_for gives the terminal back to shell_pgrp. If some other
  4549. process group should have it, give it away to that group here.
  4550. pipeline_pgrp is non-zero only while we are constructing a
  4551. pipline, so what we are concerned about is whether or not that
  4552. pipeline was started in the background. A pipeline started in
  4553. the background should never get the tty back here. */
  4554. #if 0
  4555. if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid)
  4556. #else
  4557. if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0)
  4558. #endif
  4559. give_terminal_to (pipeline_pgrp, 0);
  4560. #endif /* JOB_CONTROL */
  4561. ret = alloc_word_desc ();
  4562. ret->word = istring;
  4563. ret->flags = tflag;
  4564. return ret;
  4565. }
  4566. }
  4567. /********************************************************
  4568. * *
  4569. * Utility functions for parameter expansion *
  4570. * *
  4571. ********************************************************/
  4572. #if defined (ARRAY_VARS)
  4573. static arrayind_t
  4574. array_length_reference (s)
  4575. char *s;
  4576. {
  4577. int len;
  4578. arrayind_t ind;
  4579. char *akey;
  4580. char *t, c;
  4581. ARRAY *array;
  4582. SHELL_VAR *var;
  4583. var = array_variable_part (s, &t, &len);
  4584. /* If unbound variables should generate an error, report one and return
  4585. failure. */
  4586. if ((var == 0 || (assoc_p (var) == 0 && array_p (var) == 0)) && unbound_vars_is_error)
  4587. {
  4588. c = *--t;
  4589. *t = '\0';
  4590. last_command_exit_value = EXECUTION_FAILURE;
  4591. err_unboundvar (s);
  4592. *t = c;
  4593. return (-1);
  4594. }
  4595. else if (var == 0)
  4596. return 0;
  4597. /* We support a couple of expansions for variables that are not arrays.
  4598. We'll return the length of the value for v[0], and 1 for v[@] or
  4599. v[*]. Return 0 for everything else. */
  4600. array = array_p (var) ? array_cell (var) : (ARRAY *)NULL;
  4601. if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
  4602. {
  4603. if (assoc_p (var))
  4604. return (assoc_num_elements (assoc_cell (var)));
  4605. else if (array_p (var))
  4606. return (array_num_elements (array));
  4607. else
  4608. return 1;
  4609. }
  4610. if (assoc_p (var))
  4611. {
  4612. t[len - 1] = '\0';
  4613. akey = expand_assignment_string_to_string (t, 0); /* [ */
  4614. t[len - 1] = ']';
  4615. if (akey == 0 || *akey == 0)
  4616. {
  4617. err_badarraysub (t);
  4618. return (-1);
  4619. }
  4620. t = assoc_reference (assoc_cell (var), akey);
  4621. }
  4622. else
  4623. {
  4624. ind = array_expand_index (t, len);
  4625. if (ind < 0)
  4626. {
  4627. err_badarraysub (t);
  4628. return (-1);
  4629. }
  4630. if (array_p (var))
  4631. t = array_reference (array, ind);
  4632. else
  4633. t = (ind == 0) ? value_cell (var) : (char *)NULL;
  4634. }
  4635. len = MB_STRLEN (t);
  4636. return (len);
  4637. }
  4638. #endif /* ARRAY_VARS */
  4639. static int
  4640. valid_brace_expansion_word (name, var_is_special)
  4641. char *name;
  4642. int var_is_special;
  4643. {
  4644. if (DIGIT (*name) && all_digits (name))
  4645. return 1;
  4646. else if (var_is_special)
  4647. return 1;
  4648. #if defined (ARRAY_VARS)
  4649. else if (valid_array_reference (name))
  4650. return 1;
  4651. #endif /* ARRAY_VARS */
  4652. else if (legal_identifier (name))
  4653. return 1;
  4654. else
  4655. return 0;
  4656. }
  4657. static int
  4658. chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at)
  4659. char *name;
  4660. int quoted;
  4661. int *quoted_dollar_atp, *contains_dollar_at;
  4662. {
  4663. char *temp1;
  4664. if (name == 0)
  4665. {
  4666. if (quoted_dollar_atp)
  4667. *quoted_dollar_atp = 0;
  4668. if (contains_dollar_at)
  4669. *contains_dollar_at = 0;
  4670. return 0;
  4671. }
  4672. /* check for $@ and $* */
  4673. if (name[0] == '@' && name[1] == 0)
  4674. {
  4675. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
  4676. *quoted_dollar_atp = 1;
  4677. if (contains_dollar_at)
  4678. *contains_dollar_at = 1;
  4679. return 1;
  4680. }
  4681. else if (name[0] == '*' && name[1] == '\0' && quoted == 0)
  4682. {
  4683. if (contains_dollar_at)
  4684. *contains_dollar_at = 1;
  4685. return 1;
  4686. }
  4687. /* Now check for ${array[@]} and ${array[*]} */
  4688. #if defined (ARRAY_VARS)
  4689. else if (valid_array_reference (name))
  4690. {
  4691. temp1 = mbschr (name, '[');
  4692. if (temp1 && temp1[1] == '@' && temp1[2] == ']')
  4693. {
  4694. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
  4695. *quoted_dollar_atp = 1;
  4696. if (contains_dollar_at)
  4697. *contains_dollar_at = 1;
  4698. return 1;
  4699. } /* [ */
  4700. /* ${array[*]}, when unquoted, should be treated like ${array[@]},
  4701. which should result in separate words even when IFS is unset. */
  4702. if (temp1 && temp1[1] == '*' && temp1[2] == ']' && quoted == 0)
  4703. {
  4704. if (contains_dollar_at)
  4705. *contains_dollar_at = 1;
  4706. return 1;
  4707. }
  4708. }
  4709. #endif
  4710. return 0;
  4711. }
  4712. /* Parameter expand NAME, and return a new string which is the expansion,
  4713. or NULL if there was no expansion.
  4714. VAR_IS_SPECIAL is non-zero if NAME is one of the special variables in
  4715. the shell, e.g., "@", "$", "*", etc. QUOTED, if non-zero, means that
  4716. NAME was found inside of a double-quoted expression. */
  4717. static WORD_DESC *
  4718. parameter_brace_expand_word (name, var_is_special, quoted, pflags)
  4719. char *name;
  4720. int var_is_special, quoted, pflags;
  4721. {
  4722. WORD_DESC *ret;
  4723. char *temp, *tt;
  4724. intmax_t arg_index;
  4725. SHELL_VAR *var;
  4726. int atype, rflags;
  4727. ret = 0;
  4728. temp = 0;
  4729. rflags = 0;
  4730. /* Handle multiple digit arguments, as in ${11}. */
  4731. if (legal_number (name, &arg_index))
  4732. {
  4733. tt = get_dollar_var_value (arg_index);
  4734. if (tt)
  4735. temp = (*tt && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
  4736. ? quote_string (tt)
  4737. : quote_escapes (tt);
  4738. else
  4739. temp = (char *)NULL;
  4740. FREE (tt);
  4741. }
  4742. else if (var_is_special) /* ${@} */
  4743. {
  4744. int sindex;
  4745. tt = (char *)xmalloc (2 + strlen (name));
  4746. tt[sindex = 0] = '$';
  4747. strcpy (tt + 1, name);
  4748. ret = param_expand (tt, &sindex, quoted, (int *)NULL, (int *)NULL,
  4749. (int *)NULL, (int *)NULL, pflags);
  4750. free (tt);
  4751. }
  4752. #if defined (ARRAY_VARS)
  4753. else if (valid_array_reference (name))
  4754. {
  4755. temp = array_value (name, quoted, &atype);
  4756. if (atype == 0 && temp)
  4757. temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
  4758. ? quote_string (temp)
  4759. : quote_escapes (temp);
  4760. else if (atype == 1 && temp && QUOTED_NULL (temp) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
  4761. rflags |= W_HASQUOTEDNULL;
  4762. }
  4763. #endif
  4764. else if (var = find_variable (name))
  4765. {
  4766. if (var_isset (var) && invisible_p (var) == 0)
  4767. {
  4768. #if defined (ARRAY_VARS)
  4769. if (assoc_p (var))
  4770. temp = assoc_reference (assoc_cell (var), "0");
  4771. else if (array_p (var))
  4772. temp = array_reference (array_cell (var), 0);
  4773. else
  4774. temp = value_cell (var);
  4775. #else
  4776. temp = value_cell (var);
  4777. #endif
  4778. if (temp)
  4779. temp = (*temp && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
  4780. ? quote_string (temp)
  4781. : quote_escapes (temp);
  4782. }
  4783. else
  4784. temp = (char *)NULL;
  4785. }
  4786. else
  4787. temp = (char *)NULL;
  4788. if (ret == 0)
  4789. {
  4790. ret = alloc_word_desc ();
  4791. ret->word = temp;
  4792. ret->flags |= rflags;
  4793. }
  4794. return ret;
  4795. }
  4796. /* Expand an indirect reference to a variable: ${!NAME} expands to the
  4797. value of the variable whose name is the value of NAME. */
  4798. static WORD_DESC *
  4799. parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at)
  4800. char *name;
  4801. int var_is_special, quoted;
  4802. int *quoted_dollar_atp, *contains_dollar_at;
  4803. {
  4804. char *temp, *t;
  4805. WORD_DESC *w;
  4806. w = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND);
  4807. t = w->word;
  4808. /* Have to dequote here if necessary */
  4809. if (t)
  4810. {
  4811. temp = (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
  4812. ? dequote_string (t)
  4813. : dequote_escapes (t);
  4814. free (t);
  4815. t = temp;
  4816. }
  4817. dispose_word_desc (w);
  4818. chk_atstar (t, quoted, quoted_dollar_atp, contains_dollar_at);
  4819. if (t == 0)
  4820. return (WORD_DESC *)NULL;
  4821. w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0);
  4822. free (t);
  4823. return w;
  4824. }
  4825. /* Expand the right side of a parameter expansion of the form ${NAMEcVALUE},
  4826. depending on the value of C, the separating character. C can be one of
  4827. "-", "+", or "=". QUOTED is true if the entire brace expression occurs
  4828. between double quotes. */
  4829. static WORD_DESC *
  4830. parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat)
  4831. char *name, *value;
  4832. int c, quoted, *qdollaratp, *hasdollarat;
  4833. {
  4834. WORD_DESC *w;
  4835. WORD_LIST *l;
  4836. char *t, *t1, *temp;
  4837. int hasdol;
  4838. /* If the entire expression is between double quotes, we want to treat
  4839. the value as a double-quoted string, with the exception that we strip
  4840. embedded unescaped double quotes (for sh backwards compatibility). */
  4841. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && *value)
  4842. {
  4843. hasdol = 0;
  4844. temp = string_extract_double_quoted (value, &hasdol, 1);
  4845. }
  4846. else
  4847. temp = value;
  4848. w = alloc_word_desc ();
  4849. hasdol = 0;
  4850. /* XXX was 0 not quoted */
  4851. l = *temp ? expand_string_for_rhs (temp, quoted, &hasdol, (int *)NULL)
  4852. : (WORD_LIST *)0;
  4853. if (hasdollarat)
  4854. *hasdollarat = hasdol || (l && l->next);
  4855. if (temp != value)
  4856. free (temp);
  4857. if (l)
  4858. {
  4859. /* The expansion of TEMP returned something. We need to treat things
  4860. slightly differently if HASDOL is non-zero. If we have "$@", the
  4861. individual words have already been quoted. We need to turn them
  4862. into a string with the words separated by the first character of
  4863. $IFS without any additional quoting, so string_list_dollar_at won't
  4864. do the right thing. We use string_list_dollar_star instead. */
  4865. temp = (hasdol || l->next) ? string_list_dollar_star (l) : string_list (l);
  4866. /* If l->next is not null, we know that TEMP contained "$@", since that
  4867. is the only expansion that creates more than one word. */
  4868. if (qdollaratp && ((hasdol && quoted) || l->next))
  4869. *qdollaratp = 1;
  4870. dispose_words (l);
  4871. }
  4872. else if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && hasdol)
  4873. {
  4874. /* The brace expansion occurred between double quotes and there was
  4875. a $@ in TEMP. It does not matter if the $@ is quoted, as long as
  4876. it does not expand to anything. In this case, we want to return
  4877. a quoted empty string. */
  4878. temp = make_quoted_char ('\0');
  4879. w->flags |= W_HASQUOTEDNULL;
  4880. }
  4881. else
  4882. temp = (char *)NULL;
  4883. if (c == '-' || c == '+')
  4884. {
  4885. w->word = temp;
  4886. return w;
  4887. }
  4888. /* c == '=' */
  4889. t = temp ? savestring (temp) : savestring ("");
  4890. t1 = dequote_string (t);
  4891. free (t);
  4892. #if defined (ARRAY_VARS)
  4893. if (valid_array_reference (name))
  4894. assign_array_element (name, t1, 0);
  4895. else
  4896. #endif /* ARRAY_VARS */
  4897. bind_variable (name, t1, 0);
  4898. free (t1);
  4899. w->word = temp;
  4900. return w;
  4901. }
  4902. /* Deal with the right hand side of a ${name:?value} expansion in the case
  4903. that NAME is null or not set. If VALUE is non-null it is expanded and
  4904. used as the error message to print, otherwise a standard message is
  4905. printed. */
  4906. static void
  4907. parameter_brace_expand_error (name, value)
  4908. char *name, *value;
  4909. {
  4910. WORD_LIST *l;
  4911. char *temp;
  4912. if (value && *value)
  4913. {
  4914. l = expand_string (value, 0);
  4915. temp = string_list (l);
  4916. report_error ("%s: %s", name, temp ? temp : ""); /* XXX was value not "" */
  4917. FREE (temp);
  4918. dispose_words (l);
  4919. }
  4920. else
  4921. report_error (_("%s: parameter null or not set"), name);
  4922. /* Free the data we have allocated during this expansion, since we
  4923. are about to longjmp out. */
  4924. free (name);
  4925. FREE (value);
  4926. }
  4927. /* Return 1 if NAME is something for which parameter_brace_expand_length is
  4928. OK to do. */
  4929. static int
  4930. valid_length_expression (name)
  4931. char *name;
  4932. {
  4933. return (name[1] == '\0' || /* ${#} */
  4934. ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0') || /* special param */
  4935. (DIGIT (name[1]) && all_digits (name + 1)) || /* ${#11} */
  4936. #if defined (ARRAY_VARS)
  4937. valid_array_reference (name + 1) || /* ${#a[7]} */
  4938. #endif
  4939. legal_identifier (name + 1)); /* ${#PS1} */
  4940. }
  4941. #if defined (HANDLE_MULTIBYTE)
  4942. size_t
  4943. mbstrlen (s)
  4944. const char *s;
  4945. {
  4946. size_t clen, nc;
  4947. mbstate_t mbs, mbsbak;
  4948. nc = 0;
  4949. memset (&mbs, 0, sizeof (mbs));
  4950. mbsbak = mbs;
  4951. while ((clen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0)
  4952. {
  4953. if (MB_INVALIDCH(clen))
  4954. {
  4955. clen = 1; /* assume single byte */
  4956. mbs = mbsbak;
  4957. }
  4958. s += clen;
  4959. nc++;
  4960. mbsbak = mbs;
  4961. }
  4962. return nc;
  4963. }
  4964. #endif
  4965. /* Handle the parameter brace expansion that requires us to return the
  4966. length of a parameter. */
  4967. static intmax_t
  4968. parameter_brace_expand_length (name)
  4969. char *name;
  4970. {
  4971. char *t, *newname;
  4972. intmax_t number, arg_index;
  4973. WORD_LIST *list;
  4974. #if defined (ARRAY_VARS)
  4975. SHELL_VAR *var;
  4976. #endif
  4977. if (name[1] == '\0') /* ${#} */
  4978. number = number_of_args ();
  4979. else if ((name[1] == '@' || name[1] == '*') && name[2] == '\0') /* ${#@}, ${#*} */
  4980. number = number_of_args ();
  4981. else if ((sh_syntaxtab[(unsigned char) name[1]] & CSPECVAR) && name[2] == '\0')
  4982. {
  4983. /* Take the lengths of some of the shell's special parameters. */
  4984. switch (name[1])
  4985. {
  4986. case '-':
  4987. t = which_set_flags ();
  4988. break;
  4989. case '?':
  4990. t = itos (last_command_exit_value);
  4991. break;
  4992. case '$':
  4993. t = itos (dollar_dollar_pid);
  4994. break;
  4995. case '!':
  4996. if (last_asynchronous_pid == NO_PID)
  4997. t = (char *)NULL;
  4998. else
  4999. t = itos (last_asynchronous_pid);
  5000. break;
  5001. case '#':
  5002. t = itos (number_of_args ());
  5003. break;
  5004. }
  5005. number = STRLEN (t);
  5006. FREE (t);
  5007. }
  5008. #if defined (ARRAY_VARS)
  5009. else if (valid_array_reference (name + 1))
  5010. number = array_length_reference (name + 1);
  5011. #endif /* ARRAY_VARS */
  5012. else
  5013. {
  5014. number = 0;
  5015. if (legal_number (name + 1, &arg_index)) /* ${#1} */
  5016. {
  5017. t = get_dollar_var_value (arg_index);
  5018. number = MB_STRLEN (t);
  5019. FREE (t);
  5020. }
  5021. #if defined (ARRAY_VARS)
  5022. else if ((var = find_variable (name + 1)) && (invisible_p (var) == 0) && (array_p (var) || assoc_p (var)))
  5023. {
  5024. if (assoc_p (var))
  5025. t = assoc_reference (assoc_cell (var), "0");
  5026. else
  5027. t = array_reference (array_cell (var), 0);
  5028. number = MB_STRLEN (t);
  5029. }
  5030. #endif
  5031. else /* ${#PS1} */
  5032. {
  5033. newname = savestring (name);
  5034. newname[0] = '$';
  5035. list = expand_string (newname, Q_DOUBLE_QUOTES);
  5036. t = list ? string_list (list) : (char *)NULL;
  5037. free (newname);
  5038. if (list)
  5039. dispose_words (list);
  5040. number = MB_STRLEN (t);
  5041. FREE (t);
  5042. }
  5043. }
  5044. return (number);
  5045. }
  5046. /* Skip characters in SUBSTR until DELIM. SUBSTR is an arithmetic expression,
  5047. so we do some ad-hoc parsing of an arithmetic expression to find
  5048. the first DELIM, instead of using strchr(3). Two rules:
  5049. 1. If the substring contains a `(', read until closing `)'.
  5050. 2. If the substring contains a `?', read past one `:' for each `?'.
  5051. */
  5052. static char *
  5053. skiparith (substr, delim)
  5054. char *substr;
  5055. int delim;
  5056. {
  5057. size_t sublen;
  5058. int skipcol, pcount, i;
  5059. DECLARE_MBSTATE;
  5060. sublen = strlen (substr);
  5061. i = skipcol = pcount = 0;
  5062. while (substr[i])
  5063. {
  5064. /* Balance parens */
  5065. if (substr[i] == LPAREN)
  5066. {
  5067. pcount++;
  5068. i++;
  5069. continue;
  5070. }
  5071. if (substr[i] == RPAREN && pcount)
  5072. {
  5073. pcount--;
  5074. i++;
  5075. continue;
  5076. }
  5077. if (pcount)
  5078. {
  5079. ADVANCE_CHAR (substr, sublen, i);
  5080. continue;
  5081. }
  5082. /* Skip one `:' for each `?' */
  5083. if (substr[i] == ':' && skipcol)
  5084. {
  5085. skipcol--;
  5086. i++;
  5087. continue;
  5088. }
  5089. if (substr[i] == delim)
  5090. break;
  5091. if (substr[i] == '?')
  5092. {
  5093. skipcol++;
  5094. i++;
  5095. continue;
  5096. }
  5097. ADVANCE_CHAR (substr, sublen, i);
  5098. }
  5099. return (substr + i);
  5100. }
  5101. /* Verify and limit the start and end of the desired substring. If
  5102. VTYPE == 0, a regular shell variable is being used; if it is 1,
  5103. then the positional parameters are being used; if it is 2, then
  5104. VALUE is really a pointer to an array variable that should be used.
  5105. Return value is 1 if both values were OK, 0 if there was a problem
  5106. with an invalid expression, or -1 if the values were out of range. */
  5107. static int
  5108. verify_substring_values (v, value, substr, vtype, e1p, e2p)
  5109. SHELL_VAR *v;
  5110. char *value, *substr;
  5111. int vtype;
  5112. intmax_t *e1p, *e2p;
  5113. {
  5114. char *t, *temp1, *temp2;
  5115. arrayind_t len;
  5116. int expok;
  5117. #if defined (ARRAY_VARS)
  5118. ARRAY *a;
  5119. HASH_TABLE *h;
  5120. #endif
  5121. /* duplicate behavior of strchr(3) */
  5122. t = skiparith (substr, ':');
  5123. if (*t && *t == ':')
  5124. *t = '\0';
  5125. else
  5126. t = (char *)0;
  5127. temp1 = expand_arith_string (substr, Q_DOUBLE_QUOTES);
  5128. *e1p = evalexp (temp1, &expok);
  5129. free (temp1);
  5130. if (expok == 0)
  5131. return (0);
  5132. len = -1; /* paranoia */
  5133. switch (vtype)
  5134. {
  5135. case VT_VARIABLE:
  5136. case VT_ARRAYMEMBER:
  5137. len = MB_STRLEN (value);
  5138. break;
  5139. case VT_POSPARMS:
  5140. len = number_of_args () + 1;
  5141. if (*e1p == 0)
  5142. len++; /* add one arg if counting from $0 */
  5143. break;
  5144. #if defined (ARRAY_VARS)
  5145. case VT_ARRAYVAR:
  5146. /* For arrays, the first value deals with array indices. Negative
  5147. offsets count from one past the array's maximum index. Associative
  5148. arrays treat the number of elements as the maximum index. */
  5149. if (assoc_p (v))
  5150. {
  5151. h = assoc_cell (v);
  5152. len = assoc_num_elements (h) + (*e1p < 0);
  5153. }
  5154. else
  5155. {
  5156. a = (ARRAY *)value;
  5157. len = array_max_index (a) + (*e1p < 0); /* arrays index from 0 to n - 1 */
  5158. }
  5159. break;
  5160. #endif
  5161. }
  5162. if (len == -1) /* paranoia */
  5163. return -1;
  5164. if (*e1p < 0) /* negative offsets count from end */
  5165. *e1p += len;
  5166. if (*e1p > len || *e1p < 0)
  5167. return (-1);
  5168. #if defined (ARRAY_VARS)
  5169. /* For arrays, the second offset deals with the number of elements. */
  5170. if (vtype == VT_ARRAYVAR)
  5171. len = assoc_p (v) ? assoc_num_elements (h) : array_num_elements (a);
  5172. #endif
  5173. if (t)
  5174. {
  5175. t++;
  5176. temp2 = savestring (t);
  5177. temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
  5178. free (temp2);
  5179. t[-1] = ':';
  5180. *e2p = evalexp (temp1, &expok);
  5181. free (temp1);
  5182. if (expok == 0)
  5183. return (0);
  5184. if (*e2p < 0)
  5185. {
  5186. internal_error (_("%s: substring expression < 0"), t);
  5187. return (0);
  5188. }
  5189. #if defined (ARRAY_VARS)
  5190. /* In order to deal with sparse arrays, push the intelligence about how
  5191. to deal with the number of elements desired down to the array-
  5192. specific functions. */
  5193. if (vtype != VT_ARRAYVAR)
  5194. #endif
  5195. {
  5196. *e2p += *e1p; /* want E2 chars starting at E1 */
  5197. if (*e2p > len)
  5198. *e2p = len;
  5199. }
  5200. }
  5201. else
  5202. *e2p = len;
  5203. return (1);
  5204. }
  5205. /* Return the type of variable specified by VARNAME (simple variable,
  5206. positional param, or array variable). Also return the value specified
  5207. by VARNAME (value of a variable or a reference to an array element).
  5208. If this returns VT_VARIABLE, the caller assumes that CTLESC and CTLNUL
  5209. characters in the value are quoted with CTLESC and takes appropriate
  5210. steps. For convenience, *VALP is set to the dequoted VALUE. */
  5211. static int
  5212. get_var_and_type (varname, value, quoted, varp, valp)
  5213. char *varname, *value;
  5214. int quoted;
  5215. SHELL_VAR **varp;
  5216. char **valp;
  5217. {
  5218. int vtype;
  5219. char *temp;
  5220. #if defined (ARRAY_VARS)
  5221. SHELL_VAR *v;
  5222. #endif
  5223. /* This sets vtype to VT_VARIABLE or VT_POSPARMS */
  5224. vtype = (varname[0] == '@' || varname[0] == '*') && varname[1] == '\0';
  5225. if (vtype == VT_POSPARMS && varname[0] == '*')
  5226. vtype |= VT_STARSUB;
  5227. *varp = (SHELL_VAR *)NULL;
  5228. #if defined (ARRAY_VARS)
  5229. if (valid_array_reference (varname))
  5230. {
  5231. v = array_variable_part (varname, &temp, (int *)0);
  5232. if (v && (array_p (v) || assoc_p (v)))
  5233. { /* [ */
  5234. if (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']')
  5235. {
  5236. /* Callers have to differentiate betwen indexed and associative */
  5237. vtype = VT_ARRAYVAR;
  5238. if (temp[0] == '*')
  5239. vtype |= VT_STARSUB;
  5240. *valp = array_p (v) ? (char *)array_cell (v) : (char *)assoc_cell (v);
  5241. }
  5242. else
  5243. {
  5244. vtype = VT_ARRAYMEMBER;
  5245. *valp = array_value (varname, 1, (int *)NULL);
  5246. }
  5247. *varp = v;
  5248. }
  5249. else if (v && (ALL_ELEMENT_SUB (temp[0]) && temp[1] == ']'))
  5250. {
  5251. vtype = VT_VARIABLE;
  5252. *varp = v;
  5253. if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
  5254. *valp = dequote_string (value);
  5255. else
  5256. *valp = dequote_escapes (value);
  5257. }
  5258. else
  5259. {
  5260. vtype = VT_ARRAYMEMBER;
  5261. *varp = v;
  5262. *valp = array_value (varname, 1, (int *)NULL);
  5263. }
  5264. }
  5265. else if ((v = find_variable (varname)) && (invisible_p (v) == 0) && (assoc_p (v) || array_p (v)))
  5266. {
  5267. vtype = VT_ARRAYMEMBER;
  5268. *varp = v;
  5269. *valp = assoc_p (v) ? assoc_reference (assoc_cell (v), "0") : array_reference (array_cell (v), 0);
  5270. }
  5271. else
  5272. #endif
  5273. {
  5274. if (value && vtype == VT_VARIABLE)
  5275. {
  5276. if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
  5277. *valp = dequote_string (value);
  5278. else
  5279. *valp = dequote_escapes (value);
  5280. }
  5281. else
  5282. *valp = value;
  5283. }
  5284. return vtype;
  5285. }
  5286. /******************************************************/
  5287. /* */
  5288. /* Functions to extract substrings of variable values */
  5289. /* */
  5290. /******************************************************/
  5291. #if defined (HANDLE_MULTIBYTE)
  5292. /* Character-oriented rather than strictly byte-oriented substrings. S and
  5293. E, rather being strict indices into STRING, indicate character (possibly
  5294. multibyte character) positions that require calculation.
  5295. Used by the ${param:offset[:length]} expansion. */
  5296. static char *
  5297. mb_substring (string, s, e)
  5298. char *string;
  5299. int s, e;
  5300. {
  5301. char *tt;
  5302. int start, stop, i, slen;
  5303. DECLARE_MBSTATE;
  5304. start = 0;
  5305. /* Don't need string length in ADVANCE_CHAR unless multibyte chars possible. */
  5306. slen = (MB_CUR_MAX > 1) ? STRLEN (string) : 0;
  5307. i = s;
  5308. while (string[start] && i--)
  5309. ADVANCE_CHAR (string, slen, start);
  5310. stop = start;
  5311. i = e - s;
  5312. while (string[stop] && i--)
  5313. ADVANCE_CHAR (string, slen, stop);
  5314. tt = substring (string, start, stop);
  5315. return tt;
  5316. }
  5317. #endif
  5318. /* Process a variable substring expansion: ${name:e1[:e2]}. If VARNAME
  5319. is `@', use the positional parameters; otherwise, use the value of
  5320. VARNAME. If VARNAME is an array variable, use the array elements. */
  5321. static char *
  5322. parameter_brace_substring (varname, value, substr, quoted)
  5323. char *varname, *value, *substr;
  5324. int quoted;
  5325. {
  5326. intmax_t e1, e2;
  5327. int vtype, r, starsub;
  5328. char *temp, *val, *tt, *oname;
  5329. SHELL_VAR *v;
  5330. if (value == 0)
  5331. return ((char *)NULL);
  5332. oname = this_command_name;
  5333. this_command_name = varname;
  5334. vtype = get_var_and_type (varname, value, quoted, &v, &val);
  5335. if (vtype == -1)
  5336. {
  5337. this_command_name = oname;
  5338. return ((char *)NULL);
  5339. }
  5340. starsub = vtype & VT_STARSUB;
  5341. vtype &= ~VT_STARSUB;
  5342. r = verify_substring_values (v, val, substr, vtype, &e1, &e2);
  5343. this_command_name = oname;
  5344. if (r <= 0)
  5345. return ((r == 0) ? &expand_param_error : (char *)NULL);
  5346. switch (vtype)
  5347. {
  5348. case VT_VARIABLE:
  5349. case VT_ARRAYMEMBER:
  5350. #if defined (HANDLE_MULTIBYTE)
  5351. if (MB_CUR_MAX > 1)
  5352. tt = mb_substring (val, e1, e2);
  5353. else
  5354. #endif
  5355. tt = substring (val, e1, e2);
  5356. if (vtype == VT_VARIABLE)
  5357. FREE (val);
  5358. if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
  5359. temp = quote_string (tt);
  5360. else
  5361. temp = tt ? quote_escapes (tt) : (char *)NULL;
  5362. FREE (tt);
  5363. break;
  5364. case VT_POSPARMS:
  5365. tt = pos_params (varname, e1, e2, quoted);
  5366. if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
  5367. {
  5368. temp = tt ? quote_escapes (tt) : (char *)NULL;
  5369. FREE (tt);
  5370. }
  5371. else
  5372. temp = tt;
  5373. break;
  5374. #if defined (ARRAY_VARS)
  5375. case VT_ARRAYVAR:
  5376. if (assoc_p (v))
  5377. /* we convert to list and take first e2 elements starting at e1th
  5378. element -- officially undefined for now */
  5379. temp = assoc_subrange (assoc_cell (v), e1, e2, starsub, quoted);
  5380. else
  5381. /* We want E2 to be the number of elements desired (arrays can be sparse,
  5382. so verify_substring_values just returns the numbers specified and we
  5383. rely on array_subrange to understand how to deal with them). */
  5384. temp = array_subrange (array_cell (v), e1, e2, starsub, quoted);
  5385. /* array_subrange now calls array_quote_escapes as appropriate, so the
  5386. caller no longer needs to. */
  5387. break;
  5388. #endif
  5389. default:
  5390. temp = (char *)NULL;
  5391. }
  5392. return temp;
  5393. }
  5394. /****************************************************************/
  5395. /* */
  5396. /* Functions to perform pattern substitution on variable values */
  5397. /* */
  5398. /****************************************************************/
  5399. char *
  5400. pat_subst (string, pat, rep, mflags)
  5401. char *string, *pat, *rep;
  5402. int mflags;
  5403. {
  5404. char *ret, *s, *e, *str;
  5405. int rsize, rptr, l, replen, mtype;
  5406. mtype = mflags & MATCH_TYPEMASK;
  5407. /* Special cases:
  5408. * 1. A null pattern with mtype == MATCH_BEG means to prefix STRING
  5409. * with REP and return the result.
  5410. * 2. A null pattern with mtype == MATCH_END means to append REP to
  5411. * STRING and return the result.
  5412. */
  5413. if ((pat == 0 || *pat == 0) && (mtype == MATCH_BEG || mtype == MATCH_END))
  5414. {
  5415. replen = STRLEN (rep);
  5416. l = strlen (string);
  5417. ret = (char *)xmalloc (replen + l + 2);
  5418. if (replen == 0)
  5419. strcpy (ret, string);
  5420. else if (mtype == MATCH_BEG)
  5421. {
  5422. strcpy (ret, rep);
  5423. strcpy (ret + replen, string);
  5424. }
  5425. else
  5426. {
  5427. strcpy (ret, string);
  5428. strcpy (ret + l, rep);
  5429. }
  5430. return (ret);
  5431. }
  5432. ret = (char *)xmalloc (rsize = 64);
  5433. ret[0] = '\0';
  5434. for (replen = STRLEN (rep), rptr = 0, str = string;;)
  5435. {
  5436. if (match_pattern (str, pat, mtype, &s, &e) == 0)
  5437. break;
  5438. l = s - str;
  5439. RESIZE_MALLOCED_BUFFER (ret, rptr, (l + replen), rsize, 64);
  5440. /* OK, now copy the leading unmatched portion of the string (from
  5441. str to s) to ret starting at rptr (the current offset). Then copy
  5442. the replacement string at ret + rptr + (s - str). Increment
  5443. rptr (if necessary) and str and go on. */
  5444. if (l)
  5445. {
  5446. strncpy (ret + rptr, str, l);
  5447. rptr += l;
  5448. }
  5449. if (replen)
  5450. {
  5451. strncpy (ret + rptr, rep, replen);
  5452. rptr += replen;
  5453. }
  5454. str = e; /* e == end of match */
  5455. if (((mflags & MATCH_GLOBREP) == 0) || mtype != MATCH_ANY)
  5456. break;
  5457. if (s == e)
  5458. {
  5459. /* On a zero-length match, make sure we copy one character, since
  5460. we increment one character to avoid infinite recursion. */
  5461. RESIZE_MALLOCED_BUFFER (ret, rptr, 1, rsize, 64);
  5462. ret[rptr++] = *str++;
  5463. e++; /* avoid infinite recursion on zero-length match */
  5464. }
  5465. }
  5466. /* Now copy the unmatched portion of the input string */
  5467. if (*str)
  5468. {
  5469. RESIZE_MALLOCED_BUFFER (ret, rptr, STRLEN(str) + 1, rsize, 64);
  5470. strcpy (ret + rptr, str);
  5471. }
  5472. else
  5473. ret[rptr] = '\0';
  5474. return ret;
  5475. }
  5476. /* Do pattern match and replacement on the positional parameters. */
  5477. static char *
  5478. pos_params_pat_subst (string, pat, rep, mflags)
  5479. char *string, *pat, *rep;
  5480. int mflags;
  5481. {
  5482. WORD_LIST *save, *params;
  5483. WORD_DESC *w;
  5484. char *ret;
  5485. int pchar, qflags;
  5486. save = params = list_rest_of_args ();
  5487. if (save == 0)
  5488. return ((char *)NULL);
  5489. for ( ; params; params = params->next)
  5490. {
  5491. ret = pat_subst (params->word->word, pat, rep, mflags);
  5492. w = alloc_word_desc ();
  5493. w->word = ret ? ret : savestring ("");
  5494. dispose_word (params->word);
  5495. params->word = w;
  5496. }
  5497. pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
  5498. qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
  5499. #if 0
  5500. if ((mflags & (MATCH_QUOTED|MATCH_STARSUB)) == (MATCH_QUOTED|MATCH_STARSUB))
  5501. ret = string_list_dollar_star (quote_list (save));
  5502. else if ((mflags & MATCH_STARSUB) == MATCH_STARSUB)
  5503. ret = string_list_dollar_star (save);
  5504. else if ((mflags & MATCH_QUOTED) == MATCH_QUOTED)
  5505. ret = string_list_dollar_at (save, qflags);
  5506. else
  5507. ret = string_list_dollar_star (save);
  5508. #else
  5509. ret = string_list_pos_params (pchar, save, qflags);
  5510. #endif
  5511. dispose_words (save);
  5512. return (ret);
  5513. }
  5514. /* Perform pattern substitution on VALUE, which is the expansion of
  5515. VARNAME. PATSUB is an expression supplying the pattern to match
  5516. and the string to substitute. QUOTED is a flags word containing
  5517. the type of quoting currently in effect. */
  5518. static char *
  5519. parameter_brace_patsub (varname, value, patsub, quoted)
  5520. char *varname, *value, *patsub;
  5521. int quoted;
  5522. {
  5523. int vtype, mflags, starsub, delim;
  5524. char *val, *temp, *pat, *rep, *p, *lpatsub, *tt;
  5525. SHELL_VAR *v;
  5526. if (value == 0)
  5527. return ((char *)NULL);
  5528. this_command_name = varname;
  5529. vtype = get_var_and_type (varname, value, quoted, &v, &val);
  5530. if (vtype == -1)
  5531. return ((char *)NULL);
  5532. starsub = vtype & VT_STARSUB;
  5533. vtype &= ~VT_STARSUB;
  5534. mflags = 0;
  5535. if (patsub && *patsub == '/')
  5536. {
  5537. mflags |= MATCH_GLOBREP;
  5538. patsub++;
  5539. }
  5540. /* Malloc this because expand_string_if_necessary or one of the expansion
  5541. functions in its call chain may free it on a substitution error. */
  5542. lpatsub = savestring (patsub);
  5543. if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  5544. mflags |= MATCH_QUOTED;
  5545. if (starsub)
  5546. mflags |= MATCH_STARSUB;
  5547. /* If the pattern starts with a `/', make sure we skip over it when looking
  5548. for the replacement delimiter. */
  5549. #if 0
  5550. if (rep = quoted_strchr ((*patsub == '/') ? lpatsub+1 : lpatsub, '/', ST_BACKSL))
  5551. *rep++ = '\0';
  5552. else
  5553. rep = (char *)NULL;
  5554. #else
  5555. delim = skip_to_delim (lpatsub, ((*patsub == '/') ? 1 : 0), "/", 0);
  5556. if (lpatsub[delim] == '/')
  5557. {
  5558. lpatsub[delim] = 0;
  5559. rep = lpatsub + delim + 1;
  5560. }
  5561. else
  5562. rep = (char *)NULL;
  5563. #endif
  5564. if (rep && *rep == '\0')
  5565. rep = (char *)NULL;
  5566. /* Perform the same expansions on the pattern as performed by the
  5567. pattern removal expansions. */
  5568. pat = getpattern (lpatsub, quoted, 1);
  5569. if (rep)
  5570. {
  5571. if ((mflags & MATCH_QUOTED) == 0)
  5572. rep = expand_string_if_necessary (rep, quoted, expand_string_unsplit);
  5573. else
  5574. rep = expand_string_to_string_internal (rep, quoted, expand_string_unsplit);
  5575. }
  5576. /* ksh93 doesn't allow the match specifier to be a part of the expanded
  5577. pattern. This is an extension. Make sure we don't anchor the pattern
  5578. at the beginning or end of the string if we're doing global replacement,
  5579. though. */
  5580. p = pat;
  5581. if (mflags & MATCH_GLOBREP)
  5582. mflags |= MATCH_ANY;
  5583. else if (pat && pat[0] == '#')
  5584. {
  5585. mflags |= MATCH_BEG;
  5586. p++;
  5587. }
  5588. else if (pat && pat[0] == '%')
  5589. {
  5590. mflags |= MATCH_END;
  5591. p++;
  5592. }
  5593. else
  5594. mflags |= MATCH_ANY;
  5595. /* OK, we now want to substitute REP for PAT in VAL. If
  5596. flags & MATCH_GLOBREP is non-zero, the substitution is done
  5597. everywhere, otherwise only the first occurrence of PAT is
  5598. replaced. The pattern matching code doesn't understand
  5599. CTLESC quoting CTLESC and CTLNUL so we use the dequoted variable
  5600. values passed in (VT_VARIABLE) so the pattern substitution
  5601. code works right. We need to requote special chars after
  5602. we're done for VT_VARIABLE and VT_ARRAYMEMBER, and for the
  5603. other cases if QUOTED == 0, since the posparams and arrays
  5604. indexed by * or @ do special things when QUOTED != 0. */
  5605. switch (vtype)
  5606. {
  5607. case VT_VARIABLE:
  5608. case VT_ARRAYMEMBER:
  5609. temp = pat_subst (val, p, rep, mflags);
  5610. if (vtype == VT_VARIABLE)
  5611. FREE (val);
  5612. if (temp)
  5613. {
  5614. tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
  5615. free (temp);
  5616. temp = tt;
  5617. }
  5618. break;
  5619. case VT_POSPARMS:
  5620. temp = pos_params_pat_subst (val, p, rep, mflags);
  5621. if (temp && (mflags & MATCH_QUOTED) == 0)
  5622. {
  5623. tt = quote_escapes (temp);
  5624. free (temp);
  5625. temp = tt;
  5626. }
  5627. break;
  5628. #if defined (ARRAY_VARS)
  5629. case VT_ARRAYVAR:
  5630. temp = assoc_p (v) ? assoc_patsub (assoc_cell (v), p, rep, mflags)
  5631. : array_patsub (array_cell (v), p, rep, mflags);
  5632. /* Don't call quote_escapes anymore; array_patsub calls
  5633. array_quote_escapes as appropriate before adding the
  5634. space separators; ditto for assoc_patsub. */
  5635. break;
  5636. #endif
  5637. }
  5638. FREE (pat);
  5639. FREE (rep);
  5640. free (lpatsub);
  5641. return temp;
  5642. }
  5643. /****************************************************************/
  5644. /* */
  5645. /* Functions to perform case modification on variable values */
  5646. /* */
  5647. /****************************************************************/
  5648. /* Do case modification on the positional parameters. */
  5649. static char *
  5650. pos_params_modcase (string, pat, modop, mflags)
  5651. char *string, *pat;
  5652. int modop;
  5653. int mflags;
  5654. {
  5655. WORD_LIST *save, *params;
  5656. WORD_DESC *w;
  5657. char *ret;
  5658. int pchar, qflags;
  5659. save = params = list_rest_of_args ();
  5660. if (save == 0)
  5661. return ((char *)NULL);
  5662. for ( ; params; params = params->next)
  5663. {
  5664. ret = sh_modcase (params->word->word, pat, modop);
  5665. w = alloc_word_desc ();
  5666. w->word = ret ? ret : savestring ("");
  5667. dispose_word (params->word);
  5668. params->word = w;
  5669. }
  5670. pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
  5671. qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
  5672. ret = string_list_pos_params (pchar, save, qflags);
  5673. dispose_words (save);
  5674. return (ret);
  5675. }
  5676. /* Perform case modification on VALUE, which is the expansion of
  5677. VARNAME. MODSPEC is an expression supplying the type of modification
  5678. to perform. QUOTED is a flags word containing the type of quoting
  5679. currently in effect. */
  5680. static char *
  5681. parameter_brace_casemod (varname, value, modspec, patspec, quoted)
  5682. char *varname, *value;
  5683. int modspec;
  5684. char *patspec;
  5685. int quoted;
  5686. {
  5687. int vtype, starsub, modop, mflags, x;
  5688. char *val, *temp, *pat, *p, *lpat, *tt;
  5689. SHELL_VAR *v;
  5690. if (value == 0)
  5691. return ((char *)NULL);
  5692. this_command_name = varname;
  5693. vtype = get_var_and_type (varname, value, quoted, &v, &val);
  5694. if (vtype == -1)
  5695. return ((char *)NULL);
  5696. starsub = vtype & VT_STARSUB;
  5697. vtype &= ~VT_STARSUB;
  5698. modop = 0;
  5699. mflags = 0;
  5700. if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  5701. mflags |= MATCH_QUOTED;
  5702. if (starsub)
  5703. mflags |= MATCH_STARSUB;
  5704. p = patspec;
  5705. if (modspec == '^')
  5706. {
  5707. x = p && p[0] == modspec;
  5708. modop = x ? CASE_UPPER : CASE_UPFIRST;
  5709. p += x;
  5710. }
  5711. else if (modspec == ',')
  5712. {
  5713. x = p && p[0] == modspec;
  5714. modop = x ? CASE_LOWER : CASE_LOWFIRST;
  5715. p += x;
  5716. }
  5717. else if (modspec == '~')
  5718. {
  5719. x = p && p[0] == modspec;
  5720. modop = x ? CASE_TOGGLEALL : CASE_TOGGLE;
  5721. p += x;
  5722. }
  5723. lpat = p ? savestring (p) : 0;
  5724. /* Perform the same expansions on the pattern as performed by the
  5725. pattern removal expansions. FOR LATER */
  5726. pat = lpat ? getpattern (lpat, quoted, 1) : 0;
  5727. /* OK, now we do the case modification. */
  5728. switch (vtype)
  5729. {
  5730. case VT_VARIABLE:
  5731. case VT_ARRAYMEMBER:
  5732. temp = sh_modcase (val, pat, modop);
  5733. if (vtype == VT_VARIABLE)
  5734. FREE (val);
  5735. if (temp)
  5736. {
  5737. tt = (mflags & MATCH_QUOTED) ? quote_string (temp) : quote_escapes (temp);
  5738. free (temp);
  5739. temp = tt;
  5740. }
  5741. break;
  5742. case VT_POSPARMS:
  5743. temp = pos_params_modcase (val, pat, modop, mflags);
  5744. if (temp && (mflags & MATCH_QUOTED) == 0)
  5745. {
  5746. tt = quote_escapes (temp);
  5747. free (temp);
  5748. temp = tt;
  5749. }
  5750. break;
  5751. #if defined (ARRAY_VARS)
  5752. case VT_ARRAYVAR:
  5753. temp = assoc_p (v) ? assoc_modcase (assoc_cell (v), pat, modop, mflags)
  5754. : array_modcase (array_cell (v), pat, modop, mflags);
  5755. /* Don't call quote_escapes; array_modcase calls array_quote_escapes
  5756. as appropriate before adding the space separators; ditto for
  5757. assoc_modcase. */
  5758. break;
  5759. #endif
  5760. }
  5761. FREE (pat);
  5762. free (lpat);
  5763. return temp;
  5764. }
  5765. /* Check for unbalanced parens in S, which is the contents of $(( ... )). If
  5766. any occur, this must be a nested command substitution, so return 0.
  5767. Otherwise, return 1. A valid arithmetic expression must always have a
  5768. ( before a matching ), so any cases where there are more right parens
  5769. means that this must not be an arithmetic expression, though the parser
  5770. will not accept it without a balanced total number of parens. */
  5771. static int
  5772. chk_arithsub (s, len)
  5773. const char *s;
  5774. int len;
  5775. {
  5776. int i, count;
  5777. DECLARE_MBSTATE;
  5778. i = count = 0;
  5779. while (i < len)
  5780. {
  5781. if (s[i] == LPAREN)
  5782. count++;
  5783. else if (s[i] == RPAREN)
  5784. {
  5785. count--;
  5786. if (count < 0)
  5787. return 0;
  5788. }
  5789. switch (s[i])
  5790. {
  5791. default:
  5792. ADVANCE_CHAR (s, len, i);
  5793. break;
  5794. case '\\':
  5795. i++;
  5796. if (s[i])
  5797. ADVANCE_CHAR (s, len, i);
  5798. break;
  5799. case '\'':
  5800. i = skip_single_quoted (s, len, ++i);
  5801. break;
  5802. case '"':
  5803. i = skip_double_quoted ((char *)s, len, ++i);
  5804. break;
  5805. }
  5806. }
  5807. return (count == 0);
  5808. }
  5809. /****************************************************************/
  5810. /* */
  5811. /* Functions to perform parameter expansion on a string */
  5812. /* */
  5813. /****************************************************************/
  5814. /* ${[#][!]name[[:][^[^]][,[,]]#[#]%[%]-=?+[word][:e1[:e2]]]} */
  5815. static WORD_DESC *
  5816. parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, contains_dollar_at)
  5817. char *string;
  5818. int *indexp, quoted, *quoted_dollar_atp, *contains_dollar_at, pflags;
  5819. {
  5820. int check_nullness, var_is_set, var_is_null, var_is_special;
  5821. int want_substring, want_indir, want_patsub, want_casemod;
  5822. char *name, *value, *temp, *temp1;
  5823. WORD_DESC *tdesc, *ret;
  5824. int t_index, sindex, c, tflag, modspec;
  5825. intmax_t number;
  5826. temp = temp1 = value = (char *)NULL;
  5827. var_is_set = var_is_null = var_is_special = check_nullness = 0;
  5828. want_substring = want_indir = want_patsub = want_casemod = 0;
  5829. sindex = *indexp;
  5830. t_index = ++sindex;
  5831. /* ${#var} doesn't have any of the other parameter expansions on it. */
  5832. if (string[t_index] == '#' && legal_variable_starter (string[t_index+1])) /* {{ */
  5833. name = string_extract (string, &t_index, "}", SX_VARNAME);
  5834. else
  5835. #if defined (CASEMOD_EXPANSIONS)
  5836. /* To enable case-toggling expansions using the `~' operator character
  5837. change the 1 to 0. */
  5838. # if defined (CASEMOD_CAPCASE)
  5839. name = string_extract (string, &t_index, "#%^,~:-=?+/}", SX_VARNAME);
  5840. # else
  5841. name = string_extract (string, &t_index, "#%^,:-=?+/}", SX_VARNAME);
  5842. # endif /* CASEMOD_CAPCASE */
  5843. #else
  5844. name = string_extract (string, &t_index, "#%:-=?+/}", SX_VARNAME);
  5845. #endif /* CASEMOD_EXPANSIONS */
  5846. ret = 0;
  5847. tflag = 0;
  5848. /* If the name really consists of a special variable, then make sure
  5849. that we have the entire name. We don't allow indirect references
  5850. to special variables except `#', `?', `@' and `*'. */
  5851. if ((sindex == t_index &&
  5852. (string[t_index] == '-' ||
  5853. string[t_index] == '?' ||
  5854. string[t_index] == '#')) ||
  5855. (sindex == t_index - 1 && string[sindex] == '!' &&
  5856. (string[t_index] == '#' ||
  5857. string[t_index] == '?' ||
  5858. string[t_index] == '@' ||
  5859. string[t_index] == '*')))
  5860. {
  5861. t_index++;
  5862. free (name);
  5863. temp1 = string_extract (string, &t_index, "#%:-=?+/}", 0);
  5864. name = (char *)xmalloc (3 + (strlen (temp1)));
  5865. *name = string[sindex];
  5866. if (string[sindex] == '!')
  5867. {
  5868. /* indirect reference of $#, $?, $@, or $* */
  5869. name[1] = string[sindex + 1];
  5870. strcpy (name + 2, temp1);
  5871. }
  5872. else
  5873. strcpy (name + 1, temp1);
  5874. free (temp1);
  5875. }
  5876. sindex = t_index;
  5877. /* Find out what character ended the variable name. Then
  5878. do the appropriate thing. */
  5879. if (c = string[sindex])
  5880. sindex++;
  5881. /* If c is followed by one of the valid parameter expansion
  5882. characters, move past it as normal. If not, assume that
  5883. a substring specification is being given, and do not move
  5884. past it. */
  5885. if (c == ':' && VALID_PARAM_EXPAND_CHAR (string[sindex]))
  5886. {
  5887. check_nullness++;
  5888. if (c = string[sindex])
  5889. sindex++;
  5890. }
  5891. else if (c == ':' && string[sindex] != RBRACE)
  5892. want_substring = 1;
  5893. else if (c == '/' && string[sindex] != RBRACE)
  5894. want_patsub = 1;
  5895. #if defined (CASEMOD_EXPANSIONS)
  5896. else if (c == '^' || c == ',' || c == '~')
  5897. {
  5898. modspec = c;
  5899. want_casemod = 1;
  5900. }
  5901. #endif
  5902. /* Catch the valid and invalid brace expressions that made it through the
  5903. tests above. */
  5904. /* ${#-} is a valid expansion and means to take the length of $-.
  5905. Similarly for ${#?} and ${##}... */
  5906. if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
  5907. VALID_SPECIAL_LENGTH_PARAM (c) && string[sindex] == RBRACE)
  5908. {
  5909. name = (char *)xrealloc (name, 3);
  5910. name[1] = c;
  5911. name[2] = '\0';
  5912. c = string[sindex++];
  5913. }
  5914. /* ...but ${#%}, ${#:}, ${#=}, ${#+}, and ${#/} are errors. */
  5915. if (name[0] == '#' && name[1] == '\0' && check_nullness == 0 &&
  5916. member (c, "%:=+/") && string[sindex] == RBRACE)
  5917. {
  5918. temp = (char *)NULL;
  5919. goto bad_substitution;
  5920. }
  5921. /* Indirect expansion begins with a `!'. A valid indirect expansion is
  5922. either a variable name, one of the positional parameters or a special
  5923. variable that expands to one of the positional parameters. */
  5924. want_indir = *name == '!' &&
  5925. (legal_variable_starter ((unsigned char)name[1]) || DIGIT (name[1])
  5926. || VALID_INDIR_PARAM (name[1]));
  5927. /* Determine the value of this variable. */
  5928. /* Check for special variables, directly referenced. */
  5929. if (SPECIAL_VAR (name, want_indir))
  5930. var_is_special++;
  5931. /* Check for special expansion things, like the length of a parameter */
  5932. if (*name == '#' && name[1])
  5933. {
  5934. /* If we are not pointing at the character just after the
  5935. closing brace, then we haven't gotten all of the name.
  5936. Since it begins with a special character, this is a bad
  5937. substitution. Also check NAME for validity before trying
  5938. to go on. */
  5939. if (string[sindex - 1] != RBRACE || (valid_length_expression (name) == 0))
  5940. {
  5941. temp = (char *)NULL;
  5942. goto bad_substitution;
  5943. }
  5944. number = parameter_brace_expand_length (name);
  5945. free (name);
  5946. *indexp = sindex;
  5947. if (number < 0)
  5948. return (&expand_wdesc_error);
  5949. else
  5950. {
  5951. ret = alloc_word_desc ();
  5952. ret->word = itos (number);
  5953. return ret;
  5954. }
  5955. }
  5956. /* ${@} is identical to $@. */
  5957. if (name[0] == '@' && name[1] == '\0')
  5958. {
  5959. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
  5960. *quoted_dollar_atp = 1;
  5961. if (contains_dollar_at)
  5962. *contains_dollar_at = 1;
  5963. }
  5964. /* Process ${!PREFIX*} expansion. */
  5965. if (want_indir && string[sindex - 1] == RBRACE &&
  5966. (string[sindex - 2] == '*' || string[sindex - 2] == '@') &&
  5967. legal_variable_starter ((unsigned char) name[1]))
  5968. {
  5969. char **x;
  5970. WORD_LIST *xlist;
  5971. temp1 = savestring (name + 1);
  5972. number = strlen (temp1);
  5973. temp1[number - 1] = '\0';
  5974. x = all_variables_matching_prefix (temp1);
  5975. xlist = strvec_to_word_list (x, 0, 0);
  5976. if (string[sindex - 2] == '*')
  5977. temp = string_list_dollar_star (xlist);
  5978. else
  5979. {
  5980. temp = string_list_dollar_at (xlist, quoted);
  5981. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
  5982. *quoted_dollar_atp = 1;
  5983. if (contains_dollar_at)
  5984. *contains_dollar_at = 1;
  5985. }
  5986. free (x);
  5987. dispose_words (xlist);
  5988. free (temp1);
  5989. *indexp = sindex;
  5990. ret = alloc_word_desc ();
  5991. ret->word = temp;
  5992. return ret;
  5993. }
  5994. #if defined (ARRAY_VARS)
  5995. /* Process ${!ARRAY[@]} and ${!ARRAY[*]} expansion. */ /* [ */
  5996. if (want_indir && string[sindex - 1] == RBRACE &&
  5997. string[sindex - 2] == ']' && valid_array_reference (name+1))
  5998. {
  5999. char *x, *x1;
  6000. temp1 = savestring (name + 1);
  6001. x = array_variable_name (temp1, &x1, (int *)0); /* [ */
  6002. FREE (x);
  6003. if (ALL_ELEMENT_SUB (x1[0]) && x1[1] == ']')
  6004. {
  6005. temp = array_keys (temp1, quoted); /* handles assoc vars too */
  6006. if (x1[0] == '@')
  6007. {
  6008. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
  6009. *quoted_dollar_atp = 1;
  6010. if (contains_dollar_at)
  6011. *contains_dollar_at = 1;
  6012. }
  6013. free (temp1);
  6014. *indexp = sindex;
  6015. ret = alloc_word_desc ();
  6016. ret->word = temp;
  6017. return ret;
  6018. }
  6019. free (temp1);
  6020. }
  6021. #endif /* ARRAY_VARS */
  6022. /* Make sure that NAME is valid before trying to go on. */
  6023. if (valid_brace_expansion_word (want_indir ? name + 1 : name,
  6024. var_is_special) == 0)
  6025. {
  6026. temp = (char *)NULL;
  6027. goto bad_substitution;
  6028. }
  6029. if (want_indir)
  6030. tdesc = parameter_brace_expand_indir (name + 1, var_is_special, quoted, quoted_dollar_atp, contains_dollar_at);
  6031. else
  6032. tdesc = parameter_brace_expand_word (name, var_is_special, quoted, PF_IGNUNBOUND|(pflags&PF_NOSPLIT2));
  6033. if (tdesc)
  6034. {
  6035. temp = tdesc->word;
  6036. tflag = tdesc->flags;
  6037. dispose_word_desc (tdesc);
  6038. }
  6039. else
  6040. temp = (char *)0;
  6041. #if defined (ARRAY_VARS)
  6042. if (valid_array_reference (name))
  6043. chk_atstar (name, quoted, quoted_dollar_atp, contains_dollar_at);
  6044. #endif
  6045. var_is_set = temp != (char *)0;
  6046. var_is_null = check_nullness && (var_is_set == 0 || *temp == 0);
  6047. /* Get the rest of the stuff inside the braces. */
  6048. if (c && c != RBRACE)
  6049. {
  6050. /* Extract the contents of the ${ ... } expansion
  6051. according to the Posix.2 rules. */
  6052. value = extract_dollar_brace_string (string, &sindex, quoted, 0);
  6053. if (string[sindex] == RBRACE)
  6054. sindex++;
  6055. else
  6056. goto bad_substitution;
  6057. }
  6058. else
  6059. value = (char *)NULL;
  6060. *indexp = sindex;
  6061. /* If this is a substring spec, process it and add the result. */
  6062. if (want_substring)
  6063. {
  6064. temp1 = parameter_brace_substring (name, temp, value, quoted);
  6065. FREE (name);
  6066. FREE (value);
  6067. FREE (temp);
  6068. if (temp1 == &expand_param_error)
  6069. return (&expand_wdesc_error);
  6070. else if (temp1 == &expand_param_fatal)
  6071. return (&expand_wdesc_fatal);
  6072. ret = alloc_word_desc ();
  6073. ret->word = temp1;
  6074. if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6075. ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
  6076. return ret;
  6077. }
  6078. else if (want_patsub)
  6079. {
  6080. temp1 = parameter_brace_patsub (name, temp, value, quoted);
  6081. FREE (name);
  6082. FREE (value);
  6083. FREE (temp);
  6084. if (temp1 == &expand_param_error)
  6085. return (&expand_wdesc_error);
  6086. else if (temp1 == &expand_param_fatal)
  6087. return (&expand_wdesc_fatal);
  6088. ret = alloc_word_desc ();
  6089. ret->word = temp1;
  6090. ret = alloc_word_desc ();
  6091. ret->word = temp1;
  6092. if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6093. ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
  6094. return ret;
  6095. }
  6096. #if defined (CASEMOD_EXPANSIONS)
  6097. else if (want_casemod)
  6098. {
  6099. temp1 = parameter_brace_casemod (name, temp, modspec, value, quoted);
  6100. FREE (name);
  6101. FREE (value);
  6102. FREE (temp);
  6103. if (temp1 == &expand_param_error)
  6104. return (&expand_wdesc_error);
  6105. else if (temp1 == &expand_param_fatal)
  6106. return (&expand_wdesc_fatal);
  6107. ret = alloc_word_desc ();
  6108. ret->word = temp1;
  6109. if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6110. ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
  6111. return ret;
  6112. }
  6113. #endif
  6114. /* Do the right thing based on which character ended the variable name. */
  6115. switch (c)
  6116. {
  6117. default:
  6118. case '\0':
  6119. bad_substitution:
  6120. report_error (_("%s: bad substitution"), string ? string : "??");
  6121. FREE (value);
  6122. FREE (temp);
  6123. free (name);
  6124. return &expand_wdesc_error;
  6125. case RBRACE:
  6126. if (var_is_set == 0 && unbound_vars_is_error && ((name[0] != '@' && name[0] != '*') || name[1]))
  6127. {
  6128. last_command_exit_value = EXECUTION_FAILURE;
  6129. err_unboundvar (name);
  6130. FREE (value);
  6131. FREE (temp);
  6132. free (name);
  6133. return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
  6134. }
  6135. break;
  6136. case '#': /* ${param#[#]pattern} */
  6137. case '%': /* ${param%[%]pattern} */
  6138. if (value == 0 || *value == '\0' || temp == 0 || *temp == '\0')
  6139. {
  6140. FREE (value);
  6141. break;
  6142. }
  6143. temp1 = parameter_brace_remove_pattern (name, temp, value, c, quoted);
  6144. free (temp);
  6145. free (value);
  6146. ret = alloc_word_desc ();
  6147. ret->word = temp1;
  6148. if (temp1 && QUOTED_NULL (temp1) && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6149. ret->flags |= W_QUOTED|W_HASQUOTEDNULL;
  6150. return ret;
  6151. case '-':
  6152. case '=':
  6153. case '?':
  6154. case '+':
  6155. if (var_is_set && var_is_null == 0)
  6156. {
  6157. /* If the operator is `+', we don't want the value of the named
  6158. variable for anything, just the value of the right hand side. */
  6159. if (c == '+')
  6160. {
  6161. /* XXX -- if we're double-quoted and the named variable is "$@",
  6162. we want to turn off any special handling of "$@" --
  6163. we're not using it, so whatever is on the rhs applies. */
  6164. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
  6165. *quoted_dollar_atp = 0;
  6166. if (contains_dollar_at)
  6167. *contains_dollar_at = 0;
  6168. FREE (temp);
  6169. if (value)
  6170. {
  6171. ret = parameter_brace_expand_rhs (name, value, c,
  6172. quoted,
  6173. quoted_dollar_atp,
  6174. contains_dollar_at);
  6175. /* XXX - fix up later, esp. noting presence of
  6176. W_HASQUOTEDNULL in ret->flags */
  6177. free (value);
  6178. }
  6179. else
  6180. temp = (char *)NULL;
  6181. }
  6182. else
  6183. {
  6184. FREE (value);
  6185. }
  6186. /* Otherwise do nothing; just use the value in TEMP. */
  6187. }
  6188. else /* VAR not set or VAR is NULL. */
  6189. {
  6190. FREE (temp);
  6191. temp = (char *)NULL;
  6192. if (c == '=' && var_is_special)
  6193. {
  6194. report_error (_("$%s: cannot assign in this way"), name);
  6195. free (name);
  6196. free (value);
  6197. return &expand_wdesc_error;
  6198. }
  6199. else if (c == '?')
  6200. {
  6201. parameter_brace_expand_error (name, value);
  6202. return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
  6203. }
  6204. else if (c != '+')
  6205. {
  6206. /* XXX -- if we're double-quoted and the named variable is "$@",
  6207. we want to turn off any special handling of "$@" --
  6208. we're not using it, so whatever is on the rhs applies. */
  6209. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && quoted_dollar_atp)
  6210. *quoted_dollar_atp = 0;
  6211. if (contains_dollar_at)
  6212. *contains_dollar_at = 0;
  6213. ret = parameter_brace_expand_rhs (name, value, c, quoted,
  6214. quoted_dollar_atp,
  6215. contains_dollar_at);
  6216. /* XXX - fix up later, esp. noting presence of
  6217. W_HASQUOTEDNULL in tdesc->flags */
  6218. }
  6219. free (value);
  6220. }
  6221. break;
  6222. }
  6223. free (name);
  6224. if (ret == 0)
  6225. {
  6226. ret = alloc_word_desc ();
  6227. ret->flags = tflag;
  6228. ret->word = temp;
  6229. }
  6230. return (ret);
  6231. }
  6232. /* Expand a single ${xxx} expansion. The braces are optional. When
  6233. the braces are used, parameter_brace_expand() does the work,
  6234. possibly calling param_expand recursively. */
  6235. static WORD_DESC *
  6236. param_expand (string, sindex, quoted, expanded_something,
  6237. contains_dollar_at, quoted_dollar_at_p, had_quoted_null_p,
  6238. pflags)
  6239. char *string;
  6240. int *sindex, quoted, *expanded_something, *contains_dollar_at;
  6241. int *quoted_dollar_at_p, *had_quoted_null_p, pflags;
  6242. {
  6243. char *temp, *temp1, uerror[3];
  6244. int zindex, t_index, expok;
  6245. unsigned char c;
  6246. intmax_t number;
  6247. SHELL_VAR *var;
  6248. WORD_LIST *list;
  6249. WORD_DESC *tdesc, *ret;
  6250. int tflag;
  6251. zindex = *sindex;
  6252. c = string[++zindex];
  6253. temp = (char *)NULL;
  6254. ret = tdesc = (WORD_DESC *)NULL;
  6255. tflag = 0;
  6256. /* Do simple cases first. Switch on what follows '$'. */
  6257. switch (c)
  6258. {
  6259. /* $0 .. $9? */
  6260. case '0':
  6261. case '1':
  6262. case '2':
  6263. case '3':
  6264. case '4':
  6265. case '5':
  6266. case '6':
  6267. case '7':
  6268. case '8':
  6269. case '9':
  6270. temp1 = dollar_vars[TODIGIT (c)];
  6271. if (unbound_vars_is_error && temp1 == (char *)NULL)
  6272. {
  6273. uerror[0] = '$';
  6274. uerror[1] = c;
  6275. uerror[2] = '\0';
  6276. last_command_exit_value = EXECUTION_FAILURE;
  6277. err_unboundvar (uerror);
  6278. return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
  6279. }
  6280. if (temp1)
  6281. temp = (*temp1 && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6282. ? quote_string (temp1)
  6283. : quote_escapes (temp1);
  6284. else
  6285. temp = (char *)NULL;
  6286. break;
  6287. /* $$ -- pid of the invoking shell. */
  6288. case '$':
  6289. temp = itos (dollar_dollar_pid);
  6290. break;
  6291. /* $# -- number of positional parameters. */
  6292. case '#':
  6293. temp = itos (number_of_args ());
  6294. break;
  6295. /* $? -- return value of the last synchronous command. */
  6296. case '?':
  6297. temp = itos (last_command_exit_value);
  6298. break;
  6299. /* $- -- flags supplied to the shell on invocation or by `set'. */
  6300. case '-':
  6301. temp = which_set_flags ();
  6302. break;
  6303. /* $! -- Pid of the last asynchronous command. */
  6304. case '!':
  6305. /* If no asynchronous pids have been created, expand to nothing.
  6306. If `set -u' has been executed, and no async processes have
  6307. been created, this is an expansion error. */
  6308. if (last_asynchronous_pid == NO_PID)
  6309. {
  6310. if (expanded_something)
  6311. *expanded_something = 0;
  6312. temp = (char *)NULL;
  6313. if (unbound_vars_is_error)
  6314. {
  6315. uerror[0] = '$';
  6316. uerror[1] = c;
  6317. uerror[2] = '\0';
  6318. last_command_exit_value = EXECUTION_FAILURE;
  6319. err_unboundvar (uerror);
  6320. return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
  6321. }
  6322. }
  6323. else
  6324. temp = itos (last_asynchronous_pid);
  6325. break;
  6326. /* The only difference between this and $@ is when the arg is quoted. */
  6327. case '*': /* `$*' */
  6328. list = list_rest_of_args ();
  6329. #if 0
  6330. /* According to austin-group posix proposal by Geoff Clare in
  6331. <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
  6332. "The shell shall write a message to standard error and
  6333. immediately exit when it tries to expand an unset parameter
  6334. other than the '@' and '*' special parameters."
  6335. */
  6336. if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
  6337. {
  6338. uerror[0] = '$';
  6339. uerror[1] = '*';
  6340. uerror[2] = '\0';
  6341. last_command_exit_value = EXECUTION_FAILURE;
  6342. err_unboundvar (uerror);
  6343. return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
  6344. }
  6345. #endif
  6346. /* If there are no command-line arguments, this should just
  6347. disappear if there are other characters in the expansion,
  6348. even if it's quoted. */
  6349. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && list == 0)
  6350. temp = (char *)NULL;
  6351. else if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES|Q_PATQUOTE))
  6352. {
  6353. /* If we have "$*" we want to make a string of the positional
  6354. parameters, separated by the first character of $IFS, and
  6355. quote the whole string, including the separators. If IFS
  6356. is unset, the parameters are separated by ' '; if $IFS is
  6357. null, the parameters are concatenated. */
  6358. temp = (quoted & (Q_DOUBLE_QUOTES|Q_PATQUOTE)) ? string_list_dollar_star (list) : string_list (list);
  6359. temp1 = quote_string (temp);
  6360. if (*temp == 0)
  6361. tflag |= W_HASQUOTEDNULL;
  6362. free (temp);
  6363. temp = temp1;
  6364. }
  6365. else
  6366. {
  6367. /* We check whether or not we're eventually going to split $* here,
  6368. for example when IFS is empty and we are processing the rhs of
  6369. an assignment statement. In that case, we don't separate the
  6370. arguments at all. Otherwise, if the $* is not quoted it is
  6371. identical to $@ */
  6372. #if 1
  6373. # if defined (HANDLE_MULTIBYTE)
  6374. if (expand_no_split_dollar_star && ifs_firstc[0] == 0)
  6375. # else
  6376. if (expand_no_split_dollar_star && ifs_firstc == 0)
  6377. # endif
  6378. temp = string_list_dollar_star (list);
  6379. else
  6380. temp = string_list_dollar_at (list, quoted);
  6381. #else
  6382. temp = string_list_dollar_at (list, quoted);
  6383. #endif
  6384. if (expand_no_split_dollar_star == 0 && contains_dollar_at)
  6385. *contains_dollar_at = 1;
  6386. }
  6387. dispose_words (list);
  6388. break;
  6389. /* When we have "$@" what we want is "$1" "$2" "$3" ... This
  6390. means that we have to turn quoting off after we split into
  6391. the individually quoted arguments so that the final split
  6392. on the first character of $IFS is still done. */
  6393. case '@': /* `$@' */
  6394. list = list_rest_of_args ();
  6395. #if 0
  6396. /* According to austin-group posix proposal by Geoff Clare in
  6397. <20090505091501.GA10097@squonk.masqnet> of 5 May 2009:
  6398. "The shell shall write a message to standard error and
  6399. immediately exit when it tries to expand an unset parameter
  6400. other than the '@' and '*' special parameters."
  6401. */
  6402. if (list == 0 && unbound_vars_is_error && (pflags & PF_IGNUNBOUND) == 0)
  6403. {
  6404. uerror[0] = '$';
  6405. uerror[1] = '@';
  6406. uerror[2] = '\0';
  6407. last_command_exit_value = EXECUTION_FAILURE;
  6408. err_unboundvar (uerror);
  6409. return (interactive_shell ? &expand_wdesc_error : &expand_wdesc_fatal);
  6410. }
  6411. #endif
  6412. /* We want to flag the fact that we saw this. We can't turn
  6413. off quoting entirely, because other characters in the
  6414. string might need it (consider "\"$@\""), but we need some
  6415. way to signal that the final split on the first character
  6416. of $IFS should be done, even though QUOTED is 1. */
  6417. /* XXX - should this test include Q_PATQUOTE? */
  6418. if (quoted_dollar_at_p && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6419. *quoted_dollar_at_p = 1;
  6420. if (contains_dollar_at)
  6421. *contains_dollar_at = 1;
  6422. #if 0
  6423. if (pflags & PF_NOSPLIT2)
  6424. temp = string_list_internal (quoted ? quote_list (list) : list, " ");
  6425. else
  6426. #endif
  6427. /* We want to separate the positional parameters with the first
  6428. character of $IFS in case $IFS is something other than a space.
  6429. We also want to make sure that splitting is done no matter what --
  6430. according to POSIX.2, this expands to a list of the positional
  6431. parameters no matter what IFS is set to. */
  6432. temp = string_list_dollar_at (list, quoted);
  6433. dispose_words (list);
  6434. break;
  6435. case LBRACE:
  6436. tdesc = parameter_brace_expand (string, &zindex, quoted, pflags,
  6437. quoted_dollar_at_p,
  6438. contains_dollar_at);
  6439. if (tdesc == &expand_wdesc_error || tdesc == &expand_wdesc_fatal)
  6440. return (tdesc);
  6441. temp = tdesc ? tdesc->word : (char *)0;
  6442. /* XXX */
  6443. /* Quoted nulls should be removed if there is anything else
  6444. in the string. */
  6445. /* Note that we saw the quoted null so we can add one back at
  6446. the end of this function if there are no other characters
  6447. in the string, discard TEMP, and go on. The exception to
  6448. this is when we have "${@}" and $1 is '', since $@ needs
  6449. special handling. */
  6450. if (tdesc && tdesc->word && (tdesc->flags & W_HASQUOTEDNULL) && QUOTED_NULL (temp))
  6451. {
  6452. if (had_quoted_null_p)
  6453. *had_quoted_null_p = 1;
  6454. if (*quoted_dollar_at_p == 0)
  6455. {
  6456. free (temp);
  6457. tdesc->word = temp = (char *)NULL;
  6458. }
  6459. }
  6460. ret = tdesc;
  6461. goto return0;
  6462. /* Do command or arithmetic substitution. */
  6463. case LPAREN:
  6464. /* We have to extract the contents of this paren substitution. */
  6465. t_index = zindex + 1;
  6466. temp = extract_command_subst (string, &t_index, 0);
  6467. zindex = t_index;
  6468. /* For Posix.2-style `$(( ))' arithmetic substitution,
  6469. extract the expression and pass it to the evaluator. */
  6470. if (temp && *temp == LPAREN)
  6471. {
  6472. char *temp2;
  6473. temp1 = temp + 1;
  6474. temp2 = savestring (temp1);
  6475. t_index = strlen (temp2) - 1;
  6476. if (temp2[t_index] != RPAREN)
  6477. {
  6478. free (temp2);
  6479. goto comsub;
  6480. }
  6481. /* Cut off ending `)' */
  6482. temp2[t_index] = '\0';
  6483. if (chk_arithsub (temp2, t_index) == 0)
  6484. {
  6485. free (temp2);
  6486. #if 0
  6487. internal_warning (_("future versions of the shell will force evaluation as an arithmetic substitution"));
  6488. #endif
  6489. goto comsub;
  6490. }
  6491. /* Expand variables found inside the expression. */
  6492. temp1 = expand_arith_string (temp2, Q_DOUBLE_QUOTES);
  6493. free (temp2);
  6494. arithsub:
  6495. /* No error messages. */
  6496. this_command_name = (char *)NULL;
  6497. number = evalexp (temp1, &expok);
  6498. free (temp);
  6499. free (temp1);
  6500. if (expok == 0)
  6501. {
  6502. if (interactive_shell == 0 && posixly_correct)
  6503. {
  6504. last_command_exit_value = EXECUTION_FAILURE;
  6505. return (&expand_wdesc_fatal);
  6506. }
  6507. else
  6508. return (&expand_wdesc_error);
  6509. }
  6510. temp = itos (number);
  6511. break;
  6512. }
  6513. comsub:
  6514. if (pflags & PF_NOCOMSUB)
  6515. /* we need zindex+1 because string[zindex] == RPAREN */
  6516. temp1 = substring (string, *sindex, zindex+1);
  6517. else
  6518. {
  6519. tdesc = command_substitute (temp, quoted);
  6520. temp1 = tdesc ? tdesc->word : (char *)NULL;
  6521. if (tdesc)
  6522. dispose_word_desc (tdesc);
  6523. }
  6524. FREE (temp);
  6525. temp = temp1;
  6526. break;
  6527. /* Do POSIX.2d9-style arithmetic substitution. This will probably go
  6528. away in a future bash release. */
  6529. case '[':
  6530. /* Extract the contents of this arithmetic substitution. */
  6531. t_index = zindex + 1;
  6532. temp = extract_arithmetic_subst (string, &t_index);
  6533. zindex = t_index;
  6534. if (temp == 0)
  6535. {
  6536. temp = savestring (string);
  6537. if (expanded_something)
  6538. *expanded_something = 0;
  6539. goto return0;
  6540. }
  6541. /* Do initial variable expansion. */
  6542. temp1 = expand_arith_string (temp, Q_DOUBLE_QUOTES);
  6543. goto arithsub;
  6544. default:
  6545. /* Find the variable in VARIABLE_LIST. */
  6546. temp = (char *)NULL;
  6547. for (t_index = zindex; (c = string[zindex]) && legal_variable_char (c); zindex++)
  6548. ;
  6549. temp1 = (zindex > t_index) ? substring (string, t_index, zindex) : (char *)NULL;
  6550. /* If this isn't a variable name, then just output the `$'. */
  6551. if (temp1 == 0 || *temp1 == '\0')
  6552. {
  6553. FREE (temp1);
  6554. temp = (char *)xmalloc (2);
  6555. temp[0] = '$';
  6556. temp[1] = '\0';
  6557. if (expanded_something)
  6558. *expanded_something = 0;
  6559. goto return0;
  6560. }
  6561. /* If the variable exists, return its value cell. */
  6562. var = find_variable (temp1);
  6563. if (var && invisible_p (var) == 0 && var_isset (var))
  6564. {
  6565. #if defined (ARRAY_VARS)
  6566. if (assoc_p (var) || array_p (var))
  6567. {
  6568. temp = array_p (var) ? array_reference (array_cell (var), 0)
  6569. : assoc_reference (assoc_cell (var), "0");
  6570. if (temp)
  6571. temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6572. ? quote_string (temp)
  6573. : quote_escapes (temp);
  6574. else if (unbound_vars_is_error)
  6575. goto unbound_variable;
  6576. }
  6577. else
  6578. #endif
  6579. {
  6580. temp = value_cell (var);
  6581. temp = (*temp && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
  6582. ? quote_string (temp)
  6583. : quote_escapes (temp);
  6584. }
  6585. free (temp1);
  6586. goto return0;
  6587. }
  6588. temp = (char *)NULL;
  6589. unbound_variable:
  6590. if (unbound_vars_is_error)
  6591. {
  6592. last_command_exit_value = EXECUTION_FAILURE;
  6593. err_unboundvar (temp1);
  6594. }
  6595. else
  6596. {
  6597. free (temp1);
  6598. goto return0;
  6599. }
  6600. free (temp1);
  6601. last_command_exit_value = EXECUTION_FAILURE;
  6602. return ((unbound_vars_is_error && interactive_shell == 0)
  6603. ? &expand_wdesc_fatal
  6604. : &expand_wdesc_error);
  6605. }
  6606. if (string[zindex])
  6607. zindex++;
  6608. return0:
  6609. *sindex = zindex;
  6610. if (ret == 0)
  6611. {
  6612. ret = alloc_word_desc ();
  6613. ret->flags = tflag; /* XXX */
  6614. ret->word = temp;
  6615. }
  6616. return ret;
  6617. }
  6618. /* Make a word list which is the result of parameter and variable
  6619. expansion, command substitution, arithmetic substitution, and
  6620. quote removal of WORD. Return a pointer to a WORD_LIST which is
  6621. the result of the expansion. If WORD contains a null word, the
  6622. word list returned is also null.
  6623. QUOTED contains flag values defined in shell.h.
  6624. ISEXP is used to tell expand_word_internal that the word should be
  6625. treated as the result of an expansion. This has implications for
  6626. how IFS characters in the word are treated.
  6627. CONTAINS_DOLLAR_AT and EXPANDED_SOMETHING are return values; when non-null
  6628. they point to an integer value which receives information about expansion.
  6629. CONTAINS_DOLLAR_AT gets non-zero if WORD contained "$@", else zero.
  6630. EXPANDED_SOMETHING get non-zero if WORD contained any parameter expansions,
  6631. else zero.
  6632. This only does word splitting in the case of $@ expansion. In that
  6633. case, we split on ' '. */
  6634. /* Values for the local variable quoted_state. */
  6635. #define UNQUOTED 0
  6636. #define PARTIALLY_QUOTED 1
  6637. #define WHOLLY_QUOTED 2
  6638. static WORD_LIST *
  6639. expand_word_internal (word, quoted, isexp, contains_dollar_at, expanded_something)
  6640. WORD_DESC *word;
  6641. int quoted, isexp;
  6642. int *contains_dollar_at;
  6643. int *expanded_something;
  6644. {
  6645. WORD_LIST *list;
  6646. WORD_DESC *tword;
  6647. /* The intermediate string that we build while expanding. */
  6648. char *istring;
  6649. /* The current size of the above object. */
  6650. int istring_size;
  6651. /* Index into ISTRING. */
  6652. int istring_index;
  6653. /* Temporary string storage. */
  6654. char *temp, *temp1;
  6655. /* The text of WORD. */
  6656. register char *string;
  6657. /* The size of STRING. */
  6658. size_t string_size;
  6659. /* The index into STRING. */
  6660. int sindex;
  6661. /* This gets 1 if we see a $@ while quoted. */
  6662. int quoted_dollar_at;
  6663. /* One of UNQUOTED, PARTIALLY_QUOTED, or WHOLLY_QUOTED, depending on
  6664. whether WORD contains no quoting characters, a partially quoted
  6665. string (e.g., "xx"ab), or is fully quoted (e.g., "xxab"). */
  6666. int quoted_state;
  6667. /* State flags */
  6668. int had_quoted_null;
  6669. int has_dollar_at;
  6670. int tflag;
  6671. int pflags; /* flags passed to param_expand */
  6672. int assignoff; /* If assignment, offset of `=' */
  6673. register unsigned char c; /* Current character. */
  6674. int t_index; /* For calls to string_extract_xxx. */
  6675. char twochars[2];
  6676. DECLARE_MBSTATE;
  6677. istring = (char *)xmalloc (istring_size = DEFAULT_INITIAL_ARRAY_SIZE);
  6678. istring[istring_index = 0] = '\0';
  6679. quoted_dollar_at = had_quoted_null = has_dollar_at = 0;
  6680. quoted_state = UNQUOTED;
  6681. string = word->word;
  6682. if (string == 0)
  6683. goto finished_with_string;
  6684. /* Don't need the string length for the SADD... and COPY_ macros unless
  6685. multibyte characters are possible. */
  6686. string_size = (MB_CUR_MAX > 1) ? strlen (string) : 1;
  6687. if (contains_dollar_at)
  6688. *contains_dollar_at = 0;
  6689. assignoff = -1;
  6690. /* Begin the expansion. */
  6691. for (sindex = 0; ;)
  6692. {
  6693. c = string[sindex];
  6694. /* Case on toplevel character. */
  6695. switch (c)
  6696. {
  6697. case '\0':
  6698. goto finished_with_string;
  6699. case CTLESC:
  6700. sindex++;
  6701. #if HANDLE_MULTIBYTE
  6702. if (MB_CUR_MAX > 1 && string[sindex])
  6703. {
  6704. SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
  6705. }
  6706. else
  6707. #endif
  6708. {
  6709. temp = (char *)xmalloc (3);
  6710. temp[0] = CTLESC;
  6711. temp[1] = c = string[sindex];
  6712. temp[2] = '\0';
  6713. }
  6714. dollar_add_string:
  6715. if (string[sindex])
  6716. sindex++;
  6717. add_string:
  6718. if (temp)
  6719. {
  6720. istring = sub_append_string (temp, istring, &istring_index, &istring_size);
  6721. temp = (char *)0;
  6722. }
  6723. break;
  6724. #if defined (PROCESS_SUBSTITUTION)
  6725. /* Process substitution. */
  6726. case '<':
  6727. case '>':
  6728. {
  6729. if (string[++sindex] != LPAREN || (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (word->flags & (W_DQUOTE|W_NOPROCSUB)) || posixly_correct)
  6730. {
  6731. sindex--; /* add_character: label increments sindex */
  6732. goto add_character;
  6733. }
  6734. else
  6735. t_index = sindex + 1; /* skip past both '<' and LPAREN */
  6736. temp1 = extract_process_subst (string, (c == '<') ? "<(" : ">(", &t_index); /*))*/
  6737. sindex = t_index;
  6738. /* If the process substitution specification is `<()', we want to
  6739. open the pipe for writing in the child and produce output; if
  6740. it is `>()', we want to open the pipe for reading in the child
  6741. and consume input. */
  6742. temp = temp1 ? process_substitute (temp1, (c == '>')) : (char *)0;
  6743. FREE (temp1);
  6744. goto dollar_add_string;
  6745. }
  6746. #endif /* PROCESS_SUBSTITUTION */
  6747. case '=':
  6748. /* Posix.2 section 3.6.1 says that tildes following `=' in words
  6749. which are not assignment statements are not expanded. If the
  6750. shell isn't in posix mode, though, we perform tilde expansion
  6751. on `likely candidate' unquoted assignment statements (flags
  6752. include W_ASSIGNMENT but not W_QUOTED). A likely candidate
  6753. contains an unquoted :~ or =~. Something to think about: we
  6754. now have a flag that says to perform tilde expansion on arguments
  6755. to `assignment builtins' like declare and export that look like
  6756. assignment statements. We now do tilde expansion on such words
  6757. even in POSIX mode. */
  6758. if (word->flags & (W_ASSIGNRHS|W_NOTILDE))
  6759. {
  6760. if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
  6761. goto add_ifs_character;
  6762. else
  6763. goto add_character;
  6764. }
  6765. /* If we're not in posix mode or forcing assignment-statement tilde
  6766. expansion, note where the `=' appears in the word and prepare to
  6767. do tilde expansion following the first `='. */
  6768. if ((word->flags & W_ASSIGNMENT) &&
  6769. (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
  6770. assignoff == -1 && sindex > 0)
  6771. assignoff = sindex;
  6772. if (sindex == assignoff && string[sindex+1] == '~') /* XXX */
  6773. word->flags |= W_ITILDE;
  6774. #if 0
  6775. else if ((word->flags & W_ASSIGNMENT) &&
  6776. (posixly_correct == 0 || (word->flags & W_TILDEEXP)) &&
  6777. string[sindex+1] == '~')
  6778. word->flags |= W_ITILDE;
  6779. #endif
  6780. if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
  6781. goto add_ifs_character;
  6782. else
  6783. goto add_character;
  6784. case ':':
  6785. if (word->flags & W_NOTILDE)
  6786. {
  6787. if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
  6788. goto add_ifs_character;
  6789. else
  6790. goto add_character;
  6791. }
  6792. if ((word->flags & (W_ASSIGNMENT|W_ASSIGNRHS|W_TILDEEXP)) &&
  6793. string[sindex+1] == '~')
  6794. word->flags |= W_ITILDE;
  6795. if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c))
  6796. goto add_ifs_character;
  6797. else
  6798. goto add_character;
  6799. case '~':
  6800. /* If the word isn't supposed to be tilde expanded, or we're not
  6801. at the start of a word or after an unquoted : or = in an
  6802. assignment statement, we don't do tilde expansion. */
  6803. if ((word->flags & (W_NOTILDE|W_DQUOTE)) ||
  6804. (sindex > 0 && ((word->flags & W_ITILDE) == 0)) ||
  6805. (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
  6806. {
  6807. word->flags &= ~W_ITILDE;
  6808. if (isexp == 0 && (word->flags & (W_NOSPLIT|W_NOSPLIT2)) == 0 && isifs (c) && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) == 0)
  6809. goto add_ifs_character;
  6810. else
  6811. goto add_character;
  6812. }
  6813. if (word->flags & W_ASSIGNRHS)
  6814. tflag = 2;
  6815. else if (word->flags & (W_ASSIGNMENT|W_TILDEEXP))
  6816. tflag = 1;
  6817. else
  6818. tflag = 0;
  6819. temp = bash_tilde_find_word (string + sindex, tflag, &t_index);
  6820. word->flags &= ~W_ITILDE;
  6821. if (temp && *temp && t_index > 0)
  6822. {
  6823. temp1 = bash_tilde_expand (temp, tflag);
  6824. if (temp1 && *temp1 == '~' && STREQ (temp, temp1))
  6825. {
  6826. FREE (temp);
  6827. FREE (temp1);
  6828. goto add_character; /* tilde expansion failed */
  6829. }
  6830. free (temp);
  6831. temp = temp1;
  6832. sindex += t_index;
  6833. goto add_quoted_string; /* XXX was add_string */
  6834. }
  6835. else
  6836. {
  6837. FREE (temp);
  6838. goto add_character;
  6839. }
  6840. case '$':
  6841. if (expanded_something)
  6842. *expanded_something = 1;
  6843. has_dollar_at = 0;
  6844. pflags = (word->flags & W_NOCOMSUB) ? PF_NOCOMSUB : 0;
  6845. if (word->flags & W_NOSPLIT2)
  6846. pflags |= PF_NOSPLIT2;
  6847. tword = param_expand (string, &sindex, quoted, expanded_something,
  6848. &has_dollar_at, &quoted_dollar_at,
  6849. &had_quoted_null, pflags);
  6850. if (tword == &expand_wdesc_error || tword == &expand_wdesc_fatal)
  6851. {
  6852. free (string);
  6853. free (istring);
  6854. return ((tword == &expand_wdesc_error) ? &expand_word_error
  6855. : &expand_word_fatal);
  6856. }
  6857. if (contains_dollar_at && has_dollar_at)
  6858. *contains_dollar_at = 1;
  6859. if (tword && (tword->flags & W_HASQUOTEDNULL))
  6860. had_quoted_null = 1;
  6861. temp = tword->word;
  6862. dispose_word_desc (tword);
  6863. goto add_string;
  6864. break;
  6865. case '`': /* Backquoted command substitution. */
  6866. {
  6867. t_index = sindex++;
  6868. temp = string_extract (string, &sindex, "`", SX_REQMATCH);
  6869. /* The test of sindex against t_index is to allow bare instances of
  6870. ` to pass through, for backwards compatibility. */
  6871. if (temp == &extract_string_error || temp == &extract_string_fatal)
  6872. {
  6873. if (sindex - 1 == t_index)
  6874. {
  6875. sindex = t_index;
  6876. goto add_character;
  6877. }
  6878. report_error (_("bad substitution: no closing \"`\" in %s") , string+t_index);
  6879. free (string);
  6880. free (istring);
  6881. return ((temp == &extract_string_error) ? &expand_word_error
  6882. : &expand_word_fatal);
  6883. }
  6884. if (expanded_something)
  6885. *expanded_something = 1;
  6886. if (word->flags & W_NOCOMSUB)
  6887. /* sindex + 1 because string[sindex] == '`' */
  6888. temp1 = substring (string, t_index, sindex + 1);
  6889. else
  6890. {
  6891. de_backslash (temp);
  6892. tword = command_substitute (temp, quoted);
  6893. temp1 = tword ? tword->word : (char *)NULL;
  6894. if (tword)
  6895. dispose_word_desc (tword);
  6896. }
  6897. FREE (temp);
  6898. temp = temp1;
  6899. goto dollar_add_string;
  6900. }
  6901. case '\\':
  6902. if (string[sindex + 1] == '\n')
  6903. {
  6904. sindex += 2;
  6905. continue;
  6906. }
  6907. c = string[++sindex];
  6908. if (quoted & Q_HERE_DOCUMENT)
  6909. tflag = CBSHDOC;
  6910. else if (quoted & Q_DOUBLE_QUOTES)
  6911. tflag = CBSDQUOTE;
  6912. else
  6913. tflag = 0;
  6914. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) && ((sh_syntaxtab[c] & tflag) == 0))
  6915. {
  6916. SCOPY_CHAR_I (twochars, '\\', c, string, sindex, string_size);
  6917. }
  6918. else if (c == 0)
  6919. {
  6920. c = CTLNUL;
  6921. sindex--; /* add_character: label increments sindex */
  6922. goto add_character;
  6923. }
  6924. else
  6925. {
  6926. SCOPY_CHAR_I (twochars, CTLESC, c, string, sindex, string_size);
  6927. }
  6928. sindex++;
  6929. add_twochars:
  6930. /* BEFORE jumping here, we need to increment sindex if appropriate */
  6931. RESIZE_MALLOCED_BUFFER (istring, istring_index, 2, istring_size,
  6932. DEFAULT_ARRAY_SIZE);
  6933. istring[istring_index++] = twochars[0];
  6934. istring[istring_index++] = twochars[1];
  6935. istring[istring_index] = '\0';
  6936. break;
  6937. case '"':
  6938. #if 0
  6939. if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
  6940. #else
  6941. if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
  6942. #endif
  6943. goto add_character;
  6944. t_index = ++sindex;
  6945. temp = string_extract_double_quoted (string, &sindex, 0);
  6946. /* If the quotes surrounded the entire string, then the
  6947. whole word was quoted. */
  6948. quoted_state = (t_index == 1 && string[sindex] == '\0')
  6949. ? WHOLLY_QUOTED
  6950. : PARTIALLY_QUOTED;
  6951. if (temp && *temp)
  6952. {
  6953. tword = alloc_word_desc ();
  6954. tword->word = temp;
  6955. temp = (char *)NULL;
  6956. has_dollar_at = 0;
  6957. /* Need to get W_HASQUOTEDNULL flag through this function. */
  6958. list = expand_word_internal (tword, Q_DOUBLE_QUOTES, 0, &has_dollar_at, (int *)NULL);
  6959. if (list == &expand_word_error || list == &expand_word_fatal)
  6960. {
  6961. free (istring);
  6962. free (string);
  6963. /* expand_word_internal has already freed temp_word->word
  6964. for us because of the way it prints error messages. */
  6965. tword->word = (char *)NULL;
  6966. dispose_word (tword);
  6967. return list;
  6968. }
  6969. dispose_word (tword);
  6970. /* "$@" (a double-quoted dollar-at) expands into nothing,
  6971. not even a NULL word, when there are no positional
  6972. parameters. */
  6973. if (list == 0 && has_dollar_at)
  6974. {
  6975. quoted_dollar_at++;
  6976. break;
  6977. }
  6978. /* If we get "$@", we know we have expanded something, so we
  6979. need to remember it for the final split on $IFS. This is
  6980. a special case; it's the only case where a quoted string
  6981. can expand into more than one word. It's going to come back
  6982. from the above call to expand_word_internal as a list with
  6983. a single word, in which all characters are quoted and
  6984. separated by blanks. What we want to do is to turn it back
  6985. into a list for the next piece of code. */
  6986. if (list)
  6987. dequote_list (list);
  6988. if (list && list->word && (list->word->flags & W_HASQUOTEDNULL))
  6989. had_quoted_null = 1;
  6990. if (has_dollar_at)
  6991. {
  6992. quoted_dollar_at++;
  6993. if (contains_dollar_at)
  6994. *contains_dollar_at = 1;
  6995. if (expanded_something)
  6996. *expanded_something = 1;
  6997. }
  6998. }
  6999. else
  7000. {
  7001. /* What we have is "". This is a minor optimization. */
  7002. FREE (temp);
  7003. list = (WORD_LIST *)NULL;
  7004. }
  7005. /* The code above *might* return a list (consider the case of "$@",
  7006. where it returns "$1", "$2", etc.). We can't throw away the
  7007. rest of the list, and we have to make sure each word gets added
  7008. as quoted. We test on tresult->next: if it is non-NULL, we
  7009. quote the whole list, save it to a string with string_list, and
  7010. add that string. We don't need to quote the results of this
  7011. (and it would be wrong, since that would quote the separators
  7012. as well), so we go directly to add_string. */
  7013. if (list)
  7014. {
  7015. if (list->next)
  7016. {
  7017. #if 0
  7018. if (quoted_dollar_at && word->flags & W_NOSPLIT2)
  7019. temp = string_list_internal (quote_list (list), " ");
  7020. else
  7021. #endif
  7022. /* Testing quoted_dollar_at makes sure that "$@" is
  7023. split correctly when $IFS does not contain a space. */
  7024. temp = quoted_dollar_at
  7025. ? string_list_dollar_at (list, Q_DOUBLE_QUOTES)
  7026. : string_list (quote_list (list));
  7027. dispose_words (list);
  7028. goto add_string;
  7029. }
  7030. else
  7031. {
  7032. temp = savestring (list->word->word);
  7033. tflag = list->word->flags;
  7034. dispose_words (list);
  7035. /* If the string is not a quoted null string, we want
  7036. to remove any embedded unquoted CTLNUL characters.
  7037. We do not want to turn quoted null strings back into
  7038. the empty string, though. We do this because we
  7039. want to remove any quoted nulls from expansions that
  7040. contain other characters. For example, if we have
  7041. x"$*"y or "x$*y" and there are no positional parameters,
  7042. the $* should expand into nothing. */
  7043. /* We use the W_HASQUOTEDNULL flag to differentiate the
  7044. cases: a quoted null character as above and when
  7045. CTLNUL is contained in the (non-null) expansion
  7046. of some variable. We use the had_quoted_null flag to
  7047. pass the value through this function to its caller. */
  7048. if ((tflag & W_HASQUOTEDNULL) && QUOTED_NULL (temp) == 0)
  7049. remove_quoted_nulls (temp); /* XXX */
  7050. }
  7051. }
  7052. else
  7053. temp = (char *)NULL;
  7054. /* We do not want to add quoted nulls to strings that are only
  7055. partially quoted; we can throw them away. */
  7056. if (temp == 0 && quoted_state == PARTIALLY_QUOTED)
  7057. continue;
  7058. add_quoted_string:
  7059. if (temp)
  7060. {
  7061. temp1 = temp;
  7062. temp = quote_string (temp);
  7063. free (temp1);
  7064. goto add_string;
  7065. }
  7066. else
  7067. {
  7068. /* Add NULL arg. */
  7069. c = CTLNUL;
  7070. sindex--; /* add_character: label increments sindex */
  7071. goto add_character;
  7072. }
  7073. /* break; */
  7074. case '\'':
  7075. #if 0
  7076. if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (word->flags & W_DQUOTE))
  7077. #else
  7078. if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)))
  7079. #endif
  7080. goto add_character;
  7081. t_index = ++sindex;
  7082. temp = string_extract_single_quoted (string, &sindex);
  7083. /* If the entire STRING was surrounded by single quotes,
  7084. then the string is wholly quoted. */
  7085. quoted_state = (t_index == 1 && string[sindex] == '\0')
  7086. ? WHOLLY_QUOTED
  7087. : PARTIALLY_QUOTED;
  7088. /* If all we had was '', it is a null expansion. */
  7089. if (*temp == '\0')
  7090. {
  7091. free (temp);
  7092. temp = (char *)NULL;
  7093. }
  7094. else
  7095. remove_quoted_escapes (temp); /* ??? */
  7096. /* We do not want to add quoted nulls to strings that are only
  7097. partially quoted; such nulls are discarded. */
  7098. if (temp == 0 && (quoted_state == PARTIALLY_QUOTED))
  7099. continue;
  7100. /* If we have a quoted null expansion, add a quoted NULL to istring. */
  7101. if (temp == 0)
  7102. {
  7103. c = CTLNUL;
  7104. sindex--; /* add_character: label increments sindex */
  7105. goto add_character;
  7106. }
  7107. else
  7108. goto add_quoted_string;
  7109. /* break; */
  7110. default:
  7111. /* This is the fix for " $@ " */
  7112. add_ifs_character:
  7113. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || (isexp == 0 && isifs (c)))
  7114. {
  7115. if (string[sindex]) /* from old goto dollar_add_string */
  7116. sindex++;
  7117. if (c == 0)
  7118. {
  7119. c = CTLNUL;
  7120. goto add_character;
  7121. }
  7122. else
  7123. {
  7124. #if HANDLE_MULTIBYTE
  7125. if (MB_CUR_MAX > 1)
  7126. sindex--;
  7127. if (MB_CUR_MAX > 1)
  7128. {
  7129. SADD_MBQCHAR_BODY(temp, string, sindex, string_size);
  7130. }
  7131. else
  7132. #endif
  7133. {
  7134. twochars[0] = CTLESC;
  7135. twochars[1] = c;
  7136. goto add_twochars;
  7137. }
  7138. }
  7139. }
  7140. SADD_MBCHAR (temp, string, sindex, string_size);
  7141. add_character:
  7142. RESIZE_MALLOCED_BUFFER (istring, istring_index, 1, istring_size,
  7143. DEFAULT_ARRAY_SIZE);
  7144. istring[istring_index++] = c;
  7145. istring[istring_index] = '\0';
  7146. /* Next character. */
  7147. sindex++;
  7148. }
  7149. }
  7150. finished_with_string:
  7151. /* OK, we're ready to return. If we have a quoted string, and
  7152. quoted_dollar_at is not set, we do no splitting at all; otherwise
  7153. we split on ' '. The routines that call this will handle what to
  7154. do if nothing has been expanded. */
  7155. /* Partially and wholly quoted strings which expand to the empty
  7156. string are retained as an empty arguments. Unquoted strings
  7157. which expand to the empty string are discarded. The single
  7158. exception is the case of expanding "$@" when there are no
  7159. positional parameters. In that case, we discard the expansion. */
  7160. /* Because of how the code that handles "" and '' in partially
  7161. quoted strings works, we need to make ISTRING into a QUOTED_NULL
  7162. if we saw quoting characters, but the expansion was empty.
  7163. "" and '' are tossed away before we get to this point when
  7164. processing partially quoted strings. This makes "" and $xxx""
  7165. equivalent when xxx is unset. We also look to see whether we
  7166. saw a quoted null from a ${} expansion and add one back if we
  7167. need to. */
  7168. /* If we expand to nothing and there were no single or double quotes
  7169. in the word, we throw it away. Otherwise, we return a NULL word.
  7170. The single exception is for $@ surrounded by double quotes when
  7171. there are no positional parameters. In that case, we also throw
  7172. the word away. */
  7173. if (*istring == '\0')
  7174. {
  7175. if (quoted_dollar_at == 0 && (had_quoted_null || quoted_state == PARTIALLY_QUOTED))
  7176. {
  7177. istring[0] = CTLNUL;
  7178. istring[1] = '\0';
  7179. tword = make_bare_word (istring);
  7180. tword->flags |= W_HASQUOTEDNULL; /* XXX */
  7181. list = make_word_list (tword, (WORD_LIST *)NULL);
  7182. if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  7183. tword->flags |= W_QUOTED;
  7184. }
  7185. /* According to sh, ksh, and Posix.2, if a word expands into nothing
  7186. and a double-quoted "$@" appears anywhere in it, then the entire
  7187. word is removed. */
  7188. else if (quoted_state == UNQUOTED || quoted_dollar_at)
  7189. list = (WORD_LIST *)NULL;
  7190. #if 0
  7191. else
  7192. {
  7193. tword = make_bare_word (istring);
  7194. if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  7195. tword->flags |= W_QUOTED;
  7196. list = make_word_list (tword, (WORD_LIST *)NULL);
  7197. }
  7198. #else
  7199. else
  7200. list = (WORD_LIST *)NULL;
  7201. #endif
  7202. }
  7203. else if (word->flags & W_NOSPLIT)
  7204. {
  7205. tword = make_bare_word (istring);
  7206. if (word->flags & W_ASSIGNMENT)
  7207. tword->flags |= W_ASSIGNMENT; /* XXX */
  7208. if (word->flags & W_COMPASSIGN)
  7209. tword->flags |= W_COMPASSIGN; /* XXX */
  7210. if (word->flags & W_NOGLOB)
  7211. tword->flags |= W_NOGLOB; /* XXX */
  7212. if (word->flags & W_NOEXPAND)
  7213. tword->flags |= W_NOEXPAND; /* XXX */
  7214. if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))
  7215. tword->flags |= W_QUOTED;
  7216. if (had_quoted_null)
  7217. tword->flags |= W_HASQUOTEDNULL;
  7218. list = make_word_list (tword, (WORD_LIST *)NULL);
  7219. }
  7220. else
  7221. {
  7222. char *ifs_chars;
  7223. ifs_chars = (quoted_dollar_at || has_dollar_at) ? ifs_value : (char *)NULL;
  7224. /* If we have $@, we need to split the results no matter what. If
  7225. IFS is unset or NULL, string_list_dollar_at has separated the
  7226. positional parameters with a space, so we split on space (we have
  7227. set ifs_chars to " \t\n" above if ifs is unset). If IFS is set,
  7228. string_list_dollar_at has separated the positional parameters
  7229. with the first character of $IFS, so we split on $IFS. */
  7230. if (has_dollar_at && ifs_chars)
  7231. list = list_string (istring, *ifs_chars ? ifs_chars : " ", 1);
  7232. else
  7233. {
  7234. tword = make_bare_word (istring);
  7235. if ((quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) || (quoted_state == WHOLLY_QUOTED))
  7236. tword->flags |= W_QUOTED;
  7237. if (word->flags & W_ASSIGNMENT)
  7238. tword->flags |= W_ASSIGNMENT;
  7239. if (word->flags & W_COMPASSIGN)
  7240. tword->flags |= W_COMPASSIGN;
  7241. if (word->flags & W_NOGLOB)
  7242. tword->flags |= W_NOGLOB;
  7243. if (word->flags & W_NOEXPAND)
  7244. tword->flags |= W_NOEXPAND;
  7245. if (had_quoted_null)
  7246. tword->flags |= W_HASQUOTEDNULL; /* XXX */
  7247. list = make_word_list (tword, (WORD_LIST *)NULL);
  7248. }
  7249. }
  7250. free (istring);
  7251. return (list);
  7252. }
  7253. /* **************************************************************** */
  7254. /* */
  7255. /* Functions for Quote Removal */
  7256. /* */
  7257. /* **************************************************************** */
  7258. /* Perform quote removal on STRING. If QUOTED > 0, assume we are obeying the
  7259. backslash quoting rules for within double quotes or a here document. */
  7260. char *
  7261. string_quote_removal (string, quoted)
  7262. char *string;
  7263. int quoted;
  7264. {
  7265. size_t slen;
  7266. char *r, *result_string, *temp, *send;
  7267. int sindex, tindex, dquote;
  7268. unsigned char c;
  7269. DECLARE_MBSTATE;
  7270. /* The result can be no longer than the original string. */
  7271. slen = strlen (string);
  7272. send = string + slen;
  7273. r = result_string = (char *)xmalloc (slen + 1);
  7274. for (dquote = sindex = 0; c = string[sindex];)
  7275. {
  7276. switch (c)
  7277. {
  7278. case '\\':
  7279. c = string[++sindex];
  7280. if (c == 0)
  7281. {
  7282. *r++ = '\\';
  7283. break;
  7284. }
  7285. if (((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote) && (sh_syntaxtab[c] & CBSDQUOTE) == 0)
  7286. *r++ = '\\';
  7287. /* FALLTHROUGH */
  7288. default:
  7289. SCOPY_CHAR_M (r, string, send, sindex);
  7290. break;
  7291. case '\'':
  7292. if ((quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) || dquote)
  7293. {
  7294. *r++ = c;
  7295. sindex++;
  7296. break;
  7297. }
  7298. tindex = sindex + 1;
  7299. temp = string_extract_single_quoted (string, &tindex);
  7300. if (temp)
  7301. {
  7302. strcpy (r, temp);
  7303. r += strlen (r);
  7304. free (temp);
  7305. }
  7306. sindex = tindex;
  7307. break;
  7308. case '"':
  7309. dquote = 1 - dquote;
  7310. sindex++;
  7311. break;
  7312. }
  7313. }
  7314. *r = '\0';
  7315. return (result_string);
  7316. }
  7317. #if 0
  7318. /* UNUSED */
  7319. /* Perform quote removal on word WORD. This allocates and returns a new
  7320. WORD_DESC *. */
  7321. WORD_DESC *
  7322. word_quote_removal (word, quoted)
  7323. WORD_DESC *word;
  7324. int quoted;
  7325. {
  7326. WORD_DESC *w;
  7327. char *t;
  7328. t = string_quote_removal (word->word, quoted);
  7329. w = alloc_word_desc ();
  7330. w->word = t ? t : savestring ("");
  7331. return (w);
  7332. }
  7333. /* Perform quote removal on all words in LIST. If QUOTED is non-zero,
  7334. the members of the list are treated as if they are surrounded by
  7335. double quotes. Return a new list, or NULL if LIST is NULL. */
  7336. WORD_LIST *
  7337. word_list_quote_removal (list, quoted)
  7338. WORD_LIST *list;
  7339. int quoted;
  7340. {
  7341. WORD_LIST *result, *t, *tresult, *e;
  7342. for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
  7343. {
  7344. tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL);
  7345. #if 0
  7346. result = (WORD_LIST *) list_append (result, tresult);
  7347. #else
  7348. if (result == 0)
  7349. result = e = tresult;
  7350. else
  7351. {
  7352. e->next = tresult;
  7353. while (e->next)
  7354. e = e->next;
  7355. }
  7356. #endif
  7357. }
  7358. return (result);
  7359. }
  7360. #endif
  7361. /*******************************************
  7362. * *
  7363. * Functions to perform word splitting *
  7364. * *
  7365. *******************************************/
  7366. void
  7367. setifs (v)
  7368. SHELL_VAR *v;
  7369. {
  7370. char *t;
  7371. unsigned char uc;
  7372. ifs_var = v;
  7373. ifs_value = (v && value_cell (v)) ? value_cell (v) : " \t\n";
  7374. /* Should really merge ifs_cmap with sh_syntaxtab. XXX - doesn't yet
  7375. handle multibyte chars in IFS */
  7376. memset (ifs_cmap, '\0', sizeof (ifs_cmap));
  7377. for (t = ifs_value ; t && *t; t++)
  7378. {
  7379. uc = *t;
  7380. ifs_cmap[uc] = 1;
  7381. }
  7382. #if defined (HANDLE_MULTIBYTE)
  7383. if (ifs_value == 0)
  7384. {
  7385. ifs_firstc[0] = '\0';
  7386. ifs_firstc_len = 1;
  7387. }
  7388. else
  7389. {
  7390. size_t ifs_len;
  7391. ifs_len = strnlen (ifs_value, MB_CUR_MAX);
  7392. ifs_firstc_len = MBLEN (ifs_value, ifs_len);
  7393. if (ifs_firstc_len == 1 || ifs_firstc_len == 0 || MB_INVALIDCH (ifs_firstc_len))
  7394. {
  7395. ifs_firstc[0] = ifs_value[0];
  7396. ifs_firstc[1] = '\0';
  7397. ifs_firstc_len = 1;
  7398. }
  7399. else
  7400. memcpy (ifs_firstc, ifs_value, ifs_firstc_len);
  7401. }
  7402. #else
  7403. ifs_firstc = ifs_value ? *ifs_value : 0;
  7404. #endif
  7405. }
  7406. char *
  7407. getifs ()
  7408. {
  7409. return ifs_value;
  7410. }
  7411. /* This splits a single word into a WORD LIST on $IFS, but only if the word
  7412. is not quoted. list_string () performs quote removal for us, even if we
  7413. don't do any splitting. */
  7414. WORD_LIST *
  7415. word_split (w, ifs_chars)
  7416. WORD_DESC *w;
  7417. char *ifs_chars;
  7418. {
  7419. WORD_LIST *result;
  7420. if (w)
  7421. {
  7422. char *xifs;
  7423. xifs = ((w->flags & W_QUOTED) || ifs_chars == 0) ? "" : ifs_chars;
  7424. result = list_string (w->word, xifs, w->flags & W_QUOTED);
  7425. }
  7426. else
  7427. result = (WORD_LIST *)NULL;
  7428. return (result);
  7429. }
  7430. /* Perform word splitting on LIST and return the RESULT. It is possible
  7431. to return (WORD_LIST *)NULL. */
  7432. static WORD_LIST *
  7433. word_list_split (list)
  7434. WORD_LIST *list;
  7435. {
  7436. WORD_LIST *result, *t, *tresult, *e;
  7437. for (t = list, result = (WORD_LIST *)NULL; t; t = t->next)
  7438. {
  7439. tresult = word_split (t->word, ifs_value);
  7440. if (result == 0)
  7441. result = e = tresult;
  7442. else
  7443. {
  7444. e->next = tresult;
  7445. while (e->next)
  7446. e = e->next;
  7447. }
  7448. }
  7449. return (result);
  7450. }
  7451. /**************************************************
  7452. * *
  7453. * Functions to expand an entire WORD_LIST *
  7454. * *
  7455. **************************************************/
  7456. /* Do any word-expansion-specific cleanup and jump to top_level */
  7457. static void
  7458. exp_jump_to_top_level (v)
  7459. int v;
  7460. {
  7461. set_pipestatus_from_exit (last_command_exit_value);
  7462. /* Cleanup code goes here. */
  7463. expand_no_split_dollar_star = 0; /* XXX */
  7464. expanding_redir = 0;
  7465. assigning_in_environment = 0;
  7466. if (parse_and_execute_level == 0)
  7467. top_level_cleanup (); /* from sig.c */
  7468. jump_to_top_level (v);
  7469. }
  7470. /* Put NLIST (which is a WORD_LIST * of only one element) at the front of
  7471. ELIST, and set ELIST to the new list. */
  7472. #define PREPEND_LIST(nlist, elist) \
  7473. do { nlist->next = elist; elist = nlist; } while (0)
  7474. /* Separate out any initial variable assignments from TLIST. If set -k has
  7475. been executed, remove all assignment statements from TLIST. Initial
  7476. variable assignments and other environment assignments are placed
  7477. on SUBST_ASSIGN_VARLIST. */
  7478. static WORD_LIST *
  7479. separate_out_assignments (tlist)
  7480. WORD_LIST *tlist;
  7481. {
  7482. register WORD_LIST *vp, *lp;
  7483. if (tlist == 0)
  7484. return ((WORD_LIST *)NULL);
  7485. if (subst_assign_varlist)
  7486. dispose_words (subst_assign_varlist); /* Clean up after previous error */
  7487. subst_assign_varlist = (WORD_LIST *)NULL;
  7488. vp = lp = tlist;
  7489. /* Separate out variable assignments at the start of the command.
  7490. Loop invariant: vp->next == lp
  7491. Loop postcondition:
  7492. lp = list of words left after assignment statements skipped
  7493. tlist = original list of words
  7494. */
  7495. while (lp && (lp->word->flags & W_ASSIGNMENT))
  7496. {
  7497. vp = lp;
  7498. lp = lp->next;
  7499. }
  7500. /* If lp != tlist, we have some initial assignment statements.
  7501. We make SUBST_ASSIGN_VARLIST point to the list of assignment
  7502. words and TLIST point to the remaining words. */
  7503. if (lp != tlist)
  7504. {
  7505. subst_assign_varlist = tlist;
  7506. /* ASSERT(vp->next == lp); */
  7507. vp->next = (WORD_LIST *)NULL; /* terminate variable list */
  7508. tlist = lp; /* remainder of word list */
  7509. }
  7510. /* vp == end of variable list */
  7511. /* tlist == remainder of original word list without variable assignments */
  7512. if (!tlist)
  7513. /* All the words in tlist were assignment statements */
  7514. return ((WORD_LIST *)NULL);
  7515. /* ASSERT(tlist != NULL); */
  7516. /* ASSERT((tlist->word->flags & W_ASSIGNMENT) == 0); */
  7517. /* If the -k option is in effect, we need to go through the remaining
  7518. words, separate out the assignment words, and place them on
  7519. SUBST_ASSIGN_VARLIST. */
  7520. if (place_keywords_in_env)
  7521. {
  7522. WORD_LIST *tp; /* tp == running pointer into tlist */
  7523. tp = tlist;
  7524. lp = tlist->next;
  7525. /* Loop Invariant: tp->next == lp */
  7526. /* Loop postcondition: tlist == word list without assignment statements */
  7527. while (lp)
  7528. {
  7529. if (lp->word->flags & W_ASSIGNMENT)
  7530. {
  7531. /* Found an assignment statement, add this word to end of
  7532. subst_assign_varlist (vp). */
  7533. if (!subst_assign_varlist)
  7534. subst_assign_varlist = vp = lp;
  7535. else
  7536. {
  7537. vp->next = lp;
  7538. vp = lp;
  7539. }
  7540. /* Remove the word pointed to by LP from TLIST. */
  7541. tp->next = lp->next;
  7542. /* ASSERT(vp == lp); */
  7543. lp->next = (WORD_LIST *)NULL;
  7544. lp = tp->next;
  7545. }
  7546. else
  7547. {
  7548. tp = lp;
  7549. lp = lp->next;
  7550. }
  7551. }
  7552. }
  7553. return (tlist);
  7554. }
  7555. #define WEXP_VARASSIGN 0x001
  7556. #define WEXP_BRACEEXP 0x002
  7557. #define WEXP_TILDEEXP 0x004
  7558. #define WEXP_PARAMEXP 0x008
  7559. #define WEXP_PATHEXP 0x010
  7560. /* All of the expansions, including variable assignments at the start of
  7561. the list. */
  7562. #define WEXP_ALL (WEXP_VARASSIGN|WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
  7563. /* All of the expansions except variable assignments at the start of
  7564. the list. */
  7565. #define WEXP_NOVARS (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP|WEXP_PATHEXP)
  7566. /* All of the `shell expansions': brace expansion, tilde expansion, parameter
  7567. expansion, command substitution, arithmetic expansion, word splitting, and
  7568. quote removal. */
  7569. #define WEXP_SHELLEXP (WEXP_BRACEEXP|WEXP_TILDEEXP|WEXP_PARAMEXP)
  7570. /* Take the list of words in LIST and do the various substitutions. Return
  7571. a new list of words which is the expanded list, and without things like
  7572. variable assignments. */
  7573. WORD_LIST *
  7574. expand_words (list)
  7575. WORD_LIST *list;
  7576. {
  7577. return (expand_word_list_internal (list, WEXP_ALL));
  7578. }
  7579. /* Same as expand_words (), but doesn't hack variable or environment
  7580. variables. */
  7581. WORD_LIST *
  7582. expand_words_no_vars (list)
  7583. WORD_LIST *list;
  7584. {
  7585. return (expand_word_list_internal (list, WEXP_NOVARS));
  7586. }
  7587. WORD_LIST *
  7588. expand_words_shellexp (list)
  7589. WORD_LIST *list;
  7590. {
  7591. return (expand_word_list_internal (list, WEXP_SHELLEXP));
  7592. }
  7593. static WORD_LIST *
  7594. glob_expand_word_list (tlist, eflags)
  7595. WORD_LIST *tlist;
  7596. int eflags;
  7597. {
  7598. char **glob_array, *temp_string;
  7599. register int glob_index;
  7600. WORD_LIST *glob_list, *output_list, *disposables, *next;
  7601. WORD_DESC *tword;
  7602. output_list = disposables = (WORD_LIST *)NULL;
  7603. glob_array = (char **)NULL;
  7604. while (tlist)
  7605. {
  7606. /* For each word, either globbing is attempted or the word is
  7607. added to orig_list. If globbing succeeds, the results are
  7608. added to orig_list and the word (tlist) is added to the list
  7609. of disposable words. If globbing fails and failed glob
  7610. expansions are left unchanged (the shell default), the
  7611. original word is added to orig_list. If globbing fails and
  7612. failed glob expansions are removed, the original word is
  7613. added to the list of disposable words. orig_list ends up
  7614. in reverse order and requires a call to REVERSE_LIST to
  7615. be set right. After all words are examined, the disposable
  7616. words are freed. */
  7617. next = tlist->next;
  7618. /* If the word isn't an assignment and contains an unquoted
  7619. pattern matching character, then glob it. */
  7620. if ((tlist->word->flags & W_NOGLOB) == 0 &&
  7621. unquoted_glob_pattern_p (tlist->word->word))
  7622. {
  7623. glob_array = shell_glob_filename (tlist->word->word);
  7624. /* Handle error cases.
  7625. I don't think we should report errors like "No such file
  7626. or directory". However, I would like to report errors
  7627. like "Read failed". */
  7628. if (glob_array == 0 || GLOB_FAILED (glob_array))
  7629. {
  7630. glob_array = (char **)xmalloc (sizeof (char *));
  7631. glob_array[0] = (char *)NULL;
  7632. }
  7633. /* Dequote the current word in case we have to use it. */
  7634. if (glob_array[0] == NULL)
  7635. {
  7636. temp_string = dequote_string (tlist->word->word);
  7637. free (tlist->word->word);
  7638. tlist->word->word = temp_string;
  7639. }
  7640. /* Make the array into a word list. */
  7641. glob_list = (WORD_LIST *)NULL;
  7642. for (glob_index = 0; glob_array[glob_index]; glob_index++)
  7643. {
  7644. tword = make_bare_word (glob_array[glob_index]);
  7645. tword->flags |= W_GLOBEXP; /* XXX */
  7646. glob_list = make_word_list (tword, glob_list);
  7647. }
  7648. if (glob_list)
  7649. {
  7650. output_list = (WORD_LIST *)list_append (glob_list, output_list);
  7651. PREPEND_LIST (tlist, disposables);
  7652. }
  7653. else if (fail_glob_expansion != 0)
  7654. {
  7655. report_error (_("no match: %s"), tlist->word->word);
  7656. exp_jump_to_top_level (DISCARD);
  7657. }
  7658. else if (allow_null_glob_expansion == 0)
  7659. {
  7660. /* Failed glob expressions are left unchanged. */
  7661. PREPEND_LIST (tlist, output_list);
  7662. }
  7663. else
  7664. {
  7665. /* Failed glob expressions are removed. */
  7666. PREPEND_LIST (tlist, disposables);
  7667. }
  7668. }
  7669. else
  7670. {
  7671. /* Dequote the string. */
  7672. temp_string = dequote_string (tlist->word->word);
  7673. free (tlist->word->word);
  7674. tlist->word->word = temp_string;
  7675. PREPEND_LIST (tlist, output_list);
  7676. }
  7677. strvec_dispose (glob_array);
  7678. glob_array = (char **)NULL;
  7679. tlist = next;
  7680. }
  7681. if (disposables)
  7682. dispose_words (disposables);
  7683. if (output_list)
  7684. output_list = REVERSE_LIST (output_list, WORD_LIST *);
  7685. return (output_list);
  7686. }
  7687. #if defined (BRACE_EXPANSION)
  7688. static WORD_LIST *
  7689. brace_expand_word_list (tlist, eflags)
  7690. WORD_LIST *tlist;
  7691. int eflags;
  7692. {
  7693. register char **expansions;
  7694. char *temp_string;
  7695. WORD_LIST *disposables, *output_list, *next;
  7696. WORD_DESC *w;
  7697. int eindex;
  7698. for (disposables = output_list = (WORD_LIST *)NULL; tlist; tlist = next)
  7699. {
  7700. next = tlist->next;
  7701. if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
  7702. {
  7703. /*itrace("brace_expand_word_list: %s: W_COMPASSIGN|W_ASSIGNARG", tlist->word->word);*/
  7704. PREPEND_LIST (tlist, output_list);
  7705. continue;
  7706. }
  7707. /* Only do brace expansion if the word has a brace character. If
  7708. not, just add the word list element to BRACES and continue. In
  7709. the common case, at least when running shell scripts, this will
  7710. degenerate to a bunch of calls to `mbschr', and then what is
  7711. basically a reversal of TLIST into BRACES, which is corrected
  7712. by a call to REVERSE_LIST () on BRACES when the end of TLIST
  7713. is reached. */
  7714. if (mbschr (tlist->word->word, LBRACE))
  7715. {
  7716. expansions = brace_expand (tlist->word->word);
  7717. for (eindex = 0; temp_string = expansions[eindex]; eindex++)
  7718. {
  7719. w = make_word (temp_string);
  7720. /* If brace expansion didn't change the word, preserve
  7721. the flags. We may want to preserve the flags
  7722. unconditionally someday -- XXX */
  7723. if (STREQ (temp_string, tlist->word->word))
  7724. w->flags = tlist->word->flags;
  7725. output_list = make_word_list (w, output_list);
  7726. free (expansions[eindex]);
  7727. }
  7728. free (expansions);
  7729. /* Add TLIST to the list of words to be freed after brace
  7730. expansion has been performed. */
  7731. PREPEND_LIST (tlist, disposables);
  7732. }
  7733. else
  7734. PREPEND_LIST (tlist, output_list);
  7735. }
  7736. if (disposables)
  7737. dispose_words (disposables);
  7738. if (output_list)
  7739. output_list = REVERSE_LIST (output_list, WORD_LIST *);
  7740. return (output_list);
  7741. }
  7742. #endif
  7743. #if defined (ARRAY_VARS)
  7744. /* Take WORD, a compound associative array assignment, and internally run
  7745. 'declare -A w', where W is the variable name portion of WORD. */
  7746. static int
  7747. make_internal_declare (word, option)
  7748. char *word;
  7749. char *option;
  7750. {
  7751. int t;
  7752. WORD_LIST *wl;
  7753. WORD_DESC *w;
  7754. w = make_word (word);
  7755. t = assignment (w->word, 0);
  7756. w->word[t] = '\0';
  7757. wl = make_word_list (w, (WORD_LIST *)NULL);
  7758. wl = make_word_list (make_word (option), wl);
  7759. return (declare_builtin (wl));
  7760. }
  7761. #endif
  7762. static WORD_LIST *
  7763. shell_expand_word_list (tlist, eflags)
  7764. WORD_LIST *tlist;
  7765. int eflags;
  7766. {
  7767. WORD_LIST *expanded, *orig_list, *new_list, *next, *temp_list;
  7768. int expanded_something, has_dollar_at;
  7769. char *temp_string;
  7770. /* We do tilde expansion all the time. This is what 1003.2 says. */
  7771. new_list = (WORD_LIST *)NULL;
  7772. for (orig_list = tlist; tlist; tlist = next)
  7773. {
  7774. temp_string = tlist->word->word;
  7775. next = tlist->next;
  7776. #if defined (ARRAY_VARS)
  7777. /* If this is a compound array assignment to a builtin that accepts
  7778. such assignments (e.g., `declare'), take the assignment and perform
  7779. it separately, handling the semantics of declarations inside shell
  7780. functions. This avoids the double-evaluation of such arguments,
  7781. because `declare' does some evaluation of compound assignments on
  7782. its own. */
  7783. if ((tlist->word->flags & (W_COMPASSIGN|W_ASSIGNARG)) == (W_COMPASSIGN|W_ASSIGNARG))
  7784. {
  7785. int t;
  7786. if (tlist->word->flags & W_ASSIGNASSOC)
  7787. make_internal_declare (tlist->word->word, "-A");
  7788. t = do_word_assignment (tlist->word);
  7789. if (t == 0)
  7790. {
  7791. last_command_exit_value = EXECUTION_FAILURE;
  7792. exp_jump_to_top_level (DISCARD);
  7793. }
  7794. /* Now transform the word as ksh93 appears to do and go on */
  7795. t = assignment (tlist->word->word, 0);
  7796. tlist->word->word[t] = '\0';
  7797. tlist->word->flags &= ~(W_ASSIGNMENT|W_NOSPLIT|W_COMPASSIGN|W_ASSIGNARG|W_ASSIGNASSOC);
  7798. }
  7799. #endif
  7800. expanded_something = 0;
  7801. expanded = expand_word_internal
  7802. (tlist->word, 0, 0, &has_dollar_at, &expanded_something);
  7803. if (expanded == &expand_word_error || expanded == &expand_word_fatal)
  7804. {
  7805. /* By convention, each time this error is returned,
  7806. tlist->word->word has already been freed. */
  7807. tlist->word->word = (char *)NULL;
  7808. /* Dispose our copy of the original list. */
  7809. dispose_words (orig_list);
  7810. /* Dispose the new list we're building. */
  7811. dispose_words (new_list);
  7812. last_command_exit_value = EXECUTION_FAILURE;
  7813. if (expanded == &expand_word_error)
  7814. exp_jump_to_top_level (DISCARD);
  7815. else
  7816. exp_jump_to_top_level (FORCE_EOF);
  7817. }
  7818. /* Don't split words marked W_NOSPLIT. */
  7819. if (expanded_something && (tlist->word->flags & W_NOSPLIT) == 0)
  7820. {
  7821. temp_list = word_list_split (expanded);
  7822. dispose_words (expanded);
  7823. }
  7824. else
  7825. {
  7826. /* If no parameter expansion, command substitution, process
  7827. substitution, or arithmetic substitution took place, then
  7828. do not do word splitting. We still have to remove quoted
  7829. null characters from the result. */
  7830. word_list_remove_quoted_nulls (expanded);
  7831. temp_list = expanded;
  7832. }
  7833. expanded = REVERSE_LIST (temp_list, WORD_LIST *);
  7834. new_list = (WORD_LIST *)list_append (expanded, new_list);
  7835. }
  7836. if (orig_list)
  7837. dispose_words (orig_list);
  7838. if (new_list)
  7839. new_list = REVERSE_LIST (new_list, WORD_LIST *);
  7840. return (new_list);
  7841. }
  7842. /* The workhorse for expand_words () and expand_words_no_vars ().
  7843. First arg is LIST, a WORD_LIST of words.
  7844. Second arg EFLAGS is a flags word controlling which expansions are
  7845. performed.
  7846. This does all of the substitutions: brace expansion, tilde expansion,
  7847. parameter expansion, command substitution, arithmetic expansion,
  7848. process substitution, word splitting, and pathname expansion, according
  7849. to the bits set in EFLAGS. Words with the W_QUOTED or W_NOSPLIT bits
  7850. set, or for which no expansion is done, do not undergo word splitting.
  7851. Words with the W_NOGLOB bit set do not undergo pathname expansion. */
  7852. static WORD_LIST *
  7853. expand_word_list_internal (list, eflags)
  7854. WORD_LIST *list;
  7855. int eflags;
  7856. {
  7857. WORD_LIST *new_list, *temp_list;
  7858. int tint;
  7859. if (list == 0)
  7860. return ((WORD_LIST *)NULL);
  7861. garglist = new_list = copy_word_list (list);
  7862. if (eflags & WEXP_VARASSIGN)
  7863. {
  7864. garglist = new_list = separate_out_assignments (new_list);
  7865. if (new_list == 0)
  7866. {
  7867. if (subst_assign_varlist)
  7868. {
  7869. /* All the words were variable assignments, so they are placed
  7870. into the shell's environment. */
  7871. for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
  7872. {
  7873. this_command_name = (char *)NULL; /* no arithmetic errors */
  7874. tint = do_word_assignment (temp_list->word);
  7875. /* Variable assignment errors in non-interactive shells
  7876. running in Posix.2 mode cause the shell to exit. */
  7877. if (tint == 0)
  7878. {
  7879. last_command_exit_value = EXECUTION_FAILURE;
  7880. if (interactive_shell == 0 && posixly_correct)
  7881. exp_jump_to_top_level (FORCE_EOF);
  7882. else
  7883. exp_jump_to_top_level (DISCARD);
  7884. }
  7885. }
  7886. dispose_words (subst_assign_varlist);
  7887. subst_assign_varlist = (WORD_LIST *)NULL;
  7888. }
  7889. return ((WORD_LIST *)NULL);
  7890. }
  7891. }
  7892. /* Begin expanding the words that remain. The expansions take place on
  7893. things that aren't really variable assignments. */
  7894. #if defined (BRACE_EXPANSION)
  7895. /* Do brace expansion on this word if there are any brace characters
  7896. in the string. */
  7897. if ((eflags & WEXP_BRACEEXP) && brace_expansion && new_list)
  7898. new_list = brace_expand_word_list (new_list, eflags);
  7899. #endif /* BRACE_EXPANSION */
  7900. /* Perform the `normal' shell expansions: tilde expansion, parameter and
  7901. variable substitution, command substitution, arithmetic expansion,
  7902. and word splitting. */
  7903. new_list = shell_expand_word_list (new_list, eflags);
  7904. /* Okay, we're almost done. Now let's just do some filename
  7905. globbing. */
  7906. if (new_list)
  7907. {
  7908. if ((eflags & WEXP_PATHEXP) && disallow_filename_globbing == 0)
  7909. /* Glob expand the word list unless globbing has been disabled. */
  7910. new_list = glob_expand_word_list (new_list, eflags);
  7911. else
  7912. /* Dequote the words, because we're not performing globbing. */
  7913. new_list = dequote_list (new_list);
  7914. }
  7915. if ((eflags & WEXP_VARASSIGN) && subst_assign_varlist)
  7916. {
  7917. sh_wassign_func_t *assign_func;
  7918. /* If the remainder of the words expand to nothing, Posix.2 requires
  7919. that the variable and environment assignments affect the shell's
  7920. environment. */
  7921. assign_func = new_list ? assign_in_env : do_word_assignment;
  7922. tempenv_assign_error = 0;
  7923. for (temp_list = subst_assign_varlist; temp_list; temp_list = temp_list->next)
  7924. {
  7925. this_command_name = (char *)NULL;
  7926. assigning_in_environment = (assign_func == assign_in_env);
  7927. tint = (*assign_func) (temp_list->word);
  7928. assigning_in_environment = 0;
  7929. /* Variable assignment errors in non-interactive shells running
  7930. in Posix.2 mode cause the shell to exit. */
  7931. if (tint == 0)
  7932. {
  7933. if (assign_func == do_word_assignment)
  7934. {
  7935. last_command_exit_value = EXECUTION_FAILURE;
  7936. if (interactive_shell == 0 && posixly_correct)
  7937. exp_jump_to_top_level (FORCE_EOF);
  7938. else
  7939. exp_jump_to_top_level (DISCARD);
  7940. }
  7941. else
  7942. tempenv_assign_error++;
  7943. }
  7944. }
  7945. dispose_words (subst_assign_varlist);
  7946. subst_assign_varlist = (WORD_LIST *)NULL;
  7947. }
  7948. #if 0
  7949. tint = list_length (new_list) + 1;
  7950. RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16);
  7951. for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next)
  7952. glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';
  7953. glob_argv_flags[tint] = '\0';
  7954. #endif
  7955. return (new_list);
  7956. }