xenocara/app/xlsatoms/xlsatoms.c

282 lines
7.3 KiB
C
Raw Normal View History

2006-11-25 13:07:29 -07:00
/*
*
Copyright 1989, 1998 The Open Group
Copyright 2009 Open Text Corporation
2006-11-25 13:07:29 -07:00
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*
* Author: Jim Fulton, MIT X Consortium
* Author: Peter Harris, Open Text Corporation
2006-11-25 13:07:29 -07:00
*/
2015-05-10 04:17:40 -06:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2006-11-25 13:07:29 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#define ATOMS_PER_BATCH 100 /* This number can be tuned
higher for fewer round-trips
lower for less bandwidth wasted */
2006-11-25 13:07:29 -07:00
2012-04-07 10:04:47 -06:00
static const char *ProgramName;
static const char *DisplayString;
2006-11-25 13:07:29 -07:00
2012-04-07 10:04:47 -06:00
static void do_name ( xcb_connection_t *c, const char *format, char *name );
2006-11-25 13:07:29 -07:00
static int parse_range ( char *range, long *lowp, long *highp );
2012-04-07 10:04:47 -06:00
static void do_range ( xcb_connection_t *c, const char *format, char *range );
static void list_atoms ( xcb_connection_t *c, const char *format, int mask,
2006-11-25 13:07:29 -07:00
long low, long high );
static void
2015-05-10 04:17:40 -06:00
usage(const char *errmsg)
2006-11-25 13:07:29 -07:00
{
2015-05-10 04:17:40 -06:00
if (errmsg != NULL)
fprintf (stderr, "%s: %s\n\n", ProgramName, errmsg);
fprintf (stderr, "usage: %s [-options...]\n\n%s\n", ProgramName,
"where options include:\n"
" -display dpy X server to which to connect\n"
" -format string printf-style format to use\n"
" -range [num]-[num] atom values to list\n"
" -name string name of single atom to print\n"
" -version print program version\n"
);
2006-11-25 13:07:29 -07:00
exit (1);
}
int
main(int argc, char *argv[])
{
char *displayname = NULL;
2012-04-07 10:04:47 -06:00
const char *format = "%lu\t%s";
2006-11-25 13:07:29 -07:00
int i, doit;
int didit = 0;
xcb_connection_t *c = NULL;
2006-11-25 13:07:29 -07:00
ProgramName = argv[0];
for (doit = 0; doit < 2; doit++) { /* pre-parse to get display */
for (i = 1; i < argc; i++) {
char *arg = argv[i];
if (arg[0] == '-') {
switch (arg[1]) {
case 'd': /* -display dpy */
2015-05-10 04:17:40 -06:00
if (++i >= argc) usage ("-display requires an argument");
2006-11-25 13:07:29 -07:00
if (!doit) displayname = argv[i];
continue;
case 'f': /* -format string */
2015-05-10 04:17:40 -06:00
if (++i >= argc) usage ("-format requires an argument");
2006-11-25 13:07:29 -07:00
if (doit) format = argv[i];
continue;
case 'r': /* -range num-[num] */
2015-05-10 04:17:40 -06:00
if (++i >= argc) usage ("-range requires an argument");
2006-11-25 13:07:29 -07:00
if (doit) {
do_range (c, format, argv[i]);
2006-11-25 13:07:29 -07:00
didit = 1;
}
continue;
case 'n': /* -name string */
2015-05-10 04:17:40 -06:00
if (++i >= argc) usage ("-name requires an argument");
2006-11-25 13:07:29 -07:00
if (doit) {
do_name (c, format, argv[i]);
2006-11-25 13:07:29 -07:00
didit = 1;
}
continue;
2015-05-10 04:17:40 -06:00
case 'v':
if (strcmp(arg, "-version") == 0) {
puts(PACKAGE_STRING);
exit(0);
}
/* else FALLTHROUGH to unrecognized arg case below */
2006-11-25 13:07:29 -07:00
}
}
2015-05-10 04:17:40 -06:00
fprintf (stderr, "%s: unrecognized argument %s\n\n",
ProgramName, arg);
usage (NULL);
2006-11-25 13:07:29 -07:00
}
if (!doit) {
DisplayString = displayname;
if (!DisplayString)
DisplayString = getenv("DISPLAY");
if (!DisplayString)
DisplayString = "";
c = xcb_connect(displayname, NULL);
if (!c || xcb_connection_has_error(c)) {
2006-11-25 13:07:29 -07:00
fprintf (stderr, "%s: unable to open display \"%s\"\n",
ProgramName, DisplayString);
2006-11-25 13:07:29 -07:00
exit (1);
}
} else
if (!didit) /* no options, default is list all */
list_atoms(c, format, 0, 0, 0);
2006-11-25 13:07:29 -07:00
}
xcb_disconnect(c);
2006-11-25 13:07:29 -07:00
exit (0);
}
static void
2012-04-07 10:04:47 -06:00
do_name(xcb_connection_t *c, const char *format, char *name)
2006-11-25 13:07:29 -07:00
{
xcb_intern_atom_reply_t *a = xcb_intern_atom_reply(c,
xcb_intern_atom_unchecked(c, 1, strlen(name), name), NULL);
2006-11-25 13:07:29 -07:00
if (a && a->atom != XCB_NONE) {
printf (format, (unsigned long) a->atom, name);
2006-11-25 13:07:29 -07:00
putchar ('\n');
} else {
fprintf (stderr, "%s: no atom named \"%s\" on server \"%s\"\n",
ProgramName, name, DisplayString);
2006-11-25 13:07:29 -07:00
}
if (a)
free(a);
2006-11-25 13:07:29 -07:00
}
#define RangeLow (1 << 0)
#define RangeHigh (1 << 1)
static int
parse_range(char *range, long *lowp, long *highp)
{
char *dash;
int mask = 0;
if (!range) { /* NULL means default */
*lowp = 1;
return RangeLow;
}
dash = strchr(range, '-');
if (!dash) dash = strchr(range, ':');
if (dash) {
if (dash == range) { /* -high */
*lowp = 1;
} else { /* low-[high] */
*dash = '\0';
*lowp = atoi (range);
*dash = '-';
}
mask |= RangeLow;
dash++;
if (*dash) { /* [low]-high */
*highp = atoi (dash);
mask |= RangeHigh;
}
} else { /* number (low == high) */
*lowp = *highp = atoi (range);
mask |= (RangeLow | RangeHigh);
}
return mask;
}
static void
2012-04-07 10:04:47 -06:00
do_range(xcb_connection_t *c, const char *format, char *range)
2006-11-25 13:07:29 -07:00
{
int mask;
long low, high;
mask = parse_range (range, &low, &high);
list_atoms (c, format, mask, low, high);
2006-11-25 13:07:29 -07:00
}
static int
2012-04-07 10:04:47 -06:00
say_batch(xcb_connection_t *c, const char *format, xcb_get_atom_name_cookie_t *cookie, long low, long count)
2006-11-25 13:07:29 -07:00
{
xcb_generic_error_t *e;
char atom_name[1024];
long i;
int done = 0;
for (i = 0; i < count; i++)
cookie[i] = xcb_get_atom_name(c, i + low);
for (i = 0; i < count; i++) {
xcb_get_atom_name_reply_t *r;
r = xcb_get_atom_name_reply(c, cookie[i], &e);
if (r) {
/* We could just use %.*s in 'format', but we want to be compatible
with legacy command line usage */
snprintf(atom_name, sizeof(atom_name), "%.*s",
r->name_len, xcb_get_atom_name_name(r));
printf (format, i + low, atom_name);
putchar ('\n');
free(r);
}
if (e) {
done = 1;
free(e);
}
2006-11-25 13:07:29 -07:00
}
return done;
2006-11-25 13:07:29 -07:00
}
static void
2012-04-07 10:04:47 -06:00
list_atoms(xcb_connection_t *c, const char *format, int mask, long low, long high)
2006-11-25 13:07:29 -07:00
{
xcb_get_atom_name_cookie_t *cookie_jar;
int done = 0;
2006-11-25 13:07:29 -07:00
switch (mask) {
case RangeHigh:
low = 1;
/* fall through */
case (RangeLow | RangeHigh):
cookie_jar = malloc((high - low + 1) * sizeof(xcb_get_atom_name_cookie_t));
if (!cookie_jar) {
fprintf(stderr, "Out of memory allocating space for %ld atom requests\n", high - low);
return;
2006-11-25 13:07:29 -07:00
}
say_batch(c, format, cookie_jar, low, high - low + 1);
free(cookie_jar);
2006-11-25 13:07:29 -07:00
break;
default:
low = 1;
/* fall through */
case RangeLow:
cookie_jar = malloc(ATOMS_PER_BATCH * sizeof(xcb_get_atom_name_cookie_t));
if (!cookie_jar) {
fprintf(stderr, "Out of memory allocating space for %ld atom requests\n", (long) ATOMS_PER_BATCH);
return;
}
while (!done) {
done = say_batch(c, format, cookie_jar, low, ATOMS_PER_BATCH);
low += ATOMS_PER_BATCH;
2006-11-25 13:07:29 -07:00
}
free(cookie_jar);
2006-11-25 13:07:29 -07:00
break;
}
return;
}