static string maketex (kpathsea kpse, kpse_file_format_type format, string* args) { /* New implementation, use fork/exec pair instead of popen, since * the latter is virtually impossible to make safe. */ unsigned len; string *s; string ret = NULL; string fn; #if defined(WIN32) char fullbin[256], *wrp; wrp = kpathsea_var_value(kpse, "SELFAUTOLOC"); if(wrp == NULL) { fprintf(stderr, "I cannot get SELFAUTOLOC\n"); exit(100); } strcpy(fullbin, wrp); free(wrp); for(wrp=fullbin; *wrp; wrp++) { if(*wrp == '/') *wrp = '\\'; } strcat(fullbin, "\\"); strcat(fullbin, args[0]); #endif if (!kpse->make_tex_discard_errors) { fprintf (stderr, "\nkpathsea: Running"); for (s = &args[0]; *s != NULL; s++) fprintf (stderr, " %s", *s); fputc('\n', stderr); } #if defined (AMIGA) /* Amiga has a different interface. */ { string cmd; string newcmd; cmd = xstrdup(args[0]); for (s = &args[1]; *s != NULL; s++) { newcmd = concat(cmd, *s); free (cmd); cmd = newcmd; } ret = system(cmd) == 0 ? getenv ("LAST_FONT_CREATED"): NULL; free (cmd); } #elif defined (MSDOS) && !defined(__DJGPP__) #error Implement new MSDOS mktex call interface here #else /* WIN32 or Unix */ { #if defined (WIN32) /* spawnvp(_P_NOWAIT, ...) and pipe --ak 2002/12/15 */ unsigned long nexitcode = STILL_ACTIVE; HANDLE hchild; int hstdout, childpipe[2]; int hstderr = -1; FILE *Hnul = NULL; fn = NULL; if(_pipe(childpipe, 1024, O_TEXT | _O_NOINHERIT) == -1) { perror("kpathsea: pipe()"); goto labeldone; } hstdout = _dup(fileno(stdout)); if(_dup2(childpipe[1], fileno(stdout)) != 0) { close(hstdout); close(childpipe[0]); close(childpipe[1]); goto labeldone; } close(childpipe[1]); if(kpse->make_tex_discard_errors) { Hnul = fopen("nul", "w"); if(!Hnul) { perror("kpathsea: fopen(\"nul\")"); } else { hstderr = _dup(fileno(stderr)); _dup2(fileno(Hnul), fileno(stderr)); } } fprintf(stderr, "\nThe command name is %s\n", fullbin); hchild = (HANDLE)_spawnvp(_P_NOWAIT, fullbin, (const char * const *) args); _dup2(hstdout, fileno(stdout)); close(hstdout); if(hchild == (HANDLE)(-1)) { close(childpipe[0]); goto labeldone; } if(hchild) { char buf[1024+1]; int num; fn = xstrdup(""); while(nexitcode == STILL_ACTIVE) { num = read(childpipe[0], buf, sizeof(buf)-1); if(num) { string newfn; buf[num] = '\0'; newfn = concat(fn, buf); free(fn); fn = newfn; } if(!GetExitCodeProcess(hchild, &nexitcode)) { fn = NULL; close(childpipe[0]); goto labeldone; } } close(childpipe[0]); } labeldone: if(kpse->make_tex_discard_errors && Hnul) { _dup2(hstderr, fileno(stderr)); close(hstderr); fclose(Hnul); } #else /* !WIN32 */ /* Standard input for the child. Set to /dev/null */ int childin; /* Standard output for the child, what we're interested in. */ int childout[2]; /* Standard error for the child, same as parent or /dev/null */ int childerr; /* Child pid. */ pid_t childpid; /* Open the channels that the child will use. */ /* A fairly horrible uses of gotos for here for the error case. */ if ((childin = open("/dev/null", O_RDONLY)) < 0) { perror("kpathsea: open(\"/dev/null\", O_RDONLY)"); goto error_childin; } if (pipe(childout) < 0) { perror("kpathsea: pipe()"); goto error_childout; } if ((childerr = open("/dev/null", O_WRONLY)) < 0) { perror("kpathsea: open(\"/dev/null\", O_WRONLY)"); goto error_childerr; } if ((childpid = fork()) < 0) { perror("kpathsea: fork()"); close(childerr); error_childerr: close(childout[0]); close(childout[1]); error_childout: close(childin); error_childin: fn = NULL; } else if (childpid == 0) { /* Child * * We can use vfork, provided we're careful about what we * do here: do not return from this function, do not modify * variables, call _exit if there is a problem. * * Complete setting up the file descriptors. * We use dup(2) so the order in which we do this matters. */ close(childout[0]); /* stdin -- the child will not receive input from this */ if (childin != 0) { close(0); if (dup(childin) != 0) { perror("kpathsea: dup(2) failed for stdin"); close(childin); _exit(1); } close(childin); } /* stdout -- the output of the child's action */ if (childout[1] != 1) { close(1); if (dup(childout[1]) != 1) { perror("kpathsea: dup(2) failed for stdout"); close(childout[1]); _exit(1); } close(childout[1]); } /* stderr -- use /dev/null if we discard errors */ if (childerr != 2) { if (kpse->make_tex_discard_errors) { close(2); if (dup(childerr) != 2) { perror("kpathsea: dup(2) failed for stderr"); close(childerr); _exit(1); } } close(childerr); } /* FIXME: We could/should close all other file descriptors as well. */ /* exec -- on failure a call of _exit(2) it is the only option */ if (execvp(args[0], args)) perror(args[0]); _exit(1); } else { /* Parent */ char buf[1024+1]; int num; /* Clean up child file descriptors that we won't use anyway. */ close(childin); close(childout[1]); close(childerr); /* Get stdout of child from the pipe. */ fn = xstrdup(""); while ((num = read(childout[0],buf,sizeof(buf)-1)) != 0) { if (num == -1) { if (errno != EINTR) { perror("kpathsea: read()"); break; } } else { string newfn; buf[num] = '\0'; newfn = concat(fn, buf); free(fn); fn = newfn; } } /* End of file on pipe, child should have exited at this point. */ close(childout[0]); /* We don't really care about the exit status at this point. */ wait(NULL); } #endif /* !WIN32 */ if (fn) { len = strlen(fn); /* Remove trailing newlines and returns. */ while (len && (fn[len - 1] == '\n' || fn[len - 1] == '\r')) { fn[len - 1] = '\0'; len--; } ret = len == 0 ? NULL : kpathsea_readable_file (kpse, fn); if (!ret && len > 1) { WARNING2 ("kpathsea: %s output `%s' instead of a filename", args[0], fn); } /* Free the name if we're not returning it. */ if (fn != ret) free (fn); } } #endif /* WIN32 or Unix */ if (ret == NULL) misstex (kpse, format, args); else kpathsea_db_insert (kpse, ret); return ret; }
static string maketex P2C(kpse_file_format_type, format, string*, args) { /* New implementation, use fork/exec pair instead of popen, since * the latter is virtually impossible to make safe. */ unsigned len; string *s; string ret; string fn; if (!kpse_make_tex_discard_errors) { fprintf (stderr, "kpathsea: Running"); for (s = &args[0]; *s != NULL; s++) fprintf (stderr, " %s", *s); fputc('\n', stderr); } #if defined (AMIGA) /* Amiga has a different interface. */ { string cmd; string newcmd; cmd = xstrdup(args[0]); for (s = &args[1]; *s != NULL; s++) { newcmd = concat(cmd, *s); free (cmd); cmd = newcmd; } ret = system(cmd) == 0 ? getenv ("LAST_FONT_CREATED"): NULL; free (cmd); } #elif defined (MSDOS) #error Implement new MSDOS mktex call interface here #elif defined (WIN32) /* We would vastly prefer to link directly with mktex.c here. Unfortunately, it is not quite possible because kpathsea is not reentrant. The progname is expected to be set in mktex.c and various initialisations occur. So to be safe, we implement a call sequence equivalent to the Unix one. */ { STARTUPINFO si; PROCESS_INFORMATION pi; HANDLE child_in, child_out, child_err; HANDLE father_in, father_out_dup; HANDLE current_pid; SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE}; string new_cmd = NULL, app_name = NULL; char buf[1024+1]; int num; extern char *quote_args(char **argv); if (look_for_cmd(args[0], &app_name) == FALSE) { ret = NULL; goto error_exit; } /* Compute the command line */ new_cmd = quote_args(args); /* We need this handle to duplicate other handles */ current_pid = GetCurrentProcess(); ZeroMemory( &si, sizeof(STARTUPINFO) ); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW ; si.wShowWindow = /* 0 */ SW_HIDE ; /* Child stdin */ child_in = CreateFile("nul", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, /* non inheritable */ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); si.hStdInput = child_in; if (CreatePipe(&father_in, &child_out, NULL, 0) == FALSE) { fprintf(stderr, "popen: error CreatePipe\n"); goto error_exit; } if (DuplicateHandle(current_pid, child_out, current_pid, &father_out_dup, 0, TRUE, DUPLICATE_SAME_ACCESS) == FALSE) { fprintf(stderr, "popen: error DuplicateHandle father_in\n"); CloseHandle(father_in); CloseHandle(child_out); goto error_exit; } CloseHandle(child_out); si.hStdOutput = father_out_dup; /* Child stderr */ if (kpse_make_tex_discard_errors) { child_err = CreateFile("nul", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, /* non inheritable */ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } else { DuplicateHandle(current_pid, GetStdHandle(STD_ERROR_HANDLE), current_pid, &child_err, 0, TRUE, DUPLICATE_SAME_ACCESS); } si.hStdError = child_err; /* creating child process */ if (CreateProcess(app_name, /* pointer to name of executable module */ new_cmd, /* pointer to command line string */ NULL, /* pointer to process security attributes */ NULL, /* pointer to thread security attributes */ TRUE, /* handle inheritance flag */ 0, /* creation flags */ NULL, /* pointer to environment */ NULL, /* pointer to current directory */ &si, /* pointer to STARTUPINFO */ &pi /* pointer to PROCESS_INFORMATION */ ) == 0) { FATAL2("kpathsea: CreateProcess() failed for `%s' (Error %x)\n", new_cmd, GetLastError()); } CloseHandle(child_in); CloseHandle(father_out_dup); CloseHandle(child_err); /* Only the process handle is needed */ CloseHandle(pi.hThread); /* Get stdout of child from the pipe. */ fn = xstrdup(""); while (ReadFile(father_in,buf,sizeof(buf)-1, &num, NULL) != 0 && num > 0) { if (num <= 0) { if (GetLastError() != ERROR_BROKEN_PIPE) { FATAL1("kpathsea: read() error code for `%s' (Error %d)", GetLastError()); break; } } else { string newfn; buf[num] = '\0'; newfn = concat(fn, buf); free(fn); fn = newfn; } } /* End of file on pipe, child should have exited at this point. */ CloseHandle(father_in); if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_OBJECT_0) { WARNING2("kpathsea: failed to wait for process termination: %s (Error %d)\n", new_cmd, GetLastError()); } CloseHandle(pi.hProcess); if (new_cmd) free(new_cmd); if (app_name) free(app_name); if (fn) { len = strlen(fn); /* Remove trailing newlines and returns. */ while (len && (fn[len - 1] == '\n' || fn[len - 1] == '\r')) { fn[len - 1] = '\0'; len--; } ret = len == 0 ? NULL : kpse_readable_file (fn); if (!ret && len > 1) { WARNING1 ("kpathsea: mktexpk output `%s' instead of a filename", fn); } /* Free the name if we're not returning it. */ if (fn != ret) free (fn); } error_exit: ; } #else { /* Standard input for the child. Set to /dev/null */ int childin; /* Standard output for the child, what we're interested in. */ int childout[2]; /* Standard error for the child, same as parent or /dev/null */ int childerr; /* Child pid. */ pid_t childpid; /* Open the channels that the child will use. */ /* A fairly horrible uses of gotos for here for the error case. */ if ((childin = open("/dev/null", O_RDONLY)) < 0) { perror("kpathsea: open(\"/dev/null\", O_RDONLY)"); goto error_childin; } if (pipe(childout) < 0) { perror("kpathsea: pipe()"); goto error_childout; } if ((childerr = open("/dev/null", O_WRONLY)) < 0) { perror("kpathsea: open(\"/dev/null\", O_WRONLY)"); goto error_childerr; } if ((childpid = fork()) < 0) { perror("kpathsea: fork()"); close(childerr); error_childerr: close(childout[0]); close(childout[1]); error_childout: close(childin); error_childin: fn = NULL; } else if (childpid == 0) { /* Child * * We can use vfork, provided we're careful about what we * do here: do not return from this function, do not modify * variables, call _exit if there is a problem. * * Complete setting up the file descriptors. * We use dup(2) so the order in which we do this matters. */ close(childout[0]); /* stdin -- the child will not receive input from this */ if (childin != 0) { close(0); dup(childin); close(childin); } /* stdout -- the output of the child's action */ if (childout[1] != 1) { close(1); dup(childout[1]); close(childout[1]); } /* stderr -- use /dev/null if we discard errors */ if (childerr != 2) { if (kpse_make_tex_discard_errors) { close(2); dup(childerr); } close(childerr); } /* FIXME: We could/should close all other file descriptors as well. */ /* exec -- on failure a call of _exit(2) it is the only option */ if (execvp(args[0], args)) perror(args[0]); _exit(1); } else { /* Parent */ char buf[1024+1]; int num; int status; /* Clean up child file descriptors that we won't use anyway. */ close(childin); close(childout[1]); close(childerr); /* Get stdout of child from the pipe. */ fn = xstrdup(""); while ((num = read(childout[0],buf,sizeof(buf)-1)) != 0) { if (num == -1) { if (errno != EINTR) { perror("kpathsea: read()"); break; } } else { string newfn; buf[num] = '\0'; newfn = concat(fn, buf); free(fn); fn = newfn; } } /* End of file on pipe, child should have exited at this point. */ close(childout[0]); /* We don't really care about the exit status at this point. */ wait(NULL); } if (fn) { len = strlen(fn); /* Remove trailing newlines and returns. */ while (len && (fn[len - 1] == '\n' || fn[len - 1] == '\r')) { fn[len - 1] = '\0'; len--; } ret = len == 0 ? NULL : kpse_readable_file (fn); if (!ret && len > 1) { WARNING1 ("kpathsea: mktexpk output `%s' instead of a filename", fn); } /* Free the name if we're not returning it. */ if (fn != ret) free (fn); } else { ret = NULL; } } #endif if (ret == NULL) misstex (format, args); else kpse_db_insert (ret); return ret; }