Kernel - Cleaning up
[tpg/acess2.git] / Kernel / system.c
1 /*
2  * Acess 2
3  * Architecture Independent System Init
4  * system.c
5  */
6 #define DEBUG   0
7 #include <acess.h>
8
9 #define N_VARIABLES     16
10 #define N_MAX_ARGS      BITS
11
12 // === TYPES ===
13 typedef struct
14 {
15          int    TrueLine;
16          int    nParts;
17         char    **Parts;
18 }       tConfigLine;
19 typedef struct
20 {
21          int    nLines;
22         tConfigLine     Lines[];
23 }       tConfigFile;
24 typedef struct
25 {
26         const char      *Name;          // Name
27          int    MinArgs;        // Minimum number of arguments
28          int    MaxArgs;        // Maximum number of arguments
29         Uint    IntArgs;        // Bitmap of arguments that should be treated as integers
30         void    *Func;          // Function pointer
31         Uint    OptDefaults[N_MAX_ARGS];        // Default values for optional arguments
32 }       tConfigCommand;
33
34 // === IMPORTS ===
35 extern void     Arch_LoadBootModules(void);
36 extern int      Modules_LoadBuiltins(void);
37 extern void     Modules_SetBuiltinParams(char *Name, char *ArgString);
38 extern void     Debug_SetKTerminal(const char *File);
39
40 // === PROTOTYPES ===
41 void    System_Init(char *Commandline);
42 void    System_ParseCommandLine(char *ArgString);
43 void    System_ExecuteCommandLine(void);
44 void    System_ParseVFS(char *Arg);
45 void    System_ParseModuleArgs(char *Arg);
46 void    System_ParseSetting(char *Arg);
47 void    System_ExecuteScript(void);
48 tConfigFile     *System_Int_ParseFile(char *File);
49
50 // === CONSTANTS ===
51 const tConfigCommand    caConfigCommands[] = {
52         {"module",  1,2, 00, Module_LoadFile, {(Uint)"",0}},    // Load a module from a file
53         {"spawn",   1,1, 00, Proc_Spawn, {0}},          // Spawn a process
54         // --- VFS ---
55         {"mount",   3,4, 00, VFS_Mount, {(Uint)"",0}},          // Mount a device
56         {"symlink", 2,2, 00, VFS_Symlink, {0}}, // Create a Symbolic Link
57         {"mkdir",   1,1, 00, VFS_MkDir, {0}},           // Create a Directory
58         {"open",    1,2, 00, VFS_Open,  {VFS_OPENFLAG_READ,0}}, // Open a file
59         {"close",   1,1, 01, VFS_Close, {0}},   // Close an open file
60         {"ioctl",   3,3, 03, VFS_IOCtl, {0}},   // Call an IOCtl
61         
62         {"", 0,0, 0, NULL, {0}}
63 };
64 #define NUM_CONFIG_COMMANDS     (sizeof(caConfigCommands)/sizeof(caConfigCommands[0]))
65
66 // === GLOBALS ===
67 const char      *gsConfigScript = "/Acess/Conf/BootConf.cfg";
68 char    *argv[32];
69  int    argc;
70
71 // === CODE ===
72 void System_Init(char *CommandLine)
73 {
74         // Parse Kernel's Command Line
75         System_ParseCommandLine(CommandLine);
76         
77         // Initialise modules
78         Log_Log("Config", "Initialising builtin modules...");
79         Modules_LoadBuiltins();
80         Arch_LoadBootModules();
81         
82         System_ExecuteCommandLine();
83         
84         // - Execute the Config Script
85         Log_Log("Config", "Executing config script '%s'", gsConfigScript);
86         System_ExecuteScript();
87         
88         // Set the debug to be echoed to the terminal
89         Log_Log("Config", "Kernel now echoes to VT7 (Ctrl-Alt-F8)");
90         Debug_SetKTerminal("/Devices/VTerm/7");
91 }
92
93 /**
94  * \fn void System_ParseCommandLine(char *ArgString)
95  * \brief Parses the kernel's command line and sets the environment
96  */
97 void System_ParseCommandLine(char *ArgString)
98 {
99          int    i;
100         char    *str;
101         
102         Log_Log("Config", "Kernel Invocation (%p) \"%s\"", ArgString, ArgString);
103         Log_Log("Config", "Kernel Invocation '0x%x 0x%x'", ArgString[0], ArgString[1]);
104         
105         // --- Get Arguments ---
106         str = ArgString;
107         for( argc = 0; argc < 32; argc++ )
108         {
109                 // Eat Whitespace
110                 while(*str == ' ')      str++;
111                 // Check for the end of the string
112                 if(*str == '\0') {      argc--; break;} 
113                 argv[argc] = str;
114                 if(*str == '"') {
115                         while(*str && !(*str == '"' && str[-1] != '\\'))
116                                 str ++;
117                 }
118                 else {
119                         while(*str && *str != ' ')
120                                 str++;
121                 }
122                 if(*str == '\0')        break;  // Check for EOS
123                 *str = '\0';    // Cap off argument string
124                 str ++; // and increment the string pointer
125         }
126         if(argc < 32)
127                 argc ++;        // Count last argument
128         
129         // --- Parse Arguments (Pass 1) ---
130         for( i = 0; i < argc; i++ )
131         {
132                 switch(argv[i][0])
133                 {
134                 // --- VFS ---
135                 // Ignored on this pass
136                 case '/':
137                         break;
138                 
139                 // --- Module Paramaters ---
140                 // -VTerm:Width=640,Height=480,Scrollback=2
141                 case '-':
142                         System_ParseModuleArgs( argv[i] );
143                         break;
144                 // --- Config Options ---
145                 // SCRIPT=/Acess/Conf/BootConf.cfg
146                 default:
147                         System_ParseSetting( argv[i] );
148                         break;
149                 }
150         }
151 }
152
153 void System_ExecuteCommandLine(void)
154 {
155          int    i;
156         for( i = 0; i < argc; i++ )
157         {
158                 Log("argv[%i] = '%s'", i, argv[i]);
159                 switch(argv[i][0])
160                 {
161                 // --- VFS ---
162                 // Mount    /System=ext2:/Devices/ATA/A1
163                 // Symlink  /Acess=/System/Acess2
164                 case '/':
165                         System_ParseVFS( argv[i] );
166                         break;
167                 }
168         }
169 }
170
171 /**
172  * \fn void System_ParseVFS(char *Arg)
173  */
174 void System_ParseVFS(char *Arg)
175 {
176         char    *value;
177          int    fd;
178         
179         value = Arg;
180         // Search for the '=' token
181         while( *value && *value != '=' )
182                 value++;
183         
184         // Check if the equals was found
185         if( *value == '\0' ) {
186                 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
187                 return ;
188         }
189         
190         // Edit string
191         *value = '\0';  value ++;
192         
193         // Check assignment type
194         // - Symbolic Link <link>=<destination>
195         if(value[0] == '/')
196         {
197                 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
198                 VFS_Symlink(Arg, value);
199         }
200         // - Mount <mountpoint>=<fs>:<device>
201         else
202         {
203                 char    *dev = value;
204                 // Find colon
205                 while(*dev && *dev != ':')      dev++;
206                 if(*dev) {
207                         *dev = '\0';
208                         dev++;  // Eat ':'
209                 }
210                 // Create Mountpoint
211                 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
212                         Log_Log("Config", "Creating directory '%s'", Arg, value);
213                         VFS_MkDir( Arg );
214                 } else {
215                         VFS_Close(fd);
216                 }
217                 // Mount
218                 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
219                 VFS_Mount(dev, Arg, value, "");
220         }
221 }
222
223 /**
224  * \brief Parse a module argument string
225  * \param Arg   Argument string
226  */
227 void System_ParseModuleArgs(char *Arg)
228 {
229         char    *name, *args;
230          int    i;
231         
232         // Remove '-'   
233         name = Arg + 1;
234         
235         // Find the start of the args
236         i = strpos(name, ':');
237         if( i == -1 ) {
238                 Log_Warning("Config", "Module spec with no arguments");
239                 #if 1
240                 return ;
241                 #else
242                 i = strlen(name);
243                 args = name + i;
244                 #endif
245         }
246         else {
247                 name[i] = '\0';
248                 args = name + i + 1;
249         }
250         
251         Log_Log("Config", "Setting boot parameters for '%s' to '%s'", name, args);
252         Modules_SetBuiltinParams(name, args);
253 }
254
255 /**
256  * \fn void System_ParseSetting(char *Arg)
257  */
258 void System_ParseSetting(char *Arg)
259 {
260         char    *value;
261         value = Arg;
262
263         // Search for the '=' token
264         while( *value && *value != '=' )
265                 value++;
266         
267         // Check for boolean/flag (no '=')
268         if(*value == '\0')
269         {
270                 //if(strcmp(Arg, "") == 0) {
271                 //} else {
272                         Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
273                 //}
274         }
275         else
276         {
277                 *value = '\0';  // Remove '='
278                 value ++;       // and eat it's position
279                 
280                 if(strcmp(Arg, "SCRIPT") == 0) {
281                         Log_Log("Config", "Config Script: '%s'", value);
282                         if(strlen(value) == 0)
283                                 gsConfigScript = NULL;
284                         else
285                                 gsConfigScript = value;
286                 } else {
287                         Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
288                 }
289                 
290         }
291 }
292
293 /**
294  * \fn void System_ExecuteScript()
295  * \brief Reads and parses the boot configuration script
296  */
297 void System_ExecuteScript(void)
298 {
299          int    fp;
300          int    fLen = 0;
301          int    i, j, k;
302          int    val;
303          int    result = 0;
304          int    variables[N_VARIABLES];
305          int    bReplaced[N_MAX_ARGS];
306         char    *fData;
307         char    *jmpTarget;
308         tConfigFile     *file;
309         tConfigLine     *line;
310         
311         // Open Script
312         fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
313         if(fp == -1) {
314                 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
315                 return;
316         }
317         
318         // Get length
319         VFS_Seek(fp, 0, SEEK_END);
320         fLen = VFS_Tell(fp);
321         Log_Debug("System", "VFS_Tell(%i) = %i", fp, fLen);
322         VFS_Seek(fp, 0, SEEK_SET);
323         // Read into memory buffer
324         fData = malloc(fLen+1);
325         VFS_Read(fp, fLen, fData);
326         fData[fLen] = '\0';
327         VFS_Close(fp);
328         
329         
330         // Parse File
331         file = System_Int_ParseFile(fData);
332         
333         // Parse each line
334         for( i = 0; i < file->nLines; i++ )
335         {
336                 line = &file->Lines[i];
337                 if( line->nParts == 0 ) continue;       // Skip blank
338                 
339                 if(line->Parts[0][0] == ':')    continue;       // Ignore labels
340                 
341                 // Prescan and eliminate variables
342                 for( j = 1; j < line->nParts; j++ )
343                 {
344                         Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
345                         bReplaced[j] = 0;
346                         if( line->Parts[j][0] != '$' )  continue;
347                         if( line->Parts[j][1] == '?' ) {
348                                 val = result;
349                         }
350                         else {
351                                 val = atoi( &line->Parts[j][1] );
352                                 if( val < 0 || val > N_VARIABLES )      continue;
353                                 val = variables[ val ];
354                         }
355                         Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
356                         line->Parts[j] = malloc( BITS/8+2+1 );
357                         sprintf(line->Parts[j], "0x%x", val);
358                         bReplaced[j] = 1;
359                 }
360                 
361                 // Find the command name
362                 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
363                 {
364                         Uint    args[N_MAX_ARGS];
365                         
366                         if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0)       continue;
367                         
368                         Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
369                         
370                         // Check against minimum argument count
371                         if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
372                                 Log_Warning("Config",
373                                         "Configuration command '%s' requires at least %i arguments, %i given",
374                                         caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
375                                         );
376                                 break;
377                         }
378                         
379                         // Check for extra arguments
380                         if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
381                                 Log_Warning("Config",
382                                         "Configuration command '%s' takes at most %i arguments, %i given",
383                                         caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
384                                         );
385                                 break;
386                         }
387                         
388                         // Fill in defaults
389                         for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
390                                 args[k] = caConfigCommands[j].OptDefaults[k];
391                         }
392                         
393                         // Convert arguments to integers
394                         for( k = line->nParts-1; k--; )
395                         {
396                                 if( caConfigCommands[j].IntArgs & (1 << k) ) {
397                                         args[k] = atoi(line->Parts[k+1]);
398                                 }
399                                 else {
400                                         args[k] = (Uint)line->Parts[k+1];
401                                 }
402                                 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
403                         }
404                         result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
405                         Log_Debug("Config", "result = %i", result);
406                         break;
407                 }
408                 if( j < NUM_CONFIG_COMMANDS )   continue;
409                         
410                 // --- State and Variables ---
411                 if(strcmp(line->Parts[0], "set") == 0)
412                 {
413                          int    to, value;
414                         if( line->nParts-1 != 2 ) {
415                                 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
416                                         line->nParts-1);
417                                 continue;
418                         }
419                         
420                         to = atoi(line->Parts[1]);
421                         value = atoi(line->Parts[2]);
422                         
423                         variables[to] = value;
424                         result = value;
425                 }
426                 // if <val1> <op> <val2> <dest>
427                 else if(strcmp(line->Parts[0], "if") == 0)
428                 {
429                         if( line->nParts-1 != 4 ) {
430                                 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
431                                         line->nParts-1);
432                         }
433                         
434                         result = atoi(line->Parts[1]);
435                         val = atoi(line->Parts[3]);
436                         
437                         jmpTarget = line->Parts[4];
438                         
439                         Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
440                                 result, line->Parts[2], val, jmpTarget);
441                         
442                         if( strcmp(line->Parts[2], "<" ) == 0 ) {
443                                 if( result < val )      goto jumpToLabel;
444                         }
445                         else if( strcmp(line->Parts[2], "<=") == 0 ) {
446                                 if( result <= val )     goto jumpToLabel;
447                         }
448                         else if( strcmp(line->Parts[2], ">" ) == 0 ) {
449                                 if (result > val )      goto jumpToLabel;
450                         }
451                         else if( strcmp(line->Parts[2], ">=") == 0 ) {
452                                 if( result >= val )     goto jumpToLabel;
453                         }
454                         else if( strcmp(line->Parts[2],  "=") == 0 ) {
455                                 if( result == val )     goto jumpToLabel;
456                         }
457                         else if( strcmp(line->Parts[2], "!=") == 0 ) {
458                                 if( result != val )     goto jumpToLabel;
459                         }
460                         else {
461                                 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
462                         }
463                         
464                 }
465                 else if(strcmp(line->Parts[0], "goto") == 0) {
466                         if( line->nParts-1 != 1 ) {
467                                 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
468                                         line->nParts-1);
469                         }
470                         jmpTarget = line->Parts[1];
471                 
472                 jumpToLabel:
473                         for( j = 0; j < file->nLines; j ++ )
474                         {
475                                 if(file->Lines[j].nParts == 0)
476                                         continue;
477                                 if(file->Lines[j].Parts[0][0] != ':')
478                                         continue;
479                                 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
480                                         break;
481                         }
482                         if( j == file->nLines )
483                                 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
484                         else
485                                 i = j;
486                 }
487                 else {
488                         Log_Warning("Config", "Unknown configuration command '%s' on line %i",
489                                 line->Parts[0],
490                                 line->TrueLine
491                                 );
492                 }
493         }
494         
495         // Clean up after ourselves
496         for( i = 0; i < file->nLines; i++ ) {
497                 if( file->Lines[i].nParts == 0 )        continue;       // Skip blank
498                 for( j = 0; j < file->Lines[i].nParts; j++ ) {
499                         if(IsHeap(file->Lines[i].Parts[j]))
500                                 free(file->Lines[i].Parts[j]);
501                 }
502                 free( file->Lines[i].Parts );
503         }
504         
505         // Free data
506         free( file );
507         free( fData );
508 }
509
510 /**
511  * \brief Parses a config file
512  * \param FileData      Read/Write buffer containing the config file data
513  *                  (will be modified)
514  * \return ::tConfigFile structure that represents the original contents
515  *         of \a FileData
516  */
517 tConfigFile     *System_Int_ParseFile(char *FileData)
518 {
519         char    *ptr;
520         char    *start;
521          int    nLines = 1;
522          int    i, j;
523         tConfigFile     *ret;
524         
525         ENTER("pFileData", FileData);
526         
527         // Prescan and count the number of lines
528         for(ptr = FileData; *ptr; ptr++)
529         {               
530                 if(*ptr != '\n')        continue;
531                 
532                 if(ptr == FileData) {
533                         nLines ++;
534                         continue;
535                 }
536                 
537                 // Escaped EOL
538                 if(ptr[-1] == '\\')     continue;
539                 
540                 nLines ++;
541         }
542         
543         LOG("nLines = %i", nLines);
544         
545         // Ok so we have `nLines` lines, now to allocate our return
546         ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
547         ret->nLines = nLines;
548         
549         // Read the file for real
550         for(
551                 ptr = FileData, i = 0;
552                 *ptr;
553                 i++
554                 )
555         {
556                 start = ptr;
557                 
558                 ret->Lines[i].nParts = 0;
559                 ret->Lines[i].Parts = NULL;
560                 
561                 // Count parts
562                 for(;;)
563                 {
564                         // Read leading whitespace
565                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
566                         
567                         // End of line/file
568                         if( *ptr == '\0' || *ptr == '\n' ) {
569                                 if(*ptr == '\n')        ptr ++;
570                                 break;
571                         }
572                         // Comment
573                         if( *ptr == '#' || *ptr == ';' ) {
574                                 while( *ptr && *ptr != '\n' )   ptr ++;
575                                 if(*ptr == '\n')        ptr ++;
576                                 break;
577                         }
578                         
579                         ret->Lines[i].nParts ++;
580                         // Quoted
581                         if( *ptr == '"' ) {
582                                 ptr ++;
583                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
584                                         ptr++;
585                                 continue;
586                         }
587                         // Unquoted
588                         while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
589                                 ptr++;
590                 }
591                 
592                 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
593                 
594                 if( ret->Lines[i].nParts == 0 ) {
595                         ret->Lines[i].Parts = NULL;
596                         continue;
597                 }
598                 
599                 // Allocate part list
600                 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
601                 
602                 // Fill list
603                 for( ptr = start, j = 0; ; j++ )
604                 {
605                         // Read leading whitespace
606                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
607                         
608                         // End of line/file
609                         if( *ptr == '\0' || *ptr == '\n' ) {
610                                 if(*ptr == '\n')        ptr ++;
611                                 break;
612                         }
613                         // Comment
614                         if( *ptr == '#' || *ptr == ';' ) {
615                                 while( *ptr && *ptr != '\n' )   ptr ++;
616                                 if(*ptr == '\n')        ptr ++;
617                                 break;
618                         }
619                         
620                         ret->Lines[i].Parts[j] = ptr;
621                         
622                         // Quoted
623                         if( *ptr == '"' ) {
624                                 ptr ++;
625                                 ret->Lines[i].Parts[j] = ptr;
626                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
627                                         ptr++;
628                         }
629                         // Unquoted
630                         else {
631                                 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
632                                         ptr++;
633                         }
634                         
635                         // Break if we have reached NULL
636                         if( *ptr == '\0' ) {
637                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
638                                 break;
639                         }
640                         if( *ptr == '\n' ) {
641                                 *ptr = '\0';
642                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
643                                 ptr ++;
644                                 break;
645                         }
646                         *ptr = '\0';    // Cap off string
647                         LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
648                         ptr ++; // And increment for the next round
649                 }
650         }
651         
652         if( i < ret->nLines ) {
653                 ret->Lines[i].nParts = 0;
654                 ret->Lines[i].Parts = NULL;
655                 Log_Log("System", "Cleaning up final empty line");
656         }
657         
658         LEAVE('p', ret);
659         return ret;
660 }

UCC git Repository :: git.ucc.asn.au