1 /***************************************************************************
2  * This program reads a TECO macro contained in file CLPARS.TEC.  It writes
3  * CLPARS.H,  a C include file that is included by the ZPrsCL function.
4 ***************************************************************************/
5 
6 #ifdef sun
7 void	exit();			/* exit the program */
8 int	puts();			/* write string to stdout, with newline */
9 void	perror();		/* write a message on stdout */
10 int	fputs();		/* put string to a given stream */
11 int	fclose();		/* close a stream */
12 #define EXIT_SUCCESS 0
13 #define EXIT_FAILURE 1
14 #else
15 #include <stdlib.h>		/* define prototype for exit() */
16 #ifdef vax11c
17 #define EXIT_SUCCESS 1
18 #define EXIT_FAILURE 2
19 #endif
20 #endif
21 #include <stdio.h>		/* define prototypes for fopen(), etc. */
22 #include <string.h>		/* define prototype for strcpy */
23 
24 #ifndef FALSE
25 #define FALSE (1==0)
26 #endif
27 #ifndef TRUE
28 #define TRUE (1==1)
29 #endif
30 
31 typedef char BOOLEAN;
32 
33 #define ILINE_SIZE 132	/* maximum input file (CLPARS.TEC) line length */
34 #define OLINE_SIZE 75	/* maximum output file (CLPARS.H) line length */
35 
36 
37 
38 static void
39 #if USE_PROTOTYPES
open_clpars_tec(FILE ** ifile)40 open_clpars_tec(FILE **ifile)
41 #else
42 open_clpars_tec(ifile)
43 FILE **ifile;
44 #endif
45 {
46 	*ifile = fopen("clpars.tec","r");
47 	if (*ifile == NULL)
48 		{
49 		puts("genclp: Unable to open file CLPARS.TEC for reading");
50 		perror("genclp");
51 		exit(EXIT_FAILURE);
52 		}
53 }
54 
55 
56 
57 static void
58 #if USE_PROTOTYPES
open_clpars_h(FILE ** ofile)59 open_clpars_h(FILE **ofile)
60 #else
61 open_clpars_h(ofile)
62 FILE **ofile;
63 #endif
64 {
65 	*ofile = fopen("clpars.h","w");
66 	if (*ofile == NULL)
67 		{
68 		puts("genclp: Unable to open file CLPARS.C for writing");
69 		perror("genclp");
70 		exit(EXIT_FAILURE);
71 		}
72 }
73 
74 
75 
76 static void
77 #if USE_PROTOTYPES
write_line(char * obuf,FILE * ofile)78 write_line(char *obuf, FILE *ofile)
79 #else
80 write_line(obuf, ofile)
81 char *obuf;
82 FILE *ofile;
83 #endif
84 {
85 	if (fputs(obuf, ofile) == EOF)
86 		{
87 		puts("genclp: Unable to write to file CLPARS.C");
88 		perror("genclp");
89 		exit(EXIT_FAILURE);
90 		}
91 }
92 
93 
94 
95 static void
96 #if USE_PROTOTYPES
write_header(FILE * ofile)97 write_header(FILE *ofile)
98 #else
99 write_header(ofile)
100 FILE *ofile;
101 #endif
102 {
103 	write_line("/*\n", ofile);
104 	write_line(" * This file was created by the GENCLP program.\n",ofile);
105 	write_line(" * It should not be edited.  To make changes,\n", ofile);
106 	write_line(" * change the CLPARS.TES file,  then squeeze\n", ofile);
107 	write_line(" * CLPARS.TES to produce the CLPARS.TEC file,\n", ofile);
108 	write_line(" * then run GENCLP to produce this file.\n", ofile);
109 	write_line(" */\n", ofile);
110 }
111 
112 
113 
114 
115 static void
116 #if USE_PROTOTYPES
close_clpars_tec(FILE * ifile)117 close_clpars_tec(FILE *ifile)
118 #else
119 close_clpars_tec(ifile)
120 FILE *ifile;
121 #endif
122 {
123 	if (fclose(ifile) != 0)
124 		{
125 		puts("genclp: Unable to close input file CLPARS.C");
126 		perror("genclp");
127 		exit(EXIT_FAILURE);
128 		}
129 }
130 
131 
132 
133 static void
134 #if USE_PROTOTYPES
close_clpars_h(FILE * ofile)135 close_clpars_h(FILE *ofile)
136 #else
137 close_clpars_h(ofile)
138 FILE *ofile;
139 #endif
140 {
141 	if (fclose(ofile) != 0)
142 		{
143 		puts("genclp: Unable to close output file CLPARS.C");
144 		perror("genclp");
145 		exit(EXIT_FAILURE);
146 		}
147 }
148 
149 
150 
151 static void
152 #if USE_PROTOTYPES
cnvrt(FILE * ifile,FILE * ofile,BOOLEAN ANSI_style)153 cnvrt(	FILE *ifile,
154 	FILE *ofile,
155 	BOOLEAN ANSI_style)
156 #else
157 cnvrt(ifile, ofile, ANSI_style)
158 FILE *ifile;
159 FILE *ofile;
160 BOOLEAN ANSI_style;
161 #endif
162 {
163 	char *iptr;
164 	char *optr;
165 	int total_chars = 0;
166 	int total_lines = 0;
167 	char iline[ILINE_SIZE];
168 	char oline[OLINE_SIZE];
169 
170 	oline[0] = '\"';
171 	optr = &oline[1];
172 	while (fgets(iline, ILINE_SIZE, ifile) != NULL)
173 		{
174 		iptr = iline;
175 		while (*iptr != '\n')
176 			{
177 			switch (*iptr) {
178 			case '"':  *optr++ = '\\'; *optr++ = '"'; break;
179 			case '\\': *optr++ = '\\'; *optr++ = '\\'; break;
180 			case '\n': *optr++ = '\\'; *optr++ = 'n'; break;
181 			case '\r': *optr++ = '\\'; *optr++ = 'r'; break;
182 			default:
183 			    if (*iptr >= ' ')
184 				*optr++ = *iptr;
185 			    else {
186 				sprintf(optr,
187 					(ANSI_style) ? "\\%o\"\"" : "\\%03o",
188 					*iptr);
189 				optr += strlen(optr);
190 			      }
191 			}
192 			iptr++;
193 			total_chars += 1;
194 			if (optr > &oline[OLINE_SIZE-10])
195 				{
196 				*optr++ = '"';
197 				if (!ANSI_style)
198 					*optr++ = ',';
199 				*optr++ = '\n';
200 				*optr++ = '\0';
201 				write_line(oline, ofile);
202 				total_lines += 1;
203 				optr = &oline[1];
204 				}
205 			}
206 		}
207 	*optr++ = '"';
208 	*optr++ = '\n';
209 	*optr   = '\0';
210 	write_line(oline, ofile);
211 	total_lines += 1;
212 	write_line("};\n", ofile);
213 	sprintf(oline, "#define CLPARS_LEN %d\n", total_chars);
214 	write_line(oline, ofile);
215 	if (!ANSI_style)
216 		{
217 		sprintf(oline, "#define CLPARS_LINES %-3d\n", total_lines);
218 		write_line(oline, ofile);
219 		}
220 }
221 
222 
223 
224 int
225 #if USE_PROTOTYPES
main(void)226 main(void)
227 #else
228 main()
229 #endif
230 {
231 	FILE *ifile, *ofile;
232 
233 	open_clpars_h(&ofile);
234 
235 /*
236  * write "#if USE_ANSI_CLPARS" part
237  */
238 	open_clpars_tec(&ifile);
239 	write_header(ofile);
240 	write_line("#if USE_ANSI_CLPARS\n", ofile);
241 	write_line("unsigned const char clpars[] = {\n", ofile);
242 	cnvrt(ifile, ofile, TRUE);
243 	close_clpars_tec(ifile);
244 
245 /*
246  * write "#else" part
247  */
248 	open_clpars_tec(&ifile);
249 	write_line("#else\n", ofile);
250 	write_line("char *clpars[] = {\n", ofile);
251 	cnvrt(ifile, ofile, FALSE);
252 	close_clpars_tec(ifile);
253 
254 	write_line("#endif\n", ofile);
255 	close_clpars_h(ofile);
256 	return EXIT_SUCCESS;
257 }
258