home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 402_01 / cforms-2.2 / src / comp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-20  |  8.0 KB  |  383 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. #ifndef lint
  19. static volatile char sccs_comp_c[] = "@(#) comp.c,v 1.11 1993/05/13 21:55:42 lasse Exp";
  20. #endif
  21.  
  22. #include "config.h"
  23. #include "token.h"
  24. #include "comp.h"
  25. #include "../patchlevel.h"
  26.  
  27. struct list list;        /* The list of everything    */
  28. int n_errors = 0;        /* Number of errors        */
  29. int n_warnings = 0;        /* Number of warnings        */
  30. char *filename;            /* Name of input file        */
  31.  
  32. static int debug_flg = 0;    /* No debuging by default    */
  33.  
  34. extern int yyparse(void);    /* Declare the yyparse function    */
  35.  
  36. /******************************************************************
  37.  *        M A I N
  38.  *        -------
  39.  * Description:
  40.  *    Main routine for the cforms compiler.
  41.  *
  42.  * Arguments:
  43.  *    file    - Name of file to compile.
  44.  *
  45.  ******************************************************************/
  46. #ifdef VOID_MAIN
  47. void
  48. #else
  49. int
  50. #endif
  51. main(int argc, char *argv[])
  52. {
  53.     char token [TOKENSIZE];
  54.     int i;
  55.     char *p;
  56.     
  57.     if (argc != 2) usage();
  58.  
  59.     if (strcmp(argv[1], "-v") == 0) {
  60.     (void)printf("CForms compiler version %s\n", PATCHLEVEL);
  61.     exit(0);
  62.     }
  63.  
  64.     filename = argv[1];
  65.     
  66.     /*
  67.      * Check that file has proper extension.
  68.      */
  69.     if ((p = strrchr(filename, '.')) == NULL ||
  70.     strcmp(p, ".frm") != 0)
  71.     {
  72.     error("illegal file name suffix for file (must be .frm)");
  73. #ifdef VOID_MAIN
  74.     return;
  75. #else
  76.     return 1;
  77. #endif
  78.     }
  79.  
  80.     filename = argv[1];
  81.  
  82.     if (OpenTok(filename) == NULL) fatal("Failed to open file");
  83.     
  84.     signal(SIGINT, cleanup);
  85.     signal(SIGFPE, cleanup);
  86.  
  87.     /*
  88.      * Call the parser.
  89.      */
  90.     if (yyparse() != 0) {
  91.     error("aborted");
  92.     exit(1);
  93.     }
  94.     
  95.     if (n_errors) {
  96.         fprintf(stderr, "%d errors found.\n", n_errors);
  97. #ifdef VOID_MAIN 
  98.         return;
  99. #else
  100.         return 1;
  101. #endif
  102.     }
  103.  
  104.     /*
  105.      * Generate C-code.
  106.      */
  107.     output();
  108.     
  109. #ifdef VOID_MAIN 
  110.     return;
  111. #else
  112.     return 0;
  113. #endif
  114. }
  115.  
  116. /******************************************************************
  117.  *        S T R E Q U
  118.  *        -----------
  119.  * Description:
  120.  *    Compare strings case insensitive, otherwhise same as
  121.  *    strcmp
  122.  *
  123.  ******************************************************************/
  124. int strequ(char *s1, char *s2)
  125. {
  126.     while(*s1 && toupper(*s1) == toupper(*s2))
  127.     s1++, s2++;
  128.  
  129.     return toupper(*s1) - toupper(*s2);
  130. }
  131.  
  132. /******************************************************************
  133.  *        U S A G E
  134.  *        ---------
  135.  * Description:
  136.  *    Give usage message and exit.
  137.  *
  138.  ******************************************************************/
  139. void usage(void)
  140. {
  141.     fprintf(stderr, "Usage: comp <file>\n");
  142.     exit(1);
  143. }
  144.  
  145. /******************************************************************
  146.  *        F A T A L
  147.  *        ---------
  148.  * Description:
  149.  *    Give fatal message and exit.
  150.  *
  151.  ******************************************************************/
  152. void fatal(char *str)
  153. {
  154.     fprintf(stderr, "fatal: %s\n", str);
  155.     exit(1);
  156. }
  157.  
  158. /******************************************************************
  159.  *        E R R O R
  160.  *        ---------
  161.  * Description:
  162.  *    Give error message.
  163.  *
  164.  ******************************************************************/
  165. void error(char *str)
  166. {
  167.     static int old_line;
  168.     /*
  169.      * Maximize to one error per line.
  170.      */
  171.     if (line > old_line) {
  172.         n_errors++;
  173.     old_line = line;
  174.     fprintf(stdout, "error line %d: %s\n", line, str);
  175.     }
  176. }
  177.  
  178. /******************************************************************
  179.  *        _ M E M A L L O C
  180.  *        -----------------
  181.  * Description:
  182.  *    Allocate memory with check. If no success just abort. 
  183.  *
  184.  * Argument:
  185.  *    file    - Sourcecode filename.
  186.  *    line    - Line number of sourcecode.
  187.  *    size    - Size in bytes to allocate.
  188.  *
  189.  * Return:
  190.  *    pointer to memory allocated.
  191.  *
  192.  ******************************************************************/
  193. void *_memalloc(char *file, int line, int size)
  194. {
  195.     void *p;
  196.     if ((p = malloc(size)) == NULL) {
  197.         fprintf(stderr, "*** %s %d out of memory ***\n", file, line);
  198.         exit(1);
  199.     }
  200.     memset(p, 0, size);
  201.  
  202.     return p;
  203. }
  204.  
  205. /******************************************************************
  206.  *        _ S T R D U P L I C A T E
  207.  *        -------------------------
  208.  * Description:
  209.  *    Duplicate text string.
  210.  *
  211.  * Argument:
  212.  *    file    - Sourcecode filename.
  213.  *    line    - Line number of sourcecode.
  214.  *    str    - String to duplicate.
  215.  *
  216.  * Return:
  217.  *    pointer to string allocated.
  218.  *
  219.  ******************************************************************/
  220. char *_strduplicate(char *file, int line, const char *str)
  221. {
  222.     char *p;
  223.     if ((p = malloc(strlen(str) + 1)) == NULL) {
  224.         fprintf(stderr, "*** %s %d out of memory ***\n", file, line);
  225.         exit(1);
  226.     }
  227.     strcpy(p, str);
  228.  
  229.     return p;
  230. }
  231.  
  232. /******************************************************************
  233.  *        _ R E A L L O C M E M
  234.  *        ---------------------
  235.  * Description:
  236.  *    Reallocate memory
  237.  *
  238.  * Argument:
  239.  *    file    - Sourcecode filename.
  240.  *    line    - Line number of sourcecode.
  241.  *    ptr    - Pointer to old memory.
  242.  *    size    - Size in bytes to allocate.
  243.  *
  244.  * Return:
  245.  *    pointer to memory allocated.
  246.  *
  247.  ******************************************************************/
  248. void *_reallocmem(char *file, int line, void *ptr, int size)
  249. {
  250.     void *p;
  251.     if ((p = realloc(ptr, size)) == NULL) {
  252.         fprintf(stderr, "*** %s %d out of memory ***\n", file, line);
  253.         exit(1);
  254.     }
  255.  
  256.     return p;
  257. }
  258.  
  259. /******************************************************************
  260.  *        F I N D _ N A M E
  261.  *        -----------------
  262.  * Description:
  263.  *    Find item in list with name.
  264.  *
  265.  * Arguments:
  266.  *     first    - First item in list.
  267.  *    name    - Name to search for.
  268.  *
  269.  * Return:
  270.  *    pointer to item or NULL if not found.
  271.  *    
  272.  ******************************************************************/
  273. struct link *
  274. find_name(struct link *first, char *name)
  275. {
  276.     if (name == NULL) return NULL;
  277.     
  278.     for(; first != NULL; first = first->next) {
  279.         if (first->name != NULL && strequ(name, first->name) == 0) break;
  280.     }
  281.  
  282.     return first;
  283. }
  284.  
  285. /******************************************************************
  286.  *        D E B U G
  287.  *        ---------
  288.  *
  289.  ******************************************************************/
  290. void
  291. debug(char *fmt, ...)
  292. {
  293.     static char init;    /* This is just a dummy address holder */
  294.     static char *env = &init;
  295.     static FILE *log;
  296.     va_list arg;
  297.     extern unsigned sleep(unsigned);
  298.  
  299.     if (debug_flg == 0) return;
  300.  
  301.     va_start(arg, fmt);
  302.     vprintf(fmt, arg);
  303.     printf("\n");
  304.     fflush(stdout);
  305.     va_end(arg);
  306. }
  307.  
  308. /******************************************************************
  309.  *        S T R I P _Q U O T E S
  310.  *        ----------------------
  311.  * Description:
  312.  *    Strip leading and ending quotes and return pointer
  313.  *    to new string.
  314.  *
  315.  * Arguments:
  316.  *    str    - Pointer to string to change.
  317.  *
  318.  * Return:
  319.  *    Pointer to modified string.
  320.  *
  321.  ******************************************************************/
  322. char *
  323. strip_quotes(char *str)
  324. {
  325.      if (*str == '"') str++;
  326.      if (str[strlen(str) - 1] == '"') {
  327.      str[strlen(str) - 1] = 0;
  328.      }
  329.      return str;
  330. }
  331.  
  332. /******************************************************************
  333.  *        S T R T O U P P E R
  334.  *        -------------------
  335.  * Description:
  336.  *    Converts a whole string to uppercase.
  337.  *
  338.  * Arguments:
  339.  *    str    - Pointer to string.
  340.  *
  341.  * Return:
  342.  *    Pointer to str.
  343.  *
  344.  ******************************************************************/
  345.  
  346. char *
  347. strtoupper(char *str)
  348. {
  349.     char *p = str;
  350.  
  351.     while(*p) {
  352.     *p = toupper(*p);
  353.     p++;
  354.     }
  355.  
  356.     return str;
  357. }
  358. /******************************************************************
  359.  *              C L E A N U P
  360.  *              -------------
  361.  * Description:
  362.  *      Clean up and free everything and exit.
  363.  *
  364.  * Arguments:
  365.  *      none
  366.  *
  367.  * Return:
  368.  *      none
  369.  ******************************************************************/
  370. #ifdef SIGNAL_INT
  371. int
  372. #else
  373. void
  374. #endif
  375. cleanup(int sig)
  376. {
  377.     fprintf(stderr, "aborted\n");
  378.     exit(1);
  379. #ifdef SIGNAL_INT 
  380.     return 0;   /* Just to shut up compiler */ 
  381. #endif 
  382. }
  383.