wordexp.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  1. /* vi: set sw=4 ts=4: */
  2. /* POSIX.2 wordexp implementation.
  3. Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. #include <features.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <fcntl.h>
  22. #include <paths.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <pwd.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <fnmatch.h>
  31. #include <glob.h>
  32. #include <wordexp.h>
  33. #define __WORDEXP_FULL
  34. /*
  35. * This is a recursive-descent-style word expansion routine.
  36. */
  37. /* These variables are defined and initialized in the startup code. */
  38. /*extern int __libc_argc;*/
  39. /*extern char **__libc_argv;*/
  40. /* FIXME!!!! */
  41. int attribute_hidden __libc_argc;
  42. char attribute_hidden **__libc_argv;
  43. /* Some forward declarations */
  44. static int parse_dollars(char **word, size_t * word_length,
  45. size_t * max_length, const char *words,
  46. size_t * offset, int flags, wordexp_t * pwordexp,
  47. const char *ifs, const char *ifs_white,
  48. int quoted);
  49. static int parse_backtick(char **word, size_t * word_length,
  50. size_t * max_length, const char *words,
  51. size_t * offset, int flags, wordexp_t * pwordexp,
  52. const char *ifs, const char *ifs_white);
  53. static int parse_dquote(char **word, size_t * word_length,
  54. size_t * max_length, const char *words,
  55. size_t * offset, int flags, wordexp_t * pwordexp,
  56. const char *ifs, const char *ifs_white);
  57. /* The w_*() functions manipulate word lists. */
  58. #define W_CHUNK (100)
  59. /* Result of w_newword will be ignored if it's the last word. */
  60. static __inline__ char *w_newword(size_t * actlen, size_t * maxlen)
  61. {
  62. *actlen = *maxlen = 0;
  63. return NULL;
  64. }
  65. /* Add a character to the buffer, allocating room for it if needed. */
  66. static __inline__ char *w_addchar(char *buffer, size_t * actlen,
  67. size_t * maxlen, char ch)
  68. /* (lengths exclude trailing zero) */
  69. {
  70. if (*actlen == *maxlen) {
  71. char *old_buffer = buffer;
  72. assert(buffer == NULL || *maxlen != 0);
  73. *maxlen += W_CHUNK;
  74. buffer = realloc(buffer, 1 + *maxlen);
  75. if (buffer == NULL)
  76. free(old_buffer);
  77. }
  78. if (buffer != NULL) {
  79. buffer[*actlen] = ch;
  80. buffer[++(*actlen)] = '\0';
  81. }
  82. return buffer;
  83. }
  84. #ifndef MAX
  85. #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
  86. #endif
  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 attribute_noreturn
  627. exec_comm_child(char *comm, int *fildes, int showerr, int noexec)
  628. {
  629. int fd;
  630. const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
  631. /* Execute the command, or just check syntax? */
  632. if (noexec)
  633. args[1] = "-nc";
  634. /* Redirect output. */
  635. fd = fildes[1];
  636. if (fd != 1) {
  637. dup2(fd, 1);
  638. close(fd);
  639. }
  640. /* Redirect stderr to /dev/null if we have to. */
  641. if (showerr == 0) {
  642. close(2);
  643. fd = open(_PATH_DEVNULL, O_WRONLY);
  644. if (fd >= 0 && fd != 2) {
  645. dup2(fd, 2);
  646. close(fd);
  647. }
  648. }
  649. /* Make sure the subshell doesn't field-split on our behalf. */
  650. unsetenv("IFS");
  651. if (fildes[0] != 1)
  652. close(fildes[0]);
  653. execve(_PATH_BSHELL, (char *const *) args, __environ);
  654. /* Bad. What now? */
  655. abort();
  656. }
  657. /* Function to execute a command and retrieve the results */
  658. /* pwordexp contains NULL if field-splitting is forbidden */
  659. static int
  660. exec_comm(char *comm, char **word, size_t * word_length,
  661. size_t * max_length, int flags, wordexp_t * pwordexp,
  662. const char *ifs, const char *ifs_white)
  663. {
  664. int fildes[2];
  665. int bufsize = 128;
  666. int buflen;
  667. int i;
  668. int status = 0;
  669. size_t maxnewlines = 0;
  670. char *buffer;
  671. pid_t pid;
  672. /* Don't fork() unless necessary */
  673. if (!comm || !*comm)
  674. return 0;
  675. if (pipe(fildes))
  676. /* Bad */
  677. return WRDE_NOSPACE;
  678. if ((pid = fork()) < 0) {
  679. /* Bad */
  680. close(fildes[0]);
  681. close(fildes[1]);
  682. return WRDE_NOSPACE;
  683. }
  684. if (pid == 0)
  685. exec_comm_child(comm, fildes, (flags & WRDE_SHOWERR), 0);
  686. /* Parent */
  687. close(fildes[1]);
  688. buffer = alloca(bufsize);
  689. if (!pwordexp)
  690. /* Quoted - no field splitting */
  691. {
  692. while (1) {
  693. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  694. if (waitpid(pid, &status, WNOHANG) == 0)
  695. continue;
  696. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  697. break;
  698. }
  699. maxnewlines += buflen;
  700. *word = w_addmem(*word, word_length, max_length, buffer, buflen);
  701. if (*word == NULL)
  702. goto no_space;
  703. }
  704. } else
  705. /* Not quoted - split fields */
  706. {
  707. int copying = 0;
  708. /* 'copying' is:
  709. * 0 when searching for first character in a field not IFS white space
  710. * 1 when copying the text of a field
  711. * 2 when searching for possible non-whitespace IFS
  712. * 3 when searching for non-newline after copying field
  713. */
  714. while (1) {
  715. if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
  716. if (waitpid(pid, &status, WNOHANG) == 0)
  717. continue;
  718. if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
  719. break;
  720. }
  721. for (i = 0; i < buflen; ++i) {
  722. if (strchr(ifs, buffer[i]) != NULL) {
  723. /* Current character is IFS */
  724. if (strchr(ifs_white, buffer[i]) == NULL) {
  725. /* Current character is IFS but not whitespace */
  726. if (copying == 2) {
  727. /* current character
  728. * |
  729. * V
  730. * eg: text<space><comma><space>moretext
  731. *
  732. * So, strip whitespace IFS (like at the start)
  733. */
  734. copying = 0;
  735. continue;
  736. }
  737. copying = 0;
  738. /* fall through and delimit field.. */
  739. } else {
  740. if (buffer[i] == '\n') {
  741. /* Current character is (IFS) newline */
  742. /* If copying a field, this is the end of it,
  743. but maybe all that's left is trailing newlines.
  744. So start searching for a non-newline. */
  745. if (copying == 1)
  746. copying = 3;
  747. continue;
  748. } else {
  749. /* Current character is IFS white space, but
  750. not a newline */
  751. /* If not either copying a field or searching
  752. for non-newline after a field, ignore it */
  753. if (copying != 1 && copying != 3)
  754. continue;
  755. /* End of field (search for non-ws IFS afterwards) */
  756. copying = 2;
  757. }
  758. }
  759. /* First IFS white space (non-newline), or IFS non-whitespace.
  760. * Delimit the field. Nulls are converted by w_addword. */
  761. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  762. goto no_space;
  763. *word = w_newword(word_length, max_length);
  764. maxnewlines = 0;
  765. /* fall back round the loop.. */
  766. } else {
  767. /* Not IFS character */
  768. if (copying == 3) {
  769. /* Nothing but (IFS) newlines since the last field,
  770. so delimit it here before starting new word */
  771. if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
  772. goto no_space;
  773. *word = w_newword(word_length, max_length);
  774. }
  775. copying = 1;
  776. if (buffer[i] == '\n') /* happens if newline not in IFS */
  777. maxnewlines++;
  778. else
  779. maxnewlines = 0;
  780. *word = w_addchar(*word, word_length, max_length,
  781. buffer[i]);
  782. if (*word == NULL)
  783. goto no_space;
  784. }
  785. }
  786. }
  787. }
  788. /* Chop off trailing newlines (required by POSIX.2) */
  789. /* Ensure we don't go back further than the beginning of the
  790. substitution (i.e. remove maxnewlines bytes at most) */
  791. while (maxnewlines-- != 0 &&
  792. *word_length > 0 && (*word)[*word_length - 1] == '\n') {
  793. (*word)[--*word_length] = '\0';
  794. /* If the last word was entirely newlines, turn it into a new word
  795. * which can be ignored if there's nothing following it. */
  796. if (*word_length == 0) {
  797. free(*word);
  798. *word = w_newword(word_length, max_length);
  799. break;
  800. }
  801. }
  802. close(fildes[0]);
  803. /* Check for syntax error (re-execute but with "-n" flag) */
  804. if (buflen < 1 && status != 0) {
  805. if ((pid = fork()) < 0) {
  806. /* Bad */
  807. return WRDE_NOSPACE;
  808. }
  809. if (pid == 0) {
  810. fildes[0] = fildes[1] = -1;
  811. exec_comm_child(comm, fildes, 0, 1);
  812. }
  813. if (waitpid(pid, &status, 0) == pid && status != 0)
  814. return WRDE_SYNTAX;
  815. }
  816. return 0;
  817. no_space:
  818. kill(pid, SIGKILL);
  819. waitpid(pid, NULL, 0);
  820. close(fildes[0]);
  821. return WRDE_NOSPACE;
  822. }
  823. static int
  824. parse_comm(char **word, size_t * word_length, size_t * max_length,
  825. const char *words, size_t * offset, int flags,
  826. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  827. {
  828. /* We are poised just after "$(" */
  829. int paren_depth = 1;
  830. int error = 0;
  831. int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  832. size_t comm_length;
  833. size_t comm_maxlen;
  834. char *comm = w_newword(&comm_length, &comm_maxlen);
  835. for (; words[*offset]; ++(*offset)) {
  836. switch (words[*offset]) {
  837. case '\'':
  838. if (quoted == 0)
  839. quoted = 1;
  840. else if (quoted == 1)
  841. quoted = 0;
  842. break;
  843. case '"':
  844. if (quoted == 0)
  845. quoted = 2;
  846. else if (quoted == 2)
  847. quoted = 0;
  848. break;
  849. case ')':
  850. if (!quoted && --paren_depth == 0) {
  851. /* Go -- give script to the shell */
  852. if (comm) {
  853. error = exec_comm(comm, word, word_length, max_length,
  854. flags, pwordexp, ifs, ifs_white);
  855. free(comm);
  856. }
  857. return error;
  858. }
  859. /* This is just part of the script */
  860. break;
  861. case '(':
  862. if (!quoted)
  863. ++paren_depth;
  864. }
  865. comm = w_addchar(comm, &comm_length, &comm_maxlen, words[*offset]);
  866. if (comm == NULL)
  867. return WRDE_NOSPACE;
  868. }
  869. /* Premature end */
  870. free(comm);
  871. return WRDE_SYNTAX;
  872. }
  873. static int
  874. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  875. const char *words, size_t * offset, int flags,
  876. wordexp_t * pwordexp, const char *ifs,
  877. const char *ifs_white)
  878. {
  879. /* We are poised just after "`" */
  880. int error;
  881. int squoting = 0;
  882. size_t comm_length;
  883. size_t comm_maxlen;
  884. char *comm = w_newword(&comm_length, &comm_maxlen);
  885. for (; words[*offset]; ++(*offset)) {
  886. switch (words[*offset]) {
  887. case '`':
  888. /* Go -- give the script to the shell */
  889. error = exec_comm(comm, word, word_length, max_length, flags,
  890. pwordexp, ifs, ifs_white);
  891. free(comm);
  892. return error;
  893. case '\\':
  894. if (squoting) {
  895. error = parse_qtd_backslash(&comm, &comm_length, &comm_maxlen,
  896. words, offset);
  897. if (error) {
  898. free(comm);
  899. return error;
  900. }
  901. break;
  902. }
  903. ++(*offset);
  904. error = parse_backslash(&comm, &comm_length, &comm_maxlen, words,
  905. offset);
  906. if (error) {
  907. free(comm);
  908. return error;
  909. }
  910. break;
  911. case '\'':
  912. squoting = 1 - squoting;
  913. default:
  914. comm = w_addchar(comm, &comm_length, &comm_maxlen,
  915. words[*offset]);
  916. if (comm == NULL)
  917. return WRDE_NOSPACE;
  918. }
  919. }
  920. /* Premature end */
  921. free(comm);
  922. return WRDE_SYNTAX;
  923. }
  924. static int
  925. parse_param(char **word, size_t * word_length, size_t * max_length,
  926. const char *words, size_t * offset, int flags,
  927. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  928. int quoted)
  929. {
  930. /* We are poised just after "$" */
  931. enum action {
  932. ACT_NONE,
  933. ACT_RP_SHORT_LEFT = '#',
  934. ACT_RP_LONG_LEFT = 'L',
  935. ACT_RP_SHORT_RIGHT = '%',
  936. ACT_RP_LONG_RIGHT = 'R',
  937. ACT_NULL_ERROR = '?',
  938. ACT_NULL_SUBST = '-',
  939. ACT_NONNULL_SUBST = '+',
  940. ACT_NULL_ASSIGN = '='
  941. };
  942. size_t env_length;
  943. size_t env_maxlen;
  944. size_t pat_length;
  945. size_t pat_maxlen;
  946. size_t start = *offset;
  947. char *env;
  948. char *pattern;
  949. char *value = NULL;
  950. enum action action = ACT_NONE;
  951. int depth = 0;
  952. int colon_seen = 0;
  953. int seen_hash = 0;
  954. int free_value = 0;
  955. int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
  956. int error;
  957. int special = 0;
  958. char buffer[21];
  959. int brace = words[*offset] == '{';
  960. env = w_newword(&env_length, &env_maxlen);
  961. pattern = w_newword(&pat_length, &pat_maxlen);
  962. if (brace)
  963. ++ * offset;
  964. /* First collect the parameter name. */
  965. if (words[*offset] == '#') {
  966. seen_hash = 1;
  967. if (!brace)
  968. goto envsubst;
  969. ++*offset;
  970. }
  971. if (isalpha(words[*offset]) || words[*offset] == '_') {
  972. /* Normal parameter name. */
  973. do {
  974. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  975. if (env == NULL)
  976. goto no_space;
  977. }
  978. while (isalnum(words[++*offset]) || words[*offset] == '_');
  979. } else if (isdigit(words[*offset])) {
  980. /* Numeric parameter name. */
  981. special = 1;
  982. do {
  983. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  984. if (env == NULL)
  985. goto no_space;
  986. if (!brace)
  987. goto envsubst;
  988. }
  989. while (isdigit(words[++*offset]));
  990. } else if (strchr("*@$", words[*offset]) != NULL) {
  991. /* Special parameter. */
  992. special = 1;
  993. env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
  994. if (env == NULL)
  995. goto no_space;
  996. ++*offset;
  997. } else {
  998. if (brace)
  999. goto syntax;
  1000. }
  1001. if (brace) {
  1002. /* Check for special action to be applied to the value. */
  1003. switch (words[*offset]) {
  1004. case '}':
  1005. /* Evaluate. */
  1006. goto envsubst;
  1007. case '#':
  1008. action = ACT_RP_SHORT_LEFT;
  1009. if (words[1 + *offset] == '#') {
  1010. ++*offset;
  1011. action = ACT_RP_LONG_LEFT;
  1012. }
  1013. break;
  1014. case '%':
  1015. action = ACT_RP_SHORT_RIGHT;
  1016. if (words[1 + *offset] == '%') {
  1017. ++*offset;
  1018. action = ACT_RP_LONG_RIGHT;
  1019. }
  1020. break;
  1021. case ':':
  1022. if (strchr("-=?+", words[1 + *offset]) == NULL)
  1023. goto syntax;
  1024. colon_seen = 1;
  1025. action = words[++*offset];
  1026. break;
  1027. case '-':
  1028. case '=':
  1029. case '?':
  1030. case '+':
  1031. action = words[*offset];
  1032. break;
  1033. default:
  1034. goto syntax;
  1035. }
  1036. /* Now collect the pattern, but don't expand it yet. */
  1037. ++*offset;
  1038. for (; words[*offset]; ++(*offset)) {
  1039. switch (words[*offset]) {
  1040. case '{':
  1041. if (!pattern_is_quoted)
  1042. ++depth;
  1043. break;
  1044. case '}':
  1045. if (!pattern_is_quoted) {
  1046. if (depth == 0)
  1047. goto envsubst;
  1048. --depth;
  1049. }
  1050. break;
  1051. case '\\':
  1052. if (pattern_is_quoted)
  1053. /* Quoted; treat as normal character. */
  1054. break;
  1055. /* Otherwise, it's an escape: next character is literal. */
  1056. if (words[++*offset] == '\0')
  1057. goto syntax;
  1058. pattern = w_addchar(pattern, &pat_length, &pat_maxlen, '\\');
  1059. if (pattern == NULL)
  1060. goto no_space;
  1061. break;
  1062. case '\'':
  1063. if (pattern_is_quoted == 0)
  1064. pattern_is_quoted = 1;
  1065. else if (pattern_is_quoted == 1)
  1066. pattern_is_quoted = 0;
  1067. break;
  1068. case '"':
  1069. if (pattern_is_quoted == 0)
  1070. pattern_is_quoted = 2;
  1071. else if (pattern_is_quoted == 2)
  1072. pattern_is_quoted = 0;
  1073. break;
  1074. }
  1075. pattern = w_addchar(pattern, &pat_length, &pat_maxlen,
  1076. words[*offset]);
  1077. if (pattern == NULL)
  1078. goto no_space;
  1079. }
  1080. }
  1081. /* End of input string -- remember to reparse the character that we
  1082. * stopped at. */
  1083. --(*offset);
  1084. envsubst:
  1085. if (words[start] == '{' && words[*offset] != '}')
  1086. goto syntax;
  1087. if (env == NULL) {
  1088. if (seen_hash) {
  1089. /* $# expands to the number of positional parameters */
  1090. buffer[20] = '\0';
  1091. value = _itoa(__libc_argc - 1, &buffer[20]);
  1092. seen_hash = 0;
  1093. } else {
  1094. /* Just $ on its own */
  1095. *offset = start - 1;
  1096. *word = w_addchar(*word, word_length, max_length, '$');
  1097. return *word ? 0 : WRDE_NOSPACE;
  1098. }
  1099. }
  1100. /* Is it a numeric parameter? */
  1101. else if (isdigit(env[0])) {
  1102. int n = atoi(env);
  1103. if (n >= __libc_argc)
  1104. /* Substitute NULL. */
  1105. value = NULL;
  1106. else
  1107. /* Replace with appropriate positional parameter. */
  1108. value = __libc_argv[n];
  1109. }
  1110. /* Is it a special parameter? */
  1111. else if (special) {
  1112. /* Is it `$$'? */
  1113. if (*env == '$') {
  1114. buffer[20] = '\0';
  1115. value = _itoa(getpid(), &buffer[20]);
  1116. }
  1117. /* Is it `${#*}' or `${#@}'? */
  1118. else if ((*env == '*' || *env == '@') && seen_hash) {
  1119. buffer[20] = '\0';
  1120. value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,
  1121. &buffer[20]);
  1122. *word = w_addstr(*word, word_length, max_length, value);
  1123. free(env);
  1124. free(pattern);
  1125. return *word ? 0 : WRDE_NOSPACE;
  1126. }
  1127. /* Is it `$*' or `$@' (unquoted) ? */
  1128. else if (*env == '*' || (*env == '@' && !quoted)) {
  1129. size_t plist_len = 0;
  1130. int p;
  1131. char *end;
  1132. /* Build up value parameter by parameter (copy them) */
  1133. for (p = 1; __libc_argv[p]; ++p)
  1134. plist_len += strlen(__libc_argv[p]) + 1; /* for space */
  1135. value = malloc(plist_len);
  1136. if (value == NULL)
  1137. goto no_space;
  1138. end = value;
  1139. *end = 0;
  1140. for (p = 1; __libc_argv[p]; ++p) {
  1141. if (p > 1)
  1142. *end++ = ' ';
  1143. end = stpcpy(end, __libc_argv[p]);
  1144. }
  1145. free_value = 1;
  1146. } else {
  1147. /* Must be a quoted `$@' */
  1148. assert(*env == '@' && quoted);
  1149. /* Each parameter is a separate word ("$@") */
  1150. if (__libc_argc == 2)
  1151. value = __libc_argv[1];
  1152. else if (__libc_argc > 2) {
  1153. int p;
  1154. /* Append first parameter to current word. */
  1155. value = w_addstr(*word, word_length, max_length,
  1156. __libc_argv[1]);
  1157. if (value == NULL || w_addword(pwordexp, value))
  1158. goto no_space;
  1159. for (p = 2; __libc_argv[p + 1]; p++) {
  1160. char *newword = strdup(__libc_argv[p]);
  1161. if (newword == NULL || w_addword(pwordexp, newword))
  1162. goto no_space;
  1163. }
  1164. /* Start a new word with the last parameter. */
  1165. *word = w_newword(word_length, max_length);
  1166. value = __libc_argv[p];
  1167. } else {
  1168. free(env);
  1169. free(pattern);
  1170. return 0;
  1171. }
  1172. }
  1173. } else
  1174. value = getenv(env);
  1175. if (value == NULL && (flags & WRDE_UNDEF)) {
  1176. /* Variable not defined. */
  1177. error = WRDE_BADVAL;
  1178. goto do_error;
  1179. }
  1180. if (action != ACT_NONE) {
  1181. int expand_pattern = 0;
  1182. /* First, find out if we need to expand pattern (i.e. if we will
  1183. * use it). */
  1184. switch (action) {
  1185. case ACT_RP_SHORT_LEFT:
  1186. case ACT_RP_LONG_LEFT:
  1187. case ACT_RP_SHORT_RIGHT:
  1188. case ACT_RP_LONG_RIGHT:
  1189. /* Always expand for these. */
  1190. expand_pattern = 1;
  1191. break;
  1192. case ACT_NULL_ERROR:
  1193. case ACT_NULL_SUBST:
  1194. case ACT_NULL_ASSIGN:
  1195. if (!value || (!*value && colon_seen))
  1196. /* If param is unset, or set but null and a colon has been seen,
  1197. the expansion of the pattern will be needed. */
  1198. expand_pattern = 1;
  1199. break;
  1200. case ACT_NONNULL_SUBST:
  1201. /* Expansion of word will be needed if parameter is set and not null,
  1202. or set null but no colon has been seen. */
  1203. if (value && (*value || !colon_seen))
  1204. expand_pattern = 1;
  1205. break;
  1206. default:
  1207. assert(!"Unrecognised action!");
  1208. }
  1209. if (expand_pattern) {
  1210. /* We need to perform tilde expansion, parameter expansion,
  1211. command substitution, and arithmetic expansion. We also
  1212. have to be a bit careful with wildcard characters, as
  1213. pattern might be given to fnmatch soon. To do this, we
  1214. convert quotes to escapes. */
  1215. char *expanded;
  1216. size_t exp_len;
  1217. size_t exp_maxl;
  1218. char *p;
  1219. int quotes = 0; /* 1: single quotes; 2: double */
  1220. expanded = w_newword(&exp_len, &exp_maxl);
  1221. for (p = pattern; p && *p; p++) {
  1222. size_t _offset;
  1223. switch (*p) {
  1224. case '"':
  1225. if (quotes == 2)
  1226. quotes = 0;
  1227. else if (quotes == 0)
  1228. quotes = 2;
  1229. else
  1230. break;
  1231. continue;
  1232. case '\'':
  1233. if (quotes == 1)
  1234. quotes = 0;
  1235. else if (quotes == 0)
  1236. quotes = 1;
  1237. else
  1238. break;
  1239. continue;
  1240. case '*':
  1241. case '?':
  1242. if (quotes) {
  1243. /* Convert quoted wildchar to escaped wildchar. */
  1244. expanded = w_addchar(expanded, &exp_len,
  1245. &exp_maxl, '\\');
  1246. if (expanded == NULL)
  1247. goto no_space;
  1248. }
  1249. break;
  1250. case '$':
  1251. _offset = 0;
  1252. error = parse_dollars(&expanded, &exp_len, &exp_maxl, p,
  1253. &_offset, flags, NULL, NULL, NULL, 1);
  1254. if (error) {
  1255. if (free_value)
  1256. free(value);
  1257. free(expanded);
  1258. goto do_error;
  1259. }
  1260. p += _offset;
  1261. continue;
  1262. case '~':
  1263. if (quotes || exp_len)
  1264. break;
  1265. _offset = 0;
  1266. error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,
  1267. &_offset, 0);
  1268. if (error) {
  1269. if (free_value)
  1270. free(value);
  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. free(pattern);
  1288. pattern = expanded;
  1289. }
  1290. switch (action) {
  1291. case ACT_RP_SHORT_LEFT:
  1292. case ACT_RP_LONG_LEFT:
  1293. case ACT_RP_SHORT_RIGHT:
  1294. case ACT_RP_LONG_RIGHT:
  1295. {
  1296. char *p;
  1297. char c;
  1298. char *end;
  1299. if (value == NULL || pattern == NULL || *pattern == '\0')
  1300. break;
  1301. end = value + strlen(value);
  1302. switch (action) {
  1303. case ACT_RP_SHORT_LEFT:
  1304. for (p = value; p <= end; ++p) {
  1305. c = *p;
  1306. *p = '\0';
  1307. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1308. *p = c;
  1309. if (free_value) {
  1310. char *newval = strdup(p);
  1311. if (newval == NULL) {
  1312. free(value);
  1313. goto no_space;
  1314. }
  1315. free(value);
  1316. value = newval;
  1317. } else
  1318. value = p;
  1319. break;
  1320. }
  1321. *p = c;
  1322. }
  1323. break;
  1324. case ACT_RP_LONG_LEFT:
  1325. for (p = end; p >= value; --p) {
  1326. c = *p;
  1327. *p = '\0';
  1328. if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
  1329. *p = c;
  1330. if (free_value) {
  1331. char *newval = strdup(p);
  1332. if (newval == NULL) {
  1333. free(value);
  1334. goto no_space;
  1335. }
  1336. free(value);
  1337. value = newval;
  1338. } else
  1339. value = p;
  1340. break;
  1341. }
  1342. *p = c;
  1343. }
  1344. break;
  1345. case ACT_RP_SHORT_RIGHT:
  1346. for (p = end; p >= value; --p) {
  1347. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1348. char *newval;
  1349. newval = malloc(p - value + 1);
  1350. if (newval == NULL) {
  1351. if (free_value)
  1352. free(value);
  1353. goto no_space;
  1354. }
  1355. *(char *) mempcpy(newval, value, p - value) = '\0';
  1356. if (free_value)
  1357. free(value);
  1358. value = newval;
  1359. free_value = 1;
  1360. break;
  1361. }
  1362. }
  1363. break;
  1364. case ACT_RP_LONG_RIGHT:
  1365. for (p = value; p <= end; ++p) {
  1366. if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {
  1367. char *newval;
  1368. newval = malloc(p - value + 1);
  1369. if (newval == NULL) {
  1370. if (free_value)
  1371. free(value);
  1372. goto no_space;
  1373. }
  1374. *(char *) mempcpy(newval, value, p - value) = '\0';
  1375. if (free_value)
  1376. free(value);
  1377. value = newval;
  1378. free_value = 1;
  1379. break;
  1380. }
  1381. }
  1382. break;
  1383. default:
  1384. break;
  1385. }
  1386. break;
  1387. }
  1388. case ACT_NULL_ERROR:
  1389. if (value && *value)
  1390. /* Substitute parameter */
  1391. break;
  1392. error = 0;
  1393. if (!colon_seen && value)
  1394. /* Substitute NULL */
  1395. ;
  1396. else if (*pattern)
  1397. fprintf(stderr, "%s: %s\n", env, pattern);
  1398. else {
  1399. fprintf(stderr, "%s: parameter null or not set\n", env);
  1400. error = WRDE_BADVAL;
  1401. }
  1402. if (free_value)
  1403. free(value);
  1404. goto do_error;
  1405. case ACT_NULL_SUBST:
  1406. if (value && *value)
  1407. /* Substitute parameter */
  1408. break;
  1409. if (free_value)
  1410. free(value);
  1411. if (!colon_seen && value)
  1412. /* Substitute NULL */
  1413. goto success;
  1414. value = pattern ? strdup(pattern) : pattern;
  1415. free_value = 1;
  1416. if (pattern && !value)
  1417. goto no_space;
  1418. break;
  1419. case ACT_NONNULL_SUBST:
  1420. if (value && (*value || !colon_seen)) {
  1421. if (free_value)
  1422. free(value);
  1423. value = pattern ? strdup(pattern) : pattern;
  1424. free_value = 1;
  1425. if (pattern && !value)
  1426. goto no_space;
  1427. break;
  1428. }
  1429. /* Substitute NULL */
  1430. if (free_value)
  1431. free(value);
  1432. goto success;
  1433. case ACT_NULL_ASSIGN:
  1434. if (value && *value)
  1435. /* Substitute parameter */
  1436. break;
  1437. if (!colon_seen && value) {
  1438. /* Substitute NULL */
  1439. if (free_value)
  1440. free(value);
  1441. goto success;
  1442. }
  1443. if (free_value)
  1444. free(value);
  1445. value = pattern ? strdup(pattern) : pattern;
  1446. free_value = 1;
  1447. if (pattern && !value)
  1448. goto no_space;
  1449. setenv(env, value, 1);
  1450. break;
  1451. default:
  1452. assert(!"Unrecognised action!");
  1453. }
  1454. }
  1455. free(env);
  1456. env = NULL;
  1457. free(pattern);
  1458. pattern = NULL;
  1459. if (seen_hash) {
  1460. char param_length[21];
  1461. param_length[20] = '\0';
  1462. *word = w_addstr(*word, word_length, max_length,
  1463. _itoa(value ? strlen(value) : 0,
  1464. &param_length[20]));
  1465. if (free_value) {
  1466. assert(value != NULL);
  1467. free(value);
  1468. }
  1469. return *word ? 0 : WRDE_NOSPACE;
  1470. }
  1471. if (value == NULL)
  1472. return 0;
  1473. if (quoted || !pwordexp) {
  1474. /* Quoted - no field split */
  1475. *word = w_addstr(*word, word_length, max_length, value);
  1476. if (free_value)
  1477. free(value);
  1478. return *word ? 0 : WRDE_NOSPACE;
  1479. } else {
  1480. /* Need to field-split */
  1481. char *value_copy = strdup(value); /* Don't modify value */
  1482. char *field_begin = value_copy;
  1483. int seen_nonws_ifs = 0;
  1484. if (free_value)
  1485. free(value);
  1486. if (value_copy == NULL)
  1487. goto no_space;
  1488. do {
  1489. char *field_end = field_begin;
  1490. char *next_field;
  1491. /* If this isn't the first field, start a new word */
  1492. if (field_begin != value_copy) {
  1493. if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {
  1494. free(value_copy);
  1495. goto no_space;
  1496. }
  1497. *word = w_newword(word_length, max_length);
  1498. }
  1499. /* Skip IFS whitespace before the field */
  1500. field_begin += strspn(field_begin, ifs_white);
  1501. if (!seen_nonws_ifs && *field_begin == 0)
  1502. /* Nothing but whitespace */
  1503. break;
  1504. /* Search for the end of the field */
  1505. field_end = field_begin + strcspn(field_begin, ifs);
  1506. /* Set up pointer to the character after end of field and
  1507. skip whitespace IFS after it. */
  1508. next_field = field_end + strspn(field_end, ifs_white);
  1509. /* Skip at most one non-whitespace IFS character after the field */
  1510. seen_nonws_ifs = 0;
  1511. if (*next_field && strchr(ifs, *next_field)) {
  1512. seen_nonws_ifs = 1;
  1513. next_field++;
  1514. }
  1515. /* Null-terminate it */
  1516. *field_end = 0;
  1517. /* Tag a copy onto the current word */
  1518. *word = w_addstr(*word, word_length, max_length, field_begin);
  1519. if (*word == NULL && *field_begin != '\0') {
  1520. free(value_copy);
  1521. goto no_space;
  1522. }
  1523. field_begin = next_field;
  1524. }
  1525. while (seen_nonws_ifs || *field_begin);
  1526. free(value_copy);
  1527. }
  1528. return 0;
  1529. success:
  1530. error = 0;
  1531. goto do_error;
  1532. no_space:
  1533. error = WRDE_NOSPACE;
  1534. goto do_error;
  1535. syntax:
  1536. error = WRDE_SYNTAX;
  1537. do_error:
  1538. free(env);
  1539. free(pattern);
  1540. return error;
  1541. }
  1542. #else
  1543. static __inline__ int
  1544. parse_backtick(char **word, size_t * word_length, size_t * max_length,
  1545. const char *words, size_t * offset, int flags,
  1546. wordexp_t * pwordexp, const char *ifs,
  1547. const char *ifs_white)
  1548. {
  1549. return 0;
  1550. }
  1551. #endif
  1552. static int
  1553. parse_dollars(char **word, size_t * word_length, size_t * max_length,
  1554. const char *words, size_t * offset, int flags,
  1555. wordexp_t * pwordexp, const char *ifs, const char *ifs_white,
  1556. int quoted)
  1557. {
  1558. /* We are poised _at_ "$" */
  1559. switch (words[1 + *offset]) {
  1560. case '"':
  1561. case '\'':
  1562. case 0:
  1563. *word = w_addchar(*word, word_length, max_length, '$');
  1564. return *word ? 0 : WRDE_NOSPACE;
  1565. #ifdef __WORDEXP_FULL
  1566. case '(':
  1567. if (words[2 + *offset] == '(') {
  1568. /* Differentiate between $((1+3)) and $((echo);(ls)) */
  1569. int i = 3 + *offset;
  1570. int depth = 0;
  1571. while (words[i] && !(depth == 0 && words[i] == ')')) {
  1572. if (words[i] == '(')
  1573. ++depth;
  1574. else if (words[i] == ')')
  1575. --depth;
  1576. ++i;
  1577. }
  1578. if (words[i] == ')' && words[i + 1] == ')') {
  1579. (*offset) += 3;
  1580. /* Call parse_arith -- 0 is for "no brackets" */
  1581. return parse_arith(word, word_length, max_length, words,
  1582. offset, flags, 0);
  1583. }
  1584. }
  1585. if (flags & WRDE_NOCMD)
  1586. return WRDE_CMDSUB;
  1587. (*offset) += 2;
  1588. return parse_comm(word, word_length, max_length, words, offset,
  1589. flags, quoted ? NULL : pwordexp, ifs, ifs_white);
  1590. case '[':
  1591. (*offset) += 2;
  1592. /* Call parse_arith -- 1 is for "brackets" */
  1593. return parse_arith(word, word_length, max_length, words, offset,
  1594. flags, 1);
  1595. case '{':
  1596. default:
  1597. ++(*offset); /* parse_param needs to know if "{" is there */
  1598. return parse_param(word, word_length, max_length, words, offset,
  1599. flags, pwordexp, ifs, ifs_white, quoted);
  1600. #else
  1601. default:
  1602. ++(*offset); /* parse_param needs to know if "{" is there */
  1603. return 0;
  1604. #endif
  1605. }
  1606. }
  1607. static int
  1608. parse_dquote(char **word, size_t * word_length, size_t * max_length,
  1609. const char *words, size_t * offset, int flags,
  1610. wordexp_t * pwordexp, const char *ifs, const char *ifs_white)
  1611. {
  1612. /* We are poised just after a double-quote */
  1613. int error;
  1614. for (; words[*offset]; ++(*offset)) {
  1615. switch (words[*offset]) {
  1616. case '"':
  1617. return 0;
  1618. case '$':
  1619. error = parse_dollars(word, word_length, max_length, words, offset,
  1620. flags, pwordexp, ifs, ifs_white, 1);
  1621. /* The ``1'' here is to tell parse_dollars not to
  1622. * split the fields. It may need to, however ("$@").
  1623. */
  1624. if (error)
  1625. return error;
  1626. break;
  1627. case '`':
  1628. if (flags & WRDE_NOCMD)
  1629. return WRDE_CMDSUB;
  1630. ++(*offset);
  1631. error = parse_backtick(word, word_length, max_length, words,
  1632. offset, flags, NULL, NULL, NULL);
  1633. /* The first NULL here is to tell parse_backtick not to
  1634. * split the fields.
  1635. */
  1636. if (error)
  1637. return error;
  1638. break;
  1639. case '\\':
  1640. error = parse_qtd_backslash(word, word_length, max_length, words,
  1641. offset);
  1642. if (error)
  1643. return error;
  1644. break;
  1645. default:
  1646. *word = w_addchar(*word, word_length, max_length, words[*offset]);
  1647. if (*word == NULL)
  1648. return WRDE_NOSPACE;
  1649. }
  1650. }
  1651. /* Unterminated string */
  1652. return WRDE_SYNTAX;
  1653. }
  1654. /*
  1655. * wordfree() is to be called after pwordexp is finished with.
  1656. */
  1657. void wordfree(wordexp_t * pwordexp)
  1658. {
  1659. /* wordexp can set pwordexp to NULL */
  1660. if (pwordexp && pwordexp->we_wordv) {
  1661. char **wordv = pwordexp->we_wordv;
  1662. for (wordv += pwordexp->we_offs; *wordv; ++wordv)
  1663. free(*wordv);
  1664. free(pwordexp->we_wordv);
  1665. pwordexp->we_wordv = NULL;
  1666. }
  1667. }
  1668. libc_hidden_def(wordfree)
  1669. /*
  1670. * wordexp()
  1671. */
  1672. int wordexp(const char *words, wordexp_t * we, int flags)
  1673. {
  1674. size_t words_offset;
  1675. size_t word_length;
  1676. size_t max_length;
  1677. char *word = w_newword(&word_length, &max_length);
  1678. int error;
  1679. char *ifs;
  1680. char ifs_white[4];
  1681. wordexp_t old_word = *we;
  1682. if (flags & WRDE_REUSE) {
  1683. /* Minimal implementation of WRDE_REUSE for now */
  1684. wordfree(we);
  1685. old_word.we_wordv = NULL;
  1686. }
  1687. if ((flags & WRDE_APPEND) == 0) {
  1688. we->we_wordc = 0;
  1689. if (flags & WRDE_DOOFFS) {
  1690. we->we_wordv = calloc(1 + we->we_offs, sizeof(char *));
  1691. if (we->we_wordv == NULL) {
  1692. error = WRDE_NOSPACE;
  1693. goto do_error;
  1694. }
  1695. } else {
  1696. we->we_wordv = calloc(1, sizeof(char *));
  1697. if (we->we_wordv == NULL) {
  1698. error = WRDE_NOSPACE;
  1699. goto do_error;
  1700. }
  1701. we->we_offs = 0;
  1702. }
  1703. }
  1704. /* Find out what the field separators are.
  1705. * There are two types: whitespace and non-whitespace.
  1706. */
  1707. ifs = getenv("IFS");
  1708. if (!ifs)
  1709. /* IFS unset - use <space><tab><newline>. */
  1710. ifs = strcpy(ifs_white, " \t\n");
  1711. else {
  1712. char *ifsch = ifs;
  1713. char *whch = ifs_white;
  1714. /* Start off with no whitespace IFS characters */
  1715. ifs_white[0] = '\0';
  1716. while (*ifsch != '\0') {
  1717. if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {
  1718. /* Whitespace IFS. See first whether it is already in our
  1719. collection. */
  1720. char *runp = ifs_white;
  1721. while (runp < whch && *runp != '\0' && *runp != *ifsch)
  1722. ++runp;
  1723. if (runp == whch)
  1724. *whch++ = *ifsch;
  1725. }
  1726. ++ifsch;
  1727. }
  1728. *whch = '\0';
  1729. }
  1730. for (words_offset = 0; words[words_offset]; ++words_offset)
  1731. switch (words[words_offset]) {
  1732. case '\\':
  1733. error = parse_backslash(&word, &word_length, &max_length, words,
  1734. &words_offset);
  1735. if (error)
  1736. goto do_error;
  1737. break;
  1738. case '$':
  1739. error = parse_dollars(&word, &word_length, &max_length, words,
  1740. &words_offset, flags, we, ifs, ifs_white,
  1741. 0);
  1742. if (error)
  1743. goto do_error;
  1744. break;
  1745. case '`':
  1746. if (flags & WRDE_NOCMD) {
  1747. error = WRDE_CMDSUB;
  1748. goto do_error;
  1749. }
  1750. ++words_offset;
  1751. error = parse_backtick(&word, &word_length, &max_length, words,
  1752. &words_offset, flags, we, ifs,
  1753. ifs_white);
  1754. if (error)
  1755. goto do_error;
  1756. break;
  1757. case '"':
  1758. ++words_offset;
  1759. error = parse_dquote(&word, &word_length, &max_length, words,
  1760. &words_offset, flags, we, ifs, ifs_white);
  1761. if (error)
  1762. goto do_error;
  1763. if (!word_length) {
  1764. error = w_addword(we, NULL);
  1765. if (error)
  1766. return error;
  1767. }
  1768. break;
  1769. case '\'':
  1770. ++words_offset;
  1771. error = parse_squote(&word, &word_length, &max_length, words,
  1772. &words_offset);
  1773. if (error)
  1774. goto do_error;
  1775. if (!word_length) {
  1776. error = w_addword(we, NULL);
  1777. if (error)
  1778. return error;
  1779. }
  1780. break;
  1781. case '~':
  1782. error = parse_tilde(&word, &word_length, &max_length, words,
  1783. &words_offset, we->we_wordc);
  1784. if (error)
  1785. goto do_error;
  1786. break;
  1787. case '*':
  1788. case '[':
  1789. case '?':
  1790. error = parse_glob(&word, &word_length, &max_length, words,
  1791. &words_offset, flags, we, ifs, ifs_white);
  1792. if (error)
  1793. goto do_error;
  1794. break;
  1795. default:
  1796. /* Is it a word separator? */
  1797. if (strchr(" \t", words[words_offset]) == NULL) {
  1798. char ch = words[words_offset];
  1799. /* Not a word separator -- but is it a valid word char? */
  1800. if (strchr("\n|&;<>(){}", ch)) {
  1801. /* Fail */
  1802. error = WRDE_BADCHAR;
  1803. goto do_error;
  1804. }
  1805. /* "Ordinary" character -- add it to word */
  1806. word = w_addchar(word, &word_length, &max_length, ch);
  1807. if (word == NULL) {
  1808. error = WRDE_NOSPACE;
  1809. goto do_error;
  1810. }
  1811. break;
  1812. }
  1813. /* If a word has been delimited, add it to the list. */
  1814. if (word != NULL) {
  1815. error = w_addword(we, word);
  1816. if (error)
  1817. goto do_error;
  1818. }
  1819. word = w_newword(&word_length, &max_length);
  1820. }
  1821. /* End of string */
  1822. /* There was a word separator at the end */
  1823. if (word == NULL) /* i.e. w_newword */
  1824. return 0;
  1825. /* There was no field separator at the end */
  1826. return w_addword(we, word);
  1827. do_error:
  1828. /* Error:
  1829. * free memory used (unless error is WRDE_NOSPACE), and
  1830. * set we members back to what they were.
  1831. */
  1832. free(word);
  1833. if (error == WRDE_NOSPACE)
  1834. return WRDE_NOSPACE;
  1835. if ((flags & WRDE_APPEND) == 0)
  1836. wordfree(we);
  1837. *we = old_word;
  1838. return error;
  1839. }