-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
130 lines (110 loc) · 2.46 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "monty.h"
char *block_betty(char *blocks_betty_global);
global_t *global;
/**
* execute_cmd - sort execute command
* @cmd: command
* @line_number: line number
* Return: 1 on success
*/
int execute_cmd(char *cmd, unsigned int line_number)
{
int i = 0;
instruction_t commands[] = {
{"push", exec_push},
{"pall", exec_pall},
{"pint", exec_pint},
{"pop", exec_pop},
{"swap", exec_swap},
{"add", exec_add},
{"sub", exec_sub},
{"nop", exec_nop},
{"div", exec_div},
{"mul", exec_mul},
{"mod", exec_mod},
{"pchar", exec_pchar},
{"pstr", exec_pstr},
{"rotl", exec_rotl},
{"rotr", exec_rotr},
{"stack", exec_stack},
{"queue", exec_queue},
/* Add more opcodes and functions as needed */
{NULL, NULL}
};
while (commands[i].opcode != NULL)
{
if (strcmp(cmd, commands[i].opcode) == 0)
{
commands[i].f(&(global->stack), line_number, global->data_arg);
return (1);
}
i++;
}
fprintf(stderr, "L%d: unknown instruction %s\n", line_number, cmd);
if (global->file_ptr)
fclose(global->file_ptr);
exit(EXIT_FAILURE);
}
/**
* file_reader - reads from file
* @file: file
* Return: 1 on succes and 0 otherwise
*/
int file_reader(char *file)
{
char cmd[LENGTH] = "";
unsigned int line_number = 0;
char line[LENGTH];
char *trimmed_line;
global->file_ptr = fopen(file, "r");
if (global->file_ptr == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", file);
exit_failure(global);
}
while (fgets(line, sizeof(line), global->file_ptr) != NULL)
{
line_number++;
if (line[0] == '#' || (line[0] == ' ' && line[1] == '#'))
continue;
trimmed_line = strtok(line, " \t\n");
if (trimmed_line == NULL || trimmed_line[0] == '#')
continue;
strncpy(cmd, trimmed_line, sizeof(cmd) - 1);
global->data_arg = strtok(NULL, " \t\n");
execute_cmd(cmd, line_number);
}
return (1);
}
/**
* main - entry point for monty
* @argc: argument count
* @argv: argument vector
* Return: SUCCESS on success FAILURE on failure
*/
int main(int argc, char *argv[])
{
char *file;
global = malloc(sizeof(global_t));
if (global == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
global->data_arg = NULL;
global->file_ptr = NULL;
global->stack = NULL;
global->mode = 1;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit_failure(global);
}
file = argv[1];
file_reader(file);
free_stack(global->stack);
if (global->file_ptr)
fclose(global->file_ptr);
free(global);
return (0);
}