wordexp.c 48 KB

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