/* ===========================================================================
* Cool - Server-side Components in C - Copyright (c) 2026 Vasco Alves
*
* _.-\ _.-\
* __.-'' \ __.-'' \
* -----------------------------------'
* \ X X / \ X X /
* \ X X / \ X X /
* \ X X / \ X X /
* \---------/ \---------/
*
* "Hasta la vista, baby."
*
* ---------------------------------------------------------------------------
*
* PREFIX: COOL (macros) Cool_ (types) cool (function)
*
* USAGE:
* Can be used directly for very simple applications or through a transpiler
* that converts a separate "higher level" template language directly into
* C functions!
*
* --- view.cool ---
* COOL void GenDocs(char *name, char *func, char *desc) {
*
*
{ name }
*
{ func }
*
* { desc }
*
*
* }
*
* --- main.c ---
* #include "view.cool.c"
* int main(int argc, char **argv) {
*
* if (argc < 2) {
* puts("Usage: doc_generator [FILE]");
* return 1;
* }
*
* FILE *header_file = fopen(argv[1], "r");
*
* char *line_buf = malloc(256);
* size_t len = 0;
* while (getline(&line_buf, &len, header_file) > 0) {
* char func_name[256];
* char func_decl[256];
* char *comment_text = NULL;
*
* if (parse_header_line(line_buf, func_name, func_decl, &comment_text)) {
* GenDocs(func_name, func_decl, comment_text);
* }
* }
*
* free(line_buf);
*
* return 0;
* }
*
* =========================================================================== */
#ifndef _COOL_H_
#define _COOL_H_
#define COOL_MAJOR 0 // breaking API changes
#define COOL_MINOR 0 // non-breaking features
#define COOL_PATCH 0 // non-breaking patches and bug fixes
/* CHANGE LOG
* 0.0.0 - @vasco - server-side HTML from C functions
*/
#define COOLDEF static inline
#define COOL_OUTPUT stdout
#include
#include
#include
#include
#include
typedef struct {
char *cstr; // original cstring
size_t len; // len
} Cool_StrView;
/* Macro to wrap string literal in a string view. Since we will be dealing with a lot of */
#define COOL_SV(literal) ((Cool_StrView){ .cstr = (literal), .len = sizeof(literal) - 1 })
COOLDEF void cool_sv_write(Cool_StrView sv); // Write string view to output.
COOLDEF void cool_html(Cool_StrView sv); // SAFELY write HTML to a buffer. Handles escaping malicious sequences.
COOLDEF void cool_html_raw(Cool_StrView sv); // Writes HTML to buffer without escaping sequences. Vulnerable to XSS. Use with caution.
COOLDEF void cool_html_raw_cstr(const char *str, size_t len); // Writes unsafe HTML using C strings.
COOLDEF void cool_html_txt(const char *str, size_t len); // Meant for inner text. Escapes '<', '>' and '&'.
COOLDEF void cool_htmlf(const char *fmt, ...); // SAFELY write formatted string to buffer.
COOLDEF void cool_htmlf_raw(const char *fmt, ...); // Writes formatting HTML string to buffer without escaping sequences. Vulnerable to XSS. Use with caution.
#endif
#define COOL_IMPLEMENTATION
#ifdef COOL_IMPLEMENTATION
#undef COOL_IMPLEMENTATION
static const char* cool_url_escape_table[256] = {
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
"%20", NULL, "%22", "%23", NULL, "%25", "%26", "%27",
NULL, NULL, NULL, "%2b", NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, "%3c", "%3d", "%3e", "%3f",
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, "%5c", NULL, "%5e", NULL,
"%60", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, "%7b", "%7c", "%7d", NULL, "%7f",
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
};
COOLDEF void
cool_sv_write(Cool_StrView sv)
{
fwrite(sv.cstr, sizeof *sv.cstr, sv.len, COOL_OUTPUT);
}
COOLDEF void
cool_html(Cool_StrView sv)
{
assert(0 && "TODO");
}
COOLDEF void
cool_html_raw(Cool_StrView sv)
{
fwrite(sv.cstr, sizeof *sv.cstr, sv.len, COOL_OUTPUT);
}
COOLDEF void
cool_html_raw_cstr(const char *str, size_t len)
{
fwrite(str, sizeof *str, len, COOL_OUTPUT);
}
COOLDEF void
cool_html_txt(const char *str, size_t len)
{
const char *s = str;
while (s && *s && len--) {
int c = *str;
if (c == '<' || c == '>' || c == '&') {
cool_html_raw_cstr(str, s - str);
switch (c) {
case '>':
cool_html_raw(COOL_SV(">"));
break;
case '<':
cool_html_raw(COOL_SV("<"));
break;
case '&':
cool_html_raw(COOL_SV("&"));
break;
}
str = s + 1;
}
s++;
}
if (s != str) cool_html_raw_cstr(str, s - str);
}
COOLDEF void
cool_htmlf(const char *fmt, ...)
{
assert(0 && "TODO");
}
COOLDEF void
cool_htmlf_raw(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(COOL_OUTPUT, fmt, args);
va_end(args);
}
#endif
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2026 Vasco Alves
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/