File: core_shntool.c

package info (click to toggle)
shntool 3.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,220 kB
  • ctags: 1,118
  • sloc: ansic: 8,515; sh: 3,761; makefile: 36
file content (371 lines) | stat: -rw-r--r-- 10,027 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*  core_shntool.c - functions to handle mode verification and execution
 *  Copyright (C) 2000-2009  Jason Jordan <shnutils@freeshell.org>
 *
 *  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 2
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <string.h>
#include <signal.h>
#include "shntool.h"

CVSID("$Id: core_shntool.c,v 1.90 2009/03/16 04:46:03 jason Exp $")

private_opts st_priv;
input_files st_input;

void st_version()
{
  st_info(
          "%s%s " RELEASE "\n"
          COPYRIGHT " " AUTHOR "\n"
          "\n"
          "shorten utilities pages:\n"
          "\n"
          "  " URL1 "\n"
          "  " URL2 "\n"
          "\n"
          "This is free software.  You may redistribute copies of it under the terms of\n"
          "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
          "There is NO WARRANTY, to the extent permitted by law.\n"
          "\n"
          ,st_priv.fullprogname,(st_priv.progmode)?" mode module":""
         );
}

static void show_supported_mode_modules()
{
  int i;

  st_info("%s %s supported modes:\n",st_priv.fullprogname,RELEASE);
  st_info("\n");
  st_info("   mode  description\n");
  st_info("   ----  -----------\n");
  for (i=0;st_modes[i];i++)
    st_info("  %5s  %s\n",st_modes[i]->name,st_modes[i]->description);
  st_info("\n");
  st_info("  For help with any of the above modes, type:  '%s mode -h'\n",st_priv.progname);
  st_info("\n");
}

static void show_supported_format_modules()
{
  int i;
  char tmp[BUF_SIZE];

  st_info("%s %s supported file formats:\n",st_priv.fullprogname,RELEASE);
  st_info("\n");

  st_info(" format    ext     input    output  description\n");
  st_info(" ------    ---     -----    ------  -----------\n");
  for (i=0;st_formats[i];i++) {
    st_info("  %5s",st_formats[i]->name);

    if (st_formats[i]->supports_output && st_formats[i]->extension) {
      st_snprintf(tmp,BUF_SIZE,".%s",st_formats[i]->extension);
      st_info("  %5s",tmp);
    }
    else
      st_info("      -");

    if (st_formats[i]->supports_input) {
      if (st_formats[i]->decoder) {
        st_info("  %8s",st_formats[i]->decoder);
      }
      else {
        st_info("   shntool");
      }
    }
    else {
      st_info("         -");
    }

    if (st_formats[i]->supports_output) {
      if (st_formats[i]->encoder) {
        st_info("  %8s",st_formats[i]->encoder);
      }
      else {
        st_info("   shntool");
      }
    }
    else {
      st_info("         -");
    }

    st_info("  %s\n",st_formats[i]->description);
  }

  st_info("\n");
}

static void show_format_module_args()
{
  int i,showed_format;

  st_info("%s %s default file format arguments:\n",st_priv.fullprogname,RELEASE);
  st_info("\n");

  st_info(" format    action   arguments\n");
  st_info(" ------    ------   ---------\n");

  for (i=0;st_formats[i];i++) {

    if (!st_formats[i]->decoder && !st_formats[i]->encoder) {
      continue;
    }

    showed_format = 0;

    if (st_formats[i]->decoder) {
      st_info("  %5s  decoding: ",(showed_format)?"":st_formats[i]->name);
      st_info(" -i \"%s %s %s\"",st_formats[i]->name,st_formats[i]->decoder,st_formats[i]->decoder_args);
      st_info("\n");
      showed_format = 1;
    }

    if (st_formats[i]->encoder) {
      st_info("  %5s  encoding: ",(showed_format)?"":st_formats[i]->name);
      st_info(" -o \"%s ext=%s %s %s\"",st_formats[i]->name,st_formats[i]->extension,st_formats[i]->encoder,st_formats[i]->encoder_args);
      st_info("\n");
      showed_format = 1;
    }

  }

  st_info("\n");
}

static void core_help()
{
  st_info("Usage: %s mode ...\n",st_priv.progname);
  st_info("       %s [OPTIONS]\n",st_priv.progname);
  st_info("\n");
  st_info("Options:\n");
  st_info("\n");
  st_info("  -m      show detailed mode module information\n");
  st_info("  -f      show detailed format module information\n");
  st_info("  -a      show default format module arguments\n");
  st_info("  -v      show version information\n");
  st_info("  -h      show this help screen\n");
  st_info("\n");
}

