Remove unused PCA_KEEP_ARGS
[alexxy/gromacs.git] / src / gromacs / commandline / pargs.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gromacs/commandline/pargs.h"
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include <cctype>
45 #include <cstdio>
46 #include <cstdlib>
47 #include <cstring>
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 /* used for npri */
54 #ifdef __sgi
55 #include <sys/schedctl.h>
56 #include <sys/sysmp.h>
57 #endif
58
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"
65
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"
72
73
74 /* The source code in this file should be thread-safe.
75       Please keep it that way. */
76
77 static void usage(const char *type, const char *arg)
78 {
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);
81 }
82
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.
88  */
89 static int iscan(int argc, char *argv[], int *i)
90 {
91     const char *const arg = argv[*i];
92     if (argc <= (*i)+1)
93     {
94         usage("an integer", arg);
95     }
96     const char *const value = argv[++(*i)];
97     char             *endptr;
98     int               var = std::strtol(value, &endptr, 10);
99     if (*value == '\0' || *endptr != '\0')
100     {
101         usage("an integer", arg);
102     }
103     return var;
104 }
105
106 /* Same as above, but for large integer values */
107 static gmx_int64_t istepscan(int argc, char *argv[], int *i)
108 {
109     const char *const arg = argv[*i];
110     if (argc <= (*i)+1)
111     {
112         usage("an integer", arg);
113     }
114     const char *const value = argv[++(*i)];
115     char             *endptr;
116     gmx_int64_t       var = str_to_int64_t(value, &endptr);
117     if (*value == '\0' || *endptr != '\0')
118     {
119         usage("an integer", arg);
120     }
121     return var;
122 }
123
124 /* Routine similar to the above, but working on doubles. */
125 static double dscan(int argc, char *argv[], int *i)
126 {
127     const char *const arg = argv[*i];
128     if (argc <= (*i)+1)
129     {
130         usage("a real", arg);
131     }
132     const char *const value = argv[++(*i)];
133     char             *endptr;
134     double            var = std::strtod(value, &endptr);
135     if (*value == '\0' || *endptr != '\0')
136     {
137         usage("a real", arg);
138     }
139     return var;
140 }
141
142 /* Routine similar to the above, but working on strings. The pointer
143  * returned is a pointer to the argv field.
144  */
145 static char *sscan(int argc, char *argv[], int *i)
146 {
147     if (argc > (*i)+1)
148     {
149         if ( (argv[(*i)+1][0] == '-') && (argc > (*i)+2) &&
150              (argv[(*i)+2][0] != '-') )
151         {
152             fprintf(stderr, "Possible missing string argument for option %s\n\n",
153                     argv[*i]);
154         }
155     }
156     else
157     {
158         usage("a string", argv[*i]);
159     }
160
161     return argv[++(*i)];
162 }
163
164 gmx_bool is_hidden(t_pargs *pa)
165 {
166     return ((strstr(pa->desc, "HIDDEN") != NULL) ||
167             (strstr(pa->desc, "[hidden]") != NULL));
168 }
169
170 int nenum(const char *const enumc[])
171 {
172     int i;
173
174     i = 1;
175     /* we *can* compare pointers directly here! */
176     while (enumc[i] && enumc[0] != enumc[i])
177     {
178         i++;
179     }
180
181     return i;
182 }
183
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
187  */
188 static void get_pargs(int *argc, char *argv[], int nparg, t_pargs pa[])
189 {
190     int       i, j, k, match;
191     gmx_bool *bKeep;
192     char      buf[32];
193     char     *ptr;
194
195     snew(bKeep, *argc+1);
196     bKeep[0]     = TRUE;
197     bKeep[*argc] = TRUE;
198
199     for (i = 1; (i < *argc); i++)
200     {
201         bKeep[i] = TRUE;
202         for (j = 0; (j < nparg); j++)
203         {
204             if (pa[j].type == etBOOL)
205             {
206                 sprintf(buf, "-no%s", pa[j].option+1);
207                 if (strcmp(pa[j].option, argv[i]) == 0)
208                 {
209                     *pa[j].u.b = TRUE;
210                     pa[j].bSet = TRUE;
211                     bKeep[i]   = FALSE;
212                 }
213                 else if (strcmp(buf, argv[i]) == 0)
214                 {
215                     *pa[j].u.b = FALSE;
216                     pa[j].bSet = TRUE;
217                     bKeep[i]   = FALSE;
218                 }
219             }
220             else if (strcmp(pa[j].option, argv[i]) == 0)
221             {
222                 if (pa[j].bSet)
223                 {
224                     fprintf(stderr, "Setting option %s more than once!\n",
225                             pa[j].option);
226                 }
227                 pa[j].bSet = TRUE;
228                 bKeep[i]   = FALSE;
229                 switch (pa[j].type)
230                 {
231                     case etINT:
232                         *pa[j].u.i = iscan(*argc, argv, &i);
233                         break;
234                     case etINT64:
235                         *pa[j].u.is = istepscan(*argc, argv, &i);
236                         break;
237                     case etTIME:
238                     case etREAL:
239                         *pa[j].u.r = dscan(*argc, argv, &i);
240                         break;
241                     case etSTR:
242                         *(pa[j].u.c) = sscan(*argc, argv, &i);
243                         break;
244                     case etENUM:
245                         match = -1;
246                         ptr   = sscan(*argc, argv, &i);
247                         for (k = 1; (pa[j].u.c[k] != NULL); k++)
248                         {
249                             /* only check ptr against beginning of
250                                pa[j].u.c[k] */
251                             if (gmx_strncasecmp(ptr, pa[j].u.c[k], strlen(ptr)) == 0)
252                             {
253                                 if ( ( match == -1 ) ||
254                                      ( strlen(pa[j].u.c[k]) <
255                                        strlen(pa[j].u.c[match]) ) )
256                                 {
257                                     match = k;
258                                 }
259                             }
260                         }
261                         if (match != -1)
262                         {
263                             pa[j].u.c[0] = pa[j].u.c[match];
264                         }
265                         else
266                         {
267                             gmx_fatal(FARGS, "Invalid argument %s for option %s",
268                                       ptr, pa[j].option);
269                         }
270                         break;
271                     case etRVEC:
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]) ) )
276                         {
277                             (*pa[j].u.rv)[1]     =
278                                 (*pa[j].u.rv)[2] =
279                                     (*pa[j].u.rv)[0];
280                         }
281                         else
282                         {
283                             bKeep[i]         = FALSE;
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]) ) )
288                             {
289                                 gmx_fatal(FARGS,
290                                           "%s: vector must have 1 or 3 real parameters",
291                                           pa[j].option);
292                             }
293                             bKeep[i]         = FALSE;
294                             (*pa[j].u.rv)[2] = dscan(*argc, argv, &i);
295                         }
296                         break;
297                     default:
298                         gmx_fatal(FARGS, "Invalid type %d in pargs", pa[j].type);
299                 }
300                 /* i may be incremented, so set it to not keep */
301                 bKeep[i] = FALSE;
302             }
303         }
304     }
305
306     /* Remove used entries */
307     for (i = j = 0; (i <= *argc); i++)
308     {
309         if (bKeep[i])
310         {
311             argv[j++] = argv[i];
312         }
313     }
314     (*argc) = j-1;
315     sfree(bKeep);
316 }
317
318 int opt2parg_int(const char *option, int nparg, t_pargs pa[])
319 {
320     int i;
321
322     for (i = 0; (i < nparg); i++)
323     {
324         if (strcmp(pa[i].option, option) == 0)
325         {
326             return *pa[i].u.i;
327         }
328     }
329
330     gmx_fatal(FARGS, "No integer option %s in pargs", option);
331
332     return 0;
333 }
334
335 gmx_bool opt2parg_gmx_bool(const char *option, int nparg, t_pargs pa[])
336 {
337     int i;
338
339     for (i = 0; (i < nparg); i++)
340     {
341         if (strcmp(pa[i].option, option) == 0)
342         {
343             return *pa[i].u.b;
344         }
345     }
346
347     gmx_fatal(FARGS, "No boolean option %s in pargs", option);
348
349     return FALSE;
350 }
351
352 real opt2parg_real(const char *option, int nparg, t_pargs pa[])
353 {
354     int i;
355
356     for (i = 0; (i < nparg); i++)
357     {
358         if (strcmp(pa[i].option, option) == 0)
359         {
360             return *pa[i].u.r;
361         }
362     }
363
364     gmx_fatal(FARGS, "No real option %s in pargs", option);
365
366     return 0.0;
367 }
368
369 const char *opt2parg_str(const char *option, int nparg, t_pargs pa[])
370 {
371     int i;
372
373     for (i = 0; (i < nparg); i++)
374     {
375         if (strcmp(pa[i].option, option) == 0)
376         {
377             return *(pa[i].u.c);
378         }
379     }
380
381     gmx_fatal(FARGS, "No string option %s in pargs", option);
382
383     return NULL;
384 }
385
386 gmx_bool opt2parg_bSet(const char *option, int nparg, t_pargs pa[])
387 {
388     int i;
389
390     for (i = 0; (i < nparg); i++)
391     {
392         if (strcmp(pa[i].option, option) == 0)
393         {
394             return pa[i].bSet;
395         }
396     }
397
398     gmx_fatal(FARGS, "No such option %s in pargs", option);
399
400     return FALSE; /* Too make some compilers happy */
401 }
402
403 const char *opt2parg_enum(const char *option, int nparg, t_pargs pa[])
404 {
405     int i;
406
407     for (i = 0; (i < nparg); i++)
408     {
409         if (strcmp(pa[i].option, option) == 0)
410         {
411             return pa[i].u.c[0];
412         }
413     }
414
415     gmx_fatal(FARGS, "No such option %s in pargs", option);
416
417     return NULL;
418 }
419
420 /********************************************************************
421  * parse_common_args()
422  */
423
424 static void set_default_time_unit(const char *time_list[], gmx_bool bCanTime)
425 {
426     int         i      = 0;
427     const char *select = NULL;
428
429     if (bCanTime)
430     {
431         select = getenv("GMXTIMEUNIT");
432         if (select != NULL)
433         {
434             i = 1;
435             while (time_list[i] && strcmp(time_list[i], select) != 0)
436             {
437                 i++;
438             }
439         }
440     }
441     if (!bCanTime || select == NULL ||
442         time_list[i] == NULL || strcmp(time_list[i], select) != 0)
443     {
444         /* Set it to the default: ps */
445         i = 1;
446         while (time_list[i] && strcmp(time_list[i], "ps") != 0)
447         {
448             i++;
449         }
450
451     }
452     time_list[0] = time_list[i];
453 }
454
455 static void set_default_xvg_format(const char *xvg_list[])
456 {
457     int         i;
458     const char *select;
459
460     select = getenv("GMX_VIEW_XVG");
461     if (select == NULL)
462     {
463         /* The default is the first option */
464         xvg_list[0] = xvg_list[1];
465     }
466     else
467     {
468         i = 1;
469         while (xvg_list[i] && strcmp(xvg_list[i], select) != 0)
470         {
471             i++;
472         }
473         if (xvg_list[i] != NULL)
474         {
475             xvg_list[0] = xvg_list[i];
476         }
477         else
478         {
479             xvg_list[0] = xvg_list[exvgNONE];
480         }
481     }
482 }
483
484 static int add_parg(int npargs, t_pargs *pa, t_pargs *pa_add)
485 {
486     memcpy(&(pa[npargs]), pa_add, sizeof(*pa_add));
487
488     return npargs+1;
489 }
490
491 static char *mk_desc(t_pargs *pa, const char *time_unit_str)
492 {
493     char      *newdesc = NULL, *ndesc = NULL, *nptr = NULL;
494     const char*ptr     = NULL;
495     int        len, k;
496
497     /* First compute length for description */
498     len = strlen(pa->desc)+1;
499     if ((ptr = strstr(pa->desc, "HIDDEN")) != NULL)
500     {
501         len += 4;
502     }
503     if (pa->type == etENUM)
504     {
505         len += 10;
506         for (k = 1; (pa->u.c[k] != NULL); k++)
507         {
508             len += strlen(pa->u.c[k])+12;
509         }
510     }
511     snew(newdesc, len);
512
513     /* add label for hidden options */
514     if (is_hidden(pa))
515     {
516         sprintf(newdesc, "[hidden] %s", ptr+6);
517     }
518     else
519     {
520         strcpy(newdesc, pa->desc);
521     }
522
523     /* change '%t' into time_unit */
524 #define TUNITLABEL "%t"
525 #define NTUNIT strlen(TUNITLABEL)
526     if (pa->type == etTIME)
527     {
528         while ( (nptr = strstr(newdesc, TUNITLABEL)) != NULL)
529         {
530             nptr[0] = '\0';
531             nptr   += NTUNIT;
532             len    += strlen(time_unit_str)-NTUNIT;
533             snew(ndesc, len);
534             strcpy(ndesc, newdesc);
535             strcat(ndesc, time_unit_str);
536             strcat(ndesc, nptr);
537             sfree(newdesc);
538             newdesc = ndesc;
539             ndesc   = NULL;
540         }
541     }
542 #undef TUNITLABEL
543 #undef NTUNIT
544
545     /* Add extra comment for enumerateds */
546     if (pa->type == etENUM)
547     {
548         strcat(newdesc, ": ");
549         for (k = 1; (pa->u.c[k] != NULL); k++)
550         {
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)
556             {
557                 if (pa->u.c[k+2] == NULL)
558                 {
559                     strcat(newdesc, " or ");
560                 }
561                 else
562                 {
563                     strcat(newdesc, ", ");
564                 }
565             }
566         }
567     }
568     return newdesc;
569 }
570
571
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,
576                            output_env_t *oenv)
577 {
578     const char *manstr[] = {
579         NULL, "no", "completion", NULL
580     };
581     /* This array should match the order of the enum in oenv.h */
582     const char *xvg_format[] = { NULL, "xmgrace", "xmgr", "none", NULL };
583     /* This array should match the order of the enum in oenv.h */
584     const char *time_units[] = {
585         NULL, "fs", "ps", "ns", "us", "ms", "s",
586         NULL
587     };
588     int         nicelevel = 0, debug_level = 0;
589     char       *deffnm    = NULL;
590     real        tbegin    = 0, tend = 0, tdelta = 0;
591     gmx_bool    bView     = FALSE;
592
593     t_pargs    *all_pa = NULL;
594
595 #ifdef __sgi
596     int     npri      = 0;
597     t_pargs npri_pa   = {
598         "-npri", FALSE, etINT,   {&npri},
599         "HIDDEN Set non blocking priority (try 128)"
600     };
601 #endif
602     t_pargs nice_pa   = {
603         "-nice", FALSE, etINT,   {&nicelevel},
604         "Set the nicelevel"
605     };
606     t_pargs deffnm_pa = {
607         "-deffnm", FALSE, etSTR, {&deffnm},
608         "Set the default filename for all file options"
609     };
610     t_pargs begin_pa  = {
611         "-b",    FALSE, etTIME,  {&tbegin},
612         "First frame (%t) to read from trajectory"
613     };
614     t_pargs end_pa    = {
615         "-e",    FALSE, etTIME,  {&tend},
616         "Last frame (%t) to read from trajectory"
617     };
618     t_pargs dt_pa     = {
619         "-dt",   FALSE, etTIME,  {&tdelta},
620         "Only use frame when t MOD dt = first time (%t)"
621     };
622     t_pargs view_pa   = {
623         "-w",    FALSE, etBOOL,  {&bView},
624         "View output [TT].xvg[tt], [TT].xpm[tt], [TT].eps[tt] and [TT].pdb[tt] files"
625     };
626     t_pargs xvg_pa    = {
627         "-xvg",  FALSE, etENUM,  {xvg_format},
628         "xvg plot formatting"
629     };
630     t_pargs time_pa   = {
631         "-tu",   FALSE, etENUM,  {time_units},
632         "Time unit"
633     };
634     /* Maximum number of extra arguments */
635 #define EXTRA_PA 16
636
637     t_pargs  pca_pa[] = {
638         { "-man",  FALSE, etENUM,  {manstr},
639           "HIDDENWrite manual and quit" },
640         { "-debug", FALSE, etINT, {&debug_level},
641           "HIDDENWrite file with debug information, 1: short, 2: also x and f" },
642     };
643 #define NPCA_PA asize(pca_pa)
644     gmx_bool bXvgr;
645     int      i, j, k, npall, max_pa;
646
647     // Handle the flags argument, which is a bit field
648     // The FF macro returns whether or not the bit is set
649 #define FF(arg) ((Flags & arg) == arg)
650
651     /* Check for double arguments */
652     for (i = 1; (i < *argc); i++)
653     {
654         if (argv[i] && (strlen(argv[i]) > 1) && (!std::isdigit(argv[i][1])))
655         {
656             for (j = i+1; (j < *argc); j++)
657             {
658                 if ( (argv[i][0] == '-') && (argv[j][0] == '-') &&
659                      (strcmp(argv[i], argv[j]) == 0) )
660                 {
661                     if (FF(PCA_NOEXIT_ON_ARGS))
662                     {
663                         fprintf(stderr, "Double command line argument %s\n",
664                                 argv[i]);
665                     }
666                     else
667                     {
668                         gmx_fatal(FARGS, "Double command line argument %s\n",
669                                   argv[i]);
670                     }
671                 }
672             }
673         }
674     }
675     debug_gmx();
676
677     /* Check ALL the flags ... */
678     max_pa = NPCA_PA + EXTRA_PA + npargs+1;
679     snew(all_pa, max_pa);
680
681     for (i = npall = 0; (i < static_cast<int>(NPCA_PA)); i++)
682     {
683         npall = add_parg(npall, all_pa, &(pca_pa[i]));
684     }
685
686 #ifdef __sgi
687     const char *envstr = getenv("GMXNPRIALL");
688     if (envstr)
689     {
690         npri = strtol(envstr, NULL, 10);
691     }
692     if (FF(PCA_BE_NICE))
693     {
694         envstr = getenv("GMXNPRI");
695         if (envstr)
696         {
697             npri = strtol(envstr, NULL, 10);
698         }
699     }
700     npall = add_parg(npall, all_pa, &npri_pa);
701 #endif
702
703     if (FF(PCA_BE_NICE))
704     {
705         nicelevel = 19;
706     }
707     npall = add_parg(npall, all_pa, &nice_pa);
708
709     if (FF(PCA_CAN_SET_DEFFNM))
710     {
711         npall = add_parg(npall, all_pa, &deffnm_pa);
712     }
713     if (FF(PCA_CAN_BEGIN))
714     {
715         npall = add_parg(npall, all_pa, &begin_pa);
716     }
717     if (FF(PCA_CAN_END))
718     {
719         npall = add_parg(npall, all_pa, &end_pa);
720     }
721     if (FF(PCA_CAN_DT))
722     {
723         npall = add_parg(npall, all_pa, &dt_pa);
724     }
725     if (FF(PCA_TIME_UNIT))
726     {
727         npall = add_parg(npall, all_pa, &time_pa);
728     }
729     if (FF(PCA_CAN_VIEW))
730     {
731         npall = add_parg(npall, all_pa, &view_pa);
732     }
733
734     bXvgr = FALSE;
735     for (i = 0; (i < nfile); i++)
736     {
737         bXvgr = bXvgr ||  (fnm[i].ftp == efXVG);
738     }
739     if (bXvgr)
740     {
741         npall = add_parg(npall, all_pa, &xvg_pa);
742     }
743
744     /* Now append the program specific arguments */
745     for (i = 0; (i < npargs); i++)
746     {
747         npall = add_parg(npall, all_pa, &(pa[i]));
748     }
749
750     /* set etENUM options to default */
751     for (i = 0; (i < npall); i++)
752     {
753         if (all_pa[i].type == etENUM)
754         {
755             all_pa[i].u.c[0] = all_pa[i].u.c[1];
756         }
757     }
758     set_default_time_unit(time_units, FF(PCA_TIME_UNIT));
759     set_default_xvg_format(xvg_format);
760
761     /* Now parse all the command-line options */
762     get_pargs(argc, argv, npall, all_pa);
763
764     /* set program name, command line, and default values for output options */
765     output_env_init(oenv, *argc, argv, (time_unit_t)nenum(time_units), bView,
766                     (xvg_format_t)nenum(xvg_format), 0, debug_level);
767
768     /* Parse the file args */
769     parse_file_args(argc, argv, nfile, fnm, deffnm, !FF(PCA_NOT_READ_NODE));
770
771     /* Open the debug file */
772     if (debug_level > 0)
773     {
774         char buf[256];
775
776         if (gmx_mpi_initialized())
777         {
778             sprintf(buf, "%s%d.debug", output_env_get_short_program_name(*oenv),
779                     gmx_node_rank());
780         }
781         else
782         {
783             sprintf(buf, "%s.debug", output_env_get_short_program_name(*oenv));
784         }
785
786         init_debug(debug_level, buf);
787         fprintf(stderr, "Opening debug file %s (src code file %s, line %d)\n",
788                 buf, __FILE__, __LINE__);
789     }
790
791     /* Now copy the results back... */
792     for (i = 0, k = npall-npargs; (i < npargs); i++, k++)
793     {
794         memcpy(&(pa[i]), &(all_pa[k]), (size_t)sizeof(pa[i]));
795     }
796
797
798     for (i = 0; (i < npall); i++)
799     {
800         all_pa[i].desc = mk_desc(&(all_pa[i]), output_env_get_time_unit(*oenv));
801     }
802
803 #if (defined __sgi && USE_SGI_FPE)
804     doexceptions();
805 #endif
806
807     bool bExit = false;
808     try
809     {
810         const gmx::CommandLineHelpContext *context =
811             gmx::GlobalCommandLineHelpContext::get();
812         bExit = (context != NULL || strcmp(manstr[0], "no") != 0);
813         if (!(FF(PCA_QUIET)))
814         {
815             if (context != NULL)
816             {
817                 write_man(*context, ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
818             }
819             else if (!strcmp(manstr[0], "completion"))
820             {
821                 const char *program = output_env_get_short_program_name(*oenv);
822                 /* one file each for csh, bash and zsh if we do completions */
823                 write_completions("completion-zsh", program, nfile, fnm, npall, all_pa);
824                 write_completions("completion-bash", program, nfile, fnm, npall, all_pa);
825                 write_completions("completion-csh", program, nfile, fnm, npall, all_pa);
826             }
827         }
828     }
829     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
830
831     /* Set the nice level */
832 #ifdef __sgi
833     if (npri != 0 && !bExit)
834     {
835         schedctl(MPTS_RTPRI, 0, npri);
836     }
837 #endif
838
839 #ifdef HAVE_UNISTD_H
840 #ifndef GMX_NO_NICE
841     /* The some system, e.g. the catamount kernel on cray xt3 do not have nice(2). */
842     if (nicelevel != 0 && !bExit)
843     {
844         static gmx_bool            nice_set   = FALSE; /* only set it once */
845         static tMPI_Thread_mutex_t init_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
846         tMPI_Thread_mutex_lock(&init_mutex);
847         if (!nice_set)
848         {
849             if (nice(nicelevel) == -1)
850             {
851                 /* Do nothing, but use the return value to avoid warnings. */
852             }
853             nice_set = TRUE;
854         }
855         tMPI_Thread_mutex_unlock(&init_mutex);
856     }
857 #endif
858 #endif
859
860     /* convert time options, must be done after printing! */
861
862     for (i = 0; i < npall; i++)
863     {
864         if (all_pa[i].type == etTIME && all_pa[i].bSet)
865         {
866             *all_pa[i].u.r *= output_env_get_time_invfactor(*oenv);
867         }
868     }
869
870     /* Extract Time info from arguments */
871     if (FF(PCA_CAN_BEGIN) && opt2parg_bSet("-b", npall, all_pa))
872     {
873         setTimeValue(TBEGIN, opt2parg_real("-b", npall, all_pa));
874     }
875
876     if (FF(PCA_CAN_END) && opt2parg_bSet("-e", npall, all_pa))
877     {
878         setTimeValue(TEND, opt2parg_real("-e", npall, all_pa));
879     }
880
881     if (FF(PCA_CAN_DT) && opt2parg_bSet("-dt", npall, all_pa))
882     {
883         setTimeValue(TDELTA, opt2parg_real("-dt", npall, all_pa));
884     }
885
886     /* clear memory */
887     for (i = 0; i < npall; ++i)
888     {
889         sfree((void *)all_pa[i].desc);
890     }
891     sfree(all_pa);
892
893     if (!FF(PCA_NOEXIT_ON_ARGS))
894     {
895         if (*argc > 1)
896         {
897             gmx_cmd(argv[1]);
898         }
899     }
900     return !bExit;
901 #undef FF
902 }