示例#1
0
文件: gtk.c 项目: TheSLinux-forks/uim
static gboolean
read_cb(GIOChannel *channel, GIOCondition c, gpointer p)
{
  char buf[CANDIDATE_BUFFER_SIZE];
  char *read_buf = strdup("");
  int i = 0;
  int n;
  gchar **tmp;
  int fd = g_io_channel_unix_get_fd(channel);

  while (uim_helper_fd_readable(fd) > 0) {
    n = read(fd, buf, CANDIDATE_BUFFER_SIZE - 1);
    if (n == 0) {
      close(fd);
      exit(EXIT_FAILURE);
    }
    if (n == -1)
      return TRUE;
    buf[n] = '\0';
    read_buf = realloc(read_buf, strlen(read_buf) + n + 1);
    strcat(read_buf, buf);
  }

  tmp = g_strsplit(read_buf, "\f\f", 0);

  while (tmp[i]) {
    str_parse(tmp[i]);
    i++;
  }
  g_strfreev(tmp);
  free(read_buf);
  return TRUE;
}
示例#2
0
文件: dbg.c 项目: lijie169/stm32-tcp
int8_t cmd_parse(uint8_t* cmd_str,uint8_t* num)
{
	int8_t ret ;
	uint8_t pos = 0 ;
	uint8_t curpos = 0 ;
	uint8_t idx;
	uint8_t tmp ;
	uint8_t len = *num;

	if(len < 2 || cmd_str[len - 2] !='\r' || cmd_str[len - 1] !='\n' )
	{
		printf("input must end with \\r\\n\n");
		printf("len is %d,strlen is %d,%s\n",*num,len,cmd_str);
		return ARG_ERROR_END ;
	}

	cmd_str[len-1] = '\0' ;
	cmd_str[len-2] = '\0' ;		
	
	for(idx = 0 ; idx < ARG_MAX_NUMS ; idx++)
	{
		memset(cmd_pointer[idx],0,sizeof(cmd_pointer[idx]));
		ret = str_parse(cmd_pointer[idx], cmd_str+pos, &curpos);

		pos += curpos ;	
		if(ret != ARG_SPACE_END)
			break ;		
	}

	//vertify args
	if(ret == ARG_SPACE_END)
	{
		ret = ARG_ERROR_END ;
		while( *(cmd_str+pos) == ' ') pos++ ;

		if(*(cmd_str+pos) == '\0')
			  ret = ARG_OK ;
	}
	else if(ret == ARG_EOF_END)
	{
		#if 0
		pos = strlen(cmd_pointer[idx]);

		if(cmd_pointer[idx][pos-1] == '\n' && cmd_pointer[idx][pos-2] == '\r')
		{
			ret = ARG_OK  ;
	
		}
		else
			ret = ARG_ERROR_END ;
		#endif

		ret = ARG_OK  ;
	}

	*num = idx+1 ;
	return ret ;

}
示例#3
0
文件: dbg.c 项目: lijie169/stm32-tcp
void shell(void)
{
	int8_t ret ;
	int32_t curpos = 0 ;
	int8_t curlen = 0 ;
	int32_t len = 0 ;
	uint8_t idx;
	uint8_t arg_count ;
	uint8_t count ;
	int8_t* cmd_str = usart_rx_buf ;

	
	len = input(cmd_str,0xFF,0);
	if(!len)
	{
		return  ;
	}

	while(curpos <= len)
	{
	
		for(arg_count = 0 ; arg_count < ARG_MAX_NUMS ; arg_count++)
		{
			memset(cmd_pointer[arg_count],0,sizeof(cmd_pointer[arg_count]));
			ret = str_parse(cmd_pointer[arg_count], cmd_str+curpos, &curlen);

			curpos += curlen ;	
			if(ret == ARG_NOR_END)
				break ;
			else if(ret == ARG_EOF_END)
			{
//				printf("over\n");
				return  ;
			}
		}

		count = sizeof(shell_cmd)/sizeof(cmd_typedef);
		for(idx = 0 ; idx < count ; idx++)
		{
		//	printf("cmp str :%s,%s,%d,%d\n",shell_cmd[idx].fun_str,cmd_pointer[0],strlen(cmd_pointer[0]),strlen(shell_cmd[idx].fun_str));
			
			if(!strcmp(cmd_pointer[0],shell_cmd[idx].fun_str)&& (arg_count) >= shell_cmd[idx].fun_count)
			{
				shell_cmd[idx].function();
//				printf("found\n");
				break  ;
			}
		}

//		printf("cannot found\n");
	}
	//vertify args
//	printf("end\n");
	return  ;

}
示例#4
0
/*
 * Parse a line of the form:
 *         name OP value value value ...
 * where each value is a string and OP can be '=', '+=', '-='
 *
 * NOTE: We do not allocate space for the name and values. Instead we keep
 *         pointers to the line.
 */
