344 lines
No EOL
8.9 KiB
C
344 lines
No EOL
8.9 KiB
C
#include <errno.h> // errno
|
|
#include <stddef.h> // NULL, size_t
|
|
#include <stdio.h> // fclose
|
|
#include <stdlib.h> // calloc, free
|
|
#include <string.h> // memcpy
|
|
|
|
#include "gemtext-parser.h"
|
|
|
|
gemtextParser* gemtextParserInit(FILE *stream) {
|
|
gemtextParser *parser = calloc(1, sizeof(gemtextParser));
|
|
if (parser == NULL)
|
|
return NULL;
|
|
parser->stream = stream;
|
|
parser->mode = normalMode;
|
|
parser->state = lineStart;
|
|
return parser;
|
|
}
|
|
|
|
void gemtextParserDeinit(gemtextParser *parser) {
|
|
fclose(parser->stream);
|
|
free(parser);
|
|
}
|
|
|
|
int gemtextLineQueueInit(gemtextLineQueue *queue) {
|
|
int ret;
|
|
|
|
queue->head = NULL;
|
|
queue->tail = NULL;
|
|
ret = pthread_mutex_init(&queue->mutex, NULL);
|
|
if (ret != 0)
|
|
return ret;
|
|
return pthread_cond_init(&queue->cond, NULL);
|
|
}
|
|
|
|
void gemtextLineQueuePush(gemtextLineQueue *queue, gemtextLine *line) {
|
|
pthread_mutex_lock(&queue->mutex);
|
|
if (queue->tail == NULL) {
|
|
queue->tail = queue->head = line;
|
|
} else {
|
|
line->next = queue->tail;
|
|
queue->tail->prev = line;
|
|
queue->tail = line;
|
|
}
|
|
queue->count++;
|
|
pthread_mutex_unlock(&queue->mutex);
|
|
}
|
|
|
|
gemtextLine* gemtextLineQueuePop(gemtextLineQueue *lq) {
|
|
gemtextLine *line;
|
|
|
|
while (lq->count == 0)
|
|
pthread_cond_wait(&lq->cond, &lq->mutex);
|
|
pthread_mutex_lock(&lq->mutex);
|
|
lq->count++;
|
|
line = lq->head;
|
|
if (line->lineType == endOfStream)
|
|
return line;
|
|
if (lq->tail == lq->head) {
|
|
lq->tail = lq->head = NULL;
|
|
} else {
|
|
lq->head = lq->head->prev;
|
|
}
|
|
pthread_mutex_unlock(&lq->mutex);
|
|
line->prev = line->next = NULL;
|
|
return line;
|
|
}
|
|
|
|
lineBuffer* lineBufferNew() {
|
|
char *buf;
|
|
lineBuffer *lb = calloc(1, sizeof(lineBuffer));
|
|
|
|
if (lb == NULL) return NULL;
|
|
lb->capacity = LBUF_SIZE;
|
|
lb->len = 0;
|
|
buf = calloc(1, LBUF_SIZE);
|
|
if (buf == NULL) {
|
|
free(lb);
|
|
return NULL;
|
|
}
|
|
lb->buf = buf;
|
|
lb->cursor = lb->buf;
|
|
return lb;
|
|
}
|
|
|
|
int lineBufferInit(lineBuffer *lb) {
|
|
char *buf = calloc(1, LBUF_SIZE);
|
|
if (buf == NULL) return 2;
|
|
lb->len = 0;
|
|
lb->capacity = LBUF_SIZE;
|
|
lb->buf = buf;
|
|
lb->cursor = buf;
|
|
return 0;
|
|
}
|
|
|
|
void lineBufferDeinit(lineBuffer *lb) {
|
|
free(lb->buf);
|
|
free(lb);
|
|
}
|
|
|
|
int lineBufferExtend(lineBuffer *lb, size_t len) {
|
|
char *buf = calloc(1, lb->capacity + len);
|
|
if (buf == NULL) return 2;
|
|
memcpy(buf, lb->buf, lb->len);
|
|
free(lb->buf);
|
|
lb->buf = buf;
|
|
lb->cursor = buf + lb->len;
|
|
return 0;
|
|
}
|
|
|
|
int lineBufferAppendChar(lineBuffer *lb, char c) {
|
|
int ret = 0;
|
|
|
|
if (lb->len >= lb->capacity - 1) {
|
|
ret = lineBufferExtend(lb, LBUF_SIZE);
|
|
if (ret != 0) return ret;
|
|
}
|
|
*lb->cursor = c;
|
|
lb->cursor++;
|
|
lb->len++;
|
|
return ret;
|
|
}
|
|
|
|
void lineBufferAppendCharUnchecked(lineBuffer *lb, char c) {
|
|
*lb->cursor = c;
|
|
lb->cursor++;
|
|
lb->len++;
|
|
}
|
|
|
|
int lineBufferAppendString(lineBuffer *lb, char *c, size_t len) {
|
|
int ret = 0, i = 0;
|
|
size_t rem = 0;
|
|
|
|
// Find the remaining length
|
|
rem = lb->capacity - lb->len;
|
|
// if the length won't fit our string, extend the buffer.
|
|
// We do len - rem + LBUF_SIZE for a safety margin
|
|
if (rem < len) {
|
|
ret = lineBufferExtend(lb, len - rem + LBUF_SIZE);
|
|
if (ret != 0) return ret;
|
|
}
|
|
|
|
for (i = 0; i < len; i++) {
|
|
// We use 'unchecked' because we did our checks above
|
|
lineBufferAppendCharUnchecked(lb, *c);
|
|
c++;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void lineBufferReset(lineBuffer *lb) {
|
|
lb->len = 0;
|
|
lb->cursor = lb->buf;
|
|
}
|
|
|
|
gemtextLink* readLink(FILE *stream, lineBuffer *lb) {
|
|
char c;
|
|
char *buf;
|
|
int ret = 0;
|
|
gemtextLink *link = calloc(1, sizeof(gemtextLink));
|
|
|
|
if (link == NULL) return NULL;
|
|
while (1) {
|
|
fread(&c, 1, 1, stream);
|
|
switch (c) {
|
|
case ' ':
|
|
case '\t':
|
|
if (lb->len == 0)
|
|
continue;
|
|
buf = strndup(lb->buf, lb->len);
|
|
link->url = buf;
|
|
break;
|
|
case '\n':
|
|
if (lb->len == 0) {
|
|
free(link);
|
|
return NULL;
|
|
}
|
|
buf = strndup(lb->buf, lb->len);
|
|
link->url = buf;
|
|
return link;
|
|
default:
|
|
ret = lineBufferAppendChar(lb, c);
|
|
if (ret != 0) {
|
|
free(link);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
lineBufferReset(lb);
|
|
while (1) {
|
|
fread(&c, 1, 1, stream);
|
|
switch (c) {
|
|
case '\n':
|
|
link->display = strndup(lb->buf, lb->len);
|
|
break;
|
|
default:
|
|
ret = lineBufferAppendChar(lb, c);
|
|
if (ret != 0) {
|
|
free(link->url);
|
|
free(link);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
return link;
|
|
}
|
|
|
|
int parsePreformatted(gemtextParser *parser, gemtextLineQueue *lq) {
|
|
// todo
|
|
return 0;
|
|
}
|
|
|
|
int parseQuote(gemtextParser *parser, gemtextLineQueue *lq) {
|
|
// todo
|
|
return 0;
|
|
}
|
|
|
|
int parseNormal(gemtextParser *parser, gemtextLineQueue *lq) {
|
|
char c;
|
|
int ret;
|
|
gemtextLine *line;
|
|
gemtextLink *link;
|
|
lineBuffer lb;
|
|
char *buf;
|
|
|
|
ret = lineBufferInit(&lb);
|
|
if (ret != 0) return ret;
|
|
while (1) {
|
|
ret = fread(&c, 1, 1, parser->stream);
|
|
if (c != 1) {
|
|
line = calloc(1, sizeof(gemtextLine));
|
|
if (line == NULL) return errno;
|
|
line->lineType = endOfStream;
|
|
line->prev = line->next = NULL;
|
|
line->str = NULL;
|
|
gemtextLineQueuePush(lq, line);
|
|
}
|
|
switch (parser->state) {
|
|
case lineStart:
|
|
switch (c) {
|
|
case '=':
|
|
ret = lineBufferAppendChar(&lb, c);
|
|
if (ret != 0) {
|
|
free(lb.buf);
|
|
return ret;
|
|
}
|
|
parser->state = firstLinkChar;
|
|
break;
|
|
case '>':
|
|
parser->mode = quoteMode;
|
|
ret = parseQuote(parser, lq);
|
|
if (ret != 0) {
|
|
free(lb.buf);
|
|
return ret;
|
|
}
|
|
break;
|
|
case '#':
|
|
ret = lineBufferAppendChar(&lb, c);
|
|
if (ret != 0) {
|
|
free(lb.buf);
|
|
return ret;
|
|
}
|
|
parser->state = firstHashChar;
|
|
break;
|
|
case '`':
|
|
ret = lineBufferAppendChar(&lb, c);
|
|
if (ret != 0) {
|
|
free(lb.buf);
|
|
return ret;
|
|
}
|
|
parser->state = firstBacktickChar;
|
|
break;
|
|
case '\n':
|
|
line = calloc(1, sizeof(gemtextLine));
|
|
if (line == NULL) return errno;
|
|
line->lineType = normalLine;
|
|
buf = strndup("\n", 1);
|
|
if (buf == NULL) {
|
|
free(lb.buf);
|
|
return errno;
|
|
}
|
|
line->str = buf;
|
|
gemtextLineQueuePush(lq, line);
|
|
break;
|
|
default:
|
|
ret = lineBufferAppendChar(&lb, c);
|
|
if (ret != 0) {
|
|
free(lb.buf);
|
|
return ret;
|
|
}
|
|
parser->state = normalState;
|
|
break;
|
|
}
|
|
break;
|
|
case firstLinkChar:
|
|
ret = lineBufferAppendChar(&lb, c);
|
|
if (ret != 0) {
|
|
free(lb.buf);
|
|
return ret;
|
|
}
|
|
if (c == '>') {
|
|
parser->state = secondLinkChar;
|
|
} else if (c == '\n') {
|
|
// todo - send a 'normal' line to the lineQueue and reset the
|
|
// lineBuffer
|
|
} else {
|
|
parser->state = normalState;
|
|
}
|
|
case secondLinkChar:
|
|
line = calloc(1, sizeof(gemtextLine));
|
|
if (line == NULL) return errno;
|
|
link = readLink(parser->stream, &lb);
|
|
if (link == NULL) {
|
|
line->lineType = normalLine;
|
|
buf = strndup(lb.buf, lb.len);
|
|
if (buf == NULL) {
|
|
free(lb.buf);
|
|
free(line);
|
|
return errno;
|
|
}
|
|
line->str = buf;
|
|
gemtextLineQueuePush(lq, line);
|
|
} else {
|
|
line->lineType = linkLine;
|
|
line->link = link;
|
|
gemtextLineQueuePush(lq, line);
|
|
}
|
|
lineBufferReset(&lb);
|
|
break;
|
|
case firstHashChar:
|
|
case secondHashChar:
|
|
case thirdHashChar:
|
|
case firstBacktickChar:
|
|
case secondBacktickChar:
|
|
case thirdBacktickChar:
|
|
case normalState:
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int parseGemtext(gemtextParser *parser, gemtextLineQueue *lq) {
|
|
// todo
|
|
return 0;
|
|
} |