Update to Window Maker 0.50.2
[wmaker-crm.git] / util / getstyle.c
blob3697742789f7abb11106083fee09496544dea6fc
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 #include <stdlib.h>
25 #include <stdio.h>
26 #include <proplist.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <pwd.h>
31 #include <limits.h>
33 #ifndef PATH_MAX
34 #define PATH_MAX 1024
35 #endif
37 #include "../src/wconfig.h"
39 /* table of style related options */
40 static char *options[] = {
41 "TitleJustify",
42 "ClipTitleFont",
43 "WindowTitleFont",
44 "MenuTitleFont",
45 "MenuTextFont",
46 "IconTitleFont",
47 "DisplayFont",
48 "HighlightColor",
49 "HighlightTextColor",
50 "ClipTitleColor",
51 "CClipTitleColor",
52 "FTitleColor",
53 "PTitleColor",
54 "UTitleColor",
55 "FTitleBack",
56 "PTitleBack",
57 "UTitleBack",
58 "MenuTitleColor",
59 "MenuTextColor",
60 "MenuDisabledColor",
61 "MenuTitleBack",
62 "MenuTextBack",
63 "IconBack",
64 "IconTitleColor",
65 "IconTitleBack",
66 NULL
70 /* table of theme related options */
71 static char *theme_options[] = {
72 "WorkspaceBack",
73 NULL
79 char *ProgName;
81 char *PixmapPath = NULL;
83 char *ThemePath = NULL;
86 void
87 print_help()
89 printf("usage: %s [-options] [<style file>]\n", ProgName);
90 puts("options:");
91 puts(" -h print help");
92 puts(" -t get theme options too");
93 puts(" -p produce a theme pack");
97 char*
98 defaultsPathForDomain(char *domain)
100 char path[1024];
101 char *gspath, *tmp;
103 gspath = getenv("GNUSTEP_USER_ROOT");
104 if (gspath) {
105 strcpy(path, gspath);
106 strcat(path, "/");
107 } else {
108 char *home;
110 home = getenv("HOME");
111 if (!home) {
112 printf("%s:could not get HOME environment variable!\n", ProgName);
113 exit(0);
115 strcpy(path, home);
116 strcat(path, "/GNUstep/");
118 strcat(path, DEFAULTS_DIR);
119 strcat(path, "/");
120 strcat(path, domain);
122 tmp = malloc(strlen(path)+2);
123 strcpy(tmp, path);
125 return tmp;
129 BOOL
130 StringCompareHook(proplist_t pl1, proplist_t pl2)
132 char *str1, *str2;
134 str1 = PLGetString(pl1);
135 str2 = PLGetString(pl2);
137 if (strcasecmp(str1, str2)==0)
138 return YES;
139 else
140 return NO;
144 void
145 abortar(char *reason)
147 char buffer[4000];
149 printf("%s: %s\n", ProgName, reason);
151 if (ThemePath) {
152 printf("Removing unfinished theme pack\n");
153 sprintf(buffer, "/bin/rm -fr %s", ThemePath);
155 if (system(buffer)!=0) {
156 printf("%s: could not execute command %s\n", ProgName, buffer);
159 exit(1);
165 char*
166 wgethomedir()
168 char *home = getenv("HOME");
169 struct passwd *user;
171 if (home)
172 return home;
174 user = getpwuid(getuid());
175 if (!user) {
176 char buffer[80];
178 sprintf(buffer, "could not get password entry for UID %i", getuid());
179 perror(buffer);
180 return "/";
182 if (!user->pw_dir) {
183 return "/";
184 } else {
185 return user->pw_dir;
190 void*
191 wmalloc(int size)
193 void *tmp;
195 tmp = malloc(size);
196 if (!tmp) {
197 abortar("out of memory");
200 return tmp;
204 char*
205 wstrdup(char *str)
207 char *tmp;
209 tmp = wmalloc(strlen(str)+1);
211 strcpy(tmp, str);
213 return tmp;
217 static char*
218 getuserhomedir(char *username)
220 struct passwd *user;
222 user = getpwnam(username);
223 if (!user) {
224 char buffer[100];
226 sprintf(buffer,"could not get password entry for user %s", username);
227 perror(buffer);
228 return NULL;
230 if (!user->pw_dir) {
231 return "/";
232 } else {
233 return user->pw_dir;
240 char*
241 wexpandpath(char *path)
243 char buffer2[PATH_MAX+2];
244 char buffer[PATH_MAX+2];
245 int i;
247 memset(buffer, 0, PATH_MAX+2);
249 if (*path=='~') {
250 char *home;
252 path++;
253 if (*path=='/' || *path==0) {
254 home = wgethomedir();
255 strcat(buffer, home);
256 } else {
257 int j;
258 j = 0;
259 while (*path!=0 && *path!='/') {
260 buffer2[j++] = *path;
261 buffer2[j] = 0;
262 path++;
264 home = getuserhomedir(buffer2);
265 if (!home)
266 return NULL;
267 strcat(buffer, home);
271 i = strlen(buffer);
273 while (*path!=0) {
274 char *tmp;
276 if (*path=='$') {
277 int j = 0;
278 path++;
279 /* expand $(HOME) or $HOME style environment variables */
280 if (*path=='(') {
281 path++;
282 while (*path!=0 && *path!=')') {
283 buffer2[j++] = *(path++);
284 buffer2[j] = 0;
286 if (*path==')')
287 path++;
288 tmp = getenv(buffer2);
289 if (!tmp) {
290 buffer[i] = 0;
291 strcat(buffer, "$(");
292 strcat(buffer, buffer2);
293 strcat(buffer, ")");
294 i += strlen(buffer2)+3;
295 } else {
296 strcat(buffer, tmp);
297 i += strlen(tmp);
299 } else {
300 while (*path!=0 && *path!='/') {
301 buffer2[j++] = *(path++);
302 buffer2[j] = 0;
304 tmp = getenv(buffer2);
305 if (!tmp) {
306 strcat(buffer, "$");
307 strcat(buffer, buffer2);
308 i += strlen(buffer2)+1;
309 } else {
310 strcat(buffer, tmp);
311 i += strlen(tmp);
314 } else {
315 buffer[i++] = *path;
316 path++;
320 return wstrdup(buffer);
325 char*
326 wfindfileinarray(proplist_t paths, char *file)
328 int i;
329 char *path;
330 int len, flen;
331 char *fullpath;
333 if (!file)
334 return NULL;
336 if (*file=='/' || *file=='~' || !paths || !PLIsArray(paths)
337 || PLGetNumberOfElements(paths)==0) {
338 if (access(file, R_OK)<0) {
339 fullpath = wexpandpath(file);
340 if (!fullpath)
341 return NULL;
343 if (access(fullpath, R_OK)<0) {
344 free(fullpath);
345 return NULL;
346 } else {
347 return fullpath;
349 } else {
350 return wstrdup(file);
354 flen = strlen(file);
355 for (i=0; i < PLGetNumberOfElements(paths); i++) {
356 proplist_t tmp;
357 char *dir;
359 tmp = PLGetArrayElement(paths, i);
360 if (!PLIsString(tmp) || !(dir = PLGetString(tmp)))
361 continue;
363 len = strlen(dir);
364 path = wmalloc(len+flen+2);
365 path = memcpy(path, dir, len);
366 path[len]=0;
367 strcat(path, "/");
368 strcat(path, file);
369 /* expand tilde */
370 fullpath = wexpandpath(path);
371 free(path);
372 if (fullpath) {
373 /* check if file is readable */
374 if (access(fullpath, R_OK)==0) {
375 return fullpath;
377 free(fullpath);
380 return NULL;
386 void
387 copyFile(char *dir, char *file)
389 char buffer[4000];
391 sprintf(buffer, "/bin/cp %s %s", file, dir);
392 if (system(buffer)!=0) {
393 printf("%s: could not copy file %s\n", ProgName, file);
398 void
399 findCopyFile(char *dir, char *file)
401 char *fullPath;
403 fullPath = wfindfileinarray(PixmapPath, file);
404 copyFile(dir, fullPath);
405 free(fullPath);
409 char*
410 makeThemePack(proplist_t style, char *themeName)
412 proplist_t keys;
413 proplist_t key;
414 proplist_t value;
415 int i;
416 char *themeDir;
418 themeDir = wmalloc(strlen(themeName)+50);
419 sprintf(themeDir, "%s.themed", themeName);
420 ThemePath = themeDir;
422 char *tmp;
424 tmp = wmalloc(strlen(themeDir)+20);
425 sprintf(tmp, "/bin/mkdir %s", themeDir);
426 if (system(tmp)!=0) {
427 printf("%s: could not create directory %s\n", ProgName, themeDir);
428 exit(1);
430 free(tmp);
432 keys = PLGetAllDictionaryKeys(style);
434 for (i = 0; i < PLGetNumberOfElements(keys); i++) {
435 key = PLGetArrayElement(keys, i);
437 value = PLGetDictionaryEntry(style, key);
438 if (value && PLIsArray(value) && PLGetNumberOfElements(value) > 2) {
439 proplist_t type;
440 char *t;
442 type = PLGetArrayElement(value, 0);
443 t = PLGetString(type);
444 if (t && (strcasecmp(t, "tpixmap")==0
445 || strcasecmp(t, "spixmap")==0
446 || strcasecmp(t, "cpixmap")==0
447 || strcasecmp(t, "tdgradient")==0
448 || strcasecmp(t, "tvgradient")==0
449 || strcasecmp(t, "thgradient")==0)) {
450 proplist_t file;
451 char *p;
452 char *newPath;
454 file = PLGetArrayElement(value, 1);
456 p = strrchr(PLGetString(file), '/');
457 if (p) {
458 copyFile(themeDir, PLGetString(file));
460 newPath = wstrdup(p+1);
461 PLRemoveArrayElement(value, 1);
462 PLInsertArrayElement(value, PLMakeString(newPath), 1);
463 free(newPath);
464 } else {
465 findCopyFile(themeDir, PLGetString(file));
471 return themeDir;
475 int
476 main(int argc, char **argv)
478 proplist_t prop, style, key, val;
479 char *path;
480 int i, theme_too=0;
481 int make_pack = 0;
482 char *style_file = NULL;
485 ProgName = argv[0];
487 if (argc>1) {
488 for (i=1; i<argc; i++) {
489 if (strcmp(argv[i], "-p")==0) {
490 make_pack = 1;
491 theme_too = 1;
492 } else if (strcmp(argv[i], "-t")==0) {
493 theme_too++;
494 } else if (argv[i][0] != '-') {
495 style_file = argv[i];
496 } else {
497 print_help();
498 exit(1);
503 if (make_pack && !style_file) {
504 printf("%s: you must supply a name for the theme pack\n", ProgName);
505 exit(1);
508 PLSetStringCmpHook(StringCompareHook);
510 path = defaultsPathForDomain("WindowMaker");
512 prop = PLGetProplistWithPath(path);
513 if (!prop) {
514 printf("%s:could not load WindowMaker configuration file \"%s\".\n",
515 ProgName, path);
516 exit(1);
519 style = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
522 for (i=0; options[i]!=NULL; i++) {
523 key = PLMakeString(options[i]);
525 val = PLGetDictionaryEntry(prop, key);
526 if (val)
527 PLInsertDictionaryEntry(style, key, val);
530 val = PLGetDictionaryEntry(prop, PLMakeString("PixmapPath"));
531 PixmapPath = PLGetString(val);
533 if (theme_too) {
534 for (i=0; theme_options[i]!=NULL; i++) {
535 key = PLMakeString(theme_options[i]);
537 val = PLGetDictionaryEntry(prop, key);
538 if (val)
539 PLInsertDictionaryEntry(style, key, val);
543 if (make_pack) {
544 char *path;
545 char *themeDir;
547 makeThemePack(style, style_file);
549 path = wmalloc(strlen(ThemePath)+32);
550 strcpy(path, ThemePath);
551 strcat(path, "/style");
552 PLSetFilename(style, PLMakeString(path));
553 PLSave(style, NO);
554 } else {
555 if (style_file) {
556 val = PLMakeString(style_file);
557 PLSetFilename(style, val);
558 PLSave(style, NO);
559 } else {
560 puts(PLGetDescriptionIndent(style, 0));
563 exit(0);