wordexp.c 48 KB

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