~ubuntu-branches/ubuntu/intrepid/luatex/intrepid

« back to all changes in this revision

Viewing changes to src/texk/web2c/luatexdir/lua/ltokenlib.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
 
/* $Id: ltokenlib.c 1013 2008-02-14 00:09:02Z oneiros $ */
 
1
/* ltokenlib.c
 
2
   
 
3
   Copyright 2006-2008 Taco Hoekwater <taco@luatex.org>
 
4
 
 
5
   This file is part of LuaTeX.
 
6
 
 
7
   LuaTeX is free software; you can redistribute it and/or modify it under
 
8
   the terms of the GNU General Public License as published by the Free
 
9
   Software Foundation; either version 2 of the License, or (at your
 
10
   option) any later version.
 
11
 
 
12
   LuaTeX is distributed in the hope that it will be useful, but WITHOUT
 
13
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
14
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
15
   License for more details.
 
16
 
 
17
   You should have received a copy of the GNU General Public License along
 
18
   with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
2
19
 
3
20
#include "luatex-api.h"
4
21
#include <ptexlib.h>
5
22
 
 
23
static const char _svn_version[] =
 
24
    "$Id: ltokenlib.c 1348 2008-06-30 14:59:42Z taco $ $URL: http://scm.foundry.supelec.fr/svn/luatex/tags/beta-0.28.0/src/texk/web2c/luatexdir/lua/ltokenlib.c $";
6
25
 
7
 
extern int get_command_id (char *);
 
26
extern int get_command_id(char *);
8
27
 
9
28
static int max_command = 0;
10
29
static int hash_base = 0;
11
 
static int active_base = 0;
12
30
static int null_cs = 0;
13
31
 
14
32
#define  protected_token 0x1C00001
15
33
 
16
34
#define  is_valid_token(L,i)  (lua_istable(L,i) && lua_objlen(L,i)==3)
17
 
#define  get_token_cmd(L,i)  lua_rawgeti(L,i,1) 
18
 
#define  get_token_chr(L,i)  lua_rawgeti(L,i,2) 
19
 
#define  get_token_cs(L,i)   lua_rawgeti(L,i,3) 
20
 
 
21
 
static int 
22
 
test_expandable (lua_State *L) {
23
 
  integer cmd = -1;
24
 
  if (is_valid_token(L,-1)) {
25
 
    get_token_cmd(L,-1);
26
 
    if (lua_isnumber(L,-1)) {
27
 
      cmd = lua_tointeger(L,-1);
28
 
    } else if (lua_isstring(L,-1)) {
29
 
      cmd = get_command_id((char *)lua_tostring(L,-1));
30
 
    }
31
 
    if (cmd>max_command) {
32
 
      lua_pushboolean(L,1);
33
 
    } else {
34
 
      lua_pushboolean(L,0);
35
 
    }
36
 
  } else {
37
 
    lua_pushnil(L);
38
 
  }
39
 
  return 1;
40
 
}
41
 
 
42
 
 
43
 
static int 
44
 
test_protected (lua_State *L) {
45
 
  integer chr = -1;
46
 
  if (is_valid_token(L,-1)) {
47
 
    get_token_chr(L,-1);
48
 
    if (lua_isnumber(L,-1)) {
49
 
      chr = lua_tointeger(L,-1);
50
 
    } else if (lua_isstring(L,-1)) {
51
 
      chr = get_command_id((char *)lua_tostring(L,-1));
52
 
    }
53
 
    if (fixmem[fixmem[chr].hhrh].hhlh==protected_token) {
54
 
      lua_pushboolean(L,1);
55
 
    } else {
56
 
      lua_pushboolean(L,0);
57
 
    }
58
 
  } else {
59
 
    lua_pushnil(L);
60
 
  }
61
 
  return 1;
62
 
}
63
 
 
64
 
static int 
65
 
test_activechar (lua_State *L) {
66
 
  integer cmd = -1;
67
 
  if (is_valid_token(L,-1)) {
68
 
    get_token_chr(L,-1);
69
 
    if (lua_isnumber(L,-1)) {
70
 
      cmd = lua_tointeger(L,-1);
71
 
    }
72
 
    if (cmd>0 && cmd==protected_token) {
73
 
      lua_pushboolean(L,1);
74
 
    } else {
75
 
      lua_pushboolean(L,0);
76
 
    }
77
 
  } else {
78
 
    lua_pushnil(L);
79
 
  }
80
 
  return 1;
81
 
}
82
 
 
83
 
 
84
 
static int
85
 
run_get_command_name (lua_State *L) {
86
 
  int cs;
87
 
  if (is_valid_token(L,-1)) {
88
 
    get_token_cmd(L,-1);
89
 
    if (lua_isnumber(L,-1)) {
90
 
      cs = lua_tointeger(L,-1);
91
 
      lua_pushstring(L,command_names[cs].cmd_name);
92
 
    } else {
93
 
      lua_pushstring(L,"");
94
 
    }
95
 
  } else {
96
 
    lua_pushnil(L);
97
 
  }
98
 
  return 1;
99
 
}
100
 
 
101
 
static int
102
 
run_get_csname_name (lua_State *L) {
103
 
  int cs,cmd,n;
104
 
  char *s;
105
 
 
106
 
  if (is_valid_token(L,-1)) {
107
 
    get_token_cmd(L,-1);
108
 
        if (lua_isnumber(L,-1)) {
109
 
          cmd = lua_tointeger(L,-1);
110
 
        }
111
 
        lua_pop(L,1);
112
 
    cs = 0;
113
 
    get_token_cs(L,-1);
114
 
        if (lua_isnumber(L,-1)) {
115
 
          cs = lua_tointeger(L,-1);
116
 
        }
117
 
        lua_pop(L,1);
118
 
 
119
 
        if (cs != 0 && (n = zget_cs_text(cs)) && n>=0) {
120
 
          s = makecstring(n);
121
 
          lua_pushstring(L,s);
122
 
        } else {
123
 
          lua_pushstring(L,"");
124
 
        }
125
 
  } else {
126
 
    lua_pushnil(L);
127
 
  }
128
 
  return 1;
129
 
}
130
 
 
131
 
static int
132
 
run_get_command_id (lua_State *L) {
133
 
  int cs = -1;
134
 
  if (lua_isstring(L,-1)) {
135
 
    cs = get_command_id((char *)lua_tostring(L,-1));
136
 
  }
137
 
  lua_pushnumber(L,cs);
138
 
  return 1;
139
 
}
140
 
 
141
 
 
142
 
static int
143
 
run_get_csname_id (lua_State *L) {
144
 
  int texstr;
145
 
  char *s;
146
 
  size_t k,cs = 0;
147
 
  if (lua_isstring(L,-1)) {
148
 
    s = (char *)lua_tolstring(L,-1, &k);
149
 
        texstr = maketexlstring(s,k);
150
 
    cs = string_lookup(texstr);
151
 
    flush_str(texstr);
152
 
  }
153
 
  lua_pushnumber(L,cs);
154
 
  return 1;
155
 
}
156
 
 
157
 
 
158
 
void 
159
 
make_token_table (lua_State *L, int cmd, int chr, int cs) {
160
 
  lua_createtable(L,3,0);
161
 
  lua_pushnumber(L,cmd);
162
 
  lua_rawseti(L,-2,1);
163
 
  lua_pushnumber(L,chr);
164
 
  lua_rawseti(L,-2,2);
165
 
  lua_pushnumber(L,cs);
166
 
  lua_rawseti(L,-2,3);
167
 
}
168
 
 
169
 
static int 
170
 
run_get_next (lua_State *L) {
171
 
  int save_nncs;
172
 
  save_nncs = no_new_control_sequence;
173
 
  no_new_control_sequence = 0;
174
 
  get_next();
175
 
  no_new_control_sequence = save_nncs;
176
 
  make_token_table(L,cur_cmd,cur_chr,cur_cs);
177
 
  return 1;
178
 
}
179
 
 
180
 
static int 
181
 
run_expand (lua_State *L) {
182
 
  expand();
183
 
  return 0;
184
 
}
185
 
 
186
 
 
187
 
static int 
188
 
run_lookup (lua_State *L) {
189
 
  char *s;
190
 
  size_t l;
191
 
  str_number t;
192
 
  integer cs,cmd,chr;
193
 
  int save_nncs;
194
 
  if (lua_isstring(L,-1)) {
195
 
    s = (char *)lua_tolstring(L,-1,&l);
196
 
    if (l>0) {
197
 
      save_nncs = no_new_control_sequence;
198
 
      no_new_control_sequence = true;
199
 
      cs = id_lookup((last+1),l); /* cleans up the lookup buffer */
