Skip to content

Instantly share code, notes, and snippets.

@h20y6m
Created August 5, 2024 12:48
Show Gist options
  • Save h20y6m/436c0bbaf4c2476f7e1fa90dca52ae9e to your computer and use it in GitHub Desktop.
Save h20y6m/436c0bbaf4c2476f7e1fa90dca52ae9e to your computer and use it in GitHub Desktop.
A simple wrapper to invoke the extractbb mode of xdvipdfmx
/* A simple wrapper to invoke the extractbb mode of xdvipdfmx
Copyright 2024 Yukimasa Morimi (@h20y6m)
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
static void append_arg(wchar_t *command, const wchar_t *arg);
int wmain(int argc, wchar_t *argv[])
{
wchar_t wszExePath[32768 + 256] = { 0 };
wchar_t wszCommand[32768] = { 0 };
int nRet = 0;
_wputenv_s(L"openin_any", L"r");
_wputenv_s(L"openout_any", L"p");
_wputenv_s(L"command_line_encoding", L"utf-8");
_wputenv_s(L"TEXLIVE_WINDOWS_EXTERNAL_GS", L"NUL");
/* Get xdvipdfmx.exe path in self directory. */
{
wchar_t *ptr;
DWORD nSize;
nSize = GetModuleFileNameW(NULL, wszExePath, 32768);
if (nSize == 0 || nSize >= 32768)
{
fprintf(stderr, "extractbb-wrapper: error: GetModuleFileNameW failed (LastError=%d)\n", (int)GetLastError());
return 1;
}
ptr = wcsrchr(wszExePath, L'\\');
ptr = (ptr == NULL) ? &wszExePath[0] : (ptr + 1);
wcscpy(ptr, L"xdvipdfmx.exe");
#ifdef DEBUG
wcscpy(wszExePath, L"C:\\texlive\\2024\\bin\\windows\\xdvipdfmx.exe");
#endif /* DEBUG */
}
#ifdef DEBUG
fprintf(stderr, "extractbb-wrapper: debug: wszExePath=");
WriteConsoleW(GetStdHandle(STD_ERROR_HANDLE), wszExePath, (DWORD)wcslen(wszExePath), NULL, NULL);
fprintf(stderr, "\n");
#endif /* DEBUG */
/* Make command line */
{
int i;
wcscpy(wszCommand, L"ebb");
append_arg(wszCommand, L"--extractbb");
for (i = 1; i < argc; i++)
{
append_arg(wszCommand, argv[i]);
}
}
#ifdef DEBUG
fprintf(stderr, "extractbb-wrapper: debug: wszCommand=");
WriteConsoleW(GetStdHandle(STD_ERROR_HANDLE), wszCommand, (DWORD)wcslen(wszCommand), NULL, NULL);
fprintf(stderr, "\n");
#endif /* DEBUG */
/* Execute xdvipdfmx */
{
STARTUPINFOW si = { 0 };
PROCESS_INFORMATION pi = { 0 };
DWORD dwExitCode = 0;
si.cb = (DWORD)sizeof(si);
if (!CreateProcessW(wszExePath, wszCommand, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
fprintf(stderr, "extractbb-wrapper: error: CreateProcessW failed (LastError=%d)\n", (int)GetLastError());
return 1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &dwExitCode);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
nRet = (int)dwExitCode;
}
#ifdef DEBUG
fprintf(stderr, "extractbb-wrapper: debug: nRet=%d\n", (int)nRet);
#endif /* DEBUG */
return nRet;
}
static void append_arg(wchar_t *command, const wchar_t *arg)
{
int arg_has_space = wcschr(arg, L' ') != NULL;
int arg_has_quote = wcschr(arg, L'"') != NULL;
if (arg_has_space || arg_has_quote)
{
wchar_t *p_cmd = command + wcslen(command);
const wchar_t *p_arg = arg;
size_t n = 0;
*p_cmd = L' '; p_cmd++;
if (arg_has_space)
{
*p_cmd = L'"'; p_cmd++;
}
while (*p_arg != L'\0')
{
if (*p_arg == L'"')
{
/* Double the '\\' that precedes the '"' */
while (n > 0)
{
*p_cmd = L'\\'; p_cmd++;
n--;
}
/* Escaping '"' */
*p_cmd = L'\\'; p_cmd++;
}
else if (*p_arg == L'\\')
{
/* Count '\\' */
n++;
}
else
{
/* reset '\\' count */
n = 0;
}
*p_cmd = *p_arg;
p_cmd++;
p_arg++;
}
if (arg_has_space)
{
/* Double the '\\' that precedes the '"' */
while (n > 0)
{
*p_cmd = L'\\'; p_cmd++;
n--;
}
*p_cmd = L'"'; p_cmd++;
}
*p_cmd = L'\0';
}
else
{
wcscat(command, L" ");
wcscat(command, arg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment