1 /* resource.c     resources, command line */
2 
3 /*
4  * xcheckers:     a point and click checkerboard
5  * (C):           1999, 2000 Peter Chiocchetti <girbal@tacheles.de>
6  */
7 
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <X11/Xlib.h>
13 #include <X11/Xresource.h>
14 #include "xcheckers.h"
15 #include "resource.h"
16 #include "board.h"
17 
18 
19 #define numOptions (sizeof options / sizeof (XrmOptionDescRec))
20 
21 
22 XrmDatabase     resources = NULL;
23 
24 static char     Banner[] =
25 "\nxcheckers version " VERSION
26 "\nCopyright (C) 1999, 2004 Peter Chiocchetti."
27 "\nxcheckers comes with ABSOLUTELY NO WARRANTY; for details"
28 "\nsee the file \"COPYING\" in the distribution directory.\n";
29 
30 
31 static XrmOptionDescRec options[] = {
32   {"-icds", ".icds", XrmoptionNoArg, (XPointer) "true"},
33   {"-host", ".host", XrmoptionSepArg, (XPointer) NULL},
34   {"-port", ".port", XrmoptionSepArg, (XPointer) NULL},
35   {"-local", ".icds", XrmoptionNoArg, (XPointer) "false"},
36   {"-engine", ".engine", XrmoptionSepArg, (XPointer) NULL},
37   {"-scene", ".scene", XrmoptionSepArg, (XPointer) NULL},
38   {"-font", ".font", XrmoptionSepArg, (XPointer) NULL},
39   {"-geometry", ".geometry", XrmoptionSepArg, (XPointer) NULL},
40   {"-display", ".display", XrmoptionSepArg, (XPointer) NULL},
41 };
42 
43 
44 /* write command line switches summary to stdout */
45 static void
usage(void)46 usage(void)
47 {
48   static char     Usage[] =
49   "\nusage: %s <options>\n"
50   "\t-icds\t\t\t\tconnect to an icds\n"
51   "\t-host\t\thostname\taddress named host\n"
52   "\t-port\t\tportnum\t\tat specified port\n"
53   "\n"
54   "\t-local\t\t\t\tconnect locally\n"
55   "\t-engine\t\tprogram\t\tstart named engine\n"
56   "\n"
57   "\t-scene\t\tpixmap\t\tpath to an appropriate pixmap\n"
58   "\t-font\t\tfont\t\tused in status line\n"
59   "\n"
60   "\t-geometry\tgeometry\twindow geometry\n"
61   "\t-display\tstring\t\tX server to connect\n"
62   "\n"
63   "all of these options default to resources set e.g. in your ~/.Xdefaults\n"
64   "\n";
65 
66   fprintf(stdout, Usage, progname);
67   exit(1);
68 }
69 
70 
71 /* get a resource from the database */
72 char           *
resGet(const char * id)73 resGet(const char *id)
74 {
75   char           *type;
76   XrmValue        value;
77   char            name[128], class[128];
78 
79   snprintf(name, 128, "%s.%s", progname, id);
80   snprintf(class, 128, "XCheckers.%s", id);
81 
82   XrmGetResource(resources, name, class, &type, &value);
83   return (value.addr);
84 }
85 
86 
87 /* fill resource database, get player name and programm name */
88 int
resInit(int * argc,char ** argv,char ** hostname,char ** portnum)89 resInit(int *argc, char **argv, char **hostname, char **portnum)
90 {
91   int             icds = 0;
92   char           *tmp;
93   char            defaults[100];
94 
95   fprintf(stdout, Banner);
96 
97   /* set some globals */
98   if ((myname = getenv("LOGNAME")) == NULL)
99     myname = strdup("it's me");
100   else if (strlen(myname) > 20)
101     myname[20] = '\0';
102   direction = NORTH;
103   cells = 8;
104 
105   /* set default program name for diagnostics */
106   if ((progname = strrchr(argv[0], '/')) == NULL)
107     progname = argv[0];
108   else
109     progname++;
110 
111   /* make up the resources database: sequence matters */
112   XrmInitialize();
113   resources = XrmGetFileDatabase(APPDEFS);
114   XrmCombineFileDatabase("xcheckersrc", &resources, True);
115   strcpy(defaults, getenv("HOME"));
116   strcat(defaults, "/.xcheckersrc");
117   XrmCombineFileDatabase(defaults, &resources, True);
118   strcpy(defaults, getenv("HOME"));
119   strcat(defaults, "/.Xdefaults");
120   XrmCombineFileDatabase(defaults, &resources, True);
121   XrmParseCommand(&resources, options, numOptions, progname, argc, argv);
122 
123   if (*argc != 1)
124     usage();
125 
126   if ((tmp = resGet("icds")) != NULL && (!strcmp(tmp, "true"))) {
127     if ((*hostname = resGet("host")) == NULL) {
128       fprintf(stderr, "%s: Error, no hostname specified\n", progname);
129       exit(1);
130     }
131     if ((*portnum = resGet("port")) == NULL) {
132       fprintf(stderr, "%s: Error, no portnumber specified\n", progname);
133       exit(1);
134     }
135     icds = 1;
136   } else {
137     if ((*hostname = resGet("engine")) == NULL) {
138       fprintf(stderr, "%s: Error, no engine specified\n", progname);
139       exit(1);
140     }
141     *portnum = NULL;
142   }
143   return (icds);
144 }
145