⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 param.c

📁 使用于OS/2下的小工具的C源程序,使用于OS/2下的小工具的C源程序
💻 C
字号:
/*
* PARAM.C - General parameter handling functions.
*
*
* PROGRAMMER:	    Martti Ylikoski
* CREATED:	    7.9.1991
*/
static char *VERSION = "Version 1.1 Copyright (c) Martti Ylikoski, 1991" ;
/*
*
*/
#include <os2.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "param.h"
//#define TEST
#ifdef TEST
int null( int argc, char *argv[], int index ) ;
int null2( int argc, char *argv[], int index ) ;
ParamEntry pentry[3] = {
     "A", &null, 1,
     "F", &null2, 2,
     "\0", NULL, 0
} ;

ParamBlock params = {
    "/-",   NOIGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
    pentry
} ;

#endif

static int has_trigger(char *cparams, char *triggers ) ;
static int match_param(ParamBlock *pblock, char *cparam) ;
static void remove_list(int argc, char *argv[], int index, int cremove) ;
static int handle_params_intl(ParamBlock *pblock, int *argc, char *argv[], char *trigger, int trindex) ;
/* Main test function */
#ifdef TEST

int main(int argc, char *argv[])
{

   handle_params(&params, &argc, argv) ;
   return( 0 ) ;
}
int null( int argc, char *argv[], int index )
{
printf("I am null\n") ;
return( 0 ) ;
}
int null2( int argc, char *argv[], int index )
{
   if (argc == index+1) /* indexing starts at 0! */
   {
      printf("Error\n filename missing\n") ;
      return(-1) ;
   }

printf("I am null2\n") ;
return( 0 ) ;
}
#endif

int ParamHandle(ParamBlock *pblock, int *argc, char *argv[])
{
int i, j, ret  ;
   if(pblock->pflags & NOPRIORITY)
   {
      ret = handle_params_intl(pblock, argc, argv, NULL, 0) ;
   }
   else
   {

      j = 0 ;

      while( pblock->paramsa[j].ParamStr !=  NULL &&
	 pblock->paramsa[j].ParamStr[0] != '\0' )
	 j ++ ;

      for (i = 0 ; i < j ; i++)
	 ret = handle_params_intl(pblock, argc, argv, pblock->paramsa[i].ParamStr, i) ;
   }
   return ret ;
}

static int handle_params_intl(ParamBlock *pblock, int *argc, char *argv[], char *trigger, int trindex)
{
int i, index, tlen, cremove ;

   tlen = strlen(pblock->triggers) ; /* length of trigger chars. May be 0! */

   for (i = 1 ; i < *argc ; i++)
   {
      if (strlen(argv[i]) <= 1 ) /* handle trigger without parameter */
	 continue ;

      if (tlen != 0 && has_trigger(argv[i], pblock->triggers) == FALSE)
	 continue ;

      if (trigger == NULL)
	 if(( index = match_param(pblock, argv[i])) < 0 )
	    continue ;
	 else ; /* nothing */
      else
      {
	 if (pblock->pflags & IGNORECASE)
	 {
	    if (strcmpi(&argv[i][1], trigger) == 0)
	       index = trindex ;
	    else
	       continue ;
	 }
	 else
	    if (strcmp(&argv[i][1], trigger) == 0)
	       index = trindex ;
	    else
	       continue ;
      }

//	printf("Gotcha\n") ;

      if ( pblock->paramsa[index].cParams == 0)
	 continue ; /* temporarily disabled */

      if ( pblock->paramsa[index].cParams < 0)
      {
	 remove_list(*argc, argv, i, abs(pblock->paramsa[index].cParams)) ; /* temporarily disabled */
	 continue ;
      }

      if ( pblock->paramsa[index].cParams > 1 &&
	    *argc < i + pblock->paramsa[index].cParams) /* indexing starts at 0! */
      {
	 printf("Missing command line parameter after %s\n", argv[i]) ;
	 return(-1) ;
      }

      if ( (pblock->pflags & NOTRIGGERSALLOWED) && pblock->paramsa[index].cParams > 1 )
      {
      int j ;
	 for (j = i + 1 ; j <= i + pblock->paramsa[index].cParams - 1 ; j++)
	    if ( ( (tlen != 0 && has_trigger(argv[j], pblock->triggers) == TRUE) ||
		 tlen == 0)
		  && ( match_param(pblock, argv[j]) != -1 ) )
	    {
	       printf("Too few parameters after %s(%s is an option)\n", argv[i], argv[j]) ;
	       return( -1 ) ;
	    }
      }

      cremove = pblock->paramsa[index].paramfn(*argc, argv, i) ;
      if (cremove < 0)
	 continue ;

      if (cremove == 0)
	 cremove = pblock->paramsa[index].cParams ;

      remove_list(*argc, argv, i, cremove) ;

      *argc -= cremove ;
      i-- ;
   }
   return( 0 ) ;
}

/*
   has_trigger - returns TRUE if the first parameter contains as
   its first character one of the trigger chars. Otherwise returns FALSE.
*/

static int has_trigger(char *cparams, char *triggers )
{
int i ;
   for(i = 0 ; (size_t) i < strlen(triggers) ; i ++)
   {
      if (cparams[0] == triggers[i])
	return( TRUE );
   }
   return( FALSE ) ;
}

/*
   match_param - this function returns -1 if the given parameter
   has no match in the table of parameters otherwise it returns the index into
   table.
*/

static int match_param(ParamBlock *pblock, char *cparam)
{
int i ;

   i = 0 ;

   while( pblock->paramsa[i].ParamStr !=  NULL &&
	pblock->paramsa[i].ParamStr[0] != '\0' )
   {

      if (pblock->pflags & IGNORECASE)
      {
	 if( strcmpi(pblock->paramsa[i].ParamStr, &cparam[1]) == 0)
	    return( i ) ;
      }
      else
      {
	 if( strcmp(pblock->paramsa[i].ParamStr, &cparam[1]) == 0)
	    return( i ) ;
      }
      i++ ;
   }

   return( -1 ) ;
}
/*
   remove_list - removes cremove parameters from argv startind
   from index.
*/
static void remove_list(int argc, char *argv[], int index, int cremove)
{
int i, j ;
   for ( i = 0 ; i < cremove ; i++)
   {
      for (j = index + i; j < argc-1; j++)
	 argv[j] = argv[j+1] ;
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -