VirtualBox

source: kBuild/trunk/src/lib/startuphacks-win.c@ 1106

Last change on this file since 1106 was 1106, checked in by bird, 17 years ago

fixed sed bustage after converting to msvc*.dll.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1/* $Id: startuphacks-win.c 1106 2007-09-23 04:20:22Z bird $ */
2/** @file
3 *
4 * kBuild - Alternative argument parser for the windows startup code.
5 *
6 * Copyright (c) 2006-2007 knut st. osmundsen <[email protected]>
7 *
8 * parse_args(): Copyright (c) 1992-1998 by Eberhard Mattes
9 *
10 *
11 * This file is part of kBuild.
12 *
13 * kBuild is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * kBuild is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with kBuild; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
29
30/*******************************************************************************
31* Header Files *
32*******************************************************************************/
33#include <stdlib.h>
34#include <malloc.h>
35#include <Windows.h>
36
37
38/*******************************************************************************
39* Internal Functions *
40*******************************************************************************/
41static int parse_args(const char *pszSrc, char **argv, char *pchPool);
42
43
44/*******************************************************************************
45* Global Variables *
46*******************************************************************************/
47/** argument count found by parse_args(). */
48static int g_cArgs = 0;
49/** the argument vector, for __getmainargs(). */
50static char **g_papszArgs = NULL;
51
52
53
54int __cdecl _setargv(void)
55{
56 static char s_szProgramName[MAX_PATH + 1];
57 const char *pszCmdLine;
58 char *pszCmdLineBuf;
59 int cb;
60
61 /*
62 * Set the program name.
63 */
64 GetModuleFileName(NULL, s_szProgramName, MAX_PATH);
65 s_szProgramName[MAX_PATH] = '\0';
66#if _MSC_VER >= 1400 && !defined(CRTDLL) && !defined(_DLL)
67 _set_pgmptr(s_szProgramName);
68#endif
69
70 /*
71 * Get the commandline, use the program name if nothings available.
72 */
73 pszCmdLine = (const char *)GetCommandLineA();
74 if (!pszCmdLine || !*pszCmdLine)
75 pszCmdLine = s_szProgramName;
76
77 /*
78 * Parse the argument commandline emitting the unix argument vector.
79 */
80 cb = parse_args(pszCmdLine, NULL, NULL);
81 g_papszArgs = malloc(sizeof(*g_papszArgs) * (g_cArgs + 2));
82 if (!g_papszArgs)
83 return -1;
84 pszCmdLineBuf = malloc(cb);
85 if (!pszCmdLineBuf)
86 return -1;
87 parse_args(pszCmdLine, g_papszArgs, pszCmdLineBuf);
88 g_papszArgs[g_cArgs] = g_papszArgs[g_cArgs + 1] = NULL;
89
90 /* set return variables */
91 __argc = g_cArgs;
92 __argv = g_papszArgs;
93 return 0;
94}
95
96
97/* when linking with the crtexe.c, the __getmainargs() call will redo the _setargv job inside the msvc*.dll. */
98int __cdecl __getmainargs(int *pargc, char ***pargv, char ***penvp, int dowildcard, /*_startupinfo*/ void *startinfo)
99{
100 __argc = *pargc = g_cArgs;
101 __argv = *pargv = g_papszArgs;
102 *penvp = _environ;
103 return 0;
104}
105
106#if defined(_M_IX86)
107int (__cdecl * _imp____getmainargs)(int *, char ***, char ***, int, /*_startupinfo*/ void *) = __getmainargs;
108#else
109int (__cdecl * __imp___getmainargs)(int *, char ***, char ***, int, /*_startupinfo*/ void *) = __getmainargs;
110#endif
111
112
113
114/**
115 * Parses the argument string passed in as pszSrc.
116 *
117 * @returns size of the processed arguments.
118 * @param pszSrc Pointer to the commandline that's to be parsed.
119 * @param argv Pointer to argument vector to put argument pointers in. NULL allowed.
120 * @param pchPool Pointer to memory pchPool to put the arguments into. NULL allowed.
121 */
122static int parse_args(const char *pszSrc, char **argv, char *pchPool)
123{
124 int bs;
125 char chQuote;
126 char *pfFlags;
127 int cbArgs;
128
129#define PUTC(c) do { ++cbArgs; if (pchPool != NULL) *pchPool++ = (c); } while (0)
130#define PUTV do { ++g_cArgs; if (argv != NULL) *argv++ = pchPool; } while (0)
131#define WHITE(c) ((c) == ' ' || (c) == '\t')
132
133#define _ARG_DQUOTE 0x01 /* Argument quoted (") */
134#define _ARG_RESPONSE 0x02 /* Argument read from response file */
135#define _ARG_WILDCARD 0x04 /* Argument expanded from wildcard */
136#define _ARG_ENV 0x08 /* Argument from environment */
137#define _ARG_NONZERO 0x80 /* Always set, to avoid end of string */
138
139 g_cArgs = 0; cbArgs = 0;
140
141#if 0
142 /* argv[0] */
143 PUTC((char)_ARG_NONZERO);
144 PUTV;
145 for (;;)
146 {
147 PUTC(*pszSrc);
148 if (*pszSrc == 0)
149 break;
150 ++pszSrc;
151 }
152 ++pszSrc;
153#endif
154
155 for (;;)
156 {
157 while (WHITE(*pszSrc))
158 ++pszSrc;
159 if (*pszSrc == 0)
160 break;
161 pfFlags = pchPool;
162 PUTC((char)_ARG_NONZERO);
163 PUTV;
164 bs = 0; chQuote = 0;
165 for (;;)
166 {
167 if (!chQuote ? (*pszSrc == '"' || *pszSrc == '\'') : *pszSrc == chQuote)
168 {
169 while (bs >= 2)
170 {
171 PUTC('\\');
172 bs -= 2;
173 }
174 if (bs & 1)
175 PUTC(*pszSrc);
176 else
177 {
178 chQuote = chQuote ? 0 : *pszSrc;
179 if (pfFlags != NULL)
180 *pfFlags |= _ARG_DQUOTE;
181 }
182 bs = 0;
183 }
184 else if (*pszSrc == '\\')
185 ++bs;
186 else
187 {
188 while (bs != 0)
189 {
190 PUTC('\\');
191 --bs;
192 }
193 if (*pszSrc == 0 || (WHITE(*pszSrc) && !chQuote))
194 break;
195 PUTC(*pszSrc);
196 }
197 ++pszSrc;
198 }
199 PUTC(0);
200 }
201 return cbArgs;
202}
203
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette