rl.c 11.5 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
/*
 * @(#) $Id$
 *
 * rl.c
 */

/* lsftp, an implementation of the sftp protocol
 *
 * Copyright (C) 2001, Pontus Sköld
 * 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
 */

int lsftp_rl_line_done = 0;
char* lsftp_rl_line = 0;

int sloppy_complete = 2;


#include "rl.h"

35
#ifdef WITH_READLINE
36
37
38
39
40
#include "str_utils.h"

static int lsftp_rl_line_inited=0;

#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
41
# define RL_FILENAME_COMPLETION_FUNCTION rl_filename_completion_function
42
#else /* HAVE_RL_FILENAME_COMPLETION_FUNCTION */
43
44
45
46
47
# ifdef HAVE_FILENAME_COMPLETION_FUNCTION
#  define RL_FILENAME_COMPLETION_FUNCTION filename_completion_function
# else
#  error "lsftp doesn't work with your readline library, please disable readline and contact the author."
# endif /* HAVE_FILENAME_COMPLETION_FUNCTION */
48
49
50
#endif /* HAVE_RL_FILENAME_COMPLETION_FUNCTION */

#ifdef HAVE_RL_COMPLETION_MATCHES
51
# define RL_COMPLETION_MATCHES rl_completion_matches
52
#else /* HAVE_RL_COMPLETION_MATCHES */
53
54
55
56
57
# ifdef HAVE_COMPLETION_MATCHES
#  define RL_COMPLETION_MATCHES completion_matches
# else
#  error "lsftp doesn't work with your readline library, please disable readline and contact the author."
# endif /* HAVE_COMPLETION_MATCHES */
58
59
60
61
#endif /* HAVE_RL_COMPLETION_MATCHES */


#ifdef HAVE_RL_CHAR_IS_QUOTED_P
62
# define RL_CHAR_IS_QUOTED rl_char_is_quoted_p
63
#else /* HAVE_RL_CHAR_IS_QUOTED */
64
65
66
67
68
# ifdef HAVE_CHAR_IS_QUOTED
#  define RL_CHAR_IS_QUOTED char_is_quoted
# else
#  error "lsftp doesn't work with your readline library, please disable readline and contact the author."
# endif /* HAVE_CHAR_IS_QUOTED */
69
70
#endif /* HAVE_RL_CHAR_IS_QUOTED */

71
72
73
74
75
76


/* FIXME: review readline support, proper way to do it? */



77
78
79
80
void lsftp_rl_init()
{
  /* Basic readline initialization goes here */

81
# ifdef WITH_HISTORY
82
83
84
85
86
87
88
89
90
91
92

  char* histfname = lsftp_rl_history_fname();

  using_history(); /* Use history function */

  if( histfname )
    {
      read_history( histfname ); /* Try to load old history */
      free( histfname );
    }

93
# endif /* WITH_HISTORY */
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

  /* Allow conditional parsing of the ~/.inputrc file. */
  rl_readline_name = "lsftp";

  rl_completer_quote_characters = "\'\"";  /* Quotes are performed by ", ' and \ */
  rl_basic_word_break_characters =" \n\t"; /* Only whitespace break words */

  RL_CHAR_IS_QUOTED = char_quoted;
  rl_attempted_completion_function = (CPPFunction *)lsftp_rl_completion;


  interactive = 1; /* Set the interactive flag in the main program */
}

void lsftp_rl_exit()
{
  /* Called to finish business with readline, only saves history and 
   * makes sure we don't have no callback handler */

113
# ifdef WITH_HISTORY
114
115
116
117
118
119
120
121
122
123
  char* histfname = lsftp_rl_history_fname();

  using_history(); /* Use history function */

  if( histfname )
    {
      write_history( histfname ); /* Try to load old history */
      free( histfname );
    }

124
# endif /* WITH_HISTORY */
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
  rl_callback_handler_remove();

}



