Debian Codegraph @

src/af/xap/xp/xap_Args.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998 AbiSource, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include "xap_Args.h"
24 #include "ut_string.h"
25 #include "ut_debugmsg.h"
26 #include "ut_assert.h"
27
28 /*****************************************************************/
29
30 XAP_Args::XAP_Args(int argc, char ** argv)
31 {
32 m_argc = argc;
33 m_argv = argv;
34 m_szBuf = NULL;
35 }
36
37 #ifdef WIN32
38
39 char *XX_encode(const char *str)
40 {
41 int l=strlen(str)+1;
42 const char *c;
43 char *d;
44 char *result;
45 for (c=str;*c;c++) {
46 if (*c<0 || *c=='%') l+=2;
47 }
48 result=(char*)g_malloc(l);
49 for (c=str,d=result; *c; c++) {
50 if (*c<0 || *c=='%') {
51 sprintf(d,"%%%02X",(unsigned char)*c);
52 d+=3;
53 } else {
54 *d++=*c;
55 }
56 }
57 *d=0;
58 return result;
59 }
60
61 #endif
62
63 XAP_Args::XAP_Args(const char * szCmdLine)
64 {
65 // build an argc,argv for this command line
66
67 m_argc = 0;
68 m_argv = NULL;
69 m_szBuf = NULL;
70
71 if (!szCmdLine || !*szCmdLine)
72 return;
73
74 // copy command line into work buffer
75 // and put pointers to the tokens in m_argv
76 //
77 // we support (with apologies to Flex & Bison):
78 //
79 // WHITE [ \t]+
80 // DQUOTE '"'
81 // SQUOTE '\''
82 // OTHER [^ \t'"]
83 //
84 // T1 := OTHER*
85 // T2 := DQUOTE [^DQUOTE]* DQUOTE
86 // T3 := SQUOTE [^SQUOTE]* SQUOTE
87 //
88 // WHITE ({T1|T2|T3}WHITE)* [WHITE]
89
90 #ifdef WIN32
91 // glib on Windows assumes that command line is in ANSI codepage
92 m_szBuf = XX_encode(szCmdLine);
93 #else
94 m_szBuf = g_strdup(szCmdLine);
95 #endif
96 UT_ASSERT(m_szBuf);
97
98 int count = 10; // start with 10 and g_try_realloc if necessary
99 int k = 0;
100 char ** argv = (char **)UT_calloc(count,sizeof(char *));
101
102 enum _state { S_START, S_INTOKEN, S_INDQUOTE, S_INSQUOTE } state;
103 state = S_START;
104
105 #define GrowArrayIfNecessary() \
106 do { if (k==count) \
107 { int newsize = (count+10)*sizeof(char *); \
108 argv = (char **)g_try_realloc(argv,newsize); \
109 count += 10; \
110 }} while (0)
111
112 char * p = m_szBuf;
113 while (*p)
114 {
115 switch (state)
116 {
117 case S_START:
118 if ( (*p==' ') || (*p=='\t') )
119 {
120 p++;
121 break;
122 }
123
124 if (*p=='\'')
125 {
126 state=S_INSQUOTE;
127 *p++=0; // don't include starting quote in token
128 }
129 else if (*p=='"')
130 {
131 state=S_INDQUOTE;
132 *p++=0; // don't include starting quote in token
133 }
134 else
135 state=S_INTOKEN;
136
137 GrowArrayIfNecessary();
138 argv[k++] = p++;
139 break;
140
141 case S_INTOKEN:
142 if ( (*p==' ') || (*p=='\t') )
143 {
144 state=S_START;
145 *p++=0;
146 break;
147 }
148
149 p++;
150 break;
151
152 case S_INDQUOTE:
153 if ( *p=='"' )
154 {
155 state=S_START;
156 *p++=0;
157 break;
158 }
159
160 p++;
161 break;
162
163 case S_INSQUOTE:
164 if ( *p=='\'' )
165 {
166 state=S_START;
167 *p++=0;
168 break;
169 }
170
171 p++;
172 break;
173 }
174 }
175
176 if (k==0)
177 {
178 FREEP(m_szBuf);
179 return;
180 }
181
182 m_argv = argv;
183 m_argc = k;
184
185 #ifdef DEBUG
186 for (int kk=0; kk<m_argc; kk++)
187 UT_DEBUGMSG(("ParsedCommandLine: argv[%d][%s]\n",kk,m_argv[kk]));
188 #endif
189
190 return;
191 }
192
193 XAP_Args::~XAP_Args(void)
194 {
195 if (m_szBuf)
196 {
197 FREEP(m_szBuf);
198 FREEP(m_argv);
199 }
200 }