2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * Copyright (c) 2013, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
48 #include "gmx_fatal.h"
53 #include "gromacs/onlinehelp/wman.h"
56 #include "thread_mpi.h"
59 /* NOTE: this was a cesspool of thread-unsafe code, has now been
60 properly proteced by mutexes (hopefully). */
62 /* XDR should be available on all platforms now,
63 * but we keep the possibility of turning it off...
68 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
69 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
70 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
71 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
72 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
76 eftASC, eftBIN, eftXDR, eftGEN, eftNR
79 /* To support multiple file types with one general (eg TRX) we have
82 static const int trxs[] =
87 efTRJ, efGRO, efG96, efPDB, efG87
89 #define NTRXS asize(trxs)
91 static const int tros[] =
96 efTRJ, efGRO, efG96, efPDB, efG87
98 #define NTROS asize(tros)
100 static const int trns[] =
107 #define NTRNS asize(trns)
109 static const int stos[] =
110 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
111 #define NSTOS asize(stos)
113 static const int stxs[] =
115 efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
121 #define NSTXS asize(stxs)
123 static const int tpxs[] =
130 #define NTPXS asize(tpxs)
132 static const int tpss[] =
137 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
139 #define NTPSS asize(tpss)
152 /* this array should correspond to the enum in filenm.h */
153 static const t_deffile
156 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
157 { eftGEN, ".???", "traj", "-f",
158 "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
159 { eftGEN, ".???", "trajout", "-f",
160 "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
161 { eftGEN, ".???", "traj", NULL,
162 "Full precision trajectory: trr trj cpt", NTRNS, trns },
163 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
164 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
165 { eftXDR, ".xtc", "traj", NULL,
166 "Compressed trajectory (portable xdr format)" },
167 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
168 { eftXDR, ".edr", "ener", NULL, "Energy file"},
169 { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
171 { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
173 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
174 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
175 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
176 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
177 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
178 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
179 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
180 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
181 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
182 { eftASC, ".log", "run", "-l", "Log file"},
183 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
184 { eftASC, ".out", "hello", "-o", "Generic output file"},
185 { eftASC, ".ndx", "index", "-n", "Index file", },
186 { eftASC, ".top", "topol", "-p", "Topology file"},
187 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
188 { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
190 { eftGEN, ".???", "topol", "-s",
191 "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
192 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
193 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
194 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
195 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
196 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
197 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
198 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
199 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
200 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
201 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
202 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
203 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
204 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
205 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
206 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
207 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
208 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
209 { eftASC, "", "rundir", NULL, "Run directory" }
212 static char *default_file_name = NULL;
214 #ifdef GMX_THREAD_MPI
215 static tMPI_Thread_mutex_t filenm_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
219 const char *z_ext[NZEXT] =
222 void set_default_file_name(const char *name)
225 #ifdef GMX_THREAD_MPI
226 tMPI_Thread_mutex_lock(&filenm_mutex);
228 default_file_name = strdup(name);
229 #ifdef GMX_THREAD_MPI
230 tMPI_Thread_mutex_unlock(&filenm_mutex);
235 const char *ftp2ext(int ftp)
237 if ((0 <= ftp) && (ftp < efNR))
239 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
247 const char *ftp2ext_generic(int ftp)
249 if ((0 <= ftp) && (ftp < efNR))
275 const char *ftp2ext_with_dot(int ftp)
277 if ((0 <= ftp) && (ftp < efNR))
279 return deffile[ftp].ext;
287 int ftp2generic_count(int ftp)
289 if ((0 <= ftp) && (ftp < efNR))
291 return deffile[ftp].ntps;
299 const int *ftp2generic_list(int ftp)
301 if ((0 <= ftp) && (ftp < efNR))
303 return deffile[ftp].tps;
311 const char *ftp2desc(int ftp)
313 if ((0 <= ftp) && (ftp < efNR))
315 return deffile[ftp].descr;
319 return "unknown filetype";
323 const char *ftp2ftype(int ftp)
325 if ((ftp >= 0) && (ftp < efNR))
327 switch (deffile[ftp].ftype)
334 return "XDR portable";
338 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
345 const char *ftp2defnm(int ftp)
347 const char *buf = NULL;
349 #ifdef GMX_THREAD_MPI
350 tMPI_Thread_mutex_lock(&filenm_mutex);
353 if (default_file_name)
355 buf = default_file_name;
359 if ((0 <= ftp) && (ftp < efNR))
361 buf = deffile[ftp].defnm;
364 #ifdef GMX_THREAD_MPI
365 tMPI_Thread_mutex_unlock(&filenm_mutex);
371 void pr_fns(FILE *fp, int nf, const t_filenm tfn[])
375 char buf[256], *wbuf, opt_buf[32];
378 fprintf(fp, "%6s %12s %-12s %s\n", "Option", "Filename", "Type",
381 "------------------------------------------------------------\n");
382 for (i = 0; (i < nf); i++)
384 for (f = 0; (f < tfn[i].nfiles); f++)
386 sprintf(buf, "%4s %14s %-12s ", (f == 0) ? tfn[i].opt : "",
387 tfn[i].fns[f], (f == 0) ? fileopt(tfn[i].flag, opt_buf, 32)
389 if (f < tfn[i].nfiles - 1)
391 fprintf(fp, "%s\n", buf);
394 if (tfn[i].nfiles > 0)
396 strcat(buf, deffile[tfn[i].ftp].descr);
397 if ((strlen(tfn[i].opt) > OPTLEN)
398 && (strlen(tfn[i].opt) <= ((OPTLEN + NAMELEN)
399 - strlen(tfn[i].fns[tfn[i].nfiles - 1]))))
401 for (j = strlen(tfn[i].opt); j < strlen(buf)
402 - (strlen(tfn[i].opt) - OPTLEN) + 1; j++)
404 buf[j] = buf[j + strlen(tfn[i].opt) - OPTLEN];
407 wbuf = wrap_lines(buf, 78, 35, FALSE);
408 fprintf(fp, "%s\n", wbuf);
416 void pr_fopts(FILE *fp, int nf, const t_filenm tfn[], int shell)
423 for (i = 0; (i < nf); i++)
425 fprintf(fp, " \"n/%s/f:*.", tfn[i].opt);
426 if (deffile[tfn[i].ftp].ntps)
429 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
435 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
442 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
445 for (j = 0; j < NZEXT; j++)
447 fprintf(fp, ",%s", z_ext[j]);
453 for (i = 0; (i < nf); i++)
455 fprintf(fp, "%s) COMPREPLY=( $(compgen -X '!*.", tfn[i].opt);
456 if (deffile[tfn[i].ftp].ntps)
459 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
465 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
472 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
475 for (j = 0; j < NZEXT; j++)
481 fprintf(fp, "%s", z_ext[j]);
483 fprintf(fp, ")' -f $c ; compgen -S '/' -X '.*' -d $c ));;\n");
487 for (i = 0; (i < nf); i++)
489 fprintf(fp, "- 'c[-1,%s]' -g '*.", tfn[i].opt);
490 if (deffile[tfn[i].ftp].ntps)
493 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
499 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
506 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
509 for (j = 0; j < NZEXT; j++)
511 fprintf(fp, "|%s", z_ext[j]);
513 fprintf(fp, ") *(/)' ");
519 static void check_opts(int nf, t_filenm fnm[])
524 for (i = 0; (i < nf); i++)
526 df = &(deffile[fnm[i].ftp]);
527 if (fnm[i].opt == NULL)
529 if (df->defopt == NULL)
531 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
532 deffile[fnm[i].ftp].ext, fnm[i].ftp);
536 fnm[i].opt = df->defopt;
542 int fn2ftp(const char *fn)
554 if ((len >= 4) && (fn[len - 4] == '.'))
556 feptr = &(fn[len - 4]);
563 for (i = 0; (i < efNR); i++)
565 if ((eptr = deffile[i].ext) != NULL)
567 if (gmx_strcasecmp(feptr, eptr) == 0)
577 static void set_extension(char *buf, int ftp)
582 /* check if extension is already at end of filename */
583 df = &(deffile[ftp]);
585 extlen = strlen(df->ext);
586 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
588 strcat(buf, df->ext);
592 static void add_filenm(t_filenm *fnm, const char *filenm)
594 srenew(fnm->fns, fnm->nfiles+1);
595 fnm->fns[fnm->nfiles] = strdup(filenm);
599 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
601 char buf[256], buf2[256];
607 nopts = deffile[fnm->ftp].ntps;
608 ftps = deffile[fnm->ftp].tps;
609 if ((nopts == 0) || (ftps == NULL))
611 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
615 if (name && (bCanNotOverride || (default_file_name == NULL)))
618 /* First check whether we have a valid filename already */
620 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
622 /*if file exist don't add an extension for trajectory reading*/
623 bValidExt = gmx_fexist(name);
625 for (i = 0; (i < nopts) && !bValidExt; i++)
635 /* No name given, set the default name */
636 strcpy(buf, ftp2defnm(fnm->ftp));
639 if (!bValidExt && (fnm->flag & ffREAD))
641 /* for input-files only: search for filenames in the directory */
642 for (i = 0; (i < nopts) && !bValidExt; i++)
646 set_extension(buf2, type);
647 if (gmx_fexist(buf2))
657 /* Use the first extension type */
658 set_extension(buf, ftps[0]);
661 add_filenm(fnm, buf);
664 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
667 /* Set the default filename, extension and option for those fields that
668 * are not already set. An extension is added if not present, if fn = NULL
669 * or empty, the default filename is given.
674 if ((fnm->flag & ffREAD) && !bReadNode)
679 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
681 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
688 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
690 /* check if filename ends in .gz or .Z, if so remove that: */
692 for (i = 0; i < NZEXT; i++)
694 extlen = strlen(z_ext[i]);
697 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
699 buf[len-extlen] = '\0';
706 if (deffile[fnm->ftp].ntps)
708 set_grpfnm(fnm, name ? buf : NULL, bCanNotOverride);
712 if ((name == NULL) || !(bCanNotOverride || (default_file_name == NULL)))
714 const char *defnm = ftp2defnm(fnm->ftp);
717 set_extension(buf, fnm->ftp);
719 add_filenm(fnm, buf);
723 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
727 for (i = 0; (i < nf); i++)
731 set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
736 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
737 gmx_bool bKeep, gmx_bool bReadNode)
744 for (i = 0; (i < nf); i++)
751 snew(bRemove, (*argc)+1);
755 for (j = 0; (j < nf); j++)
757 if (strcmp(argv[i], fnm[j].opt) == 0)
762 /* check if we are out of arguments for this option */
763 if ((i >= *argc) || (argv[i][0] == '-'))
765 set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
767 /* sweep up all file arguments for this option */
768 while ((i < *argc) && (argv[i][0] != '-'))
770 set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
773 /* only repeat for 'multiple' file options: */
774 if (!IS_MULT(fnm[j]))
780 break; /* jump out of 'j' loop */
783 /* No file found corresponding to option argv[i] */
793 /* Remove used entries */
794 for (i = j = 0; (i <= *argc); i++)
806 set_filenms(nf, fnm, bReadNode);
810 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
814 for (i = 0; (i < nfile); i++)
816 if (strcmp(opt, fnm[i].opt) == 0)
818 return fnm[i].fns[0];
822 fprintf(stderr, "No option %s\n", opt);
827 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
830 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
833 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
837 for (i = 0; (i < nfile); i++)
839 if (strcmp(opt, fnm[i].opt) == 0)
842 return fnm[i].nfiles;
846 fprintf(stderr, "No option %s\n", opt);
850 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
854 for (i = 0; (i < nfile); i++)
856 if (ftp == fnm[i].ftp)
858 return fnm[i].fns[0];
862 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
866 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
870 for (i = 0; (i < nfile); i++)
872 if (ftp == fnm[i].ftp)
875 return fnm[i].nfiles;
879 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
883 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
887 for (i = 0; (i < nfile); i++)
889 if (ftp == fnm[i].ftp)
891 return (gmx_bool) IS_SET(fnm[i]);
895 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
900 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
904 for (i = 0; (i < nfile); i++)
906 if (strcmp(opt, fnm[i].opt) == 0)
908 return (gmx_bool) IS_SET(fnm[i]);
912 fprintf(stderr, "No option %s\n", opt);
917 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
921 for (i = 0; (i < nfile); i++)
923 if (strcmp(opt, fnm[i].opt) == 0)
925 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
931 return fnm[i].fns[0];
935 fprintf(stderr, "No option %s\n", opt);
939 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
943 for (i = 0; (i < nfile); i++)
945 if (ftp == fnm[i].ftp)
947 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
953 return fnm[i].fns[0];
957 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
961 gmx_bool is_optional(const t_filenm *fnm)
963 return ((fnm->flag & ffOPT) == ffOPT);
966 gmx_bool is_output(const t_filenm *fnm)
968 return ((fnm->flag & ffWRITE) == ffWRITE);
971 gmx_bool is_set(const t_filenm *fnm)
973 return ((fnm->flag & ffSET) == ffSET);
976 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
979 char buf[STRLEN], newname[STRLEN];
982 for (i = 0; i < nfile; i++)
984 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
986 /* We never use multiple _outputs_, but we might as well check
987 for it, just in case... */
988 for (j = 0; j < fnm[i].nfiles; j++)
990 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
991 extpos = strrchr(buf, '.');
993 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
995 fnm[i].fns[j] = strdup(newname);
1002 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
1008 for (i = 0; i < nf; i++)
1010 ret[i] = tfn[i]; /* just directly copy all non-string fields */
1013 ret[i].opt = strdup(tfn[i].opt);
1022 ret[i].fn = strdup(tfn[i].fn);
1029 if (tfn[i].nfiles > 0)
1031 snew(ret[i].fns, tfn[i].nfiles);
1032 for (j = 0; j < tfn[i].nfiles; j++)
1034 ret[i].fns[j] = strdup(tfn[i].fns[j]);
1041 void done_filenms(int nf, t_filenm fnm[])
1045 for (i = 0; i < nf; ++i)
1047 for (j = 0; j < fnm[i].nfiles; ++j)
1049 sfree(fnm[i].fns[j]);