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