+ /*
+ * 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, "--redo")) {
+ /*
+ * This is an internal option which I don't expect
+ * users to have any particular use for. The effect of
+ * --redo is that once the game has been loaded and
+ * initialised, the next move in the redo chain is
+ * replayed, and the game screen is redrawn part way
+ * through the making of the move. This is only
+ * meaningful if there _is_ a next move in the redo
+ * chain, which means in turn that this option is only
+ * useful if you're also passing a save file on the
+ * command line.
+ *
+ * This option is used by the script which generates
+ * the puzzle icons and website screenshots, and I
+ * don't imagine it's useful for anything else.
+ * (Unless, I suppose, users don't like my screenshots
+ * and want to generate their own in the same way for
+ * some repackaged version of the puzzles.)
+ */
+ if (--ac > 0) {
+ redo_proportion = atof(*++av);
+ } else {
+ fprintf(stderr, "%s: no argument supplied to '--redo'\n",
+ pname);
+ return 1;
+ }
+ } else if (doing_opts && !strcmp(p, "--screenshot")) {
+ /*
+ * Another internal option for the icon building
+ * script. This causes a screenshot of the central
+ * drawing area (i.e. not including the menu bar or
+ * status bar) to be saved to a PNG file once the
+ * window has been drawn, and then the application
+ * quits immediately.
+ */
+ if (--ac > 0) {
+ screenshot_file = *++av;
+ } else {
+ fprintf(stderr, "%s: no argument supplied to '--screenshot'\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, "--load")) {
+ argtype = ARG_SAVE;
+ } else if (doing_opts && !strcmp(p, "--game")) {
+ argtype = ARG_ID;
+ } 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;
+ }