blob: 05fe44263f209607ba2caeccac5c96293e2e7eee (
plain)
1 /**
2 * This CGI program renders the oper.io blog
3 * Copyright (C) 2019 Aaron Ball <nullspoon@oper.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "j2.h"
19
20
21 int j2_readvar(FILE* fd, char* buf) {
22 char c;
23 int i = 0;
24 fpos_t startpos;
25
26 fgetpos(fd, &startpos);
27 //long seekstart = SEEK_CUR;
28
29 if((c = fgetc(fd)) != '{') {
30 fsetpos(fd, &startpos);
31 return -1;
32 }
33 if(fgetc(fd) != ' ') {
34 fsetpos(fd, &startpos);
35 return -1;
36 }
37
38 while((c = fgetc(fd)) != ' ') {
39 buf[i] = c;
40 i++;
41 }
42
43 buf[i] = '\0';
44 return 1;
45 }
46
47
48 /**
49 * Reads a jinja file and cats the output, interpolating variables, values from
50 * matching environment variables.
51 */
52 int j2_cat(char* path) {
53 char buf[512] = {'\0'};
54 char c;
55 FILE* fd = fopen(path, "r");
56
57 while((c = fgetc(fd)) != EOF) {
58 if(c == '{') {
59 if(j2_readvar(fd, buf) == -1) {
60 printf("{%s", buf);
61 continue;
62 }
63
64 printf("%s", getenv(buf));
65 fgetc(fd);
66 fgetc(fd);
67 continue;
68 }
69 printf("%c", c);
70 }
71
72 fclose(fd);
73 return 0;
74 }
|