dtprintinfo: Coverity 88650
[oweals/cde.git] / cde / programs / dtcalc / ds_xlib.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: ds_xlib.c /main/3 1995/11/01 12:40:45 rswiston $ */
24 /*                                                                      *
25  *  ds_xlib.c                                                           *
26  *   Contains some common functions which use the xlib library used     *
27  *   throughout the Desktop Calculator.                                 *
28  *                                                                      *
29  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
30  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
31  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
32  * (c) Copyright 1993, 1994 Novell, Inc.                                *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <stdint.h>
39 #include <stdbool.h>
40 #include <ctype.h>
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include "ds_xlib.h"
44 #include <X11/Xutil.h>
45 #include <X11/StringDefs.h>
46 #include <Xm/Xm.h>
47 #include <Xm/Protocols.h>
48
49
50 #define  EQUAL(a, b)  !strncmp(a, b, strlen(b))
51 #define  MAXLINE      120        /* Maximum length for character strings. */
52
53 /*  Function:     ds_beep()
54  *
55  *  Purpose:      Ring the bell (at base volume).
56  *
57  *  Parameters:   display    connection to the X server.
58  *                           (returned from XOpenDisplay).
59  *
60  *  Returns:      None.
61  */
62
63 void
64 ds_beep(Display *display)
65 {
66   XBell(display, 0) ;
67 }
68
69
70
71 /*  Function:     ds_get_resource()
72  *
73  *  Purpose:      Get an X resource from the server.
74  *
75  *  Parameters:   rDB          X resources database.
76  *
77  *                appname      application name.
78  *
79  *                resource     X resource string to search for.
80  *
81  *  Returns:      resource string, or NULL if not found.
82  *
83  *  Note:         The first character of the appname and resource strings may
84  *                be modified.
85  */
86
87 char *
88 ds_get_resource(XrmDatabase rDB, char *appname, char *resource)                          
89 {
90   char app[MAXLINE], res[MAXLINE] ;
91   char cstr[MAXLINE], nstr[MAXLINE] ;
92   char *str_type[20] ;
93   XrmValue value ;
94
95   snprintf(app, sizeof(app), "%s", appname) ;
96   snprintf(res, sizeof(res), "%s", resource) ;
97   if (isupper(app[0])) app[0] = tolower(app[0]) ;
98   snprintf(nstr, sizeof(nstr), "%s.%s", app, res) ;
99
100   if (islower(res[0])) res[0] = toupper(res[0]) ;
101   if (islower(app[0])) app[0] = toupper(app[0]) ;
102   snprintf(cstr, sizeof(cstr), "%s.%s", app, res) ;
103
104   if (XrmGetResource(rDB, nstr, cstr, str_type, &value) == 0)
105     return((char *) NULL) ;
106   else return(value.addr) ;
107 }
108
109
110 /*  Function:     ds_load_resources()
111  *
112  *  Purpose:      Get the resource databases. These are looked for in the
113  *                following ways:
114  *
115  *                Classname file in the app-defaults directory. 
116  *
117  *                Classname file in the directory specified by the
118  *                XUSERFILESEARCHPATH or XAPPLRESDIR environment variable.
119  *
120  *                Property set using xrdb, accessible through the
121  *                XResourceManagerString macro or, if that is empty, the
122  *                ~/.Xdefaults file.
123  *
124  *                XENVIRONMENT environment variable or, if not set,
125  *                .Xdefaults-hostname file.
126  *
127  *                DTCALCDEF environment variable or, if not set, the
128  *                ~/.dtcalcdef file
129  *
130  *  Parameters:   display    connection to the X server.
131  *                           (returned from XOpenDisplay).
132  *
133  *  Returns:      X combined resources database.
134  */
135
136 XrmDatabase
137 ds_load_resources(Display *display)
138 {
139   XrmDatabase db, rDB ;
140   char *home, name[MAXPATHLEN], *ptr ;
141   int len ;
142
143   rDB  = NULL ;
144   home = getenv("HOME") ;
145   XrmInitialize() ;
146
147 /* Merge server defaults, created by xrdb. If nor defined, use ~/.Xdefaults. */
148
149   if (XResourceManagerString(display) != NULL)
150     db = XrmGetStringDatabase(XResourceManagerString(display)) ;
151   else
152     { 
153       snprintf(name, sizeof(name), "%s/.Xdefaults", home) ;
154       db = XrmGetFileDatabase(name) ;
155     }
156   XrmMergeDatabases(db, &rDB) ;
157
158 /*  Open XENVIRONMENT file or, if not defined, the .Xdefaults, and merge
159  *  into existing database.
160  */
161
162   if ((ptr = getenv("XENVIRONMENT")) == NULL)
163     {
164       snprintf(name, sizeof(name), "%s/.Xdefaults-", home) ;
165       len = strlen(name) ;
166       gethostname(name+len, 1024-len) ;
167       db = XrmGetFileDatabase(name) ;
168     }
169   else db = XrmGetFileDatabase(ptr) ;
170   XrmMergeDatabases(db, &rDB) ;
171
172 /*  Finally merge in Dtcalc defaults via DTCALCDEF or, if not
173  *  defined, the ~/.dtcalcdef file.
174  */
175
176   if ((ptr = getenv("DTCALCDEF")) == NULL)
177     {
178       snprintf(name, MAXPATHLEN - 1, "%s/.dtcalcdef", home) ;
179       db = XrmGetFileDatabase(name) ;
180     }
181   else db = XrmGetFileDatabase(ptr) ;
182   XrmMergeDatabases(db, &rDB) ;
183   return(rDB) ;
184 }
185
186
187 /*  Function:     ds_put_resource()
188  *
189  *  Purpose:      Adds an X resource string (name and value) to a resources
190  *                database.
191  *
192  *  Parameters:   rDB          X resources database.
193  *
194  *                appname      application name.
195  *
196  *                rstr         X resource string name.
197  *
198  *                rval         X resource string value.
199  *
200  *  Returns:      None.
201  *
202  *  Note:         The first character of the appname and resource strings may
203  *                be modified.
204  */
205
206 void
207 ds_put_resource(XrmDatabase *rDB, char *appname, char *rstr, char *rval)
208 {
209   char app[MAXLINE], resource[MAXLINE] ;
210
211   snprintf(app, sizeof(app), "%s", appname) ;
212   if (isupper(app[0])) app[0] = tolower(app[0]) ;
213   snprintf(resource, sizeof(resource), "%s.%s", app, rstr) ;
214
215   XrmPutStringResource(rDB, resource, rval) ;
216 }
217
218
219 /*  Function:     ds_save_cmdline()
220  *
221  *  Purpose:      Save away the application command line options.
222  *
223  *  Parameters:   display      connection to the X server.
224  *                             (returned from XOpenDisplay).
225  *
226  *                w            The id of the applications main window.
227  *
228  *                argc         Number of command line options.
229  *
230  *                argv         An array of command line options.
231  *
232  *  Returns:      None.
233  */
234
235 void
236 ds_save_cmdline(Display *display, Window w, int argc, char **argv)
237 {
238   XSetCommand(display, w, argv, argc) ;
239 }
240
241
242 /*  Function:     ds_save_resources()
243  *
244  *  Purpose:      Save away the resources database to the file given by the
245  *                DTCALCDEF environment variable (if set), or
246  *                to $HOME/.dtcalcdef.
247  *
248  *  Parameters:   rDB        X resources database to save.
249  *
250  *  Returns:      1          if cannot access resource database to write.
251  *                0          on successful completion.
252  */
253
254
255 int
256 ds_save_resources(XrmDatabase rDB, char *filename)
257 {
258   char *home;
259   struct stat statbuf;
260   bool needsFree = false;
261
262   if (filename == NULL)
263   {
264     if ((filename = getenv("DTCALCDEF")) == NULL)
265       {
266           if ( !(home = getenv("HOME")) )
267               return 1;
268
269           size_t fileLen = strlen(home) + 18;
270           if ( (filename = calloc(1, fileLen)) != NULL )
271           {
272               needsFree = true;
273               snprintf(filename, fileLen, "%s/.dtcalcdef", home);
274           }
275           else
276           {
277               return 1;
278           }
279       }
280   }
281
282 /* If file exists but user does not have access. */
283
284   if (stat(filename, &statbuf) != -1 && access(filename, W_OK) != 0)
285     { 
286         if (needsFree)
287             free(filename);
288
289         return(1);
290     }
291
292 /* If file does not exist this call will create it. */
293
294   XrmPutFileDatabase(rDB, filename);
295   if (needsFree)
296       free(filename);
297
298   return(0);
299 }
300