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

« back to all changes in this revision

Viewing changes to commands.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2005-01-10 11:52:04 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050110115204-hpgncw5pb0m1t8i6
Tags: 6.13-5
debian/control (malaga-doc Recommends): Suggest gv as a
postscript-viewer instead of ghostview.  (Closes: #289701).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 1995 Bjoern Beutel. */
 
2
 
 
3
/* Description. =============================================================*/
 
4
 
 
5
/* Tools for a command line interpreter. */
 
6
 
 
7
/* Includes. ================================================================*/
 
8
 
 
9
#include <stdio.h>
 
10
#include <stdlib.h>
 
11
#include <setjmp.h>
 
12
#include <string.h>
 
13
#include "basic.h"
 
14
#include "input.h"
 
15
#include "files.h"
 
16
#include "commands.h"
 
17
#ifdef UNIX
 
18
#include <signal.h>
 
19
#endif
 
20
#ifdef WINDOWS
 
21
#include <windows.h>
 
22
#endif
 
23
 
 
24
/* Types. ===================================================================*/
 
25
 
 
26
typedef struct /* An alias. */
 
27
 
28
  list_node_t *next;
 
29
  string_t name;
 
30
  string_t line; /* Command line this alias stands for. */
 
31
} alias_t;
 
32
 
 
33
/* Global variables. ========================================================*/
 
34
 
 
35
command_t **options;
 
36
/* The options that can be set by the set command.
 
37
 * This has to be set before executing "set_command". */
 
38
 
 
39
bool_t leave_program; /* Set to TRUE if program is to leave. */
 
40
bool_t leave_command_loop; /* Set to TRUE if command loop is to leave. */
 
41
 
 
42
/* Variables. ===============================================================*/
 
43
 
 
44
static command_t **commands_local; 
 
45
/* Local copy of command table for "help_command". */
 
46
 
 
47
static list_t alias_list;
 
48
 
 
49
/* Functions for user interrupts. ===========================================*/
 
50
 
 
51
#ifdef UNIX
 
52
static void 
 
53
set_user_break( int signal_number )
 
54
/* Signal handler for notification that user wants to interrupt a command. */
 
55
 
56
  user_break_requested = TRUE;
 
57
  signal( SIGINT, set_user_break );
 
58
}
 
59
#endif
 
60
 
 
61
#ifdef WINDOWS
 
62
static BOOL
 
63
set_user_break( DWORD signal_type )
 
64
/* Signal handler for notification that user wants to interrupt a command. */
 
65
{
 
66
  if (signal_type == CTRL_C_EVENT) 
 
67
  {
 
68
    user_break_requested = TRUE;
 
69
    return TRUE;
 
70
  }
 
71
  return FALSE;
 
72
}
 
73
#endif
 
74
 
 
75
/*---------------------------------------------------------------------------*/
 
76
 
 
77
void 
 
78
check_user_break( void )
 
79
/* Abort command execution if user has sent an interrupt signal. */
 
80
 
81
  if (user_break_requested)
 
82
  {
 
83
    user_break_requested = FALSE;
 
84
    complain( "User interrupt." );
 
85
  }
 
86
}
 
87
 
 
88
/* Functions for reading and executing commands. ============================*/
 
89
 
 
90
static command_t *
 
91
find_command( string_t command_name, command_t *commands[] )
 
92
/* Find the entry for COMMAND_NAME in COMMANDS and return it.
 
93
 * Return NULL if COMMAND_NAME can't be found. */
 
94
 
95
  int_t i;
 
96
  string_t names, name;
 
97
  bool_t found_command;
 
98
      
 
99
  for (i = 0; commands[i] != NULL; i++) 
 
100
  { 
 
101
    names = commands[i]->names;
 
102
    while (*names != EOS) 
 
103
    { 
 
104
      name = parse_word( &names );
 
105
      found_command = (strcmp_no_case( command_name, name ) == 0);
 
106
      free_mem( &name);
 
107
      if (found_command) 
 
108
        return commands[i];
 
109
    }
 
110
  }
 
111
  return NULL;
 
112
}
 
113
 
 
114
/*---------------------------------------------------------------------------*/
 
115
 
 
116
static void 
 
117
execute_command( string_t command_line, string_t command_type,
 
118
                 command_t *commands[], bool_t use_aliases )
 
