*** empty log message ***
[dana/urxvt.git] / src / xdefaults.C
1 /*--------------------------------*-C-*---------------------------------*
2  * File:        xdefaults.C
3  *----------------------------------------------------------------------*
4  *
5  * All portions of code are copyright by their respective author/s.
6  * Copyright (c) 1994      Robert Nation <nation@rocket.sanders.lockheed.com>
7  *                              - original version
8  * Copyright (c) 1997,1998 mj olesen <olesen@me.queensu.ca>
9  * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *----------------------------------------------------------------------*/
25 /*----------------------------------------------------------------------*
26  * get resources from ~/.Xdefaults or ~/.Xresources with the memory-saving
27  * default or with XGetDefault() (#define USE_XGETDEFAULT)
28  *----------------------------------------------------------------------*/
29
30 #include "../config.h"          /* NECESSARY */
31 #include "rxvt.h"               /* NECESSARY */
32 #include "version.h"
33
34 #ifdef KEYSYM_RESOURCE
35 #include "keyboard.h"
36 #endif
37
38 /* #define DEBUG_RESOURCES */
39
40 static const char *const xnames[2] = { ".Xdefaults", ".Xresources" };
41
42 /*{{{ monolithic option/resource structure: */
43 /*
44  * `string' options MUST have a usage argument
45  * `switch' and `boolean' options have no argument
46  * if there's no desc (ription), it won't appear in rxvt_usage ()
47  */
48
49 /* INFO () - descriptive information only */
50 #define INFO(opt, arg, desc)                                    \
51     {0, -1, NULL, (opt), (arg), (desc)}
52
53 /* STRG () - command-line option, with/without resource */
54 #define STRG(rsp, kw, opt, arg, desc)                           \
55     {0, (rsp), (kw), (opt), (arg), (desc)}
56
57 /* RSTRG () - resource/long-option */
58 #define RSTRG(rsp, kw, arg)                                     \
59     {0, (rsp), (kw), NULL, (arg), NULL}
60
61 /* BOOL () - regular boolean `-/+' flag */
62 #define BOOL(rsp, kw, opt, flag, desc)                          \
63     { (Opt_Boolean| (flag)), (rsp), (kw), (opt), NULL, (desc)}
64
65 /* SWCH () - `-' flag */
66 #define SWCH(opt, flag, desc)                                   \
67     { (flag), -1, NULL, (opt), NULL, (desc)}
68
69 /* convenient macros */
70 #define optList_strlen(i)                                               \
71     (optList[i].flag ? 0 : (optList[i].arg ? strlen (optList[i].arg) : 1))
72 #define optList_isBool(i)                                               \
73     (optList[i].flag & Opt_Boolean)
74 #define optList_isReverse(i)                                            \
75     (optList[i].flag & Opt_Reverse)
76 #define optList_size()                                                  \
77     (sizeof (optList) / sizeof (optList[0]))
78
79 static const struct
80   {
81     const unsigned long flag;   /* Option flag */
82     const int       doff;       /* data offset */
83     const char     *kw;         /* keyword */
84     const char     *opt;        /* option */
85     const char     *arg;        /* argument */
86     const char     *desc;       /* description */
87   }
88 optList[] = {
89               STRG (Rs_display_name, NULL, "d", NULL, NULL),    /* short form */
90               STRG (Rs_display_name, NULL, "display", "string", "X server to contact"),
91               STRG (Rs_term_name, "termName", "tn", "string", "value of the TERM environment variable"),
92               STRG (Rs_geometry, NULL, "g", NULL, NULL),        /* short form */
93               STRG (Rs_geometry, "geometry", "geometry", "geometry", "size (in characters) and position"),
94               SWCH ("C", Opt_console, "intercept console messages"),
95               SWCH ("iconic", Opt_iconic, "start iconic"),
96               SWCH ("ic", Opt_iconic, NULL),    /* short form */
97               BOOL (Rs_reverseVideo, "reverseVideo", "rv", Opt_reverseVideo, "reverse video"),
98               BOOL (Rs_loginShell, "loginShell", "ls", Opt_loginShell, "login shell"),
99               BOOL (Rs_jumpScroll, "jumpScroll", "j", Opt_jumpScroll, "jump scrolling"),
100               BOOL (Rs_pastableTabs, "pastableTabs", "ptab", Opt_pastableTabs, "tab characters are pastable"),
101 #ifdef HAVE_SCROLLBARS
102               RSTRG (Rs_scrollstyle, "scrollstyle", "mode"),
103               BOOL (Rs_scrollBar, "scrollBar", "sb", Opt_scrollBar, "scrollbar"),
104               BOOL (Rs_scrollBar_right, "scrollBar_right", "sr", Opt_scrollBar_right, "scrollbar right"),
105               BOOL (Rs_scrollBar_floating, "scrollBar_floating", "st", Opt_scrollBar_floating, "scrollbar without a trough"),
106               RSTRG (Rs_scrollBar_align, "scrollBar_align", "mode"),
107               STRG (Rs_scrollBar_thickness, "thickness", "sbt", "number", "scrollbar thickness/width in pixels"),
108 #endif
109               BOOL (Rs_scrollTtyOutput, "scrollTtyOutput", NULL, Opt_scrollTtyOutput, NULL),
110               BOOL (Rs_scrollTtyOutput, NULL, "si", Opt_Reverse | Opt_scrollTtyOutput, "scroll-on-tty-output inhibit"),
111               BOOL (Rs_scrollTtyKeypress, "scrollTtyKeypress", "sk", Opt_scrollTtyKeypress, "scroll-on-keypress"),
112               BOOL (Rs_scrollWithBuffer, "scrollWithBuffer", "sw", Opt_scrollWithBuffer, "scroll-with-buffer"),
113 #ifdef TRANSPARENT
114               BOOL (Rs_transparent, "inheritPixmap", "ip", Opt_transparent, "inherit parent pixmap"),
115               BOOL (Rs_transparent_all, "inheritPixmapforce", "ipf", Opt_transparent_all, "forcefully inherit root pixmap"),
116               SWCH ("tr", Opt_transparent, NULL),
117 #if TINTING
118               STRG (Rs_color + Color_tint, "tintColor", "tint", "color", "tint color"),
119 #endif
120 #endif
121 #ifdef OFF_FOCUS_FADING
122               STRG (Rs_fade, "fading", "fade", "%", "make colors x% darker when rxvt-unicode is losing focus."),
123 #endif
124 #ifdef TINTING
125               STRG (Rs_shade, "shading", "sh", "%", "shade background by x% when tinting."),
126 #endif
127               BOOL (Rs_utmpInhibit, "utmpInhibit", "ut", Opt_utmpInhibit, "utmp inhibit"),
128 #ifndef NO_BELL
129               BOOL (Rs_visualBell, "visualBell", "vb", Opt_visualBell, "visual bell"),
130 # if ! defined(NO_MAPALERT) && defined(MAPALERT_OPTION)
131               BOOL (Rs_mapAlert, "mapAlert", NULL, Opt_mapAlert, NULL),
132 # endif
133 #endif
134 #ifdef META8_OPTION
135               BOOL (Rs_meta8, "meta8", NULL, Opt_meta8, NULL),
136 #endif
137 #ifdef MOUSE_WHEEL
138               BOOL (Rs_mouseWheelScrollPage, "mouseWheelScrollPage", NULL, Opt_mouseWheelScrollPage, NULL),
139 #endif
140 #if ENABLE_FRILLS
141               BOOL (Rs_tripleclickwords, "tripleclickwords", "tcw", Opt_tripleclickwords, "triple click word selection"),
142               BOOL (Rs_insecure, "insecure", "insecure", Opt_insecure, "enable possibly insecure escape sequences"),
143 #endif
144 #ifdef CURSOR_BLINK
145               BOOL (Rs_cursorBlink, "cursorBlink", "bc", Opt_cursorBlink, "blinking cursor"),
146 #endif
147 #ifdef POINTER_BLANK
148               BOOL (Rs_pointerBlank, "pointerBlank", "pb", Opt_pointerBlank, "switch off pointer after delay"),
149 #endif
150               STRG (Rs_color + Color_bg, "background", "bg", "color", "background color"),
151               STRG (Rs_color + Color_fg, "foreground", "fg", "color", "foreground color"),
152               RSTRG (Rs_color + minCOLOR + 0, "color0", "color"),
153               RSTRG (Rs_color + minCOLOR + 1, "color1", "color"),
154               RSTRG (Rs_color + minCOLOR + 2, "color2", "color"),
155               RSTRG (Rs_color + minCOLOR + 3, "color3", "color"),
156               RSTRG (Rs_color + minCOLOR + 4, "color4", "color"),
157               RSTRG (Rs_color + minCOLOR + 5, "color5", "color"),
158               RSTRG (Rs_color + minCOLOR + 6, "color6", "color"),
159               RSTRG (Rs_color + minCOLOR + 7, "color7", "color"),
160 #ifndef NO_BRIGHTCOLOR
161               RSTRG (Rs_color + minBrightCOLOR + 0, "color8", "color"),
162               RSTRG (Rs_color + minBrightCOLOR + 1, "color9", "color"),
163               RSTRG (Rs_color + minBrightCOLOR + 2, "color10", "color"),
164               RSTRG (Rs_color + minBrightCOLOR + 3, "color11", "color"),
165               RSTRG (Rs_color + minBrightCOLOR + 4, "color12", "color"),
166               RSTRG (Rs_color + minBrightCOLOR + 5, "color13", "color"),
167               RSTRG (Rs_color + minBrightCOLOR + 6, "color14", "color"),
168               RSTRG (Rs_color + minBrightCOLOR + 7, "color15", "color"),
169 #endif                          /* NO_BRIGHTCOLOR */
170 #ifndef NO_BOLD_UNDERLINE_REVERSE
171               RSTRG (Rs_color + Color_BD, "colorBD", "color"),
172               RSTRG (Rs_color + Color_IT, "colorIT", "color"),
173               RSTRG (Rs_color + Color_UL, "colorUL", "color"),
174               RSTRG (Rs_color + Color_RV, "colorRV", "color"),
175 #endif                          /* ! NO_BOLD_UNDERLINE_REVERSE */
176 #if ENABLE_FRILLS
177               RSTRG (Rs_color + Color_underline, "underlineColor", "color"),
178 #endif
179 #ifdef KEEP_SCROLLCOLOR
180               RSTRG (Rs_color + Color_scroll, "scrollColor", "color"),
181               RSTRG (Rs_color + Color_trough, "troughColor", "color"),
182 #endif                          /* KEEP_SCROLLCOLOR */
183 #ifdef OPTION_HC
184               STRG (Rs_color + Color_HC, "highlightColor", "hc", "color", "highlight color"),
185 #endif
186 #ifndef NO_CURSORCOLOR
187               STRG (Rs_color + Color_cursor, "cursorColor", "cr", "color", "cursor color"),
188               /* command-line option = resource name */
189               RSTRG (Rs_color + Color_cursor2, "cursorColor2", "color"),
190 #endif                          /* NO_CURSORCOLOR */
191               STRG (Rs_color + Color_pointer_fg, "pointerColor", "pr", "color", "pointer color"),
192               STRG (Rs_color + Color_pointer_bg, "pointerColor2", "pr2", "color", "pointer bg color"),
193               STRG (Rs_color + Color_border, "borderColor", "bd", "color", "border color"),
194 #if defined (XPM_BACKGROUND) || (MENUBAR_MAX)
195               RSTRG (Rs_path, "path", "search path"),
196 #endif                          /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */
197 #ifdef XPM_BACKGROUND
198               STRG (Rs_backgroundPixmap, "backgroundPixmap", "pixmap", "file[;geom]", "background pixmap"),
199 #endif                          /* XPM_BACKGROUND */
200 #if (MENUBAR_MAX)
201               RSTRG (Rs_menu, "menu", "name[;tag]"),
202 #endif
203               /* fonts: command-line option = resource name */
204               STRG (Rs_font, "font", "fn", "fontname", "normal text font"),
205 #if ENABLE_STYLES
206               STRG (Rs_boldFont, "boldFont", "fb", "fontname", "bold font"),
207               STRG (Rs_italicFont, "italicFont", "fi", "fontname", "italic font"),
208               STRG (Rs_boldItalicFont, "boldItalicFont", "fbi", "fontname", "bold italic font"),
209 #endif
210 #ifdef USE_XIM
211               STRG (Rs_inputMethod, "inputMethod", "im", "name", "name of input method"),
212               STRG (Rs_preeditType, "preeditType", "pt", "style", "input style: style = OverTheSpot|OffTheSpot|Root"),
213               STRG (Rs_imLocale, "imLocale", "imlocale", "string", "locale to use for input method"),
214               STRG (Rs_imFont, "imFont", "imfont", "fontname", "fontset for styles OverTheSpot and OffTheSpot"),
215 #endif                          /* USE_XIM */
216               STRG (Rs_name, NULL, "name", "string", "client instance, icon, and title strings"),
217               STRG (Rs_title, "title", "title", "string", "title name for window"),
218               STRG (Rs_title, NULL, "T", NULL, NULL),   /* short form */
219               STRG (Rs_iconName, "iconName", "n", "string", "icon name for window"),
220               STRG (Rs_saveLines, "saveLines", "sl", "number", "number of scrolled lines to save"),
221 #if ENABLE_FRILLS
222               STRG (Rs_ext_bwidth, "externalBorder", "w", "number", "external border in pixels"),
223               STRG (Rs_ext_bwidth, NULL, "bw", NULL, NULL),
224               STRG (Rs_ext_bwidth, NULL, "borderwidth", NULL, NULL),
225               STRG (Rs_int_bwidth, "internalBorder", "b", "number", "internal border in pixels"),
226               BOOL (Rs_borderLess, "borderLess", "bl", Opt_borderLess, "borderless window"),
227               STRG (Rs_lineSpace, "lineSpace", "lsp", "number", "number of extra pixels between rows"),
228 #endif
229 #ifdef POINTER_BLANK
230               RSTRG (Rs_pointerBlankDelay, "pointerBlankDelay", "number"),
231 #endif
232 #ifndef NO_BACKSPACE_KEY
233               RSTRG (Rs_backspace_key, "backspacekey", "string"),
234 #endif
235 #ifndef NO_DELETE_KEY
236               RSTRG (Rs_delete_key, "deletekey", "string"),
237 #endif
238               RSTRG (Rs_selectstyle, "selectstyle", "mode"),
239 #ifdef PRINTPIPE
240               RSTRG (Rs_print_pipe, "print-pipe", "string"),
241 #endif
242               STRG (Rs_modifier, "modifier", "mod", "modifier", "meta modifier = alt|meta|hyper|super|mod1|...|mod5"),
243 #ifdef CUTCHAR_RESOURCE
244               RSTRG (Rs_cutchars, "cutchars", "string"),
245 #endif                          /* CUTCHAR_RESOURCE */
246               RSTRG (Rs_answerbackstring, "answerbackString", "string"),
247 #ifndef NO_SECONDARY_SCREEN
248               BOOL (Rs_secondaryScreen, "secondaryScreen", "ssc", Opt_secondaryScreen, "enable secondary screen"),
249               BOOL (Rs_secondaryScroll, "secondaryScroll", "ssr", Opt_secondaryScroll, "enable secondary screen scroll"),
250 #endif
251 #if !defined(NO_RESOURCES) && defined(USE_XGETDEFAULT)
252               INFO ("xrm", "string", "X resource"),
253 #endif
254               INFO ("e", "command arg ...", "command to execute")
255             };
256
257 #undef INFO
258 #undef STRG
259 #undef RSTRG
260 #undef SWCH
261 #undef BOOL
262 /*}}} */
263
264 static const char releasestring[] = "rxvt-unicode (" RXVTNAME ") v" VERSION " - released: " DATE "\n";
265 static const char optionsstring[] = "options: "
266 #if XFT
267                                     "xft,"
268 #endif
269 #if ENABLE_STYLES
270                                     "styles,"
271 #endif
272 #if ENABLE_COMBINING
273                                     "combining,"
274 #endif
275 #if TEXT_BLINK
276                                     "blink,"
277 #endif
278 #if ISO_14755
279                                     "iso14755,"
280 #endif
281 #if UNICODE_3
282                                     "unicode3,"
283 #endif
284                                     "encodings=eu+vn"
285 #if ENCODING_JP
286                                     "+jp"
287 #endif
288 #if ENCODING_JP_EXT
289                                     "+jp-ext"
290 #endif
291 #if ENCODING_KR
292                                     "+kr"
293 #endif
294 #if ENCODING_ZH
295                                     "+zh"
296 #endif
297 #if ENCODING_ZH_EXT
298                                     "+zh-ext"
299 #endif
300                                     ","
301 #if OFF_FOCUS_FADING
302                                     "fade,"
303 #endif
304 #if defined(XPM_BACKGROUND)
305                                     "XPM,"
306 #endif
307 #if defined(TRANSPARENT)
308                                     "transparent,"
309 #endif
310 #if TINTING
311                                     "tint,"
312 #endif
313 #if defined(UTMP_SUPPORT)
314                                     "utmp,"
315 #endif
316 #if defined(MENUBAR)
317                                     "menubar,"
318 #endif
319 #if defined(USE_XIM)
320                                     "XIM,"
321 #endif
322                                     "scrollbars="
323 #if !defined(HAVE_SCROLLBARS)
324                                     "NONE"
325 #else
326 # if defined(PLAIN_SCROLLBAR)
327                                     "plain"
328 #  if defined(RXVT_SCROLLBAR) || defined(NEXT_SCROLLBAR) || defined(XTERM_SCROLLBAR)
329                                     "+"
330 #  endif
331 # endif
332 # if defined(RXVT_SCROLLBAR)
333                                     "rxvt"
334 #  if defined(NEXT_SCROLLBAR) || defined(XTERM_SCROLLBAR)
335                                     "+"
336 #  endif
337 # endif
338 # if defined(NEXT_SCROLLBAR)
339                                     "NeXT"
340 #  if defined(XTERM_SCROLLBAR)
341                                     "+"
342 #  endif
343 # endif
344 # if defined(XTERM_SCROLLBAR)
345                                     "xterm"
346 # endif
347 #endif
348                                     ","
349 #if defined(NO_BACKSPACE_KEY)
350                                     "no_backspace,"
351 #endif
352 #if defined(NO_DELETE_KEY)
353                                     "no_delete,"
354 #endif
355 #if EIGHT_BIT_CONTROLS
356                                     "8bitctrls,"
357 #endif
358 #if !defined(NO_STRINGS)
359                                     "strings,"
360 #endif
361 #if defined(ENABLE_FRILLS)
362                                     "frills,"
363 #endif
364 #if defined(PREFER_24BIT)
365                                     "24bit,"
366 #endif
367 #if defined(SELECTION_SCROLLING)
368                                     "selectionscrolling,"
369 #endif
370 #if MOUSE_WHEEL
371                                     "wheel,"
372 #endif
373 #if MOUSE_SLIP_WHEELING
374                                     "slipwheel,"
375 #endif
376 #if defined(SMART_RESIZE)
377                                     "smart-resize,"
378 #endif
379 #if defined(CURSOR_BLINK)
380                                     "cursorBlink,"
381 #endif
382 #if defined(POINTER_BLANK)
383                                     "pointerBlank,"
384 #endif
385 #if defined(NO_RESOURCES)
386                                     "NoResources"
387 #else
388 # if defined(USE_XGETDEFAULT)
389                                     "XGetDefaults"
390 # else
391                                     ".Xdefaults"
392 # endif
393 #endif
394                                     "\nUsage: ";                /* Usage */
395
396 #define INDENT 18
397
398 /*{{{ usage: */
399 /*----------------------------------------------------------------------*/
400 static void
401 rxvt_usage (int type)
402 {
403   unsigned int i, col;
404
405   rxvt_log ("%s%s%s", releasestring, optionsstring, RESNAME);
406
407   switch (type)
408     {
409       case 0:                   /* brief listing */
410         rxvt_log (" [-help] [--help]\n");
411
412         for (col = 1, i = 0; i < optList_size (); i++)
413           if (optList[i].desc != NULL)
414             {
415               int             len = 0;
416
417               if (!optList_isBool (i))
418                 {
419                   len = optList_strlen (i);
420                   if (len > 0)
421                     len++;      /* account for space */
422                 }
423 #ifdef DEBUG_STRICT
424               assert (optList[i].opt != NULL);
425 #endif
426               len += 4 + strlen (optList[i].opt) + (optList_isBool (i) ? 2: 0);
427               col += len;
428               if (col > 79)
429                 {       /* assume regular width */
430                   rxvt_log ("\n");
431                   col = 1 + len;
432                 }
433
434               rxvt_log (" [-%s%s", (optList_isBool (i) ? "/+" : ""), optList[i].opt);
435               if (optList_strlen (i))
436                 rxvt_log (" %s]", optList[i].arg);
437               else
438                 rxvt_log ("]");
439             }
440         break;
441
442       case 1:                   /* full command-line listing */
443         rxvt_log (" [options] [-e command args]\n\nwhere options include:\n");
444
445         for (i = 0; i < optList_size (); i++)
446           if (optList[i].desc != NULL)
447             {
448 #ifdef DEBUG_STRICT
449               assert (optList[i].opt != NULL);
450 #endif
451               rxvt_log ("  %s%s %-*s%s%s\n",
452                          (optList_isBool (i) ? "-/+" : "-"), optList[i].opt,
453                          (INDENT - strlen (optList[i].opt)
454                           + (optList_isBool (i) ? 0 : 2)),
455                          (optList[i].arg ? optList[i].arg : ""),
456                          (optList_isBool (i) ? "turn on/off " : ""),
457                          optList[i].desc);
458             }
459         rxvt_log ("\n  --help to list long-options");
460         break;
461
462       case 2:                   /* full resource listing */
463         rxvt_log (" [options] [-e command args]\n\n"
464                    "where resources (long-options) include:\n");
465
466         for (i = 0; i < optList_size (); i++)
467           if (optList[i].kw != NULL)
468             rxvt_log ("  %s: %*s%s\n",
469                     optList[i].kw,
470                     (INDENT - strlen (optList[i].kw)), "", /* XXX */
471                     (optList_isBool (i) ? "boolean" : optList[i].arg));
472 #ifdef KEYSYM_RESOURCE
473         rxvt_log ("  " "keysym.sym" ": %*s%s\n",
474                 (INDENT - sizeof ("keysym.sym") + 1), "", /* XXX */
475                 "keysym");
476 #endif
477         rxvt_log ("\n  -help to list options");
478         break;
479     }
480
481   rxvt_log ("\n\n");
482   rxvt_exit_failure ();
483   /* NOTREACHED */
484 }
485
486 /*}}} */
487
488 /*{{{ get command-line options before getting resources */
489 void
490 rxvt_term::get_options (int argc, const char *const *argv)
491 {
492   int             i, bad_option = 0;
493   static const char On[3] = "ON", Off[4] = "OFF";
494
495   for (i = 1; i < argc; i++)
496     {
497       unsigned int    entry, longopt = 0;
498       const char     *flag, *opt;
499
500       opt = argv[i];
501 #ifdef DEBUG_RESOURCES
502       fprintf (stderr, "argv[%d] = %s: ", i, opt);
503 #endif
504       if (*opt == '-')
505         {
506           flag = On;
507           if (*++opt == '-')
508             longopt = *opt++;   /* long option */
509         }
510       else if (*opt == '+')
511         {
512           flag = Off;
513           if (*++opt == '+')
514             longopt = *opt++;   /* long option */
515         }
516       else
517         {
518           bad_option = 1;
519           rxvt_warn ("\"%s\": malformed option.\n", opt);
520           continue;
521         }
522
523       if (!strcmp (opt, "help"))
524         rxvt_usage (longopt ? 2 : 1);
525       if (!strcmp (opt, "h"))
526         rxvt_usage (0);
527
528       /* feature: always try to match long-options */
529       for (entry = 0; entry < optList_size (); entry++)
530         if ((optList[entry].kw && !strcmp (opt, optList[entry].kw))
531             || (!longopt
532                 && optList[entry].opt && !strcmp (opt, optList[entry].opt)))
533           break;
534
535       if (entry < optList_size ())
536         {
537           if (optList_isReverse (entry))
538             flag = flag == On ? Off : On;
539
540           if (optList_strlen (entry))
541             {
542               /*
543                * special cases are handled in main.c:main () to allow
544                * X resources to set these values before we settle for
545                * default values
546                */
547
548               if (optList[entry].doff != -1)
549                 rs[optList[entry].doff] = flag == On && argv[i+1]
550                                           ? argv[++i] : 0;
551             }
552           else
553             {           /* boolean value */
554 #ifdef DEBUG_RESOURCES
555               fprintf (stderr, "boolean (%s,%s) = %s\n",
556                       optList[entry].opt, optList[entry].kw, flag);
557 #endif
558               if (flag == On)
559                 options |= (optList[entry].flag);
560               else
561                 options &= ~ (optList[entry].flag);
562
563               if (optList[entry].doff != -1)
564                 rs[optList[entry].doff] = flag;
565             }
566         }
567       else
568 #ifdef KEYSYM_RESOURCE
569         /* if (!strncmp (opt, "keysym.", sizeof ("keysym.") - 1)) */
570         if (rxvt_Str_match (opt, "keysym."))
571           {
572             const char *str = argv[++i];
573
574             if (str != NULL)
575               parse_keysym (opt + sizeof ("keysym.") - 1, str);
576           }
577         else
578 #endif
579           {
580             bad_option = 1;
581             rxvt_warn ("\"%s\": unknown or malformed option.\n", opt);
582           }
583     }
584
585   if (bad_option)
586     rxvt_usage (0);
587 }
588
589 /*}}} */
590
591 #ifndef NO_RESOURCES
592 /*----------------------------------------------------------------------*/
593
594 # ifdef KEYSYM_RESOURCE
595 /*
596  * Define key from XrmEnumerateDatabase.
597  *   quarks will be something like
598  *      "rxvt" "keysym" "0xFF01"
599  *   value will be a string
600  */
601 /* ARGSUSED */
602 int
603 rxvt_define_key (XrmDatabase *database __attribute__((unused)),
604                  XrmBindingList bindings __attribute__((unused)),
605                  XrmQuarkList quarks,
606                  XrmRepresentation *type __attribute__((unused)),
607                  XrmValue *value,
608                  XPointer closure __attribute__((unused)))
609 {
610   int last;
611
612   for (last = 0; quarks[last] != NULLQUARK; last++)     /* look for last quark in list */
613     ;
614
615   last--;
616   GET_R->parse_keysym (XrmQuarkToString (quarks[last]), (char *)value->addr);//D//TODO
617   return False;
618 }
619
620 /*
621  * look for something like this (XK_Delete)
622  * rxvt*keysym.0xFFFF: "\177"
623  *
624  * arg will be
625  *      NULL for ~/.Xdefaults and
626  *      non-NULL for command-line options (need to allocate)
627  */
628 #define NEWARGLIM       500     /* `reasonable' size */
629
630 struct keysym_vocabulary_t
631 {
632   const char    *name;
633   unsigned short len;
634   unsigned short value;
635 };
636 keysym_vocabulary_t keysym_vocabulary[] =
637 {
638   { "ISOLevel3", 9, Level3Mask    },
639   { "AppKeypad", 9, AppKeypadMask },
640   { "Control",   7, ControlMask   },
641   { "NumLock",   7, NumLockMask   },
642   { "Shift",     5, ShiftMask     },
643   { "Meta",      4, MetaMask      },
644   { "Lock",      4, LockMask      },
645   { "Mod1",      4, Mod1Mask      },
646   { "Mod2",      4, Mod2Mask      },
647   { "Mod3",      4, Mod3Mask      },
648   { "Mod4",      4, Mod4Mask      },
649   { "Mod5",      4, Mod5Mask      },
650   { "I",         1, Level3Mask    },
651   { "K",         1, AppKeypadMask },
652   { "C",         1, ControlMask   },
653   { "N",         1, NumLockMask   },
654   { "S",         1, ShiftMask     },
655   { "M",         1, MetaMask      },
656   { "A",         1, MetaMask      },
657   { "L",         1, LockMask      },
658   { "1",         1, Mod1Mask      },
659   { "2",         1, Mod2Mask      },
660   { "3",         1, Mod3Mask      },
661   { "4",         1, Mod4Mask      },
662   { "5",         1, Mod5Mask      },
663 };
664
665 int
666 rxvt_term::parse_keysym (const char *str, const char *arg)
667 {
668   int n, sym;
669   unsigned int state = 0;
670   const char *pmodend = NULL;
671   char *newarg = NULL;
672   char newargstr[NEWARGLIM];
673
674   if (arg == NULL)
675     {
676       if ((n = rxvt_Str_match (str, "keysym.")) == 0)
677         return 0;
678
679       str += n;         /* skip `keysym.' */
680       if ((pmodend = strchr (str, ':')) < str)
681         return -1;
682     }
683   else
684     pmodend = str + strlen(str);
685
686   for (--pmodend; str < pmodend; --pmodend)
687     if (*pmodend == '-')
688       break;
689
690   while (str < pmodend)
691     {
692       unsigned int i;
693
694       for (i=0; i < sizeof (keysym_vocabulary) / sizeof (keysym_vocabulary_t); ++i)
695         {
696           if (strncmp (str, keysym_vocabulary [i].name, keysym_vocabulary [i].len) == 0)
697             {
698               state |= keysym_vocabulary[i].value;
699               str += keysym_vocabulary[i].len;
700               break;
701             }
702         }
703
704       if (i >= sizeof (keysym_vocabulary) / sizeof (keysym_vocabulary_t))
705         return -1;
706
707       if (*str == '-')
708         ++str;
709     }
710
711   /* some scanf () have trouble with a 0x prefix */
712   if (str[0] == '0' && toupper (str[1]) == 'X')
713     {
714       str += 2;
715
716       if (arg)
717         {
718           if (sscanf (str, (strchr (str, ':') ? "%x:" : "%x"), &sym) != 1)
719             return -1;
720         }
721       else
722         {
723           if (sscanf (str, "%x:", &sym) != 1)
724             return -1;
725
726           /* cue to ':', it's there since sscanf () worked */
727           strncpy (newargstr, strchr (str, ':') + 1, NEWARGLIM - 1);
728           newargstr[NEWARGLIM - 1] = '\0';
729           newarg = newargstr;
730         }
731     }
732   else
733     {
734       /*
735        * convert keysym name to keysym number
736        */
737       strncpy (newargstr, str, NEWARGLIM - 1);
738       newargstr[NEWARGLIM - 1] = '\0';
739
740       if (arg == NULL)
741         {
742           if ((newarg = strchr (newargstr, ':')) == NULL)
743             return -1;
744
745           *newarg++ = '\0';     /* terminate keysym name */
746         }
747
748       if ((sym = XStringToKeysym (newargstr)) == None)
749         return -1;
750     }
751
752   if (newarg == NULL)
753     {
754       strncpy (newargstr, arg, NEWARGLIM - 1);
755       newargstr[NEWARGLIM - 1] = '\0';
756       newarg = newargstr;
757     }
758
759   rxvt_Str_trim (newarg);
760   if (*newarg == '\0' || (n = rxvt_Str_escaped (newarg)) == 0)
761     return -1;
762
763   keyboard->register_user_translation (sym, state, newarg);
764   return 1;
765 }
766
767 # endif                         /* KEYSYM_RESOURCE */
768
769 # ifndef USE_XGETDEFAULT
770 /*{{{ rxvt_get_xdefaults () */
771 /*
772  * the matching algorithm used for memory-save fake resources
773  */
774 void
775 rxvt_term::get_xdefaults (FILE *stream, const char *name)
776 {
777   unsigned int len;
778   char *str, buffer[256];
779
780   if (stream == NULL)
781     return;
782
783   len = strlen (name);
784   while ((str = fgets (buffer, sizeof (buffer), stream)) != NULL)
785     {
786       unsigned int    entry, n;
787
788       while (*str && isspace (*str))
789         str++;          /* leading whitespace */
790
791       if ((str[len] != '*' && str[len] != '.')
792           || (len && strncmp (str, name, len)))
793         continue;
794       str += (len + 1); /* skip `name*' or `name.' */
795
796 # ifdef KEYSYM_RESOURCE
797       if (!parse_keysym (str, NULL))
798 # endif                         /* KEYSYM_RESOURCE */
799         for (entry = 0; entry < optList_size (); entry++)
800           {
801             const char *kw = optList[entry].kw;
802
803             if (kw == NULL)
804               continue;
805
806             n = strlen (kw);
807             if (str[n] == ':' && rxvt_Str_match (str, kw))
808               {
809                 /* skip `keyword:' */
810                 str += n + 1;
811                 rxvt_Str_trim (str);
812                 n = strlen (str);
813                 if (n && rs[optList[entry].doff] == NULL)
814                   {
815                     /* not already set */
816                     int s;
817                     char *p = 0;
818
819                     for (int o = 0;;)
820                       {
821                         p = (char *)rxvt_realloc (p, o + n + 1);
822                         memcpy (p + o, str, n);
823                         o += n;
824                         p[o] = 0;
825
826                         if (o == 0 || p[o - 1] != '\\') // continuation line
827                           break;
828
829                         o--; // eat "\"
830
831                         if ((str = fgets (buffer, sizeof (buffer), stream)) == NULL)
832                           break;
833
834                         rxvt_Str_trim (str);
835                         n = strlen (str);
836                       }
837
838                     rs[optList[entry].doff] = p;
839                     allocated.push_back (p);
840
841                     if (optList_isBool (entry))
842                       {
843                         s = strcasecmp (str, "TRUE") == 0
844                             || strcasecmp (str, "YES") == 0
845                             || strcasecmp (str, "ON") == 0
846                             || strcasecmp (str, "1") == 0;
847
848                         if (optList_isReverse (entry))
849                           s = !s;
850
851                         if (s)
852                           options |= optList[entry].flag;
853                         else
854                           options &= ~optList[entry].flag;
855                       }
856                   }
857
858                 break;
859               }
860           }
861     }
862
863   rewind (stream);
864 }
865
866 /*}}} */
867 # endif                         /* ! USE_XGETDEFAULT */
868 #endif                          /* NO_RESOURCES */
869
870 /*{{{ read the resources files */
871 /*
872  * using XGetDefault () or the hand-rolled replacement
873  */
874 /* ARGSUSED */
875 void
876 rxvt_term::extract_resources (Display *display __attribute__ ((unused)), const char *name)
877 {
878 #ifndef NO_RESOURCES
879
880 # if defined XAPPLOADDIR
881 #  if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
882   /* Compute the path of the possibly available localized Rxvt file */
883   char *localepath = NULL;
884
885   if (locale != NULL)
886     {   /* XXX: must limit length of string */
887       localepath = (char *)rxvt_malloc (256);
888       sprintf (localepath, XAPPLOADDIRLOCALE "/" RESCLASS,
889               (int) (258 - sizeof (XAPPLOADDIRLOCALE) - sizeof (RESCLASS)),
890               locale);  /* 258 = 255 + 4 (-.*s) - 1 (/) */
891     }
892
893   {
894 #  endif
895 # endif
896
897 # ifdef USE_XGETDEFAULT
898     /*
899      * get resources using the X library function
900      */
901     int entry;
902
903 #  ifdef XrmEnumOneLevel
904     int i;
905     char *displayResource, *xe;
906     XrmName name_prefix[3];
907     XrmClass class_prefix[3];
908     XrmDatabase database, rdb1;
909     char fname[1024];
910
911     XrmInitialize ();
912     database = NULL;
913
914     /* Get any Xserver defaults */
915
916     displayResource = XResourceManagerString (display);
917     if (displayResource != NULL)
918       database = XrmGetStringDatabase (displayResource);
919
920 #   ifdef HAVE_EXTRA_XRESOURCE_FILES
921     /* Add in ~/.Xdefaults or ~/.Xresources */
922     {
923       char *ptr;
924
925       if ((ptr = (char *)getenv ("HOME")) == NULL)
926         ptr = ".";
927
928       for (i = 0; i < (sizeof (xnames) / sizeof (xnames[0])); i++)
929         {
930           sprintf (fname, "%-.*s/%s", sizeof (fname) - strlen (xnames[i]) - 2,
931                   ptr, xnames[i]);
932           if ((rdb1 = XrmGetFileDatabase (fname)))
933             {
934               XrmMergeDatabases (rdb1, &database);
935 #    ifndef HAVE_BOTH_XRESOURCE_FILES
936               break;
937 #    endif
938             }
939         }
940     }
941 #   endif
942
943     /* Add in XENVIRONMENT file */
944
945     if ((xe = (char *)getenv ("XENVIRONMENT")) != NULL
946         && (rdb1 = XrmGetFileDatabase (xe)) != NULL)
947       XrmMergeDatabases (rdb1, &database);
948
949     /* Add in Rxvt file */
950 #   if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
951     if (localepath == NULL || (rdb1 = XrmGetFileDatabase (localepath)) == NULL)
952 #   endif
953       rdb1 = XrmGetFileDatabase (XAPPLOADDIR "/" RESCLASS);
954
955     if (rdb1 != NULL)
956       XrmMergeDatabases (rdb1, &database);
957
958     /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
959     if ((xe = (char *)getenv ("XAPPLRESDIR")) != NULL)
960       {
961         sprintf (fname, "%-.*s/" RESCLASS, sizeof (fname)
962                 - sizeof (RESCLASS) - 2, xe);
963         if ((rdb1 = XrmGetFileDatabase (fname)) != NULL)
964           XrmMergeDatabases (rdb1, &database);
965       }
966
967     XrmSetDatabase (display, database);
968 #  endif
969
970     /*
971      * Query resources for options that affect us
972      */
973     for (entry = 0; entry < optList_size (); entry++)
974       {
975         int             s;
976         char           *p, *p0;
977         const char     *kw = optList[entry].kw;
978
979         if (kw == NULL || rs[optList[entry].doff] != NULL)
980           continue;             /* previously set */
981
982         p = XGetDefault (display, name, kw);
983         p0 = XGetDefault (display, "!INVALIDPROGRAMMENAMEDONTMATCH!", kw);
984         if (p == NULL || (p0 && strcmp (p, p0) == 0))
985           {
986             p = XGetDefault (display, RESCLASS, kw);
987 #ifdef RESFALLBACK
988             if (p == NULL || (p0 && strcmp (p, p0) == 0))
989               p = XGetDefault (display, RESFALLBACK, kw);
990 #endif
991           }
992
993         if (p == NULL && p0)
994           p = p0;
995
996         if (p)
997           {
998             rs[optList[entry].doff] = p;
999
1000             if (optList_isBool (entry))
1001               {
1002                 s = strcasecmp (p, "TRUE") == 0
1003                     || strcasecmp (p, "YES") == 0
1004                     || strcasecmp (p, "ON") == 0
1005                     || strcasecmp (p, "1") == 0;
1006                 if (optList_isReverse (entry))
1007                   s = !s;
1008                 if (s)
1009                   options |= (optList[entry].flag);
1010                 else
1011                   options &= ~ (optList[entry].flag);
1012               }
1013           }
1014       }
1015
1016     /*
1017      * [R5 or later]: enumerate the resource database
1018      */
1019 #  ifdef XrmEnumOneLevel
1020 #   ifdef KEYSYM_RESOURCE
1021     name_prefix[0] = XrmStringToName (name);
1022     name_prefix[1] = XrmStringToName ("keysym");
1023     name_prefix[2] = NULLQUARK;
1024     class_prefix[0] = XrmStringToName (RESCLASS);
1025     class_prefix[1] = XrmStringToName ("Keysym");
1026     class_prefix[2] = NULLQUARK;
1027     /* XXX: Need to check sizeof (rxvt_t) == sizeof (XPointer) */
1028     XrmEnumerateDatabase (XrmGetDatabase (display), name_prefix, class_prefix,
1029                           XrmEnumOneLevel, rxvt_define_key, NULL);
1030 #    ifdef RESFALLBACK
1031     name_prefix[0] = XrmStringToName (RESFALLBACK);
1032     name_prefix[1] = XrmStringToName ("keysym");
1033     class_prefix[0] = XrmStringToName (RESFALLBACK);
1034     class_prefix[1] = XrmStringToName ("Keysym");
1035     /* XXX: Need to check sizeof (rxvt_t) == sizeof (XPointer) */
1036     XrmEnumerateDatabase (XrmGetDatabase (display), name_prefix, class_prefix,
1037                           XrmEnumOneLevel, rxvt_define_key, NULL);
1038 #    endif
1039 #   endif
1040 #  endif
1041
1042 # else                          /* USE_XGETDEFAULT */
1043     /* get resources the hard way, but save lots of memory */
1044     FILE *fd = NULL;
1045     char *home;
1046
1047     if ((home = getenv ("HOME")) != NULL)
1048       {
1049         unsigned int i, len = strlen (home) + 2;
1050         char *f = NULL;
1051
1052         for (i = 0; i < (sizeof (xnames) / sizeof (xnames[0])); i++)
1053           {
1054             f = (char *)rxvt_realloc (f, len + strlen (xnames[i]));
1055
1056             sprintf (f, "%s/%s", home, xnames[i]);
1057
1058             if ((fd = fopen (f, "r")) != NULL)
1059               break;
1060           }
1061
1062         free (f);
1063       }
1064     /*
1065     * The normal order to match resources is the following:
1066     * @ global resources (partial match, ~/.Xdefaults)
1067     * @ application file resources (XAPPLOADDIR/Rxvt)
1068     * @ class resources (~/.Xdefaults)
1069     * @ private resources (~/.Xdefaults)
1070     *
1071     * However, for the hand-rolled resources, the matching algorithm
1072     * checks if a resource string value has already been allocated
1073     * and won't overwrite it with (in this case) a less specific
1074     * resource value.
1075     *
1076     * This avoids multiple allocation.  Also, when we've called this
1077     * routine command-line string options have already been applied so we
1078     * needn't to allocate for those resources.
1079     *
1080     * So, search in resources from most to least specific.
1081     *
1082     * Also, use a special sub-class so that we can use either or both of
1083     * "XTerm" and "Rxvt" as class names.
1084     */
1085
1086     get_xdefaults (fd, name);
1087     get_xdefaults (fd, RESCLASS);
1088 #  ifdef RESFALLBACK
1089     get_xdefaults (fd, RESFALLBACK);
1090 #  endif
1091
1092 #  if defined(XAPPLOADDIR) && defined(USE_XAPPLOADDIR)
1093     {
1094       FILE *ad = NULL;
1095
1096 #   if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1097       if (localepath == NULL || (ad = fopen (localepath, "r")) == NULL)
1098 #   endif
1099         ad = fopen (XAPPLOADDIR "/" RESCLASS, "r");
1100       if (ad != NULL)
1101         {
1102           get_xdefaults (ad, RESCLASS);
1103           get_xdefaults (ad, "");
1104           fclose (ad);
1105         }
1106     }
1107 #  endif                        /* XAPPLOADDIR */
1108
1109     get_xdefaults (fd, "");     /* partial match */
1110     if (fd != NULL)
1111       fclose (fd);
1112 # endif                         /* USE_XGETDEFAULT */
1113
1114 # if defined XAPPLOADDIR
1115 #  if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1116
1117   }
1118
1119   /* Free the path of the possibly available localized Rxvt file */
1120   free (localepath);
1121 #  endif
1122 # endif
1123
1124 #endif                          /* NO_RESOURCES */
1125 }
1126
1127 /*}}} */
1128 /*----------------------- end-of-file (C source) -----------------------*/