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