xscsize.c: Publish the output-format flags globally.
[xtoys] / xscsize.c
CommitLineData
90b2c5d4 1/* -*-c-*-
2 *
90b2c5d4 3 * Return X display size to shell script
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
cc52f3b6 8/*----- Licensing notice --------------------------------------------------*
90b2c5d4 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.
cc52f3b6 16 *
90b2c5d4 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.
cc52f3b6 21 *
90b2c5d4 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
90b2c5d4 27/*----- Header files ------------------------------------------------------*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
90b2c5d4 33#include <X11/Xlib.h>
34
c4efa11c 35#include <mLib/mdwopt.h>
36#include <mLib/quis.h>
f3b35b6b 37
3a4bdbcb
MW
38/*----- Static variables --------------------------------------------------*/
39
40static 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
90b2c5d4 46/*----- Main code ---------------------------------------------------------*/
47
f3b35b6b 48static void version(FILE *fp)
fb347ff4 49 { pquis(fp, "$ (xtoys version " VERSION ")\n"); }
f3b35b6b 50
51static void usage(FILE *fp)
fb347ff4 52 { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); }
cc52f3b6
MW
53
54static void help(FILE *fp)
f3b35b6b 55{
cc52f3b6
MW
56 version(fp);
57 fputc('\n', fp);
58 usage(stdout);
fb347ff4
MW
59 fputs("\n\
60Reads the size of the X root window and outputs it in a form suitable\n\
61for use as a shell assignment statement, defining variables XWIDTH and\n\
62XHEIGHT.\n\
63\n\
64Options:\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",
cc52f3b6 74 fp);
f3b35b6b 75}
76
90b2c5d4 77int main(int argc, char *argv[])
78{
cc52f3b6
MW
79 Display *dpy;
80 const char *s;
90b2c5d4 81 const char *display = 0;
82 unsigned f = 0;
83 unsigned long wd, ht;
cc52f3b6 84 int sc;
90b2c5d4 85
3a4bdbcb 86#define f_bogus 1u
90b2c5d4 87
88 /* --- Parse command line options --- */
89
f3b35b6b 90 ego(argv[0]);
91
90b2c5d4 92 for (;;) {
f3b35b6b 93 static struct option opt[] = {
c1c9f4d2
MW
94 { "help", 0, 0, 'h' },
95 { "usage", 0, 0, 'u' },
96 { "version", 0, 0, 'v' },
97 { "display", OPTF_ARGREQ, 0, 'd' },
deeda449 98 { "bourne-shell", 0, 0, 'b' },
99 { "c-shell", 0, 0, 'c' },
100 { "export", 0, 0, 'x' },
101 { 0, 0, 0, 0 }
f3b35b6b 102 };
103
fb347ff4
MW
104 int i = getopt_long(argc, argv, "huv" "d:bcx", opt, 0);
105 if (i < 0) break;
90b2c5d4 106 switch (i) {
fb347ff4
MW
107 case 'h': help(stdout); exit(0); break;
108 case 'u': usage(stdout); exit(0); break;
109 case 'v': version(stdout); exit(0); break;
110 case 'd': display = optarg; break;
3a4bdbcb
MW
111 case 'b': flags |= F_SH; break;
112 case 'c': flags |= F_CSH; break;
113 case 'x': flags |= F_EXPORT; break;
fb347ff4 114 default: f |= f_bogus; break;
90b2c5d4 115 }
116 }
117
fb347ff4
MW
118 if (optind < argc) f |= f_bogus;
119 if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); }
120
90b2c5d4 121 /* --- Sort out the shell type --- *
122 *
123 * If the shell name contains the string `csh' then assume it's a C shell.
124 * Otherwise assume it's Bourne. This seems to work in practice.
125 */
126
3a4bdbcb 127 if (!(flags & F_SHELL)) {
cc52f3b6 128 s = getenv("SHELL");
3a4bdbcb
MW
129 if (!s) flags |= F_SH;
130 if (strstr(s, "csh")) flags |= F_CSH;
131 else flags |= F_SH;
90b2c5d4 132 }
133
3a4bdbcb 134 if ((flags & F_SH) && (flags & F_CSH)) {
90b2c5d4 135 fprintf(stderr, "xscsize: make your mind up about your shell type\n");
136 exit(EXIT_FAILURE);
137 }
138
139 /* --- Get the important information --- */
140
cc52f3b6
MW
141 dpy = XOpenDisplay(display);
142 if (!dpy) {
143 fprintf(stderr, "xscsize: couldn't open display\n");
144 exit(EXIT_FAILURE);
90b2c5d4 145 }
cc52f3b6
MW
146 sc = DefaultScreen(dpy);
147 wd = DisplayWidth(dpy, sc);
148 ht = DisplayHeight(dpy, sc);
149 XCloseDisplay(dpy);
90b2c5d4 150
151 /* --- Do the output thing --- */
152
153 if (f & f_sh) {
154 printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht);
155 if (f & f_export)
156 printf("; export XWIDTH XHEIGHT");
157 }
158 if (f & f_csh) {
159 if (f & f_export)
160 printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht);
161 else
162 printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht);
163 }
164 putchar('\n');
165
166 /* --- Done --- */
167
168 return (0);
169}
170
171/*----- That's all, folks -------------------------------------------------*/