wordexp.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. /* vi: set sw=4 ts=4: */
  2. /* POSIX.2 wordexp implementation.
  3. Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. #include <features.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <fcntl.h>
  22. #include <paths.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <pwd.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <fnmatch.h>
  31. #include <glob.h>
  32. #include <wordexp.h>
  33. #define __WORDEXP_FULL
  34. /*
  35. * This is a recursive-descent-style word expansion routine.
  36. */
  37. /* These variables are defined and initialized in the startup code. */
  38. /*extern int __libc_argc;*/
  39. /*extern char **__libc_argv;*/
  40. /* FIXME!!!! */
  41. int attribute_hidden __libc_argc;
  42. char attribute_hidden **__libc_argv;
  43. /* Some forward declarations */
  44. static int parse_dollars(char **word, size_t * word_length,
  45. size_t * max_length, const char *words,
  46. size_t * offset, int flags, wordexp_t * pwordexp,
  47. const char *ifs, const char *ifs_white,
  48. int quoted);
  49. static int parse_backtick(char **word, size_t * word_length,
  50. size_t * max_length, const char *words,
  51. size_t * offset, int flags, wordexp_t * pwordexp,
  52. const char *ifs, const char *ifs_white);
  53. static int parse_dquote(char **word, size_t * word_length,
  54. size_t * max_length, const char *words,
  55. size_t * offset, int flags, wordexp_t * pwordexp,
  56. const char *ifs, const char *ifs_white);
  57. /* The w_*() functions manipulate word lists. */
  58. #define W_CHUNK (100)
  59. /* Result of w_newword will be ignored if it's the last word. */
  60. static __inline__ char *w_newword(size_t * actlen, size_t * maxlen)
  61. {
  62. *actlen = *maxlen = 0;
  63. return NULL;
  64. }
  65. /* Add a character to the buffer, allocating room for it if needed. */
  66. static __inline__ char *w_addchar(char *buffer, size_t * actlen,
  67. size_t * maxlen, char ch)
  68. /* (lengths exclude trailing zero) */
  69. {
  70. if (*actlen == *maxlen) {
  71. char *old_buffer = buffer;
  72. assert(buffer == NULL || *maxlen != 0);
  73. *maxlen += W_CHUNK;
  74. buffer = realloc(buffer, 1 + *maxlen);
  75. if (buffer == NULL)
  76. free(old_buffer);
  77. }
  78. if (buffer != NULL) {
  79. buffer[*actlen] = ch;
  80. buffer[++(*actlen)] = '\0';
  81. }
  82. return buffer;
  83. }
  84. #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
  85. static char *w_addmem(char *buffer, size_t * actlen, size_t * maxlen,
  86. const char *str, size_t len)
  87. {
  88. /* Add a string to the buffer, allocating room for it if needed.
  89. */
  90. if (*actlen + len > *maxlen) {
  91. char *old_buffer = buffer;
  92. assert(buffer == NULL || *maxlen != 0);
  93. *maxlen += MAX(2 * len, W_CHUNK);
  94. buffer = realloc(old_buffer, 1 + *maxlen);
  95. if (buffer == NULL)
  96. free(old_buffer);
  97. }
  98. if (buffer != NULL) {
  99. *((char *) mempcpy(&buffer[*actlen], str, len)) = '\0';
  100. *actlen += len;
  101. }
  102. return buffer;
  103. }
  104. /* Add a string to the buffer, allocating room for it if needed. */
  105. static char *w_addstr(char *buffer, size_t * actlen, size_t * maxlen,
  106. const char *str)
  107. /* (lengths exclude trailing zero) */
  108. {
  109. size_t len;
  110. assert(str != NULL); /* w_addstr only called from this file */
  111. len = strlen(str);
  112. return w_addmem(buffer, actlen, maxlen, str, len);
  113. }
  114. /* Add a word to the wordlist */
  115. static int w_addword(wordexp_t * pwordexp, char *word)
  116. {
  117. size_t num_p;
  118. char **new_wordv;
  119. /* Internally, NULL acts like "". Convert NULLs to "" before
  120. * the caller sees them.
  121. */
  122. if (word == NULL) {
  123. word = strdup("");
  124. if (word == NULL)
  125. goto no_space;
  126. }
  127. num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
  128. new_wordv = realloc(pwordexp->we_wordv, sizeof(char *) * num_p);
  129. if (new_wordv != NULL) {
  130. pwordexp->we_wordv = new_wordv;
  131. pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;
  132. pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;
  133. return 0;
  134. }
  135. no_space:
  136. return WRDE_NOSPACE;
  137. }
  138. /* The parse_*() functions should leave *offset being the offset in 'words'
  139. * to the last character processed.
  140. */
  141. static int
  142. parse_backslash(char **word, size_t * word_length, size_t * max_length,
  143. const char *words, size_t * offset)
  144. {
  145. /* We are poised _at_ a backslash, not in quotes */
  146. switch (words[1 + *offset]) {
  147. case 0:
  148. /* Backslash is last character of input words */
  149. return WRDE_SYNTAX;
  150. case '\n':
  151. ++(*offset);
  152. break;
  153. default:
  154. *word = w_addchar(*word, word_length, max_length, words[1 + *offset]);
  155. if (*word == NULL)
  156. return WRDE_NOSPACE;
  157. ++(*offset);
  158. break;
  159. }
  160. return 0;
  161. }
  162. static int
  163. parse_qtd_backslash(char **word, size_t * word_length, size_t * max_length,
  164. const char *words, size_t * offset)
  165. {
  166. /* We are poised _at_ a backslash, inside quotes */
  167. switch (words[1 + *offset]) {
  168. case 0:
  169. /* Backslash is last character of input words */
  170. return WRDE_SYNTAX;
  171. case '\n':
  172. ++(*offset);
  173. break;
  174. case '$':
  175. case '`':
  176. case '"':
  177. case '\\':
  178. *word =
  179. w_addchar(*word, word_length, max_length, words[1 + *offset]);
  180. if (*word == NULL)
  181. return WRDE_NOSPACE;
  182. ++(*offset);
  183. break;
  184. default:
  185. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  186. if (*word != NULL)
  187. *word =
  188. w_addchar(*word, word_length, max_length,
  189. words[1 + *offset]);
  190. if (*word == NULL)
  191. return WRDE_NOSPACE;
  192. ++(*offset);
  193. break;
  194. }
  195. return 0;
  196. }
  197. static int
  198. parse_tilde(char **word, size_t * word_length, size_t * max_length,
  199. const char *words, size_t * offset, size_t wordc)
  200. {
  201. /* We are poised _at_ a tilde */
  202. size_t i;
  203. if (*word_length != 0) {
  204. if (!((*word)[*word_length - 1] == '=' && wordc == 0)) {
  205. if (!((*word)[*word_length - 1] == ':'
  206. && strchr(*word, '=') && wordc == 0)) {
  207. *word = w_addchar(*word, word_length, max_length, '~');
  208. return *word ? 0 : WRDE_NOSPACE;
  209. }
  210. }
  211. }
  212. for (i = 1 + *offset; words[i]; i++) {
  213. if (words[i] == ':' || words[i] == '/' || words[i] == ' ' ||
  214. words[i] == '\t' || words[i] == 0)
  215. break;
  216. if (words[i] == '\\') {
  217. *word = w_addchar(*word, word_length, max_length, '~');
  218. return *word ? 0 : WRDE_NOSPACE;
  219. }
  220. }
  221. if (i == 1 + *offset) {
  222. /* Tilde appears on its own */
  223. uid_t uid;
  224. struct passwd pwd, *tpwd;
  225. int buflen = 1000;
  226. char *home;
  227. char *buffer;
  228. int result;
  229. /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
  230. results are unspecified. We do a lookup on the uid if
  231. HOME is unset. */
  232. home = getenv("HOME");
  233. if (home != NULL) {
  234. *word = w_addstr(*word, word_length, max_length, home);
  235. if (*word == NULL)
  236. return WRDE_NOSPACE;
  237. } else {
  238. uid = getuid();
  239. buffer = alloca(buflen);
  240. while ((result = getpwuid_r(uid, &pwd, buffer, buflen, &tpwd))
  241. != 0 && errno == ERANGE)
  242. {
  243. buflen += 1000;
  244. buffer = alloca(buflen);
  245. }
  246. if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL) {
  247. *word = w_addstr(*word, word_length, max_length, pwd.pw_dir);
  248. if (*word == NULL)
  249. return WRDE_NOSPACE;
  250. } else {
  251. *word = w_addchar(*word, word_length, max_length, '~');
  252. if (*word == NULL)
  253. return WRDE_NOSPACE;
  254. }
  255. }
  256. } else {
  257. /* Look up user name in database to get home directory */
  258. char *user = strndup(&words[1 + *offset], i - (1 + *offset));
  259. struct passwd pwd, *tpwd;
  260. int buflen = 1000;
  261. char *buffer = alloca(buflen);
  262. int result;
  263. while ((result = getpwnam_r(user, &pwd, buffer, buflen, &tpwd)) != 0
  264. && errno == ERANGE) {
  265. buflen += 1000;
  266. buffer = alloca(buflen);
  267. }
  268. if (result == 0 && tpwd != NULL && pwd.pw_dir)
  269. *word = w_addstr(*word, word_length, max_length, pwd.pw_dir);
  270. else {
  271. /* (invalid login name) */
  272. *word = w_addchar(*word, word_length, max_length, '~');
  273. if (*word != NULL)
  274. *word = w_addstr(*word, word_length, max_length, user);
  275. }
  276. *offset = i - 1;
  277. }
  278. return *word ? 0 : WRDE_NOSPACE;
  279. }
  280. static int
  281. do_parse_glob(const char *glob_word, char **word, size_t * word_length,
  282. size_t * max_length, wordexp_t * pwordexp, const char *ifs
  283. /*, const char *ifs_white*/)
  284. {
  285. int error;
  286. int match;
  287. glob_t globbuf;
  288. error = glob(glob_word, GLOB_NOCHECK, NULL, &globbuf);
  289. if (error != 0) {
  290. /* We can only run into memory problems. */
  291. assert(error == GLOB_NOSPACE);
  292. return WRDE_NOSPACE;
  293. }
  294. if (ifs && !*ifs) {
  295. /* No field splitting allowed. */
  296. assert(globbuf.gl_pathv[0] != NULL);
  297. *word = w_addstr(*word, word_length, max_length, globbuf.gl_pathv[0]);
  298. for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match) {
  299. *word = w_addchar(*word, word_length, max_length, ' ');
  300. if (*word != NULL)
  301. *word = w_addstr(*word, word_length, max_length,
  302. globbuf.gl_pathv[match]);
  303. }
  304. globfree(&globbuf);
  305. return *word ? 0 : WRDE_NOSPACE;
  306. }
  307. assert(ifs == NULL || *ifs != '\0');
  308. if (*word != NULL) {
  309. free(*word);
  310. *word = w_newword(word_length, max_length);
  311. }
  312. for (match = 0; match < globbuf.gl_pathc; ++match) {
  313. char *matching_word = strdup(globbuf.gl_pathv[match]);
  314. if (matching_word == NULL || w_addword(pwordexp, matching_word)) {
  315. globfree(&globbuf);
  316. return WRDE_NOSPACE;
  317. }
  318. }
  319. globfree(&globbuf);
  320. return 0;
  321. }
  322. static int
  323. parse_glob(char **word, size_t * word_length, size_t * max_length,
  324. const char *words, size_t * offset, int flags,
  325. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  326. {
  327. /* We are poised just after a '*', a '[' or a '?'. */
  328. int error = WRDE_NOSPACE;
  329. int quoted = 0; /* 1 if singly-quoted, 2 if doubly */
  330. int i;
  331. wordexp_t glob_list; /* List of words to glob */
  332. glob_list.we_wordc = 0;
  333. glob_list.we_wordv = NULL;
  334. glob_list.we_offs = 0;
  335. for (; words[*offset] != '\0'; ++*offset) {
  336. if ((ifs && strchr(ifs, words[*offset])) ||
  337. (!ifs && strchr(" \t\n", words[*offset])))
  338. /* Reached IFS */
  339. break;
  340. /* Sort out quoting */
  341. if (words[*offset] == '\'') {
  342. if (quoted == 0) {
  343. quoted = 1;
  344. continue;
  345. } else if (quoted == 1) {
  346. quoted = 0;
  347. continue;
  348. }
  349. } else if (words[*offset] == '"') {
  350. if (quoted == 0) {
  351. quoted = 2;
  352. continue;
  353. } else if (quoted == 2) {
  354. quoted = 0;
  355. continue;
  356. }
  357. }
  358. /* Sort out other special characters */
  359. if (quoted != 1 && words[*offset] == '$') {
  360. error = parse_dollars(word, word_length, max_length, words,
  361. offset, flags, &glob_list, ifs,
  362. ifs_white, quoted == 2);
  363. if (error)
  364. goto tidy_up;
  365. continue;
  366. } else if (words[*offset] == '\\') {
  367. if (quoted)
  368. error = parse_qtd_backslash(word, word_length, max_length,
  369. words, offset);
  370. else
  371. error = parse_backslash(word, word_length, max_length,
  372. words, offset);
  373. if (error)
  374. goto tidy_up;
  375. continue;
  376. }
  377. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  378. if (*word == NULL)
  379. goto tidy_up;
  380. }
  381. /* Don't forget to re-parse the character we stopped at. */
  382. --*offset;
  383. /* Glob the words */
  384. error = w_addword(&glob_list, *word);
  385. *word = w_newword(word_length, max_length);
  386. for (i = 0; error == 0 && i < glob_list.we_wordc; i++)
  387. error = do_parse_glob(glob_list.we_wordv[i], word, word_length,
  388. max_length, pwordexp, ifs /*, ifs_white*/);
  389. /* Now tidy up */
  390. tidy_up:
  391. wordfree(&glob_list);
  392. return error;
  393. }
  394. static int
  395. parse_squote(char **word, size_t * word_length, size_t * max_length,
  396. const char *words, size_t * offset)
  397. {
  398. /* We are poised just after a single quote */
  399. for (; words[*offset]; ++(*offset)) {
  400. if (words[*offset] != '\'') {
  401. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  402. if (*word == NULL)
  403. return WRDE_NOSPACE;
  404. } else
  405. return 0;
  406. }
  407. /* Unterminated string */
  408. return WRDE_SYNTAX;
  409. }
  410. #ifdef __WORDEXP_FULL
  411. static int eval_expr(char *expr, long int *result);
  412. static char *_itoa(unsigned long long int value, char *buflim)
  413. {
  414. sprintf(buflim, "%llu", value);
  415. return buflim;
  416. }
  417. /* Functions to evaluate an arithmetic expression */
  418. static int eval_expr_val(char **expr, long int *result)
  419. {
  420. int sgn = +1;
  421. char *digit;
  422. /* Skip white space */
  423. for (digit = *expr; digit && *digit && isspace(*digit); ++digit);
  424. switch (*digit) {
  425. case '(':
  426. /* Scan for closing paren */
  427. for (++digit; **expr && **expr != ')'; ++(*expr));
  428. /* Is there one? */
  429. if (!**expr)
  430. return WRDE_SYNTAX;
  431. *(*expr)++ = 0;
  432. if (eval_expr(digit, result))
  433. return WRDE_SYNTAX;
  434. return 0;
  435. case '+': /* Positive value */
  436. ++digit;
  437. break;
  438. case '-': /* Negative value */
  439. ++digit;
  440. sgn = -1;
  441. break;
  442. default:
  443. if (!isdigit(*digit))
  444. return WRDE_SYNTAX;
  445. }
  446. *result = 0;
  447. for (; *digit && isdigit(*digit); ++digit)
  448. *result = (*result * 10) + (*digit - '0');
  449. *expr = digit;
  450. *result *= sgn;
  451. return 0;
  452. }
  453. static int eval_expr_multdiv(char **expr, long int *result)
  454. {
  455. long int arg;
  456. /* Read a Value */
  457. if (eval_expr_val(expr, result) != 0)
  458. return WRDE_SYNTAX;
  459. while (**expr) {
  460. /* Skip white space */
  461. for (; *expr && **expr && isspace(**expr); ++(*expr));
  462. if (**expr == '*') {
  463. ++(*expr);
  464. if (eval_expr_val(expr, &arg) != 0)
  465. return WRDE_SYNTAX;
  466. *result *= arg;
  467. } else if (**expr == '/') {
  468. ++(*expr);
  469. if (eval_expr_val(expr, &arg) != 0)
  470. return WRDE_SYNTAX;
  471. *result /= arg;
  472. } else
  473. break;
  474. }
  475. return 0;
  476. }
  477. static int eval_expr(char *expr, long int *result)
  478. {
  479. long int arg;
  480. /* Read a Multdiv */
  481. if (eval_expr_multdiv(&expr, result) != 0)
  482. return WRDE_SYNTAX;
  483. while (*expr) {
  484. /* Skip white space */
  485. for (; expr && *expr && isspace(*expr); ++expr);
  486. if (*expr == '+') {
  487. ++expr;
  488. if (eval_expr_multdiv(&expr, &arg) != 0)
  489. return WRDE_SYNTAX;
  490. *result += arg;
  491. } else if (*expr == '-') {
  492. ++expr;
  493. if (eval_expr_multdiv(&expr, &arg) != 0)
  494. return WRDE_SYNTAX;
  495. *result -= arg;
  496. } else
  497. break;
  498. }
  499. return 0;
  500. }
  501. static int
  502. parse_arith(char **word, size_t * word_length, size_t * max_length,
  503. const char *words, size_t * offset, int flags, int bracket)
  504. {
  505. /* We are poised just after "$((" or "$[" */
  506. int error;
  507. int paren_depth = 1;
  508. size_t expr_length;
  509. size_t expr_maxlen;
  510. char *expr;
  511. expr = w_newword(&expr_length, &expr_maxlen);
  512. for (; words[*offset]; ++(*offset)) {
  513. switch (words[*offset]) {
  514. case '$':
  515. error = parse_dollars(&expr, &expr_length, &expr_maxlen,
  516. words, offset, flags, NULL, NULL, NULL,
  517. 1);
  518. /* The ``1'' here is to tell parse_dollars not to
  519. * split the fields.
  520. */
  521. if (error) {
  522. free(expr);
  523. return error;
  524. }
  525. break;
  526. case '`':
  527. (*offset)++;
  528. error = parse_backtick(&expr, &expr_length, &expr_maxlen,
  529. words, offset, flags, NULL, NULL, NULL);
  530. /* The first NULL here is to tell parse_backtick not to
  531. * split the fields.
  532. */
  533. if (error) {
  534. free(expr);
  535. return error;
  536. }
  537. break;
  538. case '\\':
  539. error = parse_qtd_backslash(&expr, &expr_length, &expr_maxlen,
  540. words, offset);
  541. if (error) {
  542. free(expr);
  543. return error;
  544. }
  545. /* I think that a backslash within an
  546. * arithmetic expansion is bound to
  547. * cause an error sooner or later anyway though.
  548. */
  549. break;
  550. case ')':
  551. if (--paren_depth == 0) {
  552. char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
  553. long int numresult = 0;
  554. long long int convertme;
  555. if (bracket || words[1 + *offset] != ')') {
  556. free(expr);
  557. return WRDE_SYNTAX;
  558. }
  559. ++(*offset);
  560. /* Go - evaluate. */
  561. if (*expr && eval_expr(expr, &numresult) != 0) {
  562. free(expr);
  563. return WRDE_SYNTAX;
  564. }
  565. if (numresult < 0) {
  566. convertme = -numresult;
  567. *word = w_addchar(*word, word_length, max_length, '-');
  568. if (!*word) {
  569. free(expr);
  570. return WRDE_NOSPACE;
  571. }
  572. } else
  573. convertme = numresult;
  574. result[20] = '\0';
  575. *word = w_addstr(*word, word_length, max_length,
  576. _itoa(convertme, &result[20]));
  577. free(expr);
  578. return *word ? 0 : WRDE_NOSPACE;
  579. }
  580. expr =
  581. w_addchar(expr, &expr_length, &expr_maxlen,
  582. words[*offset]);
  583. if (expr == NULL)
  584. return WRDE_NOSPACE;
  585. break;
  586. case ']':
  587. if (bracket && paren_depth == 1) {
  588. char result[21]; /* 21 = ceil(log10(2^64)) + 1 */
  589. long int numresult = 0;
  590. /* Go - evaluate. */
  591. if (*expr && eval_expr(expr, &numresult) != 0) {
  592. free(expr);
  593. return WRDE_SYNTAX;
  594. }
  595. result[20] = '\0';
  596. *word = w_addstr(*word, word_length, max_length,
  597. _itoa(numresult, &result[20]));
  598. free(expr);
  599. return *word ? 0 : WRDE_NOSPACE;
  600. }
  601. free(expr);
  602. return WRDE_SYNTAX;
  603. case '\n':
  604. case ';':
  605. case '{':
  606. case '}':
  607. free(expr);
  608. return WRDE_BADCHAR;
  609. case '(':
  610. ++paren_depth;
  611. default:
  612. expr =
  613. w_addchar(expr, &expr_length, &expr_maxlen,
  614. words[*offset]);
  615. if (expr == NULL)
  616. return WRDE_NOSPACE;
  617. }
  618. }
  619. /* Premature end */
  620. free(expr);
  621. return WRDE_SYNTAX;
  622. }
  623. /* Function called by child process in exec_comm() */
  624. static void attribute_noreturn
  625. exec_comm_child(char *comm, int *fildes, int showerr, int noexec)
  626. {
  627. int fd;
  628. const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
  629. /* Execute the command, or just check syntax? */
  630. if (noexec)
  631. args[1] = "-nc";
  632. /* Redirect output. */
  633. fd = fildes[1];
  634. if (fd != 1) {
  635. dup2(fd, 1);
  636. close(fd);
  637. }
  638. /* Redirect stderr to /dev/null if we have to. */
  639. if (showerr == 0) {
  640. close(2);
  641. fd = open(_PATH_DEVNULL, O_WRONLY);
  642. if (fd >= 0 && fd != 2) {
  643. dup2(fd, 2);
  644. close(fd);
  645. }
  646. }
  647. /* Make sure the subshell doesn't field-split on our behalf. */
  648. unsetenv("IFS");
  649. if (fildes[0] != 1)
  650. close(fildes[0]);
  651. execve(_PATH_BSHELL, (char *const *) args, __environ);
  652. /* Bad. What now? */
  653. abort();
  654. }
  655. /* Function to execute a command and retrieve the results */
  656. /* pwordexp contains NULL if field-splitting is forbidden */
  657. static int
  658. exec_comm(char *comm, char **word, size_t * word_length,
  659. size_t * max_length, int flags, wordexp_t * pwordexp,
  660. const char *ifs, const char *ifs_white)
  661. {
  662. int fildes[2];
  663. int bufsize = 128;
  664. int buflen;
  665. int i;
  666. int status = 0;
  667. size_t maxnewlines = 0;
  668. char *buffer;
  669. pid_t pid;
  670. /* Don't fork() unless necessary */
  671. if (!comm || !*comm)
  672. return 0;
  673. if (pipe(fildes))
  674. /* Bad */
  675. return WRDE_NOSPACE;
  676. if ((pid = fork()) < 0) {
  677. /* Bad */
  678. close(fildes[0]);
  679. close(fildes[1]);
  680. return WRDE_NOSPACE;
  681. }
  682. if (pid == 0)
  683. exec_comm_child(comm, fildes, (flags & WRDE_SHOWERR), 0);
  684. /* Parent */
  685. close(fildes[1]);
  686. buffer = alloca(bufsize);
  687. if (!pwordexp)
  688. /* Quoted - no field splitting */
  689. {
  690. while (1) {
  691. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  692. if (waitpid(pid, &status, WNOHANG) == 0)
  693. continue;
  694. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  695. break;
  696. }
  697. maxnewlines += buflen;
  698. *word = w_addmem(*word, word_length, max_length, buffer, buflen);
  699. if (*word == NULL)
  700. goto no_space;
  701. }
  702. } else
  703. /* Not quoted - split fields */
  704. {
  705. int copying = 0;
  706. /* 'copying' is:
  707. * 0 when searching for first character in a field not IFS white space
  708. * 1 when copying the text of a field
  709. * 2 when searching for possible non-whitespace IFS
  710. * 3 when searching for non-newline after copying field
  711. */
  712. while (1) {
  713. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  714. if (waitpid(pid, &status, WNOHANG) == 0)
  715. continue;
  716. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  717. break;
  718. }
  719. for (i = 0; i < buflen; ++i) {
  720. if (strchr(ifs, buffer[i]) != NULL) {
  721. /* Current character is IFS */
  722. if (strchr(ifs_white, buffer[i]) == NULL) {
  723. /* Current character is IFS but not whitespace */
  724. if (copying == 2) {
  725. /* current character
  726. * |
  727. * V
  728. * eg: text<space><comma><space>moretext
  729. *
  730. * So, strip whitespace IFS (like at the start)
  731. */
  732. copying = 0;
  733. continue;
  734. }
  735. copying = 0;
  736. /* fall through and delimit field.. */
  737. } else {
  738. if (buffer[i] == '\n') {
  739. /* Current character is (IFS) newline */
  740. /* If copying a field, this is the end of it,
  741. but maybe all that's left is trailing newlines.
  742. So start searching for a non-newline. */
  743. if (copying == 1)
  744. copying = 3;
  745. continue;
  746. } else {
  747. /* Current character is IFS white space, but
  748. not a newline */
  749. /* If not either copying a field or searching
  750. for non-newline after a field, ignore it */
  751. if (copying != 1 && copying != 3)
  752. continue;
  753. /* End of field (search for non-ws IFS afterwards) */
  754. copying = 2;
  755. }
  756. }
  757. /* First IFS white space (non-newline), or IFS non-whitespace.
  758. * Delimit the field. Nulls are converted by w_addword. */
  759. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  760. goto no_space;
  761. *word = w_newword(word_length, max_length);
  762. maxnewlines = 0;
  763. /* fall back round the loop.. */
  764. } else {
  765. /* Not IFS character */
  766. if (copying == 3) {
  767. /* Nothing but (IFS) newlines since the last field,
  768. so delimit it here before starting new word */
  769. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  770. goto no_space;
  771. *word = w_newword(word_length, max_length);
  772. }
  773. copying = 1;
  774. if (buffer[i] == '\n') /* happens if newline not in IFS */
  775. maxnewlines++;
  776. else
  777. maxnewlines = 0;
  778. *word = w_addchar(*word, word_length, max_length,
  779. buffer[i]);
  780. if (*word == NULL)
  781. goto no_space;
  782. }
  783. }
  784. }
  785. }
  786. /* Chop off trailing newlines (required by POSIX.2) */
  787. /* Ensure we don't go back further than the beginning of the
  788. substitution (i.e. remove maxnewlines bytes at most) */
  789. while (maxnewlines-- != 0 &&
  790. *word_length > 0 && (*word)[*word_length - 1] == '\n') {
  791. (*word)[--*word_length] = '\0';
  792. /* If the last word was entirely newlines, turn it into a new word
  793. * which can be ignored if there's nothing following it. */
  794. if (*word_length == 0) {
  795. free(*word);
  796. *word = w_newword(word_length, max_length);
  797. break;
  798. }
  799. }
  800. close(fildes[0]);
  801. /* Check for syntax error (re-execute but with "-n" flag) */
  802. if (buflen < 1 && status != 0) {
  803. if ((pid = fork()) < 0) {
  804. /* Bad */
  805. return WRDE_NOSPACE;
  806. }
  807. if (pid == 0) {
  808. fildes[0] = fildes[1] = -1;
  809. exec_comm_child(comm, fildes, 0, 1);
  810. }
  811. if (waitpid(pid, &status, 0) == pid && status != 0)
  812. return WRDE_SYNTAX;
  813. }
  814. return 0;
  815. no_space:
  816. kill(pid, SIGKILL);
  817. waitpid(pid, NULL, 0);
  818. close(fildes[0]);
  819. return WRDE_NOSPACE;
  820. }
  821. static int
  822. parse_comm(char **word, size_t * word_length, size_t * max_length,
  823. const char *words, size_t * offset, int flags,
  824. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  825. {
  826. /* We are poised just after "$(" */
  827. int paren_depth = 1;
  828. int error = 0;
  829. int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  830. size_t comm_length;
  831. size_t comm_maxlen;
  832. char *comm = w_newword(&comm_length, &comm_maxlen);
  833. for (; words[*offset]; ++(*offset)) {
  834. switch (words[*offset]) {
  835. case '\'':
  836. if (quoted == 0)
  837. quoted = 1;
  838. else if (quoted == 1)
  839. quoted = 0;
  840. break;
  841. case '"':
  842. if (quoted == 0)
  843. quoted = 2;
  844. else if (quoted == 2)
  845. quoted = 0;
  846. break;
  847. case ')':
  848. if (!quoted && --paren_depth == 0) {
  849. /* Go -- give script to the shell */
  850. if (comm) {
  851. error = exec_comm(comm, word, word_length, max_length,
  852. flags, pwordexp, ifs, ifs_white);
  853. free(comm);
  854. }
  855. return error;
  856. }
  857. /* This is just part of the script */
  858. break;
  859. case '(':
  860. if (!quoted)
  861. ++paren_depth;
  862. }
  863. comm = w_addchar(comm, &comm_length, &comm_maxlen, words[*offset]);
  864. if (comm == NULL)
  865. return WRDE_NOSPACE;
  866. }
  867. /* Premature end */
  868. free(comm);
  869. return WRDE_SYNTAX;
  870. }
  871. static int
  872. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  873. const char *words, size_t * offset, int flags,
  874. wordexp_t * pwordexp, const char *ifs,
  875. const char *ifs_white)
  876. {
  877. /* We are poised just after "`" */
  878. int error;
  879. int squoting = 0;
  880. size_t comm_length;
  881. size_t comm_maxlen;
  882. char *comm = w_newword(&comm_length, &comm_maxlen);
  883. for (; words[*offset]; ++(*offset)) {
  884. switch (words[*offset]) {
  885. case '`':
  886. /* Go -- give the script to the shell */
  887. error = exec_comm(comm, word, word_length, max_length, flags,
  888. pwordexp, ifs, ifs_white);
  889. free(comm);
  890. return error;
  891. case '\\':
  892. if (squoting) {
  893. error = parse_qtd_backslash(&comm, &comm_length, &comm_maxlen,
  894. words, offset);
  895. if (error) {
  896. free(comm);
  897. return error;
  898. }
  899. break;
  900. }
  901. ++(*offset);
  902. error = parse_backslash(&comm, &comm_length, &comm_maxlen, words,
  903. offset);
  904. if (error) {
  905. free(comm);
  906. return error;
  907. }
  908. break;
  909. case '\'':
  910. squoting = 1 - squoting;
  911. default:
  912. comm = w_addchar(comm, &comm_length, &comm_maxlen,
  913. words[*offset]);
  914. if (comm == NULL)
  915. return WRDE_NOSPACE;
  916. }
  917. }
  918. /* Premature end */
  919. free(comm);
  920. return WRDE_SYNTAX;
  921. }
  922. static int
  923. parse_param(char **word, size_t * word_length, size_t * max_length,
  924. const char *words, size_t * offset, int flags,
  925. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  926. int quoted)
  927. {
  928. /* We are poised just after "$" */
  929. enum action {
  930. ACT_NONE,
  931. ACT_RP_SHORT_LEFT = '#',
  932. ACT_RP_LONG_LEFT = 'L',
  933. ACT_RP_SHORT_RIGHT = '%',
  934. ACT_RP_LONG_RIGHT = 'R',
  935. ACT_NULL_ERROR = '?',
  936. ACT_NULL_SUBST = '-',
  937. ACT_NONNULL_SUBST = '+',
  938. ACT_NULL_ASSIGN = '='
  939. };
  940. size_t env_length;
  941. size_t env_maxlen;
  942. size_t pat_length;
  943. size_t pat_maxlen;
  944. size_t start = *offset;
  945. char *env;
  946. char *pattern;
  947. char *value = NULL;
  948. enum action action = ACT_NONE;
  949. int depth = 0;
  950. int colon_seen = 0;
  951. int seen_hash = 0;
  952. int free_value = 0;
  953. int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  954. int error;
  955. int special = 0;
  956. char buffer[21];
  957. int brace = words[*offset] == '{';
  958. env = w_newword(&env_length, &env_maxlen);
  959. pattern = w_newword(&pat_length, &pat_maxlen);
  960. if (brace)
  961. ++ * offset;
  962. /* First collect the parameter name. */
  963. if (words[*offset] == '#') {
  964. seen_hash = 1;
  965. if (!brace)
  966. goto envsubst;
  967. ++*offset;
  968. }
  969. if (isalpha(words[*offset]) || words[*offset] == '_') {
  970. /* Normal parameter name. */
  971. do {
  972. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  973. if (env == NULL)
  974. goto no_space;
  975. }
  976. while (isalnum(words[++*offset]) || words[*offset] == '_');
  977. } else if (isdigit(words[*offset])) {
  978. /* Numeric parameter name. */
  979. special = 1;
  980. do {
  981. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  982. if (env == NULL)
  983. goto no_space;
  984. if (!brace)
  985. goto envsubst;
  986. }
  987. while (isdigit(words[++*offset]));
  988. } else if (strchr("*@$", words[*offset]) != NULL) {
  989. /* Special parameter. */
  990. special = 1;
  991. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  992. if (env == NULL)
  993. goto no_space;
  994. ++*offset;
  995. } else {
  996. if (brace)
  997. goto syntax;
  998. }
  999. if (brace) {
  1000. /* Check for special action to be applied to the value. */
  1001. switch (words[*offset]) {
  1002. case '}':
  1003. /* Evaluate. */
  1004. goto envsubst;
  1005. case '#':
  1006. action = ACT_RP_SHORT_LEFT;
  1007. if (words[1 + *offset] == '#') {
  1008. ++*offset;
  1009. action = ACT_RP_LONG_LEFT;
  1010. }
  1011. break;
  1012. case '%':
  1013. action = ACT_RP_SHORT_RIGHT;
  1014. if (words[1 + *offset] == '%') {
  1015. ++*offset;
  1016. action = ACT_RP_LONG_RIGHT;
  1017. }
  1018. break;
  1019. case ':':
  1020. if (strchr("-=?+", words[1 + *offset]) == NULL)
  1021. goto syntax;
  1022. colon_seen = 1;
  1023. action = words[++*offset];
  1024. break;
  1025. case '-':
  1026. case '=':
  1027. case '?':
  1028. case '+':
  1029. action = words[*offset];
  1030. break;
  1031. default:
  1032. goto syntax;
  1033. }
  1034. /* Now collect the pattern, but don't expand it yet. */
  1035. ++*offset;
  1036. for (; words[*offset]; ++(*offset)) {
  1037. switch (words[*offset]) {
  1038. case '{':
  1039. if (!pattern_is_quoted)
  1040. ++depth;
  1041. break;
  1042. case '}':
  1043. if (!pattern_is_quoted) {
  1044. if (depth == 0)
  1045. goto envsubst;
  1046. --depth;
  1047. }
  1048. break;
  1049. case '\\':
  1050. if (pattern_is_quoted)
  1051. /* Quoted; treat as normal character. */
  1052. break;
  1053. /* Otherwise, it's an escape: next character is literal. */
  1054. if (words[++*offset] == '\0')
  1055. goto syntax;
  1056. pattern = w_addchar(pattern, &pat_length, &pat_maxlen, '\\');
  1057. if (pattern == NULL)
  1058. goto no_space;
  1059. break;
  1060. case '\'':
  1061. if (pattern_is_quoted == 0)
  1062. pattern_is_quoted = 1;
  1063. else if (pattern_is_quoted == 1)
  1064. pattern_is_quoted = 0;
  1065. break;
  1066. case '"':
  1067. if (pattern_is_quoted == 0)
  1068. pattern_is_quoted = 2;
  1069. else if (pattern_is_quoted == 2)
  1070. pattern_is_quoted = 0;
  1071. break;
  1072. }
  1073. pattern = w_addchar(pattern, &pat_length, &pat_maxlen,
  1074. words[*offset]);
  1075. if (pattern == NULL)
  1076. goto no_space;
  1077. }
  1078. }
  1079. /* End of input string -- remember to reparse the character that we
  1080. * stopped at. */
  1081. --(*offset);
  1082. envsubst:
  1083. if (words[start] == '{' && words[*offset] != '}')
  1084. goto syntax;
  1085. if (env == NULL) {
  1086. if (seen_hash) {
  1087. /* $# expands to the number of positional parameters */
  1088. buffer[20] = '\0';
  1089. value = _itoa(__libc_argc - 1, &buffer[20]);
  1090. seen_hash = 0;
  1091. } else {
  1092. /* Just $ on its own */
  1093. *offset = start - 1;
  1094. *word = w_addchar(*word, word_length, max_length, '$');
  1095. return *word ? 0 : WRDE_NOSPACE;
  1096. }
  1097. }
  1098. /* Is it a numeric parameter? */
  1099. else if (isdigit(env[0])) {
  1100. int n = atoi(env);
  1101. if (n >= __libc_argc)
  1102. /* Substitute NULL. */
  1103. value = NULL;
  1104. else
  1105. /* Replace with appropriate positional parameter. */
  1106. value = __libc_argv[n];
  1107. }
  1108. /* Is it a special parameter? */
  1109. else if (special) {
  1110. /* Is it `$$'? */
  1111. if (*env == '$') {
  1112. buffer[20] = '\0';
  1113. value = _itoa(getpid(), &buffer[20]);
  1114. }
  1115. /* Is it `${#*}' or `${#@}'? */
  1116. else if ((*env == '*' || *env == '@') && seen_hash) {
  1117. buffer[20] = '\0';
  1118. value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,
  1119. &buffer[20]);
  1120. *word = w_addstr(*word, word_length, max_length, value);
  1121. free(env);
  1122. free(pattern);
  1123. return *word ? 0 : WRDE_NOSPACE;
  1124. }
  1125. /* Is it `$*' or `$@' (unquoted) ? */
  1126. else if (*env == '*' || (*env == '@' && !quoted)) {
  1127. size_t plist_len = 0;
  1128. int p;
  1129. char *end;
  1130. /* Build up value parameter by parameter (copy them) */
  1131. for (p = 1; __libc_argv[p]; ++p)
  1132. plist_len += strlen(__libc_argv[p]) + 1; /* for space */
  1133. value = malloc(plist_len);
  1134. if (value == NULL)
  1135. goto no_space;
  1136. end = value;
  1137. *end = 0;
  1138. for (p = 1; __libc_argv[p]; ++p) {
  1139. if (p > 1)
  1140. *end++ = ' ';
  1141. end = stpcpy(end, __libc_argv[p]);
  1142. }
  1143. free_value = 1;
  1144. } else {
  1145. /* Must be a quoted `$@' */
  1146. assert(*env == '@' && quoted);
  1147. /* Each parameter is a separate word ("$@") */
  1148. if (__libc_argc == 2)
  1149. value = __libc_argv[1];
  1150. else if (__libc_argc > 2) {
  1151. int p;
  1152. /* Append first parameter to current word. */
  1153. value = w_addstr(*word, word_length, max_length,
  1154. __libc_argv[1]);
  1155. if (value == NULL || w_addword(pwordexp, value))
  1156. goto no_space;
  1157. for (p = 2; __libc_argv[p + 1]; p++) {
  1158. char *newword = strdup(__libc_argv[p]);
  1159. if (newword == NULL || w_addword(pwordexp, newword))
  1160. goto no_space;
  1161. }
  1162. /* Start a new word with the last parameter. */
  1163. *word = w_newword(word_length, max_length);
  1164. value = __libc_argv[p];
  1165. } else {
  1166. free(env);
  1167. free(pattern);
  1168. return 0;
  1169. }
  1170. }
  1171. } else
  1172. value = getenv(env);
  1173. if (value == NULL && (flags & WRDE_UNDEF)) {
  1174. /* Variable not defined. */
  1175. error = WRDE_BADVAL;
  1176. goto do_error;
  1177. }
  1178. if (action != ACT_NONE) {
  1179. int expand_pattern = 0;
  1180. /* First, find out if we need to expand pattern (i.e. if we will
  1181. * use it). */
  1182. switch (action) {
  1183. case ACT_RP_SHORT_LEFT:
  1184. case ACT_RP_LONG_LEFT:
  1185. case ACT_RP_SHORT_RIGHT:
  1186. case ACT_RP_LONG_RIGHT:
  1187. /* Always expand for these. */
  1188. expand_pattern = 1;
  1189. break;
  1190. case ACT_NULL_ERROR:
  1191. case ACT_NULL_SUBST:
  1192. case ACT_NULL_ASSIGN:
  1193. if (!value || (!*value && colon_seen))
  1194. /* If param is unset, or set but null and a colon has been seen,
  1195. the expansion of the pattern will be needed. */
  1196. expand_pattern = 1;
  1197. break;
  1198. case ACT_NONNULL_SUBST:
  1199. /* Expansion of word will be needed if parameter is set and not null,
  1200. or set null but no colon has been seen. */
  1201. if (value && (*value || !colon_seen))
  1202. expand_pattern = 1;
  1203. break;
  1204. default:
  1205. assert(!"Unrecognised action!");
  1206. }
  1207. if (expand_pattern) {
  1208. /* We need to perform tilde expansion, parameter expansion,
  1209. command substitution, and arithmetic expansion. We also
  1210. have to be a bit careful with wildcard characters, as
  1211. pattern might be given to fnmatch soon. To do this, we
  1212. convert quotes to escapes. */
  1213. char *expanded;
  1214. size_t exp_len;
  1215. size_t exp_maxl;
  1216. char *p;
  1217. int quotes = 0; /* 1: single quotes; 2: double */
  1218. expanded = w_newword(&exp_len, &exp_maxl);
  1219. for (p = pattern; p && *p; p++) {
  1220. size_t _offset;
  1221. switch (*p) {
  1222. case '"':
  1223. if (quotes == 2)
  1224. quotes = 0;
  1225. else if (quotes == 0)
  1226. quotes = 2;
  1227. else
  1228. break;
  1229. continue;
  1230. case '\'':
  1231. if (quotes == 1)
  1232. quotes = 0;
  1233. else if (quotes == 0)
  1234. quotes = 1;
  1235. else
  1236. break;
  1237. continue;
  1238. case '*':
  1239. case '?':
  1240. if (quotes) {
  1241. /* Convert quoted wildchar to escaped wildchar. */
  1242. expanded = w_addchar(expanded, &exp_len,
  1243. &exp_maxl, '\\');
  1244. if (expanded == NULL)
  1245. goto no_space;
  1246. }
  1247. break;
  1248. case '$':
  1249. _offset = 0;
  1250. error = parse_dollars(&expanded, &exp_len, &exp_maxl, p,
  1251. &_offset, flags, NULL, NULL, NULL, 1);
  1252. if (error) {
  1253. if (free_value)
  1254. free(value);
  1255. free(expanded);
  1256. goto do_error;
  1257. }
  1258. p += _offset;
  1259. continue;
  1260. case '~':
  1261. if (quotes || exp_len)
  1262. break;
  1263. _offset = 0;
  1264. error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,
  1265. &_offset, 0);
  1266. if (error) {
  1267. if (free_value)
  1268. free(value);
  1269. free(expanded);
  1270. goto do_error;
  1271. }
  1272. p += _offset;
  1273. continue;
  1274. case '\\':
  1275. expanded = w_addchar(expanded, &exp_len, &exp_maxl, '\\');
  1276. ++p;
  1277. assert(*p); /* checked when extracted initially */
  1278. if (expanded == NULL)
  1279. goto no_space;
  1280. }
  1281. expanded = w_addchar(expanded, &exp_len, &exp_maxl, *p);
  1282. if (expanded == NULL)
  1283. goto no_space;
  1284. }
  1285. free(pattern);
  1286. pattern = expanded;
  1287. }
  1288. switch (action) {
  1289. case ACT_RP_SHORT_LEFT:
  1290. case ACT_RP_LONG_LEFT:
  1291. case ACT_RP_SHORT_RIGHT:
  1292. case ACT_RP_LONG_RIGHT:
  1293. {
  1294. char *p;
  1295. char c;
  1296. char *end;
  1297. if (value == NULL || pattern == NULL || *pattern == '\0')
  1298. break;
  1299. end = value + strlen(value);
  1300. switch (action) {
  1301. case ACT_RP_SHORT_LEFT:
  1302. for (p = value; p <= end; ++p) {
  1303. c = *p;
  1304. *p = '\0';
  1305. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1306. *p = c;
  1307. if (free_value) {
  1308. char *newval = strdup(p);
  1309. if (newval == NULL) {
  1310. free(value);
  1311. goto no_space;
  1312. }
  1313. free(value);
  1314. value = newval;
  1315. } else
  1316. value = p;
  1317. break;
  1318. }
  1319. *p = c;
  1320. }
  1321. break;
  1322. case ACT_RP_LONG_LEFT:
  1323. for (p = end; p >= value; --p) {
  1324. c = *p;
  1325. *p = '\0';
  1326. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1327. *p = c;
  1328. if (free_value) {
  1329. char *newval = strdup(p);
  1330. if (newval == NULL) {
  1331. free(value);
  1332. goto no_space;
  1333. }
  1334. free(value);
  1335. value = newval;
  1336. } else
  1337. value = p;
  1338. break;
  1339. }
  1340. *p = c;
  1341. }
  1342. break;
  1343. case ACT_RP_SHORT_RIGHT:
  1344. for (p = end; p >= value; --p) {
  1345. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1346. char *newval;
  1347. newval = malloc(p - value + 1);
  1348. if (newval == NULL) {
  1349. if (free_value)
  1350. free(value);
  1351. goto no_space;
  1352. }
  1353. *(char *) mempcpy(newval, value, p - value) = '\0';
  1354. if (free_value)
  1355. free(value);
  1356. value = newval;
  1357. free_value = 1;
  1358. break;
  1359. }
  1360. }
  1361. break;
  1362. case ACT_RP_LONG_RIGHT:
  1363. for (p = value; p <= end; ++p) {
  1364. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1365. char *newval;
  1366. newval = malloc(p - value + 1);
  1367. if (newval == NULL) {
  1368. if (free_value)
  1369. free(value);
  1370. goto no_space;
  1371. }
  1372. *(char *) mempcpy(newval, value, p - value) = '\0';
  1373. if (free_value)
  1374. free(value);
  1375. value = newval;
  1376. free_value = 1;
  1377. break;
  1378. }
  1379. }
  1380. break;
  1381. default:
  1382. break;
  1383. }
  1384. break;
  1385. }
  1386. case ACT_NULL_ERROR:
  1387. if (value && *value)
  1388. /* Substitute parameter */
  1389. break;
  1390. error = 0;
  1391. if (!colon_seen && value)
  1392. /* Substitute NULL */
  1393. ;
  1394. else if (*pattern)
  1395. fprintf(stderr, "%s: %s\n", env, pattern);
  1396. else {
  1397. fprintf(stderr, "%s: parameter null or not set\n", env);
  1398. error = WRDE_BADVAL;
  1399. }
  1400. if (free_value)
  1401. free(value);
  1402. goto do_error;
  1403. case ACT_NULL_SUBST:
  1404. if (value && *value)
  1405. /* Substitute parameter */
  1406. break;
  1407. if (free_value)
  1408. free(value);
  1409. if (!colon_seen && value)
  1410. /* Substitute NULL */
  1411. goto success;
  1412. value = pattern ? strdup(pattern) : pattern;
  1413. free_value = 1;
  1414. if (pattern && !value)
  1415. goto no_space;
  1416. break;
  1417. case ACT_NONNULL_SUBST:
  1418. if (value && (*value || !colon_seen)) {
  1419. if (free_value)
  1420. free(value);
  1421. value = pattern ? strdup(pattern) : pattern;
  1422. free_value = 1;
  1423. if (pattern && !value)
  1424. goto no_space;
  1425. break;
  1426. }
  1427. /* Substitute NULL */
  1428. if (free_value)
  1429. free(value);
  1430. goto success;
  1431. case ACT_NULL_ASSIGN:
  1432. if (value && *value)
  1433. /* Substitute parameter */
  1434. break;
  1435. if (!colon_seen && value) {
  1436. /* Substitute NULL */
  1437. if (free_value)
  1438. free(value);
  1439. goto success;
  1440. }
  1441. if (free_value)
  1442. free(value);
  1443. value = pattern ? strdup(pattern) : pattern;
  1444. free_value = 1;
  1445. if (pattern && !value)
  1446. goto no_space;
  1447. setenv(env, value, 1);
  1448. break;
  1449. default:
  1450. assert(!"Unrecognised action!");
  1451. }
  1452. }
  1453. free(env);
  1454. env = NULL;
  1455. free(pattern);
  1456. pattern = NULL;
  1457. if (seen_hash) {
  1458. char param_length[21];
  1459. param_length[20] = '\0';
  1460. *word = w_addstr(*word, word_length, max_length,
  1461. _itoa(value ? strlen(value) : 0,
  1462. &param_length[20]));
  1463. if (free_value) {
  1464. assert(value != NULL);
  1465. free(value);
  1466. }
  1467. return *word ? 0 : WRDE_NOSPACE;
  1468. }
  1469. if (value == NULL)
  1470. return 0;
  1471. if (quoted || !pwordexp) {
  1472. /* Quoted - no field split */
  1473. *word = w_addstr(*word, word_length, max_length, value);
  1474. if (free_value)
  1475. free(value);
  1476. return *word ? 0 : WRDE_NOSPACE;
  1477. } else {
  1478. /* Need to field-split */
  1479. char *value_copy = strdup(value); /* Don't modify value */
  1480. char *field_begin = value_copy;
  1481. int seen_nonws_ifs = 0;
  1482. if (free_value)
  1483. free(value);
  1484. if (value_copy == NULL)
  1485. goto no_space;
  1486. do {
  1487. char *field_end = field_begin;
  1488. char *next_field;
  1489. /* If this isn't the first field, start a new word */
  1490. if (field_begin != value_copy) {
  1491. if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {
  1492. free(value_copy);
  1493. goto no_space;
  1494. }
  1495. *word = w_newword(word_length, max_length);
  1496. }
  1497. /* Skip IFS whitespace before the field */
  1498. field_begin += strspn(field_begin, ifs_white);
  1499. if (!seen_nonws_ifs && *field_begin == 0)
  1500. /* Nothing but whitespace */
  1501. break;
  1502. /* Search for the end of the field */
  1503. field_end = field_begin + strcspn(field_begin, ifs);
  1504. /* Set up pointer to the character after end of field and
  1505. skip whitespace IFS after it. */
  1506. next_field = field_end + strspn(field_end, ifs_white);
  1507. /* Skip at most one non-whitespace IFS character after the field */
  1508. seen_nonws_ifs = 0;
  1509. if (*next_field && strchr(ifs, *next_field)) {
  1510. seen_nonws_ifs = 1;
  1511. next_field++;
  1512. }
  1513. /* Null-terminate it */
  1514. *field_end = 0;
  1515. /* Tag a copy onto the current word */
  1516. *word = w_addstr(*word, word_length, max_length, field_begin);
  1517. if (*word == NULL && *field_begin != '\0') {
  1518. free(value_copy);
  1519. goto no_space;
  1520. }
  1521. field_begin = next_field;
  1522. }
  1523. while (seen_nonws_ifs || *field_begin);
  1524. free(value_copy);
  1525. }
  1526. return 0;
  1527. success:
  1528. error = 0;
  1529. goto do_error;
  1530. no_space:
  1531. error = WRDE_NOSPACE;
  1532. goto do_error;
  1533. syntax:
  1534. error = WRDE_SYNTAX;
  1535. do_error:
  1536. free(env);
  1537. free(pattern);
  1538. return error;
  1539. }
  1540. #else
  1541. static __inline__ int
  1542. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  1543. const char *words, size_t * offset, int flags,
  1544. wordexp_t * pwordexp, const char *ifs,
  1545. const char *ifs_white)
  1546. {
  1547. return 0;
  1548. }
  1549. #endif
  1550. static int
  1551. parse_dollars(char **word, size_t * word_length, size_t * max_length,
  1552. const char *words, size_t * offset, int flags,
  1553. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  1554. int quoted)
  1555. {
  1556. /* We are poised _at_ "$" */
  1557. switch (words[1 + *offset]) {
  1558. case '"':
  1559. case '\'':
  1560. case 0:
  1561. *word = w_addchar(*word, word_length, max_length, '$');
  1562. return *word ? 0 : WRDE_NOSPACE;
  1563. #ifdef __WORDEXP_FULL
  1564. case '(':
  1565. if (words[2 + *offset] == '(') {
  1566. /* Differentiate between $((1+3)) and $((echo);(ls)) */
  1567. int i = 3 + *offset;
  1568. int depth = 0;
  1569. while (words[i] && !(depth == 0 && words[i] == ')')) {
  1570. if (words[i] == '(')
  1571. ++depth;
  1572. else if (words[i] == ')')
  1573. --depth;
  1574. ++i;
  1575. }
  1576. if (words[i] == ')' && words[i + 1] == ')') {
  1577. (*offset) += 3;
  1578. /* Call parse_arith -- 0 is for "no brackets" */
  1579. return parse_arith(word, word_length, max_length, words,
  1580. offset, flags, 0);
  1581. }
  1582. }
  1583. if (flags & WRDE_NOCMD)
  1584. return WRDE_CMDSUB;
  1585. (*offset) += 2;
  1586. return parse_comm(word, word_length, max_length, words, offset,
  1587. flags, quoted ? NULL : pwordexp, ifs, ifs_white);
  1588. case '[':
  1589. (*offset) += 2;
  1590. /* Call parse_arith -- 1 is for "brackets" */
  1591. return parse_arith(word, word_length, max_length, words, offset,
  1592. flags, 1);
  1593. case '{':
  1594. default:
  1595. ++(*offset); /* parse_param needs to know if "{" is there */
  1596. return parse_param(word, word_length, max_length, words, offset,
  1597. flags, pwordexp, ifs, ifs_white, quoted);
  1598. #else
  1599. default:
  1600. ++(*offset); /* parse_param needs to know if "{" is there */
  1601. return 0;
  1602. #endif
  1603. }
  1604. }
  1605. static int
  1606. parse_dquote(char **word, size_t * word_length, size_t * max_length,
  1607. const char *words, size_t * offset, int flags,
  1608. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  1609. {
  1610. /* We are poised just after a double-quote */
  1611. int error;
  1612. for (; words[*offset]; ++(*offset)) {
  1613. switch (words[*offset]) {
  1614. case '"':
  1615. return 0;
  1616. case '$':
  1617. error = parse_dollars(word, word_length, max_length, words, offset,
  1618. flags, pwordexp, ifs, ifs_white, 1);
  1619. /* The ``1'' here is to tell parse_dollars not to
  1620. * split the fields. It may need to, however ("$@").
  1621. */
  1622. if (error)
  1623. return error;
  1624. break;
  1625. case '`':
  1626. if (flags & WRDE_NOCMD)
  1627. return WRDE_CMDSUB;
  1628. ++(*offset);
  1629. error = parse_backtick(word, word_length, max_length, words,
  1630. offset, flags, NULL, NULL, NULL);
  1631. /* The first NULL here is to tell parse_backtick not to
  1632. * split the fields.
  1633. */
  1634. if (error)
  1635. return error;
  1636. break;
  1637. case '\\':
  1638. error = parse_qtd_backslash(word, word_length, max_length, words,
  1639. offset);
  1640. if (error)
  1641. return error;
  1642. break;
  1643. default:
  1644. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  1645. if (*word == NULL)
  1646. return WRDE_NOSPACE;
  1647. }
  1648. }
  1649. /* Unterminated string */
  1650. return WRDE_SYNTAX;
  1651. }
  1652. /*
  1653. * wordfree() is to be called after pwordexp is finished with.
  1654. */
  1655. void wordfree(wordexp_t * pwordexp)
  1656. {
  1657. /* wordexp can set pwordexp to NULL */
  1658. if (pwordexp && pwordexp->we_wordv) {
  1659. char **wordv = pwordexp->we_wordv;
  1660. for (wordv += pwordexp->we_offs; *wordv; ++wordv)
  1661. free(*wordv);
  1662. free(pwordexp->we_wordv);
  1663. pwordexp->we_wordv = NULL;
  1664. }
  1665. }
  1666. libc_hidden_def(wordfree)
  1667. /*
  1668. * wordexp()
  1669. */
  1670. int wordexp(const char *words, wordexp_t * we, int flags)
  1671. {
  1672. size_t words_offset;
  1673. size_t word_length;
  1674. size_t max_length;
  1675. char *word = w_newword(&word_length, &max_length);
  1676. int error;
  1677. char *ifs;
  1678. char ifs_white[4];
  1679. wordexp_t old_word = *we;
  1680. if (flags & WRDE_REUSE) {
  1681. /* Minimal implementation of WRDE_REUSE for now */
  1682. wordfree(we);
  1683. old_word.we_wordv = NULL;
  1684. }
  1685. if ((flags & WRDE_APPEND) == 0) {
  1686. we->we_wordc = 0;
  1687. if (flags & WRDE_DOOFFS) {
  1688. we->we_wordv = calloc(1 + we->we_offs, sizeof(char *));
  1689. if (we->we_wordv == NULL) {
  1690. error = WRDE_NOSPACE;
  1691. goto do_error;
  1692. }
  1693. } else {
  1694. we->we_wordv = calloc(1, sizeof(char *));
  1695. if (we->we_wordv == NULL) {
  1696. error = WRDE_NOSPACE;
  1697. goto do_error;
  1698. }
  1699. we->we_offs = 0;
  1700. }
  1701. }
  1702. /* Find out what the field separators are.
  1703. * There are two types: whitespace and non-whitespace.
  1704. */
  1705. ifs = getenv("IFS");
  1706. if (!ifs)
  1707. /* IFS unset - use <space><tab><newline>. */
  1708. ifs = strcpy(ifs_white, " \t\n");
  1709. else {
  1710. char *ifsch = ifs;
  1711. char *whch = ifs_white;
  1712. /* Start off with no whitespace IFS characters */
  1713. ifs_white[0] = '\0';
  1714. while (*ifsch != '\0') {
  1715. if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {
  1716. /* Whitespace IFS. See first whether it is already in our
  1717. collection. */
  1718. char *runp = ifs_white;
  1719. while (runp < whch && *runp != '\0' && *runp != *ifsch)
  1720. ++runp;
  1721. if (runp == whch)
  1722. *whch++ = *ifsch;
  1723. }
  1724. ++ifsch;
  1725. }
  1726. *whch = '\0';
  1727. }
  1728. for (words_offset = 0; words[words_offset]; ++words_offset)
  1729. switch (words[words_offset]) {
  1730. case '\\':
  1731. error = parse_backslash(&word, &word_length, &max_length, words,
  1732. &words_offset);
  1733. if (error)
  1734. goto do_error;
  1735. break;
  1736. case '$':
  1737. error = parse_dollars(&word, &word_length, &max_length, words,
  1738. &words_offset, flags, we, ifs, ifs_white,
  1739. 0);
  1740. if (error)
  1741. goto do_error;
  1742. break;
  1743. case '`':
  1744. if (flags & WRDE_NOCMD) {
  1745. error = WRDE_CMDSUB;
  1746. goto do_error;
  1747. }
  1748. ++words_offset;
  1749. error = parse_backtick(&word, &word_length, &max_length, words,
  1750. &words_offset, flags, we, ifs,
  1751. ifs_white);
  1752. if (error)
  1753. goto do_error;
  1754. break;
  1755. case '"':
  1756. ++words_offset;
  1757. error = parse_dquote(&word, &word_length, &max_length, words,
  1758. &words_offset, flags, we, ifs, ifs_white);
  1759. if (error)
  1760. goto do_error;
  1761. if (!word_length) {
  1762. error = w_addword(we, NULL);
  1763. if (error)
  1764. return error;
  1765. }
  1766. break;
  1767. case '\'':
  1768. ++words_offset;
  1769. error = parse_squote(&word, &word_length, &max_length, words,
  1770. &words_offset);
  1771. if (error)
  1772. goto do_error;
  1773. if (!word_length) {
  1774. error = w_addword(we, NULL);
  1775. if (error)
  1776. return error;
  1777. }
  1778. break;
  1779. case '~':
  1780. error = parse_tilde(&word, &word_length, &max_length, words,
  1781. &words_offset, we->we_wordc);
  1782. if (error)
  1783. goto do_error;
  1784. break;
  1785. case '*':
  1786. case '[':
  1787. case '?':
  1788. error = parse_glob(&word, &word_length, &max_length, words,
  1789. &words_offset, flags, we, ifs, ifs_white);
  1790. if (error)
  1791. goto do_error;
  1792. break;
  1793. default:
  1794. /* Is it a word separator? */
  1795. if (strchr(" \t", words[words_offset]) == NULL) {
  1796. char ch = words[words_offset];
  1797. /* Not a word separator -- but is it a valid word char? */
  1798. if (strchr("\n|&;<>(){}", ch)) {
  1799. /* Fail */
  1800. error = WRDE_BADCHAR;
  1801. goto do_error;
  1802. }
  1803. /* "Ordinary" character -- add it to word */
  1804. word = w_addchar(word, &word_length, &max_length, ch);
  1805. if (word == NULL) {
  1806. error = WRDE_NOSPACE;
  1807. goto do_error;
  1808. }
  1809. break;
  1810. }
  1811. /* If a word has been delimited, add it to the list. */
  1812. if (word != NULL) {
  1813. error = w_addword(we, word);
  1814. if (error)
  1815. goto do_error;
  1816. }
  1817. word = w_newword(&word_length, &max_length);
  1818. }
  1819. /* End of string */
  1820. /* There was a word separator at the end */
  1821. if (word == NULL) /* i.e. w_newword */
  1822. return 0;
  1823. /* There was no field separator at the end */
  1824. return w_addword(we, word);
  1825. do_error:
  1826. /* Error:
  1827. * free memory used (unless error is WRDE_NOSPACE), and
  1828. * set we members back to what they were.
  1829. */
  1830. free(word);
  1831. if (error == WRDE_NOSPACE)
  1832. return WRDE_NOSPACE;
  1833. if ((flags & WRDE_APPEND) == 0)
  1834. wordfree(we);
  1835. *we = old_word;
  1836. return error;
  1837. }