9baa9513ce66be85291cf6861423dc481c73839f
[alexxy/gromacs.git] / src / gmxlib / wman.c
1 /*
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  * 
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * GROningen Mixture of Alchemy and Childrens' Stories
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "string2.h"
40 #include "smalloc.h"
41 #include "sysstuff.h"
42 #include "filenm.h"
43 #include "macros.h"
44 #include "replace.h"
45 #include "wman.h"
46 #include "statutil.h"
47 #include "copyrite.h"
48 #include "strdb.h"
49 #include "time.h"
50 #include "readinp.h"
51
52 /* The source code in this file should be thread-safe. 
53          Please keep it that way. */
54
55
56 typedef struct {
57   const char *search,*replace;
58 } t_sandr_const;
59
60 typedef struct {
61   char *search,*replace;
62 } t_sandr;
63
64 const t_sandr_const sandrTeX[] = {
65   { "[TT]", "{\\tt " },
66   { "[tt]", "}"      },
67   { "[BB]", "{\\bf " },
68   { "[bb]", "}"      },
69   { "[IT]", "{\\em " },
70   { "[it]", "}"      },
71   { "[PAR]","\n\n"   },
72   { "_",    "\\_"    },
73   { "$",    "\\$"    },
74   { "<",    "$<$"    },
75   { ">",    "$>$"    },
76   { "^",    "\\^"    },
77   { "\\^2",   "$^2$" },
78   { "\\^3",   "$^3$" },
79   { "\\^6",   "$^6$" },
80   { "#",    "\\#"    },
81   { "[BR]", "\\\\"   },
82   { "%",    "\\%"    },
83   { "&",    "\\&"    },
84   { "||",    "or"    },
85   { "|",     "or"    }
86 };
87 #define NSRTEX asize(sandrTeX)
88
89 const t_sandr_const sandrTty[] = {
90   { "[TT]", "" },
91   { "[tt]", "" },
92   { "[BB]", "" },
93   { "[bb]", "" },
94   { "[IT]", "" },
95   { "[it]", "" },
96   { "[PAR]","\n\n" },
97   { "[BR]", "\n"}
98 };
99 #define NSRTTY asize(sandrTty)
100
101 const t_sandr_const sandrWiki[] = {
102   { "&",    "&amp;" },
103   { "<",    "&lt;" },
104   { ">",    "&gt;" },
105   { "[TT]", "&lt;code&gt;" },
106   { "[tt]", "&lt;/code&gt;" },
107   { "[BB]", "'''" },
108   { "[bb]", "'''" },
109   { "[IT]", "''" },
110   { "[it]", "''" },
111   { "[PAR]","\n\n" },
112   { "[BR]", "\n"}
113 };
114 #define NSRWIKI asize(sandrWiki)
115
116 const t_sandr_const sandrNROFF[] = {
117   { "[TT]", "\\fB " },
118   { "[tt]", "\\fR" },
119   { "[BB]", "\\fB " },
120   { "[bb]", "\\fR" },
121   { "[IT]", "\\fI " },
122   { "[it]", "\\fR" },
123   { "[PAR]","\n\n" },
124   { "\n ",    "\n" },
125   { "<",    "" },
126   { ">",    "" },
127   { "^",    "" },
128   { "#",    "" },
129   { "[BR]", "\n"},
130   { "-",    "\\-"}
131 };
132 #define NSRNROFF asize(sandrNROFF)
133
134 const t_sandr_const sandrHTML[] = {
135   { "<",    "&lt;" },
136   { ">",    "&gt;" },
137   { "[TT]", "<tt>" },
138   { "[tt]", "</tt>" },
139   { "[BB]", "<b>" },
140   { "[bb]", "</b>" },
141   { "[IT]", "<it>" },
142   { "[it]", "</it>" },
143   { "[PAR]","<p>" },
144   { "[BR]", "<br>" }
145 };
146 #define NSRHTML asize(sandrHTML)
147
148 const t_sandr_const sandrXML[] = {
149   { "<",    "&lt;" },
150   { ">",    "&gt;" },
151   { "[TT]", "<arg>" },
152   { "[tt]", "</arg>" },
153   { "[BB]", "<emp>" },
154   { "[bb]", "</emp>" },
155   { "[IT]", "<it>" },
156   { "[it]", "</it>" },
157   { "[PAR]","</par>\n<par>" },
158   { "[BR]", "<br />" }
159 };
160 #define NSRXML asize(sandrXML)
161
162 static void mynum(char *buf,int n)
163 {
164   if (n >= 10)
165     sprintf(buf,"%2d",n);
166   else
167     sprintf(buf,"0%1d",n);
168 }
169
170 static char *mydate(char buf[], int maxsize,bool bWiki)
171 {
172   const char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
173                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
174   const char *day[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
175   const char *num[] = { "01", "02", "03", "04", "05", "06","07", "08", "09" }; 
176   time_t now;
177   struct tm *tm;
178   
179   (void) time(&now);
180   tm = localtime(&now);
181   /* subtract one from maxsize, so we have room for \0. */
182   if (bWiki) {
183     char dd[8],mm[8],ss[8],hh[8],mn[8];
184     
185     mynum(dd,tm->tm_mday);
186     mynum(mm,tm->tm_mon);
187     mynum(ss,tm->tm_sec);
188     mynum(hh,tm->tm_hour);
189     mynum(mn,tm->tm_min);
190     sprintf(buf,"%4d-%2s-%2sT%2s:%2s:%2sZ",
191              tm->tm_year+1900,mm,dd,hh,mn,ss);
192   }
193   else
194     sprintf(buf,"%s %d %s %d",day[tm->tm_wday],tm->tm_mday,
195              mon[tm->tm_mon],tm->tm_year+1900);
196   
197   return buf;
198 }
199
200 /* Data structure for saved HTML links */
201 typedef struct {
202   int     nsr;
203   t_sandr *sr;
204 } t_linkdata;
205
206 static t_linkdata *init_linkdata()
207 {
208   t_linkdata *p;
209   snew(p,1);
210   p->sr=NULL;
211   p->nsr=0;
212
213   return p;
214 }
215
216 static void finish_linkdata(t_linkdata *p)
217 {
218   int i;
219   
220   for(i=0;i<p->nsr;i++) {
221     sfree(p->sr[i].search);    
222     sfree(p->sr[i].replace);
223   }
224   sfree(p->sr);
225   sfree(p);
226 }
227
228 static char *repall(const char *s,int nsr,const t_sandr_const sa[])
229 {
230   int  i;
231   char *buf1,*buf2;
232   
233   /* Copy input to a non-constant char buffer.
234    * buf1 is allocated here 
235    */
236   buf1=strdup(s); 
237   
238   for(i=0; (i<nsr); i++) {
239     /* Replace in buffer1, put result in buffer2.
240      * buf2 is allocated here.
241      */
242     buf2=replace(buf1,sa[i].search,sa[i].replace);
243     sfree(buf1);
244     buf1=buf2;
245   }
246   
247   return buf1;
248
249
250 static char *repallww(const char *s,int nsr,const t_sandr sa[])
251 {
252   int  i;
253   char *buf1,*buf2;
254
255   /* Copy input to a non-constant char buffer.
256    * buf1 is allocated here 
257    */
258   buf1=strdup(s); 
259   
260   for(i=0; (i<nsr); i++) {
261     /* Replace in buffer1, put result in buffer2.
262      * buf2 is allocated here.
263      */
264     buf2=replaceww(buf1,sa[i].search,sa[i].replace);
265     sfree(buf1);
266     buf1=buf2;
267   }
268   return buf1;
269 }
270
271 static char *html_xref(char *s,const char *program, t_linkdata *links,bool bWiki)
272 {
273   char   buf[256],**filestr;
274   int    i,j,n;
275   
276   if (links->sr == NULL) {
277     n=get_file("links.dat",&(filestr));
278     links->nsr=n;
279     snew(links->sr,n);
280     for(i=0,j=0; (i<n); i++) {
281       if (!program || (strcasecmp(program,filestr[i])  != 0)) {
282         links->sr[j].search=strdup(filestr[i]);
283         if (bWiki)
284           sprintf(buf,"[[%s]]",filestr[i]);
285         else
286           sprintf(buf,"<a href=\"%s.html\">%s</a>",filestr[i],filestr[i]);
287         links->sr[j].replace=strdup(buf);
288         j++;
289       }
290     }
291     links->nsr=j;
292     for(i=0;i<n;i++)
293       sfree(filestr[i]);
294     sfree(filestr);
295   }
296   return repallww(s,links->nsr,links->sr);
297 }
298
299 char *check_tex(const char *s)
300 {
301   return repall(s,NSRTEX,sandrTeX);
302 }
303
304 static char *check_nroff(const char *s)
305 {
306   return repall(s,NSRNROFF,sandrNROFF);
307 }
308
309 static char *check_wiki(const char *s,const char *program, t_linkdata *links)
310 {
311   char *buf;
312   
313   buf = repall(s,NSRWIKI,sandrWiki);
314   buf = html_xref(buf,program,links,TRUE);
315   
316   return buf;
317 }
318
319 static char *check_html(const char *s,const char *program, t_linkdata *links)
320 {
321   char *buf;
322   
323   buf = repall(s,NSRHTML,sandrHTML);
324   buf = html_xref(buf,program,links,FALSE);
325   
326   return buf;
327 }
328
329 #define NWR(s) check_wiki(s,program,links)
330 #define NSR(s) check_html(s,program,links)
331   
332 #define FLAG_SET(flag, mask) ((flag & mask) == mask)
333 char *fileopt(unsigned long flag,char buf[],int maxsize)
334 {
335   char tmp[256];
336   
337   if (FLAG_SET(flag, ffRW))
338     sprintf(tmp,"In/Out");
339   else if (FLAG_SET(flag, ffREAD))
340     sprintf(tmp,"Input");
341   else if (FLAG_SET(flag, ffWRITE))
342     sprintf(tmp,"Output");
343   else
344     sprintf(tmp,"Dunno");
345
346   if (FLAG_SET(flag, ffOPT)) {
347     strcat(tmp,", Opt");
348     if (FLAG_SET(flag, ffSET)) 
349       strcat(tmp,"!");
350     else
351       strcat(tmp,".");
352   }
353   if (FLAG_SET(flag, ffLIB))
354     strcat(tmp,", Lib.");
355   if (FLAG_SET(flag, ffMULT))
356     strcat(tmp,", Mult.");
357
358   sprintf(buf,"%s",tmp);
359   
360   return buf;
361 }
362
363 static void write_texman(FILE *out,const char *program,
364                          int nldesc,const char **desc,
365                          int nfile,t_filenm *fnm,
366                          int npargs,t_pargs *pa,
367                          int nbug,const char **bugs,
368                          t_linkdata *links)
369 {
370   int i;
371   char tmp[256];
372   
373   fprintf(out,"\\section{\\normindex{%s}}\n\n",check_tex(program));
374   
375   if (nldesc > 0)
376     for(i=0; (i<nldesc); i++) 
377       fprintf(out,"%s\n",check_tex(desc[i]));
378
379   if (nfile > 0) {
380     fprintf(out,"\\vspace{-2ex}\\begin{tabbing}\n");
381     fprintf(out,"\n{\\normalsize \\bf Files}\\nopagebreak\\\\\n");
382     fprintf(out,"{\\tt ~~~~~~~} \\= {\\tt ~~~~~~~~~~~~~~} \\= "
383             "~~~~~~~~~~~~~~~~~~~~~~ \\= \\nopagebreak\\kill\n");
384     for(i=0; (i<nfile); i++)
385       fprintf(out,"\\>{\\tt %s} \\'\\> {\\tt %s} \\' %s \\> "
386               "\\parbox[t]{0.55\\linewidth}{%s} \\\\\n",
387               check_tex(fnm[i].opt),check_tex(fnm[i].fns[0]),
388               check_tex(fileopt(fnm[i].flag,tmp,255)),
389               check_tex(ftp2desc(fnm[i].ftp)));
390     fprintf(out,"\\end{tabbing}\\vspace{-4ex}\n");
391   }
392   if (npargs > 0) {
393     fprintf(out,"\\vspace{-2ex}\\begin{tabbing}\n");
394     fprintf(out,"\n{\\normalsize \\bf Other options}\\nopagebreak\\\\\n");
395     fprintf(out,"{\\tt ~~~~~~~~~~} \\= vector \\= "
396             "{\\tt ~~~~~~~} \\= \\nopagebreak\\kill\n");
397     for(i=0; (i<npargs); i++) {
398       if (strlen(check_tex(pa_val(&(pa[i]),tmp,255))) <= 8)
399         fprintf(out,"\\> {\\tt %s} \\'\\> %s \\'\\> {\\tt %s} \\' "
400                 "\\parbox[t]{0.68\\linewidth}{%s}\\\\\n",
401                 check_tex(pa[i].option),argtp[pa[i].type],
402                 check_tex(pa_val(&(pa[i]),tmp,255)),
403                 check_tex(pa[i].desc));
404       else
405         fprintf(out,"\\> {\\tt %s} \\'\\> %s \\'\\>\\\\\n"
406                 "\\> \\'\\> \\'\\> {\\tt %s} \\' "
407                 "\\parbox[t]{0.7\\linewidth}{%s}\\\\\n",
408                 check_tex(pa[i].option),argtp[pa[i].type],
409                 check_tex(pa_val(&(pa[i]),tmp,255)),
410                 check_tex(pa[i].desc));
411     }
412     fprintf(out,"\\end{tabbing}\\vspace{-4ex}\n");
413   }
414   if (nbug > 0) {
415     fprintf(out,"\n");
416     fprintf(out,"\\begin{itemize}\n");
417     for(i=0; (i<nbug); i++)
418       fprintf(out,"\\item %s\n",check_tex(bugs[i]));
419     fprintf(out,"\\end{itemize}\n");
420   }
421 /*   fprintf(out,"\n\\newpage\n"); */
422 }
423
424 static void write_nroffman(FILE *out,
425                            const char *program,
426                            int nldesc,const char **desc,
427                            int nfile,t_filenm *fnm,
428                            int npargs,t_pargs *pa,
429                            int nbug,const char **bugs,
430                            t_linkdata *links)
431
432 {
433   int i;
434   char tmp[256];
435   
436   
437   fprintf(out,".TH %s 1 \"%s\" \"\" \"GROMACS suite, %s\"\n",program,mydate(tmp,255,FALSE),GromacsVersion());
438   fprintf(out,".SH NAME\n");
439   fprintf(out,"%s\n",program);
440   fprintf(out,".B %s\n",GromacsVersion());
441   
442   fprintf(out,".SH SYNOPSIS\n");
443   fprintf(out,"\\f3%s\\fP\n",program);
444
445   /* command line arguments */
446   if (nfile > 0) {
447     for(i=0; (i<nfile); i++)
448       fprintf(out,".BI \"%s\" \" %s \"\n",check_nroff(fnm[i].opt),
449               check_nroff(fnm[i].fns[0]));
450   }
451   if (npargs > 0) {
452     for(i=0; (i<npargs); i++)
453       if (pa[i].type == etBOOL)
454         fprintf(out,".BI \"\\-[no]%s\" \"\"\n",check_nroff(pa[i].option+1));
455       else
456         fprintf(out,".BI \"%s\" \" %s \"\n",check_nroff(pa[i].option),
457                 check_nroff(argtp[pa[i].type]));
458   }
459   
460   /* description */
461   if (nldesc > 0) {
462     fprintf(out,".SH DESCRIPTION\n");
463     for(i=0; (i<nldesc); i++) 
464       fprintf(out,"\\&%s\n",check_nroff(desc[i]));
465   }
466
467   /* FILES */
468   if (nfile > 0) {
469     fprintf(out,".SH FILES\n");
470     for(i=0; (i<nfile); i++)
471       fprintf(out,".BI \"%s\" \" %s\" \n.B %s\n %s \n\n",
472               check_nroff(fnm[i].opt),
473               check_nroff(fnm[i].fns[0]),
474               check_nroff(fileopt(fnm[i].flag,tmp,255)),
475               check_nroff(ftp2desc(fnm[i].ftp)));
476   }
477   
478   /* other options */
479   fprintf(out,".SH OTHER OPTIONS\n");
480   if ( npargs > 0 ) {
481     for(i=0; (i<npargs); i++) {
482       if (pa[i].type == etBOOL)
483         fprintf(out,".BI \"\\-[no]%s\"  \"%s\"\n %s\n\n",
484                 check_nroff(pa[i].option+1),
485                 check_nroff(pa_val(&(pa[i]),tmp,255)),
486                 check_nroff(pa[i].desc));
487       else
488         fprintf(out,".BI \"%s\"  \" %s\" \" %s\" \n %s\n\n",
489                 check_nroff(pa[i].option),
490                 check_nroff(argtp[pa[i].type]),
491                 check_nroff(pa_val(&(pa[i]),tmp,255)),
492                 check_nroff(pa[i].desc));
493     }
494   }
495
496   if (nbug > 0) {
497     fprintf(out,".SH KNOWN PROBLEMS\n");
498     for(i=0; (i<nbug); i++)
499       fprintf(out,"\\- %s\n\n",check_nroff(bugs[i]));
500   }
501
502   fprintf(out,".SH SEE ALSO\n.BR gromacs(7)\n\n");
503   fprintf(out,"More information about \\fBGROMACS\\fR is available at <\\fIhttp://www.gromacs.org/\\fR>.\n");
504
505 }
506
507 char *check_tty(const char *s)
508 {
509   return repall(s,NSRTTY,sandrTty);
510 }
511
512 static void
513 print_tty_formatted(FILE *out, int nldesc, const char **desc,int indent,
514                     t_linkdata *links,const char *program,bool bWiki)
515 {
516   char *buf;
517   char *temp;
518   int buflen,i,j;
519
520   buflen = 80*nldesc;
521   snew(buf,buflen);
522   for(i=0; (i<nldesc); i++) {
523     if ((strlen(buf)>0) && 
524         (buf[strlen(buf)-1] !=' ') && (buf[strlen(buf)-1] !='\n'))
525       strcat(buf," ");
526     if (bWiki)
527       temp=NWR(desc[i]);
528     else
529       temp=check_tty(desc[i]);
530     if (strlen(buf) + strlen(temp) >= buflen-2) {
531       buflen += strlen(temp);
532       srenew(buf,buflen);
533     }
534     strcat(buf,temp);
535     sfree(temp);
536   }
537   /* Make lines of at most 79 characters */
538   temp = wrap_lines(buf,78,indent,FALSE);
539   fprintf(out,"%s\n",temp);
540   sfree(temp);
541   /* sfree(buf);*/
542 }
543
544 static void write_ttyman(FILE *out,
545                          const char *program,
546                          int nldesc,const char **desc,
547                          int nfile,t_filenm *fnm,
548                          int npargs,t_pargs *pa,
549                          int nbug,const char **bugs,bool bHeader,
550                          t_linkdata *links)
551 {
552   int i;
553   char buf[256];
554   char *tmp;
555   
556   if (bHeader) {
557     fprintf(out,"%s\n\n",check_tty(program));
558     fprintf(out,"%s\n%s\n",GromacsVersion(),mydate(buf,255,FALSE));
559   }
560   if (nldesc > 0) {
561     fprintf(out,"DESCRIPTION\n-----------\n");
562     print_tty_formatted(out,nldesc,desc,0,links,program,FALSE);
563   }
564   if (nbug > 0) {
565     fprintf(out,"\n");
566     fprintf(out,"KNOWN PROBLEMS\n----------\n");
567     for(i=0; i<nbug; i++) {
568       snew(tmp,strlen(bugs[i])+3);
569       strcpy(tmp,"* ");
570       strcpy(tmp+2,check_tty(bugs[i]));
571       fprintf(out,"%s\n",wrap_lines(tmp,78,2,FALSE));
572       sfree(tmp);
573     }
574   }
575   if (nfile > 0) {
576     fprintf(out,"\n");
577     pr_fns(out,nfile,fnm);
578   }
579   if (npargs > 0) {
580     print_pargs(out,npargs,pa,FALSE);
581   }
582 }
583
584 static void pr_html_files(FILE *out,int nfile,t_filenm fnm[],
585                           const char *program,t_linkdata *links,bool bWiki)
586
587   int  i;
588   char link[10],tmp[255];
589   
590   if (bWiki)
591     fprintf(out," %-10s %-12s %-12s %-s\n"
592             " -----------------------------------------------------\n",
593             "Option","Filename","Type","Description");
594   else
595     fprintf(out,
596             "<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2>\n"
597             "<TR>"
598             "<TH>option</TH>"
599             "<TH>filename</TH>"
600             "<TH>type</TH>"
601             "<TH>description</TH>"
602             "</TR>\n");
603   
604   for(i=0; (i<nfile); i++) {
605     strcpy(link,ftp2ext(fnm[i].ftp));
606     if (strcmp(link,"???")==0)
607       strcpy(link,"files");
608     if (bWiki)
609       fprintf(out," %-10s %-16s %-12s %-s\n",
610               fnm[i].opt,
611               NWR(fnm[i].fns[0]),
612               fileopt(fnm[i].flag,tmp,255),
613               NWR(ftp2desc(fnm[i].ftp)));
614     else
615       fprintf(out,
616               "<TR>"
617               "<TD ALIGN=RIGHT> <b><tt>%s</tt></b> </TD>"
618               "<TD ALIGN=RIGHT> <tt><a href=\"%s.html\">%12s</a></tt> </TD>"
619               "<TD> %s </TD>"
620               "<TD> %s </TD>"
621               "</TR>\n",
622               fnm[i].opt,link,fnm[i].fns[0],fileopt(fnm[i].flag,tmp,255),
623               NSR(ftp2desc(fnm[i].ftp)));
624   }
625   if (!bWiki)
626     fprintf(out,"</TABLE>\n");
627 }
628
629 static void write_wikiman(FILE *out,
630                           const char *program,
631                           int nldesc,const char **desc,
632                           int nfile,t_filenm *fnm,
633                           int npargs,t_pargs *pa,
634                           int nbug,const char **bugs,bool bHeader,
635                           t_linkdata *links)
636 {
637   int i;
638   char buf[256],link[10];
639   char *tmp,*tmp2;
640   fprintf(out,"<page>\n<title>Manual:%s_%s</title>\n",program,
641           VERSION);
642   fprintf(out,"<revision>\n");
643   fprintf(out,"<timestamp>%s</timestamp>\n",mydate(buf,255,TRUE));
644   fprintf(out,"<text xml:space=\"preserve\">\n");
645   if (nldesc > 0) {
646     fprintf(out,"== Description ==\n");
647     print_tty_formatted(out,nldesc,desc,0,links,program,TRUE);
648     fprintf(out,"\n");
649   }
650   if (nbug > 0) {
651     fprintf(out,"== Known Problems ==\n");
652     for(i=0; i<nbug; i++) {
653       snew(tmp,strlen(bugs[i])+3);
654       strcpy(tmp,"* ");
655       strcpy(tmp+2,bugs[i]);
656       fprintf(out,"%s\n",NWR(tmp));
657       sfree(tmp);
658     }
659   }
660   if (nfile > 0) {
661     fprintf(out,"\n== Files ==\n");
662     pr_html_files(out,nfile,fnm,program,links,TRUE);
663   }
664   if (npargs > 0) {
665     fprintf(out,"\n== Options ==\n");
666     fprintf(out," %-12s %-6s %-6s  %-s\n",
667             "Option","Type","Value","Description");
668     fprintf(out," ------------------------------------------------------\n");
669     for(i=0; (i<npargs); i++) {
670       tmp = NWR(pargs_print_line(&pa[i],TRUE));
671       fprintf(out,"%s",tmp);
672       sfree(tmp);
673     }
674   }
675   fprintf(out,"[[category:Manual_Pages_%s|%s]]\n",VERSION,program);
676   fprintf(out,"</text>\n");
677   fprintf(out,"</revision>\n");
678   fprintf(out,"</page>\n\n");
679 }
680
681 static void write_htmlman(FILE *out,
682                           const char *program,
683                           int nldesc,const char **desc,
684                           int nfile,t_filenm *fnm,
685                           int npargs,t_pargs *pa,
686                           int nbug,const char **bugs,
687                           t_linkdata *links)
688 {
689   int i;
690   char link[10],tmp[255];
691   
692   fprintf(out,"<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n",program);
693   fprintf(out,"<LINK rel=stylesheet href=\"style.css\" type=\"text/css\">\n");
694   fprintf(out,"<BODY text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#0000FF\" vlink=\"#990000\" alink=\"#FF0000\">\n");
695   fprintf(out,"<TABLE WIDTH=\"98%%\" NOBORDER >\n<TR><TD WIDTH=400>\n");
696   fprintf(out,"<TABLE WIDTH=400 NOBORDER>\n<TD WIDTH=116>\n");
697   fprintf(out,"<a href=\"http://www.gromacs.org/\">"
698           "<img SRC=\"../images/gmxlogo_small.png\""
699           "BORDER=0 </a></td>\n");
700   fprintf(out,"<td ALIGN=LEFT VALIGN=TOP WIDTH=280>"
701           "<br><h2>%s</h2>",program);
702   fprintf(out,"<font size=-1><A HREF=\"../online.html\">Main Table of Contents</A></font><br>");
703   fprintf(out,"<br></td>\n</TABLE></TD><TD WIDTH=\"*\" ALIGN=RIGHT VALIGN=BOTTOM><p><B>%s<br>\n",GromacsVersion());
704   fprintf(out,"%s</B></td></tr></TABLE>\n<HR>\n",mydate(tmp,255,FALSE));
705   
706   if (nldesc > 0) {
707     fprintf(out,"<H3>Description</H3>\n<p>\n");
708     for(i=0; (i<nldesc); i++) 
709       fprintf(out,"%s\n",NSR(desc[i]));
710   }
711   if (nfile > 0) {
712     fprintf(out,"<P>\n");
713     fprintf(out,"<H3>Files</H3>\n");
714     pr_html_files(out,nfile,fnm,program,links,FALSE);
715   }
716   if (npargs > 0) {
717     fprintf(out,"<P>\n");
718     fprintf(out,"<H3>Other options</H3>\n");
719     fprintf(out,
720             "<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2>\n"
721             "<TR>"
722             "<TH>option</TH>"
723             "<TH>type</TH>"
724             "<TH>default</TH>"
725             "<TH>description</TH>"
726             "</TR>\n");
727     for(i=0; (i<npargs); i++)
728       fprintf(out,
729               "<TR>"
730               "<TD ALIGN=RIGHT> <b><tt>%s%s</tt></b> </TD>"
731               "<TD ALIGN=RIGHT> %s </TD>"
732               "<TD ALIGN=RIGHT> <tt>%s</tt> </TD>"
733               "<TD> %s </TD>"
734               "</TD>\n",
735               (pa[i].type == etBOOL)?"-[no]":"-",pa[i].option+1,
736               argtp[pa[i].type],pa_val(&(pa[i]),tmp,255),NSR(pa[i].desc));
737     fprintf(out,"</TABLE>\n");
738   }
739   if (nbug > 0) {
740     fprintf(out,"<P>\n");
741     fprintf(out,"<H3>Known problems</H3>\n");
742     fprintf(out,"<UL>\n");
743     for(i=0; (i<nbug); i++)
744       fprintf(out,"<LI>%s\n",NSR(bugs[i]));
745     fprintf(out,"</UL>\n");
746   }
747   fprintf(out,"<P>\n");
748   fprintf(out,"<hr>\n<div ALIGN=RIGHT>\n");
749   fprintf(out,"<font size=\"-1\"><a href=\"http://www.gromacs.org\">"
750           "http://www.gromacs.org</a></font><br>\n");
751   fprintf(out,"<font size=\"-1\"><a href=\"mailto:gromacs@gromacs.org\">"
752           "gromacs@gromacs.org</a></font><br>\n");
753   fprintf(out,"</div>\n");
754   fprintf(out,"</BODY>\n");
755 }
756
757 char *check_xml(const char *s,const char *program,t_linkdata *links)
758 {
759   char *buf;
760   
761   buf=repall(s,NSRXML,sandrXML);
762   buf=html_xref(buf,program,links,FALSE);       /* the same in html and xml */
763   
764   return buf;
765 }
766
767 static void write_xmlman(FILE *out,
768                          const char *program,
769                          int nldesc,const char **desc,
770                          int nfile,t_filenm *fnm,
771                          int npargs,t_pargs *pa,
772                          int nbug,const char **bugs,
773                          t_linkdata *links)
774 {
775   int i;
776   char link[10],buf[256],opt[10];
777
778 #define NSR2(s) check_xml(s,program,links)
779 #define FLAG(w,f) (((w) & (f))==(f)) 
780
781   fprintf(out,"<gromacs-manual version=\"%s\" date=\"%s\" www=\"http://www.gromacs.org\">\n",GromacsVersion(),mydate(buf,255,FALSE));
782   /* fprintf(out,"<LINK rel=stylesheet href=\"style.css\" type=\"text/css\">\n"); */
783
784   fprintf(out,"<program name=\"%s\">",program);  
785   if (nldesc > 0) {
786     fprintf(out,"\n<description>\n<par>\n");
787     for(i=0; (i<nldesc); i++) 
788       fprintf(out,"%s\n",NSR2(desc[i]));
789   }
790   fprintf(out,"</par>\n</description>\n");
791
792   if (nfile > 0) {
793     fprintf(out,"\n<files>\n");
794     for(i=0; (i<nfile); i++) {
795       strcpy(link,ftp2ext(fnm[i].ftp));
796       if (strcmp(link,"???")==0)
797         strcpy(link,"files");
798         if (fnm[i].opt[0]=='-') strcpy(opt,fnm[i].opt+1);
799         else strcpy(opt,fnm[i].opt);
800       fprintf(out,
801               "<file type=\"%s\" typeid=\"%d\">\n"
802               "\t<flags read=\"%d\" write=\"%d\" optional=\"%d\"/>\n"
803               "\t<option>%s</option>\n"
804               "\t<default-name link=\"%s.html\">%s</default-name>\n"
805               "\t<description>%s</description>\n"
806               "</file>\n",
807               ftp2defnm(fnm[i].ftp),    /* from gmxlib/filenm.c */
808               fnm[i].ftp,
809               FLAG(fnm[i].flag,ffREAD), FLAG(fnm[i].flag,ffWRITE), FLAG(fnm[i].flag,ffOPT), 
810               opt,link,fnm[i].fn,/*fileopt(fnm[i].flag),*/
811               NSR(ftp2desc(fnm[i].ftp)));
812     }
813     fprintf(out,"</files>\n");
814   }
815
816   if (npargs > 0) {
817     fprintf(out,"\n<options>\n");
818     for(i=0; (i<npargs); i++)
819       fprintf(out,
820               "<option type=\"%s\" hidden=\"%d\">\n"
821               "\t<name >%s</name>\n"
822               "\t<default-value>%s</default-value>\n"
823               "\t<description>%s</description>\n"
824               "</option>\n",
825               argtp[pa[i].type], is_hidden(&pa[i]),
826               pa[i].option+1,                  /* +1 - with no trailing '-' */
827               pa_val(&(pa[i]),buf,255),pa[i].desc); /*argtp[pa[i].type],*/
828     fprintf(out,"</options>\n");
829   }
830
831   if (nbug > 0) {
832     fprintf(out,"\n<bugs>\n");
833     for(i=0; (i<nbug); i++)
834       fprintf(out,"\t<bug>%s</bug>\n",NSR(bugs[i]));
835     fprintf(out,"</bugs>\n");
836   }
837   fprintf(out,"\n</program>\n</gromacs-manual>\n");
838 #undef FLAG  
839 }
840
841 static void pr_opts(FILE *fp, 
842                     int nfile,  t_filenm *fnm, 
843                     int npargs, t_pargs pa[], int shell)
844 {
845   int i;
846   
847   switch (shell) {
848   case eshellCSH:
849     fprintf(fp," \"c/-/(");
850     for (i=0; i<nfile; i++)
851       fprintf(fp," %s",fnm[i].opt+1);
852     for (i=0; i<npargs; i++)
853       if ( (pa[i].type==etBOOL) && *(pa[i].u.b) )
854         fprintf(fp," no%s",pa[i].option+1);
855       else
856         fprintf(fp," %s",pa[i].option+1);
857     fprintf(fp,")/\"");
858     break;
859   case eshellBASH:
860     fprintf(fp,"if (( $COMP_CWORD <= 1 )) || [[ $c == -* ]]; then COMPREPLY=( $(compgen  -W '");
861     for (i=0; i<nfile; i++)
862       fprintf(fp," -%s",fnm[i].opt+1);
863     for (i=0; i<npargs; i++)
864       if ( (pa[i].type==etBOOL) && *(pa[i].u.b) )
865         fprintf(fp," -no%s",pa[i].option+1);
866       else
867         fprintf(fp," -%s",pa[i].option+1);
868     fprintf(fp,"' -- $c)); return 0; fi\n");
869     break;
870   case eshellZSH:
871     fprintf(fp," -x 's[-]' -s \"");
872     for (i=0; i<nfile; i++)
873       fprintf(fp," %s",fnm[i].opt+1);
874     for (i=0; i<npargs; i++)
875       if ( (pa[i].type==etBOOL) && *(pa[i].u.b) )
876         fprintf(fp," no%s",pa[i].option+1);
877       else
878         fprintf(fp," %s",pa[i].option+1);
879     fprintf(fp,"\" ");
880     break;
881   }
882 }
883
884 static void write_cshcompl(FILE *out,
885                            int nfile,  t_filenm *fnm,
886                            int npargs, t_pargs *pa)
887 {
888   fprintf(out,"complete %s",ShortProgram());
889   pr_enums(out,npargs,pa,eshellCSH);
890   pr_fopts(out,nfile,fnm,eshellCSH);
891   pr_opts(out,nfile,fnm,npargs,pa,eshellCSH);
892   fprintf(out,"\n");
893 }
894
895 static void write_zshcompl(FILE *out,
896                            int nfile,  t_filenm *fnm,
897                            int npargs, t_pargs *pa)
898 {
899   fprintf(out,"compctl ");
900
901   /* start with options, since they are always present */
902   pr_opts(out,nfile,fnm,npargs,pa,eshellZSH);
903   pr_enums(out,npargs,pa,eshellZSH);
904   pr_fopts(out,nfile,fnm,eshellZSH);
905   fprintf(out,"-- %s\n",ShortProgram());
906 }
907
908 static void write_bashcompl(FILE *out,
909                             int nfile,  t_filenm *fnm,
910                             int npargs, t_pargs *pa)
911 {
912   /* Advanced bash completions are handled by shell functions.
913    * p and c hold the previous and current word on the command line.
914    * We need to use extended globbing, so write it in each completion file */
915   fprintf(out,"shopt -s extglob\n");
916   fprintf(out,"_%s_compl() {\nlocal p c\n",ShortProgram());
917   fprintf(out,"COMPREPLY=() c=${COMP_WORDS[COMP_CWORD]} p=${COMP_WORDS[COMP_CWORD-1]}\n");
918   pr_opts(out,nfile,fnm,npargs,pa,eshellBASH);
919   fprintf(out,"case \"$p\" in\n");
920   
921   pr_enums(out,npargs,pa,eshellBASH);
922   pr_fopts(out,nfile,fnm,eshellBASH);
923   fprintf(out,"esac }\ncomplete -F _%s_compl %s\n",ShortProgram(),ShortProgram());
924 }
925
926 static void write_py(FILE *out,const char *program,
927                      int nldesc,const char **desc,
928                      int nfile,t_filenm *fnm,
929                      int npargs,t_pargs *pa,
930                      int nbug,const char **bugs,
931                      t_linkdata *links)
932 {
933   bool bHidden;
934   const char *cls = program;
935   char *tmp;
936   int  i,j;
937
938   /* Header stuff */  
939   fprintf(out,"#!/usr/bin/python\n\nfrom GmxDialog import *\n\n");
940   
941   /* Class definition */
942   fprintf(out,"class %s:\n",cls);
943   fprintf(out,"    def __init__(self,tk):\n");
944   
945   /* Help text */
946   fprintf(out,"        %s_help = \"\"\"\n",cls);
947   fprintf(out,"        DESCRIPTION\n");
948   print_tty_formatted(out,nldesc,desc,8,links,program,FALSE);
949   if (nbug > 0) {
950     fprintf(out,"\n        BUGS and PROBLEMS\n");
951     for(i=0; i<nbug; i++) {
952       snew(tmp,strlen(bugs[i])+3);
953       strcpy(tmp,"* ");
954       strcpy(tmp+2,check_tty(bugs[i]));
955       fprintf(out,"%s\n",wrap_lines(tmp,78,10,TRUE));
956       sfree(tmp);
957     }
958   }
959   fprintf(out,"        \"\"\"\n\n        # Command line options\n");
960   /* File options */
961   fprintf(out,"        flags = []\n");
962   for(i=0; (i<nfile); i++) 
963     fprintf(out,"        flags.append(pca_file('%s',\"%s\",0,%d))\n",
964             ftp2ext_generic(fnm[i].ftp),fnm[i].opt ? fnm[i].opt : "k",
965             is_optional(&(fnm[i])));
966             
967             
968   /* Other options */
969   for(i=0; (i<npargs); i++) {
970     switch(pa[i].type) {
971     case etINT:
972       fprintf(out,"        flags.append(pca_int(\"%s\",\"%s\",%d,%d))\n",
973               pa[i].option,pa[i].desc,*pa[i].u.i,is_hidden(&(pa[i])));
974       break;
975     case etREAL:
976     case etTIME:
977       fprintf(out,"        flags.append(pca_float(\"%s\",\"%s\",%f,%d))\n",
978               pa[i].option,pa[i].desc,*pa[i].u.r,is_hidden(&(pa[i])));
979       break;
980     case etSTR:
981     case etBOOL:
982       fprintf(out,"        flags.append(pca_bool(\"%s\",\"%s\",%d,%d))\n",
983               pa[i].option,pa[i].desc,*pa[i].u.b,is_hidden(&(pa[i])));
984       break;
985     case etRVEC:
986       fprintf(stderr,"Sorry, no rvecs yet...\n");
987       break;
988     case etENUM:
989       fprintf(out,"        flags.append(pca_enum(\"%s\",\"%s\",\n",
990               pa[i].option,pa[i].desc);
991       fprintf(out,"        ['%s'",pa[i].u.c[1]);
992       for(j=2; (pa[i].u.c[j] != NULL); j++)
993         fprintf(out,",'%s'",pa[i].u.c[j]);
994       fprintf(out,"],%d))\n",is_hidden(&(pa[i])));
995     default:
996       break;
997     }
998   }
999     
1000   /* Make the dialog box */
1001   fprintf(out,"        gmxd = gmx_dialog(tk,\"%s\",flags,%s_help)\n\n",
1002           cls,cls);
1003           
1004   /* Main loop */
1005   fprintf(out,"#####################################################\n");
1006   fprintf(out,"tk     = Tk()\n");
1007   fprintf(out,"my%s = %s(tk)\n",cls,cls);
1008   fprintf(out,"tk.mainloop()\n");
1009 }
1010
1011 void write_man(FILE *out,const char *mantp,
1012                const char *program,
1013                int nldesc,const char **desc,
1014                int nfile,t_filenm *fnm,
1015                int npargs,t_pargs *pa,
1016                int nbug,const char **bugs,
1017                bool bHidden)
1018 {
1019   const char *pr;
1020   int     i,npar;
1021   t_pargs *par;
1022  
1023   t_linkdata *links;
1024   
1025   links=init_linkdata();
1026   
1027   /* Don't write hidden options to completions, it just
1028    * makes the options more complicated for normal users
1029    */
1030
1031   if (bHidden) {
1032     npar=npargs;
1033     par=pa;
1034   }
1035   else {
1036     snew(par,npargs);
1037     npar=0;
1038     for(i=0;i<npargs;i++)
1039       if (!is_hidden(&pa[i])) {
1040         par[npar]=pa[i];
1041         npar++;
1042       }
1043   }
1044   
1045   if ((pr=strrchr(program,'/')) == NULL)
1046     pr=program;
1047   else
1048     pr+=1;
1049   if (strcmp(mantp,"tex")==0)
1050     write_texman(out,pr,nldesc,desc,nfile,fnm,npar,par,nbug,bugs,links);
1051   if (strcmp(mantp,"nroff")==0)
1052     write_nroffman(out,pr,nldesc,desc,nfile,fnm,npar,par,nbug,bugs,links);
1053   if (strcmp(mantp,"ascii")==0)
1054     write_ttyman(out,pr,nldesc,desc,nfile,fnm,npar,par,nbug,bugs,TRUE,links);
1055   if (strcmp(mantp,"wiki")==0)
1056     write_wikiman(out,pr,nldesc,desc,nfile,fnm,npar,par,nbug,bugs,TRUE,links);
1057   if (strcmp(mantp,"help")==0)
1058     write_ttyman(out,pr,nldesc,desc,nfile,fnm,npar,par,nbug,bugs,FALSE,links);
1059   if (strcmp(mantp,"html")==0)
1060     write_htmlman(out,pr,nldesc,desc,nfile,fnm,npar,par,nbug,bugs,links);
1061   if (strcmp(mantp,"py")==0)
1062     write_py(out,pr,nldesc,desc,nfile,fnm,npar,par,nbug,bugs,links);
1063   if (strcmp(mantp,"xml")==0)
1064     write_xmlman(out,pr,nldesc,desc,nfile,fnm,npargs,pa,nbug,bugs,links);       
1065   if (strcmp(mantp,"completion-zsh")==0)
1066     write_zshcompl(out,nfile,fnm,npar,par);
1067   if (strcmp(mantp,"completion-bash")==0)
1068     write_bashcompl(out,nfile,fnm,npar,par);
1069   if (strcmp(mantp,"completion-csh")==0)
1070     write_cshcompl(out,nfile,fnm,npar,par);
1071
1072   if (!bHidden)
1073     sfree(par);
1074
1075   finish_linkdata(links);
1076 }
1077