| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1995-2007 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 MIDTERM ++++++++++++++++++++++++++++++++++++ |
| 29 |
.LANGUAGE C |
| 30 |
.IDENTIFICATION MIDTERM |
| 31 |
.AUTHOR |
| 32 |
Klaus Banse |
| 33 |
.COMMENTS |
| 34 |
holds MID_VMEM, MID_VMIO, MID_TINIT, MID_TDISP, MID_TPRO, MID_TTINFO |
| 35 |
.KEYWORDS |
| 36 |
terminal interface routines |
| 37 |
.ENVIRONMENT |
| 38 |
independant. |
| 39 |
|
| 40 |
.VERSION [1.00] 880613: creation - without TermWindow functions |
| 41 |
070316 last modif |
| 42 |
|
| 43 |
------------------------------------------------------------------------*/ |
| 44 |
|
| 45 |
#include <fileexts.h> |
| 46 |
#include <stdio.h> |
| 47 |
#include <stdlib.h> |
| 48 |
#include <osterm.h> |
| 49 |
|
| 50 |
#include <osyparms.h> |
| 51 |
|
| 52 |
#define BC_LEN 120 /* size of b_line */ |
| 53 |
static char b_line[BC_LEN]; |
| 54 |
|
| 55 |
#define V_EXTNO 80 /* was 40 */ |
| 56 |
|
| 57 |
struct VMEM |
| 58 |
{ |
| 59 |
int Vnbl[V_EXTNO]; |
| 60 |
char *Vpntr[V_EXTNO]; |
| 61 |
} myVMEM; |
| 62 |
|
| 63 |
static struct VMEM *Vbase, *Vmem; |
| 64 |
|
| 65 |
static int *Vindx; |
| 66 |
static int V_MAX = 5, Vcount = -1; |
| 67 |
/* |
| 68 |
|
| 69 |
*/ |
| 70 |
|
| 71 |
int MID_VMEM(flag,parm2,fileid) |
| 72 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 73 |
.KEYWORDS |
| 74 |
virtual memory allocation |
| 75 |
.PURPOSE |
| 76 |
create an entry in the VMEM structure and allocate memory |
| 77 |
.ALGORITHM |
| 78 |
use malloc |
| 79 |
.RETURNS |
| 80 |
ERR_NORMAL : all o.k. |
| 81 |
ERR_MEMOUT : could not allocate memory |
| 82 |
: overflow in VMEM struct |
| 83 |
--------------------------------------------------*/ |
| 84 |
|
| 85 |
int flag; /* IN: 1 = "create" memory file; |
| 86 |
2 = "extend" existing memory file; |
| 87 |
3 = close memory file */ |
| 88 |
int parm2; /* IN: no. of blocks to allocate (flag = 1,2); |
| 89 |
chanl (flag = 3) */ |
| 90 |
int *fileid; /* OUT: negative index into Vpntr array */ |
| 91 |
|
| 92 |
{ |
| 93 |
int memsiz, nobl, idx, *vdx; |
| 94 |
unsigned int nono; |
| 95 |
register int nr; |
| 96 |
|
| 97 |
char *cp, *cq; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if (flag == 3) /* close a memory file */ |
| 104 |
{ |
| 105 |
idx = parm2; |
| 106 |
Vmem = Vbase + idx; |
| 107 |
|
| 108 |
for (nr=0; nr<V_EXTNO; nr++) |
| 109 |
{ |
| 110 |
if (Vmem->Vnbl[nr] == 0) break; /* end of chain reached */ |
| 111 |
|
| 112 |
free(Vmem->Vpntr[nr]); |
| 113 |
} |
| 114 |
|
| 115 |
*(Vindx + idx) = 0; /* clear entry */ |
| 116 |
return (ERR_NORMAL); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
nobl = parm2; |
| 122 |
memsiz = nobl * OUR_BLOCK_SIZE; |
| 123 |
|
| 124 |
if (flag == 1) /* create new memory frame */ |
| 125 |
{ |
| 126 |
struct VMEM *newVbase, *newVmem; |
| 127 |
int newmax, *newVindx, *newvdx; |
| 128 |
register int mr; |
| 129 |
|
| 130 |
|
| 131 |
if (Vcount == -1) /* we're here for the very first time */ |
| 132 |
{ |
| 133 |
nono = (unsigned int) (sizeof(myVMEM)*V_MAX + 8); |
| 134 |
cp = malloc((size_t)nono); /* allocate space for V_MAX VMEMs */ |
| 135 |
nono = (unsigned int) (sizeof(int)*V_MAX + 1); |
| 136 |
cq = malloc((size_t)nono); /* allocate space for Vindx array */ |
| 137 |
if ((cp == NULL) || (cq == NULL)) |
| 138 |
{ |
| 139 |
MID_ERROR("OSY","MID_VMEM",ERR_MEMOUT,0); |
| 140 |
return (ERR_MEMOUT); |
| 141 |
} |
| 142 |
|
| 143 |
Vbase = (struct VMEM *) cp; |
| 144 |
Vindx = (int *) cq; |
| 145 |
vdx = Vindx; |
| 146 |
for (nr=0; nr<V_MAX; nr++) *vdx++ = 0; /* clear all entries */ |
| 147 |
Vcount = 0; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
/* find free entry */ |
| 152 |
|
| 153 |
find_free_slot: |
| 154 |
vdx = Vindx; |
| 155 |
for (nr=0; nr<V_MAX; nr++) |
| 156 |
{ |
| 157 |
if (*vdx == 0) |
| 158 |
{ |
| 159 |
cp = malloc((size_t)memsiz); |
| 160 |
if (cp == NULL) |
| 161 |
{ |
| 162 |
MID_ERROR("OSY","MID_VMEM",ERR_MEMOUT,0); |
| 163 |
return (ERR_MEMOUT); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
Vmem = Vbase + nr; |
| 168 |
Vmem->Vpntr[0] = cp; /* save pointer to memory */ |
| 169 |
Vmem->Vnbl[0] = nobl; /* save no blocks */ |
| 170 |
Vmem->Vnbl[1] = 0; /* indicate end of chain */ |
| 171 |
*vdx = 1; /* mark as used */ |
| 172 |
*fileid = -(nr+1); /* fileid = neg. (index+1) */ |
| 173 |
return ERR_NORMAL; |
| 174 |
} |
| 175 |
} |
| 176 |
vdx ++; |
| 177 |
} |
| 178 |
|
| 179 |
/* we're out of VMEMs, so allocate/create new ones */ |
| 180 |
|
| 181 |
newmax = V_MAX * 2; /* double V_MAX */ |
| 182 |
nono = (unsigned int) (sizeof(myVMEM)*newmax + 8); |
| 183 |
cp = malloc((size_t)nono); /* allocate space for V_MAX VMEMs */ |
| 184 |
nono = (unsigned int) (sizeof(int)*newmax + 1); |
| 185 |
cq = malloc((size_t)nono); /* allocate space for Vindx array */ |
| 186 |
if ((cp == NULL) || (cq == NULL)) |
| 187 |
{ |
| 188 |
MID_ERROR("OSY","MID_VMEM",ERR_MEMOUT,0); |
| 189 |
return (ERR_MEMOUT); |
| 190 |
} |
| 191 |
|
| 192 |
newVbase = (struct VMEM *) cp; |
| 193 |
newVindx = (int *) cq; |
| 194 |
|
| 195 |
newvdx = newVindx; |
| 196 |
for (nr=0; nr<newmax; nr++) *newvdx++ = 0; /* clear all new entries */ |
| 197 |
newvdx = newVindx; |
| 198 |
vdx = Vindx; /* and copy old ones */ |
| 199 |
for (nr=0; nr<V_MAX; nr++) *newvdx++ = *vdx++; |
| 200 |
|
| 201 |
Vmem = Vbase; |
| 202 |
newVmem = newVbase; |
| 203 |
for (nr=0; nr<V_MAX; nr++) |
| 204 |
{ |
| 205 |
for (mr=0; mr<V_EXTNO; mr++) |
| 206 |
{ |
| 207 |
newVmem->Vnbl[mr] = Vmem->Vnbl[mr]; |
| 208 |
newVmem->Vpntr[mr] = Vmem->Vpntr[mr]; |
| 209 |
} |
| 210 |
newVmem ++; |
| 211 |
Vmem ++; |
| 212 |
} |
| 213 |
|
| 214 |
(void) free (Vbase); /* free old memory */ |
| 215 |
(void) free (Vindx); |
| 216 |
Vbase = newVbase; /* and use new structure */ |
| 217 |
Vindx = newVindx; |
| 218 |
V_MAX = newmax; |
| 219 |
goto find_free_slot; |
| 220 |
} |
| 221 |
|
| 222 |
else /* extend given memory frame */ |
| 223 |
{ |
| 224 |
cp = malloc((size_t)memsiz); |
| 225 |
if (cp == NULL) |
| 226 |
{ |
| 227 |
MID_ERROR("OSY","MID_VMEM",ERR_MEMOUT,0); |
| 228 |
return (ERR_MEMOUT); |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
nr = -1 - (*fileid); |
| 233 |
Vmem = Vbase + nr; |
| 234 |
|
| 235 |
for (nr=0; nr<V_EXTNO; nr++) /* max V_EXTNO extensions */ |
| 236 |
{ |
| 237 |
if (Vmem->Vnbl[nr] == 0) |
| 238 |
{ |
| 239 |
Vmem->Vpntr[nr] = cp; /* add new pointer */ |
| 240 |
Vmem->Vnbl[nr] = nobl; /* save no blocks */ |
| 241 |
idx = nr + 1; |
| 242 |
if (idx < V_EXTNO) Vmem->Vnbl[idx] = 0; /* indicate end of chain */ |
| 243 |
return ERR_NORMAL; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
MID_ERROR("OSY","MID_VMEM",2,0); |
| 250 |
return (ERR_MEMOUT); /* no free slot found */ |
| 251 |
} |
| 252 |
/* |
| 253 |
|
| 254 |
*/ |
| 255 |
|
| 256 |
int MID_VMIO(flag,idx,pbuf,nobyt,vbn) |
| 257 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 258 |
|
| 259 |
.PURPOSE write virtual blocks to disk or memory |
| 260 |
.RETURNS midas error code |
| 261 |
.REMARKS uses the os interface service osdwrite. |
| 262 |
|
| 263 |
-------------------------------------------------------------------------*/ |
| 264 |
|
| 265 |
int flag; /* IN : = 1 write; = 0 read */ |
| 266 |
int idx; /* IN : index into Vmem struct */ |
| 267 |
char *pbuf; /* IN : pointer to the buffer to write */ |
| 268 |
unsigned int nobyt; /* IN : number of bytes to be transferred */ |
| 269 |
int vbn; /* IN : starting virtual block for data transfer */ |
| 270 |
|
| 271 |
{ |
| 272 |
int kbl, mm, vsize, vbegi, vend; |
| 273 |
register int nr; |
| 274 |
unsigned int outof, amount; |
| 275 |
|
| 276 |
char *cp; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
Vmem = Vbase + idx; |
| 281 |
amount = outof = 0; |
| 282 |
kbl = 0; |
| 283 |
vbegi = 1; |
| 284 |
|
| 285 |
for (nr=0; nr<V_EXTNO; nr++) |
| 286 |
{ |
| 287 |
vsize = Vmem->Vnbl[nr]; |
| 288 |
vend = vbegi + vsize - 1; /* last block here */ |
| 289 |
if (vbn <= vend) |
| 290 |
{ |
| 291 |
mm = vbn - vbegi; |
| 292 |
cp = Vmem->Vpntr[nr] + (mm * OUR_BLOCK_SIZE); |
| 293 |
kbl = vsize - mm; |
| 294 |
amount = kbl * OUR_BLOCK_SIZE; |
| 295 |
if (amount > nobyt) amount = nobyt; |
| 296 |
|
| 297 |
if (flag == 1) /* writing */ |
| 298 |
memcpy(cp,pbuf+outof,(size_t)amount); |
| 299 |
else /* reading */ |
| 300 |
memcpy(pbuf+outof,cp,(size_t)amount); |
| 301 |
|
| 302 |
nobyt -= amount; |
| 303 |
if (nobyt < 1) return (ERR_NORMAL); /* we're done */ |
| 304 |
} |
| 305 |
|
| 306 |
vbn += kbl; |
| 307 |
vbegi += vsize; |
| 308 |
outof += amount; |
| 309 |
} |
| 310 |
|
| 311 |
return (-1); |
| 312 |
} |
| 313 |
/* |
| 314 |
|
| 315 |
*/ |
| 316 |
|
| 317 |
void MID_TINIT() |
| 318 |
|
| 319 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 320 |
.KEYWORDS |
| 321 |
terminal |
| 322 |
.PURPOSE |
| 323 |
initialize constants |
| 324 |
.ALGORITHM |
| 325 |
.RETURNS |
| 326 |
status: I*4 return status |
| 327 |
--------------------------------------------------*/ |
| 328 |
|
| 329 |
{ |
| 330 |
register int nr; |
| 331 |
|
| 332 |
for (nr=0; nr<(BC_LEN-1); nr++) |
| 333 |
b_line[nr] = '\b'; /* fill with back_spaces */ |
| 334 |
|
| 335 |
b_line[BC_LEN-1] = '\0'; |
| 336 |
} |
| 337 |
/* |
| 338 |
|
| 339 |
*/ |
| 340 |
|
| 341 |
void MID_TDISP(string,b_count,lstr) |
| 342 |
|
| 343 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 344 |
.KEYWORDS |
| 345 |
terminal |
| 346 |
.PURPOSE |
| 347 |
put text on terminal and position cursor somewhere in same line |
| 348 |
.ALGORITHM |
| 349 |
no big deal |
| 350 |
.RETURNS |
| 351 |
nothing |
| 352 |
--------------------------------------------------*/ |
| 353 |
|
| 354 |
char *string; /* IN: string to be displayed */ |
| 355 |
int b_count; /* IN: no. of backspaces to do */ |
| 356 |
int lstr; /* IN: length of string */ |
| 357 |
|
| 358 |
{ |
| 359 |
int m; |
| 360 |
|
| 361 |
char mystr[BC_LEN]; |
| 362 |
|
| 363 |
m = BC_LEN - 1; /* avoid buffer overflow */ |
| 364 |
if (lstr > m) |
| 365 |
{ |
| 366 |
lstr = m; |
| 367 |
if (b_count > m) b_count = m; |
| 368 |
} |
| 369 |
|
| 370 |
(void) memcpy(mystr,string,(size_t)lstr); |
| 371 |
mystr[lstr] = '\0'; |
| 372 |
|
| 373 |
|
| 374 |
if (b_count == m) /* max size '\0' already in place */ |
| 375 |
{ |
| 376 |
printf("%s",mystr); |
| 377 |
printf("%s",b_line); |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
printf("%s",mystr); |
| 382 |
b_line[b_count] = '\0'; |
| 383 |
printf("%s",b_line); |
| 384 |
b_line[b_count] = '\b'; |
| 385 |
} |
| 386 |
|
| 387 |
fflush(stdout); /* flush out line */ |
| 388 |
} |
| 389 |
/* |
| 390 |
|
| 391 |
*/ |
| 392 |
|
| 393 |
void MID_TPRO(prompt,line,len) |
| 394 |
|
| 395 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 396 |
.KEYWORDS |
| 397 |
terminal |
| 398 |
.PURPOSE |
| 399 |
prompt for + get an input line from terminal |
| 400 |
.ALGORITHM |
| 401 |
standard C functions |
| 402 |
.RETURNS |
| 403 |
len: I*4 length of input line |
| 404 |
--------------------------------------------------*/ |
| 405 |
|
| 406 |
char *prompt; /* IN: prompt string (terminated by \0) */ |
| 407 |
char *line; /* OUT: input line (terminated by \0) */ |
| 408 |
int len; /* IN: length of buffer 'line' */ |
| 409 |
|
| 410 |
{ |
| 411 |
|
| 412 |
(void) printf("%s ",prompt); |
| 413 |
CGN_GETLINE(line,len); |
| 414 |
|
| 415 |
} |
| 416 |
/* |
| 417 |
|
| 418 |
*/ |
| 419 |
|
| 420 |
void MID_TTINFO(tcols,tlines) |
| 421 |
int *tcols, *tlines; |
| 422 |
|
| 423 |
{ |
| 424 |
int tc; |
| 425 |
|
| 426 |
struct termstatus tstat; |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
*tcols = 80; /* default to 80 char, 24 lines window */ |
| 431 |
*tlines = 24; |
| 432 |
|
| 433 |
|
| 434 |
/* get no. of columns in terminal window */ |
| 435 |
|
| 436 |
if (ostinfo(&tstat) == 0) |
| 437 |
{ /* successful call */ |
| 438 |
tc = tstat.columns; |
| 439 |
if (tc < 8) /* keep "reasonable" no. of columns */ |
| 440 |
tc = 8; |
| 441 |
else if (tc > 127) |
| 442 |
tc = 127; |
| 443 |
*tcols = tc; |
| 444 |
*tlines = tstat.lines; |
| 445 |
} |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
|