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/>. */
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
)
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
)) {
36 } else if (lua_key_eq(s
,log
)) {
39 } else if (lua_key_eq(s
,term
)) {
46 } else if (t
== LUA_TNUMBER
) {
47 int n
= lua_tointeger(L
,i
);
48 if (file_can_be_written(n
)) {
56 luaL_error(L
, "first argument is not 'term and log', 'term', 'log' or a number");
61 static int do_texio_print(lua_State
* L
, texio_printer printfunction
)
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 */
71 if (get_selector_value(L
, i
, &selector
))
74 if (selector
!= term_and_log
&& selector
!= log_only
&& selector
!= term_only
) {
75 if (! valid_write_file(selector
)) {
76 normalize_selector(); /* sets selector */
80 if (lua_isstring(L
, i
)) { /* or number */
81 s
= lua_tostring(L
, i
);
84 luaL_error(L
, "argument is not a string");
87 selector
= save_selector
;
91 static void do_texio_ini_print(lua_State
* L
, const char *extra
)
96 int n
= lua_gettop(L
);
98 if (get_selector_value(L
, i
, &l
))
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
);
110 char *v
= concat3(loggable_info
, extra
, s
);
119 static int texio_print(lua_State
* L
)
121 if (ready_already
!= 314159 || job_name
== 0) {
122 do_texio_ini_print(L
, "");
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");
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
);
144 loggable_info
= NULL
;
148 static int texio_setescape(lua_State
* L
)
150 escape_controls
= lua_tointeger(L
,-1);
154 static const struct luaL_Reg texiolib
[] = {
155 {"write", texio_print
},
156 {"write_nl", texio_printnl
},
157 {"setescape", texio_setescape
},
161 int luaopen_texio(lua_State
* L
)
163 luaL_register(L
, "texio", texiolib
);