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,2014, 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.
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gromacs/commandline/pargs.h"
55 #include <sys/schedctl.h>
56 #include <sys/sysmp.h>
59 #include "gromacs/legacyheaders/gmx_fatal.h"
60 #include "gromacs/legacyheaders/macros.h"
61 #include "gromacs/legacyheaders/network.h"
62 #include "gromacs/legacyheaders/smalloc.h"
63 #include "gromacs/legacyheaders/string2.h"
64 #include "gromacs/legacyheaders/thread_mpi/threads.h"
66 #include "gromacs/commandline/cmdlinehelpcontext.h"
67 #include "gromacs/commandline/shellcompletions.h"
68 #include "gromacs/commandline/wman.h"
69 #include "gromacs/fileio/timecontrol.h"
70 #include "gromacs/utility/exceptions.h"
71 #include "gromacs/utility/gmxassert.h"
74 /* The source code in this file should be thread-safe.
75 Please keep it that way. */
77 static void usage(const char *type, const char *arg)
79 GMX_ASSERT(arg != NULL, "NULL command-line argument should not occur");
80 gmx_fatal(FARGS, "Expected %s argument for option %s\n", type, arg);
83 /* Scan an int for argument argv[*i] from argument at argv[*i + 1].
84 * eg: -p 32. argv[*i] is only used for error reporting.
85 * If there is no value, or the conversion is not successful, the
86 * routine exits with an error, otherwise it returns the value found.
87 * *i is incremented once.
89 static int iscan(int argc, char *argv[], int *i)
91 const char *const arg = argv[*i];
94 usage("an integer", arg);
96 const char *const value = argv[++(*i)];
98 int var = std::strtol(value, &endptr, 10);
99 if (*value == '\0' || *endptr != '\0')
101 usage("an integer", arg);
106 /* Same as above, but for large integer values */
107 static gmx_int64_t istepscan(int argc, char *argv[], int *i)
109 const char *const arg = argv[*i];
112 usage("an integer", arg);
114 const char *const value = argv[++(*i)];
116 gmx_int64_t var = str_to_int64_t(value, &endptr);
117 if (*value == '\0' || *endptr != '\0')
119 usage("an integer", arg);
124 /* Routine similar to the above, but working on doubles. */
125 static double dscan(int argc, char *argv[], int *i)
127 const char *const arg = argv[*i];
130 usage("a real", arg);
132 const char *const value = argv[++(*i)];
134 double var = std::strtod(value, &endptr);
135 if (*value == '\0' || *endptr != '\0')
137 usage("a real", arg);
142 /* Routine similar to the above, but working on strings. The pointer
143 * returned is a pointer to the argv field.
145 static char *sscan(int argc, char *argv[], int *i)
149 if ( (argv[(*i)+1][0] == '-') && (argc > (*i)+2) &&
150 (argv[(*i)+2][0] != '-') )
152 fprintf(stderr, "Possible missing string argument for option %s\n\n",
158 usage("a string", argv[*i]);
164 gmx_bool is_hidden(t_pargs *pa)
166 return ((strstr(pa->desc, "HIDDEN") != NULL) ||
167 (strstr(pa->desc, "[hidden]") != NULL));
170 int nenum(const char *const enumc[])
175 /* we *can* compare pointers directly here! */
176 while (enumc[i] && enumc[0] != enumc[i])
184 /* Read a number of arguments from the command line.
185 * For etINT, etREAL and etCHAR an extra argument is read (when present)
186 * for etBOOL the gmx_boolean option is changed to the negate value
188 static void get_pargs(int *argc, char *argv[], int nparg, t_pargs pa[])
195 snew(bKeep, *argc+1);
199 for (i = 1; (i < *argc); i++)
202 for (j = 0; (j < nparg); j++)
204 if (pa[j].type == etBOOL)
206 sprintf(buf, "-no%s", pa[j].option+1);
207 if (strcmp(pa[j].option, argv[i]) == 0)
213 else if (strcmp(buf, argv[i]) == 0)
220 else if (strcmp(pa[j].option, argv[i]) == 0)
224 fprintf(stderr, "Setting option %s more than once!\n",
232 *pa[j].u.i = iscan(*argc, argv, &i);
235 *pa[j].u.is = istepscan(*argc, argv, &i);
239 *pa[j].u.r = dscan(*argc, argv, &i);
242 *(pa[j].u.c) = sscan(*argc, argv, &i);
246 ptr = sscan(*argc, argv, &i);
247 for (k = 1; (pa[j].u.c[k] != NULL); k++)
249 /* only check ptr against beginning of
251 if (gmx_strncasecmp(ptr, pa[j].u.c[k], strlen(ptr)) == 0)
253 if ( ( match == -1 ) ||
254 ( strlen(pa[j].u.c[k]) <
255 strlen(pa[j].u.c[match]) ) )
263 pa[j].u.c[0] = pa[j].u.c[match];
267 gmx_fatal(FARGS, "Invalid argument %s for option %s",
272 (*pa[j].u.rv)[0] = dscan(*argc, argv, &i);
273 if ( (i+1 == *argc) ||
274 ( (argv[i+1][0] == '-') &&
275 !isdigit(argv[i+1][1]) ) )
284 (*pa[j].u.rv)[1] = dscan(*argc, argv, &i);
285 if ( (i+1 == *argc) ||
286 ( (argv[i+1][0] == '-') &&
287 !isdigit(argv[i+1][1]) ) )
290 "%s: vector must have 1 or 3 real parameters",
294 (*pa[j].u.rv)[2] = dscan(*argc, argv, &i);
298 gmx_fatal(FARGS, "Invalid type %d in pargs", pa[j].type);
300 /* i may be incremented, so set it to not keep */
306 /* Remove used entries */
307 for (i = j = 0; (i <= *argc); i++)
318 int opt2parg_int(const char *option, int nparg, t_pargs pa[])
322 for (i = 0; (i < nparg); i++)
324 if (strcmp(pa[i].option, option) == 0)
330 gmx_fatal(FARGS, "No integer option %s in pargs", option);
335 gmx_bool opt2parg_gmx_bool(const char *option, int nparg, t_pargs pa[])
339 for (i = 0; (i < nparg); i++)
341 if (strcmp(pa[i].option, option) == 0)
347 gmx_fatal(FARGS, "No boolean option %s in pargs", option);
352 real opt2parg_real(const char *option, int nparg, t_pargs pa[])
356 for (i = 0; (i < nparg); i++)
358 if (strcmp(pa[i].option, option) == 0)
364 gmx_fatal(FARGS, "No real option %s in pargs", option);
369 const char *opt2parg_str(const char *option, int nparg, t_pargs pa[])
373 for (i = 0; (i < nparg); i++)
375 if (strcmp(pa[i].option, option) == 0)
381 gmx_fatal(FARGS, "No string option %s in pargs", option);
386 gmx_bool opt2parg_bSet(const char *option, int nparg, t_pargs pa[])
390 for (i = 0; (i < nparg); i++)
392 if (strcmp(pa[i].option, option) == 0)
398 gmx_fatal(FARGS, "No such option %s in pargs", option);
400 return FALSE; /* Too make some compilers happy */
403 const char *opt2parg_enum(const char *option, int nparg, t_pargs pa[])
407 for (i = 0; (i < nparg); i++)
409 if (strcmp(pa[i].option, option) == 0)
415 gmx_fatal(FARGS, "No such option %s in pargs", option);
420 /********************************************************************
421 * parse_common_args()
424 static void set_default_time_unit(const char *time_list[], gmx_bool bCanTime)
427 const char *select = NULL;
431 select = getenv("GMXTIMEUNIT");
435 while (time_list[i] && strcmp(time_list[i], select) != 0)
441 if (!bCanTime || select == NULL ||
442 time_list[i] == NULL || strcmp(time_list[i], select) != 0)
444 /* Set it to the default: ps */
446 while (time_list[i] && strcmp(time_list[i], "ps") != 0)
452 time_list[0] = time_list[i];
455 static void set_default_xvg_format(const char *xvg_list[])
460 select = getenv("GMX_VIEW_XVG");
463 /* The default is the first option */
464 xvg_list[0] = xvg_list[1];
469 while (xvg_list[i] && strcmp(xvg_list[i], select) != 0)
473 if (xvg_list[i] != NULL)
475 xvg_list[0] = xvg_list[i];
479 xvg_list[0] = xvg_list[exvgNONE];
484 static int add_parg(int npargs, t_pargs *pa, t_pargs *pa_add)
486 memcpy(&(pa[npargs]), pa_add, sizeof(*pa_add));
491 static char *mk_desc(t_pargs *pa, const char *time_unit_str)
493 char *newdesc = NULL, *ndesc = NULL, *nptr = NULL;
494 const char*ptr = NULL;
497 /* First compute length for description */
498 len = strlen(pa->desc)+1;
499 if ((ptr = strstr(pa->desc, "HIDDEN")) != NULL)
503 if (pa->type == etENUM)
506 for (k = 1; (pa->u.c[k] != NULL); k++)
508 len += strlen(pa->u.c[k])+12;
513 /* add label for hidden options */
516 sprintf(newdesc, "[hidden] %s", ptr+6);
520 strcpy(newdesc, pa->desc);
523 /* change '%t' into time_unit */
524 #define TUNITLABEL "%t"
525 #define NTUNIT strlen(TUNITLABEL)
526 if (pa->type == etTIME)
528 while ( (nptr = strstr(newdesc, TUNITLABEL)) != NULL)
532 len += strlen(time_unit_str)-NTUNIT;
534 strcpy(ndesc, newdesc);
535 strcat(ndesc, time_unit_str);
545 /* Add extra comment for enumerateds */
546 if (pa->type == etENUM)
548 strcat(newdesc, ": ");
549 for (k = 1; (pa->u.c[k] != NULL); k++)
551 strcat(newdesc, "[TT]");
552 strcat(newdesc, pa->u.c[k]);
553 strcat(newdesc, "[tt]");
554 /* Print a comma everywhere but at the last one */
555 if (pa->u.c[k+1] != NULL)
557 if (pa->u.c[k+2] == NULL)
559 strcat(newdesc, " or ");
563 strcat(newdesc, ", ");
572 gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
573 int nfile, t_filenm fnm[], int npargs, t_pargs *pa,
574 int ndesc, const char **desc,
575 int nbugs, const char **bugs,
578 /* This array should match the order of the enum in oenv.h */
579 const char *xvg_format[] = { NULL, "xmgrace", "xmgr", "none", NULL };
580 /* This array should match the order of the enum in oenv.h */
581 const char *time_units[] = {
582 NULL, "fs", "ps", "ns", "us", "ms", "s",
585 int nicelevel = 0, debug_level = 0;
587 real tbegin = 0, tend = 0, tdelta = 0;
588 gmx_bool bView = FALSE;
590 t_pargs *all_pa = NULL;
595 "-npri", FALSE, etINT, {&npri},
596 "HIDDEN Set non blocking priority (try 128)"
600 "-nice", FALSE, etINT, {&nicelevel},
603 t_pargs deffnm_pa = {
604 "-deffnm", FALSE, etSTR, {&deffnm},
605 "Set the default filename for all file options"
608 "-b", FALSE, etTIME, {&tbegin},
609 "First frame (%t) to read from trajectory"
612 "-e", FALSE, etTIME, {&tend},
613 "Last frame (%t) to read from trajectory"
616 "-dt", FALSE, etTIME, {&tdelta},
617 "Only use frame when t MOD dt = first time (%t)"
620 "-w", FALSE, etBOOL, {&bView},
621 "View output [TT].xvg[tt], [TT].xpm[tt], [TT].eps[tt] and [TT].pdb[tt] files"
624 "-xvg", FALSE, etENUM, {xvg_format},
625 "xvg plot formatting"
628 "-tu", FALSE, etENUM, {time_units},
631 /* Maximum number of extra arguments */
635 { "-debug", FALSE, etINT, {&debug_level},
636 "HIDDENWrite file with debug information, 1: short, 2: also x and f" },
638 #define NPCA_PA asize(pca_pa)
640 int i, j, k, npall, max_pa;
642 // Handle the flags argument, which is a bit field
643 // The FF macro returns whether or not the bit is set
644 #define FF(arg) ((Flags & arg) == arg)
646 /* Check for double arguments */
647 for (i = 1; (i < *argc); i++)
649 if (argv[i] && (strlen(argv[i]) > 1) && (!std::isdigit(argv[i][1])))
651 for (j = i+1; (j < *argc); j++)
653 if ( (argv[i][0] == '-') && (argv[j][0] == '-') &&
654 (strcmp(argv[i], argv[j]) == 0) )
656 if (FF(PCA_NOEXIT_ON_ARGS))
658 fprintf(stderr, "Double command line argument %s\n",
663 gmx_fatal(FARGS, "Double command line argument %s\n",
672 /* Check ALL the flags ... */
673 max_pa = NPCA_PA + EXTRA_PA + npargs+1;
674 snew(all_pa, max_pa);
676 for (i = npall = 0; (i < static_cast<int>(NPCA_PA)); i++)
678 npall = add_parg(npall, all_pa, &(pca_pa[i]));
682 const char *envstr = getenv("GMXNPRIALL");
685 npri = strtol(envstr, NULL, 10);
689 envstr = getenv("GMXNPRI");
692 npri = strtol(envstr, NULL, 10);
695 npall = add_parg(npall, all_pa, &npri_pa);
702 npall = add_parg(npall, all_pa, &nice_pa);
704 if (FF(PCA_CAN_SET_DEFFNM))
706 npall = add_parg(npall, all_pa, &deffnm_pa);
708 if (FF(PCA_CAN_BEGIN))
710 npall = add_parg(npall, all_pa, &begin_pa);
714 npall = add_parg(npall, all_pa, &end_pa);
718 npall = add_parg(npall, all_pa, &dt_pa);
720 if (FF(PCA_TIME_UNIT))
722 npall = add_parg(npall, all_pa, &time_pa);
724 if (FF(PCA_CAN_VIEW))
726 npall = add_parg(npall, all_pa, &view_pa);
730 for (i = 0; (i < nfile); i++)
732 bXvgr = bXvgr || (fnm[i].ftp == efXVG);
736 npall = add_parg(npall, all_pa, &xvg_pa);
739 /* Now append the program specific arguments */
740 for (i = 0; (i < npargs); i++)
742 npall = add_parg(npall, all_pa, &(pa[i]));
745 /* set etENUM options to default */
746 for (i = 0; (i < npall); i++)
748 if (all_pa[i].type == etENUM)
750 all_pa[i].u.c[0] = all_pa[i].u.c[1];
753 set_default_time_unit(time_units, FF(PCA_TIME_UNIT));
754 set_default_xvg_format(xvg_format);
756 /* Now parse all the command-line options */
757 get_pargs(argc, argv, npall, all_pa);
759 /* set program name, command line, and default values for output options */
760 output_env_init(oenv, *argc, argv, (time_unit_t)nenum(time_units), bView,
761 (xvg_format_t)nenum(xvg_format), 0, debug_level);
763 /* Parse the file args */
764 parse_file_args(argc, argv, nfile, fnm, deffnm, !FF(PCA_NOT_READ_NODE));
766 /* Open the debug file */
771 if (gmx_mpi_initialized())
773 sprintf(buf, "%s%d.debug", output_env_get_short_program_name(*oenv),
778 sprintf(buf, "%s.debug", output_env_get_short_program_name(*oenv));
781 init_debug(debug_level, buf);
782 fprintf(stderr, "Opening debug file %s (src code file %s, line %d)\n",
783 buf, __FILE__, __LINE__);
786 /* Now copy the results back... */
787 for (i = 0, k = npall-npargs; (i < npargs); i++, k++)
789 memcpy(&(pa[i]), &(all_pa[k]), (size_t)sizeof(pa[i]));
793 for (i = 0; (i < npall); i++)
795 all_pa[i].desc = mk_desc(&(all_pa[i]), output_env_get_time_unit(*oenv));
798 #if (defined __sgi && USE_SGI_FPE)
805 const gmx::CommandLineHelpContext *context =
806 gmx::GlobalCommandLineHelpContext::get();
807 bExit = (context != NULL);
808 if (context != NULL && !(FF(PCA_QUIET)))
810 if (context->isCompletionExport())
812 context->shellCompletionWriter().writeLegacyModuleCompletions(
813 context->moduleDisplayName(), nfile, fnm, npall, all_pa);
817 write_man(*context, ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
821 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
823 /* Set the nice level */
825 if (npri != 0 && !bExit)
827 schedctl(MPTS_RTPRI, 0, npri);
833 /* The some system, e.g. the catamount kernel on cray xt3 do not have nice(2). */
834 if (nicelevel != 0 && !bExit)
836 static gmx_bool nice_set = FALSE; /* only set it once */
837 static tMPI_Thread_mutex_t init_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
838 tMPI_Thread_mutex_lock(&init_mutex);
841 if (nice(nicelevel) == -1)
843 /* Do nothing, but use the return value to avoid warnings. */
847 tMPI_Thread_mutex_unlock(&init_mutex);
852 /* convert time options, must be done after printing! */
854 for (i = 0; i < npall; i++)
856 if (all_pa[i].type == etTIME && all_pa[i].bSet)
858 *all_pa[i].u.r *= output_env_get_time_invfactor(*oenv);
862 /* Extract Time info from arguments */
863 if (FF(PCA_CAN_BEGIN) && opt2parg_bSet("-b", npall, all_pa))
865 setTimeValue(TBEGIN, opt2parg_real("-b", npall, all_pa));
868 if (FF(PCA_CAN_END) && opt2parg_bSet("-e", npall, all_pa))
870 setTimeValue(TEND, opt2parg_real("-e", npall, all_pa));
873 if (FF(PCA_CAN_DT) && opt2parg_bSet("-dt", npall, all_pa))
875 setTimeValue(TDELTA, opt2parg_real("-dt", npall, all_pa));
879 for (i = 0; i < npall; ++i)
881 sfree((void *)all_pa[i].desc);
885 if (!FF(PCA_NOEXIT_ON_ARGS))