-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
111 lines (92 loc) · 1.88 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Piglet
// Main program
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pigtypes.h"
#define MAX_FILENAME 80
int verbose = 0;
char script_file[MAX_FILENAME+1]={0};
int execute_script( char* script )
{
FILE *fp = fopen(script, "r");
char line[256];
int linenum=0;
int ret;
if (strlen(script) == 0)
{
printf("Must specify a script file.\n");
return -1;
}
fp = fopen(script, "r");
if (fp == NULL)
{
printf("Unable to open %s\n", script);
return -1;
}
while (fgets(line, 256, fp) != NULL)
{
// Simple parser for a very regular grammar
if (line[0] == '#') ;
else if ( !strcmp( line, "\n" ) ) continue;
else if ( strstr(line, "LOAD") != NULL)
{
ret = parseLoad( line );
}
else if ( strstr( line, "DUMP" ) != NULL )
{
ret = parseDump( line );
}
else if ( strstr( line, "FOREACH" ) != NULL )
{
ret = parseForeach( line );
}
else if ( strstr( line, "DESCRIBE" ) != NULL )
{
ret = parseDescribe( line );
}
else if ( strstr( line, "FILTER" ) != NULL )
{
ret = parseFilter( line );
}
else if ( strstr( line, "SORT" ) != NULL )
{
ret = parseSort( line );
}
else if ( strstr( line, "GROUP" ) != NULL )
{
ret = parseGroup( line );
}
else if ( strstr( line, "STORE" ) != NULL )
{
ret = parseStore( line );
}
else
{
printf("Cannot parse line %d\n", linenum);
ret = -1;
break;
}
linenum++;
}
return ret;
}
int main( int argc, char *argv[] )
{
int opt;
initRelations();
while ((opt = getopt(argc, argv, "vf:")) != -1)
{
switch (opt)
{
case 'v':
verbose = 1;
break;
case 'f':
strncpy( script_file, optarg, MAX_FILENAME );
break;
}
}
return execute_script( script_file );
}