~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to monit/prepa2.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: midas@eso.org
 
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
/*+++++++++++++++   MIDAS monitor routines PREPA2  +++++++++++++++++++++++++
 
29
.LANGUAGE      C
 
30
.IDENTIFICATION  Module PREPA2
 
31
.AUTHOR  K. Banse                       ESO - Garching
 
32
.KEYWORDS  MIDAS monitor
 
33
.ENVIRONMENT  VMS and UNIX
 
34
.PURPOSE
 
35
synchronize fore- and background processes; 
 
36
get the MIDAS command string + take action accordingly
 
37
.ALGORITHM
 
38
loop on command input (from terminal or procedure file),
 
39
test for background activity and synchronize
 
40
system commands are executed directly,
 
41
all others are executed as command procedures
 
42
 
 
43
holds Parse3, TTSET, TTINIT, TTEDIT, TTPRO
 
44
 
 
45
.VERSION 
 
46
 
 
47
 100608         last modif
 
48
------------------------------------------------------------------------*/
 
49
 
 
50
#define _POSIX_SOURCE 1         /* indicate that this is a POSIX program */
 
51
 
 
52
#define OLDTOKMAX 62            /* size of OLDTOKENs is fixed to 64 */
 
53
 
 
54
#include <stdio.h>
 
55
#include <strings.h>
 
56
#include <fileexts.h>
 
57
#include <signal.h>
 
58
 
 
59
#ifndef SIGUSR1
 
60
#define SIGUSR1 30
 
61
#endif
 
62
#ifndef SIGUSR2
 
63
#define SIGUSR2 31
 
64
#endif
 
65
 
 
66
#include <osyparms.h>
 
67
#include <monitdef.h>
 
68
#include <midfront.h>
 
69
 
 
70
#if vms                         /* FO's terminal stuff only used in VMS */
 
71
#define TW_import 0
 
72
#include <tw.h>
 
73
#include <osterm.h>
 
74
#endif
 
75
 
 
76
#include <ok.h>
 
77
#ifndef EOF
 
78
#define EOF   (-1)     /* End of File */
 
79
#endif
 
80
 
 
81
static char   wstr[84];
 
82
static char   blank = {' '};
 
83
 
 
84
#ifndef NO_READLINE
 
85
#define HAVE_STRING_H
 
86
#include <setjmp.h>
 
87
#include <readline/readline.h>          /* see changes in re_edit () */
 
88
#include <stdlib.h>
 
89
#include <string.h>
 
90
#include <osxdef.h>             /* MIDAS osx definitions */
 
91
#include <midback.h>            /* Context extructure */
 
92
 
 
93
extern int  is_a_tty;           /* Is this a terminal, (yes=1) set in prepa.c */
 
94
 
 
95
void using_history();
 
96
int  read_history(), rl_add_funmap_entry();
 
97
 
 
98
 
 
99
static char contxt_name[MAX_CONTXT*8+2];        /* Activated contexts */
 
100
static char acknowledge;
 
101
static char buffer[20];                         /* command for xhelp */
 
102
static char   *line_read;
 
103
static char   history_file[200];   /* FRONT.STARTUP+".history."+FRONT.DAZUNIT */
 
104
static sigjmp_buf  env;
 
105
/*
 
106
 
 
107
*/
 
108
 
 
109
/* Contains the line to push into readline. */
 
110
static char *push_to_readline = (char *)NULL;
 
111
static Function *old_rl_startup_hook = (Function *) NULL;
 
112
 
 
113
 
 
114
/* Push the contents of push_to_readline into the readline buffer. */
 
115
static void push_line ()
 
116
{
 
117
  if (push_to_readline)
 
118
    {
 
119
      rl_insert_text (push_to_readline);
 
120
      free (push_to_readline);
 
121
      push_to_readline = (char *)NULL;
 
122
      rl_startup_hook = old_rl_startup_hook;
 
123
    }
 
124
}
 
125
 
 
126
/* Call this to set the initial text for the next line to read from readline. */
 
127
int re_edit (line)
 
128
char *line;
 
129
{
 
130
  char  *xmalloc();
 
131
 
 
132
  if (push_to_readline)
 
133
    free (push_to_readline);
 
134
 
 
135
  push_to_readline = xmalloc (1 + strlen (line));
 
136
  (void) strcpy (push_to_readline,line);
 
137
 
 
138
  old_rl_startup_hook = rl_startup_hook;
 
139
  rl_startup_hook = (Function *)push_line;
 
140
 
 
141
  return (0);
 
142
}
 
143
 
 
144
struct COMMAND {
 
145
  char name[12];                /* User printable name of the function */
 
146
  struct COMMAND *prev; 
 
147
  };
 