200
 
      t = maketexlstring(s,l);
201
 
      cs = string_lookup(t);
202
 
      flush_str(t);
203
 
      cmd = zget_eq_type(cs);
204
 
      chr = zget_equiv(cs);
205
 
      make_token_table(L,cmd,chr,cs);
206
 
      no_new_control_sequence = save_nncs;
207
 
      return 1;
208
 
    }
209
 
  }
210
 
  lua_newtable(L);
211
 
  return 1;
212
 
}
213
 
 
214
 
static int 
215
 
run_build (lua_State *L) {
216
 
  integer cmd,chr,cs;
217
 
  if (lua_isnumber(L,1)) {
218
 
    cs =0;
219
 
    chr = lua_tointeger(L,1);
220
 
    cmd = luaL_optinteger(L,2,zget_char_cat_code(chr));
221
 
    if (cmd==0 || cmd == 9 || cmd == 14 || cmd == 15) {
222
 
      fprintf(stdout, "\n\nluatex error: not a good token.\nCatcode %i can not be returned, so I replaced it by 12 (other)",(int)cmd);
223
 
      error();
224
 
      cmd=12;
225
 
    }
226
 
    if (cmd == 13) {
227
 
       cs=chr+active_base;
228
 
       cmd=zget_eq_type(cs); 
229
 
       chr=zget_equiv(cs);
230
 
    }
231
 
    make_token_table(L,cmd,chr,cs);
232
 
    return 1;
233
 
  } else {
234
 
    return run_lookup(L);
235
 
  }
236
 
}
237
 
 
238
 
 
239
 
