Example #1
0
/* Free the memory allocated for the sk_list
*/
int sk_free()
{
   SK_P FAR *sk_ptr;

   ll_access(&sk_list);
   while ((sk_ptr = (SK_P FAR *)ll_next(&sk_list)) != NULL) {
      MEM_UNLOCK(&sk_ptr->ptr->sk_val);
      FREE(&sk_ptr->ptr->sk_val);
   }
   ll_deaccess(&sk_list);
   ll_free(&sk_list);
   return( db_status );
}
Example #2
0
static void _parse_input(CTX *ctx)
{
  static char *params = NULL;
  int c = getch();

  switch( c ) {
  case KEY_UP:
    _list_scroll( ctx, SCROLL_UP, 1 );
    break;
  case KEY_DOWN:
    _list_scroll( ctx, SCROLL_DOWN, 1 );
    break;
  case KEY_NPAGE:
    _list_scroll( ctx, SCROLL_DOWN, ctx->rows - 4 );
    break;
  case KEY_PPAGE:
    _list_scroll( ctx, SCROLL_UP, ctx->rows - 4 );
    break;
  case '\n':  /* edit & compile code */
  {
    char *func = ll_access( ctx->symbols.func, ctx->symbols.selected_offset );
    char *sig = ll_access( ctx->symbols.sig, ctx->symbols.selected_offset );
    char *lib = ll_access( ctx->symbols.lib, ctx->symbols.selected_offset );

    /* check for signature */
    /* the assumption here is that the sig by this stage has been validated elsewhere */
    if( memcmp( sig, "??", 2 ) != 0 ) {   /* has signature */
      char path[500];
      sprintf( path,
               "%s/.preloader/%s-%s.%s.c",
               getenv("HOME"),
               ctx->hash,
               _strip_path( ctx->filename ),
               func );

      /* check if file exists */
      struct stat s;
      /* populate if file doesn't exist or is empty */
      if( stat( path, &s ) < 0 || !s.st_size ) {
        FILE *f = fopen( path, "a" );
        if( f ) {
          char *code = code_gen( func, sig, _strip_path( lib ) );
          if( code ) {
            fwrite( code, 1, strlen( code ), f );
            N_FREE( code );
          }
          fclose(f);
        }
      }

      /* TODO: hard-coded to vim now; allow editor selection through config */
      _exec_target( "vim", path, NULL, EXEC_NOPROMPT );

      /* compile code */

      /* remove previous object file */
      char obj_path[500];
      strncpy( obj_path, path, sizeof( obj_path )- 1 );
      obj_path[strlen( obj_path ) - 1] = '\0';
      strncat( obj_path, "o", sizeof( obj_path ) - 1);
      _exec_target( "rm -f", obj_path, NULL, EXEC_NOPROMPT );

      /* compile PIC code */
      strncat( path, " -o ", sizeof( path ) - 1);
      strncat( path, obj_path, sizeof( path ) - 1);
      _exec_target( "gcc -fPIC -c", path, NULL, EXEC_NOPROMPT );

      /* check if object file is present */
      if( stat( obj_path, &s ) < 0 ) {
        /* compilation failed */
        _exec_target( "echo", "Compilation failed! Please fix errors.", NULL, EXEC_PROMPT );
        ctx->symbols.flags[ctx->symbols.selected_offset] |= SYMBOL_INVALID;
        ctx->symbols.flags[ctx->symbols.selected_offset] &= ~SYMBOL_SELECTED;
      } else {
        /* compilation succeeded */
        ctx->symbols.flags[ctx->symbols.selected_offset] &= ~SYMBOL_INVALID;
      }
    } else {  /* no signature */
      _show_notification( ctx, "Please first enter a signature for this function!" );
    }
  }
    break;
  case 's':   /* edit function signature */
  {
    char *sig = _get_input( ctx,
                            "function signature",
                            (char *)ll_access( ctx->symbols.sig,
                                               ctx->symbols.selected_offset ) );

    if( ( sig = _validate_sig( sig ) ) )
      database_add_sig( ctx->db,
                        (char *)ll_access( ctx->symbols.func,
                                           ctx->symbols.selected_offset ),
                        sig );

    /* refresh sig list */
    ctx->symbols.sig = database_get_sigs( ctx->db, &ctx->symbols.num_sigs );
  }
    break;
  case 0x20:  /* select symbol for preloading */
    if( ctx->symbols.flags[ctx->symbols.selected_offset] & SYMBOL_INVALID ) {
      _show_notification( ctx, "Could not compile this wrapper. Please check code!" );
    } else {
      ctx->symbols.flags[ctx->symbols.selected_offset] ^= SYMBOL_SELECTED;
    }
    break;
  case 'r':   /* enter params then execute */
    /* todo: check if function with invalid code is selected */
  {
    char tmp[300];
    sprintf( tmp, "'%s' args", _strip_path( ctx->filename ) );
    params = _get_input( ctx, tmp, params );
  }
  case 'R':   /* execute with previous params */
    /* todo: check if function with invalid code is selected */
    _exec_target( ctx->filename, params, NULL, EXEC_PROMPT );
    break;
  case 'q':
    ctx->running = 0;
    break;
  }
}
Example #3
0
static void _draw_display(CTX *ctx)
{
  char buf[1024];
  int len, pos_x = 0, pos_y = 0;

  getmaxyx( stdscr, ctx->rows, ctx->cols );

  clear();

  /* draw title */
  attron( COLOR_PAIR( 6 ) );
  attron( A_BOLD );
  strcpy( buf, "  -= preloader by 2of1 =-" );
  printw( "%s", buf );
  for( int i = 0; i < ctx->cols - pos_x - strlen( buf ); i++ ) addch( ' ' );
  attroff( A_BOLD );
  attroff( COLOR_PAIR( 6 ) );
  pos_y++;

  /* draw status bar */
  move( ctx->rows - 1, 0 );
  attron( COLOR_PAIR( 8 ) );
  sprintf( buf, "  %s", ctx->filename );
  attron( A_BOLD );
  printw( "%s", buf );
  attroff( A_BOLD );
  len = strlen( buf );
  sprintf( buf,
           "  [%d symbol%s, %d sig match%s]",
           ctx->symbols.count,
           ctx->symbols.count > 1 ? "s" : "",
           ctx->symbols.num_sigs,
           ctx->symbols.num_sigs > 1 ? "es" : "" );
  printw( "%s", buf );
  len += strlen( buf );
  for( int i = 0; i < ctx->cols - pos_x - len; i++ ) addch( ' ' );;
  attroff( COLOR_PAIR( 8 ) );

  switch( ctx->state ) {
  case STATE_NORMAL:
  {
    /* draw symbols */
    /* work out number of rows we have for list */
    /* print from list item ptr to end of list or num free rows (item ptr is moved up and down by up/down arrow) */
    /* format: *_if_selected function_name_bold(sig...normal) */
    pos_y = 2;
    int list_rows = ctx->rows - 1 /* top */ - 3 /* bottom */;
    for( int i = 0; i < list_rows; i ++ ) {
      if( ctx->symbols.display_offset + i == ctx->symbols.count) break;

      move( pos_y, 2 );

      attron( COLOR_PAIR( 5 ) );
      attron( A_BOLD );
      printw( "%c ",
              ctx->symbols.flags[ctx->symbols.display_offset + i] & SYMBOL_SELECTED ? '*' : ' ' );
      attroff( A_BOLD );
      attroff( COLOR_PAIR( 5 ) );

      if (i == ctx->symbols.selected_offset - ctx->symbols.display_offset)
        attron( COLOR_PAIR( 1 ) );
      else
        attron( COLOR_PAIR( 2 ) );

      attron( A_BOLD );
      printw( "%s ",
              ll_access( ctx->symbols.func, ctx->symbols.display_offset + i ) );
      attroff( A_BOLD );

      printw( "%s",
              ll_access( ctx->symbols.sig, ctx->symbols.display_offset + i ) );

      char *str = _strip_path( ll_access( ctx->symbols.lib,
                               ctx->symbols.display_offset + i ) );
      mvprintw( pos_y, ctx->cols - strlen( str ) - 4, "[%s]", str );

      if (i == ctx->symbols.selected_offset - ctx->symbols.display_offset)
        attroff( COLOR_PAIR( 1 ) );
      else
        attroff( COLOR_PAIR( 2 ) );

      pos_y++;
    }

    break;
  }
  case STATE_PROCESSING_SYMS:
  case STATE_PROCESSING_LIBS:
  case STATE_RESOLVING_SYMBOLS:
  {
    static int count = 0;
    static char swirl[] = "|/-\\";
    static char *c = swirl;

    if( count >> 24 != ctx->state ) {
      count = ctx->state << 24;
    }

    move( 2, 2 );
    attron( COLOR_PAIR( 2 ) );
    attron( A_BOLD );

    if( *(++c) == '\0' ) c = swirl;
    printw( "%c %s %s %d, please be patient...",
            *c,
            ctx->state == STATE_RESOLVING_SYMBOLS ? "matching" : "processing",
            ctx->state == STATE_PROCESSING_SYMS || STATE_RESOLVING_SYMBOLS ? "symbol" : "library",
            ++count & 0xffffff );

    attroff( A_BOLD );

    printw( " (%s)", (char *)ctx->extra );

    attroff( COLOR_PAIR( 2 ) );

    break;
  }
  }

  refresh();
}