home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- *
- * C O M P . C
- * -----------
- *
- * Description:
- * Compile a FRM-file to C-code.
- *
- * Included functions:
- * main - Main function
- *
- * Revision:
- * Ver Date By Reason
- * --- ---- -- ------
- * 1.00 900619 Lars Berntzon Created
- *
- ******************************************************************************/
- #ifndef lint
- static volatile char sccs_comp_c[] = "@(#) comp.c,v 1.11 1993/05/13 21:55:42 lasse Exp";
- #endif
-
- #include "config.h"
- #include "token.h"
- #include "comp.h"
- #include "../patchlevel.h"
-
- struct list list; /* The list of everything */
- int n_errors = 0; /* Number of errors */
- int n_warnings = 0; /* Number of warnings */
- char *filename; /* Name of input file */
-
- static int debug_flg = 0; /* No debuging by default */
-
- extern int yyparse(void); /* Declare the yyparse function */
-
- /******************************************************************
- * M A I N
- * -------
- * Description:
- * Main routine for the cforms compiler.
- *
- * Arguments:
- * file - Name of file to compile.
- *
- ******************************************************************/
- #ifdef VOID_MAIN
- void
- #else
- int
- #endif
- main(int argc, char *argv[])
- {
- char token [TOKENSIZE];
- int i;
- char *p;
-
- if (argc != 2) usage();
-
- if (strcmp(argv[1], "-v") == 0) {
- (void)printf("CForms compiler version %s\n", PATCHLEVEL);
- exit(0);
- }
-
- filename = argv[1];
-
- /*
- * Check that file has proper extension.
- */
- if ((p = strrchr(filename, '.')) == NULL ||
- strcmp(p, ".frm") != 0)
- {
- error("illegal file name suffix for file (must be .frm)");
- #ifdef VOID_MAIN
- return;
- #else
- return 1;
- #endif
- }
-
- filename = argv[1];
-
- if (OpenTok(filename) == NULL) fatal("Failed to open file");
-
- signal(SIGINT, cleanup);
- signal(SIGFPE, cleanup);
-
- /*
- * Call the parser.
- */
- if (yyparse() != 0) {
- error("aborted");
- exit(1);
- }
-
- if (n_errors) {
- fprintf(stderr, "%d errors found.\n", n_errors);
- #ifdef VOID_MAIN
- return;
- #else
- return 1;
- #endif
- }
-
- /*
- * Generate C-code.
- */
- output();
-
- #ifdef VOID_MAIN
- return;
- #else
- return 0;
- #endif
- }
-
- /******************************************************************
- * S T R E Q U
- * -----------
- * Description:
- * Compare strings case insensitive, otherwhise same as
- * strcmp
- *
- ******************************************************************/
- int strequ(char *s1, char *s2)
- {
- while(*s1 && toupper(*s1) == toupper(*s2))
- s1++, s2++;
-
- return toupper(*s1) - toupper(*s2);
- }
-
- /******************************************************************
- * U S A G E
- * ---------
- * Description:
- * Give usage message and exit.
- *
- ******************************************************************/
- void usage(void)
- {
- fprintf(stderr, "Usage: comp <file>\n");
- exit(1);
- }
-
- /******************************************************************
- * F A T A L
- * ---------
- * Description:
- * Give fatal message and exit.
- *
- ******************************************************************/
- void fatal(char *str)
- {
- fprintf(stderr, "fatal: %s\n", str);
- exit(1);
- }
-
- /******************************************************************
- * E R R O R
- * ---------
- * Description:
- * Give error message.
- *
- ******************************************************************/
- void error(char *str)
- {
- static int old_line;
- /*
- * Maximize to one error per line.
- */
- if (line > old_line) {
- n_errors++;
- old_line = line;
- fprintf(stdout, "error line %d: %s\n", line, str);
- }
- }
-
- /******************************************************************
- * _ M E M A L L O C
- * -----------------
- * Description:
- * Allocate memory with check. If no success just abort.
- *
- * Argument:
- * file - Sourcecode filename.
- * line - Line number of sourcecode.
- * size - Size in bytes to allocate.
- *
- * Return:
- * pointer to memory allocated.
- *
- ******************************************************************/
- void *_memalloc(char *file, int line, int size)
- {
- void *p;
- if ((p = malloc(size)) == NULL) {
- fprintf(stderr, "*** %s %d out of memory ***\n", file, line);
- exit(1);
- }
- memset(p, 0, size);
-
- return p;
- }
-
- /******************************************************************
- * _ S T R D U P L I C A T E
- * -------------------------
- * Description:
- * Duplicate text string.
- *
- * Argument:
- * file - Sourcecode filename.
- * line - Line number of sourcecode.
- * str - String to duplicate.
- *
- * Return:
- * pointer to string allocated.
- *
- ******************************************************************/
- char *_strduplicate(char *file, int line, const char *str)
- {
- char *p;
- if ((p = malloc(strlen(str) + 1)) == NULL) {
- fprintf(stderr, "*** %s %d out of memory ***\n", file, line);
- exit(1);
- }
- strcpy(p, str);
-
- return p;
- }
-
- /******************************************************************
- * _ R E A L L O C M E M
- * ---------------------
- * Description:
- * Reallocate memory
- *
- * Argument:
- * file - Sourcecode filename.
- * line - Line number of sourcecode.
- * ptr - Pointer to old memory.
- * size - Size in bytes to allocate.
- *
- * Return:
- * pointer to memory allocated.
- *
- ******************************************************************/
- void *_reallocmem(char *file, int line, void *ptr, int size)
- {
- void *p;
- if ((p = realloc(ptr, size)) == NULL) {
- fprintf(stderr, "*** %s %d out of memory ***\n", file, line);
- exit(1);
- }
-
- return p;
- }
-
- /******************************************************************
- * F I N D _ N A M E
- * -----------------
- * Description:
- * Find item in list with name.
- *
- * Arguments:
- * first - First item in list.
- * name - Name to search for.
- *
- * Return:
- * pointer to item or NULL if not found.
- *
- ******************************************************************/
- struct link *
- find_name(struct link *first, char *name)
- {
- if (name == NULL) return NULL;
-
- for(; first != NULL; first = first->next) {
- if (first->name != NULL && strequ(name, first->name) == 0) break;
- }
-
- return first;
- }
-
- /******************************************************************
- * D E B U G
- * ---------
- *
- ******************************************************************/
- void
- debug(char *fmt, ...)
- {
- static char init; /* This is just a dummy address holder */
- static char *env = &init;
- static FILE *log;
- va_list arg;
- extern unsigned sleep(unsigned);
-
- if (debug_flg == 0) return;
-
- va_start(arg, fmt);
- vprintf(fmt, arg);
- printf("\n");
- fflush(stdout);
- va_end(arg);
- }
-
- /******************************************************************
- * S T R I P _Q U O T E S
- * ----------------------
- * Description:
- * Strip leading and ending quotes and return pointer
- * to new string.
- *
- * Arguments:
- * str - Pointer to string to change.
- *
- * Return:
- * Pointer to modified string.
- *
- ******************************************************************/
- char *
- strip_quotes(char *str)
- {
- if (*str == '"') str++;
- if (str[strlen(str) - 1] == '"') {
- str[strlen(str) - 1] = 0;
- }
- return str;
- }
-
- /******************************************************************
- * S T R T O U P P E R
- * -------------------
- * Description:
- * Converts a whole string to uppercase.
- *
- * Arguments:
- * str - Pointer to string.
- *
- * Return:
- * Pointer to str.
- *
- ******************************************************************/
-
- char *
- strtoupper(char *str)
- {
- char *p = str;
-
- while(*p) {
- *p = toupper(*p);
- p++;
- }
-
- return str;
- }
- /******************************************************************
- * C L E A N U P
- * -------------
- * Description:
- * Clean up and free everything and exit.
- *
- * Arguments:
- * none
- *
- * Return:
- * none
- ******************************************************************/
- #ifdef SIGNAL_INT
- int
- #else
- void
- #endif
- cleanup(int sig)
- {
- fprintf(stderr, "aborted\n");
- exit(1);
- #ifdef SIGNAL_INT
- return 0; /* Just to shut up compiler */
- #endif
- }
-