-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty_main.c
96 lines (89 loc) · 1.69 KB
/
monty_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
#include "monty.h"
static stack_t *Values;
static char **Lines;
static int Lines_Count;
static int Current_Line;
static char Data_Format;
/**
* main - A Monty ByteCode interpreter
* @argc: The number of arguments passed to the program
* @argv: The arguments passed to the program
*
* Return: 0 if successful, otherwise 1
*/
int main(int argc, char *argv[])
{
if (argc == 2)
{
Data_Format = DF_LIFO;
Lines = read_file(argv[1], &Lines_Count);
while (Current_Line < Lines_Count)
{
execute_line(Lines[Current_Line], Current_Line + 1, &Values);
Current_Line++;
}
}
else
{
write(STDERR_FILENO, "USAGE: monty file\n", 18);
exit(EXIT_FAILURE);
}
clean_up_program();
return (EXIT_SUCCESS);
}
/**
* get_data_mode - Returns a pointer to the interpreter's mode flag
*
* Return: A pointer to the interpreter's mode flag
*/
char *get_data_mode(void)
{
return (&Data_Format);
}
/**
* get_lines - Retrieves the line of the current file
*
* Return: The lines of the current file
*/
char **get_lines(void)
{
return (Lines);
}
/**
* clean_up_program - Frees dynamically allocated memory blocks
*/
void clean_up_program(void)
{
stack_t *node = Values, *tmp = NULL;
int i;
if (Lines != NULL)
{
for (i = 0; i < Lines_Count; i++)
{
if (Lines[i] != NULL)
free(Lines[i]);
}
if (Lines != NULL)
free(Lines);
}
if (node != NULL)
{
while ((node != NULL) && (node->prev != NULL))
node = node->prev;
while (node != NULL)
{
tmp = node->next;
free(node);
node = tmp;
}
}
}
/**
* exit_program - Safely exits the program with the given status code
* @status: The status code to exit with
*/
void exit_program(int status)
{
clean_up_program();
exit(status);
}