status_e parse_line( char *line, 
                     char **namep, 
                     enum assign_op *opp, 
                     pset_h values )
{
   char      *value ;
   char      *values_string ;
   char      *attribute ;
   str_h    strp ;
   const char *func = "parse_line" ;

   if ( ( values_string = get_attr_op( line, &attribute, opp ) ) == NULL )
      return( FAILED ) ;

   /*
    * Now grab the values
    */
   strp = str_parse( values_string, " \t", STR_RETURN_ERROR, (int *)0 ) ;
   if ( strp == NULL )
   {
      parsemsg( LOG_CRIT, func, ES_NOMEM ) ;
      return( FAILED ) ;
   }

   while ( (value = str_component( strp )) )
   {
      if ( pset_add( values, value ) == NULL )
      {
         parsemsg( LOG_CRIT, func, ES_NOMEM ) ;
         str_endparse( strp ) ;
         return( FAILED ) ;
      }
   }

   str_endparse( strp ) ;
   *namep = attribute ;
   return( OK ) ;
}
示例#5
0
文件: parse.c 项目: ltworf/xinetd
/*
 * Find the next service entry.
 * Look for a line of the form:
 *
 *      <white-space> service <white-space> <service_name>
 *
 * followed by a line containing only the ENTRY_BEGIN character
 */
static entry_e find_next_entry( int fd, char **snamep )
{
    char           *p ;
    str_h           strp ;
    char           *sname = NULL;
    entry_e         entry_type=0;
    char           *line = next_line( fd ) ;
    const char     *func = "find_next_entry" ;

    if ( line == CHAR_NULL )
        return( NO_ENTRY ) ;

    strp = str_parse( line, " \t", STR_RETURN_ERROR, INT_NULL ) ;
    if ( strp == NULL )
    {
        parsemsg( LOG_CRIT, func, "str_parse failed" ) ;
        return( BAD_ENTRY ) ;
    }

    if ( ( p = str_component( strp ) ) == CHAR_NULL )
    {
        /*
         * This shouldn't happen since it implies that there is a bug
         * in next_line
         */
        parsemsg( LOG_WARNING, func, "empty line" ) ;
        str_endparse( strp ) ;
        return( BAD_ENTRY ) ;
    }

    /*
     * Look for a keyword
     */
    if ( EQ( p, KW_SERVICE ) || EQ( p, KW_INCLUDE ) || EQ(p, KW_INCLUDEDIR))
    {
        if ( EQ( p, KW_INCLUDE ))
            entry_type = INCLUDE_ENTRY;
        else if ( EQ( p, KW_INCLUDEDIR ))
            entry_type = INCLUDEDIR_ENTRY;

        /*
         * Now get the service name
         */
        if ( ( p = str_component( strp ) ) == CHAR_NULL )
        {
            parsemsg( LOG_ERR, func, "service name missing" ) ;
            str_endparse( strp ) ;
            return( BAD_ENTRY ) ;
        }

        sname = new_string( p ) ;
        if ( sname == CHAR_NULL )
        {
            out_of_memory( func ) ;
            str_endparse( strp ) ;
            return( BAD_ENTRY ) ;
        }
        str_endparse( strp ) ;

        if( (entry_type == INCLUDE_ENTRY) ||
                (entry_type == INCLUDEDIR_ENTRY))
        {
            *snamep = sname ;
            return( entry_type ) ;
        }
        else
            entry_type = SERVICE_ENTRY ;
    }
    else if ( EQ( p, KW_DEFAULTS ) )
    {
        str_endparse( strp ) ;
        entry_type = DEFAULTS_ENTRY ;
    }
    else
    {
        parsemsg( LOG_ERR, func, "missing service keyword" ) ;
        str_endparse( strp ) ;
        return( BAD_ENTRY ) ;
    }

    /*
     * Now look for ENTRY_BEGIN
     */
    line = next_line( fd ) ;
    if ( line == NULL || ! line_has_only_1_char( line, ENTRY_BEGIN ) )
    {
        parsemsg( LOG_ERR, func,
                  "Service %s: missing '%c'", sname, ENTRY_BEGIN ) ;
        if ( entry_type == SERVICE_ENTRY )
            free( sname ) ;
        return( BAD_ENTRY ) ;
    }
    *snamep = sname ;
    return( entry_type ) ;
}
示例#6
0
文件: inet.c 项目: HappyDg/Network-OS
static int get_next_inet_entry( int fd, pset_h sconfs, 
                          struct service_config *defaults)
{
   char *p;
   str_h strp;
   char *line = next_line(fd);
   struct service_config *scp;
   unsigned u, i;
   const char *func = "get_next_inet_entry";
   char *name = NULL, *rpcvers = NULL, *rpcproto = NULL;
   char *group, *proto, *stype;
   const struct name_value *nvp;
   struct protoent *pep ;
   struct passwd *pw ;
   struct group *grp ;
   const char *dot = ".";
   const char *slash = "/";
   pset_h args;
   
