Debian Codegraph @

cextern/xpa/port.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 /*
6 *
7 * port.c -- xpa port management
8 *
9 */
10
11 #include <xpap.h>
12
13 /*
14 *----------------------------------------------------------------------------
15 *
16 *
17 * Private Routines
18 *
19 *
20 *----------------------------------------------------------------------------
21 */
22
23 /* this is the head of the global list -- too lazy to do anything more */
24 static PORT porthead=NULL;
25
26 #ifdef ANSI_FUNC
27 static PORT
28 XPAPortLookup (char *xclass, char *name)
29 #else
30 static PORT XPAPortLookup(xclass, name)
31 char *xclass;
32 char *name;
33 #endif
34 {
35 PORT cur;
36 /* look for exact match */
37 for(cur=porthead; cur!=NULL; cur=cur->next){
38 if( !strcmp(xclass, cur->xclass) &&
39 !strcmp(name, cur->name) ){
40 return(cur);
41 }
42 }
43 /* otherwise look for a template match */
44 for(cur=porthead; cur!=NULL; cur=cur->next){
45 if( tmatch(xclass, cur->xclass) &&
46 tmatch(name, cur->name) ){
47 return(cur);
48 }
49 }
50 return(NULL);
51 }
52
53 /*
54 *----------------------------------------------------------------------------
55 *
56 * Routine: XPAPortParse
57 *
58 * Purpose: parse port list into components
59 *
60 * Returns: 0 on success, -1 on failure
61 *
62 *----------------------------------------------------------------------------
63 */
64 #ifdef ANSI_FUNC
65 static int
66 XPAPortParse (char *lbuf, char *xclass, char *name, int *port, int len)
67 #else
68 static int XPAPortParse(lbuf, xclass, name, port, len)
69 char *lbuf;
70 char *xclass;
71 char *name;
72 int *port;
73 int len;
74 #endif
75 {
76 char tbuf[SZ_LINE];
77 int lp=0;
78
79 /* init port values */
80 *port = 0;
81
82 /* class:name is required */
83 if( word(lbuf, tbuf, &lp) ){
84 XPAParseName(tbuf, xclass, name, len);
85 }
86 else{
87 return(-1);
88 }
89
90 /* port is required but can be "*" for default port */
91 if( word(lbuf, tbuf, &lp) ){
92 if( !strcmp(tbuf, "*") )
93 *port = XPA_DEFPORT;
94 else
95 *port = atoi(tbuf);
96 }
97 else{
98 return(-1);
99 }
100
101 /* made it */
102 return(0);
103 }
104
105 /*----------------------------------------------------------------------------
106 *
107 *
108 * Public Routines
109 *
110 *
111 *----------------------------------------------------------------------------
112 */
113
114 /*
115 *----------------------------------------------------------------------------
116 *
117 * Routine: XPAPortAdd
118 *
119 * Purpose: add one port entry to the xpa port list
120 *
121 * Returns: 0 on success, -1 on failure
122 *
123 *----------------------------------------------------------------------------
124 */
125 #ifdef ANSI_FUNC
126 int
127 XPAPortAdd (char *lbuf)
128 #else
129 int XPAPortAdd(lbuf)
130 char *lbuf;
131 #endif
132 {
133 PORT xnew;
134 PORT cur;
135 char xclass[SZ_LINE];
136 char name[SZ_LINE];
137 int port;
138
139 /* allocate port struct */
140 if( (xnew = (PORT)xcalloc(1, sizeof(struct portrec))) == NULL )
141 goto error;
142
143 /* parse info from line buffer */
144 if( XPAPortParse(lbuf, xclass, name, &port, SZ_LINE) < 0 )
145 goto error;
146
147 /* fill in the blanks */
148 xnew->xclass = xstrdup(xclass);
149 xnew->name = xstrdup(name);
150 xnew->port = port;
151
152 /* add this port to end of list of port's */
153 if( porthead == NULL ){
154 porthead = xnew;
155 }
156 else{
157 for(cur=porthead; cur->next!=NULL; cur=cur->next)
158 ;
159 cur->next = xnew;
160 }
161 return(0);
162
163 error:
164 if( xnew )
165 xfree(xnew);
166 return(-1);
167 }
168
169 /*
170 *---------------------------------------------------------------------------
171 *
172 * Routine: XPAPortDel
173 *
174 * Purpose: free up alloc'ed memory in the port record structure
175 *
176 * Results: 0 on success, -1 for failure
177 *
178 *---------------------------------------------------------------------------
179 */
180 #ifdef ANSI_FUNC
181 int
182 XPAPortDel (PORT port)
183 #else
184 int XPAPortDel(port)
185 PORT port;
186 #endif
187 {
188 PORT cur;
189
190 if( port == NULL )
191 return(-1);
192
193 /* remove from list of port's */
194 if( porthead ){
195 if( porthead == port ){
196 porthead = porthead->next;
197 }
198 else{
199 for(cur=porthead; cur!=NULL; cur=cur->next){
200 if( cur->next == port ){
201 cur->next = (cur->next)->next;
202 break;
203 }
204 }
205 }
206 }
207
208 /* free up string space */
209 if( port->xclass ) xfree(port->xclass);
210 if( port->name ) xfree(port->name);
211
212 /* free up record struct */
213 xfree((char *)port);
214 return(0);
215 }
216
217 /*
218 *----------------------------------------------------------------------------
219 *
220 * Routine: XPAPortFree
221 *
222 * Purpose:
223 *
224 * Results: 1 on success, 0 for failure
225 *
226 *----------------------------------------------------------------------------
227 */
228 #ifdef ANSI_FUNC
229 void
230 XPAPortFree (void)
231 #else
232 void XPAPortFree()
233 #endif
234 {
235 PORT cur;
236 PORT saveport;
237
238 for(cur=porthead; cur!=NULL; ){
239 saveport = cur->next;
240 XPAPortDel(cur);
241 cur = saveport;
242 }
243 }
244
245 /*
246 *----------------------------------------------------------------------------
247 *
248 * Routine: XPAPortNew
249 *
250 * Purpose: read or re-read the port list
251 *
252 * Results: number of lines in list (including default)
253 *
254 *----------------------------------------------------------------------------
255 */
256 #ifdef ANSI_FUNC
257 int
258 XPAPortNew (char *aname, int flag)
259 #else
260 int XPAPortNew(aname, flag)
261 char *aname;
262 int flag;
263 #endif
264 {
265 int got=0;
266 char lbuf[SZ_LINE];
267 char *s;
268 char *portname=NULL;
269 char *portpath=NULL;
270 char *portstr=NULL;
271 char *portcopy=NULL;
272 FILE *fp;
273
274 /* if there is an old list, free it */
275 if( flag == 0 )
276 XPAPortFree();
277
278 /* get port file name */
279 if( aname && *aname )
280 portname = aname;
281 else if( (portname=(char *)getenv("XPA_PORTFILE")) == NULL )
282 portname = XPA_PORTFILE;
283
284 /* get the default port */
285 portstr=(char *)getenv("XPA_PORT");
286
287 /* add the port assignments from environment first */
288 if( portstr && *portstr ){
289 portcopy=(char *)xstrdup(portstr);
290 for(s=(char *)strtok(portcopy,";"); s!=NULL; s=(char *)strtok(NULL,";")){
291 if( XPAPortAdd(s) == 0 )
292 got++;
293 }
294 if( portcopy) xfree(portcopy);
295 }
296
297 /* add the port assignments from file next */
298 if( (portpath=(char *)Access(portname, "r")) != NULL ){
299 if( (fp=fopen(portpath, "r")) != NULL ){
300 while( fgets(lbuf, SZ_LINE, fp) ){
301 if( *lbuf == '#' ){
302 continue;
303 }
304 if( XPAPortAdd(lbuf) == 0 )
305 got++;
306 }
307 fclose(fp);
308 }
309 xfree(portpath);
310 }
311
312 /* return the news */
313 return(got);
314 }
315
316 /*
317 *----------------------------------------------------------------------------
318 *
319 * Routine: XPAPort
320 *
321 * Purpose: check for pre-defined port for a given class, name and
322 * 1 for com port, 2 for data port
323 *
324 * Results: assigned port or 0
325 *
326 *----------------------------------------------------------------------------
327 */
328 #ifdef ANSI_FUNC
329 int
330 XPAPort (XPA xpa)
331 #else
332 int XPAPort(xpa)
333 XPA xpa;
334 #endif
335 {
336 int p=0;
337 PORT cur;
338
339 if( xpa == NULL )
340 return 0;
341 if( (cur = XPAPortLookup(xpa->xclass, xpa->name)) ){
342 p = cur->port;
343 }
344 return p;
345 }