/* wordsplit - a word splitter
Copyright (C) 2009-2011 Sergey Poznyakoff
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see . */
#ifdef HAVE_CONFIG_H
# include
#endif
#include
#include
#include
#include
struct flgtab
{
char *name;
int flag;
};
struct flgtab flgtab[] = {
#define FLG(n) { #n, n }
FLG(WRDSF_SHOWERR),
FLG(WRDSF_ENOMEMABRT),
FLG(WRDSF_WS),
FLG(WRDSF_QUOTE),
FLG(WRDSF_SQUEEZE_DELIMS),
FLG(WRDSF_RETURN_DELIMS),
FLG(WRDSF_SED_EXPR),
FLG(WRDSF_DEFFLAGS),
{ NULL }
#undef FLG
};
int
str2flag (const char *opt)
{
struct flgtab *f;
for (f = flgtab; f->name; f++)
if (strcmp (f->name, opt) == 0)
return f->flag;
return 0;
}
int
main (int argc, char **argv)
{
struct wordsplit ws;
int flags = 0;
int f;
size_t i;
char *text;
char *progname = argv[0];
while (--argc)
{
char *opt = *++argv;
if (strncmp (opt, "delim=", 6) == 0)
{
ws.ws_delim = opt + 6;
flags |= WRDSF_DELIM;
}
else if (strncmp (opt, "comment=", 8) == 0)
{
ws.ws_comment = opt + 8;
flags |= WRDSF_COMMENT;
}
else if ((opt[0] == '!' || opt[0] == '~')
&& strncmp (opt+1, "WRDSF_", 6) == 0)
{
f = str2flag (opt + 1);
if (!f)
{
fprintf (stderr, "%s: unknown flag: %s\n", progname, opt+1);
exit (1);
}
flags &= ~f;
}
else if (strncmp (opt, "WRDSF_", 6) == 0)
{
f = str2flag (opt);
if (!f)
{
fprintf (stderr, "%s: unknown flag: %s\n", progname, opt);
exit (1);
}
flags |= f;
}
else if (strcmp (opt, "--") == 0)
break;
else
break;
}
if (!flags)
flags = WRDSF_DEFFLAGS;
if (argc == 0)
{
fprintf (stderr, "%s: not enough arguments\n", progname);
return 1;
}
text = *argv++;
argc--;
if (wordsplit (text, &ws, flags))
{
fprintf (stderr, "%s: parse error\n", progname);
return 1;
}
if (ws.ws_wordc != argc)
{
fprintf (stderr, "%s: wrong number of fields (%lu/%d)\n",
progname, (unsigned long) ws.ws_wordc, argc);
return 1;
}
for (i = 0; i < ws.ws_wordc; i++)
if (strcmp (ws.ws_wordv[i], argv[i]))
{
fprintf (stderr, "%s: word %lu mismatch (got %s)\n",
progname, (unsigned long) i, ws.ws_wordv[i]);
return 1;
}
wordsplit_free (&ws);
return 0;
}