+ 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);
+ }
+}
+
+static void 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;
+ 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);
+ 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;
+
+ /*
+ * 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;
+ }
+
+ /*
+ * FIXME: this is a hack. Currently fonts with
+ * incomprehensible encodings are dealt with by
+ * pretending they're 8859-1. It's ugly, but it's good
+ * enough to stop things crashing. Should do something
+ * better here.
+ */
+ if (inst->fontinfo[fontid].charset == CS_NONE)
+ inst->fontinfo[fontid].charset = CS_ISO8859_1;
+
+ sfree(encoding);
+ }
+ }
+}
+
+int main(int argc, char **argv)
+{
+ extern int pty_master_fd; /* declared in pty.c */
+ extern void pty_pre_init(void); /* declared in pty.c */
+ struct gui_data *inst;
+
+ /* defer any child exit handling until we're ready to deal with
+ * it */
+ block_signal(SIGCHLD, 1);
+
+ pty_pre_init();
+
+ gtk_init(&argc, &argv);
+
+ if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
+ exit(1);
+ do_defaults(NULL, &cfg);
+ if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
+ exit(1);
+
+ /*
+ * Create an instance structure and initialise to zeroes
+ */
+ inst = smalloc(sizeof(*inst));
+ memset(inst, 0, sizeof(*inst));
+ inst->alt_keycode = -1; /* this one needs _not_ to be zero */
+