wordexp.c 49 KB

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