main.c revision 1.16 1 1.16 lukem /* $NetBSD: main.c,v 1.16 1997/01/19 14:19:15 lukem Exp $ */
2 1.12 lukem
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1985, 1989, 1993, 1994
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.3 cgd static char copyright[] =
38 1.3 cgd "@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
39 1.3 cgd The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.8 tls #if 0
44 1.8 tls static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
45 1.8 tls #else
46 1.16 lukem static char rcsid[] = "$NetBSD: main.c,v 1.16 1997/01/19 14:19:15 lukem Exp $";
47 1.8 tls #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * FTP User Program -- Command Interface.
52 1.1 cgd */
53 1.3 cgd #include <sys/types.h>
54 1.1 cgd #include <sys/socket.h>
55 1.1 cgd
56 1.3 cgd #include <err.h>
57 1.1 cgd #include <netdb.h>
58 1.1 cgd #include <pwd.h>
59 1.3 cgd #include <stdio.h>
60 1.9 cgd #include <string.h>
61 1.3 cgd #include <unistd.h>
62 1.1 cgd
63 1.3 cgd #include "ftp_var.h"
64 1.1 cgd
65 1.3 cgd int
66 1.1 cgd main(argc, argv)
67 1.3 cgd int argc;
68 1.1 cgd char *argv[];
69 1.1 cgd {
70 1.16 lukem struct servent *sp;
71 1.16 lukem int ch, top, port, rval;
72 1.1 cgd struct passwd *pw = NULL;
73 1.3 cgd char *cp, homedir[MAXPATHLEN];
74 1.1 cgd
75 1.1 cgd sp = getservbyname("ftp", "tcp");
76 1.3 cgd if (sp == 0)
77 1.16 lukem ftpport = htons(FTP_PORT); /* good fallback */
78 1.16 lukem else
79 1.16 lukem ftpport = sp->s_port;
80 1.16 lukem sp = getservbyname("http", "tcp");
81 1.16 lukem if (sp == 0)
82 1.16 lukem httpport = htons(HTTP_PORT); /* good fallback */
83 1.16 lukem else
84 1.16 lukem httpport = sp->s_port;
85 1.1 cgd doglob = 1;
86 1.1 cgd interactive = 1;
87 1.1 cgd autologin = 1;
88 1.12 lukem passivemode = 0;
89 1.13 lukem preserve = 1;
90 1.12 lukem mark = HASHBYTES;
91 1.16 lukem marg_sl = sl_init();
92 1.3 cgd
93 1.15 lukem cp = strrchr(argv[0], '/');
94 1.15 lukem cp = (cp == NULL) ? argv[0] : cp + 1;
95 1.15 lukem if (strcmp(cp, "pftp") == 0)
96 1.15 lukem passivemode = 1;
97 1.15 lukem
98 1.12 lukem while ((ch = getopt(argc, argv, "adginpP:tv")) != EOF) {
99 1.6 mycroft switch (ch) {
100 1.12 lukem case 'a':
101 1.12 lukem anonftp = 1;
102 1.12 lukem break;
103 1.12 lukem
104 1.3 cgd case 'd':
105 1.3 cgd options |= SO_DEBUG;
106 1.3 cgd debug++;
107 1.3 cgd break;
108 1.12 lukem
109 1.3 cgd case 'g':
110 1.3 cgd doglob = 0;
111 1.3 cgd break;
112 1.1 cgd
113 1.3 cgd case 'i':
114 1.3 cgd interactive = 0;
115 1.3 cgd break;
116 1.1 cgd
117 1.3 cgd case 'n':
118 1.3 cgd autologin = 0;
119 1.3 cgd break;
120 1.1 cgd
121 1.12 lukem case 'p':
122 1.12 lukem passivemode = 1;
123 1.12 lukem break;
124 1.12 lukem
125 1.12 lukem case 'P':
126 1.16 lukem port = atoi(optarg);
127 1.16 lukem if (port <= 0)
128 1.16 lukem warnx("bad port number: %s", optarg);
129 1.16 lukem else
130 1.16 lukem ftpport = htons(port);
131 1.12 lukem break;
132 1.12 lukem
133 1.3 cgd case 't':
134 1.3 cgd trace++;
135 1.3 cgd break;
136 1.1 cgd
137 1.3 cgd case 'v':
138 1.3 cgd verbose++;
139 1.3 cgd break;
140 1.1 cgd
141 1.3 cgd default:
142 1.14 lukem usage();
143 1.3 cgd }
144 1.1 cgd }
145 1.3 cgd argc -= optind;
146 1.3 cgd argv += optind;
147 1.3 cgd
148 1.1 cgd fromatty = isatty(fileno(stdin));
149 1.16 lukem if (fromatty) {
150 1.1 cgd verbose++;
151 1.16 lukem progress = 1; /* progress bar on if a tty */
152 1.16 lukem }
153 1.1 cgd cpend = 0; /* no pending replies */
154 1.1 cgd proxy = 0; /* proxy not active */
155 1.1 cgd crflag = 1; /* strip c.r. on ascii gets */
156 1.1 cgd sendport = -1; /* not using ports */
157 1.1 cgd /*
158 1.1 cgd * Set up the home directory in case we're globbing.
159 1.1 cgd */
160 1.1 cgd cp = getlogin();
161 1.1 cgd if (cp != NULL) {
162 1.1 cgd pw = getpwnam(cp);
163 1.1 cgd }
164 1.1 cgd if (pw == NULL)
165 1.1 cgd pw = getpwuid(getuid());
166 1.1 cgd if (pw != NULL) {
167 1.1 cgd home = homedir;
168 1.1 cgd (void) strcpy(home, pw->pw_dir);
169 1.1 cgd }
170 1.12 lukem
171 1.16 lukem #ifndef SMALLFTP
172 1.16 lukem editing = 0; /* command line editing off */
173 1.16 lukem if (fromatty) {
174 1.16 lukem editing = 1; /* editing mode on if a tty */
175 1.16 lukem el = el_init(__progname, stdin, stdout); /* init editline */
176 1.16 lukem
177 1.16 lukem hist = history_init(); /* init the builtin history */
178 1.16 lukem history(hist, H_EVENT, 100); /* remember 100 events */
179 1.16 lukem el_set(el, EL_HIST, history, hist); /* use history */
180 1.16 lukem
181 1.16 lukem el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
182 1.16 lukem el_set(el, EL_PROMPT, prompt); /* set the prompt function */
183 1.16 lukem
184 1.16 lukem /* add local file completion, bind to TAB */
185 1.16 lukem el_set(el, EL_ADDFN, "ftp-complete",
186 1.16 lukem "Context sensitive argument completion",
187 1.16 lukem complete);
188 1.16 lukem el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
189 1.16 lukem
190 1.16 lukem el_source(el, NULL); /* read ~/.editrc */
191 1.12 lukem }
192 1.16 lukem #endif /* !SMALLFTP */
193 1.12 lukem
194 1.1 cgd if (argc > 0) {
195 1.16 lukem if (strchr(argv[0], ':') != NULL) {
196 1.16 lukem anonftp = 1; /* Handle "automatic" transfers. */
197 1.16 lukem rval = auto_fetch(argc, argv);
198 1.16 lukem if (rval >= 0) /* -1 == connected and cd-ed */
199 1.16 lukem exit(rval);
200 1.16 lukem } else {
201 1.16 lukem char *xargv[5];
202 1.3 cgd
203 1.16 lukem if (setjmp(toplevel))
204 1.16 lukem exit(0);
205 1.16 lukem (void) signal(SIGINT, intr);
206 1.16 lukem (void) signal(SIGPIPE, lostpeer);
207 1.16 lukem xargv[0] = __progname;
208 1.16 lukem xargv[1] = argv[0];
209 1.16 lukem xargv[2] = argv[1];
210 1.16 lukem xargv[3] = argv[2];
211 1.16 lukem xargv[4] = NULL;
212 1.16 lukem setpeer(argc+1, xargv);
213 1.16 lukem }
214 1.1 cgd }
215 1.1 cgd top = setjmp(toplevel) == 0;
216 1.1 cgd if (top) {
217 1.1 cgd (void) signal(SIGINT, intr);
218 1.1 cgd (void) signal(SIGPIPE, lostpeer);
219 1.1 cgd }
220 1.1 cgd for (;;) {
221 1.1 cgd cmdscanner(top);
222 1.1 cgd top = 1;
223 1.1 cgd }
224 1.1 cgd }
225 1.1 cgd
226 1.1 cgd void
227 1.1 cgd intr()
228 1.1 cgd {
229 1.1 cgd
230 1.1 cgd longjmp(toplevel, 1);
231 1.1 cgd }
232 1.1 cgd
233 1.1 cgd void
234 1.1 cgd lostpeer()
235 1.1 cgd {
236 1.1 cgd
237 1.1 cgd if (connected) {
238 1.1 cgd if (cout != NULL) {
239 1.1 cgd (void) shutdown(fileno(cout), 1+1);
240 1.1 cgd (void) fclose(cout);
241 1.1 cgd cout = NULL;
242 1.1 cgd }
243 1.1 cgd if (data >= 0) {
244 1.1 cgd (void) shutdown(data, 1+1);
245 1.1 cgd (void) close(data);
246 1.1 cgd data = -1;
247 1.1 cgd }
248 1.1 cgd connected = 0;
249 1.1 cgd }
250 1.1 cgd pswitch(1);
251 1.1 cgd if (connected) {
252 1.1 cgd if (cout != NULL) {
253 1.1 cgd (void) shutdown(fileno(cout), 1+1);
254 1.1 cgd (void) fclose(cout);
255 1.1 cgd cout = NULL;
256 1.1 cgd }
257 1.1 cgd connected = 0;
258 1.1 cgd }
259 1.1 cgd proxflag = 0;
260 1.1 cgd pswitch(0);
261 1.1 cgd }
262 1.1 cgd
263 1.3 cgd /*
264 1.16 lukem * Generate a prompt
265 1.16 lukem */
266 1.3 cgd char *
267 1.16 lukem prompt()
268 1.1 cgd {
269 1.16 lukem return ("ftp> ");
270 1.1 cgd }
271 1.3 cgd
272 1.1 cgd /*
273 1.1 cgd * Command parser.
274 1.1 cgd */
275 1.3 cgd void
276 1.1 cgd cmdscanner(top)
277 1.1 cgd int top;
278 1.1 cgd {
279 1.3 cgd struct cmd *c;
280 1.16 lukem int num;
281 1.1 cgd
282 1.16 lukem if (!top
283 1.16 lukem #ifndef SMALLFTP
284 1.16 lukem && !editing
285 1.16 lukem #endif /* !SMALLFTP */
286 1.16 lukem )
287 1.1 cgd (void) putchar('\n');
288 1.1 cgd for (;;) {
289 1.16 lukem #ifndef SMALLFTP
290 1.16 lukem if (!editing) {
291 1.16 lukem #endif /* !SMALLFTP */
292 1.16 lukem if (fromatty) {
293 1.16 lukem printf("%s", prompt());
294 1.16 lukem (void) fflush(stdout);
295 1.16 lukem }
296 1.16 lukem if (fgets(line, sizeof(line), stdin) == NULL)
297 1.16 lukem quit(0, 0);
298 1.16 lukem num = strlen(line);
299 1.16 lukem if (num == 0)
300 1.16 lukem break;
301 1.16 lukem if (line[--num] == '\n') {
302 1.16 lukem if (num == 0)
303 1.16 lukem break;
304 1.16 lukem line[num] = '\0';
305 1.16 lukem } else if (num == sizeof(line) - 2) {
306 1.16 lukem printf("sorry, input line too long\n");
307 1.16 lukem while ((num = getchar()) != '\n' && num != EOF)
308 1.16 lukem /* void */;
309 1.16 lukem break;
310 1.16 lukem } /* else it was a line without a newline */
311 1.16 lukem #ifndef SMALLFTP
312 1.16 lukem }
313 1.16 lukem else {
314 1.16 lukem const char *buf;
315 1.16 lukem cursor_pos = NULL;
316 1.16 lukem
317 1.16 lukem if ((buf = el_gets(el, &num)) == NULL || num == 0)
318 1.16 lukem break;
319 1.16 lukem if (line[--num] == '\n') {
320 1.16 lukem if (num == 0)
321 1.16 lukem break;
322 1.16 lukem } else if (num >= sizeof(line)) {
323 1.16 lukem printf("sorry, input line too long\n");
324 1.1 cgd break;
325 1.16 lukem }
326 1.16 lukem memcpy(line, buf, num);
327 1.16 lukem line[num] = '\0';
328 1.16 lukem history(hist, H_ENTER, buf);
329 1.16 lukem }
330 1.16 lukem #endif /* !SMALLFTP */
331 1.16 lukem
332 1.1 cgd makeargv();
333 1.16 lukem if (margc == 0)
334 1.1 cgd continue;
335 1.16 lukem #if 0 || !defined(SMALLFTP) /* XXX: don't want el_parse */
336 1.16 lukem /*
337 1.16 lukem * el_parse returns -1 to signal that it's not been handled
338 1.16 lukem * internally.
339 1.16 lukem */
340 1.16 lukem if (el_parse(el, margc, margv) != -1)
341 1.16 lukem continue;
342 1.16 lukem #endif /* !SMALLFTP */
343 1.1 cgd c = getcmd(margv[0]);
344 1.1 cgd if (c == (struct cmd *)-1) {
345 1.1 cgd printf("?Ambiguous command\n");
346 1.1 cgd continue;
347 1.1 cgd }
348 1.1 cgd if (c == 0) {
349 1.1 cgd printf("?Invalid command\n");
350 1.1 cgd continue;
351 1.1 cgd }
352 1.1 cgd if (c->c_conn && !connected) {
353 1.1 cgd printf("Not connected.\n");
354 1.1 cgd continue;
355 1.1 cgd }
356 1.13 lukem confirmrest = 0;
357 1.1 cgd (*c->c_handler)(margc, margv);
358 1.1 cgd if (bell && c->c_bell)
359 1.1 cgd (void) putchar('\007');
360 1.1 cgd if (c->c_handler != help)
361 1.1 cgd break;
362 1.1 cgd }
363 1.1 cgd (void) signal(SIGINT, intr);
364 1.1 cgd (void) signal(SIGPIPE, lostpeer);
365 1.1 cgd }
366 1.1 cgd
367 1.1 cgd struct cmd *
368 1.1 cgd getcmd(name)
369 1.13 lukem const char *name;
370 1.1 cgd {
371 1.13 lukem const char *p, *q;
372 1.3 cgd struct cmd *c, *found;
373 1.3 cgd int nmatches, longest;
374 1.11 pk
375 1.11 pk if (name == NULL)
376 1.11 pk return (0);
377 1.1 cgd
378 1.1 cgd longest = 0;
379 1.1 cgd nmatches = 0;
380 1.1 cgd found = 0;
381 1.12 lukem for (c = cmdtab; (p = c->c_name) != NULL; c++) {
382 1.1 cgd for (q = name; *q == *p++; q++)
383 1.1 cgd if (*q == 0) /* exact match? */
384 1.1 cgd return (c);
385 1.1 cgd if (!*q) { /* the name was a prefix */
386 1.1 cgd if (q - name > longest) {
387 1.1 cgd longest = q - name;
388 1.1 cgd nmatches = 1;
389 1.1 cgd found = c;
390 1.1 cgd } else if (q - name == longest)
391 1.1 cgd nmatches++;
392 1.1 cgd }
393 1.1 cgd }
394 1.1 cgd if (nmatches > 1)
395 1.1 cgd return ((struct cmd *)-1);
396 1.1 cgd return (found);
397 1.1 cgd }
398 1.1 cgd
399 1.1 cgd /*
400 1.1 cgd * Slice a string up into argc/argv.
401 1.1 cgd */
402 1.1 cgd
403 1.1 cgd int slrflag;
404 1.1 cgd
405 1.3 cgd void
406 1.1 cgd makeargv()
407 1.1 cgd {
408 1.16 lukem char *argp;
409 1.1 cgd
410 1.1 cgd stringbase = line; /* scan from first of buffer */
411 1.1 cgd argbase = argbuf; /* store from first of buffer */
412 1.1 cgd slrflag = 0;
413 1.16 lukem marg_sl->sl_cur = 0; /* reset to start of marg_sl */
414 1.10 pk for (margc = 0; ; margc++) {
415 1.16 lukem argp = slurpstring();
416 1.16 lukem sl_add(marg_sl, argp);
417 1.16 lukem if (argp == NULL)
418 1.10 pk break;
419 1.10 pk }
420 1.16 lukem #ifndef SMALLFTP
421 1.16 lukem if (cursor_pos == line) {
422 1.16 lukem cursor_argc = 0;
423 1.16 lukem cursor_argo = 0;
424 1.16 lukem } else if (cursor_pos != NULL) {
425 1.16 lukem cursor_argc = margc;
426 1.16 lukem cursor_argo = strlen(margv[margc-1]);
427 1.16 lukem }
428 1.16 lukem #endif /* !SMALLFTP */
429 1.16 lukem }
430 1.10 pk
431 1.16 lukem #ifdef SMALLFTP
432 1.16 lukem #define INC_CHKCURSOR(x) (x)++
433 1.16 lukem #else /* !SMALLFTP */
434 1.16 lukem #define INC_CHKCURSOR(x) { (x)++ ; \
435 1.16 lukem if (x == cursor_pos) { \
436 1.16 lukem cursor_argc = margc; \
437 1.16 lukem cursor_argo = ap-argbase; \
438 1.16 lukem cursor_pos = NULL; \
439 1.16 lukem } }
440 1.16 lukem
441 1.16 lukem #endif /* !SMALLFTP */
442 1.1 cgd
443 1.1 cgd /*
444 1.1 cgd * Parse string into argbuf;
445 1.1 cgd * implemented with FSM to
446 1.1 cgd * handle quoting and strings
447 1.1 cgd */
448 1.1 cgd char *
449 1.1 cgd slurpstring()
450 1.1 cgd {
451 1.1 cgd int got_one = 0;
452 1.3 cgd char *sb = stringbase;
453 1.3 cgd char *ap = argbase;
454 1.1 cgd char *tmp = argbase; /* will return this if token found */
455 1.1 cgd
456 1.1 cgd if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
457 1.1 cgd switch (slrflag) { /* and $ as token for macro invoke */
458 1.1 cgd case 0:
459 1.1 cgd slrflag++;
460 1.16 lukem INC_CHKCURSOR(stringbase);
461 1.1 cgd return ((*sb == '!') ? "!" : "$");
462 1.1 cgd /* NOTREACHED */
463 1.1 cgd case 1:
464 1.1 cgd slrflag++;
465 1.1 cgd altarg = stringbase;
466 1.1 cgd break;
467 1.1 cgd default:
468 1.1 cgd break;
469 1.1 cgd }
470 1.1 cgd }
471 1.1 cgd
472 1.1 cgd S0:
473 1.1 cgd switch (*sb) {
474 1.1 cgd
475 1.1 cgd case '\0':
476 1.1 cgd goto OUT;
477 1.1 cgd
478 1.1 cgd case ' ':
479 1.1 cgd case '\t':
480 1.16 lukem INC_CHKCURSOR(sb);
481 1.16 lukem goto S0;
482 1.1 cgd
483 1.1 cgd default:
484 1.1 cgd switch (slrflag) {
485 1.1 cgd case 0:
486 1.1 cgd slrflag++;
487 1.1 cgd break;
488 1.1 cgd case 1:
489 1.1 cgd slrflag++;
490 1.1 cgd altarg = sb;
491 1.1 cgd break;
492 1.1 cgd default:
493 1.1 cgd break;
494 1.1 cgd }
495 1.1 cgd goto S1;
496 1.1 cgd }
497 1.1 cgd
498 1.1 cgd S1:
499 1.1 cgd switch (*sb) {
500 1.1 cgd
501 1.1 cgd case ' ':
502 1.1 cgd case '\t':
503 1.1 cgd case '\0':
504 1.1 cgd goto OUT; /* end of token */
505 1.1 cgd
506 1.1 cgd case '\\':
507 1.16 lukem INC_CHKCURSOR(sb);
508 1.16 lukem goto S2; /* slurp next character */
509 1.1 cgd
510 1.1 cgd case '"':
511 1.16 lukem INC_CHKCURSOR(sb);
512 1.16 lukem goto S3; /* slurp quoted string */
513 1.1 cgd
514 1.1 cgd default:
515 1.16 lukem *ap = *sb; /* add character to token */
516 1.16 lukem ap++;
517 1.16 lukem INC_CHKCURSOR(sb);
518 1.1 cgd got_one = 1;
519 1.1 cgd goto S1;
520 1.1 cgd }
521 1.1 cgd
522 1.1 cgd S2:
523 1.1 cgd switch (*sb) {
524 1.1 cgd
525 1.1 cgd case '\0':
526 1.1 cgd goto OUT;
527 1.1 cgd
528 1.1 cgd default:
529 1.16 lukem *ap = *sb;
530 1.16 lukem ap++;
531 1.16 lukem INC_CHKCURSOR(sb);
532 1.1 cgd got_one = 1;
533 1.1 cgd goto S1;
534 1.1 cgd }
535 1.1 cgd
536 1.1 cgd S3:
537 1.1 cgd switch (*sb) {
538 1.1 cgd
539 1.1 cgd case '\0':
540 1.1 cgd goto OUT;
541 1.1 cgd
542 1.1 cgd case '"':
543 1.16 lukem INC_CHKCURSOR(sb);
544 1.16 lukem goto S1;
545 1.1 cgd
546 1.1 cgd default:
547 1.16 lukem *ap = *sb;
548 1.16 lukem ap++;
549 1.16 lukem INC_CHKCURSOR(sb);
550 1.1 cgd got_one = 1;
551 1.1 cgd goto S3;
552 1.1 cgd }
553 1.1 cgd
554 1.1 cgd OUT:
555 1.1 cgd if (got_one)
556 1.1 cgd *ap++ = '\0';
557 1.1 cgd argbase = ap; /* update storage pointer */
558 1.1 cgd stringbase = sb; /* update scan pointer */
559 1.1 cgd if (got_one) {
560 1.3 cgd return (tmp);
561 1.1 cgd }
562 1.1 cgd switch (slrflag) {
563 1.1 cgd case 0:
564 1.1 cgd slrflag++;
565 1.1 cgd break;
566 1.1 cgd case 1:
567 1.1 cgd slrflag++;
568 1.1 cgd altarg = (char *) 0;
569 1.1 cgd break;
570 1.1 cgd default:
571 1.1 cgd break;
572 1.1 cgd }
573 1.3 cgd return ((char *)0);
574 1.1 cgd }
575 1.1 cgd
576 1.1 cgd /*
577 1.1 cgd * Help command.
578 1.1 cgd * Call each command handler with argc == 0 and argv[0] == name.
579 1.1 cgd */
580 1.3 cgd void
581 1.1 cgd help(argc, argv)
582 1.1 cgd int argc;
583 1.1 cgd char *argv[];
584 1.1 cgd {
585 1.3 cgd struct cmd *c;
586 1.1 cgd
587 1.1 cgd if (argc == 1) {
588 1.16 lukem StringList *buf;
589 1.1 cgd
590 1.16 lukem buf = sl_init();
591 1.16 lukem printf("%sommands may be abbreviated. Commands are:\n\n",
592 1.16 lukem proxy ? "Proxy c" : "C");
593 1.16 lukem for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
594 1.16 lukem if (c->c_name && (!proxy || c->c_proxy))
595 1.16 lukem sl_add(buf, c->c_name);
596 1.16 lukem list_vertical(buf);
597 1.16 lukem sl_free(buf, 0);
598 1.1 cgd return;
599 1.1 cgd }
600 1.16 lukem
601 1.16 lukem #define HELPINDENT ((int) sizeof ("disconnect"))
602 1.16 lukem
603 1.1 cgd while (--argc > 0) {
604 1.3 cgd char *arg;
605 1.16 lukem
606 1.1 cgd arg = *++argv;
607 1.1 cgd c = getcmd(arg);
608 1.1 cgd if (c == (struct cmd *)-1)
609 1.1 cgd printf("?Ambiguous help command %s\n", arg);
610 1.1 cgd else if (c == (struct cmd *)0)
611 1.1 cgd printf("?Invalid help command %s\n", arg);
612 1.1 cgd else
613 1.1 cgd printf("%-*s\t%s\n", HELPINDENT,
614 1.1 cgd c->c_name, c->c_help);
615 1.1 cgd }
616 1.12 lukem }
617 1.12 lukem
618 1.14 lukem void
619 1.14 lukem usage()
620 1.14 lukem {
621 1.14 lukem (void)fprintf(stderr,
622 1.16 lukem "usage: %s [-adginptv] [host [port]]\n"
623 1.16 lukem " %s host:path[/]\n"
624 1.16 lukem " %s ftp://host[:port]/path[/]\n"
625 1.16 lukem " %s http://host[:port]/file\n",
626 1.16 lukem __progname, __progname, __progname, __progname);
627 1.14 lukem exit(1);
628 1.1 cgd }
629