1 /*------------------------------------------------------------*
2  | awka.c                                                     |
3  | copyright 1999,  Andrew Sumner                             |
4  |                                                            |
5  | This is a source file for the awka package, a translator   |
6  | of the AWK programming language to ANSI C.                 |
7  |                                                            |
8  | This program is free software; you can redistribute it     |
9  | and/or modify it under the terms of the GNU General Public |
10  | License as published by the Free Software Foundation;      |
11  | either version 2 of the License, or any later version.     |
12  |                                                            |
13  | This program is distributed in the hope that it will be    |
14  | useful, but WITHOUT ANY WARRANTY; without even the implied |
15  | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR    |
16  | PURPOSE.  See the GNU General Public License for more      |
17  | details.                                                   |
18  |                                                            |
19  | You should have received a copy of the GNU General Public  |
20  | License along with this program; if not, write to the      |
21  | Free Software Foundation, Inc., 675 Mass Ave, Cambridge,   |
22  | MA 02139, USA.                                             |
23  *-----------------------------------------------------------*/
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <stdarg.h>
30 /* #include <unistd.h> */
31 
32 #define AWKA_MAIN
33 #define TEMPBUFF_GOES_HERE
34 
35 #include "../config.h"
36 #include "awka.h"
37 #include "awka_exe.h"
38 #include "mem.h"
39 
40 char ** _arraylist = NULL;
41 int _array_no = 0, _array_allc = 0;
42 struct _fargs *_farglist = NULL;
43 int _farg_no = 0, _farg_allc = 0;
44 struct _fcalls *_fcalllist = NULL;
45 int _fcall_no = 0, _fcall_allc = 0;
46 
47 struct pc *progcode = NULL;
48 FILE *outfp;
49 int curop_id, prog_allc, prog_no, curinst, curminst;
50 char buf[4096], *curarg, *curval;
51 char begin_used=FALSE, main_used=FALSE, end_used=FALSE;
52 
53 extern int var_used;
54 extern awka_varname *varname;
55 extern char *uoutfile;
56 extern char awka_exe, awka_tmp, awka_comp;
57 extern int exe_argc;
58 extern char **exe_argv;
59 extern char **incdir, **libfile, **libdir;
60 extern int incd_used, libf_used, libd_used;
61 
62 int awka_main = 0;
63 char *awka_main_func = NULL;
64 char **vardeclare = NULL;
65 int vdec_no = 0, vdec_allc = 0;
66 
67 void initialize(int, char **);
68 
69 void
awka_error(char * fmt,...)70 awka_error(char *fmt, ...)
71 {
72   va_list args;
73 
74   va_start( args, fmt );
75   vfprintf( stderr, fmt, args );
76   va_end( args );
77 
78   exit(1);
79 }
80 
81 void
awka_warning(char * fmt,...)82 awka_warning(char *fmt, ...)
83 {
84   va_list args;
85 
86   va_start( args, fmt );
87   vfprintf( stderr, fmt, args );
88   va_end( args );
89 }
90 
91 int
add2arraylist(char * str)92 add2arraylist(char *str)
93 {
94   int i;
95   char *nstr;
96 
97   nstr = (char *) malloc(strlen(str)+5);
98   sprintf(nstr, "%s_awk", str);
99 
100   for (i=0; i<_array_no; i++)
101     if (!strcmp(_arraylist[i], nstr))
102       break;
103 
104   if (i == _array_no || _array_no == 0)
105   {
106     if (!_array_allc)
107     {
108       _array_allc = 5;
109       _arraylist = (char **) malloc(_array_allc * sizeof(char **));
110     }
111     else if (_array_no == _array_allc)
112     {
113       _array_allc += 5;
114       _arraylist = (char **) realloc(_arraylist, _array_allc * sizeof(char **));
115     }
116 
117     _arraylist[_array_no] = (char *) malloc(strlen(nstr)+1);
118     strcpy(_arraylist[_array_no], nstr);
119     _array_no++;
120     free(nstr);
121     return 1;
122   }
123   free(nstr);
124   return 0;
125 }
126 
127 int
isarray(char * var)128 isarray(char *var)
129 {
130   int i;
131 
132   for (i=0; i<_array_no; i++)
133   {
134     if (!strcmp(_arraylist[i], var))
135       return 1;
136   }
137   return 0;
138 }
139 
140 int
main(int argc,char * argv[])141 main(int argc, char *argv[])
142 {
143   int i = 1, len;
144   char *c_file, *tmp;
145 
146   progcode = (struct pc *) malloc(20 * sizeof(struct pc));
147   prog_allc = 20;
148   prog_no = 0;
149 
150   initialize(argc, argv);
151   parse();
152 
153   if (awka_comp) awka_tmp = FALSE;
154 
155   if (awka_exe || awka_comp)
156   {
157     if (awka_tmp)
158     {
159       tmp = (char *) tempnam("./","");
160       c_file = (char *) malloc(strlen(tmp) + 3);
161       sprintf(c_file, "%s.c",tmp);
162       free(tmp);
163       if (!(outfp = fopen(c_file, "w")))
164         awka_error("Failed to open temporary output file.\n");
165     }
166     else
167     {
168       if (uoutfile)
169       {
170         c_file = (char *) malloc(strlen(uoutfile)+3);
171         sprintf(c_file, "%s.c", uoutfile);
172       }
173       else
174       {
175         c_file = (char *) malloc(11);
176         strcpy(c_file, "awka_out.c");
177       }
178       if (!(outfp = fopen(c_file, "w")))
179         awka_error("Failed to open awka_out.c in current directory.\n");
180     }
181   }
182   else
183     outfp = stdout;
184 
185   if (!prog_no)
186     awka_error("Sorry, program was not parsed successfully.\n");
187 
188   translate();
189 
190   if (awka_exe || awka_comp)
191   {
192     FILE *fp;
193     char *cmd;
194     char *outfile;
195     int incd_len=0, libd_len=0, libf_len=0;
196 
197     if (awka_tmp)
198       outfile = (char *) tempnam("./", "");
199     else if (uoutfile)
200       outfile = uoutfile;
201     else
202     {
203       outfile = (char *) malloc(15);
204 #if defined(__CYGWIN32__) || defined(__DJGPP__)
205       strcpy(outfile, "./awka_out.exe");
206 #else
207       strcpy(outfile, "./awka.out");
208 #endif
209     }
210 
211     fclose(outfp);
212 
213     for (i=0; i<libd_used; i++)
214       libd_len += strlen(libdir[i])+3;
215     for (i=0; i<libf_used; i++)
216       libf_len += strlen(libfile[i])+3;
217     for (i=0; i<incd_used; i++)
218       incd_len += strlen(incdir[i])+3;
219 
220     cmd = (char *) malloc( strlen(c_file) + strlen(awka_INCDIR) + strlen(awka_LIBDIR) + strlen(awka_CC) + strlen(awka_CFLAGS) + strlen(awka_MATHLIB) + strlen(awka_SOCKET_LIBS) + strlen(outfile) + incd_len + libd_len + libf_len + 35 );
221 
222     sprintf(cmd, "%s %s %s -I%s -L%s -lawka", awka_CC, awka_CFLAGS, c_file, awka_INCDIR, awka_LIBDIR);
223 
224     for (i=0; i<incd_used; i++)
225       sprintf(cmd, "%s -I%s", cmd, incdir[i]);
226     for (i=0; i<libd_used; i++)
227       sprintf(cmd, "%s -L%s", cmd, libdir[i]);
228     for (i=0; i<libf_used; i++)
229       sprintf(cmd, "%s -l%s", cmd, libfile[i]);
230 
231     if (strlen(awka_SOCKET_LIBS))
232       sprintf(cmd, "%s %s", cmd, awka_SOCKET_LIBS);
233 
234     if (strlen(awka_MATHLIB))
235       sprintf(cmd, "%s %s -o %s", cmd, awka_MATHLIB, outfile);
236     else
237       sprintf(cmd, "%s -o %s", cmd, outfile);
238 
239     system(cmd);
240 
241     if (!(fp = fopen(outfile, "r")))
242     {
243       if (awka_tmp)
244         unlink(c_file);
245       awka_error("Awka error: compile failed.\n");
246     }
247     fclose(fp);
248 
249     if (awka_exe)
250     {
251       len = strlen(outfile);
252       for (i=0; i<exe_argc; i++)
253         len += strlen(exe_argv[i]) + 2;
254 
255       cmd = (char *) realloc(cmd, len + 1);
256       strcpy(cmd, outfile);
257       for (i=0; i<exe_argc; i++)
258       {
259         strcat(cmd, " ");
260         strcat(cmd, exe_argv[i]);
261       }
262 
263       system(cmd);
264     }
265 
266     if (awka_tmp)
267     {
268       unlink(outfile);
269       unlink(c_file);
270     }
271   }
272 
273   return 0;
274 }
275