Replace Progs/RNAalifold with x64 binary and add all other programs
[jabaws.git] / binaries / src / ViennaRNA / Kinfold / cmdline.c
1 /*
2   File autogenerated by gengetopt version 2.22.5
3   generated with the following command:
4   gengetopt -i kinfold.ggo --default-optional --include-getopt
5
6   The developers of gengetopt consider the fixed text that goes in all
7   gengetopt output files to be in the public domain:
8   we make no copyright claims on it.
9 */
10
11 /* If we use autoconf.  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23
24
25 #include "cmdline.h"
26
27 const char *gengetopt_args_info_purpose = "Kinetic Folding Program for Nucleic Acids";
28
29 const char *gengetopt_args_info_usage = "Usage: Kinfold [OPTIONS]...";
30
31 const char *gengetopt_args_info_description = "";
32
33 const char *gengetopt_args_info_full_help[] = {
34   "  -h, --help            Print help and exit",
35   "      --full-help       Print help, including hidden options, and exit",
36   "  -V, --version         Print version and exit",
37   "\nEnergy Model:",
38   "  -d, --dangle=INT      <0|1|2> set dangling end model to (none|normal|double)  \n                          (possible values=\"0\", \"1\", \"2\" default=`2')",
39   "  -T, --Temp=FLOAT      simulation temperature  (default=`37')",
40   "  -P, --Par=filename    read energy-parameter-file",
41   "      --logML           use logarithmic multiloop energies instead of linear  \n                          (default=on)",
42   "\nMoveSet:",
43   "      --noShift         turn off shift-moves  (default=off)",
44   "      --noLP            forbid structures with isolated base-pairs  \n                          (default=off)",
45   "\nSimulation:",
46   "      --seed=STRING     set random number seed specify 3 integers as \n                          int=int=int  (default=`clock')",
47   "      --time=FLOAT      set maxtime of simulation  (default=`500')",
48   "      --num=INT         set number of trajectories  (default=`1')",
49   "      --start           read start structure from stdin (otherwise use open \n                          chain)  (default=off)",
50   "      --stop            read stop structure(s) from stdin (optherwise use MFE)  \n                          (default=off)",
51   "      --met             use Metropolis rule for rates (not Kawasaki rule)  \n                          (default=off)",
52   "      --fpt             compute first passage time (stop when a stop-structure \n                          is reached)  (default=on)",
53   "      --grow=FLOAT      grow chain every <float> time units  (default=`0')",
54   "      --glen=INT        initial size of growing chain  (default=`15')",
55   "      --phi=DOUBLE      set phi value",
56   "      --pbounds=STRING  specify 3 floats for phi_min, phi_inc, phi_max in the \n                          form <d1=d2=d3>",
57   "\nOutput:",
58   "      --log=filename    set basename of log-file  (default=`kinout')",
59   "  -q, --silent          no output to stdout  (default=off)",
60   "  -v, --verbose         more information to stdout  (default=off)",
61   "      --lmin            output only local minima to stdout  (default=off)",
62   "      --cut=FLOAT       output structures with E <= <float> to stdout  \n                          (default=`20')",
63   "1st line sequence2nd line start structure (if option --start is used)following \nlines stop structures",
64     0
65 };
66
67 static void
68 init_help_array(void)
69 {
70   gengetopt_args_info_help[0] = gengetopt_args_info_full_help[0];
71   gengetopt_args_info_help[1] = gengetopt_args_info_full_help[1];
72   gengetopt_args_info_help[2] = gengetopt_args_info_full_help[2];
73   gengetopt_args_info_help[3] = gengetopt_args_info_full_help[3];
74   gengetopt_args_info_help[4] = gengetopt_args_info_full_help[4];
75   gengetopt_args_info_help[5] = gengetopt_args_info_full_help[5];
76   gengetopt_args_info_help[6] = gengetopt_args_info_full_help[6];
77   gengetopt_args_info_help[7] = gengetopt_args_info_full_help[7];
78   gengetopt_args_info_help[8] = gengetopt_args_info_full_help[8];
79   gengetopt_args_info_help[9] = gengetopt_args_info_full_help[9];
80   gengetopt_args_info_help[10] = gengetopt_args_info_full_help[10];
81   gengetopt_args_info_help[11] = gengetopt_args_info_full_help[11];
82   gengetopt_args_info_help[12] = gengetopt_args_info_full_help[12];
83   gengetopt_args_info_help[13] = gengetopt_args_info_full_help[13];
84   gengetopt_args_info_help[14] = gengetopt_args_info_full_help[14];
85   gengetopt_args_info_help[15] = gengetopt_args_info_full_help[15];
86   gengetopt_args_info_help[16] = gengetopt_args_info_full_help[16];
87   gengetopt_args_info_help[17] = gengetopt_args_info_full_help[17];
88   gengetopt_args_info_help[18] = gengetopt_args_info_full_help[18];
89   gengetopt_args_info_help[19] = gengetopt_args_info_full_help[19];
90   gengetopt_args_info_help[20] = gengetopt_args_info_full_help[20];
91   gengetopt_args_info_help[21] = gengetopt_args_info_full_help[23];
92   gengetopt_args_info_help[22] = gengetopt_args_info_full_help[24];
93   gengetopt_args_info_help[23] = gengetopt_args_info_full_help[25];
94   gengetopt_args_info_help[24] = gengetopt_args_info_full_help[26];
95   gengetopt_args_info_help[25] = gengetopt_args_info_full_help[27];
96   gengetopt_args_info_help[26] = gengetopt_args_info_full_help[28];
97   gengetopt_args_info_help[27] = gengetopt_args_info_full_help[29];
98   gengetopt_args_info_help[28] = 0; 
99   
100 }
101
102 const char *gengetopt_args_info_help[29];
103
104 typedef enum {ARG_NO
105   , ARG_FLAG
106   , ARG_STRING
107   , ARG_INT
108   , ARG_FLOAT
109   , ARG_DOUBLE
110 } cmdline_parser_arg_type;
111
112 static
113 void clear_given (struct gengetopt_args_info *args_info);
114 static
115 void clear_args (struct gengetopt_args_info *args_info);
116
117 static int
118 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
119                         struct cmdline_parser_params *params, const char *additional_error);
120
121
122 const char *cmdline_parser_dangle_values[] = {"0", "1", "2", 0}; /*< Possible values for dangle. */
123
124 static char *
125 gengetopt_strdup (const char *s);
126
127 static
128 void clear_given (struct gengetopt_args_info *args_info)
129 {
130   args_info->help_given = 0 ;
131   args_info->full_help_given = 0 ;
132   args_info->version_given = 0 ;
133   args_info->dangle_given = 0 ;
134   args_info->Temp_given = 0 ;
135   args_info->Par_given = 0 ;
136   args_info->logML_given = 0 ;
137   args_info->noShift_given = 0 ;
138   args_info->noLP_given = 0 ;
139   args_info->seed_given = 0 ;
140   args_info->time_given = 0 ;
141   args_info->num_given = 0 ;
142   args_info->start_given = 0 ;
143   args_info->stop_given = 0 ;
144   args_info->met_given = 0 ;
145   args_info->fpt_given = 0 ;
146   args_info->grow_given = 0 ;
147   args_info->glen_given = 0 ;
148   args_info->phi_given = 0 ;
149   args_info->pbounds_given = 0 ;
150   args_info->log_given = 0 ;
151   args_info->silent_given = 0 ;
152   args_info->verbose_given = 0 ;
153   args_info->lmin_given = 0 ;
154   args_info->cut_given = 0 ;
155 }
156
157 static
158 void clear_args (struct gengetopt_args_info *args_info)
159 {
160   FIX_UNUSED (args_info);
161   args_info->dangle_arg = 2;
162   args_info->dangle_orig = NULL;
163   args_info->Temp_arg = 37;
164   args_info->Temp_orig = NULL;
165   args_info->Par_arg = NULL;
166   args_info->Par_orig = NULL;
167   args_info->logML_flag = 1;
168   args_info->noShift_flag = 0;
169   args_info->noLP_flag = 0;
170   args_info->seed_arg = gengetopt_strdup ("clock");
171   args_info->seed_orig = NULL;
172   args_info->time_arg = 500;
173   args_info->time_orig = NULL;
174   args_info->num_arg = 1;
175   args_info->num_orig = NULL;
176   args_info->start_flag = 0;
177   args_info->stop_flag = 0;
178   args_info->met_flag = 0;
179   args_info->fpt_flag = 1;
180   args_info->grow_arg = 0;
181   args_info->grow_orig = NULL;
182   args_info->glen_arg = 15;
183   args_info->glen_orig = NULL;
184   args_info->phi_orig = NULL;
185   args_info->pbounds_arg = NULL;
186   args_info->pbounds_orig = NULL;
187   args_info->log_arg = gengetopt_strdup ("kinout");
188   args_info->log_orig = NULL;
189   args_info->silent_flag = 0;
190   args_info->verbose_flag = 0;
191   args_info->lmin_flag = 0;
192   args_info->cut_arg = 20;
193   args_info->cut_orig = NULL;
194   
195 }
196
197 static
198 void init_args_info(struct gengetopt_args_info *args_info)
199 {
200
201   init_help_array(); 
202   args_info->help_help = gengetopt_args_info_full_help[0] ;
203   args_info->full_help_help = gengetopt_args_info_full_help[1] ;
204   args_info->version_help = gengetopt_args_info_full_help[2] ;
205   args_info->dangle_help = gengetopt_args_info_full_help[4] ;
206   args_info->Temp_help = gengetopt_args_info_full_help[5] ;
207   args_info->Par_help = gengetopt_args_info_full_help[6] ;
208   args_info->logML_help = gengetopt_args_info_full_help[7] ;
209   args_info->noShift_help = gengetopt_args_info_full_help[9] ;
210   args_info->noLP_help = gengetopt_args_info_full_help[10] ;
211   args_info->seed_help = gengetopt_args_info_full_help[12] ;
212   args_info->time_help = gengetopt_args_info_full_help[13] ;
213   args_info->num_help = gengetopt_args_info_full_help[14] ;
214   args_info->start_help = gengetopt_args_info_full_help[15] ;
215   args_info->stop_help = gengetopt_args_info_full_help[16] ;
216   args_info->met_help = gengetopt_args_info_full_help[17] ;
217   args_info->fpt_help = gengetopt_args_info_full_help[18] ;
218   args_info->grow_help = gengetopt_args_info_full_help[19] ;
219   args_info->glen_help = gengetopt_args_info_full_help[20] ;
220   args_info->phi_help = gengetopt_args_info_full_help[21] ;
221   args_info->pbounds_help = gengetopt_args_info_full_help[22] ;
222   args_info->log_help = gengetopt_args_info_full_help[24] ;
223   args_info->silent_help = gengetopt_args_info_full_help[25] ;
224   args_info->verbose_help = gengetopt_args_info_full_help[26] ;
225   args_info->lmin_help = gengetopt_args_info_full_help[27] ;
226   args_info->cut_help = gengetopt_args_info_full_help[28] ;
227   
228 }
229
230 void
231 cmdline_parser_print_version (void)
232 {
233   printf ("%s %s\n",
234      (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
235      CMDLINE_PARSER_VERSION);
236 }
237
238 static void print_help_common(void) {
239   cmdline_parser_print_version ();
240
241   if (strlen(gengetopt_args_info_purpose) > 0)
242     printf("\n%s\n", gengetopt_args_info_purpose);
243
244   if (strlen(gengetopt_args_info_usage) > 0)
245     printf("\n%s\n", gengetopt_args_info_usage);
246
247   printf("\n");
248
249   if (strlen(gengetopt_args_info_description) > 0)
250     printf("%s\n\n", gengetopt_args_info_description);
251 }
252
253 void
254 cmdline_parser_print_help (void)
255 {
256   int i = 0;
257   print_help_common();
258   while (gengetopt_args_info_help[i])
259     printf("%s\n", gengetopt_args_info_help[i++]);
260 }
261
262 void
263 cmdline_parser_print_full_help (void)
264 {
265   int i = 0;
266   print_help_common();
267   while (gengetopt_args_info_full_help[i])
268     printf("%s\n", gengetopt_args_info_full_help[i++]);
269 }
270
271 void
272 cmdline_parser_init (struct gengetopt_args_info *args_info)
273 {
274   clear_given (args_info);
275   clear_args (args_info);
276   init_args_info (args_info);
277 }
278
279 void
280 cmdline_parser_params_init(struct cmdline_parser_params *params)
281 {
282   if (params)
283     { 
284       params->override = 0;
285       params->initialize = 1;
286       params->check_required = 1;
287       params->check_ambiguity = 0;
288       params->print_errors = 1;
289     }
290 }
291
292 struct cmdline_parser_params *
293 cmdline_parser_params_create(void)
294 {
295   struct cmdline_parser_params *params = 
296     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
297   cmdline_parser_params_init(params);  
298   return params;
299 }
300
301 static void
302 free_string_field (char **s)
303 {
304   if (*s)
305     {
306       free (*s);
307       *s = 0;
308     }
309 }
310
311
312 static void
313 cmdline_parser_release (struct gengetopt_args_info *args_info)
314 {
315
316   free_string_field (&(args_info->dangle_orig));
317   free_string_field (&(args_info->Temp_orig));
318   free_string_field (&(args_info->Par_arg));
319   free_string_field (&(args_info->Par_orig));
320   free_string_field (&(args_info->seed_arg));
321   free_string_field (&(args_info->seed_orig));
322   free_string_field (&(args_info->time_orig));
323   free_string_field (&(args_info->num_orig));
324   free_string_field (&(args_info->grow_orig));
325   free_string_field (&(args_info->glen_orig));
326   free_string_field (&(args_info->phi_orig));
327   free_string_field (&(args_info->pbounds_arg));
328   free_string_field (&(args_info->pbounds_orig));
329   free_string_field (&(args_info->log_arg));
330   free_string_field (&(args_info->log_orig));
331   free_string_field (&(args_info->cut_orig));
332   
333   
334
335   clear_given (args_info);
336 }
337
338 /**
339  * @param val the value to check
340  * @param values the possible values
341  * @return the index of the matched value:
342  * -1 if no value matched,
343  * -2 if more than one value has matched
344  */
345 static int
346 check_possible_values(const char *val, const char *values[])
347 {
348   int i, found, last;
349   size_t len;
350
351   if (!val)   /* otherwise strlen() crashes below */
352     return -1; /* -1 means no argument for the option */
353
354   found = last = 0;
355
356   for (i = 0, len = strlen(val); values[i]; ++i)
357     {
358       if (strncmp(val, values[i], len) == 0)
359         {
360           ++found;
361           last = i;
362           if (strlen(values[i]) == len)
363             return i; /* exact macth no need to check more */
364         }
365     }
366
367   if (found == 1) /* one match: OK */
368     return last;
369
370   return (found ? -2 : -1); /* return many values or none matched */
371 }
372
373
374 static void
375 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
376 {
377   int found = -1;
378   if (arg) {
379     if (values) {
380       found = check_possible_values(arg, values);      
381     }
382     if (found >= 0)
383       fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
384     else
385       fprintf(outfile, "%s=\"%s\"\n", opt, arg);
386   } else {
387     fprintf(outfile, "%s\n", opt);
388   }
389 }
390
391
392 int
393 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
394 {
395   int i = 0;
396
397   if (!outfile)
398     {
399       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
400       return EXIT_FAILURE;
401     }
402
403   if (args_info->help_given)
404     write_into_file(outfile, "help", 0, 0 );
405   if (args_info->full_help_given)
406     write_into_file(outfile, "full-help", 0, 0 );
407   if (args_info->version_given)
408     write_into_file(outfile, "version", 0, 0 );
409   if (args_info->dangle_given)
410     write_into_file(outfile, "dangle", args_info->dangle_orig, cmdline_parser_dangle_values);
411   if (args_info->Temp_given)
412     write_into_file(outfile, "Temp", args_info->Temp_orig, 0);
413   if (args_info->Par_given)
414     write_into_file(outfile, "Par", args_info->Par_orig, 0);
415   if (args_info->logML_given)
416     write_into_file(outfile, "logML", 0, 0 );
417   if (args_info->noShift_given)
418     write_into_file(outfile, "noShift", 0, 0 );
419   if (args_info->noLP_given)
420     write_into_file(outfile, "noLP", 0, 0 );
421   if (args_info->seed_given)
422     write_into_file(outfile, "seed", args_info->seed_orig, 0);
423   if (args_info->time_given)
424     write_into_file(outfile, "time", args_info->time_orig, 0);
425   if (args_info->num_given)
426     write_into_file(outfile, "num", args_info->num_orig, 0);
427   if (args_info->start_given)
428     write_into_file(outfile, "start", 0, 0 );
429   if (args_info->stop_given)
430     write_into_file(outfile, "stop", 0, 0 );
431   if (args_info->met_given)
432     write_into_file(outfile, "met", 0, 0 );
433   if (args_info->fpt_given)
434     write_into_file(outfile, "fpt", 0, 0 );
435   if (args_info->grow_given)
436     write_into_file(outfile, "grow", args_info->grow_orig, 0);
437   if (args_info->glen_given)
438     write_into_file(outfile, "glen", args_info->glen_orig, 0);
439   if (args_info->phi_given)
440     write_into_file(outfile, "phi", args_info->phi_orig, 0);
441   if (args_info->pbounds_given)
442     write_into_file(outfile, "pbounds", args_info->pbounds_orig, 0);
443   if (args_info->log_given)
444     write_into_file(outfile, "log", args_info->log_orig, 0);
445   if (args_info->silent_given)
446     write_into_file(outfile, "silent", 0, 0 );
447   if (args_info->verbose_given)
448     write_into_file(outfile, "verbose", 0, 0 );
449   if (args_info->lmin_given)
450     write_into_file(outfile, "lmin", 0, 0 );
451   if (args_info->cut_given)
452     write_into_file(outfile, "cut", args_info->cut_orig, 0);
453   
454
455   i = EXIT_SUCCESS;
456   return i;
457 }
458
459 int
460 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
461 {
462   FILE *outfile;
463   int i = 0;
464
465   outfile = fopen(filename, "w");
466
467   if (!outfile)
468     {
469       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
470       return EXIT_FAILURE;
471     }
472
473   i = cmdline_parser_dump(outfile, args_info);
474   fclose (outfile);
475
476   return i;
477 }
478
479 void
480 cmdline_parser_free (struct gengetopt_args_info *args_info)
481 {
482   cmdline_parser_release (args_info);
483 }
484
485 /** @brief replacement of strdup, which is not standard */
486 char *
487 gengetopt_strdup (const char *s)
488 {
489   char *result = 0;
490   if (!s)
491     return result;
492
493   result = (char*)malloc(strlen(s) + 1);
494   if (result == (char*)0)
495     return (char*)0;
496   strcpy(result, s);
497   return result;
498 }
499
500 int
501 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
502 {
503   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
504 }
505
506 int
507 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
508                    struct cmdline_parser_params *params)
509 {
510   int result;
511   result = cmdline_parser_internal (argc, argv, args_info, params, 0);
512
513   if (result == EXIT_FAILURE)
514     {
515       cmdline_parser_free (args_info);
516       exit (EXIT_FAILURE);
517     }
518   
519   return result;
520 }
521
522 int
523 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
524 {
525   int result;
526   struct cmdline_parser_params params;
527   
528   params.override = override;
529   params.initialize = initialize;
530   params.check_required = check_required;
531   params.check_ambiguity = 0;
532   params.print_errors = 1;
533
534   result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
535
536   if (result == EXIT_FAILURE)
537     {
538       cmdline_parser_free (args_info);
539       exit (EXIT_FAILURE);
540     }
541   
542   return result;
543 }
544
545 int
546 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
547 {
548   FIX_UNUSED (args_info);
549   FIX_UNUSED (prog_name);
550   return EXIT_SUCCESS;
551 }
552
553 /*
554  * Extracted from the glibc source tree, version 2.3.6
555  *
556  * Licensed under the GPL as per the whole glibc source tree.
557  *
558  * This file was modified so that getopt_long can be called
559  * many times without risking previous memory to be spoiled.
560  *
561  * Modified by Andre Noll and Lorenzo Bettini for use in
562  * GNU gengetopt generated files.
563  *
564  */
565
566 /* 
567  * we must include anything we need since this file is not thought to be
568  * inserted in a file already using getopt.h
569  *
570  * Lorenzo
571  */
572
573 struct option
574 {
575   const char *name;
576   /* has_arg can't be an enum because some compilers complain about
577      type mismatches in all the code that assumes it is an int.  */
578   int has_arg;
579   int *flag;
580   int val;
581 };
582
583 /* This version of `getopt' appears to the caller like standard Unix `getopt'
584    but it behaves differently for the user, since it allows the user
585    to intersperse the options with the other arguments.
586
587    As `getopt' works, it permutes the elements of ARGV so that,
588    when it is done, all the options precede everything else.  Thus
589    all application programs are extended to handle flexible argument order.
590 */
591 /*
592    If the field `flag' is not NULL, it points to a variable that is set
593    to the value given in the field `val' when the option is found, but
594    left unchanged if the option is not found.
595
596    To have a long-named option do something other than set an `int' to
597    a compiled-in constant, such as set a value from `custom_optarg', set the
598    option's `flag' field to zero and its `val' field to a nonzero
599    value (the equivalent single-letter option character, if there is
600    one).  For long options that have a zero `flag' field, `getopt'
601    returns the contents of the `val' field.  */
602
603 /* Names for the values of the `has_arg' field of `struct option'.  */
604 #ifndef no_argument
605 #define no_argument             0
606 #endif
607
608 #ifndef required_argument
609 #define required_argument       1
610 #endif
611
612 #ifndef optional_argument
613 #define optional_argument       2
614 #endif
615
616 struct custom_getopt_data {
617         /*
618          * These have exactly the same meaning as the corresponding global variables,
619          * except that they are used for the reentrant versions of getopt.
620          */
621         int custom_optind;
622         int custom_opterr;
623         int custom_optopt;
624         char *custom_optarg;
625
626         /* True if the internal members have been initialized.  */
627         int initialized;
628
629         /*
630          * The next char to be scanned in the option-element in which the last option
631          * character we returned was found.  This allows us to pick up the scan where
632          * we left off.  If this is zero, or a null string, it means resume the scan by
633          * advancing to the next ARGV-element.
634          */
635         char *nextchar;
636
637         /*
638          * Describe the part of ARGV that contains non-options that have been skipped.
639          * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
640          * the index after the last of them.
641          */
642         int first_nonopt;
643         int last_nonopt;
644 };
645
646 /*
647  * the variables optarg, optind, opterr and optopt are renamed with
648  * the custom_ prefix so that they don't interfere with getopt ones.
649  *
650  * Moreover they're static so they are visible only from within the
651  * file where this very file will be included.
652  */
653
654 /*
655  * For communication from `custom_getopt' to the caller.  When `custom_getopt' finds an
656  * option that takes an argument, the argument value is returned here.
657  */
658 static char *custom_optarg;
659
660 /*
661  * Index in ARGV of the next element to be scanned.  This is used for
662  * communication to and from the caller and for communication between
663  * successive calls to `custom_getopt'.
664  *
665  * On entry to `custom_getopt', 1 means this is the first call; initialize.
666  *
667  * When `custom_getopt' returns -1, this is the index of the first of the non-option
668  * elements that the caller should itself scan.
669  *
670  * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
671  * has been scanned so far.
672  *
673  * 1003.2 says this must be 1 before any call.
674  */
675 static int custom_optind = 1;
676
677 /*
678  * Callers store zero here to inhibit the error message for unrecognized
679  * options.
680  */
681 static int custom_opterr = 1;
682
683 /*
684  * Set to an option character which was unrecognized.  This must be initialized
685  * on some systems to avoid linking in the system's own getopt implementation.
686  */
687 static int custom_optopt = '?';
688
689 /*
690  * Exchange two adjacent subsequences of ARGV.  One subsequence is elements
691  * [first_nonopt,last_nonopt) which contains all the non-options that have been
692  * skipped so far.  The other is elements [last_nonopt,custom_optind), which contains
693  * all the options processed since those non-options were skipped.
694  * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
695  * indices of the non-options in ARGV after they are moved.
696  */
697 static void exchange(char **argv, struct custom_getopt_data *d)
698 {
699         int bottom = d->first_nonopt;
700         int middle = d->last_nonopt;
701         int top = d->custom_optind;
702         char *tem;
703
704         /*
705          * Exchange the shorter segment with the far end of the longer segment.
706          * That puts the shorter segment into the right place.  It leaves the
707          * longer segment in the right place overall, but it consists of two
708          * parts that need to be swapped next.
709          */
710         while (top > middle && middle > bottom) {
711                 if (top - middle > middle - bottom) {
712                         /* Bottom segment is the short one.  */
713                         int len = middle - bottom;
714                         int i;
715
716                         /* Swap it with the top part of the top segment.  */
717                         for (i = 0; i < len; i++) {
718                                 tem = argv[bottom + i];
719                                 argv[bottom + i] =
720                                         argv[top - (middle - bottom) + i];
721                                 argv[top - (middle - bottom) + i] = tem;
722                         }
723                         /* Exclude the moved bottom segment from further swapping.  */
724                         top -= len;
725                 } else {
726                         /* Top segment is the short one.  */
727                         int len = top - middle;
728                         int i;
729
730                         /* Swap it with the bottom part of the bottom segment.  */
731                         for (i = 0; i < len; i++) {
732                                 tem = argv[bottom + i];
733                                 argv[bottom + i] = argv[middle + i];
734                                 argv[middle + i] = tem;
735                         }
736                         /* Exclude the moved top segment from further swapping.  */
737                         bottom += len;
738                 }
739         }
740         /* Update records for the slots the non-options now occupy.  */
741         d->first_nonopt += (d->custom_optind - d->last_nonopt);
742         d->last_nonopt = d->custom_optind;
743 }
744
745 /* Initialize the internal data when the first call is made.  */
746 static void custom_getopt_initialize(struct custom_getopt_data *d)
747 {
748         /*
749          * Start processing options with ARGV-element 1 (since ARGV-element 0
750          * is the program name); the sequence of previously skipped non-option
751          * ARGV-elements is empty.
752          */
753         d->first_nonopt = d->last_nonopt = d->custom_optind;
754         d->nextchar = NULL;
755         d->initialized = 1;
756 }
757
758 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
759
760 /* return: zero: continue, nonzero: return given value to user */
761 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
762         struct custom_getopt_data *d)
763 {
764         /*
765          * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
766          * moved back by the user (who may also have changed the arguments).
767          */
768         if (d->last_nonopt > d->custom_optind)
769                 d->last_nonopt = d->custom_optind;
770         if (d->first_nonopt > d->custom_optind)
771                 d->first_nonopt = d->custom_optind;
772         /*
773          * If we have just processed some options following some
774          * non-options, exchange them so that the options come first.
775          */
776         if (d->first_nonopt != d->last_nonopt &&
777                         d->last_nonopt != d->custom_optind)
778                 exchange((char **) argv, d);
779         else if (d->last_nonopt != d->custom_optind)
780                 d->first_nonopt = d->custom_optind;
781         /*
782          * Skip any additional non-options and extend the range of
783          * non-options previously skipped.
784          */
785         while (d->custom_optind < argc && NONOPTION_P)
786                 d->custom_optind++;
787         d->last_nonopt = d->custom_optind;
788         /*
789          * The special ARGV-element `--' means premature end of options.  Skip
790          * it like a null option, then exchange with previous non-options as if
791          * it were an option, then skip everything else like a non-option.
792          */
793         if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
794                 d->custom_optind++;
795                 if (d->first_nonopt != d->last_nonopt
796                                 && d->last_nonopt != d->custom_optind)
797                         exchange((char **) argv, d);
798                 else if (d->first_nonopt == d->last_nonopt)
799                         d->first_nonopt = d->custom_optind;
800                 d->last_nonopt = argc;
801                 d->custom_optind = argc;
802         }
803         /*
804          * If we have done all the ARGV-elements, stop the scan and back over
805          * any non-options that we skipped and permuted.
806          */
807         if (d->custom_optind == argc) {
808                 /*
809                  * Set the next-arg-index to point at the non-options that we
810                  * previously skipped, so the caller will digest them.
811                  */
812                 if (d->first_nonopt != d->last_nonopt)
813                         d->custom_optind = d->first_nonopt;
814                 return -1;
815         }
816         /*
817          * If we have come to a non-option and did not permute it, either stop
818          * the scan or describe it to the caller and pass it by.
819          */
820         if (NONOPTION_P) {
821                 d->custom_optarg = argv[d->custom_optind++];
822                 return 1;
823         }
824         /*
825          * We have found another option-ARGV-element. Skip the initial
826          * punctuation.
827          */
828         d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
829         return 0;
830 }
831
832 /*
833  * Check whether the ARGV-element is a long option.
834  *
835  * If there's a long option "fubar" and the ARGV-element is "-fu", consider
836  * that an abbreviation of the long option, just like "--fu", and not "-f" with
837  * arg "u".
838  *
839  * This distinction seems to be the most useful approach.
840  *
841  */
842 static int check_long_opt(int argc, char *const *argv, const char *optstring,
843                 const struct option *longopts, int *longind,
844                 int print_errors, struct custom_getopt_data *d)
845 {
846         char *nameend;
847         const struct option *p;
848         const struct option *pfound = NULL;
849         int exact = 0;
850         int ambig = 0;
851         int indfound = -1;
852         int option_index;
853
854         for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
855                 /* Do nothing.  */ ;
856
857         /* Test all long options for either exact match or abbreviated matches */
858         for (p = longopts, option_index = 0; p->name; p++, option_index++)
859                 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
860                         if ((unsigned int) (nameend - d->nextchar)
861                                         == (unsigned int) strlen(p->name)) {
862                                 /* Exact match found.  */
863                                 pfound = p;
864                                 indfound = option_index;
865                                 exact = 1;
866                                 break;
867                         } else if (pfound == NULL) {
868                                 /* First nonexact match found.  */
869                                 pfound = p;
870                                 indfound = option_index;
871                         } else if (pfound->has_arg != p->has_arg
872                                         || pfound->flag != p->flag
873                                         || pfound->val != p->val)
874                                 /* Second or later nonexact match found.  */
875                                 ambig = 1;
876                 }
877         if (ambig && !exact) {
878                 if (print_errors) {
879                         fprintf(stderr,
880                                 "%s: option `%s' is ambiguous\n",
881                                 argv[0], argv[d->custom_optind]);
882                 }
883                 d->nextchar += strlen(d->nextchar);
884                 d->custom_optind++;
885                 d->custom_optopt = 0;
886                 return '?';
887         }
888         if (pfound) {
889                 option_index = indfound;
890                 d->custom_optind++;
891                 if (*nameend) {
892                         if (pfound->has_arg != no_argument)
893                                 d->custom_optarg = nameend + 1;
894                         else {
895                                 if (print_errors) {
896                                         if (argv[d->custom_optind - 1][1] == '-') {
897                                                 /* --option */
898                                                 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
899                                                         argv[0], pfound->name);
900                                         } else {
901                                                 /* +option or -option */
902                                                 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
903                                                         argv[0], argv[d->custom_optind - 1][0], pfound->name);
904                                         }
905
906                                 }
907                                 d->nextchar += strlen(d->nextchar);
908                                 d->custom_optopt = pfound->val;
909                                 return '?';
910                         }
911                 } else if (pfound->has_arg == required_argument) {
912                         if (d->custom_optind < argc)
913                                 d->custom_optarg = argv[d->custom_optind++];
914                         else {
915                                 if (print_errors) {
916                                         fprintf(stderr,
917                                                 "%s: option `%s' requires an argument\n",
918                                                 argv[0],
919                                                 argv[d->custom_optind - 1]);
920                                 }
921                                 d->nextchar += strlen(d->nextchar);
922                                 d->custom_optopt = pfound->val;
923                                 return optstring[0] == ':' ? ':' : '?';
924                         }
925                 }
926                 d->nextchar += strlen(d->nextchar);
927                 if (longind != NULL)
928                         *longind = option_index;
929                 if (pfound->flag) {
930                         *(pfound->flag) = pfound->val;
931                         return 0;
932                 }
933                 return pfound->val;
934         }
935         /*
936          * Can't find it as a long option.  If this is not getopt_long_only, or
937          * the option starts with '--' or is not a valid short option, then
938          * it's an error.  Otherwise interpret it as a short option.
939          */
940         if (print_errors) {
941                 if (argv[d->custom_optind][1] == '-') {
942                         /* --option */
943                         fprintf(stderr,
944                                 "%s: unrecognized option `--%s'\n",
945                                 argv[0], d->nextchar);
946                 } else {
947                         /* +option or -option */
948                         fprintf(stderr,
949                                 "%s: unrecognized option `%c%s'\n",
950                                 argv[0], argv[d->custom_optind][0],
951                                 d->nextchar);
952                 }
953         }
954         d->nextchar = (char *) "";
955         d->custom_optind++;
956         d->custom_optopt = 0;
957         return '?';
958 }
959
960 static int check_short_opt(int argc, char *const *argv, const char *optstring,
961                 int print_errors, struct custom_getopt_data *d)
962 {
963         char c = *d->nextchar++;
964         const char *temp = strchr(optstring, c);
965
966         /* Increment `custom_optind' when we start to process its last character.  */
967         if (*d->nextchar == '\0')
968                 ++d->custom_optind;
969         if (!temp || c == ':') {
970                 if (print_errors)
971                         fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
972
973                 d->custom_optopt = c;
974                 return '?';
975         }
976         if (temp[1] == ':') {
977                 if (temp[2] == ':') {
978                         /* This is an option that accepts an argument optionally.  */
979                         if (*d->nextchar != '\0') {
980                                 d->custom_optarg = d->nextchar;
981                                 d->custom_optind++;
982                         } else
983                                 d->custom_optarg = NULL;
984                         d->nextchar = NULL;
985                 } else {
986                         /* This is an option that requires an argument.  */
987                         if (*d->nextchar != '\0') {
988                                 d->custom_optarg = d->nextchar;
989                                 /*
990                                  * If we end this ARGV-element by taking the
991                                  * rest as an arg, we must advance to the next
992                                  * element now.
993                                  */
994                                 d->custom_optind++;
995                         } else if (d->custom_optind == argc) {
996                                 if (print_errors) {
997                                         fprintf(stderr,
998                                                 "%s: option requires an argument -- %c\n",
999                                                 argv[0], c);
1000                                 }
1001                                 d->custom_optopt = c;
1002                                 if (optstring[0] == ':')
1003                                         c = ':';
1004                                 else
1005                                         c = '?';
1006                         } else
1007                                 /*
1008                                  * We already incremented `custom_optind' once;
1009                                  * increment it again when taking next ARGV-elt
1010                                  * as argument.
1011                                  */
1012                                 d->custom_optarg = argv[d->custom_optind++];
1013                         d->nextchar = NULL;
1014                 }
1015         }
1016         return c;
1017 }
1018
1019 /*
1020  * Scan elements of ARGV for option characters given in OPTSTRING.
1021  *
1022  * If an element of ARGV starts with '-', and is not exactly "-" or "--",
1023  * then it is an option element.  The characters of this element
1024  * (aside from the initial '-') are option characters.  If `getopt'
1025  * is called repeatedly, it returns successively each of the option characters
1026  * from each of the option elements.
1027  *
1028  * If `getopt' finds another option character, it returns that character,
1029  * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
1030  * resume the scan with the following option character or ARGV-element.
1031  *
1032  * If there are no more option characters, `getopt' returns -1.
1033  * Then `custom_optind' is the index in ARGV of the first ARGV-element
1034  * that is not an option.  (The ARGV-elements have been permuted
1035  * so that those that are not options now come last.)
1036  *
1037  * OPTSTRING is a string containing the legitimate option characters.
1038  * If an option character is seen that is not listed in OPTSTRING,
1039  * return '?' after printing an error message.  If you set `custom_opterr' to
1040  * zero, the error message is suppressed but we still return '?'.
1041  *
1042  * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
1043  * so the following text in the same ARGV-element, or the text of the following
1044  * ARGV-element, is returned in `custom_optarg'.  Two colons mean an option that
1045  * wants an optional arg; if there is text in the current ARGV-element,
1046  * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
1047  *
1048  * If OPTSTRING starts with `-' or `+', it requests different methods of
1049  * handling the non-option ARGV-elements.
1050  * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
1051  *
1052  * Long-named options begin with `--' instead of `-'.
1053  * Their names may be abbreviated as long as the abbreviation is unique
1054  * or is an exact match for some defined option.  If they have an
1055  * argument, it follows the option name in the same ARGV-element, separated
1056  * from the option name by a `=', or else the in next ARGV-element.
1057  * When `getopt' finds a long-named option, it returns 0 if that option's
1058  * `flag' field is nonzero, the value of the option's `val' field
1059  * if the `flag' field is zero.
1060  *
1061  * The elements of ARGV aren't really const, because we permute them.
1062  * But we pretend they're const in the prototype to be compatible
1063  * with other systems.
1064  *
1065  * LONGOPTS is a vector of `struct option' terminated by an
1066  * element containing a name which is zero.
1067  *
1068  * LONGIND returns the index in LONGOPT of the long-named option found.
1069  * It is only valid when a long-named option has been found by the most
1070  * recent call.
1071  *
1072  * Return the option character from OPTS just read.  Return -1 when there are
1073  * no more options.  For unrecognized options, or options missing arguments,
1074  * `custom_optopt' is set to the option letter, and '?' is returned.
1075  *
1076  * The OPTS string is a list of characters which are recognized option letters,
1077  * optionally followed by colons, specifying that that letter takes an
1078  * argument, to be placed in `custom_optarg'.
1079  *
1080  * If a letter in OPTS is followed by two colons, its argument is optional.
1081  * This behavior is specific to the GNU `getopt'.
1082  *
1083  * The argument `--' causes premature termination of argument scanning,
1084  * explicitly telling `getopt' that there are no more options.  If OPTS begins
1085  * with `--', then non-option arguments are treated as arguments to the option
1086  * '\0'.  This behavior is specific to the GNU `getopt'.
1087  */
1088
1089 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
1090                 const struct option *longopts, int *longind,
1091                 struct custom_getopt_data *d)
1092 {
1093         int ret, print_errors = d->custom_opterr;
1094
1095         if (optstring[0] == ':')
1096                 print_errors = 0;
1097         if (argc < 1)
1098                 return -1;
1099         d->custom_optarg = NULL;
1100
1101         /* 
1102          * This is a big difference with GNU getopt, since optind == 0
1103          * means initialization while here 1 means first call.
1104          */
1105         if (d->custom_optind == 0 || !d->initialized) {
1106                 if (d->custom_optind == 0)
1107                         d->custom_optind = 1;   /* Don't scan ARGV[0], the program name.  */
1108                 custom_getopt_initialize(d);
1109         }
1110         if (d->nextchar == NULL || *d->nextchar == '\0') {
1111                 ret = shuffle_argv(argc, argv, longopts, d);
1112                 if (ret)
1113                         return ret;
1114         }
1115         if (longopts && (argv[d->custom_optind][1] == '-' ))
1116                 return check_long_opt(argc, argv, optstring, longopts,
1117                         longind, print_errors, d);
1118         return check_short_opt(argc, argv, optstring, print_errors, d);
1119 }
1120
1121 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1122         const struct option *longopts, int *longind)
1123 {
1124         int result;
1125         /* Keep a global copy of all internal members of d */
1126         static struct custom_getopt_data d;
1127
1128         d.custom_optind = custom_optind;
1129         d.custom_opterr = custom_opterr;
1130         result = getopt_internal_r(argc, argv, optstring, longopts,
1131                 longind, &d);
1132         custom_optind = d.custom_optind;
1133         custom_optarg = d.custom_optarg;
1134         custom_optopt = d.custom_optopt;
1135         return result;
1136 }
1137
1138 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1139         const struct option *long_options, int *opt_index)
1140 {
1141         return custom_getopt_internal(argc, argv, options, long_options,
1142                 opt_index);
1143 }
1144
1145
1146 static char *package_name = 0;
1147
1148 /**
1149  * @brief updates an option
1150  * @param field the generic pointer to the field to update
1151  * @param orig_field the pointer to the orig field
1152  * @param field_given the pointer to the number of occurrence of this option
1153  * @param prev_given the pointer to the number of occurrence already seen
1154  * @param value the argument for this option (if null no arg was specified)
1155  * @param possible_values the possible values for this option (if specified)
1156  * @param default_value the default value (in case the option only accepts fixed values)
1157  * @param arg_type the type of this option
1158  * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
1159  * @param override @see cmdline_parser_params.override
1160  * @param no_free whether to free a possible previous value
1161  * @param multiple_option whether this is a multiple option
1162  * @param long_opt the corresponding long option
1163  * @param short_opt the corresponding short option (or '-' if none)
1164  * @param additional_error possible further error specification
1165  */
1166 static
1167 int update_arg(void *field, char **orig_field,
1168                unsigned int *field_given, unsigned int *prev_given, 
1169                char *value, const char *possible_values[],
1170                const char *default_value,
1171                cmdline_parser_arg_type arg_type,
1172                int check_ambiguity, int override,
1173                int no_free, int multiple_option,
1174                const char *long_opt, char short_opt,
1175                const char *additional_error)
1176 {
1177   char *stop_char = 0;
1178   const char *val = value;
1179   int found;
1180   char **string_field;
1181   FIX_UNUSED (field);
1182
1183   stop_char = 0;
1184   found = 0;
1185
1186   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1187     {
1188       if (short_opt != '-')
1189         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
1190                package_name, long_opt, short_opt,
1191                (additional_error ? additional_error : ""));
1192       else
1193         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
1194                package_name, long_opt,
1195                (additional_error ? additional_error : ""));
1196       return 1; /* failure */
1197     }
1198
1199   if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
1200     {
1201       if (short_opt != '-')
1202         fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n", 
1203           package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, short_opt,
1204           (additional_error ? additional_error : ""));
1205       else
1206         fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n", 
1207           package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt,
1208           (additional_error ? additional_error : ""));
1209       return 1; /* failure */
1210     }
1211     
1212   if (field_given && *field_given && ! override)
1213     return 0;
1214   if (prev_given)
1215     (*prev_given)++;
1216   if (field_given)
1217     (*field_given)++;
1218   if (possible_values)
1219     val = possible_values[found];
1220
1221   switch(arg_type) {
1222   case ARG_FLAG:
1223     *((int *)field) = !*((int *)field);
1224     break;
1225   case ARG_INT:
1226     if (val) *((int *)field) = strtol (val, &stop_char, 0);
1227     break;
1228   case ARG_FLOAT:
1229     if (val) *((float *)field) = (float)strtod (val, &stop_char);
1230     break;
1231   case ARG_DOUBLE:
1232     if (val) *((double *)field) = strtod (val, &stop_char);
1233     break;
1234   case ARG_STRING:
1235     if (val) {
1236       string_field = (char **)field;
1237       if (!no_free && *string_field)
1238         free (*string_field); /* free previous string */
1239       *string_field = gengetopt_strdup (val);
1240     }
1241     break;
1242   default:
1243     break;
1244   };
1245
1246   /* check numeric conversion */
1247   switch(arg_type) {
1248   case ARG_INT:
1249   case ARG_FLOAT:
1250   case ARG_DOUBLE:
1251     if (val && !(stop_char && *stop_char == '\0')) {
1252       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1253       return 1; /* failure */
1254     }
1255     break;
1256   default:
1257     ;
1258   };
1259
1260   /* store the original value */
1261   switch(arg_type) {
1262   case ARG_NO:
1263   case ARG_FLAG:
1264     break;
1265   default:
1266     if (value && orig_field) {
1267       if (no_free) {
1268         *orig_field = value;
1269       } else {
1270         if (*orig_field)
1271           free (*orig_field); /* free previous string */
1272         *orig_field = gengetopt_strdup (value);
1273       }
1274     }
1275   };
1276
1277   return 0; /* OK */
1278 }
1279
1280
1281 int
1282 cmdline_parser_internal (
1283   int argc, char **argv, struct gengetopt_args_info *args_info,
1284                         struct cmdline_parser_params *params, const char *additional_error)
1285 {
1286   int c;        /* Character of the parsed option.  */
1287
1288   int error = 0;
1289   struct gengetopt_args_info local_args_info;
1290   
1291   int override;
1292   int initialize;
1293   int check_required;
1294   int check_ambiguity;
1295
1296   char *optarg;
1297   int optind;
1298   int opterr;
1299   int optopt;
1300   
1301   package_name = argv[0];
1302   
1303   override = params->override;
1304   initialize = params->initialize;
1305   check_required = params->check_required;
1306   check_ambiguity = params->check_ambiguity;
1307
1308   if (initialize)
1309     cmdline_parser_init (args_info);
1310
1311   cmdline_parser_init (&local_args_info);
1312
1313   optarg = 0;
1314   optind = 0;
1315   opterr = params->print_errors;
1316   optopt = '?';
1317
1318   while (1)
1319     {
1320       int option_index = 0;
1321
1322       static struct option long_options[] = {
1323         { "help",       0, NULL, 'h' },
1324         { "full-help",  0, NULL, 0 },
1325         { "version",    0, NULL, 'V' },
1326         { "dangle",     1, NULL, 'd' },
1327         { "Temp",       1, NULL, 'T' },
1328         { "Par",        1, NULL, 'P' },
1329         { "logML",      0, NULL, 0 },
1330         { "noShift",    0, NULL, 0 },
1331         { "noLP",       0, NULL, 0 },
1332         { "seed",       1, NULL, 0 },
1333         { "time",       1, NULL, 0 },
1334         { "num",        1, NULL, 0 },
1335         { "start",      0, NULL, 0 },
1336         { "stop",       0, NULL, 0 },
1337         { "met",        0, NULL, 0 },
1338         { "fpt",        0, NULL, 0 },
1339         { "grow",       1, NULL, 0 },
1340         { "glen",       1, NULL, 0 },
1341         { "phi",        1, NULL, 0 },
1342         { "pbounds",    1, NULL, 0 },
1343         { "log",        1, NULL, 0 },
1344         { "silent",     0, NULL, 'q' },
1345         { "verbose",    0, NULL, 'v' },
1346         { "lmin",       0, NULL, 0 },
1347         { "cut",        1, NULL, 0 },
1348         { 0,  0, 0, 0 }
1349       };
1350
1351       custom_optarg = optarg;
1352       custom_optind = optind;
1353       custom_opterr = opterr;
1354       custom_optopt = optopt;
1355
1356       c = custom_getopt_long (argc, argv, "hVd:T:P:qv", long_options, &option_index);
1357
1358       optarg = custom_optarg;
1359       optind = custom_optind;
1360       opterr = custom_opterr;
1361       optopt = custom_optopt;
1362
1363       if (c == -1) break;       /* Exit from `while (1)' loop.  */
1364
1365       switch (c)
1366         {
1367         case 'h':       /* Print help and exit.  */
1368           cmdline_parser_print_help ();
1369           cmdline_parser_free (&local_args_info);
1370           exit (EXIT_SUCCESS);
1371
1372         case 'V':       /* Print version and exit.  */
1373           cmdline_parser_print_version ();
1374           cmdline_parser_free (&local_args_info);
1375           exit (EXIT_SUCCESS);
1376
1377         case 'd':       /* <0|1|2> set dangling end model to (none|normal|double).  */
1378         
1379         
1380           if (update_arg( (void *)&(args_info->dangle_arg), 
1381                &(args_info->dangle_orig), &(args_info->dangle_given),
1382               &(local_args_info.dangle_given), optarg, cmdline_parser_dangle_values, "2", ARG_INT,
1383               check_ambiguity, override, 0, 0,
1384               "dangle", 'd',
1385               additional_error))
1386             goto failure;
1387         
1388           break;
1389         case 'T':       /* simulation temperature.  */
1390         
1391         
1392           if (update_arg( (void *)&(args_info->Temp_arg), 
1393                &(args_info->Temp_orig), &(args_info->Temp_given),
1394               &(local_args_info.Temp_given), optarg, 0, "37", ARG_FLOAT,
1395               check_ambiguity, override, 0, 0,
1396               "Temp", 'T',
1397               additional_error))
1398             goto failure;
1399         
1400           break;
1401         case 'P':       /* read energy-parameter-file.  */
1402         
1403         
1404           if (update_arg( (void *)&(args_info->Par_arg), 
1405                &(args_info->Par_orig), &(args_info->Par_given),
1406               &(local_args_info.Par_given), optarg, 0, 0, ARG_STRING,
1407               check_ambiguity, override, 0, 0,
1408               "Par", 'P',
1409               additional_error))
1410             goto failure;
1411         
1412           break;
1413         case 'q':       /* no output to stdout.  */
1414         
1415         
1416           if (update_arg((void *)&(args_info->silent_flag), 0, &(args_info->silent_given),
1417               &(local_args_info.silent_given), optarg, 0, 0, ARG_FLAG,
1418               check_ambiguity, override, 1, 0, "silent", 'q',
1419               additional_error))
1420             goto failure;
1421         
1422           break;
1423         case 'v':       /* more information to stdout.  */
1424         
1425         
1426           if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given),
1427               &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG,
1428               check_ambiguity, override, 1, 0, "verbose", 'v',
1429               additional_error))
1430             goto failure;
1431         
1432           break;
1433
1434         case 0: /* Long option with no short option */
1435           if (strcmp (long_options[option_index].name, "full-help") == 0) {
1436             cmdline_parser_print_full_help ();
1437             cmdline_parser_free (&local_args_info);
1438             exit (EXIT_SUCCESS);
1439           }
1440
1441           /* use logarithmic multiloop energies instead of linear.  */
1442           if (strcmp (long_options[option_index].name, "logML") == 0)
1443           {
1444           
1445           
1446             if (update_arg((void *)&(args_info->logML_flag), 0, &(args_info->logML_given),
1447                 &(local_args_info.logML_given), optarg, 0, 0, ARG_FLAG,
1448                 check_ambiguity, override, 1, 0, "logML", '-',
1449                 additional_error))
1450               goto failure;
1451           
1452           }
1453           /* turn off shift-moves.  */
1454           else if (strcmp (long_options[option_index].name, "noShift") == 0)
1455           {
1456           
1457           
1458             if (update_arg((void *)&(args_info->noShift_flag), 0, &(args_info->noShift_given),
1459                 &(local_args_info.noShift_given), optarg, 0, 0, ARG_FLAG,
1460                 check_ambiguity, override, 1, 0, "noShift", '-',
1461                 additional_error))
1462               goto failure;
1463           
1464           }
1465           /* forbid structures with isolated base-pairs.  */
1466           else if (strcmp (long_options[option_index].name, "noLP") == 0)
1467           {
1468           
1469           
1470             if (update_arg((void *)&(args_info->noLP_flag), 0, &(args_info->noLP_given),
1471                 &(local_args_info.noLP_given), optarg, 0, 0, ARG_FLAG,
1472                 check_ambiguity, override, 1, 0, "noLP", '-',
1473                 additional_error))
1474               goto failure;
1475           
1476           }
1477           /* set random number seed specify 3 integers as int=int=int.  */
1478           else if (strcmp (long_options[option_index].name, "seed") == 0)
1479           {
1480           
1481           
1482             if (update_arg( (void *)&(args_info->seed_arg), 
1483                  &(args_info->seed_orig), &(args_info->seed_given),
1484                 &(local_args_info.seed_given), optarg, 0, "clock", ARG_STRING,
1485                 check_ambiguity, override, 0, 0,
1486                 "seed", '-',
1487                 additional_error))
1488               goto failure;
1489           
1490           }
1491           /* set maxtime of simulation.  */
1492           else if (strcmp (long_options[option_index].name, "time") == 0)
1493           {
1494           
1495           
1496             if (update_arg( (void *)&(args_info->time_arg), 
1497                  &(args_info->time_orig), &(args_info->time_given),
1498                 &(local_args_info.time_given), optarg, 0, "500", ARG_FLOAT,
1499                 check_ambiguity, override, 0, 0,
1500                 "time", '-',
1501                 additional_error))
1502               goto failure;
1503           
1504           }
1505           /* set number of trajectories.  */
1506           else if (strcmp (long_options[option_index].name, "num") == 0)
1507           {
1508           
1509           
1510             if (update_arg( (void *)&(args_info->num_arg), 
1511                  &(args_info->num_orig), &(args_info->num_given),
1512                 &(local_args_info.num_given), optarg, 0, "1", ARG_INT,
1513                 check_ambiguity, override, 0, 0,
1514                 "num", '-',
1515                 additional_error))
1516               goto failure;
1517           
1518           }
1519           /* read start structure from stdin (otherwise use open chain).  */
1520           else if (strcmp (long_options[option_index].name, "start") == 0)
1521           {
1522           
1523           
1524             if (update_arg((void *)&(args_info->start_flag), 0, &(args_info->start_given),
1525                 &(local_args_info.start_given), optarg, 0, 0, ARG_FLAG,
1526                 check_ambiguity, override, 1, 0, "start", '-',
1527                 additional_error))
1528               goto failure;
1529           
1530           }
1531           /* read stop structure(s) from stdin (optherwise use MFE).  */
1532           else if (strcmp (long_options[option_index].name, "stop") == 0)
1533           {
1534           
1535           
1536             if (update_arg((void *)&(args_info->stop_flag), 0, &(args_info->stop_given),
1537                 &(local_args_info.stop_given), optarg, 0, 0, ARG_FLAG,
1538                 check_ambiguity, override, 1, 0, "stop", '-',
1539                 additional_error))
1540               goto failure;
1541           
1542           }
1543           /* use Metropolis rule for rates (not Kawasaki rule).  */
1544           else if (strcmp (long_options[option_index].name, "met") == 0)
1545           {
1546           
1547           
1548             if (update_arg((void *)&(args_info->met_flag), 0, &(args_info->met_given),
1549                 &(local_args_info.met_given), optarg, 0, 0, ARG_FLAG,
1550                 check_ambiguity, override, 1, 0, "met", '-',
1551                 additional_error))
1552               goto failure;
1553           
1554           }
1555           /* compute first passage time (stop when a stop-structure is reached).  */
1556           else if (strcmp (long_options[option_index].name, "fpt") == 0)
1557           {
1558           
1559           
1560             if (update_arg((void *)&(args_info->fpt_flag), 0, &(args_info->fpt_given),
1561                 &(local_args_info.fpt_given), optarg, 0, 0, ARG_FLAG,
1562                 check_ambiguity, override, 1, 0, "fpt", '-',
1563                 additional_error))
1564               goto failure;
1565           
1566           }
1567           /* grow chain every <float> time units.  */
1568           else if (strcmp (long_options[option_index].name, "grow") == 0)
1569           {
1570           
1571           
1572             if (update_arg( (void *)&(args_info->grow_arg), 
1573                  &(args_info->grow_orig), &(args_info->grow_given),
1574                 &(local_args_info.grow_given), optarg, 0, "0", ARG_FLOAT,
1575                 check_ambiguity, override, 0, 0,
1576                 "grow", '-',
1577                 additional_error))
1578               goto failure;
1579           
1580           }
1581           /* initial size of growing chain.  */
1582           else if (strcmp (long_options[option_index].name, "glen") == 0)
1583           {
1584           
1585           
1586             if (update_arg( (void *)&(args_info->glen_arg), 
1587                  &(args_info->glen_orig), &(args_info->glen_given),
1588                 &(local_args_info.glen_given), optarg, 0, "15", ARG_INT,
1589                 check_ambiguity, override, 0, 0,
1590                 "glen", '-',
1591                 additional_error))
1592               goto failure;
1593           
1594           }
1595           /* set phi value.  */
1596           else if (strcmp (long_options[option_index].name, "phi") == 0)
1597           {
1598           
1599           
1600             if (update_arg( (void *)&(args_info->phi_arg), 
1601                  &(args_info->phi_orig), &(args_info->phi_given),
1602                 &(local_args_info.phi_given), optarg, 0, 0, ARG_DOUBLE,
1603                 check_ambiguity, override, 0, 0,
1604                 "phi", '-',
1605                 additional_error))
1606               goto failure;
1607           
1608           }
1609           /* specify 3 floats for phi_min, phi_inc, phi_max in the form <d1=d2=d3>.  */
1610           else if (strcmp (long_options[option_index].name, "pbounds") == 0)
1611           {
1612           
1613           
1614             if (update_arg( (void *)&(args_info->pbounds_arg), 
1615                  &(args_info->pbounds_orig), &(args_info->pbounds_given),
1616                 &(local_args_info.pbounds_given), optarg, 0, 0, ARG_STRING,
1617                 check_ambiguity, override, 0, 0,
1618                 "pbounds", '-',
1619                 additional_error))
1620               goto failure;
1621           
1622           }
1623           /* set basename of log-file.  */
1624           else if (strcmp (long_options[option_index].name, "log") == 0)
1625           {
1626           
1627           
1628             if (update_arg( (void *)&(args_info->log_arg), 
1629                  &(args_info->log_orig), &(args_info->log_given),
1630                 &(local_args_info.log_given), optarg, 0, "kinout", ARG_STRING,
1631                 check_ambiguity, override, 0, 0,
1632                 "log", '-',
1633                 additional_error))
1634               goto failure;
1635           
1636           }
1637           /* output only local minima to stdout.  */
1638           else if (strcmp (long_options[option_index].name, "lmin") == 0)
1639           {
1640           
1641           
1642             if (update_arg((void *)&(args_info->lmin_flag), 0, &(args_info->lmin_given),
1643                 &(local_args_info.lmin_given), optarg, 0, 0, ARG_FLAG,
1644                 check_ambiguity, override, 1, 0, "lmin", '-',
1645                 additional_error))
1646               goto failure;
1647           
1648           }
1649           /* output structures with E <= <float> to stdout.  */
1650           else if (strcmp (long_options[option_index].name, "cut") == 0)
1651           {
1652           
1653           
1654             if (update_arg( (void *)&(args_info->cut_arg), 
1655                  &(args_info->cut_orig), &(args_info->cut_given),
1656                 &(local_args_info.cut_given), optarg, 0, "20", ARG_FLOAT,
1657                 check_ambiguity, override, 0, 0,
1658                 "cut", '-',
1659                 additional_error))
1660               goto failure;
1661           
1662           }
1663           
1664           break;
1665         case '?':       /* Invalid option.  */
1666           /* `getopt_long' already printed an error message.  */
1667           goto failure;
1668
1669         default:        /* bug: option not considered.  */
1670           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1671           abort ();
1672         } /* switch */
1673     } /* while */
1674
1675
1676
1677
1678   cmdline_parser_release (&local_args_info);
1679
1680   if ( error )
1681     return (EXIT_FAILURE);
1682
1683   return 0;
1684
1685 failure:
1686   
1687   cmdline_parser_release (&local_args_info);
1688   return (EXIT_FAILURE);
1689 }