home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xlsatoms / xlsatoms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-17  |  5.3 KB  |  233 lines

  1. /*
  2.  * $XConsortium: xlsatoms.c,v 1.3 90/12/17 18:47:10 gildea Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Jim Fulton, MIT X Consortium
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <X11/Xos.h>
  28. #include <X11/Xlib.h>
  29. #include <X11/Xproto.h>
  30. #include <X11/Xmu/Error.h>
  31.  
  32. char *ProgramName;
  33.  
  34. static void usage ()
  35. {
  36.     fprintf (stderr, "usage:  %s [-options...]\n\n", ProgramName);
  37.     fprintf (stderr, "where options include:\n");
  38.     fprintf (stderr,
  39.          "    -display dpy            X server to which to connect\n");
  40.     fprintf (stderr,
  41.          "    -format string          printf-style format to use\n");
  42.     fprintf (stderr,
  43.          "    -range [num]-[num]      atom values to list\n");
  44.     fprintf (stderr,
  45.          "    -name string            name of single atom to print\n");
  46.     putc ('\n', stderr);
  47.     exit (1);
  48. }
  49.  
  50.  
  51. main (argc, argv)
  52.     int argc;
  53.     char **argv;
  54. {
  55.     char *displayname = NULL;
  56.     char *format = "%lu\t%s";
  57.     int i, doit;
  58.     int didit = 0;
  59.     Display *dpy = NULL;
  60.  
  61.     ProgramName = argv[0];
  62.  
  63.     for (doit = 0; doit < 2; doit++) {    /* pre-parse to get display */
  64.     for (i = 1; i < argc; i++) {
  65.         char *arg = argv[i];
  66.  
  67.         if (arg[0] == '-') {
  68.         switch (arg[1]) {
  69.           case 'd':            /* -display dpy */
  70.             if (++i >= argc) usage ();
  71.             if (!doit) displayname = argv[i];
  72.             continue;
  73.           case 'f':            /* -format string */
  74.             if (++i >= argc) usage ();
  75.             if (doit) format = argv[i];
  76.             continue;
  77.           case 'r':            /* -range num-[num] */
  78.             if (++i >= argc) usage ();
  79.             if (doit) {
  80.             do_range (dpy, format, argv[i]);
  81.             didit = 1;
  82.             }
  83.             continue;
  84.           case 'n':            /* -name string */
  85.             if (++i >= argc) usage ();
  86.             if (doit) {
  87.             do_name (dpy, format, argv[i]);
  88.             didit = 1;
  89.             }
  90.             continue;
  91.         }
  92.         }
  93.         usage ();
  94.     }
  95.     if (!doit) {
  96.         dpy = XOpenDisplay (displayname);
  97.         if (!dpy) {
  98.         fprintf (stderr, "%s:  unable to open display \"%s\"\n",
  99.              ProgramName, XDisplayName (displayname));
  100.         exit (1);
  101.         }
  102.     } else
  103.         if (!didit)        /* no options, default is list all */
  104.         list_atoms(dpy, format, 0, 0, 0);
  105.     }
  106.  
  107.     XCloseDisplay (dpy);
  108.     exit (0);
  109. }
  110.  
  111. do_name (dpy, format, name)
  112.     Display *dpy;
  113.     char *format;
  114.     char *name;
  115. {
  116.     Atom a = XInternAtom (dpy, name, True);
  117.  
  118.     if (a != None) {
  119.     printf (format, (unsigned long) a, name);
  120.     putchar ('\n');
  121.     } else {
  122.     fprintf (stderr, "%s:  no atom named \"%s\" on server \"%s\"\n",
  123.          ProgramName, name, DisplayString(dpy));
  124.     }
  125. }
  126.  
  127.  
  128. #define RangeLow (1 << 0)
  129. #define RangeHigh (1 << 1)
  130.  
  131. static int parse_range (range, lowp, highp)
  132.     char *range;
  133.     long *lowp, *highp;
  134. {
  135.     char *dash;
  136.     int mask = 0;
  137.  
  138.     if (!range) {            /* NULL means default */
  139.     *lowp = 1;
  140.     return RangeLow;
  141.     }
  142.  
  143.     dash = index (range, '-');
  144.     if (!dash) dash = index (range, ':');
  145.     if (dash) {
  146.     if (dash == range) {        /* -high */
  147.         *lowp = 1;
  148.     } else {            /* low-[high] */
  149.         *dash = '\0';
  150.         *lowp = atoi (range);
  151.         *dash = '-';
  152.     }
  153.     mask |= RangeLow;
  154.     dash++;
  155.     if (*dash) {            /* [low]-high */
  156.         *highp = atoi (dash);
  157.         mask |= RangeHigh;
  158.     }
  159.     } else {                /* number (low == high) */
  160.     *lowp = *highp = atoi (range);
  161.     mask |= (RangeLow | RangeHigh);
  162.     }
  163.  
  164.     return mask;
  165. }
  166.  
  167. do_range (dpy, format, range)
  168.     Display *dpy;
  169.     char *format;
  170.     char *range;
  171. {
  172.     int mask;
  173.     long low, high;
  174.  
  175.     mask = parse_range (range, &low, &high);
  176.     list_atoms (dpy, format, mask, low, high);
  177. }
  178.  
  179.  
  180. static int catcher (dpy, err)
  181.     Display *dpy;
  182.     XErrorEvent *err;
  183. {
  184.     if (err->request_code != X_GetAtomName) {
  185.     XmuPrintDefaultErrorMessage (dpy, err, stderr);
  186.     }
  187.     return 0;
  188. }
  189.  
  190. list_atoms (dpy, format, mask, low, high)
  191.     Display *dpy;
  192.     char *format;
  193.     int mask;
  194.     long low, high;
  195. {
  196.     int (*oldhandler)() = XSetErrorHandler (catcher);
  197.  
  198.     switch (mask) {
  199.       case RangeHigh:
  200.     low = 1;
  201.     /* fall through */
  202.       case (RangeLow | RangeHigh):
  203.     for (; low <= high; low++) {
  204.         char *s = XGetAtomName (dpy, (Atom)low);
  205.         if (s) {
  206.         printf (format, low, s);
  207.         putchar ('\n');
  208.         XFree (s);
  209.         }
  210.     }
  211.     break;
  212.  
  213.       default:
  214.     low = 1;
  215.     /* fall through */
  216.       case RangeLow:
  217.     for (; ; low++) {
  218.         char *s = XGetAtomName (dpy, (Atom)low);
  219.         if (s) {
  220.         printf (format, low, s);
  221.         putchar ('\n');
  222.         XFree (s);
  223.         } else {
  224.         break;
  225.         }
  226.     }
  227.     break;
  228.     }
  229.  
  230.     XSetErrorHandler (oldhandler);
  231.     return;
  232. }
  233.