forked from patrickschulz/openPCells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
229 lines (201 loc) · 5.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
** $Id: lua.c $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/
#include "lua/lprefix.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include "lua/lua.h"
#include "lua/lauxlib.h"
#include "lua/lualib.h"
#include <math.h>
#include <ctype.h>
#include <string.h>
#include "lpoint.h"
#include "lload.h"
#include "lbind.h"
#include "config.h"
#define MAINPROGNAME "main.lua"
#define TESTPROGNAME "testsuite/main.lua"
//static lua_State* globalL = NULL;
/*
** Hook set by signal function to stop the interpreter.
*/
//static void lstop (lua_State* L, lua_Debug* ar) {
// (void)ar; /* unused arg. */
// lua_sethook(L, NULL, 0, 0); /* reset hook */
// luaL_error(L, "interrupted!");
//}
/*
** Function to be called at a C signal. Because a C signal cannot
** just change a Lua state (as there is no proper synchronization),
** this function only sets a hook that, when called, will stop the
** interpreter.
*/
//static void laction (int i) {
// int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
// signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
// lua_sethook(globalL, lstop, flag, 1);
//}
/*
** Message handler used to run all chunks
*/
static int msghandler (lua_State* L)
{
const char* msg = lua_tostring(L, 1);
if (msg == NULL) /* is error object not a string? */
{
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
{
return 1; /* that is the message */
}
else
{
msg = lua_pushfstring(L, "(error object is a %s value)",
luaL_typename(L, 1));
}
}
luaL_traceback(L, L, msg, 2);
return 1;
}
static void load_api(lua_State* L)
{
char* modules[] = {
"config",
"object",
"shape",
"geometry",
"graphics",
"pcell",
"generics",
"stringfile",
"util",
"aux",
"funcobject",
"reduce",
"stack",
"abstract", // this must be loaded AFTER geometry and graphics. Just leave this at the end
NULL
};
char** ptr = modules;
lua_getglobal(L, "_load_module");
while(*ptr)
{
lua_pushvalue(L, -1); // copy _load_module
lua_pushstring(L, *ptr);
if(lua_pcall(L, 1, 1, 0) == LUA_OK)
{
lua_setglobal(L, *ptr);
}
else
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_close(L);
exit(1);
}
++ptr;
}
lua_pop(L, 1); // remove _load_module
}
static void create_argument_table(lua_State* L, int argc, char** argv)
{
lua_newtable(L);
int i;
for(i = 1; i < argc; ++i)
{
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
}
lua_setglobal(L, "arg");
}
static int call_main_program(lua_State* L, const char* filename)
{
int status = luaL_loadfile(L, filename);
if (status == LUA_OK) {
lua_pushcfunction(L, msghandler);
lua_insert(L, 1);
status = lua_pcall(L, 0, 0, 1);
}
if(status != LUA_OK)
{
const char* msg = lua_tostring(L, -1);
fprintf(stderr, "%s\n", msg);
lua_pop(L, 1);
return LUA_ERRRUN;
}
return LUA_OK;
}
lua_State* create_and_initialize_lua()
{
lua_State* L = luaL_newstate();
if (L == NULL)
{
fprintf(stderr, "%s\n", "cannot create state: not enough memory");
exit(EXIT_FAILURE);
}
// lua libraries
luaL_openlibs(L);
// opc libraries
open_lpoint_lib(L);
open_lload_lib(L);
open_lbind_lib(L);
load_api(L); // could fail
return L;
}
int main (int argc, char** argv)
{
lua_State* L = create_and_initialize_lua();
int status = LUA_OK;
if(argc > 1 && (strcmp(argv[1], "test") == 0))
{
create_argument_table(L, argc - 1, argv + 1); // remove 'test' from arguments
status = call_main_program(L, OPC_HOME "/" TESTPROGNAME);
}
else if(argc > 1 && (strcmp(argv[1], "watch") == 0))
{
// remove 'watch' from arguments
argc = argc - 1;
argv = argv + 1;
pid_t pid = fork();
if(pid == 0) // child
{
while(1)
{
create_argument_table(L, argc, argv);
status = call_main_program(L, OPC_HOME "/" MAINPROGNAME);
// now reinitialize the program
// this works as if the program had beed started again, which is what we want
sleep(1);
lua_close(L);
L = create_and_initialize_lua();
}
}
else // parent
{
printf("created child process (pid: %d)\nyou have to kill it manually once you're done\n", pid);
}
}
else if(argc > 2 && (strcmp(argv[1], "run") == 0))
{
const char* filename = argv[2];
// remove 'watch' from arguments
argc = argc - 2;
argv = argv + 2;
create_argument_table(L, argc, argv);
char path[200];
snprintf(path, 200, "%s/%s", OPC_HOME, filename);
status = call_main_program(L, path);
}
else
{
create_argument_table(L, argc, argv);
status = call_main_program(L, OPC_HOME "/" MAINPROGNAME);
}
lua_close(L);
return status == LUA_OK ? 0 : 1;
}