示例#1
0
/* Return pointer to copy of filename in the command buffer */
static const char * get_filename( const char ** const ibufpp )
  {
  static char * buf = 0;
  static int bufsz = 0;
  const int pmax = path_max( 0 );
  int n;

  *ibufpp = skip_blanks( *ibufpp );
  if( **ibufpp != '\n' )
    {
    int size = 0;
    if( !get_extended_line( ibufpp, &size, true ) ) return 0;
    if( **ibufpp == '!' )
      {
      ++*ibufpp;
      return get_shell_command( ibufpp );
      }
    else if( size > pmax )
      { set_error_msg( "Filename too long" ); return 0; }
    }
  else if( !traditional() && !def_filename[0] )
    { set_error_msg( "No current filename" ); return 0; }
  if( !resize_buffer( &buf, &bufsz, pmax + 1 ) ) return 0;
  for( n = 0; **ibufpp != '\n'; ++n, ++*ibufpp ) buf[n] = **ibufpp;
  buf[n] = 0;
  while( **ibufpp == '\n' ) ++*ibufpp;			/* skip newline */
  return ( may_access_filename( buf ) ? buf : 0 );
  }
示例#2
0
/* Return pointer to copy of shell command in the command buffer */
static const char * get_shell_command( const char ** const ibufpp )
  {
  static char * buf = 0;
  static int bufsz = 0;
  static char * shcmd = 0;		/* shell command buffer */
  static int shcmdsz = 0;		/* shell command buffer size */
  static int shcmdlen = 0;		/* shell command length */
  const char * p;			/* substitution char pointer */
  int i = 0, len;

  if( restricted() ) { set_error_msg( "Shell access restricted" ); return 0; }
  if( !get_extended_line( ibufpp, &len, true ) ) return 0;
  p = *ibufpp;
  if( !resize_buffer( &buf, &bufsz, len + 1 ) ) return 0;
  buf[i++] = '!';			/* prefix command w/ bang */
  while( **ibufpp != '\n' )
    {
    if( **ibufpp == '!' )
      {
      if( p != *ibufpp )
        {
        if( !resize_buffer( &buf, &bufsz, i + 1 ) ) return 0;
        buf[i++] = *(*ibufpp)++;
        }
      else if( !shcmd || ( traditional() && !*( shcmd + 1 ) ) )
        { set_error_msg( "No previous command" ); return 0; }
      else
        {
        if( !resize_buffer( &buf, &bufsz, i + shcmdlen ) ) return 0;
        for( p = shcmd + 1; p < shcmd + shcmdlen; ) buf[i++] = *p++;
        p = (*ibufpp)++;
        }
      }
    else if( **ibufpp == '%' )
      {
      if( !def_filename[0] )
        { set_error_msg( "No current filename" ); return 0; }
      p = strip_escapes( def_filename );
      len = strlen( p );
      if( !resize_buffer( &buf, &bufsz, i + len ) ) return 0;
      while( len-- ) buf[i++] = *p++;
      p = (*ibufpp)++;
      }
    else
      {
      if( !resize_buffer( &buf, &bufsz, i + 2 ) ) return 0;
      buf[i++] = **ibufpp;
      if( *(*ibufpp)++ == '\\' ) buf[i++] = *(*ibufpp)++;
      }
    }
  while( **ibufpp == '\n' ) ++*ibufpp;			/* skip newline */
  if( !resize_buffer( &shcmd, &shcmdsz, i + 1 ) ) return 0;
  memcpy( shcmd, buf, i );
  shcmdlen = i; shcmd[i] = 0;
  if( *p == '!' || *p == '%' ) printf( "%s\n", shcmd + 1 );
  return shcmd;
  }
示例#3
0
/* apply command list in the command buffer to the active lines in a
   range; return false if error */
