hex1 0[xX]{hextail}
hex2 ${hextail}
+
%%
-{hex1} { yylval.number = asciihex2int(yytext); return NUMBER; }
-{hex2} { yylval.number = asciihex2int(&yytext[1]); return NUMBER; }
+{hex1} {
+ /*
+ * No need to check return value of asciihex2int, because lex
+ * always passes a valid ASCII hex string.
+ */
+ yylval.number = asciihex2int(&yytext[2]); /* Skip "0x" prefix */
+ return NUMBER;
+ }
+{hex2} {
+ /*
+ * No need to check return value of asciihex2int, because lex
+ * always passes a valid ASCII hex string.
+ */
+ yylval.number = asciihex2int(&yytext[1]); /* Skip "$" prefix */
+ return NUMBER;
+ }
+
+
[0-9]+ { yylval.number = atoi(yytext); return NUMBER; }
[h?] return TOK_HELP;
sb return TOK_SB;
static int asciihex2int_error;
+int
+asciihex2int_get_error(void)
+{
+ return asciihex2int_error;
+}
+
/* Convert integer to ASCII hex string. */
void
int2asciihex(int val, char *str, int width)
sprintf(str , "Err");
}
-/* Convert ASCII hex string to integer. */
-int
-asciihex2int(char *str)
-{
- int val;
- int rc;
-
- rc = sscanf(str, "%X", &val);
-
- if (rc == 0) {
- log_err("ASCII to hex conversion failure");
- asciihex2int_error = true;
- }
-
- return val;
-}
-
-/* Convert an ascii string to an hexadecimal number. */
+/*
+ * Convert ASCII string in hexadecimal notation to integer, up to length
+ * characters.
+ * The string contains only [0-9] and [a-f] characters (no "0x" prefix).
+ */
static unsigned int
-asciihex2int_len(char *istring, int length)
+asciihex2int_len(char *str, int length)
{
unsigned int result = 0;
int i, ascii_code;
- if (!length || (length > (int) strlen(istring)))
- length = strlen(istring);
+ asciihex2int_error = false;
+
+ if (!length || (length > (int) strlen(str)))
+ length = strlen(str);
for (i = 0; i < length; i++) {
- ascii_code = istring[i];
+ ascii_code = str[i];
if (ascii_code > 0x39)
ascii_code &= 0xDF;
} else {
log_err("ASCII to hex conversion failure");
asciihex2int_error = true;
+ return 0;
}
}
return result;
}
+/*
+ * Convert ASCII string in hexadecimal notation to integer, up to \0 end character.
+ * The string must not contain prefix "0x", only [0-9] and [a-f] characters.
+ */
+unsigned int
+asciihex2int(char *str)
+{
+ /* Set length to zero to use full length of string. */
+ return asciihex2int_len(str, 0);
+}
+
/*
* Return value:
* true: success
#ifndef HEXFILE_H
#define HEXFILE_H 1
+int
+asciihex2int_get_error(void);
+
void
int2asciihex(int val, char *str, int width);
-int
+unsigned int
asciihex2int(char *str);
int
/* Get base address. */
gtk_tree_model_get(model, &iter, COL_ADDRESS, &str, -1);
+
+ /* No need to check error, has already been validated. */
address = asciihex2int(str);
/* Convert column number (1, 2, 3...) to index (0, 1, 2...) */
/* Convert new value (asciihex) to integer. */
new = asciihex2int(new_str);
- if ((new < 0) || (new > 255)) {
- log_info(" new value: out of range");
- new = old; /* Put back old value... */
+ if (asciihex2int_get_error()) {
+ log_warn(" new value: invalid");
+ return;
+ } else if ((new < 0) || (new > 255)) {
+ log_warn(" new value: out of range");
+ return;
} else {
log_info(" new value: $%02X", new);
}
gtk_tree_model_get(model, &iter, COL_ADDR, &str_addr, -1);
/* Convert hex address in ASCII to integer. */
+ /* No need to check error, has already been validated. */
val = asciihex2int(str_addr);
log_debug(" row address is: $%04X", val);
/* Convert new value (asciihex) to integer. */
new = asciihex2int(new_str);
- if ((new != 0) && (new != 1)) {
- log_info(" new value: out of range");
- new = old; /* Put back old value... */
+ if (asciihex2int_get_error()) {
+ log_warn(" new value: invalid");
+ return;
+ } else if ((new != 0) && (new != 1)) {
+ log_warn(" new value: out of range");
+ return;
} else {
log_info(" new value: %d", new);
}
/* Read current (old) value. */
gtk_tree_model_get(model, &iter, COL_VAL, &str, -1);
+ /* No need to check error, has already been validated. */
old = asciihex2int(str);
log_info(" old value: $%04X", old);
new = asciihex2int(new_str);
+ if (asciihex2int_get_error()) {
+ log_warn(" new value: invalid");
+ return;
+ }
+
log_info(" new value: $%04X", new);
/* Store new value in emulator register (if in range). */