static void module_sanity_check()
{
  int i,j;

  if (st_modes[0]) {
    for (i=0;st_modes[i];i++) {
      if (NULL == st_modes[i]->name)
        st_error("found a mode module without a name");

      if (NULL == st_modes[i]->description)
        st_error("mode module has no description: [%s]",st_modes[i]->name);

      if (NULL == st_modes[i]->run_main)
        st_error("mode module does not provide run_main(): [%s]",st_modes[i]->name);
    }

    for (i=0;st_modes[i];i++) {
      for (j=i+1;st_modes[j];j++) {
        if (!strcmp(st_modes[i]->name,st_modes[j]->name))
          st_error("found modes with identical name: [%s]",st_modes[i]->name);
      }
    }
  }
  else
    st_error("no mode modules found");

  if (st_formats[0]) {
    for (i=0;st_formats[i];i++) {
      if (NULL == st_formats[i]->name)
        st_error("found a format module without a name");

      if (NULL == st_formats[i]->description)
        st_error("format module has no description: [%s]",st_formats[i]->name);

      if (!st_formats[i]->supports_input && !st_formats[i]->supports_output)
        st_error("format module doesn't support input or output: [%s]",st_formats[i]->name);

      if (!st_formats[i]->supports_input && st_formats[i]->decoder)
        st_error("format module doesn't support input but defines a decoder: [%s]",st_formats[i]->name);

      if (!st_formats[i]->supports_output && st_formats[i]->encoder)
        st_error("format module doesn't support output but defines an encoder: [%s]",st_formats[i]->name);
    }

    for (i=0;st_formats[i];i++) {
      for (j=i+1;st_formats[j];j++) {
        if (!strcmp(st_formats[i]->name,st_formats[j]->name))
          st_error("found formats with identical name: [%s]",st_formats[i]->name);
      }
    }
  }
  else {
    st_error("no file formats modules found");
  }
}

static bool parse_main(int argc,char **argv)
{
  int i,j;
  int c;

  /* look for a module alias matching progname */

  for (i=0;st_modes[i];i++) {
    if (NULL != st_modes[i]->alias && !strcmp(st_priv.progname,st_modes[i]->alias)) {
      st_priv.mode = st_modes[i];
      st_priv.is_aliased = TRUE;
      st_priv.progmode = st_modes[i]->name;
      return st_modes[i]->run_main(argc,argv);
    }
  }

  /* failing that, look for a module name matching argv[1] */

  if (argc < 2)
    st_help("missing arguments");

  for (i=0;st_modes[i];i++) {
    if (!strcmp(argv[1],st_modes[i]->name)) {
      /* found mode - now run it and quit */
      st_priv.mode = st_modes[i];
      st_priv.progmode = st_modes[i]->name;
      strcat(st_priv.fullprogname," ");
      strcat(st_priv.fullprogname,st_priv.progmode);

      /* remove mode name from arg list (is this portable?) */
      for (j=1;j<argc-1;j++)
        argv[j] = argv[j+1];
      argv[argc-1] = NULL;
      argc--;

      return st_modes[i]->run_main(argc,argv);
    }
  }

  while ((c=getopt(argc,argv,GLOBAL_OPTS_CORE)) != -1) {
    switch (c) {
      case 'a':
        show_format_module_args();
        exit(ST_EXIT_SUCCESS);
        break;
      case 'f':
        show_supported_format_modules();
        exit(ST_EXIT_SUCCESS);
        break;
      case 'j':
        st_info("Guru Meditashn #00000002.9091968B\n");
        exit(ST_EXIT_SUCCESS);
        break;
      case 'm':
        show_supported_mode_modules();
        exit(ST_EXIT_SUCCESS);
        break;
      case '?':
      case 'h':
        if ('?' == c)
          st_info("\n");
        core_help();
        exit(('?' == c) ? ST_EXIT_ERROR : ST_EXIT_SUCCESS);
        break;
      case 'v':
        st_version();
        exit(ST_EXIT_SUCCESS);
        break;
    }
  }

  if (optind < argc)
    st_help("invalid mode: [%s]",argv[optind]);

  return FALSE;
}

static void modules_init()
{
  int i;

  /* sanity checks for modules */
  module_sanity_check();

  /* initialize format modules */
  for (i=0;st_formats[i];i++) {
    arg_init(st_formats[i]);
  }
}

static void globals_init(char *program)
{
  char *p;
  int n;

#ifdef WIN32
  setvbuf(stdout,NULL,_IONBF,0);
#else
  signal(SIGPIPE,SIG_IGN);
#endif

  /* public globals */
  st_ops.output_directory = ".";
  st_ops.output_prefix = "";
  st_ops.output_postfix = "";
  st_ops.output_format = NULL;

  /* private globals */
  st_priv.progname = ((p = strrchr(program,PATHSEPCHAR))) ? (p + 1) : program;
  if ((p = extname(st_priv.progname)))
    *(p-1) = 0;
  strcpy(st_priv.fullprogname,st_priv.progname);
  st_priv.progmode = NULL;
  st_priv.clobber_action = CLOBBER_ACTION_ASK;
  st_priv.reorder_type = ORDER_NATURAL;
  st_priv.progress_type = PROGRESS_PERCENT;
  st_priv.is_aliased = FALSE;
  st_priv.show_hmmss = FALSE;
  st_priv.suppress_warnings = FALSE;
  st_priv.suppress_stderr = FALSE;
  st_priv.screen_dirty = FALSE;

  st_input.type = INPUT_CMDLINE;
  st_input.filename_source = NULLDEVICE;
  st_input.fd = NULL;
  st_input.argn = 0;
  st_input.argc = 0;
  st_input.argv = NULL;
  st_input.filecur = 0;
  st_input.filemax = 0;

  p = scan_env(SHNTOOL_DEBUG_ENV);
  n = p ? atoi(p) : 0;

  st_priv.debug_level = (n > 0) ? n : 0;
}

int main(int argc,char **argv)
{
  bool success;

  /* initialize global variables */
  globals_init(argv[0]);

  /* initialize modules */
  modules_init();

  /* parse command line */
  success = parse_main(argc,argv);

  return (success) ? ST_EXIT_SUCCESS : ST_EXIT_ERROR;
}