+ }
+
+ 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;
+}
+
+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;
+ int font_charset;
+
+ /* defer any child exit handling until we're ready to deal with
+ * it */
+ block_signal(SIGCHLD, 1);
+
+ pty_pre_init();
+
+ gtk_init(&argc, &argv);
+
+ /*
+ * 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 */
+
+ if (do_cmdline(argc, argv, 0, &inst->cfg))
+ exit(1); /* pre-defaults pass to get -class */
+ do_defaults(NULL, &inst->cfg);
+ if (do_cmdline(argc, argv, 1, &inst->cfg))
+ exit(1); /* post-defaults, do everything */
+
+ inst->fonts[0] = gdk_font_load(inst->cfg.font);
+ if (!inst->fonts[0]) {
+ fprintf(stderr, "pterm: unable to load font \"%s\"\n", inst->cfg.font);
+ exit(1);
+ }
+ font_charset = set_font_info(inst, 0);
+ if (inst->cfg.boldfont[0]) {
+ inst->fonts[1] = gdk_font_load(inst->cfg.boldfont);
+ if (!inst->fonts[1]) {
+ fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
+ inst->cfg.boldfont);
+ exit(1);