home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3066 / resources.c next >
Encoding:
C/C++ Source or Header  |  1991-03-15  |  15.0 KB  |  424 lines

  1. /*
  2.  *    resources.c: Defines the application resources used
  3.  *             in XtAppInitialize().
  4.  *
  5.  *    Also, getResources() is called instead of initGraphics() when
  6.  *    we're in non-interactive mode, to try and get the few resources
  7.  *    we need to avoid opening a display.
  8.  *
  9.  *    Needs XAPPLOADDIR defined during compilation.
  10.  *
  11.  *    George Ferguson, ferguson@cs.rochester.edu, 27 Feb 1991.
  12.  *
  13.  *    $Id: resources.c,v 2.4 91/03/13 13:31:31 ferguson Exp $
  14.  *
  15.  */
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <X11/Intrinsic.h>
  19. #include <X11/StringDefs.h>
  20. #include "xkal.h"
  21. #include "app-resources.h"
  22. extern char *getenv();
  23.  
  24. /*
  25.  * Functions defined here:
  26.  */
  27. void getResources();
  28. static void readResourceValue(),setResourceValue();
  29.  
  30. /*
  31.  * Data defined here: (check resources.h if these change!!)
  32.  */
  33. /*
  34.  *    Non-widget resources settable on command line.
  35.  */
  36. XrmOptionDescRec xkalOptions[] = {
  37.     { "-appoints",    ".personalAppoints",XrmoptionSepArg,    "~/.appoints" },
  38.     { "-date",        ".date",    XrmoptionSepArg,    "" },
  39.     { "-numMonths",    ".numMonths",    XrmoptionSepArg,    "1" },
  40.     { "-bothShown",    ".bothShown",    XrmoptionNoArg,        "True" },
  41.     { "-nobothShown",    ".bothShown",    XrmoptionNoArg,        "False" },
  42.     { "-version",    ".version",    XrmoptionNoArg,        "True" },
  43.     { "-listOnly",    ".listOnly",    XrmoptionNoArg,        "True" },
  44.     { "-silent",    ".silent",    XrmoptionNoArg,        "True" },
  45.     { "-exitUsesLevels",".exitUsesLevels",XrmoptionNoArg,    "True" },
  46.     { "-noexitUsesLevels",".exitUsesLevels",XrmoptionNoArg,    "False" },
  47.     { "-opaqueDates",    ".opaqueDates",    XrmoptionNoArg,        "True" },
  48.     { "-noopaqueDates",    ".opaqueDates",    XrmoptionNoArg,        "False" },
  49.     { "-dowLabels",    ".dowLabels",    XrmoptionNoArg,        "True" },
  50.     { "-nodowLabels",    ".dowLabels",    XrmoptionNoArg,        "False" },
  51.     { "-titlebar",    ".useTitlebar",    XrmoptionNoArg,        "True" },
  52.     { "-notitlebar",    ".useTitlebar",    XrmoptionNoArg,        "False" },
  53.     { "-checkpointInterval",".checkpointInterval",XrmoptionSepArg,"5" },
  54. };
  55. /*
  56.  * Non-widget resources obtained from the resource manager
  57.  */
  58. XtResource xkalResources[] = {
  59.     { "systemAppoints", "String", XtRString, sizeof(String),
  60.       XtOffset(AppResources *,systemAppoints), XtRImmediate, "" },
  61.     { "personalAppoints", "String", XtRString, sizeof(String),
  62.       XtOffset(AppResources *,personalAppoints), XtRImmediate, "~/.appoints" },
  63.     { "backupExtension", "String", XtRString, sizeof(String),
  64.       XtOffset(AppResources *,backupExtension), XtRImmediate, "~" },
  65.     { "outputFormat", "String", XtRString, sizeof(String),
  66.       XtOffset(AppResources *,outputFormat), XtRImmediate,
  67.                         "%w%~%d%~%m%~%y%~%t%~%l%~" },
  68.     { "daySlashMonth", "Boolean", XtRBoolean, sizeof(Boolean),
  69.       XtOffset(AppResources *,daySlashMonth), XtRImmediate, (XtPointer)False },
  70.  
  71.     { "date", "String", XtRString, sizeof(String),
  72.       XtOffset(AppResources *,date), XtRImmediate, "" },
  73.     { "numMonths", "Int", XtRInt, sizeof(int),
  74.       XtOffset(AppResources *,numMonths), XtRImmediate, (XtPointer)1 },
  75.     { "bothShown", "Boolean", XtRBoolean, sizeof(Boolean),
  76.       XtOffset(AppResources *,bothShown), XtRImmediate, (XtPointer)True },
  77.     { "useTitlebar", "Boolean", XtRBoolean, sizeof(Boolean),
  78.       XtOffset(AppResources *,useTitlebar), XtRImmediate, (XtPointer)False },
  79.     { "version", "Boolean", XtRBoolean, sizeof(Boolean),
  80.       XtOffset(AppResources *,version), XtRImmediate, (XtPointer)False },
  81.     { "listOnly", "Boolean", XtRBoolean, sizeof(Boolean),
  82.       XtOffset(AppResources *,listOnly), XtRImmediate, (XtPointer)False },
  83.     { "silent", "Boolean", XtRBoolean, sizeof(Boolean),
  84.       XtOffset(AppResources *,silent), XtRImmediate, (XtPointer)False },
  85.     { "exitUsesLevels", "Boolean", XtRBoolean, sizeof(Boolean),
  86.       XtOffset(AppResources *,exitUsesLevels), XtRImmediate, (XtPointer)False },
  87.     { "opaqueDates", "Boolean", XtRBoolean, sizeof(Boolean),
  88.       XtOffset(AppResources *,opaqueDates), XtRImmediate, (XtPointer)False },
  89.     { "checkpointInterval", "Int", XtRInt, sizeof(int),
  90.       XtOffset(AppResources *,checkpointInterval),XtRImmediate,(XtPointer)5 },
  91.  
  92.     { "dowLabels", "Boolean", XtRBoolean, sizeof(Boolean),
  93.       XtOffset(AppResources *,dowLabels), XtRImmediate, (XtPointer)False },
  94.     { "longDowStrings", "String", XtRString, sizeof(String),
  95.       XtOffset(AppResources *,longDowStrings), XtRImmediate,
  96.             "Sunday Monday Tuesday Wednesday Thursday \
  97.              Friday Saturday" },
  98.     { "shortDowStrings", "String", XtRString, sizeof(String),
  99.       XtOffset(AppResources *,shortDowStrings), XtRImmediate,
  100.             "Sun Mon Tue Wed Thu Fri Sat" },
  101.     { "longMonthStrings", "String", XtRString, sizeof(String),
  102.       XtOffset(AppResources *,longMonthStrings), XtRImmediate,
  103.             "January February March April May June \
  104.               July August September October November December" },
  105.     { "shortMonthStrings", "String", XtRString, sizeof(String),
  106.       XtOffset(AppResources *,shortMonthStrings), XtRImmediate,
  107.             "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" },
  108.     { "dowOffset", "Int", XtRInt, sizeof(int),
  109.       XtOffset(AppResources *,dowOffset), XtRImmediate, (XtPointer)0 },
  110.  
  111.     { "appsStartTime", "String", XtRString, sizeof(String),
  112.       XtOffset(AppResources *,appsStartTime), XtRImmediate, "8:00" },
  113.     { "appsEndTime", "String", XtRString, sizeof(String),
  114.       XtOffset(AppResources *,appsEndTime), XtRImmediate, "17:00" },
  115.     { "appsIncrement", "String", XtRString, sizeof(String),
  116.       XtOffset(AppResources *,appsIncrement), XtRImmediate, ":30" },
  117.     { "appsUseAmPm", "Boolean", XtRBoolean, sizeof(Boolean),
  118.       XtOffset(AppResources *,appsUseAmPm), XtRImmediate, (XtPointer)False },
  119.     { "numNotes", "Int", XtRInt, sizeof(int),
  120.       XtOffset(AppResources *,numNotes), XtRImmediate, (XtPointer)2 },
  121.     { "notesLabel", "String", XtRString, sizeof(String),
  122.       XtOffset(AppResources *,notesLabel), XtRImmediate, "NOTES" },
  123.     { "rearrangeSilently", "Boolean", XtRBoolean, sizeof(Boolean),
  124.       XtOffset(AppResources *,rearrangeSilently),XtRImmediate,(XtPointer)False},
  125.     { "noonStr", "String", XtRString, sizeof(String),
  126.       XtOffset(AppResources *,noonStr), XtRImmediate, "Noon" },
  127.     { "midnightStr", "String", XtRString, sizeof(String),
  128.       XtOffset(AppResources *,midnightStr), XtRImmediate, "Midnight" },
  129.  
  130.     { "levelDelim", "String", XtRString, sizeof(String),
  131.       XtOffset(AppResources *,levelDelim),XtRImmediate,"@@" },
  132.     { "maxLevel", "Int", XtRInt, sizeof(int),
  133.       XtOffset(AppResources *,maxLevel), XtRImmediate, (XtPointer)7 },
  134.     { "defaultLevel", "Int", XtRInt, sizeof(int),
  135.       XtOffset(AppResources *,defaultLevel), XtRImmediate, (XtPointer)1 },
  136.  
  137.     { "dateFont1", "Font", XtRFont, sizeof(Font),
  138.       XtOffset(AppResources *,dateFont1), XtRString, "*fixed*bold*" },
  139.     { "dateFont2", "Font", XtRFont, sizeof(Font),
  140.       XtOffset(AppResources *,dateFont2), XtRString, "*fixed*bold*" },
  141.     { "dateFont3", "Font", XtRFont, sizeof(Font),
  142.       XtOffset(AppResources *,dateFont3), XtRString, "*fixed*bold*" },
  143.     { "dateFont12", "Font", XtRFont, sizeof(Font),
  144.       XtOffset(AppResources *,dateFont12), XtRString, "*fixed*bold*" },
  145.  
  146.     { "datePosition1", "String", XtRString, sizeof(String),
  147.       XtOffset(AppResources *,datePosition1), XtRImmediate, "+10+10" },
  148.     { "datePosition2", "String", XtRString, sizeof(String),
  149.       XtOffset(AppResources *,datePosition2), XtRImmediate, "+10+10" },
  150.     { "datePosition3", "String", XtRString, sizeof(String),
  151.       XtOffset(AppResources *,datePosition3), XtRImmediate, "+10+10" },
  152.     { "datePosition12", "String", XtRString, sizeof(String),
  153.       XtOffset(AppResources *,datePosition12), XtRImmediate, "+10+10" },
  154.  
  155.     { "dateWidth1", "Int", XtRInt, sizeof(int),
  156.       XtOffset(AppResources *,dateWidth1), XtRImmediate, (XtPointer)50 },
  157.     { "dateWidth2", "Int", XtRInt, sizeof(int),
  158.       XtOffset(AppResources *,dateWidth2), XtRImmediate, (XtPointer)18 },
  159.     { "dateWidth3", "Int", XtRInt, sizeof(int),
  160.       XtOffset(AppResources *,dateWidth3), XtRImmediate, (XtPointer)18 },
  161.     { "dateWidth12", "Int", XtRInt, sizeof(int),
  162.       XtOffset(AppResources *,dateWidth12), XtRImmediate, (XtPointer)10 },
  163.  
  164.     { "dateHeight1", "Int", XtRInt, sizeof(int),
  165.       XtOffset(AppResources *,dateHeight1), XtRImmediate, (XtPointer)50 },
  166.     { "dateHeight2", "Int", XtRInt, sizeof(int),
  167.       XtOffset(AppResources *,dateHeight2), XtRImmediate, (XtPointer)18 },
  168.     { "dateHeight3", "Int", XtRInt, sizeof(int),
  169.       XtOffset(AppResources *,dateHeight3), XtRImmediate, (XtPointer)18 },
  170.     { "dateHeight12", "Int", XtRInt, sizeof(int),
  171.       XtOffset(AppResources *,dateHeight12), XtRImmediate, (XtPointer)10 },
  172.  
  173.     { "noDayShade", "String", XtRString, sizeof(String),
  174.       XtOffset(AppResources *,noDayShade), XtRImmediate, "wide_weave" },
  175.     { "shades", "String", XtRString, sizeof(String),
  176.       XtOffset(AppResources *,shades), XtRImmediate,
  177.         "gray3 gray3 light_gray light_gray gray gray flipped_gray" },
  178.     { "colors", "String", XtRString, sizeof(String),
  179.       XtOffset(AppResources *,colors), XtRImmediate,
  180.         "Green Green Blue Blue Yellow Yellow Red" },
  181.  
  182.     { "revision", "Revision", XtRString, sizeof(String),
  183.       XtOffset(AppResources *,revision), XtRImmediate, "" },
  184. };
  185.  
  186. /*
  187.  * Resources that we need for non-interactive mode.
  188.  * See also setResourceValue() if you change the order or content of these.
  189.  */
  190. #define NUM_RESOURCES 17
  191. static char *strings[NUM_RESOURCES] = {
  192.     "systemAppoints",
  193.     "personalAppoints",
  194.     "date",
  195.     "listOnly",
  196.     "silent",
  197.     "exitUsesLevels",
  198.     "longDowStrings",
  199.     "shortDowStrings",
  200.     "longMonthStrings",
  201.     "shortMonthStrings",
  202.     "daySlashMonth",
  203.     "levelDelim",
  204.     "backupExtension",
  205.     "version",
  206.     "notesLabel",        /* needed for listAppoints() */
  207.     "noonStr",
  208.     "midnightStr",
  209. };
  210. static int strpos[NUM_RESOURCES];
  211.  
  212. static char className[] = "Xkal";
  213.  
  214. #define SKIPSPACE(FP,C)    while (C == ' ' || C == '\t') C = getc(FP);
  215. #define SKIPWHITE(FP,C)    while (C == ' ' || C == '\t' || C == '\n') C = getc(FP);
  216. #define SKIPTOEOL(FP,C)    while (C != EOF && C != '\n') C = getc(FP);
  217.  
  218. /*    -    -    -    -    -    -    -    -    */
  219. /*
  220.  *    Uses the array xkalResources[] for the defaults, looks for an
  221.  *    app-defaults file in either $XAPPLRESDIR or XAPPLOADDIR, and
  222.  *    checks command line, in that order (doesn't check ~/.Xdefaults,
  223.  *    yet). Modifies *argcp and argv to remove parsed options.
  224.  *
  225.  *    This allows us to get resources without opening the connection
  226.  *    to the X server.
  227.  *
  228.  */
  229. void
  230. getResources(argcp,argv)
  231. int *argcp;
  232. char **argv;
  233. {
  234.     FILE *fp;
  235.     char *s,**av,**retargv,buf[1024];
  236.     int i,j,c,argc;
  237.  
  238.     for (i=0 ; i < XtNumber(xkalResources); i++)
  239.     for (j=0; j < NUM_RESOURCES; j++)
  240.         if (strcmp(strings[j],xkalResources[i].resource_name) == 0)
  241.         setResourceValue(j,xkalResources[i].default_addr);
  242.     if ((s=getenv("XAPPLRESDIR")) != NULL)
  243.     strcpy(buf,s);
  244.     else
  245.     strcpy(buf,XAPPLOADDIR);
  246.     strcat(buf,"/");
  247.     strcat(buf,className);
  248.     if ((fp=fopen(buf,"r")) != NULL) {
  249.     while ((c=getc(fp)) != EOF) {
  250.         /* skip leading whitespace */
  251.         SKIPWHITE(fp,c);
  252.         /* got a comment then skip to EOL */
  253.         if (c == '!') {
  254.         SKIPTOEOL(fp,c);
  255.         continue;
  256.         }
  257.         /* skip class name and . or * after it if they are present */
  258.         i = 0;
  259.         while (c == className[i++])
  260.         c = getc(fp);
  261.         if (c == '.' || c == '*')
  262.         c = getc(fp);
  263.         /* all strings can potentially match so reset pos array */
  264.         for (i=0; i < NUM_RESOURCES; i++)
  265.         strpos[i] = 0;
  266.         /* gather characters in resource name and match strings */
  267.         while (c != EOF && c != '\n' && c != '.' && c != '*') {
  268.         for (i=0; i < NUM_RESOURCES; i++) {
  269.             /* if already mismatched then skip this possibility */
  270.             if (strpos[i] == -1)
  271.             continue;
  272.             /* if matched resource name then set it */
  273.             if (c == ':' && strings[i][strpos[i]] == '\0') {
  274.             readResourceValue(fp,buf);
  275.             setResourceValue(i,XtNewString(buf));
  276.             /* done with this line */
  277.             break;
  278.             } else if (c == strings[i][strpos[i]]) {
  279.             /* haven't matched yet, but still ok */
  280.             strpos[i] += 1;
  281.             } else {
  282.             /* can't be this resource since mismatch */
  283.             strpos[i] = -1;
  284.             }
  285.         } /* for i = 0 to NUM_RESOURCES */
  286.         c = getc(fp);
  287.         } /* while c != '\n' */
  288.     } /* while !feof(fp)) */
  289.     fclose(fp);
  290.     }
  291.     retargv = argv + 1;
  292.     for (argc = *argcp-1, av = argv+1; argc; argc--,av++) {
  293.     if (strncmp(*av,"-a",2) == 0) {
  294.         setResourceValue(1,*(av+1));
  295.         av += 1;
  296.         argc -= 1;
  297.         *argcp -= 2;
  298.     } else if (strncmp(*av,"-d",2) == 0) {
  299.         setResourceValue(2,*(av+1));
  300.         av += 1;
  301.         argc -= 1;
  302.         *argcp -= 2;
  303.     } else if (strncmp(*av,"-l",2) == 0) {
  304.         setResourceValue(3,(char *)True);
  305.         *argcp -= 1;
  306.     } else if (strncmp(*av,"-s",2) == 0) {
  307.         setResourceValue(4,(char *)True);
  308.         *argcp -= 1;
  309.     } else if (strncmp(*av,"-e",2) == 0) {
  310.         setResourceValue(5,(char *)True);
  311.         *argcp -= 1;
  312.     } else if (strncmp(*av,"-v",2) == 0) {
  313.         setResourceValue(13,(char *)True);
  314.         *argcp -= 1;
  315.     } else {
  316.         *retargv++ = *av;
  317.     }
  318.     }
  319. }
  320.  
  321. /*
  322.  * readResourceValue(fp): Assumes that fp just read `:'. Fills in buf with
  323.  * the resource value and eats the final newline. Newlines can be escaped with
  324.  * backslashes.
  325.  */
  326. static void
  327. readResourceValue(fp,buf)
  328. FILE *fp;
  329. char buf[];
  330. {
  331.     int c,n;
  332.     Boolean slash;
  333.  
  334.     c = getc(fp);
  335.     SKIPSPACE(fp,c);
  336.     if (c == EOF || c == '\n') {
  337.     buf[0] = '\0';
  338.     return;
  339.     }
  340.     n = 0;
  341.     slash = False;
  342.     do {
  343.     /* got newline */
  344.     if (c == '\n') {
  345.         if (slash) {
  346.         /* it was escaped with backslash */
  347.         slash = False;
  348.         continue;
  349.         } else {
  350.         /* otherwise its end of the resource */
  351.         break;
  352.         }
  353.     }
  354.     if (slash)
  355.         /* last backslash wasn't escaping \n so add it */
  356.         buf[n++] = '\\';
  357.     if (c == '\\') {
  358.         /* we just had a backslash (maybe an escaped one) */
  359.         slash = !slash;
  360.     } else {
  361.         /* otherwise we want this char */
  362.         buf[n++] = c;
  363.         slash = False;
  364.     }
  365.     } while ((c=getc(fp)) != EOF && n < 1022);
  366.     buf[n] = '\0';
  367.     if (n == 1023 && c != EOF)
  368.     fprintf(stderr,"%s: resource line too long for dumb parser: \"%s...\"\n",program,buf);
  369. }
  370.  
  371. /*
  372.  * setResourceValue() : Decodes which into the actual resource entry to set,
  373.  *    and sets it.
  374.  */
  375. static void
  376. setResourceValue(which,value)
  377. int which;
  378. char *value;
  379. {
  380.     Boolean bool;
  381.  
  382.     if (which == 3 || which == 4 || which == 5 || which == 10 || which == 13)
  383.     if ((int)value > 1)
  384.         bool = (strcasecmp(value,"True") == 0 || strcmp(value,"1") == 0);
  385.     else
  386.         bool = (Boolean)value;
  387.     switch (which) {
  388.     case 0: appResources.systemAppoints = value;
  389.         break;
  390.     case 1: appResources.personalAppoints = value;
  391.         break;
  392.     case 2: appResources.date = value;
  393.         break;
  394.     case 3: appResources.listOnly = bool;
  395.         break;
  396.     case 4: appResources.silent = bool;
  397.         break;
  398.     case 5: appResources.exitUsesLevels = bool;
  399.         break;
  400.     case 6: appResources.longDowStrings = value;
  401.         break;
  402.     case 7: appResources.shortDowStrings = value;
  403.         break;
  404.     case 8: appResources.longMonthStrings = value;
  405.         break;
  406.     case 9: appResources.shortMonthStrings = value;
  407.         break;
  408.     case 10: appResources.daySlashMonth = bool;
  409.          break;
  410.     case 11: appResources.levelDelim = value;
  411.          break;
  412.     case 12: appResources.backupExtension = value;
  413.          break;
  414.     case 13: appResources.version = bool;
  415.          break;
  416.     case 14: appResources.notesLabel = value;
  417.          break;
  418.     case 15: appResources.noonStr = value;
  419.          break;
  420.     case 16: appResources.midnightStr = value;
  421.          break;
  422.     }
  423. }
  424.