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
|
/* extract code fragments from the gretl manual .tex files
so they can be checked for up-to-dateness.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
char line[1024];
const char *START = "begin{code}";
const char *END = "end{code}";
int first_char (const char *s)
{
return *(s + strspn(s, " \t"));
}
int process_tex_file (const char *fname)
{
FILE *fp;
fp = fopen(fname, "r");
if (fp == NULL) {
fprintf(stderr, "couldn't open %s\n", fname);
return 1;
}
while (fgets(line, sizeof line, fp)) {
if (strstr(line, START)) {
printf("\n\n# %s", line);
while (fgets(line, sizeof line, fp)) {
if (strstr(line, END)) {
printf("# %s", line);
break;
} else if (first_char(line) == '\\') {
printf("# %s", line);
} else {
fputs(line, stdout);
}
}
}
}
fclose(fp);
return 0;
}
int main (int argc, char **argv)
{
const char *dirname = ".";
const char *fname;
DIR *dir = NULL;
struct dirent *dirent;
if ((dir = opendir(dirname)) == NULL) {
printf("%s: can't open directory\n", argv[0]);
exit(EXIT_FAILURE);
}
while ((dirent = readdir(dir)) != NULL) {
fname = dirent->d_name;
if (strstr(fname, ".tex")) {
fprintf(stderr, "processing %s\n", fname);
process_tex_file(fname);
}
}
closedir(dir);
return 0;
}
|