119
/* Execute COMMAND_LINE using COMMANDS.
 
120
 * If there is no such command, print an error.
 
121
 * COMMAND_TYPE may be "command" or "option" and is used for error messages.
 
122
 * If USE_ALIASES == TRUE, look for the command in the list of aliases. */
 
123
 
124
  string_t line, command_name;
 
125
  string_t alias_line, line_p, name_readable; 
 
126
  command_t *command;
 
127
  bool_t command_found;
 
128
  alias_t *alias;
 
129
      
 
130
  /* Divide command line into strings COMMAND and ARGUMENTS. */
 
131
  line = command_name = NULL;
 
132
  TRY 
 
133
  { 
 
134
    line_p = line = replace_vars_in_string( command_line );
 
135
    parse_whitespace( &line_p );
 
136
    if (*line_p != EOS) 
 
137
    { 
 
138
      command_name = parse_word( &line_p );
 
139
      command_found = FALSE;
 
140
      user_break_requested = FALSE;
 
141
      commands_local = commands;
 
142
      command = find_command( command_name, commands );
 
143
      if (command != NULL) /* Execute the command. */
 
144
      {
 
145
        command_found = TRUE;
 
146
        command->command( line_p );
 
147
      } 
 
148
      else if (use_aliases) 
 
149
      { 
 
150
        FOREACH( alias, alias_list ) 
 
151
        { 
 
152
          if (strcmp_no_case( alias->name, command_name ) == 0) 
 
153
          { 
 
154
            command_found = TRUE;
 
155
            alias_line = replace_arguments( alias->line, "a", line_p );
 
156
            execute_command( alias_line, command_type, commands, FALSE );
 
157
            free_mem( &alias_line );
 
158
            break;
 
159
          }
 
160
        }
 
161
      }
 
162
      if (! command_found) 
 
163
      { 
 
164
        name_readable = new_string_readable( command_name, NULL );
 
165
        free_mem( &command_name );
 
166
        command_name = name_readable;
 
167
        complain( "Unknown %s %s.", command_type, command_name );
 
168
      }
 
169
    }
 
170
  } 
 
171
  FINALLY 
 
172
  { 
 
173
    free_mem( &command_name );
 
174
    free_mem( &line );
 
175
  }
 
176
  END_TRY;
 
177
}
 
178
 
 
179
/*---------------------------------------------------------------------------*/
 
180
 
 
181
void 
 
182
execute_command_file( string_t command_file, string_t line_header, 
 
183
                      command_t *commands[] )
 
184
/* Execute commands in COMMAND_FILE using COMMANDS.
 
185
 * If LINE_HEADER != NULL, command lines must start with LINE_HEADER. */
 
186
 
187
  FILE *command_stream;
 
188
  string_t command_line, include_file, header, command_line_p;
 
189
  bool_t is_command_line;
 
190
  volatile int_t line_count;
 
191
  volatile bool_t leave_loop;
 
192
 
 
193
  command_stream = open_stream( command_file, "r" );
 
194
  leave_loop = FALSE;
 
195
  line_count = 0;
 
196
  while (! leave_loop) 
 
197
  { 
 
198
    command_line = header = NULL;
 
199
    TRY 
 
200
    { 
 
201
      /* Read a command line */
 
202
      command_line = read_line( command_stream );
 
203
      if (command_line == NULL) 
 
204
        leave_loop = TRUE;
 
205
      else 
 
206
      { 
 
207
        cut_comment( command_line );
 
208
        line_count++;
 
209
        command_line_p = command_line;
 
210
        if (*command_line_p == EOS) 
 
211
          is_command_line = FALSE;
 
212
        else if (line_header == NULL) 
 
213
          is_command_line = TRUE;
 
214
        else 
 
215
        { 
 
216
          /* Check if current line begins with LINE_HEADER. */
 
217
          header = parse_word( &command_line_p );
 
218
          is_command_line = FALSE;
 
219
          if (strcmp_no_case( header, line_header ) == 0) 
 
220
            is_command_line = TRUE;
 
221
          else if (strcmp_no_case( header, "include:" ) == 0) 
 
222
          { 
 
223
            include_file = parse_absolute_path( &command_line_p, 
 
224
                                                command_file );
 
225
            parse_end( &command_line_p );
 
226
            execute_command_file( include_file, line_header, commands );
 
227
            free_mem( &include_file );
 
228
          }
 
229
        }
 
230
        if (is_command_line) 
 
231
          execute_command( command_line_p, "command", commands, TRUE );
 
232
      }
 
233
    } 
 
234
    IF_ERROR 
 
235
    {
 
236
      print_text( error_text, " (\"%s\", line %d)", 
 
237
                  name_in_path( command_file ), line_count );
 
238
    } 
 
239
    FINALLY 
 
240
    { 
 
241
      free_mem( &header );
 
242
      free_mem( &command_line );
 
243
    }
 
244
    END_TRY;
 
245
  }
 
