aboutsummaryrefslogtreecommitdiffstats
path: root/com32/hdt/hdt-cli.c
blob: 2640d3821e05080990a6c6591cba322afbcf41e5 (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
171
172
173
174
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2009 Erwan Velu - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * -----------------------------------------------------------------------
*/

#include "hdt-cli.h"
#include "hdt-common.h"
#include <stdlib.h>
#include <string.h>

void set_mode(struct s_cli_mode *cli_mode, cli_mode_t mode, struct s_hardware *hardware) {
 switch (mode) {
  case EXIT_MODE:
        cli_mode->mode=mode;
        snprintf(cli_mode->prompt, sizeof(cli_mode->prompt), "%s> ", CLI_EXIT);
        break;

  case HDT_MODE:
        cli_mode->mode=mode;
        snprintf(cli_mode->prompt, sizeof(cli_mode->prompt), "%s> ", CLI_HDT);
        break;

  case PCI_MODE:
	cli_mode->mode=mode;
	snprintf(cli_mode->prompt,sizeof(cli_mode->prompt),"%s> ", CLI_PCI);
	if (!hardware->pci_detection)
          cli_detect_pci(hardware);
	break;

  case CPU_MODE:
	cli_mode->mode=mode;
	snprintf(cli_mode->prompt,sizeof(cli_mode->prompt),"%s> ", CLI_CPU);
	if (!hardware->dmi_detection)
          detect_dmi(hardware);
	if (!hardware->cpu_detection)
          cpu_detect(hardware);
	break;

  case DMI_MODE:
	if (!hardware->dmi_detection)
          detect_dmi(hardware);
	if (!hardware->is_dmi_valid) {
          printf("No valid DMI table found, exiting.\n");
          break;
        }
	cli_mode->mode=mode;
	snprintf(cli_mode->prompt,sizeof(cli_mode->prompt),"%s> ",CLI_DMI);
	break;
 }
}

void handle_hdt_commands(char *cli_line, struct s_cli_mode *cli_mode, struct s_hardware *hardware) {
    /* hdt cli mode specific commands */
    if ( !strncmp(cli_line, CLI_SHOW, sizeof(CLI_SHOW) - 1) ) {
	   main_show(strstr(cli_line,"show")+ sizeof (CLI_SHOW), hardware,cli_mode);
	   return;
    }
}

/* Code that manage the cli mode */
void start_cli_mode(struct s_hardware *hardware, int argc, char *argv[]) {
 char cli_line[256];
 struct s_cli_mode cli_mode;

 set_mode(&cli_mode,HDT_MODE,hardware);

 printf("Entering CLI mode\n");

 for (;;) {
  memset(cli_line,0,sizeof cli_line);
  printf("%s",cli_mode.prompt);

  fgets(cli_line, sizeof cli_line, stdin);
  cli_line[strlen(cli_line)-1]='\0';
    /* We use sizeof BLAH - 1 to remove the last \0 */

    if ( !strncmp(cli_line, CLI_EXIT, sizeof(CLI_EXIT) - 1) ) {
	   int mode=do_exit(&cli_mode);
	   if (mode ==  EXIT_MODE)
		   return;
	   set_mode(&cli_mode,mode,hardware);
	   continue;
    }

    if ( !strncmp(cli_line, CLI_HELP, sizeof(CLI_HELP) - 1) ) {
           show_cli_help(&cli_mode);
	   continue;
    }

    if ( !strncmp(cli_line, CLI_PCI, sizeof(CLI_PCI) - 1) ) {
	   set_mode(&cli_mode,PCI_MODE,hardware);
	   continue;
    }
    if ( !strncmp(cli_line, CLI_CLEAR, sizeof(CLI_CLEAR) - 1) ) {
	   clear_screen();
	   continue;
    }
    if ( !strncmp(cli_line, CLI_CPU, sizeof(CLI_CPU) - 1) ) {
	   set_mode(&cli_mode,CPU_MODE,hardware);
	   continue;
    }
    if ( !strncmp(cli_line, CLI_DMI, sizeof(CLI_DMI) - 1) ) {
	   set_mode(&cli_mode,DMI_MODE,hardware);
	   continue;
    }
    /* All commands before that line are common for all cli modes
     * the following will be specific for every mode */
    switch(cli_mode.mode) {
     case DMI_MODE: handle_dmi_commands(cli_line,&cli_mode, hardware); break;
     case PCI_MODE: handle_pci_commands(cli_line,&cli_mode, hardware); break;
     case HDT_MODE: handle_hdt_commands(cli_line,&cli_mode, hardware); break;
     case CPU_MODE: handle_cpu_commands(cli_line,&cli_mode, hardware); break;
     case EXIT_MODE: break; /* should not happend */
    }
 }
}

int do_exit(struct s_cli_mode *cli_mode) {
 switch (cli_mode->mode) {
  case HDT_MODE: return EXIT_MODE;
  case NET_MODE:
  case PCI_MODE:
  case DMI_MODE:
  case CPU_MODE: return HDT_MODE;
  case EXIT_MODE: return EXIT_MODE; /* should not happend */
 }
return HDT_MODE;
}

void show_cli_help(struct s_cli_mode *cli_mode) {
switch (cli_mode->mode) {
	case HDT_MODE:
		printf("Available commands are : %s %s %s %s %s %s\n",CLI_CLEAR, CLI_EXIT,CLI_HELP,CLI_SHOW, CLI_PCI, CLI_DMI);
		break;
	case NET_MODE:
	case CPU_MODE:
	case PCI_MODE:
	case DMI_MODE:
		printf("Available commands are : %s %s %s %s\n",CLI_CLEAR, CLI_EXIT, CLI_HELP, CLI_SHOW);
		break;
	case EXIT_MODE: /* Should not happend*/
		break;
}
}

void main_show(char *item, struct s_hardware *hardware, struct s_cli_mode *cli_mode) {
 if (!strncmp(item,CLI_PCI, sizeof (CLI_PCI))) main_show_pci(hardware);
 if (!strncmp(item,CLI_DMI, sizeof (CLI_DMI))) main_show_dmi(hardware,cli_mode);
 if (!strncmp(item,CLI_CPU, sizeof (CLI_CPU))) main_show_cpu(hardware,cli_mode);
 if (!strncmp(item,CLI_NET, sizeof (CLI_NET))) main_show_net(hardware,cli_mode);
}