VirtualBox

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

Last change on this file since 1050 was 813, checked in by bird, 18 years ago

Some property fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1/* $Id: startuphacks-win.c 813 2007-01-31 02:56:48Z 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;
49
50
51
52int __cdecl _setargv(void)
53{
54 static char s_szProgramName[MAX_PATH + 1];
55 const char *pszCmdLine;
56 char *pszCmdLineBuf;
57 char **papszArgs = NULL;
58 int cb;
59
60 /*
61 * Set the program name.
62 */
63 GetModuleFileName(NULL, s_szProgramName, MAX_PATH);
64 s_szProgramName[MAX_PATH] = '\0';
65#if _MSC_VER >= 1400
66 _set_pgmptr(s_szProgramName);
67#endif
68
69 /*
70 * Get the commandline, use the program name if nothings available.
71 */
72 pszCmdLine = (const char *)GetCommandLineA();
73 if (!pszCmdLine || !*pszCmdLine)
74 pszCmdLine = s_szProgramName;
75
76 /*
77 * Parse the argument commandline emitting the unix argument vector.
78 */
79 cb = parse_args(pszCmdLine, NULL, NULL);
80 papszArgs = malloc(sizeof(*papszArgs) * (g_cArgs + 2));
81 if (!papszArgs)
82 return -1;
83 pszCmdLineBuf = malloc(cb);
84 if (!pszCmdLineBuf)
85 return -1;
86 parse_args(pszCmdLine, papszArgs, pszCmdLineBuf);
87 papszArgs[g_cArgs] = papszArgs[g_cArgs + 1] = NULL;
88
89 /* set return variables */
90 __argc = g_cArgs;
91 __argv = papszArgs;
92 return 0;
93}
94
95
96/**
97 * Parses the argument string passed in as pszSrc.
98 *
99 * @returns size of the processed arguments.
100 * @param pszSrc Pointer to the commandline that's to be parsed.
101 * @param argv Pointer to argument vector to put argument pointers in. NULL allowed.
102 * @param pchPool Pointer to memory pchPool to put the arguments into. NULL allowed.
103 */
104static int parse_args(const char *pszSrc, char **argv, char *pchPool)
105{
106 int bs;
107 char chQuote;
108 char *pfFlags;
109 int cbArgs;
110
111#define PUTC(c) do { ++cbArgs; if (pchPool != NULL) *pchPool++ = (c); } while (0)
112#define PUTV do { ++g_cArgs; if (argv != NULL) *argv++ = pchPool; } while (0)
113#define WHITE(c) ((c) == ' ' || (c) == '\t')
114
115#define _ARG_DQUOTE 0x01 /* Argument quoted (") */
116#define _ARG_RESPONSE 0x02 /* Argument read from response file */
117#define _ARG_WILDCARD 0x04 /* Argument expanded from wildcard */
118#define _ARG_ENV 0x08 /* Argument from environment */
119#define _ARG_NONZERO 0x80 /* Always set, to avoid end of string */
120
121 g_cArgs = 0; cbArgs = 0;
122
123#if 0
124 /* argv[0] */
125 PUTC((char)_ARG_NONZERO);
126 PUTV;
127 for (;;)
128 {
129 PUTC(*pszSrc);
130 if (*pszSrc == 0)
131 break;
132 ++pszSrc;
133 }
134 ++pszSrc;
135#endif
136
137 for (;;)
138 {
139 while (WHITE(*pszSrc))
140 ++pszSrc;
141 if (*pszSrc == 0)
142 break;
143 pfFlags = pchPool;
144 PUTC((char)_ARG_NONZERO);
145 PUTV;
146 bs = 0; chQuote = 0;
147 for (;;)
148 {
149 if (!chQuote ? (*pszSrc == '"' || *pszSrc == '\'') : *pszSrc == chQuote)
150 {
151 while (bs >= 2)
152 {
153 PUTC('\\');
154 bs -= 2;
155 }
156 if (bs & 1)
157 PUTC(*pszSrc);
158 else
159 {
160 chQuote = chQuote ? 0 : *pszSrc;
161 if (pfFlags != NULL)
162 *pfFlags |= _ARG_DQUOTE;
163 }
164 bs = 0;
165 }
166 else if (*pszSrc == '\\')
167 ++bs;
168 else
169 {
170 while (bs != 0)
171 {
172 PUTC('\\');
173 --bs;
174 }
175 if (*pszSrc == 0 || (WHITE(*pszSrc) && !chQuote))
176 break;
177 PUTC(*pszSrc);
178 }
179 ++pszSrc;
180 }
181 PUTC(0);
182 }
183 return cbArgs;
184}
185
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