chiark / gitweb /
Block SIGCHLD around the `fork' call to prevent a race.
[xtoys] / xscsize.c
1 /* -*-c-*-
2  *
3  * $Id: xscsize.c,v 1.3 1998/12/11 09:50:05 mdw Exp $
4  *
5  * Return X display size to shell script
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: xscsize.c,v $
32  * Revision 1.3  1998/12/11 09:50:05  mdw
33  * Minor modifications to work with mLib and mgLib.
34  *
35  * Revision 1.2  1998/11/21 22:30:22  mdw
36  * Support GNU-style long options throughout, and introduce proper help
37  * text to all programs.  Update manual pages to match.
38  *
39  * Revision 1.1  1998/11/16 23:00:49  mdw
40  * Initial versions.
41  *
42  */
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include <X11/Xlib.h>
51
52 #include <mLib/mdwopt.h>
53 #include <mLib/quis.h>
54
55 /*----- Main code ---------------------------------------------------------*/
56
57 static void version(FILE *fp)
58 {
59   fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
60 }
61
62 static void usage(FILE *fp)
63 {
64   fprintf(fp, "Usage: %s [-bcx] [-d display]\n", QUIS);
65 }
66
67 int main(int argc, char *argv[])
68 {
69   const char *display = 0;
70   unsigned f = 0;
71   unsigned long wd, ht;
72
73   enum {
74     f_sh = 1,
75     f_csh = 2,
76     f_shell = 3,
77     f_export = 4
78   };
79
80   /* --- Parse command line options --- */
81
82   ego(argv[0]);
83
84   for (;;) {
85     static struct option opt[] = {
86       { "help",         0,                      0,      'h' },
87       { "usage",        0,                      0,      'u' },
88       { "version",      0,                      0,      'v' },
89       { "display",      required_argument,      0,      'd' },
90       { "bourne-shell", 0,                      0,      'b' },
91       { "c-shell",      0,                      0,      'c' },
92       { "export",       0,                      0,      'x' },
93       { 0,              0,                      0,      0 }
94     };
95
96     int i = getopt_long(argc, argv, "huv d:bcx", 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 "Reads the size of the X root window and outputs it in a form suitable\n"
107 "for use as a shell assignment statement, defining variables XWIDTH and\n"
108 "XHEIGHT.\n"
109 "\n"
110 "Options:\n"
111 "\n"
112 "-h, --help             Display this help text\n"
113 "-u, --usage            Display a short usage summary\n"
114 "-v, --version          Display the program's version number\n"
115 "\n"
116 "-d, --display=DISPLAY  Choose X display to connect to\n"
117 "-b, --bourne-shell     Output text suitable for a Bourne shell\n"
118 "-c, --c-shell          Output text suitable for a C shell\n"
119 "-x, --export           Export the variables into the environment\n",
120           stdout);
121         exit(0);
122         break;
123       case 'u':
124         usage(stdout);
125         exit(0);
126         break;
127       case 'v':
128         version(stdout);
129         exit(0);
130         break;
131         
132       case 'd':
133         display = optarg;
134         break;
135       case 'b':
136         f |= f_sh;
137         break;
138       case 'c':
139         f |= f_csh;
140         break;
141       case 'x':
142         f |= f_export;
143         break;
144       default:
145         usage(stderr);
146         exit(EXIT_FAILURE);
147         break;
148     }
149   }
150
151   /* --- Sort out the shell type --- *
152    *
153    * If the shell name contains the string `csh' then assume it's a C shell.
154    * Otherwise assume it's Bourne.  This seems to work in practice.
155    */
156
157   if (!(f & f_shell)) {
158     const char *s = getenv("SHELL");
159     if (!s)
160       f |= f_sh;
161     if (strstr(s, "csh"))
162       f |= f_csh;
163     else
164       f |= f_sh;
165   }
166
167   if ((f & f_sh) && (f & f_csh)) {
168     fprintf(stderr, "xscsize: make your mind up about your shell type\n");
169     exit(EXIT_FAILURE);
170   }
171
172   /* --- Get the important information --- */
173
174   {
175     Display *dpy = XOpenDisplay(display);
176     int sc;
177     if (!dpy) {
178       fprintf(stderr, "xscsize: couldn't open display\n");
179       exit(EXIT_FAILURE);
180     }
181     sc = DefaultScreen(dpy);
182     wd = DisplayWidth(dpy, sc);
183     ht = DisplayHeight(dpy, sc);
184     XCloseDisplay(dpy);
185   }
186
187   /* --- Do the output thing --- */
188
189   if (f & f_sh) {
190     printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht);
191     if (f & f_export)
192       printf("; export XWIDTH XHEIGHT");
193   }
194   if (f & f_csh) {
195     if (f & f_export)
196       printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht);
197     else
198       printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht);
199   }
200   putchar('\n');
201
202   /* --- Done --- */
203
204   return (0);
205 }
206
207 /*----- That's all, folks -------------------------------------------------*/