wordexp.c 48 KB

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