Many changes, Mostly working on improving the BootConf script engine.
[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         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 int      Modules_LoadBuiltins();
36 //extern int    PCI_Install();
37 extern void     DMA_Install();
38 extern void     Debug_SetKTerminal(char *File);
39 extern void     StartupPrint(char *Str);
40
41 // === PROTOTYPES ===
42 void    System_Init(char *ArgString);
43 void    System_ParseCommandLine(char *ArgString);
44 void    System_ParseVFS(char *Arg);
45 void    System_ParseSetting(char *Arg);
46 void    System_ExecuteScript();
47 tConfigFile     *System_Int_ParseFile(char *File);
48
49 // === CONSTANTS ===
50 const tConfigCommand    caConfigCommands[] = {
51         {"module", 1,2, 0, Module_LoadFile, {(Uint)"",0}},      // Load a module from a file
52         {"spawn", 1,1, 0, Proc_Spawn, {0}},             // Spawn a process
53         // --- VFS ---
54         {"mount", 3,4, 0, VFS_Mount, {(Uint)"",0}},             // Mount a device
55         {"symlink", 2,2, 0, VFS_Symlink, {0}},  // Create a Symbolic Link
56         {"mkdir", 1,1, 0, VFS_MkDir, {0}},              // Create a Directory
57         {"open", 1,2, 0, VFS_Open, {VFS_OPENFLAG_READ,0}},      // Open a file
58         {"close", 1,1, 0x1, VFS_Close, {0}},    // Close an open file
59         {"ioctl", 3,3, 0x3, VFS_IOCtl, {0}},    // Call an IOCtl
60         
61         {"", 0,0, 0, NULL, {0}}
62 };
63 #define NUM_CONFIG_COMMANDS     (sizeof(caConfigCommands)/sizeof(caConfigCommands[0]))
64
65 // === GLOBALS ===
66 char    *gsConfigScript = "/Acess/Conf/BootConf.cfg";
67
68 // === CODE ===
69 void System_Init(char *ArgString)
70 {       
71         // Set the debug to be echoed to the terminal
72         StartupPrint("Kernel now echoes to VT7 (Ctrl-Alt-F8)");
73         Debug_SetKTerminal("/Devices/VTerm/7");
74         
75         // - Parse Kernel's Command Line
76         System_ParseCommandLine(ArgString);
77         
78         // - Execute the Config Script
79         Log_Log("Config", "Executing config script...");
80         System_ExecuteScript();
81 }
82
83 /**
84  * \fn void System_ParseCommandLine(char *ArgString)
85  * \brief Parses the kernel's command line and sets the environment
86  */
87 void System_ParseCommandLine(char *ArgString)
88 {
89         char    *argv[32];
90          int    argc;
91          int    i;
92         char    *str;
93         
94         Log_Log("Config", "Kernel Invocation \"%s\"", ArgString);
95         
96         // --- Get Arguments ---
97         str = ArgString;
98         for( argc = 0; argc < 32; argc++ )
99         {
100                 // Eat Whitespace
101                 while(*str == ' ')      str++;
102                 // Check for the end of the string
103                 if(*str == '\0') {      argc--; break;} 
104                 argv[argc] = str;
105                 while(*str && *str != ' ')
106                 {
107                         /*if(*str == '"') {
108                                 while(*str && !(*str == '"' && str[-1] != '\\'))
109                                         str ++;
110                         }*/
111                         str++;
112                 }
113                 if(*str == '\0')        break;  // Check for EOS
114                 *str = '\0';    // Cap off argument string
115                 str ++; // and increment the string pointer
116         }
117         if(argc < 32)
118                 argc ++;        // Count last argument
119         
120         // --- Parse Arguments ---
121         for( i = 1; i < argc; i++ )
122         {
123                 if( argv[i][0] == '/' )
124                         System_ParseVFS( argv[i] );
125                 else
126                         System_ParseSetting( argv[i] );
127         }
128 }
129
130 /**
131  * \fn void System_ParseVFS(char *Arg)
132  */
133 void System_ParseVFS(char *Arg)
134 {
135         char    *value;
136          int    fd;
137         
138         value = Arg;
139         // Search for the '=' token
140         while( *value && *value != '=' )
141                 value++;
142         
143         // Check if the equals was found
144         if( *value == '\0' ) {
145                 Log_Warning("Config", "Expected '=' in the string '%s'", Arg);
146                 return ;
147         }
148         
149         // Edit string
150         *value = '\0';  value ++;
151         
152         // Check assignment type
153         // - Symbolic Link <link>=<destination>
154         if(value[0] == '/')
155         {
156                 Log_Log("Config", "Symbolic link '%s' pointing to '%s'", Arg, value);
157                 VFS_Symlink(Arg, value);
158         }
159         // - Mount <mountpoint>=<fs>:<device>
160         else
161         {
162                 char    *dev = value;
163                 // Find colon
164                 while(*dev && *dev != ':')      dev++;
165                 if(*dev) {
166                         *dev = '\0';
167                         dev++;  // Eat ':'
168                 }
169                 // Create Mountpoint
170                 if( (fd = VFS_Open(Arg, 0)) == -1 ) {
171                         Log_Log("Config", "Creating directory '%s'", Arg, value);
172                         VFS_MkDir( Arg );
173                 } else {
174                         VFS_Close(fd);
175                 }
176                 // Mount
177                 Log_Log("Config", "Mounting '%s' to '%s' ('%s')", dev, Arg, value);
178                 VFS_Mount(dev, Arg, value, "");
179         }
180 }
181
182 /**
183  * \fn void System_ParseSetting(char *Arg)
184  */
185 void System_ParseSetting(char *Arg)
186 {
187         char    *value;
188         value = Arg;
189
190         // Search for the '=' token
191         while( *value && *value != '=' )
192                 value++;
193         
194         // Check for boolean/flag (no '=')
195         if(*value == '\0')
196         {
197                 //if(strcmp(Arg, "") == 0) {
198                 //} else {
199                         Log_Warning("Config", "Kernel flag '%s' is not recognised", Arg);
200                 //}
201         }
202         else
203         {
204                 *value = '\0';  // Remove '='
205                 value ++;       // and eat it's position
206                 
207                 if(strcmp(Arg, "SCRIPT") == 0) {
208                         Log_Log("Config", "Config Script: '%s'", value);
209                         gsConfigScript = value;
210                 } else {
211                         Log_Warning("Config", "Kernel config setting '%s' is not recognised", Arg);
212                 }
213                 
214         }
215 }
216
217 /**
218  * \fn void System_ExecuteScript()
219  */
220 void System_ExecuteScript()
221 {
222          int    fp;
223          int    fLen = 0;
224          int    i, j, k;
225          int    val;
226          int    result = 0;
227          int    variables[N_VARIABLES];
228          int    bReplaced[N_MAX_ARGS];
229         char    *fData;
230         char    *jmpTarget;
231         tConfigFile     *file;
232         tConfigLine     *line;
233         
234         // Open Script
235         fp = VFS_Open(gsConfigScript, VFS_OPENFLAG_READ);
236         if(fp == -1) {
237                 Log_Warning("Config", "Passed script '%s' does not exist", gsConfigScript);
238                 return;
239         }
240         
241         // Read into memory buffer
242         VFS_Seek(fp, 0, SEEK_END);
243         fLen = VFS_Tell(fp);
244         VFS_Seek(fp, 0, SEEK_SET);
245         fData = malloc(fLen+1);
246         VFS_Read(fp, fLen, fData);
247         fData[fLen] = '\0';
248         VFS_Close(fp);
249         
250         
251         // Parse File
252         file = System_Int_ParseFile(fData);
253         
254         // Parse each line
255         for( i = 0; i < file->nLines; i++ )
256         {
257                 line = &file->Lines[i];
258                 if( line->nParts == 0 ) continue;       // Skip blank
259                 
260                 if(line->Parts[0][0] == ':')    continue;       // Ignore labels
261                 
262                 // Prescan and eliminate variables
263                 for( j = 1; j < line->nParts; j++ ) {
264                         Log_Debug("Config", "Arg #%i is '%s'", j, line->Parts[j]);
265                         bReplaced[j] = 0;
266                         if( line->Parts[j][0] != '$' )  continue;
267                         if( line->Parts[j][1] == '?' ) {
268                                 val = result;
269                         }
270                         else {
271                                 val = atoi( &line->Parts[j][1] );
272                                 if( val < 0 || val > N_VARIABLES )      continue;
273                                 val = variables[ val ];
274                         }
275                         Log_Debug("Config", "Replaced arg %i ('%s') with 0x%x", j, line->Parts[j], val);
276                         line->Parts[j] = malloc( BITS/8+2+1 );
277                         sprintf(line->Parts[j], "0x%x", val);
278                         bReplaced[j] = 1;
279                 }
280                 
281                 for( j = 0; j < NUM_CONFIG_COMMANDS; j++ )
282                 {
283                         Uint    args[N_MAX_ARGS];
284                         if(strcmp(line->Parts[0], caConfigCommands[j].Name) != 0)       continue;
285                         
286                         Log_Debug("Config", "Command '%s', %i args passed", line->Parts[0], line->nParts-1);
287                         
288                         if( line->nParts - 1 < caConfigCommands[j].MinArgs ) {
289                                 Log_Warning("Config",
290                                         "Configuration command '%s' requires at least %i arguments, %i given",
291                                         caConfigCommands[j].Name, caConfigCommands[j].MinArgs, line->nParts-1
292                                         );
293                                 break;
294                         }
295                         
296                         if( line->nParts - 1 > caConfigCommands[j].MaxArgs ) {
297                                 Log_Warning("Config",
298                                         "Configuration command '%s' takes at most %i arguments, %i given",
299                                         caConfigCommands[j].Name, caConfigCommands[j].MaxArgs, line->nParts-1
300                                         );
301                                 break;
302                         }
303                         
304                         for( k = caConfigCommands[j].MaxArgs-1; k > line->nParts - 1; k-- ) {
305                                 args[k] = caConfigCommands[j].OptDefaults[k];
306                         }
307                         
308                         for( k = line->nParts-1; k--; )
309                         {
310                                 if( caConfigCommands[j].IntArgs & (1 << k) ) {
311                                         args[k] = atoi(line->Parts[k+1]);
312                                 }
313                                 else {
314                                         args[k] = (Uint)line->Parts[k+1];
315                                 }
316                                 Log_Debug("Config", "args[%i] = 0x%x", k, args[k]);
317                         }
318                         result = CallWithArgArray(caConfigCommands[j].Func, caConfigCommands[j].MaxArgs, args);
319                         Log_Debug("Config", "result = %i", result);
320                         break;
321                 }
322                 if( j < NUM_CONFIG_COMMANDS )   continue;
323                         
324                 // --- State and Variables ---
325                 if(strcmp(line->Parts[0], "set") == 0)
326                 {
327                          int    to, value;
328                         if( line->nParts-1 != 2 ) {
329                                 Log_Warning("Config", "Configuration command 'set' requires 2 arguments, %i given",
330                                         line->nParts-1);
331                                 continue;
332                         }
333                         
334                         to = atoi(line->Parts[1]);
335                         value = atoi(line->Parts[2]);
336                         
337                         variables[to] = value;
338                         result = value;
339                 }
340                 // if <val1> <op> <val2> <dest>
341                 else if(strcmp(line->Parts[0], "if") == 0)
342                 {
343                         if( line->nParts-1 != 4 ) {
344                                 Log_Warning("Config", "Configuration command 'if' requires 4 arguments, %i given",
345                                         line->nParts-1);
346                         }
347                         
348                         result = atoi(line->Parts[1]);
349                         val = atoi(line->Parts[3]);
350                         
351                         jmpTarget = line->Parts[4];
352                         
353                         Log_Log("Config", "IF 0x%x %s 0x%x THEN GOTO %s",
354                                 result, line->Parts[2], val, jmpTarget);
355                         
356                         if( strcmp(line->Parts[2], "<" ) == 0 ) {
357                                 if( result < val )      goto jumpToLabel;
358                         }
359                         else if( strcmp(line->Parts[2], "<=") == 0 ) {
360                                 if( result <= val )     goto jumpToLabel;
361                         }
362                         else if( strcmp(line->Parts[2], ">" ) == 0 ) {
363                                 if (result > val )      goto jumpToLabel;
364                         }
365                         else if( strcmp(line->Parts[2], ">=") == 0 ) {
366                                 if( result >= val )     goto jumpToLabel;
367                         }
368                         else if( strcmp(line->Parts[2],  "=") == 0 ) {
369                                 if( result == val )     goto jumpToLabel;
370                         }
371                         else if( strcmp(line->Parts[2], "!=") == 0 ) {
372                                 if( result != val )     goto jumpToLabel;
373                         }
374                         else {
375                                 Log_Warning("Config", "Unknown comparision '%s' in `if`", line->Parts[2]);
376                         }
377                         
378                 }
379                 else if(strcmp(line->Parts[0], "goto") == 0) {
380                         if( line->nParts-1 != 1 ) {
381                                 Log_Warning("Config", "Configuration command 'goto' requires 1 arguments, %i given",
382                                         line->nParts-1);
383                         }
384                         jmpTarget = line->Parts[1];
385                 
386                 jumpToLabel:
387                         for( j = 0; j < file->nLines; j ++ )
388                         {
389                                 if(file->Lines[j].nParts == 0)
390                                         continue;
391                                 if(file->Lines[j].Parts[0][0] != ':')
392                                         continue;
393                                 if( strcmp(file->Lines[j].Parts[0]+1, jmpTarget) == 0)
394                                         break;
395                         }
396                         if( j == file->nLines )
397                                 Log_Warning("Config", "Unable to find label '%s'", jmpTarget);
398                         else
399                                 i = j;
400                 }
401                 else {
402                         Log_Warning("Config", "Unknown configuration command '%s' on line %i",
403                                 line->Parts[0],
404                                 line->TrueLine
405                                 );
406                 }
407         }
408         
409         // Clean up after ourselves
410         for( i = 0; i < file->nLines; i++ ) {
411                 if( file->Lines[i].nParts == 0 )        continue;       // Skip blank
412                 for( j = 0; j < file->Lines[i].nParts; j++ ) {
413                         if(IsHeap(file->Lines[i].Parts[j]))
414                                 free(file->Lines[i].Parts[j]);
415                 }
416                 free( file->Lines[i].Parts );
417         }
418         free( file );
419         free( fData );
420 }
421
422 /**
423  * \brief Parses a config file
424  * \param FileData      Read/Write buffer containing the config file data
425  *                  (will be modified)
426  * \return ::tConfigFile structure that represents the original contents
427  *         of \a FileData
428  */
429 tConfigFile     *System_Int_ParseFile(char *FileData)
430 {
431         char    *ptr;
432         char    *start;
433          int    nLines = 1;
434          int    i, j;
435         tConfigFile     *ret;
436         
437         ENTER("pFileData", FileData);
438         
439         // Prescan and count the number of lines
440         for(ptr = FileData; *ptr; ptr++)
441         {               
442                 if(*ptr != '\n')        continue;
443                 
444                 if(ptr == FileData) {
445                         nLines ++;
446                         continue;
447                 }
448                 
449                 // Escaped EOL
450                 if(ptr[-1] == '\\')     continue;
451                 
452                 nLines ++;
453         }
454         
455         LOG("nLines = %i", nLines);
456         
457         // Ok so we have `nLines` lines, now to allocate our return
458         ret = malloc( sizeof(tConfigFile) + sizeof(tConfigLine)*nLines );
459         ret->nLines = nLines;
460         
461         // Read the file for real
462         for(
463                 ptr = FileData, i = 0;
464                 *ptr;
465                 i++
466                 )
467         {
468                 start = ptr;
469                 
470                 ret->Lines[i].nParts = 0;
471                 
472                 // Count parts
473                 for(;;)
474                 {
475                         // Read leading whitespace
476                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
477                         
478                         // End of line/file
479                         if( *ptr == '\0' || *ptr == '\n' ) {
480                                 if(*ptr == '\n')        ptr ++;
481                                 break;
482                         }
483                         // Comment
484                         if( *ptr == '#' || *ptr == ';' ) {
485                                 while( *ptr && *ptr != '\n' )   ptr ++;
486                                 if(*ptr == '\n')        ptr ++;
487                                 break;
488                         }
489                         
490                         ret->Lines[i].nParts ++;
491                         // Quoted
492                         if( *ptr == '"' ) {
493                                 ptr ++;
494                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
495                                         ptr++;
496                                 continue;
497                         }
498                         // Unquoted
499                         while( *ptr && !(*ptr == '\t' || *ptr == ' ') && *ptr != '\n' )
500                                 ptr++;
501                 }
502                 
503                 LOG("ret->Lines[%i].nParts = %i", i, ret->Lines[i].nParts);
504                 
505                 if( ret->Lines[i].nParts == 0 ) {
506                         ret->Lines[i].Parts = NULL;
507                         continue;
508                 }
509                 
510                 // Allocate part list
511                 ret->Lines[i].Parts = malloc( sizeof(char*) * ret->Lines[i].nParts );
512                 
513                 // Fill list
514                 for( ptr = start, j = 0; ; j++ )
515                 {
516                         // Read leading whitespace
517                         while( *ptr == '\t' || *ptr == ' ' )    ptr++;
518                         
519                         // End of line/file
520                         if( *ptr == '\0' || *ptr == '\n' ) {
521                                 if(*ptr == '\n')        ptr ++;
522                                 break;
523                         }
524                         // Comment
525                         if( *ptr == '#' || *ptr == ';' ) {
526                                 while( *ptr && *ptr != '\n' )   ptr ++;
527                                 if(*ptr == '\n')        ptr ++;
528                                 break;
529                         }
530                         
531                         ret->Lines[i].Parts[j] = ptr;
532                         
533                         // Quoted
534                         if( *ptr == '"' ) {
535                                 ptr ++;
536                                 while( *ptr && !(*ptr == '"' && ptr[-1] == '\\') && *ptr != '\n' )
537                                         ptr++;
538                         }
539                         // Unquoted
540                         else {
541                                 while( *ptr != '\t' && *ptr != ' ' && *ptr != '\n' )
542                                         ptr++;
543                         }
544                         
545                         // Break if we have reached NULL
546                         if( *ptr == '\0' ) {
547                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
548                                 break;
549                         }
550                         if( *ptr == '\n' ) {
551                                 *ptr = '\0';
552                                 LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
553                                 ptr ++;
554                                 break;
555                         }
556                         *ptr = '\0';    // Cap off string
557                         LOG("ret->Lines[%i].Parts[%i] = '%s'", i, j, ret->Lines[i].Parts[j]);
558                         ptr ++; // And increment for the next round
559                 }
560         }
561         
562         LEAVE('p', ret);
563         return ret;
564 }

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