wordexp.c 48 KB

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