/* * man2HTML.c * * It converts man output to HTML format. * It supports bold and italic styles of text. * * Copyright (C) 1997 Damjan Lampret, damjan.lampret@s-sser.lj.edus.si * This is free software. See LICENSE for more information. * * v1.0: * - Thu., 3. July 1997 ... started from scratch. * * slightly hacked by Nat (nat@linux-france.com) 19980927 */ #include #include #include #include #include #include #define TMPBUFSIZE 500 #define SMALLBUF 100 #define TITLE_DEPTH 20 char sversion[SMALLBUF]="man2HTML v1.0"; char sauthor[SMALLBUF]="Copyright (C) 1997 by Damjan Lampret, damjan.lampret@s-sser.lj.edus.si"; char shomeurl[SMALLBUF]="\042http://www.s-sser.lj.edus.si/gizmox/man2HTML/index.html\042"; char ix_file[SMALLBUF]="\0"; /* index file */ char man_file[SMALLBUF]="\0"; /* man file */ char title[SMALLBUF]="Page de man"; /* Title for man pages */ char *bg_color="\"ffffff\""; /* Background color */ char *tn_color="\"000010\""; /* Normal text color */ char *ti_color="\"0000ff\""; /* Italic text color */ char *tb_color="\"000000\""; /* Bold text color */ char *txt_size="\"3\""; /* Text size */ int rm_esc=1; int lines=0, bytes=0; int extracting=0; int freadln(FILE *infile, char *buf, int maxcount) { char ch; int count, fe; count = 0; while ((fread(&ch,1,1,infile) != 0) && ((fe=feof(infile)) == 0) && (ch != '\n') &&(count < maxcount)) buf[count++]=ch; buf[count] = '\n'; buf[++count] = '\0'; if (fe != 0) { buf[0]='\0'; return(EOF); } return(0); } /* * Integer to string * */ char *itos(int value, char *result) { int cifra=0; long divider=100000; char *tmpstr; tmpstr = result; strcpy(result,"0\0"); while (divider>0) { result[cifra]=(value / divider)+48; value=value % divider; divider=divider / 10; cifra++; } result[cifra]='\0'; return result; } /* * Str remove between * strrbt("ABCDEF","B","D") will return "AEF" */ char *strrbt(char *str, char *from, char *to) { int b,s; /* begin, stop */ while ((b=strcspn(str,from)) < (s=strcspn(str,to))) { int i; for (i=b;iPages de manuel\n"); fprintf(f,"\n"); fprintf(f,"\n"); fprintf(f,"
\n"); fprintf(f,"\n"); fprintf(f,"\n"); fprintf(f,"\n"); fprintf(f,"\n"); fprintf(f,"
\n"); fprintf(f,"Pages de manuel
\n"); fprintf(f,"
\n"); fprintf(f,"
Commande
\n"); fprintf(f,"
\n"); fprintf(f,"
Description sommaire
\n"); fprintf(f,"
\n"); fprintf(f,"
Taille
\n"); fprintf(f,"
\n"); fprintf(f,"Conversion en HTML effectuée sur "); fprintf(f,"%s, %s avec ", hostname, convtime); fprintf(f,"%s (modifié par Nat), à partir du man-fr-0.6 de C. Blaess.\n",shomeurl,sversion); fprintf(f,"
\n"); fprintf(f,"
\n"); fprintf(f,"
"); } void add_index_entry(FILE *f, char *name, char *desc, int filelen) { fprintf(f,"\n",name); fprintf(f,"\n"); fprintf(f,"%s\n",man_file,name); fprintf(f,"\n"); fprintf(f,"%s",desc); fprintf(f,"\n"); if (filelen > 1023) fprintf(f,"%u KB\n",filelen / 1024); else fprintf(f,"%u by\n",filelen); fprintf(f,"\n"); } void create_header(char *str) { strcpy(str,""); strcat(str,title); strcat(str,"\n
");
}