int char_quoted( char* text, int index )
{
  int i = 0;

  int literalq = 0;
  int nextliteral = 0;
  int normalq = 0;

  while( i < index )  /* Found the correct position yet? */
    {
      
      if( nextliteral )
	{
	  nextliteral = 0;
	  i++;
	}
      else
	{
	  if( literalq )                /* In a 'foo block? */
	    {
	      if( '\'' == text[i] )      /* Finishing bar' ? */
		{
		  literalq = 0;
		  i++;
		}
	    }
	  else
	    {
	      /* Neither nextliteral or literalq */
	      
	      switch( text[i] )
		{
		case '\'':
		  literalq = 1;
		  i++;
		  break;
		  
		case '\\':
		  nextliteral = 1;
		  i++;
		  break;
		  
		default: /* Not a quote */
		  i++;
		}
	    }
	}
    }
    

    return normalq || nextliteral || literalq;
}

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
char** lsftp_rl_completion(char* text, int start, int end)
{
  char** matches=NULL;
  int s;

  /* If this word is at the start of the line, then it is a command to
   * complete. 
n   */

  rl_completion_append_character = ' '; /* Trailing space after word */

  s = lsftp_s_skip( rl_line_buffer, " \n\t\r" ) - rl_line_buffer;

  if ( start == s ) /* The first word should be a command, even with spaces in front */
    {
      matches = RL_COMPLETION_MATCHES( 
				      text, 
				      lsftp_rl_command_generator 
				      );  
    }
  else
    {
      /* We're not completing the command */
      char tmp[256];
      int i=0;
      char* cmdname;

      /* Get the first word from the line (the command) */

      lsftp_s_strtok( rl_line_buffer+s," \n\t\r", tmp, 256 );

      while( (cmdname = commands[i].name) )
	if ( ! strncmp( cmdname, tmp, commands[i++].uniquelen ) )       /* We've got the command? */
	  switch ( commands[i-1].arg_type )
	    {

	    case COMMAND: /* Command takes a command as argument? */
	      matches = RL_COMPLETION_MATCHES( 
					      text, 
					      lsftp_rl_command_generator
					      );
	      break;

	    case REMOTEFILE:  /* Remote file or dir as argument? */
	      matches = RL_COMPLETION_MATCHES( 
					      text, 
					      lsftp_rl_remotefile_generator
					      );
	      break;

	    case LOCALFILE:
	      matches = RL_COMPLETION_MATCHES(
					      text, 
					      RL_FILENAME_COMPLETION_FUNCTION
					      );
	      break;

	    default:
	    case NOARG:  /* Doesn't take any argument? */
	      matches = RL_COMPLETION_MATCHES(
					      text, 
					      lsftp_rl_no_generator 
					      ); 
	      break;
	    }

      if( !matches ) /* No matching command? */
	  {
	    /* No command yet given - complete command */
	    matches = RL_COMPLETION_MATCHES( 
					  text, 
					  lsftp_rl_command_generator 
					  );  
	  }
    }

  return (matches);
}



/* Generator function for command completion.  STATE lets us know whether
   to start from scratch; without any state (i.e. STATE == 0), then we
   start at the top of the list. */
/* FIXME: type? */
char* lsftp_rl_command_generator (char* text, int state)
{
  static int list_index, len;
  char* name;

  /* If this is a new word to complete, initialize now.  This includes
     saving the length of TEXT for efficiency, and initializing the index
     variable to 0. */
  if (!state)
    {
      list_index = 0;
      len = strlen (text);
    }

  /* Return the next name which partially matches from the command list. */
  while ((name = commands[list_index].name))
    {
      list_index++;

      if (strncmp (name, text, len) == 0)
        return strdup(name);
    }

  rl_attempted_completion_over = 1; /* Don't do local filename completion */

  /* If no names matched, then return NULL. */
  return ((char *)NULL);
}



/* Generator function for remote completion.  STATE lets us know whether
   to start from scratch; without any state (i.e. STATE == 0), then we
   start at the top of the list. */
char* lsftp_rl_remotefile_generator( char* text, int state )
{
  static int list_index;
  char* name;
  static char** glob;

  /* If this is a new word to complete, initialize now. */

  if( !state )
    {

      name = lsftp_concat( text, "*" ); /* Make name contain text* */ 
      
      list_index = 0;
      glob = 0;

      switch( sloppy_complete )
	{
	case 0: /* No sloppy completion */

	  if( name ) /* concat OK? */
	    /* Do non-sloppy globbing */
	    glob = lsftp_dc_r_startglob( name, 0, 0 ); 

	  break;

	case 1: /* Sloppy completion */

	  if( name ) /* concat OK? */
	    /* Do sloppy globbing */
	    glob = lsftp_dc_r_startglob( name, 1, 0 ); 

	  break;

	default:
	case 2: /* Try sloppy completion */
	  {
	    int matches = 0;

	    if( name ) /* concat OK? */
	      /* Do sloppy globbing */
	      glob = lsftp_dc_r_startglob( name, 1, 0 ); 

	    matches = lsftp_dc_numglob( glob );

	    if( !matches ) /* Glob gave no matches? */
	      {
		lsftp_dc_endglob( glob ); /* Free old glob */
		/* Do non-sloppy globbing */
		glob = lsftp_dc_r_startglob( name, 0, 0 ); 
	      }
	  }
	}

      
      free( name );

    }

  /* Return the next match */

  if( glob[list_index] ) /* Not the end? */
    {
      char* tmp;
      char* tmp2;

      if( !lsftp_dc_r_isdir( glob[list_index] ) )
	return lsftp_quote( glob[list_index++] );
      
      /* Directory - add trailing slash */

      rl_completion_append_character = 0; /* No trailing space */

      tmp = lsftp_concat( glob[list_index++], "/" ); 
      
      if( !tmp ) /* concat failed? */ 
	return lsftp_quote( glob[list_index-1] );
      
      tmp2 = lsftp_quote( tmp );
      
      if( !tmp2 ) /* Failed ? */
	return tmp;

      free( tmp );
      return tmp2;

    }

  rl_attempted_completion_over = 1; /* Don't do local filename completion */

  lsftp_dc_endglob( glob ); /* Free memory used by glob */

  list_index = 0;
  glob = 0;

  /* If no names matched, then return NULL. */
  return ((char *)NULL);
}




