|
|
1.1.1.3 root 1: /* The source code contained in this file has been derived from the source code
2: of Encryption for the Masses 2.02a by Paul Le Roux. Modifications and
3: additions to that source code contained in this file are Copyright (c) 2004
1.1.1.4 ! root 4: TrueCrypt Foundation and Copyright (c) 2004 TrueCrypt Team. Unmodified
1.1.1.3 root 5: parts are Copyright (c) 1998-99 Paul Le Roux. This is a TrueCrypt Foundation
6: release. Please see the file license.txt for full license details. */
1.1 root 7:
8: #include "TCdefs.h"
9:
10: #include <malloc.h>
11: #include <ctype.h>
12: #include "cmdline.h"
13:
14: #include "resource.h"
15: #include "crypto.h"
16: #include "apidrvr.h"
17: #include "dlgcode.h"
18:
19: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
20: should return nonzero if it processes the message, and zero if it does
21: not. - see DialogProc */
22: BOOL WINAPI
23: CommandHelpDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
24: {
25: if (lParam); /* remove warning */
26: if (wParam); /* remove warning */
27:
28: switch (msg)
29: {
30: case WM_INITDIALOG:
31: {
32: char * tmp = err_malloc(8192);
33: char tmp2[256];
34: argumentspec *as;
35: int i;
36:
37: SetDefaultUserFont (hwndDlg);
38:
39: as = (argumentspec*) lParam;
40:
41: *tmp = 0;
42:
1.1.1.4 ! root 43: strcpy (tmp, "Command line options:\n\n");
1.1 root 44: for (i = 0; i < as->arg_cnt; i ++)
45: {
1.1.1.4 ! root 46: sprintf(tmp2, "%s\t%s\n", as->args[i].short_name, as->args[i].long_name);
1.1 root 47: strcat(tmp,tmp2);
48: }
49:
50: SetWindowText (GetDlgItem (hwndDlg, IDC_COMMANDHELP_TEXT), (char*) tmp);
51: return 1;
52: }
53:
54: case WM_COMMAND:
55: EndDialog (hwndDlg, IDOK);
56: return 1;
57: case WM_CLOSE:
58: EndDialog (hwndDlg, 0);
59: return 1;
60: }
61:
62: return 0;
63: }
64:
65: int
66: Win32CommandLine (char *lpszCommandLine, char ***lpszArgs)
67: {
68: int i = 0, k = 0, x = 0, nValid = TRUE;
69: int nLen = strlen (lpszCommandLine);
70: int nArrSize = 16;
71: char szTmp[256];
72:
73: *lpszArgs = malloc (sizeof (char *)* nArrSize);
74:
75: if (*lpszArgs == NULL)
76: return 0;
77:
78: while (i < nLen)
79: {
80: if (lpszCommandLine[i] == ' ')
81: {
82: if (k > 0)
83: {
84: szTmp[k] = 0;
85: (*lpszArgs)[x] = _strdup (szTmp);
86: if ((*lpszArgs)[x] == NULL)
87: {
88: free (*lpszArgs);
89: return 0;
90: }
91: x++;
92: k = 0;
93: if (x == nArrSize)
94: {
95: break;
96: }
97: }
98: i++;
99: continue;
100: }
101: if (lpszCommandLine[i] == '"')
102: {
103: i++;
104: while (i < nLen)
105: {
106: if (lpszCommandLine[i] == '"')
107: break;
108: if (k < sizeof (szTmp))
109: szTmp[k++] = lpszCommandLine[i++];
110: else
111: {
112: free (*lpszArgs);
113: return 0;
114: }
115: }
116:
117: if (lpszCommandLine[i] != '"')
118: {
119: nValid = FALSE;
120: break;
121: }
122: }
123: else
124: {
125: if (k < sizeof (szTmp))
126: szTmp[k++] = lpszCommandLine[i];
127: else
128: {
129: free (*lpszArgs);
130: return 0;
131: }
132: }
133:
134: i++;
135: }
136:
137: if (nValid == FALSE)
138: {
139: free (*lpszArgs);
140: return 0;
141: }
142: else if (k > 0)
143: {
144: szTmp[k] = 0;
145: (*lpszArgs)[x] = _strdup (szTmp);
146: if ((*lpszArgs)[x] == NULL)
147: {
148: free (*lpszArgs);
149: return 0;
150: }
151: x++;
152: k = 0;
153: }
154: if (!x)
155: {
156: free (*lpszArgs);
157: return 0;
158: }
159: return x;
160: }
161:
162: int
163: GetArgSepPosOffset (char *lpszArgument)
164: {
165: if (lpszArgument[0] == '/')
166: return 1;
167: else if (lpszArgument[0] == '-' && lpszArgument[1] == '-')
168: return 2;
169: else if (lpszArgument[0] == '-')
170: return 1;
171: else
172: return 0;
173: }
174:
175: int
176: GetArgumentID (argumentspec *as, char *lpszArgument, int *nArgPos)
177: {
178: char szTmp[256];
179: int i;
180:
181: i = strlen (lpszArgument);
182: szTmp[i] = 0;
183: while (--i >= 0)
184: {
185: szTmp[i] = (char) tolower (lpszArgument[i]);
186: }
187:
188: for (i = 0; i < as->arg_cnt; i++)
189: {
190: size_t k;
191:
192: k = strlen (as->args[i].long_name);
193: if (memcmp (as->args[i].long_name, szTmp, k * sizeof (char)) == 0)
194: {
195: int x;
196: for (x = i + 1; x < as->arg_cnt; x++)
197: {
198: size_t m;
199:
200: m = strlen (as->args[x].long_name);
201: if (memcmp (as->args[x].long_name, szTmp, m * sizeof (char)) == 0)
202: {
203: break;
204: }
205: }
206:
207: if (x == as->arg_cnt)
208: {
209: if (strlen (lpszArgument) != k)
210: *nArgPos = k;
211: else
212: *nArgPos = 0;
213: return as->args[i].short_name[1];
214: }
215: }
216: }
217:
218: for (i = 0; i < as->arg_cnt; i++)
219: {
220: size_t k;
221:
222: k = strlen (as->args[i].short_name);
223: if (memcmp (as->args[i].short_name, szTmp, k * sizeof (char)) == 0)
224: {
225: int x;
226: for (x = i + 1; x < as->arg_cnt; x++)
227: {
228: size_t m;
229:
230: m = strlen (as->args[x].short_name);
231: if (memcmp (as->args[x].short_name, szTmp, m * sizeof (char)) == 0)
232: {
233: break;
234: }
235: }
236:
237: if (x == as->arg_cnt)
238: {
239: if (strlen (lpszArgument) != k)
240: *nArgPos = k;
241: else
242: *nArgPos = 0;
243: return as->args[i].short_name[1];
244: }
245: }
246: }
247:
248:
249: return -1;
250: }
251:
252: int
253: GetArgumentValue (char **lpszCommandLineArgs, int nArgPos, int *nArgIdx,
254: int nNoCommandLineArgs, char *lpszValue, int nValueSize)
255: {
256: *lpszValue = 0;
257:
258: if (nArgPos)
259: {
260: /* Handles the case of no space between parameter code and
261: value */
262: memcpy (lpszValue, &lpszCommandLineArgs[*nArgIdx][nArgPos], nValueSize * sizeof (char));
263: lpszValue[nValueSize - 1] = 0;
264: return HAS_ARGUMENT;
265: }
266: else if (*nArgIdx + 1 != nNoCommandLineArgs)
267: {
268: int x = GetArgSepPosOffset (lpszCommandLineArgs[*nArgIdx + 1]);
269: if (x == 0)
270: {
271: /* Handles the case of space between parameter code
272: and value */
273: memcpy (lpszValue, &lpszCommandLineArgs[*nArgIdx + 1][x], nValueSize * sizeof (char));
274: lpszValue[nValueSize - 1] = 0;
275: (*nArgIdx)++;
276: return HAS_ARGUMENT;
277: }
278: }
279:
280: return HAS_NO_ARGUMENT;
281: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.