wordexp.c 48 KB

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