~ubuntu-branches/ubuntu/jaunty/luatex/jaunty

« back to all changes in this revision

Viewing changes to src/texk/web2c/luatexdir/lua/luastuff.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2007-09-24 12:56:11 UTC
  • Revision ID: james.westby@ubuntu.com-20070924125611-a8ge689azbptxvla
Tags: upstream-0.11.2
ImportĀ upstreamĀ versionĀ 0.11.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: luastuff.c,v 1.16 2005/08/10 22:21:53 hahe Exp hahe $ */
 
2
 
 
3
#include "luatex-api.h"
 
4
#include <ptexlib.h>
 
5
 
 
6
lua_State *Luas[65536];
 
7
 
 
8
extern char *startup_filename;
 
9
extern int safer_option;
 
10
 
 
11
int luastate_max = 0;
 
12
int luastate_bytes = 0;
 
13
 
 
14
void
 
15
make_table (lua_State *L, char *tab, char *getfunc, char *setfunc) {
 
16
  /* make the table */            /* [{<tex>}] */
 
17
  lua_pushstring(L,tab);          /* [{<tex>},"dimen"] */
 
18
  lua_newtable(L);                /* [{<tex>},"dimen",{}] */
 
19
  lua_settable(L, -3);            /* [{<tex>}] */
 
20
  /* fetch it back */
 
21
  lua_pushstring(L,tab);          /* [{<tex>},"dimen"] */
 
22
  lua_gettable(L, -2);            /* [{<tex>},{<dimen>}] */
 
23
  /* make the meta entries */
 
24
  luaL_newmetatable(L,tab);       /* [{<tex>},{<dimen>},{<dimen_m>}] */
 
25
  lua_pushstring(L, "__index");   /* [{<tex>},{<dimen>},{<dimen_m>},"__index"] */
 
26
  lua_pushstring(L, getfunc);     /* [{<tex>},{<dimen>},{<dimen_m>},"__index","getdimen"] */
 
27
  lua_gettable(L, -5);            /* [{<tex>},{<dimen>},{<dimen_m>},"__index",<tex.getdimen>]  */
 
28
  lua_settable(L, -3);            /* [{<tex>},{<dimen>},{<dimen_m>}]  */
 
29
  lua_pushstring(L, "__newindex");/* [{<tex>},{<dimen>},{<dimen_m>},"__newindex"] */
 
30
  lua_pushstring(L, setfunc);     /* [{<tex>},{<dimen>},{<dimen_m>},"__newindex","setdimen"] */
 
31
  lua_gettable(L, -5);            /* [{<tex>},{<dimen>},{<dimen_m>},"__newindex",<tex.setdimen>]  */
 
32
  lua_settable(L, -3);            /* [{<tex>},{<dimen>},{<dimen_m>}]  */ 
 
33
  lua_setmetatable(L,-2);         /* [{<tex>},{<dimen>}] : assign the metatable */
 
34
  lua_pop(L,1);                   /* [{<tex>}] : clean the stack */
 
35
}
 
36
 
 
37
static 
 
38
const char *getS(lua_State * L, void *ud, size_t * size) {
 
39
    LoadS *ls = (LoadS *) ud;
 
40
    (void) L;
 
41
    if (ls->size == 0)
 
42
        return NULL;
 
43
    *size = ls->size;
 
44
    ls->size = 0;
 
45
    return ls->s;
 
46
}
 
47
 
 
48
 
 
49
#ifdef WIN32
 
50
#include <process.h>
 
51
#define exec_command(a,b,c) execvpe(a,(const char* const*)b,(const char* const*)c)
 
52
#else
 
53
#include <unistd.h>
 
54
#define DEFAULT_PATH    "/bin:/usr/bin:."
 
55
 
 
56
int exec_command(const char *file, char *const *argv, char *const *envp)
 
