update devspec.en_US/1.0.general.md.
[devspec.git] / devspec.en_US / project / recutils / utils / recinf.c
bloba875b54889f8c7ebb334c1e8a6e256e40366fe9f
1 /* -*- mode: C -*-
3 * File: recinf.c
4 * Date: Mon Dec 28 08:54:38 2009
6 * GNU recutils - recinf
8 */
10 /* Copyright (C) 2009-2019 Jose E. Marchesi */
12 /* This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <getopt.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <xalloc.h>
32 #include <gettext.h>
33 #define _(str) gettext (str)
35 #include <rec.h>
36 #include <recutl.h>
39 * Global variables
42 rec_writer_mode_t recinf_write_mode = REC_WRITER_NORMAL;
45 * Command line options management
48 enum
50 COMMON_ARGS,
51 DESCRIPTOR_ARG,
52 NAMES_ARG,
53 TYPE_ARG,
54 PRINT_SEXPS_ARG
57 static const struct option GNU_longOptions[] =
59 COMMON_LONG_ARGS,
60 {"descriptor", no_argument, NULL, DESCRIPTOR_ARG},
61 {"names-only", no_argument, NULL, NAMES_ARG},
62 {"type", required_argument, NULL, TYPE_ARG},
63 {"print-sexps", no_argument, NULL, PRINT_SEXPS_ARG},
64 {NULL, 0, NULL, 0}
68 * Global variables.
71 bool recinf_descriptor = false;
72 bool recinf_names_only = false;
73 char *recinf_type = NULL;
76 * Functions.
79 void
80 recutl_print_help (void)
82 /* TRANSLATORS: --help output, recinf synopsis.
83 no-wrap */
84 printf (_("\
85 Usage: recinf [OPTION]... [FILE]...\n"));
87 /* TRANSLATORS: --help output, recinf short description.
88 no-wrap */
89 fputs (_("\
90 Print information about the types of records stored in the input.\n"),
91 stdout);
93 puts ("");
94 /* TRANSLATORS: --help output, recinf arguments.
95 no-wrap */
96 fputs (_("\
97 -t, --type=RECORD_TYPE print information on the records having the\n\
98 specified type.\n\
99 -d, --descriptor include the full record descriptors.\n\
100 -n, --names-only output just the names of the record files\n\
101 found in the input.\n"),
102 stdout);
104 recutl_print_help_common ();
106 puts ("");
107 /* TRANSLATORS: --help output, recinf special options.
108 no-wrap */
109 fputs (_("\
110 Special options:\n\
111 -S, --print-sexps print the data in sexps instead of rec format.\n"),
112 stdout);
114 puts ("");
115 recutl_print_help_footer ();
118 bool
119 print_info_file (FILE *in,
120 char *file_name)
122 bool ret;
123 rec_db_t db;
124 rec_rset_t rset;
125 rec_record_t descriptor;
126 rec_parser_t parser;
127 int position;
129 ret = true;
130 parser = rec_parser_new (in, file_name);
132 ret = rec_parse_db (parser, &db);
133 if (ret)
135 for (position = 0; position < rec_db_size (db); position++)
137 rset = rec_db_get_rset (db, position);
138 descriptor = rec_rset_descriptor (rset);
140 if (recinf_type
141 && descriptor
142 && (strcmp (rec_rset_type (rset), recinf_type) != 0))
144 continue;
147 if (recinf_descriptor)
149 rec_writer_t writer;
151 if (descriptor)
153 writer = rec_writer_new (stdout);
154 rec_writer_set_mode (writer, recinf_write_mode);
155 rec_write_record (writer, descriptor);
156 rec_write_string (writer, "\n");
157 rec_writer_destroy (writer);
159 else
161 if (recinf_write_mode == REC_WRITER_NORMAL)
163 printf ("unknown\n");
167 if (position < (rec_db_size (db) - 1))
169 printf ("\n");
172 else
174 if (descriptor)
176 if (!recinf_names_only)
178 fprintf (stdout, "%zd ", rec_rset_num_records (rset));
180 fprintf (stdout, "%s\n", rec_rset_type (rset));
182 else
184 if (!recinf_names_only)
186 printf ("%zd\n", rec_rset_num_records (rset));
193 if (rec_parser_error (parser))
195 rec_parser_perror (parser, file_name);
198 rec_parser_destroy (parser);
200 return ret;
204 main (int argc, char *argv[])
206 char c;
207 int ret;
208 char *file_name;
209 FILE *in;
211 recutl_init ("recinf");
213 while ((ret = getopt_long (argc,
214 argv,
215 "Sdnt:",
216 GNU_longOptions,
217 NULL)) != -1)
219 c = ret;
220 switch (c)
222 COMMON_ARGS_CASES
223 case PRINT_SEXPS_ARG:
224 case 'S':
226 recinf_write_mode = REC_WRITER_SEXP;
227 break;
229 case DESCRIPTOR_ARG:
230 case 'd':
232 recinf_descriptor = true;
233 break;
235 case NAMES_ARG:
236 case 'n':
238 recinf_names_only = true;
239 break;
241 case TYPE_ARG:
242 case 't':
244 recinf_type = xstrdup (optarg);
245 break;
247 default:
249 exit (EXIT_FAILURE);
254 /* Process the input files, if any. Otherwise use the standard
255 input to read the rec data. */
256 if (optind < argc)
258 while (optind < argc)
260 file_name = argv[optind++];
261 if (!(in = fopen (file_name, "r")))
263 printf(_("error: cannot read file %s\n"), file_name);
264 exit (EXIT_FAILURE);
266 else
268 if (!print_info_file (in, file_name))
270 /* Parse error */
271 exit (EXIT_FAILURE);
274 fclose (in);
278 else
280 if (!print_info_file (stdin, "stdin"))
282 /* Parse error */
283 exit (EXIT_FAILURE);
287 return EXIT_SUCCESS;
290 /* End of recinf.c */