2 * Copyright (c) 1985, 1989 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 "@(#) Copyright (c) 1985, 1989 Regents of the University of California.\n\
21 All rights reserved.\n";
25 static char sccsid
[] = "@(#)main.c based on 5.13 (Berkeley) 3/14/89";
29 * FTP User Program -- Command Interface.
33 #if defined(sun) && !defined(FD_SET)
50 /* Lot's of options... */
52 * Options and other state info.
54 int trace
; /* trace packets exchanged */
55 int hash
; /* print # for each buffer transferred */
56 //int sendport; /* use PORT cmd for each data connection */
57 int verbose
; /* print messages coming back from server */
58 int connected
; /* connected to server */
59 int fromatty
; /* input is from a terminal */
60 int interactive
; /* interactively prompt on m* cmds */
61 int debug
; /* debugging level */
62 int bell
; /* ring bell on cmd completion */
63 int doglob
; /* glob local file names */
64 int proxy
; /* proxy server connection active */
66 int proxflag
; /* proxy connection exists */
67 int sunique
; /* store files on server with unique name */
68 int runique
; /* store local files with unique name */
69 int mcase
; /* map upper to lower case for mget names */
70 int ntflag
; /* use ntin ntout tables for name translation */
71 int mapflag
; /* use mapin mapout templates on file names */
72 int code
; /* return/reply code for ftp command */
73 int crflag
; /* if 1, strip car. rets. on ascii gets */
74 char pasv
[64]; /* passive port for proxy data connection */
75 char *altarg
; /* argv[1] with no shell-like preprocessing */
76 char ntin
[17]; /* input translation table */
77 char ntout
[17]; /* output translation table */
78 // #include <sys/param.h>
79 char mapin
[MAXPATHLEN
]; /* input map template */
80 char mapout
[MAXPATHLEN
]; /* output map template */
81 char typename
[32]; /* name of file transfer type */
82 int type
; /* file transfer type */
83 char structname
[32]; /* name of file transfer structure */
84 int stru
; /* file transfer structure */
85 char formname
[32]; /* name of file transfer format */
86 int form
; /* file transfer format */
87 char modename
[32]; /* name of file transfer mode */
88 int mode
; /* file transfer mode */
89 char bytename
[32]; /* local byte size in ascii */
90 int bytesize
; /* local byte size in binary */
92 jmp_buf toplevel
; /* non-local goto stuff for cmd scanner */
94 char line
[200]; /* input line buffer */
95 char *stringbase
; /* current scan point in line buffer */
96 char argbuf
[200]; /* argument storage buffer */
97 char *argbase
; /* current storage point in arg buffer */
98 int margc
; /* count of arguments on input line */
99 const char *margv
[20]; /* args parsed from input line */
100 int cpend
; /* flag: if != 0, then pending server reply */
101 int mflag
; /* flag: if != 0, then active multi command */
103 int options
; /* used during socket creation */
105 int macnum
; /* number of defined macros */
106 struct macel macros
[16];
110 * Need to start a listen on the data channel
111 * before we send the command, otherwise the
112 * server's connect may fail.
116 static const char *slurpstring();
119 int main(int argc
, const char *argv
[])
124 char homedir
[MAXPATHLEN
];
131 struct servent
*sp
; /* service spec for tcp/ftp */
133 /* Disable output buffering, for the benefit of Emacs. */
134 //setbuf(stdout, NULL);
136 _fmode
= O_BINARY
; // This causes an error somewhere.
138 wVerReq
= MAKEWORD(1,1);
140 err
= WSAStartup(wVerReq
, &WSAData
);
143 fprintf(stderr
, "Could not initialize Windows socket interface.");
147 sp
= getservbyname("ftp", "tcp");
149 fprintf(stderr
, "ftp: ftp/tcp: unknown service\n");
153 portnum
= sp
->s_port
;
160 while (argc
> 0 && **argv
== '-') {
161 for (cp
= *argv
+ 1; *cp
; cp
++)
191 "ftp: %c: unknown option\n", *cp
);
196 // fromatty = isatty(fileno(stdin));
197 fromatty
= 1; // Strengthen this test
199 * Set up defaults for FTP.
201 (void) strcpy(typename
, "ascii"), type
= TYPE_A
;
202 (void) strcpy(formname
, "non-print"), form
= FORM_N
;
203 (void) strcpy(modename
, "stream"), mode
= MODE_S
;
204 (void) strcpy(structname
, "file"), stru
= STRU_F
;
205 (void) strcpy(bytename
, "8"), bytesize
= 8;
208 cpend
= 0; /* no pending replies */
209 proxy
= 0; /* proxy not active */
210 passivemode
= 1; /* passive mode *is* active */
211 crflag
= 1; /* strip c.r. on ascii gets */
213 * Set up the home directory in case we're globbing.
221 pw
= getpwuid(getuid());
224 (void) strcpy(home
, pw
->pw_dir
);
229 if (setjmp(toplevel
))
231 // (void) signal(SIGINT, intr);
232 // (void) signal(SIGPIPE, lostpeer);
233 setpeer(argc
+ 1, argv
- 1);
235 top
= setjmp(toplevel
) == 0;
237 // (void) signal(SIGINT, intr);
238 // (void) signal(SIGPIPE, lostpeer);
248 longjmp(toplevel
, 1);
262 (void) shutdown(data
, 1+1);
286 s = rindex(filename, '/');
299 void cmdscanner(int top
)
301 register struct cmd
*c
;
304 (void) putchar('\n');
306 (void) fflush(stdout
);
309 (void) fflush(stdout
);
311 if (gets(line
) == 0) {
312 if (feof(stdin
) || ferror(stdin
))
322 c
= getcmd(margv
[0]);
323 if (c
== (struct cmd
*)-1) {
324 printf("?Ambiguous command\n");
328 printf("?Invalid command\n");
331 if (c
->c_conn
&& !connected
) {
332 printf ("Not connected.\n");
335 (*c
->c_handler
)(margc
, margv
);
336 if (bell
&& c
->c_bell
)
337 (void) putchar('\007');
338 if (c
->c_handler
!= help
)
341 (void) fflush(stdout
);
342 // (void) signal(SIGINT, intr);
343 // (void) signal(SIGPIPE, lostpeer);
347 getcmd(const char *name
)
349 extern struct cmd cmdtab
[];
351 struct cmd
*c
, *found
;
352 int nmatches
, longest
;
357 for (c
= cmdtab
; (p
= c
->c_name
); c
++) {
358 for (q
= name
; *q
== *p
++; q
++)
359 if (*q
== 0) /* exact match? */
361 if (!*q
) { /* the name was a prefix */
362 if (q
- name
> longest
) {
366 } else if (q
- name
== longest
)
371 return ((struct cmd
*)-1);
376 * Slice a string up into argc/argv.
387 stringbase
= line
; /* scan from first of buffer */
388 argbase
= argbuf
; /* store from first of buffer */
390 while ((*argp
++ = slurpstring()))
395 * Parse string into argbuf;
396 * implemented with FSM to
397 * handle quoting and strings
403 register char *sb
= stringbase
;
404 register char *ap
= argbase
;
405 char *tmp
= argbase
; /* will return this if token found */
407 if (*sb
== '!' || *sb
== '$') { /* recognize ! as a token for shell */
408 switch (slrflag
) { /* and $ as token for macro invoke */
412 return ((*sb
== '!') ? "!" : "$");
454 goto OUT1
; /* end of token */
457 sb
++; goto S2
; /* slurp next character */
460 sb
++; goto S3
; /* slurp quoted string */
463 *ap
++ = *sb
++; /* add character to token */
498 argbase
= ap
; /* update storage pointer */
499 stringbase
= sb
; /* update scan pointer */
517 #define HELPINDENT (sizeof ("directory"))
521 * Call each command handler with argc == 0 and argv[0] == name.
523 void help(int argc
, const char *argv
[])
525 extern struct cmd cmdtab
[];
529 register int i
, j
, w
, k
;
530 int columns
, width
= 0, lines
;
533 printf("Commands may be abbreviated. Commands are:\n\n");
534 for (c
= cmdtab
; c
< &cmdtab
[NCMDS
]; c
++) {
535 int len
= strlen(c
->c_name
);
540 width
= (width
+ 8) &~ 7;
541 columns
= 80 / width
;
544 lines
= (NCMDS
+ columns
- 1) / columns
;
545 for (i
= 0; i
< lines
; i
++) {
546 for (j
= 0; j
< columns
; j
++) {
547 c
= cmdtab
+ j
* lines
+ i
;
548 if (c
->c_name
&& (!proxy
|| c
->c_proxy
)) {
549 printf("%s", c
->c_name
);
551 else if (c
->c_name
) {
552 for (k
=0; k
< (int) strlen(c
->c_name
); k
++) {
556 if (c
+ lines
>= &cmdtab
[NCMDS
]) {
560 w
= strlen(c
->c_name
);
563 (void) putchar('\t');
567 (void) fflush(stdout
);
574 if (c
== (struct cmd
*)-1)
575 printf("?Ambiguous help command %s\n", arg
);
576 else if (c
== (struct cmd
*)0)
577 printf("?Invalid help command %s\n", arg
);
579 printf("%-*s\t%s\n", (int)HELPINDENT
,
580 c
->c_name
, c
->c_help
);
582 (void) fflush(stdout
);