1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
|
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define import_spp
#include <iraf.h>
/*
* GENERIC -- This filter takes a file containing a generic operator as input
* and generates as output either a set of files, one for each of the data
* types in the generic family, or a single file wherein the generic section
* has been duplicated for each case.
*/
#define input lex_input
#define unput lex_unput
extern char *yytext;
extern int yyleng;
extern FILE *yyin;
extern FILE *yyout;
#define MAXFILES 512
#define MAXNEST 50
#define OK 0
#define SZ_FORSTK 20
/* $FOR contstruct descriptor.
*/
struct _for {
char f_prevtype; /* type before $for */
char f_types[20]; /* "csilrdx" */
char *f_curtype; /* pointer into f_types */
long f_fpos; /* seek offset of $FOR */
};
struct _for forstk[SZ_FORSTK];
int forlev;
char *type_string;
char xtype_string[SZ_FNAME+1];
char type_char;
int pass_output = 1;
int clobber = NO;
extern int yylex (void);
extern int lex_input (void);
extern void lex_unput (int ch);
extern long lex_tell (void);
extern void lex_seek (long fpos);
char *make_typed_filename (char *template, char type_char);
void set_type_string (char ch);
void copy_line (void);
void copy_string (void);
void copy_comment (void);
void make_float (char type_ch);
void output_indef (char ch);
void output_upper (char *s);
void pass_through (void);
void do_for (void);
void do_endfor (void);
void do_if (void);
void do_else (void);
void do_endif (void);
int evaluate_expr (void);
int parse_relational (int *size1, int *size2, int *op);
int relop (void);
int gsize (char ch);
char nextch (void);
char gch (void);
void uch (char ch);
void output (char ch);
void outstr (char *s);
/**
* GENERIC: e.g., generic [-k] [-t csilrdx] file
*/
int main (int argc, char *argv[])
{
char *files[MAXFILES], *s, **p, *ip;
char fname[SZ_FNAME], *extension;
char *types = "i", *t;
char *prefix = "";
char genfname[SZ_FNAME+1];
char template[SZ_FNAME+1];
char input_file[SZ_FNAME+1];
int n, nfiles;
FILE *fp;
genfname[0] = EOS;
nfiles = 0;
for (p = &argv[1]; *p != NULL; p++) {
s = *p;
if (s[0] == '-') {
switch (s[1]) {
case 'k':
clobber = YES;
break;
case 't':
if (*(p+1) != NULL)
types = *++p;
break;
case 'o':
if (*(p+1) != NULL)
strcpy (genfname, *++p);
break;
case 'p':
if (*(p+1) != NULL)
prefix = *++p;
break;
}
} else {
files[nfiles++] = s;
if (genfname[0] != EOS && nfiles > 1) {
fprintf (stderr,
"Cannot process multiple files with '-o' flag\n");
exit (OSOK+1);
}
}
}
for (n=0; n < nfiles; n++) {
strcpy (input_file, files[n]);
yyin = fopen (input_file, "r");
if (yyin == NULL) {
fprintf (stderr, "Cannot open input file '%s'\n", input_file);
continue;
}
/* Set pointer to the filename extension string. If the file name
* has an extension, lop it off by overwriting the '.' with EOS.
* The first character of the extension of a generic file is
* normally a 'g', e.g., ".gx" or ".gc", but we want to generate
* a ".x" or ".c" file, so lop off any leading g in the extension.
*/
if ((extension = strrchr (input_file, '.')) != NULL) {
*extension++ = EOS;
if (*extension == 'g')
extension++;
} else
extension = "";
for (t=types; *t != EOS; t++) {
/* Make output file name */
strcpy (fname, prefix);
/* Expand a template of the form "chars$tchars" into the root
* name of the new file, replacing the $t by the type char.
* If using input filename as the root, add "$t"; otherwise,
* check whether or not the generic filename string has a
* "$t" in it, and add one at end if it does not.
*/
if (genfname[0] == EOS) {
strcpy (template, input_file);
strcat (template, "$t");
} else {
strcpy (template, genfname);
for (ip=strchr(genfname,'$'); ip != NULL;
ip = strchr(ip,'$')) {
if (*(ip+1) == '$')
ip += 2;
else if (*(ip+1) == 't')
break;
}
if (ip == NULL && strlen(types) > 1)
strcat (template, "$t");
}
if (genfname[0] == EOS || strlen (types) > 1)
strcat (fname, make_typed_filename (template, *t));
else
strcat (fname, template);
/* If the user supplied the output filename template, we
* assume that it already contains an extension.
*/
if (genfname[0] == EOS) {
strcat (fname, ".");
strcat (fname, extension);
}
if (access(fname,0) == 0) {
if (clobber == NO) {
fprintf (stderr, "File `%s' already exists\n", fname);
continue;
} else
unlink (fname);
}
if ((fp = fopen (fname, "w")) == NULL) {
fprintf (stderr, "Cannot open file `%s'\n", fname);
continue;
}
yyout = fp;
set_type_string (*t);
type_char = *t;
forlev = -1;
yylex(); /* do it */
fclose (fp);
lex_seek (0L);
}
fclose (yyin);
}
exit (OSOK);
}
/* MAKE_TYPED_FILENAME -- Make a copy of a filename string, substituting
* the given type suffix character for the every sequence "$t" found in the
* input string. The output string is retained in an internal static buffer.
* Any sequence "$$" is converted into a single "$".
*/
char *
make_typed_filename (char *template, char type_char)
{
register char *ip, *op;
char ch;
static char fname[SZ_FNAME+1];
if (isupper (type_char))
ch = tolower (type_char);
else
ch = type_char;
for (ip=template, op=fname; *ip != EOS; )
if (*ip == '$' && *(ip+1) == '$') {
*op++ = '$';
ip += 2;
} else if (*ip == '$' && *(ip+1) == 't') {
*op++ = ch;
ip += 2;
} else
*op++ = *ip++;
return (fname);
}
/* SET_TYPE_STRING -- Given the type suffix character, set the external
* array "type_string" to the name of the corresponding SPP datatype.
*/
void
set_type_string (char ch)
{
char *ip, *op;
switch (ch) {
case 'B':
type_string = "ubyte"; /* unsigned byte */
break;
case 'U':
type_string = "ushort";
break;
case 'b':
type_string = "bool";
break;
case 'c':
type_string = "char";
break;
case 's':
type_string = "short";
break;
case 'i':
type_string = "int";
break;
case 'l':
type_string = "long";
break;
case 'r':
type_string = "real";
break;
case 'd':
type_string = "double";
break;
case 'x':
type_string = "complex";
break;
case 'p':
type_string = "pointer";
break;
default:
fprintf (stderr, "Unknown type suffix char `%c'\n", ch);
}
op = xtype_string;
*op++ = 'X';
for (ip=type_string; *ip != EOS; ip++)
*op++ = toupper (*ip);
*op++ = EOS;
}
/* COPY_LINE -- Output whatever is in the yylex token buffer, followed by the
* remainder of the line from which the token was extracted.
*/
void
copy_line (void)
{
char ch;
outstr(yytext);
while ((ch = input()) != '\n')
output(ch);
unput(ch);
}
/* COPY_STRING -- Called when the opening quote of a string is seen in the
* input. Copy the opening quote followed by all input characters until the
* end of string is seen.
*/
void
copy_string (void)
{
char ch;
outstr(yytext);
for (;;) {
switch (ch = input()) {
case '"':
output(ch);
return;
case '\\':
output(ch);
if ((ch = input()) != '\n')
output(ch);
else
unput(ch);
break;
case '\n':
unput(ch);
return;
default:
output(ch);
}
}
}
/* COPY_COMMENT -- Copy a C style comment to the output file.
*/
void
copy_comment (void)
{
char ch;
int flag = 0;
outstr (yytext);
while ((ch = input()) != EOF) {
output (ch);
switch (ch) {
case '*':
flag = 1;
break;
case '/':
if (flag == 1)
return;
else
flag = 0;
break;
default:
flag = 0;
break;
}
}
}
/* MAKE_FLOAT -- Called when a n$f is seen in the input to convert a numeric
* constant to the form appropriate for the indicated datatype, e.g., "0",
* "0.", "0.0D0", etc.
*/
void
make_float (char type_ch)
{
char *p;
for (p=yytext; *p != '$'; p++)
;
*p = EOS;
if (type_ch == 'x') {
output ('(');
outstr (yytext);
outstr (".0,");
outstr (yytext);
outstr (".0)");
} else {
outstr (yytext);
switch (type_ch) {
case 'r':
outstr (".0");
break;
case 'd':
outstr (".0D0");
break;
}
}
}
/* OUTPUT_INDEF -- Output the INDEF string for the indicated datatype.
*/
void
output_indef (char ch) /* output INDEF, INDEFS, INDEFL, etc. */
{
outstr(yytext);
switch (ch) {
case 's':
output ('S');
break;
case 'i':
output ('I');
break;
case 'l':
output ('L');
break;
case 'r':
output ('R');
break;
case 'd':
output ('D');
break;
case 'x':
output ('X');
break;
}
}
/* OUTPUT_UPPER -- Output the name of the current datatype (INT, REAL, etc.)
* in upper case.
*/
void
output_upper (char *s)
{
char ch, *p;
outstr(s);
for (p=type_string; (ch = *p) != EOS; p++)
output(toupper(ch));
}
/* PASS_THROUGH -- Used to pass text on to the output without modification.
* The text is delimited as "$/ (text) /" in the input file. The delimited
* section may enclose newlines.
*/
void
pass_through (void)
{
char ch;
while ((ch = input()) != '/')
output(ch);
}
/* DO_FOR -- Process a "$FOR (types)" statement. The sequence of statements
* bracketed by $for ... $endfor will be processed and output (to a single
* output stream) for each datatype named in the for predicate.
*/
void
do_for (void)
{
register char *op;
register int ch;
register struct _for *fp;
char types[20];
if (++forlev + 1 >= SZ_FORSTK) {
fprintf (stderr, "$for statements nested too deeply\n");
exit (OSOK+1);
}
/* Extract list of types.
*/
while ((ch = input()) != '(')
if (ch == EOF || ch == '\n') {
fprintf (stderr, "$for must have () delimited list of types\n");
strcpy (types, "i");
goto init_;
}
for (op=types; (ch = input()) != ')'; op++)
if (ch == EOF || ch == '\n') {
fprintf (stderr, "missing right paren in $for statement\n");
break;
} else
*op = ch;
*op = EOS;
if (op == types) {
fprintf (stderr, "null typelist in $for statement\n");
strcpy (types, "i");
}
init_:
fp = &forstk[forlev];
fp->f_prevtype = type_char;
strcpy (fp->f_types, types);
fp->f_curtype = fp->f_types;
fp->f_fpos = lex_tell();
type_char = *(fp->f_curtype)++;
set_type_string (type_char);
}
/* DO_ENDFOR -- Called to process a $ENDFOR. Set the next datatype and seek
* back to the line following the matching $FOR statement. When the type list
* is exhausted pop the $for stack and continue normal processing.
*/
void
do_endfor (void)
{
register struct _for *fp;
if (forlev < 0) {
fprintf (stderr, "$endfor with no matching $for\n");
return;
}
fp = &forstk[forlev];
if ((type_char = *(fp->f_curtype)++) != EOS) {
set_type_string (type_char);
lex_seek (fp->f_fpos);
} else {
type_char = fp->f_prevtype;
set_type_string (type_char);
--forlev;
}
}
/*
* Conditional Compilation
* -------------------------
*/
#define TRUE 1
#define FALSE 0
#define EQ 0
#define NE 1
#define LE 2
#define LT 3
#define GE 4
#define GT 5
char expr_buf[80], *expr;
int level = 0;
struct if_stack {
int oldstate;
int active;
} stk[MAXNEST];
/* DO_IF -- Process a $IF statement. Evaluate the predicate and push a
* pass or stop output flag on the if stack.
*/
void
do_if (void)
{
char ch;
int expr_value;
struct if_stack *p;
level += 1;
p = &stk[level];
p->oldstate = pass_output;
p->active = (pass_output == TRUE);
if ((expr_value = evaluate_expr()) == ERR)
expr_value = FALSE;
if ((ch = input()) != '\n')
unput(ch);
if (p->active == FALSE)
return;
else if (expr_value == FALSE)
pass_output = FALSE;
}
/* DO_ELSE -- Process a $ELSE statement. Toggle the pass/stop output flag
* on top of the if stack.
*/
void
do_else (void)
{
char ch;
if (level == 0)
fprintf (stderr, "Unmatched $else statement\n");
else if (stk[level].active) /* toggle pass_output */
pass_output = (pass_output == FALSE);
if ((ch = input()) != '\n')
unput(ch);
}
/* DO_ENDIF -- Process a $ENDIF statement. Pop the if stack.
*/
void
do_endif (void) /* $endif statement */
{
char ch;
if (level == 0)
fprintf (stderr, "Too many $endif statements\n");
else
pass_output = stk[level--].oldstate;
if ((ch = input()) != '\n')
unput(ch);
}
/* EVALUATE_EXPR -- Kludge to evaluate boolean expressions in $if statements.
* Two kinds of expressions are permitted: (datatype relop chars), or
* (sizeof(char) relop sizeof(char)), where relop = (==, !=, <= etc.).
*
* Examples: $if (datatype != dx)
* (code to be compiled if type not d or x)
*
* $if (sizeof(i) <= sizeof(r))
* (code to be compiled if size int <= real)
*/
int
evaluate_expr (void)
{
char ch=0, *p;
int lpar, size1, size2, op;
/* Advance to start of expression (discard '(') */
if (nextch() != '(')
goto err;
else
input();
/* Extract expression string into buffer */
expr = expr_buf;
nextch();
for (p=expr_buf, lpar=1; lpar > 0 && (*p = input()) != EOF; p++)
switch (ch = *p) {
case '(':
lpar++;
break;
case ')':
if (--lpar == 0)
*p = EOS;
break;
case '\n':
goto err;
}
/* Is current type in set or not in set */
if (strncmp (expr,"datatype",8) == 0) {
expr += 8;
switch (relop()) {
case EQ:
return (strchr(expr,type_char) != NULL);
case NE:
return (strchr(expr,type_char) == NULL);
default:
goto err;
}
/* Compare sizes of two data types */
} else if (strncmp(expr,"sizeof",6) == 0) {
if (parse_relational (&size1, &size2, &op) == ERR) {
ch = 0;
goto err;
}
switch (op) {
case EQ:
return (size1 == size2);
case NE:
return (size1 != size2);
case LE:
return (size1 <= size2);
case LT:
return (size1 < size2);
case GE:
return (size1 >= size2);
case GT:
return (size1 > size2);
}
/* only "type" and "sizeof" are implemented */
} else {
err: fprintf (stderr, "Syntax error in $if statement\n");
if (ch != '\n') {
/* skip rest of line */
while ((ch = input()) != '\n')
;
unput(ch);
}
}
return (ERR);
}
/* PARSE_RELATIONAL -- Parse "sizeof(t1) relop sizeof(t2)" (via brute force...) */
int
parse_relational (int *size1, int *size2, int *op)
{
expr += 6; /* ... (t1) */
if (gch() != '(')
return (ERR);
if ((*size1 = gsize(gch())) == ERR)
return (ERR);
if (gch() != ')')
return (ERR); /* relop */
if ((*op = relop()) == ERR)
return (ERR);
uch (gch()); /* skip whitespace */
if (strncmp(expr,"sizeof",6) != 0) /* sizeof(t2) */
return (ERR);
expr += 6;
if (gch() != '(')
return (ERR);
if ((*size2 = gsize(gch())) == ERR)
return (ERR);
if (gch() != ')')
return (ERR);
return (OK);
}
/* RELOP -- Return a code for the next relational operator token in the input
* stream.
*/
int
relop (void)
{
char ch;
switch (gch()) {
case '!':
if (gch() == '=')
return (NE);
return (ERR);
case '=':
if (gch() == '=')
return (EQ);
return (ERR);
case '<':
if ((ch = gch()) == '=')
return (LE);
uch(ch);
return (LT);
case '>':
if ((ch = gch()) == '=')
return (GE);
uch(ch);
return (GT);
default:
return (ERR);
}
}
/* GSIZE -- Return the size of a datatype given its character code.
*/
int
gsize (char ch)
{
switch (ch) {
case 'B':
return (sizeof(XUBYTE));
case 'U':
return (sizeof(XUSHORT));
case 't':
return (gsize(type_char));
case 'c':
return (sizeof(XCHAR));
case 's':
return (sizeof(XSHORT));
case 'i':
return (sizeof(XINT));
case 'l':
return (sizeof(XLONG));
case 'r':
return (sizeof(XREAL));
case 'd':
return (sizeof(XDOUBLE));
case 'x':
return (sizeof(XCOMPLEX));
case 'p':
return (sizeof(XPOINTER));
default:
return (ERR);
}
}
/* NEXTCH -- Advance to next non-whitespace character.
*/
char
nextch (void)
{
char ch;
for (ch=input(); ch == ' ' || ch == '\t'; ch=input())
;
unput (ch);
return (ch);
}
/* GCH -- Get next nonwhite char from expression buffer.
*/
char
gch (void)
{
while (*expr == ' ' || *expr == '\t')
expr++;
if (*expr != EOS)
return (*expr++);
else
return (EOS);
}
/* UCH -- Put char back into expression buffer.
*/
void
uch (char ch)
{
*--expr = ch;
}
/* OUTPUT -- Write a single character to the output file, if output is
* currently enabled (else throw it away).
*/
void
output (char ch)
{
if (pass_output)
putc (ch, yyout);
}
/* OUTSTR -- Output a string.
*/
void
outstr (char *s)
{
if (pass_output)
fputs (s, yyout);
}
|