-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
78 lines (68 loc) · 1.7 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
#include "monty.h"
void _monty(stack_t **stack, char *command, unsigned int line_number);
state_t state = {"stack", NULL, NULL, NULL};
/**
* main - entry point of monty bytecode interpreter
* @argc: arguments count
* @argv: arguments vector
* Return: 0 (success), non-zero on error
*/
int main(int argc, char *argv[])
{
FILE *file;
char *buffer, *command;
size_t buffer_len;
unsigned int line_number;
stack_t *stack = NULL;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
file = fopen(argv[1], "r");
if (file == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
line_number = 0, buffer = NULL, buffer_len = 0;
while (getline(&buffer, &buffer_len, file) != -1)
{
line_number++;
if (*buffer == 10) /* skip empty lines */
continue;
command = strtok(buffer, " \t\n");
state.argument = strtok(NULL, " \t\n");
if (command == NULL || *command == '#') /* skip comments */
continue;
_monty(&stack, command, line_number);
}
if (buffer)
free(buffer);
free_stack(stack);
fclose(file);
return (0);
}
/**
* _monty - a monty bytecode interpreter for working with stacks and queues
* @stack: double pointer to stack
* @command: command or opcode of instruction
* @line_number: line number of instruction file
*/
void _monty(stack_t **stack, char *command, unsigned int line_number)
{
int i = 0;
instruction_t instructions[] = INSTRUCTIONS;
while (instructions[i].opcode)
{
if (strcmp(command, instructions[i].opcode) == 0)
{
instructions[i].f(stack, line_number);
return;
}
i++;
}
fprintf(stderr, "L%d: unknown instruction %s\n", line_number, command);
free_stack(*stack);
exit(EXIT_FAILURE);
}