Skip to content

Commit

Permalink
Initial implementation. Has problem on x86-64 when compiled without -O2
Browse files Browse the repository at this point in the history
Import files from discount-1.2.6. The library works fine compiled using the
rockspec on my 32 bit gcc 4.1.2 system. But when compiled on an x86-64 box
(gcc 4.3.1) without -O2, calling the function exported by ldiscount just
causes the program to acquire memory until it dies with a segmentation fault.
It appears to work fine when built with -O2 in the CFLAGS (as in the included
Makefile).
  • Loading branch information
asb committed Jul 25, 2008
1 parent 6d89fc8 commit 32a5740
Show file tree
Hide file tree
Showing 14 changed files with 3,154 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Makefile
@@ -0,0 +1,32 @@
VERSION= 0.1

# change these to reflect your Lua installation
LUA= /usr
LUAINC= $(LUA)/include
LUALIB= $(LUA)/lib
LUABIN= $(LUA)/bin

# probably no need to change anything below here
CC= gcc
CFLAGS= $(INCS) $(WARN) -O2 -fPIC
WARN= -Wall
INCS= -I$(LUAINC)

DISCOUNT_OBJS = docheader.o \
dumptree.o \
generate.o \
markdown.o \
mkdio.o \
resource.o
OBJS= $(DISCOUNT_OBJS) ldiscount.o
SOS= discount.so

all: $(SOS)

$(SOS): $(OBJS)
$(CC) -o $@ -shared $(OBJS) $(LIBS)

.PHONY: clean test distr
clean:
rm -f $(OBJS) $(SOS) core core.* a.out

29 changes: 29 additions & 0 deletions amalloc.h
@@ -0,0 +1,29 @@
/*
* debugging malloc()/realloc()/calloc()/free() that attempts
* to keep track of just what's been allocated today.
*/
#ifndef AMALLOC_D
#define AMALLOC_D

#include "config.h"

#ifdef USE_AMALLOC

extern void *amalloc(int);
extern void *acalloc(int,int);
extern void *arealloc(void*,int);
extern void afree(void*);
extern void adump();

#define malloc amalloc
#define calloc acalloc
#define realloc arealloc
#define free afree

#else

#define adump() (void)1

#endif

#endif/*AMALLOC_D*/
5 changes: 5 additions & 0 deletions config.h
@@ -0,0 +1,5 @@
#undef USE_AMALLOC

#define TABSTOP 4
#define COINTOSS() (random()&1)
#define INITRNG(x) srandom((unsigned int)x)
68 changes: 68 additions & 0 deletions cstring.h
@@ -0,0 +1,68 @@
/* two template types: STRING(t) which defines a pascal-style string
* of element (t) [STRING(char) is the closest to the pascal string],
* and ANCHOR(t) which defines a baseplate that a linked list can be
* built up from. [The linked list /must/ contain a ->next pointer
* for linking the list together with.]
*/
#ifndef _CSTRING_D
#define _CSTRING_D

#include <string.h>
#include <stdlib.h>

#include "amalloc.h"

/* expandable Pascal-style string.
*/
#define STRING(type) struct { type *text; int size, alloc; }

#define CREATE(x) T(x) = (void*)(S(x) = (x).alloc = 0)
#define EXPAND(x) (S(x)++)[(S(x) < (x).alloc) \
? (T(x)) \
: (T(x) = T(x) ? realloc(T(x), sizeof T(x)[0] * ((x).alloc += 100)) \
: malloc(sizeof T(x)[0] * ((x).alloc += 100)) )]

#define DELETE(x) (x).alloc ? (free(T(x)), S(x) = (x).alloc = 0) \
: ( S(x) = 0 )
#define CLIP(t,i,sz) \
( ((i) >= 0) && ((sz) > 0) && (((i)+(sz)) <= S(t)) ) ? \
(memmove(&T(t)[i], &T(t)[i+sz], (S(t)-(i+sz)+1)*sizeof(T(t)[0])), \
S(t) -= (sz)) : -1

#define RESERVE(x, sz) T(x) = ((x).alloc > S(x) + (sz) \
? T(x) \
: T(x) \
? realloc(T(x), sizeof T(x)[0] * ((x).alloc = 100+(sz)+S(x))) \
: malloc(sizeof T(x)[0] * ((x).alloc = 100+(sz)+S(x))))
#define SUFFIX(t,p,sz) \
memcpy(((S(t) += (sz)) - (sz)) + \
(T(t) = T(t) ? realloc(T(t), sizeof T(t)[0] * ((t).alloc += sz)) \
: malloc(sizeof T(t)[0] * ((t).alloc += sz))), \
(p), sizeof(T(t)[0])*(sz))

