chiark / gitweb /
A slew of whitespace cleanups.
[xtoys] / xscsize.c
1 /* -*-c-*-
2  *
3  * Return X display size to shell script
4  *
5  * (c) 1998 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the Edgeware X tools collection.
11  *
12  * X tools is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * X tools is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with X tools; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <X11/Xlib.h>
34
35 #include <mLib/mdwopt.h>
36 #include <mLib/quis.h>
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 static void version(FILE *fp)
41   { fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); }
42
43 static void usage(FILE *fp)
44   { fprintf(fp, "Usage: %s [-bcx] [-d display]\n", QUIS); }
45
46 static void help(FILE *fp)
47 {
48   version(fp);
49   fputc('\n', fp);
50   usage(stdout);
51   fputs("\n"
52 "Reads the size of the X root window and outputs it in a form suitable\n"
53 "for use as a shell assignment statement, defining variables XWIDTH and\n"
54 "XHEIGHT.\n"
55 "\n"
56 "Options:\n"
57 "\n"
58 "-h, --help             Display this help text\n"
59 "-u, --usage            Display a short usage summary\n"
60 "-v, --version          Display the program's version number\n"
61 "\n"
62 "-d, --display=DISPLAY  Choose X display to connect to\n"
63 "-b, --bourne-shell     Output text suitable for a Bourne shell\n"
64 "-c, --c-shell          Output text suitable for a C shell\n"
65 "-x, --export           Export the variables into the environment\n",
66         fp);
67 }
68
69 int main(int argc, char *argv[])
70 {
71   Display *dpy;
72   const char *s;
73   const char *display = 0;
74   unsigned f = 0;
75   unsigned long wd, ht;
76   int sc;
77
78 #define f_sh 1u
79 #define f_csh 2u
80 #define f_shell 3u
81 #define f_export 4u
82
83   /* --- Parse command line options --- */
84
85   ego(argv[0]);
86
87   for (;;) {
88     static struct option opt[] = {
89       { "help",         0,              0,      'h' },
90       { "usage",        0,              0,      'u' },
91       { "version",      0,              0,      'v' },
92       { "display",      OPTF_ARGREQ,    0,      'd' },
93       { "bourne-shell", 0,              0,      'b' },
94       { "c-shell",      0,              0,      'c' },
95       { "export",       0,              0,      'x' },
96       { 0,              0,              0,      0 }
97     };
98
99     int i = getopt_long(argc, argv, "huv d:bcx", opt, 0);
100     if (i < 0)
101       break;
102     switch (i) {
103       case 'h':
104         help(stdout);
105         exit(0);
106         break;
107       case 'u':
108         usage(stdout);
109         exit(0);
110         break;
111       case 'v':
112         version(stdout);
113         exit(0);
114         break;
115
116       case 'd':
117         display = optarg;
118         break;
119       case 'b':
120         f |= f_sh;
121         break;
122       case 'c':
123         f |= f_csh;
124         break;
125       case 'x':
126         f |= f_export;
127         break;
128       default:
129         usage(stderr);
130         exit(EXIT_FAILURE);
131         break;
132     }
133   }
134
135   /* --- Sort out the shell type --- *
136    *
137    * If the shell name contains the string `csh' then assume it's a C shell.
138    * Otherwise assume it's Bourne.  This seems to work in practice.
139    */
140
141   if (!(f & f_shell)) {
142     s = getenv("SHELL");
143     if (!s)
144       f |= f_sh;
145     if (strstr(s, "csh"))
146       f |= f_csh;
147     else
148       f |= f_sh;
149   }
150
151   if ((f & f_sh) && (f & f_csh)) {
152     fprintf(stderr, "xscsize: make your mind up about your shell type\n");
153     exit(EXIT_FAILURE);
154   }
155
156   /* --- Get the important information --- */
157
158   dpy = XOpenDisplay(display);
159   if (!dpy) {
160     fprintf(stderr, "xscsize: couldn't open display\n");
161     exit(EXIT_FAILURE);
162   }
163   sc = DefaultScreen(dpy);
164   wd = DisplayWidth(dpy, sc);
165   ht = DisplayHeight(dpy, sc);
166   XCloseDisplay(dpy);
167
168   /* --- Do the output thing --- */
169
170   if (f & f_sh) {
171     printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht);
172     if (f & f_export)
173       printf("; export XWIDTH XHEIGHT");
174   }
175   if (f & f_csh) {
176     if (f & f_export)
177       printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht);
178     else
179       printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht);
180   }
181   putchar('\n');
182
183   /* --- Done --- */
184
185   return (0);
186 }
187
188 /*----- That's all, folks -------------------------------------------------*/