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
|
/* Extract downloadable example scripts from the tex files that constitute
the Gretl User's Guide, 2020-12-06, revised 2022-08-09, revised again
2024-02-12.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
char line[1024];
char label[32];
/* max scripts per chapter, should be generous */
#define MAX_SCRIPTS 20
char *labels[MAX_SCRIPTS] = {NULL};
int nlines[MAX_SCRIPTS] = {0};
const char *destpath;
int get_chapter_label (char *label, const char *line)
{
const char *targ = "\\label{chap:";
const char *s = strstr(line, targ);
if (s != NULL) {
s += strlen(targ);
*label = '\0';
strncat(label, s, strcspn(s, "}"));
return 1;
} else {
return 0;
}
}
char *get_script_label (const char *line)
{
const char *targ = "\\scriptinfo{";
const char *s = strstr(line, targ) + strlen(targ);
int n = strcspn(s, "}");
char *label = calloc(n+1, 1);
strncat(label, s, n);
return label;
}
int process_tex_file (const char *fname)
{
char outtemp[32];
char chaplabel[32];
FILE *fp, *fs;
int n_scripts = 0;
int err = 0;
fp = fopen(fname, "r");
if (fp == NULL) {
fprintf(stderr, "%s: not found\n", fname);
return 1;
} else {
printf("\nprocessing %s\n", fname);
}
while (fgets(line, sizeof line, fp)) {
if (get_chapter_label(chaplabel, line)) {
continue;
}
if (strstr(line, "begin{script}")) {
int record = 0;
sprintf(outtemp, "script%d", n_scripts + 1);
fs = fopen(outtemp, "w");
if (fs == NULL) {
fprintf(stderr, "couldn't write to '%s'\n", outtemp);
exit(EXIT_FAILURE);
}
nlines[n_scripts] = 0;
while (fgets(line, sizeof line, fp)) {
if (strstr(line, "scriptinfo")) {
labels[n_scripts] = get_script_label(line);
record = 1;
n_scripts++;
}
if (record) {
if (strstr(line, "begin{scode}")) {
while (fgets(line, sizeof line, fp)) {
if (strstr(line, "end{scode}")) {
break;
} else {
fputs(line, fs);
nlines[n_scripts-1] += 1;
}
}
}
}
if (strstr(line, "end{script}")) {
fclose(fs);
break;
}
}
}
}
fclose(fp);
if (n_scripts > 0) {
char outname[32];
int i;
printf("Chapter ID '%s': %d script(s)\n", chaplabel, n_scripts);
for (i=0; i<n_scripts; i++) {
sprintf(outtemp, "script%d", i + 1);
printf(" %d: '%s'\n", i+1, labels[i]);
if (nlines[i] < 5) {
printf(" nlines = %d, fail\n", nlines[i]);
remove(outtemp);
err = 1;
} else {
sprintf(outname, "%s.inp", labels[i]);
rename(outtemp, outname);
}
free(labels[i]);
labels[i] = NULL;
nlines[i] = 0;
}
} else {
printf("Chapter ID '%s': no downloadable scripts\n", chaplabel);
}
return err;
}
int main (int argc, char **argv)
{
const char *texpath;
char fullname[512];
char chap[32];
FILE *fp;
int err = 0;
if (argc < 2) {
fprintf(stderr, "%s: need path to tex files\n", argv[0]);
exit(EXIT_FAILURE);
}
texpath = argv[1];
printf("tex files: '%s'\n", texpath);
destpath = getenv("DESTDIR");
sprintf(fullname, "%s/gretl-guide.tex", texpath);
fp = fopen(fullname, "rb");
if (fp == NULL) {
printf("%s: can't open '%s'\n", argv[0], fullname);
exit(EXIT_FAILURE);
}
while (fgets(line, sizeof line, fp) && !err) {
if (!strncmp(line, "\\guidechap{", 11)) {
sscanf(line + 11, "%[^}]", chap);
sprintf(fullname, "%s/%s.tex", texpath, chap);
err = process_tex_file(fullname);
}
}
fclose(fp);
if (err) {
exit(EXIT_FAILURE);
}
return 0;
}
|