4
0

wordsplit.c 53 KB

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