Debian Codegraph @

xpa/xpaget.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 extern char *optarg;
8 extern int optind;
9
10 #ifdef ANSI_FUNC
11 void
12 usage (char *s)
13 #else
14 void usage(s)
15 char *s;
16 #endif
17 {
18 fprintf(stderr, "\n");
19 fprintf(stderr, "usage:\n");
20 fprintf(stderr, " %s [-h] [-i nsinet] [-m method] [-n] [-s] [-t sval,lval] [-u users] <tmpl|host:port> [paramlist]\n", s);
21 fprintf(stderr, "\n");
22 fprintf(stderr, "switches:\n");
23 fprintf(stderr, "\t-h\tprint this message\n");
24 fprintf(stderr, "\t-i\toverride XPA_NSINET environment variable\n");
25 fprintf(stderr, "\t-m\toverride XPA_METHOD environment variable\n");
26 fprintf(stderr, "\t-n\tdon't wait for message after server completes (kind of useless!)\n");
27 fprintf(stderr, "\t-s\tserver mode\n");
28 fprintf(stderr, "\t-t \toverride XPA_[SHORT,LONG]_TIMEOUT environment variables\n");
29 fprintf(stderr, "\t-u\toverride XPA_NSUSERS environment variable\n");
30 fprintf(stderr, "\t--version display version and exit\n");
31 fprintf(stderr, "\n");
32 fprintf(stderr, "Data will be retrieved from access points matching the template or host:port.\n");
33 fprintf(stderr, "Templates are of the form [class:]name. Wildcards *,?,[] are accepted.\n");
34 fprintf(stderr, "A set of qualifying parameters can be appended.\n");
35 fprintf(stderr, "\n");
36 fprintf(stderr, "examples:\n");
37 fprintf(stderr, "\tcsh> xpaget ds9 file\n");
38 fprintf(stderr, "\tcsh> xpaget bynars.harvard.edu:1147\n");
39 fprintf(stderr, "\n(version: %s)\n", XPA_VERSION);
40 exit(1);
41 }
42
43 #ifdef ANSI_FUNC
44 int
45 main (int argc, char **argv)
46 #else
47 int
48 main(argc, argv)
49 int argc;
50 char **argv;
51 #endif
52 {
53 int i;
54 int c;
55 int got;
56 int lp;
57 int errcode=0;
58 int server=0;
59 int maxhosts=XPA_MAXHOSTS;
60 char *paramlist=NULL;
61 char *xtemplate=NULL;
62 char lbuf[SZ_LINE];
63 char tbuf[SZ_LINE];
64 char mode[SZ_LINE];
65 char cmd[SZ_LINE];
66 char *s;
67 char **errs=NULL;
68 char **names=NULL;
69 int fds[1];
70 XPA xpa=NULL;
71
72 /* display version and exit, if necessary */
73 if( (argc == 2) && !strcmp(argv[1], "--version") ){
74 fprintf(stderr, "%s\n", XPA_VERSION);
75 exit(0);
76 }
77
78 /* make sure we have enough arguments */
79 if( argc < 2 )
80 usage(argv[0]);
81
82 /* start with no mode flag */
83 *mode = '\0';
84
85 /* we want the args in the same order in which they arrived, and
86 gnu getopt sometimes changes things without this */
87 putenv("POSIXLY_CORRECT=true");
88
89 /* process switch arguments */
90 while ((c = getopt(argc, argv, "hi:m:nst:u:wW")) != -1){
91 switch(c){
92 case 'h':
93 usage(argv[0]);
94 case 'i':
95 snprintf(cmd, SZ_LINE, "XPA_NSINET=%s", optarg);
96 putenv(xstrdup(cmd));
97 break;
98 case 'm':
99 snprintf(cmd, SZ_LINE, "XPA_METHOD=%s", optarg);
100 putenv(xstrdup(cmd));
101 break;
102 case 'n':
103 if( *mode != '\0' )
104 strcat(mode, ",");
105 strcat(mode, "ack=false");
106 break;
107 case 's':
108 server = 1;
109 xpa = XPAOpen(NULL);
110 break;
111 case 't':
112 {
113 int xip=0;
114 char xbuf[SZ_LINE];
115 newdtable(",;");
116 if( word(optarg, xbuf, &xip) && *xbuf && (*xbuf != '*') ){
117 snprintf(cmd, SZ_LINE, "XPA_SHORT_TIMEOUT=%s", xbuf);
118 putenv(xstrdup(cmd));
119 }
120 if( word(optarg, xbuf, &xip) && *xbuf && (*xbuf != '*') ){
121 snprintf(cmd, SZ_LINE, "XPA_LONG_TIMEOUT=%s", xbuf);
122 putenv(xstrdup(cmd));
123 }
124 freedtable();
125 }
126 break;
127 case 'u':
128 snprintf(cmd, SZ_LINE, "XPA_NSUSERS=%s", optarg);
129 putenv(xstrdup(cmd));
130 break;
131 /* for backward compatibility with 1.0 */
132 case 'w':
133 case 'W':
134 break;
135 }
136 }
137
138 /* no explicit host:port specified, so we should have a name */
139 if( optind >= argc ){
140 /* in server mode, we can skip the host on the command line */
141 if( !server )
142 usage(argv[0]);
143 }
144 else{
145 xtemplate = xstrdup(argv[optind]);
146 optind++;
147 }
148
149 /* make the paramlist */
150 paramlist = XPAArgvParamlist(argc, argv, optind);
151
152 /* init variables for names and ports */
153 if( (s=(char *)getenv("XPA_MAXHOSTS")) != NULL )
154 maxhosts = atoi(s);
155 names = (char **)xcalloc(maxhosts, sizeof(char *));
156 errs = (char **)xcalloc(maxhosts, sizeof(char *));
157
158 /* we only send to stdout */
159 fds[0] = fileno(stdout);
160
161 again:
162 /* if we are in server mode, we might have to read a line from stdin
163 to grab the template and paramlist */
164 if( server && (xtemplate==NULL) ){
165 /* read description of template and paramlist */
166 if( fgets(lbuf, SZ_LINE, stdin) == NULL )
167 exit(errcode);
168 if( (*lbuf == '#') || (*lbuf == '\n') )
169 goto again;
170 lp = 0;
171 if( word(lbuf, tbuf, &lp) && !strcmp(tbuf, "xpaget") &&
172 word(lbuf, tbuf, &lp) ){
173 xtemplate = xstrdup(tbuf);
174 paramlist = xstrdup(&(lbuf[lp]));
175 nowhite(paramlist, paramlist);
176 }
177 else{
178 fprintf(stderr, "XPA$ERROR invalid command: %s", lbuf);
179 exit(++errcode);
180 }
181 }
182
183 /* process xpa requests */
184 got = XPAGetFd(xpa, xtemplate, paramlist, mode, fds, names, errs, -maxhosts);
185 if( got == 0 ){
186 fprintf(stderr, "XPA$ERROR no 'xpaget' access points match template: %s\n",
187 xtemplate);
188 errcode++;
189 }
190 else{
191 /* display errors and free up strings */
192 for(i=0; i<got; i++){
193 if( errs[i] != NULL ){
194 fprintf(stderr, "%s", errs[i]);
195 xfree(errs[i]);
196 errcode++;
197 }
198 if( names[i] != NULL )
199 xfree(names[i]);
200 }
201 }
202
203 /* free the paramlist */
204 if( paramlist!= NULL ){
205 xfree(paramlist);
206 paramlist = NULL;
207 }
208 /* and the template */
209 if( xtemplate != NULL ){
210 xfree(xtemplate);
211 xtemplate = NULL;
212 }
213
214 /* if we are in server mode, go back for more */
215 if( server )
216 goto again;
217 else{
218 /* clean up */
219 if( errs ) xfree(errs);
220 if( names ) xfree(names);
221 XPACleanup();
222 exit(errcode);
223 }
224 return(0);
225 }