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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2008-07-07 11:01:13 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080707110113-1y7lam37zbbb7bbt
Tags: 0.28.0-1
* two new upstream releases, see the respective ANNOUCE files
* add luasocket license statement to debian/copyright
* activate the pdfluatex format
* prepare for latex based formats
  - add the ini files from TeX Live
  - add debian/formats file
  - adjust dh_installtex incantation
  the problem is that luatex dies when loading ukrhypmp.tex from 
  texlive-lang-cyrillic, but we don't want to conflict with it by now.
* policy 3.8.0, rename README.Debian-source to README.source, and add
  notes about quilt usage
* disable patch fix-pwd-inclusion (it was from svn)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** $Id: texluac.c 1013 2008-02-14 00:09:02Z oneiros $
3
 
** Lua compiler (saves bytecodes to files; also list bytecodes)
4
 
** See Copyright Notice in lua.h
 
1
/* texlua.c
 
2
 
 
3
* Copyright (C) 1994-2007 Lua.org, PUC-Rio.  All rights reserved.
 
4
*
 
5
* Permission is hereby granted, free of charge, to any person obtaining
 
6
* a copy of this software and associated documentation files (the
 
7
* "Software"), to deal in the Software without restriction, including
 
8
* without limitation the rights to use, copy, modify, merge, publish,
 
9
* distribute, sublicense, and/or sell copies of the Software, and to
 
10
* permit persons to whom the Software is furnished to do so, subject to
 
11
* the following conditions:
 
12
*
 
13
* The above copyright notice and this permission notice shall be
 
14
* included in all copies or substantial portions of the Software.
 
15
*
 
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
18
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
19
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 
20
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 
21
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 
22
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
23
 
 
24
   Copyright 2006-2008 Taco Hoekwater <taco@luatex.org>
 
25
 
 
26
   This file is part of LuaTeX.
5
27
*/
6
28
 
7
29
#include <errno.h>
23
45
#include <../lua51/lstring.h>
24
46
#include <../lua51/lundump.h>
25
47
 
26
 
#define PROGNAME        "texluac"               /* default program name */
27
 
#define OUTPUT          PROGNAME ".out" /* default output file */
28
 
 
29
 
static int dumping=1;                   /* dump bytecodes? */
30
 
static int stripping=0;                 /* strip debug information? */
31
 
static char Output[]={ OUTPUT };        /* default output file name */
32
 
static const char* output=Output;       /* actual output file name */
33
 
static const char* progname=PROGNAME;   /* actual program name */
34
 
 
35
 
static void fatal(const char* message)
36
 
{
37
 
 fprintf(stderr,"%s: %s\n",progname,message);
38
 
 exit(EXIT_FAILURE);
39
 
}
40
 
 
41
 
static void cannot(const char* what)
42
 
{
43
 
 fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
44
 
 exit(EXIT_FAILURE);
45
 
}
46
 
 
47
 
static void usage(const char* message)
48
 
{
49
 
 if (*message=='-')
50
 
  fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
51
 
 else
52
 
  fprintf(stderr,"%s: %s\n",progname,message);
53
 
 fprintf(stderr,
54
 
 "usage: %s [options] [filenames].\n"
55
 
 "Available options are:\n"
56
 
 "  -        process stdin\n"
57
 
 "  -o name  output to file " LUA_QL("name") " (default is \"%s\")\n"
58
 
 "  -p       parse only\n"
59
 
 "  -s       strip debug information\n"
60
 
 "  -v       show version information\n"
61
 
 "  --       stop handling options\n",
62
 
 progname,Output);
63
 
 exit(EXIT_FAILURE);
64
 
}
65
 
 
66
 
#define IS(s)   (strcmp(argv[i],s)==0)
67
 
 
68
 
static int doargs(int argc, char* argv[])
69
 
