wordexp.c 48 KB

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