summaryrefslogtreecommitdiff
path: root/input.c
blob: e70c6a7133b58927b46ccdeceb9ad52edece7bb8 (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
82
83
84
85
/*
 * input.c: read the source form
 */

#include <stdio.h>
#include <assert.h>
#include "buttress.h"

static void unget(input *in, char c) {
    assert(in->npushback < INPUT_PUSHBACK_MAX);
    in->pushback[in->npushback++] = c;
}

/*
 * Can return EOF
 */
static int get(input *in) {
    if (in->npushback)
	return (unsigned char)in->pushback[--in->npushback];
    else if (in->currfp) {
	int c = getc(in->currfp);
	if (c == EOF) {
	    fclose(in->currfp);
	    in->currfp = NULL;
	}
	return c;
    } else
	return EOF;
}

/*
 * Lexical analysis of source files.
 */
typedef struct tagToken token;
struct tagToken {
    int type;
    char *text;
};
enum {
    tok_eof,
    tok_eop,			       /* end of paragraph */
    tok_word,			       /* an ordinary word */
    tok_cmd,			       /* \command */
    tok_bracetext		       /* {text} */
};

/*
 * Adds a new paragraph to a linked list
 */
static paragraph addpara(paragraph ***hptrptr) {
    paragraph *newpara = smalloc(sizeof(paragraph));
    newpara->next = NULL;
    **hptrptr = newpara;
    *hptrptr = &newpara->next;
    return newpara;
}

/*
 * Reads a single file (ie until get() returns EOF)
 */
static void read_file(paragraph ***ret, input *in) {
    int c;

    while (1) {
	/*
	 * Get a token.
	 */
	token t = get_token(in);
	if (t.type == tok_eof)
	    return;
	printf("token: %d\n", t.type);
    }
}

paragraph *read_input(input *in) {
    paragraph *head = NULL;
    paragraph **hptr = &head;

    while (in->currindex < in->nfiles) {
	in->currfp = fopen(in->filenames[in->currindex], "r");
	if (in->currfp)
	    read_file(&hptr, in);
	in->currindex++;
    }
}