246
  close_stream( &command_stream, command_file );
 
247
}
 
248
 
 
249
/*---------------------------------------------------------------------------*/
 
250
 
 
251
void 
 
252
command_loop( string_t prompt, command_t *commands[] )
 
253
/* Read user commands, look for them in COMMANDS and execute them
 
254
 * until a command function returns FALSE. */
 
255
 
256
  string_t command_line;
 
257
 
 
258
  /* Install a Ctrl-C handler. */
 
259
#ifdef UNIX
 
260
  signal( SIGINT, set_user_break );
 
261
#endif
 
262
#ifdef WINDOWS
 
263
  static bool_t user_break_installed;
 
264
 
 
265
  if (! user_break_installed)
 
266
    SetConsoleCtrlHandler( (PHANDLER_ROUTINE) set_user_break, TRUE );
 
267
  user_break_installed = TRUE;
 
268
#endif
 
269
 
 
270
  /* Repeat the command loop. */
 
271
  while (! leave_command_loop && ! leave_program) 
 
272
  { /* Read command line. */
 
273
    command_line = NULL;
 
274
    TRY 
 
275
    { 
 
276
      printf( "%s> ", prompt );
 
277
      command_line = read_line( stdin );
 
278
      if (command_line != NULL)
 
279
        execute_command( command_line, "command", commands, TRUE );
 
280
    }
 
281
    IF_ERROR
 
282
    { 
 
283
      printf( "%s\n", error_text->buffer );
 
284
      RESUME;
 
285
    } 
 
286
    FINALLY 
 
287
      free_mem( &command_line );
 
288
    END_TRY;
 
289
  }
 
290
  leave_command_loop = FALSE;
 
291
}
 
292
 
 
293
/*---------------------------------------------------------------------------*/
 
294
 
 
295
void 
 
296
free_aliases( void )
 
297
/* Free all currently defined aliases. */
 
298
 
299
  alias_t *alias;
 
300
 
 
301
  FOREACH_FREE( alias, alias_list ) 
 
302
  { 
 
303
    free_mem( &alias->name );
 
304
    free_mem( &alias->line );
 
305
  }
 
306
}
 
307
 
 
308
/* General commands. ========================================================*/
 
309
 
 
310
static void 
 
311
print_help_summary( command_t *commands[] )
 
312
/* Print names of all commands in COMMANDS. */
 
313
 
314
  int i, j;
 
315
  string_t names, name;
 
316
 
 
317
  for (i = 0; commands[i] != NULL; i++) 
 
318
  { 
 
319
    names = commands[i]->names;
 
320
    name = parse_word( &names );
 
321
    if (i > 0 && i % 5 == 0) 
 
322
      printf( "\n" );
 
323
    printf( "%s", name );
 
324
    for (j = strlen( name ); j < 15; j++) 
 
325
      printf( " " );
 
326
    free_mem( &name );
 
327
  }
 
328
  printf( "\n" );
 
329
}
 
330
 
 
331
/*---------------------------------------------------------------------------*/
 
332
 
 
333
static void 
 
334
print_help( string_t command_type, command_t *command )
 
335
/* Print help about COMMAND, 
 
336
 * which is of COMMAND_TYPE (either "command" or "option"). */
 
337
 
338
  string_t names, name;
 
339
 
 
340
  names = command->names;
 
341
 
 
342
  /* Print full name. */
 
343
  name = parse_word( &names );
 
344
  printf( "%s \"%s\"", command_type, name );
 
345
  free_mem( &name );
 
346
 
 
347
  /* Print shortcuts. */
 
348
  while (*names != EOS) 
 
