home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.misc
- From: Brad Appleton <brad@hcx1.ssd.csd.harris.com>
- Subject: v26i066: parseargs - functions to parse command line arguments, Patch09b/2
- Message-ID: <1991Nov26.023515.26816@sparky.imd.sterling.com>
- X-Md4-Signature: 02ad4830d62790c0e43b254f7b67c5be
- Date: Tue, 26 Nov 1991 02:35:15 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: Brad Appleton <brad@hcx1.ssd.csd.harris.com>
- Posting-number: Volume 26, Issue 66
- Archive-name: parseargs/patch09b
- Environment: UNIX, VMS, MS-DOS, OS/2, Amiga
- Patch-To: parseargs: Volume 17, Issue 46-57
-
- #!/bin/sh
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file PATCH09 continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 2; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test ! -f _shar_wnt_.tmp; then
- echo 'x - still skipping PATCH09'
- else
- echo 'x - continuing file PATCH09'
- sed 's/^X//' << 'SHAR_EOF' >> 'PATCH09' &&
- X ** ProgName (which is initially NULL) will be used to point to the
- X ** command-name (specified on the command-line) of the command that
- X ** has most recently invoked a function in the parseargs library.
- + ** ProgNameLen will be set to the length of the string.
- X ***^^**********************************************************************/
- X CONST char *ProgName = CHARNULL;
- + int ProgNameLen = 0;
- X
- X EXTERN VOID syserr ARGS((const char *, ...));
- X EXTERN VOID usrerr ARGS((const char *, ...));
- ***************
- *** 195,201 ****
- X
- X
- X /* override argument descriptor, if none given by user */
- ! static ARGDESC Empty_ArgDesc[] = { START_ARGUMENTS, END_ARGUMENTS };
- X
- X /***************************************************************************
- X ** ^SECTION: DEFAULT-ARGUMENTS
- --- 206,212 ----
- X
- X
- X /* override argument descriptor, if none given by user */
- ! static ARGDESC Empty_ArgDesc[] = { START_ARGUMENTS, END_ARGUMENTS };
- X
- X /***************************************************************************
- X ** ^SECTION: DEFAULT-ARGUMENTS
- ***************
- *** 387,479 ****
- X
- X
- X /***************************************************************************
- - ** ^FUNCTION: get_description - get the description portion of a string
- - **
- - ** ^SYNOPSIS:
- - */
- - #ifndef __ANSI_C__
- - static char *get_description( str )
- - /*
- - ** ^PARAMETERS:
- - */
- - char *str;
- - /* -- the string to parse for a description
- - */
- - #endif /* !__ANSI_C__ */
- -
- - /* ^DESCRIPTION:
- - ** Get_description null terminates the first portion of the string and
- - ** returns a pointer to the second portion.
- - **
- - ** Two "portions" must be either separated by whitespace or the second
- - ** portion may be within "(),{},[], or <>" delimiters. The second
- - ** portion is assumed to begin with the first alphabetic following
- - ** separator.
- - **
- - ** ^REQUIREMENTS:
- - ** str should be non-null and non-empty
- - **
- - ** ^SIDE-EFFECTS:
- - ** The characters which separated the two portions of <str> are
- - ** replaced with a single '\0'.
- - **
- - ** ^RETURN-VALUE:
- - ** Address of the description (or NULL if the string has no description).
- - **
- - ** ^ALGORITHM:
- - ** - locate the end of the first portion by scanning for whitespace or
- - ** balanced delimiters.
- - ** - locate the beginning of the second portion by scanning for the first
- - ** alpha-numeric following the end of the first portion.
- - ** - return the address of the description.
- - ***^^**********************************************************************/
- - #ifdef __ANSI_C__
- - static char *get_description( char *str )
- - #endif
- - {
- - register char *description = CHARNULL;
- - BOOL is_end = FALSE, is_balanced = FALSE;
- - char *p;
- - static CONST char whitespace[] = " \t\n\r\f\v";
- - static CONST char beg_portion[] = "(<{[";
- - static CONST char end_portion[] = ")>}]";
- -
- - description = strpbrk( str, whitespace );
- - if ( description ) {
- - is_end = TRUE;
- - *description++ = '\0'; /* null terminate the 1st portion */
- - while ( isspace(*description) ) ++description; /* trim leading ' ' */
- - }
- -
- - if ( !is_end ) {
- - p = strpbrk( str, beg_portion );
- - if ( p ) description = p;
- - }
- -
- - if ( description ) {
- - if ( !is_end ) { /* null terminate and skip leading '(' */
- - is_end = is_balanced = TRUE;
- - *description++ = '\0';
- - }
- - else if ( strchr(beg_portion, *description) ) {
- - is_balanced = TRUE;
- - ++description;
- - }
- - if ( is_balanced ) { /* remove trailing ')' */
- - p = description + (strlen( description ) - 1);
- - if ( strchr(end_portion, *p) ) *p = '\0';
- - }
- - }/*end-if*/
- -
- - if ( description && !is_balanced ) {
- - while ( !isalnum(*description) ) ++description;
- - }
- -
- - return description;
- - }
- -
- -
- - /***************************************************************************
- X ** ^FUNCTION: init_args - Initialize the command object
- X **
- X ** ^SYNOPSIS:
- --- 398,403 ----
- ***************
- *** 522,527 ****
- --- 446,452 ----
- X int ad_count = 0;
- X BOOL old_style = FALSE;
- X char *description = CHARNULL, *purpose = CHARNULL;
- + int desclen;
- X
- X if ( !argd ) return;
- X
- ***************
- *** 542,556 ****
- X if (ARG_isBOOLEAN(ad) || ARG_isPSEUDOARG(ad))
- X BSET( arg_flags(ad), ARGNOVAL );
- X
- ! #ifdef UNWRITABLE_STRING_LITERALS
- ! ad->ad_prompt = strdup( ad->ad_prompt );
- ! #endif
- !
- ! description = get_description( (char *)arg_sname(ad) );
- ! if ( description ) {
- X BSET(arg_flags(ad), ARGDESCRIBED);
- - strcpy( (char *)arg_sdesc(ad), description );
- - }
- X }
- X
- X /* shift all the entries down one to make room for a new 1st-entry
- --- 467,474 ----
- X if (ARG_isBOOLEAN(ad) || ARG_isPSEUDOARG(ad))
- X BSET( arg_flags(ad), ARGNOVAL );
- X
- ! if ( get_argdesc( (char *)arg_sname(ad), &desclen ) )
- X BSET(arg_flags(ad), ARGDESCRIBED);
- X }
- X
- X /* shift all the entries down one to make room for a new 1st-entry
- ***************
- *** 580,592 ****
- X }
- X #endif
- X
- ! #ifdef UNWRITABLE_STRING_LITERALS
- ! cmd_name(argd) = strdup( cmd_name(argd) );
- ! #endif
- !
- X /* if new-style, get the purpose from the command name */
- X if ( !old_style && cmd_name(argd) ) {
- ! purpose = get_description( (char *)cmd_name(argd) );
- X }
- X
- X /* set the program name */
- --- 498,507 ----
- X }
- X #endif
- X
- ! if ( cmd_name(argd) ) cmd_name(argd) = strdup( cmd_name(argd) );
- X /* if new-style, get the purpose from the command name */
- X if ( !old_style && cmd_name(argd) ) {
- ! purpose = cmd_name(argd);
- X }
- X
- X /* set the program name */
- ***************
- *** 1244,1250 ****
- X
- X /* the combination of parse-flags that may be set via $PARSECNTL */
- X #define pa_USRFLAGS \
- ! (pa_PROMPT|pa_IGNORE|pa_ANYCASE|pa_OPTSONLY|pa_KWDSONLY|pa_FLAGS1ST)
- X
- X #ifdef __ANSI_C__
- X static void get_parse_flags( ARGDESC *cmd )
- --- 1159,1165 ----
- X
- X /* the combination of parse-flags that may be set via $PARSECNTL */
- X #define pa_USRFLAGS \
- ! (pa_PROMPT|pa_IGNORE|pa_ANYCASE|pa_OPTSONLY|pa_KWDSONLY|pa_FLAGS1ST)
- X
- X #ifdef __ANSI_C__
- X static void get_parse_flags( ARGDESC *cmd )
- ***************
- *** 1379,1385 ****
- X if ( BTEST(cmd_state(cmd), ps_NOCMDENV) ) return;
- X
- X /* build the name of the environment variable */
- ! strucpy( env_name, ProgName );
- X strcat( env_name, ENV_SUFFIX );
- X
- X /* get the value of the environment variable,
- --- 1294,1302 ----
- X if ( BTEST(cmd_state(cmd), ps_NOCMDENV) ) return;
- X
- X /* build the name of the environment variable */
- ! strncpy( env_name, ProgName, ProgNameLen );
- ! env_name[ProgNameLen] = 0;
- ! (VOID) strupr( env_name );
- X strcat( env_name, ENV_SUFFIX );
- X
- X /* get the value of the environment variable,
- ***************
- *** 1400,1405 ****
- --- 1317,1326 ----
- X return;
- X }
- X
- + #ifdef vms_style
- + BSET( cmd_state(cmd), ps_NOTCMDLINE );
- + #endif
- +
- X rc = parse_argv_style( argv, cmd );
- X free( argv );
- X cmd_list(cmd) = ARGDESCNULL; /* dont allow lists to continue on */
- ***************
- *** 1407,1416 ****
- X cmd_prev(cmd) = ARGDESCNULL;
- X #endif
- X
- X /* check for errors */
- X if ( rc && !BTEST(cmd_flags(cmd), pa_IGNORE) ) {
- ! eprintf( "%s: syntax-error in %s \"%s\".\n",
- ! ProgName, USER_VARIABLE, env_name );
- X eprintf( "\t%s = \"%s\"\n\n", env_name, env_args );
- X free( env_val );
- X usage( cmd );
- --- 1328,1341 ----
- X cmd_prev(cmd) = ARGDESCNULL;
- X #endif
- X
- + #ifdef vms_style
- + BCLEAR( cmd_state(cmd), ps_NOTCMDLINE );
- + #endif
- +
- X /* check for errors */
- X if ( rc && !BTEST(cmd_flags(cmd), pa_IGNORE) ) {
- ! eprintf( "%.*s: syntax-error in %s \"%s\".\n",
- ! ProgNameLen, ProgName, USER_VARIABLE, env_name );
- X eprintf( "\t%s = \"%s\"\n\n", env_name, env_args );
- X free( env_val );
- X usage( cmd );
- ***************
- *** 1451,1457 ****
- X ** macros or with the ENDOFARGS (and possible STARTOFARGS) macros.
- X **
- X ** ^SIDE-EFFECTS:
- ! ** Initialize argd and parses any default arguments.
- X **
- X ** ^RETURN-VALUE:
- X ** pointer to the arg-descriptors
- --- 1376,1382 ----
- X ** macros or with the ENDOFARGS (and possible STARTOFARGS) macros.
- X **
- X ** ^SIDE-EFFECTS:
- ! ** Initialize argd and parses any default arguments.
- X **
- X ** ^RETURN-VALUE:
- X ** pointer to the arg-descriptors
- ***************
- *** 1481,1489 ****
- --- 1406,1416 ----
- X /* save the name of this program (for error messages) */
- X if ( cmd_argv0(cmd) && *(cmd_argv0(cmd)) ) {
- X ProgName = cmd_argv0(cmd);
- + ProgNameLen = get_argpfx(ProgName);
- X }
- X else if ( cmd_name(cmd) && *(cmd_name(cmd)) ) {
- X ProgName = cmd_name(cmd);
- + ProgNameLen = get_argpfx(ProgName);
- X }
- X else {
- X unnamed = TRUE;
- ***************
- *** 1559,1567 ****
- --- 1486,1496 ----
- X
- X if ( cmd_argv0(cmd) && *(cmd_argv0(cmd)) ) {
- X ProgName = cmd_argv0(cmd);
- + ProgNameLen = get_argpfx(ProgName);
- X }
- X else if ( cmd_name(cmd) && *(cmd_name(cmd)) ) {
- X ProgName = cmd_name(cmd);
- + ProgNameLen = get_argpfx(ProgName);
- X }
- X
- X usg_ctl = get_usage_flags(cmd);
- ***************
- *** 1700,1706 ****
- X {
- X register ARGDESC *ad, *args;
- X char *name = VA_ARG( ap, char * );
- ! int *argflags;
- X BOOL is_match = FALSE;
- X
- X /* first we have to find the argument whose flags we need */
- --- 1629,1635 ----
- X {
- X register ARGDESC *ad, *args;
- X char *name = VA_ARG( ap, char * );
- ! argMask_t *argflags;
- X BOOL is_match = FALSE;
- X
- X /* first we have to find the argument whose flags we need */
- ***************
- *** 1722,1729 ****
- X
- X /* now that we found it - retrieve the argument flags */
- X if ( isREADING(mode) ) {
- ! argflags = VA_ARG( ap, int * );
- ! *argflags = (int) arg_flags(ad);
- X }
- X else {
- X rc = pe_BADMODE; /* parsecntl() wont set ARGFLAGS */
- --- 1651,1658 ----
- X
- X /* now that we found it - retrieve the argument flags */
- X if ( isREADING(mode) ) {
- ! argflags = VA_ARG( ap, argMask_t * );
- ! *argflags = arg_flags(ad);
- X }
- X else {
- X rc = pe_BADMODE; /* parsecntl() wont set ARGFLAGS */
- ***************
- *** 1733,1752 ****
- X
- X case pc_PARSEFLAGS : /* get/set the parse-flags */
- X {
- ! int *pflags, flags;
- X
- X /* get value from call-stack (dependent on the mode) */
- X if ( isREADING(mode) ) {
- ! pflags = VA_ARG( ap, int * );
- ! flags = (int) cmd_flags(cmd);
- X }
- X else {
- ! flags = VA_ARG( ap, int );
- X pflags = &flags;
- X }
- X
- X /* perform the desired action(s) */
- ! if ( isWRITING(mode) ) cmd_flags(cmd) = (argMask_t) *pflags;
- X if ( isREADING(mode) ) *pflags = flags;
- X }/*block*/
- X break;
- --- 1662,1681 ----
- X
- X case pc_PARSEFLAGS : /* get/set the parse-flags */
- X {
- ! argMask_t *pflags, flags;
- X
- X /* get value from call-stack (dependent on the mode) */
- X if ( isREADING(mode) ) {
- ! pflags = VA_ARG( ap, argMask_t * );
- ! flags = cmd_flags(cmd);
- X }
- X else {
- ! flags = (argMask_t) VA_ARG( ap, int );
- X pflags = &flags;
- X }
- X
- X /* perform the desired action(s) */
- ! if ( isWRITING(mode) ) cmd_flags(cmd) = *pflags;
- X if ( isREADING(mode) ) *pflags = flags;
- X }/*block*/
- X break;
- ***************
- *** 1793,1800 ****
- X /* get value from call-stack (dependent on the mode) */
- X if ( isREADING(mode) ) {
- X pstr = VA_ARG( ap, CONST char ** );
- ! if ( cntl == pc_NAME ) str = cmd_name(cmd);
- ! else if ( cntl == pc_PURPOSE ) str = cmd_purpose(cmd);
- X else /* cntl == pc_DESCRIPTION */ str = cmd_description(cmd);
- X }
- X else {
- --- 1722,1768 ----
- X /* get value from call-stack (dependent on the mode) */
- X if ( isREADING(mode) ) {
- X pstr = VA_ARG( ap, CONST char ** );
- ! if ( cntl == pc_NAME ) {
- ! if ( !BTEST(cmd_state(cmd), ps_USERNAME|ps_FREENAME) ) {
- ! if ( cmd_name(argd) ) {
- ! int n;
- ! char *p;
- !
- ! n = get_argpfx( (char *)cmd_name(argd) );
- ! p = (char *)malloc( n+1 );
- ! if ( !p ) {
- ! syserr( "malloc failed in parsecntl()" );
- ! }
- ! strncpy(p, (char *)cmd_name(argd), n);
- ! p[n] = 0;
- ! cmd_name(argd) = p;
- ! BSET(cmd_state(cmd), ps_FREENAME);
- ! }
- ! }
- ! str = cmd_name(cmd);
- ! }
- ! else if ( cntl == pc_PURPOSE ) {
- ! if ( !BTEST(cmd_state(cmd), ps_USERPURPOSE|ps_FREEPURPOSE) ) {
- ! if ( cmd_purpose(argd) ) {
- ! int n;
- ! char *p, *q;
- !
- ! p = get_argdesc( (char *)cmd_purpose(argd), &n );
- ! if ( p ) {
- ! q = (char *)malloc( n+1 );
- ! if ( !q ) {
- ! syserr( "malloc failed in parsecntl()" );
- ! }
- ! strncpy(q, p, n);
- ! q[n] = 0;
- ! p = q;
- ! BSET(cmd_state(cmd), ps_FREEPURPOSE);
- ! }
- ! cmd_purpose(cmd) = p;
- ! }
- ! }
- ! str = cmd_purpose(cmd);
- ! }
- X else /* cntl == pc_DESCRIPTION */ str = cmd_description(cmd);
- X }
- X else {
- ***************
- *** 1804,1811 ****
- X
- X /* perform the desired action(s) */
- X if ( isWRITING(mode) ) {
- ! if ( cntl == pc_NAME ) cmd_name(cmd) = *pstr;
- ! else if ( cntl == pc_PURPOSE ) cmd_purpose(cmd) = *pstr;
- X else /* cntl == pc_DESCRIPTION */ cmd_description(cmd) = *pstr;
- X }
- X if ( isREADING(mode) ) *pstr = str;
- --- 1772,1792 ----
- X
- X /* perform the desired action(s) */
- X if ( isWRITING(mode) ) {
- ! if ( cntl == pc_NAME ) {
- ! if ( BTEST(cmd_state(cmd), ps_FREENAME) )
- ! free( cmd_name(cmd) );
- ! BCLEAR( cmd_state(cmd), ps_FREENAME );
- ! BSET( cmd_state(cmd), ps_USERNAME );
- ! cmd_name(cmd) = *pstr;
- ! }
- ! else if ( cntl == pc_PURPOSE ) {
- ! if ( BTEST(cmd_state(cmd), ps_FREEPURPOSE) ) {
- ! free( cmd_purpose(cmd) );
- ! }
- ! BCLEAR( cmd_state(cmd), ps_FREEPURPOSE );
- ! BSET( cmd_state(cmd), ps_USERPURPOSE );
- ! cmd_purpose(cmd) = *pstr;
- ! }
- X else /* cntl == pc_DESCRIPTION */ cmd_description(cmd) = *pstr;
- X }
- X if ( isREADING(mode) ) *pstr = str;
- ***************
- *** 1896,1909 ****
- X BSET(cmd_flags(argd), pa_ARGV0);
- X
- X /* split line up into whitespace separated tokens */
- ! if ( !strsplit( &argv, str, CHARNULL ) ) {
- ! free( argv );
- ! return rc;
- ! }
- X
- X rc = parse_argv_style( argv, parse_init( &argd ) );
- X free( argv );
- X
- X /* scan for missing required arguments */
- X if ( SYNTAX_ERROR(rc, argd) ) {
- X fputc( '\n', stderr );
- --- 1877,1895 ----
- X BSET(cmd_flags(argd), pa_ARGV0);
- X
- X /* split line up into whitespace separated tokens */
- ! (void) strsplit( &argv, str, CHARNULL );
- X
- + #ifdef vms_style
- + BSET( cmd_state(argd), ps_NOTCMDLINE );
- + #endif
- +
- X rc = parse_argv_style( argv, parse_init( &argd ) );
- X free( argv );
- X
- + #ifdef vms_style
- + BCLEAR( cmd_state(argd), ps_NOTCMDLINE );
- + #endif
- +
- X /* scan for missing required arguments */
- X if ( SYNTAX_ERROR(rc, argd) ) {
- X fputc( '\n', stderr );
- ***************
- *** 2131,2136 ****
- --- 2117,2126 ----
- X argv[i] = L_STRING(ls);
- X }
- X
- + #ifdef vms_style
- + BSET( cmd_state(argd), ps_NOTCMDLINE );
- + #endif
- +
- X /* parse the list */
- X saveflags = cmd_flags(argd);
- X BSET(cmd_flags(argd), pa_ARGV0);
- ***************
- *** 2137,2142 ****
- --- 2127,2136 ----
- X rc = parse_argv_style( argv, parse_init( &argd ) );
- X free( argv );
- X
- + #ifdef vms_style
- + BCLEAR( cmd_state(argd), ps_NOTCMDLINE );
- + #endif
- +
- X /* scan for missing required arguments */
- X if ( SYNTAX_ERROR(rc, argd) ) {
- X fputc( '\n', stderr );
- ***************
- *** 2239,2247 ****
- --- 2233,2249 ----
- X }
- X VA_END(ap);
- X
- + #ifdef vms_style
- + BSET( cmd_state(argd), ps_NOTCMDLINE );
- + #endif
- +
- X /* parse the arguments */
- X rc = parse_argv_style( argv, parse_init( &argd ) );
- X free( argv );
- +
- + #ifdef vms_style
- + BCLEAR( cmd_state(argd), ps_NOTCMDLINE );
- + #endif
- X
- X /* scan for missing required arguments */
- X if ( SYNTAX_ERROR(rc, argd) ) {
- SHAR_EOF
- echo 'File PATCH09 is complete' &&
- chmod 0664 PATCH09 ||
- echo 'restore of PATCH09 failed'
- Wc_c="`wc -c < 'PATCH09'`"
- test 66669 -eq "$Wc_c" ||
- echo 'PATCH09: original size 66669, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- rm -f _shar_seq_.tmp
- echo You have unpacked the last part
- exit 0
- exit 0 # Just in case...
- --
- Kent Landfield INTERNET: kent@sparky.IMD.Sterling.COM
- Sterling Software, IMD UUCP: uunet!sparky!kent
- Phone: (402) 291-8300 FAX: (402) 291-4362
- Please send comp.sources.misc-related mail to kent@uunet.uu.net.
-