2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
25 * (c) Copyright 1993, 1994 International Business Machines Corp. *
26 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
27 * (c) Copyright 1993, 1994 Novell, Inc. *
29 /*Add a string to the XA_RESOURCE_MANAGER*/
32 * DIGITAL EQUIPMENT CORPORATION
33 * MAYNARD, MASSACHUSETTS
34 * ALL RIGHTS RESERVED.
36 * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
37 * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
38 * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
39 * ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
41 * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT RIGHTS,
42 * APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN ADDITION TO THAT
46 * Permission to use, copy, modify, and distribute this software and its
47 * documentation for any purpose and without fee is hereby granted, provided
48 * that the above copyright notice appear in all copies and that both that
49 * copyright notice and this permission notice appear in supporting
50 * documentation, and that the name of Digital Equipment Corporation not be
51 * used in advertising or publicity pertaining to distribution of the software
52 * without specific, written prior permission.
55 /*Lifted from xrdb(1X)*/
57 *******************************************************************************
59 * File: addToResource.c
60 * RCS: $TOG: addToRes.c /main/8 1999/10/14 16:01:17 mgreess $
61 * Description: Source code for adding strings to RESOURCE_PROPERTY on
63 * Author: DEC, Robert Williams
64 * Created: Thu Apr 26 14:42:08 PDT 1990
65 * Modified: Kim Dronesen
68 * Status: Experimental (Do Not Distribute)
70 * (C) Copyright 1990, Hewlett-Packard, all rights reserved.
72 *******************************************************************************
74 /*$TOG: addToRes.c /main/8 1999/10/14 16:01:17 mgreess $ */
77 #include <X11/Xatom.h>
83 #include <Dt/SessionM.h>
84 #include "DtSvcLock.h"
86 /****************************************/
87 /* Global variables */
88 /****************************************/
90 typedef struct _Entry {
95 typedef struct _Buffer {
100 typedef struct _Entries {
105 #define INIT_BUFFER_SIZE 10000
106 #define INIT_ENTRY_SIZE 500
109 /******** Public Function Declarations ********/
111 static Buffer * _DtAllocBuffer(const char **buff);
112 static void _DtFreeBuffer(Buffer *b);
113 static void _DtAppendToBuffer(
117 static void _DtAppendEntryToBuffer(
118 register Buffer *buffer,
121 static Entries * _DtAllocEntries( void) ;
122 static void _DtFreeEntries( Entries *) ;
123 static void _DtAddEntry(
126 static int _DtCompareEntries(
129 static char * _DtFindFirst(
130 register char *string,
131 register char dest) ;
132 static void _DtGetEntries(
133 register Entries *entries,
136 static void _DtMergeEntries(
140 static void _DtAddToResProp(
144 static void _getWinProp(
150 /******** End Public Function Declarations ********/
152 /****************************************/
154 /****************************************/
160 Buffer *b = (Buffer *)malloc(sizeof(Buffer));
161 b->room = INIT_BUFFER_SIZE;
162 b->buff = buff ? (char*) *buff :
163 (char *)malloc(INIT_BUFFER_SIZE*sizeof(char));
164 b->used = buff && *buff ? strlen(*buff) : 0;
165 b->freebuff = buff ? False : True;
173 if (b->freebuff == True) free(b->buff);
183 while (b->used + len > b->room) {
184 b->buff = (char *)realloc(b->buff, 2*b->room*(sizeof(char)));
187 strncpy(b->buff + b->used, str, len);
192 _DtAllocEntries( void )
194 Entries *e = (Entries *)malloc(sizeof(Entries));
195 e->room = INIT_ENTRY_SIZE;
197 e->entry = (Entry *)malloc(INIT_ENTRY_SIZE*sizeof(Entry));
209 free(e->entry[n].tag);
210 free(e->entry[n].value);
224 for (n = 0; n < e->used; n++)
226 if (strcmp(e->entry[n].tag, entry.tag) == 0)
227 { /* overwrite old entry - free its memory first*/
228 free(e->entry[n].tag);
229 free(e->entry[n].value);
231 return ; /* ok to leave, now there's only one of each tag in db */
235 if (e->used == e->room) {
236 e->entry = (Entry *)realloc(e->entry, 2*e->room*(sizeof(Entry)));
240 e->entry[e->used++] = entry;
248 return strcmp(e1->tag, e2->tag);
252 _DtAppendEntryToBuffer(
253 register Buffer *buffer,
256 _DtAppendToBuffer(buffer, entry.tag, strlen(entry.tag));
257 _DtAppendToBuffer(buffer, ":\t", 2);
258 _DtAppendToBuffer(buffer, entry.value, strlen(entry.value));
259 _DtAppendToBuffer(buffer, "\n", 1);
264 register char *string,
270 if((len = mblen(string, MB_CUR_MAX)) > 1) {
276 if (*string == '\\') {
277 if (*++string == '\0')
280 else if (*string == dest)
288 register Entries *entries,
292 register char *line, *colon, *temp, *str, *temp2;
299 for (; str < buff->buff + buff->used; str = line + 1)
301 line = _DtFindFirst(str, '\n');
312 if (sscanf (str, "# %d", &dummy) == 1) lineno = dummy - 1;
316 *temp && *temp != '\n' && isascii(*temp) && isspace((u_char)*temp);
318 if (!*temp || *temp == '\n') continue;
320 colon = _DtFindFirst(str, ':');
327 "%s: colon missing on line %d, ignoring entry \"%s\"\n",
328 "dtprefs", lineno, str);
333 while (temp2[0] == ' ' || temp2[0] == '\t')
337 temp = (char *)malloc((length = colon - temp2) + 1);
338 strncpy(temp, temp2, length);
340 while (temp[length-1] == ' ' || temp[length-1] == '\t')
341 temp[--length] = '\0';
346 while (temp2[0] == ' ' || temp2[0] == '\t')
350 temp = (char *)malloc((length = line - temp2) + 1);
351 strncpy(temp, temp2, length);
354 entry.lineno = lineno;
356 _DtAddEntry(entries, entry);
359 if (dosort && (entries->used > 0))
360 qsort(entries->entry, entries->used, sizeof(Entry),
361 (int (*)(const void *, const void *))_DtCompareEntries);
374 while ((n < new.used) && (o < old.used))
376 cmp = strcmp(new.entry[n].tag, old.entry[o].tag);
379 _DtAppendEntryToBuffer(buffer, old.entry[o]);
384 _DtAppendEntryToBuffer(buffer, new.entry[n]);
395 _DtAppendEntryToBuffer(buffer, new.entry[n]);
400 _DtAppendEntryToBuffer(buffer, old.entry[o]);
404 _DtAppendToBuffer(buffer, "\0", 1);
414 static Bool init = True;
415 static Window winprop;
416 static Atom xa_resmgr;
417 static Atom xa_prefs;
422 winprop = XRootWindow(dpy, 0);
423 xa_resmgr = XA_RESOURCE_MANAGER;
424 xa_prefs = XInternAtom (dpy, _XA_DT_SM_PREFERENCES, False);
427 _DtSvcProcessUnlock();
430 *prop = id == _DT_ATR_RESMGR ? xa_resmgr : xa_prefs;
441 Buffer *oldBuffer, *newBuffer;
446 unsigned long nitems, leftover;
451 * Get window and property
453 _getWinProp(dpy, id, &win, &prop);
454 if (win == (Window)0)
460 * Get resource database from specified window and property.
462 defStatus = XGetWindowProperty(dpy, win,
464 100000000L,False,XA_STRING,&actualType,
465 &actualFormat,&nitems,&leftover,
466 (unsigned char**) &xdefs);
470 * Allocate oldBuffer and init from resource database string
472 oldBuffer = _DtAllocBuffer((const char**) &xdefs);
477 oldDB = _DtAllocEntries();
480 * Convert oldBuffer to oldDB.
482 _DtGetEntries(oldDB, oldBuffer, 1);
485 * Init empty newBuffer, then populate by merging db into oldDB.
487 newBuffer = _DtAllocBuffer(NULL);
488 _DtMergeEntries(newBuffer, db, *oldDB);
491 * Finally, store newBuffer into resource database.
493 XChangeProperty (dpy, win, prop,
494 XA_STRING, 8, PropModeReplace,
495 (unsigned char *)newBuffer->buff, newBuffer->used);
502 if (oldBuffer->buff) XFree(oldBuffer->buff);
503 _DtFreeBuffer(oldBuffer);
504 _DtFreeBuffer(newBuffer);
505 _DtFreeEntries(oldDB);
514 Buffer *oldBuffer, *newBuffer;
519 unsigned long nitems, leftover;
524 * Get window and property
526 _getWinProp(dpy, id, &win, &prop);
527 if (win == (Window)0)
533 * Get resource database from specified window and property.
535 defStatus = XGetWindowProperty(dpy, win,
537 100000000L,False,XA_STRING,&actualType,
538 &actualFormat,&nitems,&leftover,
539 (unsigned char**) &xdefs);
549 _DtAddResString( dpy, data, _DT_ATR_RESMGR|_DT_ATR_PREFS);
565 unsigned long nitems, leftover;
567 if((data == NULL) || (*data == NULL))
573 * Init buffer with input data
575 buffer = _DtAllocBuffer(&data);
578 * Init, then populate, newDB from buffer
580 newDB = _DtAllocEntries();
581 _DtGetEntries(newDB, buffer, 1);
583 if (flags & _DT_ATR_RESMGR)
586 * Merge newDB into RESOURCE_MANAGER
588 _DtAddToResProp(dpy, _DT_ATR_RESMGR, *newDB);
591 if (flags & _DT_ATR_PREFS)
594 * Merge newDB into _DT_SM_PREFERENCES
596 _DtAddToResProp(dpy, _DT_ATR_PREFS, *newDB);
602 _DtFreeBuffer(buffer);
603 _DtFreeEntries(newDB);