wordexp.c 48 KB

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