chiark / gitweb /
Return exit status of child process, rather than always returning
[xtoys] / xtell.c
1 /* -*-c-*-
2  *
3  * $Id: xtell.c,v 1.4 1998/12/11 09:50:06 mdw Exp $
4  *
5  * Wake up a waiting xwait process
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the Edgeware X tools collection.
13  *
14  * X tools is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * X tools is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with X tools; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: xtell.c,v $
32  * Revision 1.4  1998/12/11 09:50:06  mdw
33  * Minor modifications to work with mLib and mgLib.
34  *
35  * Revision 1.3  1998/11/30 22:36:51  mdw
36  * Tidy up tabbing in help texts very slightly.
37  *
38  * Revision 1.2  1998/11/21 22:30:25  mdw
39  * Support GNU-style long options throughout, and introduce proper help
40  * text to all programs.  Update manual pages to match.
41  *
42  * Revision 1.1  1998/11/16 23:00:49  mdw
43  * Initial versions.
44  *
45  */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include <X11/Xlib.h>
54 #include <X11/Xutil.h>
55
56 #include <mLib/mdwopt.h>
57 #include <mLib/quis.h>
58
59 #include "xwait.h"
60
61 /*----- Main code ---------------------------------------------------------*/
62
63 static void version(FILE *fp)
64 {
65   fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
66 }
67
68 static void usage(FILE *fp)
69 {
70   fprintf(fp, "Usage: %s [-d DISPLAY] [-a ATOM] [-m MSG]\n", QUIS);
71 }
72
73 int main(int argc, char *argv[])
74 {
75   char *display = 0;
76   Display *dpy;
77   Atom xwait_die;
78   char *atom = XWAIT_DIE;
79   char *msg = XWAIT_DIE_MSG;
80
81   /* --- Parse options --- */
82
83   ego(argv[0]);
84
85   for (;;) {
86     static struct option opt[] = {
87       { "help",         0,                      0,      'h' },
88       { "usage",        0,                      0,      'u' },
89       { "version",      0,                      0,      'v' },
90       { "display",      required_argument,      0,      'd' },
91       { "atom",         required_argument,      0,      'a' },
92       { "msg",          required_argument,      0,      'm' },
93       { 0,              0,                      0,      0 }
94     };
95
96     int i = getopt_long(argc, argv, "d:a:m:", opt, 0);
97     if (i < 0)
98       break;
99     switch (i) {
100       case 'h':
101         version(stdout);
102         fputs("\n", stdout);
103         usage(stdout);
104         fputs(
105 "\n"
106 "Signals a waiting `xwait' process.  Specifically, writes a property\n"
107 "named ATOM to the X root window, with value MSG.\n"
108 "\n"
109 "Options:\n"
110 "\n"
111 "-h, --help             Display this help text\n"
112 "-u, --usage            Display a short usage summary\n"
113 "-v, --version          Display the program's version number\n"
114 "\n"
115 "-d, --display=DISPLAY  Choose X display to connect to\n"
116 "-a, --atom=ATOM\t      Choose property name to set\n"
117 "-m, --msg=MSG          Choose value of property to set\n",
118           stdout);
119         exit(0);
120         break;
121       case 'u':
122         usage(stdout);
123         exit(0);
124         break;
125       case 'v':
126         version(stdout);
127         exit(0);
128         break;
129         
130       case 'd':
131         display = optarg;
132         break;
133       case 'a':
134         atom = optarg;
135         break;
136       case 'm':
137         msg = optarg;
138         break;
139       default:
140         fprintf(stderr, "Usage: xtell [-d DISPLAY] [-a ATOM] [-m MSG]\n");
141         exit(EXIT_FAILURE);
142         break;
143     }
144   }
145
146   /* --- Connect to the display --- */
147
148   dpy = XOpenDisplay(display);
149   if (!dpy) {
150     fprintf(stderr, "xtell: couldn't open display\n");
151     exit(EXIT_FAILURE);
152   }
153
154   /* --- Find the right atom --- */
155
156   xwait_die = XInternAtom(dpy, atom, False);
157
158   /* --- Set the property value --- */
159
160   {
161     XTextProperty prop;
162     if (!XGetTextProperty(dpy, DefaultRootWindow(dpy), &prop, xwait_die)) {
163       fprintf(stderr, "xtell: no xwait listening for `%s'\n", atom);
164       exit(EXIT_FAILURE);
165     }
166     XStringListToTextProperty(&msg, 1, &prop);
167     XSetTextProperty(dpy, DefaultRootWindow(dpy), &prop, xwait_die);
168   }
169
170   /* --- Done --- */
171
172   XCloseDisplay(dpy);
173   return (0);
174 }
175
176 /*----- That's all, folks -------------------------------------------------*/