dsimple.c revision 94a251fd
1/* $Xorg: dsimple.c,v 1.4 2001/02/09 02:05:54 xorgcvs Exp $ */
2/*
3
4Copyright 1993, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included
13in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall
24not be used in advertising or otherwise to promote the sale, use or
25other dealings in this Software without prior written authorization
26from The Open Group.
27
28*/
29/* $XFree86: xc/programs/xlsfonts/dsimple.c,v 3.6 2001/12/14 20:02:09 dawes Exp $ */
30
31#include <X11/Xos.h>
32#include <X11/Xlib.h>
33#include <X11/Xutil.h>
34#include <X11/cursorfont.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <stdarg.h>
38/*
39 * Other_stuff.h: Definitions of routines in other_stuff.
40 *
41 * Written by Mark Lillibridge.   Last updated 7/1/87
42 */
43
44#include "dsimple.h"
45
46/*
47 * Just_display: A group of routines designed to make the writting of simple
48 *               X11 applications which open a display but do not open
49 *               any windows much faster and easier.  Unless a routine says
50 *               otherwise, it may be assumed to require program_name, dpy,
51 *               and screen already defined on entry.
52 *
53 * Written by Mark Lillibridge.   Last updated 7/1/87
54 */
55
56
57/* This stuff is defined in the calling program by just_display.h */
58const char    *program_name = "unknown_program";
59Display *dpy = NULL;
60int      screen = 0;
61
62/*
63 * Get_Display_Name (argc, argv) Look for -display, -d, or host:dpy (obselete)
64 * If found, remove it from command line.  Don't go past a lone -.
65 */
66static char *
67Get_Display_Name(int *pargc/* MODIFIED */, char **argv/* MODIFIED */)
68{
69    int argc = *pargc;
70    char **pargv = argv+1;
71    char *displayname = NULL;
72    int i;
73
74    for (i = 1; i < argc; i++) {
75	char *arg = argv[i];
76
77	if (!strcmp (arg, "-display") || !strcmp (arg, "-d")) {
78	    if (++i >= argc) usage ();
79
80	    displayname = argv[i];
81	    *pargc -= 2;
82	    continue;
83	}
84	if (!strcmp(arg,"-")) {
85		while (i<argc)
86			*pargv++ = argv[i++];
87		break;
88	}
89	*pargv++ = arg;
90    }
91
92    *pargv = NULL;
93    return (displayname);
94}
95
96
97/*
98 * Open_Display: Routine to open a display with correct error handling.
99 *               Does not require dpy or screen defined on entry.
100 */
101static Display *
102Open_Display(char *display_name)
103{
104	Display *d;
105
106	d = XOpenDisplay(display_name);
107	if (d == NULL) {
108	    fprintf (stderr, "%s:  unable to open display '%s'\n",
109		     program_name, XDisplayName (display_name));
110	    exit(1);
111	}
112
113	return(d);
114}
115
116
117/*
118 * Setup_Display_And_Screen: This routine opens up the correct display (i.e.,
119 *                           it calls Get_Display_Name) and then stores a
120 *                           pointer to it in dpy.  The default screen
121 *                           for this display is then stored in screen.
122 *                           Does not require dpy or screen defined.
123 */
124void
125Setup_Display_And_Screen(int *argc/* MODIFIED */, char **argv/* MODIFIED */)
126{
127	char *displayname = Get_Display_Name(argc, argv);
128
129	dpy = Open_Display (displayname);
130	screen = XDefaultScreen(dpy);
131}
132
133/*
134 * Close_Display: Close display
135 */
136void Close_Display(void)
137{
138    if (dpy == NULL)
139      return;
140
141    XCloseDisplay(dpy);
142    dpy = NULL;
143}
144
145
146/*
147 * Standard fatal error routine - call like printf but maximum of 7 arguments.
148 * Does not require dpy or screen defined.
149 */
150void Fatal_Error(const char *msg, ...)
151{
152	va_list args;
153	fflush(stdout);
154	fflush(stderr);
155	fprintf(stderr, "%s: error: ", program_name);
156	va_start(args, msg);
157	vfprintf(stderr, msg, args);
158	va_end(args);
159	fprintf(stderr, "\n");
160        Close_Display();
161	exit(EXIT_FAILURE);
162}
163