示例#1
0
int
main(int argc, char **argv)
{
        char *correct, *guess, *file, guess_char, true_char;
        int known_incorrect = 0, i;
        FILE *f;

        if (argc != 3) {
                fprintf(stderr, "Usage: %s file guess\n\nCompares the contents of a file with a guess, and\nmakes fun of you if you didn't get it right.\n", argv[0]);
                exit(1);
        }

        file = argv[1];
        guess = argv[2];

        if (!(correct = malloc(1024))) {
                fprintf(stderr, "Error allocating buffer\n");
                exit(1);
        }

        if (!(f = fopen(file, "r"))) {
                fprintf(stderr, "Error opening file: %s\n", file);
                exit(1);
        }

        if (!fgets(correct, 1024, f)) {
                fprintf(stderr, "Error reading file: %s\n", file);
                exit(1);
        }

        if (correct[strlen(correct)-1] == '\n')
                correct[strlen(correct)-1] = '\0';

        fprintf(stderr, "Welcome to the password checker!\n");

        for (i = 0; i < strlen(guess); i++) {
                guess_char = char_at(guess, i);
                true_char = char_at(correct, i);
                fprintf(stderr, ".");
                if (!known_incorrect && (guess_char != true_char)) {
                        known_incorrect = 1;
                        taunt();
                }
        }

        if (!known_incorrect && strlen(guess) != strlen(correct)) {
                known_incorrect = 1;
                taunt();
        }

        fprintf(stderr, "\n");

        if (!known_incorrect) {
                fprintf(stderr, "Wait, how did you know that the password was %s?\n", correct);
        }

        return 0;
}
address NativeGeneralJump::jump_destination() const {  
  int op_code = char_at(0)&0xFF;
  bool is_long = (op_code == 0xE9 || op_code == 0x0F);
  int  offset  = (op_code == 0x0F)  ? 2 : 1;
  int  length  = offset + ((is_long) ? 4 : 1);
  
  if (is_long) 
    return addr_at(0) + length + long_at(offset);
  else
    return addr_at(0) + length + ((int)(char_at(offset)&0xFF));
}
示例#3
0
// remove white space at the start and end of a string
std::string trim_spaces(const std::string &str)
{
    size_t n = 0;
    for ( ;std::isspace(char_at(str, n));++n) { }
    if (n >= str.length()) { return ""; }

    size_t m = str.length() - 1;
    for ( ;std::isspace(char_at(str, m));--m) { }

    return str.substr(n, m - n + 1);
}
示例#4
0
// check if a string looks like a number (with no trailing characters)
bool is_number(const std::string &str)
{
    size_t pos = 0;
    bool any_digits = false;

    if (str[0] == '-') { ++pos; }
    while (std::isdigit(char_at(str, pos))) { ++pos; any_digits = true; }
    if (char_at(str, pos) == '.')
    {
        ++pos;
        while (std::isdigit(char_at(str, pos))) { ++pos; any_digits = true; }
    }

    // check for scientific notation e.g. 3.2e+34, -1e99, 0.01e-45
    if (any_digits && std::tolower(char_at(str, pos)) == 'e')
    {
        size_t pos2 = pos + 1;
        any_digits = false;
        if (char_at(str, pos2) == '+' || char_at(str, pos2) == '-') { ++pos2; }
        if (std::isdigit(char_at(str, pos2)))
        {
            any_digits = true;
            for (pos = pos2 + 1;std::isdigit(char_at(str, pos));++pos) { }
        }
    }

    return (any_digits && pos >= str.length());
}
示例#5
0
static gint
get_end_pos(ScintillaObject *sci, gint line)
{
	gint end;
	gchar ch;
	end = sci_get_line_end_position(sci, line);
	ch = char_at(sci, end - 1);
	/* ignore spaces and "}" */
	while (isspace_no_newline(ch) || '}' == ch)
	{
		end--;
		ch = char_at(sci, end - 1);
	}
	return end;
}
示例#6
0
/**
 * This function is based on Geany's source but has different meaning: check
 * ability to enclose selection. Calls only for selected text so using
 * sci_get_selection_start/end is ok here.
 * */
