cgi.c (view raw)
1/* Copyright (c) 2021, la-ninpre
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 *
15 * *************************************************************************
16 *
17 * this file contains code for cgi version of nimisewi.
18 *
19 * i know that there are libraries for cgi in c, but c'mon, it's so small and
20 * simple.
21 * stylesheet is arbitrary, replace with anything you want if you don't like it.
22 */
23
24#include <err.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28
29#include "cgi.h"
30
31#include "nimisewi.h"
32
33void
34print_response_header(void)
35{
36 printf("Status: 200 OK\r");
37 printf("Content-Type: text/html\r\r");
38}
39
40void
41print_html_header(void)
42{
43 printf("<!doctype html>\n");
44 printf("<html>\n");
45 printf("<head>\n");
46 printf("<title>nimi sewi</title>\n");
47 printf("<link rel=\"icon\" href=\"/assets/img/favicon.ico\" type=\"image/x-icon\">\n");
48 print_html_stylesheet();
49 printf("</head>\n");
50 printf("<body>\n");
51}
52
53void
54print_html_stylesheet(void)
55{
56 printf("<style>\n");
57 printf("body { background: #ff8; }\n");
58 printf("h1 { text-align: center; font: bold 48pt monospace; color: #4ad }\n");
59 printf("</style>\n");
60}
61
62void
63print_html_footer(void)
64{
65 printf("</body>\n");
66 printf("</html>\n");
67}
68
69int
70main(void)
71{
72#ifdef __OpenBSD__
73 if (pledge("stdio", NULL) == -1) {
74 err(EXIT_FAILURE, "pledge");
75 }
76#endif
77
78 char *ns;
79 ns = nimi_sewi();
80 if (ns == NULL) {
81 err(EXIT_FAILURE, "memory could'n be allocated");
82 }
83
84 print_response_header();
85 print_html_header();
86
87 printf("<h1>");
88 printf("%s", ns);
89 printf("</h1>\n");
90
91 weka_e_nimi_sewi(ns);
92
93 print_html_footer();
94
95 return EXIT_SUCCESS;
96}