Debian Codegraph @

texk/texlive/windows_mingw_wrapper/runscript_dll.c

raw
1 /************************************************************************
2
3 This file is a part of the wrapper program for launching scripts
4 and programs in TeX Live on Windows. See readme.txt for more details.
5
6 This file was originally written in 2009 by Tomasz M. Trzeciak and
7 was placed in the Public Domain.
8
9 ************************************************************************/
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <windows.h>
14 #define IS_WHITESPACE(c) ((c == ' ') || (c == '\t'))
15 #define MAX_MSG 2*MAX_PATH
16 #define DIE(...) { _snprintf( msg_buf, MAX_MSG - 1, __VA_ARGS__ ); goto DIE; }
17
18 char module_name[] = "runscript.dll";
19 char script_name[] = "runscript.tlu";
20 char texlua_name[] = "texlua"; // just a bare name, luatex strips the rest anyway
21 char subsys_mode[] = "CUI_MODE\n";
22 char err_env_var[] = "RUNSCRIPT_ERROR_MESSAGE";
23 char msg_buf[MAX_MSG];
24
25
26 __declspec(dllexport) int dllrunscript( int argc, char *argv[] )
27 {
28 static char own_path[MAX_PATH];
29 static char fpath[MAX_PATH];
30 char *fname, *argline, **lua_argv;
31 char *quoted_argline;
32 int k, quoted, lua_argc;
33 HMODULE module_handle = NULL;
34
35 // file path of the executable
36 k = (int) GetModuleFileName(NULL, own_path, MAX_PATH);
37 if ( !k || (k == MAX_PATH) )
38 DIE("cannot get own path (may be too long): %s\n", own_path);
39
40 // script path (the dir of this library)
41 module_handle = GetModuleHandle(module_name);
42 // if ( module_handle == NULL ) exe path will be used, which is OK too
43 k = (int) GetModuleFileName(module_handle, fpath, MAX_PATH);
44 if ( !k || (k == MAX_PATH) )
45 DIE("cannot get module path (may be too long): %s\n", fpath);
46 fname = strrchr(fpath, '\\');
47 if ( fname == NULL ) DIE("no directory part in module path: %s\n", fpath);
48 fname++;
49 if ( fname + strlen(script_name) >= fpath + MAX_PATH - 1 )
50 DIE("path too long: %s\n", fpath);
51 strcpy(fname, script_name);
52 if ( GetFileAttributes(fpath) == INVALID_FILE_ATTRIBUTES )
53 DIE("main lua script not found: %s\n", fpath);
54
55 // get command line of this process
56 argline = GetCommandLine();
57
58 if ( argline == NULL ) DIE("failed to retrieve command line string\n");
59 quoted_argline = malloc((1+strlen(argline)+1 + 1)*sizeof(char));
60 if ( quoted_argline == NULL ) DIE("failed to quote command line string\n");
61 // skip over argv[0] (it can contain embedded double quotes if launched from cmd.exe!)
62 for ( quoted = 0; (*argline) && ( !IS_WHITESPACE(*argline) || quoted ); argline++ )
63 if ( *argline == '"' ) quoted = !quoted;
64 while ( IS_WHITESPACE(*argline) ) argline++; // remove leading whitespace if any
65 // we need to quote our string , it seems.
66 snprintf(quoted_argline, (size_t)(1+strlen(argline)+1 + 1), "\"%s\"", argline);
67
68 // set up argument list for texlua script
69 lua_argv = (char **)malloc( (argc + 6) * sizeof(char *) );
70 lua_argv[lua_argc=0] = texlua_name;
71 lua_argv[++lua_argc] = fpath; // script to execute
72 for ( k = 1; k < argc; k++ ) lua_argv[++lua_argc] = argv[k]; // copy argument list
73 lua_argv[++lua_argc] = subsys_mode; // sentinel argument
74 lua_argv[++lua_argc] = argc ? argv[0] : own_path; // original argv[0]
75 //lua_argv[++lua_argc] = argline; // unparsed arguments
76 lua_argv[++lua_argc] = quoted_argline; // unparsed arguments
77 lua_argv[++lua_argc] = NULL;
78
79 // call texlua interpreter
80 // for(int j=0; j<lua_argc; j++){printf("lua_argv[%d]=%s\n",j,lua_argv[j]);}
81 k = _spawnvp(_P_WAIT, "texlua", (const char * const *)lua_argv);
82 if (lua_argv) free(lua_argv);
83 return k;
84
85 DIE:
86 fprintf(stderr, "%s: ", module_name);
87 fprintf(stderr, msg_buf);
88 if (*subsys_mode == 'G')
89 MessageBox( NULL, msg_buf, module_name, MB_ICONERROR | MB_SETFOREGROUND );
90 return 1;
91 }
92
93 void finalize( void )
94 {
95 // check for and display error message if any
96 char *err_msg;
97 if ( err_msg = (char *) getenv(err_env_var) )
98 MessageBox( NULL, err_msg, script_name, MB_ICONERROR | MB_SETFOREGROUND );
99 }
100
101 __declspec(dllexport) int dllwrunscript(
102 HINSTANCE hInstance,
103 HINSTANCE hPrevInstance,
104 char *argline,
105 int winshow
106 ) {
107 // set sentinel argument (G for GUI_MODE)
108 *subsys_mode = 'G';
109 // clear error var in case it exists already
110 SetEnvironmentVariable(err_env_var, NULL);
111 // register atexit handler to recover control before terminating
112 atexit( finalize );
113 // call the console entry point routine
114 #if defined(__MSVCRT__) && !defined(__MINGW32__)
115 // WinMain doesn't provide argc & argv, call MSVCRT proc to get them
116 int argc = 0;
117 char **argv, **env;
118 int expand_wildcards = 0;
119 int new_mode;
120 __getmainargs(&argc, &argv, &env, expand_wildcards, &new_mode);
121 return dllrunscript( argc, argv );
122 #else
123 return dllrunscript( 0, NULL );
124 #endif
125 }
126
127