wordsplit.c 53 KB

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