luxos/Modules/Rom image maker/main.cpp
Tiberiu Chibici 852cf1bb17 [GOOD] BUILD 0.1.0.590 DATE 9/05/2011 AT 2:40 PM
====================================================
Mainly changed: FS.Initrd
+ (kind of) refractored VFS, bugfixed
+ Rewrote 'initrd' file system, fixed many problems
+ Working 'cat' and 'dir' console commands
+ Wrote 'initrd' image write application (for windows), however it may
be bugged
2021-09-14 18:52:47 +03:00

132 lines
3.6 KiB
C++

/*
* File: main.cpp
* Author: Tiberiu
*
* Created on August 31, 2011, 2:54 PM
*/
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "types.h"
using namespace std;
extern void CommandCreate (char** argv, int argc);
extern void CommandSetFlags (char** argv, int argc);
extern void CommandAddFile (char** argv, int argc);
extern void CommandAddDirectory (char** argv, int argc);
extern void CommandChangeDirectory (char** argv, int argc);
extern void CommandClose ();
void GetParams(char* buffer, char* params[], int &count)
{
bool inside_quotes = false;
int len = strlen(buffer);
count = 0;
if (buffer[0] == '#')
{
count = 1; params[0] = ""; return;
}
if (!isspace(buffer[0])) params[count++] = buffer;
else buffer[0] = 0;
for (int i = 1; i < len && count < 16; i++)
{
if (buffer[i] == '"') {
inside_quotes = (inside_quotes) ? false : true;
if (inside_quotes && buffer[i-1] == 0) params[count++] = &buffer[i];
++i;
}
if (inside_quotes) continue;
// Comment
if (buffer[i] == '#') {
buffer[i] = 0;
break;
}
if (!isspace(buffer[i]) && (buffer[i-1]==0))
params[count++] = &buffer[i];
else if (isspace(buffer[i])) buffer[i] = 0;
}
if (count == 0) {
count = 1;
params[0] = "";
}
}
bool LineEmpty (char* s)
{
for (int i = 0; i < strlen(s); i++)
if (!isspace(s[i])) return false;
}
void ExecuteScript (char* input)
{
char buffer[1024];
char* argv[16]; int argc;
// Try to open file
FILE* in = fopen(input, "r");
if (!in) { perror(input); return ; }
// Read file line by line
printf("Reading script file %s...\n", input);
for (int line = 0; fgets(buffer, 1024, in); line++)
{
GetParams(buffer, argv, argc);
try {
if (strcmp(argv[0], "CREATE") == 0) CommandCreate(argv, argc);
else if (strcmp(argv[0], "CLOSE") == 0) CommandClose();
else if (strcmp(argv[0], "MKDIR") == 0) CommandAddDirectory(argv, argc);
else if (strcmp(argv[0], "CD") == 0) CommandChangeDirectory(argv, argc);
else if (strcmp(argv[0], "ADD") == 0) CommandAddFile(argv, argc);
else if (strcmp(argv[0], "SETFLAGS") == 0) CommandSetFlags(argv, argc);
else if (LineEmpty(argv[0])) continue;
else {
fprintf(stderr, "%i: Invalid command.\n", line);
exit(1);
}
}
catch (int exc) {
// Exception handler
switch (exc) {
case ExcCannotOpenInput: fprintf(stderr, "%i: Cannot open input file.\n", line); break;
case ExcCannotOpenOutput: fprintf(stderr, "%i: Cannot open output file.\n", line); break;
case ExcInvalidCommand: fprintf(stderr, "%i: Invalid command.\n", line); break;
case ExcInvalidPath: fprintf(stderr, "%i: Invalid path.\n", line); break;
case ExcSyntaxError: fprintf(stderr, "%i: Syntax error, robably missing parameter.\n", line); break;
}
printf("An error occurred, execution has been terminated.\n");
exit(1);
}
memset(buffer, 0, 1024 * sizeof(char));
}
fclose (in);
}
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "Missing parameter: script file.");
return 1;
}
for (int i = 1; i < argc; i++)
ExecuteScript (argv[i]);
return 0;
}