+ /*
+ * Command line parsing in this function is rather fiddly,
+ * because GTK wants to have a go at argc/argv _first_ - and
+ * yet we can't let it, because gtk_init() will bomb out if it
+ * can't open an X display, whereas in fact we want to permit
+ * our --generate and --print modes to run without an X
+ * display.
+ *
+ * So what we do is:
+ * - we parse the command line ourselves, without modifying
+ * argc/argv
+ * - if we encounter an error which might plausibly be the
+ * result of a GTK command line (i.e. not detailed errors in
+ * particular options of ours) we store the error message
+ * and terminate parsing.
+ * - if we got enough out of the command line to know it
+ * specifies a non-X mode of operation, we either display
+ * the stored error and return failure, or if there is no
+ * stored error we do the non-X operation and return
+ * success.
+ * - otherwise, we go straight to gtk_init().
+ */
+
+ errbuf[0] = '\0';
+ while (--ac > 0) {
+ char *p = *++av;
+ if (doing_opts && !strcmp(p, "--version")) {
+ printf("%s, from Simon Tatham's Portable Puzzle Collection\n%s\n",
+ thegame.name, ver);
+ return 0;
+ } else if (doing_opts && !strcmp(p, "--generate")) {
+ if (--ac > 0) {
+ ngenerate = atoi(*++av);
+ if (!ngenerate) {
+ fprintf(stderr, "%s: '--generate' expected a number\n",
+ pname);
+ return 1;
+ }
+ } else
+ ngenerate = 1;
+ } else if (doing_opts && !strcmp(p, "--print")) {
+ if (!thegame.can_print) {
+ fprintf(stderr, "%s: this game does not support printing\n",
+ pname);
+ return 1;
+ }
+ print = TRUE;
+ if (--ac > 0) {
+ char *dim = *++av;
+ if (sscanf(dim, "%dx%d", &px, &py) != 2) {
+ fprintf(stderr, "%s: unable to parse argument '%s' to "
+ "'--print'\n", pname, dim);
+ return 1;
+ }
+ } else {
+ px = py = 1;
+ }
+ } else if (doing_opts && !strcmp(p, "--scale")) {
+ if (--ac > 0) {
+ scale = atof(*++av);
+ } else {
+ fprintf(stderr, "%s: no argument supplied to '--scale'\n",
+ pname);
+ return 1;
+ }
+ } else if (doing_opts && (!strcmp(p, "--with-solutions") ||
+ !strcmp(p, "--with-solution") ||
+ !strcmp(p, "--with-solns") ||
+ !strcmp(p, "--with-soln") ||
+ !strcmp(p, "--solutions") ||
+ !strcmp(p, "--solution") ||
+ !strcmp(p, "--solns") ||
+ !strcmp(p, "--soln"))) {
+ soln = TRUE;
+ } else if (doing_opts && !strcmp(p, "--colour")) {
+ if (!thegame.can_print_in_colour) {
+ fprintf(stderr, "%s: this game does not support colour"
+ " printing\n", pname);
+ return 1;
+ }
+ colour = TRUE;
+ } else if (doing_opts && !strcmp(p, "--")) {
+ doing_opts = FALSE;
+ } else if (!doing_opts || p[0] != '-') {
+ if (arg) {
+ fprintf(stderr, "%s: more than one argument supplied\n",
+ pname);
+ return 1;
+ }
+ arg = p;
+ } else {
+ sprintf(errbuf, "%.100s: unrecognised option '%.100s'\n",
+ pname, p);
+ break;
+ }