~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/3rdparty/squirrel/sq/sq.c

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      see copyright notice in squirrel.h */
 
2
 
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
#include <string.h>
 
6
#include <stdarg.h>
 
7
 
 
8
#if defined(_MSC_VER) && defined(_DEBUG)
 
9
#include <crtdbg.h>
 
10
#include <conio.h>
 
11
#endif
 
12
#include <squirrel.h>
 
13
#include <sqstdblob.h>
 
14
#include <sqstdsystem.h>
 
15
#include <sqstdio.h>
 
16
#include <sqstdmath.h>
 
17
#include <sqstdstring.h>
 
18
#include <sqstdaux.h>
 
19
 
 
20
#ifdef SQUNICODE
 
21
#define scfprintf fwprintf
 
22
#define scfopen _wfopen
 
23
#define scvprintf vwprintf
 
24
#else
 
25
#define scfprintf fprintf
 
26
#define scfopen fopen
 
27
#define scvprintf vprintf
 
28
#endif
 
29
 
 
30
 
 
31
void PrintVersionInfos();
 
32
 
 
33
#if defined(_MSC_VER) && defined(_DEBUG)
 
34
int MemAllocHook( int allocType, void *userData, size_t size, int blockType,
 
35
   long requestNumber, const unsigned char *filename, int lineNumber)
 
36
{
 
37
//      if(requestNumber==585)_asm int 3;
 
38
        return 1;
 
39
}
 
40
#endif
 
41
 
 
42
 
 
43
SQInteger quit(HSQUIRRELVM v)
 
44
{
 
45
        int *done;
 
46
        sq_getuserpointer(v,-1,(SQUserPointer*)&done);
 
47
        *done=1;
 
48
        return 0;
 
49
}
 
50
 
 
51
void printfunc(HSQUIRRELVM v,const SQChar *s,...)
 
52
{
 
53
        va_list vl;
 
54
        va_start(vl, s);
 
55
        scvprintf( s, vl);
 
56
        va_end(vl);
 
57
}
 
58
 
 
59
void PrintVersionInfos()
 
60
{
 
61
        scfprintf(stdout,_SC("%s %s (%d bits)\n"),SQUIRREL_VERSION,SQUIRREL_COPYRIGHT,sizeof(SQInteger)*8);
 
62
}
 
63
 
 
64
void PrintUsage()
 
65
{
 
66
        scfprintf(stderr,_SC("usage: sq <options> <scriptpath [args]>.\n")
 
67
                _SC("Available options are:\n")
 
68
                _SC("   -c              compiles the file to bytecode(default output 'out.cnut')\n")
 
69
                _SC("   -o              specifies output file for the -c option\n")
 
70
                _SC("   -c              compiles only\n")
 
71
                _SC("   -d              generates debug infos\n")
 
72
                _SC("   -v              displays version infos\n")
 
73
                _SC("   -h              prints help\n"));
 
74
}
 
75
 
 
76
#define _INTERACTIVE 0
 
77
#define _DONE 2
 
78
//<<FIXME>> this func is a mess
 
79
int getargs(HSQUIRRELVM v,int argc, char* argv[])
 
