]> pd.if.org Git - pccts/blob - dlg/support.c
auto commit for import
[pccts] / dlg / support.c
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-1995
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 internal_error(s,file,line)
44 char *s,*file;
45 int line;
46 {
47         fprintf(stderr,s,file,line);
48         exit(PCCTS_EXIT_FAILURE);
49 }
50
51 char *dlg_malloc(bytes,file,line)
52 int bytes;
53 char *file;
54 int line;
55 {
56         char *t;
57
58         t = (char *) malloc(bytes);
59         if (!t){
60                 /* error */
61                 internal_error("%s(%d): unable to allocate memory\n",
62                         file,line);
63         }
64         return t;
65 }
66
67
68 char *dlg_calloc(n,bytes,file,line)
69 int n,bytes;
70 char *file;
71 int line;
72 {
73         char *t;
74
75         t = (char *) calloc(n,bytes);
76         if (!t){
77                 /* error */
78                 internal_error("%s(%d): unable to allocate memory\n",
79                         file,line);
80         }
81         return t;
82 }
83
84
85 FILE *read_stream(name)
86 char *name;
87 {
88         FILE *f;
89
90         if (name){
91                 if (name[0] == '-') {
92                         fprintf(stderr, "dlg: invalid option: '%s'\n", name);
93                         f = NULL;
94                 }else{
95                         f = fopen(name, "r");
96                         if (f == NULL){
97                                 /* couldn't open file */
98                                 fprintf(stderr,
99                                         "dlg: Warning: Can't read file %s.\n",
100                                         name);
101                         }
102                 }
103         }else{
104                 /* open stdin if nothing there */
105                 f = stdin;
106         }
107         return f;
108 }
109
110 FILE *write_stream(name)
111 char *name;
112 {
113         FILE *f;
114
115         if (name){
116                 if (name[0] == '-') {
117                         fprintf(stderr, "dlg: invalid option: '%s'\n", name);
118                         f = NULL;
119                 }else{
120                         f = fopen(OutMetaName(name), "w");
121                         if (f == NULL){
122                                 /* couldn't open file */
123                                 fprintf(stderr,
124                                         "dlg: Warning: Can't write to file %s.\n",
125                                         name);
126                         }
127                         else
128                 special_fopen_actions(OutMetaName(name));
129                 }
130         }else{
131                 /* open stdout if nothing there */
132                 f = stdout;
133         }
134         return f;
135 }
136
137
138 void fatal(message,line_no)
139 char *message;
140 int line_no;
141 {
142         fprintf(stderr,ErrHdr,
143                 (file_str[0] ? file_str[0] : "stdin"), line_no);
144         fprintf(stderr, " Fatal: %s\n", message);
145         exit(PCCTS_EXIT_FAILURE);
146 }
147
148 void error(message,line_no)
149 char *message;
150 int line_no;
151 {
152         fprintf(stderr,ErrHdr,
153                 (file_str[0] ? file_str[0] : "stdin"), line_no);
154         fprintf(stderr, " Error: %s\n", message);
155         err_found = 1;
156 }
157
158 void warning(message,line_no)
159 char *message;
160 int line_no;
161 {
162         fprintf(stderr,ErrHdr,
163                 (file_str[0] ? file_str[0] : "stdin"), line_no);
164         fprintf(stderr, " Warning: %s\n", message);
165 }
166
167 char *
168 #ifdef __STDC__
169 OutMetaName(char *n)
170 #else
171 OutMetaName(n)
172 char *n;
173 #endif
174 {
175         static char buf[200+1];
176
177         if ( strcmp(outdir,TopDirectory)==0 ) return n;
178         strcpy(buf, outdir);
179         if ( strcmp(&buf[strlen(buf) - 1], DirectorySymbol ) )
180                 strcat(buf, DirectorySymbol);
181         strcat(buf, n);
182         return buf;
183 }