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
)
120 sprintf(path
, "%s/WindowMaker/%s", SYSCONFDIR
, domain
);
122 tmp
= malloc(strlen(path
)+2);
131 defaultsPathForDomain(char *domain
)
136 gspath
= getenv("GNUSTEP_USER_ROOT");
138 strcpy(path
, gspath
);
143 home
= getenv("HOME");
145 printf("%s:could not get HOME environment variable!\n", ProgName
);
149 strcat(path
, "/GNUstep/");
151 strcat(path
, DEFAULTS_DIR
);
153 strcat(path
, domain
);
155 tmp
= malloc(strlen(path
)+2);
163 StringCompareHook(proplist_t pl1
, proplist_t pl2
)
167 str1
= PLGetString(pl1
);
168 str2
= PLGetString(pl2
);
170 if (strcasecmp(str1
, str2
)==0)
178 abortar(char *reason
)
182 printf("%s: %s\n", ProgName
, reason
);
185 printf("Removing unfinished theme pack\n");
186 sprintf(buffer
, "/bin/rm -fr \"%s\"", ThemePath
);
188 if (system(buffer
)!=0) {
189 printf("%s: could not execute command %s\n", ProgName
, buffer
);
201 char *home
= getenv("HOME");
207 user
= getpwuid(getuid());
211 sprintf(buffer
, "could not get password entry for UID %i", getuid());
230 abortar("out of memory");
242 tmp
= wmalloc(strlen(str
)+1);
251 getuserhomedir(char *username
)
255 user
= getpwnam(username
);
259 sprintf(buffer
,"could not get password entry for user %s", username
);
274 wexpandpath(char *path
)
276 char buffer2
[PATH_MAX
+2];
277 char buffer
[PATH_MAX
+2];
280 memset(buffer
, 0, PATH_MAX
+2);
286 if (*path
=='/' || *path
==0) {
287 home
= wgethomedir();
288 strcat(buffer
, home
);
292 while (*path
!=0 && *path
!='/') {
293 buffer2
[j
++] = *path
;
297 home
= getuserhomedir(buffer2
);
300 strcat(buffer
, home
);
312 /* expand $(HOME) or $HOME style environment variables */
315 while (*path
!=0 && *path
!=')') {
316 buffer2
[j
++] = *(path
++);
321 tmp
= getenv(buffer2
);
324 strcat(buffer
, "$(");
325 strcat(buffer
, buffer2
);
327 i
+= strlen(buffer2
)+3;
333 while (*path
!=0 && *path
!='/') {
334 buffer2
[j
++] = *(path
++);
337 tmp
= getenv(buffer2
);
340 strcat(buffer
, buffer2
);
341 i
+= strlen(buffer2
)+1;
353 return wstrdup(buffer
);
359 wfindfileinarray(proplist_t paths
, char *file
)
369 if (*file
=='/' || *file
=='~' || !paths
|| !PLIsArray(paths
)
370 || PLGetNumberOfElements(paths
)==0) {
371 if (access(file
, R_OK
)<0) {
372 fullpath
= wexpandpath(file
);
376 if (access(fullpath
, R_OK
)<0) {
383 return wstrdup(file
);
388 for (i
=0; i
< PLGetNumberOfElements(paths
); i
++) {
392 tmp
= PLGetArrayElement(paths
, i
);
393 if (!PLIsString(tmp
) || !(dir
= PLGetString(tmp
)))
397 path
= wmalloc(len
+flen
+2);
398 path
= memcpy(path
, dir
, len
);
403 fullpath
= wexpandpath(path
);
406 /* check if file is readable */
407 if (access(fullpath
, R_OK
)==0) {
420 copyFile(char *dir
, char *file
)
424 sprintf(buffer
, "/bin/cp \"%s\" \"%s\"", file
, dir
);
425 if (system(buffer
)!=0) {
426 printf("%s: could not copy file %s\n", ProgName
, file
);
432 findCopyFile(char *dir
, char *file
)
436 fullPath
= wfindfileinarray(PixmapPath
, file
);
440 sprintf(buffer
, "coould not find file %s", file
);
443 copyFile(dir
, fullPath
);
449 makeThemePack(proplist_t style
, char *themeName
)
457 themeDir
= wmalloc(strlen(themeName
)+50);
458 sprintf(themeDir
, "%s.themed", themeName
);
459 ThemePath
= themeDir
;
463 tmp
= wmalloc(strlen(themeDir
)+20);
464 sprintf(tmp
, "/bin/mkdir \"%s\"", themeDir
);
465 if (system(tmp
)!=0) {
466 printf("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n", ProgName
, themeDir
);
471 keys
= PLGetAllDictionaryKeys(style
);
473 for (i
= 0; i
< PLGetNumberOfElements(keys
); i
++) {
474 key
= PLGetArrayElement(keys
, i
);
476 value
= PLGetDictionaryEntry(style
, key
);
477 if (value
&& PLIsArray(value
) && PLGetNumberOfElements(value
) > 2) {
481 type
= PLGetArrayElement(value
, 0);
482 t
= PLGetString(type
);
486 if (strcasecmp(t
, "tpixmap")==0
487 || strcasecmp(t
, "spixmap")==0
488 || strcasecmp(t
, "cpixmap")==0
489 || strcasecmp(t
, "mpixmap")==0
490 || strcasecmp(t
, "tdgradient")==0
491 || strcasecmp(t
, "tvgradient")==0
492 || strcasecmp(t
, "thgradient")==0) {
497 file
= PLGetArrayElement(value
, 1);
499 p
= strrchr(PLGetString(file
), '/');
501 copyFile(themeDir
, PLGetString(file
));
503 newPath
= wstrdup(p
+1);
504 PLRemoveArrayElement(value
, 1);
505 PLInsertArrayElement(value
, PLMakeString(newPath
), 1);
508 findCopyFile(themeDir
, PLGetString(file
));
510 } else if (strcasecmp(t
, "bitmap")==0) {
515 file
= PLGetArrayElement(value
, 1);
517 p
= strrchr(PLGetString(file
), '/');
519 copyFile(themeDir
, PLGetString(file
));
521 newPath
= wstrdup(p
+1);
522 PLRemoveArrayElement(value
, 1);
523 PLInsertArrayElement(value
, PLMakeString(newPath
), 1);
526 findCopyFile(themeDir
, PLGetString(file
));
530 file
= PLGetArrayElement(value
, 2);
532 p
= strrchr(PLGetString(file
), '/');
534 copyFile(themeDir
, PLGetString(file
));
536 newPath
= wstrdup(p
+1);
537 PLRemoveArrayElement(value
, 2);
538 PLInsertArrayElement(value
, PLMakeString(newPath
), 2);
541 findCopyFile(themeDir
, PLGetString(file
));
552 main(int argc
, char **argv
)
554 proplist_t prop
, style
, key
, val
;
558 char *style_file
= NULL
;
564 for (i
=1; i
<argc
; i
++) {
565 if (strcmp(argv
[i
], "-p")==0
566 || strcmp(argv
[i
], "--pack")==0) {
569 } else if (strcmp(argv
[i
], "-t")==0
570 || strcmp(argv
[i
], "--theme-options")==0) {
572 } else if (strcmp(argv
[i
], "--help")==0) {
575 } else if (strcmp(argv
[i
], "--version")==0) {
579 if (style_file
!=NULL
) {
580 printf("%s: invalid argument '%s'\n", argv
[0],
581 style_file
[0]=='-' ? style_file
: argv
[i
]);
582 printf("Try '%s --help' for more information\n", argv
[0]);
585 style_file
= argv
[i
];
590 if (make_pack
&& !style_file
) {
591 printf("%s: you must supply a name for the theme pack\n", ProgName
);
595 PLSetStringCmpHook(StringCompareHook
);
597 path
= defaultsPathForDomain("WindowMaker");
599 prop
= PLGetProplistWithPath(path
);
601 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
607 /* get global value */
608 path
= globalDefaultsPathForDomain("WindowMaker");
609 val
= PLGetProplistWithPath(path
);
611 PLMergeDictionaries(val
, prop
);
616 style
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
619 for (i
=0; options
[i
]!=NULL
; i
++) {
620 key
= PLMakeString(options
[i
]);
622 val
= PLGetDictionaryEntry(prop
, key
);
624 PLInsertDictionaryEntry(style
, key
, val
);
627 val
= PLGetDictionaryEntry(prop
, PLMakeString("PixmapPath"));
632 for (i
=0; theme_options
[i
]!=NULL
; i
++) {
633 key
= PLMakeString(theme_options
[i
]);
635 val
= PLGetDictionaryEntry(prop
, key
);
637 PLInsertDictionaryEntry(style
, key
, val
);
644 makeThemePack(style
, style_file
);
646 path
= wmalloc(strlen(ThemePath
)+32);
647 strcpy(path
, ThemePath
);
648 strcat(path
, "/style");
649 PLSetFilename(style
, PLMakeString(path
));
653 val
= PLMakeString(style_file
);
654 PLSetFilename(style
, val
);
657 puts(PLGetDescriptionIndent(style
, 0));