home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / cforms / part01 / src / comp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-18  |  5.3 KB  |  277 lines

  1. /*******************************************************************************
  2.  *
  3.  *        C O M P . C
  4.  *        -----------
  5.  *
  6.  * Description:
  7.  *    Compile a FRM-file to C-code.
  8.  *
  9.  * Included functions:
  10.  *    main    - Main function
  11.  *
  12.  * Revision:
  13.  *    Ver    Date    By        Reason
  14.  *    ---    ----    --        ------
  15.  *    1.00    900619    Lars Berntzon    Created
  16.  *
  17.  ******************************************************************************/
  18.  
  19. #include "config.h"
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <signal.h>
  26. #ifdef STDLIB_H
  27. #include <stdlib.h>
  28. #endif
  29. #ifdef MALLOC_H
  30. #include <malloc.h>
  31. #endif
  32.  
  33. #include "token.h"
  34. #include "comp.h"
  35.  
  36. struct list list;
  37. char version[] = "1.1";
  38. int n_errors = 0;
  39. int n_warnings = 0;
  40.  
  41. extern int cleanup();
  42.  
  43. static int found_picture();
  44. static int found_event();
  45. static int found_viewport();
  46. static int found_ccode();
  47.  
  48. static int previous_x, previous_y;
  49.  
  50. static struct lookup_s lookup[] = {    /* Aviable commands at top level */
  51.     "PICTURE",  found_picture,
  52.     "EVENT",    found_event,
  53.     "VIEWPORT", found_viewport,
  54.     "CCODE",    found_ccode
  55. };
  56.  
  57. #ifdef VOID_MAIN
  58.     void main(int argc, char *argv[])
  59. #else
  60.     main(int argc, char *argv[])
  61. #endif
  62. {
  63.     char token [TOKENSIZE];
  64.     int i;
  65.     
  66.     if (argc != 2) usage();
  67.     
  68.     if (OpenTok(argv[1]) == NULL) fatal("Failed to open file");
  69.     
  70.     signal(SIGINT, (SIGNAL_TYPE)cleanup);
  71.     signal(SIGFPE, (SIGNAL_TYPE)cleanup);
  72.     
  73.     while(GetTokNC(token) != NULL) {
  74.         for (i = 0; i < N_CMDS; i++) {
  75.             if (strequ(token, lookup[i].cmd) == 0)
  76.                 break;
  77.         }
  78.         if (i < N_CMDS) {
  79.             (*lookup[i].func)();
  80.         }
  81.         else {
  82.             error("Unknown command");
  83.         }
  84.     }
  85.     
  86.     if (n_errors) {
  87.         fprintf(stderr, "%d errors found.\n", n_errors);
  88. #ifdef VOID_MAIN 
  89.         return;
  90. #else
  91.         return 1;
  92. #endif
  93.     }
  94.  
  95.     /*
  96.      * Generate C-code.
  97.      */
  98.     output();
  99.     
  100. #ifdef VOID_MAIN 
  101.     return;
  102. #else
  103.     return 0;
  104. #endif
  105. }
  106.  
  107. static int found_viewport()
  108. {
  109.     link((struct link **)&list.viewport, &get_viewport()->link, TYPE_VIEWPORT);
  110.     return OK;
  111. }
  112. static int found_picture()
  113. {
  114.     link((struct link **)&list.picture, &get_picture()->link, TYPE_PICTURE);
  115.     return OK;
  116. }
  117. static int found_event()
  118. {
  119.     link((struct link **)&list.event, &get_event()->link, TYPE_EVENT);
  120.     return OK;
  121. }
  122. static int found_ccode()
  123. {
  124.     link((struct link **)&list.ccode, &get_ccode()->link, TYPE_CCODE);
  125.     return OK;
  126. }
  127.  
  128. /******************************************************************************/
  129. int strequ(char *s1, char *s2)
  130. {
  131.     while(*s1 && toupper(*s1) == toupper(*s2))
  132.     s1++, s2++;
  133.  
  134.     return toupper(*s1) - toupper(*s2);
  135. }
  136.  
  137. void usage(void)
  138. {
  139.     fprintf(stderr, "Usage: comp <file>\n");
  140.     exit(1);
  141. }
  142.  
  143. void fatal(char *str)
  144. {
  145.     fprintf(stderr, "fatal: %s\n", str);
  146.     exit(1);
  147. }
  148.  
  149. void error(char *str)
  150. {
  151.     static int old_line;
  152.     /*
  153.      * Maximize to one error per line.
  154.      */
  155.     if (line > old_line) {
  156.         n_errors++;
  157.     old_line = line;
  158.     fprintf(stderr, "error line %d: %s\n", line, str);
  159.     }
  160. }
  161.  
  162. void *_memalloc(char *file, int line, int size)
  163. {
  164.     void *p;
  165.     if ((p = malloc(size)) == NULL) {
  166.         fprintf(stderr, "*** %s %d out of memory ***\n", file, line);
  167.         exit(1);
  168.     }
  169.     memset(p, 0, size);
  170.  
  171.     return p;
  172. }
  173.  
  174. void link(struct link **head, struct link *item, int type)
  175. {
  176.     if (item == NULL) return;
  177.     
  178.     item->type = type;
  179.  
  180.     /*
  181.      * Sort by name if aviable, otherwise last in list.
  182.      */    
  183.     if (item->name) {
  184.         for(; *head != NULL; head = &(*head)->next) {
  185.             if (strequ((*head)->name, item->name) > 0) break;
  186.         }
  187.     }
  188.     else {
  189.         for(; *head != NULL; head = & (*head)->next) 
  190.             ;
  191.     }
  192.  
  193.     item->next = *head;
  194.     *head = item;
  195. }
  196.  
  197. struct link *find_name(struct link *first, char *name)
  198. {
  199.     if (name == NULL) return NULL;
  200.     
  201.     for(; first != NULL; first = first->next) {
  202.         if (first->name != NULL && strequ(name, first->name) == 0) break;
  203.     }
  204.  
  205.     return first;
  206. }
  207.  
  208. void link_name(struct link *item, char *name)
  209. {
  210.     if (item == NULL || name == NULL) return;
  211.  
  212.     item->name = memalloc(strlen(name) + 1);
  213.     strcpy(item->name, name);
  214. }
  215.  
  216. GetPos(int *x, int *y)
  217. {
  218.     char token[TOKENSIZE];
  219.     char sign = 0;
  220.     
  221.     if (GetTokNC(token) == NULL) return FAIL;
  222.     
  223.     if (token[0] == '+' || token[0] == '-') {
  224.         sign = token[0];
  225.         if (GetTokNC(token) == NULL) return FAIL;
  226.     }
  227.  
  228.     if (!isdigit(token[0])) return FAIL;
  229.     
  230.     *x = atoi(token);
  231.     if (sign) *x = previous_x + (sign == '-' ? - *x : *x);
  232.     sign = 0;
  233.     
  234.     if (GetTokNC(token) == NULL || strcmp(token, ",")) return FAIL;
  235.     
  236.     if (GetTokNC(token) == NULL) return FAIL;
  237.     
  238.     if (token[0] == '+' || token[0] == '-') {
  239.         sign = token[0];
  240.         if (GetTokNC(token) == NULL) return FAIL;
  241.     }
  242.     if (!isdigit(token[0])) return FAIL;
  243.     
  244.     *y = atoi(token);
  245.     if (sign) *y = previous_y + (sign == '-' ? - *y : *y);
  246.     sign = 0;
  247.     
  248.     previous_x = *x;
  249.     previous_y = *y;
  250.     
  251.     return OK;
  252. }
  253.  
  254. cleanup()
  255. {
  256.     struct viewport *vp;
  257.     struct picture *pp;
  258.     struct event *ep;
  259.     struct ccode *cp;
  260.     
  261.     for(vp = list.viewport; vp != NULL; vp = unget_viewport(vp))
  262.     ;
  263.  
  264.     for(pp = list.picture; pp != NULL; pp = unget_picture(pp))
  265.     ;
  266.  
  267.     for(ep = list.event; ep != NULL; ep = unget_event(ep))
  268.     ;
  269.  
  270.     for(cp = list.ccode; cp != NULL; cp = unget_ccode(cp))
  271.     ;
  272.     
  273.     exit(1);
  274.  
  275.     return 0;
  276. }
  277.