]> asedeno.scripts.mit.edu Git - pssh.git/blob - data/prefs.c
Greg Parker's 2005-06-23 release.
[pssh.git] / data / prefs.c
1 /**********
2  * Copyright (c) 2004 Greg Parker.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY GREG PARKER ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  **********/
24
25 #include "includes.h"
26 #include "prefs.h"
27
28
29 // There is one global default prefs list. 
30 // There is also a set of override values for each connection, mostly 
31 // for terminal settings.
32
33 // Each preference is a RESOURCE
34 // res type: 'dflt' or some ID for a connection override
35 // res id: which pref
36
37 /* 
38    interface: 
39    echo password (NO)
40    keyboard pane(s)
41    
42    terminal:
43    TERM type ("xterm")
44    scrollback line count (500?)
45    scroll down on input (NO)
46    scroll down on keypress (YES)
47    
48    ssh:
49    compression (NO) (fixme implement)
50    ciphers ("aes128-cbc,3des-cbc")
51    authmethod (fixme implement)
52    
53    putty terminal:
54    wrap_mode (YES==autowrap)
55    dec_om "DEC Origin Mode" (YES)
56    lfhascr "Implicit CR in every LF" (NO)
57    bce "Background Color Erase" (?)
58    blink_text "Blink" (NO)
59    answerback "Answerback to ^E" ("pssh")
60    localecho (NO)
61    localedit (NO)
62    printing (NO)
63
64    putty keyboard:
65    bksp_is_delete "Backspace is DEL" (YES==^? rather than ^H)
66    rxvt_homeend "rxvt Home and End" (NO)
67    funky_type "Function Keys" (DIGITAL, Linux, xterm, VT400, VT100+, SCO)
68    app_cursor "Application Cursor Keys" (NO?)
69    app_keypad "Application Keypad" (NO?)
70    nethack "NetHack Keypad" (NO)
71    compose (NO) (use real compose key on keyboard?)
72    ctrlalt (NO)
73    
74    lots more putty...
75    
76 */
77
78
79 #define noRecord ((UInt16)0xffff)
80
81
82 static DmOpenRef PrefsDB = 0;
83 // fixme implement overrides
84
85 extern DmOpenRef OpenDB(UInt32 type, char *name, Boolean resDB, Boolean create);
86
87 int PrefsInit(void)
88 {
89     PrefsDB = OpenDB('Pref', "pssh Preferences", true, true);
90     return (PrefsDB != 0);
91 }
92
93
94 static MemHandle PrefsGetHandle(uint32_t which, Boolean fallback)
95 {
96     UInt16 index;
97
98     // fixme search override here unless fallback==false
99
100     // search default prefs
101     index = DmFindResource(PrefsDB, 'dflt', which, NULL);
102     if (index == noRecord) return NULL;
103
104     return DmGetResourceIndex(PrefsDB, index);
105 }
106
107
108 uint32_t PrefsGetInt(uint32_t which, uint32_t defaultValue)
109 {
110     uint32_t result;
111     uint32_t *p;
112     MemHandle h = PrefsGetHandle(which, true);
113     if (!h) return defaultValue;
114     if (MemHandleSize(h) != 4) return defaultValue;
115     
116     p = MemHandleLock(h);
117     result = *p;
118     MemHandleUnlock(h);
119     DmReleaseResource(h);
120
121     return result;
122 }
123
124
125 void PrefsPutInt(uint32_t which, uint32_t value)
126 {
127     uint32_t *p;
128     MemHandle h = PrefsGetHandle(which, false);
129     if (h  &&  MemHandleSize(h) == sizeof(value)) {
130         // resource exists, and it's the right size
131         p = MemHandleLock(h);
132         DmWrite(p, 0, &value, sizeof(value));
133         MemHandleUnlock(h);
134         DmReleaseResource(h);
135         return;
136     }
137     else if (h) {
138         // resource exists, but its the wrong size
139         h = DmResizeResource(h, sizeof(value));
140         p = MemHandleLock(h);
141         DmWrite(p, 0, &value, sizeof(value));
142         MemHandleUnlock(h);
143         DmReleaseResource(h);
144     }
145     else {
146         // resource does not exist
147         h = DmNewResource(PrefsDB, 'dflt', which, sizeof(value));
148         p = MemHandleLock(h);
149         DmWrite(p, 0, &value, sizeof(value));
150         MemHandleUnlock(h);
151         DmReleaseResource(h);
152     }
153 }
154
155
156 char *PrefsGetString(uint32_t which, char *defaultValue)
157 {
158     char *result;
159     char *p;
160     size_t size;
161     MemHandle h = PrefsGetHandle(which, true);
162     if (!h) return arena_strdup(defaultValue);
163     size = MemHandleSize(h);
164     
165     p = MemHandleLock(h);
166     result = arena_malloc(size + 1); // be paranoid about nul terminator
167     memcpy(result, p, size);
168     result[size] = '\0';
169     MemHandleUnlock(h);
170     DmReleaseResource(h);
171
172     return result;
173 }
174
175
176 void PrefsPutString(uint32_t which, char *value)
177 {
178     char *p;
179     size_t sizeNeeded = strlen(value)+1;
180     MemHandle h = PrefsGetHandle(which, false);
181     if (h  &&  MemHandleSize(h) >= sizeNeeded) {
182         // resource exists, and it's big enough
183         p = MemHandleLock(h);
184         DmWrite(p, 0, value, sizeNeeded);
185         MemHandleUnlock(h);
186         DmReleaseResource(h);
187         return;
188     }
189     else if (h) {
190         // resource exists, but it's the wrong size
191         h = DmResizeResource(h, sizeNeeded);
192         p = MemHandleLock(h);
193         DmWrite(p, 0, value, sizeNeeded);
194         MemHandleUnlock(h);
195         DmReleaseResource(h);        
196     }
197     else {
198         // resource does not exist
199         h = DmNewResource(PrefsDB, 'dflt', which, sizeNeeded);
200         p = MemHandleLock(h);
201         DmWrite(p, 0, value, sizeNeeded);
202         MemHandleUnlock(h);
203         DmReleaseResource(h);
204     }
205 }
206