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