source: trunk/dll/pathutil.c@ 920

Last change on this file since 920 was 920, checked in by Gregg Young, 18 years ago

Cleanup of NormalizeCmdLine moved to pathutil.c

File size: 9.4 KB
Line 
1
2/***********************************************************************
3
4 $Id: $
5
6 Path handling utility functions
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2001, 2008 Steven H. Levine
10
11 05 Jan 08 SHL Move from arccnrs.c and comp.c to here
12 06 Jan 08 GKY Add NormalizeCmdLine to check program strings on entry
13
14***********************************************************************/
15
16#include <string.h>
17
18#define INCL_WIN
19#define INCL_DOS
20#define INCL_LONGLONG
21
22#include "pathutil.h"
23#include "fm3dll.h" // needs_quoting
24#include "fm3str.h"
25#include "errutil.h" // Dos_Error...
26#include "strutil.h" // GetPString
27
28// #pragma data_seg(DATA1)
29
30/**
31 * Build full path name in callers buffer given directory
32 * name and filename
33 * @param pszPathName points to drive/directory if not NULL
34 * @returns pointer to full path name in caller's buffer
35 * @note OK for pszFullPathName and pszPathName to point to same buffer
36 *
37 */
38
39PSZ BldFullPathName(PSZ pszFullPathName, PSZ pszPathName, PSZ pszFileName)
40{
41 UINT c = pszPathName ? strlen(pszPathName) : 0;
42 if (c > 0) {
43 memcpy(pszFullPathName, pszPathName, c);
44 if (pszFullPathName[c - 1] != '\\')
45 pszFullPathName[c++] = '\\';
46 }
47 strcpy(pszFullPathName + c, pszFileName);
48 return pszFullPathName;
49}
50
51/**
52 * Build quoted full path name in callers buffer given
53 * directory name and filename
54 * @param pszPathName points to drive/directory if not NULL
55 * @returns pointer to quoted path name in caller's buffer
56 */
57
58PSZ BldQuotedFullPathName(PSZ pszFullPathName, PSZ pszPathName, PSZ pszFileName)
59{
60 UINT c = pszPathName ? strlen(pszPathName) : 0;
61 BOOL q = needs_quoting(pszPathName) ||
62 needs_quoting(pszFileName);
63 PSZ psz = pszFullPathName;
64
65 if (q)
66 *psz++ = '"';
67 if (c > 0) {
68 memcpy(psz, pszPathName, c);
69 psz += c;
70 if (*(psz - 1) != '\\')
71 *psz++ = '\\';
72 }
73 strcpy(psz, pszFileName);
74 if (q) {
75 psz += strlen(psz);
76 *psz++ = '"';
77 *psz = 0;
78 }
79 return pszFullPathName;
80}
81
82/**
83 * Build quoted full path name in callers buffer given a filename
84 * @returns pointer to quoted file name in caller's buffer
85 */
86
87PSZ BldQuotedFileName(PSZ pszQuotedFileName, PSZ pszFileName)
88{
89 BOOL q = needs_quoting(pszFileName);
90 PSZ psz = pszQuotedFileName;
91
92 if (q)
93 *psz++ = '"';
94 strcpy(psz, pszFileName);
95 if (q) {
96 psz += strlen(psz);
97 *psz++ = '"';
98 *psz = 0;
99 }
100 return pszQuotedFileName;
101}
102
103/** NormalizeCmdLine
104 * Checks a command line for common errors (missing quotes, missing extension,
105 * no space between exe and args etc)
106 * Command line passed as pszCmdLine_
107 * A pointer to a buffer of the size MAXCOMLINESTRG should be supplied in
108 * pszWorkBuf. This is where the quoted etc as necessary command
109 * line string will be returned.
110 */
111
112PCSZ NormalizeCmdLine(PSZ pszWorkBuf, PSZ pszCmdLine_)
113{
114 char szCmdLine[MAXCOMLINESTRG], szArgs[MAXCOMLINESTRG];
115 char *offset = '\0', *offsetexe, *offsetcom, *offsetcmd, *offsetbtm, *offsetbat;
116 APIRET ret;
117 ULONG ulAppType;
118 char *pszChar;
119 FILEFINDBUF3 FindBuffer;
120 ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
121 HDIR hdirFindHandle = HDIR_CREATE;
122 ULONG ulFindCount = 1;
123 PSZ pszNewCmdLine = pszWorkBuf;
124
125 bstrip(pszCmdLine_);
126 strcpy(szCmdLine, pszCmdLine_);
127 if (szCmdLine[0] != '\0') {
128 offsetexe = strstr(strlwr(pszCmdLine_), ".exe");
129 offsetcmd = strstr(strlwr(pszCmdLine_), ".cmd");
130 offsetcom = strstr(strlwr(pszCmdLine_), ".com");
131 offsetbtm = strstr(strlwr(pszCmdLine_), ".btm");
132 offsetbat = strstr(strlwr(pszCmdLine_), ".bat");
133 if (offsetexe)
134 offset = offsetexe;
135 else {
136 if (offsetcom)
137 offset = offsetcom;
138 else {
139 if (offsetcmd)
140 offset = offsetcmd;
141 else {
142 if (offsetbtm)
143 offset = offsetbtm;
144 else {
145 if (offsetbat)
146 offset = offsetexe;
147 }
148 }
149 }
150 }
151 if (offset) {
152 szCmdLine[offset + 4 - pszCmdLine_] = '\0';
153 strcpy(szArgs, &pszCmdLine_[offset + 4 - pszCmdLine_]);
154 while (strchr(szCmdLine, '\"'))
155 remove_first_occurence_of_character("\"", szCmdLine);
156 if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
157 !strstr(pszCmdLine_, "\\:")||
158 strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
159 remove_first_occurence_of_character("\"", szArgs);
160 if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
161 saymsg(MB_OK, HWND_DESKTOP,
162 NullStr,
163 GetPString(IDS_QUOTESINARGSTEXT),
164 pszCmdLine_);
165 if (!offsetexe) {
166 ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
167 ulResultBufLen, &ulFindCount, FIL_STANDARD);
168 if (ret) {
169 pszChar = szCmdLine;
170 while (pszChar) {
171 if (*pszChar == ' ') {
172 *pszChar = '\0';
173 strcat(szCmdLine, ".exe");
174 ret = DosQueryAppType(szCmdLine, &ulAppType);
175 //printf("%d %s\n", ret, szCmdLine); fflush(stdout);
176 if (!ret) {
177 strcpy(szArgs, pszCmdLine_ + strlen(szCmdLine) - 3);
178 break;
179 }
180 }
181 strcpy(szCmdLine, pszCmdLine_);
182 pszChar++;
183 }
184 }
185 }
186 else
187 ret = DosQueryAppType(szCmdLine, &ulAppType);
188 BldQuotedFileName(pszNewCmdLine, szCmdLine);
189 //printf("%d A", ret); fflush(stdout);
190 if (ret) {
191 ret = saymsg(MB_YESNO,
192 HWND_DESKTOP,
193 NullStr,
194 GetPString(IDS_PROGRAMNOTFOUNDTEXT),
195 pszCmdLine_);
196 if (ret == MBID_YES){
197 pszNewCmdLine = pszCmdLine_;
198 }
199 else{
200 fCancelAction = TRUE;
201 pszNewCmdLine = pszCmdLine_;
202 }
203 }
204 else{
205 if (szArgs[0] != ' ')
206 strcat(pszNewCmdLine, " ");
207 strcat(pszNewCmdLine, szArgs);
208 }
209
210 }
211 else if (szCmdLine && (!strchr(szCmdLine, '.') ||
212 strrchr(szCmdLine, '.' ) < strrchr(szCmdLine, '\\'))) {
213 if (!strchr(szCmdLine, ' ')) {
214 while (strchr(szCmdLine, '\"'))
215 remove_first_occurence_of_character("\"", szCmdLine);
216 strcat(szCmdLine, ".exe");
217 ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
218 ulResultBufLen, &ulFindCount, FIL_STANDARD);
219 //printf("%d", ret); fflush(stdout);
220 }
221 else {
222 pszChar = szCmdLine;
223 while (pszChar) {
224 while (strchr(szCmdLine, '\"'))
225 remove_first_occurence_of_character("\"", szCmdLine);
226 if (*pszChar == ' ') {
227 *pszChar = '\0';
228 strcat(szCmdLine, ".exe");
229 ret = DosQueryAppType(szCmdLine, &ulAppType);
230 //printf("%d %s\n", ret, szCmdLine); fflush(stdout);
231 if (!ret) {
232 break;
233 }
234 }
235 strcpy(szCmdLine, pszCmdLine_);
236 pszChar++;
237 }
238 }
239 if (!ret){
240 BldQuotedFileName(pszNewCmdLine, szCmdLine);
241 strcpy(szArgs, pszCmdLine_ + strlen(szCmdLine) - 3);
242 if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
243 !strstr(pszCmdLine_, "\\:" ) ||
244 strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
245 remove_first_occurence_of_character("\"", szArgs);
246 if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
247 saymsg(MB_OK, HWND_DESKTOP,
248 NullStr,
249 GetPString(IDS_QUOTESINARGSTEXT),
250 pszCmdLine_);
251 if (szArgs[0] != ' ')
252 strcat(pszNewCmdLine, " ");
253 strcat(pszNewCmdLine, szArgs);
254 }
255 else {
256 ret = saymsg(MB_OK,
257 HWND_DESKTOP,
258 NullStr,
259 GetPString(IDS_PROGRAMNOTEXE2TEXT),
260 pszCmdLine_);
261 fCancelAction = TRUE;
262 pszNewCmdLine = pszCmdLine_;
263 }
264 }
265 else {
266 pszChar = strrchr(szCmdLine, '.');
267 while (pszChar && *pszChar !=' ') {
268 pszChar++;
269 }
270 *pszChar = '\0';
271 strcpy (szArgs, pszCmdLine_ + strlen(szCmdLine));
272 while (strchr(szCmdLine, '\"'))
273 remove_first_occurence_of_character("\"", szCmdLine);
274 if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
275 !strstr(pszCmdLine_, "\\:")||
276 strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
277 remove_first_occurence_of_character("\"", szArgs);
278 if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
279 saymsg(MB_OK, HWND_DESKTOP,
280 NullStr,
281 GetPString(IDS_QUOTESINARGSTEXT),
282 pszCmdLine_);
283 ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
284 ulResultBufLen, &ulFindCount, FIL_STANDARD);
285
286 BldQuotedFileName(pszNewCmdLine, szCmdLine);
287 //printf("%d %s ", ret, szCmdLine); fflush(stdout);
288 if (ret) {
289 ret = saymsg(MB_YESNO,
290 HWND_DESKTOP,
291 NullStr,
292 GetPString(IDS_PROGRAMNOTFOUNDTEXT),
293 pszCmdLine_);
294 if (ret == MBID_YES) {
295 pszWorkBuf = pszCmdLine_;
296 }
297 else {
298 fCancelAction = TRUE;
299 pszWorkBuf = pszCmdLine_;
300 }
301 }
302 ret = saymsg(MB_YESNOCANCEL,
303 HWND_DESKTOP,
304 NullStr,
305 GetPString(IDS_PROGRAMNOTEXE3TEXT),
306 pszCmdLine_, pszNewCmdLine);
307 if (ret == MBID_YES){
308 if (szArgs[0] != ' ')
309 strcat(pszNewCmdLine, " ");
310 strcat(pszNewCmdLine, szArgs);
311 }
312 if (ret == MBID_CANCEL){
313 fCancelAction = TRUE;
314 pszNewCmdLine = pszCmdLine_;
315 }
316 }
317 }
318 return pszWorkBuf;
319}
320
321#pragma alloc_text(PATHUTIL,BldFullPathName)
322#pragma alloc_text(PATHUTIL,BldQuotedFileName)
323#pragma alloc_text(PATHUTIL,BldQuotedFullPathName)
324#pragma alloc_text(PATHUTIL,NormalizeCmdLine)
Note: See TracBrowser for help on using the repository browser.