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