Commit | Line | Data |
---|---|---|
edf71f48 | 1 | /* |
3371ac92 | 2 | * Copyright (c) 1985, 1989 Regents of the University of California. |
11c5f0a3 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b36fc510 KB |
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 | |
ff00793c | 15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
edf71f48 DF |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
3371ac92 | 20 | "@(#) Copyright (c) 1985, 1989 Regents of the University of California.\n\ |
edf71f48 | 21 | All rights reserved.\n"; |
11c5f0a3 | 22 | #endif /* not lint */ |
edf71f48 | 23 | |
ba5bf9f5 | 24 | #ifndef lint |
3371ac92 | 25 | static char sccsid[] = "@(#)main.c 5.13 (Berkeley) %G%"; |
11c5f0a3 | 26 | #endif /* not lint */ |
ba5bf9f5 SL |
27 | |
28 | /* | |
29 | * FTP User Program -- Command Interface. | |
30 | */ | |
9069f68e | 31 | #include "ftp_var.h" |
ba5bf9f5 SL |
32 | #include <sys/socket.h> |
33 | #include <sys/ioctl.h> | |
04480325 | 34 | #include <sys/types.h> |
ba5bf9f5 | 35 | |
62c4a390 SL |
36 | #include <arpa/ftp.h> |
37 | ||
ba5bf9f5 SL |
38 | #include <signal.h> |
39 | #include <stdio.h> | |
40 | #include <errno.h> | |
41 | #include <ctype.h> | |
42 | #include <netdb.h> | |
3b645f65 | 43 | #include <pwd.h> |
ba5bf9f5 | 44 | |
ba5bf9f5 | 45 | |
04480325 | 46 | uid_t getuid(); |
ba5bf9f5 SL |
47 | int intr(); |
48 | int lostpeer(); | |
3b645f65 | 49 | extern char *home; |
14433a73 | 50 | char *getlogin(); |
ba5bf9f5 SL |
51 | |
52 | main(argc, argv) | |
53 | char *argv[]; | |
54 | { | |
55 | register char *cp; | |
56 | int top; | |
14433a73 | 57 | struct passwd *pw = NULL; |
3b645f65 | 58 | char homedir[MAXPATHLEN]; |
ba5bf9f5 SL |
59 | |
60 | sp = getservbyname("ftp", "tcp"); | |
61 | if (sp == 0) { | |
62 | fprintf(stderr, "ftp: ftp/tcp: unknown service\n"); | |
63 | exit(1); | |
64 | } | |
a16b8edf | 65 | doglob = 1; |
5ac6fc46 | 66 | interactive = 1; |
a16b8edf | 67 | autologin = 1; |
ba5bf9f5 SL |
68 | argc--, argv++; |
69 | while (argc > 0 && **argv == '-') { | |
70 | for (cp = *argv + 1; *cp; cp++) | |
71 | switch (*cp) { | |
72 | ||
73 | case 'd': | |
74 | options |= SO_DEBUG; | |
75 | debug++; | |
76 | break; | |
77 | ||
78 | case 'v': | |
79 | verbose++; | |
80 | break; | |
81 | ||
82 | case 't': | |
83 | trace++; | |
84 | break; | |
85 | ||
86 | case 'i': | |
5ac6fc46 | 87 | interactive = 0; |
ba5bf9f5 SL |
88 | break; |
89 | ||
90 | case 'n': | |
91 | autologin = 0; | |
92 | break; | |
93 | ||
a16b8edf SL |
94 | case 'g': |
95 | doglob = 0; | |
96 | break; | |
97 | ||
ba5bf9f5 | 98 | default: |
04480325 | 99 | fprintf(stdout, |
ba5bf9f5 SL |
100 | "ftp: %c: unknown option\n", *cp); |
101 | exit(1); | |
102 | } | |
103 | argc--, argv++; | |
104 | } | |
105 | fromatty = isatty(fileno(stdin)); | |
106 | /* | |
107 | * Set up defaults for FTP. | |
108 | */ | |
04480325 GM |
109 | (void) strcpy(typename, "ascii"), type = TYPE_A; |
110 | (void) strcpy(formname, "non-print"), form = FORM_N; | |
111 | (void) strcpy(modename, "stream"), mode = MODE_S; | |
112 | (void) strcpy(structname, "file"), stru = STRU_F; | |
113 | (void) strcpy(bytename, "8"), bytesize = 8; | |
ba5bf9f5 SL |
114 | if (fromatty) |
115 | verbose++; | |
9069f68e GM |
116 | cpend = 0; /* no pending replies */ |
117 | proxy = 0; /* proxy not active */ | |
118 | crflag = 1; /* strip c.r. on ascii gets */ | |
3b645f65 SL |
119 | /* |
120 | * Set up the home directory in case we're globbing. | |
121 | */ | |
14433a73 | 122 | cp = getlogin(); |
9069f68e | 123 | if (cp != NULL) { |
14433a73 | 124 | pw = getpwnam(cp); |
9069f68e | 125 | } |
3b645f65 SL |
126 | if (pw == NULL) |
127 | pw = getpwuid(getuid()); | |
128 | if (pw != NULL) { | |
129 | home = homedir; | |
04480325 | 130 | (void) strcpy(home, pw->pw_dir); |
3b645f65 | 131 | } |
ba5bf9f5 SL |
132 | if (argc > 0) { |
133 | if (setjmp(toplevel)) | |
134 | exit(0); | |
04480325 GM |
135 | (void) signal(SIGINT, intr); |
136 | (void) signal(SIGPIPE, lostpeer); | |
ba5bf9f5 SL |
137 | setpeer(argc + 1, argv - 1); |
138 | } | |
139 | top = setjmp(toplevel) == 0; | |
140 | if (top) { | |
04480325 GM |
141 | (void) signal(SIGINT, intr); |
142 | (void) signal(SIGPIPE, lostpeer); | |
ba5bf9f5 SL |
143 | } |
144 | for (;;) { | |
145 | cmdscanner(top); | |
146 | top = 1; | |
147 | } | |
148 | } | |
149 | ||
150 | intr() | |
151 | { | |
152 | ||
153 | longjmp(toplevel, 1); | |
154 | } | |
155 | ||
156 | lostpeer() | |
157 | { | |
158 | extern FILE *cout; | |
159 | extern int data; | |
ba5bf9f5 SL |
160 | |
161 | if (connected) { | |
162 | if (cout != NULL) { | |
04480325 GM |
163 | (void) shutdown(fileno(cout), 1+1); |
164 | (void) fclose(cout); | |
ba5bf9f5 SL |
165 | cout = NULL; |
166 | } | |
167 | if (data >= 0) { | |
04480325 | 168 | (void) shutdown(data, 1+1); |
ba5bf9f5 SL |
169 | (void) close(data); |
170 | data = -1; | |
171 | } | |
172 | connected = 0; | |
173 | } | |
9069f68e GM |
174 | pswitch(1); |
175 | if (connected) { | |
176 | if (cout != NULL) { | |
04480325 GM |
177 | (void) shutdown(fileno(cout), 1+1); |
178 | (void) fclose(cout); | |
9069f68e GM |
179 | cout = NULL; |
180 | } | |
181 | connected = 0; | |
182 | } | |
183 | proxflag = 0; | |
184 | pswitch(0); | |
ba5bf9f5 SL |
185 | } |
186 | ||
04480325 | 187 | /*char * |
ba5bf9f5 SL |
188 | tail(filename) |
189 | char *filename; | |
190 | { | |
191 | register char *s; | |
192 | ||
193 | while (*filename) { | |
194 | s = rindex(filename, '/'); | |
195 | if (s == NULL) | |
196 | break; | |
197 | if (s[1]) | |
198 | return (s + 1); | |
199 | *s = '\0'; | |
200 | } | |
201 | return (filename); | |
202 | } | |
04480325 | 203 | */ |
ba5bf9f5 SL |
204 | /* |
205 | * Command parser. | |
206 | */ | |
207 | cmdscanner(top) | |
208 | int top; | |
209 | { | |
210 | register struct cmd *c; | |
211 | struct cmd *getcmd(); | |
ba5bf9f5 SL |
212 | extern int help(); |
213 | ||
214 | if (!top) | |
04480325 | 215 | (void) putchar('\n'); |
ba5bf9f5 SL |
216 | for (;;) { |
217 | if (fromatty) { | |
218 | printf("ftp> "); | |
04480325 | 219 | (void) fflush(stdout); |
ba5bf9f5 | 220 | } |
5a94754b | 221 | if (gets(line) == 0) { |
f888943d | 222 | if (feof(stdin) || ferror(stdin)) |
c3b6c3a8 | 223 | quit(); |
ba5bf9f5 | 224 | break; |
5a94754b | 225 | } |
ba5bf9f5 SL |
226 | if (line[0] == 0) |
227 | break; | |
228 | makeargv(); | |
9069f68e | 229 | if (margc == 0) { |
14433a73 | 230 | continue; |
9069f68e | 231 | } |
ba5bf9f5 SL |
232 | c = getcmd(margv[0]); |
233 | if (c == (struct cmd *)-1) { | |
234 | printf("?Ambiguous command\n"); | |
235 | continue; | |
236 | } | |
237 | if (c == 0) { | |
238 | printf("?Invalid command\n"); | |
239 | continue; | |
240 | } | |
5ac6fc46 SL |
241 | if (c->c_conn && !connected) { |
242 | printf ("Not connected.\n"); | |
243 | continue; | |
244 | } | |
ba5bf9f5 SL |
245 | (*c->c_handler)(margc, margv); |
246 | if (bell && c->c_bell) | |
ff00793c | 247 | (void) putchar('\007'); |
ba5bf9f5 SL |
248 | if (c->c_handler != help) |
249 | break; | |
250 | } | |
04480325 GM |
251 | (void) signal(SIGINT, intr); |
252 | (void) signal(SIGPIPE, lostpeer); | |
ba5bf9f5 SL |
253 | } |
254 | ||
255 | struct cmd * | |
256 | getcmd(name) | |
257 | register char *name; | |
258 | { | |
a7d96790 | 259 | extern struct cmd cmdtab[]; |
ba5bf9f5 SL |
260 | register char *p, *q; |
261 | register struct cmd *c, *found; | |
262 | register int nmatches, longest; | |
263 | ||
264 | longest = 0; | |
265 | nmatches = 0; | |
266 | found = 0; | |
267 | for (c = cmdtab; p = c->c_name; c++) { | |
268 | for (q = name; *q == *p++; q++) | |
269 | if (*q == 0) /* exact match? */ | |
270 | return (c); | |
271 | if (!*q) { /* the name was a prefix */ | |
272 | if (q - name > longest) { | |
273 | longest = q - name; | |
274 | nmatches = 1; | |
275 | found = c; | |
276 | } else if (q - name == longest) | |
277 | nmatches++; | |
278 | } | |
279 | } | |
280 | if (nmatches > 1) | |
281 | return ((struct cmd *)-1); | |
282 | return (found); | |
283 | } | |
284 | ||
285 | /* | |
286 | * Slice a string up into argc/argv. | |
287 | */ | |
9069f68e GM |
288 | |
289 | int slrflag; | |
290 | ||
ba5bf9f5 SL |
291 | makeargv() |
292 | { | |
293 | char **argp; | |
294 | char *slurpstring(); | |
295 | ||
296 | margc = 0; | |
297 | argp = margv; | |
298 | stringbase = line; /* scan from first of buffer */ | |
299 | argbase = argbuf; /* store from first of buffer */ | |
9069f68e GM |
300 | slrflag = 0; |
301 | while (*argp++ = slurpstring()) | |
ba5bf9f5 SL |
302 | margc++; |
303 | } | |
304 | ||
305 | /* | |
306 | * Parse string into argbuf; | |
307 | * implemented with FSM to | |
308 | * handle quoting and strings | |
309 | */ | |
310 | char * | |
311 | slurpstring() | |
312 | { | |
313 | int got_one = 0; | |
314 | register char *sb = stringbase; | |
315 | register char *ap = argbase; | |
316 | char *tmp = argbase; /* will return this if token found */ | |
317 | ||
9069f68e GM |
318 | if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */ |
319 | switch (slrflag) { /* and $ as token for macro invoke */ | |
320 | case 0: | |
321 | slrflag++; | |
322 | stringbase++; | |
323 | return ((*sb == '!') ? "!" : "$"); | |
ff00793c | 324 | /* NOTREACHED */ |
9069f68e GM |
325 | case 1: |
326 | slrflag++; | |
327 | altarg = stringbase; | |
328 | break; | |
329 | default: | |
330 | break; | |
331 | } | |
332 | } | |
333 | ||
ba5bf9f5 SL |
334 | S0: |
335 | switch (*sb) { | |
336 | ||
337 | case '\0': | |
338 | goto OUT; | |
339 | ||
340 | case ' ': | |
341 | case '\t': | |
342 | sb++; goto S0; | |
343 | ||
344 | default: | |
9069f68e GM |
345 | switch (slrflag) { |
346 | case 0: | |
347 | slrflag++; | |
348 | break; | |
349 | case 1: | |
350 | slrflag++; | |
351 | altarg = sb; | |
352 | break; | |
353 | default: | |
354 | break; | |
355 | } | |
ba5bf9f5 SL |
356 | goto S1; |
357 | } | |
358 | ||
359 | S1: | |
360 | switch (*sb) { | |
361 | ||
362 | case ' ': | |
363 | case '\t': | |
364 | case '\0': | |
365 | goto OUT; /* end of token */ | |
366 | ||
367 | case '\\': | |
368 | sb++; goto S2; /* slurp next character */ | |
369 | ||
370 | case '"': | |
371 | sb++; goto S3; /* slurp quoted string */ | |
372 | ||
373 | default: | |
374 | *ap++ = *sb++; /* add character to token */ | |
375 | got_one = 1; | |
376 | goto S1; | |
377 | } | |
378 | ||
379 | S2: | |
380 | switch (*sb) { | |
381 | ||
382 | case '\0': | |
383 | goto OUT; | |
384 | ||
385 | default: | |
386 | *ap++ = *sb++; | |
387 | got_one = 1; | |
388 | goto S1; | |
389 | } | |
390 | ||
391 | S3: | |
392 | switch (*sb) { | |
393 | ||
394 | case '\0': | |
395 | goto OUT; | |
396 | ||
397 | case '"': | |
398 | sb++; goto S1; | |
399 | ||
400 | default: | |
401 | *ap++ = *sb++; | |
402 | got_one = 1; | |
403 | goto S3; | |
404 | } | |
405 | ||
406 | OUT: | |
407 | if (got_one) | |
408 | *ap++ = '\0'; | |
409 | argbase = ap; /* update storage pointer */ | |
410 | stringbase = sb; /* update scan pointer */ | |
9069f68e | 411 | if (got_one) { |
ba5bf9f5 | 412 | return(tmp); |
9069f68e GM |
413 | } |
414 | switch (slrflag) { | |
415 | case 0: | |
416 | slrflag++; | |
417 | break; | |
418 | case 1: | |
419 | slrflag++; | |
420 | altarg = (char *) 0; | |
421 | break; | |
422 | default: | |
423 | break; | |
424 | } | |
ba5bf9f5 SL |
425 | return((char *)0); |
426 | } | |
427 | ||
428 | #define HELPINDENT (sizeof ("directory")) | |
429 | ||
430 | /* | |
431 | * Help command. | |
432 | * Call each command handler with argc == 0 and argv[0] == name. | |
433 | */ | |
434 | help(argc, argv) | |
435 | int argc; | |
436 | char *argv[]; | |
437 | { | |
a7d96790 | 438 | extern struct cmd cmdtab[]; |
ba5bf9f5 SL |
439 | register struct cmd *c; |
440 | ||
441 | if (argc == 1) { | |
9069f68e | 442 | register int i, j, w, k; |
ba5bf9f5 SL |
443 | int columns, width = 0, lines; |
444 | extern int NCMDS; | |
445 | ||
446 | printf("Commands may be abbreviated. Commands are:\n\n"); | |
447 | for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { | |
448 | int len = strlen(c->c_name); | |
449 | ||
450 | if (len > width) | |
451 | width = len; | |
452 | } | |
453 | width = (width + 8) &~ 7; | |
454 | columns = 80 / width; | |
455 | if (columns == 0) | |
456 | columns = 1; | |
457 | lines = (NCMDS + columns - 1) / columns; | |
458 | for (i = 0; i < lines; i++) { | |
459 | for (j = 0; j < columns; j++) { | |
460 | c = cmdtab + j * lines + i; | |
9069f68e | 461 | if (c->c_name && (!proxy || c->c_proxy)) { |
14433a73 | 462 | printf("%s", c->c_name); |
9069f68e GM |
463 | } |
464 | else if (c->c_name) { | |
465 | for (k=0; k < strlen(c->c_name); k++) { | |
04480325 | 466 | (void) putchar(' '); |
9069f68e GM |
467 | } |
468 | } | |
ba5bf9f5 SL |
469 | if (c + lines >= &cmdtab[NCMDS]) { |
470 | printf("\n"); | |
471 | break; | |
472 | } | |
473 | w = strlen(c->c_name); | |
474 | while (w < width) { | |
475 | w = (w + 8) &~ 7; | |
04480325 | 476 | (void) putchar('\t'); |
ba5bf9f5 SL |
477 | } |
478 | } | |
479 | } | |
480 | return; | |
481 | } | |
482 | while (--argc > 0) { | |
483 | register char *arg; | |
484 | arg = *++argv; | |
485 | c = getcmd(arg); | |
486 | if (c == (struct cmd *)-1) | |
487 | printf("?Ambiguous help command %s\n", arg); | |
488 | else if (c == (struct cmd *)0) | |
489 | printf("?Invalid help command %s\n", arg); | |
490 | else | |
491 | printf("%-*s\t%s\n", HELPINDENT, | |
492 | c->c_name, c->c_help); | |
493 | } | |
494 | } | |
495 | ||
496 | /* | |
497 | * Call routine with argc, argv set from args (terminated by 0). | |
498 | */ | |
04480325 | 499 | /*VARARGS1*/ |
ba5bf9f5 SL |
500 | call(routine, args) |
501 | int (*routine)(); | |
502 | int args; | |
503 | { | |
504 | register int *argp; | |
505 | register int argc; | |
506 | ||
507 | for (argc = 0, argp = &args; *argp++ != 0; argc++) | |
508 | ; | |
509 | (*routine)(argc, &args); | |
510 | } |