cmd.c revision 1.1 1 /* $NetBSD: cmd.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <lib/libsa/stand.h>
40 #include <lib/libkern/libkern.h>
41
42 #include "cmd.h"
43 #include "local.h"
44
45 struct cmd {
46 char *name;
47 int (*func)(int, char**, int);
48 } cmdtab[] = {
49 { "batch", cmd_batch },
50 { "help", cmd_help },
51 { "boot", cmd_boot },
52 { "bootux", cmd_boot_ux },
53 { "loadbin", cmd_load_binary },
54 { "jump", cmd_jump },
55 { "reboot", cmd_reboot },
56 { "mem", cmd_mem },
57 { "info", cmd_info },
58
59 { "disklabel", cmd_disklabel },
60 { "ls", cmd_ls },
61 { "log_save", cmd_log_save },
62
63 { "test", cmd_test },
64 { "tlb", cmd_tlb },
65 { "cop0", cmd_cop0 },
66 { "ks", cmd_kbd_scancode },
67 { "ether_test", cmd_ether_test },
68 { "ga_test", cmd_ga_test },
69 { 0, 0 } /* terminate */
70 };
71
72 void
73 cmd_exec(const char *buf)
74 {
75 char cmdbuf[CMDBUF_SIZE];
76 char *argp[CMDARG_MAX];
77 struct cmd *cmd;
78 char *p;
79 char *q;
80 int i, argc, sep;
81
82 strncpy(cmdbuf, buf, CMDBUF_SIZE);
83 printf("%s\n", cmdbuf);
84
85 /* find command */
86 for (cmd = cmdtab; cmd->name; cmd++) {
87 for (q = cmdbuf, p = cmd->name; *p; p++, q++) {
88 if (*p != *q)
89 break;
90 }
91 if (*p == 0 && (*q == '\0' || *q == ' '))
92 goto found;
93 }
94 printf("***ERROR*** %s\n", cmdbuf);
95
96 cmd_help(0, 0, 0);
97 return;
98
99 found:
100 /* setup argument */
101 p = cmdbuf;
102 argc = 0;
103 argp[argc++] = p;
104 sep = 0;
105 for (i = 0; (i < CMDBUF_SIZE) && (argc < CMDARG_MAX); i++, p++) {
106 if (*p == ' ') {
107 *p = 0;
108 sep = 1;
109 } else if (sep && (*p != ' ' || *p == '\0')) {
110 sep = 0;
111 argp[argc++] = p;
112 }
113 }
114 *--p = 0;
115
116 cmd->func (argc, argp, 1);
117 }
118
119 int
120 cmd_batch(int argc, char *argp[], int interactive)
121 {
122 struct cmd_batch_tab *p;
123 char *args[CMDARG_MAX];
124 int i;
125
126 args[0] = "batch";
127 for (p = cmd_batch_tab; p->func; p++) {
128 for (i = 0; i < p->argc; i++)
129 args[i + 1] = p->arg[i];
130
131 if (p->func(p->argc + 1, args, interactive)) {
132 printf("batch aborted.\n");
133 return 1;
134 }
135 }
136
137 return 0;
138 }
139
140 int
141 cmd_help(int argc, char *argp[], int interactive)
142 {
143 struct cmd *cmd;
144
145 printf("command: ");
146 for (cmd = cmdtab; cmd->name; cmd++)
147 printf("%s ", cmd->name);
148 printf("\n");
149
150 return 0;
151 }
152