4 * Date: Mon Dec 28 08:54:38 2009
6 * GNU recutils - recinf
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/>.
33 #define _(str) gettext (str)
42 rec_writer_mode_t recinf_write_mode
= REC_WRITER_NORMAL
;
45 * Command line options management
57 static const struct option GNU_longOptions
[] =
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
},
71 bool recinf_descriptor
= false;
72 bool recinf_names_only
= false;
73 char *recinf_type
= NULL
;
80 recutl_print_help (void)
82 /* TRANSLATORS: --help output, recinf synopsis.
85 Usage: recinf [OPTION]... [FILE]...\n"));
87 /* TRANSLATORS: --help output, recinf short description.
90 Print information about the types of records stored in the input.\n"),
94 /* TRANSLATORS: --help output, recinf arguments.
97 -t, --type=RECORD_TYPE print information on the records having the\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"),
104 recutl_print_help_common ();
107 /* TRANSLATORS: --help output, recinf special options.
111 -S, --print-sexps print the data in sexps instead of rec format.\n"),
115 recutl_print_help_footer ();
119 print_info_file (FILE *in
,
125 rec_record_t descriptor
;
130 parser
= rec_parser_new (in
, file_name
);
132 ret
= rec_parse_db (parser
, &db
);
135 for (position
= 0; position
< rec_db_size (db
); position
++)
137 rset
= rec_db_get_rset (db
, position
);
138 descriptor
= rec_rset_descriptor (rset
);
142 && (strcmp (rec_rset_type (rset
), recinf_type
) != 0))
147 if (recinf_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
);
161 if (recinf_write_mode
== REC_WRITER_NORMAL
)
163 printf ("unknown\n");
167 if (position
< (rec_db_size (db
) - 1))
176 if (!recinf_names_only
)
178 fprintf (stdout
, "%zd ", rec_rset_num_records (rset
));
180 fprintf (stdout
, "%s\n", rec_rset_type (rset
));
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
);
204 main (int argc
, char *argv
[])
211 recutl_init ("recinf");
213 while ((ret
= getopt_long (argc
,
223 case PRINT_SEXPS_ARG
:
226 recinf_write_mode
= REC_WRITER_SEXP
;
232 recinf_descriptor
= true;
238 recinf_names_only
= true;
244 recinf_type
= xstrdup (optarg
);
254 /* Process the input files, if any. Otherwise use the standard
255 input to read the rec data. */
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
);
268 if (!print_info_file (in
, file_name
))
280 if (!print_info_file (stdin
, "stdin"))
290 /* End of recinf.c */