19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 35fd129a02SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 388049f797SMark Murray #endif 399b50d902SRodney W. Grimes 408f4c8256SDavid Malone #if 0 419b50d902SRodney W. Grimes #ifndef lint 428f4c8256SDavid Malone static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 43fd129a02SPhilippe Charnier #endif 448f4c8256SDavid Malone #endif 458f4c8256SDavid Malone 468f4c8256SDavid Malone #include <sys/cdefs.h> 478f4c8256SDavid Malone __FBSDID("$FreeBSD$"); 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes /* 529b50d902SRodney W. Grimes * TFTP User Program -- Command Interface. 539b50d902SRodney W. Grimes */ 54fd129a02SPhilippe Charnier #include <sys/param.h> 559b50d902SRodney W. Grimes #include <sys/types.h> 569b50d902SRodney W. Grimes #include <sys/socket.h> 579b50d902SRodney W. Grimes #include <sys/file.h> 58a716ad66SWarner Losh #include <sys/param.h> 599b50d902SRodney W. Grimes 609b50d902SRodney W. Grimes #include <netinet/in.h> 619b50d902SRodney W. Grimes 629b50d902SRodney W. Grimes #include <arpa/inet.h> 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes #include <ctype.h> 65fd129a02SPhilippe Charnier #include <err.h> 668979160aSBruce Evans #include <histedit.h> 679b50d902SRodney W. Grimes #include <netdb.h> 689b50d902SRodney W. Grimes #include <setjmp.h> 699b50d902SRodney W. Grimes #include <signal.h> 709b50d902SRodney W. Grimes #include <stdio.h> 719b50d902SRodney W. Grimes #include <stdlib.h> 729b50d902SRodney W. Grimes #include <string.h> 739b50d902SRodney W. Grimes #include <unistd.h> 749b50d902SRodney W. Grimes 759b50d902SRodney W. Grimes #include "extern.h" 769b50d902SRodney W. Grimes 77c33ed450SMatthew N. Dodd #define MAXLINE 200 788979160aSBruce Evans #define TIMEOUT 5 /* secs between rexmt's */ 79c33ed450SMatthew N. Dodd 804dac6235SHajimu UMEMOTO struct sockaddr_storage peeraddr; 819b50d902SRodney W. Grimes int f; 829b50d902SRodney W. Grimes int trace; 839b50d902SRodney W. Grimes int verbose; 849b50d902SRodney W. Grimes int connected; 859b50d902SRodney W. Grimes char mode[32]; 86c33ed450SMatthew N. Dodd char line[MAXLINE]; 879b50d902SRodney W. Grimes int margc; 889b50d902SRodney W. Grimes char *margv[20]; 899b50d902SRodney W. Grimes jmp_buf toplevel; 909b50d902SRodney W. Grimes 913f330d7dSWarner Losh void get(int, char **); 923f330d7dSWarner Losh void help(int, char **); 933f330d7dSWarner Losh void intr(int); 943f330d7dSWarner Losh void modecmd(int, char **); 953f330d7dSWarner Losh void put(int, char **); 963f330d7dSWarner Losh void quit(int, char **); 973f330d7dSWarner Losh void setascii(int, char **); 983f330d7dSWarner Losh void setbinary(int, char **); 994dac6235SHajimu UMEMOTO void setpeer0(char *, char *); 1003f330d7dSWarner Losh void setpeer(int, char **); 1013f330d7dSWarner Losh void setrexmt(int, char **); 1023f330d7dSWarner Losh void settimeout(int, char **); 1033f330d7dSWarner Losh void settrace(int, char **); 1043f330d7dSWarner Losh void setverbose(int, char **); 1053f330d7dSWarner Losh void status(int, char **); 1069b50d902SRodney W. Grimes 1073f330d7dSWarner Losh static void command(void) __dead2; 1083f330d7dSWarner Losh static const char *command_prompt(void); 1099b50d902SRodney W. Grimes 1103f330d7dSWarner Losh static void getusage(char *); 1113f330d7dSWarner Losh static void makeargv(void); 1123f330d7dSWarner Losh static void putusage(char *); 1133f330d7dSWarner Losh static void settftpmode(const char *); 1148049f797SMark Murray 1153f330d7dSWarner Losh char *tail(char *); 1163f330d7dSWarner Losh struct cmd *getcmd(char *); 1179b50d902SRodney W. Grimes 1189b50d902SRodney W. Grimes #define HELPINDENT (sizeof("connect")) 1199b50d902SRodney W. Grimes 1209b50d902SRodney W. Grimes struct cmd { 1218049f797SMark Murray const char *name; 1229b50d902SRodney W. Grimes char *help; 1233f330d7dSWarner Losh void (*handler)(int, char **); 1249b50d902SRodney W. Grimes }; 1259b50d902SRodney W. Grimes 1269b50d902SRodney W. Grimes char vhelp[] = "toggle verbose mode"; 1279b50d902SRodney W. Grimes char thelp[] = "toggle packet tracing"; 1289b50d902SRodney W. Grimes char chelp[] = "connect to remote tftp"; 1299b50d902SRodney W. Grimes char qhelp[] = "exit tftp"; 1309b50d902SRodney W. Grimes char hhelp[] = "print help information"; 1319b50d902SRodney W. Grimes char shelp[] = "send file"; 1329b50d902SRodney W. Grimes char rhelp[] = "receive file"; 1339b50d902SRodney W. Grimes char mhelp[] = "set file transfer mode"; 1349b50d902SRodney W. Grimes char sthelp[] = "show current status"; 1359b50d902SRodney W. Grimes char xhelp[] = "set per-packet retransmission timeout"; 1369b50d902SRodney W. Grimes char ihelp[] = "set total retransmission timeout"; 1379b50d902SRodney W. Grimes char ashelp[] = "set mode to netascii"; 1389b50d902SRodney W. Grimes char bnhelp[] = "set mode to octet"; 1399b50d902SRodney W. Grimes 1409b50d902SRodney W. Grimes struct cmd cmdtab[] = { 1419b50d902SRodney W. Grimes { "connect", chelp, setpeer }, 1429b50d902SRodney W. Grimes { "mode", mhelp, modecmd }, 1439b50d902SRodney W. Grimes { "put", shelp, put }, 1449b50d902SRodney W. Grimes { "get", rhelp, get }, 1459b50d902SRodney W. Grimes { "quit", qhelp, quit }, 1469b50d902SRodney W. Grimes { "verbose", vhelp, setverbose }, 1479b50d902SRodney W. Grimes { "trace", thelp, settrace }, 1489b50d902SRodney W. Grimes { "status", sthelp, status }, 1499b50d902SRodney W. Grimes { "binary", bnhelp, setbinary }, 1509b50d902SRodney W. Grimes { "ascii", ashelp, setascii }, 1519b50d902SRodney W. Grimes { "rexmt", xhelp, setrexmt }, 1529b50d902SRodney W. Grimes { "timeout", ihelp, settimeout }, 1539b50d902SRodney W. Grimes { "?", hhelp, help }, 1548049f797SMark Murray { NULL, NULL, NULL } 1559b50d902SRodney W. Grimes }; 1569b50d902SRodney W. Grimes 1579b50d902SRodney W. Grimes int 1589b50d902SRodney W. Grimes main(argc, argv) 1599b50d902SRodney W. Grimes int argc; 1609b50d902SRodney W. Grimes char *argv[]; 1619b50d902SRodney W. Grimes { 1624dac6235SHajimu UMEMOTO f = -1; 1639b50d902SRodney W. Grimes strcpy(mode, "netascii"); 1649b50d902SRodney W. Grimes signal(SIGINT, intr); 1659b50d902SRodney W. Grimes if (argc > 1) { 1669b50d902SRodney W. Grimes if (setjmp(toplevel) != 0) 1679b50d902SRodney W. Grimes exit(0); 1689b50d902SRodney W. Grimes setpeer(argc, argv); 1699b50d902SRodney W. Grimes } 1709b50d902SRodney W. Grimes if (setjmp(toplevel) != 0) 1719b50d902SRodney W. Grimes (void)putchar('\n'); 1729b50d902SRodney W. Grimes command(); 1739b50d902SRodney W. Grimes } 1749b50d902SRodney W. Grimes 175fd129a02SPhilippe Charnier char hostname[MAXHOSTNAMELEN]; 1769b50d902SRodney W. Grimes 1779b50d902SRodney W. Grimes void 1784dac6235SHajimu UMEMOTO setpeer0(host, port) 1794dac6235SHajimu UMEMOTO char *host; 1804dac6235SHajimu UMEMOTO char *port; 1814dac6235SHajimu UMEMOTO { 1824dac6235SHajimu UMEMOTO struct addrinfo hints, *res0, *res; 1834dac6235SHajimu UMEMOTO int error; 1844dac6235SHajimu UMEMOTO struct sockaddr_storage ss; 1854dac6235SHajimu UMEMOTO char *cause = "unknown"; 1864dac6235SHajimu UMEMOTO 1874dac6235SHajimu UMEMOTO if (connected) { 1884dac6235SHajimu UMEMOTO close(f); 1894dac6235SHajimu UMEMOTO f = -1; 1904dac6235SHajimu UMEMOTO } 1914dac6235SHajimu UMEMOTO connected = 0; 1924dac6235SHajimu UMEMOTO 1934dac6235SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 1944dac6235SHajimu UMEMOTO hints.ai_family = PF_UNSPEC; 1954dac6235SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; 1964dac6235SHajimu UMEMOTO hints.ai_protocol = IPPROTO_UDP; 1974dac6235SHajimu UMEMOTO hints.ai_flags = AI_CANONNAME; 1984dac6235SHajimu UMEMOTO if (!port) 1994dac6235SHajimu UMEMOTO port = "tftp"; 2004dac6235SHajimu UMEMOTO error = getaddrinfo(host, port, &hints, &res0); 2014dac6235SHajimu UMEMOTO if (error) { 2024dac6235SHajimu UMEMOTO warnx("%s", gai_strerror(error)); 2034dac6235SHajimu UMEMOTO return; 2044dac6235SHajimu UMEMOTO } 2054dac6235SHajimu UMEMOTO 2064dac6235SHajimu UMEMOTO for (res = res0; res; res = res->ai_next) { 2074dac6235SHajimu UMEMOTO if (res->ai_addrlen > sizeof(peeraddr)) 2084dac6235SHajimu UMEMOTO continue; 2094dac6235SHajimu UMEMOTO f = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 2104dac6235SHajimu UMEMOTO if (f < 0) { 2114dac6235SHajimu UMEMOTO cause = "socket"; 2124dac6235SHajimu UMEMOTO continue; 2134dac6235SHajimu UMEMOTO } 2144dac6235SHajimu UMEMOTO 2154dac6235SHajimu UMEMOTO memset(&ss, 0, sizeof(ss)); 2164dac6235SHajimu UMEMOTO ss.ss_family = res->ai_family; 2174dac6235SHajimu UMEMOTO ss.ss_len = res->ai_addrlen; 2184dac6235SHajimu UMEMOTO if (bind(f, (struct sockaddr *)&ss, ss.ss_len) < 0) { 2194dac6235SHajimu UMEMOTO cause = "bind"; 2204dac6235SHajimu UMEMOTO close(f); 2214dac6235SHajimu UMEMOTO f = -1; 2224dac6235SHajimu UMEMOTO continue; 2234dac6235SHajimu UMEMOTO } 2244dac6235SHajimu UMEMOTO 2254dac6235SHajimu UMEMOTO break; 2264dac6235SHajimu UMEMOTO } 2274dac6235SHajimu UMEMOTO 2284dac6235SHajimu UMEMOTO if (f < 0) 2294dac6235SHajimu UMEMOTO warn("%s", cause); 2304dac6235SHajimu UMEMOTO else { 2314dac6235SHajimu UMEMOTO /* res->ai_addr <= sizeof(peeraddr) is guaranteed */ 2324dac6235SHajimu UMEMOTO memcpy(&peeraddr, res->ai_addr, res->ai_addrlen); 2334dac6235SHajimu UMEMOTO if (res->ai_canonname) { 2344dac6235SHajimu UMEMOTO (void) strncpy(hostname, res->ai_canonname, 2354dac6235SHajimu UMEMOTO sizeof(hostname)); 2364dac6235SHajimu UMEMOTO } else 2374dac6235SHajimu UMEMOTO (void) strncpy(hostname, host, sizeof(hostname)); 2384dac6235SHajimu UMEMOTO hostname[sizeof(hostname)-1] = 0; 2394dac6235SHajimu UMEMOTO connected = 1; 2404dac6235SHajimu UMEMOTO } 2414dac6235SHajimu UMEMOTO 2424dac6235SHajimu UMEMOTO freeaddrinfo(res0); 2434dac6235SHajimu UMEMOTO } 2444dac6235SHajimu UMEMOTO 2454dac6235SHajimu UMEMOTO void 2469b50d902SRodney W. Grimes setpeer(argc, argv) 2479b50d902SRodney W. Grimes int argc; 2489b50d902SRodney W. Grimes char *argv[]; 2499b50d902SRodney W. Grimes { 2509b50d902SRodney W. Grimes 2519b50d902SRodney W. Grimes if (argc < 2) { 2529b50d902SRodney W. Grimes strcpy(line, "Connect "); 2539b50d902SRodney W. Grimes printf("(to) "); 254ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 2559b50d902SRodney W. Grimes makeargv(); 2569b50d902SRodney W. Grimes argc = margc; 2579b50d902SRodney W. Grimes argv = margv; 2589b50d902SRodney W. Grimes } 259b50764b0SGuido van Rooij if ((argc < 2) || (argc > 3)) { 2609b50d902SRodney W. Grimes printf("usage: %s host-name [port]\n", argv[0]); 2619b50d902SRodney W. Grimes return; 2629b50d902SRodney W. Grimes } 2634dac6235SHajimu UMEMOTO if (argc == 3) 2644dac6235SHajimu UMEMOTO setpeer0(argv[1], NULL); 2654dac6235SHajimu UMEMOTO else 2664dac6235SHajimu UMEMOTO setpeer0(argv[1], argv[2]); 2679b50d902SRodney W. Grimes } 2689b50d902SRodney W. Grimes 2699b50d902SRodney W. Grimes struct modes { 2708049f797SMark Murray const char *m_name; 2718049f797SMark Murray const char *m_mode; 2729b50d902SRodney W. Grimes } modes[] = { 2739b50d902SRodney W. Grimes { "ascii", "netascii" }, 2749b50d902SRodney W. Grimes { "netascii", "netascii" }, 2759b50d902SRodney W. Grimes { "binary", "octet" }, 2769b50d902SRodney W. Grimes { "image", "octet" }, 2779b50d902SRodney W. Grimes { "octet", "octet" }, 2789b50d902SRodney W. Grimes /* { "mail", "mail" }, */ 2799b50d902SRodney W. Grimes { 0, 0 } 2809b50d902SRodney W. Grimes }; 2819b50d902SRodney W. Grimes 2829b50d902SRodney W. Grimes void 2839b50d902SRodney W. Grimes modecmd(argc, argv) 2849b50d902SRodney W. Grimes int argc; 2859b50d902SRodney W. Grimes char *argv[]; 2869b50d902SRodney W. Grimes { 2878049f797SMark Murray struct modes *p; 2888049f797SMark Murray const char *sep; 2899b50d902SRodney W. Grimes 2909b50d902SRodney W. Grimes if (argc < 2) { 2919b50d902SRodney W. Grimes printf("Using %s mode to transfer files.\n", mode); 2929b50d902SRodney W. Grimes return; 2939b50d902SRodney W. Grimes } 2949b50d902SRodney W. Grimes if (argc == 2) { 2959b50d902SRodney W. Grimes for (p = modes; p->m_name; p++) 2969b50d902SRodney W. Grimes if (strcmp(argv[1], p->m_name) == 0) 2979b50d902SRodney W. Grimes break; 2989b50d902SRodney W. Grimes if (p->m_name) { 2999b50d902SRodney W. Grimes settftpmode(p->m_mode); 3009b50d902SRodney W. Grimes return; 3019b50d902SRodney W. Grimes } 3029b50d902SRodney W. Grimes printf("%s: unknown mode\n", argv[1]); 3039b50d902SRodney W. Grimes /* drop through and print usage message */ 3049b50d902SRodney W. Grimes } 3059b50d902SRodney W. Grimes 3069b50d902SRodney W. Grimes printf("usage: %s [", argv[0]); 3079b50d902SRodney W. Grimes sep = " "; 3089b50d902SRodney W. Grimes for (p = modes; p->m_name; p++) { 3099b50d902SRodney W. Grimes printf("%s%s", sep, p->m_name); 3109b50d902SRodney W. Grimes if (*sep == ' ') 3119b50d902SRodney W. Grimes sep = " | "; 3129b50d902SRodney W. Grimes } 3139b50d902SRodney W. Grimes printf(" ]\n"); 3149b50d902SRodney W. Grimes return; 3159b50d902SRodney W. Grimes } 3169b50d902SRodney W. Grimes 3179b50d902SRodney W. Grimes void 3189b50d902SRodney W. Grimes setbinary(argc, argv) 3198049f797SMark Murray int argc __unused; 3208049f797SMark Murray char *argv[] __unused; 3219b50d902SRodney W. Grimes { 3229b50d902SRodney W. Grimes 3239b50d902SRodney W. Grimes settftpmode("octet"); 3249b50d902SRodney W. Grimes } 3259b50d902SRodney W. Grimes 3269b50d902SRodney W. Grimes void 3279b50d902SRodney W. Grimes setascii(argc, argv) 3288049f797SMark Murray int argc __unused; 3298049f797SMark Murray char *argv[] __unused; 3309b50d902SRodney W. Grimes { 3319b50d902SRodney W. Grimes 3329b50d902SRodney W. Grimes settftpmode("netascii"); 3339b50d902SRodney W. Grimes } 3349b50d902SRodney W. Grimes 3359b50d902SRodney W. Grimes static void 3369b50d902SRodney W. Grimes settftpmode(newmode) 3378049f797SMark Murray const char *newmode; 3389b50d902SRodney W. Grimes { 3399b50d902SRodney W. Grimes strcpy(mode, newmode); 3409b50d902SRodney W. Grimes if (verbose) 3419b50d902SRodney W. Grimes printf("mode set to %s\n", mode); 3429b50d902SRodney W. Grimes } 3439b50d902SRodney W. Grimes 3449b50d902SRodney W. Grimes 3459b50d902SRodney W. Grimes /* 3469b50d902SRodney W. Grimes * Send file(s). 3479b50d902SRodney W. Grimes */ 3489b50d902SRodney W. Grimes void 3499b50d902SRodney W. Grimes put(argc, argv) 3509b50d902SRodney W. Grimes int argc; 3519b50d902SRodney W. Grimes char *argv[]; 3529b50d902SRodney W. Grimes { 3539b50d902SRodney W. Grimes int fd; 3548049f797SMark Murray int n; 3558049f797SMark Murray char *cp, *targ; 3569b50d902SRodney W. Grimes 3579b50d902SRodney W. Grimes if (argc < 2) { 3589b50d902SRodney W. Grimes strcpy(line, "send "); 3599b50d902SRodney W. Grimes printf("(file) "); 360ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 3619b50d902SRodney W. Grimes makeargv(); 3629b50d902SRodney W. Grimes argc = margc; 3639b50d902SRodney W. Grimes argv = margv; 3649b50d902SRodney W. Grimes } 3659b50d902SRodney W. Grimes if (argc < 2) { 3669b50d902SRodney W. Grimes putusage(argv[0]); 3679b50d902SRodney W. Grimes return; 3689b50d902SRodney W. Grimes } 3699b50d902SRodney W. Grimes targ = argv[argc - 1]; 3704dac6235SHajimu UMEMOTO if (rindex(argv[argc - 1], ':')) { 3718049f797SMark Murray char *lcp; 3729b50d902SRodney W. Grimes 3739b50d902SRodney W. Grimes for (n = 1; n < argc - 1; n++) 3749b50d902SRodney W. Grimes if (index(argv[n], ':')) { 3759b50d902SRodney W. Grimes putusage(argv[0]); 3769b50d902SRodney W. Grimes return; 3779b50d902SRodney W. Grimes } 3788049f797SMark Murray lcp = argv[argc - 1]; 3794dac6235SHajimu UMEMOTO targ = rindex(lcp, ':'); 3809b50d902SRodney W. Grimes *targ++ = 0; 3814dac6235SHajimu UMEMOTO if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') { 3824dac6235SHajimu UMEMOTO lcp[strlen(lcp) - 1] = '\0'; 3834dac6235SHajimu UMEMOTO lcp++; 3849b50d902SRodney W. Grimes } 3854dac6235SHajimu UMEMOTO setpeer0(lcp, NULL); 3869b50d902SRodney W. Grimes } 3879b50d902SRodney W. Grimes if (!connected) { 3889b50d902SRodney W. Grimes printf("No target machine specified.\n"); 3899b50d902SRodney W. Grimes return; 3909b50d902SRodney W. Grimes } 3919b50d902SRodney W. Grimes if (argc < 4) { 3929b50d902SRodney W. Grimes cp = argc == 2 ? tail(targ) : argv[1]; 3939b50d902SRodney W. Grimes fd = open(cp, O_RDONLY); 3949b50d902SRodney W. Grimes if (fd < 0) { 395fd129a02SPhilippe Charnier warn("%s", cp); 3969b50d902SRodney W. Grimes return; 3979b50d902SRodney W. Grimes } 3989b50d902SRodney W. Grimes if (verbose) 3999b50d902SRodney W. Grimes printf("putting %s to %s:%s [%s]\n", 4009b50d902SRodney W. Grimes cp, hostname, targ, mode); 4018692ad46SDavid Greenman xmitfile(fd, targ, mode); 4029b50d902SRodney W. Grimes return; 4039b50d902SRodney W. Grimes } 4049b50d902SRodney W. Grimes /* this assumes the target is a directory */ 4059b50d902SRodney W. Grimes /* on a remote unix system. hmmmm. */ 4069b50d902SRodney W. Grimes cp = index(targ, '\0'); 4079b50d902SRodney W. Grimes *cp++ = '/'; 4089b50d902SRodney W. Grimes for (n = 1; n < argc - 1; n++) { 4099b50d902SRodney W. Grimes strcpy(cp, tail(argv[n])); 4109b50d902SRodney W. Grimes fd = open(argv[n], O_RDONLY); 4119b50d902SRodney W. Grimes if (fd < 0) { 412fd129a02SPhilippe Charnier warn("%s", argv[n]); 4139b50d902SRodney W. Grimes continue; 4149b50d902SRodney W. Grimes } 4159b50d902SRodney W. Grimes if (verbose) 4169b50d902SRodney W. Grimes printf("putting %s to %s:%s [%s]\n", 4179b50d902SRodney W. Grimes argv[n], hostname, targ, mode); 4188692ad46SDavid Greenman xmitfile(fd, targ, mode); 4199b50d902SRodney W. Grimes } 4209b50d902SRodney W. Grimes } 4219b50d902SRodney W. Grimes 4229b50d902SRodney W. Grimes static void 4239b50d902SRodney W. Grimes putusage(s) 4249b50d902SRodney W. Grimes char *s; 4259b50d902SRodney W. Grimes { 4269b50d902SRodney W. Grimes printf("usage: %s file ... host:target, or\n", s); 4279b50d902SRodney W. Grimes printf(" %s file ... target (when already connected)\n", s); 4289b50d902SRodney W. Grimes } 4299b50d902SRodney W. Grimes 4309b50d902SRodney W. Grimes /* 4319b50d902SRodney W. Grimes * Receive file(s). 4329b50d902SRodney W. Grimes */ 4339b50d902SRodney W. Grimes void 4349b50d902SRodney W. Grimes get(argc, argv) 4359b50d902SRodney W. Grimes int argc; 4369b50d902SRodney W. Grimes char *argv[]; 4379b50d902SRodney W. Grimes { 4389b50d902SRodney W. Grimes int fd; 4398049f797SMark Murray int n; 4408049f797SMark Murray char *cp; 4419b50d902SRodney W. Grimes char *src; 4429b50d902SRodney W. Grimes 4439b50d902SRodney W. Grimes if (argc < 2) { 4449b50d902SRodney W. Grimes strcpy(line, "get "); 4459b50d902SRodney W. Grimes printf("(files) "); 446ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 4479b50d902SRodney W. Grimes makeargv(); 4489b50d902SRodney W. Grimes argc = margc; 4499b50d902SRodney W. Grimes argv = margv; 4509b50d902SRodney W. Grimes } 4519b50d902SRodney W. Grimes if (argc < 2) { 4529b50d902SRodney W. Grimes getusage(argv[0]); 4539b50d902SRodney W. Grimes return; 4549b50d902SRodney W. Grimes } 4559b50d902SRodney W. Grimes if (!connected) { 4569b50d902SRodney W. Grimes for (n = 1; n < argc ; n++) 4574dac6235SHajimu UMEMOTO if (rindex(argv[n], ':') == 0) { 4589b50d902SRodney W. Grimes getusage(argv[0]); 4599b50d902SRodney W. Grimes return; 4609b50d902SRodney W. Grimes } 4619b50d902SRodney W. Grimes } 4629b50d902SRodney W. Grimes for (n = 1; n < argc ; n++) { 4634dac6235SHajimu UMEMOTO src = rindex(argv[n], ':'); 4649b50d902SRodney W. Grimes if (src == NULL) 4659b50d902SRodney W. Grimes src = argv[n]; 4669b50d902SRodney W. Grimes else { 4674dac6235SHajimu UMEMOTO char *lcp; 4689b50d902SRodney W. Grimes 4699b50d902SRodney W. Grimes *src++ = 0; 4704dac6235SHajimu UMEMOTO lcp = argv[n]; 4714dac6235SHajimu UMEMOTO if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') { 4724dac6235SHajimu UMEMOTO lcp[strlen(lcp) - 1] = '\0'; 4734dac6235SHajimu UMEMOTO lcp++; 4749b50d902SRodney W. Grimes } 4754dac6235SHajimu UMEMOTO setpeer0(lcp, NULL); 4764dac6235SHajimu UMEMOTO if (!connected) 4774dac6235SHajimu UMEMOTO continue; 4789b50d902SRodney W. Grimes } 4799b50d902SRodney W. Grimes if (argc < 4) { 4809b50d902SRodney W. Grimes cp = argc == 3 ? argv[2] : tail(src); 4819b50d902SRodney W. Grimes fd = creat(cp, 0644); 4829b50d902SRodney W. Grimes if (fd < 0) { 483fd129a02SPhilippe Charnier warn("%s", cp); 4849b50d902SRodney W. Grimes return; 4859b50d902SRodney W. Grimes } 4869b50d902SRodney W. Grimes if (verbose) 4879b50d902SRodney W. Grimes printf("getting from %s:%s to %s [%s]\n", 4889b50d902SRodney W. Grimes hostname, src, cp, mode); 4899b50d902SRodney W. Grimes recvfile(fd, src, mode); 4909b50d902SRodney W. Grimes break; 4919b50d902SRodney W. Grimes } 4929b50d902SRodney W. Grimes cp = tail(src); /* new .. jdg */ 4939b50d902SRodney W. Grimes fd = creat(cp, 0644); 4949b50d902SRodney W. Grimes if (fd < 0) { 495fd129a02SPhilippe Charnier warn("%s", cp); 4969b50d902SRodney W. Grimes continue; 4979b50d902SRodney W. Grimes } 4989b50d902SRodney W. Grimes if (verbose) 4999b50d902SRodney W. Grimes printf("getting from %s:%s to %s [%s]\n", 5009b50d902SRodney W. Grimes hostname, src, cp, mode); 5019b50d902SRodney W. Grimes recvfile(fd, src, mode); 5029b50d902SRodney W. Grimes } 5039b50d902SRodney W. Grimes } 5049b50d902SRodney W. Grimes 5059b50d902SRodney W. Grimes static void 5069b50d902SRodney W. Grimes getusage(s) 5079b50d902SRodney W. Grimes char *s; 5089b50d902SRodney W. Grimes { 5099b50d902SRodney W. Grimes printf("usage: %s host:file host:file ... file, or\n", s); 5109b50d902SRodney W. Grimes printf(" %s file file ... file if connected\n", s); 5119b50d902SRodney W. Grimes } 5129b50d902SRodney W. Grimes 5139b50d902SRodney W. Grimes int rexmtval = TIMEOUT; 5149b50d902SRodney W. Grimes 5159b50d902SRodney W. Grimes void 5169b50d902SRodney W. Grimes setrexmt(argc, argv) 5179b50d902SRodney W. Grimes int argc; 5189b50d902SRodney W. Grimes char *argv[]; 5199b50d902SRodney W. Grimes { 5209b50d902SRodney W. Grimes int t; 5219b50d902SRodney W. Grimes 5229b50d902SRodney W. Grimes if (argc < 2) { 5239b50d902SRodney W. Grimes strcpy(line, "Rexmt-timeout "); 5249b50d902SRodney W. Grimes printf("(value) "); 525ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 5269b50d902SRodney W. Grimes makeargv(); 5279b50d902SRodney W. Grimes argc = margc; 5289b50d902SRodney W. Grimes argv = margv; 5299b50d902SRodney W. Grimes } 5309b50d902SRodney W. Grimes if (argc != 2) { 5319b50d902SRodney W. Grimes printf("usage: %s value\n", argv[0]); 5329b50d902SRodney W. Grimes return; 5339b50d902SRodney W. Grimes } 5349b50d902SRodney W. Grimes t = atoi(argv[1]); 5359b50d902SRodney W. Grimes if (t < 0) 5369b50d902SRodney W. Grimes printf("%s: bad value\n", argv[1]); 5379b50d902SRodney W. Grimes else 5389b50d902SRodney W. Grimes rexmtval = t; 5399b50d902SRodney W. Grimes } 5409b50d902SRodney W. Grimes 5419b50d902SRodney W. Grimes int maxtimeout = 5 * TIMEOUT; 5429b50d902SRodney W. Grimes 5439b50d902SRodney W. Grimes void 5449b50d902SRodney W. Grimes settimeout(argc, argv) 5459b50d902SRodney W. Grimes int argc; 5469b50d902SRodney W. Grimes char *argv[]; 5479b50d902SRodney W. Grimes { 5489b50d902SRodney W. Grimes int t; 5499b50d902SRodney W. Grimes 5509b50d902SRodney W. Grimes if (argc < 2) { 5519b50d902SRodney W. Grimes strcpy(line, "Maximum-timeout "); 5529b50d902SRodney W. Grimes printf("(value) "); 553ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 5549b50d902SRodney W. Grimes makeargv(); 5559b50d902SRodney W. Grimes argc = margc; 5569b50d902SRodney W. Grimes argv = margv; 5579b50d902SRodney W. Grimes } 5589b50d902SRodney W. Grimes if (argc != 2) { 5599b50d902SRodney W. Grimes printf("usage: %s value\n", argv[0]); 5609b50d902SRodney W. Grimes return; 5619b50d902SRodney W. Grimes } 5629b50d902SRodney W. Grimes t = atoi(argv[1]); 5639b50d902SRodney W. Grimes if (t < 0) 5649b50d902SRodney W. Grimes printf("%s: bad value\n", argv[1]); 5659b50d902SRodney W. Grimes else 5669b50d902SRodney W. Grimes maxtimeout = t; 5679b50d902SRodney W. Grimes } 5689b50d902SRodney W. Grimes 5699b50d902SRodney W. Grimes void 5709b50d902SRodney W. Grimes status(argc, argv) 5718049f797SMark Murray int argc __unused; 5728049f797SMark Murray char *argv[] __unused; 5739b50d902SRodney W. Grimes { 5749b50d902SRodney W. Grimes if (connected) 5759b50d902SRodney W. Grimes printf("Connected to %s.\n", hostname); 5769b50d902SRodney W. Grimes else 5779b50d902SRodney W. Grimes printf("Not connected.\n"); 5789b50d902SRodney W. Grimes printf("Mode: %s Verbose: %s Tracing: %s\n", mode, 5799b50d902SRodney W. Grimes verbose ? "on" : "off", trace ? "on" : "off"); 5809b50d902SRodney W. Grimes printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n", 5819b50d902SRodney W. Grimes rexmtval, maxtimeout); 5829b50d902SRodney W. Grimes } 5839b50d902SRodney W. Grimes 5849b50d902SRodney W. Grimes void 5858049f797SMark Murray intr(dummy) 5868049f797SMark Murray int dummy __unused; 5879b50d902SRodney W. Grimes { 5889b50d902SRodney W. Grimes 5899b50d902SRodney W. Grimes signal(SIGALRM, SIG_IGN); 5909b50d902SRodney W. Grimes alarm(0); 5919b50d902SRodney W. Grimes longjmp(toplevel, -1); 5929b50d902SRodney W. Grimes } 5939b50d902SRodney W. Grimes 5949b50d902SRodney W. Grimes char * 5959b50d902SRodney W. Grimes tail(filename) 5969b50d902SRodney W. Grimes char *filename; 5979b50d902SRodney W. Grimes { 5988049f797SMark Murray char *s; 5999b50d902SRodney W. Grimes 6009b50d902SRodney W. Grimes while (*filename) { 6019b50d902SRodney W. Grimes s = rindex(filename, '/'); 6029b50d902SRodney W. Grimes if (s == NULL) 6039b50d902SRodney W. Grimes break; 6049b50d902SRodney W. Grimes if (s[1]) 6059b50d902SRodney W. Grimes return (s + 1); 6069b50d902SRodney W. Grimes *s = '\0'; 6079b50d902SRodney W. Grimes } 6089b50d902SRodney W. Grimes return (filename); 6099b50d902SRodney W. Grimes } 6109b50d902SRodney W. Grimes 611c33ed450SMatthew N. Dodd static const char * 6128979160aSBruce Evans command_prompt() 6138979160aSBruce Evans { 6148979160aSBruce Evans 615c33ed450SMatthew N. Dodd return ("tftp> "); 616c33ed450SMatthew N. Dodd } 617c33ed450SMatthew N. Dodd 6189b50d902SRodney W. Grimes /* 6199b50d902SRodney W. Grimes * Command parser. 6209b50d902SRodney W. Grimes */ 621eaa86f9dSBruce Evans static void 6229b50d902SRodney W. Grimes command() 6239b50d902SRodney W. Grimes { 624c33ed450SMatthew N. Dodd HistEvent he; 6258049f797SMark Murray struct cmd *c; 6268979160aSBruce Evans static EditLine *el; 6278979160aSBruce Evans static History *hist; 628c33ed450SMatthew N. Dodd const char *bp; 6298979160aSBruce Evans char *cp; 6308049f797SMark Murray int len, num, vrbose; 631c33ed450SMatthew N. Dodd 6328049f797SMark Murray vrbose = isatty(0); 6338049f797SMark Murray if (vrbose) { 634c33ed450SMatthew N. Dodd el = el_init("tftp", stdin, stdout, stderr); 635c33ed450SMatthew N. Dodd hist = history_init(); 636c33ed450SMatthew N. Dodd history(hist, &he, H_EVENT, 100); 637c33ed450SMatthew N. Dodd el_set(el, EL_HIST, history, hist); 638c33ed450SMatthew N. Dodd el_set(el, EL_EDITOR, "emacs"); 639c33ed450SMatthew N. Dodd el_set(el, EL_PROMPT, command_prompt); 640c33ed450SMatthew N. Dodd el_set(el, EL_SIGNAL, 1); 641c33ed450SMatthew N. Dodd el_source(el, NULL); 642c33ed450SMatthew N. Dodd } 6439b50d902SRodney W. Grimes for (;;) { 6448049f797SMark Murray if (vrbose) { 645c33ed450SMatthew N. Dodd if ((bp = el_gets(el, &num)) == NULL || num == 0) 646c33ed450SMatthew N. Dodd exit(0); 647c33ed450SMatthew N. Dodd len = (num > MAXLINE) ? MAXLINE : num; 648c33ed450SMatthew N. Dodd memcpy(line, bp, len); 649c33ed450SMatthew N. Dodd line[len] = '\0'; 650c33ed450SMatthew N. Dodd history(hist, &he, H_ENTER, bp); 651c33ed450SMatthew N. Dodd } else { 652ca22ff9eSJoerg Wunsch if (fgets(line, sizeof line , stdin) == 0) { 6539b50d902SRodney W. Grimes if (feof(stdin)) { 6549b50d902SRodney W. Grimes exit(0); 6559b50d902SRodney W. Grimes } else { 6569b50d902SRodney W. Grimes continue; 6579b50d902SRodney W. Grimes } 6589b50d902SRodney W. Grimes } 659c33ed450SMatthew N. Dodd } 660ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 661ca22ff9eSJoerg Wunsch *cp = '\0'; 6629b50d902SRodney W. Grimes if (line[0] == 0) 6639b50d902SRodney W. Grimes continue; 6649b50d902SRodney W. Grimes makeargv(); 6659b50d902SRodney W. Grimes if (margc == 0) 6669b50d902SRodney W. Grimes continue; 6679b50d902SRodney W. Grimes c = getcmd(margv[0]); 6689b50d902SRodney W. Grimes if (c == (struct cmd *)-1) { 6699b50d902SRodney W. Grimes printf("?Ambiguous command\n"); 6709b50d902SRodney W. Grimes continue; 6719b50d902SRodney W. Grimes } 6729b50d902SRodney W. Grimes if (c == 0) { 6739b50d902SRodney W. Grimes printf("?Invalid command\n"); 6749b50d902SRodney W. Grimes continue; 6759b50d902SRodney W. Grimes } 6769b50d902SRodney W. Grimes (*c->handler)(margc, margv); 6779b50d902SRodney W. Grimes } 6789b50d902SRodney W. Grimes } 6799b50d902SRodney W. Grimes 6809b50d902SRodney W. Grimes struct cmd * 6819b50d902SRodney W. Grimes getcmd(name) 6828049f797SMark Murray char *name; 6839b50d902SRodney W. Grimes { 6848049f797SMark Murray const char *p, *q; 6858049f797SMark Murray struct cmd *c, *found; 6868049f797SMark Murray int nmatches, longest; 6879b50d902SRodney W. Grimes 6889b50d902SRodney W. Grimes longest = 0; 6899b50d902SRodney W. Grimes nmatches = 0; 6909b50d902SRodney W. Grimes found = 0; 6919b50d902SRodney W. Grimes for (c = cmdtab; (p = c->name) != NULL; c++) { 6929b50d902SRodney W. Grimes for (q = name; *q == *p++; q++) 6939b50d902SRodney W. Grimes if (*q == 0) /* exact match? */ 6949b50d902SRodney W. Grimes return (c); 6959b50d902SRodney W. Grimes if (!*q) { /* the name was a prefix */ 6969b50d902SRodney W. Grimes if (q - name > longest) { 6979b50d902SRodney W. Grimes longest = q - name; 6989b50d902SRodney W. Grimes nmatches = 1; 6999b50d902SRodney W. Grimes found = c; 7009b50d902SRodney W. Grimes } else if (q - name == longest) 7019b50d902SRodney W. Grimes nmatches++; 7029b50d902SRodney W. Grimes } 7039b50d902SRodney W. Grimes } 7049b50d902SRodney W. Grimes if (nmatches > 1) 7059b50d902SRodney W. Grimes return ((struct cmd *)-1); 7069b50d902SRodney W. Grimes return (found); 7079b50d902SRodney W. Grimes } 7089b50d902SRodney W. Grimes 7099b50d902SRodney W. Grimes /* 7109b50d902SRodney W. Grimes * Slice a string up into argc/argv. 7119b50d902SRodney W. Grimes */ 7129b50d902SRodney W. Grimes static void 7139b50d902SRodney W. Grimes makeargv() 7149b50d902SRodney W. Grimes { 7158049f797SMark Murray char *cp; 7168049f797SMark Murray char **argp = margv; 7179b50d902SRodney W. Grimes 7189b50d902SRodney W. Grimes margc = 0; 719ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 720ca22ff9eSJoerg Wunsch *cp = '\0'; 7219b50d902SRodney W. Grimes for (cp = line; *cp;) { 7229b50d902SRodney W. Grimes while (isspace(*cp)) 7239b50d902SRodney W. Grimes cp++; 7249b50d902SRodney W. Grimes if (*cp == '\0') 7259b50d902SRodney W. Grimes break; 7269b50d902SRodney W. Grimes *argp++ = cp; 7279b50d902SRodney W. Grimes margc += 1; 7289b50d902SRodney W. Grimes while (*cp != '\0' && !isspace(*cp)) 7299b50d902SRodney W. Grimes cp++; 7309b50d902SRodney W. Grimes if (*cp == '\0') 7319b50d902SRodney W. Grimes break; 7329b50d902SRodney W. Grimes *cp++ = '\0'; 7339b50d902SRodney W. Grimes } 7349b50d902SRodney W. Grimes *argp++ = 0; 7359b50d902SRodney W. Grimes } 7369b50d902SRodney W. Grimes 7379b50d902SRodney W. Grimes void 7389b50d902SRodney W. Grimes quit(argc, argv) 7398049f797SMark Murray int argc __unused; 7408049f797SMark Murray char *argv[] __unused; 7419b50d902SRodney W. Grimes { 7429b50d902SRodney W. Grimes 7439b50d902SRodney W. Grimes exit(0); 7449b50d902SRodney W. Grimes } 7459b50d902SRodney W. Grimes 7469b50d902SRodney W. Grimes /* 7479b50d902SRodney W. Grimes * Help command. 7489b50d902SRodney W. Grimes */ 7499b50d902SRodney W. Grimes void 7509b50d902SRodney W. Grimes help(argc, argv) 7519b50d902SRodney W. Grimes int argc; 7529b50d902SRodney W. Grimes char *argv[]; 7539b50d902SRodney W. Grimes { 7548049f797SMark Murray struct cmd *c; 7559b50d902SRodney W. Grimes 7569b50d902SRodney W. Grimes if (argc == 1) { 7579b50d902SRodney W. Grimes printf("Commands may be abbreviated. Commands are:\n\n"); 7589b50d902SRodney W. Grimes for (c = cmdtab; c->name; c++) 7599b50d902SRodney W. Grimes printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help); 7609b50d902SRodney W. Grimes return; 7619b50d902SRodney W. Grimes } 7629b50d902SRodney W. Grimes while (--argc > 0) { 7638049f797SMark Murray char *arg; 7649b50d902SRodney W. Grimes arg = *++argv; 7659b50d902SRodney W. Grimes c = getcmd(arg); 7669b50d902SRodney W. Grimes if (c == (struct cmd *)-1) 7679b50d902SRodney W. Grimes printf("?Ambiguous help command %s\n", arg); 7689b50d902SRodney W. Grimes else if (c == (struct cmd *)0) 7699b50d902SRodney W. Grimes printf("?Invalid help command %s\n", arg); 7709b50d902SRodney W. Grimes else 7719b50d902SRodney W. Grimes printf("%s\n", c->help); 7729b50d902SRodney W. Grimes } 7739b50d902SRodney W. Grimes } 7749b50d902SRodney W. Grimes 7759b50d902SRodney W. Grimes void 7769b50d902SRodney W. Grimes settrace(argc, argv) 7778049f797SMark Murray int argc __unused; 7788049f797SMark Murray char **argv __unused; 7799b50d902SRodney W. Grimes { 7809b50d902SRodney W. Grimes trace = !trace; 7819b50d902SRodney W. Grimes printf("Packet tracing %s.\n", trace ? "on" : "off"); 7829b50d902SRodney W. Grimes } 7839b50d902SRodney W. Grimes 7849b50d902SRodney W. Grimes void 7859b50d902SRodney W. Grimes setverbose(argc, argv) 7868049f797SMark Murray int argc __unused; 7878049f797SMark Murray char **argv __unused; 7889b50d902SRodney W. Grimes { 7899b50d902SRodney W. Grimes verbose = !verbose; 7909b50d902SRodney W. Grimes printf("Verbose mode %s.\n", verbose ? "on" : "off"); 7919b50d902SRodney W. Grimes } 792