Debian Codegraph @

texk/texlive/windows_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 __declspec(dllimport) int dllluatexmain( int argc, char *argv[] );
26
27 __declspec(dllexport) int dllrunscript( int argc, char *argv[] )
28 {
29 static char own_path[MAX_PATH];
30 static char fpath[MAX_PATH];
31 char *fname, *argline, **lua_argv;
32 int k, quoted, lua_argc;
33 HMODULE module_handle = NULL;
34
35 /* change argv[0] into long file name */
36
37 char buff[260];
38 char infile[260];
39 char *fp;
40
41 if (argc) {
42 char *pp;
43 char ppp[260];
44 strcpy(ppp, argv[0]);
45 pp = strrchr(ppp, '.');
46 if (!pp || stricmp(pp, ".exe")) {
47 strcat(ppp, ".exe");
48 }
49 argv[0] = ppp;
50 k = SearchPath(NULL, argv[0], NULL, 260, infile, &fp);
51 if (!k)
52 DIE("cannot find %s\n", argv[0]);
53 k = GetLongPathNameA(infile, buff, 260);
54 if (!k)
55 DIE("cannot find %s\n", argv[0]);
56 argv[0] = buff;
57 }
58
59 // file path of the executable
60 k = (int) GetModuleFileName(NULL, own_path, MAX_PATH);
61 if ( !k || (k == MAX_PATH) )
62 DIE("cannot get own path (may be too long): %s\n", own_path);
63
64 // script path (the dir of this library)
65 module_handle = GetModuleHandle(module_name);
66 // if ( module_handle == NULL ) exe path will be used, which is OK too
67 k = (int) GetModuleFileName(module_handle, fpath, MAX_PATH);
68 if ( !k || (k == MAX_PATH) )
69 DIE("cannot get module path (may be too long): %s\n", fpath);
70 fname = strrchr(fpath, '\\');
71 if ( fname == NULL ) DIE("no directory part in module path: %s\n", fpath);
72 fname++;
73 if ( fname + strlen(script_name) >= fpath + MAX_PATH - 1 )
74 DIE("path too long: %s\n", fpath);
75 strcpy(fname, script_name);
76 if ( GetFileAttributes(fpath) == INVALID_FILE_ATTRIBUTES )
77 DIE("main lua script not found: %s\n", fpath);
78
79 // get command line of this process
80 argline = GetCommandLine();
81 if ( argline == NULL ) DIE("failed to retrieve command line string\n");
82 // skip over argv[0] (it can contain embedded double quotes if launched from cmd.exe!)
83 for ( quoted = 0; (*argline) && ( !IS_WHITESPACE(*argline) || quoted ); argline++ )
84 if ( *argline == '"' ) quoted = !quoted;
85 while ( IS_WHITESPACE(*argline) ) argline++; // remove leading whitespace if any
86
87 // set up argument list for texlua script
88 lua_argv = (char **)malloc( (argc + 6) * sizeof(char *) );
89 lua_argv[lua_argc=0] = texlua_name;
90 lua_argv[++lua_argc] = fpath; // script to execute
91 for ( k = 1; k < argc; k++ ) lua_argv[++lua_argc] = argv[k]; // copy argument list
92 lua_argv[++lua_argc] = subsys_mode; // sentinel argument
93 lua_argv[++lua_argc] = argc ? argv[0] : own_path; // original argv[0]
94 lua_argv[++lua_argc] = argline; // unparsed arguments
95 lua_argv[++lua_argc] = NULL;
96
97 // call texlua interpreter
98 // dllluatexmain never returns, but we pretend that it does
99 k = dllluatexmain( lua_argc, lua_argv );
100 if (lua_argv) free(lua_argv);
101 return k;
102
103 DIE:
104 if (*subsys_mode != 'G') {
105 fprintf(stderr, "%s: ", module_name);
106 fprintf(stderr, msg_buf);
107 }
108 if (*subsys_mode == 'G')
109 MessageBox( NULL, msg_buf, module_name, MB_ICONERROR | MB_SETFOREGROUND );
110 return 1;
111 }
112
113 void finalize( void )
114 {
115 // check for and display error message if any
116 char *err_msg;
117 if (*subsys_mode == 'G') {
118 if ( err_msg = (char *) getenv(err_env_var) )
119 MessageBox( NULL, err_msg, script_name, MB_ICONERROR | MB_SETFOREGROUND );
120 }
121 if (*subsys_mode != 'G') {
122 if ( err_msg = (char *) getenv(err_env_var) ) {
123 fprintf(stderr, "%s: ", script_name);
124 fprintf(stderr, "%s\n", err_msg);
125 }
126 }
127 }
128
129 __declspec(dllexport) int dllwrunscript(
130 HINSTANCE hInstance,
131 HINSTANCE hPrevInstance,
132 char *argline,
133 int winshow
134 ) {
135 // set sentinel argument (G for GUI_MODE)
136 *subsys_mode = 'G';
137 // clear error var in case it exists already
138 SetEnvironmentVariable(err_env_var, NULL);
139 // register atexit handler to recover control before terminating
140 atexit( finalize );
141 // call the console entry point routine
142 #ifdef __MSVCRT__
143 // WinMain doesn't provide argc & argv, call MSVCRT proc to get them
144 int argc = 0;
145 char **argv, **env;
146 int expand_wildcards = 0;
147 int new_mode;
148 __getmainargs(&argc, &argv, &env, expand_wildcards, &new_mode);
149 return dllrunscript( argc, argv );
150 #else
151 return dllrunscript( 0, NULL );
152 #endif
153 }