57
{
 
58
        char path[PATH_MAX];
 
59
        const char *searchpath, *esp;
 
60
        size_t prefixlen, filelen, totallen;
 
61
 
 
62
        if (strchr(file, '/'))  /* Specific path */
 
63
                return execve(file, argv, envp);
 
64
 
 
65
        filelen = strlen(file);
 
66
 
 
67
        searchpath = getenv("PATH");
 
68
        if (!searchpath)
 
69
                searchpath = DEFAULT_PATH;
 
70
 
 
71
        errno = ENOENT;         /* Default errno, if execve() doesn't
 
72
                                   change it */
 
73
 
 
74
        do {
 
75
                esp = strchr(searchpath, ':');
 
76
                if (esp)
 
77
                        prefixlen = esp - searchpath;
 
78
                else
 
79
                        prefixlen = strlen(searchpath);
 
80
 
 
81
                if (prefixlen == 0 || searchpath[prefixlen - 1] == '/') {
 
82
                        totallen = prefixlen + filelen;
 
83
                        if (totallen >= PATH_MAX)
 
84
                                continue;
 
85
                        memcpy(path, searchpath, prefixlen);
 
86
                        memcpy(path + prefixlen, file, filelen);
 
87
                } else {
 
88
                        totallen = prefixlen + filelen + 1;
 
89
                        if (totallen >= PATH_MAX)
 
90
                                continue;
 
91
                        memcpy(path, searchpath, prefixlen);
 
92
                        path[prefixlen] = '/';
 
93
                        memcpy(path + prefixlen + 1, file, filelen);
 
94
                }
 
95
                path[totallen] = '\0';
 
96
 
 
97
                execve(path, argv, envp);
 
98
                if (errno == E2BIG  || errno == ENOEXEC ||
 
99
                    errno == ENOMEM || errno == ETXTBSY)
 
100
                        break;  /* Report this as an error, no more search */
 
101
 
 
102
                searchpath = esp + 1;
 
103
        } while (esp);
 
104
 
 
105
        return -1;
 
106
}
 
107
#endif
 
108
 
 
109
extern char **environ;
 
110
 
 
111
 
 
112
#define COMMAND_PARTS 20
 
113
 
 
114
static int
 
115
do_split_command(char *maincmd, char **cmdline, char target)
 
116
{
 
117
    char *piece;
 
118
    char *cmd;
 
119
    unsigned int i;
 
120
    int ret = 0;
 
121
    int in_string = 0;
 
122
    if (strlen(maincmd) == 0)
 
123
        return 0;
 
124
    cmd = strdup(maincmd);
 
125
    i = 0;
 
126
    while (cmd[i] == ' ')
 
127
        i++;
 
128
    piece = cmd;
 
129
    for (; i <= strlen(maincmd); i++) {
 
130
        if (in_string == 1) {
 
131
            if (cmd[i] == '"') {
 
132
                in_string = 0;
 
133
            }
 
134
        } else if (in_string == 2) {
 
135
            if (cmd[i] == '\'') {
 
136
                  in_string = 0;
 
137
            }
 
138
        } else {
 
139
            if (cmd[i] == '"') {
 
140
                in_string = 1;
 
141
            } else if (cmd[i] == '\'') {
 
142
                in_string = 2;
 
143
            } else if (cmd[i] == target) {
 
144
                cmd[i] = 0;
 
145
                if (ret == COMMAND_PARTS) {
 
146
                  fprintf(stderr,"os.exec(): Executable command too complex.\n");
 
147
                }
 
148
                cmdline[ret++] = strdup(piece);
 
149
                while (i < strlen(maincmd) && cmd[(i + 1)] == ' ')
 
150
                    i++;
 
151
                piece = cmd + i + 1;
 
152
            }
 
153
        }
 
154
    }
 
155
    if (*piece) {
 
156
          if (ret == COMMAND_PARTS) {
 
157
            fprintf(stderr, "os.exec(): Executable command too complex.\n");
 
158
          }
 
159
          cmdline[ret++] = strdup(piece);
 
160
    }
 
161
    return ret;
 
162
}
 
163
 
 
164
static int os_exec (lua_State *L) {
 
165
  char * maincmd;
 
166
  char ** cmdline;
 
167
  int i;
 
168
  maincmd =  (char *)luaL_optstring(L, 1, NULL);
 
169
  if (maincmd) {
 
170
        cmdline = malloc(sizeof(char *) * COMMAND_PARTS);
 
171
        for (i = 0; i < COMMAND_PARTS; i++) {
 
172
          cmdline[i] = NULL;
 
173
        }
 
174
        i = do_split_command(maincmd, cmdline,' ');
 
175
        if (i) {
 
176
          exec_command(cmdline[0], cmdline, environ);
 
177
        }
 
178
  }
 
179
  return 0;
 
180
}
 
