wordexp.c 48 KB

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