17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0b6880ccSsp149894 * Common Development and Distribution License (the "License"). 6*0b6880ccSsp149894 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*0b6880ccSsp149894 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * FTP User Program -- Command Interface. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate #define EXTERN 457c478bd9Sstevel@tonic-gate #include "ftp_var.h" 467c478bd9Sstevel@tonic-gate #include <deflt.h> /* macros that make using libcmd easier */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate static void usage(void); 497c478bd9Sstevel@tonic-gate static void timeout_sig(int sig); 507c478bd9Sstevel@tonic-gate static void cmdscanner(int top); 517c478bd9Sstevel@tonic-gate static void intr(int sig); 527c478bd9Sstevel@tonic-gate static char *slurpstring(void); 537c478bd9Sstevel@tonic-gate extern int use_eprt; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate boolean_t ls_invokes_NLST = B_TRUE; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h> 587c478bd9Sstevel@tonic-gate #include <gssapi/gssapi_ext.h> 59*0b6880ccSsp149894 #define GETOPT_STR "dginpstvET:axfm:" 60*0b6880ccSsp149894 #define USAGE_STR "[-adfginpstvx] [-m mech] [-T timeout] " \ 617c478bd9Sstevel@tonic-gate "[hostname [port]]" 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate int 647c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate char *cp; 677c478bd9Sstevel@tonic-gate int c, top; 687c478bd9Sstevel@tonic-gate struct passwd *pw = NULL; 697c478bd9Sstevel@tonic-gate char homedir[MAXPATHLEN]; 707c478bd9Sstevel@tonic-gate char *temp_string = NULL; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate buf = (char *)memalign(getpagesize(), FTPBUFSIZ); 757c478bd9Sstevel@tonic-gate if (buf == NULL) { 767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ftp: memory allocation failed\n"); 777c478bd9Sstevel@tonic-gate return (1); 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate timeoutms = timeout = 0; 817c478bd9Sstevel@tonic-gate doglob = 1; 827c478bd9Sstevel@tonic-gate interactive = 1; 837c478bd9Sstevel@tonic-gate autologin = 1; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate autoauth = 0; 86*0b6880ccSsp149894 /* by default SYST command will be sent to determine system type */ 87*0b6880ccSsp149894 skipsyst = 0; 887c478bd9Sstevel@tonic-gate fflag = 0; 897c478bd9Sstevel@tonic-gate autoencrypt = 0; 907c478bd9Sstevel@tonic-gate goteof = 0; 917c478bd9Sstevel@tonic-gate mechstr[0] = '\0'; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate sendport = -1; /* tri-state variable. start out in "automatic" mode. */ 947c478bd9Sstevel@tonic-gate passivemode = 0; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, GETOPT_STR)) != EOF) { 977c478bd9Sstevel@tonic-gate switch (c) { 987c478bd9Sstevel@tonic-gate case 'd': 997c478bd9Sstevel@tonic-gate options |= SO_DEBUG; 1007c478bd9Sstevel@tonic-gate debug++; 1017c478bd9Sstevel@tonic-gate break; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate case 'g': 1047c478bd9Sstevel@tonic-gate doglob = 0; 1057c478bd9Sstevel@tonic-gate break; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate case 'i': 1087c478bd9Sstevel@tonic-gate interactive = 0; 1097c478bd9Sstevel@tonic-gate break; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate case 'n': 1127c478bd9Sstevel@tonic-gate autologin = 0; 1137c478bd9Sstevel@tonic-gate break; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate case 'p': 1167c478bd9Sstevel@tonic-gate passivemode = 1; 1177c478bd9Sstevel@tonic-gate break; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate case 't': 1207c478bd9Sstevel@tonic-gate trace++; 1217c478bd9Sstevel@tonic-gate break; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate case 'v': 1247c478bd9Sstevel@tonic-gate verbose++; 1257c478bd9Sstevel@tonic-gate break; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* undocumented option: allows testing of EPRT */ 1287c478bd9Sstevel@tonic-gate case 'E': 1297c478bd9Sstevel@tonic-gate use_eprt = 1; 1307c478bd9Sstevel@tonic-gate break; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate case 'T': 1337c478bd9Sstevel@tonic-gate if (!isdigit(*optarg)) { 1347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1357c478bd9Sstevel@tonic-gate "ftp: bad timeout: \"%s\"\n", optarg); 1367c478bd9Sstevel@tonic-gate break; 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate timeout = atoi(optarg); 1397c478bd9Sstevel@tonic-gate timeoutms = timeout * MILLISEC; 1407c478bd9Sstevel@tonic-gate break; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate case 'a': 1437c478bd9Sstevel@tonic-gate autoauth = 1; 1447c478bd9Sstevel@tonic-gate break; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate case 'f': 1477c478bd9Sstevel@tonic-gate autoauth = 1; 1487c478bd9Sstevel@tonic-gate fflag = 1; 1497c478bd9Sstevel@tonic-gate break; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate case 'm': 1527c478bd9Sstevel@tonic-gate autoauth = 1; 1537c478bd9Sstevel@tonic-gate call(setmech, "ftp", optarg, 0); 1547c478bd9Sstevel@tonic-gate if (code != 0) 1557c478bd9Sstevel@tonic-gate exit(1); 1567c478bd9Sstevel@tonic-gate break; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate case 'x': 1597c478bd9Sstevel@tonic-gate autoauth = 1; 1607c478bd9Sstevel@tonic-gate autoencrypt = 1; 1617c478bd9Sstevel@tonic-gate break; 1627c478bd9Sstevel@tonic-gate 163*0b6880ccSsp149894 case 's': 164*0b6880ccSsp149894 skipsyst = 1; 165*0b6880ccSsp149894 break; 166*0b6880ccSsp149894 1677c478bd9Sstevel@tonic-gate case '?': 1687c478bd9Sstevel@tonic-gate default: 1697c478bd9Sstevel@tonic-gate usage(); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate argc -= optind; 1737c478bd9Sstevel@tonic-gate argv += optind; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (argc > 2) 1767c478bd9Sstevel@tonic-gate usage(); 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate fromatty = isatty(fileno(stdin)); 1797c478bd9Sstevel@tonic-gate /* 1807c478bd9Sstevel@tonic-gate * Scan env, then DEFAULTFTPFILE 1817c478bd9Sstevel@tonic-gate * for FTP_LS_SENDS_NLST 1827c478bd9Sstevel@tonic-gate */ 1837c478bd9Sstevel@tonic-gate temp_string = getenv("FTP_LS_SENDS_NLST"); 1847c478bd9Sstevel@tonic-gate if (temp_string == NULL) { /* env var not set */ 1857c478bd9Sstevel@tonic-gate if (defopen(DEFAULTFTPFILE) == 0) { 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * turn off case sensitivity 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate int flags = defcntl(DC_GETFLAGS, 0); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate TURNOFF(flags, DC_CASE); 1927c478bd9Sstevel@tonic-gate (void) defcntl(DC_SETFLAGS, flags); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate temp_string = defread("FTP_LS_SENDS_NLST="); 1957c478bd9Sstevel@tonic-gate (void) defopen(NULL); /* close default file */ 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate if (temp_string != NULL && 1997c478bd9Sstevel@tonic-gate strncasecmp(temp_string, "n", 1) == 0) 2007c478bd9Sstevel@tonic-gate ls_invokes_NLST = B_FALSE; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * Set up defaults for FTP. 2047c478bd9Sstevel@tonic-gate */ 2057c478bd9Sstevel@tonic-gate (void) strcpy(typename, "ascii"), type = TYPE_A; 2067c478bd9Sstevel@tonic-gate (void) strcpy(formname, "non-print"), form = FORM_N; 2077c478bd9Sstevel@tonic-gate (void) strcpy(modename, "stream"), mode = MODE_S; 2087c478bd9Sstevel@tonic-gate (void) strcpy(structname, "file"), stru = STRU_F; 2097c478bd9Sstevel@tonic-gate (void) strcpy(bytename, "8"), bytesize = 8; 2107c478bd9Sstevel@tonic-gate if (fromatty) 2117c478bd9Sstevel@tonic-gate verbose++; 2127c478bd9Sstevel@tonic-gate cpend = 0; /* no pending replies */ 2137c478bd9Sstevel@tonic-gate proxy = 0; /* proxy not active */ 2147c478bd9Sstevel@tonic-gate crflag = 1; /* strip c.r. on ascii gets */ 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if (mechstr[0] == '\0') { 2177c478bd9Sstevel@tonic-gate strlcpy(mechstr, FTP_DEF_MECH, MECH_SZ); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * Set up the home directory in case we're globbing. 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate cp = getlogin(); 2247c478bd9Sstevel@tonic-gate if (cp != NULL) { 2257c478bd9Sstevel@tonic-gate pw = getpwnam(cp); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate if (pw == NULL) 2287c478bd9Sstevel@tonic-gate pw = getpwuid(getuid()); 2297c478bd9Sstevel@tonic-gate if (pw != NULL) { 2307c478bd9Sstevel@tonic-gate home = homedir; 2317c478bd9Sstevel@tonic-gate (void) strcpy(home, pw->pw_dir); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate if (setjmp(timeralarm)) { 2347c478bd9Sstevel@tonic-gate (void) fflush(stdout); 2357c478bd9Sstevel@tonic-gate (void) printf("Connection timeout\n"); 2367c478bd9Sstevel@tonic-gate exit(1); 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, timeout_sig); 2397c478bd9Sstevel@tonic-gate reset_timer(); 2407c478bd9Sstevel@tonic-gate if (argc > 0) { 2417c478bd9Sstevel@tonic-gate int nargc = 0; 2427c478bd9Sstevel@tonic-gate char *nargv[4]; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (setjmp(toplevel)) 2457c478bd9Sstevel@tonic-gate return (0); 2467c478bd9Sstevel@tonic-gate (void) signal(SIGINT, intr); 2477c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, lostpeer); 2487c478bd9Sstevel@tonic-gate nargv[nargc++] = "ftp"; 2497c478bd9Sstevel@tonic-gate nargv[nargc++] = argv[0]; /* hostname */ 2507c478bd9Sstevel@tonic-gate if (argc > 1) 2517c478bd9Sstevel@tonic-gate nargv[nargc++] = argv[1]; /* port */ 2527c478bd9Sstevel@tonic-gate nargv[nargc] = NULL; 2537c478bd9Sstevel@tonic-gate setpeer(nargc, nargv); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate top = setjmp(toplevel) == 0; 2567c478bd9Sstevel@tonic-gate if (top) { 2577c478bd9Sstevel@tonic-gate (void) signal(SIGINT, intr); 2587c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, lostpeer); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate for (;;) { 2627c478bd9Sstevel@tonic-gate cmdscanner(top); 2637c478bd9Sstevel@tonic-gate top = 1; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate static void 2687c478bd9Sstevel@tonic-gate usage(void) 2697c478bd9Sstevel@tonic-gate { 2707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "usage: ftp %s\n", USAGE_STR); 2717c478bd9Sstevel@tonic-gate exit(1); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate void 2757c478bd9Sstevel@tonic-gate reset_timer() 2767c478bd9Sstevel@tonic-gate { 2777c478bd9Sstevel@tonic-gate /* The test is just to reduce syscalls if timeouts aren't used */ 2787c478bd9Sstevel@tonic-gate if (timeout) 2797c478bd9Sstevel@tonic-gate alarm(timeout); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate void 2837c478bd9Sstevel@tonic-gate stop_timer() 2847c478bd9Sstevel@tonic-gate { 2857c478bd9Sstevel@tonic-gate if (timeout) 2867c478bd9Sstevel@tonic-gate alarm(0); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2907c478bd9Sstevel@tonic-gate static void 2917c478bd9Sstevel@tonic-gate timeout_sig(int sig) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate longjmp(timeralarm, 1); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2977c478bd9Sstevel@tonic-gate static void 2987c478bd9Sstevel@tonic-gate intr(int sig) 2997c478bd9Sstevel@tonic-gate { 3007c478bd9Sstevel@tonic-gate longjmp(toplevel, 1); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3047c478bd9Sstevel@tonic-gate void 3057c478bd9Sstevel@tonic-gate lostpeer(int sig) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate extern FILE *ctrl_out; 3087c478bd9Sstevel@tonic-gate extern int data; 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate if (connected) { 3117c478bd9Sstevel@tonic-gate if (ctrl_out != NULL) { 3127c478bd9Sstevel@tonic-gate (void) shutdown(fileno(ctrl_out), 1+1); 3137c478bd9Sstevel@tonic-gate (void) fclose(ctrl_out); 3147c478bd9Sstevel@tonic-gate ctrl_out = NULL; 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate if (data >= 0) { 3177c478bd9Sstevel@tonic-gate (void) shutdown(data, 1+1); 3187c478bd9Sstevel@tonic-gate (void) close(data); 3197c478bd9Sstevel@tonic-gate data = -1; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate connected = 0; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate auth_type = AUTHTYPE_NONE; 3247c478bd9Sstevel@tonic-gate clevel = dlevel = PROT_C; 3257c478bd9Sstevel@tonic-gate goteof = 0; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate pswitch(1); 3287c478bd9Sstevel@tonic-gate if (connected) { 3297c478bd9Sstevel@tonic-gate if (ctrl_out != NULL) { 3307c478bd9Sstevel@tonic-gate (void) shutdown(fileno(ctrl_out), 1+1); 3317c478bd9Sstevel@tonic-gate (void) fclose(ctrl_out); 3327c478bd9Sstevel@tonic-gate ctrl_out = NULL; 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate connected = 0; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate auth_type = AUTHTYPE_NONE; 3377c478bd9Sstevel@tonic-gate clevel = dlevel = PROT_C; 3387c478bd9Sstevel@tonic-gate goteof = 0; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate proxflag = 0; 3417c478bd9Sstevel@tonic-gate pswitch(0); 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate * Command parser. 3467c478bd9Sstevel@tonic-gate */ 3477c478bd9Sstevel@tonic-gate static void 3487c478bd9Sstevel@tonic-gate cmdscanner(int top) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate struct cmd *c; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if (!top) 3537c478bd9Sstevel@tonic-gate (void) putchar('\n'); 3547c478bd9Sstevel@tonic-gate for (;;) { 3557c478bd9Sstevel@tonic-gate stop_timer(); 3567c478bd9Sstevel@tonic-gate if (fromatty) { 3577c478bd9Sstevel@tonic-gate (void) printf("ftp> "); 3587c478bd9Sstevel@tonic-gate (void) fflush(stdout); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate if (fgets(line, sizeof (line), stdin) == 0) { 3617c478bd9Sstevel@tonic-gate if (feof(stdin) || ferror(stdin)) 3627c478bd9Sstevel@tonic-gate quit(0, NULL); 3637c478bd9Sstevel@tonic-gate break; 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate if (line[0] == 0) 3667c478bd9Sstevel@tonic-gate break; 3677c478bd9Sstevel@tonic-gate /* If not all, just discard rest of line */ 3687c478bd9Sstevel@tonic-gate if (line[strlen(line)-1] != '\n') { 3697c478bd9Sstevel@tonic-gate while (fgetc(stdin) != '\n' && !feof(stdin) && 3707c478bd9Sstevel@tonic-gate !ferror(stdin)) 3717c478bd9Sstevel@tonic-gate ; 3727c478bd9Sstevel@tonic-gate (void) printf("Line too long\n"); 3737c478bd9Sstevel@tonic-gate continue; 3747c478bd9Sstevel@tonic-gate } else 3757c478bd9Sstevel@tonic-gate line[strlen(line)-1] = 0; 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate makeargv(); 3787c478bd9Sstevel@tonic-gate if (margc == 0) { 3797c478bd9Sstevel@tonic-gate continue; 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate c = getcmd(margv[0]); 3827c478bd9Sstevel@tonic-gate if (c == (struct cmd *)-1) { 3837c478bd9Sstevel@tonic-gate (void) printf("?Ambiguous command\n"); 3847c478bd9Sstevel@tonic-gate continue; 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate if (c == 0) { 3877c478bd9Sstevel@tonic-gate (void) printf("?Invalid command\n"); 3887c478bd9Sstevel@tonic-gate continue; 3897c478bd9Sstevel@tonic-gate } 3907c478bd9Sstevel@tonic-gate if (c->c_conn && !connected) { 3917c478bd9Sstevel@tonic-gate (void) printf("Not connected.\n"); 3927c478bd9Sstevel@tonic-gate continue; 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate reset_timer(); 3957c478bd9Sstevel@tonic-gate (*c->c_handler)(margc, margv); 3967c478bd9Sstevel@tonic-gate #ifndef CTRL 3977c478bd9Sstevel@tonic-gate #define CTRL(c) ((c)&037) 3987c478bd9Sstevel@tonic-gate #endif 3997c478bd9Sstevel@tonic-gate stop_timer(); 4007c478bd9Sstevel@tonic-gate if (bell && c->c_bell) 4017c478bd9Sstevel@tonic-gate (void) putchar(CTRL('g')); 4027c478bd9Sstevel@tonic-gate if (c->c_handler != help) 4037c478bd9Sstevel@tonic-gate break; 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate (void) signal(SIGINT, intr); 4067c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, lostpeer); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate struct cmd * 4107c478bd9Sstevel@tonic-gate getcmd(char *name) 4117c478bd9Sstevel@tonic-gate { 4127c478bd9Sstevel@tonic-gate char *p, *q; 4137c478bd9Sstevel@tonic-gate struct cmd *c, *found; 4147c478bd9Sstevel@tonic-gate int nmatches, longest; 4157c478bd9Sstevel@tonic-gate extern struct cmd cmdtab[]; 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (name == NULL) 4187c478bd9Sstevel@tonic-gate return (0); 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate longest = 0; 4217c478bd9Sstevel@tonic-gate nmatches = 0; 4227c478bd9Sstevel@tonic-gate found = 0; 4237c478bd9Sstevel@tonic-gate for (c = cmdtab; (p = c->c_name) != NULL; c++) { 4247c478bd9Sstevel@tonic-gate for (q = name; *q == *p++; q++) 4257c478bd9Sstevel@tonic-gate if (*q == 0) /* exact match? */ 4267c478bd9Sstevel@tonic-gate return (c); 4277c478bd9Sstevel@tonic-gate if (!*q) { /* the name was a prefix */ 4287c478bd9Sstevel@tonic-gate if (q - name > longest) { 4297c478bd9Sstevel@tonic-gate longest = q - name; 4307c478bd9Sstevel@tonic-gate nmatches = 1; 4317c478bd9Sstevel@tonic-gate found = c; 4327c478bd9Sstevel@tonic-gate } else if (q - name == longest) 4337c478bd9Sstevel@tonic-gate nmatches++; 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate if (nmatches > 1) 4377c478bd9Sstevel@tonic-gate return ((struct cmd *)-1); 4387c478bd9Sstevel@tonic-gate return (found); 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate /* 4427c478bd9Sstevel@tonic-gate * Slice a string up into argc/argv. 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate static int slrflag; 4467c478bd9Sstevel@tonic-gate #define MARGV_INC 20 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate void 4497c478bd9Sstevel@tonic-gate makeargv(void) 4507c478bd9Sstevel@tonic-gate { 4517c478bd9Sstevel@tonic-gate char **argp; 4527c478bd9Sstevel@tonic-gate static int margv_size; 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate margc = 0; 4557c478bd9Sstevel@tonic-gate stringbase = line; /* scan from first of buffer */ 4567c478bd9Sstevel@tonic-gate argbase = argbuf; /* store from first of buffer */ 4577c478bd9Sstevel@tonic-gate slrflag = 0; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (!margv) { 4607c478bd9Sstevel@tonic-gate margv_size = MARGV_INC; 4617c478bd9Sstevel@tonic-gate if ((margv = malloc(margv_size * sizeof (char *))) == NULL) 4627c478bd9Sstevel@tonic-gate fatal("Out of memory"); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate argp = margv; 4657c478bd9Sstevel@tonic-gate while (*argp++ = slurpstring()) { 4667c478bd9Sstevel@tonic-gate margc++; 4677c478bd9Sstevel@tonic-gate if (margc == margv_size) { 4687c478bd9Sstevel@tonic-gate margv_size += MARGV_INC; 4697c478bd9Sstevel@tonic-gate if ((margv = realloc(margv, 4707c478bd9Sstevel@tonic-gate margv_size * sizeof (char *))) == NULL) 4717c478bd9Sstevel@tonic-gate fatal("Out of memory"); 4727c478bd9Sstevel@tonic-gate argp = margv + margc; 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate /* 4787c478bd9Sstevel@tonic-gate * Parse string into argbuf; 4797c478bd9Sstevel@tonic-gate * implemented with FSM to 4807c478bd9Sstevel@tonic-gate * handle quoting and strings 4817c478bd9Sstevel@tonic-gate */ 4827c478bd9Sstevel@tonic-gate static char * 4837c478bd9Sstevel@tonic-gate slurpstring(void) 4847c478bd9Sstevel@tonic-gate { 4857c478bd9Sstevel@tonic-gate int got_one = 0; 4867c478bd9Sstevel@tonic-gate char *sb = stringbase; 4877c478bd9Sstevel@tonic-gate char *ap = argbase; 4887c478bd9Sstevel@tonic-gate char *tmp = argbase; /* will return this if token found */ 4897c478bd9Sstevel@tonic-gate int len; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */ 4927c478bd9Sstevel@tonic-gate switch (slrflag) { /* and $ as token for macro invoke */ 4937c478bd9Sstevel@tonic-gate case 0: 4947c478bd9Sstevel@tonic-gate slrflag++; 4957c478bd9Sstevel@tonic-gate stringbase++; 4967c478bd9Sstevel@tonic-gate return ((*sb == '!') ? "!" : "$"); 4977c478bd9Sstevel@tonic-gate case 1: 4987c478bd9Sstevel@tonic-gate slrflag++; 4997c478bd9Sstevel@tonic-gate altarg = stringbase; 5007c478bd9Sstevel@tonic-gate break; 5017c478bd9Sstevel@tonic-gate default: 5027c478bd9Sstevel@tonic-gate break; 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate S0: 5077c478bd9Sstevel@tonic-gate switch (*sb) { 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate case '\0': 5107c478bd9Sstevel@tonic-gate goto OUT; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate case ' ': 5137c478bd9Sstevel@tonic-gate case '\t': 5147c478bd9Sstevel@tonic-gate sb++; goto S0; 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate default: 5177c478bd9Sstevel@tonic-gate switch (slrflag) { 5187c478bd9Sstevel@tonic-gate case 0: 5197c478bd9Sstevel@tonic-gate slrflag++; 5207c478bd9Sstevel@tonic-gate break; 5217c478bd9Sstevel@tonic-gate case 1: 5227c478bd9Sstevel@tonic-gate slrflag++; 5237c478bd9Sstevel@tonic-gate altarg = sb; 5247c478bd9Sstevel@tonic-gate break; 5257c478bd9Sstevel@tonic-gate default: 5267c478bd9Sstevel@tonic-gate break; 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate goto S1; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate S1: 5327c478bd9Sstevel@tonic-gate switch (*sb) { 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate case ' ': 5357c478bd9Sstevel@tonic-gate case '\t': 5367c478bd9Sstevel@tonic-gate case '\0': 5377c478bd9Sstevel@tonic-gate goto OUT; /* end of token */ 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate case '\\': 5407c478bd9Sstevel@tonic-gate sb++; goto S2; /* slurp next character */ 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate case '"': 5437c478bd9Sstevel@tonic-gate sb++; goto S3; /* slurp quoted string */ 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate default: 5467c478bd9Sstevel@tonic-gate if ((len = mblen(sb, MB_CUR_MAX)) <= 0) 5477c478bd9Sstevel@tonic-gate len = 1; 5487c478bd9Sstevel@tonic-gate memcpy(ap, sb, len); 5497c478bd9Sstevel@tonic-gate ap += len; 5507c478bd9Sstevel@tonic-gate sb += len; 5517c478bd9Sstevel@tonic-gate got_one = 1; 5527c478bd9Sstevel@tonic-gate goto S1; 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate S2: 5567c478bd9Sstevel@tonic-gate switch (*sb) { 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate case '\0': 5597c478bd9Sstevel@tonic-gate goto OUT; 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate default: 5627c478bd9Sstevel@tonic-gate if ((len = mblen(sb, MB_CUR_MAX)) <= 0) 5637c478bd9Sstevel@tonic-gate len = 1; 5647c478bd9Sstevel@tonic-gate memcpy(ap, sb, len); 5657c478bd9Sstevel@tonic-gate ap += len; 5667c478bd9Sstevel@tonic-gate sb += len; 5677c478bd9Sstevel@tonic-gate got_one = 1; 5687c478bd9Sstevel@tonic-gate goto S1; 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate S3: 5727c478bd9Sstevel@tonic-gate switch (*sb) { 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate case '\0': 5757c478bd9Sstevel@tonic-gate goto OUT; 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate case '"': 5787c478bd9Sstevel@tonic-gate sb++; goto S1; 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate default: 5817c478bd9Sstevel@tonic-gate if ((len = mblen(sb, MB_CUR_MAX)) <= 0) 5827c478bd9Sstevel@tonic-gate len = 1; 5837c478bd9Sstevel@tonic-gate memcpy(ap, sb, len); 5847c478bd9Sstevel@tonic-gate ap += len; 5857c478bd9Sstevel@tonic-gate sb += len; 5867c478bd9Sstevel@tonic-gate got_one = 1; 5877c478bd9Sstevel@tonic-gate goto S3; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate OUT: 5917c478bd9Sstevel@tonic-gate if (got_one) 5927c478bd9Sstevel@tonic-gate *ap++ = '\0'; 5937c478bd9Sstevel@tonic-gate argbase = ap; /* update storage pointer */ 5947c478bd9Sstevel@tonic-gate stringbase = sb; /* update scan pointer */ 5957c478bd9Sstevel@tonic-gate if (got_one) { 5967c478bd9Sstevel@tonic-gate return (tmp); 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate switch (slrflag) { 5997c478bd9Sstevel@tonic-gate case 0: 6007c478bd9Sstevel@tonic-gate slrflag++; 6017c478bd9Sstevel@tonic-gate break; 6027c478bd9Sstevel@tonic-gate case 1: 6037c478bd9Sstevel@tonic-gate slrflag++; 6047c478bd9Sstevel@tonic-gate altarg = (char *)0; 6057c478bd9Sstevel@tonic-gate break; 6067c478bd9Sstevel@tonic-gate default: 6077c478bd9Sstevel@tonic-gate break; 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate return ((char *)0); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate #define HELPINDENT (sizeof ("directory")) 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * Help command. 6167c478bd9Sstevel@tonic-gate * Call each command handler with argc == 0 and argv[0] == name. 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate void 6197c478bd9Sstevel@tonic-gate help(int argc, char *argv[]) 6207c478bd9Sstevel@tonic-gate { 6217c478bd9Sstevel@tonic-gate struct cmd *c; 6227c478bd9Sstevel@tonic-gate extern struct cmd cmdtab[]; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate if (argc == 1) { 6257c478bd9Sstevel@tonic-gate int i, j, w, k; 6267c478bd9Sstevel@tonic-gate int columns, width = 0, lines; 6277c478bd9Sstevel@tonic-gate extern int NCMDS; 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate (void) printf( 6307c478bd9Sstevel@tonic-gate "Commands may be abbreviated. Commands are:\n\n"); 6317c478bd9Sstevel@tonic-gate for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 6327c478bd9Sstevel@tonic-gate int len = strlen(c->c_name); 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate if (len > width) 6357c478bd9Sstevel@tonic-gate width = len; 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate width = (width + 8) &~ 7; 6387c478bd9Sstevel@tonic-gate columns = 80 / width; 6397c478bd9Sstevel@tonic-gate if (columns == 0) 6407c478bd9Sstevel@tonic-gate columns = 1; 6417c478bd9Sstevel@tonic-gate lines = (NCMDS + columns - 1) / columns; 6427c478bd9Sstevel@tonic-gate for (i = 0; i < lines; i++) { 6437c478bd9Sstevel@tonic-gate for (j = 0; j < columns; j++) { 6447c478bd9Sstevel@tonic-gate c = cmdtab + j * lines + i; 6457c478bd9Sstevel@tonic-gate if (c->c_name && (!proxy || c->c_proxy)) { 6467c478bd9Sstevel@tonic-gate (void) printf("%s", c->c_name); 6477c478bd9Sstevel@tonic-gate } else if (c->c_name) { 6487c478bd9Sstevel@tonic-gate for (k = 0; k < strlen(c->c_name); 6497c478bd9Sstevel@tonic-gate k++) { 6507c478bd9Sstevel@tonic-gate (void) putchar(' '); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate if (c + lines >= &cmdtab[NCMDS]) { 6547c478bd9Sstevel@tonic-gate (void) printf("\n"); 6557c478bd9Sstevel@tonic-gate break; 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate w = strlen(c->c_name); 6587c478bd9Sstevel@tonic-gate while (w < width) { 6597c478bd9Sstevel@tonic-gate w = (w + 8) &~ 7; 6607c478bd9Sstevel@tonic-gate (void) putchar('\t'); 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate return; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate while (--argc > 0) { 6677c478bd9Sstevel@tonic-gate char *arg; 6687c478bd9Sstevel@tonic-gate arg = *++argv; 6697c478bd9Sstevel@tonic-gate c = getcmd(arg); 6707c478bd9Sstevel@tonic-gate if (c == (struct cmd *)-1) 6717c478bd9Sstevel@tonic-gate (void) printf("?Ambiguous help command %s\n", arg); 6727c478bd9Sstevel@tonic-gate else if (c == (struct cmd *)0) 6737c478bd9Sstevel@tonic-gate (void) printf("?Invalid help command %s\n", arg); 6747c478bd9Sstevel@tonic-gate else 6757c478bd9Sstevel@tonic-gate (void) printf("%-*s\t%s\n", HELPINDENT, 6767c478bd9Sstevel@tonic-gate c->c_name, c->c_help); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* 6817c478bd9Sstevel@tonic-gate * Call routine with argc, argv set from args (terminated by 0). 6827c478bd9Sstevel@tonic-gate */ 6837c478bd9Sstevel@tonic-gate void 6847c478bd9Sstevel@tonic-gate call(void (*routine)(int argc, char *argv[]), ...) 6857c478bd9Sstevel@tonic-gate { 6867c478bd9Sstevel@tonic-gate va_list ap; 6877c478bd9Sstevel@tonic-gate char *argv[10]; 6887c478bd9Sstevel@tonic-gate int argc = 0; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate va_start(ap, routine); 6917c478bd9Sstevel@tonic-gate while ((argv[argc] = va_arg(ap, char *)) != (char *)0) 6927c478bd9Sstevel@tonic-gate argc++; 6937c478bd9Sstevel@tonic-gate va_end(ap); 6947c478bd9Sstevel@tonic-gate (*routine)(argc, argv); 6957c478bd9Sstevel@tonic-gate } 696