1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
5 * Copyright (c) 1997~2000 Alfredo K. Kojima
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,
24 #define PROG_VERSION "getstyle (Window Maker) 0.6"
42 #include "../src/wconfig.h"
44 /* table of style related options */
45 static char *options
[] = {
54 "WindowTitleExtendSpace",
55 "MenuTitleExtendSpace",
56 "MenuTextExtendSpace",
77 "WindowTitleExtendSpace",
78 "MenuTitleExtendSpace",
79 "MenuTextExtendSpace",
85 /* table of theme related options */
86 static char *theme_options
[] = {
96 proplist_t PixmapPath
= NULL
;
98 char *ThemePath
= NULL
;
104 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
105 puts("Retrieves style/theme configuration and output to FILE or to stdout");
107 puts(" -t, --theme-options output theme related options when producing a style file");
108 puts(" -p, --pack produce output as a theme pack");
109 puts(" --help display this help and exit");
110 puts(" --version output version information and exit");
115 globalDefaultsPathForDomain(char *domain
)
117 static char path
[1024];
119 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
126 defaultsPathForDomain(char *domain
)
128 static char path
[1024];
131 gspath
= getenv("GNUSTEP_USER_ROOT");
133 strcpy(path
, gspath
);
138 home
= getenv("HOME");
140 printf("%s:could not get HOME environment variable!\n", ProgName
);
144 strcat(path
, "/GNUstep/");
146 strcat(path
, DEFAULTS_DIR
);
148 strcat(path
, domain
);
155 StringCompareHook(proplist_t pl1
, proplist_t pl2
)
159 str1
= PLGetString(pl1
);
160 str2
= PLGetString(pl2
);
162 if (strcasecmp(str1
, str2
)==0)
170 abortar(char *reason
)
174 printf("%s: %s\n", ProgName
, reason
);
177 printf("Removing unfinished theme pack\n");
178 sprintf(buffer
, "/bin/rm -fr \"%s\"", ThemePath
);
180 if (system(buffer
)!=0) {
181 printf("%s: could not execute command %s\n", ProgName
, buffer
);
193 char *home
= getenv("HOME");
199 user
= getpwuid(getuid());
203 sprintf(buffer
, "could not get password entry for UID %i", getuid());
222 abortar("out of memory");
234 tmp
= wmalloc(strlen(str
)+1);
243 getuserhomedir(char *username
)
247 user
= getpwnam(username
);
251 sprintf(buffer
,"could not get password entry for user %s", username
);
266 wexpandpath(char *path
)
268 char buffer2
[PATH_MAX
+2];
269 char buffer
[PATH_MAX
+2];
272 memset(buffer
, 0, PATH_MAX
+2);
278 if (*path
=='/' || *path
==0) {
279 home
= wgethomedir();
280 strcat(buffer
, home
);
284 while (*path
!=0 && *path
!='/') {
285 buffer2
[j
++] = *path
;
289 home
= getuserhomedir(buffer2
);
292 strcat(buffer
, home
);
304 /* expand $(HOME) or $HOME style environment variables */
307 while (*path
!=0 && *path
!=')') {
308 buffer2
[j
++] = *(path
++);
313 tmp
= getenv(buffer2
);
316 strcat(buffer
, "$(");
317 strcat(buffer
, buffer2
);
319 i
+= strlen(buffer2
)+3;
325 while (*path
!=0 && *path
!='/') {
326 buffer2
[j
++] = *(path
++);
329 tmp
= getenv(buffer2
);
332 strcat(buffer
, buffer2
);
333 i
+= strlen(buffer2
)+1;
345 return wstrdup(buffer
);
351 wfindfileinarray(proplist_t paths
, char *file
)
361 if (*file
=='/' || *file
=='~' || !paths
|| !PLIsArray(paths
)
362 || PLGetNumberOfElements(paths
)==0) {
363 if (access(file
, R_OK
)<0) {
364 fullpath
= wexpandpath(file
);
368 if (access(fullpath
, R_OK
)<0) {
375 return wstrdup(file
);
380 for (i
=0; i
< PLGetNumberOfElements(paths
); i
++) {
384 tmp
= PLGetArrayElement(paths
, i
);
385 if (!PLIsString(tmp
) || !(dir
= PLGetString(tmp
)))
389 path
= wmalloc(len
+flen
+2);
390 path
= memcpy(path
, dir
, len
);
395 fullpath
= wexpandpath(path
);
398 /* check if file is readable */
399 if (access(fullpath
, R_OK
)==0) {
412 copyFile(char *dir
, char *file
)
416 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
417 if (system(buffer
)!=0) {
418 printf("%s: could not copy file %s\n", ProgName
, file
);
424 findCopyFile(char *dir
, char *file
)
428 fullPath
= wfindfileinarray(PixmapPath
, file
);
432 sprintf(buffer
, "coould not find file %s", file
);
435 copyFile(dir
, fullPath
);
441 makeThemePack(proplist_t style
, char *themeName
)
449 themeDir
= wmalloc(strlen(themeName
)+50);
450 sprintf(themeDir
, "%s.themed", themeName
);
451 ThemePath
= themeDir
;
455 tmp
= wmalloc(strlen(themeDir
)+20);
456 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
457 if (system(tmp
)!=0) {
458 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
463 keys
= PLGetAllDictionaryKeys(style
);
465 for (i
= 0; i
< PLGetNumberOfElements(keys
); i
++) {
466 key
= PLGetArrayElement(keys
, i
);
468 value
= PLGetDictionaryEntry(style
, key
);
469 if (value
&& PLIsArray(value
) && PLGetNumberOfElements(value
) > 2) {
473 type
= PLGetArrayElement(value
, 0);
474 t
= PLGetString(type
);
478 if (strcasecmp(t
, "tpixmap")==0
479 || strcasecmp(t
, "spixmap")==0
480 || strcasecmp(t
, "cpixmap")==0
481 || strcasecmp(t
, "mpixmap")==0
482 || strcasecmp(t
, "tdgradient")==0
483 || strcasecmp(t
, "tvgradient")==0
484 || strcasecmp(t
, "thgradient")==0) {
489 file
= PLGetArrayElement(value
, 1);
491 p
= strrchr(PLGetString(file
), '/');
493 copyFile(themeDir
, PLGetString(file
));
495 newPath
= wstrdup(p
+1);
496 PLRemoveArrayElement(value
, 1);
497 PLInsertArrayElement(value
, PLMakeString(newPath
), 1);
500 findCopyFile(themeDir
, PLGetString(file
));
502 } else if (strcasecmp(t
, "bitmap")==0) {
507 file
= PLGetArrayElement(value
, 1);
509 p
= strrchr(PLGetString(file
), '/');
511 copyFile(themeDir
, PLGetString(file
));
513 newPath
= wstrdup(p
+1);
514 PLRemoveArrayElement(value
, 1);
515 PLInsertArrayElement(value
, PLMakeString(newPath
), 1);
518 findCopyFile(themeDir
, PLGetString(file
));
522 file
= PLGetArrayElement(value
, 2);
524 p
= strrchr(PLGetString(file
), '/');
526 copyFile(themeDir
, PLGetString(file
));
528 newPath
= wstrdup(p
+1);
529 PLRemoveArrayElement(value
, 2);
530 PLInsertArrayElement(value
, PLMakeString(newPath
), 2);
533 findCopyFile(themeDir
, PLGetString(file
));
544 main(int argc
, char **argv
)
546 proplist_t prop
, style
, key
, val
;
550 char *style_file
= NULL
;
556 for (i
=1; i
<argc
; i
++) {
557 if (strcmp(argv
[i
], "-p")==0
558 || strcmp(argv
[i
], "--pack")==0) {
561 } else if (strcmp(argv
[i
], "-t")==0
562 || strcmp(argv
[i
], "--theme-options")==0) {
564 } else if (strcmp(argv
[i
], "--help")==0) {
567 } else if (strcmp(argv
[i
], "--version")==0) {
571 if (style_file
!=NULL
) {
572 printf("%s: invalid argument '%s'\n", argv
[0],
573 style_file
[0]=='-' ? style_file
: argv
[i
]);
574 printf("Try '%s --help' for more information\n", argv
[0]);
577 style_file
= argv
[i
];
582 if (make_pack
&& !style_file
) {
583 printf("%s: you must supply a name for the theme pack\n", ProgName
);
587 PLSetStringCmpHook(StringCompareHook
);
589 path
= defaultsPathForDomain("WindowMaker");
591 prop
= PLGetProplistWithPath(path
);
593 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
598 /* get global value */
599 path
= globalDefaultsPathForDomain("WindowMaker");
600 val
= PLGetProplistWithPath(path
);
602 PLMergeDictionaries(val
, prop
);
607 style
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
610 for (i
=0; options
[i
]!=NULL
; i
++) {
611 key
= PLMakeString(options
[i
]);
613 val
= PLGetDictionaryEntry(prop
, key
);
615 PLInsertDictionaryEntry(style
, key
, val
);
618 val
= PLGetDictionaryEntry(prop
, PLMakeString("PixmapPath"));
623 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
624 key
= PLMakeString(theme_options
[i
]);
626 val
= PLGetDictionaryEntry(prop
, key
);
628 PLInsertDictionaryEntry(style
, key
, val
);
635 makeThemePack(style
, style_file
);
637 path
= wmalloc(strlen(ThemePath
)+32);
638 strcpy(path
, ThemePath
);
639 strcat(path
, "/style");
640 PLSetFilename(style
, PLMakeString(path
));
644 val
= PLMakeString(style_file
);
645 PLSetFilename(style
, val
);
648 puts(PLGetDescriptionIndent(style
, 0));