Fix copy-and-paste error in command-line font selection in r9314.
[u/mdw/putty] / cmdline.c
CommitLineData
eaf1e20a 1/*
2 * cmdline.c - command-line parsing shared between many of the
3 * PuTTY applications
4 */
5
dfe3332b 6#include <stdio.h>
7#include <assert.h>
8#include <stdlib.h>
9#include "putty.h"
10
11/*
12 * Some command-line parameters need to be saved up until after
13 * we've loaded the saved session which will form the basis of our
14 * eventual running configuration. For this we use the macro
15 * SAVEABLE, which notices if the `need_save' parameter is set and
16 * saves the parameter and value on a list.
17 *
18 * We also assign priorities to saved parameters, just to slightly
19 * ameliorate silly ordering problems. For example, if you specify
20 * a saved session to load, it will be loaded _before_ all your
21 * local modifications such as -L are evaluated; and if you specify
22 * a protocol and a port, the protocol is set up first so that the
23 * port can override its choice of port number.
c725e24c 24 *
25 * (In fact -load is not saved at all, since in at least Plink the
26 * processing of further command-line options depends on whether or
27 * not the loaded session contained a hostname. So it must be
28 * executed immediately.)
dfe3332b 29 */
30
c725e24c 31#define NPRIORITIES 2
dfe3332b 32
33struct cmdline_saved_param {
34 char *p, *value;
35};
36struct cmdline_saved_param_set {
37 struct cmdline_saved_param *params;
38 int nsaved, savesize;
39};
40
41/*
42 * C guarantees this structure will be initialised to all zero at
43 * program start, which is exactly what we want.
44 */
45static struct cmdline_saved_param_set saves[NPRIORITIES];
46
47static void cmdline_save_param(char *p, char *value, int pri)
48{
49 if (saves[pri].nsaved >= saves[pri].savesize) {
50 saves[pri].savesize = saves[pri].nsaved + 32;
3d88e64d 51 saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
52 struct cmdline_saved_param);
dfe3332b 53 }
54 saves[pri].params[saves[pri].nsaved].p = p;
55 saves[pri].params[saves[pri].nsaved].value = value;
56 saves[pri].nsaved++;
57}
58
b72bdf11 59static char *cmdline_password = NULL;
60
679539d7 61void cmdline_cleanup(void)
62{
63 int pri;
64
b72bdf11 65 if (cmdline_password) {
66 memset(cmdline_password, 0, strlen(cmdline_password));
67 sfree(cmdline_password);
68 cmdline_password = NULL;
69 }
70
71 for (pri = 0; pri < NPRIORITIES; pri++) {
679539d7 72 sfree(saves[pri].params);
b72bdf11 73 saves[pri].params = NULL;
74 saves[pri].savesize = 0;
75 saves[pri].nsaved = 0;
76 }
679539d7 77}
78
dfe3332b 79#define SAVEABLE(pri) do { \
80 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
81} while (0)
82
edd0cb8a 83/*
84 * Similar interface to get_userpass_input(), except that here a -1
85 * return means that we aren't capable of processing the prompt and
86 * someone else should do it.
87 */
88int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen) {
dfe3332b 89
dfe3332b 90 static int tried_once = 0;
91
edd0cb8a 92 /*
93 * We only handle prompts which don't echo (which we assume to be
94 * passwords), and (currently) we only cope with a password prompt
95 * that comes in a prompt-set on its own.
96 */
97 if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {
98 return -1;
99 }
dfe3332b 100
edd0cb8a 101 /*
102 * If we've tried once, return utter failure (no more passwords left
103 * to try).
104 */
105 if (tried_once)
dfe3332b 106 return 0;
edd0cb8a 107
108 strncpy(p->prompts[0]->result, cmdline_password,
109 p->prompts[0]->result_len);
110 p->prompts[0]->result[p->prompts[0]->result_len-1] = '\0';
111 memset(cmdline_password, 0, strlen(cmdline_password));
b72bdf11 112 sfree(cmdline_password);
113 cmdline_password = NULL;
edd0cb8a 114 tried_once = 1;
115 return 1;
116
dfe3332b 117}
118
119/*
120 * Here we have a flags word which describes the capabilities of
121 * the particular tool on whose behalf we're running. We will
122 * refuse certain command-line options if a particular tool
123 * inherently can't do anything sensible. For example, the file
124 * transfer tools (psftp, pscp) can't do a great deal with protocol
125 * selections (ever tried running scp over telnet?) or with port
126 * forwarding (even if it wasn't a hideously bad idea, they don't
127 * have the select() infrastructure to make them work).
128 */
129int cmdline_tooltype = 0;
130
131static int cmdline_check_unavailable(int flag, char *p)
132{
133 if (cmdline_tooltype & flag) {
134 cmdline_error("option \"%s\" not available in this tool", p);
135 return 1;
136 }
137 return 0;
138}
139
140#define UNAVAILABLE_IN(flag) do { \
141 if (cmdline_check_unavailable(flag, p)) return ret; \
142} while (0)
143
144/*
145 * Process a standard command-line parameter. `p' is the parameter
146 * in question; `value' is the subsequent element of argv, which
147 * may or may not be required as an operand to the parameter.
b552f500 148 * If `need_save' is 1, arguments which need to be saved as
149 * described at this top of this file are, for later execution;
150 * if 0, they are processed normally. (-1 is a special value used
151 * by pterm to count arguments for a preliminary pass through the
152 * argument list; it causes immediate return with an appropriate
153 * value with no action taken.)
dfe3332b 154 * Return value is 2 if both arguments were used; 1 if only p was
155 * used; 0 if the parameter wasn't one we recognised; -2 if it
156 * should have been 2 but value was NULL.
157 */
158
159#define RETURN(x) do { \
160 if ((x) == 2 && !value) return -2; \
161 ret = x; \
46a3419b 162 if (need_save < 0) return x; \
dfe3332b 163} while (0)
164
4a693cfc 165int cmdline_process_param(char *p, char *value, int need_save, Conf *conf)
dfe3332b 166{
167 int ret = 0;
168
169 if (!strcmp(p, "-load")) {
170 RETURN(2);
c725e24c 171 /* This parameter must be processed immediately rather than being
172 * saved. */
4a693cfc 173 do_defaults(value, conf);
18e62ad8 174 loaded_session = TRUE;
073e9f42 175 cmdline_session_name = dupstr(value);
dfe3332b 176 return 2;
177 }
178 if (!strcmp(p, "-ssh")) {
179 RETURN(1);
46a3419b 180 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 181 SAVEABLE(0);
4a693cfc 182 default_protocol = PROT_SSH;
183 default_port = 22;
184 conf_set_int(conf, CONF_protocol, default_protocol);
185 conf_set_int(conf, CONF_port, default_port);
dfe3332b 186 return 1;
187 }
188 if (!strcmp(p, "-telnet")) {
189 RETURN(1);
46a3419b 190 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 191 SAVEABLE(0);
4a693cfc 192 default_protocol = PROT_TELNET;
193 default_port = 23;
194 conf_set_int(conf, CONF_protocol, default_protocol);
195 conf_set_int(conf, CONF_port, default_port);
dfe3332b 196 return 1;
197 }
198 if (!strcmp(p, "-rlogin")) {
199 RETURN(1);
46a3419b 200 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 201 SAVEABLE(0);
4a693cfc 202 default_protocol = PROT_RLOGIN;
203 default_port = 513;
204 conf_set_int(conf, CONF_protocol, default_protocol);
205 conf_set_int(conf, CONF_port, default_port);
dfe3332b 206 return 1;
207 }
208 if (!strcmp(p, "-raw")) {
209 RETURN(1);
46a3419b 210 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 211 SAVEABLE(0);
4a693cfc 212 default_protocol = PROT_RAW;
213 conf_set_int(conf, CONF_protocol, default_protocol);
dfe3332b 214 }
9621bbab 215 if (!strcmp(p, "-serial")) {
216 RETURN(1);
217 /* Serial is not NONNETWORK in an odd sense of the word */
218 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
219 SAVEABLE(0);
4a693cfc 220 default_protocol = PROT_SERIAL;
221 conf_set_int(conf, CONF_protocol, default_protocol);
222 /* The host parameter will already be loaded into CONF_host,
223 * so copy it across */
224 conf_set_str(conf, CONF_serline, conf_get_str(conf, CONF_host));
9621bbab 225 }
dfe3332b 226 if (!strcmp(p, "-v")) {
227 RETURN(1);
228 flags |= FLAG_VERBOSE;
229 }
230 if (!strcmp(p, "-l")) {
231 RETURN(2);
46a3419b 232 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 233 SAVEABLE(0);
4a693cfc 234 conf_set_str(conf, CONF_username, value);
dfe3332b 235 }
881da168 236 if (!strcmp(p, "-loghost")) {
237 RETURN(2);
238 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
239 SAVEABLE(0);
4a693cfc 240 conf_set_str(conf, CONF_loghost, value);
881da168 241 }
820ebe3b 242 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
4a693cfc 243 char type, *q, *qq, *key, *val;
dfe3332b 244 RETURN(2);
46a3419b 245 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 246 SAVEABLE(0);
4a693cfc 247 if (strcmp(p, "-D")) {
820ebe3b 248 /*
4a693cfc 249 * For -L or -R forwarding types:
250 *
820ebe3b 251 * We expect _at least_ two colons in this string. The
252 * possible formats are `sourceport:desthost:destport',
253 * or `sourceip:sourceport:desthost:destport' if you're
254 * specifying a particular loopback address. We need to
255 * replace the one between source and dest with a \t;
256 * this means we must find the second-to-last colon in
257 * the string.
4a693cfc 258 *
259 * (This looks like a foolish way of doing it given the
260 * existence of strrchr, but it's more efficient than
261 * two strrchrs - not to mention that the second strrchr
262 * would require us to modify the input string!)
820ebe3b 263 */
4a693cfc 264
265 type = p[1]; /* 'L' or 'R' */
266
267 q = qq = strchr(value, ':');
820ebe3b 268 while (qq) {
269 char *qqq = strchr(qq+1, ':');
270 if (qqq)
271 q = qq;
272 qq = qqq;
273 }
4a693cfc 274
275 if (!q) {
276 cmdline_error("-%c expects at least two colons in its"
277 " argument", type);
278 return ret;
279 }
280
281 key = dupprintf("%c%.*s", type, q - value, value);
282 val = dupstr(q+1);
283 } else {
284 /*
285 * Dynamic port forwardings are entered under the same key
286 * as if they were local (because they occupy the same
287 * port space - a local and a dynamic forwarding on the
288 * same local port are mutually exclusive), with the
289 * special value "D" (which can be distinguished from
290 * anything in the ordinary -L case by containing no
291 * colon).
292 */
293 key = dupprintf("L%s", value);
294 val = dupstr("D");
6ee9b735 295 }
4a693cfc 296 conf_set_str_str(conf, CONF_portfwd, key, val);
297 sfree(key);
298 sfree(val);
dfe3332b 299 }
feb02b4e 300 if ((!strcmp(p, "-nc"))) {
301 char *host, *portp;
302
303 RETURN(2);
304 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
305 SAVEABLE(0);
306
4a693cfc 307 portp = strchr(value, ':');
308 if (!portp) {
feb02b4e 309 cmdline_error("-nc expects argument of form 'host:port'");
310 return ret;
311 }
4a693cfc 312
313 host = dupprintf("%.*s", portp - value, value);
314 conf_set_str(conf, CONF_ssh_nc_host, host);
315 conf_set_int(conf, CONF_ssh_nc_port, atoi(portp + 1));
783c82b3 316 sfree(host);
feb02b4e 317 }
dfe3332b 318 if (!strcmp(p, "-m")) {
319 char *filename, *command;
320 int cmdlen, cmdsize;
321 FILE *fp;
322 int c, d;
323
324 RETURN(2);
46a3419b 325 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 326 SAVEABLE(0);
dfe3332b 327
328 filename = value;
329
330 cmdlen = cmdsize = 0;
331 command = NULL;
332 fp = fopen(filename, "r");
333 if (!fp) {
4a693cfc 334 cmdline_error("unable to open command file \"%s\"", filename);
dfe3332b 335 return ret;
336 }
337 do {
338 c = fgetc(fp);
339 d = c;
340 if (c == EOF)
341 d = 0;
342 if (cmdlen >= cmdsize) {
343 cmdsize = cmdlen + 512;
3d88e64d 344 command = sresize(command, cmdsize, char);
dfe3332b 345 }
346 command[cmdlen++] = d;
347 } while (c != EOF);
2fa6ce2c 348 fclose(fp);
4a693cfc 349 conf_set_str(conf, CONF_remote_cmd, command);
350 conf_set_str(conf, CONF_remote_cmd2, "");
351 conf_set_int(conf, CONF_nopty, TRUE); /* command => no terminal */
352 sfree(command);
dfe3332b 353 }
354 if (!strcmp(p, "-P")) {
355 RETURN(2);
46a3419b 356 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 357 SAVEABLE(1); /* lower priority than -ssh,-telnet */
4a693cfc 358 conf_set_int(conf, CONF_port, atoi(value));
dfe3332b 359 }
360 if (!strcmp(p, "-pw")) {
361 RETURN(2);
46a3419b 362 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
01ccff1b 363 SAVEABLE(1);
364 /* We delay evaluating this until after the protocol is decided,
365 * so that we can warn if it's of no use with the selected protocol */
4a693cfc 366 if (conf_get_int(conf, CONF_protocol) != PROT_SSH)
4858529b 367 cmdline_error("the -pw option can only be used with the "
01ccff1b 368 "SSH protocol");
4858529b 369 else {
370 cmdline_password = dupstr(value);
371 /* Assuming that `value' is directly from argv, make a good faith
372 * attempt to trample it, to stop it showing up in `ps' output
373 * on Unix-like systems. Not guaranteed, of course. */
374 memset(value, 0, strlen(value));
375 }
dfe3332b 376 }
377
973612f5 378 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
379 !strcmp(p, "-pageant")) {
380 RETURN(1);
381 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 382 SAVEABLE(0);
4a693cfc 383 conf_set_int(conf, CONF_tryagent, TRUE);
973612f5 384 }
385 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
386 !strcmp(p, "-nopageant")) {
387 RETURN(1);
388 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 389 SAVEABLE(0);
4a693cfc 390 conf_set_int(conf, CONF_tryagent, FALSE);
973612f5 391 }
392
dfe3332b 393 if (!strcmp(p, "-A")) {
394 RETURN(1);
46a3419b 395 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 396 SAVEABLE(0);
4a693cfc 397 conf_set_int(conf, CONF_agentfwd, 1);
dfe3332b 398 }
399 if (!strcmp(p, "-a")) {
400 RETURN(1);
46a3419b 401 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 402 SAVEABLE(0);
4a693cfc 403 conf_set_int(conf, CONF_agentfwd, 0);
dfe3332b 404 }
405
406 if (!strcmp(p, "-X")) {
407 RETURN(1);
46a3419b 408 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 409 SAVEABLE(0);
4a693cfc 410 conf_set_int(conf, CONF_x11_forward, 1);
dfe3332b 411 }
412 if (!strcmp(p, "-x")) {
413 RETURN(1);
46a3419b 414 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 415 SAVEABLE(0);
4a693cfc 416 conf_set_int(conf, CONF_x11_forward, 0);
dfe3332b 417 }
418
419 if (!strcmp(p, "-t")) {
420 RETURN(1);
46a3419b 421 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 422 SAVEABLE(1); /* lower priority than -m */
4a693cfc 423 conf_set_int(conf, CONF_nopty, 0);
dfe3332b 424 }
425 if (!strcmp(p, "-T")) {
426 RETURN(1);
46a3419b 427 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 428 SAVEABLE(1);
4a693cfc 429 conf_set_int(conf, CONF_nopty, 1);
dfe3332b 430 }
431
0ed48730 432 if (!strcmp(p, "-N")) {
433 RETURN(1);
434 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
435 SAVEABLE(0);
4a693cfc 436 conf_set_int(conf, CONF_ssh_no_shell, 1);
0ed48730 437 }
438
dfe3332b 439 if (!strcmp(p, "-C")) {
440 RETURN(1);
46a3419b 441 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 442 SAVEABLE(0);
4a693cfc 443 conf_set_int(conf, CONF_compression, 1);
dfe3332b 444 }
445
446 if (!strcmp(p, "-1")) {
447 RETURN(1);
46a3419b 448 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 449 SAVEABLE(0);
4a693cfc 450 conf_set_int(conf, CONF_sshprot, 0); /* ssh protocol 1 only */
dfe3332b 451 }
452 if (!strcmp(p, "-2")) {
453 RETURN(1);
46a3419b 454 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 455 SAVEABLE(0);
4a693cfc 456 conf_set_int(conf, CONF_sshprot, 3); /* ssh protocol 2 only */
dfe3332b 457 }
458
459 if (!strcmp(p, "-i")) {
4a693cfc 460 Filename fn;
dfe3332b 461 RETURN(2);
46a3419b 462 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 463 SAVEABLE(0);
4a693cfc 464 fn = filename_from_str(value);
465 conf_set_filename(conf, CONF_keyfile, &fn);
dfe3332b 466 }
467
05581745 468 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
469 RETURN(1);
470 SAVEABLE(1);
4a693cfc 471 conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4);
05581745 472 }
473 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
474 RETURN(1);
475 SAVEABLE(1);
4a693cfc 476 conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6);
05581745 477 }
9621bbab 478 if (!strcmp(p, "-sercfg")) {
479 char* nextitem;
480 RETURN(2);
481 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
482 SAVEABLE(1);
4a693cfc 483 if (conf_get_int(conf, CONF_protocol) != PROT_SERIAL)
9621bbab 484 cmdline_error("the -sercfg option can only be used with the "
485 "serial protocol");
486 /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
487 nextitem = value;
488 while (nextitem[0] != '\0') {
489 int length, skip;
490 char *end = strchr(nextitem, ',');
491 if (!end) {
492 length = strlen(nextitem);
493 skip = 0;
494 } else {
495 length = end - nextitem;
496 nextitem[length] = '\0';
497 skip = 1;
498 }
499 if (length == 1) {
500 switch (*nextitem) {
501 case '1':
9621bbab 502 case '2':
4a693cfc 503 conf_set_int(conf, CONF_serstopbits, 2 * (*nextitem-'0'));
9621bbab 504 break;
505
506 case '5':
a3e17bf8 507 case '6':
508 case '7':
509 case '8':
510 case '9':
4a693cfc 511 conf_set_int(conf, CONF_serdatabits, *nextitem-'0');
9621bbab 512 break;
513
514 case 'n':
4a693cfc 515 conf_set_int(conf, CONF_serparity, SER_PAR_NONE);
9621bbab 516 break;
517 case 'o':
4a693cfc 518 conf_set_int(conf, CONF_serparity, SER_PAR_ODD);
9621bbab 519 break;
520 case 'e':
4a693cfc 521 conf_set_int(conf, CONF_serparity, SER_PAR_EVEN);
9621bbab 522 break;
523 case 'm':
4a693cfc 524 conf_set_int(conf, CONF_serparity, SER_PAR_MARK);
9621bbab 525 break;
526 case 's':
4a693cfc 527 conf_set_int(conf, CONF_serparity, SER_PAR_SPACE);
9621bbab 528 break;
529
530 case 'N':
4a693cfc 531 conf_set_int(conf, CONF_serflow, SER_FLOW_NONE);
9621bbab 532 break;
533 case 'X':
4a693cfc 534 conf_set_int(conf, CONF_serflow, SER_FLOW_XONXOFF);
9621bbab 535 break;
536 case 'R':
4a693cfc 537 conf_set_int(conf, CONF_serflow, SER_FLOW_RTSCTS);
9621bbab 538 break;
539 case 'D':
4a693cfc 540 conf_set_int(conf, CONF_serflow, SER_FLOW_DSRDTR);
9621bbab 541 break;
542
543 default:
544 cmdline_error("Unrecognised suboption \"-sercfg %c\"",
545 *nextitem);
546 }
547 } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
548 /* Messy special case */
4a693cfc 549 conf_set_int(conf, CONF_serstopbits, 3);
9621bbab 550 } else {
551 int serspeed = atoi(nextitem);
552 if (serspeed != 0) {
4a693cfc 553 conf_set_int(conf, CONF_serspeed, serspeed);
9621bbab 554 } else {
555 cmdline_error("Unrecognised suboption \"-sercfg %s\"",
556 nextitem);
557 }
558 }
559 nextitem += length + skip;
560 }
561 }
dfe3332b 562 return ret; /* unrecognised */
563}
564
4a693cfc 565void cmdline_run_saved(Conf *conf)
dfe3332b 566{
567 int pri, i;
568 for (pri = 0; pri < NPRIORITIES; pri++)
569 for (i = 0; i < saves[pri].nsaved; i++)
570 cmdline_process_param(saves[pri].params[i].p,
4a693cfc 571 saves[pri].params[i].value, 0, conf);
dfe3332b 572}