static bool exec_global( const char ** const ibufpp, const int gflags,
                         const bool interactive )
  {
  static char * buf = 0;
  static int bufsz = 0;
  const char * cmd = 0;

  if( !interactive )
    {
    if( traditional() && !strcmp( *ibufpp, "\n" ) )
      cmd = "p\n";			/* null cmd_list == 'p' */
    else
      {
      if( !get_extended_line( ibufpp, 0, false ) ) return false;
      cmd = *ibufpp;
      }
    }
  clear_undo_stack();
  while( true )
    {
    const line_t * const lp = next_active_node();
    if( !lp ) break;
    set_current_addr( get_line_node_addr( lp ) );
    if( current_addr() < 0 ) return false;
    if( interactive )
      {
      /* print current_addr; get a command in global syntax */
      int len;
      if( !display_lines( current_addr(), current_addr(), gflags ) )
        return false;
      do { *ibufpp = get_tty_line( &len ); }
      while( *ibufpp && len > 0 && (*ibufpp)[len-1] != '\n' );
      if( !*ibufpp ) return false;
      if( len == 0 )
        { set_error_msg( "Unexpected end-of-file" ); return false; }
      if( len == 1 && !strcmp( *ibufpp, "\n" ) ) continue;
      if( len == 2 && !strcmp( *ibufpp, "&\n" ) )
        { if( !cmd ) { set_error_msg( "No previous command" ); return false; } }
      else
        {
        if( !get_extended_line( ibufpp, &len, false ) ||
            !resize_buffer( &buf, &bufsz, len + 1 ) ) return false;
        memcpy( buf, *ibufpp, len + 1 );
        cmd = buf;
        }
      }
    *ibufpp = cmd;
    while( **ibufpp ) if( exec_command( ibufpp, 0, true ) < 0 ) return false;
    }
  return true;
  }
示例#4
0
/* get a valid address from the command buffer */
static bool get_third_addr( const char ** const ibufpp, int * const addr )
  {
  const int old1 = first_addr;
  const int old2 = second_addr;
  int addr_cnt = extract_addr_range( ibufpp );

  if( addr_cnt < 0 ) return false;
  if( traditional() && addr_cnt == 0 )
    { set_error_msg( "Destination expected" ); return false; }
  if( second_addr < 0 || second_addr > last_addr() )
    { invalid_address(); return false; }
  *addr = second_addr;
  first_addr = old1; second_addr = old2;
  return true;
  }
示例#5
0
 // fromBSON to Key format
 KeyV1Owned::KeyV1Owned(const BSONObj& obj) {
     BSONObj::iterator i(obj);
     unsigned char bits = 0;
     while( 1 ) { 
         BSONElement e = i.next();
         if( i.more() )
             bits |= cHASMORE;
         switch( e.type() ) { 
         case MinKey:
             b.appendUChar(cminkey|bits);
             break;
         case jstNULL:
             b.appendUChar(cnull|bits);
             break;
         case MaxKey:
             b.appendUChar(cmaxkey|bits);
             break;
         case Bool:
             b.appendUChar( (e.boolean()?ctrue:cfalse) | bits );
             break;
         case jstOID:
             b.appendUChar(coid|bits);
             b.appendBuf(&e.__oid(), sizeof(OID));
             break;
         case BinData:
             {
                 int t = e.binDataType();
                 // 0-7 and 0x80 to 0x87 are supported by Key
                 if( (t & 0x78) == 0 && t != ByteArrayDeprecated ) {
                     int len;
                     const char * d = e.binData(len);
                     if( len <= BinDataLenMax ) {
                         int code = BinDataLengthToCode[len];
                         if( code >= 0 ) {
                             if( t >= 128 )
                                 t = (t-128) | 0x08;
                             dassert( (code&t) == 0 );
                             b.appendUChar( cbindata|bits );
                             b.appendUChar( code | t );
                             b.appendBuf(d, len);
                             break;
                         }
                     }
                 }
                 traditional(obj);
                 return;
             }
         case Date:
             b.appendUChar(cdate|bits);
             b.appendStruct(e.date());
             break;
         case String:
             {
                 b.appendUChar(cstring|bits);
                 // note we do not store the terminating null, to save space.
                 unsigned x = (unsigned) e.valuestrsize() - 1;
                 if( x > 255 ) { 
                     traditional(obj);
                     return;
                 }
                 b.appendUChar(x);
                 b.appendBuf(e.valuestr(), x);
                 break;
             }
         case NumberInt:
             b.appendUChar(cint|bits);
             b.appendNum((double) e._numberInt());
             break;
         case NumberLong:
             {
                 long long n = e._numberLong();
                 long long m = 2LL << 52;
                 DEV {
                     long long d = m-1;
                     verify( ((long long) ((double) -d)) == -d );
                 }
                 if( n >= m || n <= -m ) {
                     // can't represent exactly as a double
                     traditional(obj);
                     return;
                 }
                 b.appendUChar(clong|bits);
                 b.appendNum((double) n);
                 break;
             }
         case NumberDouble:
             {
                 double d = e._numberDouble();
                 if( isNaN(d) ) {
                     traditional(obj);
                     return;
                 }
                 b.appendUChar(cdouble|bits);
                 b.appendNum(d);
                 break;
             }
         default:
             // if other types involved, store as traditional BSON
             traditional(obj);
             return;
         }
         if( !i.more() )
             break;
         bits = 0;
     }
     _keyData = (const unsigned char *) b.buf();
     dassert( b.len() == dataSize() ); // check datasize method is correct
     dassert( (*_keyData & cNOTUSED) == 0 );
 }
