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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/* stackenv.c: routines that help to mimic shell scripts.
Copyright (C) 1997 Fabrice POPINEAU.
Adapted to DJGPP by Eli Zaretskii.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include "stackenv.h"
#include "variables.h"
string mktex_version_string = "2.0";
extern char *output;
extern char tmpdir[];
extern char *progname;
/* The global stack. */
static mod_env stack_env[256];
static int index_env = 0;
void mt_exit(int);
static FILE *fnul = NULL;
pfnOutputAndCleanup output_and_cleanup_function;
#ifndef _WIN32
#include <unistd.h>
void flushall (void)
{
/* Simulate `flushall' by only flushing standard streams.
This should be enough, since the non-standard are going
to be closed in a moment, and will be flushed then. */
fflush (stdout);
fsync (fileno (stdout));
fflush (stderr);
fsync (fileno (stderr));
}
#endif
#ifndef _WIN32
#ifdef __DJGPP__
/* DJGPP-specific emulation of functions which aren't in the library
and aren't ANSI/POSIX. */
#include <errno.h>
#include <libc/file.h>
static void fcloseall_func (FILE *f)
{
fflush (f);
if (fileno (f) > 2)
fclose(f);
else
fsync (fileno (f));
}
void fcloseall (void)
{
_fwalk (fcloseall_func);
}
#else /* not __DJGPP__ */
/* FIXME: is this enough on Unix? */
static void fcloseall (void)
{
flushall ();
}
#endif /* not __DJGPP__ */
#endif /* not _WIN32 */
void
oops(const char *message, ...)
{
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
va_end(args);
fputc('\n', stderr);
mt_exit(1);
}
/* pushd */
void pushd(char *p)
{
if (index_env >= sizeof(stack_env)/sizeof(stack_env[0])) {
fprintf(stderr, "%s: stack overflow in pushd\n", progname);
mt_exit(1);
}
if ((stack_env[index_env].data.path = malloc(MAXPATHLEN)) == NULL
|| getcwd(stack_env[index_env].data.path, MAXPATHLEN) == NULL) {
fprintf(stderr, "pushd error!\n");
mt_exit(1);
}
stack_env[index_env].op = CHDIR;
if (KPSE_DEBUG_P(MKTEX_FINE_DEBUG)) {
fprintf(stderr, "At %d, pushing from %s to %s\n", index_env,
stack_env[index_env].data.path, p);
}
index_env++;
if (chdir(p) == -1) {
perror(p);
mt_exit(1);
}
}
/* popd */
void popd()
{
if (index_env == 0)
return;
index_env--;
if (KPSE_DEBUG_P(MKTEX_FINE_DEBUG)) {
fprintf(stderr, "At %d, popping %s\n", index_env, stack_env[index_env].data.path);
}
assert(stack_env[index_env].op == CHDIR);
if (chdir(stack_env[index_env].data.path) == -1) {
perror(stack_env[index_env].data.path);
mt_exit(1);
}
free(stack_env[index_env].data.path);
return;
}
/* random access to directories' stack */
char *peek_dir(int n)
{
int i;
int level = n;
/* The stack holds both directories pushed by `pushd' and file
handles pushed by `push_fd'. When they say "peek_dir(n)", they
mean the n'th DIRECTORY entry, not the n'th entry counting from
the stack bottom.
For example, if the stack looks like this:
idx type
0 handles
1 handles
2 directory
3 handles <--- top of the stack
4 <empty> <--- index_env
then "peek_dir(0)" should return the entry with the index 2, not
the one with the index 0.
The following loop looks for the index i of a stack slot which
holds the n'th directory pushed on to the stack. */
do {
for (i = 0; i < index_env && stack_env[i].op != CHDIR; i++)
;
} while (i < index_env && n--);
if (i < index_env)
return stack_env[i].data.path;
else {
fprintf (stderr, "%s: attempt to peek at invalid stack level %d\n",
progname, level);
mt_exit(1);
}
}
/* redirect */
void push_fd(int newfd[3])
{
int i;
if (KPSE_DEBUG_P(MKTEX_FINE_DEBUG)) {
fprintf(stderr, "At %d, pushing fds %d %d %d\n", index_env, newfd[0],
newfd[1], newfd[2]);
}
flushall();
if (index_env >= sizeof(stack_env)/sizeof(stack_env[0])) {
fprintf(stderr, "%s: stack overflow in push_fd\n", progname);
mt_exit(1);
}
stack_env[index_env].op = REDIRECT;
for(i = 0; i < 3; i++) {
if (i == newfd[i])
stack_env[index_env].data.oldfd[i] = i;
else {
if ((stack_env[index_env].data.oldfd[i] = dup(i)) == -1) {
perror("push_fd: dup");
mt_exit(1);
}
if (dup2(newfd[i], i) == -1) {
perror("push_fd : dup2");
mt_exit(1);
}
}
}
index_env++;
}
void pop_fd()
{
int i;
index_env--;
assert(stack_env[index_env].op == REDIRECT);
if (KPSE_DEBUG_P(MKTEX_FINE_DEBUG)) {
fprintf(stderr, "At %d; popping fds %d %d %d\n",
index_env,
stack_env[index_env].data.oldfd[0],
stack_env[index_env].data.oldfd[1],
stack_env[index_env].data.oldfd[2]);
}
flushall();
for(i = 0; i < 3; i++)
if (i != stack_env[index_env].data.oldfd[i]) {
if (fnul && i != fileno(fnul)) close(i);
if (dup2(stack_env[index_env].data.oldfd[i], i) == -1) {
perror("pop_fd : dup2");
mt_exit(1);
}
if (fnul && stack_env[index_env].data.oldfd[i] != fileno(fnul))
close(stack_env[index_env].data.oldfd[i]);
}
}
/* popenv */
void popenv()
{
if (index_env <= 0) {
if (KPSE_DEBUG_P(MKTEX_FINE_DEBUG))
fprintf(stderr, "Trying to popenv() from empty stack!\n");
return;
}
switch(stack_env[index_env-1].op) {
case CHDIR:
popd();
break;
case REDIRECT:
pop_fd();
break;
default:
fprintf(stderr, "popenv : unknown op %d.\n", stack_env[index_env-1].op);
break;
}
}
#ifdef _WIN32
BOOL sigint_handler(DWORD dwCtrlType)
{
/* Fix me : there is a problem if a system() command is running.
We should wait for the son process to be interrupted.
Only way I can think of to do that : rewrite system() based on
spawn() with parsing of the command line and set a global pid
Next cwait(pid) in the HandlerRoutine.
*/
/* This is not that good, but else we would need to wait for
the child processes to finish ! */
Sleep(250);
mt_exit(3);
return FALSE; /* return value obligatory */
}
#else
void sigint_handler (int sig)
{
mt_exit(3);
}
#endif
void mt_exit(int code)
{
/* rtablir les 0, 1 et 2 d'origine avant de tenter quoi que ce soit ! */
fcloseall();
/* unstack all env from main) */
for( ; index_env > 0; popenv());
/* output the result and rm any unneeded file */
if (output_and_cleanup_function != 0)
(*output_and_cleanup_function)(code);
exit(code);
}
void
dump_stack()
{
int i;
for (i = 0; i < index_env; i++)
switch (stack_env[i].op) {
case CHDIR:
fprintf(stderr, "Env %d : chdir %s\n", i, stack_env[i].data.path);
break;
case REDIRECT:
fprintf(stderr, "Env %d : redirect %d %d %d\n", i,
stack_env[i].data.oldfd[0],
stack_env[i].data.oldfd[1],
stack_env[i].data.oldfd[2]);
break;
}
}
/*
This is used by mktex{mf,tfm,pk}
if the argument is true, stdout is redirected to fnul
else to stderr.
*/
void start_redirection(boolean discard_stdout)
{
int newfd[3];
if (fnul == NULL)
#ifdef _WIN32
fnul = fopen("nul", "r"); /* fopen /dev/null */
#else
fnul = fopen("/dev/null", "r");
#endif
newfd[0] = fileno(fnul);
newfd[1] = (discard_stdout ? fileno(fnul) : fileno(stderr));
newfd[2] = fileno(stderr);
push_fd(newfd);
}
|