1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 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/>. */
29 #include "libpspp/assertion.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/float-format.h"
32 #include "libpspp/integer-format.h"
36 #include "gl/intprops.h"
37 #include "gl/progname.h"
38 #include "gl/xalloc.h"
47 static void buffer_put (struct buffer *, const void *, size_t);
48 static void *buffer_put_uninit (struct buffer *, size_t);
65 static enum token_type token;
66 static unsigned long int tok_integer;
67 static double tok_float;
68 static char *tok_string;
69 static size_t tok_strlen, tok_allocated;
71 /* --be, --le: Integer and floating-point formats. */
72 static enum float_format float_format = FLOAT_IEEE_DOUBLE_BE;
73 static enum integer_format integer_format = INTEGER_MSB_FIRST;
75 /* Input file and current position. */
77 static const char *input_file_name;
78 static int line_number;
80 static void PRINTF_FORMAT (1, 2)
81 fatal (const char *message, ...)
85 fprintf (stderr, "%s:%d: ", input_file_name, line_number);
86 va_start (args, message);
87 vfprintf (stderr, message, args);
97 if (tok_strlen >= tok_allocated)
98 tok_string = x2realloc (tok_string, &tok_allocated);
100 tok_string[tok_strlen++] = c;
113 while ((c = getc (input)) != '\n' && c != EOF)
119 while (isspace (c) || c == '<' || c == '>');
125 fatal ("unexpected end of input");
128 else if (isdigit (c) || c == '-')
137 while (isdigit (c) || isalpha (c) || c == '.');
142 if (strchr (tok_string, '.') == NULL)
145 tok_integer = strtoul (tok_string, &tail, 0);
150 tok_float = strtod (tok_string, &tail);
153 fatal ("invalid numeric syntax");
158 while ((c = getc (input)) != '"')
161 fatal ("new-line inside string");
173 else if (isalpha (c))
180 while (isdigit (c) || isalpha (c) || c == '.');
184 if (!strcmp (tok_string, "i8"))
186 else if (tok_string[0] == 's')
189 tok_integer = atoi (tok_string + 1);
191 else if (!strcmp (tok_string, "SYSMIS"))
194 tok_float = -DBL_MAX;
196 else if (!strcmp (tok_string, "LOWEST"))
199 tok_float = float_get_lowest ();
201 else if (!strcmp (tok_string, "HIGHEST"))
206 else if (!strcmp (tok_string, "ENDIAN"))
209 tok_integer = integer_format == INTEGER_MSB_FIRST ? 1 : 2;
211 else if (!strcmp (tok_string, "COUNT"))
214 fatal ("invalid token `%s'", tok_string);
217 fatal ("invalid input byte `%c'", c);
221 buffer_put (struct buffer *buffer, const void *data, size_t n)
223 memcpy (buffer_put_uninit (buffer, n), data, n);
227 buffer_put_uninit (struct buffer *buffer, size_t n)
230 if (buffer->size > buffer->allocated)
232 buffer->allocated = buffer->size * 2;
233 buffer->data = xrealloc (buffer->data, buffer->allocated);
235 return &buffer->data[buffer->size - n];
242 %s, SAv Construction Kit\n\
243 usage: %s [OPTIONS] INPUT\n\
245 --be big-endian output format (default)\n\
246 --le little-endian output format\n\
247 --help print this help message and exit\n\
249 The input is a sequence of data items, each followed by a semicolon.\n\
250 Each data item is converted to the output format and written on\n\
251 stdout. A data item is one of the following\n\
253 - An integer in decimal, in hexadecimal prefixed by 0x, or in octal\n\
254 prefixed by 0. Output as a 32-bit binary integer.\n\
256 - A floating-point number. Output in 64-bit IEEE 754 format.\n\
258 - A string enclosed in double quotes. Output literally. There is\n\
259 no syntax for \"escapes\". Strings may not contain new-lines.\n\
261 - A literal of the form s<number> followed by a quoted string as\n\
262 above. Output as the string's contents followed by enough spaces\n\
263 to fill up <number> bytes. For example, s8 \"foo\" is output as\n\
264 the \"foo\" followed by 5 spaces.\n\
266 - The literal \"i8\" followed by an integer. Output as a single\n\
267 byte with the specified value.\n\
269 - One of the literals SYSMIS, LOWEST, or HIGHEST. Output as a\n\
270 64-bit IEEE 754 float of the appropriate PSPP value.\n\
272 - The literal ENDIAN. Output as a 32-bit binary integer, either\n\
273 with value 1 if --be is in effect or 2 if --le is in effect.\n\
275 - A pair of parentheses enclosing a sequence of data items, each\n\
276 followed by a semicolon (the last semicolon is optional).\n\
277 Output as the enclosed data items in sequence.\n\
279 - The literal COUNT followed by a sequence of parenthesized data\n\
280 items, as above. Output as a 32-bit binary integer whose value\n\
281 is the number of bytes enclosed within the parentheses, followed\n\
282 by the enclosed data items themselves.\n\
284 optionally followed by an asterisk and a positive integer, which\n\
285 specifies a repeat count for the data item.\n\
287 The md5sum of the data written to stdout is written to stderr as\n\
288 16 hexadecimal digits followed by a new-line.\n",
289 program_name, program_name);
294 parse_options (int argc, char **argv)
299 OPT_BE = UCHAR_MAX + 1,
303 static const struct option options[] =
305 {"be", no_argument, NULL, OPT_BE},
306 {"le", no_argument, NULL, OPT_LE},
307 {"help", no_argument, NULL, OPT_HELP},
311 int c = getopt_long (argc, argv, "", options, NULL);
318 float_format = FLOAT_IEEE_DOUBLE_BE;
319 integer_format = INTEGER_MSB_FIRST;
323 float_format = FLOAT_IEEE_DOUBLE_LE;
324 integer_format = INTEGER_LSB_FIRST;
343 if (optind + 1 != argc)
344 error (1, 0, "exactly one non-option argument required; "
345 "use --help for help");
350 parse_data_item (struct buffer *output)
352 size_t old_size = output->size;
354 if (token == T_INTEGER)
356 integer_put (tok_integer, integer_format,
357 buffer_put_uninit (output, 4), 4);
360 else if (token == T_FLOAT)
362 float_convert (FLOAT_NATIVE_DOUBLE, &tok_float,
363 float_format, buffer_put_uninit (output, 8));
366 else if (token == T_I8)
373 if (token != T_INTEGER)
374 fatal ("integer expected after `i8'");
376 buffer_put (output, &byte, 1);
379 while (token == T_INTEGER);
381 else if (token == T_STRING)
383 buffer_put (output, tok_string, tok_strlen);
386 else if (token == T_S)
393 if (token != T_STRING)
394 fatal ("string expected");
396 fatal ("%zu-byte string is longer than pad length %d",
399 buffer_put (output, tok_string, tok_strlen);
400 memset (buffer_put_uninit (output, n - tok_strlen), ' ',
404 else if (token == T_LPAREN)
408 while (token != T_RPAREN)
409 parse_data_item (output);
413 else if (token == T_COUNT)
415 buffer_put_uninit (output, 4);
418 if (token != T_LPAREN)
419 fatal ("`(' expected after COUNT");
422 while (token != T_RPAREN)
423 parse_data_item (output);
426 integer_put (output->size - old_size - 4, integer_format,
427 output->data + old_size, 4);
430 fatal ("syntax error");
432 if (token == T_ASTERISK)
434 size_t n = output->size - old_size;
439 if (token != T_INTEGER || tok_integer < 1)
440 fatal ("positive integer expected after `*'");
441 p = buffer_put_uninit (output, (tok_integer - 1) * n);
442 while (--tok_integer > 0)
444 memcpy (p, output->data + old_size, n);
451 if (token == T_SEMICOLON)
453 else if (token != T_RPAREN)
454 fatal ("`;' expected");
458 main (int argc, char **argv)
460 struct buffer output;
464 set_program_name (argv[0]);
465 input_file_name = parse_options (argc, argv);
467 if (!strcmp (input_file_name, "-"))
471 input = fopen (input_file_name, "r");
473 error (1, errno, "%s: open failed", input_file_name);
476 if (isatty (STDOUT_FILENO))
477 error (1, 0, "not writing binary data to a terminal; redirect to a file");
481 output.allocated = 0;
485 while (token != T_EOF)
486 parse_data_item (&output);
491 fwrite (output.data, output.size, 1, stdout);
493 md5_buffer ((const char *) output.data, output.size, digest);
494 for (i = 0; i < sizeof digest; i++)
495 fprintf (stderr, "%02x", digest[i]);