static gboolean
lexer_has_braces(ScintillaObject *sci, gint lexer)
{
	gint sel_start;
	switch (lexer)
	{
		case SCLEX_CPP:
		case SCLEX_D:
		case SCLEX_PASCAL:
		case SCLEX_TCL:
		case SCLEX_CSS:
			return TRUE;
		case SCLEX_HTML:	/* for PHP & JS */
		case SCLEX_PERL:
		case SCLEX_BASH:
			/* PHP, Perl, bash has vars like ${var} */
			if (get_lines_selected(sci) > 1)
				return TRUE;
			sel_start = sci_get_selection_start(sci);
			if ('$' == char_at(sci, sel_start - 1))
				return FALSE;
			return TRUE;
		default:
			return FALSE;
	}
}
示例#7
0
static gboolean
improve_indent(
	ScintillaObject *sci,
	GeanyEditor     *editor,
	gint             pos)
{
	gint ch, ch_next;
	gint line;
	gint indent, indent_width;
	gint end_pos;
	if (!ac_info->improved_cbracket_indent)
		return AC_CONTINUE_ACTION;
	ch = char_at(sci, pos - 1);
	if (ch != '{')
		return AC_CONTINUE_ACTION;
	/* if curly bracket completion is enabled - just make indents
	 * but ensure that second "}" exists. If disabled - make indent
	 * and complete second curly bracket */
	ch_next = char_at(sci, pos);
	if (ac_info->cbracket && ch_next != '}')
		return AC_CONTINUE_ACTION;
	line = sci_get_line_from_position(sci, pos);
	indent = sci_get_line_indentation(sci, line);
	indent_width = editor_get_indent_prefs(editor)->width;
	sci_start_undo_action(sci);
	if (ac_info->cbracket)
		SSM(sci, SCI_ADDTEXT, 2, (sptr_t)"\n\n");
	else
		SSM(sci, SCI_ADDTEXT, 3, (sptr_t)"\n\n}");
	if (ac_info->whitesmiths_style)
	{
		sci_set_line_indentation(sci, line,     indent);
		sci_set_line_indentation(sci, line + 1, indent);
		sci_set_line_indentation(sci, line + 2, indent);
	}
	else
	{
		sci_set_line_indentation(sci, line + 1, indent + indent_width);
		sci_set_line_indentation(sci, line + 2, indent);
	}
	/* move to the end of added line */
	end_pos = sci_get_line_end_position(sci, line + 1);
	sci_set_current_position(sci, end_pos, TRUE);
	sci_end_undo_action(sci);
	/* do not alow internal auto-indenter to do the work */
	return AC_STOP_ACTION;
}
示例#8
0
// get the length of the operator starting at pos (e.g. returns 2 for ">=" or 1 for "+")
size_t operator_length(const std::string &str, size_t pos)
{
    if (str.substr(pos, 3) == "+/-") { return 3; }

    const char *two_char_op[] =
    { "<=", ">=", "==", "!=", "<>", "+=", "-=", "*=", "/=", "%=", ">>", "<<", "**", "//", 0 };

    char ch = char_at(str, pos);
    char next_ch = char_at(str, pos + 1);

    for (size_t n = 0;two_char_op[n];++n)
    {
        if (ch == two_char_op[n][0] && next_ch == two_char_op[n][1]) { return 2; }
    }

    return 1;
}
void NativeCall::verify() {
  // Make sure code pattern is actually a call imm32 instruction.
  int inst = char_at(0) & 0xFF;
  if (inst != instruction_code) {
    tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", instruction_address(),
                                                        inst);
    fatal("not a call imm32");
  }
}
示例#10
0
void FilePath::string_copy(PathChar* dst, int buf_length) {
  int i;
  // be on safer side - keep everything including final '\0' in buf_length
  buf_length--;
  
  for (i=0; i < buf_length && i<this->length(); i++) {
    dst[i] = (PathChar)char_at(i);
  }
  dst[i] = 0;
}
示例#11
0
static void
ada_val_print_string (struct type *type, const gdb_byte *valaddr,
		      int offset, int offset_aligned, CORE_ADDR address,
		      struct ui_file *stream, int recurse,
		      struct value *original_value,
		      const struct value_print_options *options)
{
  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
  struct type *elttype = TYPE_TARGET_TYPE (type);
  unsigned int eltlen;
  unsigned int len;

  /* We know that ELTTYPE cannot possibly be null, because we assume
     that we're called only when TYPE is a string-like type.
     Similarly, the size of ELTTYPE should also be non-null, since
     it's a character-like type.  */
  gdb_assert (elttype != NULL);
  gdb_assert (TYPE_LENGTH (elttype) != 0);

  eltlen = TYPE_LENGTH (elttype);
  len = TYPE_LENGTH (type) / eltlen;

  if (options->prettyformat_arrays)
    print_spaces_filtered (2 + 2 * recurse, stream);

  /* If requested, look for the first null char and only print
     elements up to it.  */
  if (options->stop_print_at_null)
    {
      int temp_len;

      /* Look for a NULL char.  */
      for (temp_len = 0;
	   (temp_len < len
	    && temp_len < options->print_max
	    && char_at (valaddr + offset_aligned,
			temp_len, eltlen, byte_order) != 0);
	   temp_len += 1);
      len = temp_len;
    }

  printstr (stream, elttype, valaddr + offset_aligned, len, 0,
	    eltlen, options);
}
示例#12
0
double _parse(gchar *args, struct global_vars *gvars)
{
	gchar mul_char = '*';
	gdouble minus_one = -1.0;
	gchar null = 0;
	gint args_len = strlen(args);
	
	struct stack *arguments = stack_init(sizeof(gdouble));
	struct stack *operators = stack_init(sizeof(gchar));

	gint i = 0;
	gint j = 0;
	gint local_nest_level = 0;

	gint8 last_p = 0;  /** priority of last parsed operator */
	gboolean coef_flag = FALSE;  /** set if value might preceed a bracket and thus become a coefficient */
	gboolean func_flag = FALSE;  /** set if result of next bracket is to be passed as an argument to function <symbol> */
	gboolean nest_flag = FALSE;  /** indicates characters are being collected and not parsed */
	gboolean nest_init_flag = FALSE;  /** necessary to skip first character '(' during string collection */
	gboolean neg_flag = TRUE;  /** set if next '-' will make number signed and not be an operator */
	gboolean frac_flag = FALSE;
	
	
	//char nested_term[100] = { 0 }; /** collector string for contents of nested term */
	GString *nested_term = g_string_new(&null);
	//char symbol[100] = { 0 }; /** collector string for symbol name */
	GString *symbol = g_string_new(&null);


	for (i=0; i < args_len; i++)
	{

		if (nest_init_flag) {nest_init_flag = FALSE;}

		/** lock computing by raising nest level, substitute '*' if coefficient exists */
		if (args[i] == '(')
		{
			if (!nest_flag) /** nested interpreting is just about to be initialized */
			{
				if (coef_flag) {stack_push(operators, &mul_char); last_p = priority(mul_char);}
				coef_flag = TRUE;
				nest_flag = TRUE;
				nest_init_flag = TRUE;
				gvars->nest_level += 1;
				nested_term = g_string_new(&null);
			}
			else  /** nested interpreting is in progress */
			{
				local_nest_level += 1;
			}
		}

		else if (args[i] == ')')
		{
			if (nest_flag && local_nest_level == 0) /** nesting has reached end */
			{
				nest_flag = FALSE;
				gdouble nested_term_result = _parse(nested_term->str, gvars);
				gvars->nest_level -= 1;
				g_string_free(nested_term, TRUE);
				nested_term = g_string_new(&null);
				if (func_flag)
				{
					gdouble compute_function_results = compute_function(symbol->str, nested_term_result, gvars);
					stack_push(arguments, &compute_function_results);
					func_flag = FALSE;
					g_string_free(symbol, TRUE);
					symbol = g_string_new(&null);
				}
				else {stack_push(arguments, &nested_term_result);}
			}
			else  /** nested interpreting is in progress, passing by uninterpreted ')' */
			{
				local_nest_level -= 1;
			}
		}


		if (!nest_flag)
		{

			if (args[i] == '.' || args[i] == ',')
			{
				if (g_ascii_isdigit(char_at(args,i+1))) {frac_flag = TRUE;}
				else {gvars->error_type = 3; return 0;}
			}

			else if (g_ascii_isdigit(args[i]))  /** parse number */
			{
				if (gvars->debug)
					{for (j=0;j<gvars->nest_level;j++) {g_printf("   ");} g_printf("args[%d] is digit\n", i);}

				gint8 dig = to_d(args[i]);
				stack_push(gvars->digits, &dig);
				if (frac_flag) {gvars->frac_point -= 1;}
				/** check if there is more than one digit or fractal part */
				if (!(g_ascii_isdigit(char_at(args, i+1)) || char_at(args, i+1) == '.' || char_at(args, i+1) == ','))
				{
					if (coef_flag) {stack_push(operators, &mul_char); last_p = priority(mul_char);}
					gdouble joined_dig =  join_digits(gvars->digits, gvars->frac_point);
					stack_push(arguments, &joined_dig);
					neg_flag = FALSE; coef_flag = TRUE; frac_flag = FALSE; gvars->frac_point = 0;
				}
			}

			else if (isoperator(args[i]))  /** parse operators */
			{
				if (gvars->debug)
					{for (j=0;j<gvars->nest_level;j++) {g_printf("   ");} g_printf("args[%d] is operator\n", i);}

				if (neg_flag && args[i] == '-')  /** check if preceeding minus changes sign of next symbol */
				{
					neg_flag = FALSE;
					stack_push(arguments, &minus_one); stack_push(operators, &mul_char); last_p = priority(mul_char);
				}
				else
				{
					if (stack_length(arguments) <= stack_length(operators)) {gvars->error_type = 4; break;}
					/** check beforehand if lower priority operator is encountered */
					if (priority(args[i]) < last_p) {compute(arguments, operators, gvars);}
					last_p = priority(args[i]);
					stack_push(operators, &args[i]);
					coef_flag = FALSE;
					neg_flag = TRUE;
				}
			}

			else if (g_ascii_isalpha(args[i])) /** parse letters */
			{
				if (gvars->debug)
					{for (j=0;j<gvars->nest_level;j++) {g_printf("   ");} printf("args[%d] is letter\n", i);}

				if (coef_flag) {coef_flag = FALSE; stack_push(operators, &mul_char); last_p = priority(mul_char);}
				if (neg_flag) {neg_flag = FALSE;}
				g_string_append_c(symbol, args[i]);
				if (char_at(args,i+1) == '(')
				{
					compute_function(symbol->str, 1.337, gvars);
					if (gvars->error_type != 0)
					{
						gvars->error_type = 0;
						gdouble looked_up_c = lookup_constant(symbol->str, gvars);
						stack_push(arguments, &looked_up_c);
						//+symbol = "";
						g_string_free(symbol, TRUE);
						symbol = g_string_new(&null);
						coef_flag = TRUE;
					}
					else {func_flag = TRUE;}
				}
				else if (!g_ascii_isalpha(char_at(args,i+1)))
				{
					gdouble looked_up_c = lookup_constant(symbol->str, gvars);
					stack_push(arguments, &looked_up_c);
					g_string_free(symbol, TRUE);
					symbol = g_string_new(&null);
					coef_flag = TRUE;
				}
			}

		}

		else if (!nest_init_flag) /** this collector block needs to be skipped once so the first '(' isn't collected */
		{
			g_string_append_c(nested_term, args[i]);
		}

		if (args[i] == ' ') {coef_flag = FALSE;}

		if (char_at(args,i) == '#') {break;}  /** failsafe, in case array bounds are left */
	}
	if (gvars->debug)
		{printf("<args>\n");stack_dump(arguments, 'd');printf("<ops>\n");stack_dump(operators, 'c');printf("<>\n");}

	if (local_nest_level != 0 && gvars->error_type == 0) {gvars->error_type = 1;}
	if (neg_flag && gvars->error_type == 0) {gvars->error_type = 4;}
	if (gvars->error_type == 0) {compute(arguments, operators, gvars);}
	if (stack_length(arguments) > 1 && gvars->error_type == 0) {gvars->error_type = 4;printf("no2\n");}

	gdouble return_value = 0;
	if (gvars->error_type == 0) {stack_pop(arguments, &return_value);}
	stack_destroy(arguments);
	stack_destroy(operators);

	return return_value;
}
示例#13
0
static void
printstr (struct ui_file *stream, struct type *elttype, const gdb_byte *string,
	  unsigned int length, int force_ellipses, int type_len,
	  const struct value_print_options *options)
{
  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (elttype));
  unsigned int i;
  unsigned int things_printed = 0;
  int in_quotes = 0;
  int need_comma = 0;

  if (length == 0)
    {
      fputs_filtered ("\"\"", stream);
      return;
    }

  for (i = 0; i < length && things_printed < options->print_max; i += 1)
    {
      /* Position of the character we are examining
         to see whether it is repeated.  */
      unsigned int rep1;
      /* Number of repetitions we have detected so far.  */
      unsigned int reps;

      QUIT;

      if (need_comma)
	{
	  fputs_filtered (", ", stream);
	  need_comma = 0;
	}

      rep1 = i + 1;
      reps = 1;
      while (rep1 < length
	     && char_at (string, rep1, type_len, byte_order)
		== char_at (string, i, type_len, byte_order))
	{
	  rep1 += 1;
	  reps += 1;
	}

      if (reps > options->repeat_count_threshold)
	{
	  if (in_quotes)
	    {
	      fputs_filtered ("\", ", stream);
	      in_quotes = 0;
	    }
	  fputs_filtered ("'", stream);
	  ada_emit_char (char_at (string, i, type_len, byte_order),
			 elttype, stream, '\'', type_len);
	  fputs_filtered ("'", stream);
	  fprintf_filtered (stream, _(" <repeats %u times>"), reps);
	  i = rep1 - 1;
	  things_printed += options->repeat_count_threshold;
	  need_comma = 1;
	}
      else
	{
	  if (!in_quotes)
	    {
	      fputs_filtered ("\"", stream);
	      in_quotes = 1;
	    }
	  ada_emit_char (char_at (string, i, type_len, byte_order),
			 elttype, stream, '"', type_len);
	  things_printed += 1;
	}
    }

  /* Terminate the quotes if necessary.  */
  if (in_quotes)
    fputs_filtered ("\"", stream);

  if (force_ellipses || i < length)
    fputs_filtered ("...", stream);
}
static int
ada_val_print_1 (struct type *type, char *valaddr0, int embedded_offset,
		 CORE_ADDR address, struct ui_file *stream, int format,
		 int deref_ref, int recurse, enum val_prettyprint pretty)
{
  unsigned int len;
  int i;
  struct type *elttype;
  unsigned int eltlen;
  LONGEST val;
  char *valaddr = valaddr0 + embedded_offset;

  CHECK_TYPEDEF (type);

  if (ada_is_array_descriptor_type (type) || ada_is_packed_array_type (type))
    {
      int retn;
      struct value *mark = value_mark ();
      struct value *val;
      val = value_from_contents_and_address (type, valaddr, address);
      val = ada_coerce_to_simple_array_ptr (val);
      if (val == NULL)
	{
	  fprintf_filtered (stream, "(null)");
	  retn = 0;
	}
      else
	retn = ada_val_print_1 (VALUE_TYPE (val), VALUE_CONTENTS (val), 0,
				VALUE_ADDRESS (val), stream, format,
				deref_ref, recurse, pretty);
      value_free_to_mark (mark);
      return retn;
    }

  valaddr = ada_aligned_value_addr (type, valaddr);
  embedded_offset -= valaddr - valaddr0 - embedded_offset;
  type = printable_val_type (type, valaddr);

  switch (TYPE_CODE (type))
    {
    default:
      return c_val_print (type, valaddr0, embedded_offset, address, stream,
			  format, deref_ref, recurse, pretty);

    case TYPE_CODE_PTR:
      {
	int ret = c_val_print (type, valaddr0, embedded_offset, address, 
			       stream, format, deref_ref, recurse, pretty);
	if (ada_is_tag_type (type))
	  {
	    struct value *val = 
	      value_from_contents_and_address (type, valaddr, address);
	    const char *name = ada_tag_name (val);
	    if (name != NULL) 
	      fprintf_filtered (stream, " (%s)", name);
	    return 0;
	}
	return ret;
      }

    case TYPE_CODE_INT:
    case TYPE_CODE_RANGE:
      if (ada_is_fixed_point_type (type))
	{
	  LONGEST v = unpack_long (type, valaddr);
	  int len = TYPE_LENGTH (type);

	  fprintf_filtered (stream, len < 4 ? "%.11g" : "%.17g",
			    (double) ada_fixed_to_float (type, v));
	  return 0;
	}
      else if (ada_is_vax_floating_type (type))
	{
	  struct value *val =
	    value_from_contents_and_address (type, valaddr, address);
	  struct value *func = ada_vax_float_print_function (type);
	  if (func != 0)
	    {
	      static struct type *parray_of_char = NULL;
	      struct value *printable_val;

	      if (parray_of_char == NULL)
		parray_of_char =
		  make_pointer_type
		  (create_array_type
		   (NULL, builtin_type_char,
		    create_range_type (NULL, builtin_type_int, 0, 32)), NULL);

	      printable_val =
		value_ind (value_cast (parray_of_char,
				       call_function_by_hand (func, 1,
							      &val)));

	      fprintf_filtered (stream, "%s", VALUE_CONTENTS (printable_val));
	      return 0;
	    }
	  /* No special printing function.  Do as best we can.  */
	}
      else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
	{
	  struct type *target_type = TYPE_TARGET_TYPE (type);
	  if (TYPE_LENGTH (type) != TYPE_LENGTH (target_type))
	    {
	      /* Obscure case of range type that has different length from
	         its base type.  Perform a conversion, or we will get a
	         nonsense value.  Actually, we could use the same
	         code regardless of lengths; I'm just avoiding a cast.  */
	      struct value *v = value_cast (target_type,
					    value_from_contents_and_address
					    (type, valaddr, 0));
	      return ada_val_print_1 (target_type, VALUE_CONTENTS (v), 0, 0,
				      stream, format, 0, recurse + 1, pretty);
	    }
	  else
	    return ada_val_print_1 (TYPE_TARGET_TYPE (type),
				    valaddr0, embedded_offset,
				    address, stream, format, deref_ref,
				    recurse, pretty);
	}
      else
	{
	  format = format ? format : output_format;
	  if (format)
	    {
	      print_scalar_formatted (valaddr, type, format, 0, stream);
	    }
          else if (ada_is_system_address_type (type))
            {
              /* FIXME: We want to print System.Address variables using
                 the same format as for any access type.  But for some
                 reason GNAT encodes the System.Address type as an int,
                 so we have to work-around this deficiency by handling
                 System.Address values as a special case.  */
              fprintf_filtered (stream, "(");
              type_print (type, "", stream, -1);
              fprintf_filtered (stream, ") ");
              print_address_numeric 
		(extract_typed_address (valaddr, builtin_type_void_data_ptr),
                 1, stream);
            }
	  else
	    {
	      val_print_type_code_int (type, valaddr, stream);
	      if (ada_is_character_type (type))
		{
		  fputs_filtered (" ", stream);
		  ada_printchar ((unsigned char) unpack_long (type, valaddr),
				 stream);
		}
	    }
	  return 0;
	}

    case TYPE_CODE_ENUM:
      if (format)
	{
	  print_scalar_formatted (valaddr, type, format, 0, stream);
	  break;
	}
      len = TYPE_NFIELDS (type);
      val = unpack_long (type, valaddr);
      for (i = 0; i < len; i++)
	{
	  QUIT;
	  if (val == TYPE_FIELD_BITPOS (type, i))
	    {
	      break;
	    }
	}
      if (i < len)
	{
	  const char *name = ada_enum_name (TYPE_FIELD_NAME (type, i));
	  if (name[0] == '\'')
	    fprintf_filtered (stream, "%ld %s", (long) val, name);
	  else
	    fputs_filtered (name, stream);
	}
      else
	{
	  print_longest (stream, 'd', 0, val);
	}
      break;

    case TYPE_CODE_FLT:
      if (format)
	return c_val_print (type, valaddr0, embedded_offset, address, stream,
			    format, deref_ref, recurse, pretty);
      else
	ada_print_floating (valaddr0 + embedded_offset, type, stream);
      break;

    case TYPE_CODE_UNION:
    case TYPE_CODE_STRUCT:
      if (ada_is_bogus_array_descriptor (type))
	{
	  fprintf_filtered (stream, "(...?)");
	  return 0;
	}
      else
	{
	  print_record (type, valaddr, stream, format, recurse, pretty);
	  return 0;
	}

    case TYPE_CODE_ARRAY:
      elttype = TYPE_TARGET_TYPE (type);
      if (elttype == NULL)
	eltlen = 0;
      else
	eltlen = TYPE_LENGTH (elttype);
      /* FIXME: This doesn't deal with non-empty arrays of
	 0-length items (not a typical case!) */
      if (eltlen == 0)
	len = 0;
      else
	len = TYPE_LENGTH (type) / eltlen;

	  /* For an array of chars, print with string syntax.  */
      if (ada_is_string_type (type) && (format == 0 || format == 's'))
	{
	  if (prettyprint_arrays)
	    {
	      print_spaces_filtered (2 + 2 * recurse, stream);
	    }
	  /* If requested, look for the first null char and only print
	     elements up to it.  */
	  if (stop_print_at_null)
	    {
	      int temp_len;

	      /* Look for a NULL char.  */
	      for (temp_len = 0;
		   temp_len < len && temp_len < print_max
		     && char_at (valaddr, temp_len, eltlen) != 0;
		   temp_len += 1);
	      len = temp_len;
	    }

	  printstr (stream, valaddr, len, 0, eltlen);
	}
      else
	{
	  len = 0;
	  fprintf_filtered (stream, "(");
	  print_optional_low_bound (stream, type);
	  if (TYPE_FIELD_BITSIZE (type, 0) > 0)
	    val_print_packed_array_elements (type, valaddr, 0, stream,
					     format, recurse, pretty);
	  else
	    val_print_array_elements (type, valaddr, address, stream,
				      format, deref_ref, recurse,
				      pretty, 0);
	  fprintf_filtered (stream, ")");
	}
      gdb_flush (stream);
      return len;

    case TYPE_CODE_REF:
      elttype = check_typedef (TYPE_TARGET_TYPE (type));
      /* De-reference the reference */
      if (deref_ref)
	{
	  if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
	    {
	      LONGEST deref_val_int = (LONGEST)
		unpack_pointer (lookup_pointer_type (builtin_type_void),
				valaddr);
	      if (deref_val_int != 0)
		{
		  struct value *deref_val =
		    ada_value_ind (value_from_longest
				   (lookup_pointer_type (elttype),
				    deref_val_int));
		  val_print (VALUE_TYPE (deref_val),
			     VALUE_CONTENTS (deref_val), 0,
			     VALUE_ADDRESS (deref_val), stream, format,
			     deref_ref, recurse + 1, pretty);
		}
	      else
		fputs_filtered ("(null)", stream);
	    }
	  else
	    fputs_filtered ("???", stream);
	}
      break;
    }
  gdb_flush (stream);
  return 0;
}
static void
printstr (struct ui_file *stream, char *string, unsigned int length,
	  int force_ellipses, int type_len)
{
  unsigned int i;
  unsigned int things_printed = 0;
  int in_quotes = 0;
  int need_comma = 0;

  if (length == 0)
    {
      fputs_filtered ("\"\"", stream);
      return;
    }

  for (i = 0; i < length && things_printed < print_max; i += 1)
    {
      /* Position of the character we are examining
         to see whether it is repeated.  */
      unsigned int rep1;
      /* Number of repetitions we have detected so far.  */
      unsigned int reps;

      QUIT;

      if (need_comma)
	{
	  fputs_filtered (", ", stream);
	  need_comma = 0;
	}

      rep1 = i + 1;
      reps = 1;
      while (rep1 < length
	     && char_at (string, rep1, type_len) == char_at (string, i,
							     type_len))
	{
	  rep1 += 1;
	  reps += 1;
	}

      if (reps > repeat_count_threshold)
	{
	  if (in_quotes)
	    {
	      if (inspect_it)
		fputs_filtered ("\\\", ", stream);
	      else
		fputs_filtered ("\", ", stream);
	      in_quotes = 0;
	    }
	  fputs_filtered ("'", stream);
	  ada_emit_char (char_at (string, i, type_len), stream, '\'',
			 type_len);
	  fputs_filtered ("'", stream);
	  fprintf_filtered (stream, " <repeats %u times>", reps);
	  i = rep1 - 1;
	  things_printed += repeat_count_threshold;
	  need_comma = 1;
	}
      else
	{
	  if (!in_quotes)
	    {
	      if (inspect_it)
		fputs_filtered ("\\\"", stream);
	      else
		fputs_filtered ("\"", stream);
	      in_quotes = 1;
	    }
	  ada_emit_char (char_at (string, i, type_len), stream, '"',
			 type_len);
	  things_printed += 1;
	}
    }

  /* Terminate the quotes if necessary.  */
  if (in_quotes)
    {
      if (inspect_it)
	fputs_filtered ("\\\"", stream);
      else
	fputs_filtered ("\"", stream);
    }

  if (force_ellipses || i < length)
    fputs_filtered ("...", stream);
}
示例#16
0
static int
getfence(
int ch, /* fence type to match against */
int sdir) /* direction to scan if we're not on a fence to begin with */
{
	MARK	oldpos; 	/* original pointer */
	register int ofence = 0;	/* open fence */
	int s, i;
	int key = CPP_UNKNOWN;
	char *C_fences, *ptr;
	int fch;

	/* save the original cursor position */
	oldpos = DOT;

	/* ch may have been passed, if being used internally */
	if (!ch) {
		if ((i = firstchar(DOT.l)) < 0)	/* offset of first nonblank */
			return FALSE;		/* line is entirely blank */

		if (DOT.o <= i && (ch = lgetc(DOT.l,i)) == '#') {
			if (llength(DOT.l) < i+3)
				return FALSE;
		} else if ((ch = char_at(DOT)) == '/' || ch == '*') {
			/* EMPTY */;
		} else if (sdir == FORWARD) {
			/* get the current character */
			if (oldpos.o < llength(oldpos.l)) {
				do {
					ch = char_at(oldpos);
				} while(!is_user_fence(ch, (int *)0) &&
					++oldpos.o < llength(oldpos.l));
			}
			if (is_at_end_of_line(oldpos)) {
				return FALSE;
			}
		} else {
			/* get the current character */
			if (oldpos.o >= 0) {
				do {
					ch = char_at(oldpos);
				} while(!is_user_fence(ch, (int *)0) &&
					--oldpos.o >= 0);
			}

			if (oldpos.o < 0) {
				return FALSE;
			}
		}

		/* we've at least found a fence -- move us that far */
		DOT.o = oldpos.o;
	}

	fch = ch;

	/* is it a "special" fence char? */
	C_fences = "/*#";
	ptr = strchr(C_fences, ch);
	
	if (!ptr) {
		ofence = is_user_fence(ch, &sdir);
		if (ofence)
			fch = PAIRED_FENCE_CH;
	}

	/* setup proper matching fence */
	switch (fch) {
		case PAIRED_FENCE_CH:
			/* NOTHING */
			break;
		case '#':
			if ((i = firstchar(DOT.l)) < 0)
				return FALSE;	/* line is entirely blank */
			if ((i = nextchar(DOT.l, i+1)) >= 0
			 && ((key = cpp_keyword(DOT.l, i)) != CPP_UNKNOWN))
			 	break;
			return FALSE;
		case '*':
			ch = '/';
			if (NextCharIs('/')) {
				sdir = REVERSE;
				forwchar(TRUE,1);
				break;
			} else if (PrevCharIs('/')) {
				sdir = FORWARD;
				backchar(TRUE,1);
				if (doingopcmd)
					pre_op_dot = DOT;
				break;
			}
			return FALSE;
		case '/':
			if (NextCharIs('*')) {
				sdir = FORWARD;
				break;
			} else if (PrevCharIs('*')) {
				sdir = REVERSE;
				break;
			}
			/* FALL THROUGH */
		default: 
			return(FALSE);
	}

	/* ops are inclusive of the endpoint */
	if (doingopcmd && sdir == REVERSE) {
		forwchar(TRUE,1);
		pre_op_dot = DOT;
		backchar(TRUE,1);
	}

	if (key != CPP_UNKNOWN) {  /* we're searching for a cpp keyword */
		s = cpp_fence(sdir, key);
	} else if (ch == '/') {
		s = comment_fence(sdir);
	} else {
		s = simple_fence(sdir, ch, ofence);
	}

	if (s == TRUE)
		return TRUE;

	/* restore the current position */
	DOT = oldpos;
	return(FALSE);
}
示例#17
0
// split a string into individual tokens
void tokenise(const std::string &line, const std::string &filename, int line_num, const Options &opt,
    /*out*/ std::vector<Token> &tokens,
    bool test_is_raw_python = false   // whether --test input is raw python code (vs just a list of boolean expressions)
)
{
    size_t pos = 0;
    size_t len = line.length();
    bool found_assign_op = false;
    bool ids_can_be_keywords = opt.assign || (opt.test && !test_is_raw_python);

    while (pos < len)
    {
        size_t space_start = pos;

        // skip whitespace (and finish if end of line or comment)
        while (std::isspace(char_at(line, pos))) { ++pos; }
        if (pos >= len || line[pos] == '#') { break; }

        size_t num_spaces = pos - space_start;
        size_t tok_start = pos;
        char ch = line[pos];
        char next_ch = char_at(line, pos + 1);
        token_type type = t_undefined;
        std::string tok_str;

        if (opt.assign && found_assign_op)
        {
            type = t_string;
            pos = len;
            tok_str = trim_spaces(line.substr(tok_start, pos - tok_start));
            if (is_quoted(tok_str))
            {
                // make sure quotes are of the right type
                std::string unquoted_tok_str = tok_str.substr(1, tok_str.length() - 2);
                tok_str = quote(unquoted_tok_str, '\'');
            }
            else if (!is_number(tok_str)) { tok_str = quote(tok_str, '\''); }
        }
        else
        if (is_start_of_id(ch))
        {
            // TODO: maybe allow spaces around array indexes , e.g. "a/b[ 10 ]/c"
            while (is_id(char_at(line, pos))) { ++pos; }
            std::string id = line.substr(tok_start, pos - tok_start);
            check_transform_id(id);
            if (is_keyword(id) && !(ids_can_be_keywords && is_keyword_allowed_as_id(id))) { tok_str = id; type = t_keyword; }
            else if (id != kwd_expect && next_nonblank_char(line, pos) == '(') { tok_str = id; type = t_function; }
            else { tok_str = (opt.demangle ? demangle_id(id, true) : mangle_id(id)); type = t_id; }
        }
        else
        if (std::isdigit(ch) ||
            (ch == '.' && std::isdigit(next_ch)) ||
            (ch == '-' && (std::isdigit(next_ch) || next_ch == '.')))
        {
            bool any_digits = false;

            if (ch == '-') { ++pos; }
            while (std::isdigit(char_at(line, pos))) { ++pos; any_digits = true; }

            if (char_at(line, pos) == '.')
            {
                ++pos;
                while (std::isdigit(char_at(line, pos))) { ++pos; any_digits = true; }
            }

            // check for scientific notation
            // (TODO: merge common code with is_number function)
            if (any_digits && std::tolower(char_at(line, pos)) == 'e')
            {
                size_t pos2 = pos + 1;
                if (char_at(line, pos2) == '+' || char_at(line, pos2) == '-')
                {
                    ++pos2;
                    if (std::isdigit(char_at(line, pos2)))
                    { for (pos = pos2 + 1;std::isdigit(char_at(line, pos));++pos) { } }
                }
            }

            if (any_digits) { type = t_number; }
            else { type = t_operator; pos = tok_start + 1; }  // token is a single character ("-" or ".")

            tok_str = line.substr(tok_start, pos - tok_start);
        }
        else
        if (ch == '\"' || ch == '\'')
        {
            for (++pos;pos < len && line[pos] != ch;++pos)
            {
                // check for escape character
                if (line[pos] == '\\') { if (++pos >= len) break; }
            }
            if (pos < len) { ++pos; }
            tok_str = line.substr(tok_start, pos - tok_start);
            type = t_string;
        }
        else
        {
            pos += operator_length(line, pos);
            tok_str = line.substr(tok_start, pos - tok_start);
            type = t_operator;
            if (tok_str == "=")
            {
                found_assign_op = true;
                if (opt.test && (!test_is_raw_python || 
                    (tokens.size() > 0 && tokens[0].type == t_id && tokens[0].str == kwd_expect)))
                { tok_str = "=="; }
            }
        }

        tokens.push_back(Token(type, tok_str, num_spaces));

        if (!opt.command && tokens.size() == 1)
        {
            // Python will complain if the line is indented
            tokens[0].spaces_before = 0;
        }
    }

    transform_special_tokens(tokens, filename, line_num);

    if (opt.assign)
    {
        // transform var= into var=''
        if (tokens.size() == 2 && tokens[1].str == "=") { tokens.push_back(Token(t_string, "''", 0)); }
    }

    if (opt.test) { force_string_comparison_if_quoted(tokens); }

    if (opt.command)
    {
        // add any missing ":" at the end of the line
        if (tokens.size() > 0 && tokens[0].type == t_keyword)
        {
            // check if there is a ":" anywhere in the line
            if (find_token(tokens, t_operator, ":") == -1)
            {
                if (tokens[0].str == "if" || tokens[0].str == "else" || tokens[0].str == "elif" ||
                    tokens[0].str == "while" || tokens[0].str == "for" || tokens[0].str == "try" ||
                    tokens[0].str == "except" || tokens[0].str == "finally")
                { tokens.push_back(Token(t_operator, ":", 0)); }
            }
        }
    }
}
示例#18
0
static gboolean
handle_backspace(
	AutocloseUserData *data,
	ScintillaObject   *sci,
	gchar              ch,
	gchar             *ch_left,
	gchar             *ch_right,
	GdkEventKey       *event,
	gint               indent_width)
{
	gint pos = sci_get_current_position(sci);
	gint end_pos;
	gint line_start, line_end, line;
	gint i;
	if (!ac_info->delete_pairing_brace)
		return AC_CONTINUE_ACTION;
	ch = char_at(sci, pos - 1);

	if (!check_chars(sci, ch, ch_left, ch_right))
		return AC_CONTINUE_ACTION;

	if (event->state & GDK_SHIFT_MASK)
	{
		if ((ch_left[0] == ch || ch_right[0] == ch) &&
				ac_info->bcksp_remove_pair)
		{
			end_pos = sci_find_matching_brace(sci, pos - 1);
			if (-1 == end_pos)
				return AC_CONTINUE_ACTION;
			sci_start_undo_action(sci);
			line_start = sci_get_line_from_position(sci, pos);
			line_end = sci_get_line_from_position(sci, end_pos);
			SSM(sci, SCI_DELETERANGE, end_pos, 1);
			if (end_pos < pos)
				pos--;
			SSM(sci, SCI_DELETERANGE, pos - 1, 1);
			/* remove indentation magick */
			if (char_is_curly_bracket(ch))
			{
				if (line_start == line_end)
					goto final;
				if (line_start > line_end)
				{
					line = line_end;
					line_end = line_start;
					line_start = line;
				}
				if (blank_line(sci, line_start))
				{
					delete_line(sci, line_start);
					line_end--;
				}
				else
					line_start++;
				if (blank_line(sci, line_end))
					delete_line(sci, line_end);
				line_end--;
				/* unindent */
				for (i = line_start; i <= line_end; i++)
				{
					unindent_line(sci, i, indent_width);
				}
			}
final:
			sci_end_undo_action(sci);
			return AC_STOP_ACTION;
		}