1 /***************************************
4 C Cross Referencing & Documentation tool. Version 1.5f.
6 Writes the Latex output.
7 ******************/ /******************
8 Written by Andrew M. Bishop
10 This file Copyright 1995,96,97,98,2001,04 Andrew M. Bishop
11 It may be distributed under the GNU Public License, version 2, or
12 any higher version. See section COPYING of the GNU Public license
13 for conditions under which this file may be redistributed.
14 ***************************************/
19 #include <sys/types.h>
24 #define min(x,y) ( (x) < (y) ? (x) : (y) )
31 /*+ The name of the output tex file that includes each of the others. +*/
32 #define LATEX_FILE ".tex"
33 #define LATEX_FILE_BACKUP ".tex~"
35 /*+ The name of the output tex file that contains the appendix. +*/
36 #define LATEX_APDX ".apdx"
38 /*+ The comments are to be inserted verbatim. +*/
39 extern int option_verbatim_comments
;
41 /*+ The type of LaTeX output to produce. +*/
42 extern int option_latex
;
44 /*+ The name of the directory for the output. +*/
45 extern char* option_odir
;
47 /*+ The base name of the file for the output. +*/
48 extern char* option_name
;
50 /*+ The information about the cxref run, +*/
51 extern char *run_command
, /*+ the command line options. +*/
52 *run_cpp_command
; /*+ the cpp command and options. +*/
54 extern char *latex_fonts_style
,*latex_page_style
,*latex_cxref_style
;
56 static void WriteLatexFilePart(File file
);
57 static void WriteLatexInclude(Include inc
);
58 static void WriteLatexSubInclude(Include inc
,int depth
);
59 static void WriteLatexDefine(Define def
);
60 static void WriteLatexTypedef(Typedef type
,char* filename
);
61 static void WriteLatexStructUnion(StructUnion su
,int depth
);
62 static void WriteLatexVariable(Variable var
,char* filename
);
63 static void WriteLatexFunction(Function func
,char* filename
);
65 static void WriteLatexDocument(char* name
,int appendix
);
66 static void WriteLatexTemplate(char* name
);
68 static char* latex(char* c
,int verbatim
);
70 /*+ The output file for the latex. +*/
73 /*+ Counts the lines in a table to insert breaks. +*/
74 static int countlines
=0;
77 /*++++++++++++++++++++++++++++++++++++++
78 Write a Latex file for a complete File structure and all components.
80 File file The File structure to output.
81 ++++++++++++++++++++++++++++++++++++++*/
83 void WriteLatexFile(File file
)
87 /* Write the including file. */
89 WriteLatexDocument(file
->name
,0);
93 ofile
=ConcatStrings(4,option_odir
,"/",file
->name
,LATEX_FILE
);
99 int i
,ofl
=strlen(ofile
);
101 for(i
=strlen(option_odir
)+1;i
<ofl
;i
++)
105 if(stat(ofile
,&stat_buf
))
106 mkdir(ofile
,S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
);
114 {fprintf(stderr
,"cxref: Failed to open the LaTeX output file '%s'\n",ofile
);exit(1);}
116 /* Write out a header. */
118 fputs("% This LaTeX file generated by cxref\n",of
);
119 fputs("% cxref program (c) Andrew M. Bishop 1995,96,97,98,99.\n",of
);
121 fprintf(of
,"%% Cxref: %s %s\n",run_command
,file
->name
);
122 fprintf(of
,"%% CPP : %s\n",run_cpp_command
);
125 /*+ The file structure is broken into its components and they are each written out. +*/
127 WriteLatexFilePart(file
);
131 Include inc
=file
->includes
;
132 fprintf(of
,"\n\\subsection*{Included Files}\n\n");
134 if(inc
!=file
->includes
)
135 fprintf(of
,"\\medskip\n");
136 WriteLatexInclude(inc
);
138 while((inc
=inc
->next
));
143 Define def
=file
->defines
;
144 fprintf(of
,"\n\\subsection*{Preprocessor definitions}\n\n");
146 if(def
!=file
->defines
)
147 fprintf(of
,"\\medskip\n");
148 WriteLatexDefine(def
);
150 while((def
=def
->next
));
155 Typedef type
=file
->typedefs
;
156 fprintf(of
,"\n\\subsection{Type definitions}\n\n");
158 WriteLatexTypedef(type
,file
->name
);
160 while((type
=type
->next
));
165 int any_to_mention
=0;
166 Variable var
=file
->variables
;
169 if(var
->scope
&(GLOBAL
|LOCAL
|EXTERNAL
|EXTERN_F
))
172 while((var
=var
->next
));
176 int first_ext
=1,first_local
=1;
177 Variable var
=file
->variables
;
178 fprintf(of
,"\n\\subsection{Variables}\n\n");
180 if(var
->scope
&GLOBAL
)
181 WriteLatexVariable(var
,file
->name
);
183 while((var
=var
->next
));
186 if(var
->scope
&(EXTERNAL
|EXTERN_F
) && !(var
->scope
&GLOBAL
))
189 {fprintf(of
,"\n\\subsubsection{External Variables}\n\n"); first_ext
=0;}
191 fprintf(of
,"\\medskip\n");
192 WriteLatexVariable(var
,file
->name
);
195 while((var
=var
->next
));
201 {fprintf(of
,"\n\\subsubsection{Local Variables}\n\n"); first_local
=0;}
203 fprintf(of
,"\\medskip\n");
204 WriteLatexVariable(var
,file
->name
);
207 while((var
=var
->next
));
213 Function func
=file
->functions
;
214 fprintf(of
,"\n\\subsection{Functions}\n\n");
216 if(func
->scope
&(GLOBAL
|EXTERNAL
))
217 WriteLatexFunction(func
,file
->name
);
219 while((func
=func
->next
));
220 func
=file
->functions
;
222 if(func
->scope
&LOCAL
)
223 WriteLatexFunction(func
,file
->name
);
225 while((func
=func
->next
));
230 /* Clear the memory in latex() */
232 latex(NULL
,0); latex(NULL
,0); latex(NULL
,0); latex(NULL
,0);
236 /*++++++++++++++++++++++++++++++++++++++
237 Write a File structure out.
239 File file The File to output.
240 ++++++++++++++++++++++++++++++++++++++*/
242 static void WriteLatexFilePart(File file
)
246 fprintf(of
,"\\markboth{File %s}{File %s}\n",latex(file
->name
,0),latex(file
->name
,0));
247 fprintf(of
,"\\section{File %s}\n",latex(file
->name
,0));
248 fprintf(of
,"\\label{file_%s}\n\n",file
->name
);
252 if(option_verbatim_comments
)
253 fprintf(of
,"\\begin{verbatim}\n%s\n\\end{verbatim}\n\n",latex(file
->comment
,1));
256 char *rcs1
=strstr(file
->comment
,"$Header"),*rcs2
=NULL
;
259 rcs2
=strstr(&rcs1
[1],"$");
263 fprintf(of
,"{\\bf RCS %s}\n\n",latex(&rcs1
[1],0));
264 fprintf(of
,"\\smallskip\n");
269 fprintf(of
,"%s\n\n",latex(&rcs2
[2],0));
271 fprintf(of
,"%s\n\n",latex(file
->comment
,0));
280 fprintf(of
,"\\medskip\n");
281 fprintf(of
,"\\begin{cxreftabii}\nIncluded in:");
282 for(i
=0;i
<file
->inc_in
->n
;i
++)
283 {/* Allow a break in every 8 (or so) items to allow the table to break over the page. */
284 if(min(i
,file
->inc_in
->n
-i
)%8 == 4)
285 fprintf(of
,"\\cxreftabbreak{cxreftabii}\n");
286 fprintf(of
,"\\ & %s & \\cxreffile{%s}\\\\\n",latex(file
->inc_in
->s
[i
],0),file
->inc_in
->s
[i
]);
288 fprintf(of
,"\\end{cxreftabii}\n\n");
291 if(file
->f_refs
->n
|| file
->v_refs
->n
)
294 fprintf(of
,"\\smallskip\n");
295 fprintf(of
,"\\begin{cxreftabiii}\n");
301 fprintf(of
,"Refs Func:");
303 for(i
=0;i
<file
->f_refs
->n
;i
++)
304 if(file
->f_refs
->s2
[i
])
306 fprintf(of
,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(file
->f_refs
->s1
[i
],0),latex(file
->f_refs
->s2
[i
],0),file
->f_refs
->s1
[i
],file
->f_refs
->s2
[i
]);
307 if(++tabcount
%8 == 4)
308 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
315 fprintf(of
,"\\ & \\cxreftabiiispan{");
316 for(i
=0;i
<file
->f_refs
->n
;i
++)
317 if(!file
->f_refs
->s2
[i
])
318 fprintf(of
,--others
?"%s(), ":"%s()",latex(file
->f_refs
->s1
[i
],0));
319 fprintf(of
,"} &\\\\\n");
327 fprintf(of
,"Refs Var:");
329 for(i
=0;i
<file
->v_refs
->n
;i
++)
330 if(file
->v_refs
->s2
[i
])
332 fprintf(of
,"\\ & %s & %s & \\cxrefvar{%s}{%s}\\\\\n",latex(file
->v_refs
->s1
[i
],0),latex(file
->v_refs
->s2
[i
],0),file
->v_refs
->s1
[i
],file
->v_refs
->s2
[i
]);
333 if(++tabcount
%8 == 4)
334 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
341 fprintf(of
,"\\ & \\cxreftabiiispan{");
342 for(i
=0;i
<file
->v_refs
->n
;i
++)
343 if(!file
->v_refs
->s2
[i
])
344 fprintf(of
,--others
?" %s,":" %s",latex(file
->v_refs
->s1
[i
],0));
345 fprintf(of
,"} &\\\\\n");
349 fprintf(of
,"\\end{cxreftabiii}\n\n");
354 /*++++++++++++++++++++++++++++++++++++++
355 Write an Include structure out.
357 Include inc The Include structure to output.
358 ++++++++++++++++++++++++++++++++++++++*/
360 static void WriteLatexInclude(Include inc
)
363 fprintf(of
,"%s\n\n\\smallskip\n",latex(inc
->comment
,0));
365 fprintf(of
,"\\begin{cxreftabi}\n"); countlines
=1;
367 if(inc
->scope
==LOCAL
)
368 fprintf(of
,"{\\stt \\#include \"%s\"} &\\cxreffile{%s}\\\\\n",latex(inc
->name
,0),inc
->name
);
370 fprintf(of
,"{\\stt \\#include <%s>} &\\\\\n",latex(inc
->name
,0));
373 WriteLatexSubInclude(inc
->includes
,1);
375 fprintf(of
,"\\end{cxreftabi}\n\n");
379 /*++++++++++++++++++++++++++++++++++++++
380 Write an Sub Include structure out. (An include structure that is included from another file.)
382 Include inc The Include structure to output.
384 int depth The depth of the include hierarchy.
385 ++++++++++++++++++++++++++++++++++++++*/
387 static void WriteLatexSubInclude(Include inc
,int depth
)
391 if(countlines
++%8==4)
392 fprintf(of
,"\\cxreftabbreak{cxreftabi}\n");
394 fprintf(of
,"\\hspace*{%3.1fin}",0.2*depth
);
396 if(inc
->scope
==LOCAL
)
397 fprintf(of
,"{\\stt \\#include \"%s\"} &\\cxreffile{%s}\\\\\n",latex(inc
->name
,0),inc
->name
);
399 fprintf(of
,"{\\stt \\#include <%s>} &\\\\\n",latex(inc
->name
,0));
402 WriteLatexSubInclude(inc
->includes
,depth
+1);
409 /*++++++++++++++++++++++++++++++++++++++
410 Write a Define structure out.
412 Define def The Define structure to output.
413 ++++++++++++++++++++++++++++++++++++++*/
415 static void WriteLatexDefine(Define def
)
421 fprintf(of
,"%s\n\n\\smallskip\n",latex(def
->comment
,0));
423 fprintf(of
,"{\\stt \\#define %s",latex(def
->name
,0));
426 fprintf(of
," %s",latex(def
->value
,0));
431 for(i
=0;i
<def
->args
->n
;i
++)
432 fprintf(of
,i
?", %s":"%s",latex(def
->args
->s1
[i
],0));
437 for(i
=0;i
<def
->args
->n
;i
++)
443 fprintf(of
,"\\smallskip\n");
444 fprintf(of
,"\\begin{cxrefarglist}\n");
445 for(i
=0;i
<def
->args
->n
;i
++)
446 fprintf(of
,"\\cxrefargitem{%s} %s\n",latex(def
->args
->s1
[i
],0),def
->args
->s2
[i
]?latex(def
->args
->s2
[i
],0):"\\ ");
447 fprintf(of
,"\\end{cxrefarglist}\n\n");
452 /*++++++++++++++++++++++++++++++++++++++
453 Write a Typedef structure out.
455 Typedef type The Typedef structure to output.
457 char* filename The name of the file that is being processed (required for the cross reference label).
458 ++++++++++++++++++++++++++++++++++++++*/
460 static void WriteLatexTypedef(Typedef type
,char* filename
)
463 fprintf(of
,"\n\\subsubsection{Typedef %s}\n",latex(type
->name
,0));
465 fprintf(of
,"\n\\subsubsection{Type %s}\n",latex(type
->name
,0));
467 if(!strncmp("enum",type
->name
,4))
468 fprintf(of
,"\\label{type_enum_%s_%s}\n\n",&type
->name
[5],filename
);
470 if(!strncmp("union",type
->name
,5))
471 fprintf(of
,"\\label{type_union_%s_%s}\n\n",&type
->name
[6],filename
);
473 if(!strncmp("struct",type
->name
,6))
474 fprintf(of
,"\\label{type_struct_%s_%s}\n\n",&type
->name
[7],filename
);
476 fprintf(of
,"\\label{type_%s_%s}\n\n",type
->name
,filename
);
479 fprintf(of
,"%s\n\n\\smallskip\n",latex(type
->comment
,0));
482 fprintf(of
,"{\\stt typedef %s}\n\n",latex(type
->type
,0));
486 fprintf(of
,"\\smallskip\n");
487 fprintf(of
,"\\begin{cxreftabiia}\n"); countlines
=0;
488 WriteLatexStructUnion(type
->sutype
,0);
489 fprintf(of
,"\\end{cxreftabiia}\n\n");
494 fprintf(of
,"\\smallskip\n");
495 fprintf(of
,"\\begin{cxreftabii}\n");
496 if(type
->typexref
->type
)
497 fprintf(of
,"See:& Typedef %s & \\cxreftype{%s}{%s}\\\\\n",latex(type
->typexref
->name
,0),type
->typexref
->name
,filename
);
499 if(!strncmp("enum",type
->typexref
->name
,4))
500 fprintf(of
,"See:& Type %s & \\cxreftype{enum_%s}{%s}\\\\\n",latex(type
->typexref
->name
,0),&type
->typexref
->name
[5],filename
);
502 if(!strncmp("union",type
->typexref
->name
,5))
503 fprintf(of
,"See:& Type %s & \\cxreftype{union_%s}{%s}\\\\\n",latex(type
->typexref
->name
,0),&type
->typexref
->name
[6],filename
);
505 if(!strncmp("struct",type
->typexref
->name
,6))
506 fprintf(of
,"See:& Type %s & \\cxreftype{struct_%s}{%s}\\\\\n",latex(type
->typexref
->name
,0),&type
->typexref
->name
[7],filename
);
507 fprintf(of
,"\\end{cxreftabii}\n\n");
512 /*++++++++++++++++++++++++++++++++++++++
513 Write a structure / union structure out.
515 StructUnion su The structure / union to write.
517 int depth The current depth within the structure.
518 ++++++++++++++++++++++++++++++++++++++*/
520 static void WriteLatexStructUnion(StructUnion su
, int depth
)
525 splitsu
=strstr(su
->name
,"{...}");
526 if(splitsu
) splitsu
[-1]=0;
528 if(countlines
++%8==4)
529 fprintf(of
,"\\cxreftabbreak{cxreftabiia}\n");
530 fprintf(of
,"\\hspace*{%3.1fin}",0.2*depth
);
532 if(depth
&& su
->comment
&& !su
->comps
)
533 fprintf(of
,"{\\stt %s;} & %s \\\\\n",latex(su
->name
,0),latex(su
->comment
,0));
534 else if(!depth
|| su
->comps
)
535 fprintf(of
,"{\\stt %s} &\\\\\n",latex(su
->name
,0));
537 fprintf(of
,"{\\stt %s;} &\\\\\n",latex(su
->name
,0));
539 if(!depth
|| su
->comps
)
541 fprintf(of
,"\\hspace*{%3.1fin}",0.1+0.2*depth
);
542 fprintf(of
,"{\\stt \\{} &\\\\\n");
544 for(i
=0;i
<su
->n_comp
;i
++)
545 WriteLatexStructUnion(su
->comps
[i
],depth
+1);
547 fprintf(of
,"\\hspace*{%3.1fin}",0.1+0.2*depth
);
548 fprintf(of
,"{\\stt \\}} &\\\\\n");
551 fprintf(of
,"\\hspace*{%3.1fin}",0.1+0.2*depth
);
552 if(depth
&& su
->comment
)
553 fprintf(of
,"{\\stt %s;} & %s \\\\\n",splitsu
[5]?latex(&splitsu
[6],0):"",latex(su
->comment
,0));
555 fprintf(of
,"{\\stt %s;} &\\\\\n",splitsu
[5]?latex(&splitsu
[6],0):"");
559 if(splitsu
) splitsu
[-1]=' ';
563 /*++++++++++++++++++++++++++++++++++++++
564 Write a Variable structure out.
566 Variable var The Variable structure to output.
568 char* filename The name of the file that is being processed (required for the cross reference label).
569 ++++++++++++++++++++++++++++++++++++++*/
571 static void WriteLatexVariable(Variable var
,char* filename
)
575 if(var
->scope
&GLOBAL
)
576 fprintf(of
,"\n\\subsubsection{Variable %s}\n",latex(var
->name
,0));
578 fprintf(of
,"{\\bf %s}\n",latex(var
->name
,0));
580 fprintf(of
,"\\label{var_%s_%s}\n\n",var
->name
,filename
);
583 fprintf(of
,"%s\n\n\\smallskip\n",latex(var
->comment
,0));
585 fprintf(of
,"{\\stt ");
588 fprintf(of
,"static ");
590 if(!(var
->scope
&GLOBAL
) && var
->scope
&(EXTERNAL
|EXTERN_F
))
591 fprintf(of
,"extern ");
593 fprintf(of
,"%s}\n\n",latex(var
->type
,0));
595 if(var
->scope
&(GLOBAL
|LOCAL
))
597 if(var
->incfrom
|| var
->used
->n
|| var
->visible
->n
)
599 fprintf(of
,"\\smallskip\n");
600 fprintf(of
,"\\begin{cxreftabiii}\n");
603 fprintf(of
,"Inc. from:& %s & \\ & \\cxrefvar{%s}{%s}\\\\\n",latex(var
->incfrom
,0),var
->name
,var
->incfrom
);
605 for(i
=0;i
<var
->visible
->n
;i
++)
607 if(min(i
,var
->visible
->n
+var
->used
->n
-i
)%8 == 4)
608 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
609 if(i
==0) fprintf(of
,"Visible in:");
610 if(var
->visible
->s1
[i
][0]=='$' && !var
->visible
->s1
[i
][1])
611 fprintf(of
,"\\ & %s & \\ & \\cxreffile{%s}\\\\\n",latex(var
->visible
->s2
[i
],0),var
->visible
->s2
[i
]);
613 fprintf(of
,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(var
->visible
->s1
[i
],0),latex(var
->visible
->s2
[i
],0),var
->visible
->s1
[i
],var
->visible
->s2
[i
]);
616 for(i
=0;i
<var
->used
->n
;i
++)
618 if(min(i
,var
->visible
->n
+var
->used
->n
-i
)%8 == 4)
619 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
620 if(i
==0) fprintf(of
,"Used in:");
621 if(var
->used
->s1
[i
][0]=='$' && !var
->used
->s1
[i
][1])
622 fprintf(of
,"\\ & %s & \\ & \\cxreffile{%s}\\\\\n",latex(var
->used
->s2
[i
],0),var
->used
->s2
[i
]);
625 fprintf(of
,"\\ & %s() & \\ & \\cxreffunc{%s}{%s}\\\\\n",latex(var
->used
->s1
[i
],0),var
->used
->s1
[i
],var
->used
->s2
[i
]);
627 fprintf(of
,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(var
->used
->s1
[i
],0),latex(var
->used
->s2
[i
],0),var
->used
->s1
[i
],var
->used
->s2
[i
]);
630 fprintf(of
,"\\end{cxreftabiii}\n\n");
634 if(var
->scope
&(EXTERNAL
|EXTERN_F
) && var
->defined
)
636 fprintf(of
,"\\smallskip\n");
637 fprintf(of
,"\\begin{cxreftabiii}\n");
638 fprintf(of
,"Defined in:& %s & \\ & \\cxrefvar{%s}{%s}\\\\\n",latex(var
->defined
,0),var
->name
,var
->defined
);
639 fprintf(of
,"\\end{cxreftabiii}\n\n");
644 /*++++++++++++++++++++++++++++++++++++++
645 Write a Function structure out.
647 Function func The Function structure to output.
649 char* filename The name of the file that is being processed (required for the cross reference label).
650 ++++++++++++++++++++++++++++++++++++++*/
652 static void WriteLatexFunction(Function func
,char* filename
)
655 char* comment2
=NULL
,*type
;
657 if(func
->scope
&(GLOBAL
|EXTERNAL
))
658 fprintf(of
,"\n\\subsubsection{Global Function %s()}\n",latex(func
->name
,0));
660 fprintf(of
,"\n\\subsubsection{Local Function %s()}\n",latex(func
->name
,0));
661 fprintf(of
,"\\label{func_%s_%s}\n\n",func
->name
,filename
);
665 if(option_verbatim_comments
)
666 fprintf(of
,"\\begin{verbatim}\n%s\n\\end{verbatim}\n\n",latex(func
->comment
,1));
669 comment2
=strstr(func
->comment
,"\n\n");
672 fprintf(of
,"%s\n\n",latex(func
->comment
,0));
673 fprintf(of
,"\\smallskip\n");
677 fprintf(of
,"{\\stt ");
679 if(func
->scope
&LOCAL
)
680 fprintf(of
,"static ");
681 if(func
->scope
&INLINED
)
682 fprintf(of
,"inline ");
684 if((type
=strstr(func
->type
,"()")))
686 fprintf(of
,"%s ( ",latex(func
->type
,0));
688 for(i
=0;i
<func
->args
->n
;i
++)
689 fprintf(of
,i
?", %s":"%s",latex(func
->args
->s1
[i
],0));
692 {fprintf(of
," %s}\n\n",&type
[1]);type
[0]='(';}
694 fprintf(of
," )}\n\n");
696 pret
=strncmp("void ",func
->type
,5) && func
->cret
;
697 for(pargs
=0,i
=0;i
<func
->args
->n
;i
++)
698 pargs
= pargs
|| ( strcmp("void",func
->args
->s1
[i
]) && func
->args
->s2
[i
] );
702 fprintf(of
,"\\smallskip\n");
703 fprintf(of
,"\\begin{cxrefarglist}\n");
705 fprintf(of
,"\\cxrefargitem{%s} %s\n",latex(func
->type
,0),func
->cret
?latex(func
->cret
,0):"\\ ");
707 for(i
=0;i
<func
->args
->n
;i
++)
708 fprintf(of
,"\\cxrefargitem{%s} %s\n",latex(func
->args
->s1
[i
],0),func
->args
->s2
[i
]?latex(func
->args
->s2
[i
],0):"\\ ");
709 fprintf(of
,"\\end{cxrefarglist}\n\n");
714 fprintf(of
,"\\smallskip\n");
715 fprintf(of
,"%s\n\n",latex(&comment2
[2],0));
719 if(func
->protofile
|| func
->incfrom
|| func
->calls
->n
|| func
->called
->n
|| func
->used
->n
|| func
->f_refs
->n
|| func
->v_refs
->n
)
721 int tabcount
=func
->protofile
?1:0;
722 fprintf(of
,"\\smallskip\n");
723 fprintf(of
,"\\begin{cxreftabiii}\n");
726 fprintf(of
,"Prototype:& %s & \\ & \\cxreffile{%s}\\\\\n",latex(func
->protofile
,0),func
->protofile
);
729 fprintf(of
,"Inc. from:& %s & \\ & \\cxreffunc{%s}{%s}\\\\\n",latex(func
->incfrom
,0),func
->name
,func
->incfrom
);
735 fprintf(of
,"Calls:");
737 for(i
=0;i
<func
->calls
->n
;i
++)
738 if(func
->calls
->s2
[i
])
740 fprintf(of
,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func
->calls
->s1
[i
],0),latex(func
->calls
->s2
[i
],0),func
->calls
->s1
[i
],func
->calls
->s2
[i
]);
741 if(++tabcount
%8 == 4)
742 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
749 fprintf(of
,"\\ & \\cxreftabiiispan{");
750 for(i
=0;i
<func
->calls
->n
;i
++)
751 if(!func
->calls
->s2
[i
])
752 fprintf(of
,--others
?" %s(),":" %s()",latex(func
->calls
->s1
[i
],0));
753 fprintf(of
,"} &\\\\\n");
759 fprintf(of
,"Called by:");
761 for(i
=0;i
<func
->called
->n
;i
++)
763 fprintf(of
,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func
->called
->s1
[i
],0),latex(func
->called
->s2
[i
],0),func
->called
->s1
[i
],func
->called
->s2
[i
]);
764 if(++tabcount
%8 == 4)
765 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
771 fprintf(of
,"Used in:");
773 for(i
=0;i
<func
->used
->n
;i
++)
775 if(func
->used
->s1
[i
][0]=='$' && !func
->used
->s1
[i
][1])
776 fprintf(of
,"\\ & %s & \\ & \\cxreffile{%s}\\\\\n",latex(func
->used
->s2
[i
],0),func
->used
->s2
[i
]);
778 fprintf(of
,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func
->used
->s1
[i
],0),latex(func
->used
->s2
[i
],0),func
->used
->s1
[i
],func
->used
->s2
[i
]);
779 if(++tabcount
%8 == 4)
780 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
788 fprintf(of
,"Refs Func:");
790 for(i
=0;i
<func
->f_refs
->n
;i
++)
791 if(func
->f_refs
->s2
[i
])
793 fprintf(of
,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(func
->f_refs
->s1
[i
],0),latex(func
->f_refs
->s2
[i
],0),func
->f_refs
->s1
[i
],func
->f_refs
->s2
[i
]);
794 if(++tabcount
%8 == 4)
795 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
802 fprintf(of
,"\\ & \\cxreftabiiispan{");
803 for(i
=0;i
<func
->f_refs
->n
;i
++)
804 if(!func
->f_refs
->s2
[i
])
805 fprintf(of
,--others
?" %s(),":" %s()",latex(func
->f_refs
->s1
[i
],0));
806 fprintf(of
,"} &\\\\\n");
814 fprintf(of
,"Refs Var:");
816 for(i
=0;i
<func
->v_refs
->n
;i
++)
817 if(func
->v_refs
->s2
[i
])
819 fprintf(of
,"\\ & %s & %s & \\cxrefvar{%s}{%s}\\\\\n",latex(func
->v_refs
->s1
[i
],0),latex(func
->v_refs
->s2
[i
],0),func
->v_refs
->s1
[i
],func
->v_refs
->s2
[i
]);
820 if(++tabcount
%8 == 4)
821 fprintf(of
,"\\cxreftabbreak{cxreftabiii}\n");
828 fprintf(of
,"\\ & \\cxreftabiiispan{");
829 for(i
=0;i
<func
->v_refs
->n
;i
++)
830 if(!func
->v_refs
->s2
[i
])
831 fprintf(of
,--others
?" %s,":" %s",latex(func
->v_refs
->s1
[i
],0));
832 fprintf(of
,"} &\\\\\n");
836 fprintf(of
,"\\end{cxreftabiii}\n\n");
841 /*++++++++++++++++++++++++++++++++++++++
842 Write out a file that will include the current information.
844 char* name The name of the file (without the LaTeX extension).
846 int appendix set to non-zero if the appendix file is to be added, else a normal source file.
847 ++++++++++++++++++++++++++++++++++++++*/
849 static void WriteLatexDocument(char* name
,int appendix
)
854 char *inc_file
,*ofile
,*ifile
;
856 inc_file
=ConcatStrings(4,"\\input{",name
,LATEX_FILE
,"}\n");
857 ifile
=ConcatStrings(4,option_odir
,"/",option_name
,LATEX_FILE
);
858 ofile
=ConcatStrings(4,option_odir
,"/",option_name
,LATEX_FILE_BACKUP
);
860 in
=fopen(ifile
,"r");
863 WriteLatexTemplate(ifile
);
864 in
=fopen(ifile
,"r");
867 out
=fopen(ofile
,"w");
870 {fprintf(stderr
,"cxref: Failed to open the main LaTeX output file '%s'\n",ofile
);exit(1);}
872 while(fgets(line
,256,in
))
874 if(!strcmp(inc_file
,line
) ||
875 (line
[0]=='%' && !strcmp(inc_file
,line
+1)) ||
876 (line
[0]=='%' && line
[1]==' ' && !strcmp(inc_file
,line
+2)))
878 if(line
[0]=='%' && !strcmp("% End-Of-Source-Files\n",line
))
884 fputs("% Appendix\n",out
);
886 fputs("\\appendix\n",out
);
887 fputs("\\markboth{Appendix}{Appendix}\n",out
);
914 /*++++++++++++++++++++++++++++++++++++++
915 Write out the standard template for the main LaTeX file.
916 This sets up the page styles, and includes markers for the start and end of included source code.
918 char* name The name of the file to write the template to.
919 ++++++++++++++++++++++++++++++++++++++*/
921 static void WriteLatexTemplate(char* name
)
924 struct stat stat_buf
;
927 template=fopen(name
,"w");
930 {fprintf(stderr
,"cxref: Failed to open the main LaTeX output file '%s'\n",name
);exit(1);}
932 fputs("% This LaTeX file generated by cxref\n",template);
933 fputs("% cxref program (c) Andrew M. Bishop 1995,96,97,98,99.\n",template);
934 fputs("\n",template);
936 fputs("\\documentstyle[fonts,page,cxref]{report}\n",template);
939 fputs("\\documentclass{report}\n",template);
940 fputs("\\usepackage{fonts,page,cxref}\n",template);
942 fputs("\\pagestyle{myheadings}\n",template);
943 fputs("\n",template);
944 fputs("\\begin{document}\n",template);
945 fputs("\n",template);
946 fputs("% Contents (Optional, either here or at end)\n",template);
947 fputs("\n",template);
948 fputs("%\\markboth{Contents}{Contents}\n",template);
949 fputs("%\\tableofcontents\n",template);
950 fputs("\n",template);
951 fputs("\\chapter{Source Files}\n",template);
952 fputs("\n",template);
953 fputs("% Begin-Of-Source-Files\n",template);
954 fputs("\n",template);
955 fputs("% End-Of-Source-Files\n",template);
956 fputs("\n",template);
957 fputs("% Contents (Optional, either here or at beginning)\n",template);
958 fputs("\n",template);
959 fputs("\\markboth{Contents}{Contents}\n",template);
960 fputs("\\tableofcontents\n",template);
961 fputs("\n",template);
962 fputs("\\end{document}\n",template);
966 fname
=ConcatStrings(2,option_odir
,"/fonts.sty");
967 if(stat(fname
,&stat_buf
))
969 FILE* file
=fopen(fname
,"w");
971 {fprintf(stderr
,"cxref: Cannot write the LaTeX style file '%s'\n",fname
);exit(1);}
972 fputs(latex_fonts_style
,file
);
976 fname
=ConcatStrings(2,option_odir
,"/page.sty");
977 if(stat(fname
,&stat_buf
))
979 FILE* file
=fopen(fname
,"w");
981 {fprintf(stderr
,"cxref: Cannot write the LaTeX style file '%s'\n",fname
);exit(1);}
982 fputs(latex_page_style
,file
);
986 fname
=ConcatStrings(2,option_odir
,"/cxref.sty");
987 if(stat(fname
,&stat_buf
))
989 FILE* file
=fopen(fname
,"w");
991 {fprintf(stderr
,"cxref: Cannot write the LaTeX style file '%s'\n",fname
);exit(1);}
992 fputs(latex_cxref_style
,file
);
998 /*++++++++++++++++++++++++++++++++++++++
999 Write out the appendix information.
1001 StringList files The list of files to write.
1003 StringList2 funcs The list of functions to write.
1005 StringList2 vars The list of variables to write.
1007 StringList2 types The list of types to write.
1008 ++++++++++++++++++++++++++++++++++++++*/
1010 void WriteLatexAppendix(StringList files
,StringList2 funcs
,StringList2 vars
,StringList2 types
)
1015 /* Write the bits to the including file. */
1017 WriteLatexDocument(ConcatStrings(2,option_name
,LATEX_APDX
),1);
1021 ofile
=ConcatStrings(5,option_odir
,"/",option_name
,LATEX_APDX
,LATEX_FILE
);
1023 of
=fopen(ofile
,"w");
1026 {fprintf(stderr
,"cxref: Failed to open the LaTeX appendix file '%s'\n",ofile
);exit(1);}
1028 /* Write out a header. */
1030 fputs("% This LaTeX file generated by cxref\n",of
);
1031 fputs("% cxref program (c) Andrew M. Bishop 1995,96,97,98,99.\n",of
);
1033 fprintf(of
,"%% Cxref: %s\n",run_command
);
1034 fprintf(of
,"%% CPP : %s\n",run_cpp_command
);
1037 /* Write the file structure out */
1039 fprintf(of
,"\\chapter{Cross References}\n");
1041 /* Write out the appendix of files. */
1045 fprintf(of
,"\n\\section{Files}\n");
1046 fprintf(of
,"\\label{appendix_file}\n\n");
1047 fprintf(of
,"\\begin{cxreftabiib}\n");
1048 for(i
=0;i
<files
->n
;i
++)
1050 if(min(i
,files
->n
-i
)%8 == 4)
1051 fprintf(of
,"\\cxreftabbreak{cxreftabiib}\n");
1052 fprintf(of
,"%s & \\ & \\cxreffile{%s}\\\\\n",latex(files
->s
[i
],0),files
->s
[i
]);
1054 fprintf(of
,"\\end{cxreftabiib}\n\n");
1057 /* Write out the appendix of functions. */
1061 fprintf(of
,"\n\\section{Global Functions}\n");
1062 fprintf(of
,"\\label{appendix_func}\n\n");
1063 fprintf(of
,"\\begin{cxreftabiib}\n");
1064 for(i
=0;i
<funcs
->n
;i
++)
1066 if(min(i
,funcs
->n
-i
)%8 == 4)
1067 fprintf(of
,"\\cxreftabbreak{cxreftabiib}\n");
1068 fprintf(of
,"%s & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(funcs
->s1
[i
],0),latex(funcs
->s2
[i
],0),funcs
->s1
[i
],funcs
->s2
[i
]);
1070 fprintf(of
,"\\end{cxreftabiib}\n\n");
1073 /* Write out the appendix of variables. */
1077 fprintf(of
,"\n\\section{Global Variables}\n");
1078 fprintf(of
,"\\label{appendix_var}\n\n");
1079 fprintf(of
,"\\begin{cxreftabiib}\n");
1080 for(i
=0;i
<vars
->n
;i
++)
1082 if(min(i
,vars
->n
-i
)%8 == 4)
1083 fprintf(of
,"\\cxreftabbreak{cxreftabiib}\n");
1084 fprintf(of
,"%s & %s & \\cxrefvar{%s}{%s}\\\\\n",latex(vars
->s1
[i
],0),latex(vars
->s2
[i
],0),vars
->s1
[i
],vars
->s2
[i
]);
1086 fprintf(of
,"\\end{cxreftabiib}\n\n");
1089 /* Write out the appendix of types. */
1093 fprintf(of
,"\n\\section{Defined Types}\n");
1094 fprintf(of
,"\\label{appendix_type}\n\n");
1095 fprintf(of
,"\\begin{cxreftabiib}\n");
1096 for(i
=0;i
<types
->n
;i
++)
1098 if(min(i
,types
->n
-i
)%8 == 4)
1099 fprintf(of
,"\\cxreftabbreak{cxreftabiib}\n");
1100 if(!strncmp("enum",types
->s1
[i
],4))
1101 fprintf(of
,"%s & %s & \\cxreftype{enum_%s}{%s}\\\\\n",latex(types
->s1
[i
],0),latex(types
->s2
[i
],0),&types
->s1
[i
][5],types
->s2
[i
]);
1103 if(!strncmp("union",types
->s1
[i
],5))
1104 fprintf(of
,"%s & %s & \\cxreftype{union_%s}{%s}\\\\\n",latex(types
->s1
[i
],0),latex(types
->s2
[i
],0),&types
->s1
[i
][6],types
->s2
[i
]);
1106 if(!strncmp("struct",types
->s1
[i
],6))
1107 fprintf(of
,"%s & %s & \\cxreftype{struct_%s}{%s}\\\\\n",latex(types
->s1
[i
],0),latex(types
->s2
[i
],0),&types
->s1
[i
][7],types
->s2
[i
]);
1109 fprintf(of
,"%s & %s & \\cxreftype{%s}{%s}\\\\\n",latex(types
->s1
[i
],0),latex(types
->s2
[i
],0),types
->s1
[i
],types
->s2
[i
]);
1111 fprintf(of
,"\\end{cxreftabiib}\n\n");
1116 /* Clear the memory in latex(,0) */
1118 latex(NULL
,0); latex(NULL
,0); latex(NULL
,0); latex(NULL
,0);
1122 /*++++++++++++++++++++++++++++++++++++++
1123 Delete the latex file and main file reference that belong to the named file.
1125 char *name The name of the file to delete.
1126 ++++++++++++++++++++++++++++++++++++++*/
1128 void WriteLatexFileDelete(char *name
)
1133 char *inc_file
,*ofile
,*ifile
;
1135 ofile
=ConcatStrings(4,option_odir
,"/",name
,LATEX_FILE
);
1138 inc_file
=ConcatStrings(4,"\\input{",name
,LATEX_FILE
,"}\n");
1139 ifile
=ConcatStrings(4,option_odir
,"/",option_name
,LATEX_FILE
);
1140 ofile
=ConcatStrings(4,option_odir
,"/",option_name
,LATEX_FILE_BACKUP
);
1142 in
=fopen(ifile
,"r");
1143 out
=fopen(ofile
,"w");
1146 {fprintf(stderr
,"cxref: Failed to open the main LaTeX output file '%s'\n",ofile
);fclose(in
);}
1149 while(fgets(line
,256,in
))
1151 if(!strcmp(inc_file
,line
) ||
1152 (line
[0]=='%' && !strcmp(inc_file
,line
+1)) ||
1153 (line
[0]=='%' && line
[1]==' ' && !strcmp(inc_file
,line
+2)))
1165 rename(ofile
,ifile
);
1178 /*++++++++++++++++++++++++++++++++++++++
1179 Make the input string safe to output as LaTeX ( not #, $, %, &, \, ^, _, {, }, <, > or ~ ).
1181 char* latex Returns a safe LaTeX string.
1183 char* c A non-safe LaTeX string.
1185 int verbatim Set to true inside a verbatim environment.
1187 The function can only be called four times in each fprintf() since it returns one of only four static strings.
1188 ++++++++++++++++++++++++++++++++++++++*/
1190 static char* latex(char* c
,int verbatim
)
1192 static char safe
[4][256],*malloced
[4]={NULL
,NULL
,NULL
,NULL
};
1195 int i
=0,j
=0,delta
=13,len
=256-delta
;
1204 {Free(malloced
[which
]);malloced
[which
]=NULL
;}
1208 i
=CopyOrSkip(c
,"latex",©
,&skip
);
1212 for(;j
<len
&& c
[i
];i
++)
1215 {ret
[j
++]=c
[i
]; if(c
[i
]=='\n') copy
=0;}
1217 { if(c
[i
]=='\n') skip
=0;}
1230 strcpy(&ret
[j
],"$\\backslash$");j
+=12;
1233 strcpy(&ret
[j
],"$\\sim$");j
+=6;
1236 strcpy(&ret
[j
],"$\\wedge$");j
+=8;
1252 i
+=CopyOrSkip(c
+i
,"latex",©
,&skip
);
1255 if(c
[i
]) /* Not finished */
1258 malloced
[which
]=Realloc(malloced
[which
],len
+delta
+256);
1260 {malloced
[which
]=Malloc(len
+delta
+256); strncpy(malloced
[which
],ret
,(unsigned)j
);}
1261 ret
=malloced
[which
];