File: recinf.c

package info (click to toggle)
recutils 1.5-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 10,212 kB
  • sloc: ansic: 55,227; sh: 16,411; lisp: 1,431; yacc: 1,373; makefile: 304; lex: 223; sed: 16
file content (282 lines) | stat: -rw-r--r-- 6,587 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* -*- mode: C -*-
 *
 *       File:         recinf.c
 *       Date:         Mon Dec 28 08:54:38 2009
 *
 *       GNU recutils - recinf
 *
 */

/* Copyright (C) 2009, 2010, 2011 Jose E. Marchesi */

/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <xalloc.h>
#include <gettext.h>
#define _(str) gettext (str)

#include <rec.h>
#include <recutl.h>

/*
 * Global variables
 */

rec_writer_mode_t recinf_write_mode = REC_WRITER_NORMAL;

/*
 * Command line options management
 */

enum
{
  COMMON_ARGS,
  DESCRIPTOR_ARG,
  NAMES_ARG,
  TYPE_ARG,
  PRINT_SEXPS_ARG
};

static const struct option GNU_longOptions[] =
  {
    COMMON_LONG_ARGS,
    {"descriptor", no_argument, NULL, DESCRIPTOR_ARG},
    {"names-only", no_argument, NULL, NAMES_ARG},
    {"type", required_argument, NULL, TYPE_ARG},
    {"print-sexps", no_argument, NULL, PRINT_SEXPS_ARG},
    {NULL, 0, NULL, 0}
  };

/*
 * Global variables.
 */

bool recinf_descriptor = false;
bool recinf_names_only = false;
char *recinf_type = NULL;

/*
 * Functions.
 */

void
recutl_print_help (void)
{
  /* TRANSLATORS: --help output, recinf synopsis.
     no-wrap */
  printf (_("\
Usage: recinf [OPTION]... [FILE]...\n"));

  /* TRANSLATORS: --help output, recinf short description.
     no-wrap */
  fputs (_("\
Print information about the types of records stored in the input.\n"),
         stdout);

  puts ("");
  /* TRANSLATORS: --help output, recinf arguments.
     no-wrap */
  fputs (_("\
  -t, --type=RECORD_TYPE          print information on the records having the\n\
                                    specified type.\n\
  -d, --descriptor                include the full record descriptors.\n\
  -n, --names-only                output just the names of the record files\n\
                                    found in the input.\n"),
         stdout);

  recutl_print_help_common ();

  puts ("");
  /* TRANSLATORS: --help output, recinf special options.
     no-wrap */
  fputs (_("\
Special options:\n\
  -S, --print-sexps                   print the data in sexps instead of rec format.\n"),
         stdout);
  
  puts ("");
  recutl_print_help_footer ();
}

bool
print_info_file (FILE *in,
                 char *file_name)
{
  bool ret;
  rec_db_t db;
  rec_rset_t rset;
  rec_record_t descriptor;
  rec_parser_t parser;
  int position;

  ret = true;
  parser = rec_parser_new (in, file_name);
  if (rec_parse_db (parser, &db))
    {
      for (position = 0; position < rec_db_size (db); position++)
        {
          rset = rec_db_get_rset (db, position);
          descriptor = rec_rset_descriptor (rset);

          if (recinf_type
              && descriptor
              && (strcmp (rec_rset_type (rset), recinf_type) != 0))
            {
              continue;
            }

          if (recinf_descriptor)
            {
              rec_writer_t writer;
              
              if (descriptor)
                {
                  writer = rec_writer_new (stdout);
                  rec_write_record (writer, descriptor, recinf_write_mode);
                  rec_writer_destroy (writer);
                }
              else
                {
                  if (recinf_write_mode == REC_WRITER_NORMAL)
                    {
                      printf ("unknown\n");
                    }
                }

              if (position < (rec_db_size (db) - 1))
                {
                  printf ("\n");
                }
            }
          else
            {
              if (descriptor)
                {
                  if (!recinf_names_only)
                    {
                      fprintf (stdout, "%zd ", rec_rset_num_records (rset));
                    }
                  fprintf (stdout, "%s\n", rec_rset_type (rset));
                }
              else
                {
                  if (!recinf_names_only)
                    {
                      printf ("%zd\n", rec_rset_num_records (rset));
                    }
                }
            }          
        }
    }
  
  if (rec_parser_error (parser))
    {
      rec_parser_perror (parser, file_name);
    }

  rec_parser_destroy (parser);

  return ret;
}

int
main (int argc, char *argv[])
{
  char c;
  int ret;
  char *file_name;
  FILE *in;

  recutl_init ("recinf");

  while ((ret = getopt_long (argc,
                             argv,
                             "Sdnt:",
                             GNU_longOptions,
                             NULL)) != -1)
    {
      c = ret;
      switch (c)
        {
          COMMON_ARGS_CASES
        case PRINT_SEXPS_ARG:
        case 'S':
          {
            recinf_write_mode = REC_WRITER_SEXP;
            break;
          }
        case DESCRIPTOR_ARG:
        case 'd':
          {
            recinf_descriptor = true;
            break;
          }
        case NAMES_ARG:
        case 'n':
          {
            recinf_names_only = true;
            break;
          }
        case TYPE_ARG:
        case 't':
          {
            recinf_type = xstrdup (optarg);
            break;
          }
        default:
          {
            exit (EXIT_FAILURE);
          }
        }
    }

  /* Process the input files, if any.  Otherwise use the standard
     input to read the rec data. */
  if (optind < argc)
    {
      while (optind < argc)
        {
          file_name = argv[optind++];
          if (!(in = fopen (file_name, "r")))
            {
              printf(_("error: cannot read file %s\n"), file_name);
              exit (EXIT_FAILURE);
            }
          else
            {
              if (!print_info_file (in, file_name))
                {
                  /* Parse error */
                  exit (EXIT_FAILURE);
                }

              fclose (in);
            }
        }
    }
  else
    {
      print_info_file (stdin, "stdin");
    }
  
  return EXIT_SUCCESS;
}

/* End of recinf.c */