8 * Some command-line parameters need to be saved up until after
\r
9 * we've loaded the saved session which will form the basis of our
\r
10 * eventual running configuration. For this we use the macro
\r
11 * SAVEABLE, which notices if the `need_save' parameter is set and
\r
12 * saves the parameter and value on a list.
\r
14 * We also assign priorities to saved parameters, just to slightly
\r
15 * ameliorate silly ordering problems. For example, if you specify
\r
16 * a saved session to load, it will be loaded _before_ all your
\r
17 * local modifications such as -L are evaluated; and if you specify
\r
18 * a protocol and a port, the protocol is set up first so that the
\r
19 * port can override its choice of port number.
\r
22 #define NPRIORITIES 3
\r
24 struct cmdline_saved_param {
\r
27 struct cmdline_saved_param_set {
\r
28 struct cmdline_saved_param *params;
\r
29 int nsaved, savesize;
\r
33 * C guarantees this structure will be initialised to all zero at
\r
34 * program start, which is exactly what we want.
\r
36 static struct cmdline_saved_param_set saves[NPRIORITIES];
\r
38 static void cmdline_save_param(char *p, char *value, int pri)
\r
40 if (saves[pri].nsaved >= saves[pri].savesize) {
\r
41 saves[pri].savesize = saves[pri].nsaved + 32;
\r
43 srealloc(saves[pri].params,
\r
44 saves[pri].savesize*sizeof(*saves[pri].params));
\r
46 saves[pri].params[saves[pri].nsaved].p = p;
\r
47 saves[pri].params[saves[pri].nsaved].value = value;
\r
48 saves[pri].nsaved++;
\r
51 #define SAVEABLE(pri) do { \
\r
52 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
\r
55 char *cmdline_password = NULL;
\r
57 static int cmdline_get_line(const char *prompt, char *str,
\r
58 int maxlen, int is_pw)
\r
60 static int tried_once = 0;
\r
62 assert(is_pw && cmdline_password);
\r
67 strncpy(str, cmdline_password, maxlen);
\r
68 str[maxlen - 1] = '\0';
\r
75 * Here we have a flags word which describes the capabilities of
\r
76 * the particular tool on whose behalf we're running. We will
\r
77 * refuse certain command-line options if a particular tool
\r
78 * inherently can't do anything sensible. For example, the file
\r
79 * transfer tools (psftp, pscp) can't do a great deal with protocol
\r
80 * selections (ever tried running scp over telnet?) or with port
\r
81 * forwarding (even if it wasn't a hideously bad idea, they don't
\r
82 * have the select() infrastructure to make them work).
\r
84 int cmdline_tooltype = 0;
\r
86 static int cmdline_check_unavailable(int flag, char *p)
\r
88 if (cmdline_tooltype & flag) {
\r
89 cmdline_error("option \"%s\" not available in this tool", p);
\r
95 #define UNAVAILABLE_IN(flag) do { \
\r
96 if (cmdline_check_unavailable(flag, p)) return ret; \
\r
100 * Process a standard command-line parameter. `p' is the parameter
\r
101 * in question; `value' is the subsequent element of argv, which
\r
102 * may or may not be required as an operand to the parameter.
\r
103 * Return value is 2 if both arguments were used; 1 if only p was
\r
104 * used; 0 if the parameter wasn't one we recognised; -2 if it
\r
105 * should have been 2 but value was NULL.
\r
108 #define RETURN(x) do { \
\r
109 if ((x) == 2 && !value) return -2; \
\r
113 int cmdline_process_param(char *p, char *value, int need_save)
\r
117 if (!strcmp(p, "-load")) {
\r
119 SAVEABLE(0); /* very high priority */
\r
120 do_defaults(value, &cfg);
\r
123 if (!strcmp(p, "-ssh")) {
\r
125 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
\r
127 default_protocol = cfg.protocol = PROT_SSH;
\r
128 default_port = cfg.port = 22;
\r
131 if (!strcmp(p, "-telnet")) {
\r
133 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
\r
135 default_protocol = cfg.protocol = PROT_TELNET;
\r
136 default_port = cfg.port = 23;
\r
139 if (!strcmp(p, "-rlogin")) {
\r
141 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
\r
143 default_protocol = cfg.protocol = PROT_RLOGIN;
\r
144 default_port = cfg.port = 513;
\r
147 if (!strcmp(p, "-raw")) {
\r
149 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
\r
151 default_protocol = cfg.protocol = PROT_RAW;
\r
153 if (!strcmp(p, "-v")) {
\r
155 flags |= FLAG_VERBOSE;
\r
157 if (!strcmp(p, "-l")) {
\r
160 strncpy(cfg.username, value, sizeof(cfg.username));
\r
161 cfg.username[sizeof(cfg.username) - 1] = '\0';
\r
163 if ((!strcmp(p, "-L") || !strcmp(p, "-R"))) {
\r
164 char *fwd, *ptr, *q;
\r
167 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
\r
171 /* if multiple forwards, find end of list */
\r
172 if (ptr[0]=='R' || ptr[0]=='L') {
\r
173 for (i = 0; i < sizeof(cfg.portfwd) - 2; i++)
\r
174 if (ptr[i]=='\000' && ptr[i+1]=='\000')
\r
176 ptr = ptr + i + 1; /* point to next forward slot */
\r
178 ptr[0] = p[1]; /* insert a 'L' or 'R' at the start */
\r
179 if (strlen(fwd) > sizeof(cfg.portfwd) - i - 2) {
\r
180 cmdline_error("out of space for port forwardings");
\r
183 strncpy(ptr+1, fwd, sizeof(cfg.portfwd) - i);
\r
184 q = strchr(ptr, ':');
\r
185 if (q) *q = '\t'; /* replace first : with \t */
\r
186 cfg.portfwd[sizeof(cfg.portfwd) - 1] = '\0';
\r
187 cfg.portfwd[sizeof(cfg.portfwd) - 2] = '\0';
\r
188 ptr[strlen(ptr)+1] = '\000'; /* append two '\000' */
\r
190 if (!strcmp(p, "-m")) {
\r
191 char *filename, *command;
\r
192 int cmdlen, cmdsize;
\r
197 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
\r
202 cmdlen = cmdsize = 0;
\r
204 fp = fopen(filename, "r");
\r
206 cmdline_error("unable to open command "
\r
207 "file \"%s\"", filename);
\r
215 if (cmdlen >= cmdsize) {
\r
216 cmdsize = cmdlen + 512;
\r
217 command = srealloc(command, cmdsize);
\r
219 command[cmdlen++] = d;
\r
220 } while (c != EOF);
\r
221 cfg.remote_cmd_ptr = command;
\r
222 cfg.remote_cmd_ptr2 = NULL;
\r
223 cfg.nopty = TRUE; /* command => no terminal */
\r
225 if (!strcmp(p, "-P")) {
\r
227 SAVEABLE(2); /* lower priority than -ssh,-telnet */
\r
228 cfg.port = atoi(value);
\r
230 if (!strcmp(p, "-pw")) {
\r
232 cmdline_password = value;
\r
233 ssh_get_line = cmdline_get_line;
\r
234 ssh_getline_pw_only = TRUE;
\r
236 return ret; /* unrecognised */
\r
239 void cmdline_run_saved(void)
\r
242 for (pri = 0; pri < NPRIORITIES; pri++)
\r
243 for (i = 0; i < saves[pri].nsaved; i++)
\r
244 cmdline_process_param(saves[pri].params[i].p,
\r
245 saves[pri].params[i].value, 0);
\r