Latest fixes for released 0.51.0
[wmaker-crm.git] / util / getstyle.c
blobedab127dd5334424b9c5c35206c572bdb16270c4
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>
37 #ifndef PATH_MAX
38 #define PATH_MAX 1024
39 #endif
41 #include "../src/wconfig.h"
43 /* table of style related options */
44 static char *options[] = {
45 "TitleJustify",
46 "ClipTitleFont",
47 "WindowTitleFont",
48 "MenuTitleFont",
49 "MenuTextFont",
50 "IconTitleFont",
51 "DisplayFont",
52 "HighlightColor",
53 "HighlightTextColor",
54 "ClipTitleColor",
55 "CClipTitleColor",
56 "FTitleColor",
57 "PTitleColor",
58 "UTitleColor",
59 "FTitleBack",
60 "PTitleBack",
61 "UTitleBack",
62 "MenuTitleColor",
63 "MenuTextColor",
64 "MenuDisabledColor",
65 "MenuTitleBack",
66 "MenuTextBack",
67 "IconBack",
68 "IconTitleColor",
69 "IconTitleBack",
70 #ifdef TITLE_TEXT_SHADOW
71 "Shadow",
72 "FShadowColor",
73 "PShadowColor",
74 "UShadowColor",
75 #endif
76 NULL
80 /* table of theme related options */
81 static char *theme_options[] = {
82 "WorkspaceBack",
83 NULL
89 char *ProgName;
91 proplist_t PixmapPath = NULL;
93 char *ThemePath = NULL;
96 void
97 print_help()
99 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
100 puts("Retrieves style/theme configuration and output to FILE or to stdout");
101 puts("");
102 puts(" -t, --theme-options output theme related options when producing a style file");
103 puts(" -p, --pack produce output as a theme pack");
104 puts(" --help display this help and exit");
105 puts(" --version output version information and exit");
109 char*
110 globalDefaultsPathForDomain(char *domain)
112 char path[1024];
113 char *tmp;
115 sprintf(path, "%s/%s/%s", PKGDATADIR, DEFAULTS_DIR, domain);
117 tmp = malloc(strlen(path)+2);
118 strcpy(tmp, path);
120 return tmp;
124 char*
125 defaultsPathForDomain(char *domain)
127 char path[1024];
128 char *gspath, *tmp;
130 gspath = getenv("GNUSTEP_USER_ROOT");
131 if (gspath) {
132 strcpy(path, gspath);
133 strcat(path, "/");
134 } else {
135 char *home;
137 home = getenv("HOME");
138 if (!home) {
139 printf("%s:could not get HOME environment variable!\n", ProgName);
140 exit(0);
142 strcpy(path, home);
143 strcat(path, "/GNUstep/");
145 strcat(path, DEFAULTS_DIR);
146 strcat(path, "/");
147 strcat(path, domain);
149 tmp = malloc(strlen(path)+2);
150 strcpy(tmp, path);
152 return tmp;
156 BOOL
157 StringCompareHook(proplist_t pl1, proplist_t pl2)
159 char *str1, *str2;
161 str1 = PLGetString(pl1);
162 str2 = PLGetString(pl2);
164 if (strcasecmp(str1, str2)==0)
165 return YES;
166 else
167 return NO;
171 void
172 abortar(char *reason)
174 char buffer[4000];
176 printf("%s: %s\n", ProgName, reason);
178 if (ThemePath) {
179 printf("Removing unfinished theme pack\n");
180 sprintf(buffer, "/bin/rm -fr %s", ThemePath);
182 if (system(buffer)!=0) {
183 printf("%s: could not execute command %s\n", ProgName, buffer);
186 exit(1);
192 char*
193 wgethomedir()
195 char *home = getenv("HOME");
196 struct passwd *user;
198 if (home)
199 return home;
201 user = getpwuid(getuid());
202 if (!user) {
203 char buffer[80];
205 sprintf(buffer, "could not get password entry for UID %i", getuid());
206 perror(buffer);
207 return "/";
209 if (!user->pw_dir) {
210 return "/";
211 } else {
212 return user->pw_dir;
217 void*
218 wmalloc(int size)
220 void *tmp;
222 tmp = malloc(size);
223 if (!tmp) {
224 abortar("out of memory");
227 return tmp;
231 char*
232 wstrdup(char *str)
234 char *tmp;
236 tmp = wmalloc(strlen(str)+1);
238 strcpy(tmp, str);
240 return tmp;
244 static char*
245 getuserhomedir(char *username)
247 struct passwd *user;
249 user = getpwnam(username);
250 if (!user) {
251 char buffer[100];
253 sprintf(buffer,"could not get password entry for user %s", username);
254 perror(buffer);
255 return NULL;
257 if (!user->pw_dir) {
258 return "/";
259 } else {
260 return user->pw_dir;
267 char*
268 wexpandpath(char *path)
270 char buffer2[PATH_MAX+2];
271 char buffer[PATH_MAX+2];
272 int i;
274 memset(buffer, 0, PATH_MAX+2);
276 if (*path=='~') {
277 char *home;
279 path++;
280 if (*path=='/' || *path==0) {
281 home = wgethomedir();
282 strcat(buffer, home);
283 } else {
284 int j;
285 j = 0;
286 while (*path!=0 && *path!='/') {
287 buffer2[j++] = *path;
288 buffer2[j] = 0;
289 path++;
291 home = getuserhomedir(buffer2);
292 if (!home)
293 return NULL;
294 strcat(buffer, home);
298 i = strlen(buffer);
300 while (*path!=0) {
301 char *tmp;
303 if (*path=='$') {
304 int j = 0;
305 path++;
306 /* expand $(HOME) or $HOME style environment variables */
307 if (*path=='(') {
308 path++;
309 while (*path!=0 && *path!=')') {
310 buffer2[j++] = *(path++);
311 buffer2[j] = 0;
313 if (*path==')')
314 path++;
315 tmp = getenv(buffer2);
316 if (!tmp) {
317 buffer[i] = 0;
318 strcat(buffer, "$(");
319 strcat(buffer, buffer2);
320 strcat(buffer, ")");
321 i += strlen(buffer2)+3;
322 } else {
323 strcat(buffer, tmp);
324 i += strlen(tmp);
326 } else {
327 while (*path!=0 && *path!='/') {
328 buffer2[j++] = *(path++);
329 buffer2[j] = 0;
331 tmp = getenv(buffer2);
332 if (!tmp) {
333 strcat(buffer, "$");
334 strcat(buffer, buffer2);
335 i += strlen(buffer2)+1;
336 } else {
337 strcat(buffer, tmp);
338 i += strlen(tmp);
341 } else {
342 buffer[i++] = *path;
343 path++;
347 return wstrdup(buffer);
352 char*
353 wfindfileinarray(proplist_t paths, char *file)
355 int i;
356 char *path;
357 int len, flen;
358 char *fullpath;
360 if (!file)
361 return NULL;
363 if (*file=='/' || *file=='~' || !paths || !PLIsArray(paths)
364 || PLGetNumberOfElements(paths)==0) {
365 if (access(file, R_OK)<0) {
366 fullpath = wexpandpath(file);
367 if (!fullpath)
368 return NULL;
370 if (access(fullpath, R_OK)<0) {
371 free(fullpath);
372 return NULL;
373 } else {
374 return fullpath;
376 } else {
377 return wstrdup(file);
381 flen = strlen(file);
382 for (i=0; i < PLGetNumberOfElements(paths); i++) {
383 proplist_t tmp;
384 char *dir;
386 tmp = PLGetArrayElement(paths, i);
387 if (!PLIsString(tmp) || !(dir = PLGetString(tmp)))
388 continue;
390 len = strlen(dir);
391 path = wmalloc(len+flen+2);
392 path = memcpy(path, dir, len);
393 path[len]=0;
394 strcat(path, "/");
395 strcat(path, file);
396 /* expand tilde */
397 fullpath = wexpandpath(path);
398 free(path);
399 if (fullpath) {
400 /* check if file is readable */
401 if (access(fullpath, R_OK)==0) {
402 return fullpath;
404 free(fullpath);
407 return NULL;
413 void
414 copyFile(char *dir, char *file)
416 char buffer[4000];
418 sprintf(buffer, "/bin/cp %s %s", file, dir);
419 if (system(buffer)!=0) {
420 printf("%s: could not copy file %s\n", ProgName, file);
425 void
426 findCopyFile(char *dir, char *file)
428 char *fullPath;
430 fullPath = wfindfileinarray(PixmapPath, file);
431 if (!fullPath) {
432 char buffer[4000];
434 sprintf(buffer, "coould not find file %s", file);
435 abortar(buffer);
437 copyFile(dir, fullPath);
438 free(fullPath);
442 char*
443 makeThemePack(proplist_t style, char *themeName)
445 proplist_t keys;
446 proplist_t key;
447 proplist_t value;
448 int i;
449 char *themeDir;
451 themeDir = wmalloc(strlen(themeName)+50);
452 sprintf(themeDir, "%s.themed", themeName);
453 ThemePath = themeDir;
455 char *tmp;
457 tmp = wmalloc(strlen(themeDir)+20);
458 sprintf(tmp, "/bin/mkdir %s", themeDir);
459 if (system(tmp)!=0) {
460 printf("%s: could not create directory %s\n", ProgName, themeDir);
461 exit(1);
463 free(tmp);
465 keys = PLGetAllDictionaryKeys(style);
467 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
468 key = PLGetArrayElement(keys, i);
470 value = PLGetDictionaryEntry(style, key);
471 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
472 proplist_t type;
473 char *t;
475 type = PLGetArrayElement(value, 0);
476 t = PLGetString(type);
477 if (t && (strcasecmp(t, "tpixmap")==0
478 || strcasecmp(t, "spixmap")==0
479 || strcasecmp(t, "cpixmap")==0
480 || strcasecmp(t, "mpixmap")==0
481 || strcasecmp(t, "tdgradient")==0
482 || strcasecmp(t, "tvgradient")==0
483 || strcasecmp(t, "thgradient")==0)) {
484 proplist_t file;
485 char *p;
486 char *newPath;
488 file = PLGetArrayElement(value, 1);
490 p = strrchr(PLGetString(file), '/');
491 if (p) {
492 copyFile(themeDir, PLGetString(file));
494 newPath = wstrdup(p+1);
495 PLRemoveArrayElement(value, 1);
496 PLInsertArrayElement(value, PLMakeString(newPath), 1);
497 free(newPath);
498 } else {
499 findCopyFile(themeDir, PLGetString(file));
505 return themeDir;
509 int
510 main(int argc, char **argv)
512 proplist_t prop, style, key, val;
513 char *path;
514 int i, theme_too=0;
515 int make_pack = 0;
516 char *style_file = NULL;
519 ProgName = argv[0];
521 if (argc>1) {
522 for (i=1; i<argc; i++) {
523 if (strcmp(argv[i], "-p")==0
524 || strcmp(argv[i], "--pack")==0) {
525 make_pack = 1;
526 theme_too = 1;
527 } else if (strcmp(argv[i], "-t")==0
528 || strcmp(argv[i], "--theme-options")==0) {
529 theme_too++;
530 } else if (strcmp(argv[i], "--help")==0) {
531 print_help();
532 exit(0);
533 } else if (strcmp(argv[i], "--version")==0) {
534 puts(PROG_VERSION);
535 exit(0);
536 } else {
537 style_file = argv[i];
542 if (make_pack && !style_file) {
543 printf("%s: you must supply a name for the theme pack\n", ProgName);
544 exit(1);
547 PLSetStringCmpHook(StringCompareHook);
549 path = defaultsPathForDomain("WindowMaker");
551 prop = PLGetProplistWithPath(path);
552 if (!prop) {
553 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
554 ProgName, path);
555 exit(1);
557 free(path);
559 /* get global value */
560 path = globalDefaultsPathForDomain("WindowMaker");
561 val = PLGetProplistWithPath(path);
562 if (val) {
563 PLMergeDictionaries(val, prop);
564 PLRelease(prop);
565 prop = val;
568 style = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
571 for (i=0; options[i]!=NULL; i++) {
572 key = PLMakeString(options[i]);
574 val = PLGetDictionaryEntry(prop, key);
575 if (val)
576 PLInsertDictionaryEntry(style, key, val);
579 val = PLGetDictionaryEntry(prop, PLMakeString("PixmapPath"));
580 if (val)
581 PixmapPath = val;
583 if (theme_too) {
584 for (i=0; theme_options[i]!=NULL; i++) {
585 key = PLMakeString(theme_options[i]);
587 val = PLGetDictionaryEntry(prop, key);
588 if (val)
589 PLInsertDictionaryEntry(style, key, val);
593 if (make_pack) {
594 char *path;
596 makeThemePack(style, style_file);
598 path = wmalloc(strlen(ThemePath)+32);
599 strcpy(path, ThemePath);
600 strcat(path, "/style");
601 PLSetFilename(style, PLMakeString(path));
602 PLSave(style, NO);
603 } else {
604 if (style_file) {
605 val = PLMakeString(style_file);
606 PLSetFilename(style, val);
607 PLSave(style, NO);
608 } else {
609 puts(PLGetDescriptionIndent(style, 0));
612 exit(0);