| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <xpap.h> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 80 |
*port = 0; |
| 81 |
|
| 82 |
|
| 83 |
if( word(lbuf, tbuf, &lp) ){ |
| 84 |
XPAParseName(tbuf, xclass, name, len); |
| 85 |
} |
| 86 |
else{ |
| 87 |
return(-1); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 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 |
|
| 102 |
return(0); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 140 |
if( (xnew = (PORT)xcalloc(1, sizeof(struct portrec))) == NULL ) |
| 141 |
goto error; |
| 142 |
|
| 143 |
|
| 144 |
if( XPAPortParse(lbuf, xclass, name, &port, SZ_LINE) < 0 ) |
| 145 |
goto error; |
| 146 |
|
| 147 |
|
| 148 |
xnew->xclass = xstrdup(xclass); |
| 149 |
xnew->name = xstrdup(name); |
| 150 |
xnew->port = port; |
| 151 |
|
| 152 |
|
| 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 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 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 |
|
| 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 |
|
| 209 |
if( port->xclass ) xfree(port->xclass); |
| 210 |
if( port->name ) xfree(port->name); |
| 211 |
|
| 212 |
|
| 213 |
xfree((char *)port); |
| 214 |
return(0); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 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 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 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 |
|
| 275 |
if( flag == 0 ) |
| 276 |
XPAPortFree(); |
| 277 |
|
| 278 |
|
| 279 |
if( aname && *aname ) |
| 280 |
portname = aname; |
| 281 |
else if( (portname=(char *)getenv("XPA_PORTFILE")) == NULL ) |
| 282 |
portname = XPA_PORTFILE; |
| 283 |
|
| 284 |
|
| 285 |
portstr=(char *)getenv("XPA_PORT"); |
| 286 |
|
| 287 |
|
| 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 |
|
| 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 |
|
| 313 |
return(got); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 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 |
} |