/* -*-c-*-
*
- * $Id: xscsize.c,v 1.6 2004/04/08 01:36:29 mdw Exp $
- *
* Return X display size to shell script
*
* (c) 1998 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of the Edgeware X tools collection.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
+ *
* X tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with X tools; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <mLib/mdwopt.h>
#include <mLib/quis.h>
+/*----- Static variables --------------------------------------------------*/
+
+static unsigned int flags = 0;
+#define F_SH 1u
+#define F_CSH 2u
+#define F_SHELL 3u
+#define F_EXPORT 4u
+
/*----- Main code ---------------------------------------------------------*/
static void version(FILE *fp)
+ { pquis(fp, "$ (xtoys version " VERSION ")\n"); }
+
+static void usage(FILE *fp)
+ { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); }
+
+static void help(FILE *fp)
{
- fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
+ version(fp);
+ fputc('\n', fp);
+ usage(stdout);
+ fputs("\n\
+Reads the size of the X root window and outputs it in a form suitable\n\
+for use as a shell assignment statement, defining variables XWIDTH and\n\
+XHEIGHT.\n\
+\n\
+Options:\n\
+\n\
+-h, --help Display this help text\n\
+-u, --usage Display a short usage summary\n\
+-v, --version Display the program's version number\n\
+\n\
+-d, --display=DISPLAY Choose X display to connect to\n\
+-b, --bourne-shell Output text suitable for a Bourne shell\n\
+-c, --c-shell Output text suitable for a C shell\n\
+-x, --export Export the variables into the environment\n",
+ fp);
}
-static void usage(FILE *fp)
+static void print_var(const char *name, unsigned long value)
{
- fprintf(fp, "Usage: %s [-bcx] [-d display]\n", QUIS);
+ if (index >= 0) {
+ dstr_putf(&d, "XSCR%d_%s", index, name);
+ name = d.buf;
+ }
+ if (flags & F_SH) {
+ printf("%s=%lu", name, value);
+ if (flags & F_EXPORT) printf("; export %s", name);
+ } else if (flags & F_CSH) {
+ if (flags & F_EXPORT) printf("setenv %s %lu", name, value);
+ else printf("set %s=%lu", name, value);
+ }
+ putchar('\n');
+ dstr_destroy(&d);
}
int main(int argc, char *argv[])
{
+ Display *dpy;
+ const char *s;
const char *display = 0;
unsigned f = 0;
unsigned long wd, ht;
+ int sc;
-#define f_sh 1u
-#define f_csh 2u
-#define f_shell 3u
-#define f_export 4u
+#define f_bogus 1u
/* --- Parse command line options --- */
for (;;) {
static struct option opt[] = {
- { "help", 0, 0, 'h' },
- { "usage", 0, 0, 'u' },
- { "version", 0, 0, 'v' },
- { "display", OPTF_ARGREQ, 0, 'd' },
+ { "help", 0, 0, 'h' },
+ { "usage", 0, 0, 'u' },
+ { "version", 0, 0, 'v' },
+ { "display", OPTF_ARGREQ, 0, 'd' },
{ "bourne-shell", 0, 0, 'b' },
{ "c-shell", 0, 0, 'c' },
{ "export", 0, 0, 'x' },
{ 0, 0, 0, 0 }
};
- int i = getopt_long(argc, argv, "huv d:bcx", opt, 0);
- if (i < 0)
- break;
+ int i = getopt_long(argc, argv, "huv" "d:bcx", opt, 0);
+ if (i < 0) break;
switch (i) {
- case 'h':
- version(stdout);
- fputs("\n", stdout);
- usage(stdout);
- fputs(
-"\n"
-"Reads the size of the X root window and outputs it in a form suitable\n"
-"for use as a shell assignment statement, defining variables XWIDTH and\n"
-"XHEIGHT.\n"
-"\n"
-"Options:\n"
-"\n"
-"-h, --help Display this help text\n"
-"-u, --usage Display a short usage summary\n"
-"-v, --version Display the program's version number\n"
-"\n"
-"-d, --display=DISPLAY Choose X display to connect to\n"
-"-b, --bourne-shell Output text suitable for a Bourne shell\n"
-"-c, --c-shell Output text suitable for a C shell\n"
-"-x, --export Export the variables into the environment\n",
- stdout);
- exit(0);
- break;
- case 'u':
- usage(stdout);
- exit(0);
- break;
- case 'v':
- version(stdout);
- exit(0);
- break;
-
- case 'd':
- display = optarg;
- break;
- case 'b':
- f |= f_sh;
- break;
- case 'c':
- f |= f_csh;
- break;
- case 'x':
- f |= f_export;
- break;
- default:
- usage(stderr);
- exit(EXIT_FAILURE);
- break;
+ case 'h': help(stdout); exit(0); break;
+ case 'u': usage(stdout); exit(0); break;
+ case 'v': version(stdout); exit(0); break;
+ case 'd': display = optarg; break;
+ case 'b': flags |= F_SH; break;
+ case 'c': flags |= F_CSH; break;
+ case 'x': flags |= F_EXPORT; break;
+ default: f |= f_bogus; break;
}
}
+ if (optind < argc) f |= f_bogus;
+ if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); }
+
/* --- Sort out the shell type --- *
*
* If the shell name contains the string `csh' then assume it's a C shell.
* Otherwise assume it's Bourne. This seems to work in practice.
*/
- if (!(f & f_shell)) {
- const char *s = getenv("SHELL");
- if (!s)
- f |= f_sh;
- if (strstr(s, "csh"))
- f |= f_csh;
- else
- f |= f_sh;
+ if (!(flags & F_SHELL)) {
+ s = getenv("SHELL");
+ if (!s) flags |= F_SH;
+ if (strstr(s, "csh")) flags |= F_CSH;
+ else flags |= F_SH;
}
- if ((f & f_sh) && (f & f_csh)) {
+ if ((flags & F_SH) && (flags & F_CSH)) {
fprintf(stderr, "xscsize: make your mind up about your shell type\n");
exit(EXIT_FAILURE);
}
/* --- Get the important information --- */
- {
- Display *dpy = XOpenDisplay(display);
- int sc;
- if (!dpy) {
- fprintf(stderr, "xscsize: couldn't open display\n");
- exit(EXIT_FAILURE);
- }
- sc = DefaultScreen(dpy);
- wd = DisplayWidth(dpy, sc);
- ht = DisplayHeight(dpy, sc);
- XCloseDisplay(dpy);
+ dpy = XOpenDisplay(display);
+ if (!dpy) {
+ fprintf(stderr, "xscsize: couldn't open display\n");
+ exit(EXIT_FAILURE);
}
+ sc = DefaultScreen(dpy);
+ wd = DisplayWidth(dpy, sc);
+ ht = DisplayHeight(dpy, sc);
+ XCloseDisplay(dpy);
/* --- Do the output thing --- */
- if (f & f_sh) {
- printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht);
- if (f & f_export)
- printf("; export XWIDTH XHEIGHT");
- }
- if (f & f_csh) {
- if (f & f_export)
- printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht);
- else
- printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht);
- }
- putchar('\n');
+ print_var("XWIDTH", wd);
+ print_var("XHEIGHT", ht);
/* --- Done --- */