1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011, 2013 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 /* -s, --strip-trailing-newline: Strip trailing newline from last line of
44 static bool strip_trailing_newline;
46 static const char *parse_options (int argc, char **argv);
47 static void usage (void) NO_RETURN;
50 main (int argc, char *argv[])
52 const char *file_name;
56 struct string_lexer slex;
58 set_program_name (argv[0]);
59 file_name = parse_options (argc, argv);
61 /* Read from stdin into 'input'. */
62 input = (!strcmp (file_name, "-")
63 ? fread_file (stdin, 0, &length)
64 : read_file (file_name, 0, &length));
66 error (EXIT_FAILURE, errno, "reading %s failed", file_name);
68 if (strip_trailing_newline && length && input[length - 1] == '\n')
71 if (length && input[length - 1] == '\r')
75 struct token *tokens = NULL;
77 size_t allocated_tokens = 0;
78 string_lexer_init (&slex, input, length, mode, false);
81 if (n_tokens >= allocated_tokens)
82 tokens = x2nrealloc (tokens, &allocated_tokens, sizeof *tokens);
83 enum string_lexer_result result
84 = string_lexer_next (&slex, &tokens[n_tokens]);
86 if (result == SLR_ERROR)
87 tokens[n_tokens].type = T_STOP;
90 if (result == SLR_END)
94 for (size_t i = 0; i < n_tokens; )
96 struct merger m = MERGER_INIT;
99 for (size_t j = i; ; j++)
101 assert (j < n_tokens);
102 retval = merger_add (&m, &tokens[j], &out);
107 const struct token *t = retval ? &out : &tokens[i];
109 fputs (token_type_to_name (t->type), stdout);
110 if (t->number != 0.0)
112 double x = t->number;
114 if (x > LONG_MIN && x <= LONG_MAX && floor (x) == x)
115 printf (" %ld", (long int) x);
119 if (t->string.string != NULL || t->string.length > 0)
120 printf (" \"%.*s\"", (int) t->string.length, t->string.string);
132 for (size_t i = 0; i < n_tokens; i++)
133 token_uninit (&tokens[i]);
141 parse_options (int argc, char **argv)
145 static const struct option options[] =
147 {"auto", no_argument, NULL, 'a'},
148 {"batch", no_argument, NULL, 'b'},
149 {"interactive", no_argument, NULL, 'i'},
150 {"strip-trailing-newline", no_argument, NULL, 's'},
151 {"help", no_argument, NULL, 'h'},
155 int c = getopt_long (argc, argv, "sabih", options, NULL);
162 mode = SEG_MODE_AUTO;
166 mode = SEG_MODE_BATCH;
170 mode = SEG_MODE_INTERACTIVE;
174 strip_trailing_newline = true;
193 if (optind + 1 != argc)
194 error (1, 0, "exactly one non-option argument required; "
195 "use --help for help");
203 %s, to test breaking PSPP syntax into tokens\n\
204 usage: %s [OPTIONS] INPUT\n\
207 -a, --auto use \"auto\" syntax mode\n\
208 -b, --batch use \"batch\" syntax mode\n\
209 -i, --interactive use \"interactive\" syntax mode (default)\n\
210 -s, --strip-trailing-newline remove newline from end of input\n\
211 -v, --verbose include rows and column numbers in output\n\
212 -h, --help print this help message\n",
213 program_name, program_name);