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