Convert to getopt, continued
[wmaker-crm.git] / util / seticons.c
blob84ea73171256b4fd98df00f42863b9c6e1bf6ade
1 /* seticons.c - sets icon configuration in WindowMaker
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 "seticons (Window Maker) 0.1"
25 #ifdef __GLIBC__
26 #define _GNU_SOURCE /* getopt_long */
27 #endif
29 #include <getopt.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include <WINGs/WUtil.h>
36 #include "../src/wconfig.h"
38 extern char *__progname;
40 void print_help(int print_usage, int exitval)
42 printf("Usage: %s [-h] [-v] [file]\n", __progname);
43 if (print_usage) {
44 puts("Reads icon configuration from FILE and updates Window Maker.");
45 puts("");
46 puts(" -h, --help display this help and exit");
47 puts(" -v, --version output version information and exit");
49 exit(exitval);
52 int main(int argc, char **argv)
54 WMPropList *window_name, *icon_key, *window_attrs, *icon_value;
55 WMPropList *all_windows, *iconset, *keylist;
56 int i, ch;
57 char *path = NULL;
59 struct option longopts[] = {
60 { "version", no_argument, NULL, 'v' },
61 { "help", no_argument, NULL, 'h' },
62 { NULL, 0, NULL, 0 }
65 while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
66 switch(ch) {
67 case 'v':
68 puts(PROG_VERSION);
69 return 0;
70 /* NOTREACHED */
71 case 'h':
72 print_help(1, 0);
73 /* NOTREACHED */
74 case 0:
75 break;
76 default:
77 print_help(0, 1);
78 /* NOTREACHED */
81 argc -= optind;
82 argv += optind;
84 if (argc != 1)
85 print_help(0, 1);
87 path = wdefaultspathfordomain("WMWindowAttributes");
89 all_windows = WMReadPropListFromFile(path);
90 if (!all_windows) {
91 printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
92 return 1;
95 iconset = WMReadPropListFromFile(argv[0]);
96 if (!iconset) {
97 printf("%s: could not load icon set file \"%s\".\n", __progname, argv[0]);
98 return 1;
101 keylist = WMGetPLDictionaryKeys(iconset);
102 icon_key = WMCreatePLString("Icon");
104 for (i = 0; i < WMGetPropListItemCount(keylist); i++) {
105 window_name = WMGetFromPLArray(keylist, i);
106 if (!WMIsPLString(window_name))
107 continue;
109 icon_value = WMGetFromPLDictionary(iconset, window_name);
110 if (!icon_value || !WMIsPLDictionary(icon_value))
111 continue;
113 window_attrs = WMGetFromPLDictionary(all_windows, window_name);
114 if (window_attrs) {
115 if (WMIsPLDictionary(window_attrs)) {
116 WMMergePLDictionaries(window_attrs, icon_value, True);
118 } else {
119 WMPutInPLDictionary(all_windows, window_name, icon_value);
123 WMWritePropListToFile(all_windows, path);
125 return 0;