- Fixed crashing bug in menu.c
[wmaker-crm.git] / util / getstyle.c
blobc5a2ddf652f724d1a8143124a19b739794e4771b
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997-2003 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.6"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <limits.h>
35 #include <assert.h>
36 #include <WINGs/WUtil.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 "LargeDisplayFont",
54 "WindowTitleExtendSpace",
55 "MenuTitleExtendSpace",
56 "MenuTextExtendSpace",
57 "HighlightColor",
58 "HighlightTextColor",
59 "ClipTitleColor",
60 "CClipTitleColor",
61 "FTitleColor",
62 "PTitleColor",
63 "UTitleColor",
64 "FTitleBack",
65 "PTitleBack",
66 "UTitleBack",
67 "ResizebarBack",
68 "MenuTitleColor",
69 "MenuTextColor",
70 "MenuDisabledColor",
71 "MenuTitleBack",
72 "MenuTextBack",
73 "IconBack",
74 "IconTitleColor",
75 "IconTitleBack",
76 "MenuStyle",
77 "WindowTitleExtendSpace",
78 "MenuTitleExtendSpace",
79 "MenuTextExtendSpace",
80 NULL
84 /* table of theme related options */
85 static char *theme_options[] = {
86 "WorkspaceBack",
87 "NormalCursor",
88 "ArrowCursor",
89 "MoveCursor",
90 "ResizeCursor",
91 "TopLeftResizeCursor",
92 "TopRightResizeCursor",
93 "BottomLeftResizeCursor",
94 "BottomRightResizeCursor",
95 "VerticalResizeCursor",
96 "HorizontalResizeCursor",
97 "WaitCursor",
98 "QuestionCursor",
99 "TextCursor",
100 "SelectCursor",
101 NULL
107 char *ProgName;
109 WMPropList *PixmapPath = NULL;
111 char *ThemePath = NULL;
114 void
115 print_help()
117 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
118 puts("Retrieves style/theme configuration and output to FILE or to stdout");
119 puts("");
120 puts(" -t, --theme-options output theme related options when producing a style file");
121 puts(" -p, --pack produce output as a theme pack");
122 puts(" --help display this help and exit");
123 puts(" --version output version information and exit");
127 char*
128 globalDefaultsPathForDomain(char *domain)
130 static char path[1024];
132 sprintf(path, "%s/WindowMaker/%s", SYSCONFDIR, domain);
134 return path;
138 char*
139 defaultsPathForDomain(char *domain)
141 static char path[1024];
142 char *gspath;
144 gspath = getenv("GNUSTEP_USER_ROOT");
145 if (gspath) {
146 strcpy(path, gspath);
147 strcat(path, "/");
148 } else {
149 char *home;
151 home = getenv("HOME");
152 if (!home) {
153 printf("%s:could not get HOME environment variable!\n", ProgName);
154 exit(0);
156 strcpy(path, home);
157 strcat(path, "/GNUstep/");
159 strcat(path, DEFAULTS_DIR);
160 strcat(path, "/");
161 strcat(path, domain);
163 return path;
167 void
168 abortar(char *reason)
170 char buffer[4000];
172 printf("%s: %s\n", ProgName, reason);
174 if (ThemePath) {
175 printf("Removing unfinished theme pack\n");
176 sprintf(buffer, "/bin/rm -fr \"%s\"", ThemePath);
178 if (system(buffer)!=0) {
179 printf("%s: could not execute command %s\n", ProgName, buffer);
182 exit(1);
188 char*
189 wgethomedir()
191 char *home = getenv("HOME");
192 struct passwd *user;
194 if (home)
195 return home;
197 user = getpwuid(getuid());
198 if (!user) {
199 char buffer[80];
201 sprintf(buffer, "could not get password entry for UID %i", getuid());
202 perror(buffer);
203 return "/";
205 if (!user->pw_dir) {
206 return "/";
207 } else {
208 return user->pw_dir;
213 void*
214 mymalloc(int size)
216 void *tmp;
218 tmp = malloc(size);
219 if (!tmp) {
220 abortar("out of memory");
223 return tmp;
227 char*
228 mystrdup(char *str)
230 char *tmp;
232 tmp = mymalloc(strlen(str)+1);
234 strcpy(tmp, str);
236 return tmp;
240 static char*
241 getuserhomedir(char *username)
243 struct passwd *user;
245 user = getpwnam(username);
246 if (!user) {
247 char buffer[100];
249 sprintf(buffer,"could not get password entry for user %s", username);
250 perror(buffer);
251 return NULL;
253 if (!user->pw_dir) {
254 return "/";
255 } else {
256 return user->pw_dir;
263 char*
264 wexpandpath(char *path)
266 char buffer2[PATH_MAX+2];
267 char buffer[PATH_MAX+2];
268 int i;
270 memset(buffer, 0, PATH_MAX+2);
272 if (*path=='~') {
273 char *home;
275 path++;
276 if (*path=='/' || *path==0) {
277 home = wgethomedir();
278 strcat(buffer, home);
279 } else {
280 int j;
281 j = 0;
282 while (*path!=0 && *path!='/') {
283 buffer2[j++] = *path;
284 buffer2[j] = 0;
285 path++;
287 home = getuserhomedir(buffer2);
288 if (!home)
289 return NULL;
290 strcat(buffer, home);
294 i = strlen(buffer);
296 while (*path!=0) {
297 char *tmp;
299 if (*path=='$') {
300 int j = 0;
301 path++;
302 /* expand $(HOME) or $HOME style environment variables */
303 if (*path=='(') {
304 path++;
305 while (*path!=0 && *path!=')') {
306 buffer2[j++] = *(path++);
307 buffer2[j] = 0;
309 if (*path==')')
310 path++;
311 tmp = getenv(buffer2);
312 if (!tmp) {
313 buffer[i] = 0;
314 strcat(buffer, "$(");
315 strcat(buffer, buffer2);
316 strcat(buffer, ")");
317 i += strlen(buffer2)+3;
318 } else {
319 strcat(buffer, tmp);
320 i += strlen(tmp);
322 } else {
323 while (*path!=0 && *path!='/') {
324 buffer2[j++] = *(path++);
325 buffer2[j] = 0;
327 tmp = getenv(buffer2);
328 if (!tmp) {
329 strcat(buffer, "$");
330 strcat(buffer, buffer2);
331 i += strlen(buffer2)+1;
332 } else {
333 strcat(buffer, tmp);
334 i += strlen(tmp);
337 } else {
338 buffer[i++] = *path;
339 path++;
343 return mystrdup(buffer);
348 char*
349 wfindfileinarray(WMPropList *paths, char *file)
351 int i;
352 char *path;
353 int len, flen;
354 char *fullpath;
356 if (!file)
357 return NULL;
359 if (*file=='/' || *file=='~' || !paths || !WMIsPLArray(paths)
360 || WMGetPropListItemCount(paths)==0) {
361 if (access(file, R_OK)<0) {
362 fullpath = wexpandpath(file);
363 if (!fullpath)
364 return NULL;
366 if (access(fullpath, R_OK)<0) {
367 free(fullpath);
368 return NULL;
369 } else {
370 return fullpath;
372 } else {
373 return mystrdup(file);
377 flen = strlen(file);
378 for (i=0; i < WMGetPropListItemCount(paths); i++) {
379 WMPropList *tmp;
380 char *dir;
382 tmp = WMGetFromPLArray(paths, i);
383 if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp)))
384 continue;
386 len = strlen(dir);
387 path = mymalloc(len+flen+2);
388 path = memcpy(path, dir, len);
389 path[len]=0;
390 strcat(path, "/");
391 strcat(path, file);
392 /* expand tilde */
393 fullpath = wexpandpath(path);
394 free(path);
395 if (fullpath) {
396 /* check if file is readable */
397 if (access(fullpath, R_OK)==0) {
398 return fullpath;
400 free(fullpath);
403 return NULL;
409 void
410 copyFile(char *dir, char *file)
412 char buffer[4000];
414 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
415 if (system(buffer)!=0) {
416 printf("%s: could not copy file %s\n", ProgName, file);
421 void
422 findCopyFile(char *dir, char *file)
424 char *fullPath;
426 fullPath = wfindfileinarray(PixmapPath, file);
427 if (!fullPath) {
428 char buffer[4000];
430 sprintf(buffer, "coould not find file %s", file);
431 abortar(buffer);
433 copyFile(dir, fullPath);
434 free(fullPath);
438 char*
439 makeThemePack(WMPropList *style, char *themeName)
441 WMPropList *keys;
442 WMPropList *key;
443 WMPropList *value;
444 int i;
445 char *themeDir;
447 themeDir = mymalloc(strlen(themeName)+50);
448 sprintf(themeDir, "%s.themed", themeName);
449 ThemePath = themeDir;
451 char *tmp;
453 tmp = mymalloc(strlen(themeDir)+20);
454 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
455 if (system(tmp)!=0) {
456 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName, themeDir);
457 exit(1);
459 free(tmp);
461 keys = WMGetPLDictionaryKeys(style);
463 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
464 key = WMGetFromPLArray(keys, i);
466 value = WMGetFromPLDictionary(style, key);
467 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
468 WMPropList *type;
469 char *t;
471 type = WMGetFromPLArray(value, 0);
472 t = WMGetFromPLString(type);
473 if (t == NULL)
474 continue;
476 if (strcasecmp(t, "tpixmap")==0
477 || strcasecmp(t, "spixmap")==0
478 || strcasecmp(t, "cpixmap")==0
479 || strcasecmp(t, "mpixmap")==0
480 || strcasecmp(t, "tdgradient")==0
481 || strcasecmp(t, "tvgradient")==0
482 || strcasecmp(t, "thgradient")==0) {
483 WMPropList *file;
484 char *p;
485 char *newPath;
487 file = WMGetFromPLArray(value, 1);
489 p = strrchr(WMGetFromPLString(file), '/');
490 if (p) {
491 copyFile(themeDir, WMGetFromPLString(file));
493 newPath = mystrdup(p+1);
494 WMDeleteFromPLArray(value, 1);
495 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
496 free(newPath);
497 } else {
498 findCopyFile(themeDir, WMGetFromPLString(file));
500 } else if (strcasecmp(t, "bitmap")==0) {
501 WMPropList *file;
502 char *p;
503 char *newPath;
505 file = WMGetFromPLArray(value, 1);
507 p = strrchr(WMGetFromPLString(file), '/');
508 if (p) {
509 copyFile(themeDir, WMGetFromPLString(file));
511 newPath = mystrdup(p+1);
512 WMDeleteFromPLArray(value, 1);
513 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
514 free(newPath);
515 } else {
516 findCopyFile(themeDir, WMGetFromPLString(file));
520 file = WMGetFromPLArray(value, 2);
522 p = strrchr(WMGetFromPLString(file), '/');
523 if (p) {
524 copyFile(themeDir, WMGetFromPLString(file));
526 newPath = mystrdup(p+1);
527 WMDeleteFromPLArray(value, 2);
528 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
529 free(newPath);
530 } else {
531 findCopyFile(themeDir, WMGetFromPLString(file));
537 return themeDir;
541 int
542 main(int argc, char **argv)
544 WMPropList *prop, *style, *key, *val;
545 char *path;
546 int i, theme_too=0;
547 int make_pack = 0;
548 char *style_file = NULL;
551 ProgName = argv[0];
553 if (argc>1) {
554 for (i=1; i<argc; i++) {
555 if (strcmp(argv[i], "-p")==0
556 || strcmp(argv[i], "--pack")==0) {
557 make_pack = 1;
558 theme_too = 1;
559 } else if (strcmp(argv[i], "-t")==0
560 || strcmp(argv[i], "--theme-options")==0) {
561 theme_too++;
562 } else if (strcmp(argv[i], "--help")==0) {
563 print_help();
564 exit(0);
565 } else if (strcmp(argv[i], "--version")==0) {
566 puts(PROG_VERSION);
567 exit(0);
568 } else {
569 if (style_file!=NULL) {
570 printf("%s: invalid argument '%s'\n", argv[0],
571 style_file[0]=='-' ? style_file : argv[i]);
572 printf("Try '%s --help' for more information\n", argv[0]);
573 exit(1);
575 style_file = argv[i];
580 if (make_pack && !style_file) {
581 printf("%s: you must supply a name for the theme pack\n", ProgName);
582 exit(1);
585 WMPLSetCaseSensitive(False);
587 path = defaultsPathForDomain("WindowMaker");
589 prop = WMReadPropListFromFile(path);
590 if (!prop) {
591 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
592 ProgName, path);
593 exit(1);
596 /* get global value */
597 path = globalDefaultsPathForDomain("WindowMaker");
598 val = WMReadPropListFromFile(path);
599 if (val) {
600 WMMergePLDictionaries(val, prop, True);
601 WMReleasePropList(prop);
602 prop = val;
605 style = WMCreatePLDictionary(NULL, NULL, NULL);
608 for (i=0; options[i]!=NULL; i++) {
609 key = WMCreatePLString(options[i]);
611 val = WMGetFromPLDictionary(prop, key);
612 if (val)
613 WMPutInPLDictionary(style, key, val);
616 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
617 if (val)
618 PixmapPath = val;
620 if (theme_too) {
621 for (i=0; theme_options[i]!=NULL; i++) {
622 key = WMCreatePLString(theme_options[i]);
624 val = WMGetFromPLDictionary(prop, key);
625 if (val)
626 WMPutInPLDictionary(style, key, val);
630 if (make_pack) {
631 char *path;
633 makeThemePack(style, style_file);
635 path = mymalloc(strlen(ThemePath)+32);
636 strcpy(path, ThemePath);
637 strcat(path, "/style");
638 WMWritePropListToFile(style, path, False);
639 wfree(path);
640 } else {
641 if (style_file) {
642 WMWritePropListToFile(style, style_file, False);
643 } else {
644 puts(WMGetPropListDescription(style, True));
647 exit(0);