Debian Codegraph @

libsrc/xcon/xcx.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++++ XC interface module XCX +++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module XCX
31 .AUTHOR K. Banse ESO - Garching
32 .KEYWORDS communication
33 .ENVIRONMENT VMS and UNIX
34 .COMMENTS
35 holds XCXINI, XCZINI,
36 XCXOPN, XCZOPN,
37 XCXSTA, XCXGFD,
38 XCXCLO, XCXSTP
39 .REMARKS
40
41 .VERSION [1.00] 940422: creation
42
43 090414 last modif
44 ----------------------------------------------------------------------------*/
45
46 #include <midas_def.h>
47 #include <xconndef.h>
48 #include <fsydef.h>
49
50 #include <stdlib.h>
51 #include <stdio.h>
52
53
54
55 static int startup = -1;
56
57 static char lt[4] = "a ";
58 static char xbuf[] = "-geometry =80x25+500+500 -e inmidas";
59
60
61 /*
62
63 */
64
65 int crea_xterm(flag,midasunit,host,display,midasvers)
66
67 /*++++++++++++++++++++++++++++++++++++++++++++++++++
68 .PURPOSE
69 Execute the `xterm' which in turn starts up a Midas session
70 .RETURNS
71 = 0; new Midas started
72 = 1; Midas already running - just connect
73 --------------------------------------------------------*/
74
75 char flag; /* IN: Z or X if called from XCZINI or XCXINI */
76 char *midasunit; /* IN: unit of Midas session to connect to */
77 char *host; /* IN: host, if network scokets sould be used,
78 else '\0' */
79 char *display; /* IN: display where Midas session will run
80 in X terminology */
81 char *midasvers; /* IN: mode+version (release) of Midas session;
82 the first char. is used to indicate a parallel
83 session (=P) or not, the version begins at the
84 2. char. of this param. E.g. with "P96NOV" we
85 use version 96NOV in parallel; with " test" we
86 use version test in non-parallel mode */
87
88 {
89 int jj, fp;
90
91 char *middir, parallel, vstr[120], xspecs[80], wstr[240], method[8];
92
93
94
95 if (*host == '\0') /* local host */
96 {
97 jj = Mrunning(midasunit,0); /* check if Midas already running */
98 if (jj == 1) return (1);
99 }
100
101
102
103 /* build up string for Unix command `xterm' */
104
105 if (!(middir = getenv("MID_WORK")))
106 {
107 if (!(middir = getenv("HOME"))) return (-1);
108
109 (void) strcpy(wstr,middir);
110 (void) strcat(wstr,"/midwork");
111 }
112 else
113 (void) strcpy(wstr,middir);
114
115 (void) strcat(wstr,"/Midxterms.def");
116 fp = osaopen(wstr,0); /* open for READ */
117
118 /*
119 printf("osaopen of %s = %d\n",wstr,fp);
120 */
121
122 xspecs[0] = '\0';
123 if (fp > -1) /* we found such a file */
124 {
125 read_loop:
126 jj = osaread(fp,wstr,82);
127 if (jj < 0) goto close_file;
128
129 if (jj > 0) /* we got something */
130 {
131 if ((wstr[0] = *midasunit) && (wstr[1] = *(midasunit+1)))
132 { /* Midas unit matches */
133 jj = CGN_INDEXC(wstr,'=') + 1;
134 if (jj > 2)
135 {
136 (void) strcpy(xspecs,&wstr[jj]);
137 goto close_file;
138 }
139 }
140 }
141 goto read_loop;
142
143 close_file:
144 (void) osaclose(fp);
145 }
146
147 if (xspecs[0] != '\0')
148 {
149 if (*display != '\0')
150 (void) sprintf(vstr,"xterm %s -d %s ",xspecs,display);
151 else
152 (void) sprintf(vstr,"xterm %s ",xspecs);
153 }
154 else
155 {
156 if (*display != '\0')
157 (void) sprintf(vstr,"xterm -d %s ",display);
158 else
159 (void) strcpy(vstr,"xterm ");
160 }
161
162 if (flag == 'X')
163 (void) strcpy(method,"sockets");
164 else
165 (void) strcpy(method,"files");
166
167 if (*host != '\0')
168 (void) sprintf(wstr,"%s %s -j \"SET/BACKGR %s,remote\" %s",
169 vstr,xbuf,method,midasunit);
170 else
171 (void) sprintf(wstr,"%s %s -j \"SET/BACKGR %s\" %s",
172 vstr,xbuf,method,midasunit);
173
174
175 parallel = *midasvers++;
176 if (parallel != '\0')
177 {
178 if (parallel == 'P')
179 {
180 if (*midasvers != '\0')
181 (void) sprintf(vstr," -r %s -p &",midasvers);
182 else
183 (void) sprintf(vstr," -p &");
184 }
185 else
186 (void) sprintf(vstr," -r %s &",midasvers);
187 (void) strcat(wstr,vstr);
188 }
189
190 else
191 (void) strcat(wstr," &");
192
193 (void) oshcmd(wstr,(char *) 0, (char *) 0, (char *) 0);
194
195 return (0);
196 }
197 /*
198
199 */
200
201 int XCZINI(midasunit,myunit,direc,display,midasvers,maxtim,cid)
202
203 /*++++++++++++++++++++++++++++++++++++++++++++++++++
204 .PURPOSE
205 start a Midas session with unit `myunit' +
206 open a connection to MIDAS session with unit `midasunit' using files
207 .ALGORITHM
208 update structures XCONNECT, BKMIDAS
209 .REMARKS
210 ~/midwork is used for storing the files used for communication
211 .RETURNS
212 = 0; o.k.
213 = 90; invalid input
214 = -1; problems with file communication
215 = -2; requested Midas session could not be started
216 = -3; requested Midas session not "ready" after `maxtim' seconds
217 = -90; invalid Midas unit given
218 --------------------------------------------------------*/
219
220 char *midasunit; /* IN: unit of Midas session to connect to */
221 char *myunit; /* IN: pseudo Midas unit of calling program */
222 char *direc; /* IN: directory for send/receive files
223 if = '\0', use MID_WORK directory */
224 char *display; /* IN: display where Midas session will run
225 in X terminology */
226 char *midasvers; /* IN: mode+version (release) of Midas session;
227 the first char. is used to indicate a parallel
228 session (=P) or not, the version begins at the
229 2. char. of this param. E.g. with "P96NOV" we
230 use version 96NOV in parallel; with " test" we
231 use version test in non-parallel mode */
232 int maxtim; /* IN: max. time (seconds) we wait until Midas session
233 is ready (maybe = 0) */
234 int *cid; /* OUT: connection id,
235 to be used in all other routines */
236
237 {
238 int stat, jj;
239
240
241
242
243 jj = crea_xterm('Z',midasunit,"",display,midasvers); /* no different host */
244
245
246 if (jj == 1) /* Midas already running */
247 stat = XCZOPN(midasunit,myunit,direc,cid);
248 else
249 { /* new Midas started */
250 jj = Mrunning(midasunit,maxtim);
251 if (jj < 1)
252 {
253 if (jj == -1)
254 stat = -2;
255 else
256 stat = -3;
257 }
258
259 else
260 stat = XCZOPN(midasunit,myunit,direc,cid);
261 }
262
263 return (stat);
264 }
265 /*
266
267 */
268
269 int XCXINI(midasunit,host,display,midasvers,maxtim,cid)
270
271 /*++++++++++++++++++++++++++++++++++++++++++++++++++
272 .PURPOSE
273 Start up a Midas session (on same host!) with unit `midasunit' and
274 version `midasvers', open a connection to that session using local or
275 network sockets depending on `host'
276 .ALGORITHM
277 Use routine Mrunning to check for running Midas session, use the Unix command
278 `xterm -e inmidas' to start up Midas.
279 in the network.
280 Update structures XCONNECT, BKMIDAS
281 .REMARKS
282 /tmp is used for internal storage
283 .RETURNS
284 = 0; o.k.
285 = -1; problems with socket communication
286 = -2; requested Midas session could not be started
287 = -3; requested Midas session not "ready" after `maxtim' seconds
288 = -90; invalid Midas unit given
289 --------------------------------------------------------*/
290
291 char *midasunit; /* IN: unit of Midas session to connect to */
292 char *host; /* IN: host, if network scokets sould be used,
293 else '\0' */
294 char *display; /* IN: display where Midas session will run
295 in X terminology */
296 char *midasvers; /* IN: mode+version (release) of Midas session;
297 the first char. is used to indicate a parallel
298 session (=P) or not, the version begins at the
299 2. char. of this param. E.g. with "P96NOV" we
300 use version 96NOV in parallel; with " test" we
301 use version test in non-parallel mode */
302 int maxtim; /* IN: max. time (seconds) we wait until Midas session
303 is ready (maybe = 0) */
304 int *cid; /* OUT: connection id,
305 to be used in all other routines */
306
307
308 {
309 int stat, jj;
310
311
312
313
314 jj = crea_xterm('X',midasunit,host,display,midasvers);
315 if (jj == 1) return (XCXOPN(midasunit,host,cid));
316
317
318 if (*host != '\0') /* remote host */
319 return (XCXOPN(midasunit,host,cid));
320
321
322 jj = Mrunning(midasunit,maxtim); /* wait `maxtim' seconds */
323 if (jj < 1) /* on local host */
324 {
325 if (jj == -1)
326 stat = -2;
327 else
328 stat = -3;
329 return (stat);
330 }
331 else
332 return (XCXOPN(midasunit,host,cid));
333
334 }
335 /*
336
337 */
338
339 int XCXOPN(midasunit,host,cid)
340
341 /*++++++++++++++++++++++++++++++++++++++++++++++++++
342 .PURPOSE
343 open a connection to MIDAS session with unit `midasunit' using sockets
344 .ALGORITHM
345 use sockets to communicate with a Midas session on same host or another host
346 in the network;
347 update structures XCONNECT, BKMIDAS
348 .REMARKS
349 /tmp is used for internal storage
350 .RETURNS
351 = 0; o.k.
352 = -1; problems with socket communication
353 = -90; invalid Midas unit given
354 = -92; Midas session `midasunit' not existing
355 --------------------------------------------------------*/
356
357 char *midasunit; /* IN: unit of Midas session to connect to */
358 char *host; /* IN: host where Midas session is running
359 if = '\0', local host */
360 int *cid; /* OUT: connection id,
361 to be used in all other routines */
362
363 {
364 register int nr;
365 int stat, jj;
366
367
368 /* check `startup' */
369
370 if (startup == -1)
371 {
372 startup = 0;
373 inxcon("OSX",""); /* init file names */
374 }
375
376
377 if (*host != '\0')
378 {
379 char wstr[100];
380
381 jj = CGN_COPY(wstr,host);
382 wstr[jj++] = ':'; /* build: host:unit */
383 wstr[jj++] = midasunit[0];
384 wstr[jj++] = midasunit[1];
385 wstr[jj] = '\0';
386 stat = msetup(1,wstr,"no",lt,"sockets");
387 }
388 else
389 stat = msetup(1,midasunit,"no",lt,"sockets");
390
391 if (stat == 0)
392 {
393 for (nr=0; nr<MAX_BACK; nr++)
394 {
395 if ((BKMIDAS[nr].UNIT[0] == midasunit[0]) &&
396 (BKMIDAS[nr].UNIT[1] == midasunit[1]))
397 {
398 *cid = nr;
399 goto next_step;
400 }
401 }
402 stat = -90;
403 }
404
405 else
406 {
407 if (stat == 4)
408 {
409 stat = -1;
410 if (*host == '\0') /* if same host, check */
411 { /* if Midas `midasunit' exists */
412 jj = Mrunning(midasunit,0);
413 if (jj == -1) stat = -92; /* Midas session is not running */
414 }
415 }
416 else if (stat == 34)
417 stat = -90;
418 }
419 startup = -1; /* reset `startup' */
420
421
422 next_step:
423 if (lt[0] < 'z') /* dummy backgr. char. */
424 lt[0] ++;
425 else
426 lt[0] = 'a';
427
428 return (stat);
429 }
430 /*
431
432 */
433
434 int XCXCLO(cid)
435
436 /*++++++++++++++++++++++++++++++++++++++++++++++++++
437 .PURPOSE
438 close connection
439 .ALGORITHM
440 synchronous disk I/O
441 .RETURNS
442 = 0; o.k.
443 = -1; problems with socket/ communication
444 = -90; invalid input to this interface
445 --------------------------------------------------------*/
446
447 int cid;
448
449 {
450 int stat;
451
452 char munit[2];
453
454
455 if ((cid < 0) || (cid >= MAX_BACK) || (BKMIDAS[cid].UNIT[0] == ' '))
456 return (-90);
457
458 munit[0] = BKMIDAS[cid].UNIT[0];
459 munit[1] = BKMIDAS[cid].UNIT[1];
460 startup = -1;
461
462 stat = msetup(0,munit,"no","no","no"); /* disconnect `munit' */
463
464 if ((stat == 4) || (stat == 5)) stat = -1;
465
466 return (stat);
467 }
468
469 /*
470
471 */
472
473 int XCXSTA(cid)
474
475 /*++++++++++++++++++++++++++++++++++++++++++++++++++
476 .PURPOSE
477 check status of background Midas
478 .ALGORITHM
479 use `outmail'
480 .RETURNS
481 return status
482 = 0; background Midas idle
483 = 1; background Midas busy
484 = -1; problems with socket/file communication
485 = -90; invalid input to this interface
486 --------------------------------------------------------*/
487
488 int cid; /* IN: connection id */
489
490 {
491 int stat, retstat[2];
492
493 char wstr[8]; /* just placeholder */
494
495
496 if ((cid < 0) || (cid >= MAX_BACK) || (BKMIDAS[cid].UNIT[0] == ' '))
497 return (-90);
498
499
500 stat = outmail(3,wstr,cid,retstat);
501 if ((stat == 4) || (stat == 5)) stat = -1;
502
503 return (stat);
504 }
505
506 /*
507
508 */
509
510 int XCXGFD(cid,fd)
511
512 /*++++++++++++++++++++++++++++++++++++++++++++++++++
513 .PURPOSE
514 get file descriptor of socket used in communication with background Midas
515 .ALGORITHM
516 .RETURNS
517 return status
518 = 0; all o.k.
519 = -1; problems with socket communication
520 = -90; invalid input to this interface
521 --------------------------------------------------------*/
522
523 int cid; /* IN: connection id */
524 int *fd; /* OUT: file descr. of used socket */
525
526 {
527 int kk, gd;
528
529
530 if ((cid < 0) || (cid >= MAX_BACK) || (BKMIDAS[cid].UNIT[0] == ' '))
531 return (-90);
532
533 kk = ClientInfo(BKMIDAS[cid].CHAN,&gd);
534 if (kk == 0)
535 {
536 *fd = gd;
537 return (0);
538 }
539 else
540 return (-1);
541 }
542
543 /*
544
545 */
546
547 int XCZOPN(midasunit,myunit,direc,cid)
548
549 /*++++++++++++++++++++++++++++++++++++++++++++++++++
550 .PURPOSE
551 open a connection to MIDAS session with unit `midasunit' using files
552 .ALGORITHM
553 update structures XCONNECT, BKMIDAS
554 .REMARKS
555 ~/midwork is used for storing the files used for communication
556 .RETURNS
557 = 0; o.k.
558 = 90; invalid input
559 = -1; problems with file communication
560 = -90; invalid Midas unit given
561 = -92; Midas session `midasunit' not existing
562 --------------------------------------------------------*/
563
564 char *midasunit; /* IN : unit of Midas session to connect to */
565 char *myunit; /* IN : pseudo Midas unit of calling program */
566 char *direc; /* IN : directory for send/receive files
567 if = '\0', use MID_WORK directory */
568 int *cid; /* OUT: connection id,
569 to be used in all other routines */
570
571 {
572 register int nr;
573 int stat, jj;
574
575 char *middir, wstr[240];
576
577
578 /* check `startup' */
579
580 if (startup == -1)
581 {
582 if (*direc == '\0')
583 {
584 if (!(middir = getenv("MID_WORK")))
585 { /* use default: ~/midwork */
586 if (!(middir = getenv("HOME"))) return (-99);
587
588 jj = (int) strlen(middir);
589 if (jj > 230) return (-99);
590
591 (void) strcpy(wstr,middir);
592 (void) strcat(wstr,"/midwork/");
593 }
594 else
595 { /* use MID_WORK directly */
596 jj = (int) strlen(middir);
597 if (jj > 238) return (-99);
598
599 (void) strcpy(wstr,middir);
600
601 if (wstr[jj-1] != FSY_DIREND) /* we need xyz../ as dirspec */
602 {
603 wstr[jj++] = FSY_DIREND;
604 wstr[jj] = '\0';
605 }
606 }
607 }
608
609 else
610 { /* use specified directory */
611 jj = (int) strlen(direc);
612 if (jj > 238) return (90);
613
614 jj = CGN_COPY(wstr,direc);
615
616 #if vms
617 #else
618 if (wstr[jj-1] != FSY_DIREND) /* we need xyz../ as dirspec */
619 {
620 wstr[jj++] = FSY_DIREND;
621 wstr[jj] = '\0';
622 }
623 #endif
624 }
625
626 inxcon(myunit,wstr); /* init file names */
627 startup = 0;
628 }
629
630
631 /* check, if required Midas is actually running */
632
633 jj = Mrunning(midasunit,0);
634
635 if (jj == -1)
636 stat = -92;
637
638 else
639 {
640 stat = msetup(1,midasunit,"no",lt,"files");
641 if (stat == 0)
642 {
643 for (nr=0; nr<MAX_BACK; nr++)
644 {
645 if ((BKMIDAS[nr].UNIT[0] == midasunit[0]) &&
646 (BKMIDAS[nr].UNIT[1] == midasunit[1]))
647 {
648 *cid = nr;
649 goto next_step;
650 }
651 }
652 stat = 90;
653 }
654 else
655 {
656 if (stat == 34)
657 stat = -90;
658 else if (stat == -2)
659 stat = -1;
660 }
661 }
662
663 startup = -1; /* error, so we reset */
664 return (stat);
665
666
667 next_step:
668 if (lt[0] < 'z')
669 lt[0] ++;
670 else
671 lt[0] = 'a';
672
673 return (0);
674 }
675 /*
676
677 */
678
679 int XCXSTP(cid)
680
681 /*++++++++++++++++++++++++++++++++++++++++++++++++++
682 .PURPOSE
683 stop connected Midas session + close connection
684 .ALGORITHM
685 use XCCSND + XCXCLO
686 .RETURNS
687 return status ( 0 = o.k )
688 --------------------------------------------------------*/
689
690 int cid;
691
692 {
693 int stat, retstat[2], n;
694
695
696 n = -1;
697 stat = XCCSND(cid,"clear/background 2; bye",n,retstat);
698 if (stat == 0)
699 stat = XCXCLO(cid);
700 else
701 (void) XCXCLO(cid);
702
703 return stat;
704 }
705