Debian Codegraph @

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