Blame


1 23a912c2 2022-01-20 op /*
2 23a912c2 2022-01-20 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 23a912c2 2022-01-20 op *
4 23a912c2 2022-01-20 op * Permission to use, copy, modify, and distribute this software for any
5 23a912c2 2022-01-20 op * purpose with or without fee is hereby granted, provided that the above
6 23a912c2 2022-01-20 op * copyright notice and this permission notice appear in all copies.
7 23a912c2 2022-01-20 op *
8 23a912c2 2022-01-20 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 23a912c2 2022-01-20 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 23a912c2 2022-01-20 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 23a912c2 2022-01-20 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 23a912c2 2022-01-20 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 23a912c2 2022-01-20 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 23a912c2 2022-01-20 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 23a912c2 2022-01-20 op */
16 23a912c2 2022-01-20 op
17 23a912c2 2022-01-20 op #include "compat.h"
18 23a912c2 2022-01-20 op
19 23a912c2 2022-01-20 op #include <stdio.h>
20 23a912c2 2022-01-20 op #include <string.h>
21 23a912c2 2022-01-20 op #include <unistd.h>
22 23a912c2 2022-01-20 op
23 23a912c2 2022-01-20 op #include "parser.h"
24 23a912c2 2022-01-20 op #include "telescope.h"
25 23a912c2 2022-01-20 op
26 23a912c2 2022-01-20 op void
27 23a912c2 2022-01-20 op erase_buffer(struct buffer *buffer)
28 23a912c2 2022-01-20 op {
29 23a912c2 2022-01-20 op return;
30 23a912c2 2022-01-20 op }
31 23a912c2 2022-01-20 op
32 23a912c2 2022-01-20 op int
33 23a912c2 2022-01-20 op main(void)
34 23a912c2 2022-01-20 op {
35 0180fcdd 2022-05-19 op FILE *fp;
36 23a912c2 2022-01-20 op struct tab tab;
37 23a912c2 2022-01-20 op struct hist hist;
38 23a912c2 2022-01-20 op ssize_t r;
39 0180fcdd 2022-05-19 op size_t blen;
40 0180fcdd 2022-05-19 op char buf[BUFSIZ], *b;
41 23a912c2 2022-01-20 op
42 23a912c2 2022-01-20 op memset(&tab, 0, sizeof(tab));
43 23a912c2 2022-01-20 op memset(&hist, 0, sizeof(hist));
44 23a912c2 2022-01-20 op tab.hist_cur = &hist;
45 23a912c2 2022-01-20 op
46 23a912c2 2022-01-20 op parser_init(&tab, gophermap_initparser);
47 23a912c2 2022-01-20 op for (;;) {
48 23a912c2 2022-01-20 op if ((r = read(0, buf, sizeof(buf))) == -1)
49 23a912c2 2022-01-20 op err(1, "read");
50 23a912c2 2022-01-20 op if (r == 0)
51 23a912c2 2022-01-20 op break;
52 23a912c2 2022-01-20 op if (!parser_parse(&tab, buf, r))
53 23a912c2 2022-01-20 op err(1, "parser_parse");
54 23a912c2 2022-01-20 op }
55 23a912c2 2022-01-20 op
56 23a912c2 2022-01-20 op if (!parser_free(&tab))
57 23a912c2 2022-01-20 op err(1, "parser_free");
58 23a912c2 2022-01-20 op
59 0180fcdd 2022-05-19 op if ((fp = open_memstream(&b, &blen)) == NULL)
60 0180fcdd 2022-05-19 op err(1, "open_memstream");
61 23a912c2 2022-01-20 op
62 0180fcdd 2022-05-19 op if (parser_serialize(&tab, fp) == -1)
63 23a912c2 2022-01-20 op err(1, "parser_serialize");
64 23a912c2 2022-01-20 op
65 0180fcdd 2022-05-19 op fclose(fp);
66 0180fcdd 2022-05-19 op write(1, b, blen);
67 23a912c2 2022-01-20 op
68 23a912c2 2022-01-20 op return 0;
69 23a912c2 2022-01-20 op }