~ubuntu-branches/ubuntu/breezy/malaga/breezy

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
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
469
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* Copyright (C) 1995 Bjoern Beutel. */

/* Description. =============================================================*/

/* Tools for a command line interpreter. */

/* Includes. ================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <string.h>
#include "basic.h"
#include "input.h"
#include "files.h"
#include "commands.h"
#ifdef UNIX
#include <signal.h>
#endif
#ifdef WINDOWS
#include <windows.h>
#endif

/* Types. ===================================================================*/

typedef struct /* An alias. */
{ 
  list_node_t *next;
  string_t name;
  string_t line; /* Command line this alias stands for. */
} alias_t;

/* Global variables. ========================================================*/

command_t **options;
/* The options that can be set by the set command.
 * This has to be set before executing "set_command". */

bool_t leave_program; /* Set to TRUE if program is to leave. */
bool_t leave_command_loop; /* Set to TRUE if command loop is to leave. */

/* Variables. ===============================================================*/

static command_t **commands_local; 
/* Local copy of command table for "help_command". */

static list_t alias_list;

/* Functions for user interrupts. ===========================================*/

#ifdef UNIX
static void 
set_user_break( int signal_number )
/* Signal handler for notification that user wants to interrupt a command. */
{ 
  user_break_requested = TRUE;
  signal( SIGINT, set_user_break );
}
#endif

#ifdef WINDOWS
static BOOL
set_user_break( DWORD signal_type )
/* Signal handler for notification that user wants to interrupt a command. */
{
  if (signal_type == CTRL_C_EVENT) 
  {
    user_break_requested = TRUE;
    return TRUE;
  }
  return FALSE;
}
#endif

/*---------------------------------------------------------------------------*/

void 
check_user_break( void )
/* Abort command execution if user has sent an interrupt signal. */
{ 
  if (user_break_requested)
  {
    user_break_requested = FALSE;
    complain( "User interrupt." );
  }
}

/* Functions for reading and executing commands. ============================*/

static command_t *
find_command( string_t command_name, command_t *commands[] )
/* Find the entry for COMMAND_NAME in COMMANDS and return it.
 * Return NULL if COMMAND_NAME can't be found. */
{ 
  int_t i;
  string_t names, name;
  bool_t found_command;
      
  for (i = 0; commands[i] != NULL; i++) 
  { 
    names = commands[i]->names;
    while (*names != EOS) 
    { 
      name = parse_word( &names );
      found_command = (strcmp_no_case( command_name, name ) == 0);
      free_mem( &name);
      if (found_command) 
	return commands[i];
    }
  }
  return NULL;
}

/*---------------------------------------------------------------------------*/

static void 
execute_command( string_t command_line, string_t command_type,
                 command_t *commands[], bool_t use_aliases )
/* Execute COMMAND_LINE using COMMANDS.
 * If there is no such command, print an error.
 * COMMAND_TYPE may be "command" or "option" and is used for error messages.
 * If USE_ALIASES == TRUE, look for the command in the list of aliases. */
{ 
  string_t line, command_name;
  string_t alias_line, line_p, name_readable; 
  command_t *command;
  bool_t command_found;
  alias_t *alias;
      
  /* Divide command line into strings COMMAND and ARGUMENTS. */
  line = command_name = NULL;
  TRY 
  { 
    line_p = line = replace_vars_in_string( command_line );
    parse_whitespace( &line_p );
    if (*line_p != EOS) 
    { 
      command_name = parse_word( &line_p );
      command_found = FALSE;
      user_break_requested = FALSE;
      commands_local = commands;
      command = find_command( command_name, commands );
      if (command != NULL) /* Execute the command. */
      {
	command_found = TRUE;
	command->command( line_p );
      } 
      else if (use_aliases) 
      { 
	FOREACH( alias, alias_list ) 
	{ 
	  if (strcmp_no_case( alias->name, command_name ) == 0) 
	  { 
	    command_found = TRUE;
	    alias_line = replace_arguments( alias->line, "a", line_p );
	    execute_command( alias_line, command_type, commands, FALSE );
	    free_mem( &alias_line );
	    break;
	  }
	}
      }
      if (! command_found) 
      { 
	name_readable = new_string_readable( command_name, NULL );
	free_mem( &command_name );
	command_name = name_readable;
	complain( "Unknown %s %s.", command_type, command_name );
      }
    }
  } 
  FINALLY 
  { 
    free_mem( &command_name );
    free_mem( &line );
  }
  END_TRY;
}

/*---------------------------------------------------------------------------*/

void 
execute_command_file( string_t command_file, string_t line_header, 
                      command_t *commands[] )
