Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2004:
[Freeciv-Dev] (PR#11168) convert command-line parameter strings
Home

[Freeciv-Dev] (PR#11168) convert command-line parameter strings

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#11168) convert command-line parameter strings
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Nov 2004 15:58:43 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=11168 >

Command-line parameter strings are in the local encoding.  This patch 
has them converted to the internal encoding.  It is done inside 
get_option().  This means get_option can no longer return a static 
string so it's renamed as get_option_malloc.

One might wonder when any command-line parameter would ever be 
non-ascii.  Well, maybe it wouldn't.  However you could imagine that if 
Freeciv used UCS-4 internally this conversion would be necessary.  More 
practically it's just good to be thorough.

Note that many of these strings may just be passed off later to other 
syscalls that will need the strings to be converted BACK into the local 
encoding.  This should also be fixed.

jason

? new
? orig
Index: client/civclient.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/civclient.c,v
retrieving revision 1.205
diff -u -r1.205 civclient.c
--- client/civclient.c  22 Nov 2004 19:24:18 -0000      1.205
+++ client/civclient.c  23 Nov 2004 23:25:46 -0000
@@ -175,6 +175,7 @@
 
   init_nls();
   audio_init();
+  init_character_encodings(gui_character_encoding);
 
   /* default argument values are set in options.c */
   loglevel=LOG_NORMAL;
@@ -223,34 +224,43 @@
    } else if (is_option("--version",argv[i])) {
     fc_fprintf(stderr, "%s %s\n", freeciv_name_version(), client_string);
     exit(EXIT_SUCCESS);
-   } else if ((option = get_option("--log",argv,&i,argc))) {
-      logfile = mystrdup(option); /* never free()d */
-   } else  if ((option = get_option("--read", argv, &i, argc)))
-      scriptfile = mystrdup(option); /* never free()d */
-   else if ((option = get_option("--name",argv,&i,argc)))
+   } else if ((option = get_option_malloc("--log",argv,&i,argc))) {
+      logfile = option; /* never free()d */
+   } else  if ((option = get_option_malloc("--read", argv, &i, argc)))
+      scriptfile = option; /* never free()d */
+   else if ((option = get_option_malloc("--name",argv,&i,argc))) {
       sz_strlcpy(user_name, option);
-   else if ((option = get_option("--meta",argv,&i,argc)))
+      free(option);
+   } else if ((option = get_option_malloc("--meta",argv,&i,argc))) {
       sz_strlcpy(metaserver, option);
-   else if ((option = get_option("--Sound", argv, &i, argc)))
+      free(option);
+   } else if ((option = get_option_malloc("--Sound", argv, &i, argc))) {
       sz_strlcpy(sound_set_name, option);
-   else if ((option = get_option("--Plugin", argv, &i, argc)))
+      free(option);
+   } else if ((option = get_option_malloc("--Plugin", argv, &i, argc))) {
       sz_strlcpy(sound_plugin_name, option);
-   else if ((option = get_option("--port",argv,&i,argc))) {
+      free(option);
+   } else if ((option = get_option_malloc("--port",argv,&i,argc))) {
      if(sscanf(option, "%d", &server_port) != 1) {
+       /* FIXME: error message */
         exit(EXIT_FAILURE);
      }
-   } else if ((option = get_option("--server",argv,&i,argc)))
+     free(option);
+   } else if ((option = get_option_malloc("--server",argv,&i,argc))) {
       sz_strlcpy(server_host, option);
-   else if (is_option("--autoconnect",argv[i]))
+      free(option);
+   } else if (is_option("--autoconnect",argv[i]))
       auto_connect = TRUE;
