Debian Codegraph @

texk/web2c/luatexdir/lua/ltexiolib.c

raw
1 /* ltexiolib.c
2
3 Copyright 2006-2010 Taco Hoekwater <[email protected]>
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/>. */
19
20 #include "ptexlib.h"
21 #include "lua/luatex-api.h"
22
23 typedef void (*texio_printer) (const char *);
24
25 static char *loggable_info = NULL;
26
27 static boolean get_selector_value(lua_State * L, int i, int *l)
28 {
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;
45 }
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;
54 }
55 } else {
56 luaL_error(L, "first argument is not 'term and log', 'term', 'log' or a number");
57 }
58 return r;
59 }
60
61 static int do_texio_print(lua_State * L, texio_printer printfunction)
62 {
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 */
69 }
70 if (n > 1) {
71 if (get_selector_value(L, i, &selector))
72 i++;
73 }
74 if (selector != term_and_log && selector != log_only && selector != term_only) {
75 if (! valid_write_file(selector)) {
76 normalize_selector(); /* sets selector */
77 }
78 }
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");
85 }
86 }
87 selector = save_selector;
88 return 0;
89 }
90
91 static void do_texio_ini_print(lua_State * L, const char *extra)
92 {
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++;
100 }
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;
113 }
114 }
115 }
116 }
117 }
118
119 static int texio_print(lua_State * L)
120 {
121 if (ready_already != 314159 || job_name == 0) {
122 do_texio_ini_print(L, "");
123 return 0;
124 }
125 return do_texio_print(L, tprint);
126 }
127
128 static int texio_printnl(lua_State * L)
129 {
130 if (ready_already != 314159 || job_name == 0) {
131 do_texio_ini_print(L, "\n");
132 return 0;
133 }
134 return do_texio_print(L, tprint_nl);
135 }
136
137 /* at the point this function is called, the selector is log_only */
138
139 void flush_loggable_info(void)
140 {
141 if (loggable_info != NULL) {
142 fprintf(log_file, "%s\n", loggable_info);
143 free(loggable_info);
144 loggable_info = NULL;
145 }
146 }
147
148 static int texio_setescape(lua_State * L)
149 {
150 if (lua_type(L, 1) == LUA_TBOOLEAN) {
151 escape_controls = lua_toboolean(L,1);
152 } else {
153 escape_controls = lua_tointeger(L,1);
154 }
155 return 0 ;
156 }
157
158 static int texio_closeinput(lua_State * L)
159 {
160 /*
161 printf("before, first %i, index %i, iname %i, inopen %i, pointer %i\n",istart,iindex,iname,in_open,input_ptr);
162 */
163 if (iindex > 0) {
164 end_token_list();
165 end_file_reading();
166 /*
167 printf("after, first %i, index %i, iname %i, inopen %i, pointer %i\n",istart,iindex,iname,in_open,input_ptr);
168 */
169 }
170 return 0 ;
171 }
172
173 static const struct luaL_Reg texiolib[] = {
174 {"write", texio_print},
175 {"write_nl", texio_printnl},
176 {"setescape", texio_setescape},
177 {"closeinput",texio_closeinput},
178 {NULL, NULL}
179 };
180
181 int luaopen_texio(lua_State * L)
182 {
183 luaL_openlib(L, "texio", texiolib, 0);
184 return 1;
185 }