148
 
 
149
struct CMD_LIST {
 
150
  struct COMMAND *curr;
 
151
  struct COMMAND *last;
 
152
  };
 
153
 
 
154
extern struct CMD_LIST cmds;
 
155
 
 
156
static int xhelp_fd = -1; 
 
157
static int xhelp_pid = 0;
 
158
static char *channame[2] = { (char *)NULL, (char *)NULL };
 
159
 
 
160
static int initialize_xhelp()
 
161
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
162
.PURPOSE Initialize channname, containing socket name and
 
163
         filename crated by XHelp.exe containing its pid.
 
164
         It opens a server connection.
 
165
         Initialize the contxt_name string to check for 
 
166
         context changes.
 
167
.RETURNS 0 OK, -1 otherwise.
 
168
------------------------------------------------------------*/
 
169
{
 
170
char *mid_work, *dazunit;
 
171
int n;
 
172
 
 
173
mid_work = getenv("MID_WORK");
 
174
 
 
175
channame[1] = (char *)
 
176
  malloc((size_t)(strlen(mid_work)+strlen("xhelp")+2+strlen("_pid")+1));
 
177
 
 
178
dazunit = getenv("DAZUNIT");
 
179
 
 
180
(void) strcpy(channame[1],mid_work);
 
181
(void) strcat(channame[1],"xhelp");
 
182
(void) strcat(channame[1],dazunit);
 
183
(void) strcat(channame[1],"_pid");
 
184
 
 
185
n = (int) strlen(channame[1]) - (int) strlen("_pid");
 
186
channame[0] = (char *) malloc((size_t)(n+1));
 
187
(void) strncpy(channame[0],channame[1],n);
 
188
channame[0][n] = '\0';
 
189
 
 
190
xhelp_fd = osxopen(channame,LOCAL|IPC_READ);
 
191
if (xhelp_fd < 0) 
 
192
   {
 
193
   (void) printf("\n\rCannot create server for GUI XHelp.exe.\n\r");
 
194
   (void) printf("Error message: %s\n\r",osmsg());
 
195
   free(channame[0]);
 
196
   free(channame[1]);
 
197
   xhelp_fd = -1;
 
198
   return(-1);
 
199
   }
 
200
 
 
201
/* Initialize the context_name and buffer strings */
 
202
memset(contxt_name,' ',MAX_CONTXT*8);   /* clear context names */
 
203
memset(buffer,'\0',20);         /* Initiate command name */
 
204
contxt_name[MAX_CONTXT*8] = '\0';       /* make sure we have an end marker */
 
205
 
 
206
return(0);
 
207
}
 
208
 
 
209
void close_xhelp()
 
210
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
211
.PURPOSE Close socket and free allocated memory.
 
212
         This routine is called by prepa.c after the BYE command.
 
213
.RETURNS void
 
214
------------------------------------------------------------*/
 
215
{
 
216
if (xhelp_fd != (-1))
 
217
   {
 
218
   (void) osxclose(xhelp_fd);
 
219
   (void) free(channame[0]);
 
220
   (void) free(channame[1]);
 
221
   }
 
222
}
 
223
 
 
224
Function *gui_xhelp()
 
225
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
226
.PURPOSE send messages to the GUI XHELP.
 
227
.RETURNS nothing
 
228
------------------------------------------------------------*/
 