static const struct luaL_reg tokenlib [] = {
240
 
  {"get_next",     run_get_next},
241
 
  {"expand",       run_expand},
242
 
  {"lookup",       run_lookup},
243
 
  {"create",       run_build},
244
 
  {"is_expandable",test_expandable},
245
 
  {"is_activechar",test_activechar},
246
 
  {"is_protected", test_protected},
247
 
  {"csname_id",    run_get_csname_id},
248
 
  {"csname_name",  run_get_csname_name},
249
 
  {"command_name", run_get_command_name},
250
 
  {"command_id",   run_get_command_id},
251
 
  {NULL, NULL}  /* sentinel */
 
35
#define  get_token_cmd(L,i)  lua_rawgeti(L,i,1)
 
36
#define  get_token_chr(L,i)  lua_rawgeti(L,i,2)
 
37
#define  get_token_cs(L,i)   lua_rawgeti(L,i,3)
 
38
#define  is_active_string(s) (strlen((char *)s)>3 && *s==0xEF && *(s+1)==0xBF && *(s+2)==0xBF)
 
39
 
 
40
 
 
41
static int test_expandable(lua_State * L)
 
42
{
 
43
    integer cmd = -1;
 
44
    if (is_valid_token(L, -1)) {
 
45
        get_token_cmd(L, -1);
 
46
        if (lua_isnumber(L, -1)) {
 
47
            cmd = lua_tointeger(L, -1);
 
48
        } else if (lua_isstring(L, -1)) {
 
49
            cmd = get_command_id((char *) lua_tostring(L, -1));
 
50
        }
 
51
        if (cmd > max_command) {
 
52
            lua_pushboolean(L, 1);
 
53
        } else {
 
54
            lua_pushboolean(L, 0);
 
55
        }
 
56
    } else {
 
57
        lua_pushnil(L);
 
58
    }
 
59
    return 1;
 
60
}
 
61
 
 
62
 
 
63
static int test_protected(lua_State * L)
 
64
{
 
65
    integer chr = -1;
 
66
    if (is_valid_token(L, -1)) {
 
67
        get_token_chr(L, -1);
 
68
        if (lua_isnumber(L, -1)) {
 
69
            chr = lua_tointeger(L, -1);
 
70
        } else if (lua_isstring(L, -1)) {
 
71
            chr = get_command_id((char *) lua_tostring(L, -1));
 
72
        }
 
73
        if (fixmem[fixmem[chr].hhrh].hhlh == protected_token) {
 
74
            lua_pushboolean(L, 1);
 
75
        } else {
 
76
            lua_pushboolean(L, 0);
 
77
        }
 
78
    } else {
 
79
        lua_pushnil(L);
 
80
    }
 
81
    return 1;
 
82
}
 
83
 
 
84
static int test_activechar(lua_State * L)
 
85
{
 
86
    if (is_valid_token(L, -1)) {
 
87
        str_number n;
 
88
        integer cs = 0;
 
89
        get_token_cs(L, -1);
 
90
        if (lua_isnumber(L, -1)) {
 
91
            cs = lua_tointeger(L, -1);
 
92
        }
 
93
        lua_pop(L, 1);
 
94
        if (cs != 0 && (n = zget_cs_text(cs)) && n > 0) {
 
95
          unsigned char *s = (unsigned char *)makecstring(n);
 
96
            if (is_active_string(s)) {
 
97
              free(s);
 
98
              lua_pushboolean(L,1);
 
99
              return 1;
 
100
            }
 
101
            free(s);
 
102
        }
 
103
    }
 
104
    lua_pushboolean(L,0);
 
105
    return 1;
 
106
}
 
107
 
 
108
 
 
109
static int run_get_command_name(lua_State * L)
 
110
{
 
111
    int cs;
 
112
    if (is_valid_token(L, -1)) {
 
113
        get_token_cmd(L, -1);
 
114
        if (lua_isnumber(L, -1)) {
 
115
            cs = lua_tointeger(L, -1);
 
116
            lua_pushstring(L, command_names[cs].cmd_name);
 
117
        } else {
 
118
            lua_pushstring(L, "");
 
119
        }
 
120
    } else {
 
121
        lua_pushnil(L);
 
122
    }
 
123
    return 1;
 
124
}
 
125
 
 
126
 
 
127
static int run_get_csname_name(lua_State * L)
 
128
{
 
129
    int cs, cmd, n;
 
130
 
 
131
    if (is_valid_token(L, -1)) {
 
132
        get_token_cmd(L, -1);
 
133
        if (lua_isnumber(L, -1)) {
 
134
            cmd = lua_tointeger(L, -1);
 
135
        }
 
136
        lua_pop(L, 1);
 
137
        cs = 0;
 
138
        get_token_cs(L, -1);
 
139
        if (lua_isnumber(L, -1)) {
 
140
            cs = lua_tointeger(L, -1);
 
141
        }
 
142
        lua_pop(L, 1);
 
143
 
 
144
        if (cs != 0 && (n = zget_cs_text(cs)) && n >= 0) {
 
145
            unsigned char *s = (unsigned char *)makecstring(n);
 
146
            if (is_active_string(s))
 
147
              lua_pushstring(L, (char *)(s+3));
 
148
            else
 
149
              lua_pushstring(L, (char *)s);
 
150
        } else {
 
151
            lua_pushstring(L, "");
 
152
        }
 
153
    } else {
 
154
        lua_pushnil(L);
 
155
    }
 
156
    return 1;
 
157
}
 
158
 
 
159
static int run_get_command_id(lua_State * L)
 
160
{
 
161
    int cs = -1;
 
162
    if (lua_isstring(L, -1)) {
 
163
        cs = get_command_id((char *) lua_tostring(L, -1));
 
164
    }
 
165
    lua_pushnumber(L, cs);
 
166
    return 1;
 
167
}
 
168
 
 
169
 
 
170
static int run_get_csname_id(lua_State * L)
 
171
{
 
172
    int texstr;
 
173
    char *s;
 
174
    size_t k, cs = 0;
 
175
    if (lua_isstring(L, -1)) {
 
176
        s = (char *) lua_tolstring(L, -1, &k);
 
177
        texstr = maketexlstring(s, k);
 
178
        cs = string_lookup(texstr);
 
179
        flush_str(texstr);
 
180
    }
 
181
    lua_pushnumber(L, cs);
 
182
    return 1;
 
183
}
 
184
 
 
185
 
 
186
void make_token_table(lua_State * L, int cmd, int chr, int cs)
 
187
{
 
188
    lua_createtable(L, 3, 0);
 
189
    lua_pushnumber(L, cmd);
 
190
    lua_rawseti(L, -2, 1);
 
191
    lua_pushnumber(L, chr);
 
192
    lua_rawseti(L, -2, 2);
 
193
    lua_pushnumber(L, cs);
 
194
    lua_rawseti(L, -2, 3);
 
195
}
 
196
 
 
197
static int run_get_next(lua_State * L)
 
198
{
 
199
    int save_nncs;
 
200
    save_nncs = no_new_control_sequence;
 
201
    no_new_control_sequence = 0;
 
202
    get_next();
 
203
    no_new_control_sequence = save_nncs;
 
204
    make_token_table(L, cur_cmd, cur_chr, cur_cs);
 
205
    return 1;
 
206
}
 
207
 
 
208
static int run_expand(lua_State * L)
 
209
{
 
210
    expand();
 
211
    return 0;
 
212
}
 
213
 
 
214
 
 
215
static int run_lookup(lua_State * L)
 
216
{
 
217
    char *s;
 
218
    size_t l;
 
219
    str_number t;
 
220
    integer cs, cmd, chr;
 
221
    int save_nncs;
 
222
    if (lua_isstring(L, -1)) {
 
223
        s = (char *) lua_tolstring(L, -1, &l);
 
224
        if (l > 0) {
 
225
            save_nncs = no_new_control_sequence;
 
226
            no_new_control_sequence = true;
 
227
            cs = id_lookup((last + 1), l);      /* cleans up the lookup buffer */
 
228
            t = maketexlstring(s, l);
 
229
            cs = string_lookup(t);
 
230
            flush_str(t);
 
231
            cmd = zget_eq_type(cs);
 
232
            chr = zget_equiv(cs);
 
233
            make_token_table(L, cmd, chr, cs);
 
234
            no_new_control_sequence = save_nncs;
 
235
            return 1;
 
236
        }
 
237
    }
 
238
    lua_newtable(L);
 
239
    return 1;
 
240
}
 
241
 
 
242
static int run_build(lua_State * L)
 
243
{
 
244
    integer cmd, chr, cs;
 
245
    if (lua_isnumber(L, 1)) {
 
246
        cs = 0;
 
247
        chr = lua_tointeger(L, 1);
 
248
        cmd = luaL_optinteger(L, 2, zget_char_cat_code(chr));
 
249
        if (cmd == 0 || cmd == 9 || cmd == 14 || cmd == 15) {
 
250
            fprintf(stdout,
 
251
                    "\n\nluatex error: not a good token.\nCatcode %i can not be returned, so I replaced it by 12 (other)",
 
252
                    (int) cmd);
 
253
            error();
 
254
            cmd = 12;
 
255
        }
 
256
        if (cmd == 13) {
 
257
          cs = active_to_cs(chr,false);
 
258
            cmd = zget_eq_type(cs);
 
259
            chr = zget_equiv(cs);
 
260
        }
 
261
        make_token_table(L, cmd, chr, cs);
 
262
        return 1;
 
263
    } else {
 
264
        return run_lookup(L);
 
265
    }
 
266
}
 
267
 
 
268
 
 
269
static const struct luaL_reg tokenlib[] = {
 
270
    {"get_next", run_get_next},
 
271
    {"expand", run_expand},
 
272
    {"lookup", run_lookup},
 
273
    {"create", run_build},
 
274
    {"is_expandable", test_expandable},
 
275
    {"is_activechar", test_activechar},
 
276
    {"is_protected", test_protected},
 
277
    {"csname_id", run_get_csname_id},
 
278
    {"csname_name", run_get_csname_name},
 
279
    {"command_name", run_get_command_name},
 
280
    {"command_id", run_get_command_id},
 
281
    {NULL, NULL}                /* sentinel */
252
282
};
253
283
 
254
 
int luaopen_token (lua_State *L) 
 
284
int luaopen_token(lua_State * L)
255
285
{
256
 
  luaL_register(L, "token", tokenlib);
257
 
  max_command  = get_max_command();
258
 
  hash_base    = get_hash_base();
259
 
  active_base  = get_active_base();
260
 
  null_cs      = get_nullcs();
261
 
  return 1;
 
286
    luaL_register(L, "token", tokenlib);
 
287
    max_command = get_max_command();
 
288
    hash_base = get_hash_base();
 
289
    null_cs = get_nullcs();
 
290
    return 1;
262
291
}
263