man2html.c

来自「很少见的linux下的红外口的工具」· C语言 代码 · 共 2,713 行 · 第 1/5 页

C
2,713
字号
    "4b", "SunOS/BSD Compatibility Package File Formats",    "5", "Headers, Tables, and Macros",    "6", "Games and Demos",    "7", "Special Files",    "7B", "SunOS/BSD Compatibility Special Files",    "7b", "SunOS/BSD Compatibility Special Files",    "8", "Maintenance Procedures",    "8C", "Maintenance Procedures",    "8c", "Maintenance Procedures",    "8S", "Maintenance Procedures",    "8s", "Maintenance Procedures",    "9", "DDI and DKI",    "9E", "DDI and DKI Driver Entry Points",    "9e", "DDI and DKI Driver Entry Points",    "9F", "DDI and DKI Kernel Functions",    "9f", "DDI and DKI Kernel Functions",    "9S", "DDI and DKI Data Structures",    "9s", "DDI and DKI Data Structures",    "L", "Local Commands",*/    NULL, "Misc. Reference Manual Pages",    NULL, NULL};char *section_name(char *c){    int i=0;    if (!c) return "";    while (section_list[i] && strcmp(c,section_list[i])) i=i+2;    if (section_list[i+1]) return section_list[i+1];    else return c;}char manidx[20000];int subs=0;int mip=0;char label[5]="lbAA";void add_to_index(int level, char *item){    char *c=NULL;    label[3]++;    if (label[3]>'Z') {	label[3]='A';	label[2]++;    }    if (level != subs) {	if (subs) {	    strcpy(manidx+mip, "</DL>\n");	    mip+=6;	} else {	    strcpy(manidx+mip, "<DL>\n");	    mip+=5;	}    }    subs=level;    scan_troff(item, 1, &c);    sprintf(manidx+mip, "<DT><A HREF=\"#%s\">%s</A><DD>\n", label, c);    if (c) free(c);    while (manidx[mip]) mip++;}char *skip_till_newline(char *c){    int lvl=0;    while ((*c && *c!='\n') || (lvl>0)) {	if (*c=='\\') {	    c++;	    if (*c=='}') lvl--; else if (*c=='{') lvl++;	}	c++;    }    c++;    if (lvl<0 && newline_for_fun) {	newline_for_fun = newline_for_fun+lvl;	if (newline_for_fun<0) newline_for_fun=0;    }    return c;}int ifelseval=0;char *scan_request(char *c){    int i,j,mode=0;    char *h;    char *wordlist[20];    int words;    char *sl;    STRDEF *owndef;    while (*c==' ' || *c=='\t') c++;    if (c[0]=='\n') return c+1;    if (c[1]=='\n') j=1; else j=2;    while (c[j]==' ' || c[j]=='\t') j++;    if (c[0]==escapesym) {	/* some pages use .\" .\$1 .\} */	/* .\$1 is too difficult/stuppid */	if (c[1]=='$') c=skip_till_newline(c);	else	    c = scan_escape(c+1);    } else {	i=V(c[0],c[1]);	switch (i) {	case V('a','b'):	    h=c+j;	    while (*h && *h !='\n') h++;	    *h='\0';	    if (scaninbuff && buffpos) {		buffer[buffpos]='\0';		printf("%s\n", buffer);	    }	    fprintf(stderr, "%s\n", c+2);	    exit(0);	    break;	case V('d','i'):	    {		STRDEF *de;		c=c+j;		i=V(c[0],c[1]);		if (*c=='\n') { c++;break; }		while (*c && *c!='\n') c++;		c++;		h=c;		while (*c && strncmp(c,".di",3)) while (*c && *c++!='\n');		*c='\0';		de=strdef;		while (de && de->nr !=i) de=de->next;		if (!de) {		    de=(STRDEF*) malloc(sizeof(STRDEF));		    de->nr=i;		    de->slen=0;		    de->next=strdef;		    de->st=NULL;		    strdef=de;		} else {		    if (de->st) free(de->st);		    de->slen=0;		    de->st=NULL;		}		scan_troff(h,0,&de->st);		*c='.';		while (*c && *c++!='\n');		break;	    }	case V('d','s'):	    mode=1;	case V('a','s'):	    {		STRDEF *de;		int oldcurpos=curpos;		c=c+j;		i=V(c[0],c[1]);		j=0;		while (c[j] && c[j]!='\n') j++;		if (j<3) { c=c+j; break; }		if (c[1]==' ') c=c+1; else c=c+2;		while (isspace(*c)) c++;		if (*c=='"') c++;		de=strdef;		while (de && de->nr != i) de=de->next;		single_escape=1;		curpos=0;		if (!de) {		    char *h;		    de=(STRDEF*) malloc(sizeof(STRDEF));		    de->nr=i;		    de->slen=0;		    de->next=strdef;		    de->st=NULL;		    strdef=de;		    h=NULL;		    c=scan_troff(c, 1, &h);		    de->st=h;		    de->slen=curpos;		} else {		    if (mode) {			char *h=NULL;			c=scan_troff(c, 1, &h);			free(de->st);			de->slen=0;			de->st=h;		    } else			c=scan_troff(c,1,&de->st);		    de->slen+=curpos;		}		single_escape=0;		curpos=oldcurpos;	    }	    break;	case V('b','r'):	    if (still_dd) out_html("<DD>");	    else out_html("<BR>\n");	    curpos=0;	    c=c+j;	    if (c[0]==escapesym) { c=scan_escape(c+1); }	    c=skip_till_newline(c);break;	case V('c','2'):	    c=c+j;	    if (*c!='\n') { nobreaksym=*c; }	    else nobreaksym='\'';	    c=skip_till_newline(c);	    break;	case V('c','c'):	    c=c+j;	    if (*c!='\n') { controlsym=*c; }	    else controlsym='.';	    c=skip_till_newline(c);	    break;	case V('c','e'):	    c=c+j;	    if (*c=='\n') { i=1; }	    else {		i=0;		while ('0'<=*c && *c<='9') {		    i=i*10+*c-'0';		    c++;		}	    }	    c=skip_till_newline(c);	    /* center next i lines */	    if (i>0) {		out_html("<CENTER>\n");		while (i && *c) {		    char *line=NULL;		    c=scan_troff(c,1, &line);		    if (line && strncmp(line, "<BR>", 4)) {			out_html(line);			out_html("<BR>\n");			i--;		    }		}		out_html("</CENTER>\n");		curpos=0;	    }	    break;	case V('e','c'):	    c=c+j;	    if (*c!='\n') { escapesym=*c; }	    else escapesym='\\';	    break;	    c=skip_till_newline(c);	case V('e','o'):	    escapesym='\0';	    c=skip_till_newline(c);	    break;	case V('e','x'):	    exit(0);	    break;	case V('f','c'):	    c=c+j;	    if  (*c=='\n') {		fieldsym=padsym='\0';	    } else {		fieldsym=c[0];		padsym=c[1];	    }	    c=skip_till_newline(c);	    break;	case V('f','i'):	    if (!fillout) {		out_html(change_to_font(0));		out_html(change_to_size('0'));		out_html("</PRE>\n");	    }	    curpos=0;	    fillout=1;	    c=skip_till_newline(c);	    break;	case V('f','t'):	    c=c+j;	    if (*c=='\n') {		out_html(change_to_font(0));	    } else {		if (*c==escapesym) {		    int fn;		    c=scan_expression(c, &fn);		    c--;		    out_html(change_to_font(fn));		} else {		    out_html(change_to_font(*c));		    c++;		}	    }	    c=skip_till_newline(c);	    break;	case V('e','l'):	    /* .el anything : else part of if else */	    if (ifelseval) {		c=c+j;		c[-1]='\n';		c=scan_troff(c,1,NULL);	    } else		c=skip_till_newline(c+j);	    break;	case V('i','e'):	    /* .ie c anything : then part of if else */		case V('i','f'):	    /* .if c anything	     * .if !c anything	     * .if N anything	     * .if !N anything	     * .if 'string1'string2' anything	     * .if !'string1'string2' anything	     */	    c=c+j;	    c=scan_expression(c, &i);	    ifelseval=!i;	    if (i) {		*c='\n';		c++;		c=scan_troff(c,1,NULL);	    } else		c=skip_till_newline(c);	    break;	case V('i','g'):	    {		char *endwith="..\n";		i=3;		c=c+j;		if (*c!='\n') {		    endwith=c-1;i=1;		    c[-1]='.';		    while (*c!='\n') c++,i++;		}		c++;		while (*c && strncmp(c,endwith,i)) while (*c++!='\n');		while (*c++!='\n');		break;	    }	case V('n','f'):	    if (fillout) {		out_html(change_to_font(0));		out_html(change_to_size('0'));		out_html("<PRE>\n");	    }	    curpos=0;	    fillout=0;	    c=skip_till_newline(c);	    break;	case V('p','s'):	    c=c+j;	    if (*c=='\n') {		out_html(change_to_size('0'));	    } else {		j=0;i=0;		if (*c=='-') { j= -1;c++; } else if (*c=='+') { j=1;c++;}		c=scan_expression(c, &i);		if (!j) { j=1; if (i>5) i=i-10; }		out_html(change_to_size(i*j));	    }	    c=skip_till_newline(c);	    break;	case V('s','p'):	    c=c+j;	    if (fillout) out_html("<P>"); else {		out_html(NEWLINE);		NEWLINE[0]='\n';	    }	    curpos=0;	    c=skip_till_newline(c);	    break;	case V('s','o'):	    {		FILE *f;		struct stat stbuf;		int l=0;char *buf;		char *name=NULL;		curpos=0;		c=c+j;		if (*c=='/') {		    h=c;		} else {		    h=c-3;		    h[0]='.';		    h[1]='.';		    h[2]='/';		}		while (*c!='\n') c++;		*c='\0';		scan_troff(h,1, &name);		if (name[3]=='/') h=name+3; else h=name;		if (stat(h, &stbuf)!=-1) l=stbuf.st_size;		buf = (char*) malloc((l+4)*sizeof(char));#if NOCGI                if (!out_length) {		    char *t,*s;		    t=strrchr(fname, '/');		    if (!t) t=fname;		    fprintf(stderr, "ln -s %s.html %s.html\n", h, t);		    s=strrchr(t, '.');if (!s) s=t;		    printf("<HTML><HEAD><TITLE> Manpage of %s</TITLE>\n"			   "</HEAD><BODY>\n"			   "See the manpage for <A HREF=\"%s.html\">%s</A>.\n"			   "</BODY></HTML>\n",			   s, h, h);		} else#endif                {		    /* this works alright, except for section 3 */		    f=fopen(h,"r");		    if (!f || !buf || !l)			fprintf(stderr, "Unable to open or read file %s.\n",				h); 		    else {			i=fread(buf+1,1,l,f);			fclose(f);			buf[0]=buf[l]='\n';			buf[l+1]=buf[l+2]='\0';			scan_troff(buf+1,0,NULL);		    }		    if (buf) free(buf);		}		*c++='\n';		break;	    }	case V('t','a'):	    c=c+j;	    j=0;	    while (*c!='\n') {		sl=scan_expression(c, &tabstops[j]);		if (*c=='-' || *c=='+') tabstops[j]+=tabstops[j-1];		c=sl;		while (*c==' ' || *c=='\t') c++;		j++;	    }	    maxtstop=j;	    curpos=0;	    break;	case V('t','i'):	    /*while (itemdepth || dl_set[itemdepth]) {		out_html("</DL>\n");		if (dl_set[itemdepth]) dl_set[itemdepth]=0;		else itemdepth--;	    }*/	    out_html("<BR>\n");	    c=c+j;	    c=scan_expression(c, &j);	    for (i=0; i<j; i++) out_html("&nbsp;");	    curpos=j;	    c=skip_till_newline(c);	    break;	case V('t','m'):	    c=c+j;	    h=c;	    while (*c!='\n') c++;	    *c='\0';	    fprintf(stderr,"%s\n", h);	    *c='\n';	    break;	case V('B',' '):	case V('B','\n'):	case V('I',' '):	case V('I','\n'):            /* parse one line in a certain font */	    out_html(change_to_font(*c));	    trans_char(c,'"','\a');	    c=c+j;	    if (*c=='\n') c++;	    c=scan_troff(c, 1, NULL);	    out_html(change_to_font('R'));	    out_html(NEWLINE);	    if (fillout) curpos++; else curpos=0;	    break;	case V('O','P'):  /* groff manpages use this construction */            /* .OP a b : [ <B>a</B> <I>b</I> ] */	    mode=1;	    c[0]='B'; c[1]='I';	    out_html(change_to_font('R'));	    out_html("[");	    curpos++;	case V('B','R'):	case V('B','I'):	case V('I','B'):	case V('I','R'):	case V('R','B'):	case V('R','I'):	    {		char font[2] = { c[0], c[1] };		c=c+j;		if (*c=='\n') c++;		sl=fill_words(c, wordlist, &words);		c=sl+1;		/* .BR name (section)		** indicates a link. It will be added in the output routine.		*/		for (i=0; i<words; i++) {		    if (mode) { out_html(" "); curpos++; }		    wordlist[i][-1]=' ';		    out_html(change_to_font(font[i&1]));		    scan_troff(wordlist[i],1,NULL);		}		out_html(change_to_font('R'));		if (mode) { out_html(" ]"); curpos++;}		out_html(NEWLINE); if (!fillout) curpos=0; else curpos++;	    }	    break;	case V('D','T'):	    for (j=0;j<20; j++) tabstops[j]=(j+1)*8;	    maxtstop=20;	    c=skip_till_newline(c); break;	case V('I','P'):	    sl=fill_words(c+j, wordlist, &words);	    c=sl+1;            if (!dl_set[itemdepth]) {		out_html("<DL COMPACT>\n");		dl_set[itemdepth]=1;	    }	    out_html("<DT>");            if (words) {		scan_troff(wordlist[0], 1,NULL);	    }	    out_html("<DD>");	    curpos=0;	    break;	case V('T','P'):	    if (!dl_set[itemdepth]) {		out_html("<DL COMPACT>\n");		dl_set[itemdepth]=1;	    }	    out_html("<DT>");	    c=skip_till_newline(c);	    /* somewhere a definition ends with '.TP' */	    if (!*c) still_dd=1; else {		c=scan_troff(c,1,NULL);		out_html("<DD>");	    }	    curpos=0;	    break;	case V('I','X'):            /* general index */	    sl = fill_words(c+j, wordlist, &words);	    c=sl+1;	    j=4;	    while (idxlabel[j]=='Z') idxlabel[j--]='A';	    idxlabel[j]++;#ifdef MAKEINDEX            fprintf(idxfile, "%s@%s@", fname, idxlabel);	    for (j=0; j<words; j++) {		h=NULL;		scan_troff(wordlist[j], 1, &h);		fprintf(idxfile, "_\b@%s", h);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?