80
{
 
81
        int i;
 
82
        int compiles_only = 0;
 
83
        static SQChar temp[500];
 
84
        const SQChar *ret=NULL;
 
85
        char * output = NULL;
 
86
        int lineinfo=0;
 
87
        if(argc>1)
 
88
        {
 
89
                int arg=1,exitloop=0;
 
90
                while(arg < argc && !exitloop)
 
91
                {
 
92
 
 
93
                        if(argv[arg][0]=='-')
 
94
                        {
 
95
                                switch(argv[arg][1])
 
96
                                {
 
97
                                case 'd': //DEBUG(debug infos)
 
98
                                        sq_enabledebuginfo(v,1);
 
99
                                        break;
 
100
                                case 'c':
 
101
                                        compiles_only = 1;
 
102
                                        break;
 
103
                                case 'o':
 
104
                                        if(arg < argc) {
 
105
                                                arg++;
 
106
                                                output = argv[arg];
 
107
                                        }
 
108
                                        break;
 
109
                                case 'v':
 
110
                                        PrintVersionInfos();
 
111
                                        return _DONE;
 
112
 
 
113
                                case 'h':
 
114
                                        PrintVersionInfos();
 
115
                                        PrintUsage();
 
116
                                        return _DONE;
 
117
                                default:
 
118
                                        PrintVersionInfos();
 
119
                                        scprintf(_SC("unknown prameter '-%c'\n"),argv[arg][1]);
 
120
                                        PrintUsage();
 
121
                                        return _DONE;
 
122
                                }
 
123
                        }else break;
 
124
                        arg++;
 
125
                }
 
126
 
 
127
                // src file
 
128
 
 
129
                if(arg<argc) {
 
130
                        const SQChar *filename=NULL;
 
131
#ifdef SQUNICODE
 
132
                        mbstowcs(temp,argv[arg],strlen(argv[arg]));
 
133
                        filename=temp;
 
134
#else
 
135
                        filename=argv[arg];
 
136
#endif
 
137
 
 
138
                        arg++;
 
139
                        sq_pushroottable(v);
 
140
                        sq_pushstring(v,_SC("ARGS"),-1);
 
141
                        sq_newarray(v,0);
 
142
                        for(i=arg;i<argc;i++)
 
143
                        {
 
144
                                const SQChar *a;
 
145
#ifdef SQUNICODE
 
146
                                int alen=(int)strlen(argv[i]);
 
147
                                a=sq_getscratchpad(v,(int)(alen*sizeof(SQChar)));
 
148
                                mbstowcs(sq_getscratchpad(v,-1),argv[i],alen);
 
149
                                sq_getscratchpad(v,-1)[alen] = _SC('\0');
 
150
#else
 
151
                                a=argv[i];
 
152
#endif
 
153
                                sq_pushstring(v,a,-1);
 
154
 
 
155
                                sq_arrayappend(v,-2);
 
156
                        }
 
157
                        sq_createslot(v,-3);
 
158
                        sq_pop(v,1);
 
159
                        if(compiles_only) {
 
160
                                if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,SQTrue))){
 
161
                                        SQChar *outfile = _SC("out.cnut");
 
162
                                        if(output) {
 
163
#ifdef SQUNICODE
 
164
                                                int len = (int)(strlen(output)+1);
 
165
                                                mbstowcs(sq_getscratchpad(v,len*sizeof(SQChar)),output,len);
 
166
                                                outfile = sq_getscratchpad(v,-1);
 
167
#else
 
168
                                                outfile = output;
 
169
#endif
 
170
                                        }
 
171
                                        if(SQ_SUCCEEDED(sqstd_writeclosuretofile(v,outfile)))
 
172
                                                return _DONE;
 
173
                                }
 
174
                        }
 
175
                        else {
 
176
                                if(SQ_SUCCEEDED(sqstd_dofile(v,filename,SQFalse,SQTrue))) {
 
177
                                        return _DONE;
 
178
                                }
 
179
                        }
 
180
                        //if this point is reached an error occured
 
181
                        {
 
182
                                const SQChar *err;
 
183
                                sq_getlasterror(v);
 
184
                                if(SQ_SUCCEEDED(sq_getstring(v,-1,&err))) {
 
185
                                        scprintf(_SC("Error [%s]\n"),err);
 
186
                                        return _DONE;
 
187
                                }
 
188
                        }
 
189
 
 
190
                }
 
191
        }
 
192
 
 
193
        return _INTERACTIVE;
 
194
}
 
195
 
 
196
void Interactive(HSQUIRRELVM v)
 