349
  { 
 
350
    name = parse_word( &names );
 
351
    printf( ", \"%s\"", name );
 
352
    free_mem( &name );
 
353
  }
 
354
 
 
355
  /* Print help text. */
 
356
  printf( ":\n%s", command->help );
 
357
}
 
358
 
 
359
/*---------------------------------------------------------------------------*/
 
360
 
 
361
static void 
 
362
do_help( string_t arguments )
 
363
/* Give help on commands. */
 
364
 
365
  string_t command_name;
 
366
  command_t *command, *option;
 
367
 
 
368
  if (*arguments == EOS) 
 
369
  { 
 
370
    printf( "Commands available: "
 
371
            "(Use \"help COMMAND\" to get help about COMMAND.)\n" );
 
372
    print_help_summary( commands_local );
 
373
    printf( "\nOptions available: "
 
374
            "(Use \"help OPTION\" to get help about OPTION.)\n" );
 
375
    print_help_summary( options );
 
376
  } 
 
377
  else 
 
378
  { 
 
379
    command_name = parse_word( &arguments );
 
380
    if (strcmp_no_case( command_name, "command" ) == 0) 
 
381
    { 
 
382
      free_mem( &command_name );
 
383
      command_name = parse_word( &arguments );
 
384
      parse_end( &arguments );
 
385
      command = find_command( command_name, commands_local );
 
386
      option = NULL;
 
387
      if (command == NULL) 
 
388
        complain( "\"%s\" is no command.", command_name );
 
389
    } 
 
390
    else if (strcmp_no_case( command_name, "option" ) == 0) 
 
391
    { 
 
392
      free_mem( &command_name );
 
393
      command_name = parse_word( &arguments );
 
394
      command = NULL;
 
395
      option = find_command( command_name, options );
 
396
      if (option == NULL) 
 
397
        complain( "\"%s\" is no option.", command_name );
 
398
    } 
 
399
    else 
 
400
    { 
 
401
      command = find_command( command_name, commands_local );
 
402
      option = find_command( command_name, options );
 
403
      if (command != NULL && option != NULL) 
 
404
      { 
 
405
        complain( "Use \"help command %s\" or \"help option %s\",", 
 
406
                  command_name, command_name );
 
407
      } 
 
408
      else if (command == NULL && option == NULL) 
 
409
        complain( "\"%s\" is neither a command nor an option.", command_name );
 
410
    }
 
411
    parse_end( &arguments );
 
412
    if (command != NULL) 
 
413
      print_help( "Command", command );
 
414
    if (option != NULL) 
 
415
      print_help( "Option", option );
 
416
    free_mem( &command_name );
 
417
  }
 
418
}
 
419
 
 
420
command_t help_command = 
 
421
 
422
  "help h ?", do_help,
 
423
  "Usage:\n"
 
424
  "  help command COMMAND -- Print help about COMMAND.\n"
 
425
  "  help option OPTION -- Print help about OPTION.\n"
 
426
  "  help COMMAND_OR_OPTION -- Print help about COMMAND_OR_OPTION if unique.\n"
 
427
  "  help -- get a list of all available commands and options\n"
 
428
};
 
429
 
 
430
/*---------------------------------------------------------------------------*/
 
431
 
 
432
static void 
 
433
do_quit( string_t arguments )
 
434
/* Quit the program. */
 
435
 
436
  parse_end( &arguments );
 
437
  leave_program = TRUE;
 
438
}
 
439
 
 
440
command_t quit_command = 
 
441
 
442
  "quit exit q", do_quit,
 
443
  "Leave the program.\n"
 
444
  "Usage: quit\n"
 
445
};
 
446
 
 
447
/*---------------------------------------------------------------------------*/
 
448
 
 
449
static void 
 
450
do_get( string_t arguments )
 
451
/* Get setting for ARGUMENTS. */
 
452
 
453
  int_t i;
 
454
  string_t option;
 
455
      
 
456
  if (*arguments == EOS) 
 
457
  { 
 
458
    for (i = 0; options[i] != NULL; i++) 
 
459
      options[i]->command( "" );
 
460
  } 
 
461
  else
 
462
  { 
 
463
    option = parse_word( &arguments );
 
464
    parse_end( &arguments );
 
465
    execute_command( option, "option", options, FALSE );
 
466
    free_mem( &option );
 
467
  }
 
468
}
 