-   else if ((option = get_option("--debug",argv,&i,argc))) {
+   else if ((option = get_option_malloc("--debug",argv,&i,argc))) {
       loglevel=log_parse_level_str(option);
       if (loglevel==-1) {
         exit(EXIT_FAILURE);
       }
-   } else if ((option = get_option("--tiles", argv, &i, argc)))
+      free(option);
+   } else if ((option = get_option_malloc("--tiles", argv, &i, argc))) {
       sz_strlcpy(tileset_name, option);
-   else if (is_option("--", argv[i])) {
+      free(option);
+   } else if (is_option("--", argv[i])) {
      ui_separator = TRUE;
    } else { 
       fc_fprintf(stderr, _("Unrecognized option: \"%s\"\n"), argv[i]);
Index: client/civclient.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/civclient.h,v
retrieving revision 1.35
diff -u -r1.35 civclient.h
--- client/civclient.h  29 Sep 2004 02:24:19 -0000      1.35
+++ client/civclient.h  23 Nov 2004 23:25:46 -0000
@@ -66,4 +66,7 @@
 void client_game_free(void);
 void ui_exit(void);
 
+/* Set in GUI code. */
+extern const char * const gui_character_encoding;
+
 #endif  /* FC__CIVCLIENT_H */
Index: client/gui-ftwl/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-ftwl/gui_main.c,v
retrieving revision 1.6
diff -u -r1.6 gui_main.c
--- client/gui-ftwl/gui_main.c  13 Nov 2004 09:26:51 -0000      1.6
+++ client/gui-ftwl/gui_main.c  23 Nov 2004 23:25:46 -0000
@@ -39,6 +39,7 @@
 
 #include "gui_main.h"
 
+const char * const gui_character_encoding = "UTF-8";
 client_option gui_options[] = {
 };
 const int num_gui_options = ARRAY_SIZE(gui_options);
@@ -178,8 +179,6 @@
   struct ct_size res;
   struct ct_size size;
 
-  init_character_encodings("ISO-8859-1");
-
   while (i < argc) {
     if (is_option("--help", argv[i])) {
       fc_fprintf(stderr, _("  -d, --dump\t\tEnable screen dumper\n"));
Index: client/gui-gtk/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gui_main.c,v
retrieving revision 1.153
diff -u -r1.153 gui_main.c
--- client/gui-gtk/gui_main.c   13 Nov 2004 09:26:51 -0000      1.153
+++ client/gui-gtk/gui_main.c   23 Nov 2004 23:25:46 -0000
@@ -124,6 +124,7 @@
 static gint timer_id;                               /*       ditto        */
 static gint gdk_input_id;
 
+const char * const gui_character_encoding = NULL;
 client_option gui_options[] = {
   GEN_BOOL_OPTION(meta_accelerators, N_("Use Alt/Meta for accelerators")),
   GEN_BOOL_OPTION(map_scrollbars, N_("Show Map Scrollbars")),
@@ -804,7 +805,7 @@
 **************************************************************************/
 void ui_init(void)
 {
-  init_character_encodings(NULL);
+
 }
 
 /**************************************************************************
Index: client/gui-gtk-2.0/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/gui_main.c,v
retrieving revision 1.93
diff -u -r1.93 gui_main.c
--- client/gui-gtk-2.0/gui_main.c       23 Nov 2004 15:44:10 -0000      1.93
+++ client/gui-gtk-2.0/gui_main.c       23 Nov 2004 23:25:48 -0000
@@ -132,6 +132,7 @@
 GtkWidget *flake_ebox;
 GtkWidget *government_ebox;
 
+const char * const gui_character_encoding = "UTF-8";
 client_option gui_options[] = {
   GEN_BOOL_OPTION(meta_accelerators,   N_("Use Alt/Meta for accelerators")),
   GEN_BOOL_OPTION(map_scrollbars,      N_("Show Map Scrollbars")),
@@ -1002,25 +1003,7 @@
 **************************************************************************/
 void ui_init(void)
 {
-  gchar *s;
-
-  init_character_encodings("UTF-8");
-
   log_set_callback(log_callback_utf8);
-
-  /* convert inputs */
-  s = g_locale_to_utf8(user_name, -1, NULL, NULL, NULL);
-  sz_strlcpy(user_name, s);
-  g_free(s);
-
-  /* this is silly, but i don't want the UI to barf on erroneous input */
-  s = g_locale_to_utf8(metaserver, -1, NULL, NULL, NULL);
-  sz_strlcpy(metaserver, s);
-  g_free(s);
-
-  s = g_locale_to_utf8(server_host, -1, NULL, NULL, NULL);
-  sz_strlcpy(server_host, s);
-  g_free(s);
 }
 
 /**************************************************************************
Index: client/gui-win32/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/gui_main.c,v
retrieving revision 1.30
diff -u -r1.30 gui_main.c
--- client/gui-win32/gui_main.c 22 Sep 2004 08:38:48 -0000      1.30
+++ client/gui-win32/gui_main.c 23 Nov 2004 23:25:49 -0000
@@ -93,6 +93,7 @@
 struct fcwin_box *main_win_box;
 struct fcwin_box *output_box;
 
+const char * const gui_character_encoding = NULL;
 client_option gui_options[] = {
   /* None. */
 };
@@ -526,7 +527,7 @@
 **************************************************************************/
 void ui_init(void)
 {
-  init_character_encodings(NULL);
+
 }
 
 /**************************************************************************
Index: client/gui-xaw/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/gui_main.c,v
retrieving revision 1.96
diff -u -r1.96 gui_main.c
--- client/gui-xaw/gui_main.c   13 Nov 2004 09:26:52 -0000      1.96
+++ client/gui-xaw/gui_main.c   23 Nov 2004 23:25:49 -0000
@@ -69,6 +69,7 @@
 
 const char *client_string = "gui-xaw";
 
+const char * const gui_character_encoding = NULL;
 client_option gui_options[] = {
   /* None. */
 };
@@ -203,7 +204,6 @@
 XtIntervalId x_interval_id;
 Atom wm_delete_window;
 
-
 #ifdef UNUSED
 /**************************************************************************
 ...
@@ -262,7 +262,7 @@
 **************************************************************************/
 void ui_init(void)
 {
-  init_character_encodings(NULL);
+
 }
 
 /**************************************************************************
Index: server/civserver.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/civserver.c,v
retrieving revision 1.225
diff -u -r1.225 civserver.c
--- server/civserver.c  19 Nov 2004 01:31:48 -0000      1.225
+++ server/civserver.c  23 Nov 2004 23:25:50 -0000
@@ -79,55 +79,61 @@
   /* yes we do have reasons ;)                                   */
   inx = 1;
   while (inx < argc) {
-    if ((option = get_option("--file", argv, &inx, argc)))
+    if ((option = get_option_malloc("--file", argv, &inx, argc))) {
       sz_strlcpy(srvarg.load_filename, option);
-    else if (is_option("--help", argv[inx])) {
+      free(option);
+    } else if (is_option("--help", argv[inx])) {
       showhelp = TRUE;
       break;
-    } else if ((option = get_option("--log", argv, &inx, argc)))
-      srvarg.log_filename = option;
-    else if ((option = get_option("--gamelog", argv, &inx, argc)))
-      srvarg.gamelog_filename = option;
-    else if (is_option("--nometa", argv[inx])) {
+    } else if ((option = get_option_malloc("--log", argv, &inx, argc))) {
+      srvarg.log_filename = option; /* Never freed. */
+    } else if ((option = get_option_malloc("--gamelog", argv, &inx, argc))) {
+      srvarg.gamelog_filename = option; /* Never freed. */
+    } else if (is_option("--nometa", argv[inx])) {
       fc_fprintf(stderr, _("Warning: the %s option is obsolete.  "
                           "Use -m to enable the metaserver.\n"), argv[inx]);
       showhelp = TRUE;
     } else if (is_option("--meta", argv[inx]))
       srvarg.metaserver_no_send = FALSE;
-    else if ((option = get_option("--Metaserver", argv, &inx, argc))) {
-      sz_strlcpy(srvarg.metaserver_addr, argv[inx]);
+    else if ((option = get_option_malloc("--Metaserver",
+                                        argv, &inx, argc))) {
+      sz_strlcpy(srvarg.metaserver_addr, option);
+      free(option);
       srvarg.metaserver_no_send = FALSE;      /* --Metaserver implies --meta */
-    } else if ((option = get_option("--port", argv, &inx, argc))) {
+    } else if ((option = get_option_malloc("--port", argv, &inx, argc))) {
       if (sscanf(option, "%d", &srvarg.port) != 1) {
        showhelp = TRUE;
        break;
       }
-    } else if ((option = get_option("--bind", argv, &inx, argc))) {
-      srvarg.bind_addr = option;
-    } else if ((option = get_option("--read", argv, &inx, argc)))
-      srvarg.script_filename = option;
-    else if ((option = get_option("--quitidle", argv, &inx, argc))) {
+      free(option);
+    } else if ((option = get_option_malloc("--bind", argv, &inx, argc))) {
+      srvarg.bind_addr = option; /* Never freed. */
+    } else if ((option = get_option_malloc("--read", argv, &inx, argc)))
+      srvarg.script_filename = option; /* Never freed. */
+    else if ((option = get_option_malloc("--quitidle", argv, &inx, argc))) {
       if (sscanf(option, "%d", &srvarg.quitidle) != 1) {
        showhelp = TRUE;
        break;
       }
+      free(option);
     } else if (is_option("--exit-on-end", argv[inx])) {
       srvarg.exit_on_end = TRUE;
-    } else if ((option = get_option("--debug", argv, &inx, argc))) {
+    } else if ((option = get_option_malloc("--debug", argv, &inx, argc))) {
       srvarg.loglevel = log_parse_level_str(option);
       if (srvarg.loglevel == -1) {
        srvarg.loglevel = LOG_NORMAL;
        showhelp = TRUE;
        break;
       }
+      free(option);
     } else if (is_option("--auth", argv[inx])) {
       srvarg.auth_enabled = TRUE;
     } else if (is_option("--Guests", argv[inx])) {
       srvarg.auth_allow_guests = TRUE;
     } else if (is_option("--Newusers", argv[inx])) {
       srvarg.auth_allow_newusers = TRUE;
-    } else if ((option = get_option("--saves", argv, &inx, argc))) {
-      srvarg.saves_pathname = option;
+    } else if ((option = get_option_malloc("--saves", argv, &inx, argc))) {
+      srvarg.saves_pathname = option; /* Never freed. */
     } else if (is_option("--version", argv[inx]))
       showvers = TRUE;
     else {
Index: utility/fciconv.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/fciconv.c,v
retrieving revision 1.10
diff -u -r1.10 fciconv.c
--- utility/fciconv.c   13 Nov 2004 09:16:59 -0000      1.10
+++ utility/fciconv.c   23 Nov 2004 23:25:50 -0000
@@ -56,7 +56,7 @@
 
   Pass an internal encoding of NULL to use the local encoding internally.
 ***************************************************************************/
-void init_character_encodings(char *my_internal_encoding)
+void init_character_encodings(const char *my_internal_encoding)
 {
 #ifdef HAVE_ICONV
   /* Set the data encoding - first check $FREECIV_DATA_ENCODING,
Index: utility/fciconv.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/fciconv.h,v
retrieving revision 1.7
diff -u -r1.7 fciconv.h
--- utility/fciconv.h   13 Nov 2004 09:16:59 -0000      1.7
+++ utility/fciconv.h   23 Nov 2004 23:25:50 -0000
@@ -19,7 +19,7 @@
 
 #define FC_DEFAULT_DATA_ENCODING "UTF-8"
 
-void init_character_encodings(char *internal_encoding);
+void init_character_encodings(const char *internal_encoding);
 
 const char *get_data_encoding(void);
 const char *get_local_encoding(void);
Index: utility/shared.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.c,v
retrieving revision 1.120
diff -u -r1.120 shared.c
--- utility/shared.c    13 Nov 2004 09:26:52 -0000      1.120
+++ utility/shared.c    23 Nov 2004 23:25:51 -0000
@@ -144,8 +144,12 @@
   *i can be increased to get next string in the array argv[].
   It is an error for the option to exist but be an empty string.
   This doesn't use freelog() because it is used before logging is set up.
+
+  The argv strings are assumed to be in the local encoding; the returned
+  string is in the internal encoding.
 **************************************************************************/
-char *get_option(const char *option_name, char **argv, int *i, int argc)
+char *get_option_malloc(const char *option_name,
+                       char **argv, int *i, int argc)
 {
   int len = strlen(option_name);
 
@@ -170,7 +174,7 @@
       }
     }
 
-    return opt;
+    return local_to_internal_string_malloc(opt);
   }
 
   return NULL;
Index: utility/shared.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.h,v
retrieving revision 1.137
diff -u -r1.137 shared.h
--- utility/shared.h    21 Oct 2004 20:27:29 -0000      1.137
+++ utility/shared.h    23 Nov 2004 23:25:51 -0000
@@ -190,7 +190,8 @@
 
 char *create_centered_string(const char *s);
 
-char * get_option(const char *option_name,char **argv,int *i,int argc);
+char *get_option_malloc(const char *option_name,
+                       char **argv, int *i, int argc);
 bool is_option(const char *option_name,char *option);
 int get_tokens(const char *str, char **tokens, size_t num_tokens,
               const char *delimiterset);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#11168) convert command-line parameter strings, Jason Short <=