tizen 2.3.1 release
[framework/uifw/xorg/util/x11-utils.git] / xlsfonts / dsimple.c
1 /* $Xorg: dsimple.c,v 1.4 2001/02/09 02:05:54 xorgcvs Exp $ */
2 /*
3
4 Copyright 1993, 1998  The Open Group
5
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the name of The Open Group shall
24 not be used in advertising or otherwise to promote the sale, use or
25 other dealings in this Software without prior written authorization
26 from 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 */
58 char    *program_name = "unknown_program";
59 Display *dpy = NULL;
60 int      screen = 0;
61
62 /*
63  * Malloc: like malloc but handles out of memory using Fatal_Error.
64  */
65 char *
66 Malloc(unsigned size)
67 {
68         char *data;
69
70         if (!(data = malloc(size)))
71           Fatal_Error("Out of memory!");
72
73         return(data);
74 }
75         
76
77 /*
78  * Realloc: like Malloc except for realloc, handles NULL using Malloc.
79  */
80 char *
81 Realloc(char *ptr, int size)
82 {
83         char *new_ptr;
84
85         if (!ptr)
86           return(Malloc(size));
87
88         if (!(new_ptr = realloc(ptr, size)))
89           Fatal_Error("Out of memory!");
90
91         return(new_ptr);
92 }
93
94
95 /*
96  * Get_Display_Name (argc, argv) Look for -display, -d, or host:dpy (obselete)
97  * If found, remove it from command line.  Don't go past a lone -.
98  */
99 static char *
100 Get_Display_Name(int *pargc/* MODIFIED */, char **argv/* MODIFIED */)
101 {
102     int argc = *pargc;
103     char **pargv = argv+1;
104     char *displayname = NULL;
105     int i;
106
107     for (i = 1; i < argc; i++) {
108         char *arg = argv[i];
109
110         if (!strcmp (arg, "-display") || !strcmp (arg, "-d")) {
111             if (++i >= argc) usage ();
112
113             displayname = argv[i];
114             *pargc -= 2;
115             continue;
116         }
117         if (!strcmp(arg,"-")) {
118                 while (i<argc)
119                         *pargv++ = argv[i++];
120                 break;
121         }
122         *pargv++ = arg;
123     }
124
125     *pargv = NULL;
126     return (displayname);
127 }
128
129
130 /*
131  * Open_Display: Routine to open a display with correct error handling.
132  *               Does not require dpy or screen defined on entry.
133  */
134 static Display *
135 Open_Display(char *display_name)
136 {
137         Display *d;
138
139         d = XOpenDisplay(display_name);
140         if (d == NULL) {
141             fprintf (stderr, "%s:  unable to open display '%s'\n",
142                      program_name, XDisplayName (display_name));
143             exit(1);
144         }
145
146         return(d);
147 }
148
149
150 /*
151  * Setup_Display_And_Screen: This routine opens up the correct display (i.e.,
152  *                           it calls Get_Display_Name) and then stores a
153  *                           pointer to it in dpy.  The default screen
154  *                           for this display is then stored in screen.
155  *                           Does not require dpy or screen defined.
156  */
157 void
158 Setup_Display_And_Screen(int *argc/* MODIFIED */, char **argv/* MODIFIED */)
159 {
160         char *displayname = Get_Display_Name(argc, argv);
161
162         dpy = Open_Display (displayname);
163         screen = XDefaultScreen(dpy);
164 }
165
166 /*
167  * Close_Display: Close display
168  */
169 void Close_Display(void)
170 {
171     if (dpy == NULL)
172       return;
173       
174     XCloseDisplay(dpy);
175     dpy = NULL;
176 }
177
178
179 /*
180  * Standard fatal error routine - call like printf but maximum of 7 arguments.
181  * Does not require dpy or screen defined.
182  */
183 void Fatal_Error(char *msg, ...)
184 {
185         va_list args;
186         fflush(stdout);
187         fflush(stderr);
188         fprintf(stderr, "%s: error: ", program_name);
189         va_start(args, msg);
190         vfprintf(stderr, msg, args);
191         va_end(args);
192         fprintf(stderr, "\n");
193         Close_Display();
194         exit(EXIT_FAILURE);
195 }