+ /*
+ * 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, "--save")) {
+ if (--ac > 0) {
+ savefile = *++av;
+ } else {
+ fprintf(stderr, "%s: '--save' expected a filename\n",
+ pname);
+ return 1;
+ }
+ } else if (doing_opts && (!strcmp(p, "--save-suffix") ||
+ !strcmp(p, "--savesuffix"))) {
+ if (--ac > 0) {
+ savesuffix = *++av;
+ } else {
+ fprintf(stderr, "%s: '--save-suffix' expected a filename\n",
+ pname);
+ return 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;
+ }
+ }
+
+ if (*errbuf) {
+ fputs(errbuf, stderr);
+ return 1;
+ }
+
+ /*
+ * Special standalone mode for generating puzzle IDs on the
+ * command line. Useful for generating puzzles to be printed
+ * out and solved offline (for puzzles where that even makes
+ * sense - Solo, for example, is a lot more pencil-and-paper
+ * friendly than Twiddle!)
+ *
+ * Usage:
+ *
+ * <puzzle-name> --generate [<n> [<params>]]
+ *
+ * <n>, if present, is the number of puzzle IDs to generate.
+ * <params>, if present, is the same type of parameter string
+ * you would pass to the puzzle when running it in GUI mode,
+ * including optional extras such as the expansion factor in
+ * Rectangles and the difficulty level in Solo.
+ *
+ * If you specify <params>, you must also specify <n> (although
+ * you may specify it to be 1). Sorry; that was the
+ * simplest-to-parse command-line syntax I came up with.
+ */
+ if (ngenerate > 0 || print || savefile || savesuffix) {
+ int i, n = 1;
+ midend *me;
+ char *id;
+ document *doc = NULL;
+
+ n = ngenerate;
+
+ me = midend_new(NULL, &thegame, NULL, NULL);
+ i = 0;
+
+ if (savefile && !savesuffix)
+ savesuffix = "";
+ if (!savefile && savesuffix)
+ savefile = "";
+
+ if (print)
+ doc = document_new(px, py, scale);
+
+ /*
+ * In this loop, we either generate a game ID or read one
+ * from stdin depending on whether we're in generate mode;
+ * then we either write it to stdout or print it, depending
+ * on whether we're in print mode. Thus, this loop handles
+ * generate-to-stdout, print-from-stdin and generate-and-
+ * immediately-print modes.
+ *
+ * (It could also handle a copy-stdin-to-stdout mode,
+ * although there's currently no combination of options
+ * which will cause this loop to be activated in that mode.
+ * It wouldn't be _entirely_ pointless, though, because
+ * stdin could contain bare params strings or random-seed
+ * IDs, and stdout would contain nothing but fully
+ * generated descriptive game IDs.)
+ */
+ while (ngenerate == 0 || i < n) {
+ char *pstr, *err;
+
+ if (ngenerate == 0) {
+ pstr = fgetline(stdin);
+ if (!pstr)
+ break;
+ pstr[strcspn(pstr, "\r\n")] = '\0';
+ } else {
+ if (arg) {
+ pstr = snewn(strlen(arg) + 40, char);
+
+ strcpy(pstr, arg);
+ if (i > 0 && strchr(arg, '#'))
+ sprintf(pstr + strlen(pstr), "-%d", i);
+ } else
+ pstr = NULL;
+ }
+
+ if (pstr) {
+ err = midend_game_id(me, pstr);
+ if (err) {
+ fprintf(stderr, "%s: error parsing '%s': %s\n",
+ pname, pstr, err);
+ return 1;
+ }
+ }
+ sfree(pstr);
+
+ midend_new_game(me);
+
+ if (doc) {
+ err = midend_print_puzzle(me, doc, soln);
+ if (err) {
+ fprintf(stderr, "%s: error in printing: %s\n", pname, err);
+ return 1;
+ }
+ }
+ if (savefile) {
+ struct savefile_write_ctx ctx;
+ char *realname = snewn(40 + strlen(savefile) +
+ strlen(savesuffix), char);
+ sprintf(realname, "%s%d%s", savefile, i, savesuffix);
+ ctx.fp = fopen(realname, "w");
+ if (!ctx.fp) {
+ fprintf(stderr, "%s: open: %s\n", realname,
+ strerror(errno));
+ return 1;
+ }
+ sfree(realname);
+ midend_serialise(me, savefile_write, &ctx);
+ if (ctx.error) {
+ fprintf(stderr, "%s: write: %s\n", realname,
+ strerror(ctx.error));
+ return 1;
+ }
+ if (fclose(ctx.fp)) {
+ fprintf(stderr, "%s: close: %s\n", realname,
+ strerror(errno));
+ return 1;
+ }
+ }
+ if (!doc && !savefile) {
+ id = midend_get_game_id(me);
+ puts(id);
+ sfree(id);
+ }
+
+ i++;
+ }
+
+ if (doc) {
+ psdata *ps = ps_init(stdout, colour);
+ document_print(doc, ps_drawing_api(ps));
+ document_free(doc);
+ ps_free(ps);
+ }
+
+ midend_free(me);
+
+ return 0;
+ } else {
+ frontend *fe;
+
+ gtk_init(&argc, &argv);
+
+ fe = new_window(arg, argtype, &error);
+
+ if (!fe) {
+ fprintf(stderr, "%s: %s\n", pname, error);
+ return 1;
+ }
+
+ if (screenshot_file) {
+ /*
+ * Some puzzles will not redraw their entire area if
+ * given a partially completed animation, which means
+ * we must redraw now and _then_ redraw again after
+ * freezing the move timer.
+ */
+ midend_force_redraw(fe->me);
+ }
+
+ if (redo_proportion) {
+ /* Start a redo. */
+ midend_process_key(fe->me, 0, 0, 'r');
+ /* And freeze the timer at the specified position. */
+ midend_freeze_timer(fe->me, redo_proportion);
+ }
+
+ if (screenshot_file) {
+ GdkPixbuf *pb;
+ GError *gerror = NULL;
+
+ midend_redraw(fe->me);
+
+ pb = gdk_pixbuf_get_from_drawable(NULL, fe->pixmap,
+ NULL, 0, 0, 0, 0, -1, -1);
+ gdk_pixbuf_save(pb, screenshot_file, "png", &gerror, NULL);
+
+ exit(0);
+ }
+
+ gtk_main();
+ }