forked from ritchielawrence/cmdow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.cpp
More file actions
523 lines (480 loc) · 14.9 KB
/
args.cpp
File metadata and controls
523 lines (480 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "header.h"
#include "st_utils.hpp"
#include <fmt/core.h>
void ParseArgs(int argc, char *argv[], struct ARGS *a)
{
int form1;
//
// init the args struct
//
a->caption = nullptr;
a->newcapt = nullptr;
a->file = nullptr;
a->params = nullptr;
a->sw_state = SW_SHOWNORMAL;
a->hwnd = static_cast<HWND>(nullptr);
*a->tasks = static_cast<TASK>(0);
a->listopts = 0;
a->actopts = 0;
a->cc = 0;
a->helpcmd = nullptr;
a->myhwnd = GetMyHandle();
LoadString(&a->exename, argv[0]);
//
// if no args default task is to list all windows
//
if (argc == 1)
{
PushTask(a->tasks, LISTALL);
return;
}
//
// grab every arg and push onto task queue/set options
//
for (int i = 1; i < argc; ++i)
{
if (!lstrcmpi("/?", argv[i]))
{
//
// see if user wants help on particular command
//
if (argc != 2)
LoadString(&a->helpcmd, argv[++i]);
PushTask(a->tasks, HELP);
return;
}
if (!lstrcmpi("/B", argv[i]))
a->listopts |= BARE;
else if (!lstrcmpi("/F", argv[i]))
a->listopts |= FULLCAPT;
else if (!lstrcmpi("/P", argv[i]))
a->listopts |= SHOWPOS;
else if (!lstrcmpi("/T", argv[i]))
a->listopts |= SHOWTB;
else if (!lstrcmpi("/CW", argv[i]))
PushTask(a->tasks, CW);
else if (!lstrcmpi("/TV", argv[i]))
PushTask(a->tasks, TV);
else if (!lstrcmpi("/TH", argv[i]))
PushTask(a->tasks, TH);
else if (!lstrcmpi("/MA", argv[i]))
PushTask(a->tasks, MA);
else if (!lstrcmpi("/UM", argv[i]))
PushTask(a->tasks, UW); // maintain /um for compatibility with old scripts
else if (!lstrcmpi("/UW", argv[i]))
PushTask(a->tasks, UW);
else if (!lstrcmpi("/AT", argv[i]))
PushTask(a->tasks, AT);
else if (!lstrcmpi("/FS", argv[i]))
PushTask(a->tasks, FS);
else if (!lstrcmpi("/WM", argv[i]))
PushTask(a->tasks, WM);
else if (!lstrcmpi("/DBM", argv[i]))
a->actopts |= DONTBLAMEME;
else if (!lstrcmpi("/MIN", argv[i]))
PushTask(a->tasks, MIN);
else if (!lstrcmpi("/MAX", argv[i]))
PushTask(a->tasks, MAX);
else if (!lstrcmpi("/RES", argv[i]))
PushTask(a->tasks, RES);
else if (!lstrcmpi("/ACT", argv[i]))
PushTask(a->tasks, ACT);
else if (!lstrcmpi("/INA", argv[i]))
PushTask(a->tasks, INA);
else if (!lstrcmpi("/ENA", argv[i]))
PushTask(a->tasks, ENA);
else if (!lstrcmpi("/DIS", argv[i]))
PushTask(a->tasks, DIS);
else if (!lstrcmpi("/HID", argv[i]))
PushTask(a->tasks, HID);
else if (!lstrcmpi("/VIS", argv[i]))
PushTask(a->tasks, VIS);
else if (!lstrcmpi("/END", argv[i]))
PushTask(a->tasks, END);
else if (!lstrcmpi("/CLS", argv[i]))
PushTask(a->tasks, CLS);
else if (!lstrcmpi("/TOP", argv[i]))
PushTask(a->tasks, TOP);
else if (!lstrcmpi("/NOT", argv[i]))
PushTask(a->tasks, NOT);
else if (!lstrcmpi("/MOV", argv[i]))
{
PushTask(a->tasks, MOV);
//
// next two args MUST be the new left and top coords
//
if ((i + 2) < argc)
{
try
{
a->left = std::stoi(argv[++i]);
a->top = std::stoi(argv[++i]);
}
catch (std::invalid_argument &e)
{
auto msg = fmt::format(
"Failed to convert value to int: {}",
argv[i]);
QuitMsg(msg);
}
}
else
{
Quit(MOVERR);
}
}
else if (!lstrcmpi("/SIZ", argv[i]))
{
//
// next two args MUST be the new width and height
//
PushTask(a->tasks, SIZ);
if ((i + 2) < argc)
{
try
{
a->width = std::stoi(argv[++i]);
a->height = std::stoi(argv[++i]);
}
catch (std::invalid_argument& e)
{
auto msg = fmt::format(
"Failed to convert value to int: {}",
argv[i]);
QuitMsg(msg);
}
}
else
{
Quit(SIZERR);
}
}
else if (!lstrcmpi("/REN", argv[i]))
{
//
// next arg MUST be the new caption
//
PushTask(a->tasks, REN);
if ((i + 1) < argc)
LoadString(&a->newcapt, argv[++i]);
else
Quit(RENERR);
}
else if (!lstrcmpi("/_RENAME_MYCONSOLE_WINDOW", argv[i]))
{
//
// next arg MUST be the new caption
//
PushTask(a->tasks, RCW);
if ((i + 1) < argc)
LoadString(&a->newcapt, argv[++i]);
else
Quit(RENERR);
}
else if (!lstrcmpi("/RUN", argv[i]))
{
PushTask(a->tasks, RUN);
//
// point to next arg and see if show state has been specced
//
if (!(((i++) + 1) < argc))
Quit(INCARG);
if (!lstrcmpi("/MIN", argv[i]))
a->sw_state = SW_SHOWMINIMIZED;
else if (!lstrcmpi("/MAX", argv[i]))
a->sw_state = SW_SHOWMAXIMIZED;
else if (!lstrcmpi("/HID", argv[i]))
a->sw_state = SW_HIDE;
else
--i;
//
// next arg MUST be file to open/run
//
if ((i + 1) < argc)
LoadString(&a->file, argv[++i]);
else
Quit(INCARG);
//
// if anymore args, get a pointer to remainder of commandline
//
if ((i + 1) < argc)
{
LoadString(&a->params, GetRestCmdline(argv[i]));
i = argc; // prevent any more iterations of for loop
}
}
else
{
//
// got this far, so assume unrecognized arg is a caption
// keep track of how many 'unknown' args. One unknown is assumed to be a caption
//
++a->cc;
if (i != 1)
Quit(UNRARG); // caption must be first arg
if (!lstrcmpi("@", argv[i]))
a->hwnd = a->myhwnd; // @ signifies this cmd prompt
else
{
LoadString(&a->caption, argv[i]);
a->hwnd = atoHandle(a->caption); // try and convert caption to a windows handle
}
}
}
//
// now check for valid combinations of args
//
int form5 = IsTask(a->tasks, RCW);
int form4 = IsTask(a->tasks, RUN); // run/open program/file
int form3 = ( // op on single window
IsTask(a->tasks, MIN) + IsTask(a->tasks, MAX) + IsTask(a->tasks, RES) + IsTask(a->tasks, ACT) +
IsTask(a->tasks, INA) + IsTask(a->tasks, ENA) + IsTask(a->tasks, DIS) + IsTask(a->tasks, HID) +
IsTask(a->tasks, VIS) + IsTask(a->tasks, END) + IsTask(a->tasks, CLS) + IsTask(a->tasks, REN) +
IsTask(a->tasks, MOV) + IsTask(a->tasks, SIZ) + IsTask(a->tasks, TOP) + IsTask(a->tasks, NOT));
int form2 = ( // op on all windows
IsTask(a->tasks, CW) + IsTask(a->tasks, TV) + IsTask(a->tasks, TH) + IsTask(a->tasks, MA) +
IsTask(a->tasks, UW) + IsTask(a->tasks, AT) + IsTask(a->tasks, FS) + IsTask(a->tasks, WM));
// if forms 5 to 2 not used or any listopts used, then form1 in use
if (((form5 + form4 + form3 + form2) == 0) || a->listopts)
form1 = 1;
else
form1 = 0;
int fcount = 0;
if (form5)
++fcount;
if (form4)
++fcount;
if (form3)
++fcount;
if (form2)
++fcount;
if (form1)
++fcount;
// make sure only one, and only one form has been used
if (fcount != 1)
Quit(CONARG);
if (form5)
{
;
}
else if (form4)
{
;
}
else if (form3)
{ // op on single window
if (!a->cc)
Quit(NOCAPT);
}
else if (form2)
{ // op on all windows, one arg and no caption
if (a->cc || (form2 > 1))
Quit(INCARG);
}
else
{ // form1. If caption do a LST else LISTALL
if ((a->listopts & SHOWTB) && (a->cc))
Quit(INCARG);
if (a->cc)
PushTask(a->tasks, LST);
else
PushTask(a->tasks, LISTALL);
}
}
enum TASK PushTask(enum TASK tasks[], enum TASK t)
{
int i = 0;
//
// goto last task plus one, null terminate task queue, then add task to queue
//
while (tasks[i++])
;
if (i == MAXTASKS)
Quit(TASKOL);
tasks[i] = NONE;
return tasks[--i] = t;
}
enum TASK PopTask(enum TASK tasks[])
{
int i;
enum TASK t;
i = 0;
while (tasks[i++])
;
if (!i)
return NONE;
i--;
t = tasks[i];
tasks[i] = NONE;
return t;
}
int IsTask(enum TASK tasks[], enum TASK t)
{
int i;
i = 0;
while (tasks[i])
if (tasks[i++] == t)
return (1);
return (0);
}
HWND atoHandle(const char *src)
{
char *p;
// string must begin with 0x or 0X
if (strncmp("0x", src, 2) != 0 &&
strncmp("0X", src, 2) != 0)
{
return 0;
}
HWND h = reinterpret_cast<HWND>(strtoul(src, &p, 16));
// all characters must be interpreted
if (*p)
{
return nullptr;
}
return h;
}
char *LoadString(char **dest, const char *src)
{
*dest = static_cast<char*>(HeapAlloc(GetProcessHeap(), 0, sizeof(char) * (lstrlen(src) + 1)));
if (!*dest)
Quit(MEMERR);
lstrcpy(*dest, src);
return (*dest);
}
void Quit(const int Err)
{
char szBuff[1024] = "Error: ";
const char *msg[] = {
// --1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
"Memory allocation failed", /* MEMERR 0 */
"/MOV command requires left and top arguments", /* MOVERR 1 */
"/SIZ command requires width and height arguments" , /* SIZERR 2 */
"/REN command requires a new caption to be specified", /* RENERR 3 */
"Unrecognized argument(s). Use CMDOW /? for help", /* UNRARG 4 */
"Incompatible argument(s). Use CMDOW /? for help", /* CONARG 5 */
"Incorrect argument(s). Use CMDOW /? for help", /* INCARG 6 */
"A window must be specified by its caption or handle", /* NOCAPT 7 */
"The specified window was not found", /* NOTFND 8 */
"Unable to retrieve image names", /* BADIMG 9 */
"Too many tasks have been specified", /* TASKOL 10 */
"Unable to execute/open specified file", /* EXEERR 11 */
"Only the /? and /RUN commands are supported on W95/98/ME platforms" /* VERERR 12 */
};
auto full_msg = fmt::format("Error: {}.\n", msg[Err]);
DWORD cbWritten;
DWORD BytesToWrite = full_msg.size();
WriteFile(GetStdHandle(STD_ERROR_HANDLE),
szBuff,
BytesToWrite,
&cbWritten,
nullptr);
ExitProcess(1);
}
void QuitMsg(const std::string &msg)
{
auto full_msg = fmt::format("Error: {}.\n", msg);
DWORD cbWritten;
DWORD BytesToWrite = full_msg.size();
WriteFile(GetStdHandle(STD_ERROR_HANDLE),
full_msg.c_str(),
BytesToWrite,
&cbWritten,
nullptr);
ExitProcess(1);
}
char *GetArgs()
{
static char *SysCmdLine; /* pointer to system commandline */
static char *SclPos; /* pointer to current position of system cmdline */
static char *Argv; /* pointer to allocated mem, holds one arg at a time */
/* pointer to current position of argv memory */
//
// if !SysCmdLine, initialise the static vars
//
if (!SysCmdLine)
{
int i;
SysCmdLine = SclPos = GetCommandLine();
//
// allocate enough memory to hold the longest argument
//
i = lstrlen(SysCmdLine);
Argv = static_cast<char*>(HeapAlloc(GetProcessHeap(), 0, i + 1));
if (!Argv)
{
Quit(MEMERR);
}
}
//
// init ArgvPos and NULL terminate Argv
//
char* ArgvPos = Argv;
*ArgvPos = 0;
while (true)
{
//
// find start of argument, skipping any whitespace
//
while (*ArgvPos && ((' ' == *ArgvPos) || ('\t' == *ArgvPos)))
{
++ArgvPos;
}
if (0 == *ArgvPos)
{
return Argv;
}
//
// now work through any quotes, copying every third quote
//
int Quotes = 0;
while ('"' == *ArgvPos)
{
++ArgvPos;
++Quotes;
if (3 == Quotes)
{
Quotes = 0;
*ArgvPos++ = '"';
}
}
if (0 == *ArgvPos)
{
return Argv;
}
//
// now work through the argument, copying to Argv
//
}
}
//+---------------------------------------------------------------------------
// Function: GetRestCmdline
//
// Synopsis: Finds the first non-whitespace character after specified
// command.
//
// Arguments: [Cmd] Pointer to cmd to find and skip past
//
// Returns: Pointer to remainder of commandline after specified cmd
//
// Notes:
//
//----------------------------------------------------------------------------
char *GetRestCmdline(const char *cmd)
{
char* SysCmdLine = GetCommandLine();
char* p = strstr(SysCmdLine, cmd);
if (p)
{
p += lstrlen(cmd); // skip pass the Cmd
if ('"' == *p)
{
p++; // if cmd was quoted, skip past the end quote
}
while (*p && ((' ' == *p) || '\t' == *p))
{
p++; // skip whitespace
}
}
return p;
}