wordexp.c 48 KB

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