| 1 | /*=========================================================================== |
| 2 | Copyright (C) 1994-2009 European Southern Observatory (ESO) |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU General Public License as |
| 6 | published by the Free Software Foundation; either version 2 of |
| 7 | the License, or (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public |
| 15 | License along with this program; if not, write to the Free |
| 16 | Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 17 | MA 02139, USA. |
| 18 | |
| 19 | Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 20 | Internet e-mail: [email protected] |
| 21 | Postal address: European Southern Observatory |
| 22 | Data Management Division |
| 23 | Karl-Schwarzschild-Strasse 2 |
| 24 | D 85748 Garching bei Muenchen |
| 25 | GERMANY |
| 26 | ===========================================================================*/ |
| 27 | |
| 28 | /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 | .LANGUAGE C |
| 30 | .IDENT dcparm.c |
| 31 | .AUTHOR Preben J. Grosbol [ESO/IPG] |
| 32 | .KEYWORDS parameter decoding, command line |
| 33 | .ENVIRON UNIX |
| 34 | .VERSION 1.0 1990-Jul-12 : Creation, PJG |
| 35 | .VERSION 1.1 1990-Aug-24 : Add gvparm function, PJG |
| 36 | .VERSION 1.2 1991-Aug-30 : Check for agrc<=0, PJG |
| 37 | .VERSION 1.4 1994-Ovt-09 : Ckeck for wrong options, PJG |
| 38 | ------------------------------------------------------------------------*/ |
| 39 | static char *cnull = ""; /* pointer to NULL string */ |
| 40 | |
| 41 | int dcparm(argc,argv,plist,pval) |
| 42 | /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 43 | .PURPOSE Decode options and parameters on the command line using |
| 44 | standard UNIX syntax. |
| 45 | .COMMENT Each option/flag and parameter is defined in a string |
| 46 | where the first character is the letter for the flag and |
| 47 | the second indicates the type '-' for flag and '+' for |
| 48 | addition string expected. After the parameter definition |
| 49 | field as comment can be given starting with the delimeter ':'. |
| 50 | If a parameter is not given it's value in a NULL popinter. |
| 51 | When a flag without string is specified the pointer points |
| 52 | to a NULL string. |
| 53 | Example: command -x -i name -o file |
| 54 | A parameter list plist[] = {"x-:Execute flag", |
| 55 | "v-:Verbose flag", |
| 56 | "i+:Input file", |
| 57 | "o+:Output file",(char *) 0} |
| 58 | would give an output pval[] = {"",(char *) 0,"name","file"} |
| 59 | .RETURN Status where 0:OK, -1:Error |
| 60 | ------------------------------------------------------------------------*/ |
| 61 | int argc; /* parameter count */ |
| 62 | char **argv; /* pointers to parameters */ |
| 63 | char *plist[]; /* pointers to parameter definitions */ |
| 64 | char *pval[]; /* pointers to parameters strings */ |
| 65 | |
| 66 | { |
| 67 | char c, *pc, *pl; |
| 68 | int n, err; |
| 69 | |
| 70 | pl = cnull; /* initiate variables */ |
| 71 | err = 0; |
| 72 | for (n=0; plist[n]; n++) pval[n] = (char *) 0; |
| 73 | if (argc<=0) return err; |
| 74 | |
| 75 | argv++; /* skip over command name */ |
| 76 | if (--argc != 0) /* decode parameters */ |
| 77 | while (argc--) { |
| 78 | pc = *argv++; |
| 79 | if (*pl=='+') { pval[n] = pc; pl = cnull; continue; } |
| 80 | if (*pc++ == '-') |
| 81 | while (c = *pc++) { |
| 82 | n = 0; |
| 83 | while ((pl=plist[n]) && (*pl!=c)) n++; |
| 84 | if (pl++) { |
| 85 | if (*pl!='+') pval[n] = cnull; |
| 86 | else { |
| 87 | if (*pc) { pval[n] = pc; pl = cnull; } |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | else { pl = cnull; err = -1; } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | char *gvparm(opt,plist,pval) |
| 99 | /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 100 | .PURPOSE Retrieve parameter value for option specified |
| 101 | .COMMENT The arrays 'plist' and 'pval' are the same as in |
| 102 | routine cdparm(). |
| 103 | .RETURN Pointer to value string, NULL pointer if not available |
| 104 | ------------------------------------------------------------------------*/ |
| 105 | char opt; /* option for parameter */ |
| 106 | char *plist[]; /* pointers to parameter definitions */ |
| 107 | char *pval[]; /* pointers to parameter strings */ |
| 108 | |
| 109 | { |
| 110 | while (*plist && **plist!=opt) plist++, pval++; |
| 111 | |
| 112 | return ((*plist) ? *pval : (char *) 0); |
| 113 | } |