Debian Codegraph @

libsrc/st/scsa.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2013 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 /*+++++++++++++++++++++++++++ SC interface module SCSA +++++++++++++++++++++
29 ..LANGUAGE C
30 ..IDENTIFICATION Module SCSA
31 ..AUTHOR K. Banse ESO - Garching 870721
32 ..KEYWORDS standard interfaces.
33 ..ENVIRONMENT theoretically environment independant.
34 ..COMMENTS holds SCSKIN - routine to read in the MIDAS keywords
35
36 ..VERSION [1.00] 920212: Creation
37 130120 last modif
38
39 -----------------------------------------------------------------------------*/
40
41 #include <fileexts.h>
42 #include <osyparms.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45
46
47
48 int SCSKIN(fname)
49 char *fname;
50
51 {
52 int actval, bytelem, status, i, k, from, in;
53 int byt_len, tot_len, n, noelem, pad, odd;
54 int prflag, slen, start, namelen, reclen;
55 int ikey[40], fp, unit, keysize, datsize;
56 register int nr;
57
58 float rkey[40];
59
60 double dkey[40];
61
62 char name[18], record[128], type[20], k_typ;
63 char devices[28], user[24];
64 char *tmpntr;
65
66 static struct KEY_STRUCT *keypntr;
67
68
69
70
71
72 i = KEY_MAXENT;
73 k = LOC_MAXENT;
74
75 keysize = (int)sizeof(struct KEY_STRUCT) * (i+k+1); /* + 1 for security */
76 tmpntr = (char *)malloc((size_t) keysize);
77 if (tmpntr == (char *) 0)
78 {
79 (void) printf("could not allocate %d bytes for key_names\n",keysize);
80 ospexit(0);
81 }
82 else
83 memset((void *)tmpntr,0,(size_t)keysize); /* init all memory */
84
85 KEYALL.KEYNAMES = (struct KEY_STRUCT *) tmpntr;
86
87 KEYALL.GLOBENT = i;
88 KEYALL.GLOBDAT = KEYALL.GLOBENT * KEY_FACT; /* counted in bytes */
89 KEYALL.GLOBNO = -1;
90 KEYALL.GLOBEND = -1;
91 KEYALL.LOCENT = i + k;
92 KEYALL.LOCDAT = KEYALL.LOCENT * KEY_FACT;
93 KEYALL.LOCNO = KEYALL.GLOBENT -1;
94 KEYALL.LOCEND = KEYALL.GLOBDAT - 1;
95
96 datsize = KEYALL.LOCDAT + 2; /* for security */
97 tmpntr = (char *)malloc((size_t) datsize);
98 if (tmpntr == (char *) 0)
99 {
100 (void) printf("could not allocate %d bytes for key_data\n",datsize);
101 ospexit(0);
102 }
103 else
104 memset((void *)tmpntr,0,(size_t)datsize); /* init all memory */
105
106 KEYALL.KEYWORDS = (double *) tmpntr;
107
108 prflag = -1;
109 keypntr = KEYALL.KEYNAMES;
110
111 KDWORDS = KEYALL.KEYWORDS;
112 KRWORDS = (float *) KDWORDS;
113 KIWORDS = (int *) KDWORDS;
114 KCWORDS = (char *) KDWORDS;
115
116
117 /* OFF_MODE = 0, offset of 1st keyword MODE... so OFF_MODE+6 = 6 */
118
119 KEYALL.KEYNAMES[0].OFFSET = 0;
120 KIWORDS[6] = 0; /* prog_level has to be set for MID_FNDKEY */
121 KEYALL.SYSNO = 40; /* keyw. DATTIM (#41) is the last */
122
123 MID_INITER(); /* init error business */
124
125
126 /* open passed keyword file */
127
128 fp = CGN_OPEN(fname,READ); /* open for reading */
129 if (fp < 0)
130 {
131 (void) printf("We couldn't open the Midas keyfile %s ...\n",fname);
132 ospexit(1);
133 }
134
135
136 /* read first keyname, type, length */
137
138 read_loop:
139 reclen = osaread(fp,record,132);
140
141 if (reclen < 0) /* EOF reached */
142 osaclose(fp);
143
144 else if (reclen == 0) /* empty line */
145 goto read_loop;
146
147 else
148 {
149 if (record[reclen-1] == 13)
150 {
151 if (reclen == 1)
152 goto read_loop;
153 else
154 reclen --;
155 }
156 if ( (record[0] == 'C') && /* skip comment lines */
157 ( (record[1] == ' ') || (record[1] == '\t') || (reclen == 1) ) )
158 goto read_loop;
159
160 start = 0; /* valid keyword encountered */
161 n = KEY_NAMELEN;
162 namelen = CGN_EXTRSS(record,reclen,'/',&start,name,n);
163 n = 20;
164 slen = CGN_EXTRSS(record,reclen,'/',&start,type,n);
165 type[19] = '\0';
166 slen = CGN_EXTRSS(record,reclen,'/',&start,user,n);
167 user[19] = '\0';
168 (void) sscanf(user,"%d",&actval);
169
170 MID_TYPCHK(type,&k_typ,&bytelem); /* check type of keyword */
171 if (k_typ == ' ')
172 {
173 status = ERR_KEYBAD;
174 goto sect_5000;
175 }
176
177 if (actval <= 0) /* test length of keyword values */
178 {
179 status = ERR_INPINV;
180 goto sect_5000;
181 }
182
183 noelem = actval; /* get noelem */
184 if ( (k_typ == 'C') && (bytelem == 0) )
185 {
186 bytelem = actval;
187 noelem = 1;
188 }
189
190
191 if (KEYALL.GLOBNO >= KEYALL.GLOBENT-1) /* check, if KEYNAME full */
192 { /* or keyname already exists */
193 status = ERR_KEYOVN;
194 goto sect_5000;
195 }
196
197 status = MID_FNDKEY(name,record,&n,&n,&unit);
198 if (status != -1) /* check, if already defined */
199 {
200 status = ERR_KEYBAD;
201 goto sect_5000;
202 }
203
204
205 /* calculate length in bytes + eventual front padding bytes */
206
207 if (k_typ == 'I')
208 {
209 pad = II_SIZE - 1 - (KEYALL.GLOBEND % II_SIZE);
210 byt_len = noelem * II_SIZE;
211 }
212 else if (k_typ == 'R')
213 {
214 pad = RR_SIZE - 1 - (KEYALL.GLOBEND % RR_SIZE);
215 byt_len = noelem * RR_SIZE;
216 }
217 else if (k_typ == 'C')
218 {
219 pad = 0;
220 byt_len = noelem*bytelem;
221 }
222 else /* only (k_typ == 'D') possible */
223 {
224 pad = DD_SIZE - 1 - (KEYALL.GLOBEND % DD_SIZE);
225 byt_len = noelem * DD_SIZE;
226 }
227
228 if (KEYALL.GLOBNO < 0) pad = 0; /* fix for very first time */
229
230 tot_len = pad + byt_len;
231 if (KEYALL.GLOBEND+tot_len > KEYALL.GLOBDAT)
232 {
233 status = ERR_KEYOVL;
234 goto sect_5000;
235 }
236
237
238 /* everything o.k. - do what's necessary */
239
240 KEYALL.GLOBNO ++;
241 memset((void *)keypntr->IDENT,32,(size_t)17);
242 (void) memcpy(keypntr->IDENT,name,(size_t)namelen);
243 keypntr->IDENT[15] = k_typ;
244 keypntr->BYTELEM = (short int) bytelem;
245 keypntr->NOELEM = noelem;
246 keypntr->LEN = byt_len;
247 keypntr->FRPAD = (short int) pad;
248 keypntr->UNIT = 0; /* not used yet */
249 in = KEYALL.GLOBEND + 1; /* point to next byte */
250
251
252 /* read data into corresponding keyword */
253
254 if (k_typ == 'I')
255 {
256 in = (in + pad) / II_SIZE; /* include front padding */
257 from = in;
258
259 i_loop:
260 reclen = osaread(fp,record,132);
261 if (reclen < 0)
262 goto no_data;
263 else if (reclen == 0) /* empty line */
264 goto i_loop;
265 if (record[reclen-1] == 13) reclen --;
266
267 slen = CGN_CNVT(record,1,noelem,ikey,rkey,dkey);
268 if (slen < 0) goto no_data;
269
270 for (nr=0; nr<slen; nr++) KIWORDS[from++] = ikey[nr];
271
272 noelem = noelem - slen;
273 if (noelem > 0) goto i_loop;
274
275 if (prflag == -1) prflag = ikey[1]; /* set it to MODE(2) */
276 }
277
278 else if (k_typ == 'R')
279 {
280 in = (in + pad) / RR_SIZE; /* include front padding */
281 from = in;
282
283 r_loop:
284 reclen = osaread(fp,record,132);
285 if (reclen < 0)
286 goto no_data;
287 else if (reclen == 0) /* empty line */
288 goto r_loop;
289 if (record[reclen-1] == 13) reclen --;
290
291 slen = CGN_CNVT(record,2,noelem,ikey,rkey,dkey);
292 if (slen < 0) goto no_data;
293
294 for (nr=0; nr<slen; nr++) KRWORDS[from++] = rkey[nr];
295
296 noelem = noelem - slen;
297 if (noelem > 0) goto r_loop;
298 }
299
300 else if (k_typ == 'C')
301 {
302 from = in; /* we can start right away */
303 actval = noelem * bytelem;
304 odd = 0; /* used as switch here */
305
306 c_loop:
307 reclen = osaread(fp,record,132);
308 if (reclen < 0)
309 goto no_data;
310 else if (reclen == 0) /* empty line */
311 goto c_loop;
312 if (record[reclen-1] == 13) reclen --;
313
314 if (odd > 0) /* only after very first follow up is read */
315 {
316 if ( (record[0] == 'C') &&
317 ( (record[1] == ' ') || (reclen == 1) ) )
318 {
319 memset((void *)(KCWORDS+from),32,(size_t)actval);
320 reclen = actval;
321 goto c_check;
322 }
323 }
324
325 (void) memcpy(KCWORDS+from,record,(size_t)reclen);
326 from += reclen;
327 odd ++;
328
329 c_check:
330 actval -= reclen;
331 if (actval > 0) goto c_loop; /* more records to read ... */
332 }
333
334 else if (k_typ == 'D')
335 {
336 in = (in + pad) / DD_SIZE; /* include front padding */
337 from = in;
338
339 d_loop:
340 reclen = osaread(fp,record,132);
341 if (reclen < 0)
342 goto no_data;
343 else if (reclen == 0) /* empty line */
344 goto d_loop;
345 if (record[reclen-1] == 13) reclen --;
346
347 slen = CGN_CNVT(record,4,noelem,ikey,rkey,dkey);
348 if (slen < 0) goto no_data;
349
350 for (nr=0; nr<slen; nr++) KDWORDS[from++] = dkey[nr];
351
352 noelem = noelem - slen;
353 if (noelem > 0) goto d_loop;
354 }
355
356
357 /* store offset already in type-units + increment pointer KEYALL->GLOBEND */
358
359 keypntr->OFFSET = in;
360 KEYALL.GLOBEND += tot_len;
361
362 if (prflag == 1) (void) printf("keyword %8s added\n",name);
363
364 keypntr ++;
365 goto read_loop;
366 }
367
368
369 /* fill keyword MID$SESS with image system unit */
370
371 (void) memcpy(devices,KCWORDS+OFF_SESS,25);
372 devices[25] = '\0';
373 memset((void *)record,32,(size_t)5);
374 OSY_GETSYMB("I_TYPE",record,6);
375 (void) strncpy(devices,record,5);
376 OSY_GETSYMB("MIDOPTION",record,6);
377 if ((record[0] == 'P') || (record[0] == 'p'))
378 devices[14] = 'P';
379 else
380 devices[14] = ' ';
381
382
383 /* in VMS test, if we are "submitted..." */
384
385 KIWORDS[OFF_MODE+2] = 0; /* interactive mode */
386
387 #if vms
388 OSY_GETSYMB("BATCH",record,4);
389 if (record[0] == 'Y')
390 {
391 (void) printf("we are in batch mode...\r\n");
392 KIWORDS[OFF_MODE+2] = 1; /* batch mode */
393 }
394 #endif
395
396
397 /* initialize logfile */
398
399 OSY_GETSYMB("DAZUNIT",record,4); /* get MIDAS unit */
400
401 status = MID_LOG('S',record,2);
402 if (status != ERR_NORMAL)
403 {
404 (void) printf("We could not start up the Midas logfile...\r\n");
405 ospexit(1);
406 }
407
408
409 /* continue filling up key MID$SESS */
410
411 devices[10] = record[0];
412 devices[11] = record[1];
413 devices[12] = ' ';
414 devices[13] = ' ';
415
416 unit = 3;
417 status = SCKWRC("MID$SESS",1,devices,1,25,&unit);
418 if (status != ERR_NORMAL) goto sect_5000;
419
420
421 /* get name of MIDAS user */
422
423 if (KIWORDS[OFF_MODE+2] == 1)
424 (void) strcpy(user,"MIDAS_JOB "); /* batch mode */
425 else
426 OSY_GETSYMB("USER",user,24);
427
428 status = SCKWRC("USER",1,user,1,20,&unit);
429 if (status == ERR_NORMAL) return status;
430
431
432 sect_5000:
433 (void) printf("problems in writing keyword %s\n\r",name);
434 ospexit(1);
435
436 no_data:
437 (void) printf("invalid data for keyword %s\n\r",name);
438 ospexit(1);
439 return ERR_NORMAL; /* like in SCSEPI ... */
440 }
441