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