wordexp.c 48 KB

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