aboutsummaryrefslogtreecommitdiff
path: root/src/wordsplit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/wordsplit.c')
-rw-r--r--src/wordsplit.c147
1 files changed, 73 insertions, 74 deletions
diff --git a/src/wordsplit.c b/src/wordsplit.c
index 05c3643..f563725 100644
--- a/src/wordsplit.c
+++ b/src/wordsplit.c
@@ -70,7 +70,7 @@ _wsplt_alloc_die (struct wordsplit *wsp)
70 abort (); 70 abort ();
71} 71}
72 72
73static void 73static void
74_wsplt_error (const char *fmt, ...) 74_wsplt_error (const char *fmt, ...)
75{ 75{
76 va_list ap; 76 va_list ap;
@@ -91,7 +91,7 @@ _wsplt_seterr (struct wordsplit *wsp, int ec)
91 wordsplit_perror (wsp); 91 wordsplit_perror (wsp);
92 return ec; 92 return ec;
93} 93}
94 94
95static int 95static int
96_wsplt_nomem (struct wordsplit *wsp) 96_wsplt_nomem (struct wordsplit *wsp)
97{ 97{
@@ -122,7 +122,7 @@ _wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss,
122 int flags, int finalize) 122 int flags, int finalize)
123{ 123{
124 int rc; 124 int rc;
125 125
126 wss->ws_delim = wsp->ws_delim; 126 wss->ws_delim = wsp->ws_delim;
127 wss->ws_debug = wsp->ws_debug; 127 wss->ws_debug = wsp->ws_debug;
128 wss->ws_error = wsp->ws_error; 128 wss->ws_error = wsp->ws_error;
@@ -146,12 +146,12 @@ _wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss,
146 } 146 }
147 147
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
156 rc = wordsplit_init (wss, str, len, flags); 156 rc = wordsplit_init (wss, str, len, flags);
157 if (rc) 157 if (rc)
@@ -205,7 +205,7 @@ wordsplit_init0 (struct wordsplit *wsp)
205} 205}
206 206
207char wordsplit_c_escape_tab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; 207char wordsplit_c_escape_tab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v";
208 208
209static int 209static int
210wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, 210wordsplit_init (struct wordsplit *wsp, const char *input, size_t len,
211 int flags) 211 int flags)
@@ -278,8 +278,8 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len,
278 { 278 {
279 wsp->ws_escape[WRDSX_WORD] = wordsplit_c_escape_tab; 279 wsp->ws_escape[WRDSX_WORD] = wordsplit_c_escape_tab;
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 }
284 else 284 else
285 { 285 {
@@ -296,16 +296,16 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len,
296 } 296 }
297 wsp->ws_paramidx = wsp->ws_paramsiz = 0; 297 wsp->ws_paramidx = wsp->ws_paramsiz = 0;
298 wsp->ws_parambuf = NULL; 298 wsp->ws_parambuf = NULL;
299 299
300 wsp->ws_endp = 0; 300 wsp->ws_endp = 0;
301 wsp->ws_wordi = 0; 301 wsp->ws_wordi = 0;
302 302
303 if (wsp->ws_flags & WRDSF_REUSE) 303 if (wsp->ws_flags & WRDSF_REUSE)
304 wordsplit_free_nodes (wsp); 304 wordsplit_free_nodes (wsp);
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;
310} 310}
311 311
@@ -591,7 +591,7 @@ coalesce_segment (struct wordsplit *wsp, struct wordsplit_node *node)
591 591
592 if (!(node->flags & _WSNF_JOIN)) 592 if (!(node->flags & _WSNF_JOIN))
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)
596 { 596 {
597 len += wsnode_len (p); 597 len += wsnode_len (p);
@@ -721,11 +721,11 @@ wordsplit_finish (struct wordsplit *wsp)
721 721
722 Nodes of type _WSNF_DELIM get inserted to the node list if either 722 Nodes of type _WSNF_DELIM get inserted to the node list if either
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:
726 726
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.
730 730
731 If WRDSO_MAXWORDS option is set, stop compressing when 731 If WRDSO_MAXWORDS option is set, stop compressing when
@@ -733,12 +733,12 @@ wordsplit_finish (struct wordsplit *wsp)
733 a single last node. 733 a single last node.
734 734
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
738 a single last node. 738 a single last node.
739 739
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.
743 */ 743 */
744 again: 744 again:
@@ -785,7 +785,7 @@ wordsplit_finish (struct wordsplit *wsp)
785 continue; 785 continue;
786 } 786 }
787 } 787 }
788 else 788 else
789 { 789 {
790 if (delim) 790 if (delim)
791 { 791 {
@@ -858,8 +858,8 @@ wordsplit_finish (struct wordsplit *wsp)
858 char *newstr = malloc (slen + 1); 858 char *newstr = malloc (slen + 1);
859 859
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;
864 if (!newstr) 864 if (!newstr)
865 return _wsplt_nomem (wsp); 865 return _wsplt_nomem (wsp);
@@ -972,7 +972,7 @@ find_closing_paren (const char *str, size_t i, size_t len, size_t *poff,
972 break; 972 break;
973 } 973 }
974 break; 974 break;
975 975
976 case '"': 976 case '"':
977 state = st_dquote; 977 state = st_dquote;
978 break; 978 break;
@@ -1052,7 +1052,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen,
1052{ 1052{
1053 int n = (wsp->ws_flags & WRDSF_ENV_KV) ? 2 : 1; 1053 int n = (wsp->ws_flags & WRDSF_ENV_KV) ? 2 : 1;
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)
1057 { 1057 {
1058 size_t sz; 1058 size_t sz;
@@ -1069,7 +1069,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen,
1069 for (; wsp->ws_env[i]; i++) 1069 for (; wsp->ws_env[i]; i++)
1070 ; 1070 ;
1071 } 1071 }
1072 1072
1073 sz = i + n + 1; 1073 sz = i + n + 1;
1074 1074
1075 newenv = calloc (sz, sizeof(newenv[0])); 1075 newenv = calloc (sz, sizeof(newenv[0]));
@@ -1088,7 +1088,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen,
1088 } 1088 }
1089 } 1089 }
1090 newenv[j] = NULL; 1090 newenv[j] = NULL;
1091 1091
1092 wsp->ws_envbuf = newenv; 1092 wsp->ws_envbuf = newenv;
1093 wsp->ws_envidx = i; 1093 wsp->ws_envidx = i;
1094 wsp->ws_envsiz = sz; 1094 wsp->ws_envsiz = sz;
@@ -1109,7 +1109,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen,
1109 else 1109 else
1110 { 1110 {
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)
1114 return _wsplt_nomem (wsp); 1114 return _wsplt_nomem (wsp);
1115 n += (n + 1) / 2; 1115 n += (n + 1) / 2;
@@ -1121,7 +1121,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen,
1121 wsp->ws_env = (const char**) wsp->ws_envbuf; 1121 wsp->ws_env = (const char**) wsp->ws_envbuf;
1122 } 1122 }
1123 } 1123 }
1124 1124
1125 if (wsp->ws_flags & WRDSF_ENV_KV) 1125 if (wsp->ws_flags & WRDSF_ENV_KV)
1126 { 1126 {
1127 /* A key-value pair environment */ 1127 /* A key-value pair environment */
@@ -1158,7 +1158,7 @@ int
1158wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) 1158wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value)
1159{ 1159{
1160 char *v;