annotate lib-src/run.c @ 391:e50d8e68d7a5

Added tag r21-2-10 for changeset c6012109f545
author cvs
date Mon, 13 Aug 2007 11:10:04 +0200
parents aabb7f5b1c81
children 74fd4e045ea6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
388
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
1 /* run -- Wrapper program for console mode programs under Windows(TM)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
2 * Copyright (C) 1998 Charles S. Wilson
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
3 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
5 * modify it under the terms of the GNU General Public License
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
6 * as published by the Free Software Foundation; either version 2
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
7 * of the License, or (at your option) any later version.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
8 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
9 * This program is distributed in the hope that it will be useful,
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
12 * GNU General Public License for more details.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
13 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
15 * along with this program; if not, write to the Free Software
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
17 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
18
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
19 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
20 * This program is based on the runemacs.c distributed with XEmacs 21.0
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
21 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
22 * Simple program to start gnu-win32 X11 programs (and native XEmacs)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
23 * with its console window hidden.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
24 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
25 * This program is provided purely for convenience, since most users will
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
26 * use XEmacs in windowing (GUI) mode, and will not want to have an extra
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
27 * console window lying around. Ditto for desktop shortcuts to gnu-win32
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
28 * X11 executables.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
29 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
30
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
31
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
32 #define WIN32
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
33
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
34 #include <windows.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
35 #include <string.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
36 #include <malloc.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
37 #include <stdlib.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
38 #include <stdio.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
39 #include <stdarg.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
40
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
41 #include "run.h"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
42
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
43 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
44 #include <sys/types.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
45 #include <sys/stat.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
46 #include <sys/cygwin.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
47 #include <sys/unistd.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
48 WinMainCRTStartup() { mainCRTStartup(); }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
49 #else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
50 #include <direct.h>
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
51 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
52
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
53
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
54 char buffer[1024];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
55
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
56 int WINAPI
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
57 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
58 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
59 int wait_for_child = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
60 int compact_invocation = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
61 DWORD ret_code = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
62
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
63
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
64 char execname[FILENAME_MAX];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
65 char execpath[MAX_PATH];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
66 char* argv[MAX_ARGS+1]; /* leave extra slot for compact_invocation argv[0] */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
67 int argc;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
68 int i,j;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
69 char exec[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
70 char cmdline2[MAX_ARGS * MAX_PATH];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
71
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
72 compact_invocation = get_exec_name_and_path(execname,execpath);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
73
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
74 if (compact_invocation)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
75 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
76 argv[0] = execname;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
77 argc = parse_cmdline_to_arg_array(&(argv[1]),cmdline);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
78 argc++;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
79 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
80 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
81 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
82 argc = parse_cmdline_to_arg_array(argv,cmdline);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
83 if (argc >= 1)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
84 strcpy(execname,argv[0]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
85 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
86 /* at this point, execpath is defined, as are argv[] and execname */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
87 #ifdef DEBUG
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
88 j = sprintf(buffer,"\nexecname : %s\nexecpath : %s\n",execname,execpath);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
89 for (i = 0; i < argc; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
90 j += sprintf(buffer+j,"argv[%d]\t: %s\n",i,argv[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
91 Trace((buffer));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
92 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
93
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
94 if (execname == NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
95 error("you must supply a program name to run");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
96
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
97 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
98 /* this insures that we search for symlinks before .exe's */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
99 if (compact_invocation)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
100 strip_exe(execname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
101 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
102
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
103 process_execname(exec,execname,execpath);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
104 Trace(("exec\t%s\nexecname\t%s\nexecpath\t%s\n",
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
105 exec,execname,execpath));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
106
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
107 wait_for_child = build_cmdline(cmdline2,exec,argc,argv);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
108 Trace((cmdline2));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
109
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
110 xemacs_special(exec);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
111 ret_code = start_child(cmdline2,wait_for_child);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
112 if (compact_invocation)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
113 for (i = 1; i < argc; i++) // argv[0] was not malloc'ed
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
114 free(argv[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
115 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
116 for (i = 0; i < argc; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
117 free(argv[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
118 return (int) ret_code;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
119 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
120 int start_child(char* cmdline, int wait_for_child)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
121 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
122 STARTUPINFO start;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
123 SECURITY_ATTRIBUTES sec_attrs;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
124 SECURITY_DESCRIPTOR sec_desc;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
125 PROCESS_INFORMATION child;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
126 int retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
127
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
128 memset (&start, 0, sizeof (start));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
129 start.cb = sizeof (start);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
130 start.dwFlags = STARTF_USESHOWWINDOW;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
131 start.wShowWindow = SW_HIDE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
132
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
133 sec_attrs.nLength = sizeof (sec_attrs);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
134 sec_attrs.lpSecurityDescriptor = NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
135 sec_attrs.bInheritHandle = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
136
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
137 if (CreateProcess (NULL, cmdline, &sec_attrs, NULL, TRUE, 0,
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
138 NULL, NULL, &start, &child))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
139 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
140 if (wait_for_child)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
141 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
142 WaitForSingleObject (child.hProcess, INFINITE);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
143 GetExitCodeProcess (child.hProcess, &retval);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
144 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
145 CloseHandle (child.hThread);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
146 CloseHandle (child.hProcess);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
147 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
148 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
149 error("could not start %s",cmdline);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
150 return retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
151 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
152 void xemacs_special(char* exec)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
153 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
154 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
155 * if we're trying to run xemacs, AND this file was in %emacs_dir%\bin,
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
156 * then set emacs_dir environment variable
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
157 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
158 char* p;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
159 char* p2;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
160 char exec2[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
161 char tmp[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
162 strcpy(exec2,exec);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
163 /* this depends on short-circuit evaluation */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
164 if ( ((p = strrchr(exec2,'\\')) && stricmp(p,"\\xemacs") == 0) ||
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
165 ((p = strrchr(exec2,'/')) && stricmp(p,"/xemacs") == 0) ||
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
166 ((p = strrchr(exec2,'\\')) && stricmp(p,"\\xemacs.exe") == 0) ||
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
167 ((p = strrchr(exec2,'/')) && stricmp(p,"/xemacs.exe") == 0) )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
168 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
169 if ( ((p2 = strrchr(p, '\\')) && stricmp(p2, "\\bin") == 0) ||
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
170 ((p2 = strrchr(p, '/')) && stricmp(p2, "/bin") == 0) )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
171 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
172 *p2 = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
173 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
174 CYGWIN_CONV_TO_POSIX_PATH((exec2,tmp));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
175 strcpy(exec2,tmp);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
176 #else /* NATIVE xemacs DOS-style paths with forward slashes */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
177 for (p = exec2; *p; p++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
178 if (*p == '\\') *p = '/';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
179 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
180 SetEnvironmentVariable ("emacs_dir", exec2);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
181 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
182 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
183 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
184 int build_cmdline(char* new_cmdline, char* exec, int argc, char* argv[])
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
185 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
186 int retval = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
187 int first_arg = 1;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
188 int i;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
189 int char_cnt = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
190 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
191 * look for "-wait" as first true argument; we'll apply that ourselves
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
192 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
193 if ((argc >= 2) && (stricmp(argv[1],"-wait") == 0))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
194 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
195 retval = TRUE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
196 first_arg++;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
197 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
198
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
199 char_cnt = strlen(exec);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
200 for (i = first_arg; i < argc; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
201 char_cnt += strlen(argv[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
202 if (char_cnt > MAX_ARGS*MAX_PATH) /* then we ran out of room */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
203 error("command line too long -\n%s",new_cmdline);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
204
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
205 strcpy(new_cmdline,exec);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
206 for (i = first_arg; i < argc; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
207 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
208 strcat(new_cmdline," ");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
209 strcat(new_cmdline,argv[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
210 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
211 return retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
212 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
213 /* process exec_arg : if it
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
214 * NATIVE:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
215 * 1) starts with '\\' or '/', it's a root-path and leave it alone
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
216 * 2) starts with 'x:\\' or 'x:/', it's a root-path and leave it alone
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
217 * 3) starts with '.\\' or './', two possible meanings:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
218 * 1) exec is in the current directory
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
219 * 2) exec in same directory as this program
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
220 * 4) otherwise, search path (and _prepend_ "." to the path!!!)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
221 * 5) convert all '/' to '\\'
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
222 * CYGWIN
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
223 * 1) starts with '\\' or '/', it's a root-path and leave it alone
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
224 * 2) starts with 'x:\\' or 'x:/', it's a root-path and leave it alone
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
225 * 3) starts with '.\\' or './', two possible meanings:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
226 * 1) exec is in the current directory
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
227 * 2) exec in same directory as this program
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
228 * 4) otherwise, search path (and _prepend_ "." to the path!!!)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
229 * 5) convert to cygwin-style path to resolve symlinks within the pathspec
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
230 * 6) check filename: if it's a symlink, resolve it by peeking inside
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
231 * 7) convert to win32-style path+filename since we're using Windows
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
232 * createProcess() to launch
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
233 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
234 void process_execname(char *exec, const char* execname,const char* execpath )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
235 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
236 char* orig_pathlist;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
237 char* pathlist;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
238 char exec_tmp[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
239 char exec_tmp2[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
240 char buf[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
241 int i,j;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
242
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
243 int len = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
244 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
245 * STARTS WITH / or \
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
246 * execpath NOT used
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
247 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
248 if ((execname[0] == '\\') || (execname[0] == '/'))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
249 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
250 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
251 strcpy(exec_tmp,execname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
252 #else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
253 exec_tmp[0] = ((char) (_getdrive() + ((int) 'A') - 1));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
254 exec_tmp[1] = ':';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
255 exec_tmp[2] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
256 strcat(exec_tmp,execname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
257 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
258 Trace(("/ -\nexec_tmp\t%s\nexecname\t%s\nexecpath\t%s\n",
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
259 exec_tmp,execname,execpath));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
260 if (! fileExistsMulti(exec_tmp2,NULL,exec_tmp,exts,NUM_EXTENSIONS) )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
261 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
262 j = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
263 for (i = 0; i < NUM_EXTENSIONS; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
264 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
265 error("Couldn't locate %s\nI tried appending the following "
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
266 "extensions: \n%s",exec_tmp,buf);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
267 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
268 Trace((exec_tmp2));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
269 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
270 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
271 * STARTS WITH x:\ or x:/
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
272 * execpath NOT used
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
273 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
274 else if ((strlen(execname) > 3) && // avoid boundary errors
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
275 (execname[1] == ':') &&
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
276 ((execname[2] == '\\') || (execname[2] == '/')))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
277 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
278 strcpy(exec_tmp,execname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
279 Trace(("x: -\nexec_tmp\t%s\nexecname\t%s\nexecpath\t%s\n",
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
280 exec_tmp,execname,execpath));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
281 if (! fileExistsMulti(exec_tmp2,NULL,exec_tmp,exts,NUM_EXTENSIONS) )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
282 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
283 j = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
284 for (i = 0; i < NUM_EXTENSIONS; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
285 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
286 error("Couldn't locate %s\nI tried appending the following "
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
287 "extensions: \n%s",exec_tmp,buf);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
288 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
289 Trace((exec_tmp2));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
290 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
291 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
292 * STARTS WITH ./ or .\
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
293 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
294 else if ((execname[0] == '.') &&
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
295 ((execname[1] == '\\') || (execname[1] == '/')))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
296 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
297 if (((char*) getcwd(exec_tmp,MAX_PATH))==NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
298 error("can't find current working directory");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
299 if (! fileExistsMulti(exec_tmp2,exec_tmp,&(execname[2]),
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
300 exts,NUM_EXTENSIONS) )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
301 if (! fileExistsMulti(exec_tmp2,execpath,&(execname[2]),
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
302 exts,NUM_EXTENSIONS) )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
303 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
304 j = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
305 for (i = 0; i < NUM_EXTENSIONS; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
306 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
307 error("Couldn't locate %s\n"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
308 "I looked in the following directories:\n [1]: %s\n [2]: %s\n"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
309 "I also tried appending the following "
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
310 "extensions: \n%s",execname,exec_tmp,execpath,buf);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
311 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
312 Trace((exec_tmp2));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
313 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
314 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
315 * OTHERWISE, SEARCH PATH (prepend '.' and run.exe's directory)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
316 * can't use fileExistsMulti because we want to search entire path
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
317 * for exts[0], then for exts[1], etc.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
318 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
319 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
320 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
321 orig_pathlist = getenv("PATH");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
322 if ((pathlist = malloc (strlen(orig_pathlist)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
323 + strlen(".")
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
324 + strlen(execpath)+ 3)) == NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
325 error("internal error - out of memory");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
326 strcpy(pathlist,".");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
327 strcat(pathlist,SEP_CHARS);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
328 strcat(pathlist,execpath);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
329 strcat(pathlist,SEP_CHARS);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
330 strcat(pathlist,orig_pathlist);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
331
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
332 Trace((pathlist));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
333 for (i = 0; i < NUM_EXTENSIONS; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
334 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
335 strcpy(exec_tmp,execname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
336 strcat(exec_tmp,exts[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
337 pfopen(exec_tmp2,exec_tmp,pathlist);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
338 if (fileExists(NULL,NULL,exec_tmp2))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
339 break;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
340 exec_tmp2[0] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
341 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
342 Trace(("exec_tmp\t%s\npathlist\t%s\n",exec_tmp2,pathlist));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
343
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
344 free(pathlist);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
345 if (exec_tmp2[0] == '\0')
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
346 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
347 j = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
348 for (i = 0; i < NUM_EXTENSIONS; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
349 j += sprintf(buf + j," [%d]: %s\n",i+1,exts[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
350 error("Couldn't find %s anywhere.\n"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
351 "I even looked in the PATH \n"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
352 "I also tried appending the following "
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
353 "extensions: \n%s",execname,buf);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
354 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
355 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
356 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
357 * At this point, we know that exec_tmp2 contains a filename
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
358 * and we know that exec_tmp2 exists.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
359 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
360 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
361 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
362 struct stat stbuf;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
363 char sym_link_name[MAX_PATH+1];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
364 char real_name[MAX_PATH+1];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
365 char dummy[MAX_PATH+1];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
366
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
367 strcpy(exec_tmp,exec_tmp2);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
368
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
369 CYGWIN_CONV_TO_POSIX_PATH((exec_tmp,sym_link_name));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
370 Trace((sym_link_name));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
371
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
372 if (lstat(sym_link_name, &stbuf) == 0)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
373 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
374 if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
375 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
376 if (readlink(sym_link_name, real_name, sizeof(real_name)) == -1)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
377 error("problem reading symbolic link for %s",exec_tmp);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
378 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
379 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
380 // if realname starts with '/' it's a rootpath
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
381 if (real_name[0] == '/')
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
382 strcpy(exec_tmp2,real_name);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
383 else // otherwise, it's relative to the symlink's location
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
384 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
385 CYGWIN_SPLIT_PATH((sym_link_name,exec_tmp2,dummy));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
386 if (!endsWith(exec_tmp2,PATH_SEP_CHAR_STR))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
387 strcat(exec_tmp2,PATH_SEP_CHAR_STR);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
388 strcat(exec_tmp2,real_name);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
389 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
390 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
391 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
392 else /* NOT a symlink */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
393 strcpy(exec_tmp2, sym_link_name);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
394 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
395 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
396 error("can't locate executable - %s",sym_link_name);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
397 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
398 CYGWIN_CONV_TO_FULL_WIN32_PATH((exec_tmp2,exec));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
399 #else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
400 strcpy (exec, exec_tmp2);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
401 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
402 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
403 int endsWith(const char* s1, const char* s2)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
404 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
405 int len1;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
406 int len2;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
407 int retval = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
408 len1 = strlen(s1);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
409 len2 = strlen(s2);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
410 if (len1 - len2 >= 0)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
411 if (stricmp(&(s1[len1-len2]),s2) == 0)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
412 retval = TRUE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
413 return retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
414 }void strip_exe(char* s)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
415 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
416 if ((strlen(s) > 4) && // long enough to have .exe extension
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
417 // second part not evaluated (short circuit) if exec_arg too short
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
418 (stricmp(&(s[strlen(s)-4]),".exe") == 0))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
419 s[strlen(s)-4] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
420 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
421 void error(char* fmt, ...)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
422 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
423 char buf[4096];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
424 int j;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
425 va_list args;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
426 va_start(args, fmt);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
427 j = sprintf(buf, "Error: ");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
428 j += vsprintf(buf + j,fmt,args);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
429 j += sprintf(buf + j,"\n");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
430 va_end(args);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
431 MessageBox(NULL, buf, "Run.exe", MB_ICONSTOP);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
432 exit(1);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
433 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
434 void message(char* fmt, ...)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
435 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
436 char buf[10000];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
437 int j;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
438 va_list args;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
439 va_start(args, fmt);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
440 j = vsprintf(buf,fmt,args);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
441 j += sprintf(buf + j,"\n");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
442 va_end(args);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
443 MessageBox(NULL, buf, "Run.exe Message", MB_ICONSTOP);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
444 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
445 void Trace_(char* fmt, ...)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
446 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
447 char buf[10000];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
448 int j;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
449 va_list args;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
450 va_start(args, fmt);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
451 j = vsprintf(buf,fmt,args);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
452 j += sprintf(buf + j,"\n");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
453 va_end(args);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
454 MessageBox(NULL, buf, "Run.exe DEBUG", MB_ICONSTOP);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
455 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
456 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
457 * Uses system info to determine the path used to invoke run
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
458 * Also attempts to deduce the target execname if "compact_invocation"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
459 * method was used.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
460 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
461 * returns TRUE if compact_invocation method was used
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
462 * (and target execname was deduced successfully)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
463 * otherwise returns FALSE, and execname == run or run.exe
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
464 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
465 int get_exec_name_and_path(char* execname, char* execpath)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
466 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
467 char modname[MAX_PATH];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
468 char* tmp_execname;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
469 char* p;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
470 int retval = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
471
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
472 if (!GetModuleFileName (NULL, modname, MAX_PATH))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
473 error("internal error - can't find my own name");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
474 if ((p = strrchr (modname, '\\')) == NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
475 error("internal error - my own name has no path\n%s",modname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
476 tmp_execname = p + 1;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
477 p[0] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
478 // if invoked by a name like "runxemacs" then strip off
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
479 // the "run" and let "xemacs" be execname.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
480 // To check for this, make that:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
481 // 1) first three chars are "run"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
482 // 2) but the string doesn't end there, or start ".exe"
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
483 // Also, set "compact_invocation" TRUE
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
484 if ( ((tmp_execname[0] == 'r') || (tmp_execname[0] == 'R')) &&
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
485 ((tmp_execname[1] == 'u') || (tmp_execname[1] == 'U')) &&
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
486 ((tmp_execname[2] == 'n') || (tmp_execname[2] == 'N')) &&
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
487 ((tmp_execname[3] != '.') && (tmp_execname[3] != '\0')) )
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
488 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
489 tmp_execname += 3;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
490 retval = TRUE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
491 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
492 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
493 tmp_execname = NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
494
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
495 if (tmp_execname == NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
496 strcpy(execname,"");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
497 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
498 strcpy(execname,tmp_execname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
499 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
500 CYGWIN_CONV_TO_POSIX_PATH((modname,execpath));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
501 #else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
502 strcpy(execpath,modname);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
503 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
504 return retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
505 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
506 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
507 * works like strtok, but:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
508 * double quotes (") suspends tokenizing until closing " reached
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
509 * CYGWIN ONLY:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
510 * additionally, backslash escapes next character, even if that
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
511 * next character is a delimiter. Or a double quote.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
512 * WARNING: this means that backslash may NOT be a delimiter
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
513 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
514 char* my_strtok(char* s, const char* delim, char** lasts)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
515 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
516 char *spanp;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
517 int c, sc;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
518 char *tok;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
519
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
520 if ((s == NULL) && ((s = *lasts) == NULL))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
521 return NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
522 /* Skip leading delimiters */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
523 cont:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
524 c = *s++;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
525 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
526 if (c == sc)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
527 goto cont;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
528 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
529 if (c == 0) { /* no non-delimiter characters */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
530 *lasts = NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
531 return (NULL);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
532 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
533 tok = s - 1;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
534 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
535 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
536 * Note that delim must have one NUL; we stop if we see that, too.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
537 * If we see a double quote, continue until next double quote, then
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
538 * start scanning for delimiters again.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
539 * CYGWIN ONLY: if we see a backslash, just copy next character -
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
540 * don't consider it as a delimiter even if it is in delim string.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
541 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
542 for (;;) {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
543 /* if this c is ", then scan until we find next " */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
544 if (c == '\"')
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
545 while ((c = *s++) != '\"')
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
546 if (c == 0) /* oops, forgot to close the ", clean up & return */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
547 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
548 s = NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
549 *lasts = s;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
550 return (tok);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
551 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
552 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
553 if (c == '\\')
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
554 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
555 c = *s++; /* skip the backslash */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
556 if (c == 0) /* if escaped character is end-of-string, clean up & return */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
557 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
558 s = NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
559 *lasts = s;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
560 return (tok);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
561 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
562 c = *s++; /* otherwise, skip the escaped character */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
563 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
564 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
565 spanp = (char *)delim;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
566 do {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
567 if ((sc = *spanp++) == c) {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
568 if (c == 0)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
569 s = NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
570 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
571 s[-1] = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
572 *lasts = s;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
573 return (tok);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
574 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
575 } while (sc != 0);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
576 c = *s++;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
577 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
578 /* NOTREACHED */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
579 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
580 int parse_cmdline_to_arg_array(char* argv[MAX_ARGS], char* cmdline)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
581 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
582 char seps[] = " \t\n";
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
583 char* token;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
584 int argc = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
585 char* lasts;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
586
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
587 token = my_strtok(cmdline, seps, &lasts);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
588 while ((token != NULL) && (argc < MAX_ARGS))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
589 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
590 if ((argv[argc] = malloc(strlen(token)+1)) == NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
591 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
592 error("internal error - out of memory");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
593 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
594 strcpy(argv[argc++],token);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
595 token = my_strtok(NULL,seps,&lasts);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
596 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
597 if (argc >= MAX_ARGS)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
598 error("too many arguments on commandline\n%s",cmdline);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
599 return argc;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
600 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
601 /* Taken from pfopen.c by David Engel (5-Jul-97).
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
602 * Original comments appear below. Superseded by next comment block.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
603 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
604 * Written and released to the public domain by David Engel.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
605 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
606 * This function attempts to open a file which may be in any of
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
607 * several directories. It is particularly useful for opening
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
608 * configuration files. For example, PROG.EXE can easily open
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
609 * PROG.CFG (which is kept in the same directory) by executing:
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
610 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
611 * cfg_file = pfopen("PROG.CFG", "r", getenv("PATH"));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
612 *
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
613 * NULL is returned if the file can't be opened.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
614 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
615
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
616 /*
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
617 * This function attempts to locate a file which may be in any of
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
618 * several directories. Unlike the original pfopen, it does not
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
619 * return a FILE pointer to the opened file, but rather returns
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
620 * the fully-qualified filename of the first match found. Returns
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
621 * empty string if not found.
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
622 */
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
623 char *pfopen(char *retval, const char *name, const char *dirs)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
624 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
625 char *ptr;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
626 char *tdirs;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
627 char returnval[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
628 char *recursive_name;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
629 int foundit = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
630
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
631 returnval[0] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
632
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
633 if (dirs == NULL || dirs[0] == '\0')
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
634 return NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
635
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
636 if ((tdirs = malloc(strlen(dirs)+1)) == NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
637 return NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
638
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
639 strcpy(tdirs, dirs);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
640
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
641 for (ptr = strtok(tdirs, SEP_CHARS); (foundit == FALSE) && ptr != NULL;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
642 ptr = strtok(NULL, SEP_CHARS))
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
643 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
644 foundit = fileExists(returnval,ptr,name);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
645 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
646
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
647 free(tdirs);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
648 if (!foundit)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
649 retval[0] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
650 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
651 strcpy(retval,returnval);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
652 return retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
653 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
654 int fileExistsMulti(char* fullname, const char* path,
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
655 const char* name_noext, const char* exts[],
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
656 const int extcnt)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
657 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
658 char tryName[MAX_PATH + FILENAME_MAX];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
659 int i = 0;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
660 int retval = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
661 fullname[0] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
662 for (i = 0; i < extcnt; i++)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
663 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
664 strcpy(tryName,name_noext);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
665 strcat(tryName,exts[i]);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
666 if (fileExists(fullname, path, tryName) == TRUE)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
667 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
668 retval = TRUE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
669 break;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
670 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
671 fullname[0] = '\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
672 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
673 return retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
674 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
675 int fileExists(char* fullname, const char* path, const char* name)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
676 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
677 int retval = FALSE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
678 FILE* file;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
679 size_t len;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
680 char work[FILENAME_MAX];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
681 char work2[MAX_PATH + FILENAME_MAX + 100];
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
682 if (path != NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
683 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
684 strcpy(work, path);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
685 len = strlen(work);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
686 if (len && work[len-1] != '/' && work[len-1] != '\\')
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
687 strcat(work, PATH_SEP_CHAR_STR);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
688 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
689 else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
690 work[0]='\0';
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
691
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
692 strcat(work, name);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
693 #if defined(__CYGWIN__)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
694 CYGWIN_CONV_TO_POSIX_PATH((work, work2));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
695 #else
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
696 strcpy(work2,work);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
697 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
698
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
699 #ifdef DEBUGALL
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
700 Trace(("looking for...\t%s\n",work2));
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
701 #endif
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
702
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
703 file = fopen(work2, "rb");
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
704 if (file != NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
705 {
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
706 if (fullname != NULL)
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
707 strcpy(fullname,work2);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
708 retval = TRUE;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
709 fclose(file);
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
710 }
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
711 return retval;
aabb7f5b1c81 Import from CVS: tag r21-2-9
cvs
parents:
diff changeset
712 }