/*

        c2html.c

        Transforma código C en una página HTML.

        (C) Angel Ortega

*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char * commands[]=
{
        "#if", "#ifdef", "#endif", "#else", "#define",
        "#ifndef", "#pragma", "#include",
        "auto", "break", "case", "const", "continue",
        "default", "do", "else", "for", "goto", "if",
        "return", "switch", "while",
        NULL
};

char * types[]=
{
        "char", "double", "enum", "int", "long", "extern",
        "float", "register", "short", "signed", "sizeof",
        "static", "struct", "typedef", "union", "unsigned",
        "void", "volatile", NULL
};


/* último comando */

char cmd[1024];

int cmdindex=0;

int inquotes=0;

char quotechar;


int notissepa(char c)
/* devuelve nocero si es un separador */
{
        if(isalpha(c) || c=='_' || c=='#' || isdigit(c))
                return(1);
        else
                return(0);
}


int isoneofthis(char * str, char ** tokens)
/* comprueba si str es alguno de los tokens */
{
        for(;*tokens!=NULL;tokens++)
        {
                if(strcmp(*tokens, str)==0)
                        return(1);
        }

        return(0);
}


void processtoken(void)
/* procesa el token en cmd */
{
        if(isoneofthis(cmd, commands))
                printf("<B>%s</B>", cmd);
        else
        if(isoneofthis(cmd, types))
                printf("<I><B>%s</B></I>", cmd);
        else
        if(isdigit(cmd[0]))
                printf("<I>%s</I>", cmd);
        else
                printf("%s", cmd);
}


int main(int argc, char * argv[])
{
        int n,c,oc=0;
        FILE * f;

        if(argc==1)
        {
                printf("\nc2html 1.1 - Transforma código C en páginas HTML.\n");
                printf("  (C) Angel Ortega 1995\n\n");

                printf("Uso: c2html {fichero[s]...} > destino.htm\n");
                return(1);
        }

        printf("<PRE>\r\n");

        for(n=1;n<argc;n++)
        {
                if((f=fopen(argv[n], "r"))==NULL)
                {
                        fprintf(stderr,"Fichero %s no encontrado.\n",argv[n]);
                        return(2);
                }

                printf("<HR><H1>%s</H1><HR>\r\n", argv[n]);

                while((c=fgetc(f))!=EOF)
                {
                        if(notissepa(c))
                        {
                                cmd[cmdindex]=c;
                                cmdindex++;
                        }
                        else
                        {
                                cmd[cmdindex]='\0';

                                if(cmdindex)
                                        processtoken();

                                cmdindex=0;

                                if(c=='<')
                                        printf("&lt;");
                                else
                                if(c=='>')
                                        printf("&gt;");
                                else
                                if(c=='&')
                                        printf("&amp;");
                                else
                                if((c=='"' || c=='\'') && oc!='\\')
                                {
                                        if(inquotes)
                                        {
                                                if(quotechar==c)
                                                {
                                                        printf("%c</EM>",c);
                                                        inquotes=0;
                                                }
                                                else
                                                        printf("%c",c);
                                        }
                                        else
                                        {
                                                printf("<EM>%c",c);
                                                quotechar=c;
                                                inquotes=1;
                                        }
                                }
                                else
                                if(c=='*')
                                {
                                        if(oc=='/')
                                                printf("<I>*");
                                        else
                                                putchar(c);
                                }
                                else
                                if(c=='/')
                                {
                                        if(oc=='*')
                                                printf("/</I>");
                                        else
                                                putchar(c);
                                }
                                else
                                putchar(c);
                        }

                        if(c=='\\' && oc=='\\')
                                oc=0;
                        else
                                oc=c;
                }

                printf("<BR>\r\n");

                fclose(f);
        }

        return(0);
}
