aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 03:01:21 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 07:09:55 +0000
commitf3f55952a552eda8622b9a8c56449fa14c5720f8 (patch)
tree1ee13d1beaf8bdc50f90afce3146aee3f4c5bc14 /src/utils.c
parentfeat(hm): add hash map (diff)
downloadcodebase-f3f55952a552eda8622b9a8c56449fa14c5720f8.tar.gz
feat(utils): add various utilities
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c255
1 files changed, 255 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..f104cf8
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,255 @@
1// utils.c --- various utilities -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// Code:
12
13#define _GNU_SOURCE
14#include <stdlib.h>
15#include <errno.h>
16#include <string.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <sys/stat.h>
20#include <sys/mman.h>
21#include <sys/syscall.h>
22#include <stdio.h>
23#include <stdarg.h>
24#include <signal.h>
25#include <ctype.h>
26
27#include "utils.h"
28#include "dbg.h"
29#include "gpa.h"
30#include "lgr.h"
31#include "sb.h"
32
33struct ms
34load_file (const char *filename)
35{
36 struct ms ms = MS_NULL;
37
38 int fd = open (filename, O_RDONLY);
39 if (fd == -1) {
40 // LOG_ERROR ("open: %s: %s", strerror (errno), filename);
41 return ms;
42 }
43
44 struct stat st;
45 if (fstat (fd, &st)) {
46 CTX_ERROR ("fstat: %s: %s", strerror (errno), filename);
47 goto err_close;
48 }
49
50 ms.beg = CTX_GRAB (1, st.st_size);
51 ms.end = ms.beg + st.st_size;
52 if (read (fd, ms.beg, st.st_size) != (isz) st.st_size) {
53 CTX_ERROR ("read: %s: %s", strerror (errno), filename);
54 CTX_DROP (ms.beg, 1, st.st_size);
55 ms.beg = NULL;
56 }
57
58err_close:
59 close (fd);
60 return ms;
61}
62
63void *
64nmmap (usz sz, int n)
65{
66 DBG_ASSERT_GT (n*sz, sz, "%lu", "%lu");
67
68 int fd = memfd_create ("", MFD_CLOEXEC);
69 if (fd == -1) {
70 CTX_ERROR ("%s", strerror (errno));
71 return NULL;
72 }
73 ftruncate (fd, sz);
74
75 void *base = mmap (NULL, n*sz, PROT_NONE, (MAP_PRIVATE |
76 MAP_ANONYMOUS), -1, 0);
77
78 for (int i = 0; i < n; ++i) {
79 mmap (base + i*sz, sz,
80 (PROT_READ |
81 PROT_WRITE),
82 (MAP_SHARED |
83 MAP_FIXED), fd, 0);
84 }
85
86 close (fd);
87
88 return base;
89}
90
91// TODO(cmmm): color hexdump based on byte value
92void
93hexdump (FILE *out, const void *buf, usz sz)
94{
95 const u8 *ptr = buf;
96 const u8 *end = buf + sz;
97 const u8 *tail = end - (sz & 0xf);
98 while (ptr < tail) {
99 fprintf (out, "%p"
100 " %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx"
101 " %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx"
102 " |%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c|\n",
103 ptr,
104 ptr[0x0], ptr[0x1], ptr[0x2], ptr[0x3],
105 ptr[0x4], ptr[0x5], ptr[0x6], ptr[0x7],
106 ptr[0x8], ptr[0x9], ptr[0xa], ptr[0xb],
107 ptr[0xc], ptr[0xd], ptr[0xe], ptr[0xf],
108 isprint (ptr[0x0]) ? ptr[0x0] : '.',
109 isprint (ptr[0x1]) ? ptr[0x1] : '.',
110 isprint (ptr[0x2]) ? ptr[0x2] : '.',
111 isprint (ptr[0x3]) ? ptr[0x3] : '.',
112 isprint (ptr[0x4]) ? ptr[0x4] : '.',
113 isprint (ptr[0x5]) ? ptr[0x5] : '.',
114 isprint (ptr[0x6]) ? ptr[0x6] : '.',
115 isprint (ptr[0x7]) ? ptr[0x7] : '.',
116 isprint (ptr[0x8]) ? ptr[0x8] : '.',
117 isprint (ptr[0x9]) ? ptr[0x9] : '.',
118 isprint (ptr[0xa]) ? ptr[0xa] : '.',
119 isprint (ptr[0xb]) ? ptr[0xb] : '.',
120 isprint (ptr[0xc]) ? ptr[0xc] : '.',
121 isprint (ptr[0xd]) ? ptr[0xd] : '.',
122 isprint (ptr[0xe]) ? ptr[0xe] : '.',
123 isprint (ptr[0xf]) ? ptr[0xf] : '.');
124 ptr += 16;
125 }
126
127 if (tail == end)
128 return;
129
130 fprintf (out, "%p", tail);
131 for (usz i = 0; i < 16; ++i) {
132 if (i % 8 == 0) fprintf (out, " ");
133 if (i % 2 == 0) fprintf (out, " ");
134 if (i < (sz & 0xf))
135 fprintf (out, "%02hhx", tail[i]);
136 else
137 fprintf (out, " ");
138 }
139 fprintf (out, " |");
140 for (usz i = 0; i < (sz & 0xf); ++i) {
141 fprintf (out, "%c", isprint (tail[i]) ? tail[i] : '.');
142 }
143 fprintf (out, "|\n");
144}
145
146struct mb
147dot_svg (struct gpa gpa, struct mv dot, struct mv css)
148{
149 const char *failed = NULL;
150 struct sb out = { .hdr.gpa = gpa };
151
152 int dot_fds[2];
153 int svg_fds[2];
154 failed = "pipe";
155 if (pipe (dot_fds) == -1) goto error;
156 if (pipe (svg_fds) == -1) goto error_close_dot_fds;
157
158 pid_t child = fork ();
159 if (child == 0) {
160 close (dot_fds[STDOUT_FILENO]);
161 close (svg_fds[STDIN_FILENO ]);
162 failed = "dup";
163 if (dup2 (dot_fds[STDIN_FILENO ], STDIN_FILENO ) == -1) goto error_child;
164 if (dup2 (svg_fds[STDOUT_FILENO], STDOUT_FILENO) == -1) goto error_child;
165 execlp ("dot", "dot", "-Tsvg", NULL);
166
167error_child:
168 CTX_ERROR ("%s: %s", failed, strerror (errno));
169 exit (1);
170 }
171
172 close (dot_fds[STDIN_FILENO]);
173 close (svg_fds[STDOUT_FILENO]);
174
175 write (dot_fds[STDOUT_FILENO], dot.beg, mv_sz (dot));
176 close (dot_fds[STDOUT_FILENO]);
177
178 u8 buf[1 << 12];
179 struct iobuf iobuf = iobuf_from (ms_from_mb (MB (buf)));
180 struct mv line;
181 for (;;) {
182 line = ms_mv (sb_read_until (&out, svg_fds[STDIN_FILENO], '\n', &iobuf));
183 if (mv_is_null (line)) { failed = "read"; goto error_drop; }
184 if (mv_is_empty (line)) { failed = "dot generated corrupted file"; goto error_drop; }
185 if (mv_affixed (line, MV0 ("<svg "), MV_BEG))
186 break;
187 }
188
189 for (;;) {
190 if (mv_affixed (line, MV0 (">\n"), MV_END))
191 break;
192
193 line = ms_mv (sb_read_until (&out, svg_fds[STDIN_FILENO], '\n', &iobuf));
194 if (mv_is_null (line)) { failed = "read"; goto error_drop; }
195 if (mv_is_empty (line)) { failed = "dot generated corrupted file"; goto error_drop; }
196 }
197
198 sb_push_mv (&out, MV0 ("<style>\n"));
199 sb_push_mv (&out, css);
200 sb_push_mv (&out, MV0 ("</style>\n"));
201
202 sb_push_mv (&out, *iobuf_rmv (&iobuf));
203 if (ms_is_null (sb_read_until_eof (&out, svg_fds[STDIN_FILENO]))) {
204 failed = "read";
205 goto error_drop;
206 }
207
208 close (svg_fds[STDIN_FILENO]);
209
210 DA_SHRINK (&out);
211 return DA_MB (out);
212
213error_drop:
214 DA_DROP (&out);
215
216 close (svg_fds[STDIN_FILENO]);
217
218error_close_dot_fds:
219 close (dot_fds[STDIN_FILENO]);
220 close (dot_fds[STDOUT_FILENO]);
221
222error:
223 CTX_ERROR ("%s: %s", failed, strerror (errno));
224 return MB_NULL;
225}
226
227int
228mkdir_p (struct mv filename)
229{
230 struct mb path = mv_dup0 (filename);
231 struct mv dir = mv_from_mb (path);
232 int success = 0;
233
234again:
235 // DBG_TRACE ("info", "%s", dir.beg);
236 if (mkdir (dir.beg, 0777) == -1) {
237 switch (errno) {
238 case EEXIST: success = 1;
239 case ENOENT:
240 dir = mv_dirname (dir);
241 if (mv_sz (dir) <= 1) goto done;
242 *(char *) dir.end = '\0';
243 goto again;
244
245 default:
246 CTX_ERROR ("mkdir: %s: %s", (char *) dir.beg, strerror (errno));
247 }
248 } else success = 1;
249
250done:
251 CTX_DROP_N (path.ptr, path.sz);
252 return success;
253}
254
255// utils.c ends here