- Atom type, v;
- unsigned long n, left;
- int fmt;
- unsigned char *buf;
-
- /* --- Fetch the property value --- */
-
- if (XGetWindowProperty(d, w, p, /* Display, window, property */
- 0, 64, /* Offset, length (both in words) */
- False, /* Delete after return? */
- XA_ATOM, /* Data format type */
- &type, &fmt, /* Actual type and format */
- &n, &left, /* Amount read, and bytes left */
- &buf) /* Where to put the buffer */
- != Success ||
- type != XA_ATOM ||
- n < 1 || fmt < 32)
- return (None);
-
- /* --- OK, get the atom and return --- *
- *
- * This assumes that atoms are 32-bit things. This may not be the case.
- * That's a right pain, actually. It looks as if Xlib is trying to do the
- * right thing, so I'll go with that rather than trying to do anything
- * clever. This is actually a bit of a poor interface.
- */
-
- v = *(Atom *)buf;
- XFree(buf);
- return (v);
+ const struct cmd *c;
+
+ version(); putchar('\n');
+ if (!*av) {
+ usage(stdout);
+ fputs("\n\
+Sets, retrieves and waits for properties on an X window.\n\
+\n\
+Global command-line options:\n\
+\n\
+-h, --help [COMMAND] Display this help, or help on COMMAND.\n\
+-v, --version Display program's version number.\n\
+-u, --usage Display short usage summary.\n\
+\n\
+-d, --display=DISPLAY Connect to X DISPLAY.\n\
+-w, --window=WINDOW Use properties on WINDOW instead of root.\n\
+\n\
+The following subcommands are understood:\n\n",
+ stdout);
+ for (c = cmds; c->name; c++)
+ printf("%s\n", c->usage);
+ } else {
+ while (*av) {
+ c = findcmd(*av++);
+ printf("Usage: %s [-OPTIONS] %s\n", QUIS, c->usage);
+ if (c->help) {
+ putchar('\n');
+ pquis(stdout, c->help);
+ }
+ if (*av) putchar('\n');
+ }
+ }
+}
+
+/*----- Main program ------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+ const char *dpy = 0;
+ const char *win = 0;
+
+ unsigned f = 0;
+#define F_BOGUS 1u
+
+ ego(argv[0]);
+
+ /* --- Parse arguments --- */
+
+ for (;;) {
+ static struct option opt[] = {
+ { "help", 0, 0, 'h' },
+ { "usage", 0, 0, 'u' },
+ { "version", 0, 0, 'v' },
+ { "display", OPTF_ARGREQ, 0, 'd' },
+ { "window", OPTF_ARGREQ, 0, 'w' },
+ { 0, 0, 0, 0 }
+ };
+
+ int i = mdwopt(argc, argv, "+huvd:w:", opt, 0, 0, 0);
+ if (i < 0) break;
+ switch (i) {
+ case 'h': help(argv + optind); exit(0);
+ case 'u': usage(stdout); exit(0);
+ case 'v': version(); exit(0);
+ case 'd': dpy = optarg; break;
+ case 'w': win = optarg; break;
+ default: f |= F_BOGUS; break;
+ }
+ }
+ if ((f & F_BOGUS) || optind >= argc) {
+ usage(stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ /* --- Initialize --- */
+
+ autoreap();
+ if ((display = XOpenDisplay(dpy)) == 0)
+ die(EXIT_FAILURE, "couldn't open display");
+
+ /* --- Select a target window --- */
+
+ if (!win)
+ window = DefaultRootWindow(display);
+ else if (strcmp(win, "choose") == 0)
+ window = choosewindow();
+ else
+ window = (Window)strtoul(win, 0, 0);
+
+ /* --- Dispatch the command --- */
+
+ argc -= optind;
+ argv += optind;
+ optind = 0;
+ return (findcmd(argv[0])->cmd(argc, argv));
+
+#undef F_BOGUS