wordexp.c 48 KB

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