1 /*        $NetBSD: cmd.c,v 1.3 2016/05/30 02:30:20 dholland 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <lib/libsa/stand.h>
33 #include <lib/libkern/libkern.h>
34 
35 #include "cmd.h"
36 #include "local.h"
37 
38 struct cmd {
39           char *name;
40           int (*func)(int, char**, int);
41 } cmdtab[] = {
42           { "batch",          cmd_batch },
43           { "help", cmd_help },
44           { "boot", cmd_boot },
45           { "bootux",         cmd_boot_ux },
46           { "loadbin",        cmd_load_binary },
47           { "jump", cmd_jump },
48           { "reboot",         cmd_reboot },
49           { "mem",  cmd_mem },
50           { "info", cmd_info },
51 
52           { "disklabel",      cmd_disklabel },
53           { "ls",             cmd_ls },
54           { "log_save",       cmd_log_save },
55 
56           { "test", cmd_test },
57           { "tlb",  cmd_tlb },
58           { "cop0", cmd_cop0 },
59           { "ks",             cmd_kbd_scancode },
60           { "ether_test",     cmd_ether_test },
61           { "ga_test",        cmd_ga_test },
62           { 0, 0 }  /* terminate */
63 };
64 
65 void
cmd_exec(const char * buf)66 cmd_exec(const char *buf)
67 {
68           char cmdbuf[CMDBUF_SIZE];
69           char *argp[CMDARG_MAX];
70           struct cmd *cmd;
71           char *p;
72           char *q;
73           int i, argc, sep;
74 
75           strncpy(cmdbuf, buf, CMDBUF_SIZE);
76           printf("%s\n", cmdbuf);
77 
78           /* find command */
79           for (cmd = cmdtab; cmd->name; cmd++) {
80                     for (q = cmdbuf, p = cmd->name; *p; p++, q++) {
81                               if (*p != *q)
82                                         break;
83                     }
84                     if (*p == 0 && (*q == '\0' || *q == ' '))
85                               goto found;
86           }
87           printf("***ERROR*** %s\n", cmdbuf);
88 
89           cmd_help(0, 0, 0);
90           return;
91 
92  found:
93           /* setup argument */
94           p = cmdbuf;
95           argc = 0;
96           argp[argc++] = p;
97           sep = 0;
98           for (i = 0; (i < CMDBUF_SIZE) && (argc < CMDARG_MAX); i++, p++) {
99                     if (*p == ' ') {
100                               *p = 0;
101                               sep = 1;
102                     } else if (sep) {
103                               sep = 0;
104                               argp[argc++] = p;
105                     }
106           }
107           *--p = 0;
108 
109           cmd->func (argc, argp, 1);
110 }
111 
112 int
cmd_batch(int argc,char * argp[],int interactive)113 cmd_batch(int argc, char *argp[], int interactive)
114 {
115           struct cmd_batch_tab *p;
116           char *args[CMDARG_MAX];
117           int i;
118 
119           args[0] = "batch";
120           for (p = cmd_batch_tab; p->func; p++) {
121                     for (i = 0; i < p->argc; i++)
122                               args[i + 1] = p->arg[i];
123 
124                     if (p->func(p->argc + 1, args, interactive)) {
125                               printf("batch aborted.\n");
126                               return 1;
127                     }
128           }
129 
130           return 0;
131 }
132 
133 int
cmd_help(int argc,char * argp[],int interactive)134 cmd_help(int argc, char *argp[], int interactive)
135 {
136           struct cmd *cmd;
137 
138           printf("command: ");
139           for (cmd = cmdtab; cmd->name; cmd++)
140                     printf("%s ", cmd->name);
141           printf("\n");
142 
143           return 0;
144 }
145