The Squirrel interpreter. See http://www.squirrel-lang.org/
sqstdlib/sqstdsystem.cpp@0:97a4f8cc534c, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 10:20:34 2014 +0000
- Revision:
- 0:97a4f8cc534c
Initial import of Squirrel.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | /* see copyright notice in squirrel.h */ |
jhnwkmn | 0:97a4f8cc534c | 2 | #include <squirrel.h> |
jhnwkmn | 0:97a4f8cc534c | 3 | #include <time.h> |
jhnwkmn | 0:97a4f8cc534c | 4 | #include <stdlib.h> |
jhnwkmn | 0:97a4f8cc534c | 5 | #include <stdio.h> |
jhnwkmn | 0:97a4f8cc534c | 6 | #include <sqstdsystem.h> |
jhnwkmn | 0:97a4f8cc534c | 7 | |
jhnwkmn | 0:97a4f8cc534c | 8 | #ifdef SQUNICODE |
jhnwkmn | 0:97a4f8cc534c | 9 | #include <wchar.h> |
jhnwkmn | 0:97a4f8cc534c | 10 | #define scgetenv _wgetenv |
jhnwkmn | 0:97a4f8cc534c | 11 | #define scsystem _wsystem |
jhnwkmn | 0:97a4f8cc534c | 12 | #define scasctime _wasctime |
jhnwkmn | 0:97a4f8cc534c | 13 | #define scremove _wremove |
jhnwkmn | 0:97a4f8cc534c | 14 | #define screname _wrename |
jhnwkmn | 0:97a4f8cc534c | 15 | #else |
jhnwkmn | 0:97a4f8cc534c | 16 | #define scgetenv getenv |
jhnwkmn | 0:97a4f8cc534c | 17 | #define scsystem system |
jhnwkmn | 0:97a4f8cc534c | 18 | #define scasctime asctime |
jhnwkmn | 0:97a4f8cc534c | 19 | #define scremove remove |
jhnwkmn | 0:97a4f8cc534c | 20 | #define screname rename |
jhnwkmn | 0:97a4f8cc534c | 21 | #endif |
jhnwkmn | 0:97a4f8cc534c | 22 | |
jhnwkmn | 0:97a4f8cc534c | 23 | static SQInteger _system_getenv(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 24 | { |
jhnwkmn | 0:97a4f8cc534c | 25 | const SQChar *s; |
jhnwkmn | 0:97a4f8cc534c | 26 | if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){ |
jhnwkmn | 0:97a4f8cc534c | 27 | sq_pushstring(v,scgetenv(s),-1); |
jhnwkmn | 0:97a4f8cc534c | 28 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 29 | } |
jhnwkmn | 0:97a4f8cc534c | 30 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 31 | } |
jhnwkmn | 0:97a4f8cc534c | 32 | |
jhnwkmn | 0:97a4f8cc534c | 33 | |
jhnwkmn | 0:97a4f8cc534c | 34 | static SQInteger _system_system(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 35 | { |
jhnwkmn | 0:97a4f8cc534c | 36 | const SQChar *s; |
jhnwkmn | 0:97a4f8cc534c | 37 | if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){ |
jhnwkmn | 0:97a4f8cc534c | 38 | sq_pushinteger(v,scsystem(s)); |
jhnwkmn | 0:97a4f8cc534c | 39 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 40 | } |
jhnwkmn | 0:97a4f8cc534c | 41 | return sq_throwerror(v,_SC("wrong param")); |
jhnwkmn | 0:97a4f8cc534c | 42 | } |
jhnwkmn | 0:97a4f8cc534c | 43 | |
jhnwkmn | 0:97a4f8cc534c | 44 | |
jhnwkmn | 0:97a4f8cc534c | 45 | static SQInteger _system_clock(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 46 | { |
jhnwkmn | 0:97a4f8cc534c | 47 | sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC); |
jhnwkmn | 0:97a4f8cc534c | 48 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 49 | } |
jhnwkmn | 0:97a4f8cc534c | 50 | |
jhnwkmn | 0:97a4f8cc534c | 51 | static SQInteger _system_time(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 52 | { |
jhnwkmn | 0:97a4f8cc534c | 53 | time_t t; |
jhnwkmn | 0:97a4f8cc534c | 54 | time(&t); |
jhnwkmn | 0:97a4f8cc534c | 55 | sq_pushinteger(v,*((SQInteger *)&t)); |
jhnwkmn | 0:97a4f8cc534c | 56 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 57 | } |
jhnwkmn | 0:97a4f8cc534c | 58 | |
jhnwkmn | 0:97a4f8cc534c | 59 | static SQInteger _system_remove(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 60 | { |
jhnwkmn | 0:97a4f8cc534c | 61 | const SQChar *s; |
jhnwkmn | 0:97a4f8cc534c | 62 | sq_getstring(v,2,&s); |
jhnwkmn | 0:97a4f8cc534c | 63 | if(scremove(s)==-1) |
jhnwkmn | 0:97a4f8cc534c | 64 | return sq_throwerror(v,_SC("remove() failed")); |
jhnwkmn | 0:97a4f8cc534c | 65 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 66 | } |
jhnwkmn | 0:97a4f8cc534c | 67 | |
jhnwkmn | 0:97a4f8cc534c | 68 | static SQInteger _system_rename(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 69 | { |
jhnwkmn | 0:97a4f8cc534c | 70 | const SQChar *oldn,*newn; |
jhnwkmn | 0:97a4f8cc534c | 71 | sq_getstring(v,2,&oldn); |
jhnwkmn | 0:97a4f8cc534c | 72 | sq_getstring(v,3,&newn); |
jhnwkmn | 0:97a4f8cc534c | 73 | if(screname(oldn,newn)==-1) |
jhnwkmn | 0:97a4f8cc534c | 74 | return sq_throwerror(v,_SC("rename() failed")); |
jhnwkmn | 0:97a4f8cc534c | 75 | return 0; |
jhnwkmn | 0:97a4f8cc534c | 76 | } |
jhnwkmn | 0:97a4f8cc534c | 77 | |
jhnwkmn | 0:97a4f8cc534c | 78 | static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val) |
jhnwkmn | 0:97a4f8cc534c | 79 | { |
jhnwkmn | 0:97a4f8cc534c | 80 | sq_pushstring(v,name,-1); |
jhnwkmn | 0:97a4f8cc534c | 81 | sq_pushinteger(v,val); |
jhnwkmn | 0:97a4f8cc534c | 82 | sq_rawset(v,-3); |
jhnwkmn | 0:97a4f8cc534c | 83 | } |
jhnwkmn | 0:97a4f8cc534c | 84 | |
jhnwkmn | 0:97a4f8cc534c | 85 | static SQInteger _system_date(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 86 | { |
jhnwkmn | 0:97a4f8cc534c | 87 | time_t t; |
jhnwkmn | 0:97a4f8cc534c | 88 | SQInteger it; |
jhnwkmn | 0:97a4f8cc534c | 89 | SQInteger format = 'l'; |
jhnwkmn | 0:97a4f8cc534c | 90 | if(sq_gettop(v) > 1) { |
jhnwkmn | 0:97a4f8cc534c | 91 | sq_getinteger(v,2,&it); |
jhnwkmn | 0:97a4f8cc534c | 92 | t = it; |
jhnwkmn | 0:97a4f8cc534c | 93 | if(sq_gettop(v) > 2) { |
jhnwkmn | 0:97a4f8cc534c | 94 | sq_getinteger(v,3,(SQInteger*)&format); |
jhnwkmn | 0:97a4f8cc534c | 95 | } |
jhnwkmn | 0:97a4f8cc534c | 96 | } |
jhnwkmn | 0:97a4f8cc534c | 97 | else { |
jhnwkmn | 0:97a4f8cc534c | 98 | time(&t); |
jhnwkmn | 0:97a4f8cc534c | 99 | } |
jhnwkmn | 0:97a4f8cc534c | 100 | tm *date; |
jhnwkmn | 0:97a4f8cc534c | 101 | if(format == 'u') |
jhnwkmn | 0:97a4f8cc534c | 102 | date = gmtime(&t); |
jhnwkmn | 0:97a4f8cc534c | 103 | else |
jhnwkmn | 0:97a4f8cc534c | 104 | date = localtime(&t); |
jhnwkmn | 0:97a4f8cc534c | 105 | if(!date) |
jhnwkmn | 0:97a4f8cc534c | 106 | return sq_throwerror(v,_SC("crt api failure")); |
jhnwkmn | 0:97a4f8cc534c | 107 | sq_newtable(v); |
jhnwkmn | 0:97a4f8cc534c | 108 | _set_integer_slot(v, _SC("sec"), date->tm_sec); |
jhnwkmn | 0:97a4f8cc534c | 109 | _set_integer_slot(v, _SC("min"), date->tm_min); |
jhnwkmn | 0:97a4f8cc534c | 110 | _set_integer_slot(v, _SC("hour"), date->tm_hour); |
jhnwkmn | 0:97a4f8cc534c | 111 | _set_integer_slot(v, _SC("day"), date->tm_mday); |
jhnwkmn | 0:97a4f8cc534c | 112 | _set_integer_slot(v, _SC("month"), date->tm_mon); |
jhnwkmn | 0:97a4f8cc534c | 113 | _set_integer_slot(v, _SC("year"), date->tm_year+1900); |
jhnwkmn | 0:97a4f8cc534c | 114 | _set_integer_slot(v, _SC("wday"), date->tm_wday); |
jhnwkmn | 0:97a4f8cc534c | 115 | _set_integer_slot(v, _SC("yday"), date->tm_yday); |
jhnwkmn | 0:97a4f8cc534c | 116 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 117 | } |
jhnwkmn | 0:97a4f8cc534c | 118 | |
jhnwkmn | 0:97a4f8cc534c | 119 | |
jhnwkmn | 0:97a4f8cc534c | 120 | |
jhnwkmn | 0:97a4f8cc534c | 121 | #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask} |
jhnwkmn | 0:97a4f8cc534c | 122 | static SQRegFunction systemlib_funcs[]={ |
jhnwkmn | 0:97a4f8cc534c | 123 | _DECL_FUNC(getenv,2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 124 | _DECL_FUNC(system,2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 125 | _DECL_FUNC(clock,0,NULL), |
jhnwkmn | 0:97a4f8cc534c | 126 | _DECL_FUNC(time,1,NULL), |
jhnwkmn | 0:97a4f8cc534c | 127 | _DECL_FUNC(date,-1,_SC(".nn")), |
jhnwkmn | 0:97a4f8cc534c | 128 | _DECL_FUNC(remove,2,_SC(".s")), |
jhnwkmn | 0:97a4f8cc534c | 129 | _DECL_FUNC(rename,3,_SC(".ss")), |
jhnwkmn | 0:97a4f8cc534c | 130 | {0,0} |
jhnwkmn | 0:97a4f8cc534c | 131 | }; |
jhnwkmn | 0:97a4f8cc534c | 132 | #undef _DECL_FUNC |
jhnwkmn | 0:97a4f8cc534c | 133 | |
jhnwkmn | 0:97a4f8cc534c | 134 | SQInteger sqstd_register_systemlib(HSQUIRRELVM v) |
jhnwkmn | 0:97a4f8cc534c | 135 | { |
jhnwkmn | 0:97a4f8cc534c | 136 | SQInteger i=0; |
jhnwkmn | 0:97a4f8cc534c | 137 | while(systemlib_funcs[i].name!=0) |
jhnwkmn | 0:97a4f8cc534c | 138 | { |
jhnwkmn | 0:97a4f8cc534c | 139 | sq_pushstring(v,systemlib_funcs[i].name,-1); |
jhnwkmn | 0:97a4f8cc534c | 140 | sq_newclosure(v,systemlib_funcs[i].f,0); |
jhnwkmn | 0:97a4f8cc534c | 141 | sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask); |
jhnwkmn | 0:97a4f8cc534c | 142 | sq_setnativeclosurename(v,-1,systemlib_funcs[i].name); |
jhnwkmn | 0:97a4f8cc534c | 143 | sq_newslot(v,-3,SQFalse); |
jhnwkmn | 0:97a4f8cc534c | 144 | i++; |
jhnwkmn | 0:97a4f8cc534c | 145 | } |
jhnwkmn | 0:97a4f8cc534c | 146 | return 1; |
jhnwkmn | 0:97a4f8cc534c | 147 | } |