lsftp.c 4.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/*
 * lsftp.c
 */

/* lsftp, an implementation of the sftp protocol
 *
 * Copyright (C) 2001, Pontus Skld
 * Portions of this code originately from the readline manual
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "lsftp.h"

#include "werror.h"

int mainloop;    /* "Continue run" flag */
int interactive; /* Interactive mode flag */

const char *werror_program_name = "lsftp";



static int
lsftp_num_commands(int argc, char **argv)
{
  /* 
   * Returns how many trailing parameters to skip when
   * starting the SecSH client
   *
   */

  int i = argc - 1;     /* First element to investigate (argv[argc] should be NULL) */
  
  while( i > 0 )
    if( strcmp( LSFTP_COMMAND_MARKER, argv[i] ) ) /* Not a marker? */
      i--;
    else
      break;

  if( i )                      /* Found a marker? */
    return argc - i;           /* The number of parameters not to pass to secsh is argc - i */

  return 0;
}

static void
lsftp_interactive_mainloop(void)
{
  char* current_line;
  int fail = 0;

  mainloop=1;
  while( mainloop && !fail )
    {
      fd_set rfds;
      fd_set wfds;
      
      struct timeval tv;
      int retval;

      lsftp_rl_init_get_line( "command> " );

      while( !lsftp_rl_line_done )
	{
	  int lsh_w = lsftp_fd_write_net();
	  int lsh_r = lsftp_fd_read_net();
	  int try_write = 0;
	  int n;

	  FD_ZERO( &rfds );
	  FD_ZERO( &wfds );

	  /* Watch stdin (fd 0) to see when it has input. */
	  FD_SET( 0, &rfds );

	  if( lsftp_connected() )   /* Only if connected */
	    {

	      /* Anything from lsh would also be interesting */
	      FD_SET( lsh_r, &rfds );
	      
	      /* As would (pherhaps) if we can write to lsh */
	      
	      if( lsftp_want_to_write() )
		{
		  FD_SET( lsh_w, &wfds );
		  try_write = 1;
		}
	      
	      tv.tv_sec = 10;
	      tv.tv_usec = 0;
	      
	      if( 
		 try_write &&
		 lsh_w > lsh_r 
		 ) /* Calculate the n argument to select */
		n = lsh_w + 1;
	      else
		n = lsh_r + 1;
	    }
	  else
	    n = 1;


	  retval = select(n, &rfds, &wfds, NULL, &tv);

	  if( FD_ISSET( 0, &rfds ) )
	    lsftp_rl_check_input();

	  if( lsftp_connected() )
	    if( FD_ISSET( lsh_r, &rfds ) || 
		( 
		 try_write &&
		 FD_ISSET( lsh_w, &wfds )
		 )
		)
	      if( -1 == lsftp_callback() )
		fail++;
	}
      
      current_line = lsftp_rl_line;

      handle_command( current_line );

      free( current_line );
    }

}

static void
lsftp_noninteractive_mainloop(int startat, char **argv)
{
  int done = 0;
  int fail = 0;

  mainloop = 1;

  while( !done && !fail )
    {
      while( !fail && lsftp_active_cbs() )
	if( -1 == lsftp_callback() )            /* Checks itself with select */
	  {
	    printf( "Failure!\n");
	    fail++;
	  }

      if( argv[startat] && mainloop )            /* More commands? (no quit) */
	handle_command( argv[startat++] );         /* Do it */
      else
	done = 1;                                  /* Finito */
          
    }
}




int main(int argc, char** argv, char** envp)
{
  int i;

  do_gnu_stuff( argv ); /* Check for GNU thingies and act accordingly */

  lsftp_rl_init(); /* Safe even if built for noninteractive use only */
  mgetenv_init( envp ); /* Tell mgetenv about environment parameters */

  i = lsftp_num_commands( argc, argv );

  if( i > 1 )                      /* Anything after the last marker? */
    interactive = 0;               /* Non interactive mode */

  if( interactive )
    lsftp_welcome();

  lsftp_sftp_init();

  if( 1 < argc - i ) /* Only connect if given arguments */
  {
    lsftp_open_connection( argv, argc - i );
    lsftp_handshake();

    lsftp_do_cd( "" );                   /* Change to our home directory */
  }

  lsftp_dc_init( 1024 );                 /* Set up the directory cache */

  if( interactive )
    lsftp_interactive_mainloop();
  else
    if( i )
      lsftp_noninteractive_mainloop( argc - i + 1, argv );
    else
      printf( "%s seems to be built for noninteractive mode only,\n" 
	      "but you gave no command.\n", PACKAGE_NAME);

  lsftp_rl_exit();
  lsftp_close_connection();
  return 0;
}