Debian Codegraph @

xpa/xpainfo.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 ack from server\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, "Info will be sent to 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> xpainfo IMAGE ds9 image\n");
38 fprintf(stderr, "\n(version: %s)\n", XPA_VERSION);
39 exit(1);
40 }
41
42 #ifdef ANSI_FUNC
43 int
44 main (int argc, char **argv)
45 #else
46 int
47 main(argc, argv)
48 int argc;
49 char **argv;
50 #endif
51 {
52 int i;
53 int c;
54 int got;
55 int lp;
56 int errcode=0;
57 int server=0;
58 int maxhosts=XPA_MAXHOSTS;
59 char *paramlist=NULL;
60 char *xtemplate=NULL;
61 char lbuf[SZ_LINE];
62 char tbuf[SZ_LINE];
63 char mode[SZ_LINE];
64 char cmd[SZ_LINE];
65 char *s;
66 char **errs=NULL;
67 char **names=NULL;
68 XPA xpa=NULL;
69
70 /* display version and exit, if necessary */
71 if( (argc == 2) && !strcmp(argv[1], "--version") ){
72 fprintf(stderr, "%s\n", XPA_VERSION);
73 exit(0);
74 }
75
76 /* make sure we have enough arguments */
77 if( argc < 2 )
78 usage(argv[0]);
79
80 /* start with no mode flag */
81 *mode = '\0';
82
83 /* we want the args in the same order in which they arrived, and
84 gnu getopt sometimes changes things without this */
85 putenv("POSIXLY_CORRECT=true");
86
87 /* process switch arguments */
88 while ((c = getopt(argc, argv, "hi:m:nst:u:wW")) != -1){
89 switch(c){
90 case 'h':
91 usage(argv[0]);
92 case 'i':
93 snprintf(cmd, SZ_LINE, "XPA_NSINET=%s", optarg);
94 putenv(xstrdup(cmd));
95 break;
96 case 'm':
97 snprintf(cmd, SZ_LINE, "XPA_METHOD=%s", optarg);
98 putenv(xstrdup(cmd));
99 break;
100 case 'n':
101 if( *mode != '\0' )
102 strcat(mode, ",");
103 strcat(mode, "ack=false");
104 break;
105 case 's':
106 server = 1;
107 xpa = XPAOpen(NULL);
108 break;
109 case 't':
110 {
111 int xip=0;
112 char xbuf[SZ_LINE];
113 newdtable(",;");
114 if( word(optarg, xbuf, &xip) && *xbuf && (*xbuf != '*') ){
115 snprintf(cmd, SZ_LINE, "XPA_SHORT_TIMEOUT=%s", xbuf);
116 putenv(xstrdup(cmd));
117 }
118 if( word(optarg, xbuf, &xip) && *xbuf && (*xbuf != '*') ){
119 snprintf(cmd, SZ_LINE, "XPA_LONG_TIMEOUT=%s", xbuf);
120 putenv(xstrdup(cmd));
121 }
122 freedtable();
123 }
124 break;
125 case 'u':
126 snprintf(cmd, SZ_LINE, "XPA_NSUSERS=%s", optarg);
127 putenv(xstrdup(cmd));
128 break;
129 /* for backward compatibility with 1.0 */
130 case 'w':
131 case 'W':
132 break;
133 }
134 }
135
136 /* no explicit host:port specified, so we should have a name */
137 if( optind >= argc ){
138 /* in server mode, we can skip the host on the command line */
139 if( !server )
140 usage(argv[0]);
141 }
142 else{
143 xtemplate = xstrdup(argv[optind]);
144 optind++;
145 }
146
147 /* make the paramlist */
148 paramlist = XPAArgvParamlist(argc, argv, optind);
149
150 /* init variables for names and ports */
151 if( (s=(char *)getenv("XPA_MAXHOSTS")) != NULL )
152 maxhosts = atoi(s);
153 names = (char **)xcalloc(maxhosts, sizeof(char *));
154 errs = (char **)xcalloc(maxhosts, sizeof(char *));
155
156 again:
157 /* if we are in server mode, we might have to read a line from stdin
158 to grab the template and paramlist */
159 if( server && (xtemplate==NULL) ){
160 /* read description of template and paramlist */
161 if( fgets(lbuf, SZ_LINE, stdin) == NULL )
162 exit(errcode);
163 if( (*lbuf == '#') || (*lbuf == '\n') )
164 goto again;
165 lp = 0;
166 if( word(lbuf, tbuf, &lp) && !strcmp(tbuf, "xpaget") &&
167 word(lbuf, tbuf, &lp) ){
168 xtemplate = xstrdup(tbuf);
169 paramlist = xstrdup(&(lbuf[lp]));
170 nowhite(paramlist, paramlist);
171 }
172 else{
173 fprintf(stderr, "XPA$ERROR invalid command: %s", lbuf);
174 exit(++errcode);
175 }
176 }
177
178 /* process xpa requests */
179 got = XPAInfo(xpa, xtemplate, paramlist, mode, names, errs, -maxhosts);
180 if( got == 0 ){
181 fprintf(stderr, "XPA$ERROR no 'xpainfo' access points match template: %s\n",
182 xtemplate);
183 errcode++;
184 }
185 else{
186 /* display errors and free up strings */
187 for(i=0; i<got; i++){
188 if( errs[i] != NULL ){
189 fprintf(stderr, "%s", errs[i]);
190 xfree(errs[i]);
191 errcode++;
192 }
193 if( names[i] != NULL )
194 xfree(names[i]);
195 }
196 }
197
198 /* free the paramlist */
199 if( paramlist!= NULL ){
200 xfree(paramlist);
201 paramlist = NULL;
202 }
203 /* and the template */
204 if( xtemplate != NULL ){
205 xfree(xtemplate);
206 xtemplate = NULL;
207 }
208
209 /* if we are in server mode, go back for more */
210 if( server )
211 goto again;
212 else{
213 /* clean up */
214 if( errs ) xfree(errs);
215 if( names ) xfree(names);
216 exit(errcode);
217 }
218 return(0);
219 }