JPRED-2 Add sources of all binaries (except alscript) to Git
[jpred.git] / sources / jnet / cmdline / cmdline.c
1 /*
2   File autogenerated by gengetopt version 2.22.6
3   generated with the following command:
4   gengetopt 
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 #include <getopt.h>
25
26 #include "cmdline.h"
27
28 const char *gengetopt_args_info_purpose = "A consensus neural network protein secondary structure prediction method\n- Trained with SCOP 1.75 data and UniRef90 release 15.4";
29
30 const char *gengetopt_args_info_usage = "Usage: Jnet [OPTIONS]...";
31
32 const char *gengetopt_args_info_versiontext = "";
33
34 const char *gengetopt_args_info_description = "";
35
36 const char *gengetopt_args_info_help[] = {
37   "  -h, --help            Print help and exit",
38   "  -V, --version         Print version and exit",
39   "  -H, --hmmer=filename  HMMER profile",
40   "  -p, --pssm=filename   PSIBLAST PSSM",
41   "  -T, --test            Flag for test style output  (default=off)",
42   "\n Group: output\n  Output options",
43   "      --concise         concise output (default)",
44   "      --human[=INT]     human readable output. Optionally, can take a width\n                          argument [default: 60]",
45     0
46 };
47
48 typedef enum {ARG_NO
49   , ARG_FLAG
50   , ARG_STRING
51   , ARG_INT
52 } cmdline_parser_arg_type;
53
54 static
55 void clear_given (struct gengetopt_args_info *args_info);
56 static
57 void clear_args (struct gengetopt_args_info *args_info);
58
59 static int
60 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
61                         struct cmdline_parser_params *params, const char *additional_error);
62
63 static int
64 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
65
66 static char *
67 gengetopt_strdup (const char *s);
68
69 static
70 void clear_given (struct gengetopt_args_info *args_info)
71 {
72   args_info->help_given = 0 ;
73   args_info->version_given = 0 ;
74   args_info->hmmer_given = 0 ;
75   args_info->pssm_given = 0 ;
76   args_info->test_given = 0 ;
77   args_info->concise_given = 0 ;
78   args_info->human_given = 0 ;
79   args_info->output_group_counter = 0 ;
80 }
81
82 static
83 void clear_args (struct gengetopt_args_info *args_info)
84 {
85   FIX_UNUSED (args_info);
86   args_info->hmmer_arg = NULL;
87   args_info->hmmer_orig = NULL;
88   args_info->pssm_arg = NULL;
89   args_info->pssm_orig = NULL;
90   args_info->test_flag = 0;
91   args_info->human_orig = NULL;
92   
93 }
94
95 static
96 void init_args_info(struct gengetopt_args_info *args_info)
97 {
98
99
100   args_info->help_help = gengetopt_args_info_help[0] ;
101   args_info->version_help = gengetopt_args_info_help[1] ;
102   args_info->hmmer_help = gengetopt_args_info_help[2] ;
103   args_info->pssm_help = gengetopt_args_info_help[3] ;
104   args_info->test_help = gengetopt_args_info_help[4] ;
105   args_info->concise_help = gengetopt_args_info_help[6] ;
106   args_info->human_help = gengetopt_args_info_help[7] ;
107   
108 }
109
110 void
111 cmdline_parser_print_version (void)
112 {
113   printf ("%s %s\n",
114      (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
115      CMDLINE_PARSER_VERSION);
116
117   if (strlen(gengetopt_args_info_versiontext) > 0)
118     printf("\n%s\n", gengetopt_args_info_versiontext);
119 }
120
121 static void print_help_common(void) {
122   cmdline_parser_print_version ();
123
124   if (strlen(gengetopt_args_info_purpose) > 0)
125     printf("\n%s\n", gengetopt_args_info_purpose);
126
127   if (strlen(gengetopt_args_info_usage) > 0)
128     printf("\n%s\n", gengetopt_args_info_usage);
129
130   printf("\n");
131
132   if (strlen(gengetopt_args_info_description) > 0)
133     printf("%s\n\n", gengetopt_args_info_description);
134 }
135
136 void
137 cmdline_parser_print_help (void)
138 {
139   int i = 0;
140   print_help_common();
141   while (gengetopt_args_info_help[i])
142     printf("%s\n", gengetopt_args_info_help[i++]);
143 }
144
145 void
146 cmdline_parser_init (struct gengetopt_args_info *args_info)
147 {
148   clear_given (args_info);
149   clear_args (args_info);
150   init_args_info (args_info);
151 }
152
153 void
154 cmdline_parser_params_init(struct cmdline_parser_params *params)
155 {
156   if (params)
157     { 
158       params->override = 0;
159       params->initialize = 1;
160       params->check_required = 1;
161       params->check_ambiguity = 0;
162       params->print_errors = 1;
163     }
164 }
165
166 struct cmdline_parser_params *
167 cmdline_parser_params_create(void)
168 {
169   struct cmdline_parser_params *params = 
170     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
171   cmdline_parser_params_init(params);  
172   return params;
173 }
174
175 static void
176 free_string_field (char **s)
177 {
178   if (*s)
179     {
180       free (*s);
181       *s = 0;
182     }
183 }
184
185
186 static void
187 cmdline_parser_release (struct gengetopt_args_info *args_info)
188 {
189
190   free_string_field (&(args_info->hmmer_arg));
191   free_string_field (&(args_info->hmmer_orig));
192   free_string_field (&(args_info->pssm_arg));
193   free_string_field (&(args_info->pssm_orig));
194   free_string_field (&(args_info->human_orig));
195   
196   
197
198   clear_given (args_info);
199 }
200
201
202 static void
203 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
204 {
205   FIX_UNUSED (values);
206   if (arg) {
207     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
208   } else {
209     fprintf(outfile, "%s\n", opt);
210   }
211 }
212
213
214 int
215 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
216 {
217   int i = 0;
218
219   if (!outfile)
220     {
221       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
222       return EXIT_FAILURE;
223     }
224
225   if (args_info->help_given)
226     write_into_file(outfile, "help", 0, 0 );
227   if (args_info->version_given)
228     write_into_file(outfile, "version", 0, 0 );
229   if (args_info->hmmer_given)
230     write_into_file(outfile, "hmmer", args_info->hmmer_orig, 0);
231   if (args_info->pssm_given)
232     write_into_file(outfile, "pssm", args_info->pssm_orig, 0);
233   if (args_info->test_given)
234     write_into_file(outfile, "test", 0, 0 );
235   if (args_info->concise_given)
236     write_into_file(outfile, "concise", 0, 0 );
237   if (args_info->human_given)
238     write_into_file(outfile, "human", args_info->human_orig, 0);
239   
240
241   i = EXIT_SUCCESS;
242   return i;
243 }
244
245 int
246 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
247 {
248   FILE *outfile;
249   int i = 0;
250
251   outfile = fopen(filename, "w");
252
253   if (!outfile)
254     {
255       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
256       return EXIT_FAILURE;
257     }
258
259   i = cmdline_parser_dump(outfile, args_info);
260   fclose (outfile);
261
262   return i;
263 }
264
265 void
266 cmdline_parser_free (struct gengetopt_args_info *args_info)
267 {
268   cmdline_parser_release (args_info);
269 }
270
271 /** @brief replacement of strdup, which is not standard */
272 char *
273 gengetopt_strdup (const char *s)
274 {
275   char *result = 0;
276   if (!s)
277     return result;
278
279   result = (char*)malloc(strlen(s) + 1);
280   if (result == (char*)0)
281     return (char*)0;
282   strcpy(result, s);
283   return result;
284 }
285
286 static void
287 reset_group_output(struct gengetopt_args_info *args_info)
288 {
289   if (! args_info->output_group_counter)
290     return;
291   
292   args_info->concise_given = 0 ;
293   args_info->human_given = 0 ;
294   free_string_field (&(args_info->human_orig));
295
296   args_info->output_group_counter = 0;
297 }
298
299 int
300 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
301 {
302   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
303 }
304
305 int
306 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
307                    struct cmdline_parser_params *params)
308 {
309   int result;
310   result = cmdline_parser_internal (argc, argv, args_info, params, 0);
311
312   if (result == EXIT_FAILURE)
313     {
314       cmdline_parser_free (args_info);
315       exit (EXIT_FAILURE);
316     }
317   
318   return result;
319 }
320
321 int
322 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
323 {
324   int result;
325   struct cmdline_parser_params params;
326   
327   params.override = override;
328   params.initialize = initialize;
329   params.check_required = check_required;
330   params.check_ambiguity = 0;
331   params.print_errors = 1;
332
333   result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
334
335   if (result == EXIT_FAILURE)
336     {
337       cmdline_parser_free (args_info);
338       exit (EXIT_FAILURE);
339     }
340   
341   return result;
342 }
343
344 int
345 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
346 {
347   int result = EXIT_SUCCESS;
348
349   if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
350     result = EXIT_FAILURE;
351
352   if (result == EXIT_FAILURE)
353     {
354       cmdline_parser_free (args_info);
355       exit (EXIT_FAILURE);
356     }
357   
358   return result;
359 }
360
361 int
362 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
363 {
364   int error_occurred = 0;
365   FIX_UNUSED (additional_error);
366
367   /* checks for required options */
368   if (! args_info->hmmer_given)
369     {
370       fprintf (stderr, "%s: '--hmmer' ('-H') option required%s\n", prog_name, (additional_error ? additional_error : ""));
371       error_occurred = 1;
372     }
373   
374   
375   /* checks for dependences among options */
376
377   return error_occurred;
378 }
379
380
381 static char *package_name = 0;
382
383 /**
384  * @brief updates an option
385  * @param field the generic pointer to the field to update
386  * @param orig_field the pointer to the orig field
387  * @param field_given the pointer to the number of occurrence of this option
388  * @param prev_given the pointer to the number of occurrence already seen
389  * @param value the argument for this option (if null no arg was specified)
390  * @param possible_values the possible values for this option (if specified)
391  * @param default_value the default value (in case the option only accepts fixed values)
392  * @param arg_type the type of this option
393  * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
394  * @param override @see cmdline_parser_params.override
395  * @param no_free whether to free a possible previous value
396  * @param multiple_option whether this is a multiple option
397  * @param long_opt the corresponding long option
398  * @param short_opt the corresponding short option (or '-' if none)
399  * @param additional_error possible further error specification
400  */
401 static
402 int update_arg(void *field, char **orig_field,
403                unsigned int *field_given, unsigned int *prev_given, 
404                char *value, const char *possible_values[],
405                const char *default_value,
406                cmdline_parser_arg_type arg_type,
407                int check_ambiguity, int override,
408                int no_free, int multiple_option,
409                const char *long_opt, char short_opt,
410                const char *additional_error)
411 {
412   char *stop_char = 0;
413   const char *val = value;
414   int found;
415   char **string_field;
416   FIX_UNUSED (field);
417
418   stop_char = 0;
419   found = 0;
420
421   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
422     {
423       if (short_opt != '-')
424         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
425                package_name, long_opt, short_opt,
426                (additional_error ? additional_error : ""));
427       else
428         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
429                package_name, long_opt,
430                (additional_error ? additional_error : ""));
431       return 1; /* failure */
432     }
433
434   FIX_UNUSED (default_value);
435     
436   if (field_given && *field_given && ! override)
437     return 0;
438   if (prev_given)
439     (*prev_given)++;
440   if (field_given)
441     (*field_given)++;
442   if (possible_values)
443     val = possible_values[found];
444
445   switch(arg_type) {
446   case ARG_FLAG:
447     *((int *)field) = !*((int *)field);
448     break;
449   case ARG_INT:
450     if (val) *((int *)field) = strtol (val, &stop_char, 0);
451     break;
452   case ARG_STRING:
453     if (val) {
454       string_field = (char **)field;
455       if (!no_free && *string_field)
456         free (*string_field); /* free previous string */
457       *string_field = gengetopt_strdup (val);
458     }
459     break;
460   default:
461     break;
462   };
463
464   /* check numeric conversion */
465   switch(arg_type) {
466   case ARG_INT:
467     if (val && !(stop_char && *stop_char == '\0')) {
468       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
469       return 1; /* failure */
470     }
471     break;
472   default:
473     ;
474   };
475
476   /* store the original value */
477   switch(arg_type) {
478   case ARG_NO:
479   case ARG_FLAG:
480     break;
481   default:
482     if (value && orig_field) {
483       if (no_free) {
484         *orig_field = value;
485       } else {
486         if (*orig_field)
487           free (*orig_field); /* free previous string */
488         *orig_field = gengetopt_strdup (value);
489       }
490     }
491   };
492
493   return 0; /* OK */
494 }
495
496
497 int
498 cmdline_parser_internal (
499   int argc, char **argv, struct gengetopt_args_info *args_info,
500                         struct cmdline_parser_params *params, const char *additional_error)
501 {
502   int c;        /* Character of the parsed option.  */
503
504   int error_occurred = 0;
505   struct gengetopt_args_info local_args_info;
506   
507   int override;
508   int initialize;
509   int check_required;
510   int check_ambiguity;
511   
512   package_name = argv[0];
513   
514   override = params->override;
515   initialize = params->initialize;
516   check_required = params->check_required;
517   check_ambiguity = params->check_ambiguity;
518
519   if (initialize)
520     cmdline_parser_init (args_info);
521
522   cmdline_parser_init (&local_args_info);
523
524   optarg = 0;
525   optind = 0;
526   opterr = params->print_errors;
527   optopt = '?';
528
529   while (1)
530     {
531       int option_index = 0;
532
533       static struct option long_options[] = {
534         { "help",       0, NULL, 'h' },
535         { "version",    0, NULL, 'V' },
536         { "hmmer",      1, NULL, 'H' },
537         { "pssm",       1, NULL, 'p' },
538         { "test",       0, NULL, 'T' },
539         { "concise",    0, NULL, 0 },
540         { "human",      2, NULL, 0 },
541         { 0,  0, 0, 0 }
542       };
543
544       c = getopt_long (argc, argv, "hVH:p:T", long_options, &option_index);
545
546       if (c == -1) break;       /* Exit from `while (1)' loop.  */
547
548       switch (c)
549         {
550         case 'h':       /* Print help and exit.  */
551           cmdline_parser_print_help ();
552           cmdline_parser_free (&local_args_info);
553           exit (EXIT_SUCCESS);
554
555         case 'V':       /* Print version and exit.  */
556           cmdline_parser_print_version ();
557           cmdline_parser_free (&local_args_info);
558           exit (EXIT_SUCCESS);
559
560         case 'H':       /* HMMER profile.  */
561         
562         
563           if (update_arg( (void *)&(args_info->hmmer_arg), 
564                &(args_info->hmmer_orig), &(args_info->hmmer_given),
565               &(local_args_info.hmmer_given), optarg, 0, 0, ARG_STRING,
566               check_ambiguity, override, 0, 0,
567               "hmmer", 'H',
568               additional_error))
569             goto failure;
570         
571           break;
572         case 'p':       /* PSIBLAST PSSM.  */
573         
574         
575           if (update_arg( (void *)&(args_info->pssm_arg), 
576                &(args_info->pssm_orig), &(args_info->pssm_given),
577               &(local_args_info.pssm_given), optarg, 0, 0, ARG_STRING,
578               check_ambiguity, override, 0, 0,
579               "pssm", 'p',
580               additional_error))
581             goto failure;
582         
583           break;
584         case 'T':       /* Flag for test style output.  */
585         
586         
587           if (update_arg((void *)&(args_info->test_flag), 0, &(args_info->test_given),
588               &(local_args_info.test_given), optarg, 0, 0, ARG_FLAG,
589               check_ambiguity, override, 1, 0, "test", 'T',
590               additional_error))
591             goto failure;
592         
593           break;
594
595         case 0: /* Long option with no short option */
596           /* concise output (default).  */
597           if (strcmp (long_options[option_index].name, "concise") == 0)
598           {
599           
600             if (args_info->output_group_counter && override)
601               reset_group_output (args_info);
602             args_info->output_group_counter += 1;
603           
604             if (update_arg( 0 , 
605                  0 , &(args_info->concise_given),
606                 &(local_args_info.concise_given), optarg, 0, 0, ARG_NO,
607                 check_ambiguity, override, 0, 0,
608                 "concise", '-',
609                 additional_error))
610               goto failure;
611           
612           }
613           /* human readable output. Optionally, can take a width argument [default: 60].  */
614           else if (strcmp (long_options[option_index].name, "human") == 0)
615           {
616           
617             if (args_info->output_group_counter && override)
618               reset_group_output (args_info);
619             args_info->output_group_counter += 1;
620           
621             if (update_arg( (void *)&(args_info->human_arg), 
622                  &(args_info->human_orig), &(args_info->human_given),
623                 &(local_args_info.human_given), optarg, 0, 0, ARG_INT,
624                 check_ambiguity, override, 0, 0,
625                 "human", '-',
626                 additional_error))
627               goto failure;
628           
629           }
630           
631           break;
632         case '?':       /* Invalid option.  */
633           /* `getopt_long' already printed an error message.  */
634           goto failure;
635
636         default:        /* bug: option not considered.  */
637           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
638           abort ();
639         } /* switch */
640     } /* while */
641
642   if (args_info->output_group_counter > 1)
643     {
644       fprintf (stderr, "%s: %d options of group output were given. At most one is required%s.\n", argv[0], args_info->output_group_counter, (additional_error ? additional_error : ""));
645       error_occurred = 1;
646     }
647   
648
649
650   if (check_required)
651     {
652       error_occurred += cmdline_parser_required2 (args_info, argv[0], additional_error);
653     }
654
655   cmdline_parser_release (&local_args_info);
656
657   if ( error_occurred )
658     return (EXIT_FAILURE);
659
660   return 0;
661
662 failure:
663   
664   cmdline_parser_release (&local_args_info);
665   return (EXIT_FAILURE);
666 }