beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / lua / ltexiolib.c
blob37c8fc793364db4cbab2dd97481d49325f972dd0
1 /* ltexiolib.c
3 Copyright 2006-2010 Taco Hoekwater <taco@luatex.org>
5 This file is part of LuaTeX.
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.
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.
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/>. */
20 #include "ptexlib.h"
21 #include "lua/luatex-api.h"
23 typedef void (*texio_printer) (const char *);
25 static char *loggable_info = NULL;
27 static boolean get_selector_value(lua_State * L, int i, int *l)
29 boolean r = false;
30 int t = lua_type(L,i);
31 if (t == LUA_TSTRING) {
32 const char *s = lua_tostring(L, i);
33 if (lua_key_eq(s,term_and_log)) {
34 *l = term_and_log;
35 r = true;
36 } else if (lua_key_eq(s,log)) {
37 *l = log_only;
38 r = true;
39 } else if (lua_key_eq(s,term)) {
40 *l = term_only;
41 r = true;
42 } else {
43 *l = term_and_log;
44 r = true;
46 } else if (t == LUA_TNUMBER) {
47 int n = lua_tointeger(L,i);
48 if (file_can_be_written(n)) {
49 *l = n;
50 r = true;
51 } else {
52 *l = term_and_log;
53 r = true;
55 } else {
56 luaL_error(L, "first argument is not 'term and log', 'term', 'log' or a number");
58 return r;
61 static int do_texio_print(lua_State * L, texio_printer printfunction)
63 const char *s;
64 int i = 1;
65 int save_selector = selector;
66 int n = lua_gettop(L);
67 if (n == 0 || !lua_isstring(L, -1)) { /* or number */
68 luaL_error(L, "no string to print"); /* or number */
70 if (n > 1) {
71 if (get_selector_value(L, i, &selector))
72 i++;
74 if (selector != term_and_log && selector != log_only && selector != term_only) {
75 if (! valid_write_file(selector)) {
76 normalize_selector(); /* sets selector */
79 for (; i <= n; i++) {
80 if (lua_isstring(L, i)) { /* or number */
81 s = lua_tostring(L, i);
82 printfunction(s);
83 } else {
84 luaL_error(L, "argument is not a string");
87 selector = save_selector;
88 return 0;
91 static void do_texio_ini_print(lua_State * L, const char *extra)
93 const char *s;
94 int i = 1;
95 int l = term_and_log;
96 int n = lua_gettop(L);
97 if (n > 1) {
98 if (get_selector_value(L, i, &l))
99 i++;
101 for (; i <= n; i++) {
102 if (lua_isstring(L, i)) { /* or number */
103 s = lua_tostring(L, i);
104 if (l == term_and_log || l == term_only)
105 fprintf(stdout, "%s%s", extra, s);
106 if (l == log_only || l == term_and_log) {
107 if (loggable_info == NULL) {
108 loggable_info = strdup(s);
109 } else {
110 char *v = concat3(loggable_info, extra, s);
111 free(loggable_info);
112 loggable_info = v;
119 static int texio_print(lua_State * L)
121 if (ready_already != 314159 || job_name == 0) {
122 do_texio_ini_print(L, "");
123 return 0;
125 return do_texio_print(L, tprint);
128 static int texio_printnl(lua_State * L)
130 if (ready_already != 314159 || job_name == 0) {
131 do_texio_ini_print(L, "\n");
132 return 0;
134 return do_texio_print(L, tprint_nl);
137 /* at the point this function is called, the selector is log_only */
139 void flush_loggable_info(void)
141 if (loggable_info != NULL) {
142 fprintf(log_file, "%s\n", loggable_info);
143 free(loggable_info);
144 loggable_info = NULL;
148 static int texio_setescape(lua_State * L)
150 escape_controls = lua_tointeger(L,-1);
151 return 0 ;
154 static const struct luaL_Reg texiolib[] = {
155 {"write", texio_print},
156 {"write_nl", texio_printnl},
157 {"setescape", texio_setescape},
158 {NULL, NULL}
161 int luaopen_texio(lua_State * L)
163 luaL_register(L, "texio", texiolib);
164 return 1;