wordsplit.c 53 KB

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