chiark / gitweb /
xscsize.c: Move reporting variables to a separate function.
[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 /*----- Static variables --------------------------------------------------*/
39
40 static unsigned int flags = 0;
41 #define F_SH 1u
42 #define F_CSH 2u
43 #define F_SHELL 3u
44 #define F_EXPORT 4u
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 static void version(FILE *fp)
49   { pquis(fp, "$ (xtoys version " VERSION ")\n"); }
50
51 static void usage(FILE *fp)
52   { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); }
53
54 static void help(FILE *fp)
55 {
56   version(fp);
57   fputc('\n', fp);
58   usage(stdout);
59   fputs("\n\
60 Reads the size of the X root window and outputs it in a form suitable\n\
61 for use as a shell assignment statement, defining variables XWIDTH and\n\
62 XHEIGHT.\n\
63 \n\
64 Options:\n\
65 \n\
66 -h, --help              Display this help text\n\
67 -u, --usage             Display a short usage summary\n\
68 -v, --version           Display the program's version number\n\
69 \n\
70 -d, --display=DISPLAY   Choose X display to connect to\n\
71 -b, --bourne-shell      Output text suitable for a Bourne shell\n\
72 -c, --c-shell           Output text suitable for a C shell\n\
73 -x, --export            Export the variables into the environment\n",
74         fp);
75 }
76
77 static void print_var(const char *name, unsigned long value)
78 {
79   if (index >= 0) {
80     dstr_putf(&d, "XSCR%d_%s", index, name);
81     name = d.buf;
82   }
83   if (flags & F_SH) {
84     printf("%s=%lu", name, value);
85     if (flags & F_EXPORT) printf("; export %s", name);
86   } else if (flags & F_CSH) {
87     if (flags & F_EXPORT) printf("setenv %s %lu", name, value);
88     else printf("set %s=%lu", name, value);
89   }
90   putchar('\n');
91   dstr_destroy(&d);
92 }
93
94 int main(int argc, char *argv[])
95 {
96   Display *dpy;
97   const char *s;
98   const char *display = 0;
99   unsigned f = 0;
100   unsigned long wd, ht;
101   int sc;
102
103 #define f_bogus 1u
104
105   /* --- Parse command line options --- */
106
107   ego(argv[0]);
108
109   for (;;) {
110     static struct option opt[] = {
111       { "help",         0,              0,      'h' },
112       { "usage",        0,              0,      'u' },
113       { "version",      0,              0,      'v' },
114       { "display",      OPTF_ARGREQ,    0,      'd' },
115       { "bourne-shell", 0,              0,      'b' },
116       { "c-shell",      0,              0,      'c' },
117       { "export",       0,              0,      'x' },
118       { 0,              0,              0,      0 }
119     };
120
121     int i = getopt_long(argc, argv, "huv" "d:bcx", opt, 0);
122     if (i < 0) break;
123     switch (i) {
124       case 'h': help(stdout); exit(0); break;
125       case 'u': usage(stdout); exit(0); break;
126       case 'v': version(stdout); exit(0); break;
127       case 'd': display = optarg; break;
128       case 'b': flags |= F_SH; break;
129       case 'c': flags |= F_CSH; break;
130       case 'x': flags |= F_EXPORT; break;
131       default: f |= f_bogus; break;
132     }
133   }
134
135   if (optind < argc) f |= f_bogus;
136   if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); }
137
138   /* --- Sort out the shell type --- *
139    *
140    * If the shell name contains the string `csh' then assume it's a C shell.
141    * Otherwise assume it's Bourne.  This seems to work in practice.
142    */
143
144   if (!(flags & F_SHELL)) {
145     s = getenv("SHELL");
146     if (!s) flags |= F_SH;
147     if (strstr(s, "csh")) flags |= F_CSH;
148     else flags |= F_SH;
149   }
150
151   if ((flags & F_SH) && (flags & 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   print_var("XWIDTH", wd);
171   print_var("XHEIGHT", ht);
172
173   /* --- Done --- */
174
175   return (0);
176 }
177
178 /*----- That's all, folks -------------------------------------------------*/