]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/fteqcc-src/comprout.c
fteqcc source
[voretournament/voretournament.git] / misc / mediasource / extra / fteqcc-src / comprout.c
1 //compile routines
2
3 #include "qcc.h"
4 #undef progfuncs
5
6 char errorfile[128];
7 int errorline;
8
9 progfuncs_t *qccprogfuncs;
10
11 #include <setjmp.h>
12
13 extern int qcc_compileactive;
14 jmp_buf qcccompileerror;
15 char qcc_gamedir[128];
16 void QCC_PR_ResetErrorScope(void);
17
18
19
20 #ifdef MINIMAL
21
22 #else
23
24 int qccalloced;
25 int qcchunksize;
26 char *qcchunk;
27 void *qccHunkAlloc(size_t mem)
28 {
29         mem = (mem + 7)&~7;
30
31         qccalloced+=mem;
32         if (qccalloced > qcchunksize)
33                 QCC_Error(ERR_INTERNAL, "Compile hunk was filled");
34
35         memset(qcchunk+qccalloced-mem, 0, mem);
36         return qcchunk+qccalloced-mem;
37 }
38 void qccClearHunk(void)
39 {
40         if (qcchunk)
41         {
42                 free(qcchunk);
43                 qcchunk=NULL;
44         }
45 }
46 int qccpersisthunk;
47 void PostCompile(void)
48 {
49         if (!qccpersisthunk)
50                 qccClearHunk();
51
52         if (asmfile)
53         {
54                 fclose(asmfile);
55                 asmfile = NULL;
56         }       
57 }
58 pbool PreCompile(void)
59 {
60         QCC_PR_ResetErrorScope();
61
62         qccClearHunk();
63         strcpy(qcc_gamedir, "");
64         qcchunk = malloc(qcchunksize=128*1024*1024);
65         while(!qcchunk && qcchunksize > 8*1024*1024)
66         {
67                 qcchunksize /= 2;
68                 qcchunk = malloc(qcchunksize);
69         }
70         qccalloced=0;
71
72         return !!qcchunk;
73 }
74
75 void QCC_main (int argc, char **argv);
76 void QCC_ContinueCompile(void);
77 void QCC_FinishCompile(void);
78
79 int comp_nump;char **comp_parms;
80 //void Editor(char *fname, int line, int numparms, char **compileparms);
81 pbool CompileParams(progfuncs_t *progfuncs, int doall, int nump, char **parms)
82 {
83         comp_nump = nump;
84         comp_parms = parms;
85         *errorfile = '\0';
86         qccprogfuncs = progfuncs;
87         if (setjmp(qcccompileerror))
88         {
89                 PostCompile();
90                 if (*errorfile)
91                 {
92                         if (!externs->useeditor)
93                                 printf("Error in %s on line %i\n", errorfile, errorline);
94                         else
95                                 externs->useeditor(progfuncs, errorfile, errorline, nump, parms);
96                 }
97                 return false;
98         }
99
100         if (!PreCompile())
101                 return false;
102         QCC_main(nump, parms);
103
104         while(qcc_compileactive)
105                 QCC_ContinueCompile();
106
107         PostCompile();
108
109         return true;
110 }
111 int Comp_Begin(progfuncs_t *progfuncs, int nump, char **parms)
112 {
113         comp_nump = nump;
114         comp_parms = parms;
115         qccprogfuncs = progfuncs;
116         *errorfile = '\0';
117         if (setjmp(qcccompileerror))
118         {
119                 PostCompile();
120                 if (*errorfile)
121                         externs->useeditor(progfuncs, errorfile, errorline, nump, parms);
122                 return false;
123         }
124
125         if (!PreCompile())
126                 return false;
127         QCC_main(nump, parms);
128
129         return true;
130 }
131 int Comp_Continue(progfuncs_t *progfuncs)
132 {       
133         qccprogfuncs = progfuncs;
134         if (setjmp(qcccompileerror))
135         {
136                 PostCompile();
137                 if (*errorfile && externs->useeditor)
138                         externs->useeditor(progfuncs, errorfile, errorline, comp_nump, comp_parms);
139                 return false;
140         }
141
142         if (qcc_compileactive)
143                 QCC_ContinueCompile();
144         else
145         {
146                 PostCompile();
147
148                 if (*errorfile && externs->useeditor)
149                         externs->useeditor(progfuncs, errorfile, errorline, comp_nump, comp_parms);
150
151                 return false;
152         }
153
154         return true;
155 }
156 #endif
157 pbool CompileFile(progfuncs_t *progfuncs, char *filename)
158 {       
159 #ifdef MINIMAL
160         return false;
161 #else
162         char srcfile[32];
163         char newname[32];
164         static char *p[5];
165         int parms;
166         char *s, *s2;
167         
168         p[0] = NULL;
169         parms = 1;
170         
171         strcpy(newname, filename);
172         s = newname;
173         if (strchr(s+1, '/'))
174         {
175                 while(1)
176                 {
177                         s2 = strchr(s+1, '/');
178                         if (!s2)
179                         {
180                                 *s = '\0';
181                                 break;
182                         }
183                         s = s2;
184                 }
185                 p[parms] = "-src";
186                 p[parms+1] = newname;
187                 parms+=2;       
188                 
189                 strcpy(srcfile, s+1);
190                 srcfile[strlen(srcfile)-4] = '\0';
191                 strcat(srcfile, ".src");
192
193                 if (externs->FileSize(qcva("%s/%s", newname, srcfile))>0)
194                 {
195                         p[parms] = "-srcfile";
196                         p[parms+1] = srcfile;
197                         parms+=2;
198                 }
199         }
200         else
201         {
202                 p[parms] = "-srcfile";
203                 p[parms+1] = newname;
204                 newname[strlen(newname)-4] = '\0';
205                 strcat(newname, ".src");
206                 parms+=2;
207         }
208 //      p[2][strlen(p[2])-4] = '\0';
209 //      strcat(p[2], "/");
210
211         while (!CompileParams(progfuncs, true, parms, p))
212         {
213                 return false;
214         }
215         return true;
216 #endif
217 }
218
219 int QC_strncasecmp(const char *s1, const char *s2, int n)
220 {
221         int             c1, c2;
222         
223         while (1)
224         {
225                 c1 = *s1++;
226                 c2 = *s2++;
227
228                 if (!n--)
229                         return 0;               // strings are equal until end point
230                 
231                 if (c1 != c2)
232                 {
233                         if (c1 >= 'a' && c1 <= 'z')
234                                 c1 -= ('a' - 'A');
235                         if (c2 >= 'a' && c2 <= 'z')
236                                 c2 -= ('a' - 'A');
237                         if (c1 != c2)
238                                 return -1;              // strings not equal
239                 }
240                 if (!c1)
241                         return 0;               // strings are equal
242         }
243         
244         return -1;
245 }
246
247 void editbadfile(char *fname, int line)
248 {
249         strcpy(errorfile, fname);
250         errorline = line;
251 }
252