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