command: Factor command name matching out of command.c.
[pspp-builds.git] / tests / language / lexer / command-name-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "libpspp/assertion.h"
28 #include "libpspp/compiler.h"
29 #include "language/lexer/command-name.h"
30
31 #include "gl/error.h"
32 #include "gl/progname.h"
33
34 static char **commands, **strings;
35 static size_t n_commands, n_strings;
36
37 static void parse_options (int argc, char **argv);
38 static void usage (void) NO_RETURN;
39
40 int
41 main (int argc, char *argv[])
42 {
43   size_t i;
44
45   set_program_name (argv[0]);
46   parse_options (argc, argv);
47
48   for (i = 0; i < n_strings; i++)
49     {
50       const char *string = strings[i];
51       struct command_matcher cm;
52       const char *best;
53       size_t j;
54
55       if (i > 0)
56         putchar ('\n');
57       printf ("string=\"%s\":\n", string);
58       for (j = 0; j < n_commands; j++)
59         {
60           const char *command = commands[j];
61           int missing_words;
62           bool match, exact;
63
64           match = command_match (ss_cstr (command), ss_cstr (string),
65                                  &exact, &missing_words);
66           printf ("\tcommand=\"%s\" match=%s",
67                   command, match ? "yes" : "no");
68           if (match)
69             printf (" exact=%s missing_words=%d",
70                     exact ? "yes" : "no", missing_words);
71           putchar ('\n');
72         }
73
74       command_matcher_init (&cm, ss_cstr (string));
75       for (j = 0; j < n_commands; j++)
76         command_matcher_add (&cm, ss_cstr (commands[j]), commands[j]);
77       best = command_matcher_get_match (&cm);
78       printf ("match: %s, missing_words=%d\n",
79               best ? best : "none", command_matcher_get_missing_words (&cm));
80       command_matcher_destroy (&cm);
81     }
82
83   return 0;
84 }
85
86 static void
87 parse_options (int argc, char **argv)
88 {
89   int breakpoint;
90
91   for (;;)
92     {
93       static const struct option options[] =
94         {
95           {"help", no_argument, NULL, 'h'},
96           {NULL, 0, NULL, 0},
97         };
98
99       int c = getopt_long (argc, argv, "h", options, NULL);
100       if (c == -1)
101         break;
102
103       switch (c)
104         {
105         case 'h':
106           usage ();
107
108         case 0:
109           break;
110
111         case '?':
112           exit (EXIT_FAILURE);
113           break;
114
115         default:
116           NOT_REACHED ();
117         }
118
119     }
120
121   for (breakpoint = optind; ; breakpoint++)
122     if (breakpoint >= argc)
123       error (1, 0, "missing ',' on command line; use --help for help");
124     else if (!strcmp (argv[breakpoint], ","))
125       break;
126
127   commands = &argv[optind];
128   n_commands = breakpoint - optind;
129
130   strings = &argv[breakpoint + 1];
131   n_strings = argc - (breakpoint + 1);
132
133   if (n_commands == 0 || n_strings == 0)
134     error (1, 0, "must specify at least one command and one string; "
135            "use --help for help");
136 }
137
138 static void
139 usage (void)
140 {
141   printf ("\
142 %s, to match PSPP command names\n\
143 usage: %s [OPTIONS] COMMAND... , STRING...\n\
144 \n\
145 Options:\n\
146   -h, --help          print this help message\n",
147           program_name, program_name);
148   exit (EXIT_SUCCESS);
149 }