wordexp.c 48 KB

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