--- ray/src/util/getinfo.c 2003/02/22 02:07:30 2.5 +++ ray/src/util/getinfo.c 2022/04/13 15:43:06 2.25 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: getinfo.c,v 2.5 2003/02/22 02:07:30 greg Exp $"; +static const char RCSid[] = "$Id: getinfo.c,v 2.25 2022/04/13 15:43:06 greg Exp $"; #endif /* * getinfo.c - program to read info. header from file. @@ -7,88 +7,79 @@ static const char RCSid[] = "$Id: getinfo.c,v 2.5 2003 * 1/3/86 */ -#include +#include +#include "rtio.h" +#include "platform.h" +#include "rtprocess.h" +#include "resolu.h" -#ifdef MSDOS -#include -extern int _fmode; -#endif +static char fmt[MAXFMTLEN] = "*"; -extern int fputs(); - -int -tabstr(s) /* put out line followed by tab */ -register char *s; +static int +tabstr( /* put out line followed by tab */ + char *s, + void *p +) { while (*s) { putchar(*s); s++; } if (*--s == '\n') - putchar('\t'); + fputc('\t', stdout); return(0); } -main(argc, argv) -int argc; -char **argv; +static int +adjheadline( /* check for lines to remove */ + char *s, + void *p +) { - int dim = 0; - FILE *fp; - int i; + char **av; - if (argc > 1 && !strcmp(argv[1], "-d")) { - argc--; argv++; - dim = 1; -#ifdef MSDOS - setmode(fileno(stdin), _fmode = O_BINARY); -#endif - } else if (argc == 2 && !strcmp(argv[1], "-")) { -#ifdef MSDOS - setmode(fileno(stdin), O_BINARY); - setmode(fileno(stdout), O_BINARY); -#endif - getheader(stdin, NULL, NULL); - copycat(); - exit(0); - } - for (i = 1; i < argc; i++) { - fputs(argv[i], stdout); - if ((fp = fopen(argv[i], "r")) == NULL) - fputs(": cannot open\n", stdout); - else { - if (dim) { - fputs(": ", stdout); - getdim(fp); - } else { - tabstr(":\n"); - getheader(fp, tabstr, NULL); - putchar('\n'); - } - fclose(fp); + if (formatval(fmt, s)) + return(0); /* don't echo format */ + + /* check for match to skip */ + for (av = (char **)p; *av; av++) { + char *s1 = s; + char *s2 = *av; + if (isspace(*s2)) { + if (!isspace(*s1)) + continue; + while (isspace(*++s1)) ; + while (isspace(*++s2)) ; } - } - if (argc == 1) - if (dim) { - getdim(stdin); - } else { - getheader(stdin, fputs, stdout); - putchar('\n'); + while (*s2 && *s1 == *s2) { + if ((*s1 == '=') & (s1 > s)) + return(0); + if (isspace(*s2)) + break; + s1++; s2++; } - exit(0); + if (s1 == s) + continue; + if ((*s1 == '=') & !*s2) + return(0); + if (isspace(*s1) & isspace(*s2)) + return(0); + } + fputs(s, stdout); /* copy if no match */ + return(0); } -getdim(fp) /* get dimensions from file */ -register FILE *fp; +static void +getdim( /* get dimensions from file */ + FILE *fp +) { int j; - register int c; + int c; - getheader(fp, NULL, NULL); /* skip header */ - switch (c = getc(fp)) { case '+': /* picture */ case '-': @@ -100,14 +91,14 @@ register FILE *fp; getc(fp); j = 0; while ((c = getc(fp)) != EOF) - if (c == 0) + if (c == 0) { if (++j >= 4) break; - else - putchar(' '); - else + fputc(' ', stdout); + } else { putchar(c); - putchar('\n'); + } + fputc('\n', stdout); break; default: /* ??? */ fputs("unknown file type\n", stdout); @@ -116,10 +107,172 @@ register FILE *fp; } -copycat() /* copy input to output */ +static void +copycat(void) /* copy input to output */ { - register int c; + char buf[8192]; + int n; - while ((c = getchar()) != EOF) - putchar(c); + fflush(stdout); + while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) + if (writebuf(fileno(stdout), buf, n) != n) + break; +} + + +int +main( + int argc, + char **argv +) +{ + int dim = 0; + FILE *fp; + int i; + + if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') && + argv[1][1] == 'd') { + dim = 1 - 2*(argv[1][0] == '-'); + argc--; argv++; + } +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(stdin); + flockfile(stdout); +#endif + SET_FILE_BINARY(stdin); + if (argc > 2 && !strcmp(argv[1], "-c")) { + SET_FILE_BINARY(stdout); + setvbuf(stdin, NULL, _IONBF, 2); + if (checkheader(stdin, fmt, stdout) < 0) { + fputs("Bad header!\n", stderr); + return 1; + } + printargs(argc-2, argv+2, stdout); + if (fmt[0] != '*') /* better be the same! */ + fputformat(fmt, stdout); + fputc('\n', stdout); + if (dim) { /* copy resolution string? */ + RESOLU rs; + if (!fgetsresolu(&rs, stdin)) { + fputs("No resolution string!\n", stderr); + return 1; + } + if (dim > 0) + fputsresolu(&rs, stdout); + } + fflush(stdout); + execvp(argv[2], argv+2); + perror(argv[2]); + return 1; + } + if (argc > 2 && !strcmp(argv[1], "-a")) { + SET_FILE_BINARY(stdout); + if (checkheader(stdin, fmt, stdout) < 0) { + fputs("Bad header!\n", stderr); + return 1; + } + for (i = 2; i < argc; i++) { + int len = strlen(argv[i]); + if (!len) continue; + fputs(argv[i], stdout); + if (argv[i][len-1] != '\n') + fputc('\n', stdout); + } + if (fmt[0] != '*') + fputformat(fmt, stdout); + fputc('\n', stdout); + copycat(); + return 0; + } + if (argc > 2 && !strcmp(argv[1], "-r")) { + SET_FILE_BINARY(stdout); + if (getheader(stdin, adjheadline, argv+2) < 0) { + fputs("Bad header!\n", stderr); + return 1; + } + for (i = 2; i < argc; i++) { /* add lines w/ var[= ]value */ + int len = strlen(argv[i]); + int strt = 0, j; + while(isspace(argv[i][strt])) strt++; + if (strt == len) continue; + while (isspace(argv[i][len-1])) len--; + for (j = strt+1; j < len-1; j++) + if (argv[i][j] == '=' || isspace(argv[i][j])) { + for (j = 0; j < len; j++) + putchar(argv[i][j]); + fputc('\n', stdout); + break; + } + } + if (fmt[0] != '*') + fputformat(fmt, stdout); + fputc('\n', stdout); + copycat(); + return 0; + } + if (argc == 2 && !strcmp(argv[1], "-")) { + SET_FILE_BINARY(stdout); + if (getheader(stdin, NULL, NULL) < 0) { + fputs("Bad header!\n", stderr); + return 1; + } + if (dim < 0) { /* skip resolution string? */ + RESOLU rs; + if (!fgetsresolu(&rs, stdin)) { + fputs("No resolution string!\n", stderr); + return 1; + } + } + copycat(); + return 0; + } + for (i = 1; i < argc; i++) { + fputs(argv[i], stdout); + if ((fp = fopen(argv[i], "r")) == NULL) + fputs(": cannot open\n", stdout); + else { +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(fp); +#endif + if (dim < 0) { /* dimensions only */ + if (getheader(fp, NULL, NULL) < 0) { + fputs("bad header!\n", stdout); + continue; + } + fputs(": ", stdout); + getdim(fp); + } else { + tabstr(":\n", NULL); + if (getheader(fp, tabstr, NULL) < 0) { + fputs(argv[i], stderr); + fputs(": bad header!\n", stderr); + return 1; + } + fputc('\n', stdout); + if (dim > 0) { + fputc('\t', stdout); + getdim(fp); + } + } + fclose(fp); + } + } + if (argc == 1) { + if (dim < 0) { + if (getheader(stdin, NULL, NULL) < 0) { + fputs("Bad header!\n", stderr); + return 1; + } + getdim(stdin); + } else { + if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) { + fputs("Bad header!\n", stderr); + return 1; + } + fputc('\n', stdout); + if (dim > 0) + getdim(stdin); + } + } + return 0; }