Home | History | Annotate | Download | only in dlg
      1 /*
      2  * SOFTWARE RIGHTS
      3  *
      4  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
      5  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
      6  * company may do whatever they wish with source code distributed with
      7  * PCCTS or the code generated by PCCTS, including the incorporation of
      8  * PCCTS, or its output, into commerical software.
      9  *
     10  * We encourage users to develop software with PCCTS.  However, we do ask
     11  * that credit is given to us for developing PCCTS.  By "credit",
     12  * we mean that if you incorporate our source code into one of your
     13  * programs (commercial product, research project, or otherwise) that you
     14  * acknowledge this fact somewhere in the documentation, research report,
     15  * etc...  If you like PCCTS and have developed a nice tool with the
     16  * output, please mention that you developed it using PCCTS.  In
     17  * addition, we ask that this header remain intact in our source code.
     18  * As long as these guidelines are kept, we expect to continue enhancing
     19  * this system and expect to make other tools available as they are
     20  * completed.
     21  *
     22  * DLG 1.33
     23  * Will Cohen
     24  * With mods by Terence Parr; AHPCRC, University of Minnesota
     25  * 1989-2001
     26  */
     27 
     28 #include <stdio.h>
     29 #include <string.h>
     30 #include "dlg.h"
     31 #ifdef MEMCHK
     32 #include "trax.h"
     33 #else
     34 #ifdef __STDC__
     35 #include <stdlib.h>
     36 #else
     37 #include <malloc.h>
     38 #endif /* __STDC__ */
     39 #endif
     40 
     41 int	err_found = 0;			/* indicates whether problem found */
     42 
     43 #ifdef __USE_PROTOS
     44 void internal_error(char *s, char *file,int line)    /* MR9 23-Sep-97 */
     45 #else
     46 void internal_error(s,file,line)    /* MR9 23-Sep-97 */
     47 char *s,*file;
     48 int line;
     49 #endif
     50 {
     51 	fprintf(stderr,s,file,line);
     52 	exit(PCCTS_EXIT_FAILURE);
     53 }
     54 
     55 #ifdef __USE_PROTOS
     56 char *dlg_malloc(int bytes,char *file,int line)
     57 #else
     58 char *dlg_malloc(bytes,file,line)
     59 int bytes;
     60 char *file;
     61 int line;
     62 #endif
     63 {
     64 	char *t;
     65 
     66 	t = (char *) malloc(bytes);
     67 	if (!t){
     68 		/* error */
     69 		internal_error("%s(%d): unable to allocate memory\n",
     70 			file,line);
     71 	}
     72 	return t;
     73 }
     74 
     75 
     76 #ifdef __USE_PROTOS
     77 char *dlg_calloc(int n,int bytes,char *file,int line)
     78 #else
     79 char *dlg_calloc(n,bytes,file,line)
     80 int n,bytes;
     81 char *file;
     82 int line;
     83 #endif
     84 {
     85 	char *t;
     86 
     87 	t = (char *) calloc(n,bytes);
     88 	if (!t){
     89 		/* error */
     90 		internal_error("%s(%d): unable to allocate memory\n",
     91 			file,line);
     92 	}
     93 	return t;
     94 }
     95 
     96 
     97 #ifdef __USE_PROTOS
     98 FILE *read_stream(char *name)
     99 #else
    100 FILE *read_stream(name)
    101 char *name;
    102 #endif
    103 {
    104 	FILE *f;
    105 
    106 	if (name){
    107 		if (name[0] == '-') {
    108 			fprintf(stderr, "dlg: invalid option: '%s'\n", name);
    109 			f = NULL;
    110 		}else{
    111 			f = fopen(name, "r");
    112 			if (f == NULL){
    113 				/* couldn't open file */
    114 				fprintf(stderr,
    115 					"dlg: Warning: Can't read file %s.\n",
    116 					name);
    117 			}
    118 		}
    119 	}else{
    120 		/* open stdin if nothing there */
    121 		f = stdin;
    122 	}
    123 	return f;
    124 }
    125 
    126 #ifdef __USE_PROTOS
    127 FILE *write_stream(char *name)
    128 #else
    129 FILE *write_stream(name)
    130 char *name;
    131 #endif
    132 {
    133 	FILE *f;
    134 
    135 	if (name){
    136 		if (name[0] == '-') {
    137 			fprintf(stderr, "dlg: invalid option: '%s'\n", name);
    138 			f = NULL;
    139 		}else{
    140 			f = fopen(OutMetaName(name), "w");
    141 			if (f == NULL){
    142 				/* couldn't open file */
    143 				fprintf(stderr,
    144 					"dlg: Warning: Can't write to file %s.\n",
    145 					name);
    146 			}
    147 			else
    148 #ifdef SPECIAL_FOPEN
    149                 special_fopen_actions(OutMetaName(name));	/* MR1 */
    150 #else
    151 		;						/* MR1 */
    152 #endif
    153 		}
    154 	}else{
    155 		/* open stdout if nothing there */
    156 		f = stdout;
    157 	}
    158 	return f;
    159 }
    160 
    161 
    162 #ifdef __USE_PROTOS
    163 void fatal(char *message,int line_no)
    164 #else
    165 void fatal(message,line_no)
    166 char *message;
    167 int line_no;
    168 #endif
    169 {
    170 	fprintf(stderr,ErrHdr,
    171 		(file_str[0] ? file_str[0] : "stdin"), line_no);
    172 	fprintf(stderr, " Fatal: %s\n", message);
    173 	exit(PCCTS_EXIT_FAILURE);
    174 }
    175 
    176 #ifdef __USE_PROTOS
    177 void error(char *message,int line_no)
    178 #else
    179 void error(message,line_no)
    180 char *message;
    181 int line_no;
    182 #endif
    183 {
    184 	fprintf(stderr,ErrHdr,
    185 		(file_str[0] ? file_str[0] : "stdin"), line_no);
    186 	fprintf(stderr, " Error: %s\n", message);
    187 	err_found = 1;
    188 }
    189 
    190 #ifdef __USE_PROTOS
    191 void warning(char *message,int line_no)
    192 #else
    193 void warning(message,line_no)
    194 char *message;
    195 int line_no;
    196 #endif
    197 {
    198 	fprintf(stderr,ErrHdr,
    199 		(file_str[0] ? file_str[0] : "stdin"), line_no);
    200 	fprintf(stderr, " Warning: %s\n", message);
    201 }
    202 
    203 /* MR10: Jeff Vincent
    204    MR10: Changed to remove directory information from n only if
    205    MR10: if OutputDirectory was changed by user (-o option)
    206 */
    207 
    208 #ifdef __USE_PROTOS
    209 char *OutMetaName(char *n)
    210 #else
    211 char *OutMetaName(n)
    212 char *n;
    213 #endif
    214 {
    215     static char *dir_sym = DirectorySymbol;
    216     static char newname[MaxFileName+1];
    217     char *p;
    218 
    219 	/* If OutputDirectory is same as TopDirectory (platform default) then leave n alone. */
    220     if (strcmp(OutputDirectory, TopDirectory) == 0)
    221 		return n;
    222 
    223 	/* p will point to filename without path information */
    224 	if ((p = strrchr(n, *dir_sym)) != NULL)
    225 		p++;
    226 	else
    227 		p = n;
    228 
    229 	/* Copy new output directory into newname[] */
    230 	strcpy(newname, OutputDirectory);
    231 
    232 	/* if new output directory does not have trailing dir_sym, add it! */
    233 	if (newname[strlen(newname)-1] != *dir_sym)
    234 		strcat(newname, dir_sym);
    235 
    236 	/* contatenate FILE NAME ONLY to new output directory */
    237 	strcat(newname, p);
    238 
    239 	return newname;
    240 }
    241