#define PREFIX(t,p,sz) \
RESERVE( (t), (sz) ); \
if ( S(t) ) { memmove(T(t)+(sz), T(t), S(t)); } \
memcpy( T(t), (p), (sz) ); \
S(t) += (sz)

/* reference-style links (and images) are stored in an array
*/
#define T(x) (x).text
#define S(x) (x).size

/* abstract anchor type that defines a list base
* with a function that attaches an element to
* the end of the list.
*
* the list base field is named .text so that the T()
* macro will work with it.
*/
#define ANCHOR(t) struct { t *text, *end; }

#define ATTACH(t, p) ( (t).text ?( ((t).end->next = (p)), ((t).end = (p)) ) \
:( ((t).text = (t).end = (p)) ) )

typedef STRING(char) Cstring;

#endif/*_CSTRING_D*/
43 changes: 43 additions & 0 deletions docheader.c
@@ -0,0 +1,43 @@
/*
* docheader -- get values from the document header
*
* Copyright (C) 2007 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"

#define afterdle(t) (T((t)->text) + (t)->dle)

char *
mkd_doc_title(Document *doc)
{
if ( doc && doc->headers )
return afterdle(doc->headers);
return 0;
}


char *
mkd_doc_author(Document *doc)
{
if ( doc && doc->headers && doc->headers->next )
return afterdle(doc->headers->next);
return 0;
}


char *
mkd_doc_date(Document *doc)
{
if ( doc && doc->headers && doc->headers->next && doc->headers->next->next )
return afterdle(doc->headers->next->next);
return 0;
}
147 changes: 147 additions & 0 deletions dumptree.c
@@ -0,0 +1,147 @@
/* markdown: a C implementation of John Gruber's Markdown markup language.
*
* Copyright (C) 2007 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include "markdown.h"
#include "cstring.h"
#include "amalloc.h"

struct frame {
int indent;
char c;
};

typedef STRING(struct frame) Stack;

static char *
Pptype(int typ)
{
switch (typ) {
case WHITESPACE: return "whitespace";
case CODE : return "code";
case QUOTE : return "quote";
case MARKUP : return "markup";
case HTML : return "html";
case DL : return "dl";
case UL : return "ul";
case OL : return "ol";
case LISTITEM : return "item";
case HDR : return "header";
case HR : return "HR";
default : return "mystery node!";
}
}

static void
pushpfx(int indent, char c, Stack *sp)
{
struct frame *q = &EXPAND(*sp);

q->indent = indent;
q->c = c;
}


static void
poppfx(Stack *sp)
{
S(*sp)--;
}


static void
changepfx(Stack *sp, char c)
{
char ch;

if ( !S(*sp) ) return;

ch = T(*sp)[S(*sp)-1].c;

if ( ch == '+' || ch == '|' )
T(*sp)[S(*sp)-1].c = c;
}


static void
printpfx(Stack *sp, FILE *f)
{
int i;
char c;

if ( !S(*sp) ) return;

c = T(*sp)[S(*sp)-1].c;

if ( c == '+' || c == '-' ) {
fprintf(f, "--%c", c);
T(*sp)[S(*sp)-1].c = (c == '-') ? ' ' : '|';
}
else
for ( i=0; i < S(*sp); i++ ) {
if ( i )
fprintf(f, " ");
fprintf(f, "%*s%c", T(*sp)[i].indent + 2, " ", T(*sp)[i].c);
if ( T(*sp)[i].c == '`' )
T(*sp)[i].c = ' ';
}
fprintf(f, "--");
}


static void
dumptree(Paragraph *pp, Stack *sp, FILE *f)
{
int count;
Line *p;
int d;
static char *Begin[] = { 0, "P", "center" };

while ( pp ) {
if ( !pp->next )
changepfx(sp, '`');
printpfx(sp, f);

d = fprintf(f, "[%s", Pptype(pp->typ));
if ( pp->align )
d += fprintf(f, ", <%s>", Begin[pp->align]);

for (count=0, p=pp->text; p; ++count, (p = p->next) )
;

if ( count )
d += fprintf(f, ", %d line%s", count, (count==1)?"":"s");

d += fprintf(f, "]");

if ( pp->down ) {
pushpfx(d, pp->down->next ? '+' : '-', sp);
dumptree(pp->down, sp, f);
poppfx(sp);
}
else fputc('\n', f);
pp = pp->next;
}
}


int
mkd_dump(Document *doc, FILE *out, int flags, char *title)
{
Stack stack;

if (mkd_compile(doc, flags) ) {

CREATE(stack);
pushpfx(fprintf(out, "%s", title), doc->code->next ? '+' : '-', &stack);
dumptree(doc->code, &stack, out);
DELETE(stack);

mkd_cleanup(doc);
return 0;
}
return -1;
}

0 comments on commit 32a5740

Please sign in to comment.