1 /*
2 * runtool.c --
3 *
4 * Code for generating toolname.exe (in win32) which calls:
5 * mash.exe -name argv[0] -- args
6 * since we get the toolname from argv[0], we can reuse this code for all
7 * tools.
8 *
9 * Copyright (c) 1997-2002 The Regents of the University of California.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * A. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * B. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * C. Neither the names of the copyright holders nor the names of its
21 * contributors may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
25 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char *rcsid = "$Header: /usr/mash/src/repository/mash/mash-1/compat/runtool.c,v 1.9 2002/02/03 03:14:22 lim Exp $";
39 #endif
40
41 #ifndef WIN32
42 #error "this program is meant for win32 only"
43 #endif
44
45 #ifndef RUNTOOL_C
46 #define RUNTOOL_C
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <process.h>
51 #include <windows.h>
52 #include <errno.h>
53
54 #ifdef IS_SETUP
55 #define MASH_EXE_SUBDIR "bin\\"
56 #define SCRIPT_SUBDIR "lib\\"
57 #endif
58
59 int main(int argc, char** argv)
60 {
61 const char*szMASHEXE="mash.exe";
62 char mashpath[_MAX_FNAME];
63 char mashpathQuoted[_MAX_FNAME];
64 char **newargv=(char**)malloc(sizeof(char*)*(argc+5));
65 char toolname[_MAX_FNAME];
66 char toolpath[_MAX_FNAME];
67 char tooldir[_MAX_DIR];
68 char tooldrive[_MAX_DRIVE];
69 char *name;
70 int i, retcode, errnum;
71 char *cmdLine;
72 /* get basename */
73 _splitpath(argv[0], NULL, toolpath, toolname, NULL);
74
75 /* NOTE: we have to assume we use only one case for the toolname
76 * since in win95 argv[0] becomes all UPPERCASE. For backward
77 * compatibility, we use all lower case
78 */
79 for (i=0; toolname[i]!='\0'; i++)
80 toolname[i] = tolower(toolname[i]);
81
82 /* FIXME: quick hack to make mega<toolname> work, basically
83 if the name of the executable is mega<name> then the "name"
84 of the program is <name> but the scriptname is still mega<name> */
85 if (strncmp(toolname, "mega", 4)==0) {
86 name=&toolname[4];
87 } else {
88 name=toolname;
89 }
90
91 /* get everything but the first argument */
92 cmdLine = GetCommandLine();
93 if (cmdLine) {
94 /* if quoted go for the end of the quote, then go for
95 * the first space */
96 if (cmdLine[0] == '\"') {
97 cmdLine = strchr(cmdLine+1, '\"');
98 }
99 cmdLine = strchr(cmdLine, ' ');
100 }
101 if (cmdLine == NULL) {
102 cmdLine = "";
103 }
104
105 /* We assume that mash.exe and the script are in the same directory
106 * as <toolname>.exe,
107 * (unless MASH_EXE_DIR or SCRIPT_SUBDIR is defined)
108 * so set current directory to that of
109 * <toolname>.exe so that mash.exe can find the script. */
110 if (0==GetModuleFileName(NULL, toolpath, _MAX_FNAME)) {
111 perror("GetModuleFileName failed!");
112 }
113 _splitpath(toolpath, tooldrive, tooldir, NULL, NULL);
114 strcpy(toolpath, "\"");
115 strcat(toolpath, tooldrive);
116 strcat(toolpath, tooldir);
117 #ifdef SCRIPT_SUBDIR
118 strcat(toolpath, SCRIPT_SUBDIR);
119 #endif
120 strcat(toolpath, toolname);
121 strcat(toolpath, "\"");
122
123 strcpy(mashpath, tooldrive);
124 strcat(mashpath, tooldir);
125 #ifdef MASH_EXE_SUBDIR
126 strcat(mashpath, MASH_EXE_SUBDIR);
127 #endif
128 strcat(mashpath, szMASHEXE);
129
130 strcpy(mashpathQuoted, "\"");
131 strcat(mashpathQuoted, mashpath);
132 strcat(mashpathQuoted, "\"");
133
134 #if !defined(NDEBUG)
135 printf("executing: %s {%s %s -name %s -- %s}\n", mashpath,
136 mashpathQuoted, toolpath, name, cmdLine);
137 #endif
138 retcode=_execl(mashpath, mashpathQuoted, toolpath, "-name", name,
139 "--", cmdLine, NULL);
140 /* should not arrive here */
141 if (retcode==-1) {
142 errnum = errno;
143 if (errnum==ENOENT) {
144 fprintf(stderr, "error: cannot find %s\n", mashpath);
145 } else {
146 fprintf(stderr, "error: %s\n", strerror(errnum));
147 }
148 }
149 return retcode;
150 }
151
152 #endif /* #ifdef RUNTOOL_C */
153
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.