diff --git a/charset.c b/charset.c index 3198db6..f20ff5b 100644 --- a/charset.c +++ b/charset.c @@ -266,16 +266,18 @@ void charset_completion(StringArray *cs, const char *input) for (charset = first_charset; charset != NULL; charset = charset->next) { if (strxstart(charset->name, input, NULL)) add_string(cs, charset->name); - for (q = p = charset->aliases;; q++) { - if (*q == '\0' || *q == '|') { - if (q > p) { - pstrncpy(name, sizeof(name), p, q - p); - if (strxstart(name, input, NULL)) - add_string(cs, name); + if (charset->aliases) { + for (q = p = charset->aliases;; q++) { + if (*q == '\0' || *q == '|') { + if (q > p) { + pstrncpy(name, sizeof(name), p, q - p); + if (strxstart(name, input, NULL)) + add_string(cs, name); + } + if (*q == '\0') + break; + p = q + 1; } - if (*q == '\0') - break; - p = ++q; } } } diff --git a/latex-mode.c b/latex-mode.c index a2d3d5a..41251b9 100644 --- a/latex-mode.c +++ b/latex-mode.c @@ -135,6 +135,7 @@ static int latex_mode_probe(ModeProbeData *p) static int latex_mode_init(EditState *s, ModeSavedData *saved_data) { int ret; + ret = text_mode_init(s, saved_data); if (ret) return ret; diff --git a/libqhtml/css.c b/libqhtml/css.c index 317cfea..c425b2f 100644 --- a/libqhtml/css.c +++ b/libqhtml/css.c @@ -880,40 +880,87 @@ static CSSBox *add_before_after_box(CSSContext *s, return box1; } -/* NOTE: 'buf' must be large enough */ -/* XXX: find a shorter code :-) */ -static void css_to_roman(char *buf, int n) +/* Convert a number to alpha representation. + * - 'dest' must be large enough for maximum conversion + * namely INT32_MAX (2147483647) -> "FXSHRXW" 8 bytes + * INT64_MAX (9223372036854775807) -> "CRPXNLSKVLJFHG" 15 bytes + * - n should be greater than 0 + * - return 0 if conversion succeeds + * - return -1 if n not in range, dest untouched + */ +static int num_to_alpha(char *dest, int num, int upper) { - int n1, n10; - static const char roman_digits[] = "IVXLCDM"; - const char *p; - char buf1[17], *q; + char buf[16], *p; + int letter = upper ? 'A' : 'a'; - if (n <= 0 || n >= 4000) { - snprintf(buf, sizeof(buf), "%d", n); - return; + if (num <= 0) + return -1; + + p = buf + countof(buf); + *--p = '\0'; + + num--; + while (num >= 26) { + *--p = letter + (num % 26); + num /= 26; + num -= 1; } + *--p = letter + num; - p = roman_digits; - q = buf1; - while (n != 0) { - n10 = n % 10; - n1 = n10 % 5; + memcpy(dest, p, buf + countof(buf) - p); + return 0; +} + +/* Convert a number to roman numeral representation. + * - 'dest' must be large enough for maximum conversion + * namely 3888 -> "MMMDCCCLXXXVIII" 16 bytes + * - n should be in range 1..3999 + * - return 0 if conversion succeeds + * - return -1 if n not in range, dest untouched + */ +static int num_to_roman(char *dest, int n, int upper) +{ + char buf[16], *q; + const char *p; + char const digits[7 * 2] = "ivxlcdm" "IVXLCDM"; + + if (n <= 0 || n >= 4000) + return -1; + + p = digits + (upper ? 7 : 0); + q = buf + countof(buf); + *--q = '\0'; + + while (n > 0) { +#if 1 + /* Potentially shorter code :-) */ +#define R(a,b,c,d) (((a)<<6)|((b)<<4)|((c)<<2)|((d)<<0)) + int n10; + unsigned char const pat[10] = { /* 0, 1, 5, 21, 6, 2, 9, 37, 85, 7 */ + R(0,0,0,0), R(0,0,0,1), R(0,0,1,1), R(0,1,1,1), R(0,0,1,2), + R(0,0,0,2), R(0,0,2,1), R(0,2,1,1), R(2,1,1,1), R(0,0,1,3), + }; + for (n10 = pat[n % 10]; n10; n10 >>= 2) { + *--q = p[(n10 & 3) - 1]; + } +#else + int n10 = n % 10; + int n1 = n10 % 5; if (n1 == 4) { - *q++ = p[1 + (n10 == 9)]; - *q++ = p[0]; + *--q = p[1 + (n10 == 9)]; + *--q = p[0]; } else { while (n1--) - *q++ = p[0]; + *--q = p[0]; if (n10 >= 5) - *q++ = p[1]; + *--q = p[1]; } - n = n / 10; +#endif + n /= 10; p += 2; } - while (--q >= buf1) - *buf++ = *q; - *buf = '\0'; + memcpy(dest, q, buf + countof(buf) - q); + return 0; } /* if adjust is true, then always use zero base and increment if @@ -921,41 +968,48 @@ static void css_to_roman(char *buf, int n) static void css_counter_str(char *text, int text_size, int index, int list_style_type, int adjust) { + char buf[16]; + const char *p = buf; + int upper = 1; /* upper case */ + /* insert marker text */ + index += adjust; switch (list_style_type) { case CSS_LIST_STYLE_TYPE_DISC: - strcpy(text, "o"); - break; case CSS_LIST_STYLE_TYPE_CIRCLE: - strcpy(text, "o"); + p = "o"; + adjust = 0; break; case CSS_LIST_STYLE_TYPE_SQUARE: - strcpy(text, "."); + p = "."; + adjust = 0; + break; + case CSS_LIST_STYLE_TYPE_LOWER_ALPHA: + upper = 0; + /* FALL THRU */ + case CSS_LIST_STYLE_TYPE_UPPER_ALPHA: + if (num_to_alpha(buf, index, upper)) + goto decimal; + break; + case CSS_LIST_STYLE_TYPE_LOWER_ROMAN: + upper = 0; + /* FALL THRU */ + case CSS_LIST_STYLE_TYPE_UPPER_ROMAN: + if (num_to_roman(buf, index, upper)) + goto decimal; break; case CSS_LIST_STYLE_TYPE_DECIMAL: - snprintf(text, text_size, "%d", index + adjust); - goto add_dot; - case CSS_LIST_STYLE_TYPE_LOWER_ALPHA: - case CSS_LIST_STYLE_TYPE_UPPER_ALPHA: - if (index > 25) - index = 25; - text[0] = 'A' + index; - text[1] = '\0'; - goto add_dot; - case CSS_LIST_STYLE_TYPE_LOWER_ROMAN: - case CSS_LIST_STYLE_TYPE_UPPER_ROMAN: - css_to_roman(text, index + adjust); - add_dot: - if (adjust) - pstrcat(text, text_size, "."); + decimal: + snprintf(buf, sizeof(buf), "%d", index); break; default: - text[0] = '\0'; + p = ""; + adjust = 0; break; } - if (list_style_type == CSS_LIST_STYLE_TYPE_LOWER_ALPHA || - list_style_type == CSS_LIST_STYLE_TYPE_LOWER_ROMAN) - css_strtolower(text, text_size); + pstrcpy(text, text_size, p); + if (adjust) + pstrcat(text, text_size, "."); } /* add a marker or an inline box to a list item box */ diff --git a/libqhtml/cssparse.c b/libqhtml/cssparse.c index f40fb5c..734ca64 100644 --- a/libqhtml/cssparse.c +++ b/libqhtml/cssparse.c @@ -311,7 +311,7 @@ CSSProperty *css_parse_properties(CSSParseState *b, const char *props_str) goto next; p++; if (b->ignore_case) - css_strtolower(buf, sizeof(buf)); + qe_strtolower(buf, sizeof(buf), buf); args[nb_args].u.attr_id = css_new_ident(buf); unit = CSS_VALUE_ATTR; goto got_val; @@ -878,7 +878,7 @@ static void parse_simple_selector(CSSSimpleSelector *ss, CSSParseState *b, pclass = 0; read_ident(b, &ch, tag, sizeof(tag)); if (b->ignore_case) - css_strtolower(tag, sizeof(tag)); + qe_strtolower(tag, sizeof(tag), tag); /* read '.class', '[xxx]', ':pseudo-class' */ for (;;) { @@ -898,7 +898,7 @@ static void parse_simple_selector(CSSSimpleSelector *ss, CSSParseState *b, ch = bgetc(b); read_ident(b, &ch, attribute, sizeof(attribute)); if (b->ignore_case) - css_strtolower(attribute, sizeof(attribute)); + qe_strtolower(attribute, sizeof(attribute), attribute); switch (ch) { case '~': diff --git a/libqhtml/xmlparse.c b/libqhtml/xmlparse.c index 5af4a6d..88b2e2f 100644 --- a/libqhtml/xmlparse.c +++ b/libqhtml/xmlparse.c @@ -329,8 +329,7 @@ static const char *css_attr_strlower(CSSBox *box, CSSIdent attr_id) value = css_attr_str(box, attr_id); if (!value) return NULL; - pstrcpy(buf, sizeof(buf), value); - css_strtolower(buf, sizeof(buf)); + qe_strtolower(buf, sizeof(buf), value); return buf; } @@ -842,7 +841,7 @@ static int parse_tag(XMLState *s, const char *buf) } } if (s->ignore_case) - css_strtolower(tag, sizeof(tag)); + qe_strtolower(tag, sizeof(tag), tag); css_tag = css_new_ident(tag); /* XXX: should test html_syntax, but need more patches */ @@ -861,7 +860,7 @@ static int parse_tag(XMLState *s, const char *buf) break; get_str(&p, attr_name, sizeof(attr_name), "=/"); if (s->ignore_case) - css_strtolower(attr_name, sizeof(attr_name)); + qe_strtolower(attr_name, sizeof(attr_name), attr_name); if (*p == '=') { int och, ch; p++; diff --git a/list.c b/list.c index cd1fb6a..7dad604 100644 --- a/list.c +++ b/list.c @@ -93,6 +93,7 @@ ModeDef list_mode; static int list_init(void) { memcpy(&list_mode, &text_mode, sizeof(ModeDef)); + list_mode.name = "list"; list_mode.mode_probe = NULL; list_mode.mode_init = list_mode_init; list_mode.mode_close = list_mode_close; diff --git a/qe.c b/qe.c index c910ba1..d8ae037 100644 --- a/qe.c +++ b/qe.c @@ -94,9 +94,8 @@ void qe_register_mode(ModeDef *m) int size; /* lower case convert for C mode, Perl... */ - pstrcpy(buf, sizeof(buf) - 10, m->name); - css_strtolower(buf, sizeof(buf)); - pstrcat(buf, sizeof(buf) - 10, "-mode"); + qe_strtolower(buf, sizeof(buf) - 10, m->name); + pstrcat(buf, sizeof(buf), "-mode"); size = strlen(buf) + 1; /* constant immediate string parameter */ size += snprintf(buf + size, sizeof(buf) - size, @@ -1584,6 +1583,7 @@ static void edit_set_mode_file(EditState *s, ModeDef *m, s->mode->mode_close(s); qe_free(&s->mode_data); s->mode = NULL; + set_colorize_func(s, NULL); /* try to remove the raw or mode specific data if it is no longer used. */ @@ -1621,7 +1621,8 @@ static void edit_set_mode_file(EditState *s, ModeDef *m, m = &text_mode; b->data_type = &raw_data_type; } - } else if (b->data_type != m->data_type) { + } else + if (b->data_type != m->data_type) { /* non raw data type requested, but the the buffer has a different type: we cannot switch mode, so we fall back to text */ @@ -6580,7 +6581,6 @@ static int text_mode_probe(__unused__ ModeProbeData *p) int text_mode_init(EditState *s, ModeSavedData *saved_data) { - set_colorize_func(s, NULL); eb_add_callback(s->b, eb_offset_callback, &s->offset); eb_add_callback(s->b, eb_offset_callback, &s->offset_top); if (!saved_data) { @@ -6594,6 +6594,7 @@ int text_mode_init(EditState *s, ModeSavedData *saved_data) memcpy(s, saved_data->generic_data, SAVED_DATA_SIZE); } s->hex_mode = 0; + set_colorize_func(s, NULL); return 0; } diff --git a/qe.h b/qe.h index e999a78..12146b9 100644 --- a/qe.h +++ b/qe.h @@ -227,7 +227,7 @@ static inline int qe_tolower(int c) { return (qe_inrange(c, 'A', 'Z') ? c + 'a' - 'A' : c); } -void css_strtolower(char *buf, int buf_size); +void qe_strtolower(char *buf, int buf_size, const char *str); void skip_spaces(const char **pp); int strfind(const char *list, const char *s); @@ -980,7 +980,8 @@ struct EditState { EditState *next_window; }; -#define SAVED_DATA_SIZE ((int)&((EditState *)0)->end_of_saved_data) +/* Ugly patch for saving/restoring window data upon switching buffer */ +#define SAVED_DATA_SIZE offsetof(EditState, end_of_saved_data) struct DisplayState; diff --git a/util.c b/util.c index b890bad..8ddf9ff 100644 --- a/util.c +++ b/util.c @@ -390,6 +390,31 @@ void splitpath(char *dirname, int dirname_size, pstrcpy(filename, filename_size, base); } +/* Should return int, length of converted string? */ +void qe_strtolower(char *buf, int size, const char *str) +{ + int c; + + /* This version only handles ASCII */ + if (size > 0) { + while ((c = (unsigned char)*str++) != '\0' && size > 1) { + *buf++ = qe_tolower(c); + size--; + } + *buf = '\0'; + } +} + +void skip_spaces(const char **pp) +{ + const char *p; + + p = *pp; + while (qe_isspace(*p)) + p++; + *pp = p; +} + #if 0 /* find a word in a list using '|' as separator */ int strfind(const char *keytable, const char *str) @@ -425,6 +450,9 @@ int strfind(const char *list, const char *s) int c1, c2; q = list; + if (!q) + return 0; + if (*s == '\0') { /* special case the empty string: must match || in list */ while (*q) { @@ -465,8 +493,7 @@ int strcasefind(const char *list, const char *s) { char buf[128]; - pstrcpy(buf, sizeof(buf), s); - css_strtolower(buf, sizeof(buf)); + qe_strtolower(buf, sizeof(buf), s); return strfind(list, buf); } @@ -493,16 +520,6 @@ const void *memstr(const void *buf, int size, const char *str) return NULL; } -void skip_spaces(const char **pp) -{ - const char *p; - - p = *pp; - while (qe_isspace(*p)) - p++; - *pp = p; -} - /** * Return TRUE if val is a prefix of str (case independent). If it * returns TRUE, ptr is set to the next character in 'str' after the @@ -657,6 +674,8 @@ void get_str(const char **pp, char *buf, int buf_size, const char *stop) skip_spaces(pp); } +/* scans a comma separated list of entries, return index of match or -1 */ +/* CG: very similar to strfind */ int css_get_enum(const char *str, const char *enum_str) { int val, len; @@ -667,15 +686,15 @@ int css_get_enum(const char *str, const char *enum_str) len = strlen(str); for (;;) { s1 = strchr(s, ','); - if (!s1) { + if (s1) { + if (len == (s1 - s) && !memcmp(s, str, len)) + return val; + s = s1 + 1; + } else { if (!strcmp(s, str)) return val; else break; - } else { - if (len == (s1 - s) && !memcmp(s, str, len)) - return val; - s = s1 + 1; } val++; } @@ -1093,16 +1112,6 @@ void css_union_rect(CSSRect *a, const CSSRect *b) } } -void css_strtolower(char *buf, __unused__ int buf_size) -{ - int c; - - /* XXX: handle unicode / utf8 */ - while ((c = (unsigned char)*buf) != '\0') { - *buf++ = qe_tolower(c); - } -} - #ifdef __TINYC__ /* the glibc folks use wrappers, but forgot to put a compatibility