summaryrefslogtreecommitdiff
path: root/shell/shell_init.c
blob: 32d7bbe2ea9e0ee1cc690662cdc46e93f4207080 (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
/*
 *  This is a derived from the init.c file in the telnetd demo.
 *
 *  $Id$
 */

#include <bsp.h>

#include <errno.h>
#include <time.h>

#include <stdio.h>
#include <rtems/rtems_bsdnet.h>
#include <rtems/telnetd.h>
#include <rtems/shell.h>

#include <rtems/error.h>
#include <rpc/rpc.h>
#include <netinet/in.h>
#include <time.h>

#include <arpa/inet.h>
#include <sys/socket.h>
#include "../networkconfig.h"

// I'm not sure why this is necessary
#include <rtems/bdbuf.h>
const rtems_bdbuf_config rtems_bdbuf_configuration;

typedef char * (* prompt_function_t)(void);

static prompt_function_t prompt_function = NULL;

extern void set_prompt_function(prompt_function_t f)
{
  prompt_function = f;
}

extern char * get_prompt(void);
void rtems_shell_get_prompt(
  rtems_shell_env_t *shell_env,
  char              *prompt,
  size_t             size
)
{
  char curdir[256];

  if (prompt_function == NULL)
  {
    getcwd(curdir,sizeof(curdir));
    snprintf(prompt, size - 1, "%s%s[%s] %c ",
            ((shell_env->taskname) ? shell_env->taskname : ""),
            ((shell_env->taskname) ? " " : ""),
            curdir,
            geteuid()?'$':'#');

  }
  else
    snprintf(prompt, size - 1, prompt_function());
}

/*
 *  If true, listen on socket(s).
 *  If false, remain on console.
 */
/* #define REMAIN_ON_CONSOLE */
bool remain_on_console = false;

#include <rtems/shell.h>

#define SHELL_HELP_MSG \
  "Starting rtemsShell via telnetd -- default account is rtems w/no password\n"

#define CONFIGURE_SHELL_COMMANDS_ALL_NETWORKING
#define CONFIGURE_SHELL_COMMANDS_ALL

#define CONFIGURE_SHELL_COMMANDS_INIT
#include <rtems/shellconfig.h>


static void rtemsShell(
  char *pty_name,
  void *cmd_arg
)
{
  rtems_shell_env_t env = rtems_global_shell_env;
  
  env.devname = pty_name;
  env.taskname = "TLNT";
  env.login_check = rtems_shell_login_check;

  if ( !remain_on_console )
    printk("============== Starting Shell ==============\n");

  rtems_shell_main_loop( &env ); 

  if ( !remain_on_console )
    printk("============== Exiting Shell ==============\n");
}

#define SHELL_ENTRY rtemsShell

/*
 *  Telnet demon configuration
 */
rtems_telnetd_config_table rtems_telnetd_config = {
  .command = SHELL_ENTRY,
  .arg = NULL,
  .priority = 1, /* We feel important today */
  .stack_size = 20 * RTEMS_MINIMUM_STACK_SIZE, /* Shell needs a large stack */
  .login_check = NULL, /* Shell asks for user and password */
  .keep_stdio = false
};

/*
 *  Initialization Aid
 */
void init_telnet_daemon(void)
{
  fprintf(stderr, "\n\n*** Telnetd Server Test ***\n\r" );

  fprintf(stderr, "============== Initializing Network ==============\n");
  rtems_bsdnet_initialize_network ();

  fprintf(stderr, "============== Add Route ==============\n");
  rtems_bsdnet_show_inet_routes ();

  fprintf(stderr, "============== Start Telnetd ==============\n");

  printk( SHELL_HELP_MSG );

  #if defined(REMAIN_ON_CONSOLE)
    remain_on_console = true;
  #endif

  rtems_global_shell_env.login_check = rtems_shell_login_check;
  rtems_telnetd_config.keep_stdio = remain_on_console;
  
  rtems_telnetd_initialize();
}


#include <stdlib.h>
#include <rtems.h>
#include <rtems/telnetd.h>

/* Helper for Ada */
void invoke_rtems_shell(void)
{
  rtemsShell("console", NULL);
}