示例#6
0
/* execute the next command in command buffer; return error status */
static int exec_command( const char ** const ibufpp, const int prev_status,
                         const bool isglobal )
  {
  const char * fnp;
  int gflags = 0;
  int addr, c, n;
  const int addr_cnt = extract_addr_range( ibufpp );

  if( addr_cnt < 0 ) return ERR;
  *ibufpp = skip_blanks( *ibufpp );
  c = *(*ibufpp)++;
  switch( c )
    {
    case 'a': if( !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !append_lines( ibufpp, second_addr, isglobal ) ) return ERR;
              break;
    case 'c': if( first_addr == 0 ) first_addr = 1;
              if( second_addr == 0 ) second_addr = 1;
              if( !check_current_addr( addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !delete_lines( first_addr, second_addr, isglobal ) ||
                  !append_lines( ibufpp, current_addr(), isglobal ) ) return ERR;
              break;
    case 'd': if( !check_current_addr( addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !delete_lines( first_addr, second_addr, isglobal ) ) return ERR;
              inc_current_addr();
              break;
    case 'e': if( modified() && !scripted() && prev_status != EMOD )
                return EMOD;				/* fall through */
    case 'E': if( unexpected_address( addr_cnt ) ||
                  unexpected_command_suffix( **ibufpp ) ) return ERR;
              fnp = get_filename( ibufpp );
              if( !fnp || !delete_lines( 1, last_addr(), isglobal ) ||
                  !close_sbuf() ) return ERR;
              if( !open_sbuf() ) return FATAL;
              if( fnp[0] && fnp[0] != '!' ) set_def_filename( fnp );
              if( traditional() && !fnp[0] && !def_filename[0] )
                { set_error_msg( "No current filename" ); return ERR; }
              if( read_file( fnp[0] ? fnp : def_filename, 0 ) < 0 )
                return ERR;
              reset_undo_state(); set_modified( false );
              break;
    case 'f': if( unexpected_address( addr_cnt ) ||
                  unexpected_command_suffix( **ibufpp ) ) return ERR;
              fnp = get_filename( ibufpp );
              if( !fnp ) return ERR;
              if( fnp[0] == '!' )
                { set_error_msg( "Invalid redirection" ); return ERR; }
              if( fnp[0] ) set_def_filename( fnp );
              printf( "%s\n", strip_escapes( def_filename ) );
              break;
    case 'g':
    case 'v':
    case 'G':
    case 'V': if( isglobal )
                { set_error_msg( "Cannot nest global commands" ); return ERR; }
              n = ( c == 'g' || c == 'G' );	/* mark matching lines */
              if( !check_addr_range( 1, last_addr(), addr_cnt ) ||
                  !build_active_list( ibufpp, first_addr, second_addr, n ) )
                return ERR;
              n = ( c == 'G' || c == 'V' );		/* interactive */
              if( ( n && !get_command_suffix( ibufpp, &gflags ) ) ||
                  !exec_global( ibufpp, gflags, n ) )
                return ERR;
              break;
    case 'h':
    case 'H': if( unexpected_address( addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( c == 'H' ) verbose = !verbose;
              if( ( c == 'h' || verbose ) && errmsg[0] )
                fprintf( stderr, "%s\n", errmsg );
              break;
    case 'i': if( second_addr == 0 ) second_addr = 1;
              if( !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !append_lines( ibufpp, second_addr - 1, isglobal ) )
                return ERR;
              break;
    case 'j': if( !check_addr_range( current_addr(), current_addr() + 1, addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( first_addr != second_addr &&
                  !join_lines( first_addr, second_addr, isglobal ) ) return ERR;
              break;
    case 'k': n = *(*ibufpp)++;
              if( second_addr == 0 ) { invalid_address(); return ERR; }
              if( !get_command_suffix( ibufpp, &gflags ) ||
                  !mark_line_node( search_line_node( second_addr ), n ) )
                return ERR;
              break;
    case 'l':
    case 'n':
    case 'p': if( c == 'l' ) n = GLS; else if( c == 'n' ) n = GNP; else n = GPR;
              if( !check_current_addr( addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ||
                  !display_lines( first_addr, second_addr, gflags | n ) )
                return ERR;
              gflags = 0;
              break;
    case 'm': if( !check_current_addr( addr_cnt ) ||
                  !get_third_addr( ibufpp, &addr ) ) return ERR;
              if( addr >= first_addr && addr < second_addr )
                { set_error_msg( "Invalid destination" ); return ERR; }
              if( !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !move_lines( first_addr, second_addr, addr, isglobal ) )
                return ERR;
              break;
    case 'P':
    case 'q':
    case 'Q': if( unexpected_address( addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( c == 'P' ) prompt_on = !prompt_on;
              else if( modified() && !scripted() && c == 'q' &&
                       prev_status != EMOD ) return EMOD;
              else return QUIT;
              break;
    case 'r': if( unexpected_command_suffix( **ibufpp ) ) return ERR;
              if( addr_cnt == 0 ) second_addr = last_addr();
              fnp = get_filename( ibufpp );
              if( !fnp ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !def_filename[0] && fnp[0] != '!' ) set_def_filename( fnp );
              if( traditional() && !fnp[0] && !def_filename[0] )
                { set_error_msg( "No current filename" ); return ERR; }
              addr = read_file( fnp[0] ? fnp : def_filename, second_addr );
              if( addr < 0 ) return ERR;
              if( addr ) set_modified( true );
              break;
    case 's': if( !command_s( ibufpp, &gflags, addr_cnt, isglobal ) )
                return ERR;
              break;
    case 't': if( !check_current_addr( addr_cnt ) ||
                  !get_third_addr( ibufpp, &addr ) ||
                  !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !copy_lines( first_addr, second_addr, addr ) ) return ERR;
              break;
    case 'u': if( unexpected_address( addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ||
                  !undo( isglobal ) ) return ERR;
              break;
    case 'w':
    case 'W': n = **ibufpp;
              if( n == 'q' || n == 'Q' ) ++*ibufpp;
              if( unexpected_command_suffix( **ibufpp ) ) return ERR;
              fnp = get_filename( ibufpp );
              if( !fnp ) return ERR;
              if( addr_cnt == 0 && last_addr() == 0 )
                first_addr = second_addr = 0;
              else if( !check_addr_range( 1, last_addr(), addr_cnt ) )
                return ERR;
              if( !def_filename[0] && fnp[0] != '!' ) set_def_filename( fnp );
              if( traditional() && !fnp[0] && !def_filename[0] )
                { set_error_msg( "No current filename" ); return ERR; }
              addr = write_file( fnp[0] ? fnp : def_filename,
                     ( c == 'W' ) ? "a" : "w", first_addr, second_addr );
              if( addr < 0 ) return ERR;
              if( addr == last_addr() ) set_modified( false );
              else if( modified() && !scripted() && n == 'q' &&
                       prev_status != EMOD ) return EMOD;
              if( n == 'q' || n == 'Q' ) return QUIT;
              break;
    case 'x': if( second_addr < 0 || last_addr() < second_addr )
                { invalid_address(); return ERR; }
              if( !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              if( !isglobal ) clear_undo_stack();
              if( !put_lines( second_addr ) ) return ERR;
              break;
    case 'y': if( !check_current_addr( addr_cnt ) ||
                  !get_command_suffix( ibufpp, &gflags ) ||
                  !yank_lines( first_addr, second_addr ) ) return ERR;
              break;
    case 'z': first_addr = 1;
              if( !check_addr_range( first_addr, current_addr() +
                                     ( traditional() || !isglobal ), addr_cnt ) )
                return ERR;
              if( **ibufpp > '0' && **ibufpp <= '9' )
                { if( parse_int( &n, *ibufpp, ibufpp ) ) set_window_lines( n );
                  else return ERR; }
              if( !get_command_suffix( ibufpp, &gflags ) ||
                  !display_lines( second_addr, min( last_addr(), second_addr + window_lines() ),
                                  gflags ) )
                return ERR;
              gflags = 0;
              break;
    case '=': if( !get_command_suffix( ibufpp, &gflags ) ) return ERR;
              printf( "%d\n", addr_cnt ? second_addr : last_addr() );
              break;
    case '!': if( unexpected_address( addr_cnt ) ) return ERR;
              fnp = get_shell_command( ibufpp );
              if( !fnp ) return ERR;
              if( system( fnp + 1 ) < 0 )
                { set_error_msg( "Can't create shell process" ); return ERR; }
              if( !scripted() ) printf( "!\n" );
              break;
    case '\n': first_addr = 1;
              if( !check_addr_range( first_addr, current_addr() +
                                     ( traditional() || !isglobal ), addr_cnt ) ||
                  !display_lines( second_addr, second_addr, 0 ) )
                return ERR;
              break;
    case '#': while( *(*ibufpp)++ != '\n' ) ;
              break;
    default : set_error_msg( "Unknown command" ); return ERR;
    }
  if( gflags && !display_lines( current_addr(), current_addr(), gflags ) )
    return ERR;
  return 0;
  }
示例#7
0
文件: key.cpp 项目: kzys/mongo
// fromBSON to Key format
KeyV1Owned::KeyV1Owned(const BSONObj& obj) {
    BSONObj::iterator i(obj);
    assert( i.more() );
    unsigned char bits = 0;
    while( 1 ) {
        BSONElement e = i.next();
        if( i.more() )
            bits |= cHASMORE;
        switch( e.type() ) {
        case MinKey:
            b.appendUChar(cminkey|bits);
            break;
        case jstNULL:
            b.appendUChar(cnull|bits);
            break;
        case MaxKey:
            b.appendUChar(cmaxkey|bits);
            break;
        case Bool:
            b.appendUChar( (e.boolean()?ctrue:cfalse) | bits );
            break;
        case jstOID:
            b.appendUChar(coid|bits);
            b.appendBuf(&e.__oid(), sizeof(OID));
            break;
        case BinData:
        {
            int t = e.binDataType();
            // 0-7 and 0x80 to 0x87 are supported by Key
            if( (t & 0x78) == 0 && t != ByteArrayDeprecated ) {
                int len;
                const char * d = e.binData(len);
                int code = BinDataLengthToCode[len];
                if( code >= 0 ) {
                    if( t >= 128 )
                        t = (t-128) | 0x08;
                    dassert( (code&t) == 0 );
                    b.appendUChar( cbindata|bits );
                    b.appendUChar( code | t );
                    b.appendBuf(d, len);
                    break;
                }
            }
            traditional(obj);
            return;
        }
        case Date:
            b.appendUChar(cdate|bits);
            b.appendStruct(e.date());
            break;
        case String:
        {
            b.appendUChar(cstring|bits);
            // note we do not store the terminating null, to save space.
            unsigned x = (unsigned) e.valuestrsize() - 1;
            if( x > 255 ) {
                traditional(obj);
                return;
            }
            b.appendUChar(x);
            b.appendBuf(e.valuestr(), x);
            break;
        }
        case NumberInt:
            b.appendUChar(cint|bits);
            b.appendNum((double) e._numberInt());
            break;
        case NumberLong:
        {
            long long n = e._numberLong();
            double d = (double) n;
            if( d != n ) {
                traditional(obj);
                return;
            }
            b.appendUChar(clong|bits);
            b.appendNum(d);
            break;
        }
        case NumberDouble:
        {
            double d = e._numberDouble();
            bool nan = !( d <= numeric_limits< double >::max() &&
                          d >= -numeric_limits< double >::max() );
            if( !nan ) {
                b.appendUChar(cdouble|bits);
                b.appendNum(d);
                break;
            }
            // else fall through and return a traditional BSON obj so our compressed keys need not check for nan
        }
        default:
            // if other types involved, store as traditional BSON
            traditional(obj);
            return;
        }
        if( !i.more() )
            break;
        bits = 0;
    }
    _keyData = (const unsigned char *) b.buf();
    dassert( b.len() == dataSize() ); // check datasize method is correct
    dassert( (*_keyData & cNOTUSED) == 0 );
}