root/trunk/thune/console.c

Revision 559, 7.5 kB (checked in by krobillard, 5 weeks ago)

CallOp? cleared after halt in console.
'now accepts date! selector.
Network port now sets addr size even if created without initial address.

Line 
1/*============================================================================
2    Thune Interpreter
3    Copyright (C) 2005-2007  Karl Robillard
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18===========================================================================*/
19
20
21#include <assert.h>
22#include <stdio.h>
23#include "urlan.h"
24
25
26#if defined(LANG_RUNE)
27#define APPNAME     "Rune"
28#define CUSTOM_DT           0
29#define CUSTOM_DT_COUNT     0
30
31#elif defined(THUNE_GL)
32
33#include "gx.h"
34#define APPNAME     "Thune-GL"
35#define CUSTOM_DT           ug_datatypes
36#define CUSTOM_DT_COUNT     UG_DT_COUNT
37
38#else
39
40#define APPNAME     "Thune"
41#define CUSTOM_DT           0
42#define CUSTOM_DT_COUNT     0
43#endif
44
45
46#define ESC         27
47#define PRINT_MAX   156
48#define HALT_RESET  ut->callOp = 0
49
50
51void usage( char* arg0 )
52{
53    printf( APPNAME " %s (%s)\n\n", UR_VERSION_STR, __DATE__ );
54    printf( "Usage: %s [options] [script [script args]]\n\n", arg0 );
55    printf( "Options:\n"
56            "  -s     Disable security\n"
57            "  -h     Show this help and exit\n"
58          );
59}
60
61
62int quitResult( UThread* ut )
63{
64    UCell* val = ur_result( ut, 0 );
65    return ur_sel(val);
66}
67
68
69#if 0
70static const char* exceptionName( int type )
71{
72    switch( type )
73    {
74        case UR_EX_DATATYPE: return "Datatype";
75        case UR_EX_SCRIPT:   return "Script";
76        case UR_EX_SYNTAX:   return "Syntax";
77        case UR_EX_ACCESS:   return "Access";
78        case UR_EX_INTERNAL: return "Internal";
79        default:             return "?";
80    }
81}
82#endif
83
84
85static void reportError( UThread* ut )
86{
87    UString* str;
88#if 0
89    UCell* val;
90    val = ur_result( ut, 0 );
91    str = ur_binPtr( val->err.strN );
92
93    printf( "%s Error - %s\n",
94            exceptionName( val->err.exType ),
95            str->ptr.c );
96#else
97    str = ur_threadTmp( ut );
98    str->used = 0;
99    ur_toStr( ur_result(ut, 0), str, 0 );
100    ur_termCStr( str );
101    printf( str->ptr.c );
102#endif
103
104    //ur_threadReset( ut );
105}
106
107
108/*
109  Returns pointer in 'to' buffer at end of copy.
110*/
111char* strCopy( char* to, const char* from )
112{
113    while( *from )
114        *to++ = *from++;
115    return to;
116}
117
118
119int main( int argc, char** argv )
120{
121    UrlanEnv* env;
122    UThread* ut;
123    char cmd[ 2048 ];
124    int fileN = 0;
125    int ret = 0;
126
127
128    env = ur_makeEnv( CUSTOM_DT, CUSTOM_DT_COUNT );
129    if( ! env )
130    {
131        printf( "ur_makeEnv failed\n" );
132        return -1;
133    }
134
135#ifdef THUNE_GL
136    if( gx_startup( env, 0, 0, 0 ) != UR_EVAL_OK )
137    {
138        printf( "gx_startup failed\n" );
139        return -1;
140    }
141#endif
142
143    ur_freezeEnv( env, 128, 256 );
144    ut = ur_thread( env );
145
146    if( argc > 1 )
147    {
148        int i;
149        char* arg;
150
151        for( i = 1; i < argc; ++i )
152        {
153            arg = argv[i];
154            if( arg[0] == '-' )
155            {
156                switch( arg[1] )
157                {
158                    case 's':
159                        ur_disable( env, UR_ENV_SECURE );
160                        break;
161
162                    case 'h':
163                        usage( argv[0] );
164                        return 0;
165                }
166            }
167            else
168            {
169                fileN = i;
170                break;
171            }
172        }
173    }
174
175    if( fileN )
176    {
177        char* pos;
178
179        pos = cmd;
180        cmd[ sizeof(cmd) - 1 ] = -1;
181
182#ifdef LANG_THUNE
183        if( (argc - fileN) > 1 )
184        {
185            int i;
186            *pos++ = '[';
187            for( i = fileN + 1; i < argc; ++i )
188            {
189                if( i > 2 )
190                    *pos++ = ' ';
191                *pos++ = '"';
192                pos = strCopy( pos, argv[i] );
193                *pos++ = '"';
194            }
195            pos = strCopy( pos, "] :args " );
196        }
197        else
198        {
199            pos = strCopy( pos, "none :args " );
200        }
201#endif
202
203#ifdef LANG_RUNE
204        pos = strCopy( pos, "do load " );
205#endif
206        *pos++ = '"';
207        pos = strCopy( pos, argv[fileN] );
208        *pos++ = '"';
209#ifdef LANG_THUNE
210        pos = strCopy( pos, " load do" );
211#endif
212
213        assert( cmd[ sizeof(cmd) - 1 ] == -1 && "cmd buffer overflow" );
214
215        switch( ur_evalCStr( ut, cmd, pos - cmd ) )
216        {
217            case UR_EVAL_HALT:
218                HALT_RESET;
219                goto prompt;
220
221            case UR_EVAL_QUIT:
222                ret = quitResult( ut );
223                goto quit;
224
225            case UR_EVAL_ERROR:
226                reportError( ut );
227                goto prompt;
228        }
229    }
230    else
231    {
232        printf( APPNAME " %s (%s)\n", UR_VERSION_STR, __DATE__ );
233
234prompt:
235
236        while( 1 )
237        {
238            printf( ")> " );
239            fflush( stdout );   /* Required on Windows. */
240            fgets( cmd, 128, stdin );
241
242#if 0
243            {
244                char* cp = cmd;
245                while( *cp != '\n' )
246                    printf( " %d", (int) *cp++ );
247                printf( "\n" );
248            }
249#endif
250
251            if( cmd[0] == ESC )
252            {
253                // Up   27 91 65
254                // Down 27 91 66
255                printf( "\n" );
256            }
257            else if( cmd[0] != '\n' )
258            {
259                switch( ur_evalCStr( ut, cmd, -1 ) )
260                {
261                    case UR_EVAL_OK:
262                    {
263                        UCell* val;
264                        UString str;
265
266                        val = ur_result( ut, 0 );
267                        if( ur_is(val, UT_UNSET) ||
268                            ur_is(val, UT_CONTEXT) ||
269                            ur_is(val, UT_FUNCTION) )
270                            break;
271
272                        ur_arrayInit( &str, 1, 0 );
273                        ur_toStrT( ut, val, &str, 0 );
274                        if( str.ptr.c )
275                        {
276                            UString* sp = &str;
277                            ur_termCStr( sp );
278
279                            if( str.used > PRINT_MAX )
280                            {
281                                char* cp = strCopy( str.ptr.c + PRINT_MAX - 4,
282                                                    "..." );
283                                *cp = '\0';
284                            }
285
286                            printf( "== %s\n", str.ptr.c );
287                        }
288                        ur_arrayFree( &str );
289                    }
290                        break;
291
292                    case UR_EVAL_HALT:
293                        HALT_RESET;
294                        printf( "**halt\n" );
295                        break;
296
297                    case UR_EVAL_QUIT:
298                        ret = quitResult( ut );
299                        goto quit;
300
301                    case UR_EVAL_ERROR:
302                        reportError( ut );
303                        ur_threadReset( ut );
304                        break;
305                }
306            }
307        }
308    }
309
310quit:
311
312#ifdef THUNE_GL
313    gx_shutdown( env );
314#endif
315
316    ur_freeEnv( env );
317
318    return ret;
319}
320
321
322/*EOF*/
Note: See TracBrowser for help on using the browser.