/* Execute commands in COMMAND_FILE using COMMANDS.
 * If LINE_HEADER != NULL, command lines must start with LINE_HEADER. */
{ 
  FILE *command_stream;
  string_t command_line, include_file, header, command_line_p;
  bool_t is_command_line;
  volatile int_t line_count;
  volatile bool_t leave_loop;

  command_stream = open_stream( command_file, "r" );
  leave_loop = FALSE;
  line_count = 0;
  while (! leave_loop) 
  { 
    command_line = header = NULL;
    TRY 
    { 
      /* Read a command line */
      command_line = read_line( command_stream );
      if (command_line == NULL) 
	leave_loop = TRUE;
      else 
      { 
	cut_comment( command_line );
	line_count++;
        command_line_p = command_line;
        if (*command_line_p == EOS) 
	  is_command_line = FALSE;
	else if (line_header == NULL) 
	  is_command_line = TRUE;
	else 
        { 
	  /* Check if current line begins with LINE_HEADER. */
	  header = parse_word( &command_line_p );
          is_command_line = FALSE;
          if (strcmp_no_case( header, line_header ) == 0) 
	    is_command_line = TRUE;
	  else if (strcmp_no_case( header, "include:" ) == 0) 
	  { 
	    include_file = parse_absolute_path( &command_line_p, 
						command_file );
            parse_end( &command_line_p );
            execute_command_file( include_file, line_header, commands );
            free_mem( &include_file );
          }
        }
        if (is_command_line) 
	  execute_command( command_line_p, "command", commands, TRUE );
      }
    } 
    IF_ERROR 
    {
      print_text( error_text, " (\"%s\", line %d)", 
		  name_in_path( command_file ), line_count );
    } 
    FINALLY 
    { 
      free_mem( &header );
      free_mem( &command_line );
    }
    END_TRY;
  }
  close_stream( &command_stream, command_file );
}

/*---------------------------------------------------------------------------*/

void 
command_loop( string_t prompt, command_t *commands[] )
/* Read user commands, look for them in COMMANDS and execute them
 * until a command function returns FALSE. */
{ 
  string_t command_line;

  /* Install a Ctrl-C handler. */
#ifdef UNIX
  signal( SIGINT, set_user_break );
#endif
#ifdef WINDOWS
  static bool_t user_break_installed;

  if (! user_break_installed)
    SetConsoleCtrlHandler( (PHANDLER_ROUTINE) set_user_break, TRUE );
  user_break_installed = TRUE;
#endif

  /* Repeat the command loop. */
  while (! leave_command_loop && ! leave_program) 
  { /* Read command line. */
    command_line = NULL;
    TRY 
    { 
      printf( "%s> ", prompt );
      command_line = read_line( stdin );
      if (command_line != NULL)
	execute_command( command_line, "command", commands, TRUE );
    }
    IF_ERROR
    { 
      printf( "%s\n", error_text->buffer );
      RESUME;
    } 
    FINALLY 
      free_mem( &command_line );
    END_TRY;
  }
  leave_command_loop = FALSE;
}

/*---------------------------------------------------------------------------*/

void 
free_aliases( void )
/* Free all currently defined aliases. */
{ 
  alias_t *alias;

  FOREACH_FREE( alias, alias_list ) 
  { 
    free_mem( &alias->name );
    free_mem( &alias->line );
  }
}

/* General commands. ========================================================*/

static void 
print_help_summary( command_t *commands[] )
/* Print names of all commands in COMMANDS. */
{ 
  int i, j;
  string_t names, name;

  for (i = 0; commands[i] != NULL; i++) 
  { 
    names = commands[i]->names;
    name = parse_word( &names );
    if (i > 0 && i % 5 == 0) 
      printf( "\n" );
    printf( "%s", name );
    for (j = strlen( name ); j < 15; j++) 
      printf( " " );
    free_mem( &name );
  }
  printf( "\n" );
}

/*---------------------------------------------------------------------------*/

static void 
print_help( string_t command_type, command_t *command )
/* Print help about COMMAND, 
 * which is of COMMAND_TYPE (either "command" or "option"). */
{ 
  string_t names, name;

  names = command->names;

  /* Print full name. */
  name = parse_word( &names );
  printf( "%s \"%s\"", command_type, name );
  free_mem( &name );

  /* Print shortcuts. */
  while (*names != EOS) 
  { 
    name = parse_word( &names );
    printf( ", \"%s\"", name );
    free_mem( &name );
  }

  /* Print help text. */
  printf( ":\n%s", command->help );
}

/*---------------------------------------------------------------------------*/

static void 
do_help( string_t arguments )
/* Give help on commands. */
{ 
  string_t command_name;
  command_t *command, *option;

  if (*arguments == EOS) 
  { 
    printf( "Commands available: "
            "(Use \"help COMMAND\" to get help about COMMAND.)\n" );
    print_help_summary( commands_local );
    printf( "\nOptions available: "
            "(Use \"help OPTION\" to get help about OPTION.)\n" );
    print_help_summary( options );
  } 
  else 
  { 
    command_name = parse_word( &arguments );
    if (strcmp_no_case( command_name, "command" ) == 0) 
    { 
      free_mem( &command_name );
      command_name = parse_word( &arguments );
      parse_end( &arguments );
      command = find_command( command_name, commands_local );
      option = NULL;
      if (command == NULL) 
	complain( "\"%s\" is no command.", command_name );
    } 
    else if (strcmp_no_case( command_name, "option" ) == 0) 
    { 
      free_mem( &command_name );
      command_name = parse_word( &arguments );
      command = NULL;
      option = find_command( command_name, options );
      if (option == NULL) 
	complain( "\"%s\" is no option.", command_name );
    } 
    else 
    { 
      command = find_command( command_name, commands_local );
      option = find_command( command_name, options );
      if (command != NULL && option != NULL) 
      { 
	complain( "Use \"help command %s\" or \"help option %s\",", 
		  command_name, command_name );
      } 
      else if (command == NULL && option == NULL) 
	complain( "\"%s\" is neither a command nor an option.", command_name );
    }
    parse_end( &arguments );
    if (command != NULL) 
      print_help( "Command", command );
    if (option != NULL) 
      print_help( "Option", option );
    free_mem( &command_name );
  }
}

