#include <string.h>
#include <unistd.h>
+#include <attribute.h>
#include <c-ctype.h>
#include <ialloc.h>
#define WSP_RETURN_DELIMS(wsp) \
((wsp)->ws_flags & WRDSF_RETURN_DELIMS || ((wsp)->ws_options & WRDSO_MAXWORDS))
+/* Set escape option F in WS for words (Q==0) or quoted strings (Q==1). */
+#define WRDSO_ESC_SET(ws,q,f) ((ws)->ws_options |= ((f) << 4*(q)))
+/* Test WS for escape option F for words (Q==0) or quoted strings (Q==1). */
+#define WRDSO_ESC_TEST(ws,q,f) ((ws)->ws_options & ((f) << 4*(q)))
+
/* When printing diagnostics with %.*s, output at most this many bytes.
Users typically don't want super-long strings in diagnostics,
and anyway printf fails if it outputs more than INT_MAX bytes.
fputc ('\n', stderr);
}
+#ifdef _WORDSPLIT_EXTRAS
+# define WORDSPLIT_EXTRAS_extern extern
+#else
+# define WORDSPLIT_EXTRAS_extern static
+static void wordsplit_clearerr (struct wordsplit *);
+static void wordsplit_free_envbuf (struct wordsplit *);
+static void wordsplit_free_words (struct wordsplit *);
+static void wordsplit_perror (struct wordsplit *);
+#endif
+
static void wordsplit_free_nodes (struct wordsplit *);
static int
return WRDSE_OK;
}
\f
+#ifdef _WORDSPLIT_EXTRAS
int
wordsplit_append (wordsplit_t *wsp, int argc, char **argv)
{
wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL;
return WRDSE_OK;
}
+#endif
\f
/* Variable expansion */
static int
return WRDSE_UNDEF;
}
+/* Initial size for ws_env, if allocated automatically */
+enum { WORDSPLIT_ENV_INIT = 16 };
+
static int
wsplt_assign_var (struct wordsplit *wsp, char const *name, idx_t namelen,
char *value)
return i;
}
+#ifdef _WORDSPLIT_EXTRAS
idx_t
wordsplit_c_quoted_length (const char *str, bool quote_hex, bool *quote)
{
}
return len;
}
+#endif
static char
wsplt_unquote_char (const char *transtab, char c)
return '\0';
}
+#ifdef _WORDSPLIT_EXTRAS
static char
wsplt_quote_char (const char *transtab, char c)
{
{
return wsplt_quote_char (wordsplit_c_escape_tab, c);
}
+#endif
void
wordsplit_string_unquote_copy (struct wordsplit *ws, bool inquote,
*dst = '\0';
}
+#ifdef _WORDSPLIT_EXTRAS
void
wordsplit_c_quote_copy (char *dst, const char *src, bool quote_hex)
{
}
}
}
+#endif
/* This structure describes a single expansion phase */
return wsp->ws_errno;
}
+WORDSPLIT_EXTRAS_extern
int
wordsplit_len (char const *command, idx_t length, struct wordsplit *wsp,
unsigned flags)
return wordsplit_len (command, command ? strlen (command) : 0, ws, flags);
}
+WORDSPLIT_EXTRAS_extern
void
wordsplit_free_words (struct wordsplit *ws)
{
ws->ws_wordc = 0;
}
+WORDSPLIT_EXTRAS_extern
void
wordsplit_free_envbuf (struct wordsplit *ws)
{
}
}
+WORDSPLIT_EXTRAS_extern
void
wordsplit_clearerr (struct wordsplit *ws)
{
wordsplit_free_envbuf (ws);
}
+#ifdef _WORDSPLIT_EXTRAS
void
wordsplit_get_words (struct wordsplit *ws, idx_t *wordc, char ***wordv)
{
ws->ws_wordc = 0;
ws->ws_wordn = 0;
}
+#endif
static char const *const wordsplit_errstr[] = {
N_("no error"),
enum { wordsplit_nerrs = sizeof wordsplit_errstr / sizeof *wordsplit_errstr };
const char *
-wordsplit_strerror (struct wordsplit *ws)
+wordsplit_strerror (struct wordsplit const *ws)
{
if (ws->ws_errno == WRDSE_USERERR)
return ws->ws_usererr;
return N_("unknown error");
}
+WORDSPLIT_EXTRAS_extern
void
wordsplit_perror (struct wordsplit *wsp)
{
#define __WORDSPLIT_H
#include <stddef.h>
-#include <attribute.h>
#include <idx.h>
+/* This wordsplit code has been tuned for GNU Tar.
+ Define _WORDSPLIT_EXTRAS before including wordsplit.h
+ to define extras that GNU Tar does not need. */
+
+#ifdef _WORDSPLIT_EXTRAS
typedef struct wordsplit wordsplit_t;
+#endif
/* Structure used to direct the splitting. Members marked with [Input]
can be defined before calling wordsplit(), those marked with [Output]
const char *ws_comment; /* [Input] (WRDSF_COMMENT) Comment characters. */
const char *ws_escape[2]; /* [Input] (WRDSF_ESCAPE) Characters to be escaped
with backslash. */
- void (*ws_alloc_die) (wordsplit_t *wsp);
+ void (*ws_alloc_die) (struct wordsplit *wsp);
/* [Input] (WRDSF_ALLOC_DIE) Function called when
out of memory. Must not return. */
void (*ws_error) (const char *, ...)
- ATTRIBUTE_FORMAT ((printf, 1, 2));
+ _GL_ATTRIBUTE_FORMAT ((printf, 1, 2));
/* [Input] (WRDSF_ERROR) Function used for error
reporting */
void (*ws_debug) (const char *, ...)
- ATTRIBUTE_FORMAT ((printf, 1, 2));
+ _GL_ATTRIBUTE_FORMAT ((printf, 1, 2));
/* [Input] (WRDSF_DEBUG) Function used for debug
output. */
const char **ws_env; /* [Input] (WRDSF_ENV, !WRDSF_NOVAR) Array of
idx_t ws_lvl; /* Invocation nesting level. */
};
-/* Initial size for ws_env, if allocated automatically */
-#define WORDSPLIT_ENV_INIT 16
-
/* Wordsplit flags. */
/* Append the words found to the array resulting from a previous
call. */
#define WRDSX_WORD 0
#define WRDSX_QUOTE 1
-/* Set escape option F in WS for words (Q==0) or quoted strings (Q==1) */
-#define WRDSO_ESC_SET(ws,q,f) ((ws)->ws_options |= ((f) << 4*(q)))
-/* Test WS for escape option F for words (Q==0) or quoted strings (Q==1) */
-#define WRDSO_ESC_TEST(ws,q,f) ((ws)->ws_options & ((f) << 4*(q)))
-
/* Error codes. */
#define WRDSE_OK 0
#define WRDSE_EOF WRDSE_OK
#define WRDSE_GLOBERR 8
#define WRDSE_USERERR 9
-int wordsplit (const char *s, wordsplit_t *ws, unsigned flags);
+int wordsplit (char const *s, struct wordsplit *ws, unsigned flags);
+void wordsplit_free (struct wordsplit *ws);
+char const *wordsplit_strerror (struct wordsplit const *ws);
+
+#ifdef _WORDSPLIT_EXTRAS
int wordsplit_len (const char *s, idx_t len, wordsplit_t *ws, unsigned flags);
-void wordsplit_free (wordsplit_t *ws);
void wordsplit_free_words (wordsplit_t *ws);
void wordsplit_free_envbuf (wordsplit_t *ws);
void wordsplit_get_words (wordsplit_t *ws, idx_t *wordc, char ***wordv);
void wordsplit_c_quote_copy (char *dst, const char *src, bool quote_hex);
void wordsplit_perror (wordsplit_t *ws);
-const char *wordsplit_strerror (wordsplit_t *ws);
void wordsplit_clearerr (wordsplit_t *ws);
+#endif
#endif