Add the -xrm command-line option, to allow specification of an
[sgt/putty] / unix / uxstore.c
1 /*
2 * uxstore.c: Unix-specific implementation of the interface defined
3 * in storage.h.
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9 #include <gtk/gtk.h>
10 #include <gdk/gdkx.h>
11 #include <X11/Xlib.h>
12 #include "putty.h"
13 #include "storage.h"
14 #include "tree234.h"
15
16 /*
17 * For the moment, the only existing Unix utility is pterm and that
18 * has no GUI configuration at all, so our write routines need do
19 * nothing. Eventually I suppose these will read and write an rc
20 * file somewhere or other.
21 */
22
23 void *open_settings_w(char *sessionname)
24 {
25 return NULL;
26 }
27
28 void write_setting_s(void *handle, char *key, char *value)
29 {
30 }
31
32 void write_setting_i(void *handle, char *key, int value)
33 {
34 }
35
36 void close_settings_w(void *handle)
37 {
38 }
39
40 /*
41 * Reading settings, for the moment, is done by retrieving X
42 * resources from the X display. When we introduce disk files, I
43 * think what will happen is that the X resources will override
44 * PuTTY's inbuilt defaults, but that the disk files will then
45 * override those. This isn't optimal, but it's the best I can
46 * immediately work out.
47 */
48
49 static Display *display;
50
51 struct xrm_string {
52 char *key;
53 char *value;
54 };
55
56 static tree234 *xrmtree = NULL;
57
58 int xrmcmp(void *av, void *bv)
59 {
60 struct xrm_string *a = (struct xrm_string *)av;
61 struct xrm_string *b = (struct xrm_string *)bv;
62 return strcmp(a->key, b->key);
63 }
64
65 void provide_xrm_string(char *string)
66 {
67 char *p, *q;
68 struct xrm_string *xrms, *ret;
69
70 p = q = strchr(string, ':');
71 if (!q) {
72 fprintf(stderr, "pterm: expected a colon in resource string"
73 " \"%s\"\n", string);
74 return;
75 }
76 q++;
77 while (p > string && p[-1] != '.' && p[-1] != '*')
78 p--;
79 xrms = smalloc(sizeof(struct xrm_string));
80 xrms->key = smalloc(q-p);
81 memcpy(xrms->key, p, q-p);
82 xrms->key[q-p-1] = '\0';
83 while (*q && isspace(*q))
84 q++;
85 xrms->value = dupstr(q);
86
87 if (!xrmtree)
88 xrmtree = newtree234(xrmcmp);
89
90 ret = add234(xrmtree, xrms);
91 if (ret) {
92 /* Override an existing string. */
93 del234(xrmtree, ret);
94 add234(xrmtree, xrms);
95 }
96 }
97
98 char *get_setting(char *key)
99 {
100 struct xrm_string tmp, *ret;
101 tmp.key = key;
102 if (xrmtree) {
103 ret = find234(xrmtree, &tmp, NULL);
104 if (ret)
105 return ret->value;
106 }
107 return XGetDefault(display, app_name, key);
108 }
109
110 void *open_settings_r(char *sessionname)
111 {
112 static int thing_to_return_an_arbitrary_non_null_pointer_to;
113 display = GDK_DISPLAY();
114 if (!display)
115 return NULL;
116 else
117 return &thing_to_return_an_arbitrary_non_null_pointer_to;
118 }
119
120 char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
121 {
122 char *val = get_setting(key);
123 if (!val)
124 return NULL;
125 else {
126 strncpy(buffer, val, buflen);
127 buffer[buflen-1] = '\0';
128 return buffer;
129 }
130 }
131
132 int read_setting_i(void *handle, char *key, int defvalue)
133 {
134 char *val = get_setting(key);
135 if (!val)
136 return defvalue;
137 else
138 return atoi(val);
139 }
140
141 void close_settings_r(void *handle)
142 {
143 }
144
145 void del_settings(char *sessionname)
146 {
147 }
148
149 void *enum_settings_start(void)
150 {
151 return NULL;
152 }
153
154 char *enum_settings_next(void *handle, char *buffer, int buflen)
155 {
156 return NULL;
157 }
158
159 void enum_settings_finish(void *handle)
160 {
161 }
162
163 int verify_host_key(char *hostname, int port, char *keytype, char *key)
164 {
165 return 1; /* key does not exist in registry */
166 }
167
168 void store_host_key(char *hostname, int port, char *keytype, char *key)
169 {
170 }
171
172 void read_random_seed(noise_consumer_t consumer)
173 {
174 }
175
176 void write_random_seed(void *data, int len)
177 {
178 }
179
180 void cleanup_all(void)
181 {
182 }