// utils.c --- various utilities -*- C -*- // // Copyright (c) 2026 Martin Michalec // // SPDX-License-Identifier: MIT // // This file is part of COSMOS Codebase. // // Commentary: // // Code: #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include "utils.h" #include "dbg.h" #include "gpa.h" #include "lgr.h" #include "sb.h" struct ms load_file (const char *filename) { struct ms ms = MS_NULL; int fd = open (filename, O_RDONLY); if (fd == -1) { // LOG_ERROR ("open: %s: %s", strerror (errno), filename); return ms; } struct stat st; if (fstat (fd, &st)) { CTX_ERROR ("fstat: %s: %s", strerror (errno), filename); goto err_close; } ms.beg = CTX_GRAB (1, st.st_size); ms.end = ms.beg + st.st_size; if (read (fd, ms.beg, st.st_size) != (isz) st.st_size) { CTX_ERROR ("read: %s: %s", strerror (errno), filename); CTX_DROP (ms.beg, 1, st.st_size); ms.beg = NULL; } err_close: close (fd); return ms; } void * nmmap (usz sz, int n) { DBG_ASSERT_GT (n*sz, sz, "%lu", "%lu"); int fd = memfd_create ("", MFD_CLOEXEC); if (fd == -1) { CTX_ERROR ("%s", strerror (errno)); return NULL; } ftruncate (fd, sz); void *base = mmap (NULL, n*sz, PROT_NONE, (MAP_PRIVATE | MAP_ANONYMOUS), -1, 0); for (int i = 0; i < n; ++i) { mmap (base + i*sz, sz, (PROT_READ | PROT_WRITE), (MAP_SHARED | MAP_FIXED), fd, 0); } close (fd); return base; } // TODO(cmmm): color hexdump based on byte value void hexdump (FILE *out, const void *buf, usz sz) { const u8 *ptr = buf; const u8 *end = buf + sz; const u8 *tail = end - (sz & 0xf); while (ptr < tail) { fprintf (out, "%p" " %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx" " %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx %02hhx%02hhx" " |%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c|\n", ptr, ptr[0x0], ptr[0x1], ptr[0x2], ptr[0x3], ptr[0x4], ptr[0x5], ptr[0x6], ptr[0x7], ptr[0x8], ptr[0x9], ptr[0xa], ptr[0xb], ptr[0xc], ptr[0xd], ptr[0xe], ptr[0xf], isprint (ptr[0x0]) ? ptr[0x0] : '.', isprint (ptr[0x1]) ? ptr[0x1] : '.', isprint (ptr[0x2]) ? ptr[0x2] : '.', isprint (ptr[0x3]) ? ptr[0x3] : '.', isprint (ptr[0x4]) ? ptr[0x4] : '.', isprint (ptr[0x5]) ? ptr[0x5] : '.', isprint (ptr[0x6]) ? ptr[0x6] : '.', isprint (ptr[0x7]) ? ptr[0x7] : '.', isprint (ptr[0x8]) ? ptr[0x8] : '.', isprint (ptr[0x9]) ? ptr[0x9] : '.', isprint (ptr[0xa]) ? ptr[0xa] : '.', isprint (ptr[0xb]) ? ptr[0xb] : '.', isprint (ptr[0xc]) ? ptr[0xc] : '.', isprint (ptr[0xd]) ? ptr[0xd] : '.', isprint (ptr[0xe]) ? ptr[0xe] : '.', isprint (ptr[0xf]) ? ptr[0xf] : '.'); ptr += 16; } if (tail == end) return; fprintf (out, "%p", tail); for (usz i = 0; i < 16; ++i) { if (i % 8 == 0) fprintf (out, " "); if (i % 2 == 0) fprintf (out, " "); if (i < (sz & 0xf)) fprintf (out, "%02hhx", tail[i]); else fprintf (out, " "); } fprintf (out, " |"); for (usz i = 0; i < (sz & 0xf); ++i) { fprintf (out, "%c", isprint (tail[i]) ? tail[i] : '.'); } fprintf (out, "|\n"); } struct mb dot_svg (struct gpa gpa, struct mv dot, struct mv css) { const char *failed = NULL; struct sb out = { .hdr.gpa = gpa }; int dot_fds[2]; int svg_fds[2]; failed = "pipe"; if (pipe (dot_fds) == -1) goto error; if (pipe (svg_fds) == -1) goto error_close_dot_fds; pid_t child = fork (); if (child == 0) { close (dot_fds[STDOUT_FILENO]); close (svg_fds[STDIN_FILENO ]); failed = "dup"; if (dup2 (dot_fds[STDIN_FILENO ], STDIN_FILENO ) == -1) goto error_child; if (dup2 (svg_fds[STDOUT_FILENO], STDOUT_FILENO) == -1) goto error_child; execlp ("dot", "dot", "-Tsvg", NULL); error_child: CTX_ERROR ("%s: %s", failed, strerror (errno)); exit (1); } close (dot_fds[STDIN_FILENO]); close (svg_fds[STDOUT_FILENO]); write (dot_fds[STDOUT_FILENO], dot.beg, mv_sz (dot)); close (dot_fds[STDOUT_FILENO]); u8 buf[1 << 12]; struct iobuf iobuf = iobuf_from (ms_from_mb (MB (buf))); struct mv line; for (;;) { line = ms_mv (sb_read_until (&out, svg_fds[STDIN_FILENO], '\n', &iobuf)); if (mv_is_null (line)) { failed = "read"; goto error_drop; } if (mv_is_empty (line)) { failed = "dot generated corrupted file"; goto error_drop; } if (mv_affixed (line, MV0 ("\n"), MV_END)) break; line = ms_mv (sb_read_until (&out, svg_fds[STDIN_FILENO], '\n', &iobuf)); if (mv_is_null (line)) { failed = "read"; goto error_drop; } if (mv_is_empty (line)) { failed = "dot generated corrupted file"; goto error_drop; } } sb_push_mv (&out, MV0 ("\n")); sb_push_mv (&out, *iobuf_rmv (&iobuf)); if (ms_is_null (sb_read_until_eof (&out, svg_fds[STDIN_FILENO]))) { failed = "read"; goto error_drop; } close (svg_fds[STDIN_FILENO]); DA_SHRINK (&out); return DA_MB (out); error_drop: DA_DROP (&out); close (svg_fds[STDIN_FILENO]); error_close_dot_fds: close (dot_fds[STDIN_FILENO]); close (dot_fds[STDOUT_FILENO]); error: CTX_ERROR ("%s: %s", failed, strerror (errno)); return MB_NULL; } int mkdir_p (struct mv filename) { struct mb path = mv_dup0 (filename); struct mv dir = mv_from_mb (path); int success = 0; again: // DBG_TRACE ("info", "%s", dir.beg); if (mkdir (dir.beg, 0777) == -1) { switch (errno) { case EEXIST: success = 1; case ENOENT: dir = mv_dirname (dir); if (mv_sz (dir) <= 1) goto done; *(char *) dir.end = '\0'; goto again; default: CTX_ERROR ("mkdir: %s: %s", (char *) dir.beg, strerror (errno)); } } else success = 1; done: CTX_DROP_N (path.ptr, path.sz); return success; } // utils.c ends here