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
35 /* This file is completely threadsafe - keep it that way! */
36 #include "gromacs/commandline/pargs.h"
53 #include <sys/schedctl.h>
54 #include <sys/sysmp.h>
57 #include "gromacs/legacyheaders/gmx_fatal.h"
58 #include "gromacs/legacyheaders/macros.h"
59 #include "gromacs/legacyheaders/network.h"
60 #include "gromacs/legacyheaders/smalloc.h"
61 #include "gromacs/legacyheaders/string2.h"
62 #include "gromacs/legacyheaders/thread_mpi/threads.h"
64 #include "gromacs/commandline/cmdlinehelpcontext.h"
65 #include "gromacs/commandline/shellcompletions.h"
66 #include "gromacs/commandline/wman.h"
67 #include "gromacs/fileio/timecontrol.h"
68 #include "gromacs/utility/exceptions.h"
69 #include "gromacs/utility/gmxassert.h"
72 /* The source code in this file should be thread-safe.
73 Please keep it that way. */
75 static void usage(const char *type, const char *arg)
77 GMX_ASSERT(arg != NULL, "NULL command-line argument should not occur");
78 gmx_fatal(FARGS, "Expected %s argument for option %s\n", type, arg);
81 /* Scan an int for argument argv[*i] from argument at argv[*i + 1].
82 * eg: -p 32. argv[*i] is only used for error reporting.
83 * If there is no value, or the conversion is not successful, the
84 * routine exits with an error, otherwise it returns the value found.
85 * *i is incremented once.
87 static int iscan(int argc, char *argv[], int *i)
89 const char *const arg = argv[*i];
92 usage("an integer", arg);
94 const char *const value = argv[++(*i)];
96 int var = std::strtol(value, &endptr, 10);
97 if (*value == '\0' || *endptr != '\0')
99 usage("an integer", arg);
104 /* Same as above, but for large integer values */
105 static gmx_int64_t istepscan(int argc, char *argv[], int *i)
107 const char *const arg = argv[*i];
110 usage("an integer", arg);
112 const char *const value = argv[++(*i)];
114 gmx_int64_t var = str_to_int64_t(value, &endptr);
115 if (*value == '\0' || *endptr != '\0')
117 usage("an integer", arg);
122 /* Routine similar to the above, but working on doubles. */
123 static double dscan(int argc, char *argv[], int *i)
125 const char *const arg = argv[*i];
128 usage("a real", arg);
130 const char *const value = argv[++(*i)];
132 double var = std::strtod(value, &endptr);
133 if (*value == '\0' || *endptr != '\0')
135 usage("a real", arg);
140 /* Routine similar to the above, but working on strings. The pointer
141 * returned is a pointer to the argv field.
143 static char *sscan(int argc, char *argv[], int *i)
147 if ( (argv[(*i)+1][0] == '-') && (argc > (*i)+2) &&
148 (argv[(*i)+2][0] != '-') )
150 fprintf(stderr, "Possible missing string argument for option %s\n\n",
156 usage("a string", argv[*i]);
162 gmx_bool is_hidden(t_pargs *pa)
164 return ((strstr(pa->desc, "HIDDEN") != NULL) ||
165 (strstr(pa->desc, "[hidden]") != NULL));
168 int nenum(const char *const enumc[])
173 /* we *can* compare pointers directly here! */
174 while (enumc[i] && enumc[0] != enumc[i])
182 /* Read a number of arguments from the command line.
183 * For etINT, etREAL and etCHAR an extra argument is read (when present)
184 * for etBOOL the gmx_boolean option is changed to the negate value
185 * If !bKeepArgs, the command line arguments are removed from the command line
187 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 */
307 /* Remove used entries */
308 for (i = j = 0; (i <= *argc); i++)
320 int opt2parg_int(const char *option, int nparg, t_pargs pa[])
324 for (i = 0; (i < nparg); i++)
326 if (strcmp(pa[i].option, option) == 0)
332 gmx_fatal(FARGS, "No integer option %s in pargs", option);
337 gmx_bool opt2parg_gmx_bool(const char *option, int nparg, t_pargs pa[])
341 for (i = 0; (i < nparg); i++)
343 if (strcmp(pa[i].option, option) == 0)
349 gmx_fatal(FARGS, "No boolean option %s in pargs", option);
354 real opt2parg_real(const char *option, int nparg, t_pargs pa[])
358 for (i = 0; (i < nparg); i++)
360 if (strcmp(pa[i].option, option) == 0)
366 gmx_fatal(FARGS, "No real option %s in pargs", option);
371 const char *opt2parg_str(const char *option, int nparg, t_pargs pa[])
375 for (i = 0; (i < nparg); i++)
377 if (strcmp(pa[i].option, option) == 0)
383 gmx_fatal(FARGS, "No string option %s in pargs", option);
388 gmx_bool opt2parg_bSet(const char *option, int nparg, t_pargs pa[])
392 for (i = 0; (i < nparg); i++)
394 if (strcmp(pa[i].option, option) == 0)
400 gmx_fatal(FARGS, "No such option %s in pargs", option);
402 return FALSE; /* Too make some compilers happy */
405 const char *opt2parg_enum(const char *option, int nparg, t_pargs pa[])
409 for (i = 0; (i < nparg); i++)
411 if (strcmp(pa[i].option, option) == 0)
417 gmx_fatal(FARGS, "No such option %s in pargs", option);
422 /********************************************************************
423 * parse_common_args()
426 static void set_default_time_unit(const char *time_list[], gmx_bool bCanTime)
429 const char *select = NULL;
433 select = getenv("GMXTIMEUNIT");
437 while (time_list[i] && strcmp(time_list[i], select) != 0)
443 if (!bCanTime || select == NULL ||
444 time_list[i] == NULL || strcmp(time_list[i], select) != 0)
446 /* Set it to the default: ps */
448 while (time_list[i] && strcmp(time_list[i], "ps") != 0)
454 time_list[0] = time_list[i];
457 static void set_default_xvg_format(const char *xvg_list[])
462 select = getenv("GMX_VIEW_XVG");
465 /* The default is the first option */
466 xvg_list[0] = xvg_list[1];
471 while (xvg_list[i] && strcmp(xvg_list[i], select) != 0)
475 if (xvg_list[i] != NULL)
477 xvg_list[0] = xvg_list[i];
481 xvg_list[0] = xvg_list[exvgNONE];
486 static int add_parg(int npargs, t_pargs *pa, t_pargs *pa_add)
488 memcpy(&(pa[npargs]), pa_add, sizeof(*pa_add));
493 static char *mk_desc(t_pargs *pa, const char *time_unit_str)
495 char *newdesc = NULL, *ndesc = NULL, *nptr = NULL;
496 const char*ptr = NULL;
499 /* First compute length for description */
500 len = strlen(pa->desc)+1;
501 if ((ptr = strstr(pa->desc, "HIDDEN")) != NULL)
505 if (pa->type == etENUM)
508 for (k = 1; (pa->u.c[k] != NULL); k++)
510 len += strlen(pa->u.c[k])+12;
515 /* add label for hidden options */
518 sprintf(newdesc, "[hidden] %s", ptr+6);
522 strcpy(newdesc, pa->desc);
525 /* change '%t' into time_unit */
526 #define TUNITLABEL "%t"
527 #define NTUNIT strlen(TUNITLABEL)
528 if (pa->type == etTIME)
530 while ( (nptr = strstr(newdesc, TUNITLABEL)) != NULL)
534 len += strlen(time_unit_str)-NTUNIT;
536 strcpy(ndesc, newdesc);
537 strcat(ndesc, time_unit_str);
547 /* Add extra comment for enumerateds */
548 if (pa->type == etENUM)
550 strcat(newdesc, ": ");
551 for (k = 1; (pa->u.c[k] != NULL); k++)
553 strcat(newdesc, "[TT]");
554 strcat(newdesc, pa->u.c[k]);
555 strcat(newdesc, "[tt]");
556 /* Print a comma everywhere but at the last one */
557 if (pa->u.c[k+1] != NULL)
559 if (pa->u.c[k+2] == NULL)
561 strcat(newdesc, " or ");
565 strcat(newdesc, ", ");
574 gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
575 int nfile, t_filenm fnm[], int npargs, t_pargs *pa,
576 int ndesc, const char **desc,
577 int nbugs, const char **bugs,
580 const char *manstr[] = {
581 NULL, "no", "completion", NULL
583 /* This array should match the order of the enum in oenv.h */
584 const char *xvg_format[] = { NULL, "xmgrace", "xmgr", "none", NULL };
585 /* This array should match the order of the enum in oenv.h */
586 const char *time_units[] = {
587 NULL, "fs", "ps", "ns", "us", "ms", "s",
590 int nicelevel = 0, debug_level = 0;
592 real tbegin = 0, tend = 0, tdelta = 0;
593 gmx_bool bView = FALSE;
595 t_pargs *all_pa = NULL;
600 "-npri", FALSE, etINT, {&npri},
601 "HIDDEN Set non blocking priority (try 128)"
605 "-nice", FALSE, etINT, {&nicelevel},
608 t_pargs deffnm_pa = {
609 "-deffnm", FALSE, etSTR, {&deffnm},
610 "Set the default filename for all file options"
613 "-b", FALSE, etTIME, {&tbegin},
614 "First frame (%t) to read from trajectory"
617 "-e", FALSE, etTIME, {&tend},
618 "Last frame (%t) to read from trajectory"
621 "-dt", FALSE, etTIME, {&tdelta},
622 "Only use frame when t MOD dt = first time (%t)"
625 "-w", FALSE, etBOOL, {&bView},
626 "View output [TT].xvg[tt], [TT].xpm[tt], [TT].eps[tt] and [TT].pdb[tt] files"
629 "-xvg", FALSE, etENUM, {xvg_format},
630 "xvg plot formatting"
633 "-tu", FALSE, etENUM, {time_units},
636 /* Maximum number of extra arguments */
640 { "-man", FALSE, etENUM, {manstr},
641 "HIDDENWrite manual and quit" },
642 { "-debug", FALSE, etINT, {&debug_level},
643 "HIDDENWrite file with debug information, 1: short, 2: also x and f" },
645 #define NPCA_PA asize(pca_pa)
647 int i, j, k, npall, max_pa;
649 // Handle the flags argument, which is a bit field
650 // The FF macro returns whether or not the bit is set
651 #define FF(arg) ((Flags & arg) == arg)
653 /* Check for double arguments */
654 for (i = 1; (i < *argc); i++)
656 if (argv[i] && (strlen(argv[i]) > 1) && (!std::isdigit(argv[i][1])))
658 for (j = i+1; (j < *argc); j++)
660 if ( (argv[i][0] == '-') && (argv[j][0] == '-') &&
661 (strcmp(argv[i], argv[j]) == 0) )
663 if (FF(PCA_NOEXIT_ON_ARGS))
665 fprintf(stderr, "Double command line argument %s\n",
670 gmx_fatal(FARGS, "Double command line argument %s\n",
679 /* Check ALL the flags ... */
680 max_pa = NPCA_PA + EXTRA_PA + npargs+1;
681 snew(all_pa, max_pa);
683 for (i = npall = 0; (i < static_cast<int>(NPCA_PA)); i++)
685 npall = add_parg(npall, all_pa, &(pca_pa[i]));
689 const char *envstr = getenv("GMXNPRIALL");
692 npri = strtol(envstr, NULL, 10);
696 envstr = getenv("GMXNPRI");
699 npri = strtol(envstr, NULL, 10);
702 npall = add_parg(npall, all_pa, &npri_pa);
709 npall = add_parg(npall, all_pa, &nice_pa);
711 if (FF(PCA_CAN_SET_DEFFNM))
713 npall = add_parg(npall, all_pa, &deffnm_pa);
715 if (FF(PCA_CAN_BEGIN))
717 npall = add_parg(npall, all_pa, &begin_pa);
721 npall = add_parg(npall, all_pa, &end_pa);
725 npall = add_parg(npall, all_pa, &dt_pa);
727 if (FF(PCA_TIME_UNIT))
729 npall = add_parg(npall, all_pa, &time_pa);
731 if (FF(PCA_CAN_VIEW))
733 npall = add_parg(npall, all_pa, &view_pa);
737 for (i = 0; (i < nfile); i++)
739 bXvgr = bXvgr || (fnm[i].ftp == efXVG);
743 npall = add_parg(npall, all_pa, &xvg_pa);
746 /* Now append the program specific arguments */
747 for (i = 0; (i < npargs); i++)
749 npall = add_parg(npall, all_pa, &(pa[i]));
752 /* set etENUM options to default */
753 for (i = 0; (i < npall); i++)
755 if (all_pa[i].type == etENUM)
757 all_pa[i].u.c[0] = all_pa[i].u.c[1];
760 set_default_time_unit(time_units, FF(PCA_TIME_UNIT));
761 set_default_xvg_format(xvg_format);
763 /* Now parse all the command-line options */
764 get_pargs(argc, argv, npall, all_pa, FF(PCA_KEEP_ARGS));
766 /* set program name, command line, and default values for output options */
767 output_env_init(oenv, *argc, argv, (time_unit_t)nenum(time_units), bView,
768 (xvg_format_t)nenum(xvg_format), 0, debug_level);
770 /* Parse the file args */
771 parse_file_args(argc, argv, nfile, fnm, deffnm,
772 FF(PCA_KEEP_ARGS), !FF(PCA_NOT_READ_NODE));
774 /* Open the debug file */
779 if (gmx_mpi_initialized())
781 sprintf(buf, "%s%d.debug", output_env_get_short_program_name(*oenv),
786 sprintf(buf, "%s.debug", output_env_get_short_program_name(*oenv));
789 init_debug(debug_level, buf);
790 fprintf(stderr, "Opening debug file %s (src code file %s, line %d)\n",
791 buf, __FILE__, __LINE__);
794 /* Now copy the results back... */
795 for (i = 0, k = npall-npargs; (i < npargs); i++, k++)
797 memcpy(&(pa[i]), &(all_pa[k]), (size_t)sizeof(pa[i]));
801 for (i = 0; (i < npall); i++)
803 all_pa[i].desc = mk_desc(&(all_pa[i]), output_env_get_time_unit(*oenv));
806 #if (defined __sgi && USE_SGI_FPE)
813 const gmx::CommandLineHelpContext *context =
814 gmx::GlobalCommandLineHelpContext::get();
815 bExit = (context != NULL || strcmp(manstr[0], "no") != 0);
816 if (!(FF(PCA_QUIET)))
820 write_man(*context, ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
822 else if (!strcmp(manstr[0], "completion"))
824 const char *program = output_env_get_short_program_name(*oenv);
825 /* one file each for csh, bash and zsh if we do completions */
826 write_completions("completion-zsh", program, nfile, fnm, npall, all_pa);
827 write_completions("completion-bash", program, nfile, fnm, npall, all_pa);
828 write_completions("completion-csh", program, nfile, fnm, npall, all_pa);
832 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
834 /* Set the nice level */
836 if (npri != 0 && !bExit)
838 schedctl(MPTS_RTPRI, 0, npri);
844 /* The some system, e.g. the catamount kernel on cray xt3 do not have nice(2). */
845 if (nicelevel != 0 && !bExit)
847 static gmx_bool nice_set = FALSE; /* only set it once */
848 static tMPI_Thread_mutex_t init_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
849 tMPI_Thread_mutex_lock(&init_mutex);
852 if (nice(nicelevel) == -1)
854 /* Do nothing, but use the return value to avoid warnings. */
858 tMPI_Thread_mutex_unlock(&init_mutex);
863 /* convert time options, must be done after printing! */
865 for (i = 0; i < npall; i++)
867 if ((all_pa[i].type == etTIME) && (*all_pa[i].u.r >= 0))
869 *all_pa[i].u.r *= output_env_get_time_invfactor(*oenv);
873 /* Extract Time info from arguments */
874 if (FF(PCA_CAN_BEGIN) && opt2parg_bSet("-b", npall, all_pa))
876 setTimeValue(TBEGIN, opt2parg_real("-b", npall, all_pa));
879 if (FF(PCA_CAN_END) && opt2parg_bSet("-e", npall, all_pa))
881 setTimeValue(TEND, opt2parg_real("-e", npall, all_pa));
884 if (FF(PCA_CAN_DT) && opt2parg_bSet("-dt", npall, all_pa))
886 setTimeValue(TDELTA, opt2parg_real("-dt", npall, all_pa));
890 for (i = 0; i < npall; ++i)
892 sfree((void *)all_pa[i].desc);
896 if (!FF(PCA_NOEXIT_ON_ARGS))