aboutsummaryrefslogtreecommitdiff
path: root/tests/wstest.c
blob: c7c0a6bd86dc76eb6dd3e9648c21d9ab744ed6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* 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 <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wordsplit.h>

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;
}

Return to:

Send suggestions and report system problems to the System administrator.