469
 
 
470
command_t get_command = 
 
471
 
472
  "get", do_get,
 
473
  "Query program settings.\n"
 
474
  "Usage:\n"
 
475
  "  get OPTION -- Print the setting of OPTION.\n"
 
476
  "  get -- Print all settings.\n"
 
477
};
 
478
 
 
479
/*---------------------------------------------------------------------------*/
 
480
 
 
481
static void 
 
482
do_set( string_t arguments )
 
483
/* Set an option. */
 
484
 
485
  string_t option_arguments, option;
 
486
 
 
487
  option_arguments = arguments;
 
488
  option = parse_word( &arguments );
 
489
  if (*arguments == EOS) 
 
490
    complain( "Missing arguments to set \"%s\".", option );
 
491
  execute_command( option_arguments, "option", options, FALSE );
 
492
  free_mem( &option );
 
493
}
 
494
 
 
495
command_t set_command = 
 
496
 
497
  "set", do_set,
 
498
  "Change program settings.\n"
 
499
  "Usage:\n"
 
500
  "  set OPTION ARGUMENT -- Change value of OPTION to ARGUMENT.\n"
 
501
};
 
502
 
 
503
/*---------------------------------------------------------------------------*/
 
504
 
 
505
static command_t *set_commands[] = {&set_command, NULL};
 
506
/* The commands that can be called during initialisation. */
 
507
 
 
508
void 
 
509
execute_set_commands( string_t file_name, string_t prefix )
 
510
/* Execute set commands in file FILE_NAME that are prefixed with PREFIX. */
 
511
 
512
  execute_command_file( file_name, prefix, set_commands );
 
513
}
 
514
 
 
515
/*---------------------------------------------------------------------------*/
 
516
 
 
517
static void 
 
518
do_alias_option( string_t arguments )
 
519
 
520
  alias_t *alias;
 
521
  string_t name, line;
 
522
 
 
523
  if (*arguments == EOS) 
 
524
  { 
 
525
    if (alias_list.first == NULL) 
 
526
      printf( "alias: (none)\n" );
 
527
    else 
 
528
    { 
 
529
      FOREACH( alias, alias_list ) 
 
530
      { 
 
531
        line = new_string_readable( alias->line, NULL );
 
532
        printf( "alias \"%s\": %s\n", alias->name, line );
 
533
        free_mem( &line );
 
534
      }
 
535
    }
 
536
  } 
 
537
  else 
 
538
  { 
 
539
    name = parse_word( &arguments );
 
540
 
 
541
    /* Find the alias NAME. */
 
542
    FOREACH( alias, alias_list ) 
 
543
    { 
 
544
      if (strcmp_no_case( alias->name, name ) == 0) 
 
545
        break;
 
546
    }
 
547
 
 
548
    if (*arguments == EOS) 
 
549
    { 
 
550
      /* Delete the alias. */
 
551
      if (alias == NULL) 
 
552
        complain( "Alias \"%s\" not defined." );
 
553
      free_mem( &alias->name );
 
554
      free_mem( &alias->line );
 
555
      free_node( &alias_list, (list_node_t *) alias );
 
556
    } 
 
557
    else 
 
558
    { 
 
559
      /* Define an alias. */
 
560
      line = parse_word( &arguments );
 
561
      parse_end( &arguments );
 
562
      if (alias == NULL) /* Create alias entry if it doesn't exist. */
 
563
      { 
 
564
        alias = new_node( &alias_list, sizeof( alias_t ), LIST_END );
 
565
        alias->name = new_string( name, NULL );
 
566
      } 
 
567
      else 
 
568
        free_mem( &alias->line );
 
569
      alias->line = line;
 
570
    }
 
571
    free_mem( &name );
 
572
  }
 
573
}
 
574
 
 
575
command_t alias_option = 
 
576
 
577
  "alias", do_alias_option,
 
578
  "Define command line aliases.\n"
 
579
  "Usage:\n"
 
580
  "  alias NAME LINE -- Define alias NAME for LINE.\n"
 
581
  "  alias NAME -- Undefine alias NAME.\n"
 
582
  "The LINE may contain the following special sequence:\n"
 
583
  "  %a -- The command line arguments when invoking the alias.\n"
 
584
 
 
585
};
 
586
 
 
587
/* End of file. =============================================================*/