/* * main.cpp * * Created on: Aug 31, 2011 * Author: Tiberiu */ #include #include #include #include "app.h" using namespace std; void GetParams (char* buffer, char* params[4], int& count) { bool inside_quotes = false; int len = strlen(buffer); count = 0; if (!isspace(buffer[0])) params[count++] = buffer; else buffer[0] = 0; for (int i = 1; i < len && count < 4; 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; } } void ProcessLine (char* buffer, int lineno) { char* argv[4]; int argc, res; GetParams(buffer, argv, argc); printf("%i: ", lineno); if (strcmp(argv[0], "CREATE") == 0) res = cmd_create(argv, argc); else if (strcmp(argv[0], "CLOSE") == 0) res = cmd_close(argv, argc); else if (strcmp(argv[0], "MKDIR") == 0) res = cmd_mkdir(argv, argc); else if (strcmp(argv[0], "CD") == 0) res = cmd_cd(argv, argc); else if (strcmp(argv[0], "ADD") == 0) res = cmd_add(argv, argc); else if (strcmp(argv[0], "SETFLAGS") == 0) res = cmd_setflags(argv, argc); else if (strcmp(argv[0], "" )) return; else printf("Invalid command %s, ignored.", argv[0]); if (!res) { printf("Syntax error!"); exit(1); } printf("\n"); } int main(int argc, char **argv) { char buffer[0x1000]; int lineno = 0; // Make sure we have an argument if (argc < 2) { fprintf(stderr, "Missing argument - script file."); return 0; } // Try to open file FILE* input = fopen(argv[1], "r"); if (!input) { perror(argv[1]); return 0; } // Read file line by line printf("Reading script...\n"); while ( fgets(buffer, 0x1000, input) ) ProcessLine(buffer, ++lineno); // Cleanup fclose(input); return 0; }