PatchworkOS  621ae6b
A non-POSIX operating system.
Loading...
Searching...
No Matches
manifest.c
Go to the documentation of this file.
1#include "manifest.h"
2
3#include <ctype.h>
4#include <stdio.h>
5#include <string.h>
6#include <sys/io.h>
7#include <sys/math.h>
8#include <sys/proc.h>
9
10static char* trim_whitespace(char* str)
11{
12 char* end;
13
14 while (isspace((unsigned char)*str))
15 {
16 str++;
17 }
18
19 if (*str == '\0')
20 {
21 return str;
22 }
23
24 end = str + strlen(str) - 1;
25 while (end > str && isspace((unsigned char)*end))
26 {
27 end--;
28 }
29
30 end[1] = '\0';
31
32 return str;
33}
34
36{
37 FILE* file = fopen(path, "r");
38 if (file == NULL)
39 {
40 return ERR;
41 }
42
43 memset(manifest, 0, sizeof(manifest_t));
44
45 char line[MAX_PATH] = {0};
47 while (fgets(line, sizeof(line), file) != NULL)
48 {
49 char* p = trim_whitespace(line);
50
51 if (p[0] == '#' || p[0] == ';' || p[0] == '\0')
52 {
53 continue;
54 }
55
56 if (p[0] == '[')
57 {
58 char* end = strchr(p, ']');
59 if (end == NULL)
60 {
61 continue;
62 }
63
64 *end = '\0';
65 char* name = p + 1;
66 if (strcmp(name, "meta") == 0)
67 {
68 section = &manifest->sections[SECTION_META];
69 }
70 else if (strcmp(name, "exec") == 0)
71 {
72 section = &manifest->sections[SECTION_EXEC];
73 }
74 else if (strcmp(name, "env") == 0)
75 {
76 section = &manifest->sections[SECTION_ENV];
77 }
78 else if (strcmp(name, "sandbox") == 0)
79 {
80 section = &manifest->sections[SECTION_SANDBOX];
81 }
82 else if (strcmp(name, "namespace") == 0)
83 {
85 }
86 else
87 {
88 section = NULL;
89 }
90
91 continue;
92 }
93
94 if (section == NULL)
95 {
96 continue;
97 }
98
99 char* equal = strchr(p, '=');
100 if (equal == NULL)
101 {
102 continue;
103 }
104
105 *equal = '\0';
106
107 char* key = trim_whitespace(p);
108 char* value = trim_whitespace(equal + 1);
109
110 if (section->amount >= MANIFEST_SECTION_MAX)
111 {
112 continue;
113 }
114
115 strncpy(section->entries[section->amount].key, key, MANIFEST_STRING_MAX - 1);
116 strncpy(section->entries[section->amount].value, value, MANIFEST_STRING_MAX - 1);
117 section->amount++;
118 }
119
120 fclose(file);
121 return 0;
122}
123
125{
126 for (uint64_t i = 0; i < SECTION_TYPE_MAX; i++)
127 {
128 section_t* section = &manifest->sections[i];
129
130 for (uint64_t j = 0; j < section->amount; j++)
131 {
132 section_entry_t* entry = &section->entries[j];
133
134 for (uint64_t k = 0; k < amount; k++)
135 {
138
139 if (snprintf(search, sizeof(search), "$%s", sub->key) >= (int)sizeof(search))
140 {
141 continue;
142 }
143
144 size_t searchLen = strlen(search);
145 size_t replaceLen = strlen(sub->value);
146
147 char* ptr = entry->value;
148
149 while ((ptr = strstr(ptr, search)) != NULL)
150 {
151 size_t currentLen = strlen(entry->value);
152
154 {
155 break;
156 }
157
159 memcpy(ptr, sub->value, replaceLen);
160
161 ptr += replaceLen;
162 }
163 }
164 }
165 }
166}
167
168char* manifest_get_value(section_t* section, const char* key)
169{
170 for (uint64_t i = 0; i < section->amount; i++)
171 {
172 if (strcmp(section->entries[i].key, key) == 0)
173 {
174 return section->entries[i].value;
175 }
176 }
177 return NULL;
178}
179
181{
182 char* value = manifest_get_value(section, key);
183 if (value == NULL)
184 {
185 return ERR;
186 }
187
188 size_t len = strlen(value);
189 if (len == 0)
190 {
191 return ERR;
192 }
193
194 for (size_t i = 0; i < len; i++)
195 {
196 if (!isdigit(value[i]))
197 {
198 return ERR;
199 }
200 }
201
202 return atoll(value);
203}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
int64_t y
Definition main.c:153
#define isspace(c)
Definition ctype.h:30
#define isdigit(c)
Definition ctype.h:20
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t manifest_parse(manifest_t *manifest, const char *path)
Definition manifest.c:35
char * manifest_get_value(section_t *section, const char *key)
Definition manifest.c:168
void manifest_substitute(manifest_t *manifest, substitution_t *substitutions, uint64_t amount)
Definition manifest.c:124
#define MANIFEST_SECTION_MAX
Definition manifest.h:66
#define MANIFEST_STRING_MAX
Definition manifest.h:64
uint64_t manifest_get_integer(section_t *section, const char *key)
Definition manifest.c:180
@ SECTION_META
Definition manifest.h:82
@ SECTION_TYPE_MAX
Definition manifest.h:87
@ SECTION_EXEC
Definition manifest.h:83
@ SECTION_NAMESPACE
Definition manifest.h:86
@ SECTION_ENV
Definition manifest.h:85
@ SECTION_SANDBOX
Definition manifest.h:84
static char * trim_whitespace(char *str)
Definition manifest.c:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC char * fgets(char *_RESTRICT s, int n, FILE *_RESTRICT stream)
Definition fgets.c:5
_PUBLIC FILE * fopen(const char *_RESTRICT filename, const char *_RESTRICT mode)
Definition fopen.c:27
_PUBLIC int fclose(FILE *stream)
Definition fclose.c:7
_PUBLIC int snprintf(char *_RESTRICT s, size_t n, const char *_RESTRICT format,...)
Definition snprintf.c:3
_PUBLIC long long int atoll(const char *nptr)
Definition atoll.c:8
_PUBLIC void * memmove(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
_PUBLIC char * strncpy(char *_RESTRICT s1, const char *_RESTRICT s2, size_t n)
Definition strncpy.c:3
_PUBLIC void * memcpy(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
Definition memcpy.c:61
_PUBLIC char * strstr(const char *s1, const char *s2)
Definition strstr.c:3
_PUBLIC size_t strlen(const char *s)
Definition strlen.c:3
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
_PUBLIC void * memset(void *s, int c, size_t n)
Definition memset.c:4
_PUBLIC char * strchr(const char *s, int c)
Definition strchr.c:3
Definition file.h:34
Definition manifest.h:69
char value[MANIFEST_STRING_MAX]
Definition manifest.h:71