1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
5 * Copyright (c) 1997, 1998 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.4"
42 #include "../src/wconfig.h"
44 /* table of style related options */
45 static char *options
[] = {
78 /* table of theme related options */
79 static char *theme_options
[] = {
89 proplist_t PixmapPath
= NULL
;
91 char *ThemePath
= NULL
;
97 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
98 puts("Retrieves style/theme configuration and output to FILE or to stdout");
100 puts(" -t, --theme-options output theme related options when producing a style file");
101 puts(" -p, --pack produce output as a theme pack");
102 puts(" --help display this help and exit");
103 puts(" --version output version information and exit");
108 globalDefaultsPathForDomain(char *domain
)
113 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
115 tmp
= malloc(strlen(path
)+2);
124 defaultsPathForDomain(char *domain
)
129 gspath
= getenv("GNUSTEP_USER_ROOT");
131 strcpy(path
, gspath
);
136 home
= getenv("HOME");
138 printf("%s:could not get HOME environment variable!\n", ProgName
);
142 strcat(path
, "/GNUstep/");
144 strcat(path
, DEFAULTS_DIR
);
146 strcat(path
, domain
);
148 tmp
= malloc(strlen(path
)+2);
156 StringCompareHook(proplist_t pl1
, proplist_t pl2
)
160 str1
= PLGetString(pl1
);
161 str2
= PLGetString(pl2
);
163 if (strcasecmp(str1
, str2
)==0)
171 abortar(char *reason
)
175 printf("%s: %s\n", ProgName
, reason
);
178 printf("Removing unfinished theme pack\n");
179 sprintf(buffer
, "/bin/rm -fr \"%s\"", ThemePath
);
181 if (system(buffer
)!=0) {
182 printf("%s: could not execute command %s\n", ProgName
, buffer
);
194 char *home
= getenv("HOME");
200 user
= getpwuid(getuid());
204 sprintf(buffer
, "could not get password entry for UID %i", getuid());
223 abortar("out of memory");
235 tmp
= wmalloc(strlen(str
)+1);
244 getuserhomedir(char *username
)
248 user
= getpwnam(username
);
252 sprintf(buffer
,"could not get password entry for user %s", username
);
267 wexpandpath(char *path
)
269 char buffer2
[PATH_MAX
+2];
270 char buffer
[PATH_MAX
+2];
273 memset(buffer
, 0, PATH_MAX
+2);
279 if (*path
=='/' || *path
==0) {
280 home
= wgethomedir();
281 strcat(buffer
, home
);
285 while (*path
!=0 && *path
!='/') {
286 buffer2
[j
++] = *path
;
290 home
= getuserhomedir(buffer2
);
293 strcat(buffer
, home
);
305 /* expand $(HOME) or $HOME style environment variables */
308 while (*path
!=0 && *path
!=')') {
309 buffer2
[j
++] = *(path
++);
314 tmp
= getenv(buffer2
);
317 strcat(buffer
, "$(");
318 strcat(buffer
, buffer2
);
320 i
+= strlen(buffer2
)+3;
326 while (*path
!=0 && *path
!='/') {
327 buffer2
[j
++] = *(path
++);
330 tmp
= getenv(buffer2
);
333 strcat(buffer
, buffer2
);
334 i
+= strlen(buffer2
)+1;
346 return wstrdup(buffer
);
352 wfindfileinarray(proplist_t paths
, char *file
)
362 if (*file
=='/' || *file
=='~' || !paths
|| !PLIsArray(paths
)
363 || PLGetNumberOfElements(paths
)==0) {
364 if (access(file
, R_OK
)<0) {
365 fullpath
= wexpandpath(file
);
369 if (access(fullpath
, R_OK
)<0) {
376 return wstrdup(file
);
381 for (i
=0; i
< PLGetNumberOfElements(paths
); i
++) {
385 tmp
= PLGetArrayElement(paths
, i
);
386 if (!PLIsString(tmp
) || !(dir
= PLGetString(tmp
)))
390 path
= wmalloc(len
+flen
+2);
391 path
= memcpy(path
, dir
, len
);
396 fullpath
= wexpandpath(path
);
399 /* check if file is readable */
400 if (access(fullpath
, R_OK
)==0) {
413 copyFile(char *dir
, char *file
)
417 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
418 if (system(buffer
)!=0) {
419 printf("%s: could not copy file %s\n", ProgName
, file
);
425 findCopyFile(char *dir
, char *file
)
429 fullPath
= wfindfileinarray(PixmapPath
, file
);
433 sprintf(buffer
, "coould not find file %s", file
);
436 copyFile(dir
, fullPath
);
442 makeThemePack(proplist_t style
, char *themeName
)
450 themeDir
= wmalloc(strlen(themeName
)+50);
451 sprintf(themeDir
, "%s.themed", themeName
);
452 ThemePath
= themeDir
;
456 tmp
= wmalloc(strlen(themeDir
)+20);
457 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
458 if (system(tmp
)!=0) {
459 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
464 keys
= PLGetAllDictionaryKeys(style
);
466 for (i
= 0; i
< PLGetNumberOfElements(keys
); i
++) {
467 key
= PLGetArrayElement(keys
, i
);
469 value
= PLGetDictionaryEntry(style
, key
);
470 if (value
&& PLIsArray(value
) && PLGetNumberOfElements(value
) > 2) {
474 type
= PLGetArrayElement(value
, 0);
475 t
= PLGetString(type
);
476 if (t
&& (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)) {
487 file
= PLGetArrayElement(value
, 1);
489 p
= strrchr(PLGetString(file
), '/');
491 copyFile(themeDir
, PLGetString(file
));
493 newPath
= wstrdup(p
+1);
494 PLRemoveArrayElement(value
, 1);
495 PLInsertArrayElement(value
, PLMakeString(newPath
), 1);
498 findCopyFile(themeDir
, PLGetString(file
));
509 main(int argc
, char **argv
)
511 proplist_t prop
, style
, key
, val
;
515 char *style_file
= NULL
;
521 for (i
=1; i
<argc
; i
++) {
522 if (strcmp(argv
[i
], "-p")==0
523 || strcmp(argv
[i
], "--pack")==0) {
526 } else if (strcmp(argv
[i
], "-t")==0
527 || strcmp(argv
[i
], "--theme-options")==0) {
529 } else if (strcmp(argv
[i
], "--help")==0) {
532 } else if (strcmp(argv
[i
], "--version")==0) {
536 if (style_file
!=NULL
) {
537 printf("%s: invalid argument '%s'\n", argv
[0],
538 style_file
[0]=='-' ? style_file
: argv
[i
]);
539 printf("Try '%s --help' for more information\n", argv
[0]);
542 style_file
= argv
[i
];
547 if (make_pack
&& !style_file
) {
548 printf("%s: you must supply a name for the theme pack\n", ProgName
);
552 PLSetStringCmpHook(StringCompareHook
);
554 path
= defaultsPathForDomain("WindowMaker");
556 prop
= PLGetProplistWithPath(path
);
558 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
564 /* get global value */
565 path
= globalDefaultsPathForDomain("WindowMaker");
566 val
= PLGetProplistWithPath(path
);
568 PLMergeDictionaries(val
, prop
);
573 style
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
576 for (i
=0; options
[i
]!=NULL
; i
++) {
577 key
= PLMakeString(options
[i
]);
579 val
= PLGetDictionaryEntry(prop
, key
);
581 PLInsertDictionaryEntry(style
, key
, val
);
584 val
= PLGetDictionaryEntry(prop
, PLMakeString("PixmapPath"));
589 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
590 key
= PLMakeString(theme_options
[i
]);
592 val
= PLGetDictionaryEntry(prop
, key
);
594 PLInsertDictionaryEntry(style
, key
, val
);
601 makeThemePack(style
, style_file
);
603 path
= wmalloc(strlen(ThemePath
)+32);
604 strcpy(path
, ThemePath
);
605 strcat(path
, "/style");
606 PLSetFilename(style
, PLMakeString(path
));
610 val
= PLMakeString(style_file
);
611 PLSetFilename(style
, val
);
614 puts(PLGetDescriptionIndent(style
, 0));