scq/scq.c

823 lines
17 KiB
C
Raw Permalink Normal View History

// Copyright (C) 2025 Enno Tensing <tenno+scq@suij.in>
2025-01-21 20:47:30 +01:00
// Copyright (C) <2007, 2008> Enno Boland <g s01 de>
// SPDX-FileCopyrightText: (C) 2007 - 2014 Enno Boland <g s01 de>
// SPDX-FileCopyrightText: (C) 2025 Enno Tensing <tenno+scq@suij.in>
2025-01-21 20:47:30 +01:00
// SPDX-License-Identifier: MIT
2025-01-21 19:51:56 +01:00
#define _LARGEFILE64_SOURCE
2008-06-28 21:06:02 +02:00
#include <stdarg.h>
#include <stdio.h>
2007-12-10 02:35:44 +01:00
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
2025-01-21 19:51:56 +01:00
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
2007-12-10 02:35:44 +01:00
2025-01-21 20:17:09 +01:00
#ifndef PACKAGE
#define PACKAGE "scq"
2025-01-21 20:17:09 +01:00
#endif
#ifndef VERSION
#define VERSION "0.0"
#endif
2025-01-21 20:12:16 +01:00
#define CHARWIDTH 4
2025-01-20 18:49:00 +01:00
#define LENGTH(x) sizeof(x) / sizeof(x[0])
2025-01-21 19:51:56 +01:00
#define ADDC(b, i, a) \
do { \
if (i % BUFSIZ == 0) { \
b = realloc(b, (i + BUFSIZ) * sizeof(char)); \
if (!b) { \
eprint("Malloc failed."); \
return -1; \
} \
} \
b[i] = a; \
} while (0)
2008-06-28 21:06:02 +02:00
2025-01-21 21:07:50 +01:00
typedef int (*parser)(const char *, const char *, int);
2025-01-21 19:51:56 +01:00
struct tag {
2008-06-28 21:06:02 +02:00
char *search;
int process;
2025-01-21 19:51:56 +01:00
char *before;
char *after;
};
off64_t get_file_size(const char *);
char *read_file(const char *, off64_t);
2025-01-21 21:07:50 +01:00
static int doamp(const char *, const char *, int);
static int docomment(const char *, const char *, int);
static int dogtlt(const char *, const char *, int);
static int dohtml(const char *, const char *, int);
static int dolineprefix(const char *, const char *, int);
static int dolink(const char *, const char *, int);
static int dolist(const char *, const char *, int);
static int doparagraph(const char *, const char *, int);
static int doreplace(const char *, const char *, int);
static int doshortlink(const char *, const char *, int);
static int dosurround(const char *, const char *, int);
static int dounderline(const char *, const char *, int);
static void *ereallocz(void *, size_t);
static void eprint(const char *, ...);
static void hprint(const char *, const char *);
static int process(const char *, const char *, int);
2008-06-28 21:06:02 +02:00
/* list of parsers */
2025-01-21 21:07:50 +01:00
static parser parsers[] = { dounderline, docomment, dolineprefix, dolist,
2025-01-20 18:49:00 +01:00
doparagraph, dogtlt, dosurround, dolink,
doshortlink, dohtml, doamp, doreplace };
static int nohtml = 0;
2008-06-28 21:06:02 +02:00
2025-01-21 19:51:56 +01:00
static struct tag lineprefix[] = {
2025-01-20 18:49:00 +01:00
{ " ", 0, "<pre><code>", "\n</code></pre>" },
{ "\t", 0, "<pre><code>", "\n</code></pre>" },
{ ">", 2, "<blockquote>", "</blockquote>" },
{ "###### ", 1, "<h6>", "</h6>" },
{ "##### ", 1, "<h5>", "</h5>" },
{ "#### ", 1, "<h4>", "</h4>" },
{ "### ", 1, "<h3>", "</h3>" },
{ "## ", 1, "<h2>", "</h2>" },
{ "# ", 1, "<h1>", "</h1>" },
{ "- - -\n", 1, "<hr />", "" },
2008-06-28 21:06:02 +02:00
};
2025-01-21 19:51:56 +01:00
static struct tag underline[] = {
2025-01-20 18:49:00 +01:00
{ "=", 1, "<h1>", "</h1>\n" },
{ "-", 1, "<h2>", "</h2>\n" },
2008-06-28 21:06:02 +02:00
};
2025-01-21 19:51:56 +01:00
static struct tag surround[] = {
{ "```", 0, "<code>", "</code>" },
2025-01-20 18:49:00 +01:00
{ "``", 0, "<code>", "</code>" },
{ "`", 0, "<code>", "</code>" },
{ "___", 1, "<strong><em>", "</em></strong>" },
{ "***", 1, "<strong><em>", "</em></strong>" },
{ "__", 1, "<strong>", "</strong>" },
{ "**", 1, "<strong>", "</strong>" },
{ "_", 1, "<em>", "</em>" },
{ "*", 1, "<em>", "</em>" },
2008-06-28 21:06:02 +02:00
};
static const char *replace[][2] = {
2025-01-20 18:49:00 +01:00
{ "\\\\", "\\" }, { "\\`", "`" }, { "\\*", "*" }, { "\\_", "_" },
{ "\\{", "{" }, { "\\}", "}" }, { "\\[", "[" }, { "\\]", "]" },
{ "\\(", "(" }, { "\\)", ")" }, { "\\#", "#" }, { "\\+", "+" },
{ "\\-", "-" }, { "\\.", "." }, { "\\!", "!" },
2008-06-28 21:06:02 +02:00
};
static const char *insert[][2] = {
2025-01-20 18:49:00 +01:00
{ " \n", "<br />" },
2008-06-28 21:06:02 +02:00
};
2025-01-21 19:51:56 +01:00
off64_t get_file_size(const char *path)
{
struct stat st;
if (stat(path, &st) == 0)
return st.st_size;
return -1;
}
char *read_file(const char *path, off64_t file_size)
{
int fd = open(path, O_LARGEFILE | O_NONBLOCK);
ssize_t bytes;
2025-01-21 20:12:16 +01:00
char *buf = calloc(file_size + CHARWIDTH, sizeof(char));
2025-01-21 19:51:56 +01:00
if (!buf) {
2025-01-21 20:17:09 +01:00
perror(PACKAGE);
2025-01-21 19:51:56 +01:00
close(fd);
return NULL;
}
bytes = read(fd, buf, file_size);
if (bytes != file_size) {
2025-01-21 20:17:09 +01:00
perror(PACKAGE);
2025-01-21 19:51:56 +01:00
close(fd);
free(buf);
return NULL;
}
close(fd);
return buf;
}
2025-01-20 18:49:00 +01:00
void eprint(const char *format, ...)
{
2008-06-28 21:06:02 +02:00
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
2025-01-20 18:49:00 +01:00
int doamp(const char *begin, const char *end, int newblock)
{
2008-06-28 21:06:02 +02:00
const char *p;
2025-01-20 18:49:00 +01:00
if (*begin != '&')
2008-06-28 21:06:02 +02:00
return 0;
2025-01-20 18:49:00 +01:00
if (!nohtml) {
for (p = begin + 1; p != end && !strchr("; \\\n\t", *p); p++)
;
if (p == end || *p == ';')
2008-06-28 21:06:02 +02:00
return 0;
}
fputs("&amp;", stdout);
2008-06-28 21:06:02 +02:00
return 1;
}
2025-01-20 18:49:00 +01:00
int dogtlt(const char *begin, const char *end, int newblock)
{
2008-06-28 21:06:02 +02:00
int brpos;
char c;
2025-01-20 18:49:00 +01:00
if (nohtml || begin + 1 >= end)
2008-06-28 21:06:02 +02:00
return 0;
brpos = begin[1] == '>';
2025-01-20 18:49:00 +01:00
if (!brpos && *begin != '<')
2008-06-28 21:06:02 +02:00
return 0;
c = begin[brpos ? 0 : 1];
2025-01-20 18:49:00 +01:00
if (!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) {
fputs("&lt;", stdout);
2008-06-28 21:06:02 +02:00
return 1;
2025-01-20 18:49:00 +01:00
} else if (brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') &&
!strchr("/\"'", c)) {
fprintf(stdout, "%c&gt;", c);
2008-06-28 21:06:02 +02:00
return 2;
}
return 0;
}
2025-01-20 18:49:00 +01:00
int docomment(const char *begin, const char *end, int newblock)
{
char *p;
2025-01-20 18:49:00 +01:00
if (nohtml || strncmp("<!--", begin, 4))
return 0;
p = strstr(begin, "-->");
2025-01-20 18:49:00 +01:00
if (!p || p + 3 >= end)
return 0;
fprintf(stdout, "%.*s\n", (int)(p + 3 - begin), begin);
return (p + 3 - begin) * (newblock ? -1 : 1);
}
2025-01-20 18:49:00 +01:00
int dohtml(const char *begin, const char *end, int newblock)
{
2025-01-21 21:07:50 +01:00
const char *p;
const char *tag;
const char *tagend;
2008-06-28 21:06:02 +02:00
2025-01-20 18:49:00 +01:00
if (nohtml || begin + 2 >= end)
2008-06-28 21:06:02 +02:00
return 0;
p = begin;
2025-01-20 18:49:00 +01:00
if (p[0] != '<' || !isalpha(p[1]))
return 0;
p++;
tag = p;
2025-01-20 18:49:00 +01:00
for (; isalnum(*p) && p < end; p++)
;
tagend = p;
2025-01-20 18:49:00 +01:00
if (p > end || tag == tagend)
2008-06-28 21:06:02 +02:00
return 0;
2025-01-20 18:49:00 +01:00
while ((p = strstr(p, "</")) && p < end) {
p += 2;
2025-01-20 18:49:00 +01:00
if (strncmp(p, tag, tagend - tag) == 0 &&
p[tagend - tag] == '>') {
2008-06-28 21:06:02 +02:00
p++;
2025-01-20 18:49:00 +01:00
fwrite(begin, sizeof(char),
p - begin + tagend - tag - 1, stdout);
2021-10-14 22:23:34 -04:00
return p - begin + tagend - tag - 1;
2008-06-28 21:06:02 +02:00
}
}
p = strchr(tagend, '>');
2025-01-20 18:49:00 +01:00
if (p) {
2021-10-14 22:23:34 -04:00
fwrite(begin, sizeof(char), p - begin + 1, stdout);
return p - begin + 1;
2025-01-20 18:49:00 +01:00
} else
return 0;
2008-06-28 21:06:02 +02:00
}
2025-01-20 18:49:00 +01:00
int dolineprefix(const char *begin, const char *end, int newblock)
{
2008-06-28 21:06:02 +02:00
unsigned int i, j, l;
char *buffer;
const char *p;
2025-01-20 18:49:00 +01:00
if (newblock)
2008-06-28 21:06:02 +02:00
p = begin;
2025-01-20 18:49:00 +01:00
else if (*begin == '\n')
2008-06-28 21:06:02 +02:00
p = begin + 1;
else
return 0;
2025-01-20 18:49:00 +01:00
for (i = 0; i < LENGTH(lineprefix); i++) {
2008-06-28 21:06:02 +02:00
l = strlen(lineprefix[i].search);
2025-01-20 18:49:00 +01:00
if (end - p < l)
2008-06-28 21:06:02 +02:00
continue;
2025-01-20 18:49:00 +01:00
if (strncmp(lineprefix[i].search, p, l))
2008-06-28 21:06:02 +02:00
continue;
2025-01-20 18:49:00 +01:00
if (*begin == '\n')
fputc('\n', stdout);
fputs(lineprefix[i].before, stdout);
2025-01-20 18:49:00 +01:00
if (lineprefix[i].search[l - 1] == '\n') {
fputc('\n', stdout);
return l - 1;
2008-06-28 21:06:02 +02:00
}
2025-01-20 18:49:00 +01:00
if (!(buffer = malloc(BUFSIZ)))
2008-06-28 21:06:02 +02:00
eprint("Malloc failed.");
buffer[0] = '\0';
/* Collect lines into buffer while they start with the prefix */
j = 0;
2025-01-20 18:49:00 +01:00
while ((strncmp(lineprefix[i].search, p, l) == 0) &&
p + l < end) {
p += l;
/* Special case for blockquotes: optional space after > */
2025-01-20 18:49:00 +01:00
if (lineprefix[i].search[0] == '>' && *p == ' ') {
p++;
}
2025-01-20 18:49:00 +01:00
while (p < end) {
2025-01-21 19:51:56 +01:00
ADDC(buffer, j, *p);
j++;
2025-01-20 18:49:00 +01:00
if (*(p++) == '\n')
2008-06-28 21:06:02 +02:00
break;
}
}
/* Skip empty lines in block */
2025-01-20 18:49:00 +01:00
while (buffer + j - 1 >= buffer && *(buffer + j - 1) == '\n') {
j--;
}
2025-01-21 19:51:56 +01:00
ADDC(buffer, j, '\0');
2025-01-20 18:49:00 +01:00
if (lineprefix[i].process)
process(buffer, buffer + strlen(buffer),
lineprefix[i].process >= 2);
2008-06-28 21:06:02 +02:00
else
hprint(buffer, buffer + strlen(buffer));
puts(lineprefix[i].after);
free(buffer);
return -(p - begin);
}
return 0;
}
2025-01-20 18:49:00 +01:00
int dolink(const char *begin, const char *end, int newblock)
{
2025-01-21 21:07:50 +01:00
int img;
int len;
int sep;
int parens_depth = 1;
const char *desc;
const char *link;
const char *p;
const char *q;
const char *descend;
const char *linkend;
const char *title = NULL;
const char *titleend = NULL;
2008-06-28 21:06:02 +02:00
2025-01-20 18:49:00 +01:00
if (*begin == '[')
2008-06-28 21:06:02 +02:00
img = 0;
2025-01-20 18:49:00 +01:00
else if (strncmp(begin, "![", 2) == 0)
2008-06-28 21:06:02 +02:00
img = 1;
else
return 0;
p = desc = begin + 1 + img;
2025-01-20 18:49:00 +01:00
if (!(p = strstr(desc, "](")) || p > end)
2008-06-28 21:06:02 +02:00
return 0;
2025-01-20 18:49:00 +01:00
for (q = strstr(desc, "!["); q && q < end && q < p;
q = strstr(q + 1, "!["))
if (!(p = strstr(p + 1, "](")) || p > end)
2008-06-28 21:06:02 +02:00
return 0;
descend = p;
link = p + 2;
/* find end of link while handling nested parens */
q = link;
2025-01-20 18:49:00 +01:00
while (parens_depth) {
if (!(q = strpbrk(q, "()")) || q > end)
return 0;
2025-01-20 18:49:00 +01:00
if (*q == '(')
parens_depth++;
else
parens_depth--;
2025-01-20 18:49:00 +01:00
if (parens_depth && q < end)
q++;
}
2025-01-20 18:49:00 +01:00
if ((p = strpbrk(link, "\"'")) && p < end && q - 1 > p + 1) {
sep = p[0]; /* separator: can be " or ' */
title = p + 1;
/* strip trailing whitespace */
2025-01-20 18:49:00 +01:00
for (linkend = p; linkend > link && isspace(*(linkend - 1));
linkend--)
;
for (titleend = q - 1; titleend > title && isspace(*(titleend));
titleend--)
;
if (*titleend != sep) {
return 0;
}
2025-01-20 18:49:00 +01:00
} else {
linkend = q;
}
/* Links can be given in angular brackets */
2025-01-20 18:49:00 +01:00
if (*link == '<' && *(linkend - 1) == '>') {
link++;
linkend--;
}
len = q + 1 - begin;
2025-01-20 18:49:00 +01:00
if (img) {
fputs("<img src=\"", stdout);
2008-06-28 21:06:02 +02:00
hprint(link, linkend);
fputs("\" alt=\"", stdout);
2008-06-28 21:06:02 +02:00
hprint(desc, descend);
fputs("\" ", stdout);
2025-01-20 18:49:00 +01:00
if (title && titleend) {
fputs("title=\"", stdout);
hprint(title, titleend);
fputs("\" ", stdout);
}
fputs("/>", stdout);
2025-01-20 18:49:00 +01:00
} else {
fputs("<a href=\"", stdout);
2008-06-28 21:06:02 +02:00
hprint(link, linkend);
fputs("\"", stdout);
2025-01-20 18:49:00 +01:00
if (title && titleend) {
fputs(" title=\"", stdout);
hprint(title, titleend);
fputs("\"", stdout);
}
fputs(">", stdout);
2008-06-28 21:06:02 +02:00
process(desc, descend, 0);
fputs("</a>", stdout);
2008-06-28 21:06:02 +02:00
}
return len;
2008-06-28 21:06:02 +02:00
}
2025-01-20 18:49:00 +01:00
int dolist(const char *begin, const char *end, int newblock)
{
2025-01-21 21:07:50 +01:00
unsigned int i;
unsigned int j;
unsigned int indent;
unsigned int run;
unsigned int ul;
unsigned int isblock;
const char *p;
const char *q;
char *buffer = NULL;
char marker = 0;
2008-06-28 21:06:02 +02:00
isblock = 0;
2025-01-20 18:49:00 +01:00
if (newblock)
2008-06-28 21:06:02 +02:00
p = begin;
2025-01-20 18:49:00 +01:00
else if (*begin == '\n')
2008-06-28 21:06:02 +02:00
p = begin + 1;
else
return 0;
q = p;
2025-01-20 18:49:00 +01:00
if (*p == '-' || *p == '*' || *p == '+') {
2008-06-28 21:06:02 +02:00
ul = 1;
marker = *p;
} else {
2008-06-28 21:06:02 +02:00
ul = 0;
2025-01-20 18:49:00 +01:00
for (; p < end && *p >= '0' && *p <= '9'; p++)
;
if (p >= end || *p != '.')
2008-06-28 21:06:02 +02:00
return 0;
}
p++;
2025-01-20 18:49:00 +01:00
if (p >= end || !(*p == ' ' || *p == '\t'))
2008-06-28 21:06:02 +02:00
return 0;
2025-01-20 18:49:00 +01:00
for (p++; p != end && (*p == ' ' || *p == '\t'); p++)
;
2008-06-28 21:06:02 +02:00
indent = p - q;
buffer = ereallocz(buffer, BUFSIZ);
2025-01-20 18:49:00 +01:00
if (!newblock)
fputc('\n', stdout);
fputs(ul ? "<ul>\n" : "<ol>\n", stdout);
2008-06-28 21:06:02 +02:00
run = 1;
2025-01-20 18:49:00 +01:00
for (; p < end && run; p++) {
for (i = 0; p < end && run; p++, i++) {
if (*p == '\n') {
if (p + 1 == end)
2008-06-28 21:06:02 +02:00
break;
else {
/* Handle empty lines */
2025-01-20 18:49:00 +01:00
for (q = p + 1;
(*q == ' ' || *q == '\t') &&
q < end;
q++)
;
if (*q == '\n') {
2025-01-21 19:51:56 +01:00
ADDC(buffer, i, '\0');
i++;
run = 0;
isblock++;
p = q;
}
2008-06-28 21:06:02 +02:00
}
q = p + 1;
j = 0;
2025-01-20 18:49:00 +01:00
if (ul && *q == marker)
2008-06-28 21:06:02 +02:00
j = 1;
2025-01-20 18:49:00 +01:00
else if (!ul) {
for (; q + j != end && q[j] >= '0' &&
q[j] <= '9' && j < indent;
j++)
;
if (q + j == end)
2008-06-28 21:06:02 +02:00
break;
2025-01-20 18:49:00 +01:00
if (j > 0 && q[j] == '.')
2008-06-28 21:06:02 +02:00
j++;
else
j = 0;
}
2025-01-20 18:49:00 +01:00
if (q + indent < end)
for (; (q[j] == ' ' || q[j] == '\t') &&
j < indent;
j++)
;
if (j == indent) {
2025-01-21 19:51:56 +01:00
ADDC(buffer, i, '\n');
2008-06-28 21:06:02 +02:00
i++;
p += indent;
run = 1;
2025-01-20 18:49:00 +01:00
if (*q == ' ' || *q == '\t')
2008-06-28 21:06:02 +02:00
p++;
else
break;
2025-01-20 18:49:00 +01:00
} else if (j < indent)
run = 0;
2008-06-28 21:06:02 +02:00
}
2025-01-21 19:51:56 +01:00
ADDC(buffer, i, *p);
2008-06-28 21:06:02 +02:00
}
2025-01-21 19:51:56 +01:00
ADDC(buffer, i, '\0');
fputs("<li>", stdout);
2025-01-20 18:49:00 +01:00
process(buffer, buffer + i,
isblock > 1 || (isblock == 1 && run));
fputs("</li>\n", stdout);
2008-06-28 21:06:02 +02:00
}
fputs(ul ? "</ul>\n" : "</ol>\n", stdout);
2008-06-28 21:06:02 +02:00
free(buffer);
p--;
2025-01-20 18:49:00 +01:00
while (*(--p) == '\n')
;
2008-06-28 21:06:02 +02:00
return -(p - begin + 1);
}
2025-01-20 18:49:00 +01:00
int doparagraph(const char *begin, const char *end, int newblock)
{
2008-06-28 21:06:02 +02:00
const char *p;
2025-01-20 18:49:00 +01:00
if (!newblock)
2008-06-28 21:06:02 +02:00
return 0;
p = strstr(begin, "\n\n");
2025-01-20 18:49:00 +01:00
if (!p || p > end)
2008-06-28 21:06:02 +02:00
p = end;
2025-01-20 18:49:00 +01:00
if (p - begin <= 1)
2008-06-28 21:06:02 +02:00
return 0;
2014-05-14 22:32:35 +02:00
fputs("<p>", stdout);
2008-06-28 21:06:02 +02:00
process(begin, p, 0);
fputs("</p>\n", stdout);
2008-06-28 21:06:02 +02:00
return -(p - begin);
}
2025-01-20 18:49:00 +01:00
int doreplace(const char *begin, const char *end, int newblock)
{
2025-01-21 21:07:50 +01:00
unsigned int i;
unsigned int l;
2008-06-28 21:06:02 +02:00
2025-01-20 18:49:00 +01:00
for (i = 0; i < LENGTH(insert); i++)
if (strncmp(insert[i][0], begin, strlen(insert[i][0])) == 0)
fputs(insert[i][1], stdout);
2025-01-20 18:49:00 +01:00
for (i = 0; i < LENGTH(replace); i++) {
2008-06-28 21:06:02 +02:00
l = strlen(replace[i][0]);
2025-01-20 18:49:00 +01:00
if (end - begin < l)
2008-06-28 21:06:02 +02:00
continue;
2025-01-20 18:49:00 +01:00
if (strncmp(replace[i][0], begin, l) == 0) {
fputs(replace[i][1], stdout);
2008-06-28 21:06:02 +02:00
return l;
}
}
return 0;
}
2025-01-20 18:49:00 +01:00
int doshortlink(const char *begin, const char *end, int newblock)
{
2025-01-21 21:07:50 +01:00
const char *p;
const char *c;
2008-06-28 21:06:02 +02:00
int ismail = 0;
2025-01-20 18:49:00 +01:00
if (*begin != '<')
2008-06-28 21:06:02 +02:00
return 0;
2025-01-20 18:49:00 +01:00
for (p = begin + 1; p != end; p++) {
switch (*p) {
2008-06-28 21:06:02 +02:00
case ' ':
case '\t':
case '\n':
return 0;
case '#':
case ':':
ismail = -1;
break;
case '@':
2025-01-20 18:49:00 +01:00
if (ismail == 0)
2008-06-28 21:06:02 +02:00
ismail = 1;
break;
case '>':
2025-01-20 18:49:00 +01:00
if (ismail == 0)
2008-06-28 21:06:02 +02:00
return 0;
fputs("<a href=\"", stdout);
2025-01-20 18:49:00 +01:00
if (ismail == 1) {
2008-06-28 21:06:02 +02:00
/* mailto: */
2025-01-20 18:49:00 +01:00
fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:",
stdout);
for (c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c);
fputs("\">", stdout);
2025-01-20 18:49:00 +01:00
for (c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c);
2025-01-20 18:49:00 +01:00
} else {
2008-06-28 21:06:02 +02:00
hprint(begin + 1, p);
fputs("\">", stdout);
2008-06-28 21:06:02 +02:00
hprint(begin + 1, p);
}
fputs("</a>", stdout);
2008-06-28 21:06:02 +02:00
return p - begin + 1;
}
}
return 0;
}
2025-01-20 18:49:00 +01:00
int dosurround(const char *begin, const char *end, int newblock)
{
2025-01-21 21:07:50 +01:00
unsigned int i;
unsigned int l;
const char *p;
const char *start;
const char *stop;
2008-06-28 21:06:02 +02:00
2025-01-20 18:49:00 +01:00
for (i = 0; i < LENGTH(surround); i++) {
2008-06-28 21:06:02 +02:00
l = strlen(surround[i].search);
2025-01-20 18:49:00 +01:00
if (end - begin < 2 * l ||
strncmp(begin, surround[i].search, l) != 0)
2008-06-28 21:06:02 +02:00
continue;
start = begin + l;
p = start - 1;
do {
stop = p;
2008-06-28 21:06:02 +02:00
p = strstr(p + 1, surround[i].search);
2025-01-20 18:49:00 +01:00
} while (p && p[-1] == '\\');
if (p && p[-1] != '\\')
stop = p;
2025-01-20 18:49:00 +01:00
if (!stop || stop < start || stop >= end)
2008-06-28 21:06:02 +02:00
continue;
fputs(surround[i].before, stdout);
/* Single space at start and end are ignored */
2021-10-14 22:39:58 -04:00
if (stop - start > 1 && *start == ' ' && *(stop - 1) == ' ') {
start++;
stop--;
l++;
}
2025-01-20 18:49:00 +01:00
if (surround[i].process)
2008-06-28 21:06:02 +02:00
process(start, stop, 0);
else
hprint(start, stop);
fputs(surround[i].after, stdout);
2008-06-28 21:06:02 +02:00
return stop - begin + l;
}
return 0;
}
2025-01-20 18:49:00 +01:00
int dounderline(const char *begin, const char *end, int newblock)
{
2025-01-21 21:07:50 +01:00
unsigned int i;
unsigned int j;
unsigned int l;
2008-06-28 21:06:02 +02:00
const char *p;
2025-01-20 18:49:00 +01:00
if (!newblock)
2008-06-28 21:06:02 +02:00
return 0;
p = begin;
2025-01-20 18:49:00 +01:00
for (l = 0; p + l + 1 != end && p[l] != '\n'; l++)
;
2008-06-28 21:06:02 +02:00
p += l + 1;
2025-01-20 18:49:00 +01:00
if (l == 0)
2008-06-28 21:06:02 +02:00
return 0;
2025-01-20 18:49:00 +01:00
for (i = 0; i < LENGTH(underline); i++) {
for (j = 0; p + j != end && p[j] != '\n' &&
p[j] == underline[i].search[0];
j++)
;
if (j >= l) {
fputs(underline[i].before, stdout);
2025-01-20 18:49:00 +01:00
if (underline[i].process)
2008-06-28 21:06:02 +02:00
process(begin, begin + l, 0);
else
hprint(begin, begin + l);
fputs(underline[i].after, stdout);
2008-06-28 21:06:02 +02:00
return -(j + p - begin);
}
}
return 0;
}
2025-01-20 18:49:00 +01:00
void *ereallocz(void *p, size_t size)
{
void *res;
2025-01-20 18:49:00 +01:00
if (p)
res = realloc(p, size);
else
res = calloc(1, size);
2025-01-20 18:49:00 +01:00
if (!res)
2025-01-21 21:11:59 +01:00
perror(PACKAGE);
return res;
}
2025-01-20 18:49:00 +01:00
void hprint(const char *begin, const char *end)
{
2008-06-28 21:06:02 +02:00
const char *p;
2025-01-20 18:49:00 +01:00
for (p = begin; p != end; p++) {
if (*p == '&')
fputs("&amp;", stdout);
2025-01-20 18:49:00 +01:00
else if (*p == '"')
fputs("&quot;", stdout);
2025-01-20 18:49:00 +01:00
else if (*p == '>')
fputs("&gt;", stdout);
2025-01-20 18:49:00 +01:00
else if (*p == '<')
fputs("&lt;", stdout);
2008-06-28 21:06:02 +02:00
else
fputc(*p, stdout);
2008-06-28 21:06:02 +02:00
}
}
2025-01-21 20:17:09 +01:00
int process(const char *begin, const char *end, int newblock)
2025-01-20 18:49:00 +01:00
{
2025-01-21 19:51:56 +01:00
const char *q;
const char *p;
2008-06-28 21:06:02 +02:00
int affected;
unsigned int i;
2025-01-20 18:49:00 +01:00
for (p = begin; p < end;) {
if (newblock)
while (*p == '\n')
if (++p == end)
2025-01-21 20:17:09 +01:00
return 1;
2008-06-28 21:06:02 +02:00
affected = 0;
2025-01-20 18:49:00 +01:00
for (i = 0; i < LENGTH(parsers) && !affected; i++)
2008-06-28 21:06:02 +02:00
affected = parsers[i](p, end, newblock);
p += abs(affected);
2025-01-21 20:17:09 +01:00
if (affected == -1)
return 0;
2025-01-20 18:49:00 +01:00
if (!affected) {
if (nohtml)
2008-06-28 21:06:02 +02:00
hprint(p, p + 1);
else
fputc(*p, stdout);
2008-06-28 21:06:02 +02:00
p++;
}
2025-01-20 18:49:00 +01:00
for (q = p; q != end && *q == '\n'; q++)
;
if (q == end)
2025-01-21 20:17:09 +01:00
return 1;
2025-01-20 18:49:00 +01:00
else if (p[0] == '\n' && p + 1 != end && p[1] == '\n')
2008-06-28 21:06:02 +02:00
newblock = 1;
else
newblock = affected < 0;
}
2025-01-21 20:17:09 +01:00
return 1;
2008-06-28 21:06:02 +02:00
}
2025-01-20 18:49:00 +01:00
int main(int argc, char *argv[])
{
char *buffer = NULL;
2025-01-21 21:07:50 +01:00
const char *path;
2025-01-21 19:51:56 +01:00
int i;
2025-01-21 20:12:16 +01:00
int ret = EXIT_SUCCESS;
2008-06-28 21:06:02 +02:00
2025-01-20 18:49:00 +01:00
for (i = 1; i < argc; i++) {
2025-01-21 21:07:50 +01:00
if (!strcmp("-v", argv[i])) {
2025-01-21 21:11:59 +01:00
eprint("simple markup %s (C) 2007 - 2014 Enno Boland, (C) 2025 Enno Tensing\n", VERSION);
2025-01-21 21:07:50 +01:00
goto exit;
} else if (!strcmp("-n", argv[i])) {
nohtml = 1;
2025-01-21 21:07:50 +01:00
} else if (argv[i][0] != '-') {
break;
2025-01-21 21:07:50 +01:00
} else if (!strcmp("--", argv[i])) {
i++;
break;
2025-01-21 21:07:50 +01:00
} else {
2025-01-20 18:49:00 +01:00
eprint("Usage %s [-n] [file]\n -n escape html strictly\n",
argv[0]);
2025-01-21 21:07:50 +01:00
goto exit;
}
}
2025-01-21 19:51:56 +01:00
2025-01-21 20:12:16 +01:00
if (i < argc) {
2025-01-21 19:51:56 +01:00
path = argv[i];
2025-01-21 20:12:16 +01:00
off64_t len = get_file_size(path);
if (len == -1) {
eprint("%s: %s: %s\n", argv[0], path, strerror(errno));
ret = EXIT_FAILURE;
goto exit;
}
buffer = read_file(path, len);
if (!buffer) {
2025-01-21 20:17:09 +01:00
perror(PACKAGE);
2025-01-21 20:12:16 +01:00
ret = EXIT_FAILURE;
goto exit;
}
buffer[len] = '\0';
process(buffer, buffer + len, 1);
free(buffer);
} else {
size_t buffer_size = 1024 * CHARWIDTH;
buffer = calloc(buffer_size + CHARWIDTH, sizeof(char));
if (!buffer) {
2025-01-21 20:17:09 +01:00
perror(PACKAGE);
2025-01-21 20:12:16 +01:00
ret = EXIT_FAILURE;
goto exit;
}
size_t read_bytes;
while (1) {
read_bytes = read(STDIN_FILENO, buffer, buffer_size);
if (read_bytes <= 0) {
if (errno) {
2025-01-21 20:17:09 +01:00
perror(PACKAGE);
2025-01-21 20:12:16 +01:00
ret = EXIT_FAILURE;
}
free(buffer);
break;
}
buffer[read_bytes] = '\0';
2025-01-21 20:17:09 +01:00
if (!process(buffer, buffer + read_bytes, 1)) {
ret = EXIT_FAILURE;
free(buffer);
break;
}
2025-01-21 20:12:16 +01:00
}
2008-06-28 21:06:02 +02:00
}
2025-01-21 20:12:16 +01:00
exit:
return ret;
}