-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
74 lines (67 loc) · 1.41 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
#include "monty.h"
#include "lists.h"
data_t data = DATA_INIT;
/**
* monty - helper function for main function
* @args: pointer to struct of arguments from main
*
* Description: opens and reads from the file
* containing the opcodes, and calls the function
* that will find the corresponding executing function
*/
void monty(args_t *args)
{
size_t len = 0;
int get = 0;
void (*code_func)(stack_t **, unsigned int);
if (args->ac != 2)
{
dprintf(STDERR_FILENO, USAGE);
exit(EXIT_FAILURE);
}
data.fptr = fopen(args->av, "r");
if (!data.fptr)
{
dprintf(STDERR_FILENO, FILE_ERROR, args->av);
exit(EXIT_FAILURE);
}
while (1)
{
args->line_number++;
get = getline(&(data.line), &len, data.fptr);
if (get < 0)
break;
data.words = strtow(data.line);
if (data.words[0] == NULL || data.words[0][0] == '#')
{
free_all(0);
continue;
}
code_func = get_func(data.words);
if (!code_func)
{
dprintf(STDERR_FILENO, UNKNOWN, args->line_number, data.words[0]);
free_all(1);
exit(EXIT_FAILURE);
}
code_func(&(data.stack), args->line_number);
free_all(0);
}
free_all(1);
}
/**
* main - entry point for monty bytecode interpreter
* @argc: number of arguments
* @argv: array of arguments
*
* Return: EXIT_SUCCESS or EXIT_FAILURE
*/
int main(int argc, char *argv[])
{
args_t args;
args.av = argv[1];
args.ac = argc;
args.line_number = 0;
monty(&args);
return (EXIT_SUCCESS);
}