181
 
 
182
void make_exec (lua_State *L){
 
183
  lua_getglobal(L,"os");
 
184
  lua_pushcfunction(L, os_exec);
 
185
  lua_setfield(L,-2,"exec");
 
186
 
187
 
 
188
/*  Hans wants to set env values */
 
189
 
 
190
static int os_setenv (lua_State *L) {
 
191
  char *value, *key, *val;
 
192
  key =  (char *)luaL_optstring(L, 1, NULL);
 
193
  val =  (char *)luaL_optstring(L, 2, NULL);
 
194
  if (key) {
 
195
        if (val) {
 
196
          value = xmalloc(strlen(key)+strlen(val)+2);
 
197
          sprintf(value,"%s=%s",key,val);
 
198
          if (putenv(value)) {
 
199
                return luaL_error(L, "unable to change environment");
 
200
          }
 
201
        } else {
 
202
#if defined(WIN32) || defined(__sun__)
 
203
          value = xmalloc(strlen(key)+2);
 
204
          sprintf(value,"%s=",key);
 
205
          if (putenv(value)) {
 
206
                return luaL_error(L, "unable to change environment");
 
207
          } 
 
208
#else
 
209
          (void)unsetenv(key);
 
210
#endif 
 
211
        }
 
212
  }
 
213
  lua_pushboolean (L, 1);
 
214
  return 1;
 
215
}
 
216
 
 
217
 
 
218
void put_env (lua_State *L){
 
219
  lua_getglobal(L,"os");
 
220
  lua_pushcfunction(L, os_setenv);
 
221
  lua_setfield(L,-2,"setenv");
 
222
 
223
 
 
224
void find_env (lua_State *L){
 
225
  char *envitem, *envitem_orig;
 
226
  char *envkey;
 
227
  char **envpointer;
 
228
  envpointer = environ;
 
229
  lua_getglobal(L,"os");
 
230
  if (envpointer!=NULL && lua_istable(L,-1)) {
 
231
    luaL_checkstack(L,2,"out of stack space");
 
232
    lua_pushstring(L,"env"); 
 
233
    lua_newtable(L); 
 
234
    while (*envpointer) {
 
235
      /* TODO: perhaps a memory leak here  */
 
236
      luaL_checkstack(L,2,"out of stack space");
 
237
      envitem = xstrdup(*envpointer);
 
238
      envitem_orig = envitem;
 
239
      envkey=envitem;
 
240
      while (*envitem != '=') {
 
241
        envitem++;
 
242
      }
 
243
      *envitem=0;
 
244
      envitem++;
 
245
      lua_pushstring(L,envkey);
 
246
      lua_pushstring(L,envitem);
 
247
      lua_rawset(L,-3);
 
248
      envpointer++;
 
249
      free(envitem_orig);
 
250
    }
 
251
    lua_rawset(L,-3);
 
252
  }
 
253
  lua_pop(L,1);
 
254
}
 
255
 
 
256
void *my_luaalloc (void *ud, void *ptr, size_t osize, size_t nsize) {
 
257
  void *ret = NULL;
 
258
  if (nsize == 0)
 
259
        free(ptr);
 
260
  else
 
261
        ret = realloc(ptr, nsize);
 
262
  luastate_bytes += (nsize-osize);
 
263
  return ret;
 
264
}
 
265
 
 
266
static int my_luapanic (lua_State *L) {
 
267
  (void)L;  /* to avoid warnings */
 
268
  fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
 
269
                   lua_tostring(L, -1));
 
270
  return 0;
 
271
}
 
272
 
 
273
 
 
274
void 
 
275
luainterpreter (int n) {
 
276
  lua_State *L;
 
277
  L = lua_newstate(my_luaalloc, NULL);
 
278
  if (L==NULL) {
 
279
        fprintf(stderr,"Can't create a new Lua state (%d).",n);
 
280
        return;
 
281
  }
 
282
  lua_atpanic(L, &my_luapanic);
 
283
 
 
284
  luastate_max++;
 
285
  luaL_openlibs(L);
 
286
  find_env(L);
 
287
  if (!safer_option) {
 
288
        put_env(L);
 
289
        make_exec(L);
 
290
  }
 
291
 
 
292
  /*luaopen_unicode(L);*/
 
293
  lua_pushcfunction(L, luaopen_unicode);
 
294
  lua_pushstring(L, "unicode");
 
295
  lua_call(L, 1, 0);
 
296
 
 
297
  /*luaopen_zip(L);*/
 
298
  lua_pushcfunction(L, luaopen_zip);
 
299
  lua_pushstring(L, "zip");
 
300
  lua_call(L, 1, 0);
 
301
 
 
302
  /*luaopen_lpeg(L);*/
 
303
  lua_pushcfunction(L, luaopen_lpeg);
 
304
  lua_pushstring(L, "lpeg");
 
305
  lua_call(L, 1, 0);
 
306
 
 
307
  /*luaopen_md5(L);*/
 
308
  lua_pushcfunction(L, luaopen_md5);
 
309
  lua_pushstring(L, "md5");
 
310
  lua_call(L, 1, 0);
 
311
 
 
312
  /*luaopen_lfs(L);*/
 
313
  lua_pushcfunction(L, luaopen_lfs);
 
314
  lua_pushstring(L, "lfs");
 
315
  lua_call(L, 1, 0);
 
316
 
 
317
  /* zlib. slightly odd calling convention */
 
318
  luaopen_zlib(L);
 
319
  lua_setglobal(L,"zlib");
 
320
  luaopen_gzip(L);
 
321
  /* fontforge */
 
322
  luaopen_ff(L);
 
323
 
 
324
  luaopen_pdf(L);
 
325
  luaopen_tex(L);
 
326
  luaopen_token(L);
 
327
  luaopen_node(L);
 
328
  luaopen_texio(L);
 
329
  luaopen_kpse(L);
 
330
  if (n==0) {
 
331
    luaopen_callback(L);
 
332
    lua_createtable(L, 0, 0);
 
333
    lua_setglobal(L, "texconfig");
 
334
  }
 
335
  luaopen_lua(L,n,startup_filename);
 
336
  luaopen_stats(L);
 
337
  luaopen_font(L);
 
338
 
 
339
  if (safer_option) {
 
340
        /* disable some stuff if --safer */
 
341
        (void)hide_lua_value(L, "os","execute");
 
342
        (void)hide_lua_value(L, "os","rename");
 
343
        (void)hide_lua_value(L, "os","remove");
 
344
        (void)hide_lua_value(L, "io","popen");
 
345
        /* make io.open only read files */
 
346
        luaL_checkstack(L,2,"out of stack space");
 
347
        lua_getglobal(L,"io");
 
348
        lua_getfield(L,-1,"open_ro");   
 
349
        lua_setfield(L,-2,"open");      
 
350
        (void)hide_lua_value(L, "io","tmpfile");
 
351
        (void)hide_lua_value(L, "io","output");
 
352
        (void)hide_lua_value(L, "lfs","chdir");
 
353
        (void)hide_lua_value(L, "lfs","lock");
 
354
        (void)hide_lua_value(L, "lfs","touch");
 
355
        (void)hide_lua_value(L, "lfs","rmdir");
 
356
        (void)hide_lua_value(L, "lfs","mkdir");
 
357
  }
 
358
  Luas[n] = L;
 
359
}
 
360
 
 
361
int hide_lua_table(lua_State *L, char *name) {
 
362
  int r=0;
 
363
  lua_getglobal(L,name);
 
364
  if(lua_istable(L,-1)) {
 
365
    r = luaL_ref(L,LUA_REGISTRYINDEX);
 
366
    lua_pushnil(L);
 
367
    lua_setglobal(L,name);
 
368
  }
 
369
  return r;
 
370
}
 
371
 
 
372
void unhide_lua_table(lua_State *L, char *name, int r) {
 
373
  lua_rawgeti(L,LUA_REGISTRYINDEX,r);
 
374
  lua_setglobal(L,name);
 
375
  luaL_unref(L,LUA_REGISTRYINDEX,r);
 
376
}
 
377
 
 
378
int hide_lua_value(lua_State *L, char *name, char *item) {
 
379
  int r=0;
 
380
  lua_getglobal(L,name);
 
381
  if(lua_istable(L,-1)) {
 
382
        lua_getfield(L,-1,item);
 
383
    r = luaL_ref(L,LUA_REGISTRYINDEX);
 
384
    lua_pushnil(L);
 
385
    lua_setfield(L,-2,item);
 
386
  }
 
387
  return r;
 
388
}
 
389
 
 
390
void unhide_lua_value(lua_State *L, char *name, char *item, int r) {
 
391
  lua_getglobal(L,name);
 
392
  if(lua_istable(L,-1)) {
 
393
        lua_rawgeti(L,LUA_REGISTRYINDEX,r);
 
394
        lua_setfield(L,-2,item);
 
395
        luaL_unref(L,LUA_REGISTRYINDEX,r);
 
396
  }
 
397
}
 
398
 
 
399
 
 
400
void 
 
401
luacall(int n, int s) {
 
402
  LoadS ls;
 
403
  int i, j, k ;
 
404
  char lua_id[12];
 
405
  if (Luas[n] == NULL) {
 
406
    luainterpreter(n);
 
407
  }
 
408
  luatex_load_init(s,&ls);
 
409
  if (ls.size>0) {
 
410
        snprintf((char *)lua_id,12,"luas[%d]",n);
 
411
        i = lua_load(Luas[n], getS, &ls, lua_id);
 
412
        if (i != 0) {
 
413
          Luas[n] = luatex_error(Luas[n],(i == LUA_ERRSYNTAX ? 0 : 1));
 
414
        } else {
 
415
          i = lua_pcall(Luas[n], 0, 0, 0);
 
416
          if (i != 0) {
 
417
                Luas[n] = luatex_error(Luas[n],(i == LUA_ERRRUN ? 0 : 1));
 
418
          }      
 
419
        }
 
420
  }
 
421
}
 
422
 
 
423
void 
 
424
luatokencall(int n, int p) {
 
425
  LoadS ls;
 
426
  int i, j, k, l;
 
427
  char *s=NULL;
 
428
  char lua_id[12];
 
429
  if (Luas[n] == NULL) {
 
430
    luainterpreter(n);
 
431
  }
 
432
  l = 0;
 
433
  s = tokenlist_to_cstring(p,1,&l);
 
434
  ls.s = s;
 
435
  ls.size = l;
 
436
  if (ls.size>0) {
 
437
        snprintf((char *)lua_id,12,"luas[%d]",n);
 
438
        i = lua_load(Luas[n], getS, &ls, lua_id);
 
439
        xfree(s);
 
440
        if (i != 0) {
 
441
          Luas[n] = luatex_error(Luas[n],(i == LUA_ERRSYNTAX ? 0 : 1));
 
442
        } else {
 
443
          i = lua_pcall(Luas[n], 0, 0, 0);
 
444
          if (i != 0) {
 
445
                Luas[n] = luatex_error(Luas[n],(i == LUA_ERRRUN ? 0 : 1));
 
446
          }      
 
447
        }
 
448
  }
 
449
}
 
450
 
 
451
 
 
452
 
 
453
void 
 
454
closelua(int n) {
 
455
  if (n!=0 && Luas[n] != NULL) {
 
456
    lua_close(Luas[n]);
 
457
        luastate_max--;
 
458
    Luas[n] = NULL;
 
459
  }
 
460
}
 
461
 
 
462
 
 
463
void 
 
464
luatex_load_init (int s, LoadS *ls) {
 
465
  ls->s = (const char *)&(strpool[strstart[s]]);
 
466
  ls->size = strstart[s + 1] - strstart[s];
 
467
}
 
468
 
 
469
lua_State *
 
470
luatex_error (lua_State * L, int is_fatal) {
 
471
  int i,j;
 
472
  size_t len;
 
473
  char *err;
 
474
  strnumber s;
 
475
  const char *luaerr = lua_tostring(L, -1);
 
476
  err = (char *)xmalloc(128);
 
477
  len = snprintf(err,128,"%s",luaerr);
 
478
  if (is_fatal>0) {
 
479
    /* Normally a memory error from lua. 
 
480
       The pool may overflow during the maketexstring(), but we 
 
481
       are crashing anyway so we may as well abort on the pool size */
 
482
    s = maketexstring(err);
 
483
    lua_fatal_error(s);
 
484
    /* never reached */
 
485
    xfree (err);
 
486
    lua_close(L);
 
487
        luastate_max--;
 
488
    return (lua_State *)NULL;
 
489
  } else {
 
490
    /* Here, the pool could be full already, but we can possibly escape from that 
 
491
     * condition, since the lua chunk that caused the error is the current string.
 
492
     */
 
493
    s = str_ptr-0x200000;
 
494
    //    fprintf(stderr,"poolinfo: %d: %d,%d out of %d\n",s,poolptr,strstart[(s-1)],poolsize);
 
495
    poolptr = strstart[(s-1)];
 
496
    strstart[s] = poolptr;
 
497
    if (poolptr+len>=poolsize) {
 
498
      lua_norm_error(' ');
 
499
    } else {
 
500
      s = maketexstring(err);
 
501
      lua_norm_error(s);
 
502
      flush_str(s);
 
503
    }
 
504
    xfree (err);
 
505
    return L;
 
506
  }
 
507
}
 
508