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 print_html_stylesheet();
47 printf("</head>\n");
48 printf("<body>\n");
49}
50
51void
52print_html_stylesheet(void)
53{
54 printf("<style>\n");
55 printf("body { background: #ff8; }\n");
56 printf("h1 { text-align: center; font: bold 48pt monospace; color: #4ad }\n");
57 printf("</style>\n");
58}
59
60void
61print_html_footer(void)
62{
63 printf("</body>\n");
64 printf("</html>\n");
65}
66
67int
68main(void)
69{
70#ifdef __OpenBSD__
71 if (pledge("stdio", NULL) == -1) {
72 err(EXIT_FAILURE, "pledge");
73 }
74#endif
75
76 char *ns;
77 ns = nimi_sewi();
78 if (ns == NULL) {
79 err(EXIT_FAILURE, "memory could'n be allocated");
80 }
81
82 print_response_header();
83 print_html_header();
84
85 printf("<h1>");
86 printf("%s", ns);
87 printf("</h1>\n");
88
89 weka_e_nimi_sewi(ns);
90
91 print_html_footer();
92
93 return EXIT_SUCCESS;
94}