diff --git a/Makefile b/Makefile index a048eba..c17e1ad 100644 --- a/Makefile +++ b/Makefile @@ -84,8 +84,8 @@ NC='\033[1;37m' # Lib C sources + libbuf source LibCSources = libc/mem.c libc/ctype.c \ libc/rand.c libc/sprintf.c \ - libc/errno.c libc/string.c \ - libc/strtol.c extras/argv.c \ + libc/errno.c libc/string.c \ + libc/strtol.c \ libbuf/bopen.c libbuf/bputc.c libbuf/bscroll.c \ libbuf/bprint.c libbuf/bgetc.c libbuf/bscan.c \ libbuf/bflush.c libbuf/bwrite.c libbuf/bread.c \ @@ -108,7 +108,8 @@ KernSources = kernel/ke/cpuid.c kernel/mm/paging.c \ kernel/ke/rtc.c kernel/io/keyb.c \ kernel/io/spkr.c kernel/po/shtdwn.c \ kernel/sh/shell.c kernel/sh/shcmds.c \ - kernel/sh/musage.c kernel/io/ata.c + kernel/sh/musage.c kernel/io/ata.c \ + kernel/sh/argv.c KernObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(KernSources)) KernDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(KernSources)) diff --git a/include/ex/argv.h b/include/ex/argv.h deleted file mode 100644 index a651f34..0000000 --- a/include/ex/argv.h +++ /dev/null @@ -1,299 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Desc: Command line parsing utilities // -// // -// // -// Copyright © 2018-2019 The OS/K Team // -// // -// This file is part of OS/K. // -// // -// OS/K is free software: you can redistribute it and/or modify // -// it under the terms of the GNU General Public License as published by // -// the Free Software Foundation, either version 3 of the License, or // -// any later version. // -// // -// OS/K is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY//without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU General Public License for more details. // -// // -// You should have received a copy of the GNU General Public License // -// along with OS/K. If not, see . // -//----------------------------------------------------------------------------// - -#ifndef _LIBC_H -#include -#endif - -#ifndef _KALEXTRAS_ARGV_H -#define _KALEXTRAS_ARGV_H - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum CmdOptionFlag_t CmdOptionFlag_t; -typedef enum CmdParserFlags_t CmdParserFlags_t; -typedef enum CmdParserReturn_t CmdParserReturn_t; - -typedef struct CmdOption_t CmdOption_t; -typedef struct CmdDocStrings_t CmdDocStrings_t; -typedef struct CmdParserState_t CmdParserState_t; - -//------------------------------------------// - -// -// Maximal total size of argv, including the pointers, -// the pointed-to strings and their null-terminators -// This is a 32-bit integer -// -#define ARG_MAX (1 << 16) // 64 KB - -//------------------------------------------// - -// -// Flags for options -// -enum CmdOptionFlag_t -{ - // The option parameter is optional - KALOPT_OPTIONAL = (1 << 0), - - // Do not show option in any help message - KALOPT_HIDDEN = (1 << 1), - - // This option is an alias for the previous one - KALOPT_ALIAS = (1 << 2), - - // This isn't an option but a docstring - KALOPT_DOCSTR = (1 << 3), - - // Only show in long help messages - KALOPT_LONGDOC = (1 << 4), - -}; - -// -// Flags for KalParse(CmdLine|ArgVec) -// -enum CmdParserFlags_t -{ - // Don't exit on errors - KALOPT_NO_EXIT = (1 << 0), - - // Don't react to --help - KALOPT_NO_HELP = (1 << 1), - - // Don't react to --version - KALOPT_NO_VERSION = (1 << 2), - - // Alias -h for --help and -V for --version - KALOPT_ALIASES = (1 << 3), - - // Don't print any error message - // Implies KALOPT_NO_EXIT - KALOPT_NO_ERRORS = (1 << 4) | KALOPT_NO_EXIT, - - // Use KalGetProgName() instead of argv[0] - // as program name - KALOPT_DONT_USE_ARGV0 = (1 << 5), - - // Call argument parser for non-options - // Non-options arguments will be indicated by - // a key of 0, the argument passed as a parameter - KALOPT_PARSE_ALL = (1 << 6), - - // Call argument parser with options and non-options in the - // order they were found, instead of parsing options first - KALOPT_IN_ORDER = (1 << 7) | KALOPT_PARSE_ALL, - - // Stay silent all along - KALOPT_SILENT = KALOPT_NO_EXIT | KALOPT_NO_ERRORS - | KALOPT_NO_HELP | KALOPT_NO_VERSION, -}; - -// -// Return values for the command parser -// These flags can be combined and are applied in order -// -// "Continue" and "Break" actions do not prevent -// later actions from applying -// -enum CmdParserReturn_t -{ - // Continue parsing new options - KALOPT_CONTINUE = 0, - - // Stop parsing further options - KALOPT_BREAK = (1 << 0), - - // Show help/version message (by default, continue parsing) - KALOPT_SHOWHELP = (1 << 1), - KALOPT_SHOWVERS = (1 << 2), -}; - -//------------------------------------------// - -// -// An option for a command, e.g. "-o file" in "cc -o file" -// -struct CmdOption_t -{ - // The option's name, e.g. "help" for "--help" - // May be 0, but only if letter is not zero - const char *longName; - - // The option's letter, e.g. 'h' for '-h' - int letter; - - // Address of the variable to put the parameter into - const char *param; - - // Option flags, see above - CmdOptionFlag_t flags; - - // The option's help text - // If this is 0, this option is hidden - const char *helpText; - - // The option's group, for sorting during --help - // Must be positive and < 256, or option won't shop up - // during help texts - int group; -}; - -// -// Program help/documentation strings; any can be 0 -// -// Help messages are printed in this format: -// (letting "this" be a CmdDocStrings_t* variable) -// -// Usage: (this->usage) -// (this->header) -// -// (this->groups[0]) -// -o, --option-name option description -// ... -// ... -// -// (this->bottom) -// -// XXX progname/version -// -struct CmdDocStrings_t -{ - const char *usage; - const char *header; - const char *bottom; - - // Groups documentation - // groups[n] should be either 0 or contain the - // description of the option group n - const char **groups; -}; - -// -// The state variable passed to the parser containing useful infos -// -struct CmdParserState_t { - - // Option we're currently parsing - const CmdOption_t *option; - - // Index (in argv) of the option we're parsing - int argvIndex; - - // Flags passed to KalParse(CmdLine|ArgV) - CmdParserFlags_t flags; - - // Has help/version messages been displayed already? - bool shownHelp; - bool shownVersion; - - // Output streams (may be NULL) - /*FILE*/ void *outStream; - /*FILE*/ void *errStream; - - // Private, internal data; do not touch - void *priv; - -}; - -// -// The argument parser function -// -typedef CmdParserReturn_t (*CmdParser_t)(int key, - const char *param, - CmdParserState_t *state); - -//------------------------------------------// - -// -// Misc. simple functions -// -int KalComputeArgCount(const char **argv); -size_t KalComputeArgVecSize(const char **argv); - -// -// Command line to argument vector -// -error_t KalCmdLineToArgVecEx(const char *cmdLine, - int *argcPtr, - char *bufptr, - bool doEscaping); - -// -// KalCmdLineToArgVecEx but doEscaping = false -// -error_t KalCmdLineToArgVec(const char *cmdLine, - int *argcPtr, - char *bufptr); - -// -// Argument vector to command line -// -error_t KalArgVecToCmdLineEx(char *cmdLine, - size_t lengthMax, - int argc, - const char **argv, - bool doUnEscaping); - -// -// KalArgVecToCmdLineEx but doUnEscapign = false -// -error_t KalArgVecToCmdLine(char *cmdLine, - size_t lengthMax, - int argc, - const char **argv); -// -// Command line parser; only takes an argument vector -// The argv argument *will* be modified and all parsed -// options and arguments will be removed, except argv[0] -// which is guanranteed to be left -// -error_t KalParseArgVecEx(int argc, - char **argv, - const CmdOption_t *options, - const CmdDocStrings_t *docStrings, - CmdParserFlags_t *flags, - CmdParser_t *parser, - /*FILE*/ void *outStream, - /*FILE*/ void *errStream); -// -// KalParseArgVecEx(argc, argv, options, docString, stdin, stdout, parser, NULL) -// -error_t KalParseArgVec(int argc, - char **argv, - const CmdOption_t *options, - const CmdDocStrings_t *docStrings, - CmdParserFlags_t *flags, - CmdParser_t *parser); - -//------------------------------------------// - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/init/boot.h b/include/init/boot.h index 931f996..61cfa19 100644 --- a/include/init/boot.h +++ b/include/init/boot.h @@ -26,6 +26,10 @@ #include #endif +#ifndef MULTIBOOT_HEADER +#include +#endif + #ifndef _INIT_BOOT_H #define _INIT_BOOT_H @@ -115,4 +119,9 @@ extern volatile BootInfo_t BtBootTab; //----------------------------------------------------------------------------// +void BtDoSanityChecks(uint); +void BtInitBootInfo(multiboot_info_t *, void *); + +//----------------------------------------------------------------------------// + #endif diff --git a/include/io/vga.h b/include/io/vga.h index 7d183dd..319c790 100644 --- a/include/io/vga.h +++ b/include/io/vga.h @@ -58,15 +58,14 @@ extern const char *RtlColorNames[VGA_COLOR_WHITE+1]; #define RtlColorToChar(c) ((c) + 130) #define RtlCharToColor(c) ((c) - 130) -void IoScrollDown(void); +uint IoGetScroll(void); void IoScrollUp(void); +void IoScrollDown(void); void IoChangeTermColor(int, int); error_t IoInitVGABuffer(void); -uint IoGetScroll(void); - //----------------------------------------------------------------------------// #endif diff --git a/include/kernel.h b/include/kernel.h index 41a3431..c043910 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -165,7 +165,9 @@ extern CpuCore_t _KeCPUTable[NCPUS]; //----------------------------------------------------------------------------// +#ifndef _ASM_H #include +#endif //----------------------------------------------------------------------------// diff --git a/include/sh/argv.h b/include/sh/argv.h new file mode 100644 index 0000000..843b2c7 --- /dev/null +++ b/include/sh/argv.h @@ -0,0 +1,57 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Desc: Command line parsing utilities // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + +#ifndef _KERNEL_H +#include +#endif + +#ifndef _SH_ARGV_H +#define _SH_ARGV_H + +//------------------------------------------// + +// See kaleid/sh/argv.h +#define ARG_MAX (1 << 16) // 64 KB + +//------------------------------------------// + +// +// Command line to argument vector +// Assumes sizeof(bufptr) >= ARG_MAX * 2 +// +error_t ShCmdLineToArgVecEx(const char *cmdLine, + int *argcPtr, + char *bufptr, + bool doEscaping); + +// +// ShCmdLineToArgVecEx but doEscaping = false +// +error_t ShCmdLineToArgVec(const char *cmdLine, + int *argcPtr, + char *bufptr); + +//------------------------------------------// + +#endif diff --git a/kaleid/kernel/init/init.h b/include/sh/shell.h similarity index 69% rename from kaleid/kernel/init/init.h rename to include/sh/shell.h index 4f9accf..f3da477 100644 --- a/kaleid/kernel/init/init.h +++ b/include/sh/shell.h @@ -1,7 +1,7 @@ //----------------------------------------------------------------------------// // GNU GPL OS/K // // // -// Desc: Kernel entry point // +// Desc: Kernel shell // // // // // // Copyright © 2018-2019 The OS/K Team // @@ -22,33 +22,38 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#ifndef _KERNEL_H +#include +#endif -// info.c -extern void BtDoSanityChecks(uint mbMagic); -extern void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg); +#ifndef _SH_SHELL_H +#define _SH_SHELL_H -// io/vga.c -extern error_t IoInitVGABuffer(void); +//----------------------------------------------------------------------------// -// ke/shell.c -extern void KeStartShell(void); +void ShStartShell(void); -// ps/proc.c test function -extern void pstest(void); +//----------------------------------------------------------------------------// -// interrupts tests -extern void divideByZero(void); +#define CMDBUFSIZE 256 + +typedef struct Command_t Command_t; + +struct Command_t +{ + const char *name; + error_t (*func)(int argc, char **argv, char *cmdline); + const char *help; +}; + +//----------------------------------------------------------------------------// + +extern int shcol; +extern int shargc; +extern char **shargv; +extern Command_t shcmdtable[]; + +//----------------------------------------------------------------------------// + +#endif diff --git a/kaleid/kernel/init/info.c b/kaleid/kernel/init/info.c index 8f8d6ae..1d2da60 100644 --- a/kaleid/kernel/init/info.c +++ b/kaleid/kernel/init/info.c @@ -23,7 +23,6 @@ //----------------------------------------------------------------------------// #include -#include // // BootInfo_t initialization. It is necessary because grub will potentially be diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 217f6bf..e29259e 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -22,7 +22,17 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include "init.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // // Entry point of the Kaleid kernel @@ -66,7 +76,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) IoEnableKeyb(); MmActivatePageHandler(); - KeStartShell(); + ShStartShell(); PoShutdown(); } diff --git a/kaleid/extras/argv.c b/kaleid/kernel/sh/argv.c similarity index 86% rename from kaleid/extras/argv.c rename to kaleid/kernel/sh/argv.c index 102e431..8e132d3 100644 --- a/kaleid/extras/argv.c +++ b/kaleid/kernel/sh/argv.c @@ -22,37 +22,9 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include +#include #include -#include - -// -// Computes argument count, the least N such -// that argv[N] == NULL -// -int KalComputeArgCount(const char **argv) -{ - int argc = 0; - - while (argv[argc]) argc++; - - return argc; -} - -// -// Computes the total size of argv, including -// the null-terminators -// -size_t KalComputeArgVecSize(const char *argv[]) -{ - size_t len; - - for (len = 0; *argv; len += strlen(*argv) + 1, argv++); - - return len; -} - // // Converts command line to an argument vector // @@ -61,13 +33,10 @@ size_t KalComputeArgVecSize(const char *argv[]) // with the address of the nth string stored in the first half, // specifically at argv[n-1] // -// TODO long escape sequences -// get program command line if cmdLine == NULL -// -error_t KalCmdLineToArgVecEx(const char *cmdLine, - int *argcPtr, - char *bufptr, - bool doEscaping) +error_t ShCmdLineToArgVecEx(const char *cmdLine, + int *argcPtr, + char *bufptr, + bool doEscaping) { int argc = 0; char quotes = 0; @@ -213,9 +182,9 @@ error_t KalCmdLineToArgVecEx(const char *cmdLine, #undef ISBLANK #undef NULLTERM_AND_SAVE -error_t KalCmdLineToArgVec(const char *cmdLine, - int *argcPtr, - char *bufptr) +error_t ShCmdLineToArgVec(const char *cmdLine, + int *argcPtr, + char *bufptr) { - return KalCmdLineToArgVecEx(cmdLine, argcPtr, bufptr, false); + return ShCmdLineToArgVecEx(cmdLine, argcPtr, bufptr, false); } diff --git a/kaleid/kernel/sh/musage.c b/kaleid/kernel/sh/musage.c index 4da4ca2..b97ca50 100644 --- a/kaleid/kernel/sh/musage.c +++ b/kaleid/kernel/sh/musage.c @@ -22,7 +22,10 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include "shell.h" +#include +#include +#include +#include error_t CmdMemUsage(int argc, char **argv, char *cmdline) { diff --git a/kaleid/kernel/sh/shcmds.c b/kaleid/kernel/sh/shcmds.c index 2839796..3530d2f 100644 --- a/kaleid/kernel/sh/shcmds.c +++ b/kaleid/kernel/sh/shcmds.c @@ -22,8 +22,15 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include "shell.h" - +#include +#include +#include +#include +#include +#include +#include +#include +#include int ShAtoi(char* str) { @@ -173,7 +180,7 @@ error_t CmdHelp(int argc, char **argv, char *cmdline) if (argc == 1) { KernLog("List of all shell built-ins:\n"); - for (cmd = cmdtable; cmd->name != NULL; cmd++, count++) { + for (cmd = shcmdtable; cmd->name != NULL; cmd++, count++) { KernLog("\t%s", cmd->name); for (i = strlen(cmd->name)/4; i<3; i++) { KernLog("\t"); @@ -224,7 +231,7 @@ error_t CmdReloadPage(int argc, char **argv, char *cmdline) error_t CmdShell(int argc, char **argv, char *cmdline) { - KeStartShell(); + ShStartShell(); return EOK; } @@ -285,7 +292,8 @@ error_t CmdVersion(int argc, char **argv, char *cmdline) } //----------------------------------------------------------------------------// -Command_t cmdtable[] = + +Command_t shcmdtable[] = { { "args", CmdArgs, "Print command line" }, { "beep", CmdBeep, "Make a beep" }, diff --git a/kaleid/kernel/sh/shell.c b/kaleid/kernel/sh/shell.c index 0a5a58b..f9bf1ab 100644 --- a/kaleid/kernel/sh/shell.c +++ b/kaleid/kernel/sh/shell.c @@ -22,15 +22,21 @@ // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#include "shell.h" - -int shcol = VGA_COLOR_LIGHT_GREY; +#include +#include +#include +#include +#include +#include +#include int shargc = 0; -char *argvbuf = 0; char **shargv = 0; +int shcol = VGA_COLOR_LIGHT_GREY; -void ExecuteCommand(char *cmdbuf) +static char *argvbuf = 0; + +static void ExecuteCommand(char *cmdbuf) { error_t rc; Command_t *cmd; @@ -40,10 +46,10 @@ void ExecuteCommand(char *cmdbuf) return; memzero(*shargv, ARG_MAX); - rc = KalCmdLineToArgVec(cmdbuf, &shargc, argvbuf); + rc = ShCmdLineToArgVec(cmdbuf, &shargc, argvbuf); if (rc) KeStartPanic("Shell: Couldn't parse command line: %d", rc); - for (cmd = cmdtable; cmd->name != NULL; cmd++) { + for (cmd = shcmdtable; cmd->name != NULL; cmd++) { if (!strcmp(cmd->name, shargv[0])) { cmd->func(shargc, shargv, cmdbuf); found = true; @@ -59,7 +65,7 @@ void ExecuteCommand(char *cmdbuf) } } -void KeStartShell(void) +void ShStartShell(void) { uchar ch; error_t rc; diff --git a/kaleid/kernel/sh/shell.h b/kaleid/kernel/sh/shell.h index 0edc933..e2f9b18 100644 --- a/kaleid/kernel/sh/shell.h +++ b/kaleid/kernel/sh/shell.h @@ -32,29 +32,9 @@ #include #include #include -#include #include #include #include -void KeStartShell(void); -void pstest(void); -extern int shcol; -extern int shargc; -extern char *argv0; -extern char **shargv; - -#define CMDBUFSIZE 256 - -typedef struct Command_t Command_t; - -struct Command_t -{ - const char *name; - error_t (*func)(int argc, char **argv, char *cmdline); - const char *help; -}; - -extern Command_t cmdtable[];