// // Usage: cphtml [-s] [doc.phtml]+ // // produces doc.html (or doc.shtml if -s flag is present) // // .phtml files are .html files with embedded filters: // // // Philip Thrift thrift@csc.ti.com 01 Sep 1994 // Texas Instruments // Dallas TX USA // // Absolutely no guarantees. // // CC cphtml.c -o cphtml // // Problems: // need to do more like not executing filters // when data content hasn't changed, etc. // #include #include #include inline void usage(int condition, const char* msg) { if (!condition) { fprintf(stderr, "Usage: %s\n", msg); exit(0); } } inline streql(char* s, char* t) { return (strcmp(s,t)==0); } inline FILE* fopen_chk(char* fn, char* type) { FILE* fp; if (NULL == (fp = fopen(fn, type))) { fprintf(stderr, "File %s does not exist.\n", fn); exit(0); } return fp; } void fname(char* file, char* root, char* type) { int root_len = strlen(file); strcpy(root, file); while (root_len > 0 && file[root_len] != '.') root_len--; if (root_len > 0) { root[root_len] = '\0'; strcpy(type, file+root_len+1); } else { type[0] = '\0'; } } void mkfname(char* file, char* root, char* type) { strcpy(file, root); strcat(file, "."); strcat(file, type); } int find_delimiter(int c, char* tok, FILE* out) { static int n_so_far = 0; if (tok) { if (tok[n_so_far] == c) { n_so_far++; if (n_so_far == strlen(tok)) { n_so_far = 0; return 1; } return 0; } else { for (int i = 0; i < n_so_far; i++) { fputc(tok[i], out); } fputc(c, out); n_so_far = 0; return 0; } } else { if (c) fputc(c, out); n_so_far = 0; } } int main(int argc, char** argv) { usage(argc>1, "cphtml [-s] [doc.phtml]+"); static char root[BUFSIZ]; static char type[BUFSIZ]; static char html[BUFSIZ]; int s_flag = 0; int fn = 1; if (streql(argv[1], "-s")) { s_flag = 1; fn = 2; } while (fn < argc) { fname(argv[fn], root, type); if (!streql(type, "phtml")) { fprintf(stderr, "Error: cannot process %s, must be .phtml\n", argv[fn]); fn++; continue; } mkfname(html, root, (s_flag ? "shtml" : "html")); FILE* fp = fopen_chk(argv[fn], "r"); FILE* hp = fopen_chk(html, "w"); FILE* pp = NULL; int ppi = 0, c; static char proc[BUFSIZ]; while (EOF != (c = fgetc(fp))) { if (find_delimiter(c, "", pp)) { // acually a bug fclose(pp); system("cat .ppi-in | sh .ppi-proc > .ppi-out"); pp = fopen_chk(".ppi-out", "r"); while (EOF != (c = fgetc(pp))) fputc(c, hp); fclose(pp); break; } } } } fclose(fp); fclose(hp); fn++; } }