Compare commits

..

No commits in common. "4ab889aeb4663ae0ca63bf558edda7fc881c60dc" and "7f1782d525dc63a664ab392338b789a18e8541cc" have entirely different histories.

6 changed files with 322 additions and 435 deletions

View file

@ -1,17 +1,12 @@
# libsmu - simple markup # libsmu - simple markup
# (c) 2007, 2008 Enno Boland # (c) 2007, 2008 Enno Boland
VERSION = 1.5 include config.mk
PREFIX ?= /usr/local
MANPREFIX ?= ${PREFIX}/share/man
CFLAGS += -DVERSION=\"${VERSION}\"
PGO_GEN = ${CFLAGS} -fprofile-generate
PGO_USE = ${CFLAGS} -fprofile-use -fprofile-correction
LDFLAGS ?=
CC ?= cc
SRC = smu.c
all: smu SRC = smu.c
OBJ = ${SRC:.c=.o}
all: options smu
options: options:
@echo smu build options: @echo smu build options:
@ -19,51 +14,42 @@ options:
@echo "LDFLAGS = ${LDFLAGS}" @echo "LDFLAGS = ${LDFLAGS}"
@echo "CC = ${CC}" @echo "CC = ${CC}"
smu: ${SRC} .c.o:
@echo CC $@ @echo CC $<
@${CC} -o $@ ${SRC} ${CFLAGS} ${LDFLAGS} @${CC} -c ${CFLAGS} $<
pgo-build: ${SRC} ${OBJ}: config.mk
@echo CC smu
@${CC} -o smu ${SRC} ${PGO_GEN} ${LDFLAGS}
@echo Generating pgo data
@printf .
@./smu testdoc > /dev/null
@printf .
@cat testdoc | ./smu > /dev/null
@printf .
@./smu -n testdoc > /dev/null
@printf .
@cat testdoc | ./smu -n > /dev/null
@printf .
@rm smu
@printf '\n'
@echo Recompiling with pgo data
@echo CC smu
@${CC} -o smu ${SRC} ${PGO_USE} ${LDFLAGS}
@rm smu.gcda
smu: ${OBJ}
@echo LD $@
@${CC} -o $@ ${OBJ} ${LDFLAGS}
clean: clean:
rm -f smu ${OBJ} ${LIBOBJ} smu-${VERSION}.tar.gz @echo cleaning
@rm -f smu ${OBJ} ${LIBOBJ} smu-${VERSION}.tar.gz
dist: clean dist: clean
mkdir -p smu-${VERSION} @echo creating dist tarball
cp -R LICENSE Makefile config.mk smu.1 ${SRC} smu-${VERSION} @mkdir -p smu-${VERSION}
tar -cf smu-${VERSION}.tar smu-${VERSION} @cp -R LICENSE Makefile config.mk smu.1 ${SRC} smu-${VERSION}
gzip smu-${VERSION}.tar @tar -cf smu-${VERSION}.tar smu-${VERSION}
rm -rf smu-${VERSION} @gzip smu-${VERSION}.tar
@rm -rf smu-${VERSION}
install: all install: all
mkdir -p ${DESTDIR}${PREFIX}/bin @echo installing executable file to ${DESTDIR}${PREFIX}/bin
cp -f smu ${DESTDIR}${PREFIX}/bin @mkdir -p ${DESTDIR}${PREFIX}/bin
chmod 755 ${DESTDIR}${PREFIX}/bin/smu @cp -f smu ${DESTDIR}${PREFIX}/bin
mkdir -p ${DESTDIR}${MANPREFIX}/man1 @chmod 755 ${DESTDIR}${PREFIX}/bin/smu
sed "s/VERSION/${VERSION}/g" < smu.1 > ${DESTDIR}${MANPREFIX}/man1/smu.1 @echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
chmod 644 ${DESTDIR}${MANPREFIX}/man1/smu.1 @mkdir -p ${DESTDIR}${MANPREFIX}/man1
@sed "s/VERSION/${VERSION}/g" < smu.1 > ${DESTDIR}${MANPREFIX}/man1/smu.1
@chmod 644 ${DESTDIR}${MANPREFIX}/man1/smu.1
uninstall: uninstall:
rm -f ${DESTDIR}${PREFIX}/bin/smu @echo removing executable file from ${DESTDIR}${PREFIX}/bin
rm -f ${DESTDIR}${MANPREFIX}/man1/smu.1 @rm -f ${DESTDIR}${PREFIX}/bin/smu
@echo removing manual page from ${DESTDIR}${MANPREFIX}/man1
@rm -f ${DESTDIR}${MANPREFIX}/man1/smu.1
.PHONY: all options clean dist install uninstall pgo-build .PHONY: all options clean dist install uninstall

20
config.mk Normal file
View file

@ -0,0 +1,20 @@
# smu version
VERSION = 1.5
# paths
PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man
# includes and libs
INCS = -I. -I/usr/include
LIBS = -L/usr/lib
# flags
CFLAGS = -g -O0 -Wall -Werror -ansi ${INCS} -DVERSION=\"${VERSION}\"
#CFLAGS = -fprofile-arcs -ftest-coverage -pg -g -O0 -Wall -Werror -ansi ${INCS} -DVERSION=\"${VERSION}\"
#CFLAGS = -Os -Wall -Werror -ansi ${INCS} -DVERSION=\"${VERSION}\"
#LDFLAGS = -fprofile-arcs -ftest-coverage -pg ${LIBS}
LDFLAGS = ${LIBS}
# compiler
CC = cc

2
smu.1
View file

@ -20,4 +20,4 @@ prints usage information to standard error, then exits.
.B \-n .B \-n
escapes all HTML Tags. escapes all HTML Tags.
.SH BUGS .SH BUGS
Report any bugs to tenno+smu@suij.in Please report any Bugs to https://github.com/Gottox/smu/issues or via mail.

604
smu.c
View file

@ -1,78 +1,48 @@
/* smu - simple markup /* smu - simple markup
* Copyright (C) <2007, 2008> Enno Boland <g s01 de> * Copyright (C) <2007, 2008> Enno Boland <g s01 de>
* Copyright (C) 2025 Enno Tensing <tenno+smu@suij.in>
* *
* See LICENSE for further informations * See LICENSE for further informations
*/ */
#define _LARGEFILE64_SOURCE
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#ifndef PACKAGE #define LENGTH(x) sizeof(x)/sizeof(x[0])
#define PACKAGE "smu" #define ADDC(b,i) if(i % BUFSIZ == 0) { b = realloc(b, (i + BUFSIZ) * sizeof(char)); if(!b) eprint("Malloc failed."); } b[i]
#endif
#ifndef VERSION
#define VERSION "0.0"
#endif
#define CHARWIDTH 4
#define LENGTH(x) sizeof(x) / sizeof(x[0])
#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)
typedef int (*Parser)(const char *, const char *, int); typedef int (*Parser)(const char *, const char *, int);
struct tag { typedef struct {
char *search; char *search;
int process; int process;
char *before; char *before, *after;
char *after; } Tag;
};
off64_t get_file_size(const char *); static int doamp(const char *begin, const char *end, int newblock); /* Parser for & */
char *read_file(const char *, off64_t); static int docomment(const char *begin, const char *end, int newblock); /* Parser for html-comments */
static int dogtlt(const char *begin, const char *end, int newblock); /* Parser for < and > */
static int doamp(const char *begin, const char *end, int newblock); static int dohtml(const char *begin, const char *end, int newblock); /* Parser for html */
static int docomment(const char *begin, const char *end, int newblock); static int dolineprefix(const char *begin, const char *end, int newblock);/* Parser for line prefix tags */
static int dogtlt(const char *begin, const char *end, int newblock); static int dolink(const char *begin, const char *end, int newblock); /* Parser for links and images */
static int dohtml(const char *begin, const char *end, int newblock); static int dolist(const char *begin, const char *end, int newblock); /* Parser for lists */
static int dolineprefix(const char *begin, const char *end, int newblock); static int doparagraph(const char *begin, const char *end, int newblock); /* Parser for paragraphs */
static int dolink(const char *begin, const char *end, int newblock); static int doreplace(const char *begin, const char *end, int newblock); /* Parser for simple replaces */
static int dolist(const char *begin, const char *end, int newblock); static int doshortlink(const char *begin, const char *end, int newblock); /* Parser for links and images */
static int doparagraph(const char *begin, const char *end, int newblock); static int dosurround(const char *begin, const char *end, int newblock); /* Parser for surrounding tags */
static int doreplace(const char *begin, const char *end, int newblock); static int dounderline(const char *begin, const char *end, int newblock); /* Parser for underline tags */
static int doshortlink(const char *begin, const char *end, int newblock);
static int dosurround(const char *begin, const char *end, int newblock);
static int dounderline(const char *begin, const char *end, int newblock);
static void *ereallocz(void *p, size_t size); static void *ereallocz(void *p, size_t size);
static void eprint(const char *format, ...); static void eprint(const char *format, ...);
static void hprint(const char *begin, const char *end); static void hprint(const char *begin, const char *end); /* escapes HTML and prints it to output */
static int process(const char *begin, const char *end, int isblock); static void process(const char *begin, const char *end, int isblock); /* Processes range between begin and end. */
/* list of parsers */ /* list of parsers */
static Parser parsers[] = { dounderline, docomment, dolineprefix, dolist, static Parser parsers[] = { dounderline, docomment, dolineprefix,
doparagraph, dogtlt, dosurround, dolink, dolist, doparagraph, dogtlt, dosurround, dolink,
doshortlink, dohtml, doamp, doreplace }; doshortlink, dohtml, doamp, doreplace };
static int nohtml = 0; static int nohtml = 0;
static struct tag lineprefix[] = { static Tag lineprefix[] = {
{ " ", 0, "<pre><code>", "\n</code></pre>" }, { " ", 0, "<pre><code>", "\n</code></pre>" },
{ "\t", 0, "<pre><code>", "\n</code></pre>" }, { "\t", 0, "<pre><code>", "\n</code></pre>" },
{ ">", 2, "<blockquote>", "</blockquote>" }, { ">", 2, "<blockquote>", "</blockquote>" },
@ -82,16 +52,15 @@ static struct tag lineprefix[] = {
{ "### ", 1, "<h3>", "</h3>" }, { "### ", 1, "<h3>", "</h3>" },
{ "## ", 1, "<h2>", "</h2>" }, { "## ", 1, "<h2>", "</h2>" },
{ "# ", 1, "<h1>", "</h1>" }, { "# ", 1, "<h1>", "</h1>" },
{ "- - -\n", 1, "<hr />", "" }, { "- - -\n", 1, "<hr />", ""},
}; };
static struct tag underline[] = { static Tag underline[] = {
{ "=", 1, "<h1>", "</h1>\n" }, { "=", 1, "<h1>", "</h1>\n" },
{ "-", 1, "<h2>", "</h2>\n" }, { "-", 1, "<h2>", "</h2>\n" },
}; };
static struct tag surround[] = { static Tag surround[] = {
{ "```", 0, "<code>", "</code>" },
{ "``", 0, "<code>", "</code>" }, { "``", 0, "<code>", "</code>" },
{ "`", 0, "<code>", "</code>" }, { "`", 0, "<code>", "</code>" },
{ "___", 1, "<strong><em>", "</em></strong>" }, { "___", 1, "<strong><em>", "</em></strong>" },
@ -103,203 +72,174 @@ static struct tag surround[] = {
}; };
static const char *replace[][2] = { static const char *replace[][2] = {
{ "\\\\", "\\" }, { "\\`", "`" }, { "\\*", "*" }, { "\\_", "_" }, { "\\\\", "\\" },
{ "\\{", "{" }, { "\\}", "}" }, { "\\[", "[" }, { "\\]", "]" }, { "\\`", "`" },
{ "\\(", "(" }, { "\\)", ")" }, { "\\#", "#" }, { "\\+", "+" }, { "\\*", "*" },
{ "\\-", "-" }, { "\\.", "." }, { "\\!", "!" }, { "\\_", "_" },
{ "\\{", "{" },
{ "\\}", "}" },
{ "\\[", "[" },
{ "\\]", "]" },
{ "\\(", "(" },
{ "\\)", ")" },
{ "\\#", "#" },
{ "\\+", "+" },
{ "\\-", "-" },
{ "\\.", "." },
{ "\\!", "!" },
}; };
static const char *insert[][2] = { static const char *insert[][2] = {
{ " \n", "<br />" }, { " \n", "<br />" },
}; };
off64_t get_file_size(const char *path) void
{ eprint(const char *format, ...) {
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;
char *buf = calloc(file_size + CHARWIDTH, sizeof(char));
if (!buf) {
perror(PACKAGE);
close(fd);
return NULL;
}
bytes = read(fd, buf, file_size);
if (bytes != file_size) {
perror(PACKAGE);
close(fd);
free(buf);
return NULL;
}
close(fd);
return buf;
}
void eprint(const char *format, ...)
{
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vfprintf(stderr, format, ap); vfprintf(stderr, format, ap);
va_end(ap); va_end(ap);
exit(EXIT_FAILURE);
} }
int doamp(const char *begin, const char *end, int newblock) int
{ doamp(const char *begin, const char *end, int newblock) {
const char *p; const char *p;
if (*begin != '&') if(*begin != '&')
return 0; return 0;
if (!nohtml) { if(!nohtml) {
for (p = begin + 1; p != end && !strchr("; \\\n\t", *p); p++) for(p = begin + 1; p != end && !strchr("; \\\n\t", *p); p++);
; if(p == end || *p == ';')
if (p == end || *p == ';')
return 0; return 0;
} }
fputs("&amp;", stdout); fputs("&amp;", stdout);
return 1; return 1;
} }
int dogtlt(const char *begin, const char *end, int newblock) int
{ dogtlt(const char *begin, const char *end, int newblock) {
int brpos; int brpos;
char c; char c;
if (nohtml || begin + 1 >= end) if(nohtml || begin + 1 >= end)
return 0; return 0;
brpos = begin[1] == '>'; brpos = begin[1] == '>';
if (!brpos && *begin != '<') if(!brpos && *begin != '<')
return 0; return 0;
c = begin[brpos ? 0 : 1]; c = begin[brpos ? 0 : 1];
if (!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) { if(!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) {
fputs("&lt;", stdout); fputs("&lt;", stdout);
return 1; return 1;
} else if (brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && }
!strchr("/\"'", c)) { else if(brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && !strchr("/\"'",c)) {
fprintf(stdout, "%c&gt;", c); fprintf(stdout, "%c&gt;",c);
return 2; return 2;
} }
return 0; return 0;
} }
int docomment(const char *begin, const char *end, int newblock) int
{ docomment(const char *begin, const char *end, int newblock) {
char *p; char *p;
if (nohtml || strncmp("<!--", begin, 4)) if(nohtml || strncmp("<!--", begin, 4))
return 0; return 0;
p = strstr(begin, "-->"); p = strstr(begin, "-->");
if (!p || p + 3 >= end) if(!p || p + 3 >= end)
return 0; return 0;
fprintf(stdout, "%.*s\n", (int)(p + 3 - begin), begin); fprintf(stdout, "%.*s\n", (int)(p + 3 - begin), begin);
return (p + 3 - begin) * (newblock ? -1 : 1); return (p + 3 - begin) * (newblock ? -1 : 1);
} }
int dohtml(const char *begin, const char *end, int newblock) int
{ dohtml(const char *begin, const char *end, int newblock) {
const char *p, *tag, *tagend; const char *p, *tag, *tagend;
if (nohtml || begin + 2 >= end) if(nohtml || begin + 2 >= end)
return 0; return 0;
p = begin; p = begin;
if (p[0] != '<' || !isalpha(p[1])) if(p[0] != '<' || !isalpha(p[1]))
return 0; return 0;
p++; p++;
tag = p; tag = p;
for (; isalnum(*p) && p < end; p++) for(; isalnum(*p) && p < end; p++);
;
tagend = p; tagend = p;
if (p > end || tag == tagend) if(p > end || tag == tagend)
return 0; return 0;
while ((p = strstr(p, "</")) && p < end) { while((p = strstr(p, "</")) && p < end) {
p += 2; p += 2;
if (strncmp(p, tag, tagend - tag) == 0 && if(strncmp(p, tag, tagend - tag) == 0 && p[tagend - tag] == '>') {
p[tagend - tag] == '>') {
p++; p++;
fwrite(begin, sizeof(char), fwrite(begin, sizeof(char), p - begin + tagend - tag - 1, stdout);
p - begin + tagend - tag - 1, stdout);
return p - begin + tagend - tag - 1; return p - begin + tagend - tag - 1;
} }
} }
p = strchr(tagend, '>'); p = strchr(tagend, '>');
if (p) { if(p) {
fwrite(begin, sizeof(char), p - begin + 1, stdout); fwrite(begin, sizeof(char), p - begin + 1, stdout);
return p - begin + 1; return p - begin + 1;
} else }
else
return 0; return 0;
} }
int dolineprefix(const char *begin, const char *end, int newblock) int
{ dolineprefix(const char *begin, const char *end, int newblock) {
unsigned int i, j, l; unsigned int i, j, l;
char *buffer; char *buffer;
const char *p; const char *p;
if (newblock) if(newblock)
p = begin; p = begin;
else if (*begin == '\n') else if(*begin == '\n')
p = begin + 1; p = begin + 1;
else else
return 0; return 0;
for (i = 0; i < LENGTH(lineprefix); i++) { for(i = 0; i < LENGTH(lineprefix); i++) {
l = strlen(lineprefix[i].search); l = strlen(lineprefix[i].search);
if (end - p < l) if(end - p < l)
continue; continue;
if (strncmp(lineprefix[i].search, p, l)) if(strncmp(lineprefix[i].search, p, l))
continue; continue;
if (*begin == '\n') if(*begin == '\n')
fputc('\n', stdout); fputc('\n', stdout);
fputs(lineprefix[i].before, stdout); fputs(lineprefix[i].before, stdout);
if (lineprefix[i].search[l - 1] == '\n') { if(lineprefix[i].search[l-1] == '\n') {
fputc('\n', stdout); fputc('\n', stdout);
return l - 1; return l - 1;
} }
if (!(buffer = malloc(BUFSIZ))) if(!(buffer = malloc(BUFSIZ)))
eprint("Malloc failed."); eprint("Malloc failed.");
buffer[0] = '\0'; buffer[0] = '\0';
/* Collect lines into buffer while they start with the prefix */ /* Collect lines into buffer while they start with the prefix */
j = 0; j = 0;
while ((strncmp(lineprefix[i].search, p, l) == 0) && while((strncmp(lineprefix[i].search, p, l) == 0) && p + l < end) {
p + l < end) {
p += l; p += l;
/* Special case for blockquotes: optional space after > */ /* Special case for blockquotes: optional space after > */
if (lineprefix[i].search[0] == '>' && *p == ' ') { if(lineprefix[i].search[0] == '>' && *p == ' ') {
p++; p++;
} }
while (p < end) { while(p < end) {
ADDC(buffer, j, *p); ADDC(buffer, j) = *p;
j++; j++;
if (*(p++) == '\n') if(*(p++) == '\n')
break; break;
} }
} }
/* Skip empty lines in block */ /* Skip empty lines in block */
while (buffer + j - 1 >= buffer && *(buffer + j - 1) == '\n') { while(buffer + j - 1 >= buffer && *(buffer + j - 1) == '\n') {
j--; j--;
} }
ADDC(buffer, j, '\0'); ADDC(buffer, j) = '\0';
if (lineprefix[i].process) if(lineprefix[i].process)
process(buffer, buffer + strlen(buffer), process(buffer, buffer + strlen(buffer), lineprefix[i].process >= 2);
lineprefix[i].process >= 2);
else else
hprint(buffer, buffer + strlen(buffer)); hprint(buffer, buffer + strlen(buffer));
puts(lineprefix[i].after); puts(lineprefix[i].after);
@ -309,82 +249,79 @@ int dolineprefix(const char *begin, const char *end, int newblock)
return 0; return 0;
} }
int dolink(const char *begin, const char *end, int newblock) int
{ dolink(const char *begin, const char *end, int newblock) {
int img, len, sep, parens_depth = 1; int img, len, sep, parens_depth = 1;
const char *desc, *link, *p, *q, *descend, *linkend; const char *desc, *link, *p, *q, *descend, *linkend;
const char *title = NULL, *titleend = NULL; const char *title = NULL, *titleend = NULL;
if (*begin == '[') if(*begin == '[')
img = 0; img = 0;
else if (strncmp(begin, "![", 2) == 0) else if(strncmp(begin, "![", 2) == 0)
img = 1; img = 1;
else else
return 0; return 0;
p = desc = begin + 1 + img; p = desc = begin + 1 + img;
if (!(p = strstr(desc, "](")) || p > end) if(!(p = strstr(desc, "](")) || p > end)
return 0; return 0;
for (q = strstr(desc, "!["); q && q < end && q < p; for(q = strstr(desc, "!["); q && q < end && q < p; q = strstr(q + 1, "!["))
q = strstr(q + 1, "![")) if(!(p = strstr(p + 1, "](")) || p > end)
if (!(p = strstr(p + 1, "](")) || p > end)
return 0; return 0;
descend = p; descend = p;
link = p + 2; link = p + 2;
/* find end of link while handling nested parens */ /* find end of link while handling nested parens */
q = link; q = link;
while (parens_depth) { while(parens_depth) {
if (!(q = strpbrk(q, "()")) || q > end) if(!(q = strpbrk(q, "()")) || q > end)
return 0; return 0;
if (*q == '(') if(*q == '(')
parens_depth++; parens_depth++;
else else
parens_depth--; parens_depth--;
if (parens_depth && q < end) if(parens_depth && q < end)
q++; q++;
} }
if ((p = strpbrk(link, "\"'")) && p < end && q - 1 > p + 1) { if((p = strpbrk(link, "\"'")) && p < end && q - 1 > p + 1) {
sep = p[0]; /* separator: can be " or ' */ sep = p[0]; /* separator: can be " or ' */
title = p + 1; title = p + 1;
/* strip trailing whitespace */ /* strip trailing whitespace */
for (linkend = p; linkend > link && isspace(*(linkend - 1)); for(linkend = p; linkend > link && isspace(*(linkend - 1)); linkend--);
linkend--) for(titleend = q - 1; titleend > title && isspace(*(titleend)); titleend--);
; if(*titleend != sep) {
for (titleend = q - 1; titleend > title && isspace(*(titleend));
titleend--)
;
if (*titleend != sep) {
return 0; return 0;
} }
} else { }
else {
linkend = q; linkend = q;
} }
/* Links can be given in angular brackets */ /* Links can be given in angular brackets */
if (*link == '<' && *(linkend - 1) == '>') { if(*link == '<' && *(linkend - 1) == '>') {
link++; link++;
linkend--; linkend--;
} }
len = q + 1 - begin; len = q + 1 - begin;
if (img) { if(img) {
fputs("<img src=\"", stdout); fputs("<img src=\"", stdout);
hprint(link, linkend); hprint(link, linkend);
fputs("\" alt=\"", stdout); fputs("\" alt=\"", stdout);
hprint(desc, descend); hprint(desc, descend);
fputs("\" ", stdout); fputs("\" ", stdout);
if (title && titleend) { if(title && titleend) {
fputs("title=\"", stdout); fputs("title=\"", stdout);
hprint(title, titleend); hprint(title, titleend);
fputs("\" ", stdout); fputs("\" ", stdout);
} }
fputs("/>", stdout); fputs("/>", stdout);
} else { }
else {
fputs("<a href=\"", stdout); fputs("<a href=\"", stdout);
hprint(link, linkend); hprint(link, linkend);
fputs("\"", stdout); fputs("\"", stdout);
if (title && titleend) { if(title && titleend) {
fputs(" title=\"", stdout); fputs(" title=\"", stdout);
hprint(title, titleend); hprint(title, titleend);
fputs("\"", stdout); fputs("\"", stdout);
@ -396,56 +333,50 @@ int dolink(const char *begin, const char *end, int newblock)
return len; return len;
} }
int dolist(const char *begin, const char *end, int newblock) int
{ dolist(const char *begin, const char *end, int newblock) {
unsigned int i, j, indent, run, ul, isblock; unsigned int i, j, indent, run, ul, isblock;
const char *p, *q; const char *p, *q;
char *buffer = NULL; char *buffer = NULL;
char marker = 0; char marker = 0;
isblock = 0; isblock = 0;
if (newblock) if(newblock)
p = begin; p = begin;
else if (*begin == '\n') else if(*begin == '\n')
p = begin + 1; p = begin + 1;
else else
return 0; return 0;
q = p; q = p;
if (*p == '-' || *p == '*' || *p == '+') { if(*p == '-' || *p == '*' || *p == '+') {
ul = 1; ul = 1;
marker = *p; marker = *p;
} else { } else {
ul = 0; ul = 0;
for (; p < end && *p >= '0' && *p <= '9'; p++) for(; p < end && *p >= '0' && *p <= '9'; p++);
; if(p >= end || *p != '.')
if (p >= end || *p != '.')
return 0; return 0;
} }
p++; p++;
if (p >= end || !(*p == ' ' || *p == '\t')) if(p >= end || !(*p == ' ' || *p == '\t'))
return 0; return 0;
for (p++; p != end && (*p == ' ' || *p == '\t'); p++) for(p++; p != end && (*p == ' ' || *p == '\t'); p++);
;
indent = p - q; indent = p - q;
buffer = ereallocz(buffer, BUFSIZ); buffer = ereallocz(buffer, BUFSIZ);
if (!newblock) if(!newblock)
fputc('\n', stdout); fputc('\n', stdout);
fputs(ul ? "<ul>\n" : "<ol>\n", stdout); fputs(ul ? "<ul>\n" : "<ol>\n", stdout);
run = 1; run = 1;
for (; p < end && run; p++) { for(; p < end && run; p++) {
for (i = 0; p < end && run; p++, i++) { for(i = 0; p < end && run; p++, i++) {
if (*p == '\n') { if(*p == '\n') {
if (p + 1 == end) if(p + 1 == end)
break; break;
else { else {
/* Handle empty lines */ /* Handle empty lines */
for (q = p + 1; for(q = p + 1; (*q == ' ' || *q == '\t') && q < end; q++);
(*q == ' ' || *q == '\t') && if(*q == '\n') {
q < end; ADDC(buffer, i) = '\n';
q++)
;
if (*q == '\n') {
ADDC(buffer, i, '\0');
i++; i++;
run = 0; run = 0;
isblock++; isblock++;
@ -454,63 +385,56 @@ int dolist(const char *begin, const char *end, int newblock)
} }
q = p + 1; q = p + 1;
j = 0; j = 0;
if (ul && *q == marker) if(ul && *q == marker)
j = 1; j = 1;
else if (!ul) { else if(!ul) {
for (; q + j != end && q[j] >= '0' && for(; q + j != end && q[j] >= '0' && q[j] <= '9' && j < indent; j++);
q[j] <= '9' && j < indent; if(q + j == end)
j++)
;
if (q + j == end)
break; break;
if (j > 0 && q[j] == '.') if(j > 0 && q[j] == '.')
j++; j++;
else else
j = 0; j = 0;
} }
if (q + indent < end) if(q + indent < end)
for (; (q[j] == ' ' || q[j] == '\t') && for(; (q[j] == ' ' || q[j] == '\t') && j < indent; j++);
j < indent; if(j == indent) {
j++) ADDC(buffer, i) = '\n';
;
if (j == indent) {
ADDC(buffer, i, '\n');
i++; i++;
p += indent; p += indent;
run = 1; run = 1;
if (*q == ' ' || *q == '\t') if(*q == ' ' || *q == '\t')
p++; p++;
else else
break; break;
} else if (j < indent) }
else if (j < indent)
run = 0; run = 0;
} }
ADDC(buffer, i, *p); ADDC(buffer, i) = *p;
} }
ADDC(buffer, i, '\0'); ADDC(buffer, i) = '\0';
fputs("<li>", stdout); fputs("<li>", stdout);
process(buffer, buffer + i, process(buffer, buffer + i, isblock > 1 || (isblock == 1 && run));
isblock > 1 || (isblock == 1 && run));
fputs("</li>\n", stdout); fputs("</li>\n", stdout);
} }
fputs(ul ? "</ul>\n" : "</ol>\n", stdout); fputs(ul ? "</ul>\n" : "</ol>\n", stdout);
free(buffer); free(buffer);
p--; p--;
while (*(--p) == '\n') while(*(--p) == '\n');
;
return -(p - begin + 1); return -(p - begin + 1);
} }
int doparagraph(const char *begin, const char *end, int newblock) int
{ doparagraph(const char *begin, const char *end, int newblock) {
const char *p; const char *p;
if (!newblock) if(!newblock)
return 0; return 0;
p = strstr(begin, "\n\n"); p = strstr(begin, "\n\n");
if (!p || p > end) if(!p || p > end)
p = end; p = end;
if (p - begin <= 1) if(p - begin <= 1)
return 0; return 0;
fputs("<p>", stdout); fputs("<p>", stdout);
process(begin, p, 0); process(begin, p, 0);
@ -518,18 +442,18 @@ int doparagraph(const char *begin, const char *end, int newblock)
return -(p - begin); return -(p - begin);
} }
int doreplace(const char *begin, const char *end, int newblock) int
{ doreplace(const char *begin, const char *end, int newblock) {
unsigned int i, l; unsigned int i, l;
for (i = 0; i < LENGTH(insert); i++) for(i = 0; i < LENGTH(insert); i++)
if (strncmp(insert[i][0], begin, strlen(insert[i][0])) == 0) if(strncmp(insert[i][0], begin, strlen(insert[i][0])) == 0)
fputs(insert[i][1], stdout); fputs(insert[i][1], stdout);
for (i = 0; i < LENGTH(replace); i++) { for(i = 0; i < LENGTH(replace); i++) {
l = strlen(replace[i][0]); l = strlen(replace[i][0]);
if (end - begin < l) if(end - begin < l)
continue; continue;
if (strncmp(replace[i][0], begin, l) == 0) { if(strncmp(replace[i][0], begin, l) == 0) {
fputs(replace[i][1], stdout); fputs(replace[i][1], stdout);
return l; return l;
} }
@ -537,15 +461,15 @@ int doreplace(const char *begin, const char *end, int newblock)
return 0; return 0;
} }
int doshortlink(const char *begin, const char *end, int newblock) int
{ doshortlink(const char *begin, const char *end, int newblock) {
const char *p, *c; const char *p, *c;
int ismail = 0; int ismail = 0;
if (*begin != '<') if(*begin != '<')
return 0; return 0;
for (p = begin + 1; p != end; p++) { for(p = begin + 1; p != end; p++) {
switch (*p) { switch(*p) {
case ' ': case ' ':
case '\t': case '\t':
case '\n': case '\n':
@ -555,23 +479,23 @@ int doshortlink(const char *begin, const char *end, int newblock)
ismail = -1; ismail = -1;
break; break;
case '@': case '@':
if (ismail == 0) if(ismail == 0)
ismail = 1; ismail = 1;
break; break;
case '>': case '>':
if (ismail == 0) if(ismail == 0)
return 0; return 0;
fputs("<a href=\"", stdout); fputs("<a href=\"", stdout);
if (ismail == 1) { if(ismail == 1) {
/* mailto: */ /* mailto: */
fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:", fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:", stdout);
stdout); for(c = begin + 1; *c != '>'; c++)
for (c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c); fprintf(stdout, "&#%u;", *c);
fputs("\">", stdout); fputs("\">", stdout);
for (c = begin + 1; *c != '>'; c++) for(c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c); fprintf(stdout, "&#%u;", *c);
} else { }
else {
hprint(begin + 1, p); hprint(begin + 1, p);
fputs("\">", stdout); fputs("\">", stdout);
hprint(begin + 1, p); hprint(begin + 1, p);
@ -583,25 +507,24 @@ int doshortlink(const char *begin, const char *end, int newblock)
return 0; return 0;
} }
int dosurround(const char *begin, const char *end, int newblock) int
{ dosurround(const char *begin, const char *end, int newblock) {
unsigned int i, l; unsigned int i, l;
const char *p, *start, *stop; const char *p, *start, *stop;
for (i = 0; i < LENGTH(surround); i++) { for(i = 0; i < LENGTH(surround); i++) {
l = strlen(surround[i].search); l = strlen(surround[i].search);
if (end - begin < 2 * l || if(end - begin < 2*l || strncmp(begin, surround[i].search, l) != 0)
strncmp(begin, surround[i].search, l) != 0)
continue; continue;
start = begin + l; start = begin + l;
p = start - 1; p = start - 1;
do { do {
stop = p; stop = p;
p = strstr(p + 1, surround[i].search); p = strstr(p + 1, surround[i].search);
} while (p && p[-1] == '\\'); } while(p && p[-1] == '\\');
if (p && p[-1] != '\\') if (p && p[-1] != '\\')
stop = p; stop = p;
if (!stop || stop < start || stop >= end) if(!stop || stop < start || stop >= end)
continue; continue;
fputs(surround[i].before, stdout); fputs(surround[i].before, stdout);
@ -612,7 +535,7 @@ int dosurround(const char *begin, const char *end, int newblock)
l++; l++;
} }
if (surround[i].process) if(surround[i].process)
process(start, stop, 0); process(start, stop, 0);
else else
hprint(start, stop); hprint(start, stop);
@ -622,27 +545,23 @@ int dosurround(const char *begin, const char *end, int newblock)
return 0; return 0;
} }
int dounderline(const char *begin, const char *end, int newblock) int
{ dounderline(const char *begin, const char *end, int newblock) {
unsigned int i, j, l; unsigned int i, j, l;
const char *p; const char *p;
if (!newblock) if(!newblock)
return 0; return 0;
p = begin; p = begin;
for (l = 0; p + l + 1 != end && p[l] != '\n'; l++) for(l = 0; p + l + 1 != end && p[l] != '\n'; l++);
;
p += l + 1; p += l + 1;
if (l == 0) if(l == 0)
return 0; return 0;
for (i = 0; i < LENGTH(underline); i++) { for(i = 0; i < LENGTH(underline); i++) {
for (j = 0; p + j != end && p[j] != '\n' && for(j = 0; p + j != end && p[j] != '\n' && p[j] == underline[i].search[0]; j++);
p[j] == underline[i].search[0]; if(j >= l) {
j++)
;
if (j >= l) {
fputs(underline[i].before, stdout); fputs(underline[i].before, stdout);
if (underline[i].process) if(underline[i].process)
process(begin, begin + l, 0); process(begin, begin + l, 0);
else else
hprint(begin, begin + l); hprint(begin, begin + l);
@ -653,143 +572,106 @@ int dounderline(const char *begin, const char *end, int newblock)
return 0; return 0;
} }
void *ereallocz(void *p, size_t size) void *
{ ereallocz(void *p, size_t size) {
void *res; void *res;
if (p) if(p)
res = realloc(p, size); res = realloc(p , size);
else else
res = calloc(1, size); res = calloc(1, size);
if (!res) if(!res)
eprint("fatal: could not malloc() %u bytes\n", size); eprint("fatal: could not malloc() %u bytes\n", size);
return res; return res;
} }
void hprint(const char *begin, const char *end) void
{ hprint(const char *begin, const char *end) {
const char *p; const char *p;
for (p = begin; p != end; p++) { for(p = begin; p != end; p++) {
if (*p == '&') if(*p == '&')
fputs("&amp;", stdout); fputs("&amp;", stdout);
else if (*p == '"') else if(*p == '"')
fputs("&quot;", stdout); fputs("&quot;", stdout);
else if (*p == '>') else if(*p == '>')
fputs("&gt;", stdout); fputs("&gt;", stdout);
else if (*p == '<') else if(*p == '<')
fputs("&lt;", stdout); fputs("&lt;", stdout);
else else
fputc(*p, stdout); fputc(*p, stdout);
} }
} }
int process(const char *begin, const char *end, int newblock) void
{ process(const char *begin, const char *end, int newblock) {
const char *q; const char *p, *q;
const char *p;
int affected; int affected;
unsigned int i; unsigned int i;
for (p = begin; p < end;) { for(p = begin; p < end;) {
if (newblock) if(newblock)
while (*p == '\n') while(*p == '\n')
if (++p == end) if(++p == end)
return 1; return;
affected = 0; affected = 0;
for (i = 0; i < LENGTH(parsers) && !affected; i++) for(i = 0; i < LENGTH(parsers) && !affected; i++)
affected = parsers[i](p, end, newblock); affected = parsers[i](p, end, newblock);
p += abs(affected); p += abs(affected);
if (affected == -1) if(!affected) {
return 0; if(nohtml)
if (!affected) {
if (nohtml)
hprint(p, p + 1); hprint(p, p + 1);
else else
fputc(*p, stdout); fputc(*p, stdout);
p++; p++;
} }
for (q = p; q != end && *q == '\n'; q++) for(q = p; q != end && *q == '\n'; q++);
; if(q == end)
if (q == end) return;
return 1; else if(p[0] == '\n' && p + 1 != end && p[1] == '\n')
else if (p[0] == '\n' && p + 1 != end && p[1] == '\n')
newblock = 1; newblock = 1;
else else
newblock = affected < 0; newblock = affected < 0;
} }
return 1;
} }
int main(int argc, char *argv[]) int
{ main(int argc, char *argv[]) {
char *buffer = NULL; char *buffer = NULL;
const char *path = "STDIN"; int s, i;
int i; unsigned long len, bsize;
int ret = EXIT_SUCCESS; FILE *source = stdin;
for (i = 1; i < argc; i++) { for(i = 1; i < argc; i++) {
if (!strcmp("-v", argv[i])) if(!strcmp("-v", argv[i]))
eprint("simple markup %s (C) Enno Boland\n", VERSION); eprint("simple markup %s (C) Enno Boland\n",VERSION);
else if (!strcmp("-n", argv[i])) else if(!strcmp("-n", argv[i]))
nohtml = 1; nohtml = 1;
else if (argv[i][0] != '-') else if(argv[i][0] != '-')
break; break;
else if (!strcmp("--", argv[i])) { else if(!strcmp("--", argv[i])) {
i++; i++;
break; break;
} else
eprint("Usage %s [-n] [file]\n -n escape html strictly\n",
argv[0]);
} }
else
if (i < argc) { eprint("Usage %s [-n] [file]\n -n escape html strictly\n", argv[0]);
path = argv[i]; }
if(i < argc && !(source = fopen(argv[i], "r")))
off64_t len = get_file_size(path); eprint("Cannot open file `%s`\n",argv[i]);
if (len == -1) { bsize = 2 * BUFSIZ;
eprint("%s: %s: %s\n", argv[0], path, strerror(errno)); buffer = ereallocz(buffer, bsize);
ret = EXIT_FAILURE; len = 0;
goto exit; while((s = fread(buffer + len, 1, BUFSIZ, source))) {
len += s;
if(BUFSIZ + len + 1 > bsize) {
bsize += BUFSIZ;
if(!(buffer = realloc(buffer, bsize)))
eprint("realloc failed.");
} }
buffer = read_file(path, len);
if (!buffer) {
perror(PACKAGE);
ret = EXIT_FAILURE;
goto exit;
} }
buffer[len] = '\0'; buffer[len] = '\0';
process(buffer, buffer + len, 1); process(buffer, buffer + len, 1);
fclose(source);
free(buffer); free(buffer);
} else { return EXIT_SUCCESS;
size_t buffer_size = 1024 * CHARWIDTH;
buffer = calloc(buffer_size + CHARWIDTH, sizeof(char));
if (!buffer) {
perror(PACKAGE);
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) {
perror(PACKAGE);
ret = EXIT_FAILURE;
}
free(buffer);
break;
}
buffer[read_bytes] = '\0';
if (!process(buffer, buffer + read_bytes, 1)) {
ret = EXIT_FAILURE;
free(buffer);
break;
}
}
}
exit:
return ret;
} }

1
smu.h
View file

@ -16,3 +16,4 @@ int smu_convert(FILE *out, FILE *in, int suppresshtml);
/** utility */ /** utility */
void eprint(const char *format, ...); void eprint(const char *format, ...);

View file

@ -47,11 +47,9 @@ list in list:
entity: &, <, > entity: &, <, >
code: code:
```
int powerof2(unsigned int n) { int powerof2(unsigned int n) {
return !((n - 1) & n) && n > 0; return !((n - 1) & n) && n > 0;
} }
```
links links
----- -----