Commit | Line | Data |
---|---|---|
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 | |
90b2c5d4 | 38 | /*----- Main code ---------------------------------------------------------*/ |
39 | ||
f3b35b6b | 40 | static void version(FILE *fp) |
fb347ff4 | 41 | { pquis(fp, "$ (xtoys version " VERSION ")\n"); } |
f3b35b6b | 42 | |
43 | static void usage(FILE *fp) | |
fb347ff4 | 44 | { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); } |
cc52f3b6 MW |
45 | |
46 | static void help(FILE *fp) | |
f3b35b6b | 47 | { |
cc52f3b6 MW |
48 | version(fp); |
49 | fputc('\n', fp); | |
50 | usage(stdout); | |
fb347ff4 MW |
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", | |
cc52f3b6 | 66 | fp); |
f3b35b6b | 67 | } |
68 | ||
90b2c5d4 | 69 | int main(int argc, char *argv[]) |
70 | { | |
cc52f3b6 MW |
71 | Display *dpy; |
72 | const char *s; | |
90b2c5d4 | 73 | const char *display = 0; |
74 | unsigned f = 0; | |
75 | unsigned long wd, ht; | |
cc52f3b6 | 76 | int sc; |
90b2c5d4 | 77 | |
6672918d | 78 | #define f_sh 1u |
79 | #define f_csh 2u | |
80 | #define f_shell 3u | |
81 | #define f_export 4u | |
fb347ff4 | 82 | #define f_bogus 8u |
90b2c5d4 | 83 | |
84 | /* --- Parse command line options --- */ | |
85 | ||
f3b35b6b | 86 | ego(argv[0]); |
87 | ||
90b2c5d4 | 88 | for (;;) { |
f3b35b6b | 89 | static struct option opt[] = { |
c1c9f4d2 MW |
90 | { "help", 0, 0, 'h' }, |
91 | { "usage", 0, 0, 'u' }, | |
92 | { "version", 0, 0, 'v' }, | |
93 | { "display", OPTF_ARGREQ, 0, 'd' }, | |
deeda449 | 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 | ||
fb347ff4 MW |
100 | int i = getopt_long(argc, argv, "huv" "d:bcx", opt, 0); |
101 | if (i < 0) break; | |
90b2c5d4 | 102 | switch (i) { |
fb347ff4 MW |
103 | case 'h': help(stdout); exit(0); break; |
104 | case 'u': usage(stdout); exit(0); break; | |
105 | case 'v': version(stdout); exit(0); break; | |
106 | case 'd': display = optarg; break; | |
107 | case 'b': f |= f_sh; break; | |
108 | case 'c': f |= f_csh; break; | |
109 | case 'x': f |= f_export; break; | |
110 | default: f |= f_bogus; break; | |
90b2c5d4 | 111 | } |
112 | } | |
113 | ||
fb347ff4 MW |
114 | if (optind < argc) f |= f_bogus; |
115 | if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); } | |
116 | ||
90b2c5d4 | 117 | /* --- Sort out the shell type --- * |
118 | * | |
119 | * If the shell name contains the string `csh' then assume it's a C shell. | |
120 | * Otherwise assume it's Bourne. This seems to work in practice. | |
121 | */ | |
122 | ||
123 | if (!(f & f_shell)) { | |
cc52f3b6 | 124 | s = getenv("SHELL"); |
90b2c5d4 | 125 | if (!s) |
126 | f |= f_sh; | |
127 | if (strstr(s, "csh")) | |
128 | f |= f_csh; | |
129 | else | |
130 | f |= f_sh; | |
131 | } | |
132 | ||
133 | if ((f & f_sh) && (f & f_csh)) { | |
134 | fprintf(stderr, "xscsize: make your mind up about your shell type\n"); | |
135 | exit(EXIT_FAILURE); | |
136 | } | |
137 | ||
138 | /* --- Get the important information --- */ | |
139 | ||
cc52f3b6 MW |
140 | dpy = XOpenDisplay(display); |
141 | if (!dpy) { | |
142 | fprintf(stderr, "xscsize: couldn't open display\n"); | |
143 | exit(EXIT_FAILURE); | |
90b2c5d4 | 144 | } |
cc52f3b6 MW |
145 | sc = DefaultScreen(dpy); |
146 | wd = DisplayWidth(dpy, sc); | |
147 | ht = DisplayHeight(dpy, sc); | |
148 | XCloseDisplay(dpy); | |
90b2c5d4 | 149 | |
150 | /* --- Do the output thing --- */ | |
151 | ||
152 | if (f & f_sh) { | |
153 | printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht); | |
154 | if (f & f_export) | |
155 | printf("; export XWIDTH XHEIGHT"); | |
156 | } | |
157 | if (f & f_csh) { | |
158 | if (f & f_export) | |
159 | printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht); | |
160 | else | |
161 | printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht); | |
162 | } | |
163 | putchar('\n'); | |
164 | ||
165 | /* --- Done --- */ | |
166 | ||
167 | return (0); | |
168 | } | |
169 | ||
170 | /*----- That's all, folks -------------------------------------------------*/ |