home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnunet10.zip / source / ftp / main.c < prev    next >
C/C++ Source or Header  |  1996-07-27  |  12KB  |  594 lines

  1. /*
  2.  * Copyright (c) 1985, 1989, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char copyright[] =
  36. "@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
  37.     The Regents of the University of California.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)main.c    8.6 (Berkeley) 10/9/94";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * FTP User Program -- Command Interface.
  46.  */
  47. /*#include <sys/ioctl.h>*/
  48. #include <sys/types.h>
  49. #include <sys/socket.h>
  50.  
  51. #include <arpa/ftp.h>
  52.  
  53. #include <ctype.h>
  54. #ifndef __EMX__
  55. # include <err.h>
  56. #endif
  57. #include <netdb.h>
  58. #include <pwd.h>
  59. #include <signal.h>
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <unistd.h>
  63. #include <getopt.h>
  64. #include <version.h>
  65.  
  66. #include "ftp_var.h"
  67.  
  68. #define USAGE "Usage: %s [OPTION...] [HOST [PORT]]\n"
  69.  
  70. /* basename (argv[0]).  NetBSD, linux, & gnu libc all define it.  */
  71. #ifndef __EMX__
  72. extern char *__progname;
  73. #else
  74. char *__progname;
  75. #endif
  76.  
  77. /* Print a help message describing all options to STDOUT and exit with a
  78.    status of 0.  */
  79. static void
  80. ohelp ()
  81. {
  82.   fprintf (stdout, USAGE, __progname);
  83.   puts ("\n\
  84.   -d, --debug                Turn on debugging mode\n\
  85.   -g, --no-glob              Turn off file name globbing\n\
  86.   -i, --no-prompt            Don't prompt during multiple-file transfers\n\
  87.   -n, --no-login             Don't automatically login to the remove system\n\
  88.   -t, --trace                Enable packet tracing\n\
  89.   -v, --verbose              Be verbose\n\
  90.       --help                 Give this help list\n\
  91.       --version              Print program version");
  92.   exit (0);
  93. }
  94.  
  95. /* Print a message saying to use --help to STDERR, and exit with a status of
  96.    1.  */
  97. static void
  98. try_help ()
  99. {
  100.   fprintf (stderr, "Try `%s --help' for more information.\n", __progname);
  101.   exit (1);
  102. }
  103.  
  104. /* Print a usage message to STDERR and exit with a status of 1.  */
  105. static void
  106. usage ()
  107. {
  108.   fprintf (stderr, USAGE, __progname);
  109.   try_help ();
  110. }
  111.  
  112. static struct option long_options[] =
  113. {
  114.   { "trace", no_argument, 0, 't' },
  115.   { "verbose", no_argument, 0, 'v' },
  116.   { "no-login", no_argument, 0, 'n' },
  117.   { "no-prompt", no_argument, 0, 'i' },
  118.   { "debug", no_argument, 0, 'd' },
  119.   { "no-glob", no_argument, 0, 'g' },
  120.   { "help", no_argument, 0, '&' },
  121.   { "version", no_argument, 0, 'V' },
  122.   { 0 }
  123. };
  124.  
  125. int
  126. main(argc, argv)
  127.     int argc;
  128.     char *argv[];
  129. {
  130.     int ch, top;
  131.     struct passwd *pw = NULL;
  132.     char *cp;
  133.  
  134. #ifdef __EMX__
  135.         __progname=argv[0];
  136. #endif
  137.  
  138.     sp = getservbyname("ftp", "tcp");
  139.     if (sp == 0)
  140.             errx(1, "ftp/tcp: unknown service");
  141.  
  142.     doglob = 1;
  143.     interactive = 1;
  144.     autologin = 1;
  145.  
  146.     while ((ch = getopt_long (argc, argv, "dgintv", long_options, 0))
  147.            != EOF)
  148.     {
  149.         switch (ch) {
  150.         case 'd':
  151.             options |= SO_DEBUG;
  152.             debug++;
  153.             break;
  154.             
  155.         case 'g':
  156.             doglob = 0;
  157.             break;
  158.  
  159.         case 'i':
  160.             interactive = 0;
  161.             break;
  162.  
  163.         case 'n':
  164.             autologin = 0;
  165.             break;
  166.  
  167.         case 't':
  168.             trace++;
  169.             break;
  170.  
  171.         case 'v':
  172.             verbose++;
  173.             break;
  174.  
  175.         case '&':
  176.             ohelp ();
  177.         case 'V':
  178.             puts (inetutils_version);
  179.             exit (0);
  180.  
  181.         case '?':
  182.             try_help ();
  183.  
  184.         default:
  185.             usage ();
  186.         }
  187.     }
  188.     argc -= optind;
  189.     argv += optind;
  190.  
  191.     fromatty = isatty(fileno(stdin));
  192.     if (fromatty)
  193.         verbose++;
  194.     cpend = 0;    /* no pending replies */
  195.     proxy = 0;    /* proxy not active */
  196.     passivemode = 0; /* passive mode not active */
  197.     crflag = 1;    /* strip c.r. on ascii gets */
  198.     sendport = -1;    /* not using ports */
  199.     /*
  200.      * Set up the home directory in case we're globbing.
  201.      */
  202.     cp = getlogin();
  203.     if (cp != NULL) {
  204.         pw = getpwnam(cp);
  205.     }
  206.     if (pw == NULL)
  207.         pw = getpwuid(getuid());
  208.     if (pw != NULL) {
  209.         char *buf = malloc (strlen (pw->pw_dir) + 1);
  210.         if (buf) {
  211.             strcpy(buf, pw->pw_dir);
  212.             home = buf;
  213.         }
  214.     }
  215.     if (argc > 0) {
  216.         char *xargv[5];
  217.         extern char *__progname;
  218.  
  219.         if (setjmp(toplevel))
  220.             exit(0);
  221.         (void) signal(SIGINT, intr);
  222.         (void) signal(SIGPIPE, lostpeer);
  223.         xargv[0] = __progname;
  224.         xargv[1] = argv[0];
  225.         xargv[2] = argv[1];
  226.         xargv[3] = argv[2];
  227.         xargv[4] = NULL;
  228.         setpeer(argc+1, xargv);
  229.     }
  230.     top = setjmp(toplevel) == 0;
  231.     if (top) {
  232.         (void) signal(SIGINT, intr);
  233.         (void) signal(SIGPIPE, lostpeer);
  234.     }
  235.     for (;;) {
  236.         cmdscanner(top);
  237.         top = 1;
  238.     }
  239. }
  240.  
  241. void
  242. intr()
  243. {
  244.  
  245.     longjmp(toplevel, 1);
  246. }
  247.  
  248. void
  249. lostpeer()
  250. {
  251.  
  252.     if (connected) {
  253.         if (cout != NULL) {
  254.             (void) shutdown(fileno(cout), 1+1);
  255.             (void) fclose(cout);
  256.             cout = NULL;
  257.         }
  258.         if (data >= 0) {
  259.             (void) shutdown(data, 1+1);
  260.             (void) close(data);
  261.             data = -1;
  262.         }
  263.         connected = 0;
  264.     }
  265.     pswitch(1);
  266.     if (connected) {
  267.         if (cout != NULL) {
  268.             (void) shutdown(fileno(cout), 1+1);
  269.             (void) fclose(cout);
  270.             cout = NULL;
  271.         }
  272.         connected = 0;
  273.     }
  274.     proxflag = 0;
  275.     pswitch(0);
  276. }
  277.  
  278. /*
  279. char *
  280. tail(filename)
  281.     char *filename;
  282. {
  283.     char *s;
  284.     
  285.     while (*filename) {
  286.         s = strrchr(filename, '/');
  287.         if (s == NULL)
  288.             break;
  289.         if (s[1])
  290.             return (s + 1);
  291.         *s = '\0';
  292.     }
  293.     return (filename);
  294. }
  295. */
  296.  
  297. /*
  298.  * Command parser.
  299.  */
  300. void
  301. cmdscanner(top)
  302.     int top;
  303. {
  304.     struct cmd *c;
  305.     int l;
  306.  
  307.     if (!top)
  308.         (void) putchar('\n');
  309.     for (;;) {
  310.         if (fromatty) {
  311.             printf("ftp> ");
  312.             (void) fflush(stdout);
  313.         }
  314.         if (fgets(line, sizeof line, stdin) == NULL)
  315.             quit(0, 0);
  316.         l = strlen(line);
  317.         if (l == 0)
  318.             break;
  319.         if (line[--l] == '\n') {
  320.             if (l == 0)
  321.                 break;
  322.             line[l] = '\0';
  323.         } else if (l == sizeof(line) - 2) {
  324.             printf("sorry, input line too long\n");
  325.             while ((l = getchar()) != '\n' && l != EOF)
  326.                 /* void */;
  327.             break;
  328.         } /* else it was a line without a newline */
  329.         makeargv();
  330.         if (margc == 0) {
  331.             continue;
  332.         }
  333.         c = getcmd(margv[0]);
  334.         if (c == (struct cmd *)-1) {
  335.             printf("?Ambiguous command\n");
  336.             continue;
  337.         }
  338.         if (c == 0) {
  339.             printf("?Invalid command\n");
  340.             continue;
  341.         }
  342.         if (c->c_conn && !connected) {
  343.             printf("Not connected.\n");
  344.             continue;
  345.         }
  346.         (*c->c_handler)(margc, margv);
  347.         if (bell && c->c_bell)
  348.             (void) putchar('\007');
  349.         if (c->c_handler != help)
  350.             break;
  351.     }
  352.     (void) signal(SIGINT, intr);
  353.     (void) signal(SIGPIPE, lostpeer);
  354. }
  355.  
  356. struct cmd *
  357. getcmd(name)
  358.     char *name;
  359. {
  360.     char *p, *q;
  361.     struct cmd *c, *found;
  362.     int nmatches, longest;
  363.  
  364.     longest = 0;
  365.     nmatches = 0;
  366.     found = 0;
  367.     for (c = cmdtab; p = c->c_name; c++) {
  368.         for (q = name; *q == *p++; q++)
  369.             if (*q == 0)        /* exact match? */
  370.                 return (c);
  371.         if (!*q) {            /* the name was a prefix */
  372.             if (q - name > longest) {
  373.                 longest = q - name;
  374.                 nmatches = 1;
  375.                 found = c;
  376.             } else if (q - name == longest)
  377.                 nmatches++;
  378.         }
  379.     }
  380.     if (nmatches > 1)
  381.         return ((struct cmd *)-1);
  382.     return (found);
  383. }
  384.  
  385. /*
  386.  * Slice a string up into argc/argv.
  387.  */
  388.  
  389. int slrflag;
  390.  
  391. void
  392. makeargv()
  393. {
  394.     char **argp;
  395.  
  396.     margc = 0;
  397.     argp = margv;
  398.     stringbase = line;        /* scan from first of buffer */
  399.     argbase = argbuf;        /* store from first of buffer */
  400.     slrflag = 0;
  401.     while (*argp++ = slurpstring())
  402.         margc++;
  403. }
  404.  
  405. /*
  406.  * Parse string into argbuf;
  407.  * implemented with FSM to
  408.  * handle quoting and strings
  409.  */
  410. char *
  411. slurpstring()
  412. {
  413.     int got_one = 0;
  414.     char *sb = stringbase;
  415.     char *ap = argbase;
  416.     char *tmp = argbase;        /* will return this if token found */
  417.  
  418.     if (*sb == '!' || *sb == '$') {    /* recognize ! as a token for shell */
  419.         switch (slrflag) {    /* and $ as token for macro invoke */
  420.             case 0:
  421.                 slrflag++;
  422.                 stringbase++;
  423.                 return ((*sb == '!') ? "!" : "$");
  424.                 /* NOTREACHED */
  425.             case 1:
  426.                 slrflag++;
  427.                 altarg = stringbase;
  428.                 break;
  429.             default:
  430.                 break;
  431.         }
  432.     }
  433.  
  434. S0:
  435.     switch (*sb) {
  436.  
  437.     case '\0':
  438.         goto OUT;
  439.  
  440.     case ' ':
  441.     case '\t':
  442.         sb++; goto S0;
  443.  
  444.     default:
  445.         switch (slrflag) {
  446.             case 0:
  447.                 slrflag++;
  448.                 break;
  449.             case 1:
  450.                 slrflag++;
  451.                 altarg = sb;
  452.                 break;
  453.             default:
  454.                 break;
  455.         }
  456.         goto S1;
  457.     }
  458.  
  459. S1:
  460.     switch (*sb) {
  461.  
  462.     case ' ':
  463.     case '\t':
  464.     case '\0':
  465.         goto OUT;    /* end of token */
  466.  
  467.     case '\\':
  468.         sb++; goto S2;    /* slurp next character */
  469.  
  470.     case '"':
  471.         sb++; goto S3;    /* slurp quoted string */
  472.  
  473.     default:
  474.         *ap++ = *sb++;    /* add character to token */
  475.         got_one = 1;
  476.         goto S1;
  477.     }
  478.  
  479. S2:
  480.     switch (*sb) {
  481.  
  482.     case '\0':
  483.         goto OUT;
  484.  
  485.     default:
  486.         *ap++ = *sb++;
  487.         got_one = 1;
  488.         goto S1;
  489.     }
  490.  
  491. S3:
  492.     switch (*sb) {
  493.  
  494.     case '\0':
  495.         goto OUT;
  496.  
  497.     case '"':
  498.         sb++; goto S1;
  499.  
  500.     default:
  501.         *ap++ = *sb++;
  502.         got_one = 1;
  503.         goto S3;
  504.     }
  505.  
  506. OUT:
  507.     if (got_one)
  508.         *ap++ = '\0';
  509.     argbase = ap;            /* update storage pointer */
  510.     stringbase = sb;        /* update scan pointer */
  511.     if (got_one) {
  512.         return (tmp);
  513.     }
  514.     switch (slrflag) {
  515.         case 0:
  516.             slrflag++;
  517.             break;
  518.         case 1:
  519.             slrflag++;
  520.             altarg = (char *) 0;
  521.             break;
  522.         default:
  523.             break;
  524.     }
  525.     return ((char *)0);
  526. }
  527.  
  528. #define HELPINDENT ((int) sizeof ("directory"))
  529.  
  530. /*
  531.  * Help command.
  532.  * Call each command handler with argc == 0 and argv[0] == name.
  533.  */
  534. void
  535. help(argc, argv)
  536.     int argc;
  537.     char *argv[];
  538. {
  539.     struct cmd *c;
  540.  
  541.     if (argc == 1) {
  542.         int i, j, w, k;
  543.         int columns, width = 0, lines;
  544.  
  545.         printf("Commands may be abbreviated.  Commands are:\n\n");
  546.         for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
  547.             int len = strlen(c->c_name);
  548.  
  549.             if (len > width)
  550.                 width = len;
  551.         }
  552.         width = (width + 8) &~ 7;
  553.         columns = 80 / width;
  554.         if (columns == 0)
  555.             columns = 1;
  556.         lines = (NCMDS + columns - 1) / columns;
  557.         for (i = 0; i < lines; i++) {
  558.             for (j = 0; j < columns; j++) {
  559.                 c = cmdtab + j * lines + i;
  560.                 if (c->c_name && (!proxy || c->c_proxy)) {
  561.                     printf("%s", c->c_name);
  562.                 }
  563.                 else if (c->c_name) {
  564.                     for (k=0; k < strlen(c->c_name); k++) {
  565.                         (void) putchar(' ');
  566.                     }
  567.                 }
  568.                 if (c + lines >= &cmdtab[NCMDS]) {
  569.                     printf("\n");
  570.                     break;
  571.                 }
  572.                 w = strlen(c->c_name);
  573.                 while (w < width) {
  574.                     w = (w + 8) &~ 7;
  575.                     (void) putchar('\t');
  576.                 }
  577.             }
  578.         }
  579.         return;
  580.     }
  581.     while (--argc > 0) {
  582.         char *arg;
  583.         arg = *++argv;
  584.         c = getcmd(arg);
  585.         if (c == (struct cmd *)-1)
  586.             printf("?Ambiguous help command %s\n", arg);
  587.         else if (c == (struct cmd *)0)
  588.             printf("?Invalid help command %s\n", arg);
  589.         else
  590.             printf("%-*s\t%s\n", HELPINDENT,
  591.                 c->c_name, c->c_help);
  592.     }
  593. }
  594.