mcgrew@dartagnan.rutgers.edu (Charles Mcgrew) (05/30/90)
Submitted-by: david@sun.com (indignation, dread, and nausea) Posting-number: Volume 2, Issue 15 Archive-name: refresh In article <49779@bbn.COM> you write: >I need an application (runnable in any window cmdtool/shelltool/vttool) >that can refresh the complete display. #! /bin/sh # This is a shar archive. Extract with sh, not csh. # The rest of this file will extract: # refresh.c echo extracting - refresh.c sed 's/^X//' > refresh.c << '/*EOF' X#if !defined(lint) && !defined(NOID) Xstatic char rcsID[] = X "$Header: refresh.c,v 1.3 88/12/14 19:42:20 david Exp $"; X#endif X X/* X * refresh/kill SunView desktop and/or print root window/screen name X * X * refresh [-krsw] [window|fb] X * X * -k kill X * -r refresh even if doing something else X * -s print screen name X * -w print root window name X * X * If no arg specified defaults to $WINDOW_PARENT, then /dev/fb. X */ X X/* cc -O -o refresh refresh.c -lsuntool -lsunwindow -lpixrect */ X X#include <sys/types.h> X#include <stdio.h> X#include <sunwindow/rect.h> X#include <sunwindow/win_screen.h> X Xstatic char *winname, *scrname; X Xstatic int smatch(); X Xmain(argc, argv) X int argc; X char *argv[]; X X{ X int c; X int kill = 0, print = 0, refresh = 1, screen = 0; X int winfd; X X extern int optind; X extern char *basename(), *emalloc(), *getenv(); X X#ifndef MERGE X setprogname(argv[0]); X#endif X X while ((c = getopt(argc, argv, "kprsw")) != EOF) X switch (c) { X case 'k': X kill++; X refresh = 0; X break; X case 'p': X case 'w': X print++; X refresh = 0; X break; X case 'r': X refresh++; X break; X case 's': X screen++; X refresh = 0; X break; X default: X error("usage: @P [-krsw] [window|fb]"); X } X X winname = getenv("WINDOW_PARENT"); X scrname = "/dev/fb"; X X if (argc > optind) { X if ((scrname = argv[optind])[0] != '/') { X scrname = emalloc(strlen(scrname) + 6); X sprintf(scrname, "/dev/%s", argv[optind]); X } X X winname = strncmp(basename(scrname), "win", 3) == 0 ? X scrname : 0; X } X X if (winname) { X scrname = 0; X (void) smatch(winfd = winopen(winname)); X (void) close(winfd); X } X else { X if (win_enumall(smatch) == -1) X error("window enumeration error@m"); X X if (!winname) X error("cannot find root window on %s", scrname); X } X X if (print) X printf("%s\n", winname); X X if (screen) X printf("%s\n", scrname); X X if (refresh || kill) X winfd = winopen(winname); X X if (refresh) X wmgr_refreshwindow(winfd); X X if (kill) X win_screendestroy(winfd); X X exit(0); X} X Xstatic int Xsmatch(fd) X int fd; X{ X struct screen screen; X char *estrdup(); X X (void) win_screenget(fd, &screen); X X if (scrname == 0 || X strncmp(scrname, screen.scr_fbname, SCR_NAMESIZE) == 0) { X winname = estrdup(screen.scr_rootname); X scrname = estrdup(screen.scr_fbname); X return 1; X } X X return 0; X} X Xstatic int Xwinopen(name) X char *name; X{ X int fd; X X if ((fd = open(name, 0, 0)) < 0) X error("cannot open window %s@m", name); X return fd; X} X X/* Library functions */ X X#define NOID X X#if !defined(lint) && !defined(NOID) Xstatic char rcsID[] = X "$Header: basename.c,v 1.1 87/11/06 12:52:46 david Exp $"; X#endif X Xchar * Xbasename(path) X char *path; X{ X register char *p = path, c; X X if (!p) X return ""; X X while (c = *p++) X if (c == '/') X path = p; X X return path; X} X X#if !defined(lint) && !defined(NOID) Xstatic char rcsID[] = X "$Header: estrdup.c,v 1.1 88/11/25 23:52:53 david Exp $"; X#endif X Xchar * Xestrdup(s) X char *s; X{ X register char *p; X register int len; X char *emalloc(); X X len = strlen(s) + 1; X p = emalloc(len); X bcopy(s, p, len); X X return p; X} X X#if !defined(lint) && !defined(NOID) Xstatic char rcsID[] = X "$Header: progname.c,v 1.1 87/11/06 12:57:17 david Exp $"; X#endif X Xchar *_progname; Xextern char *basename(); X Xsetprogname(name) X char *name; X{ X _progname = basename(name); X} X Xchar * Xgetprogname(name) X{ X return _progname ? _progname : ""; X} X/* $Header: error.h,v 1.1 88/11/25 23:52:34 david Exp $ */ X X#ifndef error_h X#define error_h X Xstruct _Err { X int exit; X FILE *file; X int perror; X int stat; X} _Err; X X#endif error_h X/* $Header: errdata.c,v 1.1 88/11/25 23:51:51 david Exp $ */ X X#include <stdio.h> X/*#include "error.h"*/ X Xstruct _Err _Err = { X 1, /* exit */ X stderr, /* file */ X 0, /* perror */ X 1, /* stat */ X}; X X#if !defined(lint) && !defined(NOID) Xstatic char rcsID[] = X "$Header: error.c,v 1.4 88/11/25 23:52:16 david Exp $"; X#endif X X X/* X * error("format" [, arg ...]) X * X * exit @e X * no exit @w X * no newline @c X * perror here @M X * trailing perror @m X * no trailing perror @n X * progname here @P X * leading progname @p X * no leading progname @q X * stdout @> X * stderr @! X */ X X#include <stdio.h> X#include <varargs.h> X/*#include "error.h"*/ X X#ifdef sparc X#include <alloca.h> X#endif X Xextern int errno; Xextern int sys_nerr; Xextern char *sys_errlist[]; Xextern char *malloc(), *alloca(); Xextern char *strcpy(); X Xextern char *getprogname(); X X/*VARARGS*/ Xerror(va_alist) X va_dcl X{ X va_list ap; X char *fmt; X int res; X X va_start(ap); X fmt = va_arg(ap, char *); X res = verror(fmt, ap); X va_end(ap); X return res; X} X X/*VARARGS1*/ Xverror(fmt, ap) X register char *fmt; X va_list ap; X{ X char *newfmt; X int errnum = errno; X int quit = _Err.exit; X FILE *f = _Err.file; X int newlen; X /* +1 = don't perror, 0 = trailing, -1 = embedded */ X int noerr = (!_Err.perror || errnum == 0); X int err = 0; X int nonl = 0; X int noprog = 0, prog = 0; X register char *p, c; X int errlen; X char *errstr; X int proglen; X char *progname; X X if (!fmt) X fmt = "unknown error"; X X /* pass 1 -- compute length of new format string */ X X newlen = 1; /* for trailing null */ X p = fmt; X X while (c = *p++) { X if (c != '@') { X newlen++; X continue; X } X X switch (c = *p++) { X case 'c': X nonl = 1; X break; X case 'e': X quit = 0; X break; X case 'w': X quit = 1; X break; X case 'M': X err++; X noerr = -1; X break; X case 'm': X noerr = 0; X break; X case 'n': X noerr = 1; X break; X case 'P': X prog++; X noprog = -1; X break; X case 'p': X noprog = 0; X break; X case 'q': X noprog = 1; X break; X case '>': X case '!': X break; X case 0: X p--; X /* fall through */ X case '@': X newlen++; X break; X default: X newlen += 2; X break; X } X } X X if (noprog <= 0) { X proglen = strlen(progname = getprogname()); X X newlen += proglen * prog; X } X X if (noerr <= 0) { X errlen = errnum < sys_nerr ? X strlen(errstr = sys_errlist[errnum]) : X sizeof "Error -2000000000"; X X newlen += errlen * err; X } X X /* allocate new format string */ X X /* special case, null format */ X if (newlen == 0) X p = ""; X /* first try malloc */ X else if (!(p = malloc((unsigned) newlen))) X /* if malloc fails try alloca */ X if (p = alloca((unsigned) newlen)) X newlen = 0; X /* if alloca fails use format as is */ X else { X /* make sure progname or error is printed */ X if (prog) X noprog = 0; X if (err) X noerr = 0; X } X newfmt = p; X X /* pass 2: build new format string */ X X while (c = *fmt++) { X if (c != '@') { X *p++ = c; X continue; X } X switch (c = *fmt++) { X case 'c': X break; X case 'e': X break; X case 'w': X break; X case 'M': X if (errnum < sys_nerr) X strcpy(p, errstr); X else X sprintf(p, "_Error %d", errnum); X while (*p) X p++; X break; X case 'm': X break; X case 'n': X break; X case 'P': X strcpy(p, progname); X p += proglen; X break; X case 'p': X break; X case 'q': X break; X case '>': X case '!': X break; X case 0: X fmt--; X /* fall through */ X case '@': X *p++ = '@'; X break; X default: X *p++ = '@'; X *p++ = c; X break; X } X } X *p = 0; X X if (!noprog) X fprintf(f, "%s: ", progname); X X vfprintf(f, newfmt, ap); X X if (!noerr) X if (errnum < sys_nerr) X fprintf(f, " (%s)", errstr); X else X fprintf(f, " (_Error %d)", errnum); X X if (!nonl) X putc('\n', f); X X if (quit) X exit(_Err.stat); X X if (newlen) X free(newfmt); X X errno = errnum; X return 0; X} X X#if !defined(lint) && !defined(NOID) Xstatic char rcsID[] = X "$Header: emalloc.c,v 1.4 88/11/25 23:51:46 david Exp $"; X#endif X X/* X * emalloc.c -- allocate memory, quit on failure X */ X Xchar * Xemalloc(size) X unsigned size; X{ X char *p; X extern char *malloc(); X X if (p = malloc(size)) X return p; X X error("cannot allocate %u bytes", size); X /*NOTREACHED*/ X} /*EOF exit -- David DiGiacomo, Sun Microsystems, Mt. View, CA sun!david david@eng.sun.com