summaryrefslogtreecommitdiffstats
path: root/utilities/extractqqz.c
blob: 1fafc6027249fd73a34fee18a1854df20bea31ad (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * extractqqz.c: extract QuickQuiz answers from a latex document.
 *	The resulting answers can be included into an "answers" section
 *	of the document.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 * Copyright (c) 2008-2019 Paul E. McKenney, IBM Corporation.
 * Copyright (c) 2019 Paul E. McKenney, Facebook.
 */

/* Also need to follow \input statements.  Which will require making the */
/*  current_line and current_file vars passed through arglists. */
/* Also need to test various combinations... */

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <limits.h>

char *current_file = NULL;
long current_line = 0;

int ignore_line(FILE *fp, char *buf, int bufsiz)
{
	do {
		if (index(buf, '\n'))
			return 1;
	} while (fgets(buf, bufsiz, fp) != NULL);
	return 0;
}

int output_line(FILE *fp, char *buf, int bufsiz)
{
	do {
		if (fputs(buf, stdout) == EOF) {
			fprintf(stderr, "Output error: \"%s\":%d\n",
				current_file, current_line);
			exit(-1);
		}
		if (index(buf, '\n'))
			return 1;
	} while (fgets(buf, bufsiz, fp) != NULL);
	return 0;
}

int output_line_addanswer(FILE *fp, char *buf, int bufsiz, char *outbuf)
{
	char *abp;
	char *ans = "Answer";
	char *bp;
	char *cbp;
	char *obp;

	cbp = index(buf, '{');
	if (cbp == NULL)
		abort();
	for (bp = buf, obp = outbuf; bp < cbp; bp++, obp++)
		*obp = *bp;
	for (abp = ans; *abp; abp++, obp++)
		*obp = *abp;
	for (; *bp; bp++, obp++)
		*obp = *bp;
	*obp = '\0';
	output_line(fp, outbuf, bufsiz);
}

void output_position()
{
	printf("%% %s:%d\n", current_file, current_line);
}

int sc(char *cp, char *ccp, int bufsiz)
{
	int s = strlen(ccp);

	if (s > bufsiz)
		s = bufsiz;
	return (strncmp(cp, ccp, s));
}

#define STATE_SCANNING	0
#define STATE_QQZ	1

void do_one_file(char *fn)
{
	char buf[PATH_MAX + 50];
	char *cp;
	char *fnp = NULL;
	FILE *fp;
	int lastqqz = 0;
	char outbuf[PATH_MAX + 100];
	int state;

	current_file = fn;
	current_line = 0;
	if ((fp = fopen(fn, "r")) == NULL) {
		fnp = malloc(strlen(fn) + 10);
		strcpy(fnp, fn);
		strcat(fnp, ".tex");
		current_file = fnp;
		if ((fp = fopen(fnp, "r")) == NULL) {
			current_file = fn;
			perror(fn);
			goto outerr;
		}
	}
	state = STATE_SCANNING;
	while ((cp = fgets(buf, sizeof(buf), fp)) != NULL) {
		current_line++;
		if (state == STATE_QQZ) {
			if (sc(buf, "\\QuickQuizEnd", sizeof(buf)) == 0) {
				state = STATE_SCANNING;
				ignore_line(fp, buf, sizeof(buf));
				continue;
			}
			if ((sc(buf, "\\QuickQuiz{", sizeof(buf)) == 0) ||
			    (sc(buf, "\\QuickQuizChapter{", sizeof(buf)) == 0)) {
				fprintf(stderr, "%s:%d: Bad directive\n",
					current_file, current_line);
				goto outerr;
			}
			output_line(fp, buf, sizeof(buf));
			continue;
		}
		/* state == STATE_SCANNING */
		if (sc(buf, "\\QuickQuizChapter{", sizeof(buf)) == 0) {
			output_line_addanswer(fp, buf, sizeof(buf), outbuf);
			continue;
		}
		if (sc(buf, "\\QuickQuiz{", sizeof(buf)) == 0) {
			state = STATE_QQZ;
			lastqqz = current_line;
			output_position();
			output_line_addanswer(fp, buf, sizeof(buf), outbuf);
			continue;
		}
		if (sc(buf, "\\QuickQuizEnd", sizeof(buf)) == 0) {
			fprintf(stderr, "%s:%d: Bad directive\n",
				current_file, current_line);
			goto outerr;
		}
		if (sc(buf, "\\input", sizeof(buf)) == 0) {
			char *opencurly = index(buf, '{');
			char *closecurly = index(buf, '}');
			char *savefn = current_file;
			int saveline = current_line;

			if (opencurly == NULL || closecurly == NULL) {
				fprintf(stderr,
					"%s:%d: Malformed \\input{path}\n",
					current_file, current_line);
				goto outerr;
			}
			*closecurly = '\0';
			do_one_file(opencurly + 1);
			*closecurly = '}';
			current_file = savefn;
			current_line = saveline;
		}
		ignore_line(fp, buf, sizeof(buf));
	}
	if (state != STATE_SCANNING) {
		fprintf(stderr,
			"%s:%d: \\QuickQuiz without \\QuickQuizEnd\n",
			current_file, lastqqz);
	}
	if (fnp != NULL)
		free(fnp);
	return;
outerr:
	if (fnp != NULL)
		free(fnp);
	exit(-1);
}

int main(int argc, char *argv[])
{
	int i;

	for (i = 1; i < argc; i++)
		do_one_file(argv[i]);

	return 0;
}