Debian Codegraph @

xpa/clipboard.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 /*
6 *
7 * remote.c -- xpa access control list management
8 *
9 */
10
11 #include <xpap.h>
12
13 /*
14 *----------------------------------------------------------------------------
15 *
16 *
17 * Private Routines
18 *
19 *
20 *----------------------------------------------------------------------------
21 */
22
23 #ifdef ANSI_FUNC
24 static XPAClip
25 ClipBoardNew(XPA xpa, char *name)
26 #else
27 static XPAClip ClipBoardNew(xpa, name)
28 XPA xpa;
29 char *name;
30 #endif
31 {
32 XPAClip cur, xnew;
33 if( (xnew = (XPAClip)xcalloc(1, sizeof(XPAClipRec))) == NULL )
34 return(NULL);
35
36 /* fill in record structure */
37 xnew->name = xstrdup(name);
38 xnew->ip = xpa->comm->cmdip;
39
40 /* add to the end of list */
41 if( xpa->cliphead == NULL ){
42 xpa->cliphead = xnew;
43 }
44 else{
45 for(cur=xpa->cliphead; cur->next!=NULL; cur=cur->next){
46 ;
47 }
48 cur->next = xnew;
49 }
50 return xnew;
51 }
52
53 #ifdef ANSI_FUNC
54 static XPAClip
55 ClipBoardLookup(XPA xpa, char *name)
56 #else
57 static XPAClip ClipBoardLookup(xpa, name)
58 XPA xpa;
59 char *name;
60 #endif
61 {
62 XPAClip cur;
63
64 /* look for reserved keywords that have callbacks */
65 for(cur=xpa->cliphead; cur!=NULL; cur=cur->next){
66 if( !strcmp(name, cur->name) && (xpa->comm->cmdip == cur->ip) ){
67 return(cur);
68 }
69 }
70 return NULL;
71 }
72
73 #ifdef ANSI_FUNC
74 static int
75 ClipBoardAdd(XPA xpa, char *name, char *paramlist, char *buf)
76 #else
77 static int
78 ClipBoardAdd(xpa, name, paramlist, buf)
79 XPA xpa;
80 char *name;
81 char *paramlist;
82 char *buf;
83 #endif
84 {
85 XPAClip cur;
86 if( !(cur = ClipBoardLookup(xpa, name)) )
87 cur = ClipBoardNew(xpa, name);
88 if( !cur )
89 return -1;
90 if( cur->value )
91 xfree(cur->value);
92 cur->value = xstrdup(buf);
93 return 0;
94 }
95
96 #ifdef ANSI_FUNC
97 static int
98 ClipBoardAppend(XPA xpa, char *name, char *paramlist, char *buf)
99 #else
100 static int
101 ClipBoardAppend(xpa, name, paramlist, buf)
102 XPA xpa;
103 char *name;
104 char *paramlist;
105 char *buf;
106 #endif
107 {
108 XPAClip cur;
109 if( !(cur = ClipBoardLookup(xpa, name)) )
110 cur = ClipBoardNew(xpa, name);
111 if( !cur )
112 return -1;
113 if( cur->value ){
114 if( (cur->value = (char *)xrealloc(cur->value,
115 strlen(cur->value)+strlen(buf)+1)) )
116 strcat(cur->value, buf);
117 else
118 return -1;
119 }
120 else{
121 cur->value = xstrdup(buf);
122 }
123 return 0;
124 }
125
126 #ifdef ANSI_FUNC
127 static int
128 ClipBoardDelete(XPA xpa, char *name, char *paramlist)
129 #else
130 static int
131 ClipBoardDelete(xpa, name, paramlist)
132 XPA xpa;
133 char *name;
134 char *paramlist;
135 #endif
136 {
137 XPAClip cur;
138 if( (cur = ClipBoardLookup(xpa, name)) ){
139 ClipBoardFree(xpa, cur);
140 return 0;
141 }
142 else
143 return -1;
144 }
145
146 /*
147 *----------------------------------------------------------------------------
148 *
149 *
150 * Semi-Public Routines (used by xpa.c and command.c)
151 *
152 *
153 *----------------------------------------------------------------------------
154 */
155
156 #ifdef ANSI_FUNC
157 int
158 ClipBoardFree(XPA xpa, XPAClip clip)
159 #else
160 int ClipBoardFree(xpa, clip)
161 XPA xpa;
162 XPAClip clip;
163 #endif
164 {
165 XPAClip cur;
166
167 if( !clip )
168 return 0;
169 /* remove from list */
170 if( xpa->cliphead ){
171 if( xpa->cliphead == clip ){
172 xpa->cliphead = clip->next;
173 }
174 else{
175 for(cur=xpa->cliphead; cur!=NULL; cur=cur->next){
176 if( cur->next == clip ){
177 cur->next = clip->next;
178 break;
179 }
180 }
181 }
182 }
183 if( clip->name ) xfree(clip->name);
184 if( clip->value ) xfree(clip->value);
185 xfree(clip);
186 return 1;
187 }
188
189 /*
190 *----------------------------------------------------------------------------
191 *
192 * Routine: XPAReceiveClipboard
193 *
194 * Purpose: add a new clipboard entry
195 *
196 * Returns: xpa callback error codes
197 *
198 *----------------------------------------------------------------------------
199 */
200 #ifdef ANSI_FUNC
201 int
202 XPAReceiveClipboard (void *client_data, void *call_data, char *paramlist,
203 char *buf, size_t len)
204 #else
205 int XPAReceiveClipboard(client_data, call_data, paramlist, buf, len)
206 void *client_data;
207 void *call_data;
208 char *paramlist;
209 char *buf;
210 size_t len;
211 #endif
212 {
213 XPA xpa = (XPA)call_data;
214 char cmd[SZ_LINE];
215 char name[SZ_LINE];
216 char tbuf[SZ_LINE];
217 int lp=0;
218 int status = -1;
219
220 *cmd = '\0';
221 *name = '\0';
222 if( paramlist && *paramlist ){
223 if( !word(paramlist, cmd, &lp) || !word(paramlist, name, &lp) )
224 goto done;
225 /* lookup the command */
226 if( !strcmp(cmd, "add") ){
227 status = ClipBoardAdd(xpa, name, &paramlist[lp], buf);
228 }
229 else if( !strncmp(cmd, "app", 3) ){
230 status = ClipBoardAppend(xpa, name, &paramlist[lp], buf);
231 }
232 else if( !strncmp(cmd, "del", 3) ){
233 status = ClipBoardDelete(xpa, name, &paramlist[lp]);
234 }
235 #ifdef LATER
236 else if( !strncmp(cmd, "loa", 3) ){
237 status = ClipBoardLoad(xpa, name, &paramlist[lp], buf);
238 }
239 else if( !strncmp(cmd, "sav", 3) ){
240 status = ClipBoardSave(xpa, name, &paramlist[lp]);
241 }
242 #endif
243 }
244
245 done:
246 if( status < 0 ){
247 if( !*cmd || !*name ){
248 XPAError(xpa, "XPA clipboard requires: add|append|delete name\n");
249 }
250 else{
251 snprintf(tbuf, SZ_LINE,
252 "XPA clipboard invalid cmd or name: %s %s\n", cmd, name);
253 XPAError(xpa, tbuf);
254 }
255 }
256 return(status);
257 }
258
259 /*
260 *----------------------------------------------------------------------------
261 *
262 * Routine: XPASendClipboard
263 *
264 * Purpose: return clipboard information
265 *
266 * Returns: 0 for success, -1 for failure
267 *
268 *----------------------------------------------------------------------------
269 */
270 #ifdef ANSI_FUNC
271 int
272 XPASendClipboard (void *client_data, void *call_data, char *paramlist,
273 char **buf, size_t *len)
274 #else
275 int XPASendClipboard(client_data, call_data, paramlist, buf, len)
276 void *client_data;
277 void *call_data;
278 char *paramlist;
279 char **buf;
280 size_t *len;
281 #endif
282 {
283 XPA xpa = (XPA)call_data;
284 char name[SZ_LINE];
285 char tbuf[SZ_LINE];
286 int lp=0;
287 int status = -1;
288 XPAClip cur;
289
290 *name = '\0';
291 if( paramlist && *paramlist ){
292 if( !word(paramlist, name, &lp) )
293 goto done;
294 if( !(cur = ClipBoardLookup(xpa, name)) )
295 goto done;
296 if( cur->value ){
297 send(xpa_datafd(xpa), cur->value, strlen(cur->value), 0);
298 status = 0;
299 }
300 }
301
302 done:
303 if( status < 0 ){
304 if( !*name ){
305 XPAError(xpa, "XPA clipboard requires: name\n");
306 }
307 else{
308 snprintf(tbuf, SZ_LINE, "XPA clipboard invalid name: %s\n", name);
309 XPAError(xpa, tbuf);
310 }
311 }
312 return(status);
313 }
314
315 /*
316 *----------------------------------------------------------------------------
317 *
318 *
319 * Public Routines
320 *
321 *
322 *----------------------------------------------------------------------------
323 */
324