=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tftp/main.c,v retrieving revision 1.23 retrieving revision 1.24 diff -c -r1.23 -r1.24 *** src/usr.bin/tftp/main.c 2006/01/23 17:29:22 1.23 --- src/usr.bin/tftp/main.c 2006/05/08 13:02:51 1.24 *************** *** 1,4 **** ! /* $OpenBSD: main.c,v 1.23 2006/01/23 17:29:22 millert Exp $ */ /* $NetBSD: main.c,v 1.6 1995/05/21 16:54:10 mycroft Exp $ */ /* --- 1,4 ---- ! /* $OpenBSD: main.c,v 1.24 2006/05/08 13:02:51 claudio Exp $ */ /* $NetBSD: main.c,v 1.6 1995/05/21 16:54:10 mycroft Exp $ */ /* *************** *** 40,46 **** #if 0 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; #endif ! static const char rcsid[] = "$OpenBSD: main.c,v 1.23 2006/01/23 17:29:22 millert Exp $"; #endif /* not lint */ /* Many bug fixes are from Jim Guyton */ --- 40,46 ---- #if 0 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; #endif ! static const char rcsid[] = "$OpenBSD: main.c,v 1.24 2006/05/08 13:02:51 claudio Exp $"; #endif /* not lint */ /* Many bug fixes are from Jim Guyton */ *************** *** 48,81 **** /* * TFTP User Program -- Command Interface. */ #include #include #include #include - #include #include #include #include - #include #include #include #include #include #include #include #include "extern.h" #define TIMEOUT 5 /* secs between rexmt's */ #define LBUFLEN 200 /* size of input buffer */ #define MAXARGV 20 struct sockaddr_in peeraddr; int f; ! short port; int trace; int verbose; int connected; --- 48,82 ---- /* * TFTP User Program -- Command Interface. */ + #include #include #include #include #include #include #include #include #include #include #include #include #include #include + #include #include "extern.h" #define TIMEOUT 5 /* secs between rexmt's */ #define LBUFLEN 200 /* size of input buffer */ #define MAXARGV 20 + #define HELPINDENT (sizeof("connect")) struct sockaddr_in peeraddr; int f; ! short port; int trace; int verbose; int connected; *************** *** 84,93 **** int margc; char *margv[MAXARGV+1]; char *prompt = "tftp"; - jmp_buf toplevel; void intr(int); struct servent *sp; void get(int, char **); void help(int, char **); void modecmd(int, char **); --- 85,99 ---- int margc; char *margv[MAXARGV+1]; char *prompt = "tftp"; void intr(int); struct servent *sp; + int rexmtval = TIMEOUT; + int maxtimeout = 5 * TIMEOUT; + char hostname[MAXHOSTNAMELEN]; + FILE *file = NULL; + volatile sig_atomic_t intrflag = 0; + void get(int, char **); void help(int, char **); void modecmd(int, char **); *************** *** 101,106 **** --- 107,113 ---- void settrace(int, char **); void setverbose(int, char **); void status(int, char **); + int readcmd(char *, int, FILE *); static __dead void command(void); *************** *** 109,116 **** static void putusage(char *); static void settftpmode(char *); - #define HELPINDENT (sizeof("connect")) - struct cmd { char *name; char *help; --- 116,121 ---- *************** *** 128,135 **** char sthelp[] = "show current status"; char xhelp[] = "set per-packet retransmission timeout"; char ihelp[] = "set total retransmission timeout"; ! char ashelp[] = "set mode to netascii"; ! char bnhelp[] = "set mode to octet"; struct cmd cmdtab[] = { { "connect", chelp, setpeer }, --- 133,140 ---- char sthelp[] = "show current status"; char xhelp[] = "set per-packet retransmission timeout"; char ihelp[] = "set total retransmission timeout"; ! char ashelp[] = "set mode to netascii"; ! char bnhelp[] = "set mode to octet"; struct cmd cmdtab[] = { { "connect", chelp, setpeer }, *************** *** 144,153 **** --- 149,172 ---- { "ascii", ashelp, setascii }, { "rexmt", xhelp, setrexmt }, { "timeout", ihelp, settimeout }, + { "help", hhelp, help }, { "?", hhelp, help }, { NULL, NULL, NULL } }; + struct modes { + char *m_name; + char *m_mode; + } modes[] = { + { "ascii", "netascii" }, + { "netascii", "netascii" }, + { "binary", "octet" }, + { "image", "octet" }, + { "octet", "octet" }, + /* { "mail", "mail" }, */ + { NULL, NULL } + }; + struct cmd *getcmd(char *); char *tail(char *); *************** *** 156,186 **** { struct sockaddr_in s_in; sp = getservbyname("tftp", "udp"); if (sp == 0) errx(1, "udp/tftp: unknown service"); f = socket(AF_INET, SOCK_DGRAM, 0); if (f < 0) err(3, "socket"); ! bzero((char *)&s_in, sizeof (s_in)); s_in.sin_family = AF_INET; ! if (bind(f, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) err(1, "bind"); ! strlcpy(mode, "netascii", sizeof mode); ! signal(SIGINT, intr); ! if (argc > 1) { ! if (setjmp(toplevel) != 0) ! exit(0); setpeer(argc, argv); ! } ! if (setjmp(toplevel) != 0) ! (void)putchar('\n'); command(); return (0); } - char hostname[MAXHOSTNAMELEN]; - void setpeer(int argc, char *argv[]) { --- 175,208 ---- { struct sockaddr_in s_in; + /* socket, bind */ sp = getservbyname("tftp", "udp"); if (sp == 0) errx(1, "udp/tftp: unknown service"); f = socket(AF_INET, SOCK_DGRAM, 0); if (f < 0) err(3, "socket"); ! bzero((char *)&s_in, sizeof(s_in)); s_in.sin_family = AF_INET; ! if (bind(f, (struct sockaddr *)&s_in, sizeof(s_in)) < 0) err(1, "bind"); ! ! /* set default transfer mode */ ! strlcpy(mode, "netascii", sizeof(mode)); ! ! /* set peer if given */ ! if (argc > 1) setpeer(argc, argv); ! ! /* catch SIGINT */ ! signal(SIGINT, intr); ! ! /* command prompt */ command(); + return (0); } void setpeer(int argc, char *argv[]) { *************** *** 189,195 **** if (argc < 2) { strlcpy(line, "Connect ", sizeof line); printf("(to) "); ! fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; --- 211,217 ---- if (argc < 2) { strlcpy(line, "Connect ", sizeof line); printf("(to) "); ! readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; *************** *** 227,245 **** connected = 1; } - struct modes { - char *m_name; - char *m_mode; - } modes[] = { - { "ascii", "netascii" }, - { "netascii", "netascii" }, - { "binary", "octet" }, - { "image", "octet" }, - { "octet", "octet" }, - /* { "mail", "mail" }, */ - { NULL, NULL } - }; - void modecmd(int argc, char *argv[]) { --- 249,254 ---- *************** *** 276,289 **** void setbinary(int argc, char *argv[]) { - settftpmode("octet"); } void setascii(int argc, char *argv[]) { - settftpmode("netascii"); } --- 285,296 ---- *************** *** 295,301 **** printf("mode set to %s\n", mode); } - /* * Send file(s). */ --- 302,307 ---- *************** *** 309,315 **** if (argc < 2) { strlcpy(line, "send ", sizeof line); printf("(file) "); ! fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; --- 315,321 ---- if (argc < 2) { strlcpy(line, "send ", sizeof line); printf("(file) "); ! readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin); if (makeargv()) return; argc = margc; *************** *** 362,369 **** return; } ! /* this assumes the target is a directory */ ! /* on a remote unix system. hmmmm. */ for (n = 1; n < argc - 1; n++) { if (asprintf(&cp, "%s/%s", targ, tail(argv[n])) == -1) err(1, "asprintf"); --- 368,377 ---- return; } ! /* ! * this assumes the target is a directory on ! * on a remote unix system. hmmmm. ! */ for (n = 1; n < argc - 1; n++) { if (asprintf(&cp, "%s/%s", targ, tail(argv[n])) == -1) err(1, "asprintf"); *************** *** 386,392 **** putusage(char *s) { printf("usage: %s file [[host:]remotename]\n", s); ! printf(" %s file1 file2 ... fileN [[host:]remote-directory]\n", s); } /* --- 394,401 ---- putusage(char *s) { printf("usage: %s file [[host:]remotename]\n", s); ! printf(" %s file1 file2 ... fileN [[host:]remote-directory]\n", ! s); } /* *************** *** 403,409 **** if (argc < 2) { strlcpy(line, "get ", sizeof line); printf("(files) "); ! fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; --- 412,418 ---- if (argc < 2) { strlcpy(line, "get ", sizeof line); printf("(files) "); ! readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; *************** *** 474,481 **** printf(" %s [host1:]file1 [host2:]file2 ... [hostN:]fileN\n", s); } - int rexmtval = TIMEOUT; - void setrexmt(int argc, char *argv[]) { --- 483,488 ---- *************** *** 484,490 **** if (argc < 2) { strlcpy(line, "Rexmt-timeout ", sizeof line); printf("(value) "); ! fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; --- 491,497 ---- if (argc < 2) { strlcpy(line, "Rexmt-timeout ", sizeof line); printf("(value) "); ! readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; *************** *** 501,508 **** rexmtval = t; } - int maxtimeout = 5 * TIMEOUT; - void settimeout(int argc, char *argv[]) { --- 508,513 ---- *************** *** 511,517 **** if (argc < 2) { strlcpy(line, "Maximum-timeout ", sizeof line); printf("(value) "); ! fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; --- 516,522 ---- if (argc < 2) { strlcpy(line, "Maximum-timeout ", sizeof line); printf("(value) "); ! readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin); if (makeargv()) return; argc = margc; *************** *** 544,553 **** void intr(int signo) { ! ! signal(SIGALRM, SIG_IGN); ! alarm(0); ! longjmp(toplevel, -1); } char * --- 549,555 ---- void intr(int signo) { ! intrflag = 1; } char * *************** *** 576,588 **** for (;;) { printf("%s> ", prompt); ! if (fgets(line, LBUFLEN, stdin) == 0) { ! if (feof(stdin)) { ! exit(0); ! } else { ! continue; ! } ! } if ((line[0] == 0) || (line[0] == '\n')) continue; if (makeargv()) --- 578,585 ---- for (;;) { printf("%s> ", prompt); ! if (readcmd(line, LBUFLEN, stdin) < 1) ! continue; if ((line[0] == 0) || (line[0] == '\n')) continue; if (makeargv()) *************** *** 612,617 **** --- 609,615 ---- longest = 0; nmatches = 0; found = 0; + intrflag = 0; for (c = cmdtab; (p = c->name) != NULL; c++) { for (q = name; *q == *p++; q++) if (*q == 0) /* exact match? */ *************** *** 666,672 **** void quit(int argc, char *argv[]) { - exit(0); } --- 664,669 ---- *************** *** 709,712 **** --- 706,739 ---- { verbose = !verbose; printf("Verbose mode %s.\n", verbose ? "on" : "off"); + } + + int + readcmd(char *input, int len, FILE *stream) + { + int nfds; + struct pollfd pfd[1]; + + fflush(stdout); + + pfd[0].fd = 0; + pfd[0].events = POLLIN; + nfds = poll(pfd, 1, INFTIM); + if (nfds == -1) { + if (intrflag) { + intrflag = 0; + putchar('\n'); + return (0); + } + exit(1); + } + + if (fgets(input, len, stream) == NULL) { + if (feof(stdin)) + exit(0); + else + return (-1); + } + + return (1); }