+char *get_x_display(void *frontend)
+{
+ return gdk_get_display();
+}
+
+static void help(FILE *fp) {
+ if(fprintf(fp,
+"pterm option summary:\n"
+"\n"
+" --display DISPLAY Specify X display to use (note '--')\n"
+" -name PREFIX Prefix when looking up resources (default: pterm)\n"
+" -fn FONT Normal text font\n"
+" -fb FONT Bold text font\n"
+" -geometry WIDTHxHEIGHT Size of terminal in characters\n"
+" -sl LINES Number of lines of scrollback\n"
+" -fg COLOUR, -bg COLOUR Foreground/background colour\n"
+" -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
+" -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
+" -T TITLE Window title\n"
+" -ut, +ut Do(default) or do not update utmp\n"
+" -ls, +ls Do(default) or do not make shell a login shell\n"
+" -sb, +sb Do(default) or do not display a scrollbar\n"
+" -log PATH Log all output to a file\n"
+" -nethack Map numeric keypad to hjklyubn direction keys\n"
+" -xrm RESOURCE-STRING Set an X resource\n"
+" -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
+ ) < 0 || fflush(fp) < 0) {
+ perror("output error");
+ exit(1);
+ }
+}
+
+int do_cmdline(int argc, char **argv, int do_everything)
+{
+ int err = 0;
+ extern char **pty_argv; /* declared in pty.c */
+
+ /*
+ * Macros to make argument handling easier. Note that because
+ * they need to call `continue', they cannot be contained in
+ * the usual do {...} while (0) wrapper to make them
+ * syntactically single statements; hence it is not legal to
+ * use one of these macros as an unbraced statement between
+ * `if' and `else'.
+ */
+#define EXPECTS_ARG { \
+ if (--argc <= 0) { \
+ err = 1; \
+ fprintf(stderr, "pterm: %s expects an argument\n", p); \
+ continue; \
+ } else \
+ val = *++argv; \
+}
+#define SECOND_PASS_ONLY { if (!do_everything) continue; }
+
+ /*
+ * TODO:
+ *
+ * finish -geometry
+ */
+
+ char *val;
+ while (--argc > 0) {
+ char *p = *++argv;
+ if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg.font, val, sizeof(cfg.font));
+ cfg.font[sizeof(cfg.font)-1] = '\0';
+
+ } else if (!strcmp(p, "-fb")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
+ cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
+
+ } else if (!strcmp(p, "-fw")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg.widefont, val, sizeof(cfg.widefont));
+ cfg.widefont[sizeof(cfg.widefont)-1] = '\0';
+
+ } else if (!strcmp(p, "-fwb")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg.wideboldfont, val, sizeof(cfg.wideboldfont));
+ cfg.wideboldfont[sizeof(cfg.wideboldfont)-1] = '\0';
+
+ } else if (!strcmp(p, "-cs")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg.line_codepage, val, sizeof(cfg.line_codepage));
+ cfg.line_codepage[sizeof(cfg.line_codepage)-1] = '\0';
+
+ } else if (!strcmp(p, "-geometry")) {
+ int flags, x, y, w, h;
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+
+ flags = XParseGeometry(val, &x, &y, &w, &h);
+ if (flags & WidthValue)
+ cfg.width = w;
+ if (flags & HeightValue)
+ cfg.height = h;
+
+ /*
+ * Apparently setting the initial window position is
+ * difficult in GTK 1.2. Not entirely sure why this
+ * should be. 2.0 has gtk_window_parse_geometry(),
+ * which would help... For the moment, though, I can't
+ * be bothered with this.
+ */
+
+ } else if (!strcmp(p, "-sl")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ cfg.savelines = atoi(val);
+
+ } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
+ !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
+ !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
+ GdkColor col;
+
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ if (!gdk_color_parse(val, &col)) {
+ err = 1;
+ fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
+ } else {
+ int index;
+ index = (!strcmp(p, "-fg") ? 0 :
+ !strcmp(p, "-bg") ? 2 :
+ !strcmp(p, "-bfg") ? 1 :
+ !strcmp(p, "-bbg") ? 3 :
+ !strcmp(p, "-cfg") ? 4 :
+ !strcmp(p, "-cbg") ? 5 : -1);
+ assert(index != -1);
+ cfg.colours[index][0] = col.red / 256;
+ cfg.colours[index][1] = col.green / 256;
+ cfg.colours[index][2] = col.blue / 256;
+ }
+
+ } else if (!strcmp(p, "-e")) {
+ /* This option swallows all further arguments. */
+ if (!do_everything)
+ break;
+
+ if (--argc > 0) {
+ int i;
+ pty_argv = smalloc((argc+1) * sizeof(char *));
+ ++argv;
+ for (i = 0; i < argc; i++)
+ pty_argv[i] = argv[i];
+ pty_argv[argc] = NULL;
+ break; /* finished command-line processing */
+ } else
+ err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
+
+ } else if (!strcmp(p, "-T")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
+ cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
+
+ } else if (!strcmp(p, "-log")) {
+ EXPECTS_ARG;
+ SECOND_PASS_ONLY;
+ strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
+ cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
+ cfg.logtype = LGTYP_DEBUG;
+
+ } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
+ SECOND_PASS_ONLY;
+ cfg.stamp_utmp = 0;
+
+ } else if (!strcmp(p, "-ut")) {
+ SECOND_PASS_ONLY;
+ cfg.stamp_utmp = 1;
+
+ } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
+ SECOND_PASS_ONLY;
+ cfg.login_shell = 0;
+
+ } else if (!strcmp(p, "-ls")) {
+ SECOND_PASS_ONLY;
+ cfg.login_shell = 1;
+
+ } else if (!strcmp(p, "-nethack")) {
+ SECOND_PASS_ONLY;
+ cfg.nethack_keypad = 1;
+
+ } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
+ SECOND_PASS_ONLY;
+ cfg.scrollbar = 0;
+
+ } else if (!strcmp(p, "-sb")) {
+ SECOND_PASS_ONLY;
+ cfg.scrollbar = 0;
+
+ } else if (!strcmp(p, "-name")) {
+ EXPECTS_ARG;
+ app_name = val;
+
+ } else if (!strcmp(p, "-xrm")) {
+ EXPECTS_ARG;
+ provide_xrm_string(val);
+
+ } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
+ help(stdout);
+ exit(0);
+
+ } else {
+ err = 1;
+ fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
+ }
+ }
+
+ return err;
+}
+
+static void block_signal(int sig, int block_it) {
+ sigset_t ss;
+
+ sigemptyset(&ss);
+ sigaddset(&ss, sig);
+ if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
+ perror("sigprocmask");
+ exit(1);
+ }
+}
+
+/*
+ * This function retrieves the character set encoding of a font. It
+ * returns the character set without the X11 hack (in case the user
+ * asks to use the font's own encoding).
+ */
+static int set_font_info(struct gui_data *inst, int fontid)
+{
+ GdkFont *font = inst->fonts[fontid];
+ XFontStruct *xfs = GDK_FONT_XFONT(font);
+ Display *disp = GDK_FONT_XDISPLAY(font);
+ Atom charset_registry, charset_encoding;
+ unsigned long registry_ret, encoding_ret;
+ int retval = CS_NONE;
+
+ charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
+ charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
+ inst->fontinfo[fontid].charset = CS_NONE;
+ inst->fontinfo[fontid].is_wide = 0;
+ if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
+ XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
+ char *reg, *enc;
+ reg = XGetAtomName(disp, (Atom)registry_ret);
+ enc = XGetAtomName(disp, (Atom)encoding_ret);
+ if (reg && enc) {
+ char *encoding = dupcat(reg, "-", enc, NULL);
+ retval = inst->fontinfo[fontid].charset =
+ charset_from_xenc(encoding);
+ /* FIXME: when libcharset supports wide encodings fix this. */
+ if (!strcasecmp(encoding, "iso10646-1")) {
+ inst->fontinfo[fontid].is_wide = 1;
+ retval = CS_UTF8;
+ }
+
+ /*
+ * Hack for X line-drawing characters: if the primary
+ * font is encoded as ISO-8859-anything, and has valid
+ * glyphs in the first 32 char positions, it is assumed
+ * that those glyphs are the VT100 line-drawing
+ * character set.
+ *
+ * Actually, we'll hack even harder by only checking
+ * position 0x19 (vertical line, VT100 linedrawing
+ * `x'). Then we can check it easily by seeing if the
+ * ascent and descent differ.
+ */
+ if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
+ int lb, rb, wid, asc, desc;
+ gchar text[2];
+
+ text[1] = '\0';
+ text[0] = '\x12';
+ gdk_string_extents(inst->fonts[fontid], text,
+ &lb, &rb, &wid, &asc, &desc);
+ if (asc != desc)
+ inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
+ }
+
+ sfree(encoding);
+ }
+ }
+
+ return retval;
+}
+