3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
44 #include "gmx_fatal.h"
50 #include "gromacs/onlinehelp/wman.h"
53 #include "thread_mpi.h"
56 /* NOTE: this was a cesspool of thread-unsafe code, has now been
57 properly proteced by mutexes (hopefully). */
59 /* XDR should be available on all platforms now,
60 * but we keep the possibility of turning it off...
65 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
66 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
67 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
68 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
69 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
73 eftASC, eftBIN, eftXDR, eftGEN, eftNR
76 /* To support multiple file types with one general (eg TRX) we have
79 static const int trxs[] =
84 efTRJ, efGRO, efG96, efPDB, efG87
86 #define NTRXS asize(trxs)
88 static const int tros[] =
93 efTRJ, efGRO, efG96, efPDB, efG87
95 #define NTROS asize(tros)
97 static const int trns[] =
104 #define NTRNS asize(trns)
106 static const int stos[] =
107 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
108 #define NSTOS asize(stos)
110 static const int stxs[] =
112 efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
118 #define NSTXS asize(stxs)
120 static const int tpxs[] =
127 #define NTPXS asize(tpxs)
129 static const int tpss[] =
134 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
136 #define NTPSS asize(tpss)
149 /* this array should correspond to the enum in include/types/filenm.h */
150 static const t_deffile
153 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
154 { eftGEN, ".???", "traj", "-f",
155 "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
156 { eftGEN, ".???", "trajout", "-f",
157 "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
158 { eftGEN, ".???", "traj", NULL,
159 "Full precision trajectory: trr trj cpt", NTRNS, trns },
160 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
161 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
162 { eftXDR, ".xtc", "traj", NULL,
163 "Compressed trajectory (portable xdr format)" },
164 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
165 { eftXDR, ".edr", "ener", NULL, "Energy file"},
166 { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
168 { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
170 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
171 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
172 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
173 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
174 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
175 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
176 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
177 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
178 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
179 { eftASC, ".log", "run", "-l", "Log file"},
180 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
181 { eftASC, ".out", "hello", "-o", "Generic output file"},
182 { eftASC, ".ndx", "index", "-n", "Index file", },
183 { eftASC, ".top", "topol", "-p", "Topology file"},
184 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
185 { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
187 { eftGEN, ".???", "topol", "-s",
188 "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
189 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
190 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
191 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
192 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
193 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
194 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
195 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
196 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
197 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
198 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
199 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
200 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
201 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
202 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
203 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
204 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
205 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
206 { eftASC, "", "rundir", NULL, "Run directory" }
209 static char *default_file_name = NULL;
211 #ifdef GMX_THREAD_MPI
212 static tMPI_Thread_mutex_t filenm_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
216 const char *z_ext[NZEXT] =
219 void set_default_file_name(const char *name)
222 #ifdef GMX_THREAD_MPI
223 tMPI_Thread_mutex_lock(&filenm_mutex);
225 default_file_name = strdup(name);
226 #ifdef GMX_THREAD_MPI
227 tMPI_Thread_mutex_unlock(&filenm_mutex);
231 for (i = 0; i < efNR; i++)
233 deffile[i].defnm = default_file_name;
238 const char *ftp2ext(int ftp)
240 if ((0 <= ftp) && (ftp < efNR))
242 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
250 const char *ftp2ext_generic(int ftp)
252 if ((0 <= ftp) && (ftp < efNR))
278 const char *ftp2ext_with_dot(int ftp)
280 if ((0 <= ftp) && (ftp < efNR))
282 return deffile[ftp].ext;
290 int ftp2generic_count(int ftp)
292 if ((0 <= ftp) && (ftp < efNR))
294 return deffile[ftp].ntps;
302 const int *ftp2generic_list(int ftp)
304 if ((0 <= ftp) && (ftp < efNR))
306 return deffile[ftp].tps;
314 const char *ftp2desc(int ftp)
316 if ((0 <= ftp) && (ftp < efNR))
318 return deffile[ftp].descr;
322 return "unknown filetype";
326 const char *ftp2ftype(int ftp)
328 if ((ftp >= 0) && (ftp < efNR))
330 switch (deffile[ftp].ftype)
337 return "XDR portable";
341 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
348 const char *ftp2defnm(int ftp)
350 const char *buf = NULL;
352 #ifdef GMX_THREAD_MPI
353 tMPI_Thread_mutex_lock(&filenm_mutex);
356 if (default_file_name)
358 buf = default_file_name;
362 if ((0 <= ftp) && (ftp < efNR))
364 buf = deffile[ftp].defnm;
367 #ifdef GMX_THREAD_MPI
368 tMPI_Thread_mutex_unlock(&filenm_mutex);
374 void pr_fns(FILE *fp, int nf, const t_filenm tfn[])
378 char buf[256], *wbuf, opt_buf[32];
381 fprintf(fp, "%6s %12s %-12s %s\n", "Option", "Filename", "Type",
384 "------------------------------------------------------------\n");
385 for (i = 0; (i < nf); i++)
387 for (f = 0; (f < tfn[i].nfiles); f++)
389 sprintf(buf, "%4s %14s %-12s ", (f == 0) ? tfn[i].opt : "",
390 tfn[i].fns[f], (f == 0) ? fileopt(tfn[i].flag, opt_buf, 32)
392 if (f < tfn[i].nfiles - 1)
394 fprintf(fp, "%s\n", buf);
397 if (tfn[i].nfiles > 0)
399 strcat(buf, deffile[tfn[i].ftp].descr);
400 if ((strlen(tfn[i].opt) > OPTLEN)
401 && (strlen(tfn[i].opt) <= ((OPTLEN + NAMELEN)
402 - strlen(tfn[i].fns[tfn[i].nfiles - 1]))))
404 for (j = strlen(tfn[i].opt); j < strlen(buf)
405 - (strlen(tfn[i].opt) - OPTLEN) + 1; j++)
407 buf[j] = buf[j + strlen(tfn[i].opt) - OPTLEN];
410 wbuf = wrap_lines(buf, 78, 35, FALSE);
411 fprintf(fp, "%s\n", wbuf);
419 void pr_fopts(FILE *fp, int nf, const t_filenm tfn[], int shell)
426 for (i = 0; (i < nf); i++)
428 fprintf(fp, " \"n/%s/f:*.", tfn[i].opt);
429 if (deffile[tfn[i].ftp].ntps)
432 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
438 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
445 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
448 for (j = 0; j < NZEXT; j++)
450 fprintf(fp, ",%s", z_ext[j]);
456 for (i = 0; (i < nf); i++)
458 fprintf(fp, "%s) COMPREPLY=( $(compgen -X '!*.", tfn[i].opt);
459 if (deffile[tfn[i].ftp].ntps)
462 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
468 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
475 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
478 for (j = 0; j < NZEXT; j++)
484 fprintf(fp, "%s", z_ext[j]);
486 fprintf(fp, ")' -f $c ; compgen -S '/' -X '.*' -d $c ));;\n");
490 for (i = 0; (i < nf); i++)
492 fprintf(fp, "- 'c[-1,%s]' -g '*.", tfn[i].opt);
493 if (deffile[tfn[i].ftp].ntps)
496 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
502 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
509 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
512 for (j = 0; j < NZEXT; j++)
514 fprintf(fp, "|%s", z_ext[j]);
516 fprintf(fp, ") *(/)' ");
522 static void check_opts(int nf, t_filenm fnm[])
527 for (i = 0; (i < nf); i++)
529 df = &(deffile[fnm[i].ftp]);
530 if (fnm[i].opt == NULL)
532 if (df->defopt == NULL)
534 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
535 deffile[fnm[i].ftp].ext, fnm[i].ftp);
539 fnm[i].opt = df->defopt;
545 int fn2ftp(const char *fn)
557 if ((len >= 4) && (fn[len - 4] == '.'))
559 feptr = &(fn[len - 4]);
566 for (i = 0; (i < efNR); i++)
568 if ((eptr = deffile[i].ext) != NULL)
570 if (gmx_strcasecmp(feptr, eptr) == 0)
580 static void set_extension(char *buf, int ftp)
585 /* check if extension is already at end of filename */
586 df = &(deffile[ftp]);
588 extlen = strlen(df->ext);
589 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
591 strcat(buf, df->ext);
595 static void add_filenm(t_filenm *fnm, const char *filenm)
597 srenew(fnm->fns, fnm->nfiles+1);
598 fnm->fns[fnm->nfiles] = strdup(filenm);
602 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
604 char buf[256], buf2[256];
610 nopts = deffile[fnm->ftp].ntps;
611 ftps = deffile[fnm->ftp].tps;
612 if ((nopts == 0) || (ftps == NULL))
614 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
618 if (name && (bCanNotOverride || (default_file_name == NULL)))
621 /* First check whether we have a valid filename already */
623 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
625 /*if file exist don't add an extension for trajectory reading*/
626 bValidExt = gmx_fexist(name);
628 for (i = 0; (i < nopts) && !bValidExt; i++)
638 /* No name given, set the default name */
639 strcpy(buf, ftp2defnm(fnm->ftp));
642 if (!bValidExt && (fnm->flag & ffREAD))
644 /* for input-files only: search for filenames in the directory */
645 for (i = 0; (i < nopts) && !bValidExt; i++)
649 set_extension(buf2, type);
650 if (gmx_fexist(buf2))
660 /* Use the first extension type */
661 set_extension(buf, ftps[0]);
664 add_filenm(fnm, buf);
667 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
670 /* Set the default filename, extension and option for those fields that
671 * are not already set. An extension is added if not present, if fn = NULL
672 * or empty, the default filename is given.
677 if ((fnm->flag & ffREAD) && !bReadNode)
682 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
684 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
691 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
693 /* check if filename ends in .gz or .Z, if so remove that: */
695 for (i = 0; i < NZEXT; i++)
697 extlen = strlen(z_ext[i]);
700 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
702 buf[len-extlen] = '\0';
709 if (deffile[fnm->ftp].ntps)
711 set_grpfnm(fnm, name ? buf : NULL, bCanNotOverride);
715 if ((name == NULL) || !(bCanNotOverride || (default_file_name == NULL)))
717 const char *defnm = ftp2defnm(fnm->ftp);
720 set_extension(buf, fnm->ftp);
722 add_filenm(fnm, buf);
726 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
730 for (i = 0; (i < nf); i++)
734 set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
739 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
740 gmx_bool bKeep, gmx_bool bReadNode)
747 for (i = 0; (i < nf); i++)
754 snew(bRemove, (*argc)+1);
758 for (j = 0; (j < nf); j++)
760 if (strcmp(argv[i], fnm[j].opt) == 0)
765 /* check if we are out of arguments for this option */
766 if ((i >= *argc) || (argv[i][0] == '-'))
768 set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
770 /* sweep up all file arguments for this option */
771 while ((i < *argc) && (argv[i][0] != '-'))
773 set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
776 /* only repeat for 'multiple' file options: */
777 if (!IS_MULT(fnm[j]))
783 break; /* jump out of 'j' loop */
786 /* No file found corresponding to option argv[i] */
796 /* Remove used entries */
797 for (i = j = 0; (i <= *argc); i++)
809 set_filenms(nf, fnm, bReadNode);
813 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
817 for (i = 0; (i < nfile); i++)
819 if (strcmp(opt, fnm[i].opt) == 0)
821 return fnm[i].fns[0];
825 fprintf(stderr, "No option %s\n", opt);
830 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
833 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
836 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
840 for (i = 0; (i < nfile); i++)
842 if (strcmp(opt, fnm[i].opt) == 0)
845 return fnm[i].nfiles;
849 fprintf(stderr, "No option %s\n", opt);
853 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
857 for (i = 0; (i < nfile); i++)
859 if (ftp == fnm[i].ftp)
861 return fnm[i].fns[0];
865 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
869 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
873 for (i = 0; (i < nfile); i++)
875 if (ftp == fnm[i].ftp)
878 return fnm[i].nfiles;
882 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
886 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
890 for (i = 0; (i < nfile); i++)
892 if (ftp == fnm[i].ftp)
894 return (gmx_bool) IS_SET(fnm[i]);
898 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
903 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
907 for (i = 0; (i < nfile); i++)
909 if (strcmp(opt, fnm[i].opt) == 0)
911 return (gmx_bool) IS_SET(fnm[i]);
915 fprintf(stderr, "No option %s\n", opt);
920 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
924 for (i = 0; (i < nfile); i++)
926 if (strcmp(opt, fnm[i].opt) == 0)
928 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
934 return fnm[i].fns[0];
938 fprintf(stderr, "No option %s\n", opt);
942 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
946 for (i = 0; (i < nfile); i++)
948 if (ftp == fnm[i].ftp)
950 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
956 return fnm[i].fns[0];
960 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
965 static void add_filters(char *filter, int *n, int nf, const int ftp[])
970 sprintf(filter, "*.{");
971 for (i = 0; (i < nf); i++)
973 sprintf(buf, "%s", ftp2ext(ftp[i]));
984 char *ftp2filter(int ftp)
987 static char filter[128];
994 add_filters(filter, &n, NTRXS, trxs);
997 add_filters(filter, &n, NTRNS, trns);
1000 add_filters(filter, &n, NSTOS, stos);
1003 add_filters(filter, &n, NSTXS, stxs);
1006 add_filters(filter, &n, NTPXS, tpxs);
1009 sprintf(filter, "*%s", ftp2ext(ftp));
1016 gmx_bool is_optional(const t_filenm *fnm)
1018 return ((fnm->flag & ffOPT) == ffOPT);
1021 gmx_bool is_output(const t_filenm *fnm)
1023 return ((fnm->flag & ffWRITE) == ffWRITE);
1026 gmx_bool is_set(const t_filenm *fnm)
1028 return ((fnm->flag & ffSET) == ffSET);
1031 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
1034 char buf[STRLEN], newname[STRLEN];
1037 for (i = 0; i < nfile; i++)
1039 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
1041 /* We never use multiple _outputs_, but we might as well check
1042 for it, just in case... */
1043 for (j = 0; j < fnm[i].nfiles; j++)
1045 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
1046 extpos = strrchr(buf, '.');
1048 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
1049 free(fnm[i].fns[j]);
1050 fnm[i].fns[j] = strdup(newname);
1057 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
1063 for (i = 0; i < nf; i++)
1065 ret[i] = tfn[i]; /* just directly copy all non-string fields */
1068 ret[i].opt = strdup(tfn[i].opt);
1077 ret[i].fn = strdup(tfn[i].fn);
1084 if (tfn[i].nfiles > 0)
1086 snew(ret[i].fns, tfn[i].nfiles);
1087 for (j = 0; j < tfn[i].nfiles; j++)
1089 ret[i].fns[j] = strdup(tfn[i].fns[j]);
1096 void done_filenms(int nf, t_filenm fnm[])
1100 for (i = 0; i < nf; ++i)
1102 for (j = 0; j < fnm[i].nfiles; ++j)
1104 sfree(fnm[i].fns[j]);