1 /*------------- Telecommunications & Signal Processing Lab -------------
2                            McGill University
3 
4 Routine:
5   int AOdecHelp (const char Version[], const char Usage[])
6 
7 Purpose:
8   Decode help and version options for audio utilities
9 
10 Description:
11   This routine decodes a help or version option.  The routine AOinitOpt must be
12   called first to initialize the option arguments.  If this routine fails to
13   find an input audio file option, the argument pointer is reset to allow
14   another routine to try to decode the option.  For the help or version
15   options, a message is printed and execution is halted.
16 
17 Parameters:
18   <-  int AOdecHelp
19          -1 - End of arguments/options
20           0 - Option not found
21           1 - Option found
22    -> const char Version[]
23       Program verion identification
24    -> const char Usage[]
25       Usage message for the program.  This is a format that normally contains
26       a "%s" for substitution of the program name.
27 
28 Author / revision:
29   P. Kabal  Copyright (C) 2003
30   $Revision: 1.5 $  $Date: 2003/05/09 12:32:37 $
31 
32 ----------------------------------------------------------------------*/
33 
34 #include <stdlib.h>   /* EXIT_SUCCESS */
35 
36 #include <libtsp.h>
37 #include <AObase.h>
38 
39 #define ROUTINE		"AOdecHelp"
40 #define PGM		((UTgetProg ())[0] == '\0' ? ROUTINE : UTgetProg ())
41 #define ERRSTOP(text,par)	UThalt ("%s: %s: \"%s\"", PGM, text, par)
42 
43 #define RET_ERROR	-2
44 #define RET_END		-1
45 
46 static const char *OThelp[] = {
47   "-h",  "--h*elp",
48   "-v",  "--v*ersion",
49   "--",
50   "**",
51   NULL
52 };
53 
54 
55 int
AOdecHelp(const char Version[],const char Usage[])56 AOdecHelp (const char Version[], const char Usage[])
57 
58 {
59   const char *OptArg;
60   int n, Sindex;
61   struct AO_CmdArg *Carg;
62 
63   /* Get the argument pointers */
64   Carg = AOArgs ();
65 
66   if (Carg->EndOptions)
67     return 0;
68 
69   /* Decode help options */
70   Sindex = Carg->Index;
71   n = UTgetOption (&Carg->Index, Carg->Argc, Carg->Argv, OThelp, &OptArg);
72 
73   switch (n) {
74   case RET_END:
75     break;
76   case 1:
77   case 2:
78     /* Help */
79     UTwarn (Usage, PGM);
80     exit (EXIT_SUCCESS);
81     break;
82   case 3:
83   case 4:
84     /* Version */
85     printf ("%s: %s\n", PGM, Version);
86     exit (EXIT_SUCCESS);
87     break;
88   case 5:
89     /* End of options */
90     Carg->EndOptions = 1;
91     break;
92   default:
93     Carg->Index = Sindex;		/* Reset the index */
94     n = 0;
95     break;
96   }
97 
98   return n;
99 }
100