Don't use extern for num_wished_langs

WORK IN PROGRESS -- Attempting to mend an issue where stamp descriptions
are not loading.

Also, making things safer when a problem occurs.

Using gcc 9.3.0 compiler, this was happening in 0.9.25 during
development, but also affected 0.9.24 and 0.9.23, which worked
fine under earlier versions of gcc.
This commit is contained in:
Bill Kendrick 2020-07-26 16:42:12 -07:00
parent e50a0ef84c
commit 42512a67b0
4 changed files with 48 additions and 18 deletions

View file

@ -38,6 +38,12 @@ $Id$
----
* Improved safety when copying things into string buffers.
* [WIP] Mending issue where stamp descriptions are not loading,
and making things safer when a problem occurs.
(Using gcc 9.3.0 compiler, this was happening in 0.9.25 during
development, but also affected 0.9.24 and 0.9.23, which worked
fine under earlier versions of gcc.)
2020.April.24 (0.9.24)
* New tools
---------

View file

@ -25,7 +25,7 @@
$Id$
June 14, 2002 - April 2, 2020
June 14, 2002 - July 26, 2020
*/
#include <stdio.h>
@ -239,7 +239,6 @@ int need_right_to_left;
int need_right_to_left_word;
const char *lang_prefix, *short_lang_prefix;
int num_wished_langs = 0;
w_langs wished_langs[255];
/* Mappings from human-readable language names (found in
@ -979,18 +978,17 @@ static void mysetenv(const char *name, const char *value)
* @param loc Locale
* @return The Y-nudge value for font rendering in the language.
*/
/* *INDENT-OFF* */
static int set_current_language(const char *restrict locale_choice) MUST_CHECK;
/* *INDENT-ON* */
static int set_current_language(const char *restrict loc)
static int set_current_language(const char *restrict loc, int * ptr_num_wished_langs)
{
int i;
int j = 0;
char *oldloc;
char *env_language;
char *env_language_lang;
int num_wished_langs = 0;
*ptr_num_wished_langs = 0;
if (strlen(loc) > 0)
{
@ -1140,6 +1138,8 @@ static int set_current_language(const char *restrict loc)
printf("lang_prefixes[%d] is \"%s\"\n", get_current_language(), lang_prefixes[get_current_language()]);
#endif
*ptr_num_wished_langs = num_wished_langs;
return wished_langs[0].lang_y_nudge;
}
@ -1150,11 +1150,12 @@ static int set_current_language(const char *restrict loc)
* if asked (either 'locale' or 'lang' are "help"), or if the
* given input is not recognized.
*
* @param lang Language name (or NULL)
* @param locale Locale (or NULL)
* @param char * lang Language name (or NULL)
* @param char * locale Locale (or NULL)
* @param int * a place to return the number of languages we want to use, when scanning stamp descriptions
* @return Y-nudge
*/
int setup_i18n(const char *restrict lang, const char *restrict locale)
int setup_i18n(const char *restrict lang, const char *restrict locale, int * num_wished_langs)
{
#ifdef DEBUG
printf("lang %p, locale %p\n", lang, locale);
@ -1174,7 +1175,7 @@ int setup_i18n(const char *restrict lang, const char *restrict locale)
if (lang)
locale = language_to_locale(lang);
return set_current_language(locale);
return set_current_language(locale, num_wished_langs);
}
#ifdef NO_SDLPANGO
@ -1183,6 +1184,7 @@ int setup_i18n(const char *restrict lang, const char *restrict locale)
*/
int smash_i18n(void)
{
return set_current_language("C");
int tmp;
return set_current_language("C", &tmp);
}
#endif

View file

@ -4,7 +4,7 @@
For Tux Paint
Language-related functions
Copyright (c) 2002-2012 by Bill Kendrick and others
Copyright (c) 2002-2020 by Bill Kendrick and others
bill@newbreedsoftware.com
http://www.tuxpaint.org/
@ -25,7 +25,7 @@
$Id$
June 14, 2002 - December 11, 2016
June 14, 2002 - July 26, 2020
*/
@ -188,7 +188,6 @@ extern int need_own_font;
extern int need_right_to_left; // Right-justify
extern int need_right_to_left_word; // Words need to be reversed, too! (e.g., Hebrew, but not Arabic)
extern const char *lang_prefix, *short_lang_prefix;
extern int num_wished_langs;
typedef struct w_langs
{
@ -206,7 +205,7 @@ extern w_langs wished_langs[255];
/* Function prototypes: */
int get_current_language(void);
int setup_i18n(const char *restrict lang, const char *restrict locale) MUST_CHECK;
int setup_i18n(const char *restrict lang, const char *restrict locale, int * ptr_num_wished_languages) MUST_CHECK;
#ifdef NO_SDLPANGO
int smash_i18n(void) MUST_CHECK;

View file

@ -1828,6 +1828,8 @@ static int tool_avail[NUM_TOOLS], tool_avail_bak[NUM_TOOLS];
static Uint32 cur_toggle_count;
static int num_wished_langs;
typedef struct edge_type
{
int y_upper;
@ -10985,14 +10987,26 @@ static char *loaddesc(const char *const fname, Uint8 * locale_text)
extptr = strcasestr(txt_fname, ".svg");
#endif
printf("### txt_fname = %s\n", txt_fname);
printf("### extptr = %s\n", extptr);
/*
### txt_fname = /usr/local/share/tuxpaint/stamps/animals/amphibians//frog-1.png
### extptr = .png
'/usr/local/share/tuxpaint/stamps/animals/amphibians//frog-1.png' has no locale-specific translation, using English: '01<30><31>7'
*/
if (extptr != NULL)
{
found = 0;
strcpy(def_buf, ""); /* In case of total inability to find anything */
/* Set the first available language */
for (i = 0; i < num_wished_langs && !found; i++)
{
strcpy((char *)extptr, ".txt"); /* safe; pointing into a safe spot within an existing string */
strcpy((char *)extptr, ".txt"); /* safe; pointing into a safe spot within an existing string (txt_fname) */
fprintf(stderr, "### txt_fname now '%s'\n", txt_fname);
fi = fopen(txt_fname, "r");
@ -11009,11 +11023,15 @@ static char *loaddesc(const char *const fname, Uint8 * locale_text)
do
{
fprintf(stderr, "### reading...\n");
if (fgets(buf, sizeof(buf), fi))
{
if (!feof(fi))
{
fprintf(stderr, "### READ '%s'\n", buf);
strip_trailing_whitespace(buf);
fprintf(stderr, "### TRIMMED '%s'\n", buf);
if (!got_first)
{
@ -11021,9 +11039,9 @@ static char *loaddesc(const char *const fname, Uint8 * locale_text)
strcpy(def_buf, buf); /* safe; both the same size */
got_first = 1;
fprintf(stderr, "### THAT WAS FIRST, using as default!\n");
}
debug(buf);
@ -11068,19 +11086,24 @@ static char *loaddesc(const char *const fname, Uint8 * locale_text)
if (found)
{
*locale_text = 1;
fprintf(stderr, "'%s' has a locale-specific translation, using it: '%s'\n", fname, buf + strlen(lang_prefix) + 6); /* FIXME Remove */
return (strdup(buf + (strlen(lang_prefix)) + 6));
}
else
{
/* No locale-specific translation; use the default (English) */
fprintf(stderr, "'%s' has no locale-specific translation, using English: '%s'\n", fname, def_buf); /* FIXME Remove */
return (strdup(def_buf));
}
}
else
{
fprintf(stderr, "Somehow, '%s' doesn't have a filename extension!?\n", fname);
return NULL;
}
printf("\n");
}
@ -22801,7 +22824,7 @@ static void setup_config(char *argv[])
tmpcfg.parsertmp_lang = NULL;
if (tmpcfg.parsertmp_locale == PARSE_CLOBBER)
tmpcfg.parsertmp_locale = NULL;
button_label_y_nudge = setup_i18n(tmpcfg.parsertmp_lang, tmpcfg.parsertmp_locale);
button_label_y_nudge = setup_i18n(tmpcfg.parsertmp_lang, tmpcfg.parsertmp_locale, &num_wished_langs);
/* FIXME: most of this is not required before starting the font scanner */