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