Preprocessor directives and gettext - patch for pidgin-mtn/pidgin/gtkmain.c

David Mohr damailings at mcbf.net
Fri Sep 21 10:36:30 EDT 2007


On 9/20/07, Ethan Blanton <elb at pidgin.im> wrote:
> Etan Reisner spake unto us the following wisdom:
> > Thinking about this again, if we simply set opterr to 0 (so getopt stops
> > bailing on unknown arguments for us) and then check to see if anything is
> > left in argv/argc after we call gtk_init_check I *think* we can only parse
> > arguments once ourselves, let GTK+ options propagate to GTK+, and keep our
> > current die-on-unrecognized-arguments behaviour. That is of course
> > assuming that both getopt and GTK+ remove arguments that they have
> > handled, which I know is the case for GTK+ but I am unsure about for
> > getopt.
>
> getopt does not remove arguments it has handled, but it rearranges the
> argument list so that they are at the beginning, and updates 'optind'
> to point at the first unrecognized argument.  What we should be able
> to do is parse our arguments, store optind, then let Gtk+ at its
> arguments, and check that argv[optind] == NULL.

I wrote a little patch for what you suggested, and it seems to work
fine for me.
---SNIP---
--- pidgin-2.2.0.org/pidgin/gtkmain.c   2007-09-21 08:26:55.000000000 -0600
+++ pidgin-2.2.0/pidgin/gtkmain.c       2007-09-21 08:30:45.000000000 -0600
@@ -592,7 +592,10 @@
 #endif

        /* scan command-line options */
-       opterr = 1;
+
+    /* set opterr to 0 so that unknown args have a chance to get
+     * inspected by gtk_init_check */
+       opterr = 0;
        while ((opt = getopt_long(argc, argv,
 #ifndef _WIN32
                                  "c:dhmnl::s:v",
@@ -630,16 +633,13 @@
                case 'm':   /* do not ensure single instance. */
                        opt_si = FALSE;
                        break;
-        case 'D':   /* --display */
-            /* handled by gtk_init_check below */
-            break;
                case '?':       /* show terse help */
-               default:
                        show_usage(argv[0], TRUE);
 #ifdef HAVE_SIGNAL_H
                        g_free(segfault_message);
 #endif
                        return 0;
+               default:
                        break;
                }
        }
@@ -688,6 +688,17 @@
        g_free(search_path);

        gui_check = gtk_init_check(&argc, &argv);
+
+    /* getopt reorders arguments such that the recognized ones
+     * come first. gtk_init_check removes any arguments from argv
+     * that it recognized, so if everything has been parsed, then
+     * argv[optind] should be NULL */
+    if (argv[optind] != NULL) {
+        printf(_("%s: unrecognized option `%s'\n"), argv[0], argv[optind]);
+        show_usage(argv[0], TRUE);
+        return 0;
+    }
+
        if (!gui_check) {
                char *display = gdk_get_display();
---SNAP---

I don't know anything about gettext though, is it Ok to introduce
strings that need translation just in the source, or do they need to
get added anywhere else?

What I don't know though is what other command line options gtk
actually handles. Should usage(...) let users know that all standard
gtk args are accepted?

~David




More information about the Devel mailing list