aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: cfef1b1c28d6ef7ca22740390a0a69387eae6e0f (plain)
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
/*
 * main.c
 *
 * Copyright (C) 2005 Erik Gilling, all rights reserved
 *
 *	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, version 2.
 *
 */

#include "config.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif

#include <readline/readline.h>
#include <readline/history.h>

#include <getopt.h>

#include "io.h"
#include "samba.h"
#include "cmd.h"

#define SAM7_TTY "/dev/at91_0"


static int sam7_parse_line( char *line, char *argv[] );
static int sam7_handle_line( char *line );
static void usage( void );

#define MAX_CMDS 32

int main( int argc, char *argv[] )
{
  char *cmdline;
  char *line = NULL;
  char *cmds[MAX_CMDS];
  int n_cmds=0;
  int c,i;


  while( 1 ) {
    int option_index;
    static struct option long_options[] = {
      { "line", 1, 0, 'l' },
      { "exec", 1, 0, 'e' },
      { "help", 0, 0, 'h' }
    };

    c = getopt_long( argc, argv, "l:e:h", long_options, &option_index );
    
    if( c == -1 ) {
      break;
    }

    switch( c ) {
    case 'l':
      line = optarg;
      break;
      
    case 'e':
      if( n_cmds >= MAX_CMDS ) {
	printf( "to many commands, increase MAX_CMDS\n" );
	return 1;
      }
      cmds[n_cmds] = optarg;
      n_cmds++;
      break;
      
    case 'h':
      usage();
      return 0;
      break;

    default:
      printf( "unknown option\n" );
      usage();
      return 1;
    }
  }


  if( io_init( line ) < 0 ) {
    return 1;
  }

  if( n_cmds > 0 ) {
    for( i=0 ; i<n_cmds ; i++ ) {
      sam7_handle_line( cmds[i] );
    }

  } else {
    while( (cmdline = readline( "sam7> " )) ) {
      add_history( cmdline );
      sam7_handle_line( cmdline );
    }
  }

  io_cleanup();

  return 0;
}

static void usage( void ) 
{
  printf( "usage: sam7 [options]\n" );
  printf( "  -e|--exec <cmd>   execute cmd instead of entering interactive mode.\n");
  printf( "  -l|--line <line>  specifies the line/tty which the boot agent resides.\n");
  printf( "                     only used for posix IO.\n" );
  printf( "  -h|--help         dislay this message and exit.\n");
}

static int sam7_parse_line( char *line, char *argv[] )
{
  char *p = line;
  int argc = 0;


  while( *p ) {
    // eat white space
    while( *p && isspace( *p ) ) { p++; }
    if( ! *p ) {
      break;
    }
    
    argv[argc++] = p;
    
    while( *p && !isspace( *p ) ) { p++; }
    if( *p ) {
      *p++ = '\0';
    }
  }
  
  return argc;
}

static int sam7_handle_line( char *line )
{
  int argc;
  char buff[256];
  char *argv[32];
  cmd_t *cmd;

  strncpy( buff, line, 255 );
  buff[255] = '\0';

  argc = sam7_parse_line( buff, argv );

  if( argc > 0 ) {
    if( (cmd = cmd_find( argv[0] )) != NULL ) {
      return cmd->func( argc, argv );
    }
    printf( "command %s not found\n", argv[0] );
    return -1;
  }
  
  return 0;
}