source: rtems-schedsim/schedsim/shell/shared/schedsim.c @ 36f4cb9

Last change on this file since 36f4cb9 was 36f4cb9, checked in by Joel Sherrill <joel.sherrill@…>, on 05/23/14 at 21:46:22

Use shared main() and file processor

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[f40778b]1/*
[a2aad55]2 *  COPYRIGHT (c) 1989-2013.
[f40778b]3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 */
9
10#include <newlib/getopt.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <ctype.h>
15
16#include "shell.h"
17#include "rtems_sched.h"
[36f4cb9]18#include "schedsim_shell.h"
[f40778b]19
20/*
21 *  Variables to control global behavior
22 */
23int verbose = 1;
24const char *progname;
25const char *scriptname;
26
27FILE *Script;
28int ScriptFileLine = 0;
29
30/*
31 *  Print program usage message
32 */
33void usage()
34{
35  fprintf(
36    stderr,
37    "Usage: %s [-v] script\n"
38    "\n"
39    "  -v           - enable verbose output\n",
40    progname
41  );
42  exit( -1 );
43}
44
45#define RTEMS_SHELL_MAXIMUM_ARGUMENTS (128)
46
[36f4cb9]47int ProcessScript(
[f40778b]48  FILE *script
49)
50{
51  char               buffer[512];
[44cf36b]52  int                sc;
[f40778b]53  char              *cStatus;
54  char              *c;
55  size_t             length;
56  int                argc;
57  char              *argv[RTEMS_SHELL_MAXIMUM_ARGUMENTS];
58  rtems_shell_cmd_t *shell_cmd;
59 
60 
61  while ( 1 ) {
62    cStatus = fgets( buffer, sizeof(buffer), script );
63    if ( cStatus == NULL )
64      break;
65    // If the last line does not have a CR, then we don't want to
66    // arbitrarily clobber an = instead of a \n.
67    length = strlen(buffer);
68    if ( buffer[ length - 1] == '\n' )
69      buffer[ length - 1] = '\0';
70
71    if ( verbose )
72      printf( "==> %d: %s\n", ++ScriptFileLine, buffer );
73
74    if ( buffer[0] == '#' )
75      continue;
76
77    for ( c = buffer ; *c ; c++ ) {
78      if (!isblank((int)*c))
79        break;
80    }
81
82
83    if (!strcmp(c,"bye") || !strcmp(c,"exit")) {
[44cf36b]84      exit( 0 );
[f40778b]85    }
86
87    if (rtems_shell_make_args(c, &argc, argv, RTEMS_SHELL_MAXIMUM_ARGUMENTS)) {
88      fprintf(stderr, "Error parsing arguments\n" );
89      continue;
90    }
91
92    if ( argc == 0 )
93      continue;
94
95    shell_cmd = rtems_shell_lookup_cmd(argv[0]);
96    if ( !shell_cmd ) {
97      fprintf(stderr, "%s is unknown command\n", c );
[44cf36b]98      exit( 1 );
[f40778b]99    }
100
[44cf36b]101    sc = shell_cmd->command(argc, argv);
102    if ( sc != 0 ) {
103      fprintf( stderr, "ERROR: Command %s returned %d\n", argv[0], sc );
104      exit( sc );
105    }
[f40778b]106  }
107}
108
109int main(
110  int argc,
111  char **argv
112)
113{
[36f4cb9]114  int  sc; 
115  int  opt;
116
[f40778b]117  progname = argv[0];
118 
119  while ((opt = getopt(argc, argv, "v")) != -1) {
120    switch (opt) {
121      case 'v': verbose = 0;                break;
122      default: /* '?' */
123        usage();
124    }
125  }
126
127  if ( optind >= argc ) {
128    scriptname = "-";
129  } else {
130    scriptname = argv[ optind ];
131  }
132
133  if ( !strcmp( scriptname, "-" ) ) {
134    scriptname = "/dev/stdin";
135  }
136
137  if ( verbose ) {
138    printf(
139      "Script File               : %s\n"
140      "verbose                   : %d\n",
141      scriptname,
142      verbose
143    );
144  }
145
146  //
147  //  Initialize the command interpreter
148  //
149  rtems_shell_initialize_command_set();
150  add_commands();
151
152  //
153  //  Open the script file
154  //
155  Script = fopen( scriptname, "r" );
156  if ( !Script ) {
157    fprintf( stderr, "Unable to open script file (%s)\n", scriptname );
158    exit( -1 );
159  }
160
161  //
162  //  Process the Script
163  //
[36f4cb9]164  sc = ProcessScript( Script );
[f40778b]165
166  //
167  //  Open the script file
168  //
169  (void) fclose( Script );
170
[36f4cb9]171  return sc;
[f40778b]172}
Note: See TracBrowser for help on using the repository browser.