source: rtems-schedsim/schedsim/shell/schedsim_priority/schedsim.cc @ 7c50462

Last change on this file since 7c50462 was abb18dc, checked in by Joel Sherrill <joel.sherrill@…>, on 04/25/11 at 15:53:10

Initial import.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2010.
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 *  $Id$
10 */
11
12#include <newlib/getopt.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <ctype.h>
17
18#include "shell.h"
19#include "rtems_sched.h"
20
21/*
22 *  Variables to control global behavior
23 */
24int verbose = 0;
25const char *progname;
26const char *scriptname;
27
28FILE *Script;
29int ScriptFileLine = 0;
30
31/*
32 *  Print program usage message
33 */
34void usage()
35{
36  fprintf(
37    stderr,
38    "Usage: %s [-v] script\n"
39    "\n"
40    "  -v           - enable verbose output\n",
41    progname
42  );
43  exit( -1 );
44}
45
46#define RTEMS_SHELL_MAXIMUM_ARGUMENTS (128)
47
48void ProcessScript(
49  FILE *script
50)
51{
52  char               buffer[512];
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
66    // If the last line does not have a CR, then we don't want to
67    // arbitrarily clobber an = instead of a \n.
68    length = strlen(buffer);
69    if ( buffer[ length - 1] == '\n' )
70      buffer[ length - 1] = '\0';
71
72    if ( verbose )
73      fprintf( stderr, "%d: %s\n", ++ScriptFileLine, buffer );
74
75    if ( buffer[0] == '#' )
76      continue;
77
78    for ( c = buffer ; *c ; c++ ) {
79      if (!isblank((int)*c))
80        break;
81    }
82
83
84    if (!strcmp(c,"bye") || !strcmp(c,"exit")) {
85      return;
86    }
87
88    if (rtems_shell_make_args(c, &argc, argv, RTEMS_SHELL_MAXIMUM_ARGUMENTS)) {
89      fprintf(stderr, "Error parsing arguments\n" );
90      continue;
91    }
92
93    shell_cmd = rtems_shell_lookup_cmd(argv[0]);
94    if ( !shell_cmd ) {
95      fprintf(stderr, "%s is unknown command\n", c );
96      continue;
97    }
98
99    shell_cmd->command(argc, argv);
100  }
101}
102
103int main(
104  int argc,
105  char **argv
106)
107{
108  int opt;
109  progname = argv[0];
110 
111  while ((opt = getopt(argc, argv, "v")) != -1) {
112    switch (opt) {
113      case 'v': verbose = 1;                break;
114      default: /* '?' */
115        usage();
116    }
117  }
118
119  if ( optind >= argc ) {
120    fprintf( stderr, "no script to process\n" );
121    usage();
122  }
123
124  scriptname = argv[ optind ];
125
126  if ( verbose ) {
127    fprintf(
128      stderr,
129      "Script File               : %s\n"
130      "verbose                   : %d\n",
131      scriptname,
132      verbose
133    );
134  }
135
136  //
137  //  Initialize the command interpreter
138  //
139  rtems_shell_initialize_command_set();
140
141  //
142  //  Open the script file
143  //
144  Script = fopen( scriptname, "r" );
145  if ( !Script ) {
146    fprintf( stderr, "Unable to open script file (%s)\n", scriptname );
147    exit( -1 );
148  }
149
150  //
151  //  Process the Script
152  //
153  ProcessScript( Script );
154
155  //
156  //  Open the script file
157  //
158  (void) fclose( Script );
159
160  //
161  //  Just in case something throws
162  //
163  try {
164  } catch (...) {
165    exit(-1);
166  }
167
168  return 0;
169}
Note: See TracBrowser for help on using the repository browser.