0.51.1 pre snapshot. Be careful, it may be buggy. It fixes some bugs though.
[wmaker-crm.git] / util / getstyle.c
blob87105046c9e4d0c31599604ecbc1354e1cc9d24f
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 #define PROG_VERSION "getstyle (Window Maker) 0.2"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <proplist.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <limits.h>
36 #include <assert.h>
38 #ifndef PATH_MAX
39 #define PATH_MAX 1024
40 #endif
42 #include "../src/wconfig.h"
44 /* table of style related options */
45 static char *options[] = {
46 "TitleJustify",
47 "ClipTitleFont",
48 "WindowTitleFont",
49 "MenuTitleFont",
50 "MenuTextFont",
51 "IconTitleFont",
52 "DisplayFont",
53 "HighlightColor",
54 "HighlightTextColor",
55 "ClipTitleColor",
56 "CClipTitleColor",
57 "FTitleColor",
58 "PTitleColor",
59 "UTitleColor",
60 "FTitleBack",
61 "PTitleBack",
62 "UTitleBack",
63 "MenuTitleColor",
64 "MenuTextColor",
65 "MenuDisabledColor",
66 "MenuTitleBack",
67 "MenuTextBack",
68 "IconBack",
69 "IconTitleColor",
70 "IconTitleBack",
71 #ifdef TITLE_TEXT_SHADOW
72 "Shadow",
73 "FShadowColor",
74 "PShadowColor",
75 "UShadowColor",
76 #endif
77 NULL
81 /* table of theme related options */
82 static char *theme_options[] = {
83 "WorkspaceBack",
84 NULL
90 char *ProgName;
92 proplist_t PixmapPath = NULL;
94 char *ThemePath = NULL;
97 void
98 print_help()
100 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
101 puts("Retrieves style/theme configuration and output to FILE or to stdout");
102 puts("");
103 puts(" -t, --theme-options output theme related options when producing a style file");
104 puts(" -p, --pack produce output as a theme pack");
105 puts(" --help display this help and exit");
106 puts(" --version output version information and exit");
110 char*
111 globalDefaultsPathForDomain(char *domain)
113 char path[1024];
114 char *tmp;
116 sprintf(path, "%s/%s", SYSCONFDIR, domain);
118 tmp = malloc(strlen(path)+2);
119 assert(tmp!=NULL);
120 strcpy(tmp, path);
122 return tmp;
126 char*
127 defaultsPathForDomain(char *domain)
129 char path[1024];
130 char *gspath, *tmp;
132 gspath = getenv("GNUSTEP_USER_ROOT");
133 if (gspath) {
134 strcpy(path, gspath);
135 strcat(path, "/");
136 } else {
137 char *home;
139 home = getenv("HOME");
140 if (!home) {
141 printf("%s:could not get HOME environment variable!\n", ProgName);
142 exit(0);
144 strcpy(path, home);
145 strcat(path, "/GNUstep/");
147 strcat(path, DEFAULTS_DIR);
148 strcat(path, "/");
149 strcat(path, domain);
151 tmp = malloc(strlen(path)+2);
152 strcpy(tmp, path);
154 return tmp;
158 BOOL
159 StringCompareHook(proplist_t pl1, proplist_t pl2)
161 char *str1, *str2;
163 str1 = PLGetString(pl1);
164 str2 = PLGetString(pl2);
166 if (strcasecmp(str1, str2)==0)
167 return YES;
168 else
169 return NO;
173 void
174 abortar(char *reason)
176 char buffer[4000];
178 printf("%s: %s\n", ProgName, reason);
180 if (ThemePath) {
181 printf("Removing unfinished theme pack\n");
182 sprintf(buffer, "/bin/rm -fr %s", ThemePath);
184 if (system(buffer)!=0) {
185 printf("%s: could not execute command %s\n", ProgName, buffer);
188 exit(1);
194 char*
195 wgethomedir()
197 char *home = getenv("HOME");
198 struct passwd *user;
200 if (home)
201 return home;
203 user = getpwuid(getuid());
204 if (!user) {
205 char buffer[80];
207 sprintf(buffer, "could not get password entry for UID %i", getuid());
208 perror(buffer);
209 return "/";
211 if (!user->pw_dir) {
212 return "/";
213 } else {
214 return user->pw_dir;
219 void*
220 wmalloc(int size)
222 void *tmp;
224 tmp = malloc(size);
225 if (!tmp) {
226 abortar("out of memory");
229 return tmp;
233 char*
234 wstrdup(char *str)
236 char *tmp;
238 tmp = wmalloc(strlen(str)+1);
240 strcpy(tmp, str);
242 return tmp;
246 static char*
247 getuserhomedir(char *username)
249 struct passwd *user;
251 user = getpwnam(username);
252 if (!user) {
253 char buffer[100];
255 sprintf(buffer,"could not get password entry for user %s", username);
256 perror(buffer);
257 return NULL;
259 if (!user->pw_dir) {
260 return "/";
261 } else {
262 return user->pw_dir;
269 char*
270 wexpandpath(char *path)
272 char buffer2[PATH_MAX+2];
273 char buffer[PATH_MAX+2];
274 int i;
276 memset(buffer, 0, PATH_MAX+2);
278 if (*path=='~') {
279 char *home;
281 path++;
282 if (*path=='/' || *path==0) {
283 home = wgethomedir();
284 strcat(buffer, home);
285 } else {
286 int j;
287 j = 0;
288 while (*path!=0 && *path!='/') {
289 buffer2[j++] = *path;
290 buffer2[j] = 0;
291 path++;
293 home = getuserhomedir(buffer2);
294 if (!home)
295 return NULL;
296 strcat(buffer, home);
300 i = strlen(buffer);
302 while (*path!=0) {
303 char *tmp;
305 if (*path=='$') {
306 int j = 0;
307 path++;
308 /* expand $(HOME) or $HOME style environment variables */
309 if (*path=='(') {
310 path++;
311 while (*path!=0 && *path!=')') {
312 buffer2[j++] = *(path++);
313 buffer2[j] = 0;
315 if (*path==')')
316 path++;
317 tmp = getenv(buffer2);
318 if (!tmp) {
319 buffer[i] = 0;
320 strcat(buffer, "$(");
321 strcat(buffer, buffer2);
322 strcat(buffer, ")");
323 i += strlen(buffer2)+3;
324 } else {
325 strcat(buffer, tmp);
326 i += strlen(tmp);
328 } else {
329 while (*path!=0 && *path!='/') {
330 buffer2[j++] = *(path++);
331 buffer2[j] = 0;
333 tmp = getenv(buffer2);
334 if (!tmp) {
335 strcat(buffer, "$");
336 strcat(buffer, buffer2);
337 i += strlen(buffer2)+1;
338 } else {
339 strcat(buffer, tmp);
340 i += strlen(tmp);
343 } else {
344 buffer[i++] = *path;
345 path++;
349 return wstrdup(buffer);
354 char*
355 wfindfileinarray(proplist_t paths, char *file)
357 int i;
358 char *path;
359 int len, flen;
360 char *fullpath;
362 if (!file)
363 return NULL;
365 if (*file=='/' || *file=='~' || !paths || !PLIsArray(paths)
366 || PLGetNumberOfElements(paths)==0) {
367 if (access(file, R_OK)<0) {
368 fullpath = wexpandpath(file);
369 if (!fullpath)
370 return NULL;
372 if (access(fullpath, R_OK)<0) {
373 free(fullpath);
374 return NULL;
375 } else {
376 return fullpath;
378 } else {
379 return wstrdup(file);
383 flen = strlen(file);
384 for (i=0; i < PLGetNumberOfElements(paths); i++) {
385 proplist_t tmp;
386 char *dir;
388 tmp = PLGetArrayElement(paths, i);
389 if (!PLIsString(tmp) || !(dir = PLGetString(tmp)))
390 continue;
392 len = strlen(dir);
393 path = wmalloc(len+flen+2);
394 path = memcpy(path, dir, len);
395 path[len]=0;
396 strcat(path, "/");
397 strcat(path, file);
398 /* expand tilde */
399 fullpath = wexpandpath(path);
400 free(path);
401 if (fullpath) {
402 /* check if file is readable */
403 if (access(fullpath, R_OK)==0) {
404 return fullpath;
406 free(fullpath);
409 return NULL;
415 void
416 copyFile(char *dir, char *file)
418 char buffer[4000];
420 sprintf(buffer, "/bin/cp %s %s", file, dir);
421 if (system(buffer)!=0) {
422 printf("%s: could not copy file %s\n", ProgName, file);
427 void
428 findCopyFile(char *dir, char *file)
430 char *fullPath;
432 fullPath = wfindfileinarray(PixmapPath, file);
433 if (!fullPath) {
434 char buffer[4000];
436 sprintf(buffer, "coould not find file %s", file);
437 abortar(buffer);
439 copyFile(dir, fullPath);
440 free(fullPath);
444 char*
445 makeThemePack(proplist_t style, char *themeName)
447 proplist_t keys;
448 proplist_t key;
449 proplist_t value;
450 int i;
451 char *themeDir;
453 themeDir = wmalloc(strlen(themeName)+50);
454 sprintf(themeDir, "%s.themed", themeName);
455 ThemePath = themeDir;
457 char *tmp;
459 tmp = wmalloc(strlen(themeDir)+20);
460 sprintf(tmp, "/bin/mkdir %s", themeDir);
461 if (system(tmp)!=0) {
462 printf("%s: could not create directory %s\n", ProgName, themeDir);
463 exit(1);
465 free(tmp);
467 keys = PLGetAllDictionaryKeys(style);
469 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
470 key = PLGetArrayElement(keys, i);
472 value = PLGetDictionaryEntry(style, key);
473 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
474 proplist_t type;
475 char *t;
477 type = PLGetArrayElement(value, 0);
478 t = PLGetString(type);
479 if (t && (strcasecmp(t, "tpixmap")==0
480 || strcasecmp(t, "spixmap")==0
481 || strcasecmp(t, "cpixmap")==0
482 || strcasecmp(t, "mpixmap")==0
483 || strcasecmp(t, "tdgradient")==0
484 || strcasecmp(t, "tvgradient")==0
485 || strcasecmp(t, "thgradient")==0)) {
486 proplist_t file;
487 char *p;
488 char *newPath;
490 file = PLGetArrayElement(value, 1);
492 p = strrchr(PLGetString(file), '/');
493 if (p) {
494 copyFile(themeDir, PLGetString(file));
496 newPath = wstrdup(p+1);
497 PLRemoveArrayElement(value, 1);
498 PLInsertArrayElement(value, PLMakeString(newPath), 1);
499 free(newPath);
500 } else {
501 findCopyFile(themeDir, PLGetString(file));
507 return themeDir;
511 int
512 main(int argc, char **argv)
514 proplist_t prop, style, key, val;
515 char *path;
516 int i, theme_too=0;
517 int make_pack = 0;
518 char *style_file = NULL;
521 ProgName = argv[0];
523 if (argc>1) {
524 for (i=1; i<argc; i++) {
525 if (strcmp(argv[i], "-p")==0
526 || strcmp(argv[i], "--pack")==0) {
527 make_pack = 1;
528 theme_too = 1;
529 } else if (strcmp(argv[i], "-t")==0
530 || strcmp(argv[i], "--theme-options")==0) {
531 theme_too++;
532 } else if (strcmp(argv[i], "--help")==0) {
533 print_help();
534 exit(0);
535 } else if (strcmp(argv[i], "--version")==0) {
536 puts(PROG_VERSION);
537 exit(0);
538 } else {
539 if (style_file!=NULL) {
540 printf("%s: invalid argument '%s'\n", argv[0],
541 style_file[0]=='-' ? style_file : argv[i]);
542 printf("Try '%s --help' for more information\n", argv[0]);
543 exit(1);
545 style_file = argv[i];
550 if (make_pack && !style_file) {
551 printf("%s: you must supply a name for the theme pack\n", ProgName);
552 exit(1);
555 PLSetStringCmpHook(StringCompareHook);
557 path = defaultsPathForDomain("WindowMaker");
559 prop = PLGetProplistWithPath(path);
560 if (!prop) {
561 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
562 ProgName, path);
563 exit(1);
565 free(path);
567 /* get global value */
568 path = globalDefaultsPathForDomain("WindowMaker");
569 val = PLGetProplistWithPath(path);
570 if (val) {
571 PLMergeDictionaries(val, prop);
572 PLRelease(prop);
573 prop = val;
576 style = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
579 for (i=0; options[i]!=NULL; i++) {
580 key = PLMakeString(options[i]);
582 val = PLGetDictionaryEntry(prop, key);
583 if (val)
584 PLInsertDictionaryEntry(style, key, val);
587 val = PLGetDictionaryEntry(prop, PLMakeString("PixmapPath"));
588 if (val)
589 PixmapPath = val;
591 if (theme_too) {
592 for (i=0; theme_options[i]!=NULL; i++) {
593 key = PLMakeString(theme_options[i]);
595 val = PLGetDictionaryEntry(prop, key);
596 if (val)
597 PLInsertDictionaryEntry(style, key, val);
601 if (make_pack) {
602 char *path;
604 makeThemePack(style, style_file);
606 path = wmalloc(strlen(ThemePath)+32);
607 strcpy(path, ThemePath);
608 strcat(path, "/style");
609 PLSetFilename(style, PLMakeString(path));
610 PLSave(style, NO);
611 } else {
612 if (style_file) {
613 val = PLMakeString(style_file);
614 PLSetFilename(style, val);
615 PLSave(style, NO);
616 } else {
617 puts(PLGetDescriptionIndent(style, 0));
620 exit(0);