Don't quit if getpwuid() fails. It can fail if some nameservices are not
[nedit.git] / util / prefFile.c
blobe8037aba6105b982297bc2909e5d7fd2e19d3cac
1 static const char CVSID[] = "$Id: prefFile.c,v 1.19 2003/04/03 19:05:37 jlous Exp $";
2 /*******************************************************************************
3 * *
4 * prefFile.c -- Nirvana utilities for providing application preferences files *
5 * *
6 * Copyright (C) 1999 Mark Edel *
7 * *
8 * This is free software; you can redistribute it and/or modify it under the *
9 * terms of the GNU General Public License as published by the Free Software *
10 * Foundation; either version 2 of the License, or (at your option) any later *
11 * version. *
12 * *
13 * This software is distributed in the hope that it will be useful, but WITHOUT *
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
16 * for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License along with *
19 * software; if not, write to the Free Software Foundation, Inc., 59 Temple *
20 * Place, Suite 330, Boston, MA 02111-1307 USA *
21 * *
22 * Nirvana Text Editor *
23 * June 3, 1993 *
24 * *
25 * Written by Mark Edel *
26 * *
27 *******************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "../config.h"
31 #endif
33 #include "prefFile.h"
34 #include "fileUtils.h"
35 #include "utils.h"
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #ifdef VMS
41 #include "VMSparam.h"
42 #else
43 #ifndef __MVS__
44 #include <sys/param.h>
45 #endif
46 #endif
47 #include <Xm/Xm.h>
49 #ifdef HAVE_DEBUG_H
50 #include "../debug.h"
51 #endif
53 #define N_BOOLEAN_STRINGS 13
54 static const char *TrueStrings[N_BOOLEAN_STRINGS] = {"True", "true", "TRUE", "T", "t",
55 "Yes", "yes", "YES", "y", "Y", "on", "On", "ON"};
56 static const char *FalseStrings[N_BOOLEAN_STRINGS] = {"False", "false", "FALSE", "F", "f",
57 "No", "no", "NO", "n", "N", "off", "Off", "OFF"};
59 static void readPrefs(XrmDatabase prefDB, XrmDatabase appDB,
60 const char *appName, const char *appClass,
61 PrefDescripRec *rsrcDescrip, int nRsrc, int overlay);
62 static int stringToPref(const char *string, PrefDescripRec *rsrcDescrip);
63 static char *removeWhiteSpace(const char *string);
66 ** Preferences File
68 ** An application maintains a preferences file so that users can
69 ** quickly save and restore program options from within a program,
70 ** without being forced to learn the X resource mechanism.
72 ** Preference files are the same format as X resource files, and
73 ** are read using the X resource file reader. X-savvy users are allowed
74 ** to move resources out of a preferences file to their X resource
75 ** files. They would do so if they wanted to attach server-specific
76 ** preferences (such as fonts and colors) to different X servers, or to
77 ** combine additional preferences served only by X resources with those
78 ** provided by the program's menus.
82 ** Preference description table
84 ** A preference description table contains the information necessary
85 ** to read preference resources and store their values in a data
86 ** structure. The table can, so far, describe four types
87 ** of values (this will probably be expanded in the future to include
88 ** more types): ints, booleans, enumerations, and strings. Each entry
89 ** includes the name and class for saving and restoring the parameter
90 ** in X database format, the data type, a default value in the form of
91 ** a character string, and the address where the parameter value is
92 ** be stored. Strings and enumerations take an additional argument.
93 ** For strings, it is the maximum length string that can safely be
94 ** stored or NULL to indicate that new space should be allocated and a
95 ** pointer to it stored in the value address. For enums, it is an array
96 ** of string pointers to the names of each of its possible values. The
97 ** last value in a preference record is a flag for determining whether
98 ** the value should be written to the save file by SavePreferences.
102 ** CreatePreferencesDatabase
104 ** Process a preferences file and the command line options pertaining to
105 ** the X resources used to set those preferences. Create an X database
106 ** of the results. The reason for this odd set of functionality is
107 ** to process command line options before XtDisplayInitialize reads them
108 ** into the application database that the toolkit attaches to the display.
109 ** This allows command line arguments to properly override values specified
110 ** in the preferences file.
112 ** fileName Name only of the preferences file to be found
113 ** in the user's home directory
114 ** appName Application name to use in reading the preference
115 ** resources
116 ** opTable Xrm command line option table for the resources
117 ** used in the preferences file ONLY. Command line
118 ** options for other X resources should be processed
119 ** by XtDisplayInitialize.
120 ** nOptions Number of items in opTable
121 ** argcInOut Address of argument count. This will be altered
122 ** to remove the command line options that are
123 ** recognized in the option table.
124 ** argvInOut Argument vector. Will be altered as argcInOut.
126 XrmDatabase CreatePreferencesDatabase(const char *fullName, const char *appName,
127 XrmOptionDescList opTable, int nOptions, unsigned int *argcInOut,
128 char **argvInOut)
130 XrmDatabase db;
131 int argcCopy;
132 char **argvCopy;
133 FILE *fp;
134 char *fileString;
135 static XrmOptionDescRec xrmOnlyTable[] =
136 {{"-xrm", NULL, XrmoptionResArg, (caddr_t)NULL}};
138 /* read the preferences file into an X database.
139 On failure prefDB will be NULL. */
140 if (fullName == NULL)
142 db = NULL;
143 } else
145 fileString = ReadAnyTextFile(fullName);
146 if (fileString == NULL){
147 db = NULL;
148 } else
150 db = XrmGetStringDatabase(fileString);
151 XtFree(fileString);
155 /* parse the command line, storing results in the preferences database */
156 XrmParseCommand(&db, opTable, nOptions, appName, (int *)argcInOut,
157 argvInOut);
159 /* process -xrm (resource setting by resource name) arguments so those
160 pertaining to preference resources will be included in the database.
161 Don't remove -xrm arguments from the argument vector, however, so
162 XtDisplayInitialize can still read the non-preference resources */
163 argvCopy = (char**)XtMalloc(sizeof(char *) * *argcInOut);
164 memcpy(argvCopy, argvInOut, sizeof(char *) * *argcInOut);
165 argcCopy = *argcInOut;
166 XrmParseCommand(&db, xrmOnlyTable, XtNumber(xrmOnlyTable), appName,
167 &argcCopy, argvCopy);
168 XtFree((char *)argvCopy);
170 /* If the resource database file existed, add a resource to the database
171 which notes this fact, so that programs will know whether the file
172 was really ever found or not */
173 if ((fp = fopen(fullName, "r")) != NULL) {
174 char *rsrcName = (char*)XtMalloc(strlen(appName) + 14);
175 fclose(fp);
176 sprintf(rsrcName, "%s.prefFileRead", appName);
177 XrmPutStringResource(&db, rsrcName, "True");
178 XtFree(rsrcName);
181 return db;
185 ** RestorePreferences
187 ** Fill in preferences data from two X databases, values in prefDB taking
188 ** precidence over those in appDB.
190 void RestorePreferences(XrmDatabase prefDB, XrmDatabase appDB,
191 const char *appName, const char *appClass, PrefDescripRec *rsrcDescrip, int nRsrc)
193 readPrefs(prefDB, appDB, appName, appClass, rsrcDescrip, nRsrc, False);
197 ** OverlayPreferences
199 ** Incorporate preference specified in database "prefDB", preserving (not
200 ** restoring to default) existing preferences, not mentioned in "prefDB"
202 void OverlayPreferences(XrmDatabase prefDB, const char *appName,
203 const char *appClass, PrefDescripRec *rsrcDescrip, int nRsrc)
205 readPrefs(NULL, prefDB, appName, appClass, rsrcDescrip, nRsrc, True);
208 static void readPrefs(XrmDatabase prefDB, XrmDatabase appDB,
209 const char *appName, const char *appClass, PrefDescripRec *rsrcDescrip,
210 int nRsrc, int overlay)
212 char rsrcName[256], rsrcClass[256], *valueString, *type;
213 XrmValue rsrcValue;
214 int i;
216 /* read each resource, trying first the preferences file database, then
217 the application database, then the default value if neither are found */
218 for (i=0; i<nRsrc; i++) {
219 sprintf(rsrcName,"%s.%s", appName, rsrcDescrip[i].name);
220 sprintf(rsrcClass, "%s.%s", appClass, rsrcDescrip[i].class);
221 if (prefDB!=NULL &&
222 XrmGetResource(prefDB, rsrcName, rsrcClass, &type, &rsrcValue)) {
223 if (strcmp(type, XmRString)) {
224 fprintf(stderr,"Internal Error: Unexpected resource type, %s\n",
225 type);
226 return;
228 valueString = rsrcValue.addr;
229 } else if (XrmGetResource(appDB,rsrcName,rsrcClass,&type,&rsrcValue)) {
230 if (strcmp(type, XmRString)) {
231 fprintf(stderr,"Internal Error: Unexpected resource type, %s\n",
232 type);
233 return;
235 valueString = rsrcValue.addr;
236 } else
237 valueString = rsrcDescrip[i].defaultString;
238 if (overlay && valueString == rsrcDescrip[i].defaultString)
239 continue;
240 if (!stringToPref(valueString, &rsrcDescrip[i]))
241 fprintf(stderr, "Could not read value of resource %s\n", rsrcName);
246 ** RestoreDefaultPreferences
248 ** Restore preferences to their default values as stored in rsrcDesrcip
250 void RestoreDefaultPreferences(PrefDescripRec *rsrcDescrip, int nRsrc)
252 int i;
254 for (i=0; i<nRsrc; i++)
255 stringToPref(rsrcDescrip[i].defaultString, &rsrcDescrip[i]);
259 ** SavePreferences
261 ** Create or replace an application preference file according to
262 ** the resource descriptions in rsrcDesrcip.
264 int SavePreferences(Display *display, const char *fullName,
265 const char *fileHeader, PrefDescripRec *rsrcDescrip, int nRsrc)
267 char *appName, *appClass, **enumStrings;
268 FILE *fp;
269 int type;
270 int i;
272 /* open the file */
273 if ((fp = fopen(fullName, "w")) == NULL)
274 return False;
276 /* write the file header text out to the file */
277 fprintf(fp, "%s\n", fileHeader);
279 /* write out the resources so they can be read by XrmGetFileDatabase */
280 XtGetApplicationNameAndClass(display, &appName, &appClass);
281 for (i=0; i<nRsrc; i++) {
282 if (rsrcDescrip[i].save) {
283 type = rsrcDescrip[i].dataType;
284 fprintf(fp, "%s.%s: ", appName, rsrcDescrip[i].name);
285 if (type == PREF_STRING)
286 fprintf(fp, "%s", (char *)rsrcDescrip[i].valueAddr);
287 if (type == PREF_ALLOC_STRING)
288 fprintf(fp, "%s", *(char **)rsrcDescrip[i].valueAddr);
289 else if (type == PREF_ENUM) {
290 enumStrings = (char **)rsrcDescrip[i].arg;
291 fprintf(fp,"%s", enumStrings[*(int *)rsrcDescrip[i].valueAddr]);
292 } else if (type == PREF_INT)
293 fprintf(fp, "%d", *(int *)rsrcDescrip[i].valueAddr);
294 else if (type == PREF_BOOLEAN) {
295 if (*(int *)rsrcDescrip[i].valueAddr)
296 fprintf(fp, "True");
297 else
298 fprintf(fp, "False");
300 fprintf(fp, "\n");
303 fclose(fp);
304 return True;
307 static int stringToPref(const char *string, PrefDescripRec *rsrcDescrip)
309 int i;
310 char *cleanStr, *endPtr, **enumStrings;
312 switch (rsrcDescrip->dataType) {
313 case PREF_INT:
314 cleanStr = removeWhiteSpace(string);
315 *(int *)rsrcDescrip->valueAddr =
316 strtol(cleanStr, &endPtr, 10);
317 if (strlen(cleanStr) == 0) { /* String is empty */
318 *(int *)rsrcDescrip->valueAddr = 0;
319 XtFree(cleanStr);
320 return False;
321 } else if (*endPtr != '\0') { /* Whole string not parsed */
322 *(int *)rsrcDescrip->valueAddr = 0;
323 XtFree(cleanStr);
324 return False;
326 XtFree(cleanStr);
327 return True;
328 case PREF_BOOLEAN:
329 cleanStr = removeWhiteSpace(string);
330 for (i=0; i<N_BOOLEAN_STRINGS; i++) {
331 if (!strcmp(TrueStrings[i], cleanStr)) {
332 *(int *)rsrcDescrip->valueAddr = True;
333 XtFree(cleanStr);
334 return True;
336 if (!strcmp(FalseStrings[i], cleanStr)) {
337 *(int *)rsrcDescrip->valueAddr = False;
338 XtFree(cleanStr);
339 return True;
342 XtFree(cleanStr);
343 *(int *)rsrcDescrip->valueAddr = False;
344 return False;
345 case PREF_ENUM:
346 cleanStr = removeWhiteSpace(string);
347 enumStrings = (char **)rsrcDescrip->arg;
348 for (i=0; enumStrings[i]!=NULL; i++) {
349 if (!strcmp(enumStrings[i], cleanStr)) {
350 *(int *)rsrcDescrip->valueAddr = i;
351 XtFree(cleanStr);
352 return True;
355 XtFree(cleanStr);
356 *(int *)rsrcDescrip->valueAddr = 0;
357 return False;
358 case PREF_STRING:
359 if ((int)strlen(string) >= (int)rsrcDescrip->arg)
360 return False;
361 strncpy(rsrcDescrip->valueAddr, string, (int)rsrcDescrip->arg);
362 return True;
363 case PREF_ALLOC_STRING:
364 *(char **)rsrcDescrip->valueAddr = XtMalloc(strlen(string) + 1);
365 strcpy(*(char **)rsrcDescrip->valueAddr, string);
366 return True;
368 return False;
372 ** Remove the white space (blanks and tabs) from a string and return
373 ** the result in a newly allocated string as the function value
375 static char *removeWhiteSpace(const char *string)
377 char *outPtr, *outString;
379 outPtr = outString = XtMalloc(strlen(string)+1);
380 while (TRUE) {
381 if (*string != ' ' && *string != '\t')
382 *(outPtr++) = *(string++);
383 else
384 string++;
385 if (*string == 0) {
386 *outPtr = 0;
387 return outString;
392 /*******************
393 Implementation Note:
394 Q: Why aren't you using the Xt type conversion services?
395 A: 1) To create a save file, you also need to convert values back to text form,
396 and there are no converters for that direction. 2) XtGetApplicationResources
397 can only be used on the resource database created by the X toolkit at
398 initialization time, and there is no way to intervene in the creation of
399 that database or store new resources in it reliably after it is created.
400 3) The alternative, XtConvertAndStore is not adequately documented. The
401 toolkit mauual does not explain why it overwrites its input value structure.
402 4) XtGetApplicationResources and XtConvertAndStore do not work well together
403 because they use different storage strategies for certain data types.
404 *******************/