2 bff58270 2022-09-19 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 bff58270 2022-09-19 op * Permission to use, copy, modify, and distribute this software for any
5 bff58270 2022-09-19 op * purpose with or without fee is hereby granted, provided that the above
6 bff58270 2022-09-19 op * copyright notice and this permission notice appear in all copies.
8 bff58270 2022-09-19 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 bff58270 2022-09-19 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 bff58270 2022-09-19 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 bff58270 2022-09-19 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 bff58270 2022-09-19 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 bff58270 2022-09-19 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 bff58270 2022-09-19 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bff58270 2022-09-19 op #include <err.h>
18 bff58270 2022-09-19 op #include <stdio.h>
19 bff58270 2022-09-19 op #include <stdlib.h>
20 bff58270 2022-09-19 op #include <unistd.h>
22 14c31a43 2022-11-25 op int parse(FILE *, const char *);
26 bff58270 2022-09-19 op static void __dead
29 bff58270 2022-09-19 op fprintf(stderr, "usage: %s [file...]\n",
30 bff58270 2022-09-19 op getprogname());
35 bff58270 2022-09-19 op main(int argc, char **argv)
37 14c31a43 2022-11-25 op FILE *fp = stdout;
38 14c31a43 2022-11-25 op const char *out = NULL;
41 14c31a43 2022-11-25 op while ((ch = getopt(argc, argv, "Go:")) != -1) {
53 bff58270 2022-09-19 op argc -= optind;
54 bff58270 2022-09-19 op argv += optind;
56 14c31a43 2022-11-25 op if (out && (fp = fopen(out, "w")) == NULL)
57 14c31a43 2022-11-25 op err(1, "can't open %s", out);
59 14c31a43 2022-11-25 op if (out && unveil(out, "wc") == -1)
60 14c31a43 2022-11-25 op err(1, "unveil %s", out);
61 14c31a43 2022-11-25 op if (unveil("/", "r") == -1)
62 14c31a43 2022-11-25 op err(1, "unveil /");
63 14c31a43 2022-11-25 op if (pledge(out ? "stdio rpath cpath" : "stdio rpath", NULL) == -1)
64 14c31a43 2022-11-25 op err(1, "pledge");
66 bff58270 2022-09-19 op /* preamble */
67 14c31a43 2022-11-25 op fprintf(fp, "#include \"tmpl.h\"\n");
69 bff58270 2022-09-19 op if (argc == 0) {
71 14c31a43 2022-11-25 op if (parse(fp, "/dev/stdin") == -1)
74 14c31a43 2022-11-25 op for (i = 0; i < argc; ++i)
75 14c31a43 2022-11-25 op if (parse(fp, argv[i]) == -1)
79 14c31a43 2022-11-25 op if (ferror(fp))
82 14c31a43 2022-11-25 op if (fclose(fp) == -1) {
92 14c31a43 2022-11-25 op if (out && unlink(out) == -1)
93 14c31a43 2022-11-25 op err(1, "unlink %s", out);