| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <xpap.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
static XPA rxpa=NULL; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#ifdef ANSI_FUNC |
| 33 |
static char * |
| 34 |
XPACmdParseNames(char *lbuf, int *ntokens) |
| 35 |
#else |
| 36 |
static char *XPACmdParseNames(lbuf, ntokens) |
| 37 |
char *lbuf; |
| 38 |
int *ntokens; |
| 39 |
#endif |
| 40 |
{ |
| 41 |
char tbuf[SZ_LINE]; |
| 42 |
int lp=0; |
| 43 |
char *buf; |
| 44 |
|
| 45 |
|
| 46 |
buf = (char *)xmalloc(strlen(lbuf)+1); |
| 47 |
*buf = '\0'; |
| 48 |
*ntokens = 0; |
| 49 |
|
| 50 |
|
| 51 |
while( word(lbuf, tbuf, &lp)){ |
| 52 |
if( *buf != '\0' ) |
| 53 |
strcat(buf, " "); |
| 54 |
strcat(buf, tbuf); |
| 55 |
*ntokens += 1; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
buf = (char *)xrealloc(buf, strlen(buf)+1); |
| 60 |
return(buf); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#ifdef ANSI_FUNC |
| 75 |
static int |
| 76 |
XPAReceiveNSConnect (void *client_data, void *call_data, char *paramlist, |
| 77 |
char *buf, size_t len) |
| 78 |
#else |
| 79 |
static int XPAReceiveNSConnect(client_data, call_data, paramlist, buf, len) |
| 80 |
void *client_data; |
| 81 |
void *call_data; |
| 82 |
char *paramlist; |
| 83 |
char *buf; |
| 84 |
size_t len; |
| 85 |
#endif |
| 86 |
{ |
| 87 |
XPA xpa = (XPA)call_data; |
| 88 |
XPA txpa; |
| 89 |
char tbuf[SZ_LINE]; |
| 90 |
int doall=0; |
| 91 |
int lp=0; |
| 92 |
|
| 93 |
if( paramlist && *paramlist ){ |
| 94 |
if( word(paramlist, tbuf, &lp) && !strcmp(tbuf, "-all") ){ |
| 95 |
doall = 1; |
| 96 |
} |
| 97 |
} |
| 98 |
if( doall ){ |
| 99 |
for(txpa=XPAListHead(); txpa!=NULL; txpa=txpa->next){ |
| 100 |
XPANSAdd(txpa, NULL, NULL); |
| 101 |
} |
| 102 |
} |
| 103 |
else{ |
| 104 |
XPANSAdd(xpa, NULL, NULL); |
| 105 |
} |
| 106 |
return(0); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#ifdef ANSI_FUNC |
| 121 |
static int |
| 122 |
XPAReceiveNSDisconnect (void *client_data, void *call_data, char *paramlist, |
| 123 |
char *buf, size_t len) |
| 124 |
#else |
| 125 |
static int XPAReceiveNSDisconnect(client_data, call_data, paramlist, buf, len) |
| 126 |
void *client_data; |
| 127 |
void *call_data; |
| 128 |
char *paramlist; |
| 129 |
char *buf; |
| 130 |
size_t len; |
| 131 |
#endif |
| 132 |
{ |
| 133 |
XPA xpa = (XPA)call_data; |
| 134 |
XPA txpa; |
| 135 |
NS ns, tns; |
| 136 |
char tbuf[SZ_LINE]; |
| 137 |
int doall=0; |
| 138 |
int lp=0; |
| 139 |
|
| 140 |
if( paramlist && *paramlist ){ |
| 141 |
if( word(paramlist, tbuf, &lp) && !strcmp(tbuf, "-all") ){ |
| 142 |
doall = 1; |
| 143 |
} |
| 144 |
} |
| 145 |
if( doall ){ |
| 146 |
for(txpa=XPAListHead(); txpa!=NULL; txpa=txpa->next){ |
| 147 |
for(ns=txpa->nshead; ns!= NULL; ){ |
| 148 |
tns = ns->next; |
| 149 |
XPANSClose(txpa, ns); |
| 150 |
ns = tns; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
else{ |
| 155 |
for(ns=xpa->nshead; ns!= NULL; ){ |
| 156 |
tns = ns->next; |
| 157 |
XPANSClose(xpa, ns); |
| 158 |
ns = tns; |
| 159 |
} |
| 160 |
} |
| 161 |
return(0); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#ifdef ANSI_FUNC |
| 176 |
static int |
| 177 |
XPAReceiveEnv (void *client_data, void *call_data, char *paramlist, |
| 178 |
char *buf, size_t len) |
| 179 |
#else |
| 180 |
static int XPAReceiveEnv(client_data, call_data, paramlist, buf, len) |
| 181 |
void *client_data; |
| 182 |
void *call_data; |
| 183 |
char *paramlist; |
| 184 |
char *buf; |
| 185 |
size_t len; |
| 186 |
#endif |
| 187 |
{ |
| 188 |
XPA xpa = (XPA)call_data; |
| 189 |
char name[SZ_LINE]; |
| 190 |
char value[SZ_LINE]; |
| 191 |
char *tbuf; |
| 192 |
int lp=0; |
| 193 |
|
| 194 |
if( word(paramlist, name, &lp) ){ |
| 195 |
if( word(paramlist, value, &lp) ){ |
| 196 |
tbuf = (char *)xmalloc(strlen(name)+1+strlen(value)+1); |
| 197 |
snprintf(tbuf, SZ_LINE, "%s=%s", name, value); |
| 198 |
putenv(tbuf); |
| 199 |
return(0); |
| 200 |
} |
| 201 |
else{ |
| 202 |
if( strchr(name, '=') != NULL ){ |
| 203 |
tbuf = xstrdup(name); |
| 204 |
putenv(tbuf); |
| 205 |
return(0); |
| 206 |
} |
| 207 |
else{ |
| 208 |
XPAError(xpa, "XPA setenv requires name and value pair\n"); |
| 209 |
return(-1); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
else{ |
| 214 |
XPAError(xpa, "XPA setenv requires name and value pair\n"); |
| 215 |
return(-1); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
#ifdef ANSI_FUNC |
| 231 |
static int |
| 232 |
XPASendEnv (void *client_data, void *call_data, char *paramlist, |
| 233 |
char **buf, size_t *len) |
| 234 |
#else |
| 235 |
static int XPASendEnv(client_data, call_data, paramlist, buf, len) |
| 236 |
void *client_data; |
| 237 |
void *call_data; |
| 238 |
char *paramlist; |
| 239 |
char **buf; |
| 240 |
size_t *len; |
| 241 |
#endif |
| 242 |
{ |
| 243 |
int tlen; |
| 244 |
char *tbuf; |
| 245 |
char *ebuf; |
| 246 |
|
| 247 |
if( (ebuf = (char *)getenv(paramlist)) != NULL ){ |
| 248 |
tlen = strlen(ebuf)+2; |
| 249 |
tbuf = (char *)xmalloc(tlen); |
| 250 |
snprintf(tbuf, tlen, "%s\n", ebuf); |
| 251 |
*buf = tbuf; |
| 252 |
*len = strlen(tbuf); |
| 253 |
} |
| 254 |
else{ |
| 255 |
*buf = xstrdup("\n"); |
| 256 |
*len = 1; |
| 257 |
} |
| 258 |
return(0); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#ifdef ANSI_FUNC |
| 273 |
static int |
| 274 |
XPAReceiveReserved (void *client_data, void *call_data, char *paramlist, |
| 275 |
char *buf, size_t len) |
| 276 |
#else |
| 277 |
static int XPAReceiveReserved(client_data, call_data, paramlist, buf, len) |
| 278 |
void *client_data; |
| 279 |
void *call_data; |
| 280 |
char *paramlist; |
| 281 |
char *buf; |
| 282 |
size_t len; |
| 283 |
#endif |
| 284 |
{ |
| 285 |
char *cmd = (char *)client_data; |
| 286 |
XPA xpa = (XPA)call_data; |
| 287 |
|
| 288 |
if( !strcmp(cmd, "end") ){ |
| 289 |
xpa->comm->status |= XPA_STATUS_ENDBUF; |
| 290 |
return(0); |
| 291 |
} |
| 292 |
else if( !strcmp(cmd, "exec") ){ |
| 293 |
xpa->comm->status |= XPA_STATUS_READBUF; |
| 294 |
return(0); |
| 295 |
} |
| 296 |
else{ |
| 297 |
return(-1); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
#ifdef ANSI_FUNC |
| 313 |
static int |
| 314 |
XPASendHelp (void *client_data, void *call_data, char *paramlist, |
| 315 |
char **buf, size_t *len) |
| 316 |
#else |
| 317 |
static int XPASendHelp(client_data, call_data, paramlist, buf, len) |
| 318 |
void *client_data; |
| 319 |
void *call_data; |
| 320 |
char *paramlist; |
| 321 |
char **buf; |
| 322 |
size_t *len; |
| 323 |
#endif |
| 324 |
{ |
| 325 |
XPA xpa = (XPA)call_data; |
| 326 |
XPACmd cmd; |
| 327 |
int lp=0; |
| 328 |
int slen; |
| 329 |
char tbuf[SZ_LINE]; |
| 330 |
char lbuf[SZ_LINE]; |
| 331 |
char *sbuf; |
| 332 |
|
| 333 |
if( !paramlist || !*paramlist ){ |
| 334 |
if( xpa->version != NULL ){ |
| 335 |
snprintf(lbuf, SZ_LINE, "XPA version: %s\n", xpa->version); |
| 336 |
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0); |
| 337 |
} |
| 338 |
} |
| 339 |
if( xpa->commands == NULL ){ |
| 340 |
if( xpa->help != NULL ){ |
| 341 |
slen = strlen(xpa->help)+SZ_LINE; |
| 342 |
sbuf = (char *)xmalloc(slen); |
| 343 |
snprintf(sbuf, slen, "%s\n", xpa->help); |
| 344 |
send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0); |
| 345 |
xfree(sbuf); |
| 346 |
} |
| 347 |
else{ |
| 348 |
strcpy(lbuf, "\n"); |
| 349 |
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0); |
| 350 |
} |
| 351 |
} |
| 352 |
else{ |
| 353 |
if( paramlist && *paramlist ){ |
| 354 |
while( word(paramlist, tbuf, &lp) ){ |
| 355 |
for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){ |
| 356 |
if( !strcmp(tbuf, cmd->name) ){ |
| 357 |
if( cmd->help != NULL ){ |
| 358 |
slen = strlen(cmd->name)+strlen(cmd->help)+SZ_LINE; |
| 359 |
sbuf = (char *)xmalloc(slen); |
| 360 |
snprintf(sbuf, slen, "%s:\t%s\n", cmd->name, cmd->help); |
| 361 |
send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0); |
| 362 |
xfree(sbuf); |
| 363 |
} |
| 364 |
else{ |
| 365 |
snprintf(lbuf, SZ_LINE, "%s:\t(no help available)\n", cmd->name); |
| 366 |
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
else{ |
| 373 |
for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){ |
| 374 |
if( cmd->help != NULL ){ |
| 375 |
slen = strlen(cmd->name)+strlen(cmd->help)+SZ_LINE; |
| 376 |
sbuf = (char *)xmalloc(slen); |
| 377 |
snprintf(sbuf, slen, "%s:\t%s\n", cmd->name, cmd->help); |
| 378 |
send(xpa_datafd(xpa), sbuf, strlen(sbuf), 0); |
| 379 |
xfree(sbuf); |
| 380 |
} |
| 381 |
else{ |
| 382 |
snprintf(lbuf, SZ_LINE, "%s:\t(no help available)\n", cmd->name); |
| 383 |
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
return(0); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
#ifdef ANSI_FUNC |
| 403 |
static int |
| 404 |
XPASendVersion (void *client_data, void *call_data, char *paramlist, |
| 405 |
char **buf, size_t *len) |
| 406 |
#else |
| 407 |
static int XPASendVersion(client_data, call_data, paramlist, buf, len) |
| 408 |
void *client_data; |
| 409 |
void *call_data; |
| 410 |
char *paramlist; |
| 411 |
char **buf; |
| 412 |
size_t *len; |
| 413 |
#endif |
| 414 |
{ |
| 415 |
XPA xpa = (XPA)call_data; |
| 416 |
char lbuf[SZ_LINE]; |
| 417 |
|
| 418 |
if( xpa->version != NULL ) |
| 419 |
snprintf(lbuf, SZ_LINE, "%s\n", xpa->version); |
| 420 |
else |
| 421 |
strcpy(lbuf, "\n"); |
| 422 |
send(xpa_datafd(xpa), lbuf, strlen(lbuf), 0); |
| 423 |
return(0); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
#ifdef ANSI_FUNC |
| 450 |
void |
| 451 |
XPAInitReserved (void) |
| 452 |
#else |
| 453 |
void XPAInitReserved() |
| 454 |
#endif |
| 455 |
{ |
| 456 |
if( !rxpa ){ |
| 457 |
if( (rxpa = (XPA)xcalloc(1, sizeof(struct xparec))) == NULL ) |
| 458 |
return; |
| 459 |
|
| 460 |
rxpa->send_callback = XPASendCommands; |
| 461 |
rxpa->receive_callback = XPAReceiveCommands; |
| 462 |
|
| 463 |
XPACmdAdd(rxpa, "-acl", |
| 464 |
"\tget (set) the access control list\n\t\t options: host type acl", |
| 465 |
XPASendAcl, NULL, NULL, XPAReceiveAcl, NULL, "fillbuf=false"); |
| 466 |
XPACmdAdd(rxpa, "-env", |
| 467 |
"\tget (set) an environment variable\n\t\t options: name (value)", |
| 468 |
XPASendEnv, NULL, NULL, XPAReceiveEnv, NULL, NULL); |
| 469 |
XPACmdAdd(rxpa, "-exec", |
| 470 |
"\texecute commands from buffer\n\t\t options: none", |
| 471 |
NULL, NULL, NULL, XPAReceiveReserved, (void *)"exec", NULL); |
| 472 |
XPACmdAdd(rxpa, "-help", |
| 473 |
"\treturn help string for specified XPA\n\t\t options: cmd name (commands only)", |
| 474 |
XPASendHelp, NULL, NULL, NULL, NULL, NULL); |
| 475 |
XPACmdAdd(rxpa, "-ltimeout", |
| 476 |
"\tget (set) long timeout\n\t\t options: seconds|reset", |
| 477 |
XPASendLTimeout, NULL, NULL, XPAReceiveLTimeout, NULL, NULL); |
| 478 |
XPACmdAdd(rxpa, "-nsconnect", |
| 479 |
"\tre-establish name server connection to this XPA\n\t\t options: -all", |
| 480 |
NULL, NULL, NULL, XPAReceiveNSConnect, NULL, NULL); |
| 481 |
XPACmdAdd(rxpa, "-nsdisconnect", |
| 482 |
"\tbreak name server connection to this XPA\n\t\t options: -all", |
| 483 |
NULL, NULL, NULL, XPAReceiveNSDisconnect, NULL, NULL); |
| 484 |
XPACmdAdd(rxpa, "-remote", |
| 485 |
"\tconnect to remote name service with specified acl \n\t\t options: host:port +|-|acl -proxy", |
| 486 |
XPASendRemote, NULL, NULL, XPAReceiveRemote, NULL, "fillbuf=false"); |
| 487 |
XPACmdAdd(rxpa, "-clipboard", |
| 488 |
"\tset/get clipboard information \n\t\t options: [cmd] name", |
| 489 |
XPASendClipboard, NULL, NULL, XPAReceiveClipboard, NULL, NULL); |
| 490 |
XPACmdAdd(rxpa, "-stimeout", |
| 491 |
"\tget (set) short timeout\n\t\t options: seconds|reset", |
| 492 |
XPASendSTimeout, NULL, NULL, XPAReceiveSTimeout, NULL, NULL); |
| 493 |
XPACmdAdd(rxpa, "-version", |
| 494 |
"\treturn XPA version string\n\t\t options: none", |
| 495 |
XPASendVersion, NULL, NULL, NULL, NULL, NULL); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
#ifdef ANSI_FUNC |
| 500 |
void |
| 501 |
XPAFreeReserved (void) |
| 502 |
#else |
| 503 |
void XPAFreeReserved() |
| 504 |
#endif |
| 505 |
{ |
| 506 |
XPACmd cmd, tcmd; |
| 507 |
if( !rxpa ) return; |
| 508 |
|
| 509 |
for(cmd=rxpa->commands; cmd!=NULL; ){ |
| 510 |
tcmd = cmd->next; |
| 511 |
XPACmdDel(rxpa, cmd); |
| 512 |
cmd = tcmd; |
| 513 |
} |
| 514 |
xfree(rxpa); |
| 515 |
rxpa = NULL; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
#ifdef ANSI_FUNC |
| 530 |
XPACmd |
| 531 |
XPACmdLookupReserved (XPA xpa, char *lbuf, int *lp) |
| 532 |
#else |
| 533 |
XPACmd XPACmdLookupReserved(xpa, lbuf, lp) |
| 534 |
XPA xpa; |
| 535 |
char *lbuf; |
| 536 |
int *lp; |
| 537 |
#endif |
| 538 |
{ |
| 539 |
XPACmd cmd; |
| 540 |
int lp2=0; |
| 541 |
char *lptr; |
| 542 |
char name[SZ_LINE]; |
| 543 |
|
| 544 |
|
| 545 |
if( (rxpa==NULL) || (lbuf==NULL) || (lbuf[*lp]=='\0') ) |
| 546 |
return(NULL); |
| 547 |
|
| 548 |
|
| 549 |
lptr = &(lbuf[*lp]); |
| 550 |
|
| 551 |
|
| 552 |
if( !word(lptr, name, &lp2) ) |
| 553 |
return(NULL); |
| 554 |
|
| 555 |
|
| 556 |
for(cmd=rxpa->commands; cmd!=NULL; cmd=cmd->next){ |
| 557 |
if( !strcmp(name, cmd->name) ){ |
| 558 |
*lp += lp2; |
| 559 |
return(cmd); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
return(NULL); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
#ifdef ANSI_FUNC |
| 579 |
XPACmd |
| 580 |
XPACmdLookup (XPA xpa, char *lbuf, int *lp) |
| 581 |
#else |
| 582 |
XPACmd XPACmdLookup(xpa, lbuf, lp) |
| 583 |
XPA xpa; |
| 584 |
char *lbuf; |
| 585 |
int *lp; |
| 586 |
#endif |
| 587 |
{ |
| 588 |
XPACmd cmd; |
| 589 |
int i; |
| 590 |
int lp2; |
| 591 |
int len, tlen; |
| 592 |
char *lptr; |
| 593 |
char tbuf[SZ_LINE]; |
| 594 |
char name[SZ_LINE]; |
| 595 |
|
| 596 |
|
| 597 |
if( (xpa==NULL) || (lbuf==NULL) || (lbuf[*lp]=='\0') ) |
| 598 |
return(NULL); |
| 599 |
|
| 600 |
|
| 601 |
lptr = &(lbuf[*lp]); |
| 602 |
|
| 603 |
|
| 604 |
for(cmd=xpa->commands; cmd!=NULL; cmd=cmd->next){ |
| 605 |
|
| 606 |
*name = '\0'; |
| 607 |
lp2 = 0; |
| 608 |
tlen = 0; |
| 609 |
for(i=0; i<cmd->ntokens; i++){ |
| 610 |
if( word(lptr, tbuf, &lp2)){ |
| 611 |
len = strlen(tbuf)+1; |
| 612 |
if( (tlen+len) <= (SZ_LINE-1) ){ |
| 613 |
if( *name != '\0' ) |
| 614 |
strcat(name, " "); |
| 615 |
strcat(name, tbuf); |
| 616 |
tlen += len; |
| 617 |
} |
| 618 |
|
| 619 |
else{ |
| 620 |
*name = '\0'; |
| 621 |
break; |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
if( *name && !strcmp(cmd->name, name) ){ |
| 627 |
*lp += lp2; |
| 628 |
return(cmd); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
return(XPACmdLookupReserved(xpa, lbuf, lp)); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
#ifdef ANSI_FUNC |
| 648 |
int |
| 649 |
XPAReceiveCommands (void *client_data, void *call_data, char *paramlist, |
| 650 |
char *buf, size_t len) |
| 651 |
#else |
| 652 |
int XPAReceiveCommands(client_data, call_data, paramlist, buf, len) |
| 653 |
void *client_data; |
| 654 |
void *call_data; |
| 655 |
char *paramlist; |
| 656 |
char *buf; |
| 657 |
size_t len; |
| 658 |
#endif |
| 659 |
{ |
| 660 |
XPA xpa = (XPA)call_data; |
| 661 |
XPACmd cmd; |
| 662 |
int lp; |
| 663 |
int savelp; |
| 664 |
int plen; |
| 665 |
int bgot; |
| 666 |
int got=0; |
| 667 |
int gotbuf=0; |
| 668 |
int freebuf=1; |
| 669 |
char lbuf[SZ_LINE]; |
| 670 |
char tbuf[SZ_LINE]; |
| 671 |
char tbuf1[SZ_LINE]; |
| 672 |
|
| 673 |
|
| 674 |
newdtable(";"); |
| 675 |
|
| 676 |
|
| 677 |
if( buf ) |
| 678 |
gotbuf++; |
| 679 |
|
| 680 |
|
| 681 |
if( (xpa_datafd(xpa) >=0) && (!paramlist || (*paramlist == '\0')) ){ |
| 682 |
xpa->comm->status |= XPA_STATUS_READBUF; |
| 683 |
XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPALongTimeout()); |
| 684 |
FPRINTF((stderr, "%sXPAReceiveCommands: read %s\n", _sp, lbuf)); |
| 685 |
} |
| 686 |
else{ |
| 687 |
xpa->comm->status &= ~XPA_STATUS_READBUF; |
| 688 |
nowhite(paramlist, lbuf); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
if( *lbuf == '\0' ){ |
| 693 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD2]); |
| 694 |
got = -1; |
| 695 |
goto done; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
while( 1 ){ |
| 702 |
FPRINTF((stderr, "%sXPAReceiveCommands: top of loop: %s\n", _sp, lbuf)); |
| 703 |
lp = 0; |
| 704 |
while( lbuf[lp] != '\0' ){ |
| 705 |
if( (cmd = XPACmdLookup(xpa, lbuf, &lp)) == NULL ){ |
| 706 |
XPAError(xpa, xpaMessbuf[XPA_RTN_UNCMD]); |
| 707 |
got = -1; |
| 708 |
goto done; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
if( (cmd->xpa == rxpa) && |
| 713 |
strcmp(cmd->name, "-help") && strcmp(cmd->name, "-version") ){ |
| 714 |
if( XPAMtype() == XPA_INET ){ |
| 715 |
if( (!xpa->comm || !LOCALIP(xpa->comm->cmdip)) ){ |
| 716 |
FPRINTF((stderr, "%sXPAReceiveCommands: rejecting reserved: %s\n", |
| 717 |
_sp, cmd->name)); |
| 718 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOAUTH]); |
| 719 |
got = -1; |
| 720 |
goto done; |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
FPRINTF((stderr, "%sXPAReceiveCommands: cmd->name: %s\n", |
| 725 |
_sp, cmd->name)); |
| 726 |
*tbuf = '\0'; |
| 727 |
if( (lastdelim() != ';') && (lastdelim() != '\n') ){ |
| 728 |
|
| 729 |
while( isspace((int)lbuf[lp]) ) |
| 730 |
lp++; |
| 731 |
|
| 732 |
savelp = lp; |
| 733 |
|
| 734 |
while( word(lbuf, tbuf1, &lp) ){ |
| 735 |
if( (lastdelim() == ';') || (lastdelim() == '\n') ){ |
| 736 |
break; |
| 737 |
} |
| 738 |
|
| 739 |
while( isspace((int)lbuf[lp]) ) |
| 740 |
lp++; |
| 741 |
if( (lbuf[lp] == ';') || (lbuf[lp] == '\n') ){ |
| 742 |
break; |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
plen = lp - savelp; |
| 747 |
|
| 748 |
if( (plen>0) && ((lastdelim() == ';')||(lastdelim() == '\n')) ) |
| 749 |
plen--; |
| 750 |
|
| 751 |
if( plen > 0 ){ |
| 752 |
strncpy(tbuf, &(lbuf[savelp]), plen); |
| 753 |
tbuf[plen] = '\0'; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
if( cmd->receive_callback != NULL ){ |
| 758 |
|
| 759 |
if( !gotbuf && (xpa_datafd(xpa) >= 0) && |
| 760 |
(cmd->receive_mode & XPA_MODE_FILLBUF) ){ |
| 761 |
|
| 762 |
FPRINTF((stderr, "%sXPAReceiveCommands: at XPAGetBuf\n", _sp)); |
| 763 |
bgot = XPAGetBuf(xpa, xpa_datafd(xpa), &buf, &len, -1); |
| 764 |
|
| 765 |
XPACloseData(xpa, xpa->comm); |
| 766 |
if( bgot >= 0 ){ |
| 767 |
|
| 768 |
gotbuf++; |
| 769 |
} |
| 770 |
|
| 771 |
else{ |
| 772 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NODATA]); |
| 773 |
got = -1; |
| 774 |
goto done; |
| 775 |
} |
| 776 |
} |
| 777 |
got = (cmd->receive_callback)(cmd->receive_data, call_data, |
| 778 |
tbuf, buf, len); |
| 779 |
|
| 780 |
if( (buf != NULL) && !(cmd->receive_mode & XPA_MODE_FREEBUF) ){ |
| 781 |
freebuf=0; |
| 782 |
} |
| 783 |
if( got != 0 ){ |
| 784 |
goto done; |
| 785 |
} |
| 786 |
} |
| 787 |
else{ |
| 788 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOREC]); |
| 789 |
got = -1; |
| 790 |
goto done; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
if( xpa->comm->status & XPA_STATUS_ENDBUF ) |
| 796 |
break; |
| 797 |
|
| 798 |
if( !(xpa->comm->status & XPA_STATUS_READBUF) ) |
| 799 |
break; |
| 800 |
|
| 801 |
if( XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPALongTimeout()) <=0 ) |
| 802 |
break; |
| 803 |
} |
| 804 |
|
| 805 |
done: |
| 806 |
|
| 807 |
if( freebuf ) |
| 808 |
xfree(buf); |
| 809 |
|
| 810 |
freedtable(); |
| 811 |
|
| 812 |
return(got); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
#ifdef ANSI_FUNC |
| 827 |
int |
| 828 |
XPASendCommands (void *client_data, void *call_data, char *paramlist, |
| 829 |
char **buf, size_t *len) |
| 830 |
#else |
| 831 |
int XPASendCommands(client_data, call_data, paramlist, buf, len) |
| 832 |
void *client_data; |
| 833 |
void *call_data; |
| 834 |
char *paramlist; |
| 835 |
char **buf; |
| 836 |
size_t *len; |
| 837 |
#endif |
| 838 |
{ |
| 839 |
XPA xpa = (XPA)call_data; |
| 840 |
XPACmd cmd; |
| 841 |
char tbuf[SZ_LINE]; |
| 842 |
int lp=0; |
| 843 |
int got=0; |
| 844 |
|
| 845 |
|
| 846 |
if( *paramlist == '\0' ){ |
| 847 |
paramlist = "-help"; |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
if( (cmd = XPACmdLookup(xpa, paramlist, &lp)) != NULL ){ |
| 852 |
|
| 853 |
|
| 854 |
if( (cmd->xpa == rxpa) && |
| 855 |
strcmp(cmd->name, "-help") && strcmp(cmd->name, "-version") ){ |
| 856 |
if( XPAMtype() == XPA_INET ){ |
| 857 |
if( (!xpa->comm || !LOCALIP(xpa->comm->cmdip)) ){ |
| 858 |
FPRINTF((stderr, "%sXPAReceiveCommands: rejecting reserved: %s\n", |
| 859 |
_sp, cmd->name)); |
| 860 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOAUTH]); |
| 861 |
got = -1; |
| 862 |
goto done; |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
strcpy(tbuf, ¶mlist[lp]); |
| 869 |
nocr(tbuf); |
| 870 |
if( !strcmp(tbuf, "-help") ){ |
| 871 |
if( cmd->help != NULL ) |
| 872 |
snprintf(tbuf, SZ_LINE, "%s\n", cmd->help); |
| 873 |
else |
| 874 |
snprintf(tbuf, SZ_LINE, "\n"); |
| 875 |
send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0); |
| 876 |
got = 0; |
| 877 |
} |
| 878 |
else if( cmd->send_callback != NULL ){ |
| 879 |
got = (cmd->send_callback)(cmd->send_data, call_data, |
| 880 |
¶mlist[lp], buf, len); |
| 881 |
} |
| 882 |
else{ |
| 883 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOSEND]); |
| 884 |
got = -1; |
| 885 |
goto done; |
| 886 |
} |
| 887 |
} |
| 888 |
else{ |
| 889 |
XPAError(xpa, xpaMessbuf[XPA_RTN_UNCMD]); |
| 890 |
got = -1; |
| 891 |
goto done; |
| 892 |
} |
| 893 |
|
| 894 |
done: |
| 895 |
|
| 896 |
return(got); |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
#ifdef ANSI_FUNC |
| 921 |
XPA |
| 922 |
XPACmdNew (char *xclass, char *name) |
| 923 |
#else |
| 924 |
XPA XPACmdNew(xclass, name) |
| 925 |
char *xclass; |
| 926 |
char *name; |
| 927 |
#endif |
| 928 |
{ |
| 929 |
XPA xpa; |
| 930 |
char tbuf[SZ_LINE]; |
| 931 |
|
| 932 |
|
| 933 |
if( (name == NULL) || (*name == '\0') ) |
| 934 |
return(NULL); |
| 935 |
|
| 936 |
|
| 937 |
if( (xclass == NULL) || (*xclass == '\0') ) |
| 938 |
xclass = "*"; |
| 939 |
|
| 940 |
|
| 941 |
snprintf(tbuf, SZ_LINE, "XPA commands for %s:%s\n\t\t options: see -help", |
| 942 |
xclass, name); |
| 943 |
|
| 944 |
|
| 945 |
xpa = XPANew(xclass, name, tbuf, |
| 946 |
XPASendCommands, NULL, NULL, |
| 947 |
XPAReceiveCommands, NULL, "fillbuf=false"); |
| 948 |
|
| 949 |
|
| 950 |
return(xpa); |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
#ifdef ANSI_FUNC |
| 965 |
XPACmd |
| 966 |
XPACmdAdd (XPA xpa, char *name, char *help, |
| 967 |
SendCb send_callback, void *send_data, char *send_mode, |
| 968 |
ReceiveCb rec_callback, void *rec_data, char *rec_mode) |
| 969 |
#else |
| 970 |
XPACmd XPACmdAdd(xpa, name, help, |
| 971 |
send_callback, send_data, send_mode, |
| 972 |
rec_callback, rec_data, rec_mode) |
| 973 |
XPA xpa; |
| 974 |
char *name; |
| 975 |
char *help; |
| 976 |
SendCb send_callback; |
| 977 |
void *send_data; |
| 978 |
char *send_mode; |
| 979 |
ReceiveCb rec_callback; |
| 980 |
void *rec_data; |
| 981 |
char *rec_mode; |
| 982 |
#endif |
| 983 |
{ |
| 984 |
XPACmd xnew; |
| 985 |
XPACmd cur; |
| 986 |
XPACmd prev; |
| 987 |
|
| 988 |
|
| 989 |
if( !xpa || |
| 990 |
(xpa->send_callback != XPASendCommands) || |
| 991 |
(xpa->receive_callback != XPAReceiveCommands) ){ |
| 992 |
return(NULL); |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
if( (send_callback == NULL) && (rec_callback == NULL ) ){ |
| 997 |
return(NULL); |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
if( strlen(name) > XPA_NAMELEN ){ |
| 1002 |
return(NULL); |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
xnew = (XPACmd)xcalloc(1, sizeof(struct xpacmdrec)); |
| 1007 |
|
| 1008 |
|
| 1009 |
xnew->xpa = xpa; |
| 1010 |
|
| 1011 |
|
| 1012 |
xnew->name = XPACmdParseNames(name, &(xnew->ntokens)); |
| 1013 |
xnew->help = xstrdup(help); |
| 1014 |
|
| 1015 |
|
| 1016 |
xnew->send_callback = send_callback; |
| 1017 |
xnew->send_data = send_data; |
| 1018 |
xnew->send_mode = XPA_DEF_MODE_SEND; |
| 1019 |
XPAMode(send_mode, &(xnew->send_mode), "freebuf", XPA_MODE_FREEBUF,1); |
| 1020 |
|
| 1021 |
XPAMode(send_mode, &(xpa->send_mode), "acl", XPA_MODE_ACL, 1); |
| 1022 |
|
| 1023 |
|
| 1024 |
xnew->receive_callback = rec_callback; |
| 1025 |
xnew->receive_data = rec_data; |
| 1026 |
|
| 1027 |
xnew->receive_mode = XPA_DEF_MODE_REC; |
| 1028 |
XPAMode(rec_mode, &(xnew->receive_mode), "usebuf", XPA_MODE_BUF,1); |
| 1029 |
XPAMode(rec_mode, &(xnew->receive_mode), "fillbuf", XPA_MODE_FILLBUF,1); |
| 1030 |
XPAMode(rec_mode, &(xnew->receive_mode), "freebuf", XPA_MODE_FREEBUF,1); |
| 1031 |
|
| 1032 |
XPAMode(rec_mode, &(xpa->receive_mode), "acl", XPA_MODE_ACL, 1); |
| 1033 |
|
| 1034 |
|
| 1035 |
if( xpa->commands == NULL ){ |
| 1036 |
xpa->commands = xnew; |
| 1037 |
return(xnew); |
| 1038 |
} |
| 1039 |
else{ |
| 1040 |
for(prev=NULL, cur=xpa->commands; cur!=NULL; prev=cur, cur=cur->next){ |
| 1041 |
|
| 1042 |
if( strcmp(xnew->name, cur->name) ==0 ) |
| 1043 |
goto addname; |
| 1044 |
|
| 1045 |
else if( !strncmp(xnew->name, cur->name, strlen(cur->name)) ) |
| 1046 |
goto addname; |
| 1047 |
|
| 1048 |
else if( !strncmp(xnew->name, cur->name, strlen(xnew->name)) ) |
| 1049 |
continue; |
| 1050 |
|
| 1051 |
else if( strcmp(xnew->name, cur->name) >0 ) |
| 1052 |
continue; |
| 1053 |
addname: |
| 1054 |
|
| 1055 |
if( prev == NULL ){ |
| 1056 |
xpa->commands = xnew; |
| 1057 |
xnew->next = cur; |
| 1058 |
return(xnew); |
| 1059 |
} |
| 1060 |
else{ |
| 1061 |
prev->next = xnew; |
| 1062 |
xnew->next = cur; |
| 1063 |
return(xnew); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
prev->next = xnew; |
| 1069 |
return(xnew); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
#ifdef ANSI_FUNC |
| 1085 |
int |
| 1086 |
XPACmdDel (XPA xpa, XPACmd cmd) |
| 1087 |
#else |
| 1088 |
int XPACmdDel(xpa, cmd) |
| 1089 |
XPA xpa; |
| 1090 |
XPACmd cmd; |
| 1091 |
#endif |
| 1092 |
{ |
| 1093 |
XPACmd cur; |
| 1094 |
int got=0; |
| 1095 |
|
| 1096 |
|
| 1097 |
if( cmd == NULL ) |
| 1098 |
return(-1); |
| 1099 |
|
| 1100 |
|
| 1101 |
if( xpa && xpa->commands ){ |
| 1102 |
if( xpa->commands == cmd ){ |
| 1103 |
xpa->commands = cmd->next; |
| 1104 |
got++; |
| 1105 |
} |
| 1106 |
else{ |
| 1107 |
for(cur=xpa->commands; cur!=NULL; cur=cur->next){ |
| 1108 |
if( cur->next == cmd ){ |
| 1109 |
cur->next = cmd->next; |
| 1110 |
got++; |
| 1111 |
break; |
| 1112 |
} |
| 1113 |
} |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
if( got ){ |
| 1119 |
if( cmd->name ) |
| 1120 |
xfree(cmd->name); |
| 1121 |
if( cmd->help ) |
| 1122 |
xfree(cmd->help); |
| 1123 |
xfree(cmd); |
| 1124 |
return(0); |
| 1125 |
} |
| 1126 |
else{ |
| 1127 |
return(-1); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
#ifdef ANSI_FUNC |
| 1143 |
int |
| 1144 |
XPACmdInternalReceive (void *client_data, void *call_data, |
| 1145 |
char *paramlist, char *buf, size_t len) |
| 1146 |
#else |
| 1147 |
int XPACmdInternalReceive(client_data, call_data, paramlist, buf, len) |
| 1148 |
void *client_data; |
| 1149 |
void *call_data; |
| 1150 |
char *paramlist; |
| 1151 |
char *buf; |
| 1152 |
size_t len; |
| 1153 |
#endif |
| 1154 |
{ |
| 1155 |
XPA xpa = (XPA)call_data; |
| 1156 |
|
| 1157 |
|
| 1158 |
if( xpa == NULL ) |
| 1159 |
return(-1); |
| 1160 |
|
| 1161 |
return(XPAReceiveCommands(client_data, xpa, paramlist, buf, len)); |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
#ifdef ANSI_FUNC |
| 1176 |
int |
| 1177 |
XPACmdInternalSend (void *client_data, void *call_data, |
| 1178 |
char *paramlist, char **buf, size_t *len) |
| 1179 |
#else |
| 1180 |
int XPACmdInternalSend(client_data, call_data, paramlist, buf, len) |
| 1181 |
void *client_data; |
| 1182 |
void *call_data; |
| 1183 |
char *paramlist; |
| 1184 |
char **buf; |
| 1185 |
size_t *len; |
| 1186 |
#endif |
| 1187 |
{ |
| 1188 |
XPA xpa = (XPA)call_data; |
| 1189 |
|
| 1190 |
|
| 1191 |
if( xpa == NULL ) |
| 1192 |
return(-1); |
| 1193 |
|
| 1194 |
|
| 1195 |
return(XPASendCommands(client_data, xpa, paramlist, buf, len)); |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
#ifdef ANSI_FUNC |
| 1210 |
XPA |
| 1211 |
XPAGetReserved (void) |
| 1212 |
#else |
| 1213 |
XPA XPAGetReserved() |
| 1214 |
#endif |
| 1215 |
{ |
| 1216 |
return(rxpa); |
| 1217 |
} |