   if( line == CHAR_NULL )
      return -2;

   strp = str_parse( line, " \t", STR_RETURN_ERROR, INT_NULL ) ;
   if( strp == NULL )
   {
      parsemsg( LOG_CRIT, func, "inetd.conf - str_parse failed" ) ;
      return( -1 ) ;
   }

   if( (args = pset_create(10,10)) == NULL )
   {
      out_of_memory(func);
      return -1;
   }

   /* Break the line into components, based on spaces */
   while( (p = str_component( strp )) )
   {
      if( pset_add(args, p) == NULL )
      {
         parsemsg( LOG_CRIT, func, ES_NOMEM );
         pset_destroy(args);
         return -1;
      }
   }
   str_endparse( strp );

   /* get the service name */
   name = new_string((char *)pset_pointer( args, 0 ));
   if( name == NULL ) {
      parsemsg( LOG_ERR, func, "inetd.conf - Invalid service name" );
      pset_destroy(args);
      return -1;
   }

   /* Check to find the '/' for specifying RPC version numbers */
   if( (rpcvers = strstr(name, slash)) != NULL ) {
      *rpcvers = '\0';
      rpcvers++;
   }

   scp = sc_alloc( name );
   if( scp == NULL )
   {
      pset_destroy(args);
      free( name );
      return -1;
   }
   /*
    * sc_alloc makes its own copy of name. At this point, sc_alloc worked
    * so we will free our copy to avoid leaks.
    */
   free( name );

   /* Replicate inetd behavior in this regard.  Also makes sure the
    * service actually works on system where setgroups(0,NULL) doesn't
    * work.
    */
   SC_GROUPS(scp) = YES;
   SC_SPECIFY( scp, A_GROUPS );

   /* Get the socket type (stream dgram) */
   stype = (char *)pset_pointer(args, 1);
   if( stype == NULL ) {
      parsemsg( LOG_ERR, func, "inetd.conf - Invalid socket type" );
      pset_destroy(args);
      sc_free(scp);
      return -1;
   }
   nvp = nv_find_value( socket_types, stype );
   if( nvp == NULL )
   {
      parsemsg( LOG_ERR, func, "inetd.conf - Bad socket type: %s", p);
      pset_destroy(args);
      sc_free(scp);
      return -1;
   }

   SC_SOCKET_TYPE(scp) = nvp->value;

