/*******************************************/ /* */ /* File: c2html.c */ /* Purpose: c2html main file */ /* */ /* Author: Sfiligoi Igor */ /* */ /* Created : 30.01.1997 */ /* Last modified: 02.04.1997 */ /* */ /*******************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include "y.tab.c" #include "chtmlidfs.c" #include "chtmldefs.c" #define ERROR_OK 0 #define ERROR_HELP 1 #define ERROR_INFILE 2 #define ERROR_OUTFILE 3 /* extract params */ int getparams(int argc, char *argv[], char **filename, char **indir) { int i; if (argc==1) return ERROR_HELP; /* find the options */ for (i=1;i<argc;i++) { if (strcmp(argv[i],"-p")==0) doprint=1; else if (strcmp(argv[i],"-p-")==0) doprint=0; else if ((strcmp(argv[i],"-h")==0)||(strcmp(argv[i],"-?")==0)||(strcmp(argv[i],"?")==0)) return ERROR_HELP; else if (strcmp(argv[i],"-l")==0) { i++; free(protourl); protourl = (char *) malloc(strlen(argv[i])+1); strcpy(protourl,argv[i]); } else if (strcmp(argv[i],"-l-")==0) free(protourl); else if (strcmp(argv[i],"-m")==0) { i++; free(macrourl); macrourl = (char *) malloc(strlen(argv[i])+1); strcpy(macrourl,argv[i]); } else if (strcmp(argv[i],"-m-")==0) free(macrourl); else if (strcmp(argv[i],"-u")==0) { i++; free(funcurl); funcurl = (char *) malloc(strlen(argv[i])+1); strcpy(funcurl,argv[i]); } else if (strcmp(argv[i],"-u-")==0) free(funcurl); else if (strcmp(argv[i],"-c")==0) { i++; free(chidir); chidir = (char *) malloc(strlen(argv[i])+2); strcpy(chidir,argv[i]); if (chidir[strlen(argv[i])]!='/') strcat(chidir,"/"); } else if (strcmp(argv[i],"-c-")==0) free(chidir); else if (strcmp(argv[i],"-I")==0) { i++; strcat(path,":"); strcat(path,argv[i]); } else break; /* not a option */ } if (i<argc) { (*filename) = (char *) malloc(strlen(argv[i])+1); strcpy((*filename),argv[i]); } else { fprintf(stderr,"arg %i: filename expected. \n\n",i+1); return ERROR_HELP; /* filename expected */ } i++; if (i<argc) { int len; len = strlen(argv[i]); outdir = (char *) malloc(len+2); strcpy(outdir,argv[i]); /* terminate with / if not yet terminated */ if (outdir[len-1]!='/') { outdir[len]='/'; outdir[len+1]=0; } } else { /* set default indir and outdir*/ outdir = (char *) malloc(3); strcpy(outdir,"./"); (*indir) = (char *) malloc(3); strcpy((*indir),"./"); return ERROR_OK; } i++; if (i<argc) { int len; len = strlen(argv[i]); (*indir) = (char *) malloc(len+2); strcpy((*indir),argv[i]); /* terminate with / if not yet terminated */ if ((*indir)[len-1]!='/') { (*indir)[len]='/'; (*indir)[len+1]=0; } } else { /* set default indir*/ (*indir) = (char *) malloc(3); strcpy((*indir),"./"); return ERROR_OK; } i++; if (i<argc) { /* not fatal error */ fprintf(stderr,"from arg %i : too many parameters\n\n",i+1); } return ERROR_OK; } void printhelp() { fprintf(stderr,"c2html, v1\n"); fprintf(stderr,"Crated and written by Sfiligoi Igor\n\n"); fprintf(stderr,"use:\nc2html <options> filename [outdir] [indir]\n\n"); fprintf(stderr,"where options are:\n"); fprintf(stderr,"-p : write all the input in stderr\n"); fprintf(stderr,"-p- : do not write any input in stderr (default)\n"); fprintf(stderr,"-l file : link function prototypes to the file\n"); fprintf(stderr,"-l- : do not link function prototypes (default)\n"); fprintf(stderr,"-u file : link functions without prototype to the file\n"); fprintf(stderr,"-u- : do not link functions without prototype (default)\n"); fprintf(stderr,"-m file : link macros to the file\n"); fprintf(stderr,"-m- : no not link macros (default)\n"); fprintf(stderr,"-c dir : create the CHI file in the dir\n"); fprintf(stderr,"-c- : do not create the chi file (default)\n"); fprintf(stderr,"-I path : add path to the include path\n"); fprintf(stderr,"-h\n-?\n? : this help\n\n"); fprintf(stderr,"example:\n"); fprintf(stderr,"c2html -I $C2HTMLDOC c2html.c $C2HTMLDOC $C2HTMLSRC\n"); } int openfile(char * dir, char *filename) { char *fullname; fullname = (char *) malloc(strlen(dir)+strlen(filename)+1); strcpy(fullname,dir); strcat(fullname,filename); yyin = fopen(fullname,"r"); if (yyin==NULL) { fprintf(stderr,"%s not found.\n",fullname); return ERROR_INFILE; } free(fullname); return ERROR_OK; } int writeresult(char *dir, char *infilename) { char *fullname; char *filename; char *chifilename; char *tmp; FILE *outfile; filename = (char *) malloc(strlen(infilename)+6); strcpy(filename,infilename); /* convert last . in _ */ tmp = strrchr(filename,'.'); if (tmp!=NULL) tmp[0]='_'; fullname = (char *) malloc(strlen(dir)+strlen(filename)+10); strcpy(fullname,dir); strcat(fullname,filename); strcat(fullname,".html"); outfile = fopen(fullname,"w"); if (outfile==NULL) { fprintf(stderr,"Cannot create %s.\n",fullname); return ERROR_OUTFILE; } fprintf(outfile,"<HTML>\n\n"); fprintf(outfile,"<HEAD>\n<TITLE>\n"); fprintf(outfile,"%s",infilename); fprintf(outfile,"</TITLE>\n</HEAD>\n"); fprintf(outfile,"<BASEFONT SIZE=4>\n<BODY TEXT=\"#000000\" BGCOLOR=\"#FFFFFF\">\n<PRE>\n"); fprintf(outfile,"%s",finalprog); fprintf(outfile,"</PRE>\n</BODY>\n</HTML>"); fclose(outfile); if (chidir!=NULL) { char *chifullname; chifullname = (char *) malloc(strlen(chidir)+strlen(filename)+10); strcpy(chifullname,chidir); strcat(chifullname,filename); strcat(chifullname,".chi"); hash_save(mainSTable,chifullname,fullname); free(chifullname); } free(fullname); free(filename); return ERROR_OK; } int main(int argc, char *argv[]) { char *filename; char *indir; char *tmp; int err; macrourl = NULL; funcurl = NULL; protourl = NULL; mainSTable = hash_new(); thisSTable = hash_new(); path = (char *) malloc(32*1024); strcpy(path,"."); chidir = NULL; err = getparams(argc,argv,&filename,&indir); /* print help and exit if something wrong with the parameters */ if (err!=ERROR_OK) { printhelp(); return err; } if (protourl!=NULL) { tmp = protourl; protourl = normalize_dir(tmp,outdir); free(tmp); } if (macrourl!=NULL) { tmp = macrourl; macrourl = normalize_dir(tmp,outdir); free(tmp); } if (funcurl!=NULL) { tmp = funcurl; funcurl = normalize_dir(tmp,outdir); free(tmp); } err = openfile(indir,filename); if (err!=ERROR_OK) { fprintf(stderr,"Error opening file.\n\n"); return 2; } fprintf(stderr,"Start parsing.\n"); err = yyparse(); if (err!=0) { fprintf(stderr,"Parsing failed!\n\n"); return 3; } err = writeresult(outdir,filename); if (err!=ERROR_OK) { fprintf(stderr,"Error writing file.\n\n"); return 2; } fprintf(stderr,"Operation successful.\n"); return 0; }