| 2 |
|
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 4 |
|
/* |
| 5 |
< |
* rcalc.c - record calculator program. |
| 5 |
> |
* rcalc.c - record calculator program. |
| 6 |
|
* |
| 7 |
< |
* 9/11/87 |
| 7 |
> |
* 9/11/87 |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
< |
#include <stdlib.h> |
| 11 |
< |
#include <fcntl.h> |
| 12 |
< |
#include <stdio.h> |
| 13 |
< |
#include <string.h> |
| 14 |
< |
#include <math.h> |
| 15 |
< |
#include <ctype.h> |
| 10 |
> |
#include <stdlib.h> |
| 11 |
> |
#include <math.h> |
| 12 |
> |
#include <ctype.h> |
| 13 |
|
|
| 14 |
< |
#include "platform.h" |
| 15 |
< |
#include "rterror.h" |
| 16 |
< |
#include "rtmisc.h" |
| 17 |
< |
#include "rtio.h" |
| 18 |
< |
#include "calcomp.h" |
| 14 |
> |
#include "platform.h" |
| 15 |
> |
#include "rterror.h" |
| 16 |
> |
#include "rtmisc.h" |
| 17 |
> |
#include "rtio.h" |
| 18 |
> |
#include "calcomp.h" |
| 19 |
|
|
| 20 |
< |
#define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ |
| 21 |
< |
|| (c)=='+' || (c)=='e' || (c)=='E') |
| 20 |
> |
#define isnum(c) (isdigit(c) || ((c)=='-') | ((c)=='.') \ |
| 21 |
> |
| ((c)=='+') | ((c)=='e') | ((c)=='E')) |
| 22 |
|
|
| 23 |
< |
#define isblnk(c) (igneol ? isspace(c) : (c)==' '||(c)=='\t') |
| 23 |
> |
#define isblnk(c) (igneol ? isspace(c) : ((c)==' ')|((c)=='\t')) |
| 24 |
|
|
| 25 |
< |
#define INBSIZ 16384 /* longest record */ |
| 26 |
< |
#define MAXCOL 32 /* number of columns recorded */ |
| 25 |
> |
#define INBSIZ 16384 /* longest record */ |
| 26 |
> |
#define MAXCOL 32 /* number of columns recorded */ |
| 27 |
|
|
| 28 |
|
/* field type specifications */ |
| 29 |
< |
#define F_NUL 0 /* empty */ |
| 30 |
< |
#define F_TYP 0x7000 /* mask for type */ |
| 31 |
< |
#define F_WID 0x0fff /* mask for width */ |
| 32 |
< |
#define T_LIT 0x1000 /* string literal */ |
| 33 |
< |
#define T_STR 0x2000 /* string variable */ |
| 34 |
< |
#define T_NUM 0x3000 /* numeric value */ |
| 29 |
> |
#define F_NUL 0 /* empty */ |
| 30 |
> |
#define F_TYP 0x7000 /* mask for type */ |
| 31 |
> |
#define F_WID 0x0fff /* mask for width */ |
| 32 |
> |
#define T_LIT 0x1000 /* string literal */ |
| 33 |
> |
#define T_STR 0x2000 /* string variable */ |
| 34 |
> |
#define T_NUM 0x3000 /* numeric value */ |
| 35 |
|
|
| 36 |
< |
struct strvar { /* string variable */ |
| 37 |
< |
char *name; |
| 38 |
< |
char *val; |
| 39 |
< |
char *preset; |
| 40 |
< |
struct strvar *next; |
| 36 |
> |
struct strvar { /* string variable */ |
| 37 |
> |
char *name; |
| 38 |
> |
char *val; |
| 39 |
> |
char *preset; |
| 40 |
> |
struct strvar *next; |
| 41 |
|
}; |
| 42 |
|
|
| 43 |
< |
struct field { /* record format structure */ |
| 44 |
< |
int type; /* type of field (& width) */ |
| 43 |
> |
struct field { /* record format structure */ |
| 44 |
> |
int type; /* type of field (& width) */ |
| 45 |
|
union { |
| 46 |
< |
char *sl; /* string literal */ |
| 47 |
< |
struct strvar *sv; /* string variable */ |
| 48 |
< |
char *nv; /* numeric variable */ |
| 49 |
< |
EPNODE *ne; /* numeric expression */ |
| 50 |
< |
} f; /* field contents */ |
| 51 |
< |
struct field *next; /* next field in record */ |
| 46 |
> |
char *sl; /* string literal */ |
| 47 |
> |
struct strvar *sv; /* string variable */ |
| 48 |
> |
char *nv; /* numeric variable */ |
| 49 |
> |
EPNODE *ne; /* numeric expression */ |
| 50 |
> |
} f; /* field contents */ |
| 51 |
> |
struct field *next; /* next field in record */ |
| 52 |
|
}; |
| 53 |
|
|
| 54 |
< |
#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) |
| 55 |
< |
#define freqstr(s) efree(s) |
| 54 |
> |
#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) |
| 55 |
> |
#define freqstr(s) efree(s) |
| 56 |
|
|
| 57 |
|
static int getinputrec(FILE *fp); |
| 58 |
< |
static void scaninp(void), advinp(void), resetinp(void); |
| 58 |
> |
static void scaninp(void), advinp(void), passinp(void), skipinp(void); |
| 59 |
|
static void putrec(void), putout(void), nbsynch(void); |
| 60 |
|
static int getrec(void); |
| 61 |
|
static void execute(char *file); |
| 69 |
|
static struct strvar* getsvar(char *svname); |
| 70 |
|
static double l_in(char *); |
| 71 |
|
|
| 72 |
< |
struct field *inpfmt = NULL; /* input record format */ |
| 73 |
< |
struct field *outfmt = NULL; /* output record structure */ |
| 74 |
< |
struct strvar *svhead = NULL; /* string variables */ |
| 72 |
> |
struct field *inpfmt = NULL; /* input record format */ |
| 73 |
> |
struct field *outfmt = NULL; /* output record structure */ |
| 74 |
> |
struct strvar *svhead = NULL; /* string variables */ |
| 75 |
|
|
| 76 |
< |
int blnkeq = 1; /* blanks compare equal? */ |
| 77 |
< |
int igneol = 0; /* ignore end of line? */ |
| 81 |
< |
int passive = 0; /* passive mode (transmit unmatched input) */ |
| 82 |
< |
char sepchar = '\t'; /* input/output separator */ |
| 83 |
< |
int noinput = 0; /* no input records? */ |
| 84 |
< |
int nbicols = 0; /* number of binary input columns */ |
| 85 |
< |
int bocols = 0; /* produce binary output columns */ |
| 86 |
< |
char inpbuf[INBSIZ]; /* input buffer */ |
| 87 |
< |
double colval[MAXCOL]; /* input column values */ |
| 88 |
< |
unsigned long colflg = 0; /* column retrieved flags */ |
| 89 |
< |
int colpos; /* output column position */ |
| 76 |
> |
long incnt = 0; /* limit number of input records? */ |
| 77 |
> |
long outcnt = 0; /* limit number of output records? */ |
| 78 |
|
|
| 79 |
< |
int nowarn = 0; /* non-fatal diagnostic output */ |
| 80 |
< |
int unbuff = 0; /* unbuffered output (flush each record) */ |
| 79 |
> |
int blnkeq = 1; /* blanks compare equal? */ |
| 80 |
> |
int igneol = 0; /* ignore end of line? */ |
| 81 |
> |
int passive = 0; /* passive mode (transmit unmatched input) */ |
| 82 |
> |
char sepchar = '\t'; /* input/output separator */ |
| 83 |
> |
int noinput = 0; /* no input records? */ |
| 84 |
> |
int itype = 'a'; /* input type (a/f/F/d/D) */ |
| 85 |
> |
int nbicols = 0; /* number of binary input columns */ |
| 86 |
> |
int otype = 'a'; /* output format (a/f/F/d/D) */ |
| 87 |
> |
char inpbuf[INBSIZ]; /* input buffer */ |
| 88 |
> |
double colval[MAXCOL]; /* input column values */ |
| 89 |
> |
unsigned long colflg = 0; /* column retrieved flags */ |
| 90 |
> |
int colpos; /* output column position */ |
| 91 |
|
|
| 92 |
+ |
int nowarn = 0; /* non-fatal diagnostic output */ |
| 93 |
+ |
int unbuff = 0; /* unbuffered output (flush each record) */ |
| 94 |
+ |
|
| 95 |
|
struct { |
| 96 |
< |
FILE *fin; /* input file */ |
| 97 |
< |
int chr; /* next character */ |
| 98 |
< |
char *beg; /* home position */ |
| 99 |
< |
char *pos; /* scan position */ |
| 100 |
< |
char *end; /* read position */ |
| 101 |
< |
} ipb; /* circular lookahead buffer */ |
| 96 |
> |
FILE *fin; /* input file */ |
| 97 |
> |
int chr; /* next character */ |
| 98 |
> |
char *beg; /* home position */ |
| 99 |
> |
char *pos; /* scan position */ |
| 100 |
> |
char *end; /* read position */ |
| 101 |
> |
} ipb; /* circular lookahead buffer */ |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
int |
| 105 |
|
main( |
| 106 |
< |
int argc, |
| 107 |
< |
char *argv[] |
| 106 |
> |
int argc, |
| 107 |
> |
char *argv[] |
| 108 |
|
) |
| 109 |
|
{ |
| 110 |
< |
int i; |
| 110 |
> |
char *fpath; |
| 111 |
> |
int i; |
| 112 |
|
|
| 113 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
| 114 |
|
esupport &= ~(E_REDEFW); |
| 115 |
|
|
| 116 |
< |
#ifdef BIGGERLIB |
| 116 |
> |
#ifdef BIGGERLIB |
| 117 |
|
biggerlib(); |
| 118 |
|
#endif |
| 119 |
|
varset("PI", ':', 3.14159265358979323846); |
| 128 |
|
igneol = !igneol; |
| 129 |
|
break; |
| 130 |
|
case 'p': |
| 131 |
< |
passive = !passive; |
| 131 |
> |
passive = 1; |
| 132 |
|
break; |
| 133 |
+ |
case 'P': |
| 134 |
+ |
passive = -1; |
| 135 |
+ |
break; |
| 136 |
|
case 't': |
| 137 |
|
sepchar = argv[i][2]; |
| 138 |
|
break; |
| 140 |
|
svpreset(argv[++i]); |
| 141 |
|
break; |
| 142 |
|
case 'f': |
| 143 |
< |
fcompile(argv[++i]); |
| 143 |
> |
fpath = getpath(argv[++i], getrlibpath(), 0); |
| 144 |
> |
if (fpath == NULL) { |
| 145 |
> |
eputs(argv[0]); |
| 146 |
> |
eputs(": cannot find file '"); |
| 147 |
> |
eputs(argv[i]); |
| 148 |
> |
eputs("'\n"); |
| 149 |
> |
quit(1); |
| 150 |
> |
} |
| 151 |
> |
fcompile(fpath); |
| 152 |
|
break; |
| 153 |
|
case 'e': |
| 154 |
|
scompile(argv[++i], NULL, 0); |
| 159 |
|
case 'i': |
| 160 |
|
switch (argv[i][2]) { |
| 161 |
|
case '\0': |
| 162 |
+ |
itype = 'a'; |
| 163 |
|
nbicols = 0; |
| 164 |
|
readfmt(argv[++i], 0); |
| 165 |
|
break; |
| 166 |
+ |
case 'n': |
| 167 |
+ |
incnt = atol(argv[++i]); |
| 168 |
+ |
break; |
| 169 |
|
case 'a': |
| 170 |
+ |
itype = 'a'; |
| 171 |
|
nbicols = 0; |
| 172 |
|
break; |
| 173 |
|
case 'd': |
| 174 |
+ |
case 'D': |
| 175 |
+ |
itype = argv[i][2]; |
| 176 |
|
if (isdigit(argv[i][3])) |
| 177 |
|
nbicols = atoi(argv[i]+3); |
| 178 |
|
else |
| 184 |
|
} |
| 185 |
|
break; |
| 186 |
|
case 'f': |
| 187 |
+ |
case 'F': |
| 188 |
+ |
itype = argv[i][2]; |
| 189 |
|
if (isdigit(argv[i][3])) |
| 190 |
< |
nbicols = -atoi(argv[i]+3); |
| 190 |
> |
nbicols = atoi(argv[i]+3); |
| 191 |
|
else |
| 192 |
< |
nbicols = -1; |
| 193 |
< |
if (-nbicols*sizeof(float) > INBSIZ) { |
| 192 |
> |
nbicols = 1; |
| 193 |
> |
if (nbicols*sizeof(float) > INBSIZ) { |
| 194 |
|
eputs(argv[0]); |
| 195 |
|
eputs(": too many input columns\n"); |
| 196 |
|
quit(1); |
| 203 |
|
case 'o': |
| 204 |
|
switch (argv[i][2]) { |
| 205 |
|
case '\0': |
| 206 |
< |
bocols = 0; |
| 206 |
> |
otype = 'a'; |
| 207 |
|
readfmt(argv[++i], 1); |
| 208 |
|
break; |
| 209 |
+ |
case 'n': |
| 210 |
+ |
outcnt = atol(argv[++i]); |
| 211 |
+ |
break; |
| 212 |
|
case 'a': |
| 213 |
< |
bocols = 0; |
| 213 |
> |
otype = 'a'; |
| 214 |
|
break; |
| 215 |
|
case 'd': |
| 216 |
< |
bocols = 1; |
| 192 |
< |
break; |
| 216 |
> |
case 'D': |
| 217 |
|
case 'f': |
| 218 |
< |
bocols = -1; |
| 218 |
> |
case 'F': |
| 219 |
> |
otype = argv[i][2]; |
| 220 |
|
break; |
| 221 |
+ |
default: |
| 222 |
+ |
goto userr; |
| 223 |
|
} |
| 224 |
|
break; |
| 225 |
|
case 'w': |
| 232 |
|
userr: |
| 233 |
|
eputs("Usage: "); |
| 234 |
|
eputs(argv[0]); |
| 235 |
< |
eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
| 235 |
> |
eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
| 236 |
|
quit(1); |
| 237 |
|
} |
| 238 |
< |
|
| 239 |
< |
if (noinput) { /* produce a single output record */ |
| 238 |
> |
if (otype != 'a') |
| 239 |
> |
SET_FILE_BINARY(stdout); |
| 240 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 241 |
> |
flockfile(stdout); |
| 242 |
> |
#endif |
| 243 |
> |
if (noinput) { /* produce a single output record */ |
| 244 |
> |
if (i < argc) { |
| 245 |
> |
eputs(argv[0]); |
| 246 |
> |
eputs(": file argument(s) incompatible with -n\n"); |
| 247 |
> |
quit(1); |
| 248 |
> |
} |
| 249 |
|
eclock++; |
| 250 |
|
putout(); |
| 251 |
|
quit(0); |
| 252 |
|
} |
| 253 |
< |
|
| 254 |
< |
if (blnkeq) /* for efficiency */ |
| 253 |
> |
if (passive && (inpfmt == NULL) | (outfmt == NULL)) { |
| 254 |
> |
eputs(argv[0]); |
| 255 |
> |
eputs(": options -p and -P require -i and -o formats\n"); |
| 256 |
> |
quit(1); |
| 257 |
> |
} |
| 258 |
> |
if (blnkeq) /* for efficiency */ |
| 259 |
|
nbsynch(); |
| 260 |
|
|
| 261 |
< |
if (i == argc) /* from stdin */ |
| 261 |
> |
if (i == argc) /* from stdin */ |
| 262 |
|
execute(NULL); |
| 263 |
< |
else /* from one or more files */ |
| 263 |
> |
else /* from one or more files */ |
| 264 |
|
for ( ; i < argc; i++) |
| 265 |
|
execute(argv[i]); |
| 266 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
static void |
| 273 |
< |
nbsynch(void) /* non-blank starting synch character */ |
| 273 |
> |
nbsynch(void) /* non-blank starting synch character */ |
| 274 |
|
{ |
| 275 |
|
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
| 276 |
|
return; |
| 283 |
|
|
| 284 |
|
static int |
| 285 |
|
getinputrec( /* get next input record */ |
| 286 |
< |
FILE *fp |
| 286 |
> |
FILE *fp |
| 287 |
|
) |
| 288 |
|
{ |
| 289 |
< |
if (inpfmt != NULL) |
| 290 |
< |
return(getrec()); |
| 291 |
< |
if (nbicols > 0) |
| 292 |
< |
return(fread(inpbuf, sizeof(double), |
| 293 |
< |
nbicols, fp) == nbicols); |
| 294 |
< |
if (nbicols < 0) |
| 295 |
< |
return(fread(inpbuf, sizeof(float), |
| 296 |
< |
-nbicols, fp) == -nbicols); |
| 289 |
> |
if ((itype == 'd') | (itype == 'D')) { |
| 290 |
> |
if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
| 291 |
> |
return(0); |
| 292 |
> |
if (itype == 'D') |
| 293 |
> |
swap64(inpbuf, nbicols); |
| 294 |
> |
return(1); |
| 295 |
> |
} |
| 296 |
> |
if ((itype == 'f') | (itype == 'F')) { |
| 297 |
> |
if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols) |
| 298 |
> |
return(0); |
| 299 |
> |
if (itype == 'F') |
| 300 |
> |
swap32(inpbuf, nbicols); |
| 301 |
> |
return(1); |
| 302 |
> |
} |
| 303 |
|
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
| 304 |
|
} |
| 305 |
|
|
| 306 |
|
|
| 307 |
|
static void |
| 308 |
< |
execute( /* process a file */ |
| 309 |
< |
char *file |
| 308 |
> |
execute( /* process a file */ |
| 309 |
> |
char *file |
| 310 |
|
) |
| 311 |
|
{ |
| 312 |
< |
int conditional = vardefined("cond"); |
| 313 |
< |
long nrecs = 0; |
| 314 |
< |
long nout = 0; |
| 315 |
< |
FILE *fp; |
| 312 |
> |
static char condVN[] = "cond"; |
| 313 |
> |
static char recnoVN[] = "recno"; |
| 314 |
> |
static char outnoVN[] = "outno"; |
| 315 |
> |
const int conditional = vardefined(condVN); |
| 316 |
> |
const int set_recno = (varlookup(recnoVN) != NULL); |
| 317 |
> |
const int set_outno = (varlookup(outnoVN) != NULL); |
| 318 |
> |
long nrecs = 0; |
| 319 |
> |
long nout = 0; |
| 320 |
> |
FILE *fp; |
| 321 |
|
|
| 322 |
|
if (file == NULL) |
| 323 |
|
fp = stdin; |
| 326 |
|
eputs(": cannot open\n"); |
| 327 |
|
quit(1); |
| 328 |
|
} |
| 329 |
+ |
if (itype != 'a') |
| 330 |
+ |
SET_FILE_BINARY(fp); |
| 331 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 332 |
+ |
flockfile(fp); |
| 333 |
+ |
#endif |
| 334 |
+ |
if (conditional == ':') { |
| 335 |
+ |
eputs(condVN); |
| 336 |
+ |
eputs(": defined as constant\n"); |
| 337 |
+ |
quit(1); |
| 338 |
+ |
} |
| 339 |
|
if (inpfmt != NULL) |
| 340 |
|
initinp(fp); |
| 341 |
|
|
| 342 |
< |
while (getinputrec(fp)) { |
| 343 |
< |
varset("recno", '=', (double)++nrecs); |
| 342 |
> |
while (inpfmt != NULL ? getrec() : getinputrec(fp)) { |
| 343 |
> |
++nrecs; |
| 344 |
> |
if (set_recno) |
| 345 |
> |
varset(recnoVN, '=', (double)nrecs); |
| 346 |
> |
if (set_outno) |
| 347 |
> |
varset(outnoVN, '=', (double)(nout+1)); |
| 348 |
|
colflg = 0; |
| 349 |
|
eclock++; |
| 350 |
< |
if (!conditional || varvalue("cond") > 0.0) { |
| 286 |
< |
varset("outno", '=', (double)++nout); |
| 350 |
> |
if (!conditional || varvalue(condVN) > 0.0) { |
| 351 |
|
putout(); |
| 352 |
+ |
++nout; |
| 353 |
+ |
advinp(); |
| 354 |
+ |
} else if (inpfmt != NULL) { |
| 355 |
+ |
if (passive < 0) |
| 356 |
+ |
passinp(); |
| 357 |
+ |
else |
| 358 |
+ |
advinp(); |
| 359 |
|
} |
| 360 |
+ |
if (incnt && nrecs >= incnt) |
| 361 |
+ |
break; |
| 362 |
+ |
if (outcnt && nout >= outcnt) |
| 363 |
+ |
break; |
| 364 |
|
} |
| 365 |
|
fclose(fp); |
| 366 |
|
} |
| 367 |
|
|
| 368 |
|
|
| 369 |
|
static void |
| 370 |
< |
putout(void) /* produce an output record */ |
| 370 |
> |
putout(void) /* produce an output record */ |
| 371 |
|
{ |
| 372 |
|
|
| 373 |
|
colpos = 0; |
| 374 |
|
if (outfmt != NULL) |
| 375 |
|
putrec(); |
| 376 |
< |
else if (bocols) |
| 302 |
< |
chanout(bchanset); |
| 303 |
< |
else |
| 376 |
> |
else if (otype == 'a') |
| 377 |
|
chanout(chanset); |
| 378 |
< |
if (colpos && !bocols) |
| 378 |
> |
else |
| 379 |
> |
chanout(bchanset); |
| 380 |
> |
if (colpos && otype == 'a') |
| 381 |
|
putchar('\n'); |
| 382 |
|
if (unbuff) |
| 383 |
|
fflush(stdout); |
| 387 |
|
static double |
| 388 |
|
l_in(char *funame) /* function call for $channel */ |
| 389 |
|
{ |
| 390 |
< |
int n; |
| 391 |
< |
register char *cp; |
| 390 |
> |
int n; |
| 391 |
> |
char *cp; |
| 392 |
|
/* get argument as integer */ |
| 393 |
|
n = (int)(argument(1) + .5); |
| 394 |
|
if (n != 0) /* return channel value */ |
| 396 |
|
/* determine number of channels */ |
| 397 |
|
if (noinput || inpfmt != NULL) |
| 398 |
|
return(0); |
| 399 |
< |
if (nbicols > 0) |
| 399 |
> |
if (nbicols) |
| 400 |
|
return(nbicols); |
| 326 |
– |
if (nbicols < 0) |
| 327 |
– |
return(-nbicols); |
| 401 |
|
cp = inpbuf; /* need to count */ |
| 402 |
|
for (n = 0; *cp; ) |
| 403 |
|
if (blnkeq && isspace(sepchar)) { |
| 415 |
|
} |
| 416 |
|
|
| 417 |
|
double |
| 418 |
< |
chanvalue( /* return value for column n */ |
| 419 |
< |
int n |
| 418 |
> |
chanvalue( /* return value for column n */ |
| 419 |
> |
int n |
| 420 |
|
) |
| 421 |
|
{ |
| 422 |
< |
int i; |
| 423 |
< |
register char *cp; |
| 422 |
> |
int i; |
| 423 |
> |
char *cp; |
| 424 |
|
|
| 425 |
|
if (noinput || inpfmt != NULL) { |
| 426 |
|
eputs("no column input\n"); |
| 430 |
|
eputs("illegal channel number\n"); |
| 431 |
|
quit(1); |
| 432 |
|
} |
| 433 |
< |
if (nbicols > 0) { |
| 433 |
> |
if (nbicols) { |
| 434 |
|
if (n > nbicols) |
| 435 |
|
return(0.0); |
| 436 |
< |
cp = inpbuf + (n-1)*sizeof(double); |
| 437 |
< |
return(*(double *)cp); |
| 438 |
< |
} |
| 439 |
< |
if (nbicols < 0) { |
| 367 |
< |
if (n > -nbicols) |
| 368 |
< |
return(0.0); |
| 436 |
> |
if ((itype == 'd') | (itype == 'D')) { |
| 437 |
> |
cp = inpbuf + (n-1)*sizeof(double); |
| 438 |
> |
return(*(double *)cp); |
| 439 |
> |
} |
| 440 |
|
cp = inpbuf + (n-1)*sizeof(float); |
| 441 |
|
return(*(float *)cp); |
| 442 |
|
} |
| 454 |
|
while (*cp && *cp++ != sepchar) |
| 455 |
|
; |
| 456 |
|
|
| 457 |
< |
while (isspace(*cp)) /* some atof()'s don't like tabs */ |
| 457 |
> |
while (isspace(*cp)) /* some atof()'s don't like tabs */ |
| 458 |
|
cp++; |
| 459 |
|
|
| 460 |
|
if (n <= MAXCOL) { |
| 466 |
|
|
| 467 |
|
|
| 468 |
|
void |
| 469 |
< |
chanset( /* output column n */ |
| 470 |
< |
int n, |
| 471 |
< |
double v |
| 469 |
> |
chanset( /* output column n */ |
| 470 |
> |
int n, |
| 471 |
> |
double v |
| 472 |
|
) |
| 473 |
|
{ |
| 474 |
< |
if (colpos == 0) /* no leading separator */ |
| 474 |
> |
if (colpos == 0) /* no leading separator */ |
| 475 |
|
colpos = 1; |
| 476 |
|
while (colpos < n) { |
| 477 |
|
putchar(sepchar); |
| 482 |
|
|
| 483 |
|
|
| 484 |
|
void |
| 485 |
< |
bchanset( /* output binary channel n */ |
| 486 |
< |
int n, |
| 487 |
< |
double v |
| 485 |
> |
bchanset( /* output binary channel n */ |
| 486 |
> |
int n, |
| 487 |
> |
double v |
| 488 |
|
) |
| 489 |
|
{ |
| 490 |
|
static char zerobuf[sizeof(double)]; |
| 491 |
+ |
const int otlen = ((otype == 'd') | (otype == 'D')) ? |
| 492 |
+ |
sizeof(double) : sizeof(float); |
| 493 |
+ |
float fval = v; |
| 494 |
|
|
| 495 |
|
while (++colpos < n) |
| 496 |
< |
fwrite(zerobuf, |
| 497 |
< |
bocols>0 ? sizeof(double) : sizeof(float), |
| 498 |
< |
1, stdout); |
| 499 |
< |
if (bocols > 0) |
| 500 |
< |
fwrite(&v, sizeof(double), 1, stdout); |
| 501 |
< |
else { |
| 502 |
< |
float fval = v; |
| 503 |
< |
fwrite(&fval, sizeof(float), 1, stdout); |
| 496 |
> |
putbinary(zerobuf, otlen, 1, stdout); |
| 497 |
> |
switch (otype) { |
| 498 |
> |
case 'D': |
| 499 |
> |
swap64((char *)&v, 1); |
| 500 |
> |
/* fall through */ |
| 501 |
> |
case 'd': |
| 502 |
> |
putbinary(&v, sizeof(double), 1, stdout); |
| 503 |
> |
break; |
| 504 |
> |
case 'F': |
| 505 |
> |
swap32((char *)&fval, 1); |
| 506 |
> |
/* fall through */ |
| 507 |
> |
case 'f': |
| 508 |
> |
putbinary(&fval, sizeof(float), 1, stdout); |
| 509 |
> |
break; |
| 510 |
|
} |
| 511 |
|
} |
| 512 |
|
|
| 513 |
|
|
| 514 |
|
static void |
| 515 |
< |
readfmt( /* read record format */ |
| 516 |
< |
char *spec, |
| 517 |
< |
int output |
| 515 |
> |
readfmt( /* read record format */ |
| 516 |
> |
char *spec, |
| 517 |
> |
int output |
| 518 |
|
) |
| 519 |
|
{ |
| 520 |
< |
int fd; |
| 521 |
< |
char *inptr; |
| 522 |
< |
struct field fmt; |
| 523 |
< |
int res; |
| 524 |
< |
register struct field *f; |
| 520 |
> |
int fd; |
| 521 |
> |
char *inptr; |
| 522 |
> |
struct field fmt; |
| 523 |
> |
int res; |
| 524 |
> |
struct field *f; |
| 525 |
|
/* check for inline format */ |
| 526 |
|
for (inptr = spec; *inptr; inptr++) |
| 527 |
|
if (*inptr == '$') |
| 528 |
|
break; |
| 529 |
< |
if (*inptr) /* inline */ |
| 529 |
> |
if (*inptr) /* inline */ |
| 530 |
|
inptr = spec; |
| 531 |
< |
else { /* from file */ |
| 531 |
> |
else { /* from file */ |
| 532 |
|
if ((fd = open(spec, 0)) == -1) { |
| 533 |
|
eputs(spec); |
| 534 |
|
eputs(": cannot open\n"); |
| 548 |
|
close(fd); |
| 549 |
|
(inptr=inpbuf+2)[res] = '\0'; |
| 550 |
|
} |
| 551 |
< |
f = &fmt; /* get fields */ |
| 551 |
> |
f = &fmt; /* get fields */ |
| 552 |
|
while ((res = readfield(&inptr)) != F_NUL) { |
| 553 |
|
f->next = (struct field *)emalloc(sizeof(struct field)); |
| 554 |
|
f = f->next; |
| 580 |
|
|
| 581 |
|
|
| 582 |
|
static int |
| 583 |
< |
readfield( /* get next field in format */ |
| 584 |
< |
register char **pp |
| 583 |
> |
readfield( /* get next field in format */ |
| 584 |
> |
char **pp |
| 585 |
|
) |
| 586 |
|
{ |
| 587 |
< |
int type = F_NUL; |
| 588 |
< |
int width = 0; |
| 589 |
< |
register char *cp; |
| 587 |
> |
int type = F_NUL; |
| 588 |
> |
int width = 0; |
| 589 |
> |
char *cp; |
| 590 |
|
|
| 591 |
|
cp = inpbuf; |
| 592 |
|
while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') { |
| 647 |
|
|
| 648 |
|
|
| 649 |
|
struct strvar * |
| 650 |
< |
getsvar( /* get string variable */ |
| 651 |
< |
char *svname |
| 650 |
> |
getsvar( /* get string variable */ |
| 651 |
> |
char *svname |
| 652 |
|
) |
| 653 |
|
{ |
| 654 |
< |
register struct strvar *sv; |
| 654 |
> |
struct strvar *sv; |
| 655 |
|
|
| 656 |
|
for (sv = svhead; sv != NULL; sv = sv->next) |
| 657 |
|
if (!strcmp(sv->name, svname)) |
| 666 |
|
|
| 667 |
|
|
| 668 |
|
static void |
| 669 |
< |
svpreset( /* preset a string variable */ |
| 670 |
< |
char *eqn |
| 669 |
> |
svpreset( /* preset a string variable */ |
| 670 |
> |
char *eqn |
| 671 |
|
) |
| 672 |
|
{ |
| 673 |
< |
register struct strvar *sv; |
| 674 |
< |
register char *val; |
| 673 |
> |
struct strvar *sv; |
| 674 |
> |
char *val; |
| 675 |
|
|
| 676 |
|
for (val = eqn; *val != '='; val++) |
| 677 |
|
if (!*val) |
| 690 |
|
static void |
| 691 |
|
clearrec(void) /* clear input record variables */ |
| 692 |
|
{ |
| 693 |
< |
register struct field *f; |
| 693 |
> |
struct field *f; |
| 694 |
|
|
| 695 |
|
for (f = inpfmt; f != NULL; f = f->next) |
| 696 |
|
switch (f->type & F_TYP) { |
| 708 |
|
|
| 709 |
|
|
| 710 |
|
static int |
| 711 |
< |
getrec(void) /* get next record from file */ |
| 711 |
> |
getrec(void) /* get next record from file */ |
| 712 |
|
{ |
| 713 |
< |
int eatline; |
| 714 |
< |
register struct field *f; |
| 713 |
> |
int eatline; |
| 714 |
> |
struct field *f; |
| 715 |
|
|
| 716 |
|
while (ipb.chr != EOF) { |
| 717 |
< |
if (blnkeq) /* beware of nbsynch() */ |
| 717 |
> |
if (blnkeq) { /* beware of nbsynch() */ |
| 718 |
|
while (isblnk(ipb.chr)) |
| 719 |
< |
resetinp(); |
| 720 |
< |
eatline = (!igneol && ipb.chr != '\n'); |
| 719 |
> |
skipinp(); |
| 720 |
> |
if (ipb.chr == EOF) |
| 721 |
> |
return(0); |
| 722 |
> |
} |
| 723 |
> |
eatline = !igneol & (ipb.chr != '\n'); |
| 724 |
|
clearrec(); /* start with fresh record */ |
| 725 |
|
for (f = inpfmt; f != NULL; f = f->next) |
| 726 |
< |
if (getfield(f) == -1) |
| 726 |
> |
if (!getfield(f)) |
| 727 |
|
break; |
| 728 |
< |
if (f == NULL) { |
| 646 |
< |
advinp(); /* got one! */ |
| 728 |
> |
if (f == NULL) /* got one? */ |
| 729 |
|
return(1); |
| 730 |
< |
} |
| 731 |
< |
resetinp(); /* eat false start */ |
| 650 |
< |
if (eatline) { /* eat rest of line */ |
| 730 |
> |
skipinp(); /* else eat false start */ |
| 731 |
> |
if (eatline) { /* eat rest of line */ |
| 732 |
|
while (ipb.chr != '\n') { |
| 733 |
|
if (ipb.chr == EOF) |
| 734 |
|
return(0); |
| 735 |
< |
resetinp(); |
| 735 |
> |
skipinp(); |
| 736 |
|
} |
| 737 |
< |
resetinp(); |
| 737 |
> |
skipinp(); |
| 738 |
|
} |
| 739 |
|
} |
| 740 |
|
return(0); |
| 742 |
|
|
| 743 |
|
|
| 744 |
|
static int |
| 745 |
< |
getfield( /* get next field */ |
| 746 |
< |
register struct field *f |
| 745 |
> |
getfield( /* get next field */ |
| 746 |
> |
struct field *f |
| 747 |
|
) |
| 748 |
|
{ |
| 749 |
< |
static char buf[RMAXWORD+1]; /* no recursion! */ |
| 750 |
< |
int delim, inword; |
| 751 |
< |
double d; |
| 752 |
< |
char *np; |
| 753 |
< |
register char *cp; |
| 749 |
> |
static char buf[RMAXWORD+1]; /* no recursion! */ |
| 750 |
> |
int delim, inword; |
| 751 |
> |
double d; |
| 752 |
> |
char *np; |
| 753 |
> |
char *cp; |
| 754 |
|
|
| 755 |
|
switch (f->type & F_TYP) { |
| 756 |
|
case T_LIT: |
| 758 |
|
do { |
| 759 |
|
if (blnkeq && isblnk(*cp)) { |
| 760 |
|
if (!isblnk(ipb.chr)) |
| 761 |
< |
return(-1); |
| 761 |
> |
return(0); |
| 762 |
|
do |
| 763 |
|
cp++; |
| 764 |
|
while (isblnk(*cp)); |
| 769 |
|
cp++; |
| 770 |
|
scaninp(); |
| 771 |
|
} else |
| 772 |
< |
return(-1); |
| 772 |
> |
return(0); |
| 773 |
|
} while (*cp); |
| 774 |
< |
return(0); |
| 774 |
> |
break; |
| 775 |
|
case T_STR: |
| 776 |
|
if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) |
| 777 |
|
delim = EOF; |
| 779 |
|
delim = f->next->f.sl[0]; |
| 780 |
|
cp = buf; |
| 781 |
|
do { |
| 782 |
< |
if (ipb.chr == EOF || ipb.chr == '\n') |
| 782 |
> |
if ((ipb.chr == EOF) | (ipb.chr == '\n')) |
| 783 |
|
inword = 0; |
| 784 |
|
else if (blnkeq && delim != EOF) |
| 785 |
|
inword = isblnk(delim) ? |
| 796 |
|
if (f->f.sv->val == NULL) |
| 797 |
|
f->f.sv->val = savqstr(buf); /* first setting */ |
| 798 |
|
else if (strcmp(f->f.sv->val, buf)) |
| 799 |
< |
return(-1); /* doesn't match! */ |
| 800 |
< |
return(0); |
| 799 |
> |
return(0); /* doesn't match! */ |
| 800 |
> |
break; |
| 801 |
|
case T_NUM: |
| 802 |
|
if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) |
| 803 |
|
delim = EOF; |
| 827 |
|
varset(f->f.nv, '=', d); /* first setting */ |
| 828 |
|
else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001 |
| 829 |
|
|| d < -.001) |
| 830 |
< |
return(-1); /* doesn't match! */ |
| 831 |
< |
return(0); |
| 830 |
> |
return(0); /* doesn't match! */ |
| 831 |
> |
break; |
| 832 |
|
} |
| 833 |
< |
return -1; /* pro forma return */ |
| 833 |
> |
return(1); /* success! */ |
| 834 |
|
} |
| 835 |
|
|
| 836 |
|
|
| 837 |
|
static void |
| 838 |
< |
putrec(void) /* output a record */ |
| 838 |
> |
putrec(void) /* output a record */ |
| 839 |
|
{ |
| 840 |
< |
char fmt[32]; |
| 841 |
< |
register int n; |
| 842 |
< |
register struct field *f; |
| 843 |
< |
int adlast, adnext; |
| 840 |
> |
char fmt[32], typ[16]; |
| 841 |
> |
int n; |
| 842 |
> |
struct field *f; |
| 843 |
> |
int adlast, adnext; |
| 844 |
> |
double dv, av; |
| 845 |
|
|
| 846 |
|
adlast = 0; |
| 847 |
|
for (f = outfmt; f != NULL; f = f->next) { |
| 848 |
< |
adnext = blnkeq && |
| 848 |
> |
adnext = blnkeq && |
| 849 |
|
f->next != NULL && |
| 850 |
|
!( (f->next->type&F_TYP) == T_LIT && |
| 851 |
|
f->next->f.sl[0] == ' ' ); |
| 872 |
|
break; |
| 873 |
|
case T_NUM: |
| 874 |
|
n = f->type & F_WID; |
| 875 |
+ |
dv = evalue(f->f.ne); |
| 876 |
+ |
av = fabs(dv); |
| 877 |
+ |
if (n <= 9) |
| 878 |
+ |
strcpy(typ, "g"); |
| 879 |
+ |
else |
| 880 |
+ |
sprintf(typ, ".%de", n-5); |
| 881 |
+ |
if (av < 1L<<31) { |
| 882 |
+ |
long iv = (int)(av + .5); |
| 883 |
+ |
if (iv && fabs(av-iv) <= av*1e-14) |
| 884 |
+ |
strcpy(typ, ".0f"); |
| 885 |
+ |
} |
| 886 |
|
if (adlast && adnext) |
| 887 |
< |
strcpy(fmt, "%g"); |
| 887 |
> |
sprintf(fmt, "%%%s", typ); |
| 888 |
|
else if (adlast) |
| 889 |
< |
sprintf(fmt, "%%-%dg", n); |
| 889 |
> |
sprintf(fmt, "%%-%d%s", n, typ); |
| 890 |
|
else |
| 891 |
< |
sprintf(fmt, "%%%dg", n); |
| 892 |
< |
printf(fmt, evalue(f->f.ne)); |
| 891 |
> |
sprintf(fmt, "%%%d%s", n, typ); |
| 892 |
> |
printf(fmt, dv); |
| 893 |
|
adlast = 1; |
| 894 |
|
break; |
| 895 |
|
} |
| 898 |
|
|
| 899 |
|
|
| 900 |
|
static void |
| 901 |
< |
initinp(FILE *fp) /* prepare lookahead buffer */ |
| 901 |
> |
initinp(FILE *fp) /* prepare lookahead buffer */ |
| 902 |
|
|
| 903 |
|
{ |
| 904 |
|
ipb.fin = fp; |
| 905 |
|
ipb.beg = ipb.end = inpbuf; |
| 906 |
< |
ipb.pos = inpbuf-1; /* position before beginning */ |
| 906 |
> |
ipb.pos = inpbuf-1; /* position before beginning */ |
| 907 |
|
ipb.chr = '\0'; |
| 908 |
|
scaninp(); |
| 909 |
|
} |
| 910 |
|
|
| 911 |
|
|
| 912 |
|
static void |
| 913 |
< |
scaninp(void) /* scan next character */ |
| 913 |
> |
scaninp(void) /* scan next character */ |
| 914 |
|
{ |
| 915 |
|
if (ipb.chr == EOF) |
| 916 |
|
return; |
| 917 |
|
if (++ipb.pos >= &inpbuf[INBSIZ]) |
| 918 |
|
ipb.pos = inpbuf; |
| 919 |
< |
if (ipb.pos == ipb.end) { /* new character */ |
| 919 |
> |
if (ipb.pos == ipb.end) { /* new character */ |
| 920 |
|
if ((ipb.chr = getc(ipb.fin)) != EOF) { |
| 921 |
|
*ipb.end = ipb.chr; |
| 922 |
|
if (++ipb.end >= &inpbuf[INBSIZ]) |
| 930 |
|
|
| 931 |
|
|
| 932 |
|
static void |
| 933 |
< |
advinp(void) /* move home to current position */ |
| 933 |
> |
advinp(void) /* move home to current position */ |
| 934 |
|
{ |
| 935 |
|
ipb.beg = ipb.pos; |
| 936 |
|
} |
| 937 |
|
|
| 938 |
|
|
| 939 |
|
static void |
| 940 |
< |
resetinp(void) /* rewind position and advance 1 */ |
| 940 |
> |
passinp(void) /* pass beginning to current position */ |
| 941 |
|
{ |
| 942 |
< |
if (ipb.beg == NULL) /* full */ |
| 942 |
> |
if (!passive) { |
| 943 |
> |
advinp(); /* mistake to call us */ |
| 944 |
> |
return; |
| 945 |
> |
} |
| 946 |
> |
if (ipb.beg == NULL) /* buffer overflowed a bit */ |
| 947 |
|
ipb.beg = ipb.end; |
| 948 |
+ |
while (ipb.beg != ipb.pos) { /* transfer buffer unaltered */ |
| 949 |
+ |
putchar(*ipb.beg); |
| 950 |
+ |
if (++ipb.beg >= &inpbuf[INBSIZ]) |
| 951 |
+ |
ipb.beg = inpbuf; |
| 952 |
+ |
} |
| 953 |
+ |
} |
| 954 |
+ |
|
| 955 |
+ |
|
| 956 |
+ |
static void |
| 957 |
+ |
skipinp(void) /* rewind position and advance 1 */ |
| 958 |
+ |
{ |
| 959 |
+ |
if (ipb.beg == NULL) /* overflow - can't fully rewind */ |
| 960 |
+ |
ipb.beg = ipb.end; |
| 961 |
|
ipb.pos = ipb.beg; |
| 962 |
|
ipb.chr = *ipb.pos; |
| 963 |
|
if (passive) /* transmit unmatched character? */ |
| 964 |
< |
fputc(ipb.chr, stdout); |
| 964 |
> |
putchar(ipb.chr); |
| 965 |
|
if (++ipb.beg >= &inpbuf[INBSIZ]) |
| 966 |
|
ipb.beg = inpbuf; |
| 967 |
|
scaninp(); |
| 969 |
|
|
| 970 |
|
|
| 971 |
|
void |
| 972 |
< |
eputs(char *msg) |
| 972 |
> |
eputs(char *msg) |
| 973 |
|
{ |
| 974 |
|
fputs(msg, stderr); |
| 975 |
|
} |
| 976 |
|
|
| 977 |
|
|
| 978 |
|
void |
| 979 |
< |
wputs(char *msg) |
| 979 |
> |
wputs(char *msg) |
| 980 |
|
{ |
| 981 |
|
if (!nowarn) |
| 982 |
|
eputs(msg); |
| 984 |
|
|
| 985 |
|
|
| 986 |
|
void |
| 987 |
< |
quit(int code) |
| 987 |
> |
quit(int code) |
| 988 |
|
{ |
| 989 |
|
exit(code); |
| 990 |
|
} |