Debian Codegraph @

xpa/xpaaccess.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 #define MAX_XPAS 10000
8
9 extern char *optarg;
10 extern int optind;
11
12 #ifdef ANSI_FUNC
13 void
14 usage (char *s)
15 #else
16 void usage(s)
17 char *s;
18 #endif
19 {
20 fprintf(stderr, "\n");
21 fprintf(stderr, "usage:\n");
22 fprintf(stderr, " %s [-c] [-h] [-i nsinet] [-m method] [-n] [-t sval,lval] [-u users] [-v|-V] <template> [type]\n", s);
23 fprintf(stderr, "\n");
24 fprintf(stderr, "switches:\n");
25 fprintf(stderr, "\t-c\tcontact each access point individually to see if it is available\n");
26 fprintf(stderr, "\t-h\tprint this message\n");
27 fprintf(stderr, "\t-i\toverride XPA_NSINET environment variable\n");
28 fprintf(stderr, "\t-m\toverride XPA_METHOD environment variable\n");
29 fprintf(stderr, "\t-n\treturn number of matches instead of \"yes\" or \"no\"\n");
30 fprintf(stderr, "\t-t \toverride XPA_[SHORT,LONG]_TIMEOUT environment variables\n");
31 fprintf(stderr, "\t-u \toverride XPA_NSUSERS environment variable\n");
32 fprintf(stderr, "\t-v\tprint info about successful access points\n");
33 fprintf(stderr, "\t-V\tprint info and errors about all access points\n");
34 fprintf(stderr, "\t--version display version and exit\n");
35 fprintf(stderr, "\n");
36 fprintf(stderr, "xpaaccess returns \"yes\" if there are existing XPA access points that match\n");
37 fprintf(stderr, "the template (and optional access type: g,i,s), and returns \"no\" otherwise.\n");
38 fprintf(stderr, "If -n is specified, the number of matches is returned.\n");
39 fprintf(stderr, "Templates are of the form [class:]name. Wildcards *,?,[] are accepted.\n");
40 fprintf(stderr, "\n");
41 fprintf(stderr, "examples:\n");
42 fprintf(stderr, "\tcsh> if( `xpaaccess ds9` == \"yes\") then ...\n" );
43 fprintf(stderr, "\tcsh> set got=`xpaaccess -n ds9 g`\n");
44 fprintf(stderr, "\n(version: %s)\n", XPA_VERSION);
45 exit(0);
46 }
47
48 /* catch error from pre 2.1 server -- if we find this error, we know the
49 access point is available */
50 #define OLD_SERVER(s) strstr(s, "XPA$ERROR invalid xpa command in initialization string")
51
52 #ifdef ANSI_FUNC
53 int
54 main (int argc, char **argv)
55 #else
56 int
57 main(argc, argv)
58 int argc;
59 char **argv;
60 #endif
61 {
62 int i;
63 int c;
64 int got;
65 int n=0;
66 int donum=0;
67 int verbosity=0;
68 int contact=0;
69 char cmd[SZ_LINE];
70 char mode[SZ_LINE];
71 char *type=NULL;
72 char *xtmpl=NULL;
73 char **rclasses;
74 char **rnames;
75 char **rmethods;
76 char **rinfos;
77 char *names[MAX_XPAS];
78 char *errs[MAX_XPAS];
79 XPA xpa=NULL;
80
81 /* display version and exit, if necessary */
82 if( (argc == 2) && !strcmp(argv[1], "--version") ){
83 fprintf(stderr, "%s\n", XPA_VERSION);
84 exit(0);
85 }
86
87 /* start with no mode flag */
88 *mode = '\0';
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, "chi:m:nt:u:vVwW")) != -1){
96 switch(c){
97 case 'c':
98 contact=1;
99 break;
100 case 'h':
101 usage(argv[0]);
102 case 'i':
103 snprintf(cmd, SZ_LINE, "XPA_NSINET=%s", optarg);
104 putenv(xstrdup(cmd));
105 break;
106 case 'm':
107 snprintf(cmd, SZ_LINE, "XPA_METHOD=%s", optarg);
108 putenv(xstrdup(cmd));
109 break;
110 case 'n':
111 donum=1;
112 break;
113 case 't':
114 {
115 int xip=0;
116 char xbuf[SZ_LINE];
117 newdtable(",;");
118 if( word(optarg, xbuf, &xip) && *xbuf && (*xbuf != '*') ){
119 snprintf(cmd, SZ_LINE, "XPA_SHORT_TIMEOUT=%s", xbuf);
120 putenv(xstrdup(cmd));
121 }
122 if( word(optarg, xbuf, &xip) && *xbuf && (*xbuf != '*') ){
123 snprintf(cmd, SZ_LINE, "XPA_LONG_TIMEOUT=%s", xbuf);
124 putenv(xstrdup(cmd));
125 }
126 freedtable();
127 }
128 break;
129 case 'u':
130 snprintf(cmd, SZ_LINE, "XPA_NSUSERS=%s", optarg);
131 putenv(xstrdup(cmd));
132 break;
133 case 'v':
134 verbosity=1;
135 break;
136 case 'V':
137 verbosity=2;
138 break;
139 /* for backward compatibility with 1.0 */
140 case 'w':
141 case 'W':
142 break;
143 }
144 }
145
146 /* we must have a template */
147 if( optind >= argc ){
148 usage(argv[0]);
149 }
150 else{
151 xtmpl = argv[optind];
152 optind++;
153 if( optind < argc ){
154 type = argv[optind];
155 }
156 }
157
158 /* if we need to contact each server ... */
159 if( contact ){
160 got = XPAAccess(xpa, xtmpl, type, mode, names, errs, MAX_XPAS);
161 for(i=0; i<got; i++){
162 if( !errs[i] || OLD_SERVER(errs[i]) ){
163 if( verbosity ) fprintf(stdout, "%s\n", names[i]);
164 n++;
165 }
166 else{
167 if( verbosity >= 2 ) fprintf(stdout, "%s", errs[i]);
168 }
169 if( errs[i] ) xfree(errs[i]);
170 if( names[i] ) xfree(names[i]);
171 }
172 }
173 /* only contact name server */
174 else{
175 n = XPANSLookup(xpa, xtmpl, type, &rclasses, &rnames, &rmethods, &rinfos);
176 /* free up the space */
177 for(i=0; i<n; i++){
178 if( verbosity ){
179 fprintf(stdout, "%s", rmethods[i]);
180 if( strcmp(rinfos[i], XPA_DEF_CLIENT_INFO) )
181 fprintf(stdout, " %s", rinfos[i]);
182 fprintf(stdout, "\n");
183 }
184 /* done with these strings */
185 xfree(rclasses[i]);
186 xfree(rnames[i]);
187 xfree(rmethods[i]);
188 xfree(rinfos[i]);
189 }
190 /* free up arrays returned by name server */
191 if( n > 0 ){
192 xfree(rclasses);
193 xfree(rnames);
194 xfree(rmethods);
195 xfree(rinfos);
196 }
197 }
198
199 /* print out number */
200 if( !verbosity && donum ){
201 fprintf(stdout, "%d\n", n);
202 }
203 /* print out yes/no */
204 else if( !verbosity && !donum ){
205 if( n > 0 )
206 fprintf(stdout, "yes\n");
207 else
208 fprintf(stdout, "no\n");
209 }
210 fflush(stdout);
211 return(n);
212 }