Skip to content

Instantly share code, notes, and snippets.

@MacoChave
Created August 9, 2018 03:16
Show Gist options
  • Save MacoChave/c445602ad0e801a90a4e343dc8828cf2 to your computer and use it in GitHub Desktop.
Save MacoChave/c445602ad0e801a90a4e343dc8828cf2 to your computer and use it in GitHub Desktop.
Analizador para proyecto de MIA 2s 2018
#ifndef ANALIZER_H
#define ANALIZER_H
#include <stdio.h>
#include "structs/mlist.h"
#include "structs/comando.h"
#include "structs/parametro.h"
#include "commands/cmd_mkdisk.h"
#include "commands/cmd_rmdisk.h"
#include "commands/cmd_fdisk.h"
//#include <stdlib.h>
//#include <string.h>
//#include <ctype.h>
//#include <time.h>
//#include <sys/types.h>
//#include <dirent.h>
/************************************************************
* ANALIZAR UNA LINEA DE COMANDO
************************************************************/
MList * analize(char * command, int * command_type)
{
/*
* -1 -> NONE
* 0 -> MKDISK
* 1 -> RMDISK
* 2 -> FDISK
* 3 -> MOUNT
* 4 -> UNMOUNT
* 5 -> REP
* 6 -> EXEC
*/
char * name;
char * value;
MList * parameters = new_MList();
char * temp = (char * )malloc(sizeof(char) * 125);
memset(temp, 0, sizeof(char) * 125);
char * s = command;
/*
* 0 -> TYPE
* 1 -> PARAMETER
* 2 -> VALUE
*/
int step = 0;
int quotation = -1;
while (*s) {
if (step == 0)
{
if (*s != ' ')
{
sprintf(temp, "%s%c", temp, *s);
s++;
continue;
}
}
else if (step == 1)
{
if (*s == '-')
{
s++;
continue;
}
else if (*s != '>')
{
sprintf(temp, "%s%c", temp, *s);
s++;
continue;
}
}
else if (step == 2)
{
if (*s == '\"')
{
s++;
quotation *= -1;
continue;
}
else if (*s == ' ')
{
if (quotation == 1)
{
sprintf(temp, "%s%c", temp, *s);
s++;
continue;
}
}
else
{
sprintf(temp, "%s%c", temp, *s);
s++;
if (*s)
continue;
}
}
else if (*s == '#')
break;
if (strcasecmp(temp, "mkdisk") == 0)
{
*command_type = 0;
step = 1;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "rmdisk") == 0)
{
*command_type = 1;
step = 1;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "fdisk") == 0)
{
*command_type = 2;
step = 1;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "mount") == 0)
{
*command_type = 3;
step = 1;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "unmount") == 0)
{
*command_type = 4;
step = 1;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "rep") == 0)
{
*command_type = 5;
step = 1;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "exec") == 0)
{
*command_type = 6;
step = 1;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "size") == 0)
{
name = (char * )malloc(sizeof (char) * 5);
strcpy(name, "size");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "path") == 0)
{
name = (char * )malloc(sizeof (char) * 5);
strcpy(name, "path");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "unit") == 0)
{
name = (char * )malloc(sizeof (char) * 5);
strcpy(name, "unit");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "name") == 0)
{
name = (char * )malloc(sizeof (char) * 5);
strcpy(name, "name");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "type") == 0)
{
name = (char * )malloc(sizeof (char) * 5);
strcpy(name, "type");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "fit") == 0)
{
name = (char * )malloc(sizeof (char) * 4);
strcpy(name, "fit");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "delete") == 0)
{
name = (char * )malloc(sizeof (char) * 7);
strcpy(name, "delete");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "add") == 0)
{
name = (char * )malloc(sizeof (char) * 4);
strcpy(name, "add");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else if (strcasecmp(temp, "id") == 0)
{
name = (char * )malloc(sizeof (char) * 3);
strcpy(name, "id");
step = 2;
memset(temp, 0, sizeof(char) * 125);
s++;
continue;
}
else
{
value = (char * )malloc(strlen(temp));
memset(value, 0, strlen(temp));
strcpy(value, temp); // sprintf(value, "%s", temp);
step = 1;
s++;
ParameterStruct * ps = (ParameterStruct * )malloc(sizeof (ParameterStruct));
ps->clave = (char * )malloc(strlen(name));
ps->valor = (char * )malloc(strlen(value));
strcpy(ps->clave, name);
strcpy(ps->valor, value);
push_back(&parameters, ps);
memset(value, 0, strlen(temp));
memset(temp, 0, sizeof(char) * 125);
}
}
return parameters;
}
/************************************************************
* EJECUTAR UNA LÍNEA DE COMANDO
************************************************************/
void execute(int command_type, MList * parameters)
{
switch (command_type) {
case 0: // MKDISK
exec_mkdisk(parameters);
break;
case 1: // RMDISK
exec_rmdisk(parameters);
break;
case 2: //FDISK
exec_fdisk(parameters);
break;
case 3: // MOUNT
break;
case 4: // UNMOUNT
break;
case 5: // REP
break;
}
}
/************************************************************
* DESEMPAQUETAR UN ARCHIVO DE COMANDOS
************************************************************/
void executeCommandFile(MList * comands)
{
MListNode * currentCommand = comands->first;
while (currentCommand != NULL) {
CommandStruct * cs = (CommandStruct *)currentCommand->data;
MList * pc;
int ct;
pc = analize(cs->comando, &ct);
execute(ct, pc);
currentCommand = currentCommand->next;
}
}
void executeFile(char * filename)
{
FILE * file;
MList * comands = new_MList();
file = fopen(filename, "r");
if (file == NULL)
{
perror("Archivo no disponible\n");
return;
}
while (!feof(file))
{
char line_text[100];
memset(line_text, 0, 100);
fgets(line_text, 100, file);
if (line_text[0] == '#')
continue;
CommandStruct * cs = (CommandStruct *)malloc(sizeof (CommandStruct));
cs->comando = (char *)malloc(strlen(line_text));
strcpy(cs->comando, line_text);
push_back(&comands, cs);
fflush(file);
}
fclose(file);
executeCommandFile(comands);
}
/************************************************************
* DESEMPAQUETAR UNA LÍNEA DE COMANDO
************************************************************/
void executeCommand(char * command)
{
MList * pc;
int ct;
pc = analize(command, &ct);
if (ct == 6)
{
ParameterStruct * ps = NULL;
MListNode * current = pc->first;
while (current != NULL) {
ps = (ParameterStruct * )current->data;
if (strcasecmp(ps->clave, "path") == 0)
break;
else
current = current->next;
}
if (ps != NULL)
executeFile(ps->valor);
else
perror("Se requiere 'path' para comando exec.\n");
}
else
execute(ct, pc);
}
#endif // ANALIZER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment