Compare commits

..

10 commits

6 changed files with 435 additions and 322 deletions

View file

@ -1,12 +1,17 @@
# libsmu - simple markup
# (c) 2007, 2008 Enno Boland
include config.mk
VERSION = 1.5
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
OBJ = ${SRC:.c=.o}
all: options smu
all: smu
options:
@echo smu build options:
@ -14,42 +19,51 @@ options:
@echo "LDFLAGS = ${LDFLAGS}"
@echo "CC = ${CC}"
.c.o:
@echo CC $<
@${CC} -c ${CFLAGS} $<
smu: ${SRC}
@echo CC $@
@${CC} -o $@ ${SRC} ${CFLAGS} ${LDFLAGS}
${OBJ}: config.mk
pgo-build: ${SRC}
@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:
@echo cleaning
@rm -f smu ${OBJ} ${LIBOBJ} smu-${VERSION}.tar.gz
rm -f smu ${OBJ} ${LIBOBJ} smu-${VERSION}.tar.gz
dist: clean
@echo creating dist tarball
@mkdir -p smu-${VERSION}
@cp -R LICENSE Makefile config.mk smu.1 ${SRC} smu-${VERSION}
@tar -cf smu-${VERSION}.tar smu-${VERSION}
@gzip smu-${VERSION}.tar
@rm -rf smu-${VERSION}
mkdir -p smu-${VERSION}
cp -R LICENSE Makefile config.mk smu.1 ${SRC} smu-${VERSION}
tar -cf smu-${VERSION}.tar smu-${VERSION}
gzip smu-${VERSION}.tar
rm -rf smu-${VERSION}
install: all
@echo installing executable file to ${DESTDIR}${PREFIX}/bin
@mkdir -p ${DESTDIR}${PREFIX}/bin
@cp -f smu ${DESTDIR}${PREFIX}/bin
@chmod 755 ${DESTDIR}${PREFIX}/bin/smu
@echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
@mkdir -p ${DESTDIR}${MANPREFIX}/man1
@sed "s/VERSION/${VERSION}/g" < smu.1 > ${DESTDIR}${MANPREFIX}/man1/smu.1
@chmod 644 ${DESTDIR}${MANPREFIX}/man1/smu.1
mkdir -p ${DESTDIR}${PREFIX}/bin
cp -f smu ${DESTDIR}${PREFIX}/bin
chmod 755 ${DESTDIR}${PREFIX}/bin/smu
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:
@echo removing executable file from ${DESTDIR}${PREFIX}/bin
@rm -f ${DESTDIR}${PREFIX}/bin/smu
@echo removing manual page from ${DESTDIR}${MANPREFIX}/man1
@rm -f ${DESTDIR}${MANPREFIX}/man1/smu.1
rm -f ${DESTDIR}${PREFIX}/bin/smu
rm -f ${DESTDIR}${MANPREFIX}/man1/smu.1
.PHONY: all options clean dist install uninstall
.PHONY: all options clean dist install uninstall pgo-build

View file

@ -1,20 +0,0 @@
# 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
escapes all HTML Tags.
.SH BUGS
Please report any Bugs to https://github.com/Gottox/smu/issues or via mail.
Report any bugs to tenno+smu@suij.in

386
smu.c
View file

@ -1,48 +1,78 @@
/* smu - simple markup
* Copyright (C) <2007, 2008> Enno Boland <g s01 de>
* Copyright (C) 2025 Enno Tensing <tenno+smu@suij.in>
*
* See LICENSE for further informations
*/
#define _LARGEFILE64_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#ifndef PACKAGE
#define PACKAGE "smu"
#endif
#ifndef VERSION
#define VERSION "0.0"
#endif
#define CHARWIDTH 4
#define LENGTH(x) sizeof(x) / sizeof(x[0])
#define ADDC(b,i) if(i % BUFSIZ == 0) { b = realloc(b, (i + BUFSIZ) * sizeof(char)); if(!b) eprint("Malloc failed."); } b[i]
#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 struct {
struct tag {
char *search;
int process;
char *before, *after;
} Tag;
char *before;
char *after;
};
static int doamp(const char *begin, const char *end, int newblock); /* Parser for & */
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 dohtml(const char *begin, const char *end, int newblock); /* Parser for html */
static int dolineprefix(const char *begin, const char *end, int newblock);/* Parser for line prefix tags */
static int dolink(const char *begin, const char *end, int newblock); /* Parser for links and images */
static int dolist(const char *begin, const char *end, int newblock); /* Parser for lists */
static int doparagraph(const char *begin, const char *end, int newblock); /* Parser for paragraphs */
static int doreplace(const char *begin, const char *end, int newblock); /* Parser for simple replaces */
static int doshortlink(const char *begin, const char *end, int newblock); /* Parser for links and images */
static int dosurround(const char *begin, const char *end, int newblock); /* Parser for surrounding tags */
static int dounderline(const char *begin, const char *end, int newblock); /* Parser for underline tags */
off64_t get_file_size(const char *);
char *read_file(const char *, off64_t);
static int doamp(const char *begin, const char *end, int newblock);
static int docomment(const char *begin, const char *end, int newblock);
static int dogtlt(const char *begin, const char *end, int newblock);
static int dohtml(const char *begin, const char *end, int newblock);
static int dolineprefix(const char *begin, const char *end, int newblock);
static int dolink(const char *begin, const char *end, int newblock);
static int dolist(const char *begin, const char *end, int newblock);
static int doparagraph(const char *begin, const char *end, int newblock);
static int doreplace(const char *begin, const char *end, int newblock);
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 eprint(const char *format, ...);
static void hprint(const char *begin, const char *end); /* escapes HTML and prints it to output */
static void process(const char *begin, const char *end, int isblock); /* Processes range between begin and end. */
static void hprint(const char *begin, const char *end);
static int process(const char *begin, const char *end, int isblock);
/* list of parsers */
static Parser parsers[] = { dounderline, docomment, dolineprefix,
dolist, doparagraph, dogtlt, dosurround, dolink,
static Parser parsers[] = { dounderline, docomment, dolineprefix, dolist,
doparagraph, dogtlt, dosurround, dolink,
doshortlink, dohtml, doamp, doreplace };
static int nohtml = 0;
static Tag lineprefix[] = {
static struct tag lineprefix[] = {
{ " ", 0, "<pre><code>", "\n</code></pre>" },
{ "\t", 0, "<pre><code>", "\n</code></pre>" },
{ ">", 2, "<blockquote>", "</blockquote>" },
@ -55,12 +85,13 @@ static Tag lineprefix[] = {
{ "- - -\n", 1, "<hr />", "" },
};
static Tag underline[] = {
static struct tag underline[] = {
{ "=", 1, "<h1>", "</h1>\n" },
{ "-", 1, "<h2>", "</h2>\n" },
};
static Tag surround[] = {
static struct tag surround[] = {
{ "```", 0, "<code>", "</code>" },
{ "``", 0, "<code>", "</code>" },
{ "`", 0, "<code>", "</code>" },
{ "___", 1, "<strong><em>", "</em></strong>" },
@ -72,45 +103,70 @@ static Tag surround[] = {
};
static const char *replace[][2] = {
{ "\\\\", "\\" },
{ "\\`", "`" },
{ "\\*", "*" },
{ "\\_", "_" },
{ "\\{", "{" },
{ "\\}", "}" },
{ "\\[", "[" },
{ "\\]", "]" },
{ "\\(", "(" },
{ "\\)", ")" },
{ "\\#", "#" },
{ "\\+", "+" },
{ "\\-", "-" },
{ "\\.", "." },
{ "\\!", "!" },
{ "\\\\", "\\" }, { "\\`", "`" }, { "\\*", "*" }, { "\\_", "_" },
{ "\\{", "{" }, { "\\}", "}" }, { "\\[", "[" }, { "\\]", "]" },
{ "\\(", "(" }, { "\\)", ")" }, { "\\#", "#" }, { "\\+", "+" },
{ "\\-", "-" }, { "\\.", "." }, { "\\!", "!" },
};
static const char *insert[][2] = {
{ " \n", "<br />" },
};
void
eprint(const char *format, ...) {
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;
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_start(ap, format);
vfprintf(stderr, format, 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;
if (*begin != '&')
return 0;
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 == ';')
return 0;
}
@ -118,8 +174,8 @@ doamp(const char *begin, const char *end, int newblock) {
return 1;
}
int
dogtlt(const char *begin, const char *end, int newblock) {
int dogtlt(const char *begin, const char *end, int newblock)
{
int brpos;
char c;
@ -132,16 +188,16 @@ dogtlt(const char *begin, const char *end, int newblock) {
if (!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) {
fputs("&lt;", stdout);
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);
return 2;
}
return 0;
}
int
docomment(const char *begin, const char *end, int newblock) {
int docomment(const char *begin, const char *end, int newblock)
{
char *p;
if (nohtml || strncmp("<!--", begin, 4))
@ -153,8 +209,8 @@ docomment(const char *begin, const char *end, int newblock) {
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;
if (nohtml || begin + 2 >= end)
@ -164,15 +220,18 @@ dohtml(const char *begin, const char *end, int newblock) {
return 0;
p++;
tag = p;
for(; isalnum(*p) && p < end; p++);
for (; isalnum(*p) && p < end; p++)
;
tagend = p;
if (p > end || tag == tagend)
return 0;
while ((p = strstr(p, "</")) && p < end) {
p += 2;
if(strncmp(p, tag, tagend - tag) == 0 && p[tagend - tag] == '>') {
if (strncmp(p, tag, tagend - tag) == 0 &&
p[tagend - tag] == '>') {
p++;
fwrite(begin, sizeof(char), p - begin + tagend - tag - 1, stdout);
fwrite(begin, sizeof(char),
p - begin + tagend - tag - 1, stdout);
return p - begin + tagend - tag - 1;
}
}
@ -180,13 +239,12 @@ dohtml(const char *begin, const char *end, int newblock) {
if (p) {
fwrite(begin, sizeof(char), p - begin + 1, stdout);
return p - begin + 1;
}
else
} else
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;
char *buffer;
const char *p;
@ -216,7 +274,8 @@ dolineprefix(const char *begin, const char *end, int newblock) {
/* Collect lines into buffer while they start with the prefix */
j = 0;
while((strncmp(lineprefix[i].search, p, l) == 0) && p + l < end) {
while ((strncmp(lineprefix[i].search, p, l) == 0) &&
p + l < end) {
p += l;
/* Special case for blockquotes: optional space after > */
@ -225,7 +284,7 @@ dolineprefix(const char *begin, const char *end, int newblock) {
}
while (p < end) {
ADDC(buffer, j) = *p;
ADDC(buffer, j, *p);
j++;
if (*(p++) == '\n')
break;
@ -237,9 +296,10 @@ dolineprefix(const char *begin, const char *end, int newblock) {
j--;
}
ADDC(buffer, j) = '\0';
ADDC(buffer, j, '\0');
if (lineprefix[i].process)
process(buffer, buffer + strlen(buffer), lineprefix[i].process >= 2);
process(buffer, buffer + strlen(buffer),
lineprefix[i].process >= 2);
else
hprint(buffer, buffer + strlen(buffer));
puts(lineprefix[i].after);
@ -249,8 +309,8 @@ dolineprefix(const char *begin, const char *end, int newblock) {
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;
const char *desc, *link, *p, *q, *descend, *linkend;
const char *title = NULL, *titleend = NULL;
@ -264,7 +324,8 @@ dolink(const char *begin, const char *end, int newblock) {
p = desc = begin + 1 + img;
if (!(p = strstr(desc, "](")) || p > end)
return 0;
for(q = strstr(desc, "!["); q && q < end && q < p; q = strstr(q + 1, "!["))
for (q = strstr(desc, "!["); q && q < end && q < p;
q = strstr(q + 1, "!["))
if (!(p = strstr(p + 1, "](")) || p > end)
return 0;
descend = p;
@ -287,13 +348,16 @@ dolink(const char *begin, const char *end, int newblock) {
sep = p[0]; /* separator: can be " or ' */
title = p + 1;
/* strip trailing whitespace */
for(linkend = p; linkend > link && isspace(*(linkend - 1)); linkend--);
for(titleend = q - 1; titleend > title && isspace(*(titleend)); titleend--);
for (linkend = p; linkend > link && isspace(*(linkend - 1));
linkend--)
;
for (titleend = q - 1; titleend > title && isspace(*(titleend));
titleend--)
;
if (*titleend != sep) {
return 0;
}
}
else {
} else {
linkend = q;
}
@ -316,8 +380,7 @@ dolink(const char *begin, const char *end, int newblock) {
fputs("\" ", stdout);
}
fputs("/>", stdout);
}
else {
} else {
fputs("<a href=\"", stdout);
hprint(link, linkend);
fputs("\"", stdout);
@ -333,8 +396,8 @@ dolink(const char *begin, const char *end, int newblock) {
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;
const char *p, *q;
char *buffer = NULL;
@ -353,14 +416,16 @@ dolist(const char *begin, const char *end, int newblock) {
marker = *p;
} else {
ul = 0;
for(; p < end && *p >= '0' && *p <= '9'; p++);
for (; p < end && *p >= '0' && *p <= '9'; p++)
;
if (p >= end || *p != '.')
return 0;
}
p++;
if (p >= end || !(*p == ' ' || *p == '\t'))
return 0;
for(p++; p != end && (*p == ' ' || *p == '\t'); p++);
for (p++; p != end && (*p == ' ' || *p == '\t'); p++)
;
indent = p - q;
buffer = ereallocz(buffer, BUFSIZ);
if (!newblock)
@ -374,9 +439,13 @@ dolist(const char *begin, const char *end, int newblock) {
break;
else {
/* Handle empty lines */
for(q = p + 1; (*q == ' ' || *q == '\t') && q < end; q++);
for (q = p + 1;
(*q == ' ' || *q == '\t') &&
q < end;
q++)
;
if (*q == '\n') {
ADDC(buffer, i) = '\n';
ADDC(buffer, i, '\0');
i++;
run = 0;
isblock++;
@ -388,7 +457,10 @@ dolist(const char *begin, const char *end, int newblock) {
if (ul && *q == marker)
j = 1;
else if (!ul) {
for(; q + j != end && q[j] >= '0' && q[j] <= '9' && j < indent; j++);
for (; q + j != end && q[j] >= '0' &&
q[j] <= '9' && j < indent;
j++)
;
if (q + j == end)
break;
if (j > 0 && q[j] == '.')
@ -397,9 +469,12 @@ dolist(const char *begin, const char *end, int newblock) {
j = 0;
}
if (q + indent < end)
for(; (q[j] == ' ' || q[j] == '\t') && j < indent; j++);
for (; (q[j] == ' ' || q[j] == '\t') &&
j < indent;
j++)
;
if (j == indent) {
ADDC(buffer, i) = '\n';
ADDC(buffer, i, '\n');
i++;
p += indent;
run = 1;
@ -407,26 +482,27 @@ dolist(const char *begin, const char *end, int newblock) {
p++;
else
break;
}
else if (j < indent)
} else if (j < indent)
run = 0;
}
ADDC(buffer, i) = *p;
ADDC(buffer, i, *p);
}
ADDC(buffer, i) = '\0';
ADDC(buffer, i, '\0');
fputs("<li>", stdout);
process(buffer, buffer + i, isblock > 1 || (isblock == 1 && run));
process(buffer, buffer + i,
isblock > 1 || (isblock == 1 && run));
fputs("</li>\n", stdout);
}
fputs(ul ? "</ul>\n" : "</ol>\n", stdout);
free(buffer);
p--;
while(*(--p) == '\n');
while (*(--p) == '\n')
;
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;
if (!newblock)
@ -442,8 +518,8 @@ doparagraph(const char *begin, const char *end, int newblock) {
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;
for (i = 0; i < LENGTH(insert); i++)
@ -461,8 +537,8 @@ doreplace(const char *begin, const char *end, int newblock) {
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;
int ismail = 0;
@ -488,14 +564,14 @@ doshortlink(const char *begin, const char *end, int newblock) {
fputs("<a href=\"", stdout);
if (ismail == 1) {
/* mailto: */
fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:", stdout);
fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:",
stdout);
for (c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c);
fputs("\">", stdout);
for (c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c);
}
else {
} else {
hprint(begin + 1, p);
fputs("\">", stdout);
hprint(begin + 1, p);
@ -507,14 +583,15 @@ doshortlink(const char *begin, const char *end, int newblock) {
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;
const char *p, *start, *stop;
for (i = 0; i < LENGTH(surround); i++) {
l = strlen(surround[i].search);
if(end - begin < 2*l || strncmp(begin, surround[i].search, l) != 0)
if (end - begin < 2 * l ||
strncmp(begin, surround[i].search, l) != 0)
continue;
start = begin + l;
p = start - 1;
@ -545,20 +622,24 @@ dosurround(const char *begin, const char *end, int newblock) {
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;
const char *p;
if (!newblock)
return 0;
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;
if (l == 0)
return 0;
for (i = 0; i < LENGTH(underline); i++) {
for(j = 0; p + j != end && p[j] != '\n' && p[j] == underline[i].search[0]; j++);
for (j = 0; p + j != end && p[j] != '\n' &&
p[j] == underline[i].search[0];
j++)
;
if (j >= l) {
fputs(underline[i].before, stdout);
if (underline[i].process)
@ -572,8 +653,8 @@ dounderline(const char *begin, const char *end, int newblock) {
return 0;
}
void *
ereallocz(void *p, size_t size) {
void *ereallocz(void *p, size_t size)
{
void *res;
if (p)
res = realloc(p, size);
@ -585,8 +666,8 @@ ereallocz(void *p, size_t size) {
return res;
}
void
hprint(const char *begin, const char *end) {
void hprint(const char *begin, const char *end)
{
const char *p;
for (p = begin; p != end; p++) {
@ -603,9 +684,10 @@ hprint(const char *begin, const char *end) {
}
}
void
process(const char *begin, const char *end, int newblock) {
const char *p, *q;
int process(const char *begin, const char *end, int newblock)
{
const char *q;
const char *p;
int affected;
unsigned int i;
@ -613,11 +695,13 @@ process(const char *begin, const char *end, int newblock) {
if (newblock)
while (*p == '\n')
if (++p == end)
return;
return 1;
affected = 0;
for (i = 0; i < LENGTH(parsers) && !affected; i++)
affected = parsers[i](p, end, newblock);
p += abs(affected);
if (affected == -1)
return 0;
if (!affected) {
if (nohtml)
hprint(p, p + 1);
@ -625,22 +709,24 @@ process(const char *begin, const char *end, int newblock) {
fputc(*p, stdout);
p++;
}
for(q = p; q != end && *q == '\n'; q++);
for (q = p; q != end && *q == '\n'; q++)
;
if (q == end)
return;
return 1;
else if (p[0] == '\n' && p + 1 != end && p[1] == '\n')
newblock = 1;
else
newblock = affected < 0;
}
return 1;
}
int
main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
char *buffer = NULL;
int s, i;
unsigned long len, bsize;
FILE *source = stdin;
const char *path = "STDIN";
int i;
int ret = EXIT_SUCCESS;
for (i = 1; i < argc; i++) {
if (!strcmp("-v", argv[i]))
@ -652,26 +738,58 @@ main(int argc, char *argv[]) {
else if (!strcmp("--", argv[i])) {
i++;
break;
} else
eprint("Usage %s [-n] [file]\n -n escape html strictly\n",
argv[0]);
}
else
eprint("Usage %s [-n] [file]\n -n escape html strictly\n", argv[0]);
}
if(i < argc && !(source = fopen(argv[i], "r")))
eprint("Cannot open file `%s`\n",argv[i]);
bsize = 2 * BUFSIZ;
buffer = ereallocz(buffer, bsize);
len = 0;
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.");
if (i < argc) {
path = argv[i];
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) {
perror(PACKAGE);
ret = EXIT_FAILURE;
goto exit;
}
buffer[len] = '\0';
process(buffer, buffer + len, 1);
fclose(source);
free(buffer);
return EXIT_SUCCESS;
} else {
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,4 +16,3 @@ int smu_convert(FILE *out, FILE *in, int suppresshtml);
/** utility */
void eprint(const char *format, ...);

View file

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