aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2014-10-23 17:29:01 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2015-12-17 15:25:24 +0200
commit82f402caf19c334f3975fd5a74c2a7fd3f21603d (patch)
tree07718190450cb5f892e56bd212a8258b79a7a9dc
parentfc6012da9a3c264d168f47219630054747a553ac (diff)
downloadgrecs-82f402caf19c334f3975fd5a74c2a7fd3f21603d.tar.gz
grecs-82f402caf19c334f3975fd5a74c2a7fd3f21603d.tar.bz2
cleanup in wordsplit
-rw-r--r--src/wordsplit.c83
-rw-r--r--src/wordsplit.h31
2 files changed, 43 insertions, 71 deletions
diff --git a/src/wordsplit.c b/src/wordsplit.c
index a67e455..b2382c8 100644
--- a/src/wordsplit.c
+++ b/src/wordsplit.c
@@ -802,6 +802,8 @@ expvar (struct wordsplit *wsp, const char *str, size_t len,
break;
case WRDSE_USERERR:
+ if (wsp->ws_errno == WRDSE_USERERR)
+ free (wsp->ws_usererr);
wsp->ws_usererr = value;
/* fall through */
default:
@@ -1007,7 +1009,7 @@ node_expand (struct wordsplit *wsp, struct wordsplit_node *node,
return 0;
}
-/* Remove NULL lists */
+/* Remove NULL nodes from the list */
static void
wsnode_nullelim (struct wordsplit *wsp)
{
@@ -1063,7 +1065,7 @@ expcmd (struct wordsplit *wsp, const char *str, size_t len,
if (find_closing_paren (str, 0, len, &j, "()"))
{
- _wsplt_seterr (wsp, WRDSE_CBRACE);
+ _wsplt_seterr (wsp, WRDSE_PAREN);
return 1;
}
@@ -1086,7 +1088,11 @@ expcmd (struct wordsplit *wsp, const char *str, size_t len,
else if (rc)
{
if (rc == WRDSE_USERERR)
- wsp->ws_usererr = value;
+ {
+ if (wsp->ws_errno == WRDSE_USERERR)
+ free (wsp->ws_usererr);
+ wsp->ws_usererr = value;
+ }
_wsplt_seterr (wsp, rc);
return 1;
}
@@ -1647,7 +1653,7 @@ wordsplit_process_list (struct wordsplit *wsp, size_t start)
}
}
- /* Expand variables (FIXME: & commands) */
+ /* Expand variables */
if (!(wsp->ws_flags & WRDSF_NOVAR))
{
if (wordsplit_varexp (wsp))
@@ -1662,6 +1668,7 @@ wordsplit_process_list (struct wordsplit *wsp, size_t start)
}
}
+ /* Expand commands */
if (!(wsp->ws_flags & WRDSF_NOCMD))
{
if (wsnode_coalesce (wsp))
@@ -1774,8 +1781,7 @@ wordsplit_len (const char *command, size_t length, struct wordsplit *wsp,
int
wordsplit (const char *command, struct wordsplit *ws, int flags)
{
- return wordsplit_len (command, command ? strlen (command) : 0, ws,
- flags);
+ return wordsplit_len (command, command ? strlen (command) : 0, ws, flags);
}
void
@@ -1812,59 +1818,15 @@ wordsplit_free (struct wordsplit *ws)
ws->ws_wordv = NULL;
}
-void
-wordsplit_perror (struct wordsplit *wsp)
-{
- switch (wsp->ws_errno)
- {
- case WRDSE_EOF:
- wsp->ws_error (_("no error"));
- break;
-
- case WRDSE_QUOTE:
- wsp->ws_error (_("missing closing %c (start near #%lu)"),
- wsp->ws_input[wsp->ws_endp],
- (unsigned long) wsp->ws_endp);
- break;
-
- case WRDSE_NOSPACE:
- wsp->ws_error (_("memory exhausted"));
- break;
-
- case WRDSE_NOSUPP:
- wsp->ws_error (_("command substitution is not yet supported"));
- break;
-
- case WRDSE_USAGE:
- wsp->ws_error (_("invalid wordsplit usage"));
- break;
-
- case WRDSE_CBRACE:
- wsp->ws_error (_("unbalanced curly brace"));
- break;
-
- case WRDSE_UNDEF:
- wsp->ws_error (_("undefined variable"));
- break;
-
- case WRDSE_NOINPUT:
- wsp->ws_error (_("input exhausted"));
- break;
-
- default:
- wsp->ws_error (_("unknown error"));
- }
-}
-
const char *_wordsplit_errstr[] = {
N_("no error"),
N_("missing closing quote"),
N_("memory exhausted"),
- N_("command substitution is not yet supported"),
N_("invalid wordsplit usage"),
N_("unbalanced curly brace"),
N_("undefined variable"),
- N_("input exhausted")
+ N_("input exhausted"),
+ N_("unbalanced parenthesis")
};
int _wordsplit_nerrs =
sizeof (_wordsplit_errstr) / sizeof (_wordsplit_errstr[0]);
@@ -1878,3 +1840,20 @@ wordsplit_strerror (struct wordsplit *ws)
return _wordsplit_errstr[ws->ws_errno];
return N_("unknown error");
}
+
+void
+wordsplit_perror (struct wordsplit *wsp)
+{
+ switch (wsp->ws_errno)
+ {
+ case WRDSE_QUOTE:
+ wsp->ws_error (_("missing closing %c (start near #%lu)"),
+ wsp->ws_input[wsp->ws_endp],
+ (unsigned long) wsp->ws_endp);
+ break;
+
+ default:
+ wsp->ws_error (wordsplit_strerror (wsp));
+ }
+}
+
diff --git a/src/wordsplit.h b/src/wordsplit.h
index 4a0cd88..eee262d 100644
--- a/src/wordsplit.h
+++ b/src/wordsplit.h
@@ -54,10 +54,10 @@ struct wordsplit
/* Append the words found to the array resulting from a previous
call. */
#define WRDSF_APPEND 0x00000001
-/* Insert we_offs initial NULLs in the array ws_wordv.
+/* Insert ws_offs initial NULLs in the array ws_wordv.
(These are not counted in the returned ws_wordc.) */
#define WRDSF_DOOFFS 0x00000002
-/* Don't do command substitution. Reserved for future use. */
+/* Don't do command substitution. */
#define WRDSF_NOCMD 0x00000004
/* The parameter p resulted from a previous call to
wordsplit(), and wordsplit_free() was not called. Reuse the
@@ -65,10 +65,8 @@ struct wordsplit
#define WRDSF_REUSE 0x00000008
/* Print errors */
#define WRDSF_SHOWERR 0x00000010
-/* Consider it an error if an undefined shell variable
- is expanded. */
+/* Consider it an error if an undefined variable is expanded. */
#define WRDSF_UNDEF 0x00000020
-
/* Don't do variable expansion. */
#define WRDSF_NOVAR 0x00000040
/* Abort on ENOMEM error */
@@ -79,7 +77,7 @@ struct wordsplit
#define WRDSF_SQUOTE 0x00000200
/* Handle double quotes */
#define WRDSF_DQUOTE 0x00000400
-/* Handle quotes and escape directives */
+/* Handle single and double quotes */
#define WRDSF_QUOTE (WRDSF_SQUOTE|WRDSF_DQUOTE)
/* Replace each input sequence of repeated delimiters with a single
delimiter */
@@ -107,25 +105,21 @@ struct wordsplit
/* Don't split input into words. Useful for side effects. */
#define WRDSF_NOSPLIT 0x00400000
/* Keep undefined variables in place, instead of expanding them to
- empty string */
+ empty strings. */
#define WRDSF_KEEPUNDEF 0x00800000
/* Warn about undefined variables */
#define WRDSF_WARNUNDEF 0x01000000
/* Handle C escapes */
#define WRDSF_CESCAPES 0x02000000
-
/* ws_closure is set */
#define WRDSF_CLOSURE 0x04000000
/* ws_env is a Key/Value environment, i.e. the value of a variable is
stored in the element that follows its name. */
#define WRDSF_ENV_KV 0x08000000
-
/* ws_escape is set */
#define WRDSF_ESCAPE 0x10000000
-
/* Incremental mode */
#define WRDSF_INCREMENTAL 0x20000000
-
/* ws_command needs argv parameter */
#define WRDSF_ARGV 0x40000000
@@ -134,19 +128,19 @@ struct wordsplit
WRDSF_QUOTE | WRDSF_SQUEEZE_DELIMS | WRDSF_CESCAPES)
#define WRDSE_OK 0
-#define WRDSE_EOF 0
+#define WRDSE_EOF WRDSE_OK
#define WRDSE_QUOTE 1
#define WRDSE_NOSPACE 2
-#define WRDSE_NOSUPP 3
-#define WRDSE_USAGE 4
-#define WRDSE_CBRACE 5
-#define WRDSE_UNDEF 6
-#define WRDSE_NOINPUT 7
+#define WRDSE_USAGE 3
+#define WRDSE_CBRACE 4
+#define WRDSE_UNDEF 5
+#define WRDSE_NOINPUT 6
+#define WRDSE_PAREN 7
#define WRDSE_USERERR 8
int wordsplit (const char *s, struct wordsplit *p, int flags);
int wordsplit_len (const char *s, size_t len,
- struct wordsplit *p, int flags);
+ struct wordsplit *p, int flags);
void wordsplit_free (struct wordsplit *p);
void wordsplit_free_words (struct wordsplit *ws);
@@ -165,5 +159,4 @@ const char *wordsplit_strerror (struct wordsplit *ws);
void wordsplit_clearerr (struct wordsplit *ws);
-
#endif

Return to:

Send suggestions and report system problems to the System administrator.