Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.26.2.1
      1  1.26.2.1    mellon /*	$NetBSD: main.c,v 1.26.2.1 1997/12/14 01:17:06 mellon 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.23     lukem #include <sys/cdefs.h>
     37       1.1       cgd #ifndef lint
     38      1.23     lukem __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
     39      1.23     lukem 	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.26.2.1    mellon __RCSID("$NetBSD: main.c,v 1.26.2.1 1997/12/14 01:17:06 mellon 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.22     lukem #include <stdlib.h>
     61       1.9       cgd #include <string.h>
     62       1.3       cgd #include <unistd.h>
     63       1.1       cgd 
     64       1.3       cgd #include "ftp_var.h"
     65      1.25     lukem #include "pathnames.h"
     66       1.1       cgd 
     67      1.23     lukem int main __P((int, char **));
     68      1.23     lukem 
     69       1.3       cgd int
     70       1.1       cgd main(argc, argv)
     71       1.3       cgd 	int argc;
     72       1.1       cgd 	char *argv[];
     73       1.1       cgd {
     74      1.16     lukem 	struct servent *sp;
     75      1.23     lukem 	int ch, top, rval;
     76      1.23     lukem 	long port;
     77       1.1       cgd 	struct passwd *pw = NULL;
     78      1.24     lukem 	char *cp, *ep, homedir[MAXPATHLEN];
     79      1.22     lukem 	int dumbterm;
     80       1.1       cgd 
     81       1.1       cgd 	sp = getservbyname("ftp", "tcp");
     82       1.3       cgd 	if (sp == 0)
     83      1.16     lukem 		ftpport = htons(FTP_PORT);	/* good fallback */
     84      1.16     lukem 	else
     85      1.16     lukem 		ftpport = sp->s_port;
     86      1.16     lukem 	sp = getservbyname("http", "tcp");
     87      1.16     lukem 	if (sp == 0)
     88      1.16     lukem 		httpport = htons(HTTP_PORT);	/* good fallback */
     89      1.16     lukem 	else
     90      1.16     lukem 		httpport = sp->s_port;
     91      1.24     lukem 	gateport = 0;
     92      1.24     lukem 	cp = getenv("FTPSERVERPORT");
     93      1.24     lukem 	if (cp != NULL) {
     94      1.24     lukem 		port = strtol(cp, &ep, 10);
     95      1.24     lukem 		if (port < 1 || port > 0xffff || *ep != '\0')
     96      1.24     lukem 			warnx("bad FTPSERVERPORT port number: %s (ignored)",
     97      1.24     lukem 			    cp);
     98      1.24     lukem 		else
     99      1.24     lukem 			gateport = htons(port);
    100      1.24     lukem 	}
    101      1.24     lukem 	if (gateport == 0) {
    102      1.24     lukem 		sp = getservbyname("ftpgate", "tcp");
    103      1.24     lukem 		if (sp == 0)
    104      1.24     lukem 			gateport = htons(GATE_PORT);
    105      1.24     lukem 		else
    106      1.24     lukem 			gateport = sp->s_port;
    107      1.24     lukem 	}
    108       1.1       cgd 	doglob = 1;
    109       1.1       cgd 	interactive = 1;
    110       1.1       cgd 	autologin = 1;
    111      1.12     lukem 	passivemode = 0;
    112      1.13     lukem 	preserve = 1;
    113      1.17     lukem 	verbose = 0;
    114      1.17     lukem 	progress = 0;
    115      1.24     lukem 	gatemode = 0;
    116      1.19  christos #ifndef SMALL
    117      1.18     lukem 	editing = 0;
    118      1.21     lukem 	el = NULL;
    119      1.21     lukem 	hist = NULL;
    120      1.18     lukem #endif
    121      1.12     lukem 	mark = HASHBYTES;
    122      1.16     lukem 	marg_sl = sl_init();
    123      1.25     lukem 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    124      1.25     lukem 		tmpdir = _PATH_TMP;
    125       1.3       cgd 
    126      1.15     lukem 	cp = strrchr(argv[0], '/');
    127      1.15     lukem 	cp = (cp == NULL) ? argv[0] : cp + 1;
    128      1.15     lukem 	if (strcmp(cp, "pftp") == 0)
    129      1.15     lukem 		passivemode = 1;
    130      1.24     lukem 	else if (strcmp(cp, "gate-ftp") == 0)
    131      1.24     lukem 		gatemode = 1;
    132      1.24     lukem 
    133      1.24     lukem 	gateserver = getenv("FTPSERVER");
    134      1.24     lukem 	if (gateserver == NULL || *gateserver == '\0')
    135      1.24     lukem 		gateserver = GATE_SERVER;
    136      1.24     lukem 	if (gatemode) {
    137      1.24     lukem 		if (*gateserver == '\0') {
    138      1.24     lukem 			warnx(
    139      1.24     lukem "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
    140      1.24     lukem 			gatemode = 0;
    141      1.24     lukem 		}
    142      1.24     lukem 	}
    143      1.15     lukem 
    144      1.22     lukem 	cp = getenv("TERM");
    145      1.22     lukem 	if (cp == NULL || strcmp(cp, "dumb") == 0)
    146      1.22     lukem 		dumbterm = 1;
    147      1.22     lukem 	else
    148      1.22     lukem 		dumbterm = 0;
    149      1.17     lukem 	fromatty = isatty(fileno(stdin));
    150      1.18     lukem 	if (fromatty) {
    151      1.17     lukem 		verbose = 1;		/* verbose if from a tty */
    152      1.19  christos #ifndef SMALL
    153      1.22     lukem 		if (! dumbterm)
    154      1.22     lukem 			editing = 1;	/* editing mode on if tty is usable */
    155      1.18     lukem #endif
    156      1.18     lukem 	}
    157  1.26.2.1    mellon #ifndef SMALL
    158      1.22     lukem 	if (isatty(fileno(stdout)) && !dumbterm)
    159      1.22     lukem 		progress = 1;		/* progress bar on if tty is usable */
    160  1.26.2.1    mellon #endif
    161      1.17     lukem 
    162      1.18     lukem 	while ((ch = getopt(argc, argv, "adeginpP:tvV")) != -1) {
    163       1.6   mycroft 		switch (ch) {
    164      1.12     lukem 		case 'a':
    165      1.12     lukem 			anonftp = 1;
    166      1.12     lukem 			break;
    167      1.12     lukem 
    168       1.3       cgd 		case 'd':
    169       1.3       cgd 			options |= SO_DEBUG;
    170       1.3       cgd 			debug++;
    171       1.3       cgd 			break;
    172      1.12     lukem 
    173      1.18     lukem 		case 'e':
    174      1.19  christos #ifndef SMALL
    175      1.18     lukem 			editing = 0;
    176      1.18     lukem #endif
    177      1.18     lukem 			break;
    178      1.18     lukem 
    179       1.3       cgd 		case 'g':
    180       1.3       cgd 			doglob = 0;
    181       1.3       cgd 			break;
    182       1.1       cgd 
    183       1.3       cgd 		case 'i':
    184       1.3       cgd 			interactive = 0;
    185       1.3       cgd 			break;
    186       1.1       cgd 
    187       1.3       cgd 		case 'n':
    188       1.3       cgd 			autologin = 0;
    189       1.3       cgd 			break;
    190       1.1       cgd 
    191      1.12     lukem 		case 'p':
    192      1.12     lukem 			passivemode = 1;
    193      1.12     lukem 			break;
    194      1.12     lukem 
    195      1.12     lukem 		case 'P':
    196      1.24     lukem 			port = strtol(optarg, &ep, 10);
    197      1.24     lukem 			if (port < 1 || port > 0xffff || *ep != '\0')
    198      1.18     lukem 				warnx("bad port number: %s (ignored)", optarg);
    199      1.16     lukem 			else
    200      1.16     lukem 				ftpport = htons(port);
    201      1.12     lukem 			break;
    202      1.12     lukem 
    203       1.3       cgd 		case 't':
    204      1.17     lukem 			trace = 1;
    205       1.3       cgd 			break;
    206       1.1       cgd 
    207       1.3       cgd 		case 'v':
    208      1.17     lukem 			verbose = 1;
    209      1.17     lukem 			break;
    210      1.17     lukem 
    211      1.17     lukem 		case 'V':
    212      1.17     lukem 			verbose = 0;
    213       1.3       cgd 			break;
    214       1.1       cgd 
    215       1.3       cgd 		default:
    216      1.14     lukem 			usage();
    217       1.3       cgd 		}
    218       1.1       cgd 	}
    219       1.3       cgd 	argc -= optind;
    220       1.3       cgd 	argv += optind;
    221       1.3       cgd 
    222       1.1       cgd 	cpend = 0;	/* no pending replies */
    223       1.1       cgd 	proxy = 0;	/* proxy not active */
    224       1.1       cgd 	crflag = 1;	/* strip c.r. on ascii gets */
    225       1.1       cgd 	sendport = -1;	/* not using ports */
    226       1.1       cgd 	/*
    227       1.1       cgd 	 * Set up the home directory in case we're globbing.
    228       1.1       cgd 	 */
    229       1.1       cgd 	cp = getlogin();
    230       1.1       cgd 	if (cp != NULL) {
    231       1.1       cgd 		pw = getpwnam(cp);
    232       1.1       cgd 	}
    233       1.1       cgd 	if (pw == NULL)
    234       1.1       cgd 		pw = getpwuid(getuid());
    235       1.1       cgd 	if (pw != NULL) {
    236       1.1       cgd 		home = homedir;
    237      1.18     lukem 		(void)strcpy(home, pw->pw_dir);
    238       1.1       cgd 	}
    239      1.12     lukem 
    240      1.17     lukem 	setttywidth(0);
    241      1.18     lukem 	(void)signal(SIGWINCH, setttywidth);
    242      1.23     lukem 
    243      1.23     lukem #ifdef __GNUC__			/* XXX: to shut up gcc warnings */
    244      1.23     lukem 	(void)&argc;
    245      1.23     lukem 	(void)&argv;
    246      1.23     lukem #endif
    247      1.17     lukem 
    248       1.1       cgd 	if (argc > 0) {
    249      1.16     lukem 		if (strchr(argv[0], ':') != NULL) {
    250      1.16     lukem 			anonftp = 1;	/* Handle "automatic" transfers. */
    251      1.16     lukem 			rval = auto_fetch(argc, argv);
    252      1.16     lukem 			if (rval >= 0)		/* -1 == connected and cd-ed */
    253      1.16     lukem 				exit(rval);
    254      1.16     lukem 		} else {
    255      1.16     lukem 			char *xargv[5];
    256       1.3       cgd 
    257      1.16     lukem 			if (setjmp(toplevel))
    258      1.16     lukem 				exit(0);
    259      1.18     lukem 			(void)signal(SIGINT, (sig_t)intr);
    260      1.18     lukem 			(void)signal(SIGPIPE, (sig_t)lostpeer);
    261      1.16     lukem 			xargv[0] = __progname;
    262      1.16     lukem 			xargv[1] = argv[0];
    263      1.16     lukem 			xargv[2] = argv[1];
    264      1.16     lukem 			xargv[3] = argv[2];
    265      1.16     lukem 			xargv[4] = NULL;
    266      1.16     lukem 			setpeer(argc+1, xargv);
    267      1.16     lukem 		}
    268       1.1       cgd 	}
    269      1.21     lukem #ifndef SMALL
    270      1.21     lukem 	controlediting();
    271      1.21     lukem #endif /* !SMALL */
    272       1.1       cgd 	top = setjmp(toplevel) == 0;
    273       1.1       cgd 	if (top) {
    274      1.18     lukem 		(void)signal(SIGINT, (sig_t)intr);
    275      1.18     lukem 		(void)signal(SIGPIPE, (sig_t)lostpeer);
    276       1.1       cgd 	}
    277       1.1       cgd 	for (;;) {
    278       1.1       cgd 		cmdscanner(top);
    279       1.1       cgd 		top = 1;
    280       1.1       cgd 	}
    281       1.1       cgd }
    282       1.1       cgd 
    283       1.1       cgd void
    284       1.1       cgd intr()
    285       1.1       cgd {
    286       1.1       cgd 
    287      1.17     lukem 	alarmtimer(0);
    288       1.1       cgd 	longjmp(toplevel, 1);
    289       1.1       cgd }
    290       1.1       cgd 
    291       1.1       cgd void
    292       1.1       cgd lostpeer()
    293       1.1       cgd {
    294       1.1       cgd 
    295      1.17     lukem 	alarmtimer(0);
    296       1.1       cgd 	if (connected) {
    297       1.1       cgd 		if (cout != NULL) {
    298      1.18     lukem 			(void)shutdown(fileno(cout), 1+1);
    299      1.18     lukem 			(void)fclose(cout);
    300       1.1       cgd 			cout = NULL;
    301       1.1       cgd 		}
    302       1.1       cgd 		if (data >= 0) {
    303      1.18     lukem 			(void)shutdown(data, 1+1);
    304      1.18     lukem 			(void)close(data);
    305       1.1       cgd 			data = -1;
    306       1.1       cgd 		}
    307       1.1       cgd 		connected = 0;
    308       1.1       cgd 	}
    309       1.1       cgd 	pswitch(1);
    310       1.1       cgd 	if (connected) {
    311       1.1       cgd 		if (cout != NULL) {
    312      1.18     lukem 			(void)shutdown(fileno(cout), 1+1);
    313      1.18     lukem 			(void)fclose(cout);
    314       1.1       cgd 			cout = NULL;
    315       1.1       cgd 		}
    316       1.1       cgd 		connected = 0;
    317       1.1       cgd 	}
    318       1.1       cgd 	proxflag = 0;
    319       1.1       cgd 	pswitch(0);
    320       1.1       cgd }
    321       1.1       cgd 
    322       1.3       cgd /*
    323      1.16     lukem  * Generate a prompt
    324      1.16     lukem  */
    325       1.3       cgd char *
    326      1.16     lukem prompt()
    327       1.1       cgd {
    328      1.16     lukem 	return ("ftp> ");
    329       1.1       cgd }
    330       1.3       cgd 
    331       1.1       cgd /*
    332       1.1       cgd  * Command parser.
    333       1.1       cgd  */
    334       1.3       cgd void
    335       1.1       cgd cmdscanner(top)
    336       1.1       cgd 	int top;
    337       1.1       cgd {
    338       1.3       cgd 	struct cmd *c;
    339      1.16     lukem 	int num;
    340       1.1       cgd 
    341      1.16     lukem 	if (!top
    342      1.19  christos #ifndef SMALL
    343      1.16     lukem 	    && !editing
    344      1.19  christos #endif /* !SMALL */
    345      1.16     lukem 	    )
    346      1.18     lukem 		(void)putchar('\n');
    347       1.1       cgd 	for (;;) {
    348      1.19  christos #ifndef SMALL
    349      1.16     lukem 		if (!editing) {
    350      1.19  christos #endif /* !SMALL */
    351      1.16     lukem 			if (fromatty) {
    352      1.18     lukem 				fputs(prompt(), stdout);
    353      1.18     lukem 				(void)fflush(stdout);
    354      1.16     lukem 			}
    355      1.16     lukem 			if (fgets(line, sizeof(line), stdin) == NULL)
    356      1.16     lukem 				quit(0, 0);
    357      1.16     lukem 			num = strlen(line);
    358      1.16     lukem 			if (num == 0)
    359      1.16     lukem 				break;
    360      1.16     lukem 			if (line[--num] == '\n') {
    361      1.16     lukem 				if (num == 0)
    362      1.16     lukem 					break;
    363      1.16     lukem 				line[num] = '\0';
    364      1.16     lukem 			} else if (num == sizeof(line) - 2) {
    365      1.18     lukem 				puts("sorry, input line too long.");
    366      1.16     lukem 				while ((num = getchar()) != '\n' && num != EOF)
    367      1.16     lukem 					/* void */;
    368      1.16     lukem 				break;
    369      1.16     lukem 			} /* else it was a line without a newline */
    370      1.19  christos #ifndef SMALL
    371      1.17     lukem 		} else {
    372      1.16     lukem 			const char *buf;
    373      1.26  christos 			HistEvent ev;
    374      1.16     lukem 			cursor_pos = NULL;
    375      1.16     lukem 
    376      1.16     lukem 			if ((buf = el_gets(el, &num)) == NULL || num == 0)
    377      1.17     lukem 				quit(0, 0);
    378      1.16     lukem 			if (line[--num] == '\n') {
    379      1.16     lukem 				if (num == 0)
    380      1.16     lukem 					break;
    381      1.16     lukem 			} else if (num >= sizeof(line)) {
    382      1.18     lukem 				puts("sorry, input line too long.");
    383       1.1       cgd 				break;
    384      1.16     lukem 			}
    385      1.16     lukem 			memcpy(line, buf, num);
    386      1.16     lukem 			line[num] = '\0';
    387      1.26  christos 			history(hist, &ev, H_ENTER, buf);
    388      1.16     lukem 		}
    389      1.19  christos #endif /* !SMALL */
    390      1.16     lukem 
    391       1.1       cgd 		makeargv();
    392      1.16     lukem 		if (margc == 0)
    393       1.1       cgd 			continue;
    394      1.19  christos #if 0 && !defined(SMALL)	/* XXX: don't want el_parse */
    395      1.16     lukem 		/*
    396      1.16     lukem 		 * el_parse returns -1 to signal that it's not been handled
    397      1.16     lukem 		 * internally.
    398      1.16     lukem 		 */
    399      1.16     lukem 		if (el_parse(el, margc, margv) != -1)
    400      1.16     lukem 			continue;
    401      1.19  christos #endif /* !SMALL */
    402       1.1       cgd 		c = getcmd(margv[0]);
    403       1.1       cgd 		if (c == (struct cmd *)-1) {
    404      1.18     lukem 			puts("?Ambiguous command.");
    405       1.1       cgd 			continue;
    406       1.1       cgd 		}
    407       1.1       cgd 		if (c == 0) {
    408      1.18     lukem 			puts("?Invalid command.");
    409       1.1       cgd 			continue;
    410       1.1       cgd 		}
    411       1.1       cgd 		if (c->c_conn && !connected) {
    412      1.18     lukem 			puts("Not connected.");
    413       1.1       cgd 			continue;
    414       1.1       cgd 		}
    415      1.13     lukem 		confirmrest = 0;
    416       1.1       cgd 		(*c->c_handler)(margc, margv);
    417       1.1       cgd 		if (bell && c->c_bell)
    418      1.18     lukem 			(void)putchar('\007');
    419       1.1       cgd 		if (c->c_handler != help)
    420       1.1       cgd 			break;
    421       1.1       cgd 	}
    422      1.18     lukem 	(void)signal(SIGINT, (sig_t)intr);
    423      1.18     lukem 	(void)signal(SIGPIPE, (sig_t)lostpeer);
    424       1.1       cgd }
    425       1.1       cgd 
    426       1.1       cgd struct cmd *
    427       1.1       cgd getcmd(name)
    428      1.13     lukem 	const char *name;
    429       1.1       cgd {
    430      1.13     lukem 	const char *p, *q;
    431       1.3       cgd 	struct cmd *c, *found;
    432       1.3       cgd 	int nmatches, longest;
    433      1.11        pk 
    434      1.11        pk 	if (name == NULL)
    435      1.11        pk 		return (0);
    436       1.1       cgd 
    437       1.1       cgd 	longest = 0;
    438       1.1       cgd 	nmatches = 0;
    439       1.1       cgd 	found = 0;
    440      1.12     lukem 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
    441       1.1       cgd 		for (q = name; *q == *p++; q++)
    442       1.1       cgd 			if (*q == 0)		/* exact match? */
    443       1.1       cgd 				return (c);
    444       1.1       cgd 		if (!*q) {			/* the name was a prefix */
    445       1.1       cgd 			if (q - name > longest) {
    446       1.1       cgd 				longest = q - name;
    447       1.1       cgd 				nmatches = 1;
    448       1.1       cgd 				found = c;
    449       1.1       cgd 			} else if (q - name == longest)
    450       1.1       cgd 				nmatches++;
    451       1.1       cgd 		}
    452       1.1       cgd 	}
    453       1.1       cgd 	if (nmatches > 1)
    454       1.1       cgd 		return ((struct cmd *)-1);
    455       1.1       cgd 	return (found);
    456       1.1       cgd }
    457       1.1       cgd 
    458       1.1       cgd /*
    459       1.1       cgd  * Slice a string up into argc/argv.
    460       1.1       cgd  */
    461       1.1       cgd 
    462       1.1       cgd int slrflag;
    463       1.1       cgd 
    464       1.3       cgd void
    465       1.1       cgd makeargv()
    466       1.1       cgd {
    467      1.16     lukem 	char *argp;
    468       1.1       cgd 
    469       1.1       cgd 	stringbase = line;		/* scan from first of buffer */
    470       1.1       cgd 	argbase = argbuf;		/* store from first of buffer */
    471       1.1       cgd 	slrflag = 0;
    472      1.16     lukem 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
    473      1.10        pk 	for (margc = 0; ; margc++) {
    474      1.16     lukem 		argp = slurpstring();
    475      1.16     lukem 		sl_add(marg_sl, argp);
    476      1.16     lukem 		if (argp == NULL)
    477      1.10        pk 			break;
    478      1.10        pk 	}
    479      1.19  christos #ifndef SMALL
    480      1.16     lukem 	if (cursor_pos == line) {
    481      1.16     lukem 		cursor_argc = 0;
    482      1.16     lukem 		cursor_argo = 0;
    483      1.16     lukem 	} else if (cursor_pos != NULL) {
    484      1.16     lukem 		cursor_argc = margc;
    485      1.16     lukem 		cursor_argo = strlen(margv[margc-1]);
    486      1.16     lukem 	}
    487      1.19  christos #endif /* !SMALL */
    488      1.16     lukem }
    489      1.10        pk 
    490      1.19  christos #ifdef SMALL
    491      1.16     lukem #define INC_CHKCURSOR(x)	(x)++
    492      1.19  christos #else  /* !SMALL */
    493      1.16     lukem #define INC_CHKCURSOR(x)	{ (x)++ ; \
    494      1.16     lukem 				if (x == cursor_pos) { \
    495      1.16     lukem 					cursor_argc = margc; \
    496      1.16     lukem 					cursor_argo = ap-argbase; \
    497      1.16     lukem 					cursor_pos = NULL; \
    498      1.16     lukem 				} }
    499      1.16     lukem 
    500      1.19  christos #endif /* !SMALL */
    501       1.1       cgd 
    502       1.1       cgd /*
    503       1.1       cgd  * Parse string into argbuf;
    504       1.1       cgd  * implemented with FSM to
    505       1.1       cgd  * handle quoting and strings
    506       1.1       cgd  */
    507       1.1       cgd char *
    508       1.1       cgd slurpstring()
    509       1.1       cgd {
    510       1.1       cgd 	int got_one = 0;
    511       1.3       cgd 	char *sb = stringbase;
    512       1.3       cgd 	char *ap = argbase;
    513       1.1       cgd 	char *tmp = argbase;		/* will return this if token found */
    514       1.1       cgd 
    515       1.1       cgd 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    516       1.1       cgd 		switch (slrflag) {	/* and $ as token for macro invoke */
    517       1.1       cgd 			case 0:
    518       1.1       cgd 				slrflag++;
    519      1.16     lukem 				INC_CHKCURSOR(stringbase);
    520       1.1       cgd 				return ((*sb == '!') ? "!" : "$");
    521       1.1       cgd 				/* NOTREACHED */
    522       1.1       cgd 			case 1:
    523       1.1       cgd 				slrflag++;
    524       1.1       cgd 				altarg = stringbase;
    525       1.1       cgd 				break;
    526       1.1       cgd 			default:
    527       1.1       cgd 				break;
    528       1.1       cgd 		}
    529       1.1       cgd 	}
    530       1.1       cgd 
    531       1.1       cgd S0:
    532       1.1       cgd 	switch (*sb) {
    533       1.1       cgd 
    534       1.1       cgd 	case '\0':
    535       1.1       cgd 		goto OUT;
    536       1.1       cgd 
    537       1.1       cgd 	case ' ':
    538       1.1       cgd 	case '\t':
    539      1.16     lukem 		INC_CHKCURSOR(sb);
    540      1.16     lukem 		goto S0;
    541       1.1       cgd 
    542       1.1       cgd 	default:
    543       1.1       cgd 		switch (slrflag) {
    544       1.1       cgd 			case 0:
    545       1.1       cgd 				slrflag++;
    546       1.1       cgd 				break;
    547       1.1       cgd 			case 1:
    548       1.1       cgd 				slrflag++;
    549       1.1       cgd 				altarg = sb;
    550       1.1       cgd 				break;
    551       1.1       cgd 			default:
    552       1.1       cgd 				break;
    553       1.1       cgd 		}
    554       1.1       cgd 		goto S1;
    555       1.1       cgd 	}
    556       1.1       cgd 
    557       1.1       cgd S1:
    558       1.1       cgd 	switch (*sb) {
    559       1.1       cgd 
    560       1.1       cgd 	case ' ':
    561       1.1       cgd 	case '\t':
    562       1.1       cgd 	case '\0':
    563       1.1       cgd 		goto OUT;	/* end of token */
    564       1.1       cgd 
    565       1.1       cgd 	case '\\':
    566      1.16     lukem 		INC_CHKCURSOR(sb);
    567      1.16     lukem 		goto S2;	/* slurp next character */
    568       1.1       cgd 
    569       1.1       cgd 	case '"':
    570      1.16     lukem 		INC_CHKCURSOR(sb);
    571      1.16     lukem 		goto S3;	/* slurp quoted string */
    572       1.1       cgd 
    573       1.1       cgd 	default:
    574      1.16     lukem 		*ap = *sb;	/* add character to token */
    575      1.16     lukem 		ap++;
    576      1.16     lukem 		INC_CHKCURSOR(sb);
    577       1.1       cgd 		got_one = 1;
    578       1.1       cgd 		goto S1;
    579       1.1       cgd 	}
    580       1.1       cgd 
    581       1.1       cgd S2:
    582       1.1       cgd 	switch (*sb) {
    583       1.1       cgd 
    584       1.1       cgd 	case '\0':
    585       1.1       cgd 		goto OUT;
    586       1.1       cgd 
    587       1.1       cgd 	default:
    588      1.16     lukem 		*ap = *sb;
    589      1.16     lukem 		ap++;
    590      1.16     lukem 		INC_CHKCURSOR(sb);
    591       1.1       cgd 		got_one = 1;
    592       1.1       cgd 		goto S1;
    593       1.1       cgd 	}
    594       1.1       cgd 
    595       1.1       cgd S3:
    596       1.1       cgd 	switch (*sb) {
    597       1.1       cgd 
    598       1.1       cgd 	case '\0':
    599       1.1       cgd 		goto OUT;
    600       1.1       cgd 
    601       1.1       cgd 	case '"':
    602      1.16     lukem 		INC_CHKCURSOR(sb);
    603      1.16     lukem 		goto S1;
    604       1.1       cgd 
    605       1.1       cgd 	default:
    606      1.16     lukem 		*ap = *sb;
    607      1.16     lukem 		ap++;
    608      1.16     lukem 		INC_CHKCURSOR(sb);
    609       1.1       cgd 		got_one = 1;
    610       1.1       cgd 		goto S3;
    611       1.1       cgd 	}
    612       1.1       cgd 
    613       1.1       cgd OUT:
    614       1.1       cgd 	if (got_one)
    615       1.1       cgd 		*ap++ = '\0';
    616       1.1       cgd 	argbase = ap;			/* update storage pointer */
    617       1.1       cgd 	stringbase = sb;		/* update scan pointer */
    618       1.1       cgd 	if (got_one) {
    619       1.3       cgd 		return (tmp);
    620       1.1       cgd 	}
    621       1.1       cgd 	switch (slrflag) {
    622       1.1       cgd 		case 0:
    623       1.1       cgd 			slrflag++;
    624       1.1       cgd 			break;
    625       1.1       cgd 		case 1:
    626       1.1       cgd 			slrflag++;
    627       1.1       cgd 			altarg = (char *) 0;
    628       1.1       cgd 			break;
    629       1.1       cgd 		default:
    630       1.1       cgd 			break;
    631       1.1       cgd 	}
    632       1.3       cgd 	return ((char *)0);
    633       1.1       cgd }
    634       1.1       cgd 
    635       1.1       cgd /*
    636       1.1       cgd  * Help command.
    637       1.1       cgd  * Call each command handler with argc == 0 and argv[0] == name.
    638       1.1       cgd  */
    639       1.3       cgd void
    640       1.1       cgd help(argc, argv)
    641       1.1       cgd 	int argc;
    642       1.1       cgd 	char *argv[];
    643       1.1       cgd {
    644       1.3       cgd 	struct cmd *c;
    645       1.1       cgd 
    646       1.1       cgd 	if (argc == 1) {
    647      1.16     lukem 		StringList *buf;
    648       1.1       cgd 
    649      1.16     lukem 		buf = sl_init();
    650      1.16     lukem 		printf("%sommands may be abbreviated.  Commands are:\n\n",
    651      1.16     lukem 		    proxy ? "Proxy c" : "C");
    652      1.16     lukem 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
    653      1.16     lukem 			if (c->c_name && (!proxy || c->c_proxy))
    654      1.16     lukem 				sl_add(buf, c->c_name);
    655      1.16     lukem 		list_vertical(buf);
    656      1.16     lukem 		sl_free(buf, 0);
    657       1.1       cgd 		return;
    658       1.1       cgd 	}
    659      1.16     lukem 
    660      1.18     lukem #define HELPINDENT ((int) sizeof("disconnect"))
    661      1.16     lukem 
    662       1.1       cgd 	while (--argc > 0) {
    663       1.3       cgd 		char *arg;
    664      1.16     lukem 
    665       1.1       cgd 		arg = *++argv;
    666       1.1       cgd 		c = getcmd(arg);
    667       1.1       cgd 		if (c == (struct cmd *)-1)
    668       1.1       cgd 			printf("?Ambiguous help command %s\n", arg);
    669       1.1       cgd 		else if (c == (struct cmd *)0)
    670       1.1       cgd 			printf("?Invalid help command %s\n", arg);
    671       1.1       cgd 		else
    672       1.1       cgd 			printf("%-*s\t%s\n", HELPINDENT,
    673       1.1       cgd 				c->c_name, c->c_help);
    674       1.1       cgd 	}
    675      1.12     lukem }
    676      1.12     lukem 
    677      1.14     lukem void
    678      1.14     lukem usage()
    679      1.14     lukem {
    680      1.14     lukem 	(void)fprintf(stderr,
    681      1.18     lukem 	    "usage: %s [-adeginptvV] [host [port]]\n"
    682      1.16     lukem 	    "       %s host:path[/]\n"
    683      1.16     lukem 	    "       %s ftp://host[:port]/path[/]\n"
    684      1.16     lukem 	    "       %s http://host[:port]/file\n",
    685      1.16     lukem 	    __progname, __progname, __progname, __progname);
    686      1.14     lukem 	exit(1);
    687       1.1       cgd }
    688