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.5"
42 #include "../src/wconfig.h"
44 /* table of style related options */
45 static char *options
[] = {
74 "WindowTitleExtendSpace",
75 "MenuTitleExtendSpace",
76 "MenuTextExtendSpace",
82 /* table of theme related options */
83 static char *theme_options
[] = {
93 proplist_t PixmapPath
= NULL
;
95 char *ThemePath
= NULL
;
101 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName
);
102 puts("Retrieves style/theme configuration and output to FILE or to stdout");
104 puts(" -t, --theme-options output theme related options when producing a style file");
105 puts(" -p, --pack produce output as a theme pack");
106 puts(" --help display this help and exit");
107 puts(" --version output version information and exit");
112 globalDefaultsPathForDomain(char *domain
)
117 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
119 tmp
= malloc(strlen(path
)+2);
128 defaultsPathForDomain(char *domain
)
133 gspath
= getenv("GNUSTEP_USER_ROOT");
135 strcpy(path
, gspath
);
140 home
= getenv("HOME");
142 printf("%s:could not get HOME environment variable!\n", ProgName
);
146 strcat(path
, "/GNUstep/");
148 strcat(path
, DEFAULTS_DIR
);
150 strcat(path
, domain
);
152 tmp
= malloc(strlen(path
)+2);
160 StringCompareHook(proplist_t pl1
, proplist_t pl2
)
164 str1
= PLGetString(pl1
);
165 str2
= PLGetString(pl2
);
167 if (strcasecmp(str1
, str2
)==0)
175 abortar(char *reason
)
179 printf("%s: %s\n", ProgName
, reason
);
182 printf("Removing unfinished theme pack\n");
183 sprintf(buffer
, "/bin/rm -fr \"%s\"", ThemePath
);
185 if (system(buffer
)!=0) {
186 printf("%s: could not execute command %s\n", ProgName
, buffer
);
198 char *home
= getenv("HOME");
204 user
= getpwuid(getuid());
208 sprintf(buffer
, "could not get password entry for UID %i", getuid());
227 abortar("out of memory");
239 tmp
= wmalloc(strlen(str
)+1);
248 getuserhomedir(char *username
)
252 user
= getpwnam(username
);
256 sprintf(buffer
,"could not get password entry for user %s", username
);
271 wexpandpath(char *path
)
273 char buffer2
[PATH_MAX
+2];
274 char buffer
[PATH_MAX
+2];
277 memset(buffer
, 0, PATH_MAX
+2);
283 if (*path
=='/' || *path
==0) {
284 home
= wgethomedir();
285 strcat(buffer
, home
);
289 while (*path
!=0 && *path
!='/') {
290 buffer2
[j
++] = *path
;
294 home
= getuserhomedir(buffer2
);
297 strcat(buffer
, home
);
309 /* expand $(HOME) or $HOME style environment variables */
312 while (*path
!=0 && *path
!=')') {
313 buffer2
[j
++] = *(path
++);
318 tmp
= getenv(buffer2
);
321 strcat(buffer
, "$(");
322 strcat(buffer
, buffer2
);
324 i
+= strlen(buffer2
)+3;
330 while (*path
!=0 && *path
!='/') {
331 buffer2
[j
++] = *(path
++);
334 tmp
= getenv(buffer2
);
337 strcat(buffer
, buffer2
);
338 i
+= strlen(buffer2
)+1;
350 return wstrdup(buffer
);
356 wfindfileinarray(proplist_t paths
, char *file
)
366 if (*file
=='/' || *file
=='~' || !paths
|| !PLIsArray(paths
)
367 || PLGetNumberOfElements(paths
)==0) {
368 if (access(file
, R_OK
)<0) {
369 fullpath
= wexpandpath(file
);
373 if (access(fullpath
, R_OK
)<0) {
380 return wstrdup(file
);
385 for (i
=0; i
< PLGetNumberOfElements(paths
); i
++) {
389 tmp
= PLGetArrayElement(paths
, i
);
390 if (!PLIsString(tmp
) || !(dir
= PLGetString(tmp
)))
394 path
= wmalloc(len
+flen
+2);
395 path
= memcpy(path
, dir
, len
);
400 fullpath
= wexpandpath(path
);
403 /* check if file is readable */
404 if (access(fullpath
, R_OK
)==0) {
417 copyFile(char *dir
, char *file
)
421 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
422 if (system(buffer
)!=0) {
423 printf("%s: could not copy file %s\n", ProgName
, file
);
429 findCopyFile(char *dir
, char *file
)
433 fullPath
= wfindfileinarray(PixmapPath
, file
);
437 sprintf(buffer
, "coould not find file %s", file
);
440 copyFile(dir
, fullPath
);
446 makeThemePack(proplist_t style
, char *themeName
)
454 themeDir
= wmalloc(strlen(themeName
)+50);
455 sprintf(themeDir
, "%s.themed", themeName
);
456 ThemePath
= themeDir
;
460 tmp
= wmalloc(strlen(themeDir
)+20);
461 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
462 if (system(tmp
)!=0) {
463 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
468 keys
= PLGetAllDictionaryKeys(style
);
470 for (i
= 0; i
< PLGetNumberOfElements(keys
); i
++) {
471 key
= PLGetArrayElement(keys
, i
);
473 value
= PLGetDictionaryEntry(style
, key
);
474 if (value
&& PLIsArray(value
) && PLGetNumberOfElements(value
) > 2) {
478 type
= PLGetArrayElement(value
, 0);
479 t
= PLGetString(type
);
483 if (strcasecmp(t
, "tpixmap")==0
484 || strcasecmp(t
, "spixmap")==0
485 || strcasecmp(t
, "cpixmap")==0
486 || strcasecmp(t
, "mpixmap")==0
487 || strcasecmp(t
, "tdgradient")==0
488 || strcasecmp(t
, "tvgradient")==0
489 || strcasecmp(t
, "thgradient")==0) {
494 file
= PLGetArrayElement(value
, 1);
496 p
= strrchr(PLGetString(file
), '/');
498 copyFile(themeDir
, PLGetString(file
));
500 newPath
= wstrdup(p
+1);
501 PLRemoveArrayElement(value
, 1);
502 PLInsertArrayElement(value
, PLMakeString(newPath
), 1);
505 findCopyFile(themeDir
, PLGetString(file
));
507 } else if (strcasecmp(t
, "bitmap")==0) {
512 file
= PLGetArrayElement(value
, 1);
514 p
= strrchr(PLGetString(file
), '/');
516 copyFile(themeDir
, PLGetString(file
));
518 newPath
= wstrdup(p
+1);
519 PLRemoveArrayElement(value
, 1);
520 PLInsertArrayElement(value
, PLMakeString(newPath
), 1);
523 findCopyFile(themeDir
, PLGetString(file
));
527 file
= PLGetArrayElement(value
, 2);
529 p
= strrchr(PLGetString(file
), '/');
531 copyFile(themeDir
, PLGetString(file
));
533 newPath
= wstrdup(p
+1);
534 PLRemoveArrayElement(value
, 2);
535 PLInsertArrayElement(value
, PLMakeString(newPath
), 2);
538 findCopyFile(themeDir
, PLGetString(file
));
549 main(int argc
, char **argv
)
551 proplist_t prop
, style
, key
, val
;
555 char *style_file
= NULL
;
561 for (i
=1; i
<argc
; i
++) {
562 if (strcmp(argv
[i
], "-p")==0
563 || strcmp(argv
[i
], "--pack")==0) {
566 } else if (strcmp(argv
[i
], "-t")==0
567 || strcmp(argv
[i
], "--theme-options")==0) {
569 } else if (strcmp(argv
[i
], "--help")==0) {
572 } else if (strcmp(argv
[i
], "--version")==0) {
576 if (style_file
!=NULL
) {
577 printf("%s: invalid argument '%s'\n", argv
[0],
578 style_file
[0]=='-' ? style_file
: argv
[i
]);
579 printf("Try '%s --help' for more information\n", argv
[0]);
582 style_file
= argv
[i
];
587 if (make_pack
&& !style_file
) {
588 printf("%s: you must supply a name for the theme pack\n", ProgName
);
592 PLSetStringCmpHook(StringCompareHook
);
594 path
= defaultsPathForDomain("WindowMaker");
596 prop
= PLGetProplistWithPath(path
);
598 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
604 /* get global value */
605 path
= globalDefaultsPathForDomain("WindowMaker");
606 val
= PLGetProplistWithPath(path
);
608 PLMergeDictionaries(val
, prop
);
613 style
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
616 for (i
=0; options
[i
]!=NULL
; i
++) {
617 key
= PLMakeString(options
[i
]);
619 val
= PLGetDictionaryEntry(prop
, key
);
621 PLInsertDictionaryEntry(style
, key
, val
);
624 val
= PLGetDictionaryEntry(prop
, PLMakeString("PixmapPath"));
629 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
630 key
= PLMakeString(theme_options
[i
]);
632 val
= PLGetDictionaryEntry(prop
, key
);
634 PLInsertDictionaryEntry(style
, key
, val
);
641 makeThemePack(style
, style_file
);
643 path
= wmalloc(strlen(ThemePath
)+32);
644 strcpy(path
, ThemePath
);
645 strcat(path
, "/style");
646 PLSetFilename(style
, PLMakeString(path
));
650 val
= PLMakeString(style_file
);
651 PLSetFilename(style
, val
);
654 puts(PLGetDescriptionIndent(style
, 0));