Update for 0.51.0
[wmaker-crm.git] / util / getstyle.c
blobd8c0a0cb61d45fbbf252b91fa6e6e872d7048e11
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <proplist.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <pwd.h>
31 #include <limits.h>
33 #ifndef PATH_MAX
34 #define PATH_MAX 1024
35 #endif
37 #include "../src/wconfig.h"
39 /* table of style related options */
40 static char *options[] = {
41 "TitleJustify",
42 "ClipTitleFont",
43 "WindowTitleFont",
44 "MenuTitleFont",
45 "MenuTextFont",
46 "IconTitleFont",
47 "DisplayFont",
48 "HighlightColor",
49 "HighlightTextColor",
50 "ClipTitleColor",
51 "CClipTitleColor",
52 "FTitleColor",
53 "PTitleColor",
54 "UTitleColor",
55 "FTitleBack",
56 "PTitleBack",
57 "UTitleBack",
58 "MenuTitleColor",
59 "MenuTextColor",
60 "MenuDisabledColor",
61 "MenuTitleBack",
62 "MenuTextBack",
63 "IconBack",
64 "IconTitleColor",
65 "IconTitleBack",
66 #ifdef TITLE_TEXT_SHADOW
67 "Shadow",
68 "FShadowColor",
69 "PShadowColor",
70 "UShadowColor",
71 #endif
72 NULL
76 /* table of theme related options */
77 static char *theme_options[] = {
78 "WorkspaceBack",
79 NULL
85 char *ProgName;
87 char *PixmapPath = NULL;
89 char *ThemePath = NULL;
92 void
93 print_help()
95 printf("usage: %s [-options] [<style file>]\n", ProgName);
96 puts("options:");
97 puts(" -h print help");
98 puts(" -t get theme options too");
99 puts(" -p produce a theme pack");
103 char*
104 globalDefaultsPathForDomain(char *domain)
106 char path[1024];
107 char *tmp;
109 sprintf(path, "%s/%s/%s", PKGDATADIR, DEFAULTS_DIR, domain);
111 tmp = malloc(strlen(path)+2);
112 strcpy(tmp, path);
114 return tmp;
118 char*
119 defaultsPathForDomain(char *domain)
121 char path[1024];
122 char *gspath, *tmp;
124 gspath = getenv("GNUSTEP_USER_ROOT");
125 if (gspath) {
126 strcpy(path, gspath);
127 strcat(path, "/");
128 } else {
129 char *home;
131 home = getenv("HOME");
132 if (!home) {
133 printf("%s:could not get HOME environment variable!\n", ProgName);
134 exit(0);
136 strcpy(path, home);
137 strcat(path, "/GNUstep/");
139 strcat(path, DEFAULTS_DIR);
140 strcat(path, "/");
141 strcat(path, domain);
143 tmp = malloc(strlen(path)+2);
144 strcpy(tmp, path);
146 return tmp;
150 BOOL
151 StringCompareHook(proplist_t pl1, proplist_t pl2)
153 char *str1, *str2;
155 str1 = PLGetString(pl1);
156 str2 = PLGetString(pl2);
158 if (strcasecmp(str1, str2)==0)
159 return YES;
160 else
161 return NO;
165 void
166 abortar(char *reason)
168 char buffer[4000];
170 printf("%s: %s\n", ProgName, reason);
172 if (ThemePath) {
173 printf("Removing unfinished theme pack\n");
174 sprintf(buffer, "/bin/rm -fr %s", ThemePath);
176 if (system(buffer)!=0) {
177 printf("%s: could not execute command %s\n", ProgName, buffer);
180 exit(1);
186 char*
187 wgethomedir()
189 char *home = getenv("HOME");
190 struct passwd *user;
192 if (home)
193 return home;
195 user = getpwuid(getuid());
196 if (!user) {
197 char buffer[80];
199 sprintf(buffer, "could not get password entry for UID %i", getuid());
200 perror(buffer);
201 return "/";
203 if (!user->pw_dir) {
204 return "/";
205 } else {
206 return user->pw_dir;
211 void*
212 wmalloc(int size)
214 void *tmp;
216 tmp = malloc(size);
217 if (!tmp) {
218 abortar("out of memory");
221 return tmp;
225 char*
226 wstrdup(char *str)
228 char *tmp;
230 tmp = wmalloc(strlen(str)+1);
232 strcpy(tmp, str);
234 return tmp;
238 static char*
239 getuserhomedir(char *username)
241 struct passwd *user;
243 user = getpwnam(username);
244 if (!user) {
245 char buffer[100];
247 sprintf(buffer,"could not get password entry for user %s", username);
248 perror(buffer);
249 return NULL;
251 if (!user->pw_dir) {
252 return "/";
253 } else {
254 return user->pw_dir;
261 char*
262 wexpandpath(char *path)
264 char buffer2[PATH_MAX+2];
265 char buffer[PATH_MAX+2];
266 int i;
268 memset(buffer, 0, PATH_MAX+2);
270 if (*path=='~') {
271 char *home;
273 path++;
274 if (*path=='/' || *path==0) {
275 home = wgethomedir();
276 strcat(buffer, home);
277 } else {
278 int j;
279 j = 0;
280 while (*path!=0 && *path!='/') {
281 buffer2[j++] = *path;
282 buffer2[j] = 0;
283 path++;
285 home = getuserhomedir(buffer2);
286 if (!home)
287 return NULL;
288 strcat(buffer, home);
292 i = strlen(buffer);
294 while (*path!=0) {
295 char *tmp;
297 if (*path=='$') {
298 int j = 0;
299 path++;
300 /* expand $(HOME) or $HOME style environment variables */
301 if (*path=='(') {
302 path++;
303 while (*path!=0 && *path!=')') {
304 buffer2[j++] = *(path++);
305 buffer2[j] = 0;
307 if (*path==')')
308 path++;
309 tmp = getenv(buffer2);
310 if (!tmp) {
311 buffer[i] = 0;
312 strcat(buffer, "$(");
313 strcat(buffer, buffer2);
314 strcat(buffer, ")");
315 i += strlen(buffer2)+3;
316 } else {
317 strcat(buffer, tmp);
318 i += strlen(tmp);
320 } else {
321 while (*path!=0 && *path!='/') {
322 buffer2[j++] = *(path++);
323 buffer2[j] = 0;
325 tmp = getenv(buffer2);
326 if (!tmp) {
327 strcat(buffer, "$");
328 strcat(buffer, buffer2);
329 i += strlen(buffer2)+1;
330 } else {
331 strcat(buffer, tmp);
332 i += strlen(tmp);
335 } else {
336 buffer[i++] = *path;
337 path++;
341 return wstrdup(buffer);
346 char*
347 wfindfileinarray(proplist_t paths, char *file)
349 int i;
350 char *path;
351 int len, flen;
352 char *fullpath;
354 if (!file)
355 return NULL;
357 if (*file=='/' || *file=='~' || !paths || !PLIsArray(paths)
358 || PLGetNumberOfElements(paths)==0) {
359 if (access(file, R_OK)<0) {
360 fullpath = wexpandpath(file);
361 if (!fullpath)
362 return NULL;
364 if (access(fullpath, R_OK)<0) {
365 free(fullpath);
366 return NULL;
367 } else {
368 return fullpath;
370 } else {
371 return wstrdup(file);
375 flen = strlen(file);
376 for (i=0; i < PLGetNumberOfElements(paths); i++) {
377 proplist_t tmp;
378 char *dir;
380 tmp = PLGetArrayElement(paths, i);
381 if (!PLIsString(tmp) || !(dir = PLGetString(tmp)))
382 continue;
384 len = strlen(dir);
385 path = wmalloc(len+flen+2);
386 path = memcpy(path, dir, len);
387 path[len]=0;
388 strcat(path, "/");
389 strcat(path, file);
390 /* expand tilde */
391 fullpath = wexpandpath(path);
392 free(path);
393 if (fullpath) {
394 /* check if file is readable */
395 if (access(fullpath, R_OK)==0) {
396 return fullpath;
398 free(fullpath);
401 return NULL;
407 void
408 copyFile(char *dir, char *file)
410 char buffer[4000];
412 sprintf(buffer, "/bin/cp %s %s", file, dir);
413 if (system(buffer)!=0) {
414 printf("%s: could not copy file %s\n", ProgName, file);
419 void
420 findCopyFile(char *dir, char *file)
422 char *fullPath;
424 fullPath = wfindfileinarray(PixmapPath, file);
425 if (!fullPath) {
426 char buffer[4000];
428 sprintf(buffer, "coould not find file %s", file);
429 abortar(buffer);
431 copyFile(dir, fullPath);
432 free(fullPath);
436 char*
437 makeThemePack(proplist_t style, char *themeName)
439 proplist_t keys;
440 proplist_t key;
441 proplist_t value;
442 int i;
443 char *themeDir;
445 themeDir = wmalloc(strlen(themeName)+50);
446 sprintf(themeDir, "%s.themed", themeName);
447 ThemePath = themeDir;
449 char *tmp;
451 tmp = wmalloc(strlen(themeDir)+20);
452 sprintf(tmp, "/bin/mkdir %s", themeDir);
453 if (system(tmp)!=0) {
454 printf("%s: could not create directory %s\n", ProgName, themeDir);
455 exit(1);
457 free(tmp);
459 keys = PLGetAllDictionaryKeys(style);
461 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
462 key = PLGetArrayElement(keys, i);
464 value = PLGetDictionaryEntry(style, key);
465 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
466 proplist_t type;
467 char *t;
469 type = PLGetArrayElement(value, 0);
470 t = PLGetString(type);
471 if (t && (strcasecmp(t, "tpixmap")==0
472 || strcasecmp(t, "spixmap")==0
473 || strcasecmp(t, "cpixmap")==0
474 || strcasecmp(t, "mpixmap")==0
475 || strcasecmp(t, "tdgradient")==0
476 || strcasecmp(t, "tvgradient")==0
477 || strcasecmp(t, "thgradient")==0)) {
478 proplist_t file;
479 char *p;
480 char *newPath;
482 file = PLGetArrayElement(value, 1);
484 p = strrchr(PLGetString(file), '/');
485 if (p) {
486 copyFile(themeDir, PLGetString(file));
488 newPath = wstrdup(p+1);
489 PLRemoveArrayElement(value, 1);
490 PLInsertArrayElement(value, PLMakeString(newPath), 1);
491 free(newPath);
492 } else {
493 findCopyFile(themeDir, PLGetString(file));
499 return themeDir;
503 int
504 main(int argc, char **argv)
506 proplist_t prop, style, key, val;
507 char *path;
508 int i, theme_too=0;
509 int make_pack = 0;
510 char *style_file = NULL;
513 ProgName = argv[0];
515 if (argc>1) {
516 for (i=1; i<argc; i++) {
517 if (strcmp(argv[i], "-p")==0) {
518 make_pack = 1;
519 theme_too = 1;
520 } else if (strcmp(argv[i], "-t")==0) {
521 theme_too++;
522 } else if (argv[i][0] != '-') {
523 style_file = argv[i];
524 } else {
525 print_help();
526 exit(1);
531 if (make_pack && !style_file) {
532 printf("%s: you must supply a name for the theme pack\n", ProgName);
533 exit(1);
536 PLSetStringCmpHook(StringCompareHook);
538 path = defaultsPathForDomain("WindowMaker");
540 prop = PLGetProplistWithPath(path);
541 if (!prop) {
542 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
543 ProgName, path);
544 exit(1);
546 free(path);
548 /* get global value */
549 path = globalDefaultsPathForDomain("WindowMaker");
550 val = PLGetProplistWithPath(path);
551 if (val) {
552 PLMergeDictionaries(val, prop);
553 PLRelease(prop);
554 prop = val;
557 style = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
560 for (i=0; options[i]!=NULL; i++) {
561 key = PLMakeString(options[i]);
563 val = PLGetDictionaryEntry(prop, key);
564 if (val)
565 PLInsertDictionaryEntry(style, key, val);
568 val = PLGetDictionaryEntry(prop, PLMakeString("PixmapPath"));
569 if (val)
570 PixmapPath = PLGetString(val);
572 if (theme_too) {
573 for (i=0; theme_options[i]!=NULL; i++) {
574 key = PLMakeString(theme_options[i]);
576 val = PLGetDictionaryEntry(prop, key);
577 if (val)
578 PLInsertDictionaryEntry(style, key, val);
582 if (make_pack) {
583 char *path;
585 makeThemePack(style, style_file);
587 path = wmalloc(strlen(ThemePath)+32);
588 strcpy(path, ThemePath);
589 strcat(path, "/style");
590 PLSetFilename(style, PLMakeString(path));
591 PLSave(style, NO);
592 } else {
593 if (style_file) {
594 val = PLMakeString(style_file);
595 PLSetFilename(style, val);
596 PLSave(style, NO);
597 } else {
598 puts(PLGetDescriptionIndent(style, 0));
601 exit(0);