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! */
44 #include "gmx_fatal.h"
52 /* The source code in this file should be thread-safe.
53 Please keep it that way. */
55 gmx_bool is_hidden(t_pargs *pa)
57 return ((strstr(pa->desc,"HIDDEN") != NULL) ||
58 (strstr(pa->desc,"[hidden]") != NULL));
61 void get_pargs(int *argc,char *argv[],int nparg,t_pargs pa[],gmx_bool bKeepArgs)
72 for(i=1; (i<*argc); i++) {
74 for(j=0; (j<nparg); j++) {
75 if (pa[j].type == etBOOL) {
76 sprintf(buf,"-no%s",pa[j].option+1);
77 if (strcmp(pa[j].option,argv[i])== 0) {
82 else if (strcmp(buf,argv[i])== 0) {
88 else if (strcmp(pa[j].option,argv[i])== 0) {
90 fprintf(stderr,"Setting option %s more than once!\n",
96 *pa[j].u.i = iscan(*argc,argv,&i);
99 *pa[j].u.is = istepscan(*argc,argv,&i);
103 *pa[j].u.r = dscan(*argc,argv,&i);
106 *(pa[j].u.c) = sscan(*argc,argv,&i);
110 ptr = sscan(*argc,argv,&i);
111 for(k=1; (pa[j].u.c[k] != NULL); k++)
112 /* only check ptr against beginning of
114 if (gmx_strncasecmp(ptr,pa[j].u.c[k],strlen(ptr)) == 0)
115 if ( ( match == NOTSET ) ||
116 ( strlen(pa[j].u.c[k]) <
117 strlen(pa[j].u.c[match]) ) )
120 pa[j].u.c[0] = pa[j].u.c[match];
122 gmx_fatal(FARGS,"Invalid argument %s for option %s",
126 (*pa[j].u.rv)[0] = dscan(*argc,argv,&i);
127 if ( (i+1 == *argc) ||
128 ( (argv[i+1][0]=='-') &&
129 !isdigit(argv[i+1][1]) ) )
135 (*pa[j].u.rv)[1] = dscan(*argc,argv,&i);
136 if ( (i+1 == *argc) ||
137 ( (argv[i+1][0]=='-') &&
138 !isdigit(argv[i+1][1]) ) )
140 "%s: vector must have 1 or 3 real parameters",
143 (*pa[j].u.rv)[2] = dscan(*argc,argv,&i);
147 gmx_fatal(FARGS,"Invalid type %d in pargs",pa[j].type);
149 /* i may be incremented, so set it to not keep */
155 /* Remove used entries */
156 for(i=j=0; (i<=*argc); i++) {
165 int opt2parg_int(const char *option,int nparg,t_pargs pa[])
169 for(i=0; (i<nparg); i++)
170 if (strcmp(pa[i].option,option) == 0)
173 gmx_fatal(FARGS,"No integer option %s in pargs",option);
178 gmx_bool opt2parg_gmx_bool(const char *option,int nparg,t_pargs pa[])
182 for(i=0; (i<nparg); i++)
183 if (strcmp(pa[i].option,option) == 0)
186 gmx_fatal(FARGS,"No gmx_boolean option %s in pargs",option);
191 real opt2parg_real(const char *option,int nparg,t_pargs pa[])
195 for(i=0; (i<nparg); i++)
196 if (strcmp(pa[i].option,option) == 0)
199 gmx_fatal(FARGS,"No real option %s in pargs",option);
204 const char *opt2parg_str(const char *option,int nparg,t_pargs pa[])
208 for(i=0; (i<nparg); i++)
209 if (strcmp(pa[i].option,option) == 0)
212 gmx_fatal(FARGS,"No string option %s in pargs",option);
217 gmx_bool opt2parg_bSet(const char *option,int nparg,t_pargs pa[])
221 for(i=0; (i<nparg); i++)
222 if (strcmp(pa[i].option,option) == 0)
225 gmx_fatal(FARGS,"No such option %s in pargs",option);
227 return FALSE; /* Too make some compilers happy */
230 const char *opt2parg_enum(const char *option,int nparg,t_pargs pa[])
234 for(i=0; (i<nparg); i++)
235 if (strcmp(pa[i].option,option) == 0)
238 gmx_fatal(FARGS,"No such option %s in pargs",option);
243 char *pa_val(t_pargs *pa, char buf[], int sz)
246 char buf_str[1256]; buf_str[0]='\0';
251 gmx_fatal(FARGS,"Buffer must be at least 255 chars\n");
255 sprintf(buf,"%-d",*(pa->u.i));
257 case etGMX_LARGE_INT:
258 sprintf(buf,gmx_large_int_pfmt,*(pa->u.is));
263 sprintf(buf_str,"%-6g",r);
264 strcpy(buf, buf_str);
267 sprintf(buf,"%-6s",*(pa->u.b) ? "yes" : "no");
271 if (strlen(*(pa->u.c)) >= (size_t)sz)
272 gmx_fatal(FARGS,"Argument too long: \"%d\"\n",*(pa->u.c));
274 strcpy(buf,*(pa->u.c));
278 strcpy(buf,*(pa->u.c));
281 sprintf(buf,"%g %g %g",(*pa->u.rv)[0],
292 char *pargs_print_line(t_pargs *pa,gmx_bool bLeadingSpace)
294 char buf[LONGSTR],*buf2,*tmp,*desc;
296 snew(buf2,LONGSTR+strlen(pa->desc));
297 snew(tmp,LONGSTR+strlen(pa->desc));
299 if (pa->type == etBOOL)
300 sprintf(buf,"-[no]%s",pa->option+1);
302 strcpy(buf,pa->option);
303 desc = check_tty(pa->desc);
304 if (strlen(buf)>((OPTLEN+TYPELEN)-max(strlen(argtp[pa->type]),4))) {
305 sprintf(buf2,"%s%s %-6s %-6s %-s\n",
306 bLeadingSpace ? " " : "",buf,
307 argtp[pa->type],pa_val(pa,tmp,LONGSTR-1),desc);
308 } else if (strlen(buf)>OPTLEN) {
309 /* so type can be 3 or 4 char's, this fits in the %4s */
310 sprintf(buf2,"%s%-14s %-4s %-6s %-s\n",
311 bLeadingSpace ? " " : "",buf,argtp[pa->type],
312 pa_val(pa,tmp,LONGSTR-1),desc);
314 sprintf(buf2,"%s%-12s %-6s %-6s %-s\n",
315 bLeadingSpace ? " " : "",buf,argtp[pa->type],
316 pa_val(pa,tmp,LONGSTR-1),desc);
320 tmp = wrap_lines(buf2,78,28,FALSE);
327 void print_pargs(FILE *fp, int npargs,t_pargs pa[],gmx_bool bLeadingSpace)
329 gmx_bool bShowHidden;
330 char buf[32],buf2[256],tmp[256];
334 /* Cannot call opt2parg_bSet here, because it crashes when the option
335 * is not in the list (mdrun)
338 for(i=0; (i<npargs); i++)
339 if ((strcmp(pa[i].option,"-hidden")==0) && (pa[i].bSet))
343 fprintf(fp,"%s%-12s %-6s %-6s %-s\n",
344 bLeadingSpace ? " " : "","Option","Type","Value","Description");
345 fprintf(fp,"%s------------------------------------------------------\n",
346 bLeadingSpace ? " " : "");
347 for(i=0; (i<npargs); i++) {
348 if (bShowHidden || !is_hidden(&pa[i])) {
349 wdesc = pargs_print_line(&pa[i],bLeadingSpace);
350 fprintf(fp,"%s",wdesc);
358 void pr_enums(FILE *fp, int npargs,t_pargs pa[], int shell)
364 for (i=0; i<npargs; i++)
365 if (pa[i].type==etENUM) {
366 fprintf(fp," \"n/%s/(",pa[i].option);
367 for(j=1; pa[i].u.c[j]; j++)
368 fprintf(fp," %s",pa[i].u.c[j]);
373 for (i=0; i<npargs; i++)
374 if (pa[i].type==etENUM) {
375 fprintf(fp,"%s) COMPREPLY=( $(compgen -W '",pa[i].option);
376 for(j=1; pa[i].u.c[j]; j++)
377 fprintf(fp," %s",pa[i].u.c[j]);
378 fprintf(fp," ' -- $c ));;\n");
382 for (i=0; i<npargs; i++)
383 if (pa[i].type==etENUM) {
384 fprintf(fp,"- 'c[-1,%s]' -s \"", pa[i].option);
385 for(j=1; pa[i].u.c[j]; j++)
386 fprintf(fp," %s",pa[i].u.c[j]);