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