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/>. */
19 #include "libpspp/line-reader.h"
28 #include "libpspp/i18n.h"
29 #include "libpspp/str.h"
32 #include "gl/progname.h"
33 #include "gl/xalloc.h"
38 printf ("usage: %s COMMAND [ARG]...\n"
39 "The available commands are:\n"
41 " print this usage message\n"
43 " print the buffer size, in bytes, on stdout\n"
44 " read FILE ENCODING\n"
45 " read FILE encoded in ENCODING and print it in UTF-8\n",
51 cmd_read (int argc
, char *argv
[])
53 struct line_reader
*r
;
58 error (1, 0, "bad syntax for `%s' command; use `%s help' for help",
59 argv
[1], program_name
);
63 r
= (!strcmp(filename
, "-")
64 ? line_reader_for_fd (argv
[3], STDIN_FILENO
)
65 : line_reader_for_file (argv
[3], filename
, O_RDONLY
));
67 error (1, errno
, "line_reader_open failed");
69 char *encoding
= xstrdup (line_reader_get_encoding (r
));
70 printf ("encoded in %s", encoding
);
71 if (line_reader_is_auto (r
))
75 ds_init_empty (&line
);
76 while (line_reader_read (r
, &line
, SIZE_MAX
))
78 const char *new_encoding
;
81 new_encoding
= line_reader_get_encoding (r
);
82 if (strcmp (encoding
, new_encoding
))
85 encoding
= xstrdup (new_encoding
);
87 printf ("encoded in %s", encoding
);
88 if (line_reader_is_auto (r
))
93 utf8_line
= recode_string ("UTF-8", encoding
,
94 ds_data (&line
), ds_length (&line
));
95 printf ("\"%s\"\n", utf8_line
);
103 if (!strcmp(filename
, "-"))
104 line_reader_free (r
);
107 if (line_reader_close (r
) != 0)
108 error (1, errno
, "line_reader_close failed");
113 main (int argc
, char *argv
[])
115 set_program_name (argv
[0]);
119 error (1, 0, "missing command name; use `%s help' for help", program_name
);
120 else if (!strcmp(argv
[1], "help") || !strcmp(argv
[1], "--help"))
122 else if (!strcmp(argv
[1], "buffer-size"))
123 printf ("%d\n", LINE_READER_BUFFER_SIZE
);
124 else if (!strcmp(argv
[1], "read"))
125 cmd_read (argc
, argv
);
127 error (1, 0, "unknown command `%s'; use `%s help' for help",
128 argv
[1], program_name
);