home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume29 / parseargs / part04 / stest.c < prev    next >
C/C++ Source or Header  |  1992-05-19  |  9KB  |  322 lines

  1. /*************************************************************************
  2. ** ^FILE: stest.c - test program for parseargs()
  3. **
  4. ** ^DESCRIPTION:
  5. **    This file is the test program for the parseargs(3) function libarary.
  6. **    It is used to test parseargs for all command-line styles (which presently
  7. **    includes: UNIX, VMS, AmigaDOS, MS-DOS, and OS/2).
  8. **
  9. ** ^HISTORY:
  10. **    --/--/--    Brad Appleton    <brad@ssd.csd.harris.com>    
  11. **    - Added structured block comments
  12. **    - Added an extra test for both old-style and new-style argument arrays
  13. **    - Added a test for triggers (ARGNOVAL arguments)
  14. **    - Added a test for arguments with optional values (using parsecntl())
  15. **    - Added arg-vector arguments
  16. **
  17. **    --/--/--    Peter da Silva    <peter@ferranti.com>    
  18. **
  19. **    --/--/--    Eric P. Allman    <eric@Berkeley.EDU>     Created
  20. ***^^**********************************************************************/
  21.  
  22. #include <useful.h>
  23. #include <parseargs.h>
  24.  
  25. #ifdef vms
  26. # include <ssdef.h>
  27. #endif
  28.  
  29. VERSIONID("$Header: stest.c,v 2.0 89/12/24 00:56:29 eric Exp $");
  30.  
  31. static char Mode[4] = "OFF";
  32.  
  33. /***************************************************************************
  34. ** ^FUNCTION: argMine - example ARGNOVAL argument translation routine
  35. **
  36. ** ^SYNOPSIS:
  37. */
  38. #ifndef __ANSI_C__
  39.    BOOL argMine( ad, vp, copyf )
  40. /*
  41. ** ^PARAMETERS:
  42. */
  43.    ARGDESC *ad;
  44. /*    -- the argument descriptor for this parameter.
  45. */
  46.    char *vp;
  47. /*    -- a pointer to the string input value.
  48. */
  49.    BOOL copyf;
  50. /*    -- if TRUE, the value will be destroyed later, and so should be
  51. **       copied if it will be retained (as for a string).
  52. */
  53. #endif  /* !__ANSI_C__ */
  54.  
  55. /* ^DESCRIPTION:
  56. **    This routine is provided as a (very) simple example of how to use
  57. **    the ARGNOVAL flag to set up "trigger" arguments. Depending upon the
  58. **    implementation of the "trigger" function (this routine), the position
  59. **    of the corresponding argument on the command line may (or may not) be
  60. **    important.
  61. **
  62. ** ^REQUIREMENTS:
  63. **    ad should have ARGNOVAL set and this function as its ad_type.
  64. **
  65. ** ^SIDE-EFFECTS:
  66. **    The static global variable Mode is (re)written.
  67. **
  68. ** ^RETURN-VALUE:
  69. **    TRUE.
  70. **
  71. ** ^ALGORITHM:
  72. **    Trivial.
  73. ***^^**********************************************************************/
  74. /*ARGSUSED*/
  75. #ifdef __ANSI_C__
  76.    BOOL argMine( ARGDESC *ad, char *vp, BOOL copyf )
  77. #endif
  78. {
  79.    strcpy(Mode, "ON");
  80.    return TRUE;
  81. }
  82.  
  83. /*************************************************************************/
  84.    /* declare variables to hold values from the command-line */
  85. #define DEF_STR "Default String"
  86.  
  87. typedef ARGVEC_T(char *)  strvec_t;
  88.  
  89. static int    RepCount;
  90. static char    *Name;
  91. static char    *Str     = DEF_STR;
  92. static char    *DirName = ".";
  93. static BOOL    XRated   = FALSE;
  94. static BOOL    XFlag    = FALSE;
  95. static BOOL    YFlag    = TRUE;
  96. static BOOL    ZFlag    = FALSE;
  97. static char    TabChar  = ':';
  98. static ARGVEC_T(int) Integers = ARGVEC_EMPTY(int);
  99. static strvec_t Groups = ARGVEC_EMPTY(char *);
  100. static ArgList  *Argv  = (ArgList *)NULL;
  101.  
  102. /*************************************************************************/
  103.    /* declare a new style argument-descriptor array */
  104. static
  105. CMD_OBJECT
  106.    Cmd
  107.  
  108. CMD_NAME
  109.    "stest -- test program for parseargs"
  110.  
  111. CMD_DESCRIPTION
  112.    "This program is used to test parseargs for each desired command-line \
  113. style and for both old and new style argument-array declarations.  The actual \
  114. name of the command will be <os>_test.  parseargs will be called twice (with \
  115. the same command-line description) in order to test both the old and the new \
  116. syntax for declaring argument arrays."
  117.  
  118. CMD_ARGUMENTS
  119.    'n', ARGREQ|ARGPOS, argStr,  __ &Name,  "name (name to look for)",
  120.    's', ARGVALOPT, argStr,  __ &Str,       "STRing (optional string to use)",
  121.    'g', ARGVEC,    argStr,  __ &Groups,    "newsGROUPS (newsgroups to test)",
  122.    'c', ARGOPT,    argInt,  __ &RepCount,  "REP=count (repeat count per group)",
  123.    'd', ARGOPT,    argStr,   __ &DirName,  "DIRname (work directory)",
  124.    'i', ARGVEC,    argInt,   __ &Integers, "INTegerS (vector of numbers)",
  125.    '#', ARGHIDDEN, argBool,  __ &XRated,   "XratedMODE (naughty! naughty!)",
  126.  
  127.    'x', ARGOPT, argSBool, __ &XFlag, "Xflag (set X flag)",
  128.    'y', ARGOPT, argUBool, __ &YFlag, "Yflag (unset Y flag)",
  129.    'z', ARGOPT, argTBool, __ &ZFlag, "Zflag (toggle Z flag)",
  130.  
  131.    't', ARGOPT,   argChar, __ &TabChar, "TAB=char (field delimiter)",
  132.    'r', ARGNOVAL, argMine, __ NULL,     "raw (trigger raw-mode \
  133. before processing any more arguments on the command-line)",
  134.  
  135.    ' ', ARGLIST, listStr, __ &Argv, "file (list of files)",
  136.  
  137.    END_ARGUMENTS
  138. CMD_END
  139.  
  140.  
  141. /*************************************************************************/
  142.    /* declare an old style argument-descriptor array */
  143. static ARGDESC    Args[] =
  144. {
  145.    STARTOFARGS,
  146.  
  147.    'n', ARGREQ|ARGPOS, argStr,  __ &Name,  "name (name to look for)",
  148.    's', ARGVALOPT, argStr,  __ &Str,       "STRing (optional string to use)",
  149.    'g', ARGVEC,    argStr,  __ &Groups,    "newsGROUPS (newsgroups to test)",
  150.    'c', ARGOPT,    argInt,  __ &RepCount,  "REP=count (repeat count per group)",
  151.    'd', ARGOPT,    argStr,   __ &DirName,  "DIRname (work directory)",
  152.    'i', ARGVEC,    argInt,   __ &Integers, "INTegerS (vector of numbers)",
  153.    '#', ARGHIDDEN, argBool,  __ &XRated,   "XratedMODE (naughty! naughty!)",
  154.  
  155.    'x', ARGOPT, argSBool, __ &XFlag, "Xflag (set X flag)",
  156.    'y', ARGOPT, argUBool, __ &YFlag, "Yflag (unset Y flag)",
  157.    'z', ARGOPT, argTBool, __ &ZFlag, "Zflag (toggle Z flag)",
  158.  
  159.    't', ARGOPT,   argChar, __ &TabChar, "TAB=char (field delimiter)",
  160.    'r', ARGNOVAL, argMine, __ NULL,     "raw (trigger raw-mode \
  161. before processing any more arguments on the command-line)",
  162.  
  163.    ' ', ARGLIST, listStr, __ &Argv, "file (list of files)",
  164.  
  165.    ENDOFARGS
  166. };
  167.  
  168.  
  169. /***************************************************************************
  170. ** ^FUNCTION: reset_args - reset argument values for another pass
  171. **
  172. ** ^SYNOPSIS:
  173. */
  174. #ifndef __ANSI_C__
  175.    static VOID reset_args()
  176. #endif
  177. /*  
  178. ** ^PARAMETERS:
  179. **    None.
  180. **
  181. ** ^DESCRIPTION:
  182. **    Reset_args resets all the argument values to their corresponding
  183. **    default values so that we can (re)test the parseargs library.
  184. **
  185. ** ^REQUIREMENTS:
  186. **    None.
  187. **
  188. ** ^SIDE-EFFECTS:
  189. **    All the static-global argument variables are rewritten.
  190. **
  191. ** ^RETURN-VALUE:
  192. **    None.
  193. **
  194. ** ^ALGORITHM:
  195. **    Trivial.
  196. ***^^**********************************************************************/
  197. #ifdef __ANSI_C__
  198.    static void reset_args( void )
  199. #endif
  200. {
  201.    RepCount = 0;
  202.    Name     = CHARNULL;
  203.    Str      = DEF_STR;
  204.    DirName  = ".";
  205.    XRated   = FALSE;
  206.    XFlag    = FALSE;
  207.    YFlag    = TRUE;
  208.    ZFlag    = FALSE;
  209.    TabChar  = ':';
  210.  
  211.    vecFree(Integers, int);
  212.    vecDeepFree(Groups, char *);
  213.    listFree( Argv );
  214.    Argv = ARGLISTNULL;
  215. }
  216.  
  217.  
  218. /***************************************************************************
  219. ** ^FUNCTION: print_args - print current argument values
  220. **
  221. ** ^SYNOPSIS:
  222. */
  223. #ifndef __ANSI_C__
  224.    static void print_args( argd )
  225. /*
  226. ** ^PARAMETERS:
  227. */
  228.    ARGDESC *argd;
  229. /*    -- the command whose arg-values are to be printed
  230. */
  231. #endif  /* !__ANSI_C__ */
  232.  
  233. /* ^DESCRIPTION:
  234. **    Print the current values of all the command-line settings
  235. **
  236. ** ^REQUIREMENTS:
  237. **    The command-line should have already been parsed by one of the
  238. **    Xparseargs functions.
  239. **
  240. ** ^SIDE-EFFECTS:
  241. **    Prints on stdout.
  242. **
  243. ** ^RETURN-VALUE:
  244. **    None.
  245. **
  246. ** ^ALGORITHM:
  247. **    Trivial.
  248. ***^^**********************************************************************/
  249. #ifdef __ANSI_C__
  250.    static void print_args( const ARGDESC *argd )
  251. #endif
  252. {
  253.    int  i;
  254.    argMask_t flags;
  255.    ArgList *ls;
  256.  
  257.    printf( "Name = \"%s\", DirName = \"%s\", RepCount = %d,\n",
  258.            Name, DirName, RepCount );
  259.  
  260.    printf( "XFlag = %d, YFlag = %d, ZFlag = %d, TabChar='%c'(%03o);\n",
  261.            XFlag, YFlag, ZFlag, TabChar, TabChar );
  262.  
  263.    printf( "XRated=%d, Raw-Mode = \"%s\"\n", XRated, Mode );
  264.  
  265.       /* to call parsecntl() to see if the optional value was supplied */
  266.    i = parsecntl( (ARGDESC *)argd, pc_ARGFLAGS, pc_READ, "string", &flags);
  267.  
  268.    if ( BTEST(flags, ARGGIVEN) && !BTEST(flags, ARGVALGIVEN) ) {
  269.       printf("String=!No Value Given on CmdLine!\n" );
  270.    }
  271.    else {
  272.       printf("String=\"%s\"\n", Str);
  273.    }
  274.  
  275.    if (Groups.count) {
  276.       printf("Newsgroups:");
  277.       for (i = 0 ; i < Groups.count ; i++ ) {
  278.          printf(" %s", Groups.array[i]);
  279.       }
  280.       putchar('\n');
  281.    }
  282.  
  283.    if (Integers.count) {
  284.       printf("Integers:");
  285.       for (i = 0 ; i < Integers.count ; i++ ) {
  286.          printf(" %d", Integers.array[i]);
  287.       }
  288.       putchar('\n');
  289.    }
  290.  
  291.    if (Argv)  printf("Remaining args: ");
  292.    for ( ls = Argv ; ls ; L_ADVANCE(ls) ) {
  293.       printf("%s", L_STRING(ls));
  294.       if ( L_NEXT(ls) ) {
  295.          putchar(' ');
  296.       }
  297.       else {
  298.          putchar('\n');
  299.       }
  300.    }/*for*/
  301. }
  302.  
  303.  
  304. /*ARGSUSED*/
  305. MAIN(argc, argv)
  306. {
  307.    parseargs(argv, Cmd);   /* parse the command-line */
  308.    print_args(Cmd);        /* print what we found */
  309.  
  310.    putchar('\n');
  311.    reset_args();           /* reset args for another pass */
  312.  
  313.    parseargs(argv, Args);  /* parse same command-line using old-style argd */
  314.    print_args(Args);       /* print what we found (should be same as before) */
  315.  
  316. #ifdef vms
  317.    exit(SS$_NORMAL);
  318. #else
  319.    exit(0);  /* wave bye-bye */
  320. #endif
  321. }
  322.