229
{
 
230
  int len;
 
231
  char *start_buffer;
 
232
  FILE *fd;
 
233
 
 
234
if (xhelp_fd == (-1)) return 0;
 
235
 
 
236
 
 
237
  /* retrieve the pid number from XHELP */
 
238
  if (!xhelp_pid) {
 
239
    if ( (fd = fopen(channame[1],"r")) == (FILE *)NULL) {
 
240
      (void) printf("\n\rTry first: CREATE/GUI HELP \n\r");
 
241
      rl_refresh_line();
 
242
      return 0;
 
243
      }
 
244
    fscanf(fd,(const char *) "%d\n",&xhelp_pid);
 
245
    fclose(fd);
 
246
    if (kill(xhelp_pid,0) == -1) {
 
247
      xhelp_pid = 0;
 
248
      unlink(channame[1]);
 
249
      (void) printf("\n\rTry first: CREATE/GUI HELP \n\r");
 
250
      rl_refresh_line();
 
251
      return 0;
 
252
      }
 
253
    }
 
254
 
 
255
  /* Prepare the message to be sent to XHELP not bigger than 12 chars */
 
256
  /* Strip leading blanks */
 
257
  start_buffer = rl_line_buffer;
 
258
  while ( *start_buffer == ' ') start_buffer++;
 
259
   
 
260
  len = strlen(start_buffer);
 
261
  len = (len > 19)? 19 : len;
 
262
 
 
263
 
 
264
  strncpy(buffer,start_buffer,len);
 
265
  buffer[len] = '\0';
 
266
  if (buffer[len-1] == '/') buffer[len-1] = '\0';
 
267
 
 
268
 /* Check first if CONTEXT list has changed, if so then send USR2 */
 
269
 if (strcmp(contxt_name,CONTXT.NAME)) {
 
270
    (void) strcpy(contxt_name,CONTXT.NAME);
 
271
    /* (void) printf("sending SIGUSR2\n"); */
 
272
    if (osssend(xhelp_pid,SIGUSR2) == -1) {
 
273
      xhelp_pid = 0;
 
274
      return 0;
 
275
      }
 
276
    while (osxinfo(xhelp_fd,0,0) == NOCONN) ;
 
277
    /* (void) printf("sending context=%s\n",contxt_name); */
 
278
    if (osxwrite(xhelp_fd,contxt_name,MAX_CONTXT*8 + 2) != MAX_CONTXT*8 + 2) 
 
279
      if (osxwrite(xhelp_fd,contxt_name,MAX_CONTXT*8 + 2) != MAX_CONTXT*8 + 2) {
 
280
        xhelp_pid = 0;
 
281
        return 0;
 
282
        }
 
283
    acknowledge = 0;
 
284
    if (osxread(xhelp_fd,&acknowledge,1) != 1 && acknowledge != 1) {
 
285
      xhelp_pid = 0;
 
286
      return 0;
 
287
      }
 
288
    }
 
289
 
 
290
  /* 
 
291
   * Send the message to XHELP , and then interrupt it with USR1 signal.
 
292
   * If error, the connection is broken.
 
293
   */
 
294
  /* printf("sending SIGUSR1\n"); */
 
295
  if (osssend(xhelp_pid,SIGUSR1) == -1) {
 
296
    xhelp_pid = 0;
 
297
    return 0;
 
298
    }
 
299
  /* printf("osxinfo=%d\n",osxinfo(xhelp_fd,0,0)); */
 
300
  while (osxinfo(xhelp_fd,0,0) == NOCONN) ;
 
301
  /* printf("sending command=%s\n",buffer); */
 
302
  if (osxwrite(xhelp_fd,buffer,20) != 20 ) 
 
303
    if (osxwrite(xhelp_fd,buffer,20) != 20 ) {
 
304
      xhelp_pid = 0;
 
305
      return 0;
 
306
      }
 
307
  acknowledge = FALSE;
 
308
  if (osxread(xhelp_fd,&acknowledge,1) != 1 && acknowledge != 1) {
 
309
    xhelp_pid = 0;
 
310
    return 0;
 
311
    }
 
312
return 0;
 
313
}
 
314
 
 
315
/* Tell the GNU Readline library how to complete. We want to try to complete
 
316
 * on command names if this is the first word in the line, or on filenames
 
317
 * if not.
 
318
 */
 
319
static void initialize_readline()
 
320
{
 
321
  char **fileman_completion();
 
322
  extern Keymap _rl_keymap;
 
323
 
 
324
if (KIWORDS[OFF_MODE+2] == 0)
 
325
   {   
 
326
   if (!initialize_xhelp()) {
 
327
      rl_add_funmap_entry("gui-xhelp",gui_xhelp);
 
328
      rl_bind_key('\030',gui_xhelp);               /* Bind CTR-X */
 
329
      rl_set_key ("\\e[11~",gui_xhelp,_rl_keymap); /* F1 on Xtectronix and PC */
 
330
      rl_set_key ("\\ep\r",gui_xhelp,_rl_keymap);   /* F1 on HPs */
 
331
      rl_set_key ("\\e[[A",gui_xhelp,_rl_keymap);   /* F1 on Digital PC */
 
332
      }
 
333
   }
 
334
 
 
335
  /* Allow conditional parsing of the ~/.inputrc file */
 
336
  rl_readline_name = "MIDAS";
 
337
 
 
338
  /* Tell the completer that we want a crack first */
 
339
  rl_attempted_completion_function = (CPPFunction *)fileman_completion;
 
340
 
 
341
  if (!is_a_tty) rl_outstream = fopen("/dev/null","w");
 
342
 
 
343
}
 
344
/*
 
345
 
 
346
*/
 
347
 
 
348
void fileman_ignore(matches)
 
349
char **matches;
 