void create_footer(char *str)
{
	strcpy(str,"
"); } void formatHTML(char *in_str, char *out_str) { int bold=0, italic=0; int i; strcpy(out_str,"\0"); for (i=0;i"); if (strcmp(tn_color,tb_color) !=0) strcat(out_str,""); } if (italic == 1) { italic=0; strcat(out_str,""); if (strcmp(tn_color,ti_color) !=0) strcat(out_str,""); } if (in_str[i] == '<') strncat(out_str,"<",3); else strncat(out_str,&in_str[i],1); } else if ((in_str[i] == '_') && (bold == 0)) { if (italic == 0) { italic=1; if (strcmp(tn_color,ti_color) !=0) { strcat(out_str,""); } strcat(out_str,""); } do { i+=2; } while ((i+1"); } strcat(out_str,""); } do { i+=2; } while ((i+1"); if (strcmp(tn_color,tb_color) !=0) strcat(out_str,""); } if (italic == 1) { strcat(out_str,""); if (strcmp(tn_color,ti_color) !=0) strcat(out_str,""); } if ((rm_esc == 1) && ((void *)strstr(out_str,"\033[") != NULL)) strcpy(out_str,"\0"); if (strlen(out_str) > 0) lines++; } void options(int argc, char *argv[]) { char tmpstr[10]; int i; for (i=1;i 1) && (lines <= TITLE_DEPTH+4)) strcat(name,str); if ((extracting == 1) && ((strlen(str) < 2) || (lines > TITLE_DEPTH+4))) extracting=-1; if ((extracting == 0) && (lines < TITLE_DEPTH) && (strlen(name) == 0) && ((strcasecmp(str,"NAME\n") == 0) || (strstr(str,"NAME") != NULL) || (strstr(str,"NOM") != NULL) || (strstr(str,"Name") != NULL))) { strcpy(name,""); extracting=1; } } void update_index(char *str, int bytes) { char tmpfile[SMALLBUF] = "/tmp/"; char tmpbuf[TMPBUFSIZE] = ""; char name[SMALLBUF*8] = "\0"; char desc[TMPBUFSIZE] = "(inconnue)\0"; FILE *fs, *fd; if (strstr(str," -") != NULL) strncpy(desc,strstr(str," -")+3,TMPBUFSIZE); strncpy(name,&str[strspn(str," ")],SMALLBUF-1); if (strstr(name," -") != NULL) { char *tmp; tmp = strstr(name," - "); strcpy(tmp,"\0"); } if (strlen(name) == 0) { strncpy(name, "INCONNUE", SMALLBUF); if (strlen(man_file) > 0) strncpy(name, man_file, SMALLBUF); } itos(getpid(),tmpbuf); strcat(tmpfile,tmpbuf); strcat(tmpfile,".html"); if ((fs = fopen(ix_file, "r")) != NULL) { fclose(fs); } else if ((fs = fopen(ix_file, "w")) != NULL) { create_index(fs); fclose(fs); } else exit(0); if ((fs = fopen(ix_file, "r")) != NULL) { if ((fd = fopen(tmpfile, "w")) != NULL) { int still = 1; char cmpname[TMPBUFSIZE]=" 0)) { still=0; add_index_entry(fd, name, desc, bytes); fprintf(fd,"%s",tmpbuf); } else { fprintf(fd,"%s",tmpbuf); } } fclose(fd); } fclose(fs); } else exit(0); if ((fs = fopen(tmpfile, "r")) != NULL) { if ((fd = fopen(ix_file, "w")) != NULL) { while (!feof(fs)) { freadln(fs,tmpbuf,TMPBUFSIZE-2); fprintf(fd,"%s",tmpbuf); } fclose(fd); } fclose(fs); remove(tmpfile); } else exit(0); } int main(int argc, char *argv[]) { char in_buf[TMPBUFSIZE]=""; char out_buf[TMPBUFSIZE]=""; char name_str[TMPBUFSIZE]="\0"; options(argc, argv); freadln(stdin,in_buf,TMPBUFSIZE); if (strlen(in_buf) > 0) { create_header(out_buf); printf("%s",out_buf); bytes=strlen(out_buf); do { formatHTML(in_buf,out_buf); if (strlen(ix_file) > 0) extract_name(out_buf,name_str); if (strlen(out_buf) > 0) printf("%s",out_buf); bytes+=strlen(out_buf); } while (freadln(stdin,in_buf,TMPBUFSIZE) != EOF); formatHTML(in_buf,out_buf); printf("%s",out_buf); bytes+=strlen(out_buf); create_footer(out_buf); printf("%s",out_buf); bytes+=strlen(out_buf); if (strlen(ix_file) > 0) { strrbt(name_str,"<",">"); update_index(name_str, bytes); } } exit(0); }