Annotation of prex/usr/bin/cmdbox/main.c, Revision 1.1
1.1 ! nbrk 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 <prex/prex.h>
! 31: #include <sys/utsname.h>
! 32:
! 33: #include <limits.h>
! 34: #include <ctype.h>
! 35: #include <unistd.h>
! 36: #include <string.h>
! 37: #include <stdlib.h>
! 38: #include <stdio.h>
! 39: #include <libgen.h>
! 40: #include <termios.h>
! 41:
! 42: #include "cmdbox.h"
! 43:
! 44: #ifdef CONFIG_CMD_SH
! 45: extern int sh_main(int argc, char *argv[]);
! 46: #endif
! 47:
! 48: int
! 49: help_main(int argc, char *argv[])
! 50: {
! 51: struct cmd_entry const *entry;
! 52: struct winsize ws;
! 53: int maxcol, col = 0;
! 54:
! 55: /* Get screen size */
! 56: maxcol = 80;
! 57: if (ioctl(fileno(stderr), TIOCGWINSZ, &ws) == 0)
! 58: maxcol = (int)ws.ws_col;
! 59: if (maxcol < 80)
! 60: maxcol -= 15;
! 61: else
! 62: maxcol -= 25;
! 63:
! 64: fprintf(stderr, "usage: cmdbox [command] [arguments]...\n");
! 65: fprintf(stderr, "builtin commands:\n");
! 66: entry = builtin_cmds;
! 67: while (entry->cmd != NULL) {
! 68: col += fprintf(stderr, "%s%s", ((col == 0) ? " " : ", "),
! 69: entry->cmd);
! 70: entry++;
! 71: if (col > maxcol && entry->cmd != NULL) {
! 72: fprintf(stderr, ",\n");
! 73: col = 0;
! 74: }
! 75: }
! 76: fprintf(stderr, "\nuse `-?` to find out more about each command.\n");
! 77: return 0;
! 78: }
! 79:
! 80: static void
! 81: bannar(void)
! 82: {
! 83: struct info_kernel info;
! 84:
! 85: sys_info(INFO_KERNEL, &info);
! 86: printf("%s version %s (%s)\n",
! 87: info.sysname, info.version, info.machine);
! 88: }
! 89:
! 90: int
! 91: main(int argc, char *argv[])
! 92: {
! 93: static char *shcmd[] = {"sh"};
! 94: char *prog, *cmd;
! 95: struct cmd_entry const *entry;
! 96:
! 97: prog = basename(argv[0]);
! 98: if (!strcmp(prog, "cmdbox")) {
! 99: if (argc == 1) {
! 100: bannar();
! 101: #ifdef CONFIG_CMD_SH
! 102: exit(sh_main(1, shcmd));
! 103: #else
! 104: exit(1);
! 105: #endif
! 106: } else {
! 107: cmd = argv[1];
! 108: argv++;
! 109: argc--;
! 110: }
! 111: } else
! 112: cmd = prog;
! 113:
! 114: entry = builtin_cmds;
! 115: while (entry->cmd != NULL) {
! 116: if (!strcmp(cmd, entry->cmd))
! 117: exit(entry->func(argc, argv));
! 118: entry++;
! 119: }
! 120: return 0;
! 121: }
CVSweb