350
{
 
351
  int i;
 
352
  char *ptr;
 
353
  if (!matches) return;
 
354
  for ( i = 0; matches[i]; i++) 
 
355
     if ( (ptr = strchr(matches[i], '.')) != (char *)NULL) *ptr = '\0';
 
356
}
 
357
 
 
358
 
 
359
 
 
360
 
 
361
/* Attempt to complete on the contents of TEXT. START and END show the
 
362
 * region of TEXT that contains the word to complete. We can use the entire
 
363
 * line in case we want to do some simple parsing. Return the array of matches,
 
364
 * or NULL if there aren't any. 
 
365
 */
 
366
char **fileman_completion(text, start, end)
 
367
char *text;
 
368
int start, end;
 
369
{
 
370
  char **matches;
 
371
  char *command_generator();
 
372
  char *filename_completion_function();
 
373
  char *curr_dir, *match_dir, *dest_dir;
 
374
  char *start_buffer;
 
375
 
 
376
  matches = (char **)NULL;
 
377
  match_dir = (char *)NULL;
 
378
 
 
379
  /* If this word is at the start of the line, then it is a command to complete
 
380
   * Otherwise it is the name of a file in the current directory.
 
381
   */
 
382
  start_buffer = rl_line_buffer;
 
383
  while ( *start_buffer == ' ') { start_buffer++; start--; }
 
384
 
 
385
  if (start == 0)
 
386
    matches = completion_matches(text,command_generator);
 
387
  else if (!strncasecmp(start_buffer,"SHOW/COMM",9) ||
 
388
           !strncasecmp(start_buffer,"HELP ",5) || 
 
389
           !strncasecmp(start_buffer,"DELETE/COMM",11) ||
 
390
           !strncasecmp(start_buffer,"CREATE/COMM",11))
 
391
    matches = completion_matches(text,command_generator);
 
392
  else if (!strncasecmp(start_buffer,"@ " ,2)) match_dir = getenv("MID_PROC");
 
393
  else if (!strncasecmp(start_buffer,"@a ",3)) match_dir = getenv("APP_PROC");
 
394
  else if (!strncasecmp(start_buffer,"@s ",3)) match_dir = getenv("STD_PROC");
 
395
  else if (!strncasecmp(start_buffer,"@c ",3)) match_dir = getenv("CON_PROC");
 
396
  else if (!strncasecmp(start_buffer,"LOAD/LUT ",9) ||
 
397
           !strncasecmp(start_buffer,"LOAD/ITT ",9)) 
 
398
                match_dir = getenv("MID_SYSTAB");
 
399
  else if (!strncasecmp(start_buffer,"SET/CONT",8) ||
 
400
           !strncasecmp(start_buffer,"CLEAR/CONT",10)  ||
 
401
           !strncasecmp(start_buffer,"SHOW/CONT",10)) 
 
402
                match_dir = getenv("MID_CONTEXT");
 
403
  else if (!strncasecmp(start_buffer,"CREATE/GUI ",11))
 
404
                match_dir = getenv("GUI_EXE");
 
405
 
 
406
  if (match_dir != (char *)NULL) {
 
407
    dest_dir = malloc((size_t)(strlen(match_dir)+1));
 
408
    (void) strcpy(dest_dir,match_dir);
 
409
    oshgetcwd(&curr_dir);
 
410
    oshchdir(dest_dir);
 
411
    matches = completion_matches(text,filename_completion_function);
 
412
    oshchdir(curr_dir);
 
413
    free(dest_dir);
 
414
    }
 
415
  if (!strncasecmp(start_buffer,"SET/CONT",8) ||
 
416
      !strncasecmp(start_buffer,"CLEAR/CONT",10) ||
 
417
      !strncasecmp(start_buffer,"CREATE/GUI ",11)) fileman_ignore(matches);
 
418
 
 
419
  return(matches);
 
420
}
 
421
/*
 
422
 
 
423
*/
 
424
 
 
425
/*
 
426
 * Generator fucntion for command completion. STATE lets us know weather
 
427
 * to start from scratch; without any state (i.e. STATE =0), then we start
 
428
 * at the top of the list
 
429
 */
 
430
char *command_generator(text, state)
 
431
char *text;
 
432
int state;
 
