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