-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseHelper.c
More file actions
executable file
·71 lines (62 loc) · 1.31 KB
/
parseHelper.c
File metadata and controls
executable file
·71 lines (62 loc) · 1.31 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include "main.h"
/**
* parseline - tokenizes a line of text, storing it in line struct
* @line: struct containing line contents and line number
* @buffer: string of text read from script file
*
* Return: Nothing
*/
void parseline(line_t *line, char *buffer)
{
unsigned int i;
char *token = NULL;
line->content = malloc(sizeof(char *) * 3);
if (!line->content)
{
fprintf(stderr, "Error: malloc failed");
exit(EXIT_FAILURE);
}
token = strtok(buffer, " '\n'");
for (i = 0; token && i < 2; i++)
{
line->content[i] = token;
token = strtok(NULL, " \n");
}
line->content[i] = NULL;
}
/**
* parsefile - reads and parses file one line at a time
* @file: the script to be read
*
* Return: Nothing
*/
void parsefile(FILE *file)
{
size_t size = 0;
meta_t *meta = NULL;
line_t line;
meta = malloc(sizeof(meta_t));
if (!meta)
{
fprintf(stderr, "Error: malloc failed");
exit(EXIT_FAILURE);
}
line.number = 0;
line.content = NULL;
meta->file = file;
meta->stack = NULL;
meta->buf = NULL;
while (getline(&(meta->buf), &size, meta->file) != -1)
{
line.number++;
parseline(&line, meta->buf);
if (line.content)
get_op_func(line, meta)(&(meta->stack), line.number);
}
free(meta->buf);
free_stack(&(meta->stack));
fclose(meta->file);
free(meta);
}