433
{
 
434
static int len;
 
435
static char mytext[12]; 
 
436
static char *ptr_text;
 
437
register struct COMMAND *mycmd;
 
438
register char *slash_ptr;
 
439
register int len_cmd, max_len_cmd, len_qua;
 
440
 
 
441
/* 
 
442
* If this is a new word to complete, initialize now. This includes
 
443
* saving the length of TEXT for efficiency, an initializing the index
 
444
* variable to 0
 
445
*/
 
446
 
 
447
if (MONIT.CMD_LIST_UPDA == 1)           /* commands have been modified... */
 
448
   update_cmd_list();
 
449
 
 
450
 
 
451
if (!state) {
 
452
   len = strlen(text);
 
453
   if ((slash_ptr = (char *)strchr(text,'/')) != (char *)NULL) {
 
454
      len_cmd = (long)slash_ptr - (long)text;
 
455
      max_len_cmd = (len_cmd > 6)? 6 : len_cmd;
 
456
      (void) strncpy(mytext,text,max_len_cmd);
 
457
      mytext[max_len_cmd] = '/';
 
458
      mytext[max_len_cmd+1] = '\0';
 
459
      len_qua = strlen(&text[len_cmd+1]);
 
460
      (void) strncat(mytext,&text[len_cmd+1],(len_qua > 4)? 4 : len_qua);
 
461
      len = strlen(mytext);
 
462
      ptr_text= mytext;
 
463
      }
 
464
   else {
 
465
      len = (len > 6)? 6 : len;
 
466
      (void) strncpy(mytext,text,len);
 
467
      mytext[len] = '\0';
 
468
      ptr_text = text;
 
469
      }
 
470
   cmds.curr = cmds.last;
 
471
   }
 
472
 
 
473
/* Return the next name which partially matches from the command list. */
 
474
while ((mycmd = cmds.curr) != (struct COMMAND *)NULL) {
 
475
   cmds.curr = mycmd->prev;
 
476
   if (strncasecmp(mycmd->name,ptr_text,len) == 0) 
 
477
      return
 
478
      ((char *)strcpy((malloc((size_t)(strlen(mycmd->name)+1))),mycmd->name));
 
479
   }
 
480
 
 
481
/* If no names matched, then return NULL */
 
482
return((char *)NULL);
 
483
}
 
484
  
 
485
 
 
486
 
 
487
#ifdef __STDC__
 
488
static void TTPRO_alarm(int sig)
 
489
#else
 
490
static void TTPRO_alarm(sig)
 
491
/*++++++++++++++++
 
492
.PURPOSE Function called at end of time-out
 
493
.RETURNS Within ostread function. Internal use only.
 
494
---------*/
 
495
int sig;
 
496
#endif
 
497
{
 
498
  siglongjmp(env,1);    /* return with the saved signal mask from sigsetjmp */
 
499
}
 
500
#endif /* NO_READLINE */
 
501
/*
 
502
 
 
503
*/
 
504
 
 
505
int Parse3(start)
 
506
 
 
507
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
508
.PURPOSE
 
509
  clean input line
 
510
.ALGORITHM
 
511
.RETURNS
 
512
  return no. of tokens on line
 
513
---------------------------------------------------------------------*/
 
514
 
 
515
int  start;
 
