-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
156 lines (145 loc) · 2.79 KB
/
main.c
File metadata and controls
156 lines (145 loc) · 2.79 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[50];
char value[256];
} Variable;
#define MAX_STRING 256
char text[MAX_STRING];
#define MAX_VARIABLES 100
Variable variables[MAX_VARIABLES];
int var_count = 0;
void set_variable(const char *name, const char *value)
{
for (int i = 0; i < var_count; i++)
{
if (strcmp(variables[i].name, name) == 0)
{
strncpy(variables[i].value, value, sizeof(variables[i].value));
return;
}
}
strncpy(variables[var_count].name, name, sizeof(variables[var_count].name));
strncpy(variables[var_count].value, value, sizeof(variables[var_count].value));
var_count++;
}
const char *get_variable(const char *name)
{
for (int i = 0; i < var_count; i++)
{
if (strcmp(variables[i].name, name) == 0)
{
return variables[i].value;
}
}
return NULL;
}
void remove_quotes(char *str)
{
size_t len = strlen(str);
if (str[0] == '"' && str[len - 1] == '"')
{
memmove(str, str + 1, len - 2);
str[len - 2] = '\0';
}
}
void interpret_line(char *line)
{
if (strncmp(line, "keep", 4) == 0)
{
char name[50], value[256];
if (sscanf(line, "keep %49[^=]= %255[^;];", name, value) == 2)
{
char *trimmed_name = strtok(name, " \t");
remove_quotes(value);
set_variable(trimmed_name, value);
}
}
else if (strncmp(line, "say", 3) == 0)
{
char arg[256];
if (sscanf(line, "say(\"%255[^\"]\");", text) == 1)
{
char *endQuotes = strchr(line, '"');
if (endQuotes)
{
endQuotes = strchr(endQuotes + 1, '"');
if (endQuotes && strlen(text) >= MAX_STRING - 1)
{
printf("Error: string exceeds max length of %d characters\n", MAX_STRING - 1);
}
else
{
printf("%s\n", text);
}
}
}
else
{
printf("Syntax error in say command.\n");
}
}
else if (strncmp(line, "get", 3) == 0)
{
char name[50];
if (sscanf(line, "get %49[^;];", name) == 1)
{
const char *value = get_variable(name);
if (value)
{
printf("%s\n", value);
}
else
{
printf("Variable '%s' not found.\n", name);
}
}
}
else if (strncmp(line, "remove", 6) == 0)
{
char name[50];
if (sscanf(line, "remove %49[^;];", name) == 1)
{
for (int i = 0; i < var_count; i++)
{
if (strcmp(variables[i].name, name) == 0)
{
for (int j = i; j < var_count - 1; j++)
{
variables[j] = variables[j + 1];
}
var_count--;
return;
}
}
printf("Variable '%s' not found.\n", name);
}
}
else
{
printf("Unknown command: %s", line);
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <file.jc>\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (!file)
{
perror("Could not open file");
return 1;
}
char line[512];
while (fgets(line, sizeof(line), file))
{
interpret_line(line);
}
fclose(file);
return 0;
}