2
* Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3
* Universitaet Berlin. See the accompanying file "COPYRIGHT" for
4
* details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
7
/* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/toast.c,v 1.8 1996/07/02 10:41:04 jutta Exp $ */
11
/* toast -- lossy sound compression using the gsm library.
16
int f_decode = 0; /* decode rather than encode (-d) */
17
int f_cat = 0; /* write to stdout; implies -p (-c) */
18
int f_force = 0; /* don't ask about replacements (-f) */
19
int f_precious = 0; /* avoid deletion of original (-p) */
20
int f_fast = 0; /* use faster fpt algorithm (-F) */
21
int f_verbose = 0; /* debugging (-V) */
22
int f_ltp_cut = 0; /* LTP cut-off margin (-C) */
24
struct stat instat; /* stat (inname) */
27
char *inname, *outname;
30
* The function (*output)() writes a frame of 160 samples given as
31
* 160 signed 16 bit values (gsm_signals) to <out>.
32
* The function (*input)() reads one such frame from <in>.
33
* The function (*init_output)() begins output (e.g. writes a header).,
34
* The function (*init_input)() begins input (e.g. skips a header).
36
* There are different versions of input, output, init_input and init_output
37
* for different formats understood by toast; which ones are used
38
* depends on the command line arguments and, in their absence, the
39
* filename; the fallback is #defined in toast.h
41
* The specific implementations of input, output, init_input and init_output
42
* for a format `foo' live in toast_foo.c.
45
int (*output ) P((gsm_signal *)),
46
(*input ) P((gsm_signal *));
47
int (*init_input) P((void)),
48
(*init_output) P((void));
50
static int generic_init P0() { return 0; } /* NOP */
54
char * name, * longname, * suffix;
56
int (* init_input ) P((void)),
57
(* init_output) P((void));
59
int (* input ) P((gsm_signal * )),
60
(* output) P((gsm_signal * ));
64
"8 kHz, 8 bit u-law encoding with Sun audio header", ".au",
70
"u-law", "plain 8 kHz, 8 bit u-law encoding", ".u",
77
"A-law", "8 kHz, 8 bit A-law encoding", ".A",
85
"16 bit (13 significant) signed 8 kHz signal", ".l",
92
struct fmtdesc * alldescs[] = {
97
(struct fmtdesc *)NULL
100
#define DEFAULT_FORMAT f_ulaw /* default audio format, others */
101
/* are: f_alaw,f_audio,f_linear */
102
struct fmtdesc * f_format = 0;
105
* basename + suffix of a pathname
107
static char * endname P1((name), char * name)
110
char * s = strrchr(name, '/');
111
if (s && s[1]) name = s + 1;
118
* Try to figure out what we're supposed to do from the argv[0], if
119
* any, and set the parameters accordingly.
121
static void parse_argv0 P1((av0), char * av0 )
125
progname = av0 = endname(av0 ? av0 : "toast");
127
/* If the name starts with `un', we want to decode, not code.
128
* If the name ends in `cat', we want to write to stdout,
129
* and decode as well.
132
if (!strncmp(av0, "un", 2)) f_decode = 1;
133
if ( (l = strlen(av0)) >= 3 /* strlen("cat") */
134
&& !strcmp( av0 + l - 3, "cat" )) f_cat = f_decode = 1;
139
* Check whether the name (possibly generated by appending
140
* .gsm to something else) is short enough for this system.
142
static int length_okay P1((name), char * name)
144
long max_filename_length = 0;
147
/* If our _pathname_ is too long, we'll usually not be
148
* able to open the file at all -- don't worry about that.
150
* But if the _filename_ is too long, there is danger of
151
* silent truncation on some systems, which results
152
* in the target replacing the source!
159
max_filename_length = NAME_MAX;
167
if ((s = end) > name) {
168
if (s > name + 1) s--;
174
max_filename_length = pathconf(s > name ? name : ".",
176
if (max_filename_length == -1 && errno) {
177
perror( s > name ? name : "." );
179
"%s: cannot get dynamic filename length limit for %s.\n",
180
progname, s > name ? name : ".");
183
if (s > name) *s = tmp;
185
#endif /* USE_PATHCONF */
186
#endif /* _PC_NAME_MAX */
187
#endif /* !NAME_MAX */
189
if (max_filename_length > 0 && strlen(end) > max_filename_length) {
191
"%s: filename \"%s\" is too long (maximum is %ld)\n",
192
progname, endname(name), max_filename_length );
200
* Return a pointer the suffix of a string, if any.
201
* A suffix alone has no suffix, an empty suffix can not be had.
203
static char * suffix P2((name, suf), char *name, char * suf)
205
size_t nlen = strlen(name);
206
size_t slen = strlen(suf);
208
if (!slen || nlen <= slen) return (char *)0;
210
return memcmp(name, suf, slen) ? (char *)0 : name;
214
static void catch_signals P1((fun), SIGHANDLER_T (*fun) ())
217
signal( SIGHUP, fun );
220
signal( SIGINT, fun );
223
signal( SIGPIPE, fun );
226
signal( SIGTERM, fun );
229
signal( SIGXFSZ, fun );
233
static SIGHANDLER_T onintr P0()
235
char * tmp = outname;
237
#ifdef HAS_SYSV_SIGNALS
238
catch_signals( SIG_IGN );
242
if (tmp) (void)unlink(tmp);
248
* Allocate some memory and complain if it fails.
250
static char * emalloc P1((len), size_t len)
253
if (!(s = malloc(len))) {
254
fprintf(stderr, "%s: failed to malloc %d bytes -- abort\n",
262
static char* normalname P3((name, want, cut), char *name, char *want,char *cut)
270
maxlen = strlen(name) + 1 + strlen(want) + strlen(cut);
271
p = strcpy(emalloc(maxlen), name);
273
if (s = suffix(p, cut)) strcpy(s, want);
274
else if (*want && !suffix(p, want)) strcat(p, want);
280
* Generate a `plain' (non-encoded) name from a given name.
282
static char * plainname P1((name), char *name)
284
return normalname(name, "", SUFFIX_TOASTED );
288
* Generate a `code' name from a given name.
290
static char * codename P1((name), char *name)
292
return normalname( name, SUFFIX_TOASTED, "" );
296
* If we're supposed to ask (fileno (stderr) is a tty, and f_force not
297
* set), ask the user whether to overwrite a file or not.
299
static int ok_to_replace P1(( name ), char * name)
303
if (f_force) return 1; /* YES, do replace */
304
if (!isatty(fileno(stderr))) return 0; /* NO, don't replace */
307
"%s already exists; do you wish to overwrite %s (y or n)? ",
311
for (c = reply = getchar(); c != '\n' && c != EOF; c = getchar()) ;
312
if (reply == 'y') return 1;
314
fprintf(stderr, "\tnot overwritten\n");
318
static void update_mode P0()
320
if (!instat.st_nlink) return; /* couldn't stat in */
323
if (fchmod(fileno(out), instat.st_mode & 07777)) {
325
fprintf(stderr, "%s: could not change file mode of \"%s\"\n",
330
if (outname && chmod(outname, instat.st_mode & 07777)) {
332
fprintf(stderr, "%s: could not change file mode of \"%s\"\n",
335
#endif /* HAS_CHMOD */
336
#endif /* HAS_FCHMOD */
339
static void update_own P0()
341
if (!instat.st_nlink) return; /* couldn't stat in */
343
(void)fchown(fileno(out), instat.st_uid, instat.st_gid);
346
(void)chown(outname, instat.st_uid, instat.st_gid);
347
#endif /* HAS_CHOWN */
348
#endif /* HAS_FCHOWN */
351
static void update_times P0()
353
if (!instat.st_nlink) return; /* couldn't stat in */
357
struct timeval tv[2];
359
tv[0].tv_sec = instat.st_atime;
360
tv[1].tv_sec = instat.st_mtime;
361
tv[0].tv_usec = tv[1].tv_usec = 0;
362
(void) utimes(outname, tv);
372
ut.actime = instat.st_atime;
373
ut.modtime = instat.st_mtime;
375
# ifdef HAS_UTIMEUSEC
376
ut.acusec = instat.st_ausec;
377
ut.modusec = instat.st_musec;
378
# endif /* HAS_UTIMEUSEC */
380
(void) utime(outname, &ut);
386
ut[0] = instat.st_atime;
387
ut[1] = instat.st_mtime;
389
(void) utime(outname, ut);
393
#endif /* HAS_UTIME */
394
#endif /* HAS_UTIMES */
398
static int okay_as_input P3((name,f,st), char* name, FILE* f, struct stat * st)
401
if (fstat(fileno(f), st) < 0)
403
if (stat(name, st) < 0)
407
fprintf(stderr, "%s: cannot stat \"%s\"\n", progname, name);
411
if (!S_ISREG(st->st_mode)) {
413
"%s: \"%s\" is not a regular file -- unchanged.\n",
417
if (st->st_nlink > 1 && !f_cat && !f_precious) {
419
"%s: \"%s\" has %s other link%s -- unchanged.\n",
420
progname,name,st->st_nlink - 1,"s" + (st->st_nlink<=2));
426
static void prepare_io P1(( desc), struct fmtdesc * desc)
428
output = desc->output;
431
init_input = desc->init_input;
432
init_output = desc->init_output;
435
static struct fmtdesc * grok_format P1((name), char * name)
443
for (f = alldescs; *f; f++) {
446
&& suffix(c, (*f)->suffix)) {
455
return (struct fmtdesc *)0;
458
static int open_input P2((name, st), char * name, struct stat * st)
460
struct fmtdesc * f = f_format;
462
st->st_nlink = 0; /* indicates `undefined' value */
464
inname = (char *)NULL;
471
if (f_decode) inname = codename(name);
473
if (!f_cat && suffix(name, SUFFIX_TOASTED)) {
475
"%s: %s already has \"%s\" suffix -- unchanged.\n",
476
progname, name, SUFFIX_TOASTED );
479
inname = strcpy(emalloc(strlen(name)+1), name);
481
if (!(in = fopen(inname, READ))) {
482
perror(inname); /* not guaranteed to be valid here */
483
fprintf(stderr, "%s: cannot open \"%s\" for reading\n",
487
if (!okay_as_input(inname, in, st)) return 0;
488
if (!f) f = grok_format(inname);
490
prepare_io( f ? f : & DEFAULT_FORMAT );
494
static int open_output P1((name), char *name)
496
if (!name || f_cat) {
498
outname = (char *)NULL;
507
o = (*(f_decode ? plainname : codename))(name);
508
if (!length_okay(o)) return 0;
509
if ((outfd = open(o, O_WRITE_EXCL, 0666)) >= 0)
510
out = fdopen(outfd, WRITE);
511
else if (errno != EEXIST) out = (FILE *)NULL;
512
else if (ok_to_replace(o)) out = fopen(o, WRITE);
518
"%s: can't open \"%s\" for writing\n",
520
if (outfd >= 0) (void)close(outfd);
529
static int process_encode P0()
537
if (!(r = gsm_create())) {
541
(void)gsm_option(r, GSM_OPT_FAST, &f_fast);
542
(void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose);
543
(void)gsm_option(r, GSM_OPT_LTP_CUT, &f_ltp_cut);
545
while ((cc = (*input)(s)) > 0) {
546
if (cc < sizeof(s) / sizeof(*s))
547
memset((char *)(s+cc), 0, sizeof(s)-(cc * sizeof(*s)));
549
if (fwrite((char *)d, sizeof(d), 1, out) != 1) {
550
perror(outname ? outname : "stdout");
551
fprintf(stderr, "%s: error writing to %s\n",
552
progname, outname ? outname : "stdout");
558
perror(inname ? inname : "stdin");
559
fprintf(stderr, "%s: error reading from %s\n",
560
progname, inname ? inname : "stdin");
569
static int process_decode P0()
577
if (!(r = gsm_create())) { /* malloc failed */
581
(void)gsm_option(r, GSM_OPT_FAST, &f_fast);
582
(void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose);
584
while ((cc = fread(s, 1, sizeof(s), in)) > 0) {
586
if (cc != sizeof(s)) {
587
if (cc >= 0) fprintf(stderr,
588
"%s: incomplete frame (%d byte%s missing) from %s\n",
589
progname, sizeof(s) - cc,
590
"s" + (sizeof(s) - cc == 1),
591
inname ? inname : "stdin" );
596
if (gsm_decode(r, s, d)) {
597
fprintf(stderr, "%s: bad frame in %s\n",
598
progname, inname ? inname : "stdin");
604
if ((*output)(d) < 0) {
606
fprintf(stderr, "%s: error writing to %s\n",
614
perror(inname ? inname : "stdin" );
615
fprintf(stderr, "%s: error reading from %s\n", progname,
616
inname ? inname : "stdin");
625
static int process P1((name), char * name)
635
if (!open_input(name, &instat) || !open_output(name))
638
if ((*(f_decode ? init_output : init_input))()) {
639
fprintf(stderr, "%s: error %s %s\n",
641
f_decode ? "writing header to" : "reading header from",
642
f_decode ? (outname ? outname : "stdout")
643
: (inname ? inname : "stdin"));
647
if ((*(f_decode ? process_decode : process_encode))())
650
if (fflush(out) < 0 || ferror(out)) {
651
perror(outname ? outname : "stdout");
652
fprintf(stderr, "%s: error writing \"%s\"\n", progname,
653
outname ? outname:"stdout");
663
if (fclose(out) < 0) {
665
fprintf(stderr, "%s: error writing \"%s\"\n",
669
if (outname != name) free(outname);
674
(void)fclose(in), in = (FILE *)0;
675
if (!f_cat && !f_precious) {
676
if (unlink(inname) < 0) {
679
"%s: source \"%s\" not deleted.\n",
684
if (inname != name) free(inname);
690
* Error handling and cleanup.
693
if (out && out != stdout) {
694
(void)fclose(out), out = (FILE *)0;
695
if (unlink(outname) < 0 && errno != ENOENT && errno != EINTR) {
697
fprintf(stderr, "%s: could not unlink \"%s\"\n",
701
if (in && in != stdin) (void)fclose(in), in = (FILE *)0;
703
if (inname && inname != name) free(inname);
704
if (outname && outname != name) free(outname);
709
static void version P0()
711
printf( "%s 1.0, version %s\n",
713
"$Id: toast.c,v 1.8 1996/07/02 10:41:04 jutta Exp $" );
716
static void help P0()
718
printf("Usage: %s [-fcpdhvaulsFC] [files...]\n", progname);
721
printf(" -f force Replace existing files without asking\n");
722
printf(" -c cat Write to stdout, do not remove source files\n");
723
printf(" -d decode Decode data (default is encode)\n");
724
printf(" -p precious Do not delete the source\n");
727
printf(" -u u-law Force 8 kHz/8 bit u-law in/output format\n");
728
printf(" -s sun .au Force Sun .au u-law in/output format\n");
729
printf(" -a A-law Force 8 kHz/8 bit A-law in/output format\n");
730
printf(" -l linear Force 16 bit linear in/output format\n");
733
printf(" -F fast Sacrifice conformance to performance\n");
734
printf(" -C cutoff Ignore most samples during LTP\n");
735
printf(" -v version Show version information\n");
736
printf(" -h help Print this text\n");
741
static void set_format P1((f), struct fmtdesc * f)
743
if (f_format && f_format != f) {
745
"%s: only one of -[uals] is possible (%s -h for help)\n",
753
int main P2((ac, av), int ac, char **av)
757
extern char * optarg;
761
while ((opt = getopt(ac, av, "fcdpvhuaslVFC:")) != EOF) switch (opt) {
763
case 'd': f_decode = 1; break;
764
case 'f': f_force = 1; break;
765
case 'c': f_cat = 1; break;
766
case 'p': f_precious = 1; break;
767
case 'F': f_fast = 1; break;
768
case 'C': f_ltp_cut = 100; break;
770
case 'V': f_verbose = 1; break; /* undocumented */
773
case 'u': set_format( &f_ulaw ); break;
774
case 'l': set_format( &f_linear ); break;
775
case 'a': set_format( &f_alaw ); break;
776
case 's': set_format( &f_audio ); break;
778
case 'v': version(); exit(0);
779
case 'h': help(); exit(0);
784
"Usage: %s [-fcpdhvuaslFC] [files...] (-h for help)\n",
794
catch_signals(onintr);
796
if (ac <= 0) process( (char *)0 );
797
else while (ac--) process( *av++ );