diff options
-rw-r--r-- | include/wordsplit.h | 38 | ||||
-rw-r--r-- | src/wordsplit.c | 147 |
2 files changed, 92 insertions, 93 deletions
diff --git a/include/wordsplit.h b/include/wordsplit.h index d7eb26f..d4975b3 100644 --- a/include/wordsplit.h +++ b/include/wordsplit.h | |||
@@ -32,6 +32,6 @@ typedef struct wordsplit wordsplit_t; | |||
32 | given member. | 32 | given member. |
33 | 33 | ||
34 | If not redefined explicitly, most of them are set to some reasonable | 34 | If not redefined explicitly, most of them are set to some reasonable |
35 | default value upon entry to wordsplit(). */ | 35 | default value upon entry to wordsplit(). */ |
36 | struct wordsplit | 36 | struct wordsplit |
37 | { | 37 | { |
@@ -41,3 +41,3 @@ struct wordsplit | |||
41 | elements in ws_wordv to fill with NULLs. */ | 41 | elements in ws_wordv to fill with NULLs. */ |
42 | size_t ws_wordn; /* Number of elements ws_wordv can accomodate. */ | 42 | size_t ws_wordn; /* Number of elements ws_wordv can accomodate. */ |
43 | int ws_flags; /* [Input] Flags passed to wordsplit. */ | 43 | int ws_flags; /* [Input] Flags passed to wordsplit. */ |
@@ -55,11 +55,11 @@ struct wordsplit | |||
55 | void (*ws_alloc_die) (wordsplit_t *wsp); | 55 | void (*ws_alloc_die) (wordsplit_t *wsp); |
56 | /* [Input] (WRDSF_ALLOC_DIE) Function called when | 56 | /* [Input] (WRDSF_ALLOC_DIE) Function called when |
57 | out of memory. Must not return. */ | 57 | out of memory. Must not return. */ |
58 | void (*ws_error) (const char *, ...) | 58 | void (*ws_error) (const char *, ...) |
59 | __attribute__ ((__format__ (__printf__, 1, 2))); | 59 | __attribute__ ((__format__ (__printf__, 1, 2))); |
60 | /* [Input] (WRDSF_ERROR) Function used for error | 60 | /* [Input] (WRDSF_ERROR) Function used for error |
61 | reporting */ | 61 | reporting */ |
62 | void (*ws_debug) (const char *, ...) | 62 | void (*ws_debug) (const char *, ...) |
63 | __attribute__ ((__format__ (__printf__, 1, 2))); | 63 | __attribute__ ((__format__ (__printf__, 1, 2))); |
64 | /* [Input] (WRDSF_DEBUG) Function used for debug | 64 | /* [Input] (WRDSF_DEBUG) Function used for debug |
65 | output. */ | 65 | output. */ |
@@ -82,3 +82,3 @@ struct wordsplit | |||
82 | 82 | ||
83 | /* Temporary storage for parameters. Works similarly to ws_enbuf. | 83 | /* Temporary storage for parameters. Works similarly to ws_enbuf. |
84 | */ | 84 | */ |
@@ -87,5 +87,5 @@ struct wordsplit | |||
87 | size_t ws_paramsiz; | 87 | size_t ws_paramsiz; |
88 | 88 | ||
89 | int (*ws_getvar) (char **ret, const char *var, size_t len, void *clos); | 89 | int (*ws_getvar) (char **ret, const char *var, size_t len, void *clos); |
90 | /* [Input] (WRDSF_GETVAR, !WRDSF_NOVAR) Looks up | 90 | /* [Input] (WRDSF_GETVAR, !WRDSF_NOVAR) Looks up |
91 | the name VAR (LEN bytes long) in the table of | 91 | the name VAR (LEN bytes long) in the table of |
@@ -98,3 +98,3 @@ struct wordsplit | |||
98 | and returning WRDSE_USERERR. | 98 | and returning WRDSE_USERERR. |
99 | Whatever is stored in RET, it must be allocated | 99 | Whatever is stored in RET, it must be allocated |
100 | using malloc(3). */ | 100 | using malloc(3). */ |
@@ -103,4 +103,4 @@ struct wordsplit | |||
103 | int (*ws_command) (char **ret, const char *cmd, size_t len, char **argv, | 103 | int (*ws_command) (char **ret, const char *cmd, size_t len, char **argv, |
104 | void *clos); | 104 | void *clos); |
105 | /* [Input] (!WRDSF_NOCMD) Returns in the memory | 105 | /* [Input] (!WRDSF_NOCMD) Returns in the memory |
106 | location pointed to by RET the expansion of | 106 | location pointed to by RET the expansion of |
@@ -112,3 +112,3 @@ struct wordsplit | |||
112 | 112 | ||
113 | const char *ws_input; /* Input string (the S argument to wordsplit. */ | 113 | const char *ws_input; /* Input string (the S argument to wordsplit. */ |
114 | size_t ws_len; /* Length of ws_input. */ | 114 | size_t ws_len; /* Length of ws_input. */ |
@@ -121,3 +121,3 @@ struct wordsplit | |||
121 | struct wordsplit_node *ws_head, *ws_tail; | 121 | struct wordsplit_node *ws_head, *ws_tail; |
122 | /* Doubly-linked list of parsed out nodes. */ | 122 | /* Doubly-linked list of parsed out nodes. */ |
123 | int ws_lvl; /* Invocation nesting level. */ | 123 | int ws_lvl; /* Invocation nesting level. */ |
@@ -244,5 +244,5 @@ struct wordsplit | |||
244 | 244 | ||
245 | #define WRDSO_BSKEEP WRDSO_BSKEEP_WORD | 245 | #define WRDSO_BSKEEP WRDSO_BSKEEP_WORD |
246 | #define WRDSO_OESC WRDSO_OESC_WORD | 246 | #define WRDSO_OESC WRDSO_OESC_WORD |
247 | #define WRDSO_XESC WRDSO_XESC_WORD | 247 | #define WRDSO_XESC WRDSO_XESC_WORD |
248 | 248 | ||
diff --git a/src/wordsplit.c b/src/wordsplit.c index 05c3643..f563725 100644 --- a/src/wordsplit.c +++ b/src/wordsplit.c | |||
@@ -72,3 +72,3 @@ _wsplt_alloc_die (struct wordsplit *wsp) | |||
72 | 72 | ||
73 | static void | 73 | static void |
74 | _wsplt_error (const char *fmt, ...) | 74 | _wsplt_error (const char *fmt, ...) |
@@ -93,3 +93,3 @@ _wsplt_seterr (struct wordsplit *wsp, int ec) | |||
93 | } | 93 | } |
94 | 94 | ||
95 | static int | 95 | static int |
@@ -124,3 +124,3 @@ _wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss, | |||
124 | int rc; | 124 | int rc; |
125 | 125 | ||
126 | wss->ws_delim = wsp->ws_delim; | 126 | wss->ws_delim = wsp->ws_delim; |
@@ -148,8 +148,8 @@ _wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss, | |||
148 | wss->ws_options = wsp->ws_options; | 148 | wss->ws_options = wsp->ws_options; |
149 | 149 | ||
150 | flags |= WRDSF_DELIM | 150 | flags |= WRDSF_DELIM |
151 | | WRDSF_ALLOC_DIE | 151 | | WRDSF_ALLOC_DIE |
152 | | WRDSF_ERROR | 152 | | WRDSF_ERROR |
153 | | WRDSF_DEBUG | 153 | | WRDSF_DEBUG |
154 | | (wsp->ws_flags & (WRDSF_SHOWDBG | WRDSF_SHOWERR | WRDSF_OPTIONS)); | 154 | | (wsp->ws_flags & (WRDSF_SHOWDBG | WRDSF_SHOWERR | WRDSF_OPTIONS)); |
155 | 155 | ||
@@ -207,3 +207,3 @@ wordsplit_init0 (struct wordsplit *wsp) | |||
207 | char wordsplit_c_escape_tab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; | 207 | char wordsplit_c_escape_tab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; |
208 | 208 | ||
209 | static int | 209 | static int |
@@ -280,4 +280,4 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, | |||
280 | wsp->ws_escape[WRDSX_QUOTE] = wordsplit_c_escape_tab; | 280 | wsp->ws_escape[WRDSX_QUOTE] = wordsplit_c_escape_tab; |
281 | wsp->ws_options |= WRDSO_OESC_QUOTE | WRDSO_OESC_WORD | 281 | wsp->ws_options |= WRDSO_OESC_QUOTE | WRDSO_OESC_WORD |
282 | | WRDSO_XESC_QUOTE | WRDSO_XESC_WORD; | 282 | | WRDSO_XESC_QUOTE | WRDSO_XESC_WORD; |
283 | } | 283 | } |
@@ -298,3 +298,3 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, | |||
298 | wsp->ws_parambuf = NULL; | 298 | wsp->ws_parambuf = NULL; |
299 | 299 | ||
300 | wsp->ws_endp = 0; | 300 | wsp->ws_endp = 0; |
@@ -305,5 +305,5 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, | |||
305 | wsp->ws_head = wsp->ws_tail = NULL; | 305 | wsp->ws_head = wsp->ws_tail = NULL; |
306 | 306 | ||
307 | wordsplit_init0 (wsp); | 307 | wordsplit_init0 (wsp); |
308 | 308 | ||
309 | return 0; | 309 | return 0; |
@@ -593,3 +593,3 @@ coalesce_segment (struct wordsplit *wsp, struct wordsplit_node *node) | |||
593 | return 0; | 593 | return 0; |
594 | 594 | ||
595 | for (p = node; p && (p->flags & _WSNF_JOIN); p = p->next) | 595 | for (p = node; p && (p->flags & _WSNF_JOIN); p = p->next) |
@@ -723,3 +723,3 @@ wordsplit_finish (struct wordsplit *wsp) | |||
723 | WRDSF_RETURN_DELIMS flag or WRDSO_MAXWORDS option is set. | 723 | WRDSF_RETURN_DELIMS flag or WRDSO_MAXWORDS option is set. |
724 | 724 | ||
725 | The following cases should be distinguished: | 725 | The following cases should be distinguished: |
@@ -727,3 +727,3 @@ wordsplit_finish (struct wordsplit *wsp) | |||
727 | 1. If both WRDSF_SQUEEZE_DELIMS and WRDSF_RETURN_DELIMS are set, compress | 727 | 1. If both WRDSF_SQUEEZE_DELIMS and WRDSF_RETURN_DELIMS are set, compress |
728 | any runs of similar delimiter nodes to a single node. The nodes are | 728 | any runs of similar delimiter nodes to a single node. The nodes are |
729 | 'similar' if they point to the same delimiter character. | 729 | 'similar' if they point to the same delimiter character. |
@@ -735,3 +735,3 @@ wordsplit_finish (struct wordsplit *wsp) | |||
735 | 2. If WRDSO_MAXWORDS option is set, but WRDSF_RETURN_DELIMS is not, | 735 | 2. If WRDSO_MAXWORDS option is set, but WRDSF_RETURN_DELIMS is not, |
736 | remove any delimiter nodes. Stop operation when | 736 | remove any delimiter nodes. Stop operation when |
737 | ws_wordi + 1 == ws_maxwords, and coalesce the rest of nodes into | 737 | ws_wordi + 1 == ws_maxwords, and coalesce the rest of nodes into |
@@ -740,3 +740,3 @@ wordsplit_finish (struct wordsplit *wsp) | |||
740 | 3. If incremental operation is in progress, restart the loop any time | 740 | 3. If incremental operation is in progress, restart the loop any time |
741 | a delimiter node is about to be returned, unless WRDSF_RETURN_DELIMS | 741 | a delimiter node is about to be returned, unless WRDSF_RETURN_DELIMS |
742 | is set. | 742 | is set. |
@@ -787,3 +787,3 @@ wordsplit_finish (struct wordsplit *wsp) | |||
787 | } | 787 | } |
788 | else | 788 | else |
789 | { | 789 | { |
@@ -860,4 +860,4 @@ wordsplit_finish (struct wordsplit *wsp) | |||
860 | /* Assign newstr first, even if it is NULL. This way | 860 | /* Assign newstr first, even if it is NULL. This way |
861 | wordsplit_free will work even if we return | 861 | wordsplit_free will work even if we return |
862 | nomem later. */ | 862 | nomem later. */ |
863 | wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = newstr; | 863 | wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = newstr; |
@@ -974,3 +974,3 @@ find_closing_paren (const char *str, size_t i, size_t len, size_t *poff, | |||
974 | break; | 974 | break; |
975 | 975 | ||
976 | case '"': | 976 | case '"': |
@@ -1054,3 +1054,3 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, | |||
1054 | char *v; | 1054 | char *v; |
1055 | 1055 | ||
1056 | if (wsp->ws_envidx + n >= wsp->ws_envsiz) | 1056 | if (wsp->ws_envidx + n >= wsp->ws_envsiz) |
@@ -1071,3 +1071,3 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, | |||
1071 | } | 1071 | } |
1072 | 1072 | ||
1073 | sz = i + n + 1; | 1073 | sz = i + n + 1; |
@@ -1090,3 +1090,3 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, | |||
1090 | newenv[j] = NULL; | 1090 | newenv[j] = NULL; |
1091 | 1091 | ||
1092 | wsp->ws_envbuf = newenv; | 1092 | wsp->ws_envbuf = newenv; |
@@ -1111,3 +1111,3 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, | |||
1111 | size_t n = wsp->ws_envsiz; | 1111 | size_t n = wsp->ws_envsiz; |
1112 | 1112 | ||
1113 | if ((size_t) -1 / 3 * 2 / sizeof (wsp->ws_envbuf[0]) <= n) | 1113 | if ((size_t) -1 / 3 * 2 / sizeof (wsp->ws_envbuf[0]) <= n) |
@@ -1123,3 +1123,3 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, | |||
1123 | } | 1123 | } |
1124 | 1124 | ||
1125 | if (wsp->ws_flags & WRDSF_ENV_KV) | 1125 | if (wsp->ws_flags & WRDSF_ENV_KV) |
@@ -1160,3 +1160,3 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) | |||
1160 | char *v; | 1160 | char *v; |
1161 | 1161 | ||
1162 | if (param_idx < 0) | 1162 | if (param_idx < 0) |
@@ -1169,3 +1169,3 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) | |||
1169 | size_t i; | 1169 | size_t i; |
1170 | 1170 | ||
1171 | parambuf = calloc ((size_t)param_idx + 1, sizeof (parambuf[0])); | 1171 | parambuf = calloc ((size_t)param_idx + 1, sizeof (parambuf[0])); |
@@ -1185,3 +1185,3 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) | |||
1185 | } | 1185 | } |
1186 | 1186 | ||
1187 | wsp->ws_parambuf = parambuf; | 1187 | wsp->ws_parambuf = parambuf; |
@@ -1193,3 +1193,3 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) | |||
1193 | size_t n = wsp->ws_paramsiz; | 1193 | size_t n = wsp->ws_paramsiz; |
1194 | 1194 | ||
1195 | if ((size_t) -1 / 3 * 2 / sizeof (wsp->ws_parambuf[0]) <= n) | 1195 | if ((size_t) -1 / 3 * 2 / sizeof (wsp->ws_parambuf[0]) <= n) |
@@ -1210,3 +1210,3 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) | |||
1210 | return WRDSE_BADPARAM; | 1210 | return WRDSE_BADPARAM; |
1211 | 1211 | ||
1212 | v = strdup (value); | 1212 | v = strdup (value); |
@@ -1214,3 +1214,3 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) | |||
1214 | return _wsplt_nomem (wsp); | 1214 | return _wsplt_nomem (wsp); |
1215 | 1215 | ||
1216 | free (wsp->ws_parambuf[param_idx]); | 1216 | free (wsp->ws_parambuf[param_idx]); |
@@ -1249,6 +1249,6 @@ expand_paramv (struct wordsplit *wsp, struct wordsplit_node **ptail, int flg, | |||
1249 | int wsflags = WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_QUOTE | 1249 | int wsflags = WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_QUOTE |
1250 | | (WSP_RETURN_DELIMS (wsp) ? WRDSF_RETURN_DELIMS : 0) | 1250 | | (WSP_RETURN_DELIMS (wsp) ? WRDSF_RETURN_DELIMS : 0) |
1251 |