Statistics
| Branch: | Revision:

root / prex-0.9.0 / usr / bin / cmdbox / main.c @ 03e9c04a

History | View | Annotate | Download (3.7 KB)

1
/*
2
 * Copyright (c) 2005-2006, 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
#include <sys/prex.h>
31

    
32
#include <limits.h>
33
#include <ctype.h>
34
#include <unistd.h>
35
#include <string.h>
36
#include <stdlib.h>
37
#include <stdio.h>
38
#include <libgen.h>
39
#include <termios.h>
40

    
41
#include "cmdbox.h"
42

    
43
#ifdef CONFIG_CMD_SH
44
extern int sh_main(int argc, char *argv[]);
45
#endif
46

    
47
int
48
null_main(int argc, char *argv[])
49
{
50
        return -1;
51
}
52

    
53
int
54
help_main(int argc, char *argv[])
55
{
56
        struct cmdentry const *entry;
57
        struct winsize ws;
58
        int maxcol, col = 0;
59

    
60
        /* Get screen size */
61
        maxcol = 80;
62
        if (ioctl(fileno(stderr), TIOCGWINSZ, &ws) == 0)
63
                maxcol = (int)ws.ws_col;
64
        if (maxcol < 80)
65
                maxcol -= 15;
66
        else
67
                maxcol -= 25;
68

    
69
        fprintf(stderr, "usage: cmdbox [command] [arguments]...\n");
70
        fprintf(stderr, "builtin commands:\n");
71

    
72
        /*
73
         * Show help for builtin commands.
74
         */
75
        entry = builtin_cmds;
76
        while (entry->cmd != NULL) {
77
                col += fprintf(stderr, "%s%s", ((col == 0) ? "    " : ", "),
78
                               entry->cmd);
79
                entry++;
80
                if (col > maxcol && entry->cmd != NULL) {
81
                        fprintf(stderr, ",\n");
82
                        col = 0;
83
                }
84
        }
85

    
86
#ifdef CONFIG_CMD_SH
87
        /*
88
         * Show help for shell commands.
89
         */
90
        entry = shell_cmds;
91
        while (entry->cmd != NULL) {
92
                col += fprintf(stderr, "%s%s", ((col == 0) ? "    " : ", "),
93
                               entry->cmd);
94
                entry++;
95
                if (col > maxcol && entry->cmd != NULL) {
96
                        fprintf(stderr, ",\n");
97
                        col = 0;
98
                }
99
        }
100
#endif
101
        fprintf(stderr, "\nuse `-?` to find out more about each command.\n");
102
        return 0;
103
}
104

    
105
int
106
main(int argc, char *argv[])
107
{
108
        char *prog, *cmd;
109
        struct cmdentry const *entry;
110
        int shcmd = 0;
111

    
112
        prog = basename(argv[0]);
113
        cmd = prog;
114

    
115
        /*
116
         * Alias:
117
         * 'sh'            => sh
118
         * 'cmdbox'        => sh
119
         * 'cmdbox sh'     => sh
120
         * 'cmdbox cmd'    => cmd
121
         * 'cmd' (symlink) => cmd
122
         */
123
        if (!strcmp(prog, "sh"))
124
                shcmd = 1;
125
        else if (!strcmp(prog, "cmdbox")) {
126
                if (argc == 1)
127
                        shcmd = 1;
128
                else {
129
                        if (!strcmp(argv[1], "sh"))
130
                                shcmd = 1;
131
                        else
132
                                cmd = argv[1];
133
                        argv++;
134
                        argc--;
135
                }
136
        }
137

    
138
        if (shcmd) {
139
                task_setname(task_self(), "sh");
140
                exit(sh_main(argc, argv));
141
        }
142

    
143
        entry = builtin_cmds;
144
        while (entry->cmd != NULL) {
145
                if (!strcmp(cmd, entry->cmd)) {
146
                        task_setname(task_self(), cmd);
147
                        exit(entry->func(argc, argv));
148
                }
149
                entry++;
150
        }
151
        fprintf(stderr, "No such command: %s\n", cmd);
152
        return 0;
153
}