annotate lib-src/run.c @ 432:3a7e78e1142d r21-2-24

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