wordsplit.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. /* wordsplit - a word splitter
  2. Copyright (C) 2009-2018 Sergey Poznyakoff
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3 of the License, or (at your
  6. option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifdef HAVE_CONFIG_H
  14. # include <config.h>
  15. #endif
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <pwd.h>
  24. #include <glob.h>
  25. #if ENABLE_NLS
  26. # include <gettext.h>
  27. #else
  28. # define gettext(msgid) msgid
  29. #endif
  30. #define _(msgid) gettext (msgid)
  31. #define N_(msgid) msgid
  32. #include <wordsplit.h>
  33. #define ISWS(c) ((c)==' '||(c)=='\t'||(c)=='\n')
  34. #define ISDELIM(ws,c) \
  35. (strchr ((ws)->ws_delim, (c)) != NULL)
  36. #define ISPUNCT(c) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",(c))!=NULL)
  37. #define ISUPPER(c) ('A' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'Z')
  38. #define ISLOWER(c) ('a' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'z')
  39. #define ISALPHA(c) (ISUPPER(c) || ISLOWER(c))
  40. #define ISDIGIT(c) ('0' <= ((unsigned) (c)) && ((unsigned) (c)) <= '9')
  41. #define ISXDIGIT(c) (strchr("abcdefABCDEF", c)!=NULL)
  42. #define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
  43. #define ISPRINT(c) (' ' <= ((unsigned) (c)) && ((unsigned) (c)) <= 127)
  44. #define ISVARBEG(c) (ISALPHA(c) || c == '_')
  45. #define ISVARCHR(c) (ISALNUM(c) || c == '_')
  46. #define WSP_RETURN_DELIMS(wsp) \
  47. ((wsp)->ws_flags & WRDSF_RETURN_DELIMS || ((wsp)->ws_options & WRDSO_MAXWORDS))
  48. #define ALLOC_INIT 128
  49. #define ALLOC_INCR 128
  50. static void
  51. _wsplt_alloc_die (struct wordsplit *wsp)
  52. {
  53. wsp->ws_error ("%s", _("memory exhausted"));
  54. abort ();
  55. }
  56. static void __WORDSPLIT_ATTRIBUTE_FORMAT ((__printf__, 1, 2))
  57. _wsplt_error (const char *fmt, ...)
  58. {
  59. va_list ap;
  60. va_start (ap, fmt);
  61. vfprintf (stderr, fmt, ap);
  62. va_end (ap);
  63. fputc ('\n', stderr);
  64. }
  65. static void wordsplit_free_nodes (struct wordsplit *);
  66. static int
  67. _wsplt_seterr (struct wordsplit *wsp, int ec)
  68. {
  69. wsp->ws_errno = ec;
  70. if (wsp->ws_flags & WRDSF_SHOWERR)
  71. wordsplit_perror (wsp);
  72. return ec;
  73. }
  74. static int
  75. _wsplt_nomem (struct wordsplit *wsp)
  76. {
  77. errno = ENOMEM;
  78. wsp->ws_errno = WRDSE_NOSPACE;
  79. if (wsp->ws_flags & WRDSF_ENOMEMABRT)
  80. wsp->ws_alloc_die (wsp);
  81. if (wsp->ws_flags & WRDSF_SHOWERR)
  82. wordsplit_perror (wsp);
  83. if (!(wsp->ws_flags & WRDSF_REUSE))
  84. wordsplit_free (wsp);
  85. wordsplit_free_nodes (wsp);
  86. return wsp->ws_errno;
  87. }
  88. static int wordsplit_run (const char *command, size_t length,
  89. struct wordsplit *wsp,
  90. int flags, int lvl);
  91. static int wordsplit_init (struct wordsplit *wsp, const char *input, size_t len,
  92. int flags);
  93. static int wordsplit_process_list (struct wordsplit *wsp, size_t start);
  94. static int wordsplit_finish (struct wordsplit *wsp);
  95. static int
  96. _wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss,
  97. char const *str, int len,
  98. int flags, int finalize)
  99. {
  100. int rc;
  101. wss->ws_delim = wsp->ws_delim;
  102. wss->ws_debug = wsp->ws_debug;
  103. wss->ws_error = wsp->ws_error;
  104. wss->ws_alloc_die = wsp->ws_alloc_die;
  105. if (!(flags & WRDSF_NOVAR))
  106. {
  107. wss->ws_env = wsp->ws_env;
  108. wss->ws_getvar = wsp->ws_getvar;
  109. flags |= wsp->ws_flags & (WRDSF_ENV | WRDSF_ENV_KV | WRDSF_GETVAR);
  110. }
  111. if (!(flags & WRDSF_NOCMD))
  112. {
  113. wss->ws_command = wsp->ws_command;
  114. }
  115. if ((flags & (WRDSF_NOVAR|WRDSF_NOCMD)) != (WRDSF_NOVAR|WRDSF_NOCMD))
  116. {
  117. wss->ws_closure = wsp->ws_closure;
  118. flags |= wsp->ws_flags & WRDSF_CLOSURE;
  119. }
  120. wss->ws_options = wsp->ws_options;
  121. flags |= WRDSF_DELIM
  122. | WRDSF_ALLOC_DIE
  123. | WRDSF_ERROR
  124. | WRDSF_DEBUG
  125. | (wsp->ws_flags & (WRDSF_SHOWDBG | WRDSF_SHOWERR | WRDSF_OPTIONS));
  126. rc = wordsplit_init (wss, str, len, flags);
  127. if (rc)
  128. return rc;
  129. wss->ws_lvl = wsp->ws_lvl + 1;
  130. rc = wordsplit_process_list (wss, 0);
  131. if (rc)
  132. {
  133. wordsplit_free_nodes (wss);
  134. return rc;
  135. }
  136. if (finalize)
  137. {
  138. rc = wordsplit_finish (wss);
  139. wordsplit_free_nodes (wss);
  140. }
  141. return rc;
  142. }
  143. static void
  144. _wsplt_seterr_sub (struct wordsplit *wsp, struct wordsplit *wss)
  145. {
  146. if (wsp->ws_errno == WRDSE_USERERR)
  147. free (wsp->ws_usererr);
  148. wsp->ws_errno = wss->ws_errno;
  149. if (wss->ws_errno == WRDSE_USERERR)
  150. {
  151. wsp->ws_usererr = wss->ws_usererr;
  152. wss->ws_errno = WRDSE_EOF;
  153. wss->ws_usererr = NULL;
  154. }
  155. }
  156. static void
  157. wordsplit_init0 (struct wordsplit *wsp)
  158. {
  159. if (wsp->ws_flags & WRDSF_REUSE)
  160. {
  161. if (!(wsp->ws_flags & WRDSF_APPEND))
  162. wordsplit_free_words (wsp);
  163. wordsplit_clearerr (wsp);
  164. }
  165. else
  166. {
  167. wsp->ws_wordv = NULL;
  168. wsp->ws_wordc = 0;
  169. wsp->ws_wordn = 0;
  170. }
  171. wsp->ws_errno = 0;
  172. }
  173. char wordsplit_c_escape_tab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v";
  174. static int
  175. wordsplit_init (struct wordsplit *wsp, const char *input, size_t len,
  176. int flags)
  177. {
  178. wsp->ws_flags = flags;
  179. if (!(wsp->ws_flags & WRDSF_ALLOC_DIE))
  180. wsp->ws_alloc_die = _wsplt_alloc_die;
  181. if (!(wsp->ws_flags & WRDSF_ERROR))
  182. wsp->ws_error = _wsplt_error;
  183. if (!(wsp->ws_flags & WRDSF_NOVAR))
  184. {
  185. /* These will be initialized on first variable assignment */
  186. wsp->ws_envidx = wsp->ws_envsiz = 0;
  187. wsp->ws_envbuf = NULL;
  188. }
  189. if (!(wsp->ws_flags & WRDSF_NOCMD))
  190. {
  191. if (!wsp->ws_command)
  192. {
  193. _wsplt_seterr (wsp, WRDSE_USAGE);
  194. errno = EINVAL;
  195. return wsp->ws_errno;
  196. }
  197. }
  198. if (wsp->ws_flags & WRDSF_SHOWDBG)
  199. {
  200. if (!(wsp->ws_flags & WRDSF_DEBUG))
  201. {
  202. if (wsp->ws_flags & WRDSF_ERROR)
  203. wsp->ws_debug = wsp->ws_error;
  204. else if (wsp->ws_flags & WRDSF_SHOWERR)
  205. wsp->ws_debug = _wsplt_error;
  206. else
  207. wsp->ws_flags &= ~WRDSF_SHOWDBG;
  208. }
  209. }
  210. wsp->ws_input = input;
  211. wsp->ws_len = len;
  212. if (!(wsp->ws_flags & WRDSF_DOOFFS))
  213. wsp->ws_offs = 0;
  214. if (!(wsp->ws_flags & WRDSF_DELIM))
  215. wsp->ws_delim = " \t\n";
  216. if (!(wsp->ws_flags & WRDSF_COMMENT))
  217. wsp->ws_comment = NULL;
  218. if (!(wsp->ws_flags & WRDSF_CLOSURE))
  219. wsp->ws_closure = NULL;
  220. if (!(wsp->ws_flags & WRDSF_OPTIONS))
  221. wsp->ws_options = 0;
  222. if (wsp->ws_flags & WRDSF_ESCAPE)
  223. {
  224. if (!wsp->ws_escape[WRDSX_WORD])
  225. wsp->ws_escape[WRDSX_WORD] = "";
  226. if (!wsp->ws_escape[WRDSX_QUOTE])
  227. wsp->ws_escape[WRDSX_QUOTE] = "";
  228. }
  229. else
  230. {
  231. if (wsp->ws_flags & WRDSF_CESCAPES)
  232. {
  233. wsp->ws_escape[WRDSX_WORD] = wordsplit_c_escape_tab;
  234. wsp->ws_escape[WRDSX_QUOTE] = wordsplit_c_escape_tab;
  235. wsp->ws_options |= WRDSO_OESC_QUOTE | WRDSO_OESC_WORD
  236. | WRDSO_XESC_QUOTE | WRDSO_XESC_WORD;
  237. }
  238. else
  239. {
  240. wsp->ws_escape[WRDSX_WORD] = "";
  241. wsp->ws_escape[WRDSX_QUOTE] = "\\\\\"\"";
  242. wsp->ws_options |= WRDSO_BSKEEP_QUOTE;
  243. }
  244. }
  245. wsp->ws_endp = 0;
  246. wsp->ws_wordi = 0;
  247. if (wsp->ws_flags & WRDSF_REUSE)
  248. wordsplit_free_nodes (wsp);
  249. wsp->ws_head = wsp->ws_tail = NULL;
  250. wordsplit_init0 (wsp);
  251. return 0;
  252. }
  253. static int
  254. alloc_space (struct wordsplit *wsp, size_t count)
  255. {
  256. size_t offs = (wsp->ws_flags & WRDSF_DOOFFS) ? wsp->ws_offs : 0;
  257. char **ptr;
  258. size_t newalloc;
  259. if (wsp->ws_wordv == NULL)
  260. {
  261. newalloc = offs + count > ALLOC_INIT ? count : ALLOC_INIT;
  262. ptr = calloc (newalloc, sizeof (ptr[0]));
  263. }
  264. else if (wsp->ws_wordn < offs + wsp->ws_wordc + count)
  265. {
  266. newalloc = offs + wsp->ws_wordc +
  267. (count > ALLOC_INCR ? count : ALLOC_INCR);
  268. ptr = realloc (wsp->ws_wordv, newalloc * sizeof (ptr[0]));
  269. }
  270. else
  271. return 0;
  272. if (ptr)
  273. {
  274. wsp->ws_wordn = newalloc;
  275. wsp->ws_wordv = ptr;
  276. }
  277. else
  278. return _wsplt_nomem (wsp);
  279. return 0;
  280. }
  281. /* Node state flags */
  282. #define _WSNF_NULL 0x01 /* null node (a noop) */
  283. #define _WSNF_WORD 0x02 /* node contains word in v.word */
  284. #define _WSNF_QUOTE 0x04 /* text is quoted */
  285. #define _WSNF_NOEXPAND 0x08 /* text is not subject to expansion */
  286. #define _WSNF_JOIN 0x10 /* node must be joined with the next node */
  287. #define _WSNF_SEXP 0x20 /* is a sed expression */
  288. #define _WSNF_DELIM 0x40 /* node is a delimiter */
  289. #define _WSNF_EMPTYOK 0x0100 /* special flag indicating that
  290. wordsplit_add_segm must add the
  291. segment even if it is empty */
  292. struct wordsplit_node
  293. {
  294. struct wordsplit_node *prev; /* Previous element */
  295. struct wordsplit_node *next; /* Next element */
  296. int flags; /* Node flags */
  297. union
  298. {
  299. struct
  300. {
  301. size_t beg; /* Start of word in ws_input */
  302. size_t end; /* End of word in ws_input */
  303. } segm;
  304. char *word;
  305. } v;
  306. };
  307. static const char *
  308. wsnode_flagstr (int flags)
  309. {
  310. static char retbuf[7];
  311. char *p = retbuf;
  312. if (flags & _WSNF_WORD)
  313. *p++ = 'w';
  314. else if (flags & _WSNF_NULL)
  315. *p++ = 'n';
  316. else
  317. *p++ = '-';
  318. if (flags & _WSNF_QUOTE)
  319. *p++ = 'q';
  320. else
  321. *p++ = '-';
  322. if (flags & _WSNF_NOEXPAND)
  323. *p++ = 'E';
  324. else
  325. *p++ = '-';
  326. if (flags & _WSNF_JOIN)
  327. *p++ = 'j';
  328. else
  329. *p++ = '-';
  330. if (flags & _WSNF_SEXP)
  331. *p++ = 's';
  332. else
  333. *p++ = '-';
  334. if (flags & _WSNF_DELIM)
  335. *p++ = 'd';
  336. else
  337. *p++ = '-';
  338. *p = 0;
  339. return retbuf;
  340. }
  341. static const char *
  342. wsnode_ptr (struct wordsplit *wsp, struct wordsplit_node *p)
  343. {
  344. if (p->flags & _WSNF_NULL)
  345. return "";
  346. else if (p->flags & _WSNF_WORD)
  347. return p->v.word;
  348. else
  349. return wsp->ws_input + p->v.segm.beg;
  350. }
  351. static size_t
  352. wsnode_len (struct wordsplit_node *p)
  353. {
  354. if (p->flags & _WSNF_NULL)
  355. return 0;
  356. else if (p->flags & _WSNF_WORD)
  357. return strlen (p->v.word);
  358. else
  359. return p->v.segm.end - p->v.segm.beg;
  360. }
  361. static int
  362. wsnode_new (struct wordsplit *wsp, struct wordsplit_node **pnode)
  363. {
  364. struct wordsplit_node *node = calloc (1, sizeof (*node));
  365. if (!node)
  366. return _wsplt_nomem (wsp);
  367. *pnode = node;
  368. return 0;
  369. }
  370. static void
  371. wsnode_free (struct wordsplit_node *p)
  372. {
  373. if (p->flags & _WSNF_WORD)
  374. free (p->v.word);
  375. free (p);
  376. }
  377. static void
  378. wsnode_append (struct wordsplit *wsp, struct wordsplit_node *node)
  379. {
  380. node->next = NULL;
  381. node->prev = wsp->ws_tail;
  382. if (wsp->ws_tail)
  383. wsp->ws_tail->next = node;
  384. else
  385. wsp->ws_head = node;
  386. wsp->ws_tail = node;
  387. }
  388. static void
  389. wsnode_remove (struct wordsplit *wsp, struct wordsplit_node *node)
  390. {
  391. struct wordsplit_node *p;
  392. p = node->prev;
  393. if (p)
  394. {
  395. p->next = node->next;
  396. if (!node->next)
  397. p->flags &= ~_WSNF_JOIN;
  398. }
  399. else
  400. wsp->ws_head = node->next;
  401. p = node->next;
  402. if (p)
  403. p->prev = node->prev;
  404. else
  405. wsp->ws_tail = node->prev;
  406. node->next = node->prev = NULL;
  407. }
  408. static struct wordsplit_node *
  409. wsnode_tail (struct wordsplit_node *p)
  410. {
  411. while (p && p->next)
  412. p = p->next;
  413. return p;
  414. }
  415. static void
  416. wsnode_insert (struct wordsplit *wsp, struct wordsplit_node *node,
  417. struct wordsplit_node *anchor, int before)
  418. {
  419. if (!wsp->ws_head)
  420. {
  421. node->next = node->prev = NULL;
  422. wsp->ws_head = wsp->ws_tail = node;
  423. }
  424. else if (before)
  425. {
  426. if (anchor->prev)
  427. wsnode_insert (wsp, node, anchor->prev, 0);
  428. else
  429. {
  430. struct wordsplit_node *tail = wsnode_tail (node);
  431. node->prev = NULL;
  432. tail->next = anchor;
  433. anchor->prev = tail;
  434. wsp->ws_head = node;
  435. }
  436. }
  437. else
  438. {
  439. struct wordsplit_node *p;
  440. struct wordsplit_node *tail = wsnode_tail (node);
  441. p = anchor->next;
  442. if (p)
  443. p->prev = tail;
  444. else
  445. wsp->ws_tail = tail;
  446. tail->next = p;
  447. node->prev = anchor;
  448. anchor->next = node;
  449. }
  450. }
  451. static int
  452. wordsplit_add_segm (struct wordsplit *wsp, size_t beg, size_t end, int flg)
  453. {
  454. struct wordsplit_node *node;
  455. int rc;
  456. if (end == beg && !(flg & _WSNF_EMPTYOK))
  457. return 0;
  458. rc = wsnode_new (wsp, &node);
  459. if (rc)
  460. return rc;
  461. node->flags = flg & ~(_WSNF_WORD | _WSNF_EMPTYOK);
  462. node->v.segm.beg = beg;
  463. node->v.segm.end = end;
  464. wsnode_append (wsp, node);
  465. return 0;
  466. }
  467. static void
  468. wordsplit_free_nodes (struct wordsplit *wsp)
  469. {
  470. struct wordsplit_node *p;
  471. for (p = wsp->ws_head; p;)
  472. {
  473. struct wordsplit_node *next = p->next;
  474. wsnode_free (p);
  475. p = next;
  476. }
  477. wsp->ws_head = wsp->ws_tail = NULL;
  478. }
  479. static void
  480. wordsplit_dump_nodes (struct wordsplit *wsp)
  481. {
  482. struct wordsplit_node *p;
  483. int n = 0;
  484. for (p = wsp->ws_head, n = 0; p; p = p->next, n++)
  485. {
  486. if (p->flags & _WSNF_WORD)
  487. wsp->ws_debug ("(%02d) %4d: %p: %#04x (%s):%s;",
  488. wsp->ws_lvl,
  489. n, p, p->flags, wsnode_flagstr (p->flags), p->v.word);
  490. else
  491. wsp->ws_debug ("(%02d) %4d: %p: %#04x (%s):%.*s;",
  492. wsp->ws_lvl,
  493. n, p, p->flags, wsnode_flagstr (p->flags),
  494. (int) (p->v.segm.end - p->v.segm.beg),
  495. wsp->ws_input + p->v.segm.beg);
  496. }
  497. }
  498. static int
  499. coalesce_segment (struct wordsplit *wsp, struct wordsplit_node *node)
  500. {
  501. struct wordsplit_node *p, *end;
  502. size_t len = 0;
  503. char *buf, *cur;
  504. int stop;
  505. if (!(node->flags & _WSNF_JOIN))
  506. return 0;
  507. for (p = node; p && (p->flags & _WSNF_JOIN); p = p->next)
  508. {
  509. len += wsnode_len (p);
  510. }
  511. if (p)
  512. len += wsnode_len (p);
  513. end = p;
  514. buf = malloc (len + 1);
  515. if (!buf)
  516. return _wsplt_nomem (wsp);
  517. cur = buf;
  518. p = node;
  519. for (stop = 0; !stop;)
  520. {
  521. struct wordsplit_node *next = p->next;
  522. const char *str = wsnode_ptr (wsp, p);
  523. size_t slen = wsnode_len (p);
  524. memcpy (cur, str, slen);
  525. cur += slen;
  526. if (p != node)
  527. {
  528. node->flags |= p->flags & _WSNF_QUOTE;
  529. wsnode_remove (wsp, p);
  530. stop = p == end;
  531. wsnode_free (p);
  532. }
  533. p = next;
  534. }
  535. *cur = 0;
  536. node->flags &= ~_WSNF_JOIN;
  537. if (node->flags & _WSNF_WORD)
  538. free (node->v.word);
  539. else
  540. node->flags |= _WSNF_WORD;
  541. node->v.word = buf;
  542. return 0;
  543. }
  544. static void wordsplit_string_unquote_copy (struct wordsplit *ws, int inquote,
  545. char *dst, const char *src,
  546. size_t n);
  547. static int
  548. wsnode_quoteremoval (struct wordsplit *wsp)
  549. {
  550. struct wordsplit_node *p;
  551. for (p = wsp->ws_head; p; p = p->next)
  552. {
  553. const char *str = wsnode_ptr (wsp, p);
  554. size_t slen = wsnode_len (p);
  555. int unquote;
  556. if (wsp->ws_flags & WRDSF_QUOTE)
  557. unquote = !(p->flags & _WSNF_NOEXPAND);
  558. else
  559. unquote = 0;
  560. if (unquote)
  561. {
  562. if (!(p->flags & _WSNF_WORD))
  563. {
  564. char *newstr = malloc (slen + 1);
  565. if (!newstr)
  566. return _wsplt_nomem (wsp);
  567. memcpy (newstr, str, slen);
  568. newstr[slen] = 0;
  569. p->v.word = newstr;
  570. p->flags |= _WSNF_WORD;
  571. }
  572. wordsplit_string_unquote_copy (wsp, p->flags & _WSNF_QUOTE,
  573. p->v.word, str, slen);
  574. }
  575. }
  576. return 0;
  577. }
  578. static int
  579. wsnode_coalesce (struct wordsplit *wsp)
  580. {
  581. struct wordsplit_node *p;
  582. for (p = wsp->ws_head; p; p = p->next)
  583. {
  584. if (p->flags & _WSNF_JOIN)
  585. if (coalesce_segment (wsp, p))
  586. return 1;
  587. }
  588. return 0;
  589. }
  590. static int
  591. wsnode_tail_coalesce (struct wordsplit *wsp, struct wordsplit_node *p)
  592. {
  593. if (p->next)
  594. {
  595. struct wordsplit_node *np = p;
  596. while (np && np->next)
  597. {
  598. np->flags |= _WSNF_JOIN;
  599. np = np->next;
  600. }
  601. if (coalesce_segment (wsp, p))
  602. return 1;
  603. }
  604. return 0;
  605. }
  606. static size_t skip_delim (struct wordsplit *wsp);
  607. static int
  608. wordsplit_finish (struct wordsplit *wsp)
  609. {
  610. struct wordsplit_node *p;
  611. size_t n;
  612. int delim;
  613. /* Postprocess delimiters. It would be rather simple, if it weren't for
  614. the incremental operation.
  615. Nodes of type _WSNF_DELIM get inserted to the node list if either
  616. WRDSF_RETURN_DELIMS flag or WRDSO_MAXWORDS option is set.
  617. The following cases should be distinguished:
  618. 1. If both WRDSF_SQUEEZE_DELIMS and WRDSF_RETURN_DELIMS are set, compress
  619. any runs of similar delimiter nodes to a single node. The nodes are
  620. 'similar' if they point to the same delimiter character.
  621. If WRDSO_MAXWORDS option is set, stop compressing when
  622. ws_wordi + 1 == ws_maxwords, and coalesce the rest of nodes into
  623. a single last node.
  624. 2. If WRDSO_MAXWORDS option is set, but WRDSF_RETURN_DELIMS is not,
  625. remove any delimiter nodes. Stop operation when
  626. ws_wordi + 1 == ws_maxwords, and coalesce the rest of nodes into
  627. a single last node.
  628. 3. If incremental operation is in progress, restart the loop any time
  629. a delimiter node is about to be returned, unless WRDSF_RETURN_DELIMS
  630. is set.
  631. */
  632. again:
  633. delim = 0; /* Delimiter being processed (if any) */
  634. n = 0; /* Number of words processed so far */
  635. p = wsp->ws_head; /* Current node */
  636. while (p)
  637. {
  638. struct wordsplit_node *next = p->next;
  639. if (p->flags & _WSNF_DELIM)
  640. {
  641. if (wsp->ws_flags & WRDSF_RETURN_DELIMS)
  642. {
  643. if (wsp->ws_flags & WRDSF_SQUEEZE_DELIMS)
  644. {
  645. char const *s = wsnode_ptr (wsp, p);
  646. if (delim)
  647. {
  648. if (delim == *s)
  649. {
  650. wsnode_remove (wsp, p);
  651. p = next;
  652. continue;
  653. }
  654. else
  655. {
  656. delim = 0;
  657. n++; /* Count this node; it will be returned */
  658. }
  659. }
  660. else
  661. {
  662. delim = *s;
  663. p = next;
  664. continue;
  665. }
  666. }
  667. }
  668. else if (wsp->ws_options & WRDSO_MAXWORDS)
  669. {
  670. wsnode_remove (wsp, p);
  671. p = next;
  672. continue;
  673. }
  674. }
  675. else
  676. {
  677. if (delim)
  678. {
  679. /* Last node was a delimiter or a compressed run of delimiters;
  680. Count it, and clear the delimiter marker */
  681. n++;
  682. delim = 0;
  683. }
  684. if (wsp->ws_options & WRDSO_MAXWORDS)
  685. {
  686. if (wsp->ws_wordi + n + 1 == wsp->ws_maxwords)
  687. break;
  688. }
  689. }
  690. n++;
  691. if (wsp->ws_flags & WRDSF_INCREMENTAL)
  692. p = NULL; /* Break the loop */
  693. else
  694. p = next;
  695. }
  696. if (p)
  697. {
  698. /* We're here if WRDSO_MAXWORDS is in effect and wsp->ws_maxwords
  699. words have already been collected. Reconstruct a single final
  700. node from the remaining nodes. */
  701. if (wsnode_tail_coalesce (wsp, p))
  702. return wsp->ws_errno;
  703. n++;
  704. }
  705. if (n == 0 && (wsp->ws_flags & WRDSF_INCREMENTAL))
  706. {
  707. /* The loop above have eliminated all nodes. Restart the
  708. processing, if there's any input left. */
  709. if (wsp->ws_endp < wsp->ws_len)
  710. {
  711. int rc;
  712. if (wsp->ws_flags & WRDSF_SHOWDBG)
  713. wsp->ws_debug (_("Restarting"));
  714. rc = wordsplit_process_list (wsp, skip_delim (wsp));
  715. if (rc)
  716. return rc;
  717. }
  718. else
  719. {
  720. wsp->ws_error = WRDSE_EOF;
  721. return WRDSE_EOF;
  722. }
  723. goto again;
  724. }
  725. if (alloc_space (wsp, n + 1))
  726. return wsp->ws_errno;
  727. while (wsp->ws_head)
  728. {
  729. const char *str = wsnode_ptr (wsp, wsp->ws_head);
  730. size_t slen = wsnode_len (wsp->ws_head);
  731. char *newstr = malloc (slen + 1);
  732. /* Assign newstr first, even if it is NULL. This way
  733. wordsplit_free will work even if we return
  734. nomem later. */
  735. wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = newstr;
  736. if (!newstr)
  737. return _wsplt_nomem (wsp);
  738. memcpy (newstr, str, slen);
  739. newstr[slen] = 0;
  740. wsnode_remove (wsp, wsp->ws_head);
  741. wsp->ws_wordc++;
  742. wsp->ws_wordi++;
  743. if (wsp->ws_flags & WRDSF_INCREMENTAL)
  744. break;
  745. }
  746. wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL;
  747. return 0;
  748. }
  749. int
  750. wordsplit_append (wordsplit_t *wsp, int argc, char **argv)
  751. {
  752. int rc;
  753. size_t i;
  754. rc = alloc_space (wsp, wsp->ws_wordc + argc + 1);
  755. if (rc)
  756. return rc;
  757. for (i = 0; i < argc; i++)
  758. {
  759. char *newstr = strdup (argv[i]);
  760. if (!newstr)
  761. {
  762. while (i > 0)
  763. {
  764. free (wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i - 1]);
  765. wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i - 1] = NULL;
  766. i--;
  767. }
  768. return _wsplt_nomem (wsp);
  769. }
  770. wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i] = newstr;
  771. }
  772. wsp->ws_wordc += i;
  773. wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL;
  774. return 0;
  775. }
  776. /* Variable expansion */
  777. static int
  778. node_split_prefix (struct wordsplit *wsp,
  779. struct wordsplit_node **ptail,
  780. struct wordsplit_node *node,
  781. size_t beg, size_t len, int flg)
  782. {
  783. struct wordsplit_node *newnode;
  784. if (len == 0)
  785. return 0;
  786. if (wsnode_new (wsp, &newnode))
  787. return 1;
  788. wsnode_insert (wsp, newnode, *ptail, 0);
  789. if (node->flags & _WSNF_WORD)
  790. {
  791. const char *str = wsnode_ptr (wsp, node);
  792. char *newstr = malloc (len + 1);
  793. if (!newstr)
  794. return _wsplt_nomem (wsp);
  795. memcpy (newstr, str + beg, len);
  796. newstr[len] = 0;
  797. newnode->flags = _WSNF_WORD;
  798. newnode->v.word = newstr;
  799. }
  800. else
  801. {
  802. newnode->v.segm.beg = node->v.segm.beg + beg;
  803. newnode->v.segm.end = newnode->v.segm.beg + len;
  804. }
  805. newnode->flags |= flg;
  806. *ptail = newnode;
  807. return 0;
  808. }
  809. static int
  810. find_closing_paren (const char *str, size_t i, size_t len, size_t *poff,
  811. char const *paren)
  812. {
  813. enum { st_init, st_squote, st_dquote } state = st_init;
  814. size_t level = 1;
  815. for (; i < len; i++)
  816. {
  817. switch (state)
  818. {
  819. case st_init:
  820. switch (str[i])
  821. {
  822. default:
  823. if (str[i] == paren[0])
  824. {
  825. level++;
  826. break;
  827. }
  828. else if (str[i] == paren[1])
  829. {
  830. if (--level == 0)
  831. {
  832. *poff = i;
  833. return 0;
  834. }
  835. break;
  836. }
  837. break;
  838. case '"':
  839. state = st_dquote;
  840. break;
  841. case '\'':
  842. state = st_squote;
  843. break;
  844. }
  845. break;
  846. case st_squote:
  847. if (str[i] == '\'')
  848. state = st_init;
  849. break;
  850. case st_dquote:
  851. if (str[i] == '\\')
  852. i++;
  853. else if (str[i] == '"')
  854. state = st_init;
  855. break;
  856. }
  857. }
  858. return 1;
  859. }
  860. static int
  861. wordsplit_find_env (struct wordsplit *wsp, const char *name, size_t len,
  862. char const **ret)
  863. {
  864. size_t i;
  865. if (!(wsp->ws_flags & WRDSF_ENV))
  866. return WRDSE_UNDEF;
  867. if (wsp->ws_flags & WRDSF_ENV_KV)
  868. {
  869. /* A key-value pair environment */
  870. for (i = 0; wsp->ws_env[i]; i++)
  871. {
  872. size_t elen = strlen (wsp->ws_env[i]);
  873. if (elen == len && memcmp (wsp->ws_env[i], name, elen) == 0)
  874. {
  875. *ret = wsp->ws_env[i + 1];
  876. return WRDSE_OK;
  877. }
  878. /* Skip the value. Break the loop if it is NULL. */
  879. i++;
  880. if (wsp->ws_env[i] == NULL)
  881. break;
  882. }
  883. }
  884. else if (wsp->ws_env)
  885. {
  886. /* Usual (A=B) environment. */
  887. for (i = 0; wsp->ws_env[i]; i++)
  888. {
  889. size_t j;
  890. const char *var = wsp->ws_env[i];
  891. for (j = 0; j < len; j++)
  892. if (name[j] != var[j])
  893. break;
  894. if (j == len && var[j] == '=')
  895. {
  896. *ret = var + j + 1;
  897. return WRDSE_OK;
  898. }
  899. }
  900. }
  901. return WRDSE_UNDEF;
  902. }
  903. static int
  904. wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen,
  905. char *value)
  906. {
  907. int n = (wsp->ws_flags & WRDSF_ENV_KV) ? 2 : 1;
  908. char *v;
  909. if (wsp->ws_envidx + n >= wsp->ws_envsiz)
  910. {
  911. size_t sz;
  912. char **newenv;
  913. if (!wsp->ws_envbuf)
  914. {
  915. if (wsp->ws_flags & WRDSF_ENV)
  916. {
  917. size_t i = 0, j;
  918. if (wsp->ws_env)
  919. {
  920. for (; wsp->ws_env[i]; i++)
  921. ;
  922. }
  923. sz = i + n + 1;
  924. newenv = calloc (sz, sizeof(newenv[0]));
  925. if (!newenv)
  926. return _wsplt_nomem (wsp);
  927. for (j = 0; j < i; j++)
  928. {
  929. newenv[j] = strdup (wsp->ws_env[j]);
  930. if (!newenv[j])
  931. {
  932. for (; j > 1; j--)
  933. free (newenv[j-1]);
  934. free (newenv[j-1]);
  935. return _wsplt_nomem (wsp);
  936. }
  937. }
  938. newenv[j] = NULL;
  939. wsp->ws_envbuf = newenv;
  940. wsp->ws_envidx = i;
  941. wsp->ws_envsiz = sz;
  942. wsp->ws_env = (const char**) wsp->ws_envbuf;
  943. }
  944. else
  945. {
  946. newenv = calloc (WORDSPLIT_ENV_INIT, sizeof(newenv[0]));
  947. if (!newenv)
  948. return _wsplt_nomem (wsp);
  949. wsp->ws_envbuf = newenv;
  950. wsp->ws_envidx = 0;
  951. wsp->ws_envsiz = WORDSPLIT_ENV_INIT;
  952. wsp->ws_env = (const char**) wsp->ws_envbuf;
  953. wsp->ws_flags |= WRDSF_ENV;
  954. }
  955. }
  956. else
  957. {
  958. wsp->ws_envsiz *= 2;
  959. newenv = realloc (wsp->ws_envbuf,
  960. wsp->ws_envsiz * sizeof (wsp->ws_envbuf[0]));
  961. if (!newenv)
  962. return _wsplt_nomem (wsp);
  963. wsp->ws_envbuf = newenv;
  964. wsp->ws_env = (const char**) wsp->ws_envbuf;
  965. }
  966. }
  967. if (wsp->ws_flags & WRDSF_ENV_KV)
  968. {
  969. /* A key-value pair environment */
  970. char *p = malloc (namelen + 1);
  971. if (!p)
  972. return _wsplt_nomem (wsp);
  973. memcpy (p, name, namelen);
  974. p[namelen] = 0;
  975. v = strdup (value);
  976. if (!v)
  977. {
  978. free (p);
  979. return _wsplt_nomem (wsp);
  980. }
  981. wsp->ws_env[wsp->ws_envidx++] = p;
  982. wsp->ws_env[wsp->ws_envidx++] = v;
  983. }
  984. else
  985. {
  986. v = malloc (namelen + strlen(value) + 2);
  987. if (!v)
  988. return _wsplt_nomem (wsp);
  989. memcpy (v, name, namelen);
  990. v[namelen++] = '=';
  991. strcpy(v + namelen, value);
  992. wsp->ws_env[wsp->ws_envidx++] = v;
  993. }
  994. wsp->ws_env[wsp->ws_envidx++] = NULL;
  995. return WRDSE_OK;
  996. }
  997. static int
  998. expvar (struct wordsplit *wsp, const char *str, size_t len,
  999. struct wordsplit_node **ptail, const char **pend, int flg)
  1000. {
  1001. size_t i = 0;
  1002. const char *defstr = NULL;
  1003. char *value;
  1004. const char *vptr;
  1005. struct wordsplit_node *newnode;
  1006. const char *start = str - 1;
  1007. int rc;
  1008. struct wordsplit ws;
  1009. if (ISVARBEG (str[0]))
  1010. {
  1011. for (i = 1; i < len; i++)
  1012. if (!ISVARCHR (str[i]))
  1013. break;
  1014. *pend = str + i - 1;
  1015. }
  1016. else if (str[0] == '{')
  1017. {
  1018. str++;
  1019. len--;
  1020. for (i = 1; i < len; i++)
  1021. {
  1022. if (str[i] == ':')
  1023. {
  1024. size_t j;
  1025. defstr = str + i + 1;
  1026. if (find_closing_paren (str, i + 1, len, &j, "{}"))
  1027. return _wsplt_seterr (wsp, WRDSE_CBRACE);
  1028. *pend = str + j;
  1029. break;
  1030. }
  1031. else if (str[i] == '}')
  1032. {
  1033. defstr = NULL;
  1034. *pend = str + i;
  1035. break;
  1036. }
  1037. else if (strchr ("-+?=", str[i]))
  1038. {
  1039. size_t j;
  1040. defstr = str + i;
  1041. if (find_closing_paren (str, i, len, &j, "{}"))
  1042. return _wsplt_seterr (wsp, WRDSE_CBRACE);
  1043. *pend = str + j;
  1044. break;
  1045. }
  1046. }
  1047. if (i == len)
  1048. return _wsplt_seterr (wsp, WRDSE_CBRACE);
  1049. }
  1050. else
  1051. {
  1052. if (wsnode_new (wsp, &newnode))
  1053. return 1;
  1054. wsnode_insert (wsp, newnode, *ptail, 0);
  1055. *ptail = newnode;
  1056. newnode->flags = _WSNF_WORD | flg;
  1057. newnode->v.word = malloc (3);
  1058. if (!newnode->v.word)
  1059. return _wsplt_nomem (wsp);
  1060. newnode->v.word[0] = '$';
  1061. newnode->v.word[1] = str[0];
  1062. newnode->v.word[2] = 0;
  1063. *pend = str;
  1064. return 0;
  1065. }
  1066. /* Actually expand the variable */
  1067. /* str - start of the variable name
  1068. i - its length
  1069. defstr - default replacement str */
  1070. if (defstr && strchr("-+?=", defstr[0]) == 0)
  1071. {
  1072. rc = WRDSE_UNDEF;
  1073. defstr = NULL;
  1074. }
  1075. else
  1076. {
  1077. rc = wordsplit_find_env (wsp, str, i, &vptr);
  1078. if (rc == WRDSE_OK)
  1079. {
  1080. if (vptr)
  1081. {
  1082. value = strdup (vptr);
  1083. if (!value)
  1084. rc = WRDSE_NOSPACE;
  1085. }
  1086. else
  1087. rc = WRDSE_UNDEF;
  1088. }
  1089. else if (wsp->ws_flags & WRDSF_GETVAR)
  1090. rc = wsp->ws_getvar (&value, str, i, wsp->ws_closure);
  1091. else
  1092. rc = WRDSE_UNDEF;
  1093. if (rc == WRDSE_OK
  1094. && (!value || value[0] == 0)
  1095. && defstr && defstr[-1] == ':')
  1096. {
  1097. free (value);
  1098. rc = WRDSE_UNDEF;
  1099. }
  1100. }
  1101. switch (rc)
  1102. {
  1103. case WRDSE_OK:
  1104. if (defstr && *defstr == '+')
  1105. {
  1106. size_t size = *pend - ++defstr;
  1107. rc = _wsplt_subsplit (wsp, &ws, defstr, size,
  1108. WRDSF_NOSPLIT | WRDSF_WS | WRDSF_QUOTE |
  1109. (wsp->ws_flags &
  1110. (WRDSF_NOVAR | WRDSF_NOCMD)), 1);
  1111. if (rc)
  1112. return rc;
  1113. free (value);
  1114. value = ws.ws_wordv[0];
  1115. ws.ws_wordv[0] = NULL;
  1116. wordsplit_free (&ws);
  1117. }
  1118. break;
  1119. case WRDSE_UNDEF:
  1120. if (defstr)
  1121. {
  1122. size_t size;
  1123. if (*defstr == '-' || *defstr == '=')
  1124. {
  1125. size = *pend - ++defstr;
  1126. rc = _wsplt_subsplit (wsp, &ws, defstr, size,
  1127. WRDSF_NOSPLIT | WRDSF_WS | WRDSF_QUOTE |
  1128. (wsp->ws_flags &
  1129. (WRDSF_NOVAR | WRDSF_NOCMD)),
  1130. 1);
  1131. if (rc)
  1132. return rc;
  1133. value = ws.ws_wordv[0];
  1134. ws.ws_wordv[0] = NULL;
  1135. wordsplit_free (&ws);
  1136. if (defstr[-1] == '=')
  1137. wsplt_assign_var (wsp, str, i, value);
  1138. }
  1139. else
  1140. {
  1141. if (*defstr == '?')
  1142. {
  1143. size = *pend - ++defstr;
  1144. if (size == 0)
  1145. wsp->ws_error (_("%.*s: variable null or not set"),
  1146. (int) i, str);
  1147. else
  1148. {
  1149. rc = _wsplt_subsplit (wsp, &ws, defstr, size,
  1150. WRDSF_NOSPLIT | WRDSF_WS |
  1151. WRDSF_QUOTE |
  1152. (wsp->ws_flags &
  1153. (WRDSF_NOVAR | WRDSF_NOCMD)),
  1154. 1);
  1155. if (rc == 0)
  1156. wsp->ws_error ("%.*s: %s",
  1157. (int) i, str, ws.ws_wordv[0]);
  1158. else
  1159. wsp->ws_error ("%.*s: %.*s",
  1160. (int) i, str, (int) size, defstr);
  1161. wordsplit_free (&ws);
  1162. }
  1163. }
  1164. value = NULL;
  1165. }
  1166. }
  1167. else if (wsp->ws_flags & WRDSF_UNDEF)
  1168. {
  1169. _wsplt_seterr (wsp, WRDSE_UNDEF);
  1170. return 1;
  1171. }
  1172. else
  1173. {
  1174. if (wsp->ws_flags & WRDSF_WARNUNDEF)
  1175. wsp->ws_error (_("warning: undefined variable `%.*s'"),
  1176. (int) i, str);
  1177. if (wsp->ws_flags & WRDSF_KEEPUNDEF)
  1178. value = NULL;
  1179. else
  1180. {
  1181. value = strdup ("");
  1182. if (!value)
  1183. return _wsplt_nomem (wsp);
  1184. }
  1185. }
  1186. break;
  1187. case WRDSE_NOSPACE:
  1188. return _wsplt_nomem (wsp);
  1189. case WRDSE_USERERR:
  1190. if (wsp->ws_errno == WRDSE_USERERR)
  1191. free (wsp->ws_usererr);
  1192. wsp->ws_usererr = value;
  1193. /* fall through */
  1194. default:
  1195. _wsplt_seterr (wsp, rc);
  1196. return 1;
  1197. }
  1198. if (value)
  1199. {
  1200. if (flg & _WSNF_QUOTE)
  1201. {
  1202. if (wsnode_new (wsp, &newnode))
  1203. return 1;
  1204. wsnode_insert (wsp, newnode, *ptail, 0);
  1205. *ptail = newnode;
  1206. newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
  1207. newnode->v.word = value;
  1208. }
  1209. else if (*value == 0)
  1210. {
  1211. free (value);
  1212. /* Empty string is a special case */
  1213. if (wsnode_new (wsp, &newnode))
  1214. return 1;
  1215. wsnode_insert (wsp, newnode, *ptail, 0);
  1216. *ptail = newnode;
  1217. newnode->flags = _WSNF_NULL;
  1218. }
  1219. else
  1220. {
  1221. struct wordsplit ws;
  1222. int rc;
  1223. rc = _wsplt_subsplit (wsp, &ws, value, strlen (value),
  1224. WRDSF_NOVAR | WRDSF_NOCMD |
  1225. WRDSF_QUOTE
  1226. | (WSP_RETURN_DELIMS (wsp) ? WRDSF_RETURN_DELIMS : 0) ,
  1227. 0);
  1228. free (value);
  1229. if (rc)
  1230. {
  1231. _wsplt_seterr_sub (wsp, &ws);
  1232. wordsplit_free (&ws);
  1233. return 1;
  1234. }
  1235. wsnode_insert (wsp, ws.ws_head, *ptail, 0);
  1236. *ptail = ws.ws_tail;
  1237. ws.ws_head = ws.ws_tail = NULL;
  1238. wordsplit_free (&ws);
  1239. }
  1240. }
  1241. else if (wsp->ws_flags & WRDSF_KEEPUNDEF)
  1242. {
  1243. size_t size = *pend - start + 1;
  1244. if (wsnode_new (wsp, &newnode))
  1245. return 1;
  1246. wsnode_insert (wsp, newnode, *ptail, 0);
  1247. *ptail = newnode;
  1248. newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
  1249. newnode->v.word = malloc (size + 1);
  1250. if (!newnode->v.word)
  1251. return _wsplt_nomem (wsp);
  1252. memcpy (newnode->v.word, start, size);
  1253. newnode->v.word[size] = 0;
  1254. }
  1255. else
  1256. {
  1257. if (wsnode_new (wsp, &newnode))
  1258. return 1;
  1259. wsnode_insert (wsp, newnode, *ptail, 0);
  1260. *ptail = newnode;
  1261. newnode->flags = _WSNF_NULL;
  1262. }
  1263. return 0;
  1264. }
  1265. static int
  1266. begin_var_p (int c)
  1267. {
  1268. return c == '{' || ISVARBEG (c);
  1269. }
  1270. static int
  1271. node_expand (struct wordsplit *wsp, struct wordsplit_node *node,
  1272. int (*beg_p) (int),
  1273. int (*ws_exp_fn) (struct wordsplit *wsp,
  1274. const char *str, size_t len,
  1275. struct wordsplit_node **ptail,
  1276. const char **pend,
  1277. int flg))
  1278. {
  1279. const char *str = wsnode_ptr (wsp, node);
  1280. size_t slen = wsnode_len (node);
  1281. const char *end = str + slen;
  1282. const char *p;
  1283. size_t off = 0;
  1284. struct wordsplit_node *tail = node;
  1285. for (p = str; p < end; p++)
  1286. {
  1287. if (*p == '\\')
  1288. {
  1289. p++;
  1290. continue;
  1291. }
  1292. if (*p == '$' && beg_p (p[1]))
  1293. {
  1294. size_t n = p - str;
  1295. if (tail != node)
  1296. tail->flags |= _WSNF_JOIN;
  1297. if (node_split_prefix (wsp, &tail, node, off, n, _WSNF_JOIN))
  1298. return 1;
  1299. p++;
  1300. if (ws_exp_fn (wsp, p, slen - n, &tail, &p,
  1301. node->flags & (_WSNF_JOIN | _WSNF_QUOTE)))
  1302. return 1;
  1303. off += p - str + 1;
  1304. str = p + 1;
  1305. }
  1306. }
  1307. if (p > str)
  1308. {
  1309. if (tail != node)
  1310. tail->flags |= _WSNF_JOIN;
  1311. if (node_split_prefix (wsp, &tail, node, off, p - str,
  1312. node->flags & (_WSNF_JOIN|_WSNF_QUOTE)))
  1313. return 1;
  1314. }
  1315. if (tail != node)
  1316. {
  1317. wsnode_remove (wsp, node);
  1318. wsnode_free (node);
  1319. }
  1320. return 0;
  1321. }
  1322. /* Remove NULL nodes from the list */
  1323. static void
  1324. wsnode_nullelim (struct wordsplit *wsp)
  1325. {
  1326. struct wordsplit_node *p;
  1327. for (p = wsp->ws_head; p;)
  1328. {
  1329. struct wordsplit_node *next = p->next;
  1330. if (p->flags & _WSNF_DELIM && p->prev)
  1331. p->prev->flags &= ~_WSNF_JOIN;
  1332. if (p->flags & _WSNF_NULL)
  1333. {
  1334. wsnode_remove (wsp, p);
  1335. wsnode_free (p);
  1336. }
  1337. p = next;
  1338. }
  1339. }
  1340. static int
  1341. wordsplit_varexp (struct wordsplit *wsp)
  1342. {
  1343. struct wordsplit_node *p;
  1344. for (p = wsp->ws_head; p;)
  1345. {
  1346. struct wordsplit_node *next = p->next;
  1347. if (!(p->flags & (_WSNF_NOEXPAND|_WSNF_DELIM)))
  1348. if (node_expand (wsp, p, begin_var_p, expvar))
  1349. return 1;
  1350. p = next;
  1351. }
  1352. wsnode_nullelim (wsp);
  1353. return 0;
  1354. }
  1355. static int
  1356. begin_cmd_p (int c)
  1357. {
  1358. return c == '(';
  1359. }
  1360. static int
  1361. expcmd (struct wordsplit *wsp, const char *str, size_t len,
  1362. struct wordsplit_node **ptail, const char **pend, int flg)
  1363. {
  1364. int rc;
  1365. size_t j;
  1366. char *value;
  1367. struct wordsplit_node *newnode;
  1368. str++;
  1369. len--;
  1370. if (find_closing_paren (str, 0, len, &j, "()"))
  1371. {
  1372. _wsplt_seterr (wsp, WRDSE_PAREN);
  1373. return 1;
  1374. }
  1375. *pend = str + j;
  1376. if (wsp->ws_options & WRDSO_ARGV)
  1377. {
  1378. struct wordsplit ws;
  1379. rc = _wsplt_subsplit (wsp, &ws, str, j, WRDSF_WS | WRDSF_QUOTE, 1);
  1380. if (rc)
  1381. {
  1382. _wsplt_seterr_sub (wsp, &ws);
  1383. wordsplit_free (&ws);
  1384. return 1;
  1385. }
  1386. rc = wsp->ws_command (&value, str, j, ws.ws_wordv, wsp->ws_closure);
  1387. wordsplit_free (&ws);
  1388. }
  1389. else
  1390. rc = wsp->ws_command (&value, str, j, NULL, wsp->ws_closure);
  1391. if (rc == WRDSE_NOSPACE)
  1392. return _wsplt_nomem (wsp);
  1393. else if (rc)
  1394. {
  1395. if (rc == WRDSE_USERERR)
  1396. {
  1397. if (wsp->ws_errno == WRDSE_USERERR)
  1398. free (wsp->ws_usererr);
  1399. wsp->ws_usererr = value;
  1400. }
  1401. _wsplt_seterr (wsp, rc);
  1402. return 1;
  1403. }
  1404. if (value)
  1405. {
  1406. if (flg & _WSNF_QUOTE)
  1407. {
  1408. if (wsnode_new (wsp, &newnode))
  1409. return 1;
  1410. wsnode_insert (wsp, newnode, *ptail, 0);
  1411. *ptail = newnode;
  1412. newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg;
  1413. newnode->v.word = value;
  1414. }
  1415. else if (*value == 0)
  1416. {
  1417. free (value);
  1418. /* Empty string is a special case */
  1419. if (wsnode_new (wsp, &newnode))
  1420. return 1;
  1421. wsnode_insert (wsp, newnode, *ptail, 0);
  1422. *ptail = newnode;
  1423. newnode->flags = _WSNF_NULL;
  1424. }
  1425. else
  1426. {
  1427. struct wordsplit ws;
  1428. int rc;
  1429. rc = _wsplt_subsplit (wsp, &ws, value, strlen (value),
  1430. WRDSF_NOVAR | WRDSF_NOCMD
  1431. | WRDSF_WS | WRDSF_QUOTE
  1432. | (WSP_RETURN_DELIMS (wsp) ? WRDSF_RETURN_DELIMS : 0),
  1433. 0);
  1434. free (value);
  1435. if (rc)
  1436. {
  1437. _wsplt_seterr_sub (wsp, &ws);
  1438. wordsplit_free (&ws);
  1439. return 1;
  1440. }
  1441. wsnode_insert (wsp, ws.ws_head, *ptail, 0);
  1442. *ptail = ws.ws_tail;
  1443. ws.ws_head = ws.ws_tail = NULL;
  1444. wordsplit_free (&ws);
  1445. }
  1446. }
  1447. else
  1448. {
  1449. if (wsnode_new (wsp, &newnode))
  1450. return 1;
  1451. wsnode_insert (wsp, newnode, *ptail, 0);
  1452. *ptail = newnode;
  1453. newnode->flags = _WSNF_NULL;
  1454. }
  1455. return 0;
  1456. }
  1457. static int
  1458. wordsplit_cmdexp (struct wordsplit *wsp)
  1459. {
  1460. struct wordsplit_node *p;
  1461. for (p = wsp->ws_head; p;)
  1462. {
  1463. struct wordsplit_node *next = p->next;
  1464. if (!(p->flags & _WSNF_NOEXPAND))
  1465. if (node_expand (wsp, p, begin_cmd_p, expcmd))
  1466. return 1;
  1467. p = next;
  1468. }
  1469. wsnode_nullelim (wsp);
  1470. return 0;
  1471. }
  1472. /* Strip off any leading and trailing whitespace. This function is called
  1473. right after the initial scanning, therefore it assumes that every
  1474. node in the list is a text reference node. */
  1475. static int
  1476. wordsplit_trimws (struct wordsplit *wsp)
  1477. {
  1478. struct wordsplit_node *p;
  1479. for (p = wsp->ws_head; p; p = p->next)
  1480. {
  1481. size_t n;
  1482. if (!(p->flags & _WSNF_QUOTE))
  1483. {
  1484. /* Skip leading whitespace: */
  1485. for (n = p->v.segm.beg; n < p->v.segm.end && ISWS (wsp->ws_input[n]);
  1486. n++)
  1487. ;
  1488. p->v.segm.beg = n;
  1489. }
  1490. while (p->next && (p->flags & _WSNF_JOIN))
  1491. p = p->next;
  1492. if (p->flags & _WSNF_QUOTE)
  1493. continue;
  1494. /* Trim trailing whitespace */
  1495. for (n = p->v.segm.end;
  1496. n > p->v.segm.beg && ISWS (wsp->ws_input[n - 1]); n--);
  1497. p->v.segm.end = n;
  1498. if (p->v.segm.beg == p->v.segm.end)
  1499. p->flags |= _WSNF_NULL;
  1500. }
  1501. wsnode_nullelim (wsp);
  1502. return 0;
  1503. }
  1504. static int
  1505. wordsplit_tildexpand (struct wordsplit *wsp)
  1506. {
  1507. struct wordsplit_node *p;
  1508. char *uname = NULL;
  1509. size_t usize = 0;
  1510. for (p = wsp->ws_head; p; p = p->next)
  1511. {
  1512. const char *str;
  1513. if (p->flags & _WSNF_QUOTE)
  1514. continue;
  1515. str = wsnode_ptr (wsp, p);
  1516. if (str[0] == '~')
  1517. {
  1518. size_t i, size, dlen;
  1519. size_t slen = wsnode_len (p);
  1520. struct passwd *pw;
  1521. char *newstr;
  1522. for (i = 1; i < slen && str[i] != '/'; i++)
  1523. ;
  1524. if (i == slen)
  1525. continue;
  1526. if (i > 1)
  1527. {
  1528. if (i > usize)
  1529. {
  1530. char *p = realloc (uname, i);
  1531. if (!p)
  1532. {
  1533. free (uname);
  1534. return _wsplt_nomem (wsp);
  1535. }
  1536. uname = p;
  1537. usize = i;
  1538. }
  1539. --i;
  1540. memcpy (uname, str + 1, i);
  1541. uname[i] = 0;
  1542. pw = getpwnam (uname);
  1543. }
  1544. else
  1545. pw = getpwuid (getuid ());
  1546. if (!pw)
  1547. continue;
  1548. dlen = strlen (pw->pw_dir);
  1549. size = slen - i + dlen;
  1550. newstr = malloc (size);
  1551. if (!newstr)
  1552. {
  1553. free (uname);
  1554. return _wsplt_nomem (wsp);
  1555. }
  1556. --size;
  1557. memcpy (newstr, pw->pw_dir, dlen);
  1558. memcpy (newstr + dlen, str + i + 1, slen - i - 1);
  1559. newstr[size] = 0;
  1560. if (p->flags & _WSNF_WORD)
  1561. free (p->v.word);
  1562. p->v.word = newstr;
  1563. p->flags |= _WSNF_WORD;
  1564. }
  1565. }
  1566. free (uname);
  1567. return 0;
  1568. }
  1569. static int
  1570. isglob (const char *s, int l)
  1571. {
  1572. while (l--)
  1573. {
  1574. if (strchr ("*?[", *s++))
  1575. return 1;
  1576. }
  1577. return 0;
  1578. }
  1579. static int
  1580. wordsplit_pathexpand (struct wordsplit *wsp)
  1581. {
  1582. struct wordsplit_node *p, *next;
  1583. char *pattern = NULL;
  1584. size_t patsize = 0;
  1585. size_t slen;
  1586. int flags = 0;
  1587. #ifdef GLOB_PERIOD
  1588. if (wsp->ws_options & WRDSO_DOTGLOB)
  1589. flags = GLOB_PERIOD;
  1590. #endif
  1591. for (p = wsp->ws_head; p; p = next)
  1592. {
  1593. const char *str;
  1594. next = p->next;
  1595. if (p->flags & _WSNF_QUOTE)
  1596. continue;
  1597. str = wsnode_ptr (wsp, p);
  1598. slen = wsnode_len (p);
  1599. if (isglob (str, slen))
  1600. {
  1601. int i;
  1602. glob_t g;
  1603. struct wordsplit_node *prev;
  1604. if (slen + 1 > patsize)
  1605. {
  1606. char *p = realloc (pattern, slen + 1);
  1607. if (!p)
  1608. return _wsplt_nomem (wsp);
  1609. pattern = p;
  1610. patsize = slen + 1;
  1611. }
  1612. memcpy (pattern, str, slen);
  1613. pattern[slen] = 0;
  1614. switch (glob (pattern, flags, NULL, &g))
  1615. {
  1616. case 0:
  1617. break;
  1618. case GLOB_NOSPACE:
  1619. free (pattern);
  1620. return _wsplt_nomem (wsp);
  1621. case GLOB_NOMATCH:
  1622. if (wsp->ws_options & WRDSO_NULLGLOB)
  1623. {
  1624. wsnode_remove (wsp, p);
  1625. wsnode_free (p);
  1626. }
  1627. else if (wsp->ws_options & WRDSO_FAILGLOB)
  1628. {
  1629. char buf[128];
  1630. if (wsp->ws_errno == WRDSE_USERERR)
  1631. free (wsp->ws_usererr);
  1632. snprintf (buf, sizeof (buf), _("no files match pattern %s"),
  1633. pattern);
  1634. free (pattern);
  1635. wsp->ws_usererr = strdup (buf);
  1636. if (!wsp->ws_usererr)
  1637. return _wsplt_nomem (wsp);
  1638. else
  1639. return _wsplt_seterr (wsp, WRDSE_USERERR);
  1640. }
  1641. continue;
  1642. default:
  1643. free (pattern);
  1644. return _wsplt_seterr (wsp, WRDSE_GLOBERR);
  1645. }
  1646. prev = p;
  1647. for (i = 0; i < g.gl_pathc; i++)
  1648. {
  1649. struct wordsplit_node *newnode;
  1650. char *newstr;
  1651. if (wsnode_new (wsp, &newnode))
  1652. return 1;
  1653. newstr = strdup (g.gl_pathv[i]);
  1654. if (!newstr)
  1655. return _wsplt_nomem (wsp);
  1656. newnode->v.word = newstr;
  1657. newnode->flags |= _WSNF_WORD|_WSNF_QUOTE;
  1658. wsnode_insert (wsp, newnode, prev, 0);
  1659. prev = newnode;
  1660. }
  1661. globfree (&g);
  1662. wsnode_remove (wsp, p);
  1663. wsnode_free (p);
  1664. }
  1665. }
  1666. free (pattern);
  1667. return 0;
  1668. }
  1669. static int
  1670. skip_sed_expr (const char *command, size_t i, size_t len)
  1671. {
  1672. int state;
  1673. do
  1674. {
  1675. int delim;
  1676. if (command[i] == ';')
  1677. i++;
  1678. if (!(command[i] == 's' && i + 3 < len && ISPUNCT (command[i + 1])))
  1679. break;
  1680. delim = command[++i];
  1681. state = 1;
  1682. for (i++; i < len; i++)
  1683. {
  1684. if (state == 3)
  1685. {
  1686. if (command[i] == delim || !ISALNUM (command[i]))
  1687. break;
  1688. }
  1689. else if (command[i] == '\\')
  1690. i++;
  1691. else if (command[i] == delim)
  1692. state++;
  1693. }
  1694. }
  1695. while (state == 3 && i < len && command[i] == ';');
  1696. return i;
  1697. }
  1698. /* wsp->ws_endp points to a delimiter character. If RETURN_DELIMS
  1699. is true, return its value, otherwise return the index past it. */
  1700. static inline size_t
  1701. skip_delim_internal (struct wordsplit *wsp, int return_delims)
  1702. {
  1703. return return_delims ? wsp->ws_endp : wsp->ws_endp + 1;
  1704. }
  1705. static inline size_t
  1706. skip_delim (struct wordsplit *wsp)
  1707. {
  1708. return skip_delim_internal (wsp, WSP_RETURN_DELIMS (wsp));
  1709. }
  1710. static inline size_t
  1711. skip_delim_real (struct wordsplit *wsp)
  1712. {
  1713. return skip_delim_internal (wsp, wsp->ws_flags & WRDSF_RETURN_DELIMS);
  1714. }
  1715. #define _WRDS_EOF 0
  1716. #define _WRDS_OK 1
  1717. #define _WRDS_ERR 2
  1718. static int
  1719. scan_qstring (struct wordsplit *wsp, size_t start, size_t *end)
  1720. {
  1721. size_t j;
  1722. const char *command = wsp->ws_input;
  1723. size_t len = wsp->ws_len;
  1724. char q = command[start];
  1725. for (j = start + 1; j < len && command[j] != q; j++)
  1726. if (q == '"' && command[j] == '\\')
  1727. j++;
  1728. if (j < len && command[j] == q)
  1729. {
  1730. int flags = _WSNF_QUOTE | _WSNF_EMPTYOK;
  1731. if (q == '\'')
  1732. flags |= _WSNF_NOEXPAND;
  1733. if (wordsplit_add_segm (wsp, start + 1, j, flags))
  1734. return _WRDS_ERR;
  1735. *end = j;
  1736. }
  1737. else
  1738. {
  1739. wsp->ws_endp = start;
  1740. _wsplt_seterr (wsp, WRDSE_QUOTE);
  1741. return _WRDS_ERR;
  1742. }
  1743. return 0;
  1744. }
  1745. static int
  1746. scan_word (struct wordsplit *wsp, size_t start, int consume_all)
  1747. {
  1748. size_t len = wsp->ws_len;
  1749. const char *command = wsp->ws_input;
  1750. const char *comment = wsp->ws_comment;
  1751. int join = 0;
  1752. int flags = 0;
  1753. struct wordsplit_node *np = wsp->ws_tail;
  1754. size_t i = start;
  1755. if (i >= len)
  1756. {
  1757. wsp->ws_errno = WRDSE_EOF;
  1758. return _WRDS_EOF;
  1759. }
  1760. start = i;
  1761. if (wsp->ws_flags & WRDSF_SED_EXPR
  1762. && command[i] == 's' && i + 3 < len && ISPUNCT (command[i + 1]))
  1763. {
  1764. flags = _WSNF_SEXP;
  1765. i = skip_sed_expr (command, i, len);
  1766. }
  1767. else if (consume_all || !ISDELIM (wsp, command[i]))
  1768. {
  1769. while (i < len)
  1770. {
  1771. if (comment && strchr (comment, command[i]) != NULL)
  1772. {
  1773. size_t j;
  1774. for (j = i + 1; j < len && command[j] != '\n'; j++)
  1775. ;
  1776. if (wordsplit_add_segm (wsp, start, i, 0))
  1777. return _WRDS_ERR;
  1778. wsp->ws_endp = j;
  1779. return _WRDS_OK;
  1780. }
  1781. if (wsp->ws_flags & WRDSF_QUOTE)
  1782. {
  1783. if (command[i] == '\\')
  1784. {
  1785. if (++i == len)
  1786. break;
  1787. i++;
  1788. continue;
  1789. }
  1790. if (((wsp->ws_flags & WRDSF_SQUOTE) && command[i] == '\'') ||
  1791. ((wsp->ws_flags & WRDSF_DQUOTE) && command[i] == '"'))
  1792. {
  1793. if (join && wsp->ws_tail)
  1794. wsp->ws_tail->flags |= _WSNF_JOIN;
  1795. if (wordsplit_add_segm (wsp, start, i, _WSNF_JOIN))
  1796. return _WRDS_ERR;
  1797. if (scan_qstring (wsp, i, &i))
  1798. return _WRDS_ERR;
  1799. start = i + 1;
  1800. join = 1;
  1801. }
  1802. }
  1803. if (command[i] == '$')
  1804. {
  1805. if (!(wsp->ws_flags & WRDSF_NOVAR)
  1806. && command[i+1] == '{'
  1807. && find_closing_paren (command, i + 2, len, &i, "{}") == 0)
  1808. continue;
  1809. if (!(wsp->ws_flags & WRDSF_NOCMD)
  1810. && command[i+1] == '('
  1811. && find_closing_paren (command, i + 2, len, &i, "()") == 0)
  1812. continue;
  1813. }
  1814. if (!consume_all && ISDELIM (wsp, command[i]))
  1815. break;
  1816. else
  1817. i++;
  1818. }
  1819. }
  1820. else if (WSP_RETURN_DELIMS (wsp))
  1821. {
  1822. i++;
  1823. flags |= _WSNF_DELIM;
  1824. }
  1825. else if (!(wsp->ws_flags & WRDSF_SQUEEZE_DELIMS))
  1826. flags |= _WSNF_EMPTYOK;
  1827. if (join && i > start && wsp->ws_tail)
  1828. wsp->ws_tail->flags |= _WSNF_JOIN;
  1829. if (wordsplit_add_segm (wsp, start, i, flags))
  1830. return _WRDS_ERR;
  1831. wsp->ws_endp = i;
  1832. if (wsp->ws_flags & WRDSF_INCREMENTAL)
  1833. return _WRDS_EOF;
  1834. if (consume_all)
  1835. {
  1836. if (!np)
  1837. np = wsp->ws_head;
  1838. while (np)
  1839. {
  1840. np->flags |= _WSNF_QUOTE;
  1841. np = np->next;
  1842. }
  1843. }
  1844. return _WRDS_OK;
  1845. }
  1846. #define to_num(c) \
  1847. (ISDIGIT(c) ? c - '0' : (ISXDIGIT(c) ? toupper(c) - 'A' + 10 : 255 ))
  1848. static int
  1849. xtonum (int *pval, const char *src, int base, int cnt)
  1850. {
  1851. int i, val;
  1852. for (i = 0, val = 0; i < cnt; i++, src++)
  1853. {
  1854. int n = *(unsigned char *) src;
  1855. if (n > 127 || (n = to_num (n)) >= base)
  1856. break;
  1857. val = val * base + n;
  1858. }
  1859. *pval = val;
  1860. return i;
  1861. }
  1862. size_t
  1863. wordsplit_c_quoted_length (const char *str, int quote_hex, int *quote)
  1864. {
  1865. size_t len = 0;
  1866. *quote = 0;
  1867. for (; *str; str++)
  1868. {
  1869. if (strchr (" \"", *str))
  1870. *quote = 1;
  1871. if (*str == ' ')
  1872. len++;
  1873. else if (*str == '"')
  1874. len += 2;
  1875. else if (*str != '\t' && *str != '\\' && ISPRINT (*str))
  1876. len++;
  1877. else if (quote_hex)
  1878. len += 3;
  1879. else
  1880. {
  1881. if (wordsplit_c_quote_char (*str))
  1882. len += 2;
  1883. else
  1884. len += 4;
  1885. }
  1886. }
  1887. return len;
  1888. }
  1889. static int
  1890. wsplt_unquote_char (const char *transtab, int c)
  1891. {
  1892. while (*transtab && transtab[1])
  1893. {
  1894. if (*transtab++ == c)
  1895. return *transtab;
  1896. ++transtab;
  1897. }
  1898. return 0;
  1899. }
  1900. static int
  1901. wsplt_quote_char (const char *transtab, int c)
  1902. {
  1903. for (; *transtab && transtab[1]; transtab += 2)
  1904. {
  1905. if (transtab[1] == c)
  1906. return *transtab;
  1907. }
  1908. return 0;
  1909. }
  1910. int
  1911. wordsplit_c_unquote_char (int c)
  1912. {
  1913. return wsplt_unquote_char (wordsplit_c_escape_tab, c);
  1914. }
  1915. int
  1916. wordsplit_c_quote_char (int c)
  1917. {
  1918. return wsplt_quote_char (wordsplit_c_escape_tab, c);
  1919. }
  1920. void
  1921. wordsplit_string_unquote_copy (struct wordsplit *ws, int inquote,
  1922. char *dst, const char *src, size_t n)
  1923. {
  1924. int i = 0;
  1925. int c;
  1926. inquote = !!inquote;
  1927. while (i < n)
  1928. {
  1929. if (src[i] == '\\')
  1930. {
  1931. ++i;
  1932. if (WRDSO_ESC_TEST (ws, inquote, WRDSO_XESC)
  1933. && (src[i] == 'x' || src[i] == 'X'))
  1934. {
  1935. if (n - i < 2)
  1936. {
  1937. *dst++ = '\\';
  1938. *dst++ = src[i++];
  1939. }
  1940. else
  1941. {
  1942. int off = xtonum (&c, src + i + 1,
  1943. 16, 2);
  1944. if (off == 0)
  1945. {
  1946. *dst++ = '\\';
  1947. *dst++ = src[i++];
  1948. }
  1949. else
  1950. {
  1951. *dst++ = c;
  1952. i += off + 1;
  1953. }
  1954. }
  1955. }
  1956. else if (WRDSO_ESC_TEST (ws, inquote, WRDSO_OESC)
  1957. && (unsigned char) src[i] < 128 && ISDIGIT (src[i]))
  1958. {
  1959. if (n - i < 1)
  1960. {
  1961. *dst++ = '\\';
  1962. *dst++ = src[i++];
  1963. }
  1964. else
  1965. {
  1966. int off = xtonum (&c, src + i, 8, 3);
  1967. if (off == 0)
  1968. {
  1969. *dst++ = '\\';
  1970. *dst++ = src[i++];
  1971. }
  1972. else
  1973. {
  1974. *dst++ = c;
  1975. i += off;
  1976. }
  1977. }
  1978. }
  1979. else if ((c = wsplt_unquote_char (ws->ws_escape[inquote], src[i])))
  1980. {
  1981. *dst++ = c;
  1982. ++i;
  1983. }
  1984. else
  1985. {
  1986. if (WRDSO_ESC_TEST (ws, inquote, WRDSO_BSKEEP))
  1987. *dst++ = '\\';
  1988. *dst++ = src[i++];
  1989. }
  1990. }
  1991. else
  1992. *dst++ = src[i++];
  1993. }
  1994. *dst = 0;
  1995. }
  1996. void
  1997. wordsplit_c_quote_copy (char *dst, const char *src, int quote_hex)
  1998. {
  1999. for (; *src; src++)
  2000. {
  2001. if (*src == '"')
  2002. {
  2003. *dst++ = '\\';
  2004. *dst++ = *src;
  2005. }
  2006. else if (*src != '\t' && *src != '\\' && ISPRINT (*src))
  2007. *dst++ = *src;
  2008. else
  2009. {
  2010. char tmp[4];
  2011. if (quote_hex)
  2012. {
  2013. snprintf (tmp, sizeof tmp, "%%%02X", *(unsigned char *) src);
  2014. memcpy (dst, tmp, 3);
  2015. dst += 3;
  2016. }
  2017. else
  2018. {
  2019. int c = wordsplit_c_quote_char (*src);
  2020. *dst++ = '\\';
  2021. if (c)
  2022. *dst++ = c;
  2023. else
  2024. {
  2025. snprintf (tmp, sizeof tmp, "%03o", *(unsigned char *) src);
  2026. memcpy (dst, tmp, 3);
  2027. dst += 3;
  2028. }
  2029. }
  2030. }
  2031. }
  2032. }
  2033. /* This structure describes a single expansion phase */
  2034. struct exptab
  2035. {
  2036. char const *descr; /* Textual description (for debugging) */
  2037. int flag; /* WRDSF_ bit that controls this phase */
  2038. int opt; /* Entry-specific options (see EXPOPT_ flags below */
  2039. int (*expansion) (struct wordsplit *wsp); /* expansion function */
  2040. };
  2041. /* The following options control expansions: */
  2042. /* Normally the exptab entry is run if its flag bit is set in struct
  2043. wordsplit. The EXPOPT_NEG option negates this test so that expansion
  2044. is performed if its associated flag bit is not set in struct wordsplit. */
  2045. #define EXPOPT_NEG 0x01
  2046. /* All bits in flag must be set in order for entry to match */
  2047. #define EXPORT_ALLOF 0x02
  2048. /* Coalesce the input list before running the expansion. */
  2049. #define EXPOPT_COALESCE 0x04
  2050. static struct exptab exptab[] = {
  2051. { N_("WS trimming"), WRDSF_WS, 0,
  2052. wordsplit_trimws },
  2053. { N_("command substitution"), WRDSF_NOCMD, EXPOPT_NEG|EXPOPT_COALESCE,
  2054. wordsplit_cmdexp },
  2055. { N_("coalesce list"), 0, EXPOPT_NEG|EXPOPT_COALESCE,
  2056. NULL },
  2057. { N_("tilde expansion"), WRDSF_PATHEXPAND, 0,
  2058. wordsplit_tildexpand },
  2059. { N_("variable expansion"), WRDSF_NOVAR, EXPOPT_NEG,
  2060. wordsplit_varexp },
  2061. { N_("quote removal"), 0, EXPOPT_NEG,
  2062. wsnode_quoteremoval },
  2063. { N_("coalesce list"), 0, EXPOPT_NEG|EXPOPT_COALESCE,
  2064. NULL },
  2065. { N_("path expansion"), WRDSF_PATHEXPAND, 0,
  2066. wordsplit_pathexpand },
  2067. { NULL }
  2068. };
  2069. static inline int
  2070. exptab_matches(struct exptab *p, struct wordsplit *wsp)
  2071. {
  2072. int result;
  2073. result = (wsp->ws_flags & p->flag);
  2074. if (p->opt & EXPORT_ALLOF)
  2075. result = result == p->flag;
  2076. if (p->opt & EXPOPT_NEG)
  2077. result = !result;
  2078. return result;
  2079. }
  2080. static int
  2081. wordsplit_process_list (struct wordsplit *wsp, size_t start)
  2082. {
  2083. struct exptab *p;
  2084. if (wsp->ws_flags & WRDSF_SHOWDBG)
  2085. wsp->ws_debug (_("(%02d) Input:%.*s;"),
  2086. wsp->ws_lvl, (int) wsp->ws_len, wsp->ws_input);
  2087. if ((wsp->ws_flags & WRDSF_NOSPLIT)
  2088. || ((wsp->ws_options & WRDSO_MAXWORDS)
  2089. && wsp->ws_wordi + 1 == wsp->ws_maxwords))
  2090. {
  2091. /* Treat entire input as a single word */
  2092. if (scan_word (wsp, start, 1) == _WRDS_ERR)
  2093. return wsp->ws_errno;
  2094. }
  2095. else
  2096. {
  2097. int rc;
  2098. while ((rc = scan_word (wsp, start, 0)) == _WRDS_OK)
  2099. start = skip_delim (wsp);
  2100. /* Make sure tail element is not joinable */
  2101. if (wsp->ws_tail)
  2102. wsp->ws_tail->flags &= ~_WSNF_JOIN;
  2103. if (rc == _WRDS_ERR)
  2104. return wsp->ws_errno;
  2105. }
  2106. if (wsp->ws_flags & WRDSF_SHOWDBG)
  2107. {
  2108. wsp->ws_debug ("(%02d) %s", wsp->ws_lvl, _("Initial list:"));
  2109. wordsplit_dump_nodes (wsp);
  2110. }
  2111. for (p = exptab; p->descr; p++)
  2112. {
  2113. if (exptab_matches(p, wsp))
  2114. {
  2115. if (p->opt & EXPOPT_COALESCE)
  2116. {
  2117. if (wsnode_coalesce (wsp))
  2118. break;
  2119. if (wsp->ws_flags & WRDSF_SHOWDBG)
  2120. {
  2121. wsp->ws_debug ("(%02d) %s", wsp->ws_lvl,
  2122. _("Coalesced list:"));
  2123. wordsplit_dump_nodes (wsp);
  2124. }
  2125. }
  2126. if (p->expansion)
  2127. {
  2128. if (p->expansion (wsp))
  2129. break;
  2130. if (wsp->ws_flags & WRDSF_SHOWDBG)
  2131. {
  2132. wsp->ws_debug ("(%02d) %s", wsp->ws_lvl, _(p->descr));
  2133. wordsplit_dump_nodes (wsp);
  2134. }
  2135. }
  2136. }
  2137. }
  2138. return wsp->ws_errno;
  2139. }
  2140. static int
  2141. wordsplit_run (const char *command, size_t length, struct wordsplit *wsp,
  2142. int flags, int lvl)
  2143. {
  2144. int rc;
  2145. size_t start;
  2146. if (!command)
  2147. {
  2148. if (!(flags & WRDSF_INCREMENTAL))
  2149. return _wsplt_seterr (wsp, WRDSE_USAGE);
  2150. if (wsp->ws_head)
  2151. return wordsplit_finish (wsp);
  2152. start = skip_delim_real (wsp);
  2153. if (wsp->ws_endp == wsp->ws_len)
  2154. return _wsplt_seterr (wsp, WRDSE_NOINPUT);
  2155. wsp->ws_flags |= WRDSF_REUSE;
  2156. wordsplit_init0 (wsp);
  2157. }
  2158. else
  2159. {
  2160. start = 0;
  2161. rc = wordsplit_init (wsp, command, length, flags);
  2162. if (rc)
  2163. return rc;
  2164. wsp->ws_lvl = lvl;
  2165. }
  2166. rc = wordsplit_process_list (wsp, start);
  2167. if (rc)
  2168. return rc;
  2169. return wordsplit_finish (wsp);
  2170. }
  2171. int
  2172. wordsplit_len (const char *command, size_t length, struct wordsplit *wsp,
  2173. int flags)
  2174. {
  2175. return wordsplit_run (command, length, wsp, flags, 0);
  2176. }
  2177. int
  2178. wordsplit (const char *command, struct wordsplit *ws, int flags)
  2179. {
  2180. return wordsplit_len (command, command ? strlen (command) : 0, ws, flags);
  2181. }
  2182. void
  2183. wordsplit_free_words (struct wordsplit *ws)
  2184. {
  2185. size_t i;
  2186. for (i = 0; i < ws->ws_wordc; i++)
  2187. {
  2188. char *p = ws->ws_wordv[ws->ws_offs + i];
  2189. if (p)
  2190. {
  2191. free (p);
  2192. ws->ws_wordv[ws->ws_offs + i] = NULL;
  2193. }
  2194. }
  2195. ws->ws_wordc = 0;
  2196. }
  2197. void
  2198. wordsplit_free_envbuf (struct wordsplit *ws)
  2199. {
  2200. if (ws->ws_flags & WRDSF_NOCMD)
  2201. return;
  2202. if (ws->ws_envbuf)
  2203. {
  2204. size_t i;
  2205. for (i = 0; ws->ws_envbuf[i]; i++)
  2206. free (ws->ws_envbuf[i]);
  2207. free (ws->ws_envbuf);
  2208. ws->ws_envidx = ws->ws_envsiz = 0;
  2209. ws->ws_envbuf = NULL;
  2210. }
  2211. }
  2212. void
  2213. wordsplit_clearerr (struct wordsplit *ws)
  2214. {
  2215. if (ws->ws_errno == WRDSE_USERERR)
  2216. free (ws->ws_usererr);
  2217. ws->ws_usererr = NULL;
  2218. ws->ws_errno = WRDSE_OK;
  2219. }
  2220. void
  2221. wordsplit_free (struct wordsplit *ws)
  2222. {
  2223. wordsplit_free_nodes (ws);
  2224. wordsplit_free_words (ws);
  2225. free (ws->ws_wordv);
  2226. ws->ws_wordv = NULL;
  2227. wordsplit_free_envbuf (ws);
  2228. }
  2229. int
  2230. wordsplit_get_words (struct wordsplit *ws, size_t *wordc, char ***wordv)
  2231. {
  2232. char **p = realloc (ws->ws_wordv,
  2233. (ws->ws_wordc + 1) * sizeof (ws->ws_wordv[0]));
  2234. if (!p)
  2235. return -1;
  2236. *wordv = p;
  2237. *wordc = ws->ws_wordc;
  2238. ws->ws_wordv = NULL;
  2239. ws->ws_wordc = 0;
  2240. ws->ws_wordn = 0;
  2241. return 0;
  2242. }
  2243. const char *_wordsplit_errstr[] = {
  2244. N_("no error"),
  2245. N_("missing closing quote"),
  2246. N_("memory exhausted"),
  2247. N_("invalid wordsplit usage"),
  2248. N_("unbalanced curly brace"),
  2249. N_("undefined variable"),
  2250. N_("input exhausted"),
  2251. N_("unbalanced parenthesis"),
  2252. N_("globbing error")
  2253. };
  2254. int _wordsplit_nerrs =
  2255. sizeof (_wordsplit_errstr) / sizeof (_wordsplit_errstr[0]);
  2256. const char *
  2257. wordsplit_strerror (struct wordsplit *ws)
  2258. {
  2259. if (ws->ws_errno == WRDSE_USERERR)
  2260. return ws->ws_usererr;
  2261. if (ws->ws_errno < _wordsplit_nerrs)
  2262. return _wordsplit_errstr[ws->ws_errno];
  2263. return N_("unknown error");
  2264. }
  2265. void
  2266. wordsplit_perror (struct wordsplit *wsp)
  2267. {
  2268. switch (wsp->ws_errno)
  2269. {
  2270. case WRDSE_QUOTE:
  2271. wsp->ws_error (_("missing closing %c (start near #%lu)"),
  2272. wsp->ws_input[wsp->ws_endp],
  2273. (unsigned long) wsp->ws_endp);
  2274. break;
  2275. default:
  2276. wsp->ws_error ("%s", wordsplit_strerror (wsp));
  2277. }
  2278. }