Fix multiple errors in findfile.c
[wmaker-crm.git] / util / getstyle.c
blob458c518df8390a7a43a69e30ec4d80c9ce16cc3b
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
5 * Copyright (c) 1997-2003 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,
20 * USA.
23 #define PROG_VERSION "getstyle (Window Maker) 0.6"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <pwd.h>
31 #include <limits.h>
32 #include <assert.h>
33 #include <WINGs/WUtil.h>
35 #ifndef PATH_MAX
36 #define PATH_MAX 1024
37 #endif
39 #include "../src/wconfig.h"
41 /* table of style related options */
42 static char *options[] = {
43 "TitleJustify",
44 "ClipTitleFont",
45 "WindowTitleFont",
46 "MenuTitleFont",
47 "MenuTextFont",
48 "IconTitleFont",
49 "DisplayFont",
50 "LargeDisplayFont",
51 "WindowTitleExtendSpace",
52 "MenuTitleExtendSpace",
53 "MenuTextExtendSpace",
54 "HighlightColor",
55 "HighlightTextColor",
56 "ClipTitleColor",
57 "CClipTitleColor",
58 "FTitleColor",
59 "PTitleColor",
60 "UTitleColor",
61 "FTitleBack",
62 "PTitleBack",
63 "UTitleBack",
64 "ResizebarBack",
65 "MenuTitleColor",
66 "MenuTextColor",
67 "MenuDisabledColor",
68 "MenuTitleBack",
69 "MenuTextBack",
70 "IconBack",
71 "IconTitleColor",
72 "IconTitleBack",
73 "MenuStyle",
74 "WindowTitleExtendSpace",
75 "MenuTitleExtendSpace",
76 "MenuTextExtendSpace",
77 NULL
80 /* table of theme related options */
81 static char *theme_options[] = {
82 "WorkspaceBack",
83 "NormalCursor",
84 "ArrowCursor",
85 "MoveCursor",
86 "ResizeCursor",
87 "TopLeftResizeCursor",
88 "TopRightResizeCursor",
89 "BottomLeftResizeCursor",
90 "BottomRightResizeCursor",
91 "VerticalResizeCursor",
92 "HorizontalResizeCursor",
93 "WaitCursor",
94 "QuestionCursor",
95 "TextCursor",
96 "SelectCursor",
97 NULL
100 /* table of style related fonts */
102 static char *font_options[] = {
103 "ClipTitleFont",
104 "WindowTitleFont",
105 "MenuTitleFont",
106 "MenuTextFont",
107 "IconTitleFont",
108 "DisplayFont",
109 "LargeDisplayFont",
110 NULL
113 char *ProgName;
115 WMPropList *PixmapPath = NULL;
117 char *ThemePath = NULL;
119 extern char *convertFont(char *font, Bool keepXLFD);
121 void print_help()
123 printf("Usage: %s [OPTIONS] [FILE]\n", ProgName);
124 puts("Retrieves style/theme configuration and output to FILE or to stdout");
125 puts("");
126 puts(" -t, --theme-options output theme related options when producing a style file");
127 puts(" -p, --pack produce output as a theme pack");
128 puts(" --help display this help and exit");
129 puts(" --version output version information and exit");
132 char *globalDefaultsPathForDomain(char *domain)
134 static char path[1024];
136 sprintf(path, "%s/WindowMaker/%s", SYSCONFDIR, domain);
138 return path;
141 char *defaultsPathForDomain(char *domain)
143 static char path[1024];
144 char *gspath;
146 gspath = getenv("GNUSTEP_USER_ROOT");
147 if (gspath) {
148 strcpy(path, gspath);
149 strcat(path, "/");
150 } else {
151 char *home;
153 home = getenv("HOME");
154 if (!home) {
155 printf("%s:could not get HOME environment variable!\n", ProgName);
156 exit(0);
158 strcpy(path, home);
159 strcat(path, "/GNUstep/");
161 strcat(path, DEFAULTS_DIR);
162 strcat(path, "/");
163 strcat(path, domain);
165 return path;
168 void abortar(char *reason)
170 char buffer[4000];
172 printf("%s: %s\n", ProgName, reason);
174 if (ThemePath) {
175 printf("Removing unfinished theme pack\n");
176 sprintf(buffer, "/bin/rm -fr \"%s\"", ThemePath);
178 if (system(buffer) != 0) {
179 printf("%s: could not execute command %s\n", ProgName, buffer);
182 exit(1);
185 char *wgethomedir()
187 char *home = getenv("HOME");
188 struct passwd *user;
190 if (home)
191 return home;
193 user = getpwuid(getuid());
194 if (!user) {
195 char buffer[80];
197 sprintf(buffer, "could not get password entry for UID %i", getuid());
198 perror(buffer);
199 return "/";
201 if (!user->pw_dir) {
202 return "/";
203 } else {
204 return user->pw_dir;
208 static char *getuserhomedir(char *username)
210 struct passwd *user;
212 user = getpwnam(username);
213 if (!user) {
214 char buffer[100];
216 sprintf(buffer, "could not get password entry for user %s", username);
217 perror(buffer);
218 return NULL;
220 if (!user->pw_dir) {
221 return "/";
222 } else {
223 return user->pw_dir;
227 char *wexpandpath(char *path)
229 char buffer2[PATH_MAX + 2];
230 char buffer[PATH_MAX + 2];
231 int i;
233 memset(buffer, 0, PATH_MAX + 2);
235 if (*path == '~') {
236 char *home;
238 path++;
239 if (*path == '/' || *path == 0) {
240 home = wgethomedir();
241 strcat(buffer, home);
242 } else {
243 int j;
244 j = 0;
245 while (*path != 0 && *path != '/') {
246 buffer2[j++] = *path;
247 buffer2[j] = 0;
248 path++;
250 home = getuserhomedir(buffer2);
251 if (!home)
252 return NULL;
253 strcat(buffer, home);
257 i = strlen(buffer);
259 while (*path != 0) {
260 char *tmp;
262 if (*path == '$') {
263 int j = 0;
264 path++;
265 /* expand $(HOME) or $HOME style environment variables */
266 if (*path == '(') {
267 path++;
268 while (*path != 0 && *path != ')') {
269 buffer2[j++] = *(path++);
270 buffer2[j] = 0;
272 if (*path == ')')
273 path++;
274 tmp = getenv(buffer2);
275 if (!tmp) {
276 buffer[i] = 0;
277 strcat(buffer, "$(");
278 strcat(buffer, buffer2);
279 strcat(buffer, ")");
280 i += strlen(buffer2) + 3;
281 } else {
282 strcat(buffer, tmp);
283 i += strlen(tmp);
285 } else {
286 while (*path != 0 && *path != '/') {
287 buffer2[j++] = *(path++);
288 buffer2[j] = 0;
290 tmp = getenv(buffer2);
291 if (!tmp) {
292 strcat(buffer, "$");
293 strcat(buffer, buffer2);
294 i += strlen(buffer2) + 1;
295 } else {
296 strcat(buffer, tmp);
297 i += strlen(tmp);
300 } else {
301 buffer[i++] = *path;
302 path++;
306 return wstrdup(buffer);
309 char *wfindfileinarray(WMPropList * paths, char *file)
311 int i;
312 char *path;
313 int len, flen;
314 char *fullpath;
316 if (!file)
317 return NULL;
319 if (*file == '/' || *file == '~' || !paths || !WMIsPLArray(paths)
320 || WMGetPropListItemCount(paths) == 0) {
321 if (access(file, R_OK) < 0) {
322 fullpath = wexpandpath(file);
323 if (!fullpath)
324 return NULL;
326 if (access(fullpath, R_OK) < 0) {
327 free(fullpath);
328 return NULL;
329 } else {
330 return fullpath;
332 } else {
333 return wstrdup(file);
337 flen = strlen(file);
338 for (i = 0; i < WMGetPropListItemCount(paths); i++) {
339 WMPropList *tmp;
340 char *dir;
342 tmp = WMGetFromPLArray(paths, i);
343 if (!WMIsPLString(tmp) || !(dir = WMGetFromPLString(tmp)))
344 continue;
346 len = strlen(dir);
347 path = wmalloc(len + flen + 2);
348 path = memcpy(path, dir, len);
349 path[len] = 0;
350 strcat(path, "/");
351 strcat(path, file);
352 /* expand tilde */
353 fullpath = wexpandpath(path);
354 free(path);
355 if (fullpath) {
356 /* check if file is readable */
357 if (access(fullpath, R_OK) == 0) {
358 return fullpath;
360 free(fullpath);
363 return NULL;
366 static Bool isFontOption(char *option)
368 int i;
370 for (i = 0; font_options[i] != NULL; i++) {
371 if (strcasecmp(option, font_options[i]) == 0) {
372 return True;
376 return False;
379 void copyFile(char *dir, char *file)
381 char buffer[4000];
383 sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
384 if (system(buffer) != 0) {
385 printf("%s: could not copy file %s\n", ProgName, file);
389 void findCopyFile(char *dir, char *file)
391 char *fullPath;
393 fullPath = wfindfileinarray(PixmapPath, file);
394 if (!fullPath) {
395 char buffer[4000];
397 sprintf(buffer, "could not find file %s", file);
398 abortar(buffer);
400 copyFile(dir, fullPath);
401 free(fullPath);
404 char *makeThemePack(WMPropList * style, char *themeName)
406 WMPropList *keys;
407 WMPropList *key;
408 WMPropList *value;
409 int i;
410 char *themeDir;
412 themeDir = wmalloc(strlen(themeName) + 50);
413 sprintf(themeDir, "%s.themed", themeName);
414 ThemePath = themeDir;
416 char *tmp;
418 tmp = wmalloc(strlen(themeDir) + 20);
419 sprintf(tmp, "/bin/mkdir \"%s\"", themeDir);
420 if (system(tmp) != 0) {
421 printf
422 ("%s: could not create directory %s. Probably there's already a theme with that name in this directory.\n",
423 ProgName, themeDir);
424 exit(1);
426 free(tmp);
428 keys = WMGetPLDictionaryKeys(style);
430 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
431 key = WMGetFromPLArray(keys, i);
433 value = WMGetFromPLDictionary(style, key);
434 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
435 WMPropList *type;
436 char *t;
438 type = WMGetFromPLArray(value, 0);
439 t = WMGetFromPLString(type);
440 if (t == NULL)
441 continue;
443 if (strcasecmp(t, "tpixmap") == 0
444 || strcasecmp(t, "spixmap") == 0
445 || strcasecmp(t, "cpixmap") == 0
446 || strcasecmp(t, "mpixmap") == 0
447 || strcasecmp(t, "tdgradient") == 0
448 || strcasecmp(t, "tvgradient") == 0 || strcasecmp(t, "thgradient") == 0) {
449 WMPropList *file;
450 char *p;
451 char *newPath;
453 file = WMGetFromPLArray(value, 1);
455 p = strrchr(WMGetFromPLString(file), '/');
456 if (p) {
457 copyFile(themeDir, WMGetFromPLString(file));
459 newPath = wstrdup(p + 1);
460 WMDeleteFromPLArray(value, 1);
461 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
462 free(newPath);
463 } else {
464 findCopyFile(themeDir, WMGetFromPLString(file));
466 } else if (strcasecmp(t, "bitmap") == 0) {
467 WMPropList *file;
468 char *p;
469 char *newPath;
471 file = WMGetFromPLArray(value, 1);
473 p = strrchr(WMGetFromPLString(file), '/');
474 if (p) {
475 copyFile(themeDir, WMGetFromPLString(file));
477 newPath = wstrdup(p + 1);
478 WMDeleteFromPLArray(value, 1);
479 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
480 free(newPath);
481 } else {
482 findCopyFile(themeDir, WMGetFromPLString(file));
485 file = WMGetFromPLArray(value, 2);
487 p = strrchr(WMGetFromPLString(file), '/');
488 if (p) {
489 copyFile(themeDir, WMGetFromPLString(file));
491 newPath = wstrdup(p + 1);
492 WMDeleteFromPLArray(value, 2);
493 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
494 free(newPath);
495 } else {
496 findCopyFile(themeDir, WMGetFromPLString(file));
502 return themeDir;
505 int main(int argc, char **argv)
507 WMPropList *prop, *style, *key, *val;
508 char *path;
509 int i, theme_too = 0, make_pack = 0;
510 char *style_file = NULL;
512 ProgName = argv[0];
514 if (argc > 1) {
515 for (i = 1; i < argc; i++) {
516 if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--pack") == 0) {
517 make_pack = 1;
518 theme_too = 1;
519 } else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--theme-options") == 0) {
520 theme_too++;
521 } else if (strcmp(argv[i], "--help") == 0) {
522 print_help();
523 exit(0);
524 } else if (strcmp(argv[i], "--version") == 0) {
525 puts(PROG_VERSION);
526 exit(0);
527 } else {
528 if (style_file != NULL) {
529 printf("%s: invalid argument '%s'\n", argv[0],
530 style_file[0] == '-' ? style_file : argv[i]);
531 printf("Try '%s --help' for more information\n", argv[0]);
532 exit(1);
534 style_file = argv[i];
539 if (make_pack && !style_file) {
540 printf("%s: you must supply a name for the theme pack\n", ProgName);
541 exit(1);
544 WMPLSetCaseSensitive(False);
546 path = defaultsPathForDomain("WindowMaker");
548 prop = WMReadPropListFromFile(path);
549 if (!prop) {
550 printf("%s:could not load WindowMaker configuration file \"%s\".\n", ProgName, path);
551 exit(1);
554 /* get global value */
555 path = globalDefaultsPathForDomain("WindowMaker");
556 val = WMReadPropListFromFile(path);
557 if (val) {
558 WMMergePLDictionaries(val, prop, True);
559 WMReleasePropList(prop);
560 prop = val;
563 style = WMCreatePLDictionary(NULL, NULL);
565 for (i = 0; options[i] != NULL; i++) {
566 key = WMCreatePLString(options[i]);
568 val = WMGetFromPLDictionary(prop, key);
569 if (val) {
570 WMRetainPropList(val);
571 if (isFontOption(options[i])) {
572 char *newfont, *oldfont;
574 oldfont = WMGetFromPLString(val);
575 newfont = convertFont(oldfont, False);
576 /* newfont is a reference to old if conversion is not needed */
577 if (newfont != oldfont) {
578 WMReleasePropList(val);
579 val = WMCreatePLString(newfont);
580 wfree(newfont);
583 WMPutInPLDictionary(style, key, val);
584 WMReleasePropList(val);
586 WMReleasePropList(key);
589 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
590 if (val)
591 PixmapPath = val;
593 if (theme_too) {
594 for (i = 0; theme_options[i] != NULL; i++) {
595 key = WMCreatePLString(theme_options[i]);
597 val = WMGetFromPLDictionary(prop, key);
598 if (val)
599 WMPutInPLDictionary(style, key, val);
603 if (make_pack) {
604 char *path;
606 makeThemePack(style, style_file);
608 path = wmalloc(strlen(ThemePath) + 32);
609 strcpy(path, ThemePath);
610 strcat(path, "/style");
611 WMWritePropListToFile(style, path, False);
612 wfree(path);
613 } else {
614 if (style_file) {
615 WMWritePropListToFile(style, style_file, False);
616 } else {
617 puts(WMGetPropListDescription(style, True));
620 exit(0);