| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <xpap.h> |
| 11 |
|
| 12 |
#define MAX_XPAS 10 |
| 13 |
|
| 14 |
extern char *optarg; |
| 15 |
extern int optind; |
| 16 |
|
| 17 |
#define BUFSIZE 1000000 |
| 18 |
|
| 19 |
char xbuf[BUFSIZE+1]; |
| 20 |
size_t xlen; |
| 21 |
int quiet=0; |
| 22 |
|
| 23 |
#ifdef ANSI_FUNC |
| 24 |
int send_cb (void *client_data, void *call_data, char *paramlist, |
| 25 |
char **buf, size_t *len) |
| 26 |
#else |
| 27 |
int send_cb(client_data, call_data, paramlist, buf, len) |
| 28 |
void *client_data; |
| 29 |
void *call_data; |
| 30 |
char *paramlist; |
| 31 |
char **buf; |
| 32 |
size_t *len; |
| 33 |
#endif |
| 34 |
{ |
| 35 |
XPA xpa = (XPA)call_data; |
| 36 |
|
| 37 |
if( !xpa ) return(-1); |
| 38 |
*len = xlen; |
| 39 |
*buf = (char *)xmalloc(*len); |
| 40 |
memcpy(*buf, xbuf, *len); |
| 41 |
if( !quiet ) fprintf(stderr, "sent (%s): %lu\n", |
| 42 |
paramlist, (unsigned long)xlen); |
| 43 |
return(0); |
| 44 |
} |
| 45 |
|
| 46 |
#ifdef ANSI_FUNC |
| 47 |
int receive_cb (void *client_data, void *call_data, char *paramlist, |
| 48 |
char *buf, size_t len) |
| 49 |
#else |
| 50 |
int receive_cb(client_data, call_data, paramlist, buf, len) |
| 51 |
void *client_data; |
| 52 |
void *call_data; |
| 53 |
char *paramlist; |
| 54 |
char *buf; |
| 55 |
size_t len; |
| 56 |
#endif |
| 57 |
{ |
| 58 |
XPA xpa = (XPA)call_data; |
| 59 |
|
| 60 |
if( !xpa ) return(-1); |
| 61 |
if( !quiet ) fprintf(stderr, "recd (%s): %s\n", paramlist, buf); |
| 62 |
return(0); |
| 63 |
} |
| 64 |
|
| 65 |
#ifdef ANSI_FUNC |
| 66 |
int |
| 67 |
main (int argc, char **argv) |
| 68 |
#else |
| 69 |
main(argc, argv) |
| 70 |
int argc; |
| 71 |
char **argv; |
| 72 |
#endif |
| 73 |
{ |
| 74 |
int c; |
| 75 |
int i, j; |
| 76 |
int got; |
| 77 |
int delay=-1; |
| 78 |
int total=0; |
| 79 |
int dobig=0; |
| 80 |
int dofd=0; |
| 81 |
int doexit=0; |
| 82 |
int poll=1; |
| 83 |
int msec=100; |
| 84 |
int verbose=0; |
| 85 |
size_t lens[MAX_XPAS]; |
| 86 |
char *bufs[MAX_XPAS]; |
| 87 |
char *names[MAX_XPAS]; |
| 88 |
char *errs[MAX_XPAS]; |
| 89 |
char *xmode=""; |
| 90 |
char xname[SZ_LINE]; |
| 91 |
char xclass[SZ_LINE]; |
| 92 |
char paramlist[SZ_LINE]; |
| 93 |
|
| 94 |
int xfds[1]; |
| 95 |
XPA xpa; |
| 96 |
|
| 97 |
|
| 98 |
putenv("XPA_TIMESTAMP_ERRORS=true"); |
| 99 |
|
| 100 |
|
| 101 |
while ((c = getopt(argc, argv, "bd:fm:pqv")) != -1){ |
| 102 |
switch(c){ |
| 103 |
case 'b': |
| 104 |
dobig = 1; |
| 105 |
break; |
| 106 |
case 'd': |
| 107 |
delay = atoi(optarg); |
| 108 |
break; |
| 109 |
case 'f': |
| 110 |
dofd = 1; |
| 111 |
break; |
| 112 |
case 'm': |
| 113 |
msec = atoi(optarg); |
| 114 |
break; |
| 115 |
case 'p': |
| 116 |
poll = 0; |
| 117 |
break; |
| 118 |
case 'q': |
| 119 |
quiet = 1; |
| 120 |
break; |
| 121 |
case 'v': |
| 122 |
verbose = 1; |
| 123 |
break; |
| 124 |
default: |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if( optind >= argc ){ |
| 130 |
fprintf(stderr, "usage: %s [xpa] [target1 target2 ...]\n", argv[0]); |
| 131 |
exit(1); |
| 132 |
} |
| 133 |
strcpy(xname, argv[optind++]); |
| 134 |
strcpy(xclass, xname); |
| 135 |
cluc(xclass); |
| 136 |
|
| 137 |
if( (xpa = XPANew(xclass, xname, "help is on the way", |
| 138 |
send_cb, (void *)xname, xmode, |
| 139 |
receive_cb, (void *)xname, xmode)) ){ |
| 140 |
fprintf(stdout, "%s using method: %s\n", xpa_name(xpa), xpa_method(xpa)); |
| 141 |
} |
| 142 |
else{ |
| 143 |
fprintf(stderr, "ERROR: could not init xpa\n"); |
| 144 |
exit(1); |
| 145 |
} |
| 146 |
|
| 147 |
if( dobig ){ |
| 148 |
for(i=0; i<BUFSIZE; i++){ |
| 149 |
xbuf[i] = 'a' + (i%26); |
| 150 |
} |
| 151 |
} |
| 152 |
else{ |
| 153 |
snprintf(xbuf, SZ_LINE, |
| 154 |
"%s:%s %s (%s);", |
| 155 |
xpa_class(xpa), xpa_name(xpa), xpa_method(xpa), xpa_sendian(xpa)); |
| 156 |
} |
| 157 |
xlen = strlen(xbuf); |
| 158 |
xfds[0] = fileno(stderr); |
| 159 |
|
| 160 |
fprintf(stdout, "\nEntering select loop ...\n"); |
| 161 |
while( 1 ){ |
| 162 |
if( poll ){ |
| 163 |
fprintf(stderr, "*"); |
| 164 |
XPAPoll(msec, 1); |
| 165 |
} |
| 166 |
else{ |
| 167 |
fprintf(stderr, "."); |
| 168 |
} |
| 169 |
for(doexit=0, j=optind; j<argc; j++){ |
| 170 |
snprintf(paramlist, SZ_LINE, "#%d", total++); |
| 171 |
if( total && ((total % 1000) == 0) ) fprintf(stderr, "%d", total); |
| 172 |
if( dofd ){ |
| 173 |
got = XPAGetFd(NULL, argv[j], paramlist, NULL, xfds, names, errs, |
| 174 |
-MAX_XPAS); |
| 175 |
for(i=0; i<got; i++){ |
| 176 |
if( errs[i] ){ |
| 177 |
fprintf(stderr, "ERROR in %s:\n%s", names[i], errs[i]); |
| 178 |
fprintf(stderr, "Exiting after error\n"); |
| 179 |
doexit = 1; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
else{ |
| 184 |
got = XPAGet(NULL, argv[j], paramlist, NULL, bufs, lens, names, errs, |
| 185 |
MAX_XPAS); |
| 186 |
for(i=0; i<got; i++){ |
| 187 |
if( errs[i] ){ |
| 188 |
fprintf(stderr, "ERROR in %s:\n%s", names[i], errs[i]); |
| 189 |
fprintf(stderr, "Exiting after error\n"); |
| 190 |
doexit = 1; |
| 191 |
} |
| 192 |
else{ |
| 193 |
if( !quiet ) fprintf(stderr, "%s", bufs[i]); |
| 194 |
if( bufs[i] ) xfree(bufs[i]); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
for(i=0; i<got; i++){ |
| 199 |
if( names[i] ) xfree(names[i]); |
| 200 |
if( errs[i] ) xfree(errs[i]); |
| 201 |
} |
| 202 |
if( doexit ) exit(1); |
| 203 |
XPASet(NULL, argv[j], paramlist, NULL, xbuf, xlen, names, errs, |
| 204 |
MAX_XPAS); |
| 205 |
for(i=0; i<got; i++){ |
| 206 |
if( errs[i] ){ |
| 207 |
fprintf(stderr, "ERROR in %s:\n%s", names[i], errs[i]); |
| 208 |
fprintf(stderr, "Exiting after error\n"); |
| 209 |
doexit = 1; |
| 210 |
} |
| 211 |
} |
| 212 |
for(i=0; i<got; i++){ |
| 213 |
if( names[i] ) xfree(names[i]); |
| 214 |
if( errs[i] ) xfree(errs[i]); |
| 215 |
} |
| 216 |
if( doexit ) goto done; |
| 217 |
if( delay > 0 ) XPASleep(delay); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
done: |
| 222 |
XPAFree(xpa); |
| 223 |
exit(0); |
| 224 |
} |