516
 
 
517
{
 
518
int   lstart, lsave;
 
519
int   n, count, substi, kk, nn, mm;
 
520
register int  nr;
 
521
 
 
522
char  wbuf[MAX_TOKEN]; 
 
523
register char   sngc;
 
524
 
 
525
 
 
526
 
 
527
/*  extract all tokens + get their length, also use OLD_TOKEN   */
 
528
 
 
529
lstart = start;
 
530
substi = 0;
 
531
 
 
532
for (n=0; n<10; n++)
 
533
   {
 
534
   TOKEN[n].LEN = 
 
535
        CGN_EXTRSS(LINE.STR,LINE.LEN,blank,&lstart,TOKEN[n].STR,MAX_TOKEN);
 
536
   if (TOKEN[n].LEN <= 0)                       /* end of parsing reached... */
 
537
      {
 
538
      if (TOKEN[n].LEN == -2)                   /* overflow of single token */
 
539
         {
 
540
         ERRORS.SYS = 7;
 
541
         PREPERR("MIDAS",LINE.STR,TOKEN[n].STR);
 
542
         return (-1);
 
543
         }
 
544
 
 
545
      count = n ;
 
546
      for (nr=count; nr<10; nr++)
 
547
         {
 
548
         TOKEN[nr].STR[0] = '?';             /* init empty TOKEN + LTOKEN  */
 
549
         TOKEN[nr].STR[1] = '\0' ;
 
550
         TOKEN[nr].LEN = 1 ;
 
551
         }
 
552
      goto end_of_it;
 
553
      }
 
554
 
 
555
 
 
556
   /* test for interactive input with "."  */
 
557
 
 
558
   if ( (TOKEN[n].STR[0] == '.') && (TOKEN[n].LEN == 1) )
 
559
      {
 
560
      TOKEN[n].LEN = CGN_COPY(TOKEN[n].STR,OLDTOKEN[n].STR);
 
561
      substi = 1 ;                           /* it's like a substitution */
 
562
      }
 
563
   else
 
564
      {
 
565
      sngc = TOKEN[n].STR[OLDTOKMAX];                   /* save char. */
 
566
      TOKEN[n].STR[OLDTOKMAX] = '\0';                   /* so, no overrun... */
 
567
      OLDTOKEN[n].LEN = CGN_COPY(OLDTOKEN[n].STR,TOKEN[n].STR); 
 
568
      TOKEN[n].STR[OLDTOKMAX] = sngc;
 
569
      }
 
570
   }
 
571
 
 
572
count = 10 ;
 
573
 
 
574
 
 
575
/*  test, if more than 10 tokens on line  */
 
576
 
 
577
if (LINE.LEN >= lstart)
 
578
   {
 
579
   for (nr=lstart; nr<LINE.LEN; nr++)
 
580
      {
 
581
      if (LINE.STR[nr] != ' ') goto collect_rest;
 
582
      }
 
583
   goto end_of_it;                              /* only trailing blanks */
 
584
 
 
585
 
 
586
collect_rest:
 
587
   nn = TOKEN[9].LEN;
 
588
   kk = 0;
 
589
   for (nr=0; nr< 39; nr++)       /* garbage collection of 40 more tokens...  */
 
590
      {
 
591
      lsave = CGN_EXTRSS(LINE.STR,LINE.LEN,blank,&lstart,wbuf,MAX_TOKEN);
 
592
      if (lsave > 0)
 
593
         {
 
594
         if ((lsave+nn+1) > MAX_TOKEN)
 
595
            {
 
596
            kk = 1;
 
597
            mm = MAX_TOKEN - nn;
 
598
            }
 
599
         else
 
600
            {
 
601
            TOKEN[9].STR[nn++] = ' ';
 
602
            mm = lsave;
 
603
            }
 
604
         (void) strncpy(&TOKEN[9].STR[nn],wbuf,mm);
 
605
         nn += mm;
 
606
         if (kk == 1) break;
 
607
         }
 
608
      }
 
609
 
 
610
   TOKEN[9].STR[nn] = '\0' ;
 
611
   TOKEN[9].LEN = nn;
 
612
   }
 
613
 
 
614
end_of_it:
 
615
if (substi > 0)                 /* rebuild input line */
 
616
   {
 
617
   int  indx;
 
618
 
 
619
   indx = 0;
 
620
   for (nr=0; nr<count; nr++)
 
621
      {
 
622
      (void) strncpy(&LINE.STR[indx],TOKEN[nr].STR,TOKEN[nr].LEN);
 
623
      indx += TOKEN[nr].LEN;
 
624
      LINE.STR[indx++] = ' ';
 
625
      if (indx >= MAX_LINE)
 
626
         {
 
627
         ERRORS.SYS = 7;
 
628
         PREPERR("MIDAS",LINE.STR," ");
 
629
         return (-1);
 
630
         }
 
631
      }
 
632
   LINE.STR[--indx] = '\0';             /* last blank -> \0    */
 
633
   LINE.LEN = indx;
 
634
   }
 
635
 
 
636
return (count);
 
637
}
 
638
/*
 
639
 
 
640
*/
 
641
 
 
642
#if vms
 
643
 
 
644
void TTSET(flg)                 /* only used in VMS */
 
645
 
 
646
int  flg;       /* IN: 0 = clear, 1 = set terminal, 2 = byebye */
 
647
 
 
648
{
 
649
 
 
650
if (TERM.FLAG == 1)
 
651
   {
 
652
   if (flg < 2)
 
653
      ostraw(flg);              /* clear/set terminal settings */
 
654
 
 
655
   else
 
656
      {
 
657
      CloseTerm();              /* reset terminal completely */
 
658
      ospwait(1);
 
659
      }
 
660
   }
 
661
 
 
662
}
 
663
 
 
664
#endif
 
665
/*
 
666
 
 
667
*/
 
668
 
 
669
void TTINIT(hdr)
 
670
 
 
671
/*++++++++++++++++++++++++++++++++++++++++++++++++++
 
672
.KEYWORDS
 
673
  terminal, window
 
674
.PURPOSE
 
675
  manage the windows
 
676
.ALGORITHM
 
677
  open foreground window only
 
678
.RETURNS
 
679
  nnothing
 
680
--------------------------------------------------*/
 
681
 
 
682
int  hdr;       /* IN: header flag = 0 (no) or = 1 (yes)  */
 
