forked from KFredlund/monty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
93 lines (89 loc) · 1.44 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
#include "monty.h"
#include <fcntl.h>
#include <ctype.h>
/**
* main - Entry point
* @argc: number of args
* @argv: pointer to args
*
* Return: Never
*/
int main(int argc, char **argv)
{
char *line = NULL;
ssize_t i = 0, c = 1;
size_t buffersize = 1024;
void (*func)(stack_t **, unsigned int);
unsigned int count = 1;
fp = NULL;
if (argc != 2)
handle_error(0, count, argv[1]);
fp = fopen(argv[1], "r");
if (!fp)
handle_error(2, count, argv[1]);
tokens = NULL;
while (c == 1)
{
line = NULL;
i = getline(&line, &buffersize, fp);
add_node(line);
if (i != -1 && line[0] == '#')
{
count++;
continue;
}
if (i != -1)
{
tokens = parse_line(line);
func = getop(tokens[0], count);
if (tokens[0])
check_push(tokens, count);
func(&list, count), free(tokens);
}
else
c = 0;
count++;
}
fclose(fp);
free_list(&tok_get), free_stack(list);
return (0);
}
/**
* add_node - Function that adds a node
* @str: top of stack
*
* Return: Never
*/
void add_node(char *str)
{
used_m *new;
new = malloc(sizeof(used_m));
if (new == NULL)
exit(EXIT_FAILURE);
new->data = str;
new->next = tok_get;
tok_get = new;
}
/**
* free_list - Function that frees a list
* @head: top of stack
*
* Return: Never
*/
void free_list(used_m **head)
{
used_m *new;
unsigned int i = 0;
if (*head)
{
while (*head)
{
new = *head;
*head = (*head)->next;
if (new->data != NULL)
free(new->data);
free(new);
i++;
}
}
}