wordexp.c 48 KB

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