xlsatoms.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. *
  3. Copyright 1989, 1998 The Open Group
  4. Copyright 2009 Open Text Corporation
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation.
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  16. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. Except as contained in this notice, the name of The Open Group shall not be
  19. used in advertising or otherwise to promote the sale, use or other dealings
  20. in this Software without prior written authorization from The Open Group.
  21. *
  22. * Author: Jim Fulton, MIT X Consortium
  23. * Author: Peter Harris, Open Text Corporation
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. # include "config.h"
  27. #endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <xcb/xcb.h>
  32. #include <xcb/xproto.h>
  33. #define ATOMS_PER_BATCH 100 /* This number can be tuned
  34. higher for fewer round-trips
  35. lower for less bandwidth wasted */
  36. static const char *ProgramName;
  37. static const char *DisplayString;
  38. static void do_name ( xcb_connection_t *c, const char *format, char *name );
  39. static int parse_range ( char *range, long *lowp, long *highp );
  40. static void do_range ( xcb_connection_t *c, const char *format, char *range );
  41. static void list_atoms ( xcb_connection_t *c, const char *format, int mask,
  42. long low, long high );
  43. static void
  44. usage(const char *errmsg)
  45. {
  46. if (errmsg != NULL)
  47. fprintf (stderr, "%s: %s\n\n", ProgramName, errmsg);
  48. fprintf (stderr, "usage: %s [-options...]\n\n%s\n", ProgramName,
  49. "where options include:\n"
  50. " -display dpy X server to which to connect\n"
  51. " -format string printf-style format to use\n"
  52. " -range [num]-[num] atom values to list\n"
  53. " -name string name of single atom to print\n"
  54. " -version print program version\n"
  55. );
  56. exit (1);
  57. }
  58. int
  59. main(int argc, char *argv[])
  60. {
  61. char *displayname = NULL;
  62. const char *format = "%lu\t%s";
  63. int i, doit;
  64. int didit = 0;
  65. xcb_connection_t *c = NULL;
  66. ProgramName = argv[0];
  67. for (doit = 0; doit < 2; doit++) { /* pre-parse to get display */
  68. for (i = 1; i < argc; i++) {
  69. char *arg = argv[i];
  70. if (arg[0] == '-') {
  71. switch (arg[1]) {
  72. case 'd': /* -display dpy */
  73. if (++i >= argc) usage ("-display requires an argument");
  74. if (!doit) displayname = argv[i];
  75. continue;
  76. case 'f': /* -format string */
  77. if (++i >= argc) usage ("-format requires an argument");
  78. if (doit) format = argv[i];
  79. continue;
  80. case 'r': /* -range num-[num] */
  81. if (++i >= argc) usage ("-range requires an argument");
  82. if (doit) {
  83. do_range (c, format, argv[i]);
  84. didit = 1;
  85. }
  86. continue;
  87. case 'n': /* -name string */
  88. if (++i >= argc) usage ("-name requires an argument");
  89. if (doit) {
  90. do_name (c, format, argv[i]);
  91. didit = 1;
  92. }
  93. continue;
  94. case 'v':
  95. if (strcmp(arg, "-version") == 0) {
  96. puts(PACKAGE_STRING);
  97. exit(0);
  98. }
  99. /* else FALLTHROUGH to unrecognized arg case below */
  100. }
  101. }
  102. fprintf (stderr, "%s: unrecognized argument %s\n\n",
  103. ProgramName, arg);
  104. usage (NULL);
  105. }
  106. if (!doit) {
  107. DisplayString = displayname;
  108. if (!DisplayString)
  109. DisplayString = getenv("DISPLAY");
  110. if (!DisplayString)
  111. DisplayString = "";
  112. c = xcb_connect(displayname, NULL);
  113. if (!c || xcb_connection_has_error(c)) {
  114. fprintf (stderr, "%s: unable to open display \"%s\"\n",
  115. ProgramName, DisplayString);
  116. exit (1);
  117. }
  118. } else
  119. if (!didit) /* no options, default is list all */
  120. list_atoms(c, format, 0, 0, 0);
  121. }
  122. xcb_disconnect(c);
  123. exit (0);
  124. }
  125. static void
  126. do_name(xcb_connection_t *c, const char *format, char *name)
  127. {
  128. xcb_intern_atom_reply_t *a = xcb_intern_atom_reply(c,
  129. xcb_intern_atom_unchecked(c, 1, strlen(name), name), NULL);
  130. if (a && a->atom != XCB_NONE) {
  131. printf (format, (unsigned long) a->atom, name);
  132. putchar ('\n');
  133. } else {
  134. fprintf (stderr, "%s: no atom named \"%s\" on server \"%s\"\n",
  135. ProgramName, name, DisplayString);
  136. }
  137. if (a)
  138. free(a);
  139. }
  140. #define RangeLow (1 << 0)
  141. #define RangeHigh (1 << 1)
  142. static int
  143. parse_range(char *range, long *lowp, long *highp)
  144. {
  145. char *dash;
  146. int mask = 0;
  147. if (!range) { /* NULL means default */
  148. *lowp = 1;
  149. return RangeLow;
  150. }
  151. dash = strchr(range, '-');
  152. if (!dash) dash = strchr(range, ':');
  153. if (dash) {
  154. if (dash == range) { /* -high */
  155. *lowp = 1;
  156. } else { /* low-[high] */
  157. *dash = '\0';
  158. *lowp = atoi (range);
  159. *dash = '-';
  160. }
  161. mask |= RangeLow;
  162. dash++;
  163. if (*dash) { /* [low]-high */
  164. *highp = atoi (dash);
  165. mask |= RangeHigh;
  166. }
  167. } else { /* number (low == high) */
  168. *lowp = *highp = atoi (range);
  169. mask |= (RangeLow | RangeHigh);
  170. }
  171. return mask;
  172. }
  173. static void
  174. do_range(xcb_connection_t *c, const char *format, char *range)
  175. {
  176. int mask;
  177. long low, high;
  178. mask = parse_range (range, &low, &high);
  179. list_atoms (c, format, mask, low, high);
  180. }
  181. static int
  182. say_batch(xcb_connection_t *c, const char *format, xcb_get_atom_name_cookie_t *cookie, long low, long count)
  183. {
  184. xcb_generic_error_t *e;
  185. char atom_name[1024];
  186. long i;
  187. int done = 0;
  188. for (i = 0; i < count; i++)
  189. cookie[i] = xcb_get_atom_name(c, i + low);
  190. for (i = 0; i < count; i++) {
  191. xcb_get_atom_name_reply_t *r;
  192. r = xcb_get_atom_name_reply(c, cookie[i], &e);
  193. if (r) {
  194. /* We could just use %.*s in 'format', but we want to be compatible
  195. with legacy command line usage */
  196. snprintf(atom_name, sizeof(atom_name), "%.*s",
  197. r->name_len, xcb_get_atom_name_name(r));
  198. printf (format, i + low, atom_name);
  199. putchar ('\n');
  200. free(r);
  201. }
  202. if (e) {
  203. done = 1;
  204. free(e);
  205. }
  206. }
  207. return done;
  208. }
  209. static void
  210. list_atoms(xcb_connection_t *c, const char *format, int mask, long low, long high)
  211. {
  212. xcb_get_atom_name_cookie_t *cookie_jar;
  213. int done = 0;
  214. switch (mask) {
  215. case RangeHigh:
  216. low = 1;
  217. /* fall through */
  218. case (RangeLow | RangeHigh):
  219. cookie_jar = malloc((high - low + 1) * sizeof(xcb_get_atom_name_cookie_t));
  220. if (!cookie_jar) {
  221. fprintf(stderr, "Out of memory allocating space for %ld atom requests\n", high - low);
  222. return;
  223. }
  224. say_batch(c, format, cookie_jar, low, high - low + 1);
  225. free(cookie_jar);
  226. break;
  227. default:
  228. low = 1;
  229. /* fall through */
  230. case RangeLow:
  231. cookie_jar = malloc(ATOMS_PER_BATCH * sizeof(xcb_get_atom_name_cookie_t));
  232. if (!cookie_jar) {
  233. fprintf(stderr, "Out of memory allocating space for %ld atom requests\n", (long) ATOMS_PER_BATCH);
  234. return;
  235. }
  236. while (!done) {
  237. done = say_batch(c, format, cookie_jar, low, ATOMS_PER_BATCH);
  238. low += ATOMS_PER_BATCH;
  239. }
  240. free(cookie_jar);
  241. break;
  242. }
  243. return;
  244. }