Debian Codegraph @

libsrc/st/midkey.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2010 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 /*+++++++++++++++++++++ Module MIDKEY +++++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module MIDKEY
31 .AUTHOR Klaus Banse ESO - Garching
32 .KEYWORDS Midas keyword utility routines.
33 .ENVIRONMENT VMS and UNIX
34 .COMMENT
35 holds FNDKEY, MOVKEY, TYPCHK, GETLOCAL
36
37 .VERSION
38 100222 last modif
39
40 ------------------------------------------------------------------------*/
41
42 #include <fileexts.h>
43 #include <fsydef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 #if vms
48 #else
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <sys/mman.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #endif
55
56 #define READ 0
57 #define WRITE 1
58 #define READ_WRITE 2
59 #define APPEND 3
60
61 #define MAX_LEVEL 25 /* synchronize with midback.h ...*/
62
63
64 static struct KEY_STRUCT *keypntr;
65
66 static char level[MAX_LEVEL+4] = "ABCDEFGHIJKLMNOPQRSTUVWXY";
67 /* synchronize with MID_DEFKEY + CLEAR_LOCAL... */
68
69 static char cdif = 'A' - 'a';
70
71 /*
72
73 */
74
75 int MID_FNDKEY(name,type,bytelm,noelm,unit)
76
77 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78 .PURPOSE
79 find the entry of a given keyword + return related info, if found
80 .ALGORITHM
81 first search IDENT[0 - 14] for matching name + level (in IDENT[16])
82 equal current procedure (i.e. treat as local key);
83 if not found search as global key
84 the current procedure level is stored (as integer) in keyword MODE(7)
85 global keys are stored bottom-up, local keys top-down...
86 .RETURN
87 kentry: I*4 no. of entry, if found; -1, if not found
88 -----------------------------------------------------------------------------*/
89
90 char *name /* IN : name of keyword */;
91 char *type /* OUT: type of keyword,
92 currently I,R,C,D,S are recognized for
93 I*4,R*4,CHAR*n,R*8, size_t */;
94 int *bytelm /* OUT: no. of bytes per element of keyword data */;
95 int *noelm /* OUT: no. of elements connected to keyword */;
96 int *unit /* OUT: pointer to units-pointer */;
97
98 {
99 int kstrn, proglevel;
100 register int nr;
101
102 char loclevel, kname[20];
103 register char nc;
104
105
106
107
108 for (nr=0; nr<=KEY_NAMELEN; nr++)
109 {
110 nc = name[nr];
111 if (nc == '\0')
112 {
113 kstrn = nr;
114 goto step_a;
115 }
116 if ((nc >= 'a') && (nc <= 'z')) /* convert to upper case */
117 nc += cdif;
118 kname[nr] = nc;
119 }
120 return (-1); /* bad (too long) keyname */
121
122
123 step_a:
124 if (kstrn < KEY_NAMELEN) kname[kstrn++] = ' '; /* append a blank */
125
126 proglevel = KIWORDS[OFF_MODE+6] - 1; /* get procedure level */
127 if (proglevel >= 0)
128 {
129
130 /* first loop through LOCNAMES from KEYALL.LOCENT till KEYALL.LOCNO
131 and compare the key names as well as the levels */
132
133 keypntr = KEYALL.KEYNAMES + KEYALL.LOCNO;
134 loclevel = level[proglevel];
135
136 for (nr=KEYALL.LOCNO; nr>=KEYALL.GLOBENT; nr--)
137 {
138 if ( (keypntr->IDENT[16] >= loclevel) &&
139 (strncmp(keypntr->IDENT,kname,(size_t)kstrn) == 0) ) goto gotcha;
140
141 keypntr --;
142 }
143 }
144
145
146 /* then also loop through GLOBNAMES from 0 till KEYALL.GLOBNO */
147
148 keypntr = KEYALL.KEYNAMES; /* restart at beginning */
149 for (nr=0; nr<=KEYALL.GLOBNO; nr++) /* KEYALL.GLOBNO is last entry used */
150 {
151 if (strncmp(keypntr->IDENT,kname,(size_t)kstrn) == 0)
152 goto gotcha;
153
154 keypntr ++;
155 }
156
157 /* arriving here means, that we didn't find it */
158
159 return (-1);
160
161 gotcha:
162 *type = keypntr->IDENT[15];
163 *bytelm = keypntr->BYTELEM;
164 *noelm = keypntr->NOELEM;
165 *unit = 0; /* not used currently */
166
167 return nr; /* keyword found */
168 }
169 /*
170
171 */
172
173 void MID_TYPCHK(intype,outtype,bytelm)
174
175 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
176 .PURPOSE
177 check given data type + return cleaned type and no. of bytes per element
178 .ALGORITHM
179 straight forward
180 .RETURNS nothing.
181 ---------------------------------------------------------------------------*/
182
183 char *intype; /* IN : type of keyword/descr */
184 char *outtype; /* OUT: type of keyword/descr currently I,R,C,D,S
185 set to ' ', if invalid INTYPE given */
186 int *bytelm; /* OUT: no. of bytes per element of keyword/descr data */
187
188 {
189 int mm, lbytlm;
190
191 register char test;
192
193
194
195 *outtype = ' '; /* default to no_success ... */
196
197 test = *intype;
198 if ((test >= 'a') && (test <= 'z')) /* convert to upper case */
199 test += cdif;
200
201
202 /* first look for type CHAR*len */
203
204 if (test == 'C')
205 {
206 mm = CGN_INDEXC(intype,'*'); /* search for '*' */
207 if (mm < 0 )
208 {
209 *bytelm = 1; /* that is o.k. */
210 *outtype = 'C';
211 }
212 else
213 { /* point to char. following '*' */
214 lbytlm = -1;
215 lbytlm = atoi(intype+mm+1);
216 if ( lbytlm > 0)
217 {
218 *bytelm = lbytlm;
219 *outtype = 'C'; /* only o.k., if valid number */
220 }
221 }
222 }
223
224
225 /* then for integer, real + double precision */
226
227 else if (test == 'I')
228 {
229 *bytelm = II_SIZE;
230 *outtype = 'I';
231 }
232
233 else if (test == 'R')
234 { /* distinguish between R*4 and R*8 */
235 if ( CGN_INDEXS(intype,"*8") > 0)
236 {
237 *bytelm = DD_SIZE;
238 *outtype = 'D';
239 }
240 else
241 {
242 *bytelm = RR_SIZE;
243 *outtype = 'R';
244 }
245 }
246
247 else if (test == 'D')
248 {
249 *bytelm = DD_SIZE;
250 *outtype = 'D';
251 }
252
253 else if (test == 'S')
254 {
255 *bytelm = SS_SIZE;
256 *outtype = 'S';
257 }
258 }
259 /*
260
261 */
262
263 void MID_GETLOCAL(inoutno,keyid,byti,noi)
264
265 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
266 .PURPOSE
267 loop through local keys + return type and no. of bytes per element
268 .ALGORITHM
269 straight forward
270 .RETURNS nothing.
271 ---------------------------------------------------------------------------*/
272
273 int *inoutno; /* IN/OUT: = 0 to start search, > 0 to continue (IN),
274 n on return for nth local key (n > 0),
275 -1 on return if no more local keyw. */
276 char *keyid; /* OUT: keyname in [0] - [14]
277 type in [15]
278 level in [16] */
279 int *byti; /* OUT: no. of bytes per elem */
280 int *noi; /* OUT: no. of elements of key */
281
282 {
283 register int nr;
284 int proglevel;
285 static int localcount;
286
287 static struct KEY_STRUCT *lkeypntr;
288
289 static char kbloclevel;
290
291
292
293
294 if (*inoutno == 0) /* start of search for local keys */
295 {
296 proglevel = KIWORDS[OFF_MODE+6] - 1; /* get procedure level */
297 if (proglevel >= 0)
298 {
299 /* loop through LOCNAMES from KEYALL.LOCENT till KEYALL.LOCNO
300 and get all local keynames */
301
302 kbloclevel = level[proglevel];
303 lkeypntr = KEYALL.KEYNAMES + KEYALL.LOCNO;
304 localcount = 0;
305 for (nr=KEYALL.LOCNO; nr>=KEYALL.GLOBENT; nr--)
306 {
307 if (lkeypntr->IDENT[16] >= kbloclevel) goto gotcha;
308
309 lkeypntr --;
310 }
311 }
312 }
313 else
314 {
315 if (lkeypntr >= KEYALL.KEYNAMES + KEYALL.GLOBENT)
316 {
317 lkeypntr --;
318 if (lkeypntr->IDENT[16] >= kbloclevel) goto gotcha;
319 }
320 }
321 *inoutno = -1;
322 return;
323
324
325 gotcha:
326 localcount ++;
327 *inoutno = localcount;
328 (void) strncpy(keyid,lkeypntr->IDENT,17);
329 *byti = lkeypntr->BYTELEM;
330 *noi = lkeypntr->NOELEM;
331 }
332 /*
333
334 */
335
336 #if vms
337
338 int MID_MOVKEY(direc,option)
339
340 /*++++++++++++++++++++++++++++++++++++++++++++++++++
341 .PURPOSE
342 initialize the system keywords from binary file FORGRxy.KEY
343 or write keywords to that file
344 .ALGORITHM
345 either use par. 'option' as keyfile name or build name
346 .RETURNS
347 status: I*4 return status
348 --------------------------------------------------*/
349 char *direc /* IN: [0] = direction of moving, I or O \
350 [1] = M(onitor) or X(tension) or A(pplication) */;
351 char *option /* IN: if not blank, it's already the name of the keyfile */;
352
353 {
354 static int initflg = -1;
355 static int gp;
356
357 int ksz, dsz, totsize, keysize, datsize;
358 int ggp, status, n, mm;
359
360 char *tmpntr;
361 char buf[4], work[200], *cpntr;
362
363 struct KEY_STRUCT *keyname;
364
365
366
367 totsize = sizeof(struct KEY_ALL);
368
369
370
371 /* --------------------------------*/
372 /* move keywords in */
373 /* --------------------------------*/
374
375 if (*direc++ == 'I') /* already move to direc[1] */
376 {
377 initflg ++;
378 ggp = -1;
379
380
381 /* if Midas from Unix command line => DAZUNIT is not defined */
382
383 if (*direc == 'A')
384 {
385 ksz = 0; /* MIDAS application */
386 OSY_GETSYMB("DAZUNIT",buf,4);
387 if ((buf[0] == 'D') && (buf[1] == 'A') && (buf[2] == 'Z'))
388 {
389 (void) strcpy(work,"FORGRdrs.KEY");
390 ggp = 0;
391 ksz += 10;
392 }
393 else
394 {
395 (void) OSY_TRNLOG("MID_WORK",work,168,&mm);
396 if (work[mm-1] != FSY_DIREND) work[mm++] = FSY_DIREND;
397 (void) strcpy(&work[mm],"FORGR .KEY");
398 work[mm+5] = buf[0];
399 work[mm+6] = buf[1];
400 }
401 }
402 else
403 {
404 ksz = 1; /* MIDAS monitor */
405 if (*option == ' ') /* use default keyword file */
406 {
407 (void) OSY_TRNLOG("MID_WORK",work,168,&mm);
408 if (work[mm-1] != FSY_DIREND) work[mm++] = FSY_DIREND;
409 (void) strcpy(&work[mm],"FORGR .KEY");
410 OSY_GETSYMB("DAZUNIT",buf,4);
411 work[mm+5] = buf[0];
412 work[mm+6] = buf[1];
413 }
414 else if (*option == '*') /* use fixed (for Unix) keyword file */
415 {
416 (void) strcpy(work,"FORGRdrs.KEY");
417 ggp = 0;
418 ksz += 10;
419 }
420 else
421 (void) strcpy(work,option);
422 }
423
424 keyname = KEYALL.KEYNAMES; /* save pointers in case of need */
425 KDWORDS = KEYALL.KEYWORDS;
426
427 gp = osdopen(work,READ_WRITE);
428
429 if (gp == -1)
430 {
431 if (ggp == 0)
432 { /* get FORGRdrs.KEY from MID_MONIT */
433 (void) OSY_TRNLOG("MID_MONIT",work,156,&mm);
434 if (work[mm-1] != FSY_DIREND) work[mm++] = FSY_DIREND;
435 (void) strcpy(&work[mm],"FORGRdrs.KEY");
436 ggp = osdopen(work,READ);
437 if (ggp == -1)
438 { /* we can't use MID_ERROR yet... */
439 (void) printf("Could not open keyfile %s \n\r",work);
440 return ERR_KEYBAD;
441 }
442 gp = ggp;
443 }
444 else
445 {
446 (void) printf("Could not open keyfile %s \n\r",work);
447 return ERR_KEYBAD;
448 }
449 }
450 else
451 ggp = -1; /* avoid using ggp */
452
453
454 /* read binary keyfile via 3 osdread's */
455
456 cpntr = (char *) &KEYALL;
457 status = osdread(gp,cpntr,(unsigned int)totsize);
458 if (status < totsize) goto osd_rerror;
459 /*
460 printf("totsize = %d\n",totsize);
461 */
462
463
464 keysize = sizeof(struct KEY_STRUCT) * (KEYALL.LOCENT+1);
465 datsize = KEYALL.LOCDAT + 2; /* synchronize with midasgo.c */
466 KEYALL.ORIGIN = ksz; /* 0 (applic) or 1 (monitor) */
467
468
469 /* now also check, if we extend the keyword area */
470
471 if (*direc == 'X')
472 {
473 n = KIWORDS[OFF_MONPAR+2]; /* new no. of global keys */
474 mm = KIWORDS[OFF_MONPAR+3]; /* new no. of local keys */
475 KEYALL.GLOBENT = n;
476 KEYALL.GLOBDAT = KEYALL.GLOBENT * KEY_FACT;
477 KEYALL.LOCENT = n + mm;
478 KEYALL.LOCDAT = KEYALL.LOCENT * KEY_FACT;
479 KEYALL.LOCNO = KEYALL.GLOBENT - 1; /* reset local keywords */
480 KEYALL.LOCEND = KEYALL.GLOBDAT - 1;
481 ksz = sizeof(struct KEY_STRUCT) * (KEYALL.LOCENT+1);
482 dsz = KEYALL.LOCDAT + 2;
483
484 cpntr = (char *) KEYALL.KEYNAMES; /* release previous memory */
485 free(cpntr);
486 cpntr = (char *) KEYALL.KEYWORDS;
487 free(cpntr);
488 initflg = 0; /* enforce memory allocation */
489 }
490 else
491 {
492 ksz = keysize;
493 dsz = datsize;
494 }
495
496 if (initflg == 0)
497 {
498 tmpntr = malloc((size_t)ksz);
499 if (tmpntr == NULL)
500 {
501 (void) printf("could not allocate %d bytes for key_names\n",ksz);
502 ospexit(0);
503 }
504 memset(tmpntr,0,ksz);
505 KEYALL.KEYNAMES = (struct KEY_STRUCT *) tmpntr;
506
507 tmpntr = malloc((size_t)dsz);
508 if (tmpntr == NULL)
509 {
510 (void) printf("could not allocate %d bytes for key_data\n",dsz);
511 ospexit(0);
512 }
513 memset(tmpntr,0,dsz);
514 KEYALL.KEYWORDS = (double *) tmpntr;
515 }
516 else if ((KEYALL.ORIGIN == 1) || (KEYALL.ORIGIN == 11))
517 { /* for monitor reuse old pointers */
518 KEYALL.KEYNAMES = keyname;
519 KEYALL.KEYWORDS = KDWORDS;
520 }
521
522 status = osdread(gp,(char *) KEYALL.KEYNAMES,(unsigned int)keysize);
523 if (status < keysize) goto osd_rerror;
524
525
526 status = osdread(gp,(char *) KEYALL.KEYWORDS,(unsigned int)datsize);
527 if (status < datsize) goto osd_rerror;
528 /*
529 printf("keysize = %d, "datsize = %d\n",keysize,datsize);
530 */
531
532 if (ggp > -1)
533 { /* we must create local FORGRdrs.KEY */
534 (void) osdclose(ggp);
535 gp = osdopen("FORGRdrs.KEY",WRITE);
536 if (gp == -1)
537 { /* we can't use MID_ERROR yet... */
538 (void) printf("Could not create local keyfile FORGRdrs.KEY\n\r");
539 return ERR_KEYBAD;
540 }
541 }
542
543
544 /* initialize the data pointers */
545
546 KDWORDS = KEYALL.KEYWORDS;
547 KIWORDS = (int *) KEYALL.KEYWORDS;
548 KRWORDS = (float *) KEYALL.KEYWORDS;
549 KCWORDS = (char *) KEYALL.KEYWORDS;
550 }
551
552
553 /* --------------------------------*/
554 /* move keywords out */
555 /* --------------------------------*/
556
557 else
558 {
559 status = osdseek(gp,(off_t)0,FILE_START); /* rewind file */
560
561 cpntr = (char *) &KEYALL;
562 status = osdwrite(gp,cpntr,(unsigned int)totsize);
563 if (status < totsize) goto osd_werror;
564
565 keysize = sizeof(struct KEY_STRUCT) * (KEYALL.LOCENT+1);
566 cpntr = (char *) KEYALL.KEYNAMES;
567 status = osdwrite(gp,cpntr,(unsigned int)keysize);
568 if (status < keysize) goto osd_werror;
569
570 datsize = KEYALL.LOCDAT + 2;
571 cpntr = (char *) KEYALL.KEYWORDS;
572 status = osdwrite(gp,cpntr,(unsigned int)datsize);
573 if (status < datsize) goto osd_werror;
574
575 status = osdclose(gp);
576 }
577
578 /* normal end */
579 return ERR_NORMAL;
580
581 osd_rerror:
582 (void) printf("MID_MOVKEY - Problems reading keyfile: %s \n\r",work);
583 osdclose(gp);
584 if (KEYALL.ORIGIN > 9)
585 ospexit(ERR_KEYBAD);
586 else
587 return ERR_KEYBAD;
588
589 osd_werror:
590 status = ERR_KEYBAD; /* here we can use MID_ERROR... */
591 MID_ERROR("MIDAS","MID_MOVKEY",status,0);
592 osdclose(gp);
593 return status;
594 }
595
596 #else
597
598 /*
599
600 */
601
602 /* here the Linux/Unix version */
603
604 int MID_MOVKEY(direc,option)
605
606 /*++++++++++++++++++++++++++++++++++++++++++++++++++
607 .PURPOSE
608 initialize the system keywords from binary file FORGRxy.KEY
609 or write keywords to that file
610 .ALGORITHM
611 either use par. 'option' as keyfile name or build name
612 .RETURNS
613 status: I*4 return status
614 --------------------------------------------------*/
615 char *direc /* IN: [0] = direction of moving, I or O \
616 [1] = M(onitor) or X(tension) or A(pplication) */;
617 char *option /* IN: if not blank, it's already the name of the keyfile
618 if "*" or "**" it's an outside job */;
619
620 {
621 static int initflg = -1;
622 static int gp;
623
624 int ksz, dsz, totsize, keysize, datsize;
625 int status, n, mm;
626
627 char *tmpntr;
628 char buf[4], work[200], *cpntr;
629
630 struct KEY_STRUCT *keyname;
631
632
633
634 totsize = sizeof(struct KEY_ALL);
635
636
637
638 /* --------------------------------*/
639 /* move keywords in */
640 /* --------------------------------*/
641
642 if (*direc++ == 'I') /* already move to direc[1] */
643 {
644 initflg ++;
645 if (*direc == 'A')
646 ksz = 0; /* MIDAS application */
647 else
648 ksz = 1; /* MIDAS monitor */
649
650 keyname = KEYALL.KEYNAMES; /* save pointers in case of need */
651 KDWORDS = KEYALL.KEYWORDS;
652
653 /* work will hold the relevant keyword file name */
654
655 (void) OSY_TRNLOG("MID_WORK",work,168,&mm);
656 if (work[mm-1] != FSY_DIREND) work[mm++] = FSY_DIREND;
657
658 if (*option == ' ') /* use default keyword file */
659 {
660 (void) memcpy(&work[mm],"FORGR .KEY",(size_t)12);
661 OSY_GETSYMB("DAZUNIT",buf,4);
662 work[mm+5] = buf[0];
663 work[mm+6] = buf[1];
664 }
665
666 else if (*option != '*') /* use passed keyword file */
667 (void) strcpy(work,option);
668
669 else
670 {
671 ksz += 10;
672 (void) memcpy(work,"FORGRdrs.KEY",(size_t)13);
673 option++; /* check next char. */
674 if (*option != '*') /* overwrite any FORGdrs.KEY */
675 {
676 gp = -1;
677 goto test_gp; /* avoid opening of file */
678 }
679 }
680
681 gp = osdopen(work,READ_WRITE);
682
683 test_gp:
684 if (gp == -1)
685 {
686 if (ksz >= 10)
687 { /* get FORGRdrs.KEY from MID_MONIT */
688 char cmd[240], cmm[160];
689
690 (void) OSY_TRNLOG("MID_MONIT",cmm,156,&mm);
691 if (strncmp("MID_MONIT",cmm,mm) == 0)
692 { /* we can't use MID_ERROR yet... */
693 (void) printf("variable MID_MONIT is not defined...\n");
694
695 gp = osdopen("FORGRdrs.KEY",READ_WRITE);
696 if (gp == -1)
697 {
698 (void) printf
699 ("also, no existing keyfile here - we give up...\n");
700 return ERR_KEYBAD;
701 }
702 goto keyfile_ok;
703 }
704
705 if (cmm[mm-1] != FSY_DIREND) cmm[mm++] = FSY_DIREND;
706 (void) memcpy(&cmm[mm],"FORGRdrs.KEY",(size_t)13);
707
708 (void) strcpy(cmd,"cp "); /* build up command to copy keyfile */
709 (void) strcpy(&cmd[3],cmm); /* from MID_MONIT to here */
710 (void) strcat(cmd," FORGRdrs.KEY");
711
712 status = system(cmd); /* let HostSyS do the copying */
713 if (status != 0)
714 { /* we can't use MID_ERROR yet... */
715 (void) printf
716 ("System could not copy FORGRdrs.KEY from MID_MONIT to here\n");
717 return ERR_KEYBAD;
718 }
719
720 gp = osdopen(work,READ_WRITE);
721 if (gp == -1)
722 { /* we can't use MID_ERROR yet... */
723 (void) printf("Could not open keyfile %s \n",work);
724 return ERR_KEYBAD;
725 }
726 }
727
728 else
729 {
730 (void) printf("Could not open keyfile %s \n",work);
731 return ERR_KEYBAD;
732 }
733 }
734
735
736 /* read binary keyfile via 3 osdread's */
737
738 keyfile_ok:
739 cpntr = (char *) &KEYALL;
740 status = osdread(gp,cpntr,(unsigned int)totsize);
741 if (status < totsize) goto osd_rerror;
742 /* printf("totsize = %d\n",totsize); */
743
744
745 keysize = sizeof(struct KEY_STRUCT) * (KEYALL.LOCENT+1);
746 datsize = KEYALL.LOCDAT + 2; /* synchronize with midasgo.c */
747 KEYALL.ORIGIN = ksz; /* 0 (applic) or 1 (monitor) */
748
749
750 /* now also check, if we extend the keyword area */
751
752 if (*direc == 'X')
753 {
754 n = KIWORDS[OFF_MONPAR+2]; /* new no. of global keys */
755 mm = KIWORDS[OFF_MONPAR+3]; /* new no. of local keys */
756 KEYALL.GLOBENT = n;
757 KEYALL.GLOBDAT = KEYALL.GLOBENT * KEY_FACT;
758 KEYALL.LOCENT = n + mm;
759 KEYALL.LOCDAT = KEYALL.LOCENT * KEY_FACT;
760 KEYALL.LOCNO = KEYALL.GLOBENT - 1; /* reset local keywords */
761 KEYALL.LOCEND = KEYALL.GLOBDAT - 1;
762 ksz = sizeof(struct KEY_STRUCT) * (KEYALL.LOCENT+1);
763 dsz = KEYALL.LOCDAT + 2;
764
765 cpntr = (char *) KEYALL.KEYNAMES; /* release previous memory */
766 free(cpntr);
767 cpntr = (char *) KEYALL.KEYWORDS;
768 free(cpntr);
769 initflg = 0; /* enforce memory allocation */
770 }
771 else
772 {
773 ksz = keysize;
774 dsz = datsize;
775 }
776
777 if (initflg == 0)
778 {
779 tmpntr = malloc((size_t)ksz);
780 if (tmpntr == NULL)
781 {
782 (void) printf("could not allocate %d bytes for key_names\n",ksz);
783 ospexit(0);
784 }
785 memset(tmpntr,0,ksz);
786 KEYALL.KEYNAMES = (struct KEY_STRUCT *) tmpntr;
787
788 tmpntr = malloc((size_t)dsz);
789 if (tmpntr == NULL)
790 {
791 (void) printf("could not allocate %d bytes for key_data\n",dsz);
792 ospexit(0);
793 }
794 memset(tmpntr,0,dsz);
795 KEYALL.KEYWORDS = (double *) tmpntr;
796 }
797 else if ((KEYALL.ORIGIN == 1) || (KEYALL.ORIGIN == 11))
798 { /* for monitor reuse old pointers */
799 KEYALL.KEYNAMES = keyname;
800 KEYALL.KEYWORDS = KDWORDS;
801 }
802
803 status = osdread(gp,(char *) KEYALL.KEYNAMES,(unsigned int)keysize);
804 if (status < keysize) goto osd_rerror;
805
806
807 status = osdread(gp,(char *) KEYALL.KEYWORDS,(unsigned int)datsize);
808 if (status < datsize) goto osd_rerror;
809 /*
810 printf("keysize = %d, "datsize = %d\n",keysize,datsize);
811 */
812
813
814 /* initialize the data pointers */
815
816 KDWORDS = KEYALL.KEYWORDS;
817 KSWORDS = (size_t *) KEYALL.KEYWORDS;
818 KIWORDS = (int *) KEYALL.KEYWORDS;
819 KRWORDS = (float *) KEYALL.KEYWORDS;
820 KCWORDS = (char *) KEYALL.KEYWORDS;
821 }
822
823
824 /* --------------------------------*/
825 /* move keywords out */
826 /* --------------------------------*/
827
828 else
829 {
830 status = osdseek(gp,(off_t)0,FILE_START); /* rewind file */
831
832 cpntr = (char *) &KEYALL;
833 status = osdwrite(gp,cpntr,(unsigned int)totsize);
834 if (status < totsize) goto osd_werror;
835
836 keysize = sizeof(struct KEY_STRUCT) * (KEYALL.LOCENT+1);
837 cpntr = (char *) KEYALL.KEYNAMES;
838 status = osdwrite(gp,cpntr,(unsigned int)keysize);
839 if (status < keysize) goto osd_werror;
840
841 datsize = KEYALL.LOCDAT + 2;
842 cpntr = (char *) KEYALL.KEYWORDS;
843 status = osdwrite(gp,cpntr,(unsigned int)datsize);
844 if (status < datsize) goto osd_werror;
845
846 status = osdclose(gp);
847 }
848
849 /* normal end */
850 return ERR_NORMAL;
851
852 osd_rerror:
853 (void) printf("MID_MOVKEY - Problems reading keyfile: %s \n\r",work);
854 osdclose(gp);
855 (void) printf("did you execute once \"Midas> @ ascii_bin\" after Midas installation?\n\r");
856 if (KEYALL.ORIGIN > 9)
857 ospexit(ERR_KEYBAD);
858 else
859 return ERR_KEYBAD;
860
861 osd_werror:
862 status = ERR_KEYBAD; /* here we can use MID_ERROR... */
863 MID_ERROR("MIDAS","MID_MOVKEY",status,0);
864 osdclose(gp);
865 return status;
866 }
867
868 #endif
869
870 /*
871
872 */
873
874 /* here the Linux/Unix version using mmap, munmap
875 *
876 * which we abandon for now (060505)
877 * since it gives problems on some systems, e.g.
878 *
879 * HP-Compaq laptop with RedHat,
880 * Pentium with Scientific Linux 4.2
881 *
882 */
883
884 #if Klaus-is-retired
885
886 int MID_MOVKEY(direc,option)
887
888 /*++++++++++++++++++++++++++++++++++++++++++++++++++
889 .PURPOSE
890 initialize the system keywords from binary file FORGRxy.KEY
891 or write keywords to that file
892 .ALGORITHM
893 either use par. 'option' as keyfile name or build name
894 .RETURNS
895 status: I*4 return status
896 --------------------------------------------------*/
897 char *direc /* IN: [0] = direction of moving, I or O \
898 [1] = M(onitor) or X(tension) or A(pplication) */;
899 char *option /* IN: if not blank, it's already the name of the keyfile */;
900
901 {
902 int whoami, keysize, datsize;
903 int status, n, mm;
904 static int initflg = -1;
905 static int gp;
906 static int szkeytotal, szkeyall, szkeynam, szkeydat;
907
908 char action1, action2, buf[4], work[200], *cpntr, *tmpntr;
909 static char *mapbuf;
910
911 double *keywords;
912
913 struct stat statbuf;
914
915 void *mapadr;
916
917
918
919
920 szkeyall = (int) sizeof(struct KEY_ALL);
921 action1 = *direc++; /* already move to direc[1] */
922
923
924 /* --------------------------------*/
925 /* move keywords in */
926 /* --------------------------------*/
927
928 if (action1 == 'I')
929 {
930 initflg ++;
931
932 whoami = 1; /* default to MIDAS monitor */
933 action2 = *direc;
934 if (action2 == 'A') whoami = 0; /* it's a MIDAS application */
935
936 (void) OSY_TRNLOG("MID_WORK",work,168,&mm);
937 if (work[mm-1] != FSY_DIREND) work[mm++] = FSY_DIREND;
938
939 if (*option == ' ') /* use default keyword file */
940 {
941 (void) memcpy(&work[mm],"FORGR .KEY",(size_t)12);
942 OSY_GETSYMB("DAZUNIT",buf,4);
943 work[mm+5] = buf[0];
944 work[mm+6] = buf[1];
945 }
946
947 else if (*option != '*') /* use passed keyword file */
948 (void) strcpy(work,option);
949
950
951 /* for outside-Midas-work, option = '*' and we use FORGRdrs.KEY */
952
953 else
954 {
955 whoami += 10;
956
957 (void) memcpy(&work[mm],"FORGRdrs.KEY",(size_t)13);
958 gp = open(work,O_RDWR);
959 if (gp >= 0)
960 goto get_file_info;
961 else
962 { /* copy FORGRdrs.KEY from MID_MONIT to MID_WORK */
963 char cmd[400], wwork[200];
964
965 (void) OSY_TRNLOG("MID_MONIT",wwork,156,&mm);
966 if (wwork[mm-1] != FSY_DIREND) wwork[mm++] = FSY_DIREND;
967 (void) memcpy(&wwork[mm],"FORGRdrs.KEY",(size_t)13);
968
969 (void) strcpy(cmd,"cp "); /* build up command to copy keyfile */
970 (void) strcpy(&cmd[3],wwork); /* from MID_MONIT to MID_WORK */
971 (void) strcat(cmd," ");
972 (void) strcat(cmd,work);
973
974 status = system(cmd); /* let HostSyS do the copying */
975 if (status != 0)
976 { /* we can't use MID_ERROR yet... */
977 (void) printf
978 ("Could not copy FORGRdrs.KEY from MID_MONIT to MID_WORK dir");
979 return ERR_KEYBAD;
980 }
981 }
982 }
983
984
985 map_open: /* come back here, if keywords extension! */
986 gp = open(work,O_RDWR); /* open file for read/write */
987 if (gp == -1) /* open failed ... */
988 {
989 (void) printf("Could not open keyfile %s \n",work);
990 return ERR_KEYBAD;
991 }
992
993
994 /* whoami = 0/10 (applic) or 1/11 (monitor) */
995
996 get_file_info:
997 keywords = KEYALL.KEYWORDS; /* save pointer in case of need */
998
999 status = fstat(gp,&statbuf); /* get all info about file */
1000 if (status != 0)
1001 {
1002 (void) printf("Could not fstat keyfile %s \n",work);
1003 return ERR_KEYBAD;
1004 }
1005 szkeytotal = statbuf.st_size; /* total file size in bytes */
1006
1007 mapadr = 0;
1008 mapbuf = (char *) mmap(mapadr,(size_t)szkeytotal,PROT_READ | PROT_WRITE,
1009 MAP_SHARED,gp,(off_t)0);
1010 if (mapbuf == (char *) (-1)) /* that's MAP_FAILED */
1011 {
1012 close (gp);
1013 (void) printf("Could not mmap keyfile %s \n",work);
1014 return ERR_KEYBAD;
1015 }
1016
1017 cpntr = (char *) &KEYALL;
1018 (void) memcpy(cpntr,mapbuf,(size_t)szkeyall);
1019
1020 keysize = (int) sizeof(struct KEY_STRUCT)*(KEYALL.LOCENT+1);
1021 datsize = KEYALL.LOCDAT + 2; /* synchronize with midasgo.c!! */
1022
1023 KEYALL.ORIGIN = whoami; /* do not use the one just copied */
1024 /* from mapbuf ... */
1025
1026 /* now also check, if we extend the keyword area */
1027
1028 if (action2 == 'X') /* here only, if we are the Monitor ... */
1029 {
1030 int ksz, dsz;
1031
1032 n = KIWORDS[OFF_MONPAR+2]; /* new no. of global keys */
1033 mm = KIWORDS[OFF_MONPAR+3]; /* new no. of local keys */
1034
1035 munmap(mapbuf,(size_t)szkeytotal); /* unmap + close keyfile */
1036 close(gp); /* we just opened */
1037 tmpntr = (char *) KEYALL.KEYWORDS;
1038 free(tmpntr);
1039
1040 /* now open keyfile as binary file + read "old" KEYALL structure */
1041
1042 gp = osdopen(work,READ_WRITE);
1043 if (gp == -1)
1044 {
1045 (void) printf("MID_MOVKEY + osdopen => we should NOT come here!!!\n");
1046 ospexit(1);
1047 }
1048 status = osdread(gp,(char *) &KEYALL,(unsigned int)szkeyall);
1049 if (status < szkeyall) goto osd_rerror;
1050
1051 KEYALL.GLOBENT = n; /* update KEYALL structure */
1052 KEYALL.GLOBDAT = KEYALL.GLOBENT * KEY_FACT;
1053 KEYALL.LOCENT = n + mm;
1054 KEYALL.LOCDAT = KEYALL.LOCENT * KEY_FACT;
1055 KEYALL.LOCNO = KEYALL.GLOBENT - 1; /* reset local keywords */
1056 KEYALL.LOCEND = KEYALL.GLOBDAT - 1;
1057
1058 ksz = (int) sizeof(struct KEY_STRUCT)*(KEYALL.LOCENT+1);
1059 dsz = KEYALL.LOCDAT + 2; /* new sizes */
1060
1061 cpntr = malloc((size_t)ksz); /* aligned to char */
1062 if (cpntr == NULL)
1063 {
1064 (void) printf("could not allocate %d bytes for key_names\n",ksz);
1065 if (*option == '*')
1066 {
1067 SCTSYS(M_BLUE_COLOR,
1068 "maybe an old, incompatible FORGRdrs.KEY file in MID_WORK?");
1069 SCTSYS(M_BLUE_COLOR,
1070 "copy a platform compatible FORGRdrs.KEY from MID_MONIT to MID_WORK.");
1071 }
1072 ospexit(0);
1073 }
1074
1075 tmpntr = malloc((size_t)dsz); /* will be aligned to double */
1076 if (tmpntr == NULL)
1077 {
1078 (void) printf("could not allocate %d bytes for key_data\n",dsz);
1079 if (*option == '*')
1080 {
1081 SCTSYS(M_BLUE_COLOR,
1082 "maybe an old, incompatible FORGRdrs.KEY file in MID_WORK?");
1083 SCTSYS(M_BLUE_COLOR,
1084 "copy a platform compatible FORGRdrs.KEY from MID_MONIT to MID_WORK.");
1085 }
1086 ospexit(0);
1087 }
1088 KEYALL.KEYWORDS = (double *) tmpntr;
1089
1090 /* now read KEYNAMES + KEYWORDS into new larger buffers */
1091
1092 status = osdread(gp,cpntr,(unsigned int)keysize);
1093 if (status < keysize) goto osd_rerror;
1094 status = osdread(gp,(char *) KEYALL.KEYWORDS,(unsigned int)datsize);
1095 if (status < datsize) goto osd_rerror;
1096
1097 /* and write the larger buffers out - thus increasing the file size */
1098
1099 status = osdseek(gp,(off_t)0,FILE_START); /* rewind file */
1100 status = osdwrite(gp,(char *) &KEYALL,(unsigned int)szkeyall);
1101 if (status < szkeyall) goto osd_werror;
1102 status = osdwrite(gp,cpntr,(unsigned int)ksz);
1103 if (status < ksz) goto osd_werror;
1104 status = osdwrite(gp,(char *) KEYALL.KEYWORDS,(unsigned int)dsz);
1105 if (status < dsz) goto osd_werror;
1106 status = osdclose(gp);
1107 free(cpntr); /* we don't need KEYNAMES buffer anymore */
1108
1109 /* now we use the new, larger keyfile and do again the mapping */
1110
1111 action2 = 'M';
1112 goto map_open;
1113 }
1114
1115
1116
1117 if (initflg == 0) /* here also as an application ... */
1118 {
1119 tmpntr = malloc((size_t)datsize); /* will be aligned to double */
1120 if (tmpntr == NULL)
1121 {
1122 (void)
1123 printf("could not allocate %d bytes for key_data\n",datsize);
1124 if (*option == '*')
1125 {
1126 SCTSYS(M_BLUE_COLOR,
1127 "maybe an old, incompatible FORGRdrs.KEY file in MID_WORK?");
1128 SCTSYS(M_BLUE_COLOR,
1129 "copy a platform compatible FORGRdrs.KEY from MID_MONIT to MID_WORK.");
1130 }
1131 ospexit(0);
1132 }
1133 KEYALL.KEYWORDS = (double *) tmpntr;
1134 }
1135
1136 else if ((KEYALL.ORIGIN == 1) || (KEYALL.ORIGIN == 11))
1137 { /* for monitor reuse old pointers */
1138 KEYALL.KEYWORDS = keywords;
1139 }
1140
1141 KEYALL.KEYNAMES = (struct KEY_STRUCT *) (mapbuf + szkeyall);
1142
1143 szkeynam = keysize;
1144 szkeydat = datsize;
1145
1146 cpntr = (char *) KEYALL.KEYWORDS;
1147 (void) memcpy(cpntr,mapbuf+szkeyall+szkeynam,(size_t)datsize);
1148
1149 KDWORDS = KEYALL.KEYWORDS; /* initialize the data pointers */
1150 KIWORDS = (int *) KEYALL.KEYWORDS;
1151 KRWORDS = (float *) KEYALL.KEYWORDS;
1152 KCWORDS = (char *) KEYALL.KEYWORDS;
1153 }
1154
1155
1156 /* --------------------------------*/
1157 /* move keywords out */
1158 /* --------------------------------*/
1159
1160 else
1161 {
1162 cpntr = (char *) &KEYALL;
1163 (void) memcpy(mapbuf,cpntr,(size_t)szkeyall);
1164 cpntr = (char *) KEYALL.KEYWORDS;
1165 (void) memcpy(mapbuf+szkeyall+szkeynam,cpntr,(size_t)szkeydat);
1166
1167 n = szkeyall + szkeynam + szkeydat;
1168 if (n > szkeytotal)
1169 {
1170 printf("OJO: szkeyall+szkeynam+szkeydat (= %d) > szkeytotal (= %d)\n",
1171 n,szkeytotal);
1172 return ERR_KEYBAD;
1173 }
1174
1175 status = munmap(mapbuf,(size_t)szkeytotal);
1176 if (status != 0)
1177 {
1178 (void) printf("munmap failed...\n");
1179 }
1180 close(gp);
1181 }
1182
1183 /* normal end */
1184 return ERR_NORMAL;
1185
1186 osd_rerror:
1187 (void) printf("MID_MOVKEY - Problems reading keyfile: %s \n",work);
1188 goto closing;
1189 osd_werror:
1190 (void) printf("MID_MOVKEY - Problems writing keyfile: %s \n",work);
1191
1192 closing:
1193 osdclose(gp);
1194 if (KEYALL.ORIGIN > 9)
1195 ospexit(ERR_KEYBAD);
1196 else
1197 return ERR_KEYBAD;
1198 }
1199
1200 #endif
1201