Statistics
| Branch: | Revision:

root / prex-0.9.0 / usr / test / kmon / kmon.c @ 03e9c04a

History | View | Annotate | Download (2.82 KB)

1
/*
2
 * Copyright (c) 2005, Kohsuke Ohtani
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the author nor the names of any co-contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29

    
30
/*
31
 * kmon.c - main routine for kernel monitor
32
 */
33

    
34
#include <sys/prex.h>
35
#include <sys/keycode.h>
36
#include <ctype.h>
37
#include <stdio.h>
38
#include <limits.h>
39

    
40
#define ARGMAX                32
41

    
42
extern int dispatch_cmd(int argc, char **args);
43

    
44
/*
45
 * Parse an entire given line.
46
 */
47
static int
48
parse_line(char *line)
49
{
50
        static char *args[ARGMAX];
51
        char *p, *word = NULL;
52
        int argc = 0;
53
        int rc = 0;
54

    
55
        if (line[0] != ' ' && line[0] != '\t')
56
                word = line;
57

    
58
        p = line;
59
        while (*p) {
60
                if (word == NULL) {
61
                        /* Skip white space. */
62
                        if (*p != ' ' && *p != '\t')
63
                                word = p;
64
                } else {
65
                        if (*p == ' ' || *p == '\t') {
66
                                *p = '\0';
67
                                args[argc++] = word;
68
                                word = NULL;
69
                                if (argc >= ARGMAX - 1) {
70
                                        printf("Too many args\n");
71
                                        return 0;
72
                                }
73
                        }
74
                }
75
                p++;
76
        }
77
        if (word)
78
                args[argc++] = word;
79
        args[argc] = NULL;
80

    
81
        if (argc) {
82
                if (dispatch_cmd(argc, args))
83
                        rc = 1;
84
        }
85
        return rc;
86
}
87

    
88
static void
89
read_line(char *line)
90
{
91
        int c, len = 0;
92
        char *p = line;
93

    
94
        for (;;) {
95
                c = getchar();
96
                if (c == '\n' || len > LINE_MAX) {
97
                        *p = '\0';
98
                        return;
99
                }
100
                *p = (char)c;
101
                p++;
102
                len++;
103
        }
104
}
105

    
106
int
107
main(int argc, char *argv[])
108
{
109
        static char line[LINE_MAX];
110

    
111
        printf("Prex kernel monitor - type 'help' to list commands\n");
112

    
113
        for (;;) {
114
                printf("[kmon]$ ");
115
                read_line(line);
116
                if (parse_line(line))
117
                        break;
118
        }
119
        return 0;
120
}