683
 
 
684
{
 
685
 
 
686
#if vms                 /* --- here for VMS --- */
 
687
 
 
688
int  n;
 
689
 
 
690
/* TERM.FLAG = 0/1 for TermWindow use (only for VMS!) */
 
691
 
 
692
if (TERM.FLAG == 1)
 
693
   {                            /* (device,termcapfile,control_chars  */
 
694
   n = OpenTerm((char *) 0,(char *) 0,TW_cc_VMS);
 
695
   if (n != OK)
 
696
      TERM.FLAG = 0;
 
697
   else
 
698
      {
 
699
      TERM.FLAG = 1;
 
700
      if (hdr == 0)
 
701
         {
 
702
         ClearScreen(); 
 
703
         CursorTo(5,0);
 
704
         }
 
705
      }
 
706
   }
 
707
 
 
708
#ifndef NO_READLINE
 
709
 
 
710
else 
 
711
   {
 
712
   (void) strncpy(history_file,FRONT.STARTUP,80);
 
713
   (void) strcat(history_file,"midtemp");
 
714
   (void) strncat(history_file,FRONT.DAZUNIT,2);
 
715
   (void) strcat(history_file,".prg");
 
716
   using_history();
 
717
   read_history(history_file);
 
718
   initialize_readline ();              /* Bind our completer */
 
719
   }
 
720
 
 
721
#endif          /* endif NO_READLINE */
 
722
 
 
723
 
 
724
#else                   /* --- here for Unix/Linux --- */
 
725
 
 
726
#ifndef NO_READLINE
 
727
(void) strncpy(history_file,FRONT.STARTUP,80);
 
728
(void) strcat(history_file,"midtemp");
 
729
(void) strncat(history_file,FRONT.DAZUNIT,2);
 
730
(void) strcat(history_file,".prg");
 
731
using_history();
 
732
read_history(history_file);
 
733
initialize_readline ();              /* Bind our completer */
 
734
 
 
735
#endif          /* endif NO_READLINE */
 
736
 
 
737
 
 
738
#endif                  /* endif vms */
 
739
 
 
740
 
 
741
 
 
742
if (hdr == 0)
 
743
   {
 
744
   if (FRONT.TITLE[0] == 'F')
 
745
      {
 
746
      (void) strcpy(wstr,"                   FrontEnd to MIDAS ");
 
747
      }
 
748
   else
 
749
      {
 
750
      (void) strcpy(wstr,"              ESO-MIDAS version ");
 
751
      (void) strcat(wstr,&FRONT.TITLE[1]);
 
752
      (void) strcat(wstr," on ");
 
753
      FRONT.SYSTEM[19] = '\0';
 
754
      (void) strcat(wstr,FRONT.SYSTEM);
 
755
      FRONT.SYSTEM[19] = ' ';
 
756
      }
 
757
   (void) printf("\n\n\r%s\n\r",wstr);
 
758
   }
 
759
 
 
760
 
 
761
/* get new size of terminal window */
 
762
 
 
763
MID_TTINFO(&TERM.COLS,&TERM.LINES);
 
764
 
 
765
 
 
766
#if vms
 
767
if (TERM.FLAG == 1)
 
768
   {
 
769
   n = TERM.LINES - 1;
 
770
   CursorTo(n,0);                       /* move cursor to lower left corner */
 
771
   }
 
772
#endif
 
773
 
 
774
}
 
775
/*
 
776
 
 
777
*/
 
778
 
 
779
int TTEDIT(line,len)
 
780
 
 
781
/*++++++++++++++++++++++++++++++++++++++++++++++++++
 
782
.KEYWORDS
 
783
  terminal, window
 
784
.PURPOSE
 
785
  edit a command line
 
786
.ALGORITHM
 
787
  use FO's window interfaces
 
788
.RETURNS
 
789
status
 
790
--------------------------------------------------*/
 
791
 
 
792
char    *line;          /* IN/OUT: line to be edited (terminated by \0)  */
 
793
int   len;              /* IN: max. length of 'line'  */
 
794
 
 
795
{
 
796
#ifndef NO_READLINE
 
797
struct sigaction act, oact;
 
798
#endif
 
799
 
 
800
 
 
801
#if vms
 
802
 
 
803
if (TERM.FLAG != 0)                     /* we use FO's termwin stuff */
 
804
   {
 
805
   int   status, n;
 
806
 
 
807
   MID_TTINFO(&TERM.COLS,&TERM.LINES);  /* get new size of terminal window */
 
808
   n = TERM.LINES - 1;
 
809
   CursorTo(n,0);               /* move cursor to lower left corner */
 
810
 
 
811
   status = tv_kmods(TERM.EDITMODE,line,len,strlen(line));
 
812
   if (status != OK) return (-1);
 
813
 
 
814
   NewLine();
 
815
   return 0;
 
816
   }
 
817
#endif          /* endif vms */
 
818
 
 
819
 
 
820
/* for TERM.FLAG = 0 (as in Unix) check, if we use readline library */
 
821
 
 
822
#ifndef NO_READLINE
 
823
 
 
824
line_read = (char *)NULL;
 
825
act.sa_handler = TTPRO_alarm;
 
826
sigemptyset(&act.sa_mask);
 
827
act.sa_flags = 0;
 
828
(void) sigaction(SIGALRM,&act,&oact);
 
829
 
 
830
if ( sigsetjmp(env,1) == 0 ) 
 
831
   {
 
832
   re_edit(line);
 
833
   line_read = readline("");
 
834
   if (line_read && *line_read) 
 
835
      {
 
836
      (void) strcpy(line,line_read); /* before: add_history(line_read); */
 
837
      free(line_read);          /* before:                         */ 
 
838
      }
 
839
   else 
 
840
      line[0] = '\0';
 
841
   }
 
842
 
 
843
(void) sigaction(SIGALRM,&oact,&act);
 
844
 
 
845
#else
 
846
 
 
847
(void) printf("%s\n\r",line);
 
848
 
 
849
#endif
 
850
 
 
851
 
 
852
return (0);
 
853
}
 
854
/*
 
855
 
 
856
*/
 
857
 
 
858
void TTPRO(prompt,line,len)
 
859
 
 
860
/*++++++++++++++++++++++++++++++++++++++++++++++++++
 
861
.KEYWORDS
 
862
  terminal, window
 
863
.PURPOSE
 
864
  prompt for + get an input line from windows
 
865
.ALGORITHM
 
866
  use FO's window interfaces
 
867
.RETURNS
 
868
  Nothing
 
869
--------------------------------------------------*/
 
870
 
 
871
char    *prompt;        /* IN: prompt string (terminated by \0)  */
 
872
char    *line;          /* OUT: input line (terminated by \0)    */
 
873
int     len;            /* IN: length of buffer 'line'  */
 
874
 
 
875
{
 
876
#ifndef NO_READLINE
 
877
struct sigaction act, oact;
 
878
#endif
 
879
 
 
880
 
 
881
 
 
882
TERM.TIMEOUT = 0;
 
883
 
 
884
 
 
885
#if vms
 
886
 
 
887
if (TERM.FLAG != 0)                     /* we use FO's termwin stuff */
 
888
   {
 
889
   int   n;
 
890
 
 
891
   NewLine();
 
892
 
 
893
   Put(prompt);
 
894
   n = tv_kmods(TERM.EDITMODE,line,len,0);
 
895
 
 
896
 
 
897
   /*  interrupt routine `intermail' sets TERM.TIMEOUT to 2 */
 
898
 
 
899
   if (TERM.TIMEOUT == 0)
 
900
      {
 
901
      if (n == EOF) (void) strcpy(line,"BYE");
 
902
      NewLine();
 
903
      }
 
904
   return;
 
905
   }
 
906
 
 
907
#endif          /* endif vms */
 
908
 
 
909
 
 
910
/* for TERM.FLAG = 0 (as in Unix) check, if we use readline library */
 
911
 
 
912
#ifndef NO_READLINE
 
913
 
 
914
line_read = (char *)NULL;
 
915
act.sa_handler = TTPRO_alarm;
 
916
sigemptyset(&act.sa_mask);
 
917
act.sa_flags = 0;
 
918
(void) sigaction(SIGALRM,&act,&oact);
 
919
 
 
920
if ( sigsetjmp(env,1) == 0 ) 
 
921
   {
 
922
   if (is_a_tty) 
 
923
      line_read = readline(prompt);
 
924
   else          
 
925
      line_read = readline("");
 
926
 
 
927
   if (line_read) 
 
928
      {
 
929
      if (*line_read) 
 
930
         { 
 
931
         (void) strcpy(line,line_read); /* before: add_history(line_read); */
 
932
         free(line_read);               /* before:                         */
 
933
         }
 
934
      else line[0] = '\0';
 
935
      }
 
936
   else 
 
937
      (void) strcpy(line,"bye");
 
938
   }
 
939
(void) sigaction(SIGALRM,&oact,&act);
 
940
 
 
941
#else
 
942
 
 
943
(void) printf("%s ",prompt);
 
944
(void) CGN_GETLINE(line,len);
 
945
 
 
946
#endif
 
947
 
 
948
}