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"
52 #include "thread_mpi.h"
55 /* NOTE: this was a cesspool of thread-unsafe code, has now been
56 properly proteced by mutexes (hopefully). */
58 /* XDR should be available on all platforms now,
59 * but we keep the possibility of turning it off...
64 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
65 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
66 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
67 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
68 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
72 eftASC, eftBIN, eftXDR, eftGEN, eftNR
75 /* To support multiple file types with one general (eg TRX) we have
78 static const int trxs[] =
83 efTRJ, efGRO, efG96, efPDB, efG87
85 #define NTRXS asize(trxs)
87 static const int tros[] =
92 efTRJ, efGRO, efG96, efPDB, efG87
94 #define NTROS asize(tros)
96 static const int trns[] =
103 #define NTRNS asize(trns)
105 static const int stos[] =
106 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
107 #define NSTOS asize(stos)
109 static const int stxs[] =
111 efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
117 #define NSTXS asize(stxs)
119 static const int tpxs[] =
126 #define NTPXS asize(tpxs)
128 static const int tpss[] =
133 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
135 #define NTPSS asize(tpss)
148 /* this array should correspond to the enum in include/types/filenm.h */
149 static const t_deffile
152 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
153 { eftASC, ".gct", "gct", "-f", "General coupling stuff"},
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, ".hat", "gk", NULL, "Fourier transform of spread function" },
205 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
206 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
207 { eftASC, "", "rundir", NULL, "Run directory" }
210 static char *default_file_name = NULL;
212 #ifdef GMX_THREAD_MPI
213 static tMPI_Thread_mutex_t filenm_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
217 const char *z_ext[NZEXT] =
220 void set_default_file_name(const char *name)
223 #ifdef GMX_THREAD_MPI
224 tMPI_Thread_mutex_lock(&filenm_mutex);
226 default_file_name = strdup(name);
227 #ifdef GMX_THREAD_MPI
228 tMPI_Thread_mutex_unlock(&filenm_mutex);
232 for (i = 0; i < efNR; i++)
234 deffile[i].defnm = default_file_name;
239 const char *ftp2ext(int ftp)
241 if ((0 <= ftp) && (ftp < efNR))
243 return deffile[ftp].ext + 1;
251 const char *ftp2ext_generic(int ftp)
253 if ((0 <= ftp) && (ftp < efNR))
279 const char *ftp2desc(int ftp)
281 if ((0 <= ftp) && (ftp < efNR))
283 return deffile[ftp].descr;
287 return "unknown filetype";
291 const char *ftp2ftype(int ftp)
293 if ((ftp >= 0) && (ftp < efNR))
295 switch (deffile[ftp].ftype)
302 return "XDR portable";
306 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
313 const char *ftp2defnm(int ftp)
315 const char *buf = NULL;
317 #ifdef GMX_THREAD_MPI
318 tMPI_Thread_mutex_lock(&filenm_mutex);
321 if (default_file_name)
323 buf = default_file_name;
327 if ((0 <= ftp) && (ftp < efNR))
329 buf = deffile[ftp].defnm;
332 #ifdef GMX_THREAD_MPI
333 tMPI_Thread_mutex_unlock(&filenm_mutex);
339 void pr_def(FILE *fp, int ftp)
342 const char *s = NULL;
343 char *flst, *tmp, *desc;
347 df = &(deffile[ftp]);
348 defnm = ftp2defnm(ftp);
349 /* find default file extension and \tt-ify description */
350 /* FIXME: The constness should not be cast away */
352 desc = strdup(df->descr);
356 ext = deffile[df->tps[0]].ext;
357 tmp = strstr(desc, ": ") + 1;
362 snew(flst, strlen(tmp)+6);
363 strcpy(flst, " \\tt ");
380 /* set file contents type */
396 gmx_fatal(FARGS, "Unimplemented filetype %d %d", ftp,
399 fprintf(fp, "\\tt %8s & \\tt %3s & %3s & \\tt %2s & %s%s \\\\[-0.1ex]\n",
400 defnm, ext, s, df->defopt ? df->defopt : "",
401 check_tex(desc), check_tex(flst));
405 void pr_fns(FILE *fp, int nf, const t_filenm tfn[])
409 char buf[256], *wbuf, opt_buf[32];
412 fprintf(fp, "%6s %12s %-12s %s\n", "Option", "Filename", "Type",
415 "------------------------------------------------------------\n");
416 for (i = 0; (i < nf); i++)
418 for (f = 0; (f < tfn[i].nfiles); f++)
420 sprintf(buf, "%4s %14s %-12s ", (f == 0) ? tfn[i].opt : "",
421 tfn[i].fns[f], (f == 0) ? fileopt(tfn[i].flag, opt_buf, 32)
423 if (f < tfn[i].nfiles - 1)
425 fprintf(fp, "%s\n", buf);
428 if (tfn[i].nfiles > 0)
430 strcat(buf, deffile[tfn[i].ftp].descr);
431 if ((strlen(tfn[i].opt) > OPTLEN)
432 && (strlen(tfn[i].opt) <= ((OPTLEN + NAMELEN)
433 - strlen(tfn[i].fns[tfn[i].nfiles - 1]))))
435 for (j = strlen(tfn[i].opt); j < strlen(buf)
436 - (strlen(tfn[i].opt) - OPTLEN) + 1; j++)
438 buf[j] = buf[j + strlen(tfn[i].opt) - OPTLEN];
441 wbuf = wrap_lines(buf, 78, 35, FALSE);
442 fprintf(fp, "%s\n", wbuf);
450 void pr_fopts(FILE *fp, int nf, const t_filenm tfn[], int shell)
457 for (i = 0; (i < nf); i++)
459 fprintf(fp, " \"n/%s/f:*.", tfn[i].opt);
460 if (deffile[tfn[i].ftp].ntps)
463 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
469 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
476 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
479 for (j = 0; j < NZEXT; j++)
481 fprintf(fp, ",%s", z_ext[j]);
487 for (i = 0; (i < nf); i++)
489 fprintf(fp, "%s) COMPREPLY=( $(compgen -X '!*.", 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++)
515 fprintf(fp, "%s", z_ext[j]);
517 fprintf(fp, ")' -f $c ; compgen -S '/' -X '.*' -d $c ));;\n");
521 for (i = 0; (i < nf); i++)
523 fprintf(fp, "- 'c[-1,%s]' -g '*.", tfn[i].opt);
524 if (deffile[tfn[i].ftp].ntps)
527 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
533 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
540 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
543 for (j = 0; j < NZEXT; j++)
545 fprintf(fp, "|%s", z_ext[j]);
547 fprintf(fp, ") *(/)' ");
553 static void check_opts(int nf, t_filenm fnm[])
558 for (i = 0; (i < nf); i++)
560 df = &(deffile[fnm[i].ftp]);
561 if (fnm[i].opt == NULL)
563 if (df->defopt == NULL)
565 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
566 deffile[fnm[i].ftp].ext, fnm[i].ftp);
570 fnm[i].opt = df->defopt;
576 int fn2ftp(const char *fn)
588 if ((len >= 4) && (fn[len - 4] == '.'))
590 feptr = &(fn[len - 4]);
597 for (i = 0; (i < efNR); i++)
599 if ((eptr = deffile[i].ext) != NULL)
601 if (gmx_strcasecmp(feptr, eptr) == 0)
611 static void set_extension(char *buf, int ftp)
616 /* check if extension is already at end of filename */
617 df = &(deffile[ftp]);
619 extlen = strlen(df->ext);
620 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
622 strcat(buf, df->ext);
626 static void add_filenm(t_filenm *fnm, const char *filenm)
628 srenew(fnm->fns, fnm->nfiles+1);
629 fnm->fns[fnm->nfiles] = strdup(filenm);
633 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
635 char buf[256], buf2[256];
641 nopts = deffile[fnm->ftp].ntps;
642 ftps = deffile[fnm->ftp].tps;
643 if ((nopts == 0) || (ftps == NULL))
645 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
649 if (name && (bCanNotOverride || (default_file_name == NULL)))
652 /* First check whether we have a valid filename already */
654 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
656 /*if file exist don't add an extension for trajectory reading*/
657 bValidExt = gmx_fexist(name);
659 for (i = 0; (i < nopts) && !bValidExt; i++)
669 /* No name given, set the default name */
670 strcpy(buf, ftp2defnm(fnm->ftp));
673 if (!bValidExt && (fnm->flag & ffREAD))
675 /* for input-files only: search for filenames in the directory */
676 for (i = 0; (i < nopts) && !bValidExt; i++)
680 set_extension(buf2, type);
681 if (gmx_fexist(buf2))
691 /* Use the first extension type */
692 set_extension(buf, ftps[0]);
695 add_filenm(fnm, buf);
698 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
701 /* Set the default filename, extension and option for those fields that
702 * are not already set. An extension is added if not present, if fn = NULL
703 * or empty, the default filename is given.
708 if ((fnm->flag & ffREAD) && !bReadNode)
713 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
715 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
722 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
724 /* check if filename ends in .gz or .Z, if so remove that: */
726 for (i = 0; i < NZEXT; i++)
728 extlen = strlen(z_ext[i]);
731 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
733 buf[len-extlen] = '\0';
740 if (deffile[fnm->ftp].ntps)
742 set_grpfnm(fnm, name ? buf : NULL, bCanNotOverride);
746 if ((name == NULL) || !(bCanNotOverride || (default_file_name == NULL)))
748 const char *defnm = ftp2defnm(fnm->ftp);
751 set_extension(buf, fnm->ftp);
753 add_filenm(fnm, buf);
757 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
761 for (i = 0; (i < nf); i++)
765 set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
770 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
771 gmx_bool bKeep, gmx_bool bReadNode)
778 for (i = 0; (i < nf); i++)
785 snew(bRemove, (*argc)+1);
789 for (j = 0; (j < nf); j++)
791 if (strcmp(argv[i], fnm[j].opt) == 0)
796 /* check if we are out of arguments for this option */
797 if ((i >= *argc) || (argv[i][0] == '-'))
799 set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
801 /* sweep up all file arguments for this option */
802 while ((i < *argc) && (argv[i][0] != '-'))
804 set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
807 /* only repeat for 'multiple' file options: */
808 if (!IS_MULT(fnm[j]))
814 break; /* jump out of 'j' loop */
817 /* No file found corresponding to option argv[i] */
827 /* Remove used entries */
828 for (i = j = 0; (i <= *argc); i++)
840 set_filenms(nf, fnm, bReadNode);
844 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
848 for (i = 0; (i < nfile); i++)
850 if (strcmp(opt, fnm[i].opt) == 0)
852 return fnm[i].fns[0];
856 fprintf(stderr, "No option %s\n", opt);
861 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
864 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
867 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
871 for (i = 0; (i < nfile); i++)
873 if (strcmp(opt, fnm[i].opt) == 0)
876 return fnm[i].nfiles;
880 fprintf(stderr, "No option %s\n", opt);
884 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
888 for (i = 0; (i < nfile); i++)
890 if (ftp == fnm[i].ftp)
892 return fnm[i].fns[0];
896 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
900 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
904 for (i = 0; (i < nfile); i++)
906 if (ftp == fnm[i].ftp)
909 return fnm[i].nfiles;
913 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
917 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
921 for (i = 0; (i < nfile); i++)
923 if (ftp == fnm[i].ftp)
925 return (gmx_bool) IS_SET(fnm[i]);
929 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
934 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
938 for (i = 0; (i < nfile); i++)
940 if (strcmp(opt, fnm[i].opt) == 0)
942 return (gmx_bool) IS_SET(fnm[i]);
946 fprintf(stderr, "No option %s\n", opt);
951 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
955 for (i = 0; (i < nfile); i++)
957 if (strcmp(opt, fnm[i].opt) == 0)
959 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
965 return fnm[i].fns[0];
969 fprintf(stderr, "No option %s\n", opt);
973 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
977 for (i = 0; (i < nfile); i++)
979 if (ftp == fnm[i].ftp)
981 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
987 return fnm[i].fns[0];
991 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
996 static void add_filters(char *filter, int *n, int nf, const int ftp[])
1001 sprintf(filter, "*.{");
1002 for (i = 0; (i < nf); i++)
1004 sprintf(buf, "%s", ftp2ext(ftp[i]));
1007 strcat(filter, ",");
1009 strcat(filter, buf);
1012 strcat(filter, "}");
1015 char *ftp2filter(int ftp)
1018 static char filter[128];
1025 add_filters(filter, &n, NTRXS, trxs);
1028 add_filters(filter, &n, NTRNS, trns);
1031 add_filters(filter, &n, NSTOS, stos);
1034 add_filters(filter, &n, NSTXS, stxs);
1037 add_filters(filter, &n, NTPXS, tpxs);
1040 sprintf(filter, "*%s", ftp2ext(ftp));
1047 gmx_bool is_optional(const t_filenm *fnm)
1049 return ((fnm->flag & ffOPT) == ffOPT);
1052 gmx_bool is_output(const t_filenm *fnm)
1054 return ((fnm->flag & ffWRITE) == ffWRITE);
1057 gmx_bool is_set(const t_filenm *fnm)
1059 return ((fnm->flag & ffSET) == ffSET);
1062 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
1065 char buf[STRLEN], newname[STRLEN];
1068 for (i = 0; i < nfile; i++)
1070 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
1072 /* We never use multiple _outputs_, but we might as well check
1073 for it, just in case... */
1074 for (j = 0; j < fnm[i].nfiles; j++)
1076 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
1077 extpos = strrchr(buf, '.');
1079 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
1080 free(fnm[i].fns[j]);
1081 fnm[i].fns[j] = strdup(newname);
1088 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
1094 for (i = 0; i < nf; i++)
1096 ret[i] = tfn[i]; /* just directly copy all non-string fields */
1099 ret[i].opt = strdup(tfn[i].opt);
1108 ret[i].fn = strdup(tfn[i].fn);
1115 if (tfn[i].nfiles > 0)
1117 snew(ret[i].fns, tfn[i].nfiles);
1118 for (j = 0; j < tfn[i].nfiles; j++)
1120 ret[i].fns[j] = strdup(tfn[i].fns[j]);
1127 void done_filenms(int nf, t_filenm fnm[])
1131 for (i = 0; i < nf; ++i)
1133 for (j = 0; j < fnm[i].nfiles; ++j)
1135 sfree(fnm[i].fns[j]);