   /* Get the protocol type */
   proto = (char *)pset_pointer(args,2);
   if( strstr(proto, "rpc") != NULL )
   {
      int rpcmin, rpcmax;
      struct rpc_data *rdp = SC_RPCDATA( scp ) ;

      if( rpcvers == NULL ) {
         pset_destroy(args);
         sc_free(scp);
         return -1;
         /* uh oh */
      }

      p = strchr(rpcvers, '-');
      if( p && parse_int(rpcvers, 10, '-', &rpcmin) == 0 ) {
         if( parse_base10(p + 1, &rpcmax) || rpcmin > rpcmax ) {
            pset_destroy(args);
            sc_free(scp);
            return -1;
         }
      } else {
         if( parse_base10(rpcvers, &rpcmin) ) {
            pset_destroy(args);
            sc_free(scp);
            return -1;
         }

         rpcmax = rpcmin;
      }

      /* now have min and max rpc versions */
      rdp->rd_min_version = rpcmin;      
      rdp->rd_max_version = rpcmax;      

      rpcproto = strstr(proto, slash);
      if( rpcproto == NULL ) {
         parsemsg( LOG_ERR, func, "inetd.conf - bad rpc version numbers" );
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }
      *rpcproto = '\0';
      rpcproto++;
      proto = rpcproto;

      /* Set the RPC type field */
      nvp = nv_find_value( service_types, "RPC" );
      if ( nvp == NULL )
      {
         parsemsg( LOG_WARNING, func, "inetd.conf - Bad foo %s", name ) ;
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }

      M_SET(SC_TYPE(scp), nvp->value);
   }
   if ( ( pep = getprotobyname( proto ) ) == NULL )
   {
      parsemsg( LOG_ERR, func, "inetd.conf - Protocol %s not in /etc/protocols",
	        proto ) ;
      pset_destroy(args);
      sc_free(scp);
      return -1;
   }

   SC_PROTONAME(scp) = new_string( proto ) ;
   if ( SC_PROTONAME(scp) == NULL )
   {
      out_of_memory( func ) ;
      pset_destroy(args);
      sc_free(scp);
      return -1;
   }
   SC_PROTOVAL(scp) = pep->p_proto;
   SC_SPECIFY(scp, A_PROTOCOL);

   /* Get the wait attribute */
   p = (char *)pset_pointer(args, 3);
   if ( p == NULL ) {
      parsemsg( LOG_ERR, func, "inetd.conf - No value specified for wait" );
      sc_free(scp);
      return -1;
   }
   if ( EQ( p, "wait" ) )
      SC_WAIT(scp) = YES ;
   else if ( EQ( p, "nowait" ) )
      SC_WAIT(scp) = NO ;
   else
      parsemsg( LOG_ERR, func, "inetd.conf - Bad value for wait: %s", p ) ;

   /* Get the user to run as */
   p = (char *)pset_pointer(args, 4);
   if ( p == NULL ) {
      parsemsg( LOG_ERR, func, "inetd.conf - No value specified for user" );
      sc_free(scp);
      return -1;
   }
   if( (group = strstr(p, dot)) )
   {
      *group = '\0';
      group++;
   
      grp = (struct group *)getgrnam( (char *)group ) ;
      if ( grp == NULL )
      {
         parsemsg( LOG_ERR, func, "inetd.conf - Unknown group: %s", group ) ;
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }   

      SC_GID(scp) = ((struct group *)grp)->gr_gid;
      SC_SPECIFY( scp, A_GROUP );
   }

   pw = getpwnam( p );
   if ( pw == NULL )
   {
      parsemsg( LOG_ERR, func, "inetd.conf - Unknown user: %s", p ) ;
      pset_destroy(args);
      sc_free(scp);
      return -1;
   }
   str_fill( pw->pw_passwd, ' ' );
   SC_UID(scp) = pw->pw_uid;
   SC_USER_GID(scp) = pw->pw_gid;