{
70
 
 int i;
71
 
 int version=0;
72
 
 if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
73
 
 for (i=1; i<argc; i++)
74
 
 {
75
 
  if (*argv[i]!='-')                    /* end of options; keep it */
76
 
   break;
77
 
  else if (IS("--"))                    /* end of options; skip it */
78
 
  {
79
 
   ++i;
80
 
   if (version) ++version;
81
 
   break;
82
 
  }
83
 
  else if (IS("-"))                     /* end of options; use stdin */
84
 
   break;
85
 
  else if (IS("-o"))                    /* output file */
86
 
  {
87
 
   output=argv[++i];
88
 
   if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
89
 
   if (IS("-")) output=NULL;
90
 
  }
91
 
  else if (IS("-p"))                    /* parse only */
92
 
   dumping=0;
93
 
  else if (IS("-s"))                    /* strip debug information */
94
 
   stripping=1;
95
 
  else if (IS("-v"))                    /* show version */
96
 
   ++version;
97
 
  else if (IS("--luaconly"))    /* ignore */
98
 
        ;
99
 
  else if (IS("--luac"))        /* ignore */
100
 
        ;
101
 
  else                                  /* unknown option */
102
 
   usage(argv[i]);
103
 
 }
104
 
 if (i==argc && (!dumping))
105
 
 {
106
 
  dumping=0;
107
 
  argv[--i]=Output;
108
 
 }
109
 
 if (version)
110
 
 {
111
 
  printf("%s  %s\n",LUA_RELEASE,LUA_COPYRIGHT);
112
 
  if (version==argc-1) exit(EXIT_SUCCESS);
113
 
 }
114
 
 return i;
 
48
static const char _svn_version[] =
 
49
    "$Id: texluac.c 1226 2008-05-02 16:11:02Z oneiros $ $URL: http://scm.foundry.supelec.fr/svn/luatex/tags/beta-0.28.0/src/texk/web2c/luatexdir/lua/texluac.c $";
 
50
 
 
51
#define PROGNAME        "texluac"       /* default program name */
 
52
#define OUTPUT          PROGNAME ".out" /* default output file */
 
53
 
 
54
static int dumping = 1;         /* dump bytecodes? */
 
55
static int stripping = 0;       /* strip debug information? */
 
56
static char Output[] = { OUTPUT };      /* default output file name */
 
57
static const char *output = Output;     /* actual output file name */
 
58
static const char *progname = PROGNAME; /* actual program name */
 
59
 
 
60
static void fatal(const char *message)
 
61
{
 
62
    fprintf(stderr, "%s: %s\n", progname, message);
 
63
    exit(EXIT_FAILURE);
 
64
}
 
65
 
 
66
static void cannot(const char *what)
 
67
{
 
68
    fprintf(stderr, "%s: cannot %s %s: %s\n", progname, what, output,
 
69
            strerror(errno));
 
70
    exit(EXIT_FAILURE);
 
71
}
 
72
 
 
73
static void usage(const char *message)
 
74
{
 
75
    if (*message == '-')
 
76
        fprintf(stderr, "%s: unrecognized option " LUA_QS "\n", progname,
 
77
                message);
 
78
    else
 
79
        fprintf(stderr, "%s: %s\n", progname, message);
 
80
    fprintf(stderr,
 
81
            "usage: %s [options] [filenames].\n"
 
82
            "Available options are:\n"
 
83
            "  -        process stdin\n"
 
84
            "  -o name  output to file " LUA_QL("name") " (default is \"%s\")\n"
 
85
            "  -p       parse only\n"
 
86
            "  -s       strip debug information\n"
 
87
            "  -v       show version information\n"
 
88
            "  --       stop handling options\n", progname, Output);
 
89
    exit(EXIT_FAILURE);
 
90
}
 
91
 
 
92
#define IS(s)   (strcmp(argv[i],s)==0)
 
93
 
 
94
static int doargs(int argc, char *argv[])
 
95
{
 
96
    int i;
 
97
    int version = 0;
 
98
    if (argv[0] != NULL && *argv[0] != 0)
 
99
        progname = argv[0];
 
100
    for (i = 1; i < argc; i++) {
 
101
        if (*argv[i] != '-')    /* end of options; keep it */
 
102
            break;
 
103
        else if (IS("--")) {    /* end of options; skip it */
 
104
            ++i;
 
105
            if (version)
 
106
                ++version;
 
107
            break;
 
108
        } else if (IS("-"))     /* end of options; use stdin */
 
109
            break;
 
110
        else if (IS("-o")) {    /* output file */
 
111
            output = argv[++i];
 
112
            if (output == NULL || *output == 0)
 
113
                usage(LUA_QL("-o") " needs argument");
 
114
            if (IS("-"))
 
115
                output = NULL;
 
116
        } else if (IS("-p"))    /* parse only */
 
117
            dumping = 0;
 
118
        else if (IS("-s"))      /* strip debug information */
 
119
            stripping = 1;
 
120
        else if (IS("-v"))      /* show version */
 
121
            ++version;
 
122
        else if (IS("--luaconly"))      /* ignore */
 
123
            ;
 
124
        else if (IS("--luac"))  /* ignore */
 
125
            ;
 
126
        else                    /* unknown option */
 
127
            usage(argv[i]);
 
128
    }
 
129
    if (i == argc && (!dumping)) {
 
130
        dumping = 0;
 
131
        argv[--i] = Output;
 
132
    }
 
133
    if (version) {
 
134
        printf("%s  %s\n", LUA_RELEASE, LUA_COPYRIGHT);
 
135
        if (version == argc - 1)
 
136
            exit(EXIT_SUCCESS);
 
137
    }
 
138
    return i;
115
139
}
116
140
 
117
141
#define toproto(L,i) (clvalue(L->top+(i))->l.p)
118
142
 
119
 
static const Proto* combine(lua_State* L, int n)
 
143
static const Proto *combine(lua_State * L, int n)
120
144
{
121
 
 if (n==1)
122
 
  return toproto(L,-1);
123
 
 else
124
 
 {
125
 
  int i,pc;
126
 
  Proto* f=luaF_newproto(L);
127
 
  setptvalue2s(L,L->top,f); incr_top(L);
128
 
  f->source=luaS_newliteral(L,"=(" PROGNAME ")");
129
 
  f->maxstacksize=1;
130
 
  pc=2*n+1;
131
 
  f->code=luaM_newvector(L,pc,Instruction);
132
 
  f->sizecode=pc;
133
 
  f->p=luaM_newvector(L,n,Proto*);
134
 
  f->sizep=n;
135
 
  pc=0;
136
 
  for (i=0; i<n; i++)
137
 
  {
138
 
   f->p[i]=toproto(L,i-n-1);
139
 
   f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
140
 
   f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
141
 
  }
142
 
  f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
143
 
  return f;
144
 
 }
 
145
    if (n == 1)
 
146
        return toproto(L, -1);
 
147
    else {
 
148
        int i, pc;
 
149
        Proto *f = luaF_newproto(L);
 
150
        setptvalue2s(L, L->top, f);
 
151
        incr_top(L);
 
152
        f->source = luaS_newliteral(L, "=(" PROGNAME ")");
 
153
        f->maxstacksize = 1;
 
154
        pc = 2 * n + 1;
 
155
        f->code = luaM_newvector(L, pc, Instruction);
 
156
        f->sizecode = pc;
 
157
        f->p = luaM_newvector(L, n, Proto *);
 
158
        f->sizep = n;
 
159
        pc = 0;
 
160
        for (i = 0; i < n; i++) {
 
161
            f->p[i] = toproto(L, i - n - 1);
 
162
            f->code[pc++] = CREATE_ABx(OP_CLOSURE, 0, i);
 
163
            f->code[pc++] = CREATE_ABC(OP_CALL, 0, 1, 1);
 
164
        }
 
165
        f->code[pc++] = CREATE_ABC(OP_RETURN, 0, 1, 0);
 
166
        return f;
 
167
    }
145
168
}
146
169
 
147
 
static int writer(lua_State* L, const void* p, size_t size, void* u)
 
170
static int writer(lua_State * L, const void *p, size_t size, void *u)
148
171
{
149
 
 UNUSED(L);
150
 
 return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
 
172
    UNUSED(L);
 
173
    return (fwrite(p, size, 1, (FILE *) u) != 1) && (size != 0);
151
174
}
152
175
 
153
176
struct Smain {
154
 
 int argc;
155
 
 char** argv;
 
177
    int argc;
 
178
    char **argv;
156
179
};
157
180
 
158
 
static int pmain(lua_State* L)
 
181
static int pmain(lua_State * L)
159
182
{
160
 
 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
161
 
 int argc=s->argc;
162
 
 char** argv=s->argv;
163
 
 const Proto* f;
164
 
 int i;
165
 
 if (!lua_checkstack(L,argc)) fatal("too many input files");
166
 
 for (i=0; i<argc; i++)
167
 
 {
168
 
  const char* filename=IS("-") ? NULL : argv[i];
169
 
  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
170
 
 }
171
 
 f=combine(L,argc);
172
 
 if (dumping)
173
 
 {
174
 
  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
175
 
  if (D==NULL) cannot("open");
176
 
  lua_lock(L);
177
 
  luaU_dump(L,f,writer,D,stripping);
178
 
  lua_unlock(L);
179
 
  if (ferror(D)) cannot("write");
180
 
  if (fclose(D)) cannot("close");
181
 
 }
182
 
 return 0;
 
183
    struct Smain *s = (struct Smain *) lua_touserdata(L, 1);
 
184
    int argc = s->argc;
 
185
    char **argv = s->argv;
 
186
    const Proto *f;
 
187
    int i;
 
188
    if (!lua_checkstack(L, argc))
 
189
        fatal("too many input files");
 
190
    for (i = 0; i < argc; i++) {
 
191
        const char *filename = IS("-") ? NULL : argv[i];
 
192
        if (luaL_loadfile(L, filename) != 0)
 
193
            fatal(lua_tostring(L, -1));
 
194
    }
 
195
    f = combine(L, argc);
 
196
    if (dumping) {
 
197
        FILE *D = (output == NULL) ? stdout : fopen(output, "wb");
 
198
        if (D == NULL)
 
199
            cannot("open");
 
200
        lua_lock(L);
 
201
        luaU_dump(L, f, writer, D, stripping);
 
202
        lua_unlock(L);
 
203
        if (ferror(D))
 
204
            cannot("write");
 
205
        if (fclose(D))
 
206
            cannot("close");
 
207
    }
 
208
    return 0;
183
209
}
184
210
 
185
 
int luac_main(int argc, char* argv[])
 
211
int luac_main(int argc, char *argv[])
186
212
{
187
 
 lua_State* L;
188
 
 struct Smain s;
189
 
 int i=doargs(argc,argv);
190
 
 argc-=i; argv+=i;
191
 
 if (argc<=0) usage("no input files given");
192
 
 L=lua_open();
193
 
 if (L==NULL) fatal("not enough memory for state");
194
 
 s.argc=argc;
195
 
 s.argv=argv;
196
 
 if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));
197
 
 lua_close(L);
198
 
 return EXIT_SUCCESS;
 
213
    lua_State *L;
 
214
    struct Smain s;
 
215
    int i = doargs(argc, argv);
 
216
    argc -= i;
 
217
    argv += i;
 
218
    if (argc <= 0)
 
219
        usage("no input files given");
 
220
    L = lua_open();
 
221
    if (L == NULL)
 
222
        fatal("not enough memory for state");
 
223
    s.argc = argc;
 
224
    s.argv = argv;
 
225
    if (lua_cpcall(L, pmain, &s) != 0)
 
226
        fatal(lua_tostring(L, -1));
 
227
    lua_close(L);
 
228
    return EXIT_SUCCESS;
199
229
}