/*------------------------------------------------------+ | Author: Maurizio Loreti, aka MLO or (HAM) I3NOO | | Work: University of Padova - Department of Physics | | Via F. Marzolo, 8 - 35131 PADOVA - Italy | | Phone: +39 (049) 827-7216 FAX: +39 (049) 827-7102 | | EMail: loreti@pd.infn.it | | WWW: http://www.pd.infn.it/~loreti/mlo.html | +------------------------------------------------------+ Quick-and-dirty program that generates web pages containing a file pretty-printed by "source-highlight" (a GNU utility available from http://www.gnu.org/software/src-highlite/source-highlight.html). The program 'tohtml' should be invoked as: tohtml [ ] - is the name of the file to be pretty printed (let's say "source.cxx"); and may be prefixed by a path, like in "~/work/source.cxx". - contains the host name and the full path of the URL where the named file will be available on the net (e.g., giving as the string "wwwcdf.pd.infn.it/labo/Source" means that in order to obtain the file "source.cxx" we are supposed to open the URL "http://wwwcdf.pd.infn.it/labo/Source/source.cxx" in our browser). - All the remaining parameters, if any, will be passed to the rendering program source-highlight; the default (i.e., when only two command line parameters are given to tohtml) is "-s cpp": to mean that the language of the input file is C or C++. The HTML code is printed to .html, e.g. "source.cxx.html"; if contains a path, only the basename is used: and the output will be written in the current directory. A few informative messages are printed on the standard output stream (possible error messages will go to the standard error stream). ------------------------------------------------------------------- $Id: tohtml.c,v 1.4 2006/11/09 09:49:17 loreti Exp $ -------------------------------------------------------------------*/ #include /* Standard Library */ #include #include #include #include #include /* POSIX headers */ #include #include #define BUFLEN 512 static char *baseName(char *); static char *myStrncpy(char *, const char *, const char *); int main( int argc, char *argv[] ) { /** | Local variables: | - buffer: to store the command to execute source-highlight | - progName: the name of the current program (without the eventual | path). | - fileName: the name of the input file, without the eventual path | information | - outName: the name of the output file | - lastBuffer, lastOutName: pointers to the last char of buffer | and outName **/ char buffer[BUFLEN]; const char *lastBuffer = buffer + BUFLEN - 1; char *progName = baseName(argv[0]); char *fileName; char outName[FILENAME_MAX]; const char *lastOutName = outName + FILENAME_MAX - 1; FILE *fpOut; struct stat statInfo; char *pc; int saveErrno; /** | Validation: at least two command line parameters, the first one | being the name of a readable file. **/ if (argc < 3) { fprintf(stderr, "\nUsage:\t%s [ ]\n\n", progName); fputs("- : the source file to be processed;\n", stderr); fputs("- : host and path of the URL where the source file " "may be found;\n", stderr); fputs("- : optional parameters to be passed to " "source-highlight:\n", stderr); fputs(" defaults to \"-s cpp\" (the input language is C " "or C++).\n\n", stderr); return EXIT_FAILURE; } if (access(argv[1], R_OK) != 0) { saveErrno = errno; fprintf(stderr, "%s: file \"%s\" is not available for reading\n" "access: %s\n", progName, argv[1], strerror(saveErrno)); return EXIT_FAILURE; } if (stat(argv[1], &statInfo) != 0) { saveErrno = errno; fprintf(stderr, "%s: couldn't stat file \"%s\"\n" "stat: %s\n", progName, argv[1], strerror(saveErrno)); return EXIT_FAILURE; } /** | Builds fileName and outName; opens the output file and writes the | HTML preamble **/ fileName = baseName(argv[1]); pc = myStrncpy(outName, fileName, lastOutName); myStrncpy(pc, ".html", lastOutName); if ((fpOut = fopen(outName, "w")) == 0) { saveErrno = errno; fprintf(stderr, "%s: couldn't open output file \"%s\"\n" "fopen: %s\n", progName, outName, strerror(saveErrno)); return EXIT_FAILURE; } fputs("\n", fpOut); fprintf(fpOut, "\n", __DATE__, __TIME__); fprintf(fpOut, "%s\n", fileName); fputs("\n", fpOut); fputs("\n", fpOut); fputs("\n", fpOut); fputs("\n", fpOut); fprintf(fpOut, "

%s

\n", fileName); fputs("


\n", fpOut); fputs("Per salvare il file listato qui avanti, scaricatelo " "direttamente\n", fpOut); fprintf(fpOut, "da questo link\n", argv[2], fileName); fputs("(right-click, Save Link As ...).\n", fpOut); fputs("


\n", fpOut); fputs("

\n", fpOut); fputs("\n", fpOut); fputs("
\n", fpOut); if (ferror(fpOut)) { saveErrno = errno; fprintf(stderr, "%s: error writing output file \"%s\"\n" "fprintf/fputs (1): %s\n", progName, outName, strerror(saveErrno)); return EXIT_FAILURE; } fclose(fpOut); /** | Builds the command that invokes source-highlight, and appends the | highlighted input file to the output file; then reopens the | output file to append the HTML trailer. **/ pc = buffer + snprintf(buffer, BUFLEN, "source-highlight -i \"%s\" -f html", argv[1]); if (argc > 3) { int i; for (i = 3; argv[i] != 0; ++i) { pc = myStrncpy(pc, " ", lastBuffer); pc = myStrncpy(pc, argv[i], lastBuffer); } } else { pc = myStrncpy(pc, " -s cpp", lastBuffer); } pc = myStrncpy(pc, " >> \"", lastBuffer); pc = myStrncpy(pc, outName, lastBuffer); pc = myStrncpy(pc, "\"", lastBuffer); fprintf(stdout, "%s: %s\n", progName, buffer); if (system(buffer) != 0) { fprintf(stdout, "%s: failure\n", progName); return EXIT_FAILURE; } else { fprintf(stdout, "%s: success\n", progName); } if ((fpOut = fopen(outName, "a")) == 0) { saveErrno = errno; fprintf(stderr, "%s: can't happen\nfopen/append: %s\n", progName, strerror(saveErrno)); return EXIT_FAILURE; } fputs("
\n", fpOut); fputs("


\n", fpOut); fprintf(fpOut, "File name: %s
\n", fileName); fprintf(fpOut, "File size: %lu bytes
\n", (unsigned long) statInfo.st_size); fprintf(fpOut, "Last modified: %s", asctime(localtime(&statInfo.st_mtime))); fputs("\n", fpOut); if (ferror(fpOut)) { saveErrno = errno; fprintf(stderr, "%s: error writing output file \"%s\"\n" "fprintf/fputs (2): %s\n", progName, outName, strerror(saveErrno)); return EXIT_FAILURE; } fclose(fpOut); return EXIT_SUCCESS; } /**********************************************************************/ /***************************** baseName *******************************/ /**********************************************************************/ static char *baseName( char *pc ){ /** | Strips the (eventual) path information from "pc"; returns a | pointer to the terminating '\0' if "pc" ends in '/'. **/ char *p; p = strrchr(pc, '/'); if (p == 0) return pc; return ++p; } /**********************************************************************/ /***************************** myStrncpy ******************************/ /**********************************************************************/ static char *myStrncpy( char *to, const char *from, const char *last ) { /** | Copies the string in "from" to "to", until the '\0' terminator or | until the character pointed to by "last"; returns a pointer to | the '\0' terminator in "to" (that is always written). **/ while ((*to = *from++) != '\0') { if (to == last) { *to = '\0'; break; } ++to; } return to; }