summaryrefslogtreecommitdiff
path: root/source/luametatex/source/lua/lmttexiolib.c
blob: 416293164b93a07a2ac0b907c9a5748fe9b64d46 (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
    See license.txt in the root of this project.
*/

/*tex

    This is a small module that deals with logging. We inherit from \TEX\ the dualistic model
    of console (terminal) and log file. One can write to one of them or both at the same time.
    We also inherit most of the logic that deals with going to a new line but we don't have the
    escaping with |^^| any longer: we live in \UNICODE\ times now. Because \TEX\ itself often
    outputs single characters and/or small strings, the console actually can have some real
    impact on performance: updating the display, rendering with complex fonts, intercepting
    \ANSI\ control sequences, scrolling, etc.

*/

# include "luametatex.h"

FILE *lmt_valid_file(lua_State *L) {
    luaL_Stream *p = (luaL_Stream *) lua_touserdata(L, 1);
    if (p && lua_getmetatable(L, 1)) {
     // luaL_getmetatable(L, LUA_FILEHANDLE);
        lua_get_metatablelua(file_handle_instance);
        if (! lua_rawequal(L, -1, -2)) {
            p = NULL;
        }
        lua_pop(L, 2);
        return (p && (p)->closef) ? p->f : NULL;
    }
    return NULL;
}

typedef void (*texio_printer) (const char *);

inline static int texiolib_aux_get_selector_value(lua_State *L, int i, int *l, int dflt)
{
    switch (lua_type(L, i)) {
        case LUA_TSTRING:
            {
                const char *s = lua_tostring(L, i);
                if (lua_key_eq(s, logfile)) {
                    *l = logfile_selector_code;
                } else if (lua_key_eq(s, terminal)) {
                    *l = terminal_selector_code;
                } else if (lua_key_eq(s, terminal_and_logfile)) {
                    *l = terminal_and_logfile_selector_code;
                } else {
                    *l = dflt;
                }
                return 1;
            }
        case LUA_TNUMBER:
            {
                int n = lmt_tointeger(L, i);
                *l = n >= terminal_selector_code && n <= terminal_and_logfile_selector_code ? n : dflt;
                return 1;
            }
        default:
            return luaL_error(L, "(first) argument is not 'terminal_and_logfile', 'terminal' or 'logfile'");
    }
}

static void texiolib_aux_print(lua_State *L, int n, texio_printer printfunction, const char *dflt)
{
    int i = 1;
    int saved_selector = lmt_print_state.selector;
    if (n > 1 && texiolib_aux_get_selector_value(L, i, &lmt_print_state.selector, terminal_selector_code)) {
        i++;
    }
    switch (lmt_print_state.selector) {
        case terminal_and_logfile_selector_code:
        case logfile_selector_code:
        case terminal_selector_code:
            if (i <= n) {
                do {
                    switch (lua_type(L, i)) {
                        case LUA_TNIL:
                            break;
                        case LUA_TBOOLEAN:
                        case LUA_TNUMBER:
                        case LUA_TSTRING:
                            printfunction(lua_tostring(L, i));
                            break;
                        default:
                            luaL_error(L, "argument is not a string, number or boolean");
                    }
                    i++;
                } while (i <= n);
            } else if (dflt) {
                printfunction(dflt);
            }
        break;
    }
    lmt_print_state.selector = saved_selector;
}

static void texiolib_aux_print_selector(lua_State *L, int n, texio_printer printfunction, const char *dflt)
{
    int saved_selector = lmt_print_state.selector;
    texiolib_aux_get_selector_value(L, 1, &lmt_print_state.selector, no_print_selector_code);
    switch (lmt_print_state.selector) {
        case terminal_and_logfile_selector_code:
        case logfile_selector_code:
        case terminal_selector_code:
            {
                if (n > 1) {
                    for (int i = 2; i <= n; i++) {
                        switch (lua_type(L, i)) {
                            case LUA_TNIL:
                                break;
                            case LUA_TBOOLEAN:
                            case LUA_TNUMBER:
                            case LUA_TSTRING:
                                printfunction(lua_tostring(L, i));
                                break;
                            default:
                                luaL_error(L, "argument is not a string, number or boolean");
                        }
                    };
                } else if (dflt) {
                    printfunction(dflt);
                }
                break;
            }
    }
    lmt_print_state.selector = saved_selector;
}

static void texiolib_aux_print_stdout(lua_State *L, const char *extra)
{
    int i = 1;
    int l = terminal_and_logfile_selector_code;
    int n = lua_gettop(L);
    if (n > 1 && texiolib_aux_get_selector_value(L, i, &l, terminal_selector_code)) {
        i++;
    }
    for (; i <= n; i++) {
        if (lua_isstring(L, i)) { /* or number */
            const char *s = lua_tostring(L, i);
            if (l == terminal_and_logfile_selector_code || l == terminal_selector_code) {
                fputs(extra, stdout);
                fputs(s, stdout);
            }
            if (l == terminal_and_logfile_selector_code || l == logfile_selector_code) {
                if (lmt_print_state.loggable_info) {
                    char *v = (char*) lmt_memory_malloc(strlen(lmt_print_state.loggable_info) + strlen(extra) + strlen(s) + 1);
                    if (v) {
                        sprintf(v, "%s%s%s", lmt_print_state.loggable_info, extra, s);
                    }
                    lmt_memory_free(lmt_print_state.loggable_info);
                    lmt_print_state.loggable_info = v;
                } else {
                    lmt_print_state.loggable_info = lmt_memory_strdup(s);
                }
            }
        }
    }
}

static void texiolib_aux_print_nlp_str(const char *s)
{
    tex_print_nlp();
    tex_print_str(s);
}

static int texiolib_write(lua_State *L)
{
    if (lmt_main_state.ready_already == output_disabled_state || ! lmt_fileio_state.job_name) {
        texiolib_aux_print_stdout(L, "");
    } else {
        int n = lua_gettop(L);
        if (n > 0) {
            texiolib_aux_print(L, n, tex_print_str, NULL);
        } else {
            /*tex We silently ignore bogus calls. */
        }
    }
    return 0;
}


static int texiolib_write_nl(lua_State *L)
{
    if (lmt_main_state.ready_already == output_disabled_state || ! lmt_fileio_state.job_name) {
        texiolib_aux_print_stdout(L, "\n");
    } else {
        int n = lua_gettop(L);
        if (n > 0) {
            texiolib_aux_print(L, n, texiolib_aux_print_nlp_str, "\n");
        } else {
            /*tex We silently ignore bogus calls. */
        }
    }
    return 0;
}

static int texiolib_write_selector(lua_State *L)
{
    if (lmt_main_state.ready_already == output_disabled_state || ! lmt_fileio_state.job_name) {
        texiolib_aux_print_stdout(L, "");
    } else {
        int n = lua_gettop(L);
        if (n > 1) {
            texiolib_aux_print_selector(L, n, tex_print_str, NULL);
        } else {
            /*tex We silently ignore bogus calls. */
        }
    }
    return 0;
}


static int texiolib_write_selector_nl(lua_State *L)
{
    if (lmt_main_state.ready_already == output_disabled_state || ! lmt_fileio_state.job_name) {
        texiolib_aux_print_stdout(L, "\n");
    } else {
        int n = lua_gettop(L);
        if (n > 1) {
            texiolib_aux_print_selector(L, n, texiolib_aux_print_nlp_str, "");
        } else {
            /*tex We silently ignore bogus calls. */
        }
    }
    return 0;
}

static int texiolib_write_selector_lf(lua_State *L)
{
    if (lmt_main_state.ready_already == output_disabled_state || ! lmt_fileio_state.job_name) {
        texiolib_aux_print_stdout(L, "\n");
    } else {
        int n = lua_gettop(L);
        if (n >= 1) {
            texiolib_aux_print_selector(L, n, texiolib_aux_print_nlp_str, "");
        } else {
            /*tex We silently ignore bogus calls. */
        }
    }
    return 0;
}

/*tex At the point this function is called, the selector is log_only. */

static int texiolib_closeinput(lua_State *L)
{
    (void) (L);
    if (lmt_input_state.cur_input.index > 0) {
        tex_end_token_list();
        tex_end_file_reading();
    }
    return 0 ;
}

/*tex
    This is a private hack, handy for testing runtime math font patches in lfg files with a bit of
    low level tracing. Setting the logfile is already handles by a callback so we don't support
    string argument here because we'd end up in that callback which then returns the same logfile
    name as we already had.
*/

static int texiolib_setlogfile(lua_State *L)
{
    FILE *f = lmt_valid_file(L);
    if (f) {
        /* If not writeable then all goes into the void. */
        if (! lmt_print_state.logfile) {
            lmt_print_state.saved_logfile = lmt_print_state.logfile;
            lmt_print_state.saved_logfile_offset = lmt_print_state.logfile_offset;

        }
        lmt_print_state.logfile = f;
        lmt_print_state.logfile_offset = 0;
    } else if (lmt_print_state.logfile) {
        lmt_print_state.logfile = lmt_print_state.saved_logfile;
        lmt_print_state.logfile_offset = lmt_print_state.saved_logfile_offset;
    }
    return 0;
}

static const struct luaL_Reg texiolib_function_list[] = {
    { "write",           texiolib_write             },
    { "writenl",         texiolib_write_nl          },
    { "write_nl",        texiolib_write_nl          }, /* depricated */
    { "writeselector",   texiolib_write_selector    },
    { "writeselectornl", texiolib_write_selector_nl },
    { "writeselectorlf", texiolib_write_selector_lf },
    { "closeinput",      texiolib_closeinput        },
    { "setlogfile",      texiolib_setlogfile        },
    { NULL,              NULL                       },
};

static const struct luaL_Reg texiolib_function_list_only[] = {
    { "write",           texiolib_write             },
    { "writenl",         texiolib_write_nl          },
    { "write_nl",        texiolib_write_nl          }, /* depricated */
    { "writeselector",   texiolib_write_selector    },
    { "writeselectornl", texiolib_write_selector_nl },
    { "writeselectorlf", texiolib_write_selector_lf },
    { NULL,              NULL                       },
};

int luaopen_texio(lua_State *L)
{
    lua_newtable(L);
    luaL_setfuncs(L, lmt_engine_state.lua_only ? texiolib_function_list_only : texiolib_function_list, 0);
    return 1;
}