   /* Get server name, or flag as internal */
   p = (char *)pset_pointer(args, 5);
   if ( p == NULL ) {
      parsemsg( LOG_ERR, func, "inetd.conf - No value specified for user" );
      sc_free(scp);
      return -1;
   }
   if( EQ( p, "internal" ) ) 
   {
      nvp = nv_find_value( service_types, "INTERNAL" );
      if ( nvp == NULL )
      {
         parsemsg( LOG_WARNING, func, "inetd.conf - Bad foo %s", name ) ;
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }

      M_SET(SC_TYPE(scp), nvp->value);

      if( EQ( SC_NAME(scp), "time" ) ) {
         if( EQ( proto, "stream" ) )
            SC_ID(scp) = new_string("time-stream");
         else
            SC_ID(scp) = new_string("time-dgram");
      }

      if( EQ( SC_NAME(scp), "daytime" ) ) {
         if( EQ( proto, "stream" ) )
            SC_ID(scp) = new_string("daytime-stream");
         else
            SC_ID(scp) = new_string("daytime-dgram");
      }

      if( EQ( SC_NAME(scp), "chargen" ) ) {
         if( EQ( proto, "stream" ) )
            SC_ID(scp) = new_string("chargen-stream");
         else
            SC_ID(scp) = new_string("chargen-dgram");
      }

      if( EQ( SC_NAME(scp), "echo" ) ) {
         if( EQ( proto, "stream" ) )
            SC_ID(scp) = new_string("echo-stream");
         else
            SC_ID(scp) = new_string("echo-dgram");
      }

      if( EQ( SC_NAME(scp), "discard" ) ) 
      {
         parsemsg(LOG_WARNING, func, 
		  "inetd.conf - service discard not supported");
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }
   }
   else
   {
      SC_SERVER(scp) = new_string( p );
      if ( SC_SERVER(scp) == NULL )
      {
         out_of_memory( func ) ;
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }
      SC_SPECIFY( scp, A_SERVER);

      /* Get argv */ 
      SC_SERVER_ARGV(scp) = (char **)argv_alloc(pset_count(args)+1);

      for( u = 0; u < pset_count(args)-6 ; u++ )
      {
         p = new_string((char *)pset_pointer(args, u+6));
         if( p == NULL )
         {
            for ( i = 1 ; i < u ; i++ )
               free( SC_SERVER_ARGV(scp)[i] );
            free( SC_SERVER_ARGV(scp) );
            pset_destroy(args);
            sc_free(scp);
            return -1;
         }
         SC_SERVER_ARGV(scp)[u] = p;
      }
      /* Set the reuse flag, as this is the default for inetd */
      nvp = nv_find_value( service_flags, "REUSE" );
      if ( nvp == NULL )
      {
         parsemsg( LOG_WARNING, func, "inetd.conf - Bad foo %s", name ) ;
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }
      M_SET(SC_XFLAGS(scp), nvp->value);

      /* Set the NOLIBWRAP flag, since inetd doesn't have libwrap built in */
      nvp = nv_find_value( service_flags, "NOLIBWRAP" );
      if ( nvp == NULL )
      {
         parsemsg( LOG_WARNING, func, "inetd.conf - Bad foo %s", name ) ;
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }
      M_SET(SC_XFLAGS(scp), nvp->value);
   
      /* Set the NAMEINARGS flag, as that's the default for inetd */
      nvp = nv_find_value( service_flags, "NAMEINARGS" );
      if ( nvp == NULL )
      {
         parsemsg( LOG_WARNING, func, "inetd.conf - Bad foo %s", name ) ;
         pset_destroy(args);
         sc_free(scp);
         return (-1);
      }
      M_SET(SC_XFLAGS(scp), nvp->value);
      SC_SPECIFY( scp, A_SERVER_ARGS );

      if ( (SC_ID(scp) = new_string( SC_NAME(scp) )) )
         SC_PRESENT( scp, A_ID ) ;
      else
      {
         out_of_memory( func ) ;
         pset_destroy(args);
         sc_free(scp);
         return -1;
      }
   }
   
   SC_SPECIFY( scp, A_PROTOCOL );
   SC_SPECIFY( scp, A_USER );
   SC_SPECIFY( scp, A_SOCKET_TYPE );
   SC_SPECIFY( scp, A_WAIT );

   if( ! pset_add(sconfs, scp) )
   {
      out_of_memory( func );
      pset_destroy(args);
      sc_free(scp);
      return -1;
   }

   pset_destroy(args);
   parsemsg( LOG_DEBUG, func, "added service %s", SC_NAME(scp));
   return 0;
}