19 WPLString
= 0x57504c01,
21 WPLArray
= 0x57504c03,
22 WPLDictionary
= 0x57504c04
25 typedef struct W_PropList
{
38 typedef struct PLData
{
45 typedef struct StringBuffer
{
50 static unsigned hashPropList(const void *param
);
51 static WMPropList
*getPLString(PLData
* pldata
);
52 static WMPropList
*getPLQString(PLData
* pldata
);
53 static WMPropList
*getPLData(PLData
* pldata
);
54 static WMPropList
*getPLArray(PLData
* pldata
);
55 static WMPropList
*getPLDictionary(PLData
* pldata
);
56 static WMPropList
*getPropList(PLData
* pldata
);
58 typedef Bool(*isEqualFunc
) (const void *, const void *);
60 static const WMHashTableCallbacks WMPropListHashCallbacks
= {
62 (isEqualFunc
) WMIsPropListEqualTo
,
67 static Bool caseSensitive
= True
;
69 #define BUFFERSIZE 8192
70 #define BUFFERSIZE_INCREMENT 1024
73 # define DPUT(s) puts(s)
78 #define COMPLAIN(pld, msg) wwarning(_("syntax error in %s %s, line %i: %s"),\
79 (pld)->filename ? "file" : "PropList",\
80 (pld)->filename ? (pld)->filename : "description",\
81 (pld)->lineNumber, msg)
83 #define ISSTRINGABLE(c) (isalnum(c) || (c)=='.' || (c)=='_' || (c)=='/' \
86 #define CHECK_BUFFER_SIZE(buf, ptr) \
87 if ((ptr) >= (buf).size-1) {\
88 (buf).size += BUFFERSIZE_INCREMENT;\
89 (buf).str = wrealloc((buf).str, (buf).size);\
92 #define inrange(ch, min, max) ((ch)>=(min) && (ch)<=(max))
93 #define noquote(ch) (inrange(ch, 'a', 'z') || inrange(ch, 'A', 'Z') || inrange(ch, '0', '9') || ((ch)=='_') || ((ch)=='.') || ((ch)=='$'))
94 #define charesc(ch) (inrange(ch, 0x07, 0x0c) || ((ch)=='"') || ((ch)=='\\'))
95 #define numesc(ch) (((ch)<=0x06) || inrange(ch, 0x0d, 0x1f) || ((ch)>0x7e))
96 #define ishexdigit(ch) (inrange(ch, 'a', 'f') || inrange(ch, 'A', 'F') || inrange(ch, '0', '9'))
97 #define char2num(ch) (inrange(ch,'0','9') ? ((ch)-'0') : (inrange(ch,'a','f') ? ((ch)-0x57) : ((ch)-0x37)))
98 #define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
100 #define MaxHashLength 64
102 static unsigned hashPropList(const void *param
)
104 WMPropList
*plist
= (WMPropList
*) param
;
110 switch (plist
->type
) {
112 key
= plist
->d
.string
;
113 len
= WMIN(strlen(key
), MaxHashLength
);
114 for (i
= 0; i
< len
; i
++) {
115 ret
^= tolower(key
[i
]) << ctr
;
116 ctr
= (ctr
+ 1) % sizeof(char *);
119 ret ^= tolower(*key++) << ctr;
120 ctr = (ctr + 1) % sizeof (char *);
125 key
= WMDataBytes(plist
->d
.data
);
126 len
= WMIN(WMGetDataLength(plist
->d
.data
), MaxHashLength
);
127 for (i
= 0; i
< len
; i
++) {
128 ret
^= key
[i
] << ctr
;
129 ctr
= (ctr
+ 1) % sizeof(char *);
134 wwarning(_("Only string or data is supported for a proplist dictionary key"));
142 static WMPropList
*retainPropListByCount(WMPropList
* plist
, int count
)
144 WMPropList
*key
, *value
;
148 plist
->retainCount
+= count
;
150 switch (plist
->type
) {
155 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
156 retainPropListByCount(WMGetFromArray(plist
->d
.array
, i
), count
);
160 e
= WMEnumerateHashTable(plist
->d
.dict
);
161 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
162 retainPropListByCount(key
, count
);
163 retainPropListByCount(value
, count
);
167 wwarning(_("Used proplist functions on non-WMPropLists objects"));
168 wassertrv(False
, NULL
);
175 static void releasePropListByCount(WMPropList
* plist
, int count
)
177 WMPropList
*key
, *value
;
181 plist
->retainCount
-= count
;
183 switch (plist
->type
) {
185 if (plist
->retainCount
< 1) {
186 wfree(plist
->d
.string
);
191 if (plist
->retainCount
< 1) {
192 WMReleaseData(plist
->d
.data
);
197 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
198 releasePropListByCount(WMGetFromArray(plist
->d
.array
, i
), count
);
200 if (plist
->retainCount
< 1) {
201 WMFreeArray(plist
->d
.array
);
206 e
= WMEnumerateHashTable(plist
->d
.dict
);
207 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
208 releasePropListByCount(key
, count
);
209 releasePropListByCount(value
, count
);
211 if (plist
->retainCount
< 1) {
212 WMFreeHashTable(plist
->d
.dict
);
217 wwarning(_("Used proplist functions on non-WMPropLists objects"));
223 static char *dataDescription(WMPropList
* plist
)
225 const unsigned char *data
;
229 data
= WMDataBytes(plist
->d
.data
);
230 length
= WMGetDataLength(plist
->d
.data
);
232 retVal
= (char *)wmalloc(2 * length
+ length
/ 4 + 3);
235 for (i
= 0, j
= 1; i
< length
; i
++) {
236 retVal
[j
++] = num2char((data
[i
] >> 4) & 0x0f);
237 retVal
[j
++] = num2char(data
[i
] & 0x0f);
238 if ((i
& 0x03) == 3 && i
!= length
- 1) {
239 /* if we've just finished a 32-bit int, add a space */
249 static char *stringDescription(WMPropList
* plist
)
252 char *retVal
, *sPtr
, *dPtr
;
256 str
= plist
->d
.string
;
258 if (strlen(str
) == 0) {
259 return wstrdup("\"\"");
262 /* FIXME: make this work with unichars. */
267 while ((ch
= *sPtr
)) {
282 retVal
= (char *)wmalloc(len
+ 1);
290 while ((ch
= *sPtr
)) {
313 *dPtr
= ch
; /* " or \ */
315 } else if (numesc(ch
)) {
317 *(dPtr
++) = '0' + ((ch
>> 6) & 07);
318 *(dPtr
++) = '0' + ((ch
>> 3) & 07);
319 *dPtr
= '0' + (ch
& 07);
335 static char *description(WMPropList
* plist
)
337 WMPropList
*key
, *val
;
339 char *str
, *tmp
, *skey
, *sval
;
343 switch (plist
->type
) {
345 retstr
= stringDescription(plist
);
348 retstr
= dataDescription(plist
);
351 retstr
= wstrdup("(");
352 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
353 str
= description(WMGetFromArray(plist
->d
.array
, i
));
355 retstr
= wstrappend(retstr
, str
);
357 tmp
= (char *)wmalloc(strlen(retstr
) + strlen(str
) + 3);
358 sprintf(tmp
, "%s, %s", retstr
, str
);
364 retstr
= wstrappend(retstr
, ")");
367 retstr
= wstrdup("{");
368 e
= WMEnumerateHashTable(plist
->d
.dict
);
369 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&val
, (void **)&key
)) {
370 skey
= description(key
);
371 sval
= description(val
);
372 tmp
= (char *)wmalloc(strlen(retstr
) + strlen(skey
) + strlen(sval
) + 5);
373 sprintf(tmp
, "%s%s = %s;", retstr
, skey
, sval
);
379 retstr
= wstrappend(retstr
, "}");
382 wwarning(_("Used proplist functions on non-WMPropLists objects"));
383 wassertrv(False
, NULL
);
390 static char *indentedDescription(WMPropList
* plist
, int level
)
392 WMPropList
*key
, *val
;
394 char *str
, *tmp
, *skey
, *sval
;
398 if (plist
->type
== WPLArray
/* || plist->type==WPLDictionary */ ) {
399 retstr
= description(plist
);
401 if (retstr
&& ((2 * (level
+ 1) + strlen(retstr
)) <= 77)) {
409 switch (plist
->type
) {
411 retstr
= stringDescription(plist
);
414 retstr
= dataDescription(plist
);
417 retstr
= wstrdup("(\n");
418 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
419 str
= indentedDescription(WMGetFromArray(plist
->d
.array
, i
), level
+ 1);
421 tmp
= (char *)wmalloc(2 * (level
+ 1) + strlen(retstr
) + strlen(str
) + 1);
422 sprintf(tmp
, "%s%*s%s", retstr
, 2 * (level
+ 1), "", str
);
426 tmp
= (char *)wmalloc(2 * (level
+ 1) + strlen(retstr
) + strlen(str
) + 3);
427 sprintf(tmp
, "%s,\n%*s%s", retstr
, 2 * (level
+ 1), "", str
);
433 tmp
= (char *)wmalloc(strlen(retstr
) + 2 * level
+ 3);
434 sprintf(tmp
, "%s\n%*s)", retstr
, 2 * level
, "");
439 retstr
= wstrdup("{\n");
440 e
= WMEnumerateHashTable(plist
->d
.dict
);
441 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&val
, (void **)&key
)) {
442 skey
= indentedDescription(key
, level
+ 1);
443 sval
= indentedDescription(val
, level
+ 1);
444 tmp
= (char *)wmalloc(2 * (level
+ 1) + strlen(retstr
) + strlen(skey
)
446 sprintf(tmp
, "%s%*s%s = %s;\n", retstr
, 2 * (level
+ 1), "", skey
, sval
);
452 tmp
= (char *)wmalloc(strlen(retstr
) + 2 * level
+ 2);
453 sprintf(tmp
, "%s%*s}", retstr
, 2 * level
, "");
458 wwarning(_("Used proplist functions on non-WMPropLists objects"));
459 wassertrv(False
, NULL
);
466 static inline int getChar(PLData
* pldata
)
470 c
= pldata
->ptr
[pldata
->pos
];
478 pldata
->lineNumber
++;
483 static inline int getNonSpaceChar(PLData
* pldata
)
488 c
= pldata
->ptr
[pldata
->pos
];
494 pldata
->lineNumber
++;
495 } else if (!isspace(c
)) {
503 static char *unescapestr(const char *src
)
505 char *dest
= wmalloc(strlen(src
) + 1);
509 for (dPtr
= dest
; ; dPtr
++) {
520 } else if ((ch
>= '0') && (ch
<= '7')) {
523 /* Convert octal number to character */
526 if ((ch
>= '0') && (ch
<= '7')) {
528 wch
= (wch
<< 3) | (ch
& 07);
530 if ((ch
>= '0') && (ch
<= '7')) {
532 wch
= (wch
<< 3) | (ch
& 07);
571 static WMPropList
*getPLString(PLData
* pldata
)
578 sBuf
.str
= wmalloc(BUFFERSIZE
);
579 sBuf
.size
= BUFFERSIZE
;
583 if (ISSTRINGABLE(c
)) {
584 CHECK_BUFFER_SIZE(sBuf
, ptr
);
599 char *tmp
= unescapestr(sBuf
.str
);
600 plist
= WMCreatePLString(tmp
);
609 static WMPropList
*getPLQString(PLData
* pldata
)
612 int ptr
= 0, escaping
= 0, ok
= 1;
616 sBuf
.str
= wmalloc(BUFFERSIZE
);
617 sBuf
.size
= BUFFERSIZE
;
625 } else if (c
== '"') {
629 CHECK_BUFFER_SIZE(sBuf
, ptr
);
630 sBuf
.str
[ptr
++] = '\\';
635 COMPLAIN(pldata
, _("unterminated PropList string"));
639 CHECK_BUFFER_SIZE(sBuf
, ptr
);
649 char *tmp
= unescapestr(sBuf
.str
);
650 plist
= WMCreatePLString(tmp
);
659 static WMPropList
*getPLData(PLData
* pldata
)
664 unsigned char buf
[BUFFERSIZE
], byte
;
668 data
= WMCreateDataWithCapacity(0);
671 c1
= getNonSpaceChar(pldata
);
673 COMPLAIN(pldata
, _("unterminated PropList data"));
676 } else if (c1
== '>') {
678 } else if (ishexdigit(c1
)) {
679 c2
= getNonSpaceChar(pldata
);
680 if (c2
== 0 || c2
== '>') {
681 COMPLAIN(pldata
, _("unterminated PropList data (missing hexdigit)"));
684 } else if (ishexdigit(c2
)) {
685 byte
= char2num(c1
) << 4;
686 byte
|= char2num(c2
);
688 if (len
== sizeof(buf
)) {
689 WMAppendDataBytes(data
, buf
, len
);
693 COMPLAIN(pldata
, _("non hexdigit character in PropList data"));
698 COMPLAIN(pldata
, _("non hexdigit character in PropList data"));
710 WMAppendDataBytes(data
, buf
, len
);
712 plist
= WMCreatePLData(data
);
718 static WMPropList
*getPLArray(PLData
* pldata
)
723 WMPropList
*array
, *obj
;
725 array
= WMCreatePLArray(NULL
);
728 c
= getNonSpaceChar(pldata
);
730 COMPLAIN(pldata
, _("unterminated PropList array"));
733 } else if (c
== ')') {
735 } else if (c
== ',') {
736 /* continue normally */
738 COMPLAIN(pldata
, _("missing or unterminated PropList array"));
746 obj
= getPropList(pldata
);
748 COMPLAIN(pldata
, _("could not get PropList array element"));
752 WMAddToPLArray(array
, obj
);
753 WMReleasePropList(obj
);
757 WMReleasePropList(array
);
764 static WMPropList
*getPLDictionary(PLData
* pldata
)
768 WMPropList
*dict
, *key
, *value
;
770 dict
= WMCreatePLDictionary(NULL
, NULL
);
773 c
= getNonSpaceChar(pldata
);
775 COMPLAIN(pldata
, _("unterminated PropList dictionary"));
778 } else if (c
== '}') {
782 DPUT("getting PropList dictionary key");
784 key
= getPLData(pldata
);
785 } else if (c
== '"') {
786 key
= getPLQString(pldata
);
787 } else if (ISSTRINGABLE(c
)) {
789 key
= getPLString(pldata
);
792 COMPLAIN(pldata
, _("missing PropList dictionary key"));
794 COMPLAIN(pldata
, _("missing PropList dictionary entry key "
795 "or unterminated dictionary"));
802 COMPLAIN(pldata
, _("error parsing PropList dictionary key"));
807 c
= getNonSpaceChar(pldata
);
809 WMReleasePropList(key
);
810 COMPLAIN(pldata
, _("missing = in PropList dictionary entry"));
815 DPUT("getting PropList dictionary entry value for key");
816 value
= getPropList(pldata
);
818 COMPLAIN(pldata
, _("error parsing PropList dictionary entry value"));
819 WMReleasePropList(key
);
824 c
= getNonSpaceChar(pldata
);
826 COMPLAIN(pldata
, _("missing ; in PropList dictionary entry"));
827 WMReleasePropList(key
);
828 WMReleasePropList(value
);
833 WMPutInPLDictionary(dict
, key
, value
);
834 WMReleasePropList(key
);
835 WMReleasePropList(value
);
839 WMReleasePropList(dict
);
846 static WMPropList
*getPropList(PLData
* pldata
)
851 c
= getNonSpaceChar(pldata
);
855 DPUT("End of PropList");
860 DPUT("Getting PropList dictionary");
861 plist
= getPLDictionary(pldata
);
865 DPUT("Getting PropList array");
866 plist
= getPLArray(pldata
);
870 DPUT("Getting PropList data");
871 plist
= getPLData(pldata
);
875 DPUT("Getting PropList quoted string");
876 plist
= getPLQString(pldata
);
880 if (ISSTRINGABLE(c
)) {
881 DPUT("Getting PropList string");
883 plist
= getPLString(pldata
);
885 COMPLAIN(pldata
, _("was expecting a string, data, array or "
886 "dictionary. If it's a string, try enclosing " "it with \"."));
887 if (c
== '#' || c
== '/') {
888 wwarning(_("Comments are not allowed inside WindowMaker owned" " domain files."));
898 void WMPLSetCaseSensitive(Bool caseSensitiveness
)
900 caseSensitive
= caseSensitiveness
;
903 WMPropList
*WMCreatePLString(const char *str
)
907 wassertrv(str
!= NULL
, NULL
);
909 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
910 plist
->type
= WPLString
;
911 plist
->d
.string
= wstrdup(str
);
912 plist
->retainCount
= 1;
917 WMPropList
*WMCreatePLData(WMData
* data
)
921 wassertrv(data
!= NULL
, NULL
);
923 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
924 plist
->type
= WPLData
;
925 plist
->d
.data
= WMRetainData(data
);
926 plist
->retainCount
= 1;
931 WMPropList
*WMCreatePLDataWithBytes(const unsigned char *bytes
, unsigned int length
)
935 wassertrv(bytes
!= NULL
, NULL
);
937 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
938 plist
->type
= WPLData
;
939 plist
->d
.data
= WMCreateDataWithBytes(bytes
, length
);
940 plist
->retainCount
= 1;
945 WMPropList
*WMCreatePLDataWithBytesNoCopy(unsigned char *bytes
, unsigned int length
, WMFreeDataProc
* destructor
)
949 wassertrv(bytes
!= NULL
, NULL
);
951 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
952 plist
->type
= WPLData
;
953 plist
->d
.data
= WMCreateDataWithBytesNoCopy(bytes
, length
, destructor
);
954 plist
->retainCount
= 1;
959 WMPropList
*WMCreatePLArray(WMPropList
* elem
, ...)
961 WMPropList
*plist
, *nelem
;
964 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
965 plist
->type
= WPLArray
;
966 plist
->d
.array
= WMCreateArray(4);
967 plist
->retainCount
= 1;
972 WMAddToArray(plist
->d
.array
, WMRetainPropList(elem
));
977 nelem
= va_arg(ap
, WMPropList
*);
982 WMAddToArray(plist
->d
.array
, WMRetainPropList(nelem
));
986 WMPropList
*WMCreatePLDictionary(WMPropList
* key
, WMPropList
* value
, ...)
988 WMPropList
*plist
, *nkey
, *nvalue
, *k
, *v
;
991 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
992 plist
->type
= WPLDictionary
;
993 plist
->d
.dict
= WMCreateHashTable(WMPropListHashCallbacks
);
994 plist
->retainCount
= 1;
999 WMHashInsert(plist
->d
.dict
, WMRetainPropList(key
), WMRetainPropList(value
));
1001 va_start(ap
, value
);
1004 nkey
= va_arg(ap
, WMPropList
*);
1009 nvalue
= va_arg(ap
, WMPropList
*);
1014 if (WMHashGetItemAndKey(plist
->d
.dict
, nkey
, (void **)&v
, (void **)&k
)) {
1015 WMHashRemove(plist
->d
.dict
, k
);
1016 WMReleasePropList(k
);
1017 WMReleasePropList(v
);
1019 WMHashInsert(plist
->d
.dict
, WMRetainPropList(nkey
), WMRetainPropList(nvalue
));
1023 WMPropList
*WMRetainPropList(WMPropList
* plist
)
1025 WMPropList
*key
, *value
;
1029 plist
->retainCount
++;
1031 switch (plist
->type
) {
1036 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1037 WMRetainPropList(WMGetFromArray(plist
->d
.array
, i
));
1041 e
= WMEnumerateHashTable(plist
->d
.dict
);
1042 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1043 WMRetainPropList(key
);
1044 WMRetainPropList(value
);
1048 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1049 wassertrv(False
, NULL
);
1056 void WMReleasePropList(WMPropList
* plist
)
1058 WMPropList
*key
, *value
;
1062 plist
->retainCount
--;
1064 switch (plist
->type
) {
1066 if (plist
->retainCount
< 1) {
1067 wfree(plist
->d
.string
);
1072 if (plist
->retainCount
< 1) {
1073 WMReleaseData(plist
->d
.data
);
1078 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1079 WMReleasePropList(WMGetFromArray(plist
->d
.array
, i
));
1081 if (plist
->retainCount
< 1) {
1082 WMFreeArray(plist
->d
.array
);
1087 e
= WMEnumerateHashTable(plist
->d
.dict
);
1088 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1089 WMReleasePropList(key
);
1090 WMReleasePropList(value
);
1092 if (plist
->retainCount
< 1) {
1093 WMFreeHashTable(plist
->d
.dict
);
1098 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1104 void WMInsertInPLArray(WMPropList
* plist
, int index
, WMPropList
* item
)
1106 wassertr(plist
->type
== WPLArray
);
1108 retainPropListByCount(item
, plist
->retainCount
);
1109 WMInsertInArray(plist
->d
.array
, index
, item
);
1112 void WMAddToPLArray(WMPropList
* plist
, WMPropList
* item
)
1114 wassertr(plist
->type
== WPLArray
);
1116 retainPropListByCount(item
, plist
->retainCount
);
1117 WMAddToArray(plist
->d
.array
, item
);
1120 void WMDeleteFromPLArray(WMPropList
* plist
, int index
)
1124 wassertr(plist
->type
== WPLArray
);
1126 item
= WMGetFromArray(plist
->d
.array
, index
);
1128 WMDeleteFromArray(plist
->d
.array
, index
);
1129 releasePropListByCount(item
, plist
->retainCount
);
1133 void WMRemoveFromPLArray(WMPropList
* plist
, WMPropList
* item
)
1138 wassertr(plist
->type
== WPLArray
);
1140 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1141 iPtr
= WMGetFromArray(plist
->d
.array
, i
);
1142 if (WMIsPropListEqualTo(item
, iPtr
)) {
1143 WMDeleteFromArray(plist
->d
.array
, i
);
1144 releasePropListByCount(iPtr
, plist
->retainCount
);
1150 void WMPutInPLDictionary(WMPropList
* plist
, WMPropList
* key
, WMPropList
* value
)
1152 wassertr(plist
->type
== WPLDictionary
);
1154 /*WMRetainPropList(key); */
1155 WMRemoveFromPLDictionary(plist
, key
);
1156 retainPropListByCount(key
, plist
->retainCount
);
1157 retainPropListByCount(value
, plist
->retainCount
);
1158 WMHashInsert(plist
->d
.dict
, key
, value
);
1159 /*WMReleasePropList(key); */
1162 void WMRemoveFromPLDictionary(WMPropList
* plist
, WMPropList
* key
)
1166 wassertr(plist
->type
== WPLDictionary
);
1168 if (WMHashGetItemAndKey(plist
->d
.dict
, key
, (void **)&v
, (void **)&k
)) {
1169 WMHashRemove(plist
->d
.dict
, k
);
1170 releasePropListByCount(k
, plist
->retainCount
);
1171 releasePropListByCount(v
, plist
->retainCount
);
1175 WMPropList
*WMMergePLDictionaries(WMPropList
* dest
, WMPropList
* source
, Bool recursive
)
1177 WMPropList
*key
, *value
, *dvalue
;
1180 wassertrv(source
->type
== WPLDictionary
&& dest
->type
== WPLDictionary
, NULL
);
1185 e
= WMEnumerateHashTable(source
->d
.dict
);
1186 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1187 if (recursive
&& value
->type
== WPLDictionary
) {
1188 dvalue
= WMHashGet(dest
->d
.dict
, key
);
1189 if (dvalue
&& dvalue
->type
== WPLDictionary
) {
1190 WMMergePLDictionaries(dvalue
, value
, True
);
1192 WMPutInPLDictionary(dest
, key
, value
);
1195 WMPutInPLDictionary(dest
, key
, value
);
1202 WMPropList
*WMSubtractPLDictionaries(WMPropList
* dest
, WMPropList
* source
, Bool recursive
)
1204 WMPropList
*key
, *value
, *dvalue
;
1207 wassertrv(source
->type
== WPLDictionary
&& dest
->type
== WPLDictionary
, NULL
);
1209 if (source
== dest
) {
1210 WMPropList
*keys
= WMGetPLDictionaryKeys(dest
);
1213 for (i
= 0; i
< WMGetArrayItemCount(keys
->d
.array
); i
++) {
1214 WMRemoveFromPLDictionary(dest
, WMGetFromArray(keys
->d
.array
, i
));
1216 WMReleasePropList(keys
);
1220 e
= WMEnumerateHashTable(source
->d
.dict
);
1221 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1222 dvalue
= WMHashGet(dest
->d
.dict
, key
);
1225 if (WMIsPropListEqualTo(value
, dvalue
)) {
1226 WMRemoveFromPLDictionary(dest
, key
);
1227 } else if (recursive
&& value
->type
== WPLDictionary
&& dvalue
->type
== WPLDictionary
) {
1228 WMSubtractPLDictionaries(dvalue
, value
, True
);
1235 int WMGetPropListItemCount(WMPropList
* plist
)
1237 switch (plist
->type
) {
1240 return 0; /* should this be 1 instead? */
1242 return WMGetArrayItemCount(plist
->d
.array
);
1244 return (int)WMCountHashTable(plist
->d
.dict
);
1246 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1247 wassertrv(False
, 0);
1254 Bool
WMIsPLString(WMPropList
* plist
)
1256 return (plist
->type
== WPLString
);
1259 Bool
WMIsPLData(WMPropList
* plist
)
1261 return (plist
->type
== WPLData
);
1264 Bool
WMIsPLArray(WMPropList
* plist
)
1266 return (plist
->type
== WPLArray
);
1269 Bool
WMIsPLDictionary(WMPropList
* plist
)
1271 return (plist
->type
== WPLDictionary
);
1274 Bool
WMIsPropListEqualTo(WMPropList
* plist
, WMPropList
* other
)
1276 WMPropList
*key1
, *item1
, *item2
;
1277 WMHashEnumerator enumerator
;
1280 if (plist
->type
!= other
->type
)
1283 switch (plist
->type
) {
1285 if (caseSensitive
) {
1286 return (strcmp(plist
->d
.string
, other
->d
.string
) == 0);
1288 return (strcasecmp(plist
->d
.string
, other
->d
.string
) == 0);
1291 return WMIsDataEqualToData(plist
->d
.data
, other
->d
.data
);
1293 n
= WMGetArrayItemCount(plist
->d
.array
);
1294 if (n
!= WMGetArrayItemCount(other
->d
.array
))
1296 for (i
= 0; i
< n
; i
++) {
1297 item1
= WMGetFromArray(plist
->d
.array
, i
);
1298 item2
= WMGetFromArray(other
->d
.array
, i
);
1299 if (!WMIsPropListEqualTo(item1
, item2
))
1304 if (WMCountHashTable(plist
->d
.dict
) != WMCountHashTable(other
->d
.dict
))
1306 enumerator
= WMEnumerateHashTable(plist
->d
.dict
);
1307 while (WMNextHashEnumeratorItemAndKey(&enumerator
, (void **)&item1
, (void **)&key1
)) {
1308 item2
= WMHashGet(other
->d
.dict
, key1
);
1309 if (!item2
|| !item1
|| !WMIsPropListEqualTo(item1
, item2
))
1314 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1315 wassertrv(False
, False
);
1322 char *WMGetFromPLString(WMPropList
* plist
)
1324 wassertrv(plist
->type
== WPLString
, NULL
);
1326 return plist
->d
.string
;
1329 WMData
*WMGetFromPLData(WMPropList
* plist
)
1331 wassertrv(plist
->type
== WPLData
, NULL
);
1333 return plist
->d
.data
;
1336 const unsigned char *WMGetPLDataBytes(WMPropList
* plist
)
1338 wassertrv(plist
->type
== WPLData
, NULL
);
1340 return WMDataBytes(plist
->d
.data
);
1343 int WMGetPLDataLength(WMPropList
* plist
)
1345 wassertrv(plist
->type
== WPLData
, 0);
1347 return WMGetDataLength(plist
->d
.data
);
1350 WMPropList
*WMGetFromPLArray(WMPropList
* plist
, int index
)
1352 wassertrv(plist
->type
== WPLArray
, NULL
);
1354 return WMGetFromArray(plist
->d
.array
, index
);
1357 WMPropList
*WMGetFromPLDictionary(WMPropList
* plist
, WMPropList
* key
)
1359 wassertrv(plist
->type
== WPLDictionary
, NULL
);
1361 return WMHashGet(plist
->d
.dict
, key
);
1364 WMPropList
*WMGetPLDictionaryKeys(WMPropList
* plist
)
1366 WMPropList
*array
, *key
;
1367 WMHashEnumerator enumerator
;
1369 wassertrv(plist
->type
== WPLDictionary
, NULL
);
1371 array
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
1372 array
->type
= WPLArray
;
1373 array
->d
.array
= WMCreateArray(WMCountHashTable(plist
->d
.dict
));
1374 array
->retainCount
= 1;
1376 enumerator
= WMEnumerateHashTable(plist
->d
.dict
);
1377 while ((key
= WMNextHashEnumeratorKey(&enumerator
))) {
1378 WMAddToArray(array
->d
.array
, WMRetainPropList(key
));
1384 WMPropList
*WMShallowCopyPropList(WMPropList
* plist
)
1386 WMPropList
*ret
= NULL
;
1387 WMPropList
*key
, *item
;
1392 switch (plist
->type
) {
1394 ret
= WMCreatePLString(plist
->d
.string
);
1397 data
= WMCreateDataWithData(plist
->d
.data
);
1398 ret
= WMCreatePLData(data
);
1399 WMReleaseData(data
);
1402 ret
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
1403 ret
->type
= WPLArray
;
1404 ret
->d
.array
= WMCreateArrayWithArray(plist
->d
.array
);
1405 ret
->retainCount
= 1;
1407 for (i
= 0; i
< WMGetArrayItemCount(ret
->d
.array
); i
++)
1408 WMRetainPropList(WMGetFromArray(ret
->d
.array
, i
));
1412 ret
= WMCreatePLDictionary(NULL
, NULL
);
1413 e
= WMEnumerateHashTable(plist
->d
.dict
);
1414 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&item
, (void **)&key
)) {
1415 WMPutInPLDictionary(ret
, key
, item
);
1419 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1420 wassertrv(False
, NULL
);
1427 WMPropList
*WMDeepCopyPropList(WMPropList
* plist
)
1429 WMPropList
*ret
= NULL
;
1430 WMPropList
*key
, *item
;
1435 switch (plist
->type
) {
1437 ret
= WMCreatePLString(plist
->d
.string
);
1440 data
= WMCreateDataWithData(plist
->d
.data
);
1441 ret
= WMCreatePLData(data
);
1442 WMReleaseData(data
);
1445 ret
= WMCreatePLArray(NULL
);
1446 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1447 item
= WMDeepCopyPropList(WMGetFromArray(plist
->d
.array
, i
));
1448 WMAddToArray(ret
->d
.array
, item
);
1452 ret
= WMCreatePLDictionary(NULL
, NULL
);
1453 e
= WMEnumerateHashTable(plist
->d
.dict
);
1454 /* While we copy an existing dictionary there is no way that we can
1455 * have duplicate keys, so we don't need to first remove a key/value
1456 * pair before inserting the new key/value.
1458 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&item
, (void **)&key
)) {
1459 WMHashInsert(ret
->d
.dict
, WMDeepCopyPropList(key
), WMDeepCopyPropList(item
));
1463 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1464 wassertrv(False
, NULL
);
1471 WMPropList
*WMCreatePropListFromDescription(const char *desc
)
1473 WMPropList
*plist
= NULL
;
1476 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1478 pldata
->lineNumber
= 1;
1480 plist
= getPropList(pldata
);
1482 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1483 COMPLAIN(pldata
, _("extra data after end of property list"));
1485 * We can't just ignore garbage after the end of the description
1486 * (especially if the description was read from a file), because
1487 * the "garbage" can be the real data and the real garbage is in
1488 * fact in the beginning of the file (which is now inside plist)
1490 WMReleasePropList(plist
);
1499 char *WMGetPropListDescription(WMPropList
* plist
, Bool indented
)
1501 return (indented
? indentedDescription(plist
, 0) : description(plist
));
1504 WMPropList
*WMReadPropListFromFile(const char *file
)
1506 WMPropList
*plist
= NULL
;
1513 f
= fopen(file
, "rb");
1515 /* let the user print the error message if he really needs to */
1516 /*werror(_("could not open domain file '%s' for reading"), file); */
1520 if (stat(file
, &stbuf
) == 0) {
1521 length
= (size_t) stbuf
.st_size
;
1523 werror(_("could not get size for file '%s'"), file
);
1528 read_buf
= wmalloc(length
+ 1);
1529 if (fread(read_buf
, length
, 1, f
) != 1) {
1531 werror(_("error reading from file '%s'"), file
);
1537 read_buf
[length
] = '\0';
1540 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1541 pldata
->ptr
= read_buf
;
1542 pldata
->filename
= file
;
1543 pldata
->lineNumber
= 1;
1545 plist
= getPropList(pldata
);
1547 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1548 COMPLAIN(pldata
, _("extra data after end of property list"));
1550 * We can't just ignore garbage after the end of the description
1551 * (especially if the description was read from a file), because
1552 * the "garbage" can be the real data and the real garbage is in
1553 * fact in the beginning of the file (which is now inside plist)
1555 WMReleasePropList(plist
);
1565 WMPropList
*WMReadPropListFromPipe(const char *command
)
1570 char *read_buf
, *read_ptr
;
1571 size_t remain_size
, line_size
;
1572 const size_t block_read_size
= 4096;
1573 const size_t block_read_margin
= 512;
1575 file
= popen(command
, "r");
1578 werror(_("%s:could not open menu file"), command
);
1582 /* read from file till EOF or OOM and fill proplist buffer*/
1583 remain_size
= block_read_size
;
1584 read_buf
= wmalloc(remain_size
);
1585 read_ptr
= read_buf
;
1586 while (fgets(read_ptr
, remain_size
, file
) != NULL
) {
1587 line_size
= strlen(read_ptr
);
1589 remain_size
-= line_size
;
1590 read_ptr
+= line_size
;
1592 if (remain_size
< block_read_margin
) {
1595 read_length
= read_ptr
- read_buf
;
1596 read_buf
= wrealloc(read_buf
, read_length
+ block_read_size
);
1597 read_ptr
= read_buf
+ read_length
;
1598 remain_size
= block_read_size
;
1604 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1605 pldata
->ptr
= read_buf
;
1606 pldata
->filename
= command
;
1607 pldata
->lineNumber
= 1;
1609 plist
= getPropList(pldata
);
1611 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1612 COMPLAIN(pldata
, _("extra data after end of property list"));
1614 * We can't just ignore garbage after the end of the description
1615 * (especially if the description was read from a file), because
1616 * the "garbage" can be the real data and the real garbage is in
1617 * fact in the beginning of the file (which is now inside plist)
1619 WMReleasePropList(plist
);
1629 /* TODO: review this function's code */
1631 Bool
WMWritePropListToFile(WMPropList
* plist
, const char *path
)
1633 char *thePath
= NULL
;
1640 if (!wmkdirhier(path
))
1643 /* Use the path name of the destination file as a prefix for the
1644 * mkstemp() call so that we can be sure that both files are on
1645 * the same filesystem and the subsequent rename() will work. */
1646 thePath
= wstrconcat(path
, ".XXXXXX");
1650 * We really just want to read the current umask, but as Coverity is
1651 * pointing a possible security issue:
1652 * some versions of mkstemp do not set file rights properly on the
1653 * created file, so it is recommended so set the umask beforehand.
1654 * As we need to set an umask to read the current value, we take this
1655 * opportunity to set a temporary aggresive umask so Coverity won't
1656 * complain, even if we do not really care in the present use case.
1658 mask
= umask(S_IRWXG
| S_IRWXO
);
1659 if ((fd
= mkstemp(thePath
)) < 0) {
1660 werror(_("mkstemp (%s) failed"), thePath
);
1664 fchmod(fd
, 0666 & ~mask
);
1665 if ((theFile
= fdopen(fd
, "wb")) == NULL
) {
1669 if (mktemp(thePath
) == NULL
) {
1670 werror(_("mktemp (%s) failed"), thePath
);
1673 theFile
= fopen(thePath
, "wb");
1676 if (theFile
== NULL
) {
1677 werror(_("open (%s) failed"), thePath
);
1681 desc
= indentedDescription(plist
, 0);
1683 if (fprintf(theFile
, "%s\n", desc
) != strlen(desc
) + 1) {
1684 werror(_("writing to file: %s failed"), thePath
);
1691 (void)fsync(fileno(theFile
));
1692 if (fclose(theFile
) != 0) {
1693 werror(_("fclose (%s) failed"), thePath
);
1697 /* If we used a temporary file, we still need to rename() it be the
1698 * real file. Also, we need to try to retain the file attributes of
1699 * the original file we are overwriting (if we are) */
1700 if (rename(thePath
, path
) != 0) {
1701 werror(_("rename ('%s' to '%s') failed"), thePath
, path
);
1715 * create a directory hierarchy
1717 * if the last octet of `path' is `/', the full path is
1718 * assumed to be a directory; otherwise path is assumed to be a
1719 * file, and the last component is stripped off. the rest is the
1720 * the hierarchy to be created.
1722 * refuses to create anything outside $GNUSTEP_USER_ROOT
1724 * returns 1 on success, 0 on failure
1726 int wmkdirhier(const char *path
)
1729 char *thePath
= NULL
, buf
[1024];
1733 /* Only create directories under $GNUSTEP_USER_ROOT */
1734 if ((t
= wusergnusteppath()) == NULL
)
1736 if (strncmp(path
, t
, strlen(t
)) != 0)
1739 thePath
= wstrdup(path
);
1740 /* Strip the trailing component if it is a file */
1741 p
= strlen(thePath
);
1742 while (p
&& thePath
[p
] != '/')
1743 thePath
[p
--] = '\0';
1747 /* Shortcut if it already exists */
1748 if (stat(thePath
, &st
) == 0) {
1750 if (S_ISDIR(st
.st_mode
)) {
1751 /* Is a directory alright */
1754 /* Exists, but not a directory, the caller
1755 * might just as well abort now */
1760 memset(buf
, 0, sizeof(buf
));
1761 strncpy(buf
, t
, sizeof(buf
) - 1);
1763 plen
= strlen(thePath
);
1766 while (p
++ < plen
&& thePath
[p
] != '/')
1769 strncpy(buf
, thePath
, p
);
1770 if (mkdir(buf
, 0777) == -1 && errno
== EEXIST
&&
1771 stat(buf
, &st
) == 0 && !S_ISDIR(st
.st_mode
)) {
1772 werror(_("Could not create component %s"), buf
);
1783 static int wrmdirhier_fn(const char *path
, const struct stat
*st
,
1784 int type
, struct FTW
*ftw
)
1786 /* Parameter not used, but tell the compiler that it is ok */
1799 return unlink(path
);
1812 * remove a directory hierarchy
1814 * refuses to remove anything outside $GNUSTEP_USER_ROOT
1816 * returns 1 on success, 0 on failure
1818 * TODO: revisit what's error and what's not
1820 * with inspirations from OpenBSD's bin/rm/rm.c
1822 int wrmdirhier(const char *path
)
1828 /* Only remove directories under $GNUSTEP_USER_ROOT */
1829 if ((t
= wusergnusteppath()) == NULL
)
1831 if (strncmp(path
, t
, strlen(t
)) != 0)
1834 /* Shortcut if it doesn't exist to begin with */
1835 if (stat(path
, &st
) == -1)
1838 error
= nftw(path
, wrmdirhier_fn
, 1, FTW_PHYS
);