diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2019-05-15 16:37:09 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2019-05-15 16:37:09 +0300 |
commit | 20899f652a904b510b2c7d3507b75d1399416485 (patch) | |
tree | 44f221b132d16f229fb991a03451d7039fb5f726 /src | |
parent | 7dbea4b03426d8a81dbc6563b96aa760e719f5a8 (diff) | |
download | grecs-20899f652a904b510b2c7d3507b75d1399416485.tar.gz grecs-20899f652a904b510b2c7d3507b75d1399416485.tar.bz2 |
wordsplit: introduce error context
* include/wordsplit.h (wordsplit) <ws_errctx>: New field.
* src/wordsplit.c (wordsplit_init): Initialize ws_errctx.
(expvar,wordsplit_pathexpand): Save error context.
(wordsplit_clearerr): Free error context.
(wordsplit_perror): Use error context if available.
* tests/wordsplit.at: Reflect changes.
Diffstat (limited to 'src')
-rw-r--r-- | src/wordsplit.c | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/src/wordsplit.c b/src/wordsplit.c index af747b3..6dba914 100644 --- a/src/wordsplit.c +++ b/src/wordsplit.c | |||
@@ -107,6 +107,30 @@ _wsplt_nomem (struct wordsplit *wsp) | |||
107 | return wsp->ws_errno; | 107 | return wsp->ws_errno; |
108 | } | 108 | } |
109 | 109 | ||
110 | static void | ||
111 | _wsplt_store_errctx (struct wordsplit *wsp, char const *str, size_t len) | ||
112 | { | ||
113 | free (wsp->ws_errctx); | ||
114 | wsp->ws_errctx = malloc (len + 1); | ||
115 | if (!wsp->ws_errctx) | ||
116 | { | ||
117 | wsp->ws_error ("%s", | ||
118 | _("memory exhausted while trying to store error subject")); | ||
119 | } | ||
120 | else | ||
121 | { | ||
122 | memcpy (wsp->ws_errctx, str, len); | ||
123 | wsp->ws_errctx[len] = 0; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | static inline int | ||
128 | _wsplt_setctxerr (struct wordsplit *wsp, int ec, char const *str, size_t len) | ||
129 | { | ||
130 | _wsplt_store_errctx (wsp, str, len); | ||
131 | return _wsplt_seterr (wsp, ec); | ||
132 | } | ||
133 | |||
110 | static int wordsplit_run (const char *command, size_t length, | 134 | static int wordsplit_run (const char *command, size_t length, |
111 | struct wordsplit *wsp, | 135 | struct wordsplit *wsp, |
112 | int flags, int lvl); | 136 | int flags, int lvl); |
@@ -307,6 +331,8 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, | |||
307 | wordsplit_free_nodes (wsp); | 331 | wordsplit_free_nodes (wsp); |
308 | wsp->ws_head = wsp->ws_tail = NULL; | 332 | wsp->ws_head = wsp->ws_tail = NULL; |
309 | 333 | ||
334 | wsp->ws_errctx = NULL; | ||
335 | |||
310 | wordsplit_init0 (wsp); | 336 | wordsplit_init0 (wsp); |
311 | 337 | ||
312 | return 0; | 338 | return 0; |
@@ -1575,7 +1601,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, | |||
1575 | } | 1601 | } |
1576 | else if (wsp->ws_flags & WRDSF_UNDEF) | 1602 | else if (wsp->ws_flags & WRDSF_UNDEF) |
1577 | { | 1603 | { |
1578 | _wsplt_seterr (wsp, WRDSE_UNDEF); | 1604 | _wsplt_setctxerr (wsp, WRDSE_UNDEF, str, *pend - str + 1); |
1579 | return 1; | 1605 | return 1; |
1580 | } | 1606 | } |
1581 | else | 1607 | else |
@@ -1680,7 +1706,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, | |||
1680 | static int | 1706 | static int |
1681 | begin_var_p (int c) | 1707 | begin_var_p (int c) |
1682 | { | 1708 | { |
1683 | return memchr("{#@*", c, 4) != NULL || ISVARBEG (c) || ISDIGIT (c); | 1709 | return memchr ("{#@*", c, 4) != NULL || ISVARBEG (c) || ISDIGIT (c); |
1684 | } | 1710 | } |
1685 | 1711 | ||
1686 | static int | 1712 | static int |
@@ -2098,7 +2124,7 @@ wordsplit_pathexpand (struct wordsplit *wsp) | |||
2098 | 2124 | ||
2099 | default: | 2125 | default: |
2100 | free (pattern); | 2126 | free (pattern); |
2101 | return _wsplt_seterr (wsp, WRDSE_GLOBERR); | 2127 | return _wsplt_setctxerr (wsp, WRDSE_GLOBERR, pattern, slen); |
2102 | } | 2128 | } |
2103 | 2129 | ||
2104 | prev = p; | 2130 | prev = p; |
@@ -2754,12 +2780,17 @@ wordsplit_clearerr (struct wordsplit *ws) | |||
2754 | if (ws->ws_errno == WRDSE_USERERR) | 2780 | if (ws->ws_errno == WRDSE_USERERR) |
2755 | free (ws->ws_usererr); | 2781 | free (ws->ws_usererr); |
2756 | ws->ws_usererr = NULL; | 2782 | ws->ws_usererr = NULL; |
2783 | |||
2784 | free (ws->ws_errctx); | ||
2785 | ws->ws_errctx = NULL; | ||
2786 | |||
2757 | ws->ws_errno = WRDSE_OK; | 2787 | ws->ws_errno = WRDSE_OK; |
2758 | } | 2788 | } |
2759 | 2789 | ||
2760 | void | 2790 | void |
2761 | wordsplit_free (struct wordsplit *ws) | 2791 | wordsplit_free (struct wordsplit *ws) |
2762 | { | 2792 | { |
2793 | wordsplit_clearerr (ws); | ||
2763 | wordsplit_free_nodes (ws); | 2794 | wordsplit_free_nodes (ws); |
2764 | wordsplit_free_words (ws); | 2795 | wordsplit_free_words (ws); |
2765 | free (ws->ws_wordv); | 2796 | free (ws->ws_wordv); |
@@ -2823,6 +2854,9 @@ wordsplit_perror (struct wordsplit *wsp) | |||
2823 | break; | 2854 | break; |
2824 | 2855 | ||
2825 | default: | 2856 | default: |
2826 | wsp->ws_error ("%s", wordsplit_strerror (wsp)); | 2857 | if (wsp->ws_errctx) |
2858 | wsp->ws_error ("%s: %s", wordsplit_strerror (wsp), wsp->ws_errctx); | ||
2859 | else | ||
2860 | wsp->ws_error ("%s", wordsplit_strerror (wsp)); | ||
2827 | } | 2861 | } |
2828 | } | 2862 | } |