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