command_t help_command = 
{ 
  "help h ?", do_help,
  "Usage:\n"
  "  help command COMMAND -- Print help about COMMAND.\n"
  "  help option OPTION -- Print help about OPTION.\n"
  "  help COMMAND_OR_OPTION -- Print help about COMMAND_OR_OPTION if unique.\n"
  "  help -- get a list of all available commands and options\n"
};

/*---------------------------------------------------------------------------*/

static void 
do_quit( string_t arguments )
/* Quit the program. */
{ 
  parse_end( &arguments );
  leave_program = TRUE;
}

command_t quit_command = 
{ 
  "quit exit q", do_quit,
  "Leave the program.\n"
  "Usage: quit\n"
};

/*---------------------------------------------------------------------------*/

static void 
do_get( string_t arguments )
/* Get setting for ARGUMENTS. */
{ 
  int_t i;
  string_t option;
      
  if (*arguments == EOS) 
  { 
    for (i = 0; options[i] != NULL; i++) 
      options[i]->command( "" );
  } 
  else
  { 
    option = parse_word( &arguments );
    parse_end( &arguments );
    execute_command( option, "option", options, FALSE );
    free_mem( &option );
  }
}

command_t get_command = 
{ 
  "get", do_get,
  "Query program settings.\n"
  "Usage:\n"
  "  get OPTION -- Print the setting of OPTION.\n"
  "  get -- Print all settings.\n"
};

/*---------------------------------------------------------------------------*/

static void 
do_set( string_t arguments )
/* Set an option. */
{ 
  string_t option_arguments, option;

  option_arguments = arguments;
  option = parse_word( &arguments );
  if (*arguments == EOS) 
    complain( "Missing arguments to set \"%s\".", option );
  execute_command( option_arguments, "option", options, FALSE );
  free_mem( &option );
}

command_t set_command = 
{ 
  "set", do_set,
  "Change program settings.\n"
  "Usage:\n"
  "  set OPTION ARGUMENT -- Change value of OPTION to ARGUMENT.\n"
};

/*---------------------------------------------------------------------------*/

static command_t *set_commands[] = {&set_command, NULL};
/* The commands that can be called during initialisation. */

void 
execute_set_commands( string_t file_name, string_t prefix )
/* Execute set commands in file FILE_NAME that are prefixed with PREFIX. */
{ 
  execute_command_file( file_name, prefix, set_commands );
}

/*---------------------------------------------------------------------------*/

static void 
do_alias_option( string_t arguments )
{ 
  alias_t *alias;
  string_t name, line;

  if (*arguments == EOS) 
  { 
    if (alias_list.first == NULL) 
      printf( "alias: (none)\n" );
    else 
    { 
      FOREACH( alias, alias_list ) 
      { 
	line = new_string_readable( alias->line, NULL );
        printf( "alias \"%s\": %s\n", alias->name, line );
        free_mem( &line );
      }
    }
  } 
  else 
  { 
    name = parse_word( &arguments );

    /* Find the alias NAME. */
    FOREACH( alias, alias_list ) 
    { 
      if (strcmp_no_case( alias->name, name ) == 0) 
	break;
    }

    if (*arguments == EOS) 
    { 
      /* Delete the alias. */
      if (alias == NULL) 
	complain( "Alias \"%s\" not defined." );
      free_mem( &alias->name );
      free_mem( &alias->line );
      free_node( &alias_list, (list_node_t *) alias );
    } 
    else 
    { 
      /* Define an alias. */
      line = parse_word( &arguments );
      parse_end( &arguments );
      if (alias == NULL) /* Create alias entry if it doesn't exist. */
      { 
	alias = new_node( &alias_list, sizeof( alias_t ), LIST_END );
        alias->name = new_string( name, NULL );
      } 
      else 
	free_mem( &alias->line );
      alias->line = line;
    }
    free_mem( &name );
  }
}

command_t alias_option = 
{ 
  "alias", do_alias_option,
  "Define command line aliases.\n"
  "Usage:\n"
  "  alias NAME LINE -- Define alias NAME for LINE.\n"
  "  alias NAME -- Undefine alias NAME.\n"
  "The LINE may contain the following special sequence:\n"
  "  %a -- The command line arguments when invoking the alias.\n"

};

/* End of file. =============================================================*/