1 /* run -- Wrapper program for console mode programs under Windows(TM)
2 * Copyright (C) 1998 Charles S. Wilson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * This program is based on the runemacs.c distributed with XEmacs 21.0
22 * Simple program to start gnu-win32 X11 programs (and native XEmacs)
23 * with its console window hidden.
25 * This program is provided purely for convenience, since most users will
26 * use XEmacs in windowing (GUI) mode, and will not want to have an extra
27 * console window lying around. Ditto for desktop shortcuts to gnu-win32
43 #if defined(__CYGWIN__)
44 #include <sys/types.h>
46 #include <sys/cygwin.h>
47 #include <sys/unistd.h>
48 WinMainCRTStartup() { mainCRTStartup(); }
57 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
59 int wait_for_child = FALSE;
60 int compact_invocation = FALSE;
64 char execname[FILENAME_MAX];
65 char execpath[MAX_PATH];
66 char* argv[MAX_ARGS+1]; /* leave extra slot for compact_invocation argv[0] */
69 char exec[MAX_PATH + FILENAME_MAX + 100];
70 char cmdline2[MAX_ARGS * MAX_PATH];
72 compact_invocation = get_exec_name_and_path(execname,execpath);
74 if (compact_invocation)
77 argc = parse_cmdline_to_arg_array(&(argv[1]),cmdline);
82 argc = parse_cmdline_to_arg_array(argv,cmdline);
84 strcpy(execname,argv[0]);
86 /* at this point, execpath is defined, as are argv[] and execname */
88 j = sprintf(buffer,"\nexecname : %s\nexecpath : %s\n",execname,execpath);
89 for (i = 0; i < argc; i++)
90 j += sprintf(buffer+j,"argv[%d]\t: %s\n",i,argv[i]);
95 error("you must supply a program name to run");
97 #if defined(__CYGWIN__)
98 /* this insures that we search for symlinks before .exe's */
99 if (compact_invocation)
103 process_execname(exec,execname,execpath);
104 Trace(("exec\t%s\nexecname\t%s\nexecpath\t%s\n",
105 exec,execname,execpath));
107 wait_for_child = build_cmdline(cmdline2,exec,argc,argv);
110 xemacs_special(exec);
111 ret_code = start_child(cmdline2,wait_for_child);
112 if (compact_invocation)
113 for (i = 1; i < argc; i++) // argv[0] was not malloc'ed
116 for (i = 0; i < argc; i++)
118 return (int) ret_code;
120 int start_child(char* cmdline, int wait_for_child)
123 SECURITY_ATTRIBUTES sec_attrs;
124 PROCESS_INFORMATION child;
127 memset (&start, 0, sizeof (start));
128 start.cb = sizeof (start);
129 start.dwFlags = STARTF_USESHOWWINDOW;
130 start.wShowWindow = SW_HIDE;
132 sec_attrs.nLength = sizeof (sec_attrs);
133 sec_attrs.lpSecurityDescriptor = NULL;
134 sec_attrs.bInheritHandle = FALSE;
136 if (CreateProcess (NULL, cmdline, &sec_attrs, NULL, TRUE, 0,
137 NULL, NULL, &start, &child))
141 WaitForSingleObject (child.hProcess, INFINITE);
142 GetExitCodeProcess (child.hProcess, &retval);
144 CloseHandle (child.hThread);
145 CloseHandle (child.hProcess);
148 error("could not start %s",cmdline);
151 void xemacs_special(char* exec)
154 * if we're trying to run xemacs, AND this file was in %emacs_dir%\bin,
155 * then set emacs_dir environment variable
159 char exec2[MAX_PATH + FILENAME_MAX + 100];
160 char tmp[MAX_PATH + FILENAME_MAX + 100];
162 /* this depends on short-circuit evaluation */
163 if ( ((p = strrchr(exec2,'\\')) && stricmp(p,"\\xemacs") == 0) ||
164 ((p = strrchr(exec2,'/')) && stricmp(p,"/xemacs") == 0) ||
165 ((p = strrchr(exec2,'\\')) && stricmp(p,"\\xemacs.exe") == 0) ||
166 ((p = strrchr(exec2,'/')) && stricmp(p,"/xemacs.exe") == 0) )
168 if ( ((p2 = strrchr(p, '\\')) && stricmp(p2, "\\bin") == 0) ||
169 ((p2 = strrchr(p, '/')) && stricmp(p2, "/bin") == 0) )
172 #if defined(__CYGWIN__)
173 CYGWIN_CONV_TO_POSIX_PATH((exec2,tmp));
175 #else /* NATIVE xemacs DOS-style paths with forward slashes */
176 for (p = exec2; *p; p++)
177 if (*p == '\\') *p = '/';
179 SetEnvironmentVariable ("emacs_dir", exec2);
183 int build_cmdline(char* new_cmdline, char* exec, int argc, char* argv[])
190 * look for "-wait" as first true argument; we'll apply that ourselves
192 if ((argc >= 2) && (stricmp(argv[1],"-wait") == 0))
198 char_cnt = strlen(exec);
199 for (i = first_arg; i < argc; i++)
200 char_cnt += strlen(argv[i]);
201 if (char_cnt > MAX_ARGS*MAX_PATH) /* then we ran out of room */
202 error("command line too long -\n%s",new_cmdline);
204 strcpy(new_cmdline,exec);
205 for (i = first_arg; i < argc; i++)
207 strcat(new_cmdline," ");
208 strcat(new_cmdline,argv[i]);
212 /* process exec_arg : if it
214 * 1) starts with '\\' or '/', it's a root-path and leave it alone
215 * 2) starts with 'x:\\' or 'x:/', it's a root-path and leave it alone
216 * 3) starts with '.\\' or './', two possible meanings:
217 * 1) exec is in the current directory
218 * 2) exec in same directory as this program
219 * 4) otherwise, search path (and _prepend_ "." to the path!!!)
220 * 5) convert all '/' to '\\'
222 * 1) starts with '\\' or '/', it's a root-path and leave it alone
223 * 2) starts with 'x:\\' or 'x:/', it's a root-path and leave it alone
224 * 3) starts with '.\\' or './', two possible meanings:
225 * 1) exec is in the current directory
226 * 2) exec in same directory as this program
227 * 4) otherwise, search path (and _prepend_ "." to the path!!!)
228 * 5) convert to cygwin-style path to resolve symlinks within the pathspec
229 * 6) check filename: if it's a symlink, resolve it by peeking inside
230 * 7) convert to win32-style path+filename since we're using Windows
231 * createProcess() to launch
233 void process_execname(char *exec, const char* execname,const char* execpath )
237 char exec_tmp[MAX_PATH + FILENAME_MAX + 100];
238 char exec_tmp2[MAX_PATH + FILENAME_MAX + 100];
239 char buf[MAX_PATH + FILENAME_MAX + 100];
246 if ((execname[0] == '\\') || (execname[0] == '/'))
248 #if defined(__CYGWIN__)
249 strcpy(exec_tmp,execname);
251 exec_tmp[0] = ((char) (_getdrive() + ((int) 'A') - 1));
254 strcat(exec_tmp,execname);
256 Trace(("/ -\nexec_tmp\t%s\nexecname\t%s\nexecpath\t%s\n",
257 exec_tmp,execname,execpath));
258 if (! fileExistsMulti(exec_tmp2,NULL,exec_tmp,exts,NUM_EXTENSIONS) )
261 for (i = 0; i < NUM_EXTENSIONS; i++)
262 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
263 error("Couldn't locate %s\nI tried appending the following "
264 "extensions: \n%s",exec_tmp,buf);
269 * STARTS WITH x:\ or x:/
272 else if ((strlen(execname) > 3) && // avoid boundary errors
273 (execname[1] == ':') &&
274 ((execname[2] == '\\') || (execname[2] == '/')))
276 strcpy(exec_tmp,execname);
277 Trace(("x: -\nexec_tmp\t%s\nexecname\t%s\nexecpath\t%s\n",
278 exec_tmp,execname,execpath));
279 if (! fileExistsMulti(exec_tmp2,NULL,exec_tmp,exts,NUM_EXTENSIONS) )
282 for (i = 0; i < NUM_EXTENSIONS; i++)
283 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
284 error("Couldn't locate %s\nI tried appending the following "
285 "extensions: \n%s",exec_tmp,buf);
290 * STARTS WITH ./ or .\
292 else if ((execname[0] == '.') &&
293 ((execname[1] == '\\') || (execname[1] == '/')))
295 if (((char*) getcwd(exec_tmp,MAX_PATH))==NULL)
296 error("can't find current working directory");
297 if (! fileExistsMulti(exec_tmp2,exec_tmp,&(execname[2]),
298 exts,NUM_EXTENSIONS) )
299 if (! fileExistsMulti(exec_tmp2,execpath,&(execname[2]),
300 exts,NUM_EXTENSIONS) )
303 for (i = 0; i < NUM_EXTENSIONS; i++)
304 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
305 error("Couldn't locate %s\n"
306 "I looked in the following directories:\n [1]: %s\n [2]: %s\n"
307 "I also tried appending the following "
308 "extensions: \n%s",execname,exec_tmp,execpath,buf);
313 * OTHERWISE, SEARCH PATH (prepend '.' and run.exe's directory)
314 * can't use fileExistsMulti because we want to search entire path
315 * for exts[0], then for exts[1], etc.
319 orig_pathlist = getenv("PATH");
320 if ((pathlist = malloc (strlen(orig_pathlist)
322 + strlen(execpath)+ 3)) == NULL)
323 error("internal error - out of memory");
324 strcpy(pathlist,".");
325 strcat(pathlist,SEP_CHARS);
326 strcat(pathlist,execpath);
327 strcat(pathlist,SEP_CHARS);
328 strcat(pathlist,orig_pathlist);
331 for (i = 0; i < NUM_EXTENSIONS; i++)
333 strcpy(exec_tmp,execname);
334 strcat(exec_tmp,exts[i]);
335 pfopen(exec_tmp2,exec_tmp,pathlist);
336 if (fileExists(NULL,NULL,exec_tmp2))
340 Trace(("exec_tmp\t%s\npathlist\t%s\n",exec_tmp2,pathlist));
343 if (exec_tmp2[0] == '\0')
346 for (i = 0; i < NUM_EXTENSIONS; i++)
347 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
348 error("Couldn't find %s anywhere.\n"
349 "I even looked in the PATH \n"
350 "I also tried appending the following "
351 "extensions: \n%s",execname,buf);
355 * At this point, we know that exec_tmp2 contains a filename
356 * and we know that exec_tmp2 exists.
358 #if defined(__CYGWIN__)
361 char sym_link_name[MAX_PATH+1];
362 char real_name[MAX_PATH+1];
363 char dummy[MAX_PATH+1];
365 strcpy(exec_tmp,exec_tmp2);
367 CYGWIN_CONV_TO_POSIX_PATH((exec_tmp,sym_link_name));
368 Trace((sym_link_name));
370 if (lstat(sym_link_name, &stbuf) == 0)
372 if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
374 if (readlink(sym_link_name, real_name, sizeof(real_name)) == -1)
375 error("problem reading symbolic link for %s",exec_tmp);
378 // if realname starts with '/' it's a rootpath
379 if (real_name[0] == '/')
380 strcpy(exec_tmp2,real_name);
381 else // otherwise, it's relative to the symlink's location
383 CYGWIN_SPLIT_PATH((sym_link_name,exec_tmp2,dummy));
384 if (!endsWith(exec_tmp2,PATH_SEP_CHAR_STR))
385 strcat(exec_tmp2,PATH_SEP_CHAR_STR);
386 strcat(exec_tmp2,real_name);
390 else /* NOT a symlink */
391 strcpy(exec_tmp2, sym_link_name);
394 error("can't locate executable - %s",sym_link_name);
396 CYGWIN_CONV_TO_FULL_WIN32_PATH((exec_tmp2,exec));
398 strcpy (exec, exec_tmp2);
401 int endsWith(const char* s1, const char* s2)
408 if (len1 - len2 >= 0)
409 if (stricmp(&(s1[len1-len2]),s2) == 0)
412 }void strip_exe(char* s)
414 if ((strlen(s) > 4) && // long enough to have .exe extension
415 // second part not evaluated (short circuit) if exec_arg too short
416 (stricmp(&(s[strlen(s)-4]),".exe") == 0))
417 s[strlen(s)-4] = '\0';
419 void error(char* fmt, ...)
425 j = sprintf(buf, "Error: ");
426 j += vsprintf(buf + j,fmt,args);
427 j += sprintf(buf + j,"\n");
429 MessageBox(NULL, buf, "Run.exe", MB_ICONSTOP);
432 void message(char* fmt, ...)
438 j = vsprintf(buf,fmt,args);
439 j += sprintf(buf + j,"\n");
441 MessageBox(NULL, buf, "Run.exe Message", MB_ICONSTOP);
443 void Trace_(char* fmt, ...)
449 j = vsprintf(buf,fmt,args);
450 j += sprintf(buf + j,"\n");
452 MessageBox(NULL, buf, "Run.exe DEBUG", MB_ICONSTOP);
455 * Uses system info to determine the path used to invoke run
456 * Also attempts to deduce the target execname if "compact_invocation"
459 * returns TRUE if compact_invocation method was used
460 * (and target execname was deduced successfully)
461 * otherwise returns FALSE, and execname == run or run.exe
463 int get_exec_name_and_path(char* execname, char* execpath)
465 char modname[MAX_PATH];
470 if (!GetModuleFileName (NULL, modname, MAX_PATH))
471 error("internal error - can't find my own name");
472 if ((p = strrchr (modname, '\\')) == NULL)
473 error("internal error - my own name has no path\n%s",modname);
474 tmp_execname = p + 1;
476 // if invoked by a name like "runxemacs" then strip off
477 // the "run" and let "xemacs" be execname.
478 // To check for this, make that:
479 // 1) first three chars are "run"
480 // 2) but the string doesn't end there, or start ".exe"
481 // Also, set "compact_invocation" TRUE
482 if ( ((tmp_execname[0] == 'r') || (tmp_execname[0] == 'R')) &&
483 ((tmp_execname[1] == 'u') || (tmp_execname[1] == 'U')) &&
484 ((tmp_execname[2] == 'n') || (tmp_execname[2] == 'N')) &&
485 ((tmp_execname[3] != '.') && (tmp_execname[3] != '\0')) )
493 if (tmp_execname == NULL)
496 strcpy(execname,tmp_execname);
497 #if defined(__CYGWIN__)
498 CYGWIN_CONV_TO_POSIX_PATH((modname,execpath));
500 strcpy(execpath,modname);
505 * works like strtok, but:
506 * double quotes (") suspends tokenizing until closing " reached
508 * additionally, backslash escapes next character, even if that
509 * next character is a delimiter. Or a double quote.
510 * WARNING: this means that backslash may NOT be a delimiter
512 char* my_strtok(char* s, const char* delim, char** lasts)
518 if ((s == NULL) && ((s = *lasts) == NULL))
520 /* Skip leading delimiters */
523 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
527 if (c == 0) { /* no non-delimiter characters */
533 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
534 * Note that delim must have one NUL; we stop if we see that, too.
535 * If we see a double quote, continue until next double quote, then
536 * start scanning for delimiters again.
537 * CYGWIN ONLY: if we see a backslash, just copy next character -
538 * don't consider it as a delimiter even if it is in delim string.
541 /* if this c is ", then scan until we find next " */
543 while ((c = *s++) != '\"')
544 if (c == 0) /* oops, forgot to close the ", clean up & return */
550 #if defined(__CYGWIN__)
553 c = *s++; /* skip the backslash */
554 if (c == 0) /* if escaped character is end-of-string, clean up & return */
560 c = *s++; /* otherwise, skip the escaped character */
563 spanp = (char *)delim;
565 if ((sc = *spanp++) == c) {
578 int parse_cmdline_to_arg_array(char* argv[MAX_ARGS], char* cmdline)
580 char seps[] = " \t\n";
585 token = my_strtok(cmdline, seps, &lasts);
586 while ((token != NULL) && (argc < MAX_ARGS))
588 if ((argv[argc] = malloc(strlen(token)+1)) == NULL)
590 error("internal error - out of memory");
592 strcpy(argv[argc++],token);
593 token = my_strtok(NULL,seps,&lasts);
595 if (argc >= MAX_ARGS)
596 error("too many arguments on commandline\n%s",cmdline);
599 /* Taken from pfopen.c by David Engel (5-Jul-97).
600 * Original comments appear below. Superseded by next comment block.
602 * Written and released to the public domain by David Engel.
604 * This function attempts to open a file which may be in any of
605 * several directories. It is particularly useful for opening
606 * configuration files. For example, PROG.EXE can easily open
607 * PROG.CFG (which is kept in the same directory) by executing:
609 * cfg_file = pfopen("PROG.CFG", "r", getenv("PATH"));
611 * NULL is returned if the file can't be opened.
615 * This function attempts to locate a file which may be in any of
616 * several directories. Unlike the original pfopen, it does not
617 * return a FILE pointer to the opened file, but rather returns
618 * the fully-qualified filename of the first match found. Returns
619 * empty string if not found.
621 char *pfopen(char *retval, const char *name, const char *dirs)
625 char returnval[MAX_PATH + FILENAME_MAX + 100];
630 if (dirs == NULL || dirs[0] == '\0')
633 if ((tdirs = malloc(strlen(dirs)+1)) == NULL)
638 for (ptr = strtok(tdirs, SEP_CHARS); (foundit == FALSE) && ptr != NULL;
639 ptr = strtok(NULL, SEP_CHARS))
641 foundit = fileExists(returnval,ptr,name);
648 strcpy(retval,returnval);
651 int fileExistsMulti(char* fullname, const char* path,
652 const char* name_noext, const char* exts[],
655 char tryName[MAX_PATH + FILENAME_MAX];
659 for (i = 0; i < extcnt; i++)
661 strcpy(tryName,name_noext);
662 strcat(tryName,exts[i]);
663 if (fileExists(fullname, path, tryName) == TRUE)
672 int fileExists(char* fullname, const char* path, const char* name)
677 char work[FILENAME_MAX];
678 char work2[MAX_PATH + FILENAME_MAX + 100];
683 if (len && work[len-1] != '/' && work[len-1] != '\\')
684 strcat(work, PATH_SEP_CHAR_STR);
690 #if defined(__CYGWIN__)
691 CYGWIN_CONV_TO_POSIX_PATH((work, work2));
697 Trace(("looking for...\t%s\n",work2));
700 file = fopen(work2, "rb");
703 if (fullname != NULL)
704 strcpy(fullname,work2);