197
{
 
198
 
 
199
#define MAXINPUT 1024
 
200
        SQChar buffer[MAXINPUT];
 
201
        SQInteger blocks =0;
 
202
        SQInteger string=0;
 
203
        SQInteger retval=0;
 
204
        SQInteger done=0;
 
205
        PrintVersionInfos();
 
206
 
 
207
        sq_pushroottable(v);
 
208
        sq_pushstring(v,_SC("quit"),-1);
 
209
        sq_pushuserpointer(v,&done);
 
210
        sq_newclosure(v,quit,1);
 
211
        sq_setparamscheck(v,1,NULL);
 
212
        sq_createslot(v,-3);
 
213
        sq_pop(v,1);
 
214
 
 
215
    while (!done)
 
216
        {
 
217
                SQInteger i = 0;
 
218
                scprintf(_SC("\nsq>"));
 
219
                for(;;) {
 
220
                        int c;
 
221
                        if(done)return;
 
222
                        c = getchar();
 
223
                        if (c == _SC('\n')) {
 
224
                                if (i>0 && buffer[i-1] == _SC('\\'))
 
225
                                {
 
226
                                        buffer[i-1] = _SC('\n');
 
227
                                }
 
228
                                else if(blocks==0)break;
 
229
                                buffer[i++] = _SC('\n');
 
230
                        }
 
231
                        else if (c==_SC('}')) {blocks--; buffer[i++] = (SQChar)c;}
 
232
                        else if(c==_SC('{') && !string){
 
233
                                        blocks++;
 
234
                                        buffer[i++] = (SQChar)c;
 
235
                        }
 
236
                        else if(c==_SC('"') || c==_SC('\'')){
 
237
                                        string=!string;
 
238
                                        buffer[i++] = (SQChar)c;
 
239
                        }
 
240
                        else if (i >= MAXINPUT-1) {
 
241
                                scfprintf(stderr, _SC("sq : input line too long\n"));
 
242
                                break;
 
243
                        }
 
244
                        else{
 
245
                                buffer[i++] = (SQChar)c;
 
246
                        }
 
247
                }
 
248
                buffer[i] = _SC('\0');
 
249
 
 
250
                if(buffer[0]==_SC('=')){
 
251
                        scsprintf(sq_getscratchpad(v,MAXINPUT),_SC("return (%s)"),&buffer[1]);
 
252
                        memcpy(buffer,sq_getscratchpad(v,-1),(scstrlen(sq_getscratchpad(v,-1))+1)*sizeof(SQChar));
 
253
                        retval=1;
 
254
                }
 
255
                i=scstrlen(buffer);
 
256
                if(i>0){
 
257
                        SQInteger oldtop=sq_gettop(v);
 
258
                        if(SQ_SUCCEEDED(sq_compilebuffer(v,buffer,i,_SC("interactive console"),SQTrue))){
 
259
                                sq_pushroottable(v);
 
260
                                if(SQ_SUCCEEDED(sq_call(v,1,retval,SQTrue)) &&  retval){
 
261
                                        scprintf(_SC("\n"));
 
262
                                        sq_pushroottable(v);
 
263
                                        sq_pushstring(v,_SC("print"),-1);
 
264
                                        sq_get(v,-2);
 
265
                                        sq_pushroottable(v);
 
266
                                        sq_push(v,-4);
 
267
                                        sq_call(v,2,SQFalse,SQTrue);
 
268
                                        retval=0;
 
269
                                        scprintf(_SC("\n"));
 
270
                                }
 
271
                        }
 
272
 
 
273
                        sq_settop(v,oldtop);
 
274
                }
 
275
        }
 
276
}
 
277
 
 
278
int main(int argc, char* argv[])
 
279
{
 
280
        HSQUIRRELVM v;
 
281
 
 
282
        const SQChar *filename=NULL;
 
283
#if defined(_MSC_VER) && defined(_DEBUG)
 
284
        _CrtSetAllocHook(MemAllocHook);
 
285
#endif
 
286
 
 
287
        v=sq_open(1024);
 
288
        sq_setprintfunc(v,printfunc);
 
289
 
 
290
        sq_pushroottable(v);
 
291
 
 
292
        sqstd_register_bloblib(v);
 
293
        sqstd_register_iolib(v);
 
294
        sqstd_register_systemlib(v);
 
295
        sqstd_register_mathlib(v);
 
296
        sqstd_register_stringlib(v);
 
297
 
 
298
        //aux library
 
299
        //sets error handlers
 
300
        sqstd_seterrorhandlers(v);
 
301
 
 
302
        //gets arguments
 
303
        switch(getargs(v,argc,argv))
 
304
        {
 
305
        case _INTERACTIVE:
 
306
                Interactive(v);
 
307
                break;
 
308
        case _DONE:
 
309
        default:
 
310
                break;
 
311
        }
 
312
 
 
313
        sq_close(v);
 
314
 
 
315
#if defined(_MSC_VER) && defined(_DEBUG)
 
316
        _getch();
 
317
        _CrtMemDumpAllObjectsSince( NULL );
 
318
#endif
 
319
        return 0;
 
320
}
 
321