/* Generator function for NOARG completion. 
   no arguments => nothing matches */ 

/* FIXME: Type? */ 
char* lsftp_rl_no_generator (char* text, int state)
{
  if(!state) /* On the first call, return the empty string */
    return strdup("");

  rl_attempted_completion_over = 1;

  return ((char *)NULL);
}



void lsftp_rl_unuse_line(char* line)
{
  /* A line has been malloced, so we free it */

  free(line);
}

void lsftp_rl_init_get_line(char* prompt)
{
  if( ! lsftp_rl_line_inited )
    {
      rl_callback_handler_install (prompt, &lsftp_rl_lhandler);
      
      lsftp_rl_line_inited=1; /* We've started */
      lsftp_rl_line_done=0;
    }
}


void lsftp_rl_check_input()
{
  rl_callback_read_char();
}

char* lsftp_rl_get_line()
{
  lsftp_rl_init_get_line(""); 

  while(! lsftp_rl_line_done)
    {
      lsftp_rl_check_input();
      
    }

  return lsftp_rl_line;
}

void lsftp_rl_lhandler(char* line)
{
  rl_callback_handler_remove();
  lsftp_rl_line_done=1;
  lsftp_rl_line_inited=0; 
  lsftp_rl_line=line;

469
# ifdef WITH_HISTORY
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  if ( line && *line) /* Got a line and it's not empty? */
    add_history(line); /* Save to history */
# endif

}

char* lsftp_rl_history_fname()
{
  char* username;
  char* fname;

  if( mgetenv( LSFTP_HISTNAME_VAR ) )           /* If the filename is given */
    return strdup( mgetenv( LSFTP_HISTNAME_VAR ) );

  if( mgetenv( "HOME" ) )   /* HOME is set? */
    {
      int homelen = strlen( mgetenv( "HOME" ) );
      int len = homelen + strlen( LSFTP_HIST_FILENAME ) + 1; /* We'll have $HOME/.lsftp */

      fname = malloc( len );

      if( !fname ) /* Failed to allocate memory? */
	return 0;

      strncpy( fname, mgetenv( "HOME" ), homelen ); /* Copy $HOME */
      strncpy( fname + homelen, LSFTP_HIST_FILENAME, len - homelen ); /* Copy filename part */
      fname[len-1] = 0;                /* Make sure it's terminated */
      
      return fname;
    }
  
  return 0;
}


505
#else /* ifdef WITH_READLINE */
506
507
508

/* Readline not found */

Niels Möller's avatar
Niels Möller committed
509
510
inline void
lsftp_rl_init_get_line(char* prompt UNUSED)
511
512
513
514
{
  /* Do nothing */
}

Niels Möller's avatar
Niels Möller committed
515
516
char *
lsftp_rl_get_line(void )
517
518
519
520
521
522
523
{
     /*

      */
  return 0;
}

Niels Möller's avatar
Niels Möller committed
524
525
void
lsftp_rl_check_input(void)
526
527
{
}
Niels Möller's avatar
Niels Möller committed
528
529
530

void
lsftp_rl_unuse_line(char* line)
531
532
533
534
{
  free(line);
}

Niels Möller's avatar
Niels Möller committed
535
536
void
lsftp_rl_init(void)
537
538
539
540
{
  interactive=0;
}

Niels Möller's avatar
Niels Möller committed
541
542
void
lsftp_rl_exit(void)
543
544
545
{
}

Niels Möller's avatar
Niels Möller committed
546
547
char*
lsftp_rl_history_fname(void)
548
549
550
551
{
  return 0;
}

552
#endif /* ifdef WITH_READLINE */
553