X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fsystem.c;h=546b2d80d79c668565c279d1b45eb2bc9315da94;hb=144175640d070e26aa6a661a3a0014fa69e604dd;hp=26ce100e097727c8c1ed2f09319682c80db4a595;hpb=4cbfa66238970f126b63d677ca163f4dd944ac8e;p=tpg%2Facess2.git diff --git a/Kernel/system.c b/Kernel/system.c index 26ce100e..546b2d80 100644 --- a/Kernel/system.c +++ b/Kernel/system.c @@ -3,12 +3,40 @@ * Architecture Independent System Init * system.c */ -#include +#define DEBUG 0 +#include + +#define N_VARIABLES 16 +#define N_MAX_ARGS BITS + +// === TYPES === +typedef struct +{ + int TrueLine; + int nParts; + char **Parts; +} tConfigLine; +typedef struct +{ + int nLines; + tConfigLine Lines[]; +} tConfigFile; +typedef struct +{ + char *Name; // Name + int MinArgs; // Minimum number of arguments + int MaxArgs; // Maximum number of arguments + Uint IntArgs; // Bitmap of arguments that should be treated as integers + void *Func; // Function pointer + Uint OptDefaults[N_MAX_ARGS]; // Default values for optional arguments +} tConfigCommand; // === IMPORTS === extern int Modules_LoadBuiltins(); -extern int PCI_Install(); +//extern int PCI_Install(); extern void DMA_Install(); +extern void Debug_SetKTerminal(char *File); +extern void StartupPrint(char *Str); // === PROTOTYPES === void System_Init(char *ArgString); @@ -16,26 +44,41 @@ void System_ParseCommandLine(char *ArgString); void System_ParseVFS(char *Arg); void System_ParseSetting(char *Arg); void System_ExecuteScript(); - int System_Int_GetString(char *Str, char **Dest); +tConfigFile *System_Int_ParseFile(char *File); + +// === CONSTANTS === +const tConfigCommand caConfigCommands[] = { + {"module", 1,2, 0, Module_LoadFile, {(Uint)"",0}}, // Load a module from a file + {"spawn", 1,1, 0, Proc_Spawn, {0}}, // Spawn a process + // --- VFS --- + {"mount", 3,4, 0, VFS_Mount, {(Uint)"",0}}, // Mount a device + {"symlink", 2,2, 0, VFS_Symlink, {0}}, // Create a Symbolic Link + {"mkdir", 1,1, 0, VFS_MkDir, {0}}, // Create a Directory + {"open", 1,2, 0, VFS_Open, {VFS_OPENFLAG_READ,0}}, // Open a file + {"close", 1,1, 0x1, VFS_Close, {0}}, // Close an open file + {"ioctl", 3,3, 0x3, VFS_IOCtl, {0}}, // Call an IOCtl + + {"", 0,0, 0, NULL, {0}} +}; +#define NUM_CONFIG_COMMANDS (sizeof(caConfigCommands)/sizeof(caConfigCommands[0])) // === GLOBALS === -char *gsInitPath = "/Acess/Bin/init"; char *gsConfigScript = "/Acess/Conf/BootConf.cfg"; // === CODE === void System_Init(char *ArgString) { - // - Start Builtin Drivers & Filesystems - PCI_Install(); - DMA_Install(); - Modules_LoadBuiltins(); // - Parse Kernel's Command Line System_ParseCommandLine(ArgString); // - Execute the Config Script - Log("Executing config script..."); + Log_Log("Config", "Executing config script..."); System_ExecuteScript(); + + // Set the debug to be echoed to the terminal + Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)"); + Debug_SetKTerminal("/Devices/VTerm/7"); } /** @@ -49,14 +92,16 @@ void System_ParseCommandLine(char *ArgString) int i; char *str; - Log("Kernel Command Line: \"%s\"", ArgString); + Log_Log("Config", "Kernel Invocation \"%s\"", ArgString); // --- Get Arguments --- str = ArgString; for( argc = 0; argc < 32; argc++ ) { - while(*str == ' ') str++; // Eat Whitespace - if(*str == '\0') { argc--; break;} // End of string + // Eat Whitespace + while(*str == ' ') str++; + // Check for the end of the string + if(*str == '\0') { argc--; break;} argv[argc] = str; while(*str && *str != ' ') { @@ -66,7 +111,7 @@ void System_ParseCommandLine(char *ArgString) }*/ str++; } - if(*str == '\0') break; // End of string + if(*str == '\0') break; // Check for EOS *str = '\0'; // Cap off argument string str ++; // and increment the string pointer } @@ -98,7 +143,7 @@ void System_ParseVFS(char *Arg) // Check if the equals was found if( *value == '\0' ) { - Warning("Expected '=' in the string '%s'", Arg); + Log_Warning("Config", "Expected '=' in the string '%s'", Arg); return ; } @@ -109,7 +154,7 @@ void System_ParseVFS(char *Arg) // - Symbolic Link = if(value[0] == '/') { - Log("Symbolic link '%s' pointing to '%s'", Arg, value); + Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value); VFS_Symlink(Arg, value); } // - Mount =: @@ -124,13 +169,13 @@ void System_ParseVFS(char *Arg) } // Create Mountpoint if( (fd = VFS_Open(Arg, 0)) == -1 ) { - Log("Creating directory '%s'", Arg, value); + Log_Log("Config", "Creating directory '%s'", Arg, value); VFS_MkDir( Arg ); } else { VFS_Close(fd); } // Mount - Log("Mounting '%s' to '%s' ('%s')", dev, Arg, value); + Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value); VFS_Mount(dev, Arg, value, ""); } } @@ -150,10 +195,10 @@ void System_ParseSetting(char *Arg) // Check for boolean/flag (no '=') if(*value == '\0') { - if(strcmp(Arg, "") == 0) { - } else { - Warning("Kernel flag '%s' is not recognised", Arg); - } + //if(strcmp(Arg, "") == 0) { + //} else { + Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg); + //} } else { @@ -161,10 +206,13 @@ void System_ParseSetting(char *Arg) value ++; // and eat it's position if(strcmp(Arg, "SCRIPT") == 0) { - Log("Config Script: '%s'", value); - gsConfigScript = value; + Log_Log("Config", "Config Script: '%s'", value); + if(strlen(value) == 0) + gsConfigScript = NULL; + else + gsConfigScript = value; } else { - Warning("Kernel config setting '%s' is not recognised", Arg); + Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg); } } @@ -177,14 +225,20 @@ void System_ExecuteScript() { int fp; int fLen = 0; - int i = 0, lineStart; - char *sArg1, *sArg2, *sArg3; + int i, j, k; + int val; + int result = 0; + int variables[N_VARIABLES]; + int bReplaced[N_MAX_ARGS]; char *fData; + char *jmpTarget; + tConfigFile *file; + tConfigLine *line; // Open Script fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ); if(fp == -1) { - Warning("[CFG] Passed script '%s' does not exist", gsConfigScript); + Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript); return; } @@ -197,148 +251,319 @@ void System_ExecuteScript() fData[fLen] = '\0'; VFS_Close(fp); - // Read Script - while(i < fLen) + + // Parse File + file = System_Int_ParseFile(fData); + + // Parse each line + for( i = 0; i < file->nLines; i++ ) { - sArg1 = sArg2 = sArg3 = NULL; + line = &file->Lines[i]; + if( line->nParts == 0 ) continue; // Skip blank - lineStart = i; - // Clear leading whitespace and find empty lines - while(i < fLen && (fData[i] == ' ' || fData[i]=='\t')) i ++; - if(i == fLen) break; - if(fData[i] == '\n') { - i++; - continue; - } + if(line->Parts[0][0] == ':') continue; // Ignore labels - // Comment - if(fData[i] == ';' || fData[i] == '#') { - while(i < fLen && fData[i] != '\n') i ++; - i ++; - continue; + // Prescan and eliminate variables + for( j = 1; j < line->nParts; j++ ) { + Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]); + bReplaced[j] = 0; + if( line->Parts[j][0] != '$' ) continue; + if( line->Parts[j][1] == '?' ) { + val = result; + } + else { + val = atoi( &line->Parts[j][1] ); + if( val < 0 || val > N_VARIABLES ) continue; + val = variables[ val ]; + } + Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val); + line->Parts[j] = malloc( BITS/8+2+1 ); + sprintf(line->Parts[j], "0x%x", val); + bReplaced[j] = 1; } - // Commands - // - Mount - if(strncmp("mount ", fData+i, 6) == 0) { - i += 6; - i += System_Int_GetString(fData+i, &sArg1); - if(!sArg1) goto read2eol; - i += System_Int_GetString(fData+i, &sArg2); - if(!sArg2) goto read2eol; - i += System_Int_GetString(fData+i, &sArg3); - if(!sArg3) goto read2eol; - //Log("[CFG ] Mount '%s' to '%s' (%s)\n", sArg1, sArg2, sArg3); - VFS_Mount(sArg1, sArg2, sArg3, ""); - } - // - Load Module - else if(strncmp("module ", fData+i, 6) == 0) { - i += 7; - i += System_Int_GetString(fData+i, &sArg1); - if(!sArg1) goto read2eol; - //Log("[CFG ] Load Module '%s'\n", sArg1); - Module_LoadFile(sArg1, ""); //!\todo Use the rest of the line as the argument string - } - // - Load Module - else if(strncmp("edimod ", fData+i, 6) == 0) { - i += 7; - i += System_Int_GetString(fData+i, &sArg1); - if(!sArg1) goto read2eol; - Log("[CFG ] Load EDI Module '%s'\n", sArg1); - Module_LoadFile(sArg1, ""); + for( j = 0; j < NUM_CONFIG_COMMANDS; j++ ) + { + Uint args[N_MAX_ARGS]; + if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0) continue; + + Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1); + + if( line->nParts - 1 < caConfigCommands[j].MinArgs ) { + Log_Warning("Config", + "Configuration command '%s' requires at least %i arguments, %i given", + caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1 + ); + break; + } + + if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) { + Log_Warning("Config", + "Configuration command '%s' takes at most %i arguments, %i given", + caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1 + ); + break; + } + + for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) { + args[k] = caConfigCommands[j].OptDefaults[k]; + } + + for( k = line->nParts-1; k--; ) + { + if( caConfigCommands[j].IntArgs & (1 << k) ) { + args[k] = atoi(line->Parts[k+1]); + } + else { + args[k] = (Uint)line->Parts[k+1]; + } + Log_Debug("Config", "args[%i] = 0x%x", k, args[k]); + } + result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args); + Log_Debug("Config", "result = %i", result); + break; } - // - Symlink - else if(strncmp("symlink ", fData+i, 7) == 0) { - i += 8; - i += System_Int_GetString(fData+i, &sArg1); - if(!sArg1) goto read2eol; - i += System_Int_GetString(fData+i, &sArg2); - if(!sArg2) goto read2eol; - Log("[CFG ] Symlink '%s' pointing to '%s'\n", sArg1, sArg2); - VFS_Symlink(sArg1, sArg2); + if( j < NUM_CONFIG_COMMANDS ) continue; + + // --- State and Variables --- + if(strcmp(line->Parts[0], "set") == 0) + { + int to, value; + if( line->nParts-1 != 2 ) { + Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given", + line->nParts-1); + continue; + } + + to = atoi(line->Parts[1]); + value = atoi(line->Parts[2]); + + variables[to] = value; + result = value; } - // - New Directory - else if(strncmp("mkdir ", fData+i, 5) == 0) { - i += 6; - i += System_Int_GetString(fData+i, &sArg1); - if(!sArg1) goto read2eol; - Log("[CFG ] New Directory '%s'\n", sArg1); - VFS_MkDir(sArg1); + // if + else if(strcmp(line->Parts[0], "if") == 0) + { + if( line->nParts-1 != 4 ) { + Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given", + line->nParts-1); + } + + result = atoi(line->Parts[1]); + val = atoi(line->Parts[3]); + + jmpTarget = line->Parts[4]; + + Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s", + result, line->Parts[2], val, jmpTarget); + + if( strcmp(line->Parts[2], "<" ) == 0 ) { + if( result < val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], "<=") == 0 ) { + if( result <= val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], ">" ) == 0 ) { + if (result > val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], ">=") == 0 ) { + if( result >= val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], "=") == 0 ) { + if( result == val ) goto jumpToLabel; + } + else if( strcmp(line->Parts[2], "!=") == 0 ) { + if( result != val ) goto jumpToLabel; + } + else { + Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]); + } + } - // - Spawn a task - else if(strncmp("spawn ", fData+i, 5) == 0) { - i += 6; - i += System_Int_GetString(fData+i, &sArg1); - if(!sArg1) goto read2eol; - Log("[CFG ] Starting '%s' as a new task\n", sArg1); - Proc_Spawn(sArg1); + else if(strcmp(line->Parts[0], "goto") == 0) { + if( line->nParts-1 != 1 ) { + Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given", + line->nParts-1); + } + jmpTarget = line->Parts[1]; + + jumpToLabel: + for( j = 0; j < file->nLines; j ++ ) + { + if(file->Lines[j].nParts == 0) + continue; + if(file->Lines[j].Parts[0][0] != ':') + continue; + if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0) + break; + } + if( j == file->nLines ) + Log_Warning("Config", "Unable to find label '%s'", jmpTarget); + else + i = j; } else { - Warning("Unknown configuration command, Line: '%s'", fData+i); - goto read2eol; + Log_Warning("Config", "Unknown configuration command '%s' on line %i", + line->Parts[0], + line->TrueLine + ); } - read2eol: - if(sArg1) free(sArg1); - if(sArg2) free(sArg2); - if(sArg3) free(sArg3); - // Skip to EOL - while(i < fLen && fData[i] != '\n') i++; - i ++; // Skip \n } - free(fData); + + // Clean up after ourselves + for( i = 0; i < file->nLines; i++ ) { + if( file->Lines[i].nParts == 0 ) continue; // Skip blank + for( j = 0; j < file->Lines[i].nParts; j++ ) { + if(IsHeap(file->Lines[i].Parts[j])) + free(file->Lines[i].Parts[j]); + } + free( file->Lines[i].Parts ); + } + free( file ); + free( fData ); } /** - * \fn int System_Int_GetString(char *Str, char **Dest) - * \brief Gets a string from another - * \note Destructive - * \param Str Input String - * \param Dest Pointer to output pointer - * \return Characters eaten from input + * \brief Parses a config file + * \param FileData Read/Write buffer containing the config file data + * (will be modified) + * \return ::tConfigFile structure that represents the original contents + * of \a FileData */ -int System_Int_GetString(char *Str, char **Dest) +tConfigFile *System_Int_ParseFile(char *FileData) { - int pos = 0; - int start = 0; - int len; - - //LogF("GetString: (Str='%s', Dest=0x%x)\n", Str, Dest); - - while(Str[pos] == ' ' || Str[pos] == '\t') pos++; - if(Str[pos] == '\n' || Str[pos] == '\0') { - *Dest = NULL; - return pos; - } + char *ptr; + char *start; + int nLines = 1; + int i, j; + tConfigFile *ret; - // Quoted String - if(Str[pos] == '"') - { - pos ++; - start = pos; - while(Str[pos] != '"') pos++; + ENTER("pFileData", FileData); + + // Prescan and count the number of lines + for(ptr = FileData; *ptr; ptr++) + { + if(*ptr != '\n') continue; - len = pos - start; - *Dest = malloc( len + 1 ); - memcpy( *Dest, Str+start, len ); - (*Dest)[len] = '\0'; + if(ptr == FileData) { + nLines ++; + continue; + } - //LogF("GetString: RETURN *Dest = '%s'\n", *Dest); + // Escaped EOL + if(ptr[-1] == '\\') continue; - pos++; - return pos; + nLines ++; } - // Non-Quoted String - Whitespace deliminated - start = pos; - while(Str[pos] != ' ' && Str[pos] != '\t' && Str[pos] != '\n') pos++; + LOG("nLines = %i", nLines); - len = pos - start; - //LogF(" GetString: len = %i\n", len); - *Dest = malloc( len + 1 ); - memcpy( *Dest, Str+start, len ); - (*Dest)[len] = '\0'; + // Ok so we have `nLines` lines, now to allocate our return + ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines ); + ret->nLines = nLines; - //LogF("GetString: RETURN *Dest = '%s'\n", *Dest); + // Read the file for real + for( + ptr = FileData, i = 0; + *ptr; + i++ + ) + { + start = ptr; + + ret->Lines[i].nParts = 0; + + // Count parts + for(;;) + { + // Read leading whitespace + while( *ptr == '\t' || *ptr == ' ' ) ptr++; + + // End of line/file + if( *ptr == '\0' || *ptr == '\n' ) { + if(*ptr == '\n') ptr ++; + break; + } + // Comment + if( *ptr == '#' || *ptr == ';' ) { + while( *ptr && *ptr != '\n' ) ptr ++; + if(*ptr == '\n') ptr ++; + break; + } + + ret->Lines[i].nParts ++; + // Quoted + if( *ptr == '"' ) { + ptr ++; + while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' ) + ptr++; + continue; + } + // Unquoted + while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' ) + ptr++; + } + + LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts); + + if( ret->Lines[i].nParts == 0 ) { + ret->Lines[i].Parts = NULL; + continue; + } + + // Allocate part list + ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts ); + + // Fill list + for( ptr = start, j = 0; ; j++ ) + { + // Read leading whitespace + while( *ptr == '\t' || *ptr == ' ' ) ptr++; + + // End of line/file + if( *ptr == '\0' || *ptr == '\n' ) { + if(*ptr == '\n') ptr ++; + break; + } + // Comment + if( *ptr == '#' || *ptr == ';' ) { + while( *ptr && *ptr != '\n' ) ptr ++; + if(*ptr == '\n') ptr ++; + break; + } + + ret->Lines[i].Parts[j] = ptr; + + // Quoted + if( *ptr == '"' ) { + ptr ++; + ret->Lines[i].Parts[j] = ptr; + while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' ) + ptr++; + } + // Unquoted + else { + while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' ) + ptr++; + } + + // Break if we have reached NULL + if( *ptr == '\0' ) { + LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]); + break; + } + if( *ptr == '\n' ) { + *ptr = '\0'; + LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]); + ptr ++; + break; + } + *ptr = '\0'; // Cap off string + LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]); + ptr ++; // And increment for the next round + } + } - return pos; + LEAVE('p', ret); + return ret; }