aboutsummaryrefslogtreecommitdiff
path: root/badroff.c
blob: 8cf89619b4e3ebd85e770fcc81ea0b13105823d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include "sb.h"
#include "buf.h"

typedef enum {
    false,
    true
} bool;

FILE* getfile(int argc, char** argv){
    if (argc >= 2)
        return fopen(argv[1], "r");
    else
        return stdin;
}

FILE* in;
buf* inbuf;

int width;
bool brk;


size_t fillbuf(size_t len){ // returns real len
    int c;
    size_t set;
    for (set = buflen(inbuf); set < len && (c = fgetc(in)) != EOF;set++)
        inschrbuf(inbuf, c);
    return set;
}

size_t nlfill(){
    size_t line;
    size_t len = buflen(inbuf);
    char* str = peekstrbuf(inbuf, 0, len);
    for (line = 0; line < len; line++)
        if (str[line] == '\n') return line+1;
    for (int c = 0; c != '\n' && (c = fgetc(in)) != EOF; line++)
        inschrbuf(inbuf, c);
    return line;
}

void cmd(void){
    int n = nlfill();
    popstrbuf(inbuf, n);
}

char* typeset(void){
    return popstrbuf(inbuf, nlfill());
}

char* line(void){
    size_t sz;
    if ( (sz = fillbuf(2)) == 0)
        return "";
    char* twobytes = peekstrbuf(inbuf, 0, 2); // .., .\n, or ^.?
    if (sz == 1) return typeset();
    if (twobytes[0] == '.' && twobytes[1] == '\n') return typeset();
    if (twobytes[0] == '.') popchrbuf(inbuf);
    if (twobytes[0] == '.' && twobytes[1] != '.'){
        cmd();
        return line();
    } else {
        return typeset();
    }
}

int main(int argc, char** argv){
    int c;
    in = getfile(argc, argv);
    inbuf = newbuf(256);
    if (in == NULL){
        perror(argv[1]);
        return 1;
    }
    char* out;
    while ( (out = line())[0] != '\0'){
        printf("%s",out);
    }
    fclose(in);
}