wordexp.c 48 KB

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