all repos — nimisewi_c @ 00981834d98546471e30441979a36dac52a76ecd

simple random toki pona phrase generator

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 <config.h>
 25#include <err.h>
 26#include <stdio.h>
 27#include <stdlib.h>
 28#include <unistd.h>
 29
 30#include "nimisewi.h"
 31
 32static void print_response_header(void);
 33static void print_html_header(void);
 34static void print_html_footer(void);
 35static void print_html_stylesheet(void);
 36
 37static void
 38print_response_header(void)
 39{
 40    printf("Status: 200 OK\r");
 41    printf("Content-Type: text/html\r\r");
 42}
 43
 44static void
 45print_html_header(void)
 46{
 47    printf("<!doctype html>\n");
 48    printf("<html>\n");
 49    printf("<head>\n");
 50    printf("<title>nimi sewi</title>\n");
 51    printf("<meta charset=\"utf-8\">\n");
 52    printf("<meta name=\"generator\" content=\"%s\">\n", PACKAGE_STRING);
 53    printf("<link rel=\"icon\" href=\"/assets/img/favicon.ico\" type=\"image/x-icon\">\n");
 54    print_html_stylesheet();
 55    printf("</head>\n");
 56    printf("<body>\n");
 57}
 58
 59static void
 60print_html_stylesheet(void)
 61{
 62    printf("<style>\n");
 63    printf("body { background: #ff8; }\n");
 64    printf("h1 { text-align: center; font: bold 48pt monospace; color: #4ad }\n");
 65    printf("</style>\n");
 66}
 67
 68static void
 69print_html_footer(void)
 70{
 71    printf("</body>\n");
 72    printf("</html>\n");
 73}
 74
 75int 
 76main(void)
 77{
 78#ifdef HAVE_PLEDGE
 79    if (pledge("stdio", NULL) == -1) {
 80        err(EXIT_FAILURE, "pledge");
 81    }
 82#endif
 83
 84    char *ns;
 85    ns = nimi_sewi();
 86    if (ns == NULL) {
 87        err(EXIT_FAILURE, "memory allocation failed");
 88    }
 89
 90    print_response_header();
 91    print_html_header();
 92
 93    printf("<h1>");
 94    printf("%s", ns);
 95    printf("</h1>\n");
 96
 97    weka_e_nimi_sewi(ns);
 98
 99    print_html_footer();
100
101    return EXIT_SUCCESS;
102}