| 7 |
|
* 1/3/86 |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
< |
#include <stdio.h> |
| 10 |
> |
#include <ctype.h> |
| 11 |
> |
#include "rtio.h" |
| 12 |
> |
#include "platform.h" |
| 13 |
> |
#include "rtprocess.h" |
| 14 |
> |
#include "resolu.h" |
| 15 |
|
|
| 16 |
< |
#ifdef MSDOS |
| 13 |
< |
#include <fcntl.h> |
| 14 |
< |
extern int _fmode; |
| 15 |
< |
#endif |
| 16 |
> |
static char fmt[MAXFMTLEN] = "*"; |
| 17 |
|
|
| 17 |
– |
extern int fputs(); |
| 18 |
|
|
| 19 |
< |
|
| 20 |
< |
int |
| 21 |
< |
tabstr(s) /* put out line followed by tab */ |
| 22 |
< |
register char *s; |
| 19 |
> |
static int |
| 20 |
> |
tabstr( /* put out line followed by tab */ |
| 21 |
> |
char *s, |
| 22 |
> |
void *p |
| 23 |
> |
) |
| 24 |
|
{ |
| 25 |
|
while (*s) { |
| 26 |
|
putchar(*s); |
| 27 |
|
s++; |
| 28 |
|
} |
| 29 |
|
if (*--s == '\n') |
| 30 |
< |
putchar('\t'); |
| 30 |
> |
fputc('\t', stdout); |
| 31 |
|
return(0); |
| 32 |
|
} |
| 33 |
|
|
| 34 |
|
|
| 35 |
< |
main(argc, argv) |
| 36 |
< |
int argc; |
| 37 |
< |
char **argv; |
| 35 |
> |
static int |
| 36 |
> |
adjheadline( /* check for lines to remove */ |
| 37 |
> |
char *s, |
| 38 |
> |
void *p |
| 39 |
> |
) |
| 40 |
|
{ |
| 41 |
< |
int dim = 0; |
| 39 |
< |
FILE *fp; |
| 40 |
< |
int i; |
| 41 |
> |
char **av; |
| 42 |
|
|
| 43 |
< |
if (argc > 1 && !strcmp(argv[1], "-d")) { |
| 44 |
< |
argc--; argv++; |
| 45 |
< |
dim = 1; |
| 46 |
< |
#ifdef MSDOS |
| 47 |
< |
setmode(fileno(stdin), _fmode = O_BINARY); |
| 48 |
< |
#endif |
| 49 |
< |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
| 50 |
< |
#ifdef MSDOS |
| 51 |
< |
setmode(fileno(stdin), O_BINARY); |
| 52 |
< |
setmode(fileno(stdout), O_BINARY); |
| 53 |
< |
#endif |
| 54 |
< |
getheader(stdin, NULL, NULL); |
| 54 |
< |
copycat(); |
| 55 |
< |
exit(0); |
| 56 |
< |
} |
| 57 |
< |
for (i = 1; i < argc; i++) { |
| 58 |
< |
fputs(argv[i], stdout); |
| 59 |
< |
if ((fp = fopen(argv[i], "r")) == NULL) |
| 60 |
< |
fputs(": cannot open\n", stdout); |
| 61 |
< |
else { |
| 62 |
< |
if (dim) { |
| 63 |
< |
fputs(": ", stdout); |
| 64 |
< |
getdim(fp); |
| 65 |
< |
} else { |
| 66 |
< |
tabstr(":\n"); |
| 67 |
< |
getheader(fp, tabstr, NULL); |
| 68 |
< |
putchar('\n'); |
| 69 |
< |
} |
| 70 |
< |
fclose(fp); |
| 43 |
> |
if (formatval(fmt, s)) |
| 44 |
> |
return(0); /* don't echo format */ |
| 45 |
> |
|
| 46 |
> |
/* check for match to skip */ |
| 47 |
> |
for (av = (char **)p; *av; av++) { |
| 48 |
> |
char *s1 = s; |
| 49 |
> |
char *s2 = *av; |
| 50 |
> |
if (isspace(*s2)) { |
| 51 |
> |
if (!isspace(*s1)) |
| 52 |
> |
continue; |
| 53 |
> |
while (isspace(*++s1)) ; |
| 54 |
> |
while (isspace(*++s2)) ; |
| 55 |
|
} |
| 56 |
< |
} |
| 57 |
< |
if (argc == 1) |
| 58 |
< |
if (dim) { |
| 59 |
< |
getdim(stdin); |
| 60 |
< |
} else { |
| 61 |
< |
getheader(stdin, fputs, stdout); |
| 78 |
< |
putchar('\n'); |
| 56 |
> |
while (*s2 && *s1 == *s2) { |
| 57 |
> |
if ((*s1 == '=') & (s1 > s)) |
| 58 |
> |
return(0); |
| 59 |
> |
if (isspace(*s2)) |
| 60 |
> |
break; |
| 61 |
> |
s1++; s2++; |
| 62 |
|
} |
| 63 |
< |
exit(0); |
| 63 |
> |
if (s1 == s) |
| 64 |
> |
continue; |
| 65 |
> |
if (isspace(*s1) && !*s2 | isspace(*s2)) |
| 66 |
> |
return(0); /* skip */ |
| 67 |
> |
} |
| 68 |
> |
fputs(s, stdout); /* copy if no match */ |
| 69 |
> |
return(0); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
< |
getdim(fp) /* get dimensions from file */ |
| 74 |
< |
register FILE *fp; |
| 73 |
> |
static void |
| 74 |
> |
getdim( /* get dimensions from file */ |
| 75 |
> |
FILE *fp |
| 76 |
> |
) |
| 77 |
|
{ |
| 78 |
|
int j; |
| 79 |
< |
register int c; |
| 79 |
> |
int c; |
| 80 |
|
|
| 90 |
– |
getheader(fp, NULL, NULL); /* skip header */ |
| 91 |
– |
|
| 81 |
|
switch (c = getc(fp)) { |
| 82 |
|
case '+': /* picture */ |
| 83 |
|
case '-': |
| 89 |
|
getc(fp); |
| 90 |
|
j = 0; |
| 91 |
|
while ((c = getc(fp)) != EOF) |
| 92 |
< |
if (c == 0) |
| 92 |
> |
if (c == 0) { |
| 93 |
|
if (++j >= 4) |
| 94 |
|
break; |
| 95 |
< |
else |
| 96 |
< |
putchar(' '); |
| 108 |
< |
else |
| 95 |
> |
fputc(' ', stdout); |
| 96 |
> |
} else { |
| 97 |
|
putchar(c); |
| 98 |
< |
putchar('\n'); |
| 98 |
> |
} |
| 99 |
> |
fputc('\n', stdout); |
| 100 |
|
break; |
| 101 |
|
default: /* ??? */ |
| 102 |
|
fputs("unknown file type\n", stdout); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
< |
copycat() /* copy input to output */ |
| 108 |
> |
static void |
| 109 |
> |
copycat(void) /* copy input to output */ |
| 110 |
|
{ |
| 111 |
< |
register int c; |
| 111 |
> |
char buf[8192]; |
| 112 |
> |
int n; |
| 113 |
|
|
| 114 |
< |
while ((c = getchar()) != EOF) |
| 115 |
< |
putchar(c); |
| 114 |
> |
fflush(stdout); |
| 115 |
> |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
| 116 |
> |
if (writebuf(fileno(stdout), buf, n) != n) |
| 117 |
> |
break; |
| 118 |
> |
} |
| 119 |
> |
|
| 120 |
> |
|
| 121 |
> |
int |
| 122 |
> |
main( |
| 123 |
> |
int argc, |
| 124 |
> |
char **argv |
| 125 |
> |
) |
| 126 |
> |
{ |
| 127 |
> |
int dim = 0; |
| 128 |
> |
FILE *fp; |
| 129 |
> |
int i; |
| 130 |
> |
|
| 131 |
> |
if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') && |
| 132 |
> |
argv[1][1] == 'd') { |
| 133 |
> |
dim = 1 - 2*(argv[1][0] == '-'); |
| 134 |
> |
argc--; argv++; |
| 135 |
> |
} |
| 136 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 137 |
> |
flockfile(stdin); |
| 138 |
> |
#endif |
| 139 |
> |
SET_FILE_BINARY(stdin); |
| 140 |
> |
if (argc > 2 && !strcmp(argv[1], "-c")) { |
| 141 |
> |
SET_FILE_BINARY(stdout); |
| 142 |
> |
setvbuf(stdin, NULL, _IONBF, 2); |
| 143 |
> |
if (checkheader(stdin, fmt, stdout) < 0) { |
| 144 |
> |
fputs("Bad header!\n", stderr); |
| 145 |
> |
return 1; |
| 146 |
> |
} |
| 147 |
> |
printargs(argc-2, argv+2, stdout); |
| 148 |
> |
if (fmt[0] != '*') /* better be the same! */ |
| 149 |
> |
fputformat(fmt, stdout); |
| 150 |
> |
fputc('\n', stdout); |
| 151 |
> |
if (dim) { /* copy resolution string? */ |
| 152 |
> |
RESOLU rs; |
| 153 |
> |
if (!fgetsresolu(&rs, stdin)) { |
| 154 |
> |
fputs("No resolution string!\n", stderr); |
| 155 |
> |
return 1; |
| 156 |
> |
} |
| 157 |
> |
if (dim > 0) |
| 158 |
> |
fputsresolu(&rs, stdout); |
| 159 |
> |
} |
| 160 |
> |
fflush(stdout); |
| 161 |
> |
execvp(argv[2], argv+2); |
| 162 |
> |
perror(argv[2]); |
| 163 |
> |
return 1; |
| 164 |
> |
} |
| 165 |
> |
if (argc > 2 && !strcmp(argv[1], "-a")) { |
| 166 |
> |
SET_FILE_BINARY(stdout); |
| 167 |
> |
if (checkheader(stdin, fmt, stdout) < 0) { |
| 168 |
> |
fputs("Bad header!\n", stderr); |
| 169 |
> |
return 1; |
| 170 |
> |
} |
| 171 |
> |
for (i = 2; i < argc; i++) { |
| 172 |
> |
int len = strlen(argv[i]); |
| 173 |
> |
if (!len) continue; |
| 174 |
> |
fputs(argv[i], stdout); |
| 175 |
> |
if (argv[i][len-1] != '\n') |
| 176 |
> |
fputc('\n', stdout); |
| 177 |
> |
} |
| 178 |
> |
if (fmt[0] != '*') |
| 179 |
> |
fputformat(fmt, stdout); |
| 180 |
> |
fputc('\n', stdout); |
| 181 |
> |
copycat(); |
| 182 |
> |
return 0; |
| 183 |
> |
} |
| 184 |
> |
if (argc > 2 && !strcmp(argv[1], "-r")) { |
| 185 |
> |
SET_FILE_BINARY(stdout); |
| 186 |
> |
if (getheader(stdin, adjheadline, argv+2) < 0) { |
| 187 |
> |
fputs("Bad header!\n", stderr); |
| 188 |
> |
return 1; |
| 189 |
> |
} |
| 190 |
> |
for (i = 2; i < argc; i++) { /* add lines w/ var[= ]value */ |
| 191 |
> |
int len = strlen(argv[i]); |
| 192 |
> |
int strt = 0, j; |
| 193 |
> |
while(isspace(argv[i][strt])) strt++; |
| 194 |
> |
if (strt == len) continue; |
| 195 |
> |
while (isspace(argv[i][len-1])) len--; |
| 196 |
> |
for (j = strt+1; j < len-1; j++) |
| 197 |
> |
if (argv[i][j] == '=' || isspace(argv[i][j])) { |
| 198 |
> |
for (j = 0; j < len; j++) |
| 199 |
> |
putchar(argv[i][j]); |
| 200 |
> |
fputc('\n', stdout); |
| 201 |
> |
break; |
| 202 |
> |
} |
| 203 |
> |
} |
| 204 |
> |
if (fmt[0] != '*') |
| 205 |
> |
fputformat(fmt, stdout); |
| 206 |
> |
fputc('\n', stdout); |
| 207 |
> |
copycat(); |
| 208 |
> |
return 0; |
| 209 |
> |
} |
| 210 |
> |
if (argc == 2 && !strcmp(argv[1], "-")) { |
| 211 |
> |
SET_FILE_BINARY(stdout); |
| 212 |
> |
if (getheader(stdin, NULL, NULL) < 0) { |
| 213 |
> |
fputs("Bad header!\n", stderr); |
| 214 |
> |
return 1; |
| 215 |
> |
} |
| 216 |
> |
if (dim < 0) { /* skip resolution string? */ |
| 217 |
> |
RESOLU rs; |
| 218 |
> |
if (!fgetsresolu(&rs, stdin)) { |
| 219 |
> |
fputs("No resolution string!\n", stderr); |
| 220 |
> |
return 1; |
| 221 |
> |
} |
| 222 |
> |
} |
| 223 |
> |
copycat(); |
| 224 |
> |
return 0; |
| 225 |
> |
} |
| 226 |
> |
for (i = 1; i < argc; i++) { |
| 227 |
> |
fputs(argv[i], stdout); |
| 228 |
> |
if ((fp = fopen(argv[i], "r")) == NULL) |
| 229 |
> |
fputs(": cannot open\n", stdout); |
| 230 |
> |
else { |
| 231 |
> |
if (dim < 0) { /* dimensions only */ |
| 232 |
> |
if (getheader(fp, NULL, NULL) < 0) { |
| 233 |
> |
fputs("bad header!\n", stdout); |
| 234 |
> |
continue; |
| 235 |
> |
} |
| 236 |
> |
fputs(": ", stdout); |
| 237 |
> |
getdim(fp); |
| 238 |
> |
} else { |
| 239 |
> |
tabstr(":\n", NULL); |
| 240 |
> |
if (getheader(fp, tabstr, NULL) < 0) { |
| 241 |
> |
fputs(argv[i], stderr); |
| 242 |
> |
fputs(": bad header!\n", stderr); |
| 243 |
> |
return 1; |
| 244 |
> |
} |
| 245 |
> |
fputc('\n', stdout); |
| 246 |
> |
if (dim > 0) { |
| 247 |
> |
fputc('\t', stdout); |
| 248 |
> |
getdim(fp); |
| 249 |
> |
} |
| 250 |
> |
} |
| 251 |
> |
fclose(fp); |
| 252 |
> |
} |
| 253 |
> |
} |
| 254 |
> |
if (argc == 1) { |
| 255 |
> |
if (dim < 0) { |
| 256 |
> |
if (getheader(stdin, NULL, NULL) < 0) { |
| 257 |
> |
fputs("Bad header!\n", stderr); |
| 258 |
> |
return 1; |
| 259 |
> |
} |
| 260 |
> |
getdim(stdin); |
| 261 |
> |
} else { |
| 262 |
> |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) { |
| 263 |
> |
fputs("Bad header!\n", stderr); |
| 264 |
> |
return 1; |
| 265 |
> |
} |
| 266 |
> |
fputc('\n', stdout); |
| 267 |
> |
if (dim > 0) |
| 268 |
> |
getdim(stdin); |
| 269 |
> |
} |
| 270 |
> |
} |
| 271 |
> |
return 0; |
| 272 |
|
} |