1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
27 #include "libpspp/assertion.h"
28 #include "libpspp/compiler.h"
29 #include "libpspp/misc.h"
30 #include "language/lexer/scan.h"
31 #include "language/lexer/token.h"
34 #include "gl/ftoastr.h"
35 #include "gl/progname.h"
36 #include "gl/read-file.h"
37 #include "gl/xalloc.h"
39 /* -a/--auto, -b/--batch, -i/--interactive: syntax mode. */
40 static enum segmenter_mode mode = SEG_MODE_AUTO;
42 static const char *parse_options (int argc, char **argv);
43 static void usage (void) NO_RETURN;
46 main (int argc, char *argv[])
48 const char *file_name;
52 struct string_lexer slex;
55 set_program_name (argv[0]);
56 file_name = parse_options (argc, argv);
58 /* Read from stdin into 'input'. Ensure that 'input' ends in a new-line
59 followed by a null byte. */
60 input = (!strcmp (file_name, "-")
61 ? fread_file (stdin, &length)
62 : read_file (file_name, &length));
64 error (EXIT_FAILURE, errno, "reading %s failed", file_name);
65 input = xrealloc (input, length + 3);
66 if (length == 0 || input[length - 1] != '\n')
67 input[length++] = '\n';
68 input[length++] = '\0';
70 string_lexer_init (&slex, input, mode);
75 more = string_lexer_next (&slex, &token);
77 printf ("%s", scan_type_to_string (token.type));
78 if (token.number != 0.0)
80 char s[DBL_BUFSIZE_BOUND];
82 dtoastr (s, sizeof s, 0, 0, token.number);
85 if (token.string.string != NULL || token.string.length > 0)
86 printf (" \"%.*s\"", (int) token.string.length, token.string.string);
89 token_destroy (&token);
99 parse_options (int argc, char **argv)
103 static const struct option options[] =
105 {"auto", no_argument, NULL, 'a'},
106 {"batch", no_argument, NULL, 'b'},
107 {"interactive", no_argument, NULL, 'i'},
108 {"help", no_argument, NULL, 'h'},
112 int c = getopt_long (argc, argv, "abih", options, NULL);
119 mode = SEG_MODE_AUTO;
123 mode = SEG_MODE_BATCH;
127 mode = SEG_MODE_INTERACTIVE;
146 if (optind + 1 != argc)
147 error (1, 0, "exactly one non-option argument required; "
148 "use --help for help");
156 %s, to test breaking PSPP syntax into tokens\n\
157 usage: %s [OPTIONS] INPUT\n\
160 -1, --one-segment feed one segment at a time\n\
161 -a, --auto use \"auto\" syntax mode\n\
162 -b, --batch use \"batch\" syntax mode\n\
163 -i, --interactive use \"interactive\" syntax mode (default)\n\
164 -v, --verbose include rows and column numbers in output\n\
165 -h, --help print this help message\n",
166 program_name, program_name);