FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
f_evt.c
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #include "f_stccomm.h"
9 #include "typedefs.h"
10 
11 #ifdef RFIO
12 #define RFIO_open rfio_open
13 #define RFIO_close rfio_close
14 #define RFIO_read rfio_read
15 #define RFIO_lseek rfio_lseek
16 #include "rawapin.h" /* RFIO stuff */
17 #else
18 #define RFIO_open open
19 #define RFIO_close close
20 #define RFIO_read read
21 #define RFIO_lseek lseek
22 #endif
23 
24 #ifdef GSI__LYNX
25 #undef unix
26 #endif
27 
28 #ifdef GSI__LINUX /* Linux */
29 #undef unix
30 #endif
31 
32 #ifdef GSI__SOLARIS /* Solaris */
33 #undef unix
34 #endif
35 
36 #ifdef GSI__WINNT /* Windows NT */
37 #endif
38 
39 #ifdef GSI__AIX
40 #define AIX_DEC
41 #endif
42 
43 #ifdef unix /* DEC OSF/1 */
44 #define AIX_DEC
45 #endif
46 
47 #ifdef GSI__WINNT
48 #include <fcntl.h>
49 #include <memory.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sys/stat.h>
54 #include <sys/timeb.h>
55 #include <sys/types.h>
56 #include <time.h>
57 #define DEF_FILE_ACCE S_IREAD | S_IWRITE /* rw */
58 #define GET__OPEN_FLAG O_RDONLY | O_BINARY
59 #define PUT__OPEN_APD_FLAG O_RDWR | O_APPEND
60 #define PUT__CRT_FLAG O_CREAT | O_RDWR
61 #define PUT__CRT_OPT ""
62 
63 #include <winsock.h>
64 #define WS_VERSION_REQD 0x0101
65 #define WS_VERSION_MAJOR HIBYTE(WS_VERSION_REQD)
66 #define WS_VERSION_MINOR LOBYTE(WS_VERSION_REQD)
67 #define MIN_SOCKETS_REQD 6
68 // WSADATA wsaData;
69 #endif
70 
71 #ifdef Linux /* Linux */
72 #include <fcntl.h>
73 #include <memory.h>
74 #include <pwd.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <sys/stat.h>
79 #include <sys/time.h>
80 #include <sys/timeb.h>
81 #include <sys/types.h>
82 #include <unistd.h>
83 #define DEF_FILE_ACCE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* rw-r--r-- */
84 #define GET__OPEN_FLAG O_RDONLY
85 #define PUT__OPEN_APD_FLAG O_RDWR | O_APPEND
86 #define PUT__CRT_FLAG O_CREAT | O_RDWR
87 #define PUT__CRT_OPT ""
88 #endif
89 
90 #ifdef Solaris /* Linux */
91 #include <fcntl.h>
92 #include <memory.h>
93 #include <pwd.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include <sys/stat.h>
98 #include <sys/time.h>
99 #include <sys/timeb.h>
100 #include <sys/types.h>
101 #include <unistd.h>
102 #define DEF_FILE_ACCE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* rw-r--r-- */
103 #define GET__OPEN_FLAG O_RDONLY
104 #define PUT__OPEN_APD_FLAG O_RDWR | O_APPEND
105 #define PUT__CRT_FLAG O_CREAT | O_RDWR
106 #define PUT__CRT_OPT ""
107 #endif
108 
109 #ifdef Darwin /* MaxOS X */
110 #include <fcntl.h>
111 #include <memory.h>
112 #include <pwd.h>
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <string.h>
116 #include <sys/stat.h>
117 #include <sys/time.h>
118 #include <sys/timeb.h>
119 #include <sys/types.h>
120 #include <unistd.h>
121 #define DEF_FILE_ACCE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* rw-r--r-- */
122 #define GET__OPEN_FLAG O_RDONLY
123 #define PUT__OPEN_APD_FLAG O_RDWR | O_APPEND
124 #define PUT__CRT_FLAG O_CREAT | O_RDWR
125 #define PUT__CRT_OPT ""
126 #endif
127 
128 #ifdef _AIX
129 #include <fcntl.h>
130 #include <memory.h>
131 #include <pwd.h>
132 #include <stdio.h>
133 #include <stdlib.h>
134 #include <string.h>
135 #include <sys/mode.h>
136 #include <sys/time.h>
137 #include <sys/timeb.h>
138 #include <sys/types.h>
139 #include <unistd.h>
140 #define DEF_FILE_ACCE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* rw-r--r-- */
141 #define GET__OPEN_FLAG O_RDONLY
142 #define PUT__OPEN_APD_FLAG O_RDWR | O_APPEND
143 #define PUT__CRT_FLAG O_CREAT | O_RDWR
144 #define PUT__CRT_OPT ""
145 #endif
146 
147 #if MBS_ENDIAN == 1
148 #include "s_filhe_swap.h"
149 #else
150 #include "s_filhe.h"
151 #endif
152 
153 // DABC
154 #include "fLmd.h"
155 // -- DABC
156 
157 //#include "gps_sc_def.h"
158 #include "f_evt.h"
159 //#include "f_evcli.h"
160 #include "portnum_def.h"
161 
167 
168 static struct s_tcpcomm s_tcpcomm_st_evt;
169 static CHARS c_temp[MAX_BUF_LGTH];
170 static int l_gl_rev_port = PORT__EVENT_SERV;
171 // static int l_gl_evt_check = 0;
172 
173 /*1+ C Procedure *************+****************************************/
174 /* */
175 /*+ Module : f_evt__example */
176 /* */
177 /*--------------------------------------------------------------------*/
178 /*+ CALLING : Examples for calling event API */
179 /*--------------------------------------------------------------------*/
180 /* */
181 /*+ PURPOSE : See f_evt_examples.c */
182 /* */
183 /* */
184 /*2+Implementation************+****************************************/
185 /*+ User Example : In m_lea_user.c */
186 /*+ Channel structure : defined in f_evt.h */
187 /*+ File name : f_evt.c */
188 /*+ Version : 1.01 */
189 /*+ Author : H.Essel */
190 /*+ Created : 16-Feb-2000 */
191 /*+ Updates : Date Purpose */
192 /*1- C Procedure *************+****************************************/
193 
194 /*****************+***********+****************************************/
195 /* */
196 /* GSI, Gesellschaft fuer Schwerionenforschung mbH */
197 /* Postfach 11 05 52 */
198 /* D-64220 Darmstadt */
199 /* */
200 /*1+ C Procedure *************+****************************************/
201 /* */
202 /*+ Module : f_evt_get_subevent */
203 /* */
204 /*--------------------------------------------------------------------*/
205 /*+ CALLING : l_sts = f_evt_get_subevent(ve10_1 *,subevent,**head,**data,*lwords) */
206 /*--------------------------------------------------------------------*/
207 /* */
208 /*+ PURPOSE : get subevent pointer */
209 /* */
210 /*+ ARGUMENTS : */
211 /* */
212 /*+ ve10_1 : (s_ve10_1 *) event header pointer */
213 /*+ subevent : subevent number (1,2,3...) */
214 /* If = 0, f_evt_get_subevent returns the number of */
215 /* subevents. In this case the following arguments */
216 /* might be NULL. */
217 /*+ head : Address of s_ves10_1 subevent header pointer */
218 /*+ data : Address of INTS4 event data pointer */
219 /*+ lwords : Address of INTS4 to get number of data longwords */
220 /* */
221 /*+ Return type : int */
222 /*- GETEVT__SUCCESS : Found subevent. */
223 /*- GETEVT__NOMORE : No more subevents. */
224 /*+ Declaration : */
225 /* INTS4 f_evt_get_subevent( */
226 /* s_ve10_1 *, INTS4, INTS4 **, INTS4 **, INTS4 *); */
227 /*1- C Procedure *************+****************************************/
228 INTS4 f_evt_get_subevent(s_ve10_1* ps_ve10_1, INTS4 l_subevent, INTS4** pl_se, INTS4** pl_d, INTS4* pl_lwords)
229 {
230  s_ves10_1* ps_ves10_1;
231  INTS4 l_total, l_sub, l_sum;
232  INTS4 /*l,*/ ll, /*l_status,*/ *pl_next;
233 
234  if (ps_ve10_1 == NULL) {
235  return (GETEVT__FAILURE);
236  }
237  pl_next = (INTS4*)(ps_ve10_1 + 1);
238  l_total = ps_ve10_1->l_dlen - 4; /* total words in subevents */
239  l_sum = 0;
240  ll = 0;
241  while (l_sum < l_total) {
242  ps_ves10_1 = (s_ves10_1*)pl_next;
243  ll++;
244  if (ll == l_subevent) {
245  if (pl_lwords != NULL) {
246  *pl_lwords = ps_ves10_1->l_dlen / 2 - 1;
247  }
248  if (pl_se != NULL) {
249  *pl_se = (INTS4*)ps_ves10_1;
250  }
251  if (pl_d != NULL) {
252  *pl_d = (INTS4*)(ps_ves10_1 + 1);
253  }
254  return (GETEVT__SUCCESS);
255  }
256  l_sub = ps_ves10_1->l_dlen + 4; /* total words of subevent */
257  l_sum += l_sub;
258  pl_next = (INTS4*)(ps_ves10_1);
259  pl_next += l_sub / 2;
260  }
261  if (pl_lwords != NULL) {
262  *pl_lwords = ll;
263  }
264  if (pl_se != NULL) {
265  *pl_se = NULL;
266  }
267  if (pl_d != NULL) {
268  *pl_d = NULL;
269  }
270  if (l_subevent == 0) {
271  return (ll);
272  } else {
273  return (GETEVT__NOMORE);
274  }
275 }
276 /*****************+***********+****************************************/
277 /* */
278 /* GSI, Gesellschaft fuer Schwerionenforschung mbH */
279 /* Postfach 11 05 52 */
280 /* D-64220 Darmstadt */
281 /* */
282 /*1+ C Procedure *************+****************************************/
283 /* */
284 /*+ Module : f_evt_type */
285 /* */
286 /*--------------------------------------------------------------------*/
287 /*+ CALLING : l_status = f_evt_type(bufhe,evhe,sid,long,hex,data) */
288 /*--------------------------------------------------------------------*/
289 /* */
290 /*+ PURPOSE : print event */
291 /* */
292 /*+ ARGUMENTS : */
293 /* */
294 /*+ bufhe : (s_bufhe *) buffer header pointer (=NULL no output) */
295 /*+ evhe : (s_evhe *) event header pointer (=NULL no output) */
296 /*+ sid : subevent ID (-1 is all) */
297 /*+ long : output longwords */
298 /*+ hex : output hex longwords */
299 /*+ data : output data */
300 /* */
301 /*+ Return type : int */
302 /*+ Declaration : */
303 /* INTS4 f_evt_type( */
304 /* s_bufhe *,s_evhe *, INTS4, INTS4, INTS4, INTS4); */
305 /*1- C Procedure *************+****************************************/
306 INTS4 f_evt_type(s_bufhe* ps_bufhe, s_evhe* ps_evhe, INTS4 l_subid, INTS4 l_long, INTS4 l_hex, INTS4 l_data)
307 {
308  s_ves10_1* ps_ves10_1;
309  s_ve10_1* ps_ve10_1;
310  s_filhe* ps_filhe;
311  // INTS2* pi_data;
312  INTS4* pl_data;
313  INTS4 /*l_mode,*/ l_s;
314  INTS4 l, ll, l_status, l_ldata, l_used;
315  CHARS c_line[132];
316  CHARS c_full[132];
317  CHARS c_time[32];
318 
319  strcpy(c_full, " ");
320  l_ldata = l_data;
321  if ((l_hex + l_long) > 0) {
322  l_ldata = 1;
323  }
324 
325  /* Print buffer header (file header) */
326  if (ps_bufhe != NULL) {
327  sprintf(c_line, "--------------------------------------------------------");
328  printf("%s\n", c_line);
329  /* calculate real buffer size */
330  ll = ps_bufhe->l_dlen * 2;
331  if (ll % 512 > 0) {
332  ll += 512 - ll % 512;
333  }
334  /* file header */
335  l_status = f_ut_utime(ps_bufhe->l_time[0], ps_bufhe->l_time[1], c_time);
336 
337 #ifdef VMS
338  if (1 != l_status)
339  printf("f_evt_type: error in f_ut_utime!");
340 #else
341  if (0 != l_status)
342  printf("f_evt_type: error in f_ut_utime!");
343 #endif
344 
345  if (ps_bufhe->i_type == 2000) {
346  ps_filhe = (s_filhe*)ps_bufhe;
347  sprintf(c_line, "File header info:");
348  printf("%s\n", c_line);
349  sprintf(c_line, "Size: %d [%d b], used %d [b]", ps_filhe->filhe_dlen, ll, ps_filhe->filhe_used * 2);
350  printf("%s\n", c_line);
351  sprintf(c_line, "Label: %s", ps_filhe->filhe_label);
352  printf("%s\n", c_line);
353  sprintf(c_line, "File: %s", ps_filhe->filhe_file);
354  printf("%s\n", c_line);
355  sprintf(c_line, "User: %s", ps_filhe->filhe_user);
356  printf("%s\n", c_line);
357  sprintf(c_line, "Time: %s", ps_filhe->filhe_time);
358  printf("%s\n", c_line);
359  sprintf(c_line, "Run: %s", ps_filhe->filhe_run);
360  printf("%s\n", c_line);
361  sprintf(c_line, "Exp: %s", ps_filhe->filhe_exp);
362  printf("%s\n", c_line);
363  for (ll = 0; ll < ps_filhe->filhe_lines; ll++) {
364  sprintf(c_line, "comment: %s", ps_filhe->s_strings[ll].string);
365  printf("%s\n", c_line);
366  }
367  } else {
368  l_used = ps_bufhe->i_used;
369  if (ps_bufhe->l_dlen > MAX__DLEN) {
370  l_used = ps_bufhe->l_free[2];
371  }
372  sprintf(c_line,
373  "Buffer %9d, Length %5d[w] Size %5d[b] used %5d[w] %s",
374  ps_bufhe->l_buf,
375  ps_bufhe->l_dlen,
376  ll,
377  l_used,
378  c_time);
379  printf("%s\n", c_line);
380  sprintf(c_line,
381  " Events %3d Type/Subtype %5d %5d FragEnd=%d FragBegin=%d Total %5d[w]",
382  ps_bufhe->l_evt,
383  ps_bufhe->i_type,
384  ps_bufhe->i_subtype,
385  ps_bufhe->h_end,
386  ps_bufhe->h_begin,
387  ps_bufhe->l_free[1]);
388  printf("%s\n", c_line);
389  }
390  sprintf(c_line, "--------------------------------------------------------");
391  printf("%s\n", c_line);
392  }
393 
394  if (ps_evhe == NULL) {
395  return (0);
396  }
397 
398  /* print event 4,x or 6,x */
399  if (ps_evhe->i_type != 10) {
400  sprintf(c_line,
401  "Event type %d, subtype %d, data longwords %d",
402  ps_evhe->i_type,
403  ps_evhe->i_subtype,
404  ps_evhe->l_dlen / 2);
405  printf("%s\n", c_line);
406  if ((l_ldata != 0) & ((ps_evhe->i_type == 4) | (ps_evhe->i_type == 6))) {
407  /* output data, assume data as longword */
408  pl_data = (INTS4*)ps_evhe;
409  pl_data += 2;
410  for (l = 0; l < ps_evhe->l_dlen / 2; l++) {
411  sprintf(c_line, "%08x ", *pl_data);
412  strcat(c_full, c_line);
413  pl_data++;
414  if (l % 8 == 7) {
415  printf("%s\n", c_full);
416  strcpy(c_full, " ");
417  }
418  }
419  if (strlen(c_full) > 2) {
420  printf("%s\n", c_full);
421  }
422  }
423  return (0);
424  }
425 
426  /* Print event 10,1 */
427  ps_ve10_1 = (s_ve10_1*)ps_evhe;
428  /* Print event header */
429  sprintf(c_line,
430  "Event %9d Type/Subtype %5d %5d Length %5d[w] Trigger %2d",
431  ps_ve10_1->l_count,
432  ps_ve10_1->i_type,
433  ps_ve10_1->i_subtype,
434  ps_ve10_1->l_dlen,
435  ps_ve10_1->i_trigger);
436  printf("%s\n", c_line);
437 
438  /********************/
439  l_s = 0;
440  l_status = 0;
441  while (l_status == 0) {
442  l_s++;
443  l_status = f_evt_get_subevent(ps_ve10_1, l_s, (INTS4**)&ps_ves10_1, (INTS4**)&pl_data, (INTS4*)&ll);
444  if (l_status == 0) {
445  if ((l_subid < 0) | (l_subid == ps_ves10_1->i_procid)) {
446  sprintf(c_line,
447  " SubEv ID %6d Type/Subtype %5d %5d Length %5d[w] Control %2d Subcrate %2d",
448  ps_ves10_1->i_procid,
449  ps_ves10_1->i_type,
450  ps_ves10_1->i_subtype,
451  ps_ves10_1->l_dlen,
452  ps_ves10_1->h_control,
453  ps_ves10_1->h_subcrate);
454  printf("%s\n", c_line);
455  if (l_ldata != 0) {
456  /* output data */
457  if ((l_long != 0) | (l_hex != 0)) {
458  /* In this case we assume data as one longword per channel */
459  for (l = 0; l < ll; l++) {
460  if (l_hex != 0) {
461  sprintf(c_line, "%04x.%04x ", (*pl_data >> 16) & 0xffff, *pl_data & 0xffff);
462  } else {
463  sprintf(c_line, "%8d ", *pl_data);
464  }
465  strcat(c_full, c_line);
466  pl_data++;
467  if (l % 8 == 7) {
468  printf("%s\n", c_full);
469  strcpy(c_full, " ");
470  }
471  }
472  if (strlen(c_full) > 2) {
473  printf("%s\n", c_full);
474  }
475  strcpy(c_full, " ");
476  } else {
477  /* In this case we assume data as two words per channel */
478  for (l = 0; l < ll; l++) {
479  sprintf(c_line, "%8d%8d", *pl_data & 0xffff, (*pl_data >> 16) & 0xffff);
480  strcat(c_full, c_line);
481  pl_data++;
482  if (l % 4 == 3) {
483  printf("%s\n", c_full);
484  strcpy(c_full, " ");
485  }
486  }
487  if (strlen(c_full) > 2) {
488  printf("%s\n", c_full);
489  }
490  strcpy(c_full, " ");
491  }
492  }
493  }
494  }
495  }
496  return (0);
497 }
498 /*1+ C Main ****************+******************************************/
499 /*+ Module : f_evt_rev_port */
500 /*--------------------------------------------------------------------*/
501 /*+ CALLING : f_evt_rev_port(long l_port) */
502 /*--------------------------------------------------------------------*/
503 /* */
504 /*+ PURPOSE : f_evt_rev_port sets port number for event server */
505 /*+ ARGUMENTS : */
506 /*+ l_port : Port number: */
507 /*+ Return type : int. */
508 /*+ Status codes: */
509 /*- GETEVT__SUCCESS : success. */
510 /*+ Declaration : */
511 /* INTS4 f_evt_rev_port(INTS4); */
512 /*1- C Main ****************+******************************************/
514 {
515  l_gl_rev_port = l_port;
516  printf("Use remote event server port %d\n", l_port);
517  return 0;
518 }
519 /*1+ C Main ****************+******************************************/
520 /*+ Module : f_evt_get_open */
521 /*--------------------------------------------------------------------*/
522 /*+ CALLING : f_evt_get_open(long l_mode, char &c_server[], s_evt_channel &s_chan, */
523 /* char **ps_info, long l_sample,l_para)*/
524 /*--------------------------------------------------------------------*/
525 /* */
526 /*+ PURPOSE : f_evt_get_open opens an event stream from specified*/
527 /* channel. */
528 /*+ ARGUMENTS : */
529 /*+ l_mode : Type of server: */
530 /*- GETEVT__FILE : Input from file */
531 /*- GETEVT__STREAM : Input from MBS stream server */
532 /*- GETEVT__TRANS : Input from MBS transport */
533 /*- GETEVT__EVENT : Input from MBS event server */
534 /*- GETEVT__REVSERV: Input from remote event server */
535 /*+ c_server : Node of server or file name. */
536 /*+ s_chan : structure s_evt_channel, must be allocated. */
537 /*+ ps_info : address of pointer. If it is not NULL, then */
538 /* try to return file header or other information */
539 /* about server. If it is NULL, then returns nothing. */
540 /*+ l_sample : used by event server to send only every */
541 /* 'l_sample' event. */
542 /*+ l_para : currently not used */
543 /*+ Return type : int. */
544 /*+ Status codes: */
545 /*- GETEVT__SUCCESS : success. */
546 /*- GETEVT__NOFILE : file does not exist. */
547 /*- GETEVT__RDERR : read server error. */
548 /*- GETEVT__NOSERVER : can not connect server. */
549 /*+ Declaration : */
550 /* INTS4 f_evt_get_open( */
551 /* INTS4, CHARS *, s_evt_channel *, CHARS **, INTS4, INTS4); */
552 /*+ FUNCTION : Opens the input channel and save context in s_chan. */
553 /*+ NOTE : Up to four input channels can be opened. */
554 /*1- C Main ****************+******************************************/
556  CHARS* pc_server,
557  s_evt_channel* ps_chan,
558  CHARS** ps_info,
559  INTS4 l_sample,
560  INTS4 l_param)
561 {
562 
563  INTS4 l_swap, l_swap_head, l_is_goosybuf, l_filehead = 0, l_size, l_size_head, l_dummy, l_header_size;
564  // INTS2* pi;
565  CHARS c_file[256], *pc_temp;
566  s_filhe* ps_filhe;
567  struct s_varstr
568  {
569  INTS2 i_varstr;
570  CHARS c_varstr[128];
571  } /* s_varstr_file*/;
572  INTS4 l_status; //,ll;
573  int val;
574 
575  // P.-A. Loizeau, 2015/11/26:
576  // In case of STREAM mode, catch the case where the user provided
577  // a nonstandard stream port, extract the value and clean the server
578  // name. Usefull with DABC MBS streams where the port can be user set.
579  INTS4 i_streamport = PORT__STREAM_SERV;
580  if ((GETEVT__STREAM == l_mode) && ((pc_temp = strchr(pc_server, ':')) != NULL)) {
581  // printf("input string: %s!\n", pc_server);
582  pc_server = strtok(pc_server, ":");
583  CHARS* pc_streamport = strtok(NULL, ":");
584 
585  i_streamport = atoi(pc_streamport);
586  // printf("output string: %s! Port: %s %i\n", pc_server, pc_streamport, i_streamport);
587  }
588 
589 #ifndef GSI__WINNT
590  // disable automatic detection of RFIO on Windows while file name can contain ":"
591  if ((pc_temp = strchr(pc_server, ':')) != NULL) {
592  l_mode = GETEVT__RFIO;
593  /* printf("rfio file %s\n",pc_server);*/
594  }
595 #endif
596 
597 #ifndef RFIO
598  if (l_mode == GETEVT__RFIO) {
599  printf("rfio not supported!\n");
600  return (GETEVT__NOFILE);
601  }
602 #endif
603 
604  if (ps_info != NULL) {
605  *ps_info = NULL;
606  }
607  // when timeout is already set by f_evt_timeout(), do not overwrite
608  if (ps_chan->l_timeout == 0) {
609  ps_chan->l_timeout = -1;
610  } /* no timeout */
611 
612  if (strlen(pc_server) < sizeof(ps_chan->c_channel)) { // length of pc_server < 128
613  strncpy(ps_chan->c_channel, pc_server, sizeof(ps_chan->c_channel) - 1);
614  } else {
615  printf("file name to long!\n");
616  return (GETEVT__NOFILE);
617  }
618 
619  switch (l_mode) {
620  case GETEVT__FILE:
621 
622  if (strlen(pc_server) < sizeof(c_file)) { // length of pc_server < 256
623  strncpy(c_file, pc_server, sizeof(c_file) - 1);
624  } else {
625  printf("file name to long!\n");
626  return (GETEVT__NOFILE);
627  }
628 
629  if (strlen(c_file) < 5) {
630  strcat(c_file, ".lmd");
631  } else {
632  pc_temp = (CHARS*)&c_file[strlen(c_file) - 4];
633  if ((strcmp(pc_temp, ".LMD") != 0) && (strcmp(pc_temp, ".lmd") != 0)) {
634  strcat(c_file, ".lmd");
635  }
636  }
637 
638  if ((ps_chan->l_channel_no = open(c_file, GET__OPEN_FLAG)) == -1) {
639  return (GETEVT__NOFILE);
640  }
641  /* read first 512 bytes */
642  if (read(ps_chan->l_channel_no, c_temp, MIN_BUF_LGTH) != MIN_BUF_LGTH) {
643  printf("LMD format error: no LMD file: %s\n", c_file);
644  close(ps_chan->l_channel_no);
645  ps_chan->l_channel_no = -1;
646  return (GETEVT__NOLMDFILE);
647  }
648  // DABC
649  ps_chan->pLmd = NULL;
650  if ((*((INTS4*)(c_temp + 4)) == LMD__TYPE_FILE_HEADER_101_1) || (*((INTS4*)(c_temp + 4)) == 0x65000100)) {
651  close(ps_chan->l_channel_no);
652  ps_chan->pLmd = fLmdAllocateControl();
653  fLmdGetOpen(ps_chan->pLmd, c_file, NULL, LMD__BUFFER, LMD__NO_INDEX);
654  ps_chan->l_server_type = l_mode;
655  return GETEVT__SUCCESS;
656  }
657  // -- DABC
658  /* check for file header, return size and swap */
659  f_evt_check_buf(c_temp, &l_size_head, &l_is_goosybuf, &l_swap_head, &l_filehead);
660  if (((l_is_goosybuf == 0) & (l_filehead == 0)) | (l_size_head == 0)) {
661  printf("LMD format error: swap=%d, header=%d, isLMD=%d, size=%d\n",
662  l_swap_head,
663  l_filehead,
664  l_is_goosybuf,
665  l_size_head);
666  close(ps_chan->l_channel_no);
667  ps_chan->l_channel_no = -1;
668  return (GETEVT__NOLMDFILE);
669  }
670 
671  /* read file header and first buffer and check for goosy header */
672  if (l_filehead == 1) {
673  val = lseek(ps_chan->l_channel_no, 0, SEEK_SET); /* rewind file */
674  if (-1 == val) {
675  return (GETEVT__FAILURE);
676  }
677  l_header_size = l_size_head;
678  if (((s_filhe*)c_temp)->filhe_dlen > MAX__DLEN) {
679  l_header_size = ((s_filhe*)c_temp)->filhe_used * 2 + 48;
680  // printf("Large buffer, read short header %d bytes\n",l_header_size);
681  }
682  if (read(ps_chan->l_channel_no, c_temp, l_header_size) != l_header_size) {
683  printf("LMD format error: no LMD file: %s\n", c_file);
684  close(ps_chan->l_channel_no);
685  ps_chan->l_channel_no = -1;
686  return (GETEVT__NOLMDFILE);
687  }
688  if (read(ps_chan->l_channel_no, c_temp, MIN_BUF_LGTH) != MIN_BUF_LGTH) {
689  close(ps_chan->l_channel_no);
690  ps_chan->l_channel_no = -1;
691  return (GETEVT__RDERR);
692  }
693  f_evt_check_buf(c_temp, &l_size, &l_is_goosybuf, &l_swap, &l_dummy);
694  if ((l_is_goosybuf == 0) | (l_size != l_size_head) | (l_swap != l_swap_head)) {
695  printf("LMD format error: swap=%d, isLMD=%d, size=%d\n", l_swap, l_is_goosybuf, l_size);
696  close(ps_chan->l_channel_no);
697  ps_chan->l_channel_no = -1;
698  return (GETEVT__NOLMDFILE);
699  }
700  } /* check buffer behind header */
701  ps_chan->l_buf_size = l_size_head;
702  val = lseek(ps_chan->l_channel_no, 0, SEEK_SET); /* rewind file */
703  if (-1 == val) {
704  return (GETEVT__FAILURE);
705  }
706  if (ps_info != NULL) {
707  *ps_info = NULL;
708  }
709  /* found file header */
710  if (l_filehead == 1) {
711  if (read(ps_chan->l_channel_no, c_temp, l_header_size) != l_header_size) {
712  printf("LMD format error: no LMD file: %s\n", c_file);
713  close(ps_chan->l_channel_no);
714  ps_chan->l_channel_no = -1;
715  return (GETEVT__NOLMDFILE);
716  }
717  ps_filhe = (s_filhe*)c_temp;
718  if (ps_info != NULL) { /* if user want file header be returned */
719  if (l_swap_head == 1) {
720  f_evt_swap_filhe((s_bufhe*)ps_filhe);
721  }
722  *ps_info = c_temp; /* now , get file header and return */
723  }
724  /*
725  printf("type %d, subtype %d\n",ps_filhe->filhe_type,ps_filhe->filhe_subtype);
726  printf("strings %d %d %d %d %d %x\n",
727  ps_filhe->filhe_label_l,
728  ps_filhe->filhe_file_l,
729  ps_filhe->filhe_user_l,
730  ps_filhe->filhe_run_l,
731  ps_filhe->filhe_exp_l,
732  ps_filhe->filhe_lines);
733  pi=(INTS2 *)&ps_filhe->s_strings;
734  for(l_dummy=0;l_dummy<ps_filhe->filhe_lines;l_dummy++)
735  {
736  printf("comment %d, %s\n",*pi,(c_temp+366+l_dummy*80));
737  pi += 40;
738  }
739  */
740  } /* file header */
741 
742  /* points to a real buffer start */
743  /* and read header buffer, if there */
744  ps_chan->l_io_buf_size = ps_chan->l_buf_size;
745  /* may larger, but must multiplexed */
746  break;
747  case GETEVT__STREAM:
748  /* initialize connection with stream server */
749  // P.-A. Loizeau, 2015/11/26:
750  // In case of STREAM mode, catch the case where the user provided
751  // a nonstandard stream port, extract the value and clean the server
752  // name. Usefull with DABC MBS streams where the port can be user set.
753  // if(f_stc_connectserver(pc_server,PORT__STREAM_SERV,&ps_chan->l_channel_no,
754  if (f_stc_connectserver(pc_server, i_streamport, &ps_chan->l_channel_no, &s_tcpcomm_st_evt)
755  != STC__SUCCESS) {
756  return (GETEVT__NOSERVER);
757  }
758 
759  l_status = f_stc_read(c_temp, 16, ps_chan->l_channel_no, ps_chan->l_timeout);
760  if (l_status == STC__TIMEOUT) {
761  return (GETEVT__TIMEOUT);
762  }
763  if (l_status != STC__SUCCESS) {
764  return (GETEVT__RDERR);
765  }
766 
767  if (*((INTS4*)(c_temp)) != 1) {
768  f_evt_swap(c_temp, 16);
769  }
770  ps_chan->l_buf_size = *((INTS4*)(c_temp + 4)); /* buffer size */
771  ps_chan->l_bufs_in_stream = *((INTS4*)(c_temp + 8));
772  /* # buffers per stream */
773  ps_chan->l_stream_bufs = 0; /* counter */
774 
775  ps_chan->l_io_buf_size = (ps_chan->l_buf_size) * (ps_chan->l_bufs_in_stream);
776  // DABC
777  ps_chan->pLmd = NULL;
778  if (*((INTS4*)(c_temp + 12)) == 0) {
779  ps_chan->pLmd = fLmdAllocateControl();
780  ps_chan->pLmd->pTCP = &s_tcpcomm_st_evt;
781  fLmdInitMbs(ps_chan->pLmd,
782  pc_server,
783  ps_chan->l_buf_size,
784  ps_chan->l_bufs_in_stream,
785  0,
787  ps_chan->l_timeout);
788  printf("f_evt_get_open for STREAM: setting timeout=%d n", ps_chan->l_timeout);
789 
790  ps_chan->l_server_type = l_mode;
791  return GETEVT__SUCCESS;
792  }
793  // -- DABC
794  break;
795  case GETEVT__TRANS:
796  /* initialize connection with stream server */
797  if (f_stc_connectserver(pc_server, PORT__TRANSPORT, &ps_chan->l_channel_no, &s_tcpcomm_st_evt)
798  != STC__SUCCESS) {
799  return (GETEVT__NOSERVER);
800  }
801 
802  l_status = f_stc_read(c_temp, 16, ps_chan->l_channel_no, ps_chan->l_timeout);
803  if (l_status == STC__TIMEOUT) {
804  return (GETEVT__TIMEOUT);
805  }
806  if (l_status != STC__SUCCESS) {
807  return (GETEVT__RDERR);
808  }
809 
810  if (*((INTS4*)(c_temp)) != 1) {
811  f_evt_swap(c_temp, 16);
812  }
813  ps_chan->l_buf_size = *((INTS4*)(c_temp + 4)); /* buffer size */
814  ps_chan->l_bufs_in_stream = *((INTS4*)(c_temp + 8));
815  /* # buffers per stream */
816  ps_chan->l_io_buf_size = ps_chan->l_buf_size;
817  // DABC
818  ps_chan->pLmd = NULL;
819  if (*((INTS4*)(c_temp + 12)) == 0) {
820  ps_chan->pLmd = fLmdAllocateControl();
821  ps_chan->pLmd->pTCP = &s_tcpcomm_st_evt;
822  fLmdInitMbs(ps_chan->pLmd,
823  pc_server,
824  ps_chan->l_buf_size,
825  ps_chan->l_bufs_in_stream,
826  0,
828  ps_chan->l_timeout);
829  ps_chan->l_server_type = l_mode;
830  return GETEVT__SUCCESS;
831  }
832  // -- DABC
833  break;
834  case GETEVT__REVSERV:
835  // if(f_evcli_con(ps_chan, pc_server, l_gl_rev_port, -1, l_sample)!=STC__SUCCESS) {
836  // return(GETEVT__NOSERVER);
837  // }
838  break;
839  case GETEVT__EVENT:
840  // if(f_evcli_con(ps_chan, pc_server, l_gl_rev_port, -1, l_sample)!=STC__SUCCESS) {
841  // return(GETEVT__NOSERVER);
842  // }
843  break;
844 #ifdef RFIO
845  case GETEVT__RFIO:
846  ps_chan->l_channel_no = -1;
847  ps_chan->l_channel_no = RFIO_open(pc_server, GET__OPEN_FLAG, 0);
848  if (ps_chan->l_channel_no < 0) {
849  return (GETEVT__NOSERVER);
850  }
851  /* read first 512 bytes */
852  if (RFIO_read(ps_chan->l_channel_no, c_temp, MIN_BUF_LGTH) != MIN_BUF_LGTH) {
853  printf("LMD format error: no LMD file: %s\n", pc_server);
854  RFIO_close(ps_chan->l_channel_no);
855  ps_chan->l_channel_no = -1;
856  return (GETEVT__NOLMDFILE);
857  }
858  /* check for file header, return size and swap */
859  f_evt_check_buf(c_temp, &l_size_head, &l_is_goosybuf, &l_swap_head, &l_filehead);
860  if (((l_is_goosybuf == 0) & (l_filehead == 0)) | (l_size_head == 0)) {
861  printf("LMD format error: swap=%d, header=%d, isLMD=%d, size=%d\n",
862  l_swap_head,
863  l_filehead,
864  l_is_goosybuf,
865  l_size_head);
866  RFIO_close(ps_chan->l_channel_no);
867  ps_chan->l_channel_no = -1;
868  return (GETEVT__NOLMDFILE);
869  }
870  /* read file header and first buffer and check for goosy header */
871  if (l_filehead == 1) {
872  val = RFIO_lseek(ps_chan->l_channel_no, 0, SEEK_SET); /* rewind file */
873  if (-1 == val) {
874  return (GETEVT__FAILURE);
875  }
876  if (RFIO_read(ps_chan->l_channel_no, c_temp, l_size_head) != l_size_head) {
877  printf("LMD format error: no LMD file: %s\n", pc_server);
878  RFIO_close(ps_chan->l_channel_no);
879  ps_chan->l_channel_no = -1;
880  return (GETEVT__NOLMDFILE);
881  }
882  if (RFIO_read(ps_chan->l_channel_no, c_temp, MIN_BUF_LGTH) != MIN_BUF_LGTH) {
883  RFIO_close(ps_chan->l_channel_no);
884  ps_chan->l_channel_no = -1;
885  return (GETEVT__RDERR);
886  }
887  f_evt_check_buf(c_temp, &l_size, &l_is_goosybuf, &l_swap, &l_dummy);
888  if ((l_is_goosybuf == 0) | (l_size != l_size_head) | (l_swap != l_swap_head)) {
889  printf("LMD format error: swap=%d, isLMD=%d, size=%d\n", l_swap, l_is_goosybuf, l_size);
890  RFIO_close(ps_chan->l_channel_no);
891  ps_chan->l_channel_no = -1;
892  return (GETEVT__NOLMDFILE);
893  }
894  } /* check buffer behind header */
895  ps_chan->l_buf_size = l_size_head;
896  val = RFIO_lseek(ps_chan->l_channel_no, 0, SEEK_SET); /* rewind file */
897  if (-1 == val) {
898  return (GETEVT__FAILURE);
899  }
900  if (ps_info != NULL) {
901  *ps_info = NULL;
902  }
903  /* found file header */
904  if (l_filehead == 1) {
905  if (RFIO_read(ps_chan->l_channel_no, c_temp, l_size_head) != l_size_head) {
906  printf("LMD format error: no LMD file: %s\n", pc_server);
907  RFIO_close(ps_chan->l_channel_no);
908  ps_chan->l_channel_no = -1;
909  return (GETEVT__NOLMDFILE);
910  }
911  ps_filhe = (s_filhe*)c_temp;
912  if (ps_info != NULL) { /* if user want file header be returned */
913  if (l_swap_head == 1) {
914  f_evt_swap_filhe((s_bufhe*)ps_filhe);
915  }
916  *ps_info = c_temp; /* now , get file header and return */
917  }
918  /*
919  printf("type %d, subtype %d\n",ps_filhe->filhe_type,ps_filhe->filhe_subtype);
920  printf("strings %d %d %d %d %d %x\n",
921  ps_filhe->filhe_label_l,
922  ps_filhe->filhe_file_l,
923  ps_filhe->filhe_user_l,
924  ps_filhe->filhe_run_l,
925  ps_filhe->filhe_exp_l,
926  ps_filhe->filhe_lines);
927  pi=(INTS2 *)&ps_filhe->s_strings;
928  for(l_dummy=0;l_dummy<ps_filhe->filhe_lines;l_dummy++)
929  {
930  printf("comment %d, %s\n",*pi,(c_temp+366+l_dummy*80));
931  pi += 40;
932  }
933  */
934  } /* file header */
935  ps_chan->l_io_buf_size = ps_chan->l_buf_size;
936  break;
937 #endif
938  default:
939  if (ps_info != NULL) {
940  *ps_info = NULL;
941  }
942  return (GETEVT__NOSERVER);
943  } /* end of switch */
944 
945  if ((l_mode != GETEVT__EVENT) & (l_mode != GETEVT__REVSERV)) {
946  if ((ps_chan->pc_io_buf = malloc(ps_chan->l_io_buf_size)) == NULL) {
947  printf("Memory allocation error\n");
948  exit(2);
949  }
950  ps_chan->l_evt_buf_size = ps_chan->l_io_buf_size;
951  if ((ps_chan->pc_evt_buf = malloc(ps_chan->l_evt_buf_size)) == NULL) {
952  printf("Memory allocation error\n");
953  exit(2);
954  }
955  } /* l_mode != GETEVT__EVENT */
956  ps_chan->l_server_type = l_mode;
957  ps_chan->l_first_get = 1; /* so we will first call f_getvet_get */
958  return GETEVT__SUCCESS;
959 } /* end of f_evt_get_open */
960 
961 /*1+ C Main ****************+******************************************/
962 /*+ Module : f_evt_get_event */
963 /*--------------------------------------------------------------------*/
964 /*+ CALLING : f_evt_get_event(s_evt_channel &s_chan, long **ppl_buffer, long **ppl_goobuf) */
965 /*--------------------------------------------------------------------*/
966 /* */
967 /*+ PURPOSE : f_evt_get_event returnes address of event */
968 /*+ ARGUMENTS : */
969 /*+ s_chan : Input channel from open. */
970 /*+ ppl_buffer: Address of pointer. Returns address of event. */
971 /*+ ppl_goobuf: Address of pointer. Returns address of buffer. */
972 /*+ Return type : int. */
973 /*+ Status codes: */
974 /*- GETEVT__SUCCESS : success. */
975 /*- GETEVT__FRAGMENT : Event fragment found. */
976 /*- GETEVT__NOMORE : No more events. */
977 /*- GETEVT__RDERR : read server or file error */
978 /*- GETEVT__TIMEOUT : when enabled by f_evt_timeout */
979 /*+ Declaration : */
980 /* INTS4 f_evt_get_event( */
981 /* s_evt_channel *, INTS4 **, INTS4 **); */
982 /*+ FUNCTION : Get next event and returnes pointer. The pointer */
983 /* may point to the event in the buffer or internal */
984 /* event buffer (spanned events). The content of the */
985 /* pointer may be destroyed by next call. */
986 /*1- C Main ****************+******************************************/
987 INTS4 f_evt_get_event(s_evt_channel* ps_chan, INTS4** ppl_buffer, INTS4** ppl_goobuf)
988 {
989  INTS4 l_temp, l_prev_ok = 1, l_stat = LMD__SUCCESS, l_used;
990  s_bufhe* ps_bufhe_cur;
991  // s_ve10_1* ps_ve10_1;
992  sMbsHeader* pevt;
993 
994  // DABC
995  if (ps_chan->pLmd != NULL) {
996  if (ps_chan->l_server_type == GETEVT__TRANS) {
997  l_stat = fLmdGetMbsEvent(ps_chan->pLmd, &pevt);
998  } else if (ps_chan->l_server_type == GETEVT__STREAM) {
999  l_stat = fLmdGetMbsEvent(ps_chan->pLmd, &pevt);
1000  } else if (ps_chan->l_server_type == GETEVT__FILE) {
1001  l_stat = fLmdGetElement(ps_chan->pLmd, LMD__NO_INDEX, &pevt);
1002  } else {
1003  pevt = NULL;
1004  }
1005 
1006  // any error, then pointer is null
1007  if (pevt == NULL) {
1008  if (ps_chan->l_server_type == GETEVT__FILE) {
1009  if (l_stat == GETLMD__NOMORE) {
1010  return (GETEVT__NOMORE);
1011  }
1012  if (l_stat == GETLMD__EOFILE) {
1013  return (GETEVT__NOMORE);
1014  }
1015  if (l_stat == GETLMD__NOBUFFER) {
1016  return (GETEVT__FAILURE);
1017  }
1018  return (GETEVT__RDERR);
1019  } else {
1020  if (l_stat == LMD__TIMEOUT) {
1021  return (GETEVT__TIMEOUT);
1022  } else {
1023  return (GETEVT__RDERR);
1024  }
1025  }
1026  }
1027  // OK
1028  if (ppl_goobuf) {
1029  *ppl_goobuf = NULL;
1030  }
1031  *ppl_buffer = (INTS4*)pevt;
1032  return (GETEVT__SUCCESS);
1033  }
1034  // -- DABC
1035  // if((ps_chan->l_server_type == GETEVT__EVENT)|(ps_chan->l_server_type == GETEVT__REVSERV)) {
1036  // *ppl_goobuf = NULL;
1037  // if(f_evcli_evt(ps_chan) != STC__SUCCESS) { /* no more event, get new buffer */
1038  // l_stat=f_evcli_buf(ps_chan);
1039  // if(l_stat == STC__TIMEOUT) { return(GETEVT__TIMEOUT); }
1040  // if(l_stat != STC__SUCCESS) { return(GETEVT__FAILURE); }
1041  // }
1042  // *ppl_buffer = (INTS4*) ps_chan->pc_evt_buf;
1043  // return(GETEVT__SUCCESS);
1044  // }
1045 
1046  /* e.g. read_buffer is 16384, GOOSY buffer may be only 2048, *
1047  * this ps_chan->l_io_buf_posi indicats when the read_buffer is finished */
1048 
1049  if (ps_chan->l_first_get == 1) {
1050  ps_chan->l_buf_posi = 0; /* goosy buffer position */
1051  ps_chan->l_buf_lmt = 0; /* end of this goosy buffer */
1052  ps_chan->l_io_buf_posi = MAX_LONG; /* Iuput buffer(for read file) position */
1053  ps_chan->l_first_buf = 1; /* boolean */
1054  ps_chan->l_first_get = 0; /* boolean */
1055  }
1056  /* why we need this part codes? beacause we may (open, get, close) *
1057  * and (open, get, close) again!!! Especially when we use m-histo *
1058  * so the second or later (open,get,close) should reset these *
1059  * static value */
1060 
1061  ps_chan->l_evt_buf_posi = 0;
1062  /* above, internal event buffer position, internal buffer will be returned */
1063 
1064  while (1) {
1065  /* if previous goosy buffer has finished, read a new buffer from i/o buffer,
1066  or from file if i/o buffer is all readout */
1067  while (ps_chan->l_buf_posi >= ps_chan->l_buf_lmt) {
1068  /* if this i/o buffer is read to end */
1069  /* end of this read_buffer which may contain several GOOSY buffers*/
1070  if (ps_chan->l_io_buf_posi >= ps_chan->l_io_buf_size) {
1071  if ((l_temp = f_evt_get_newbuf(ps_chan)) != GETEVT__SUCCESS) {
1072  return (l_temp);
1073  }
1074  ps_chan->l_io_buf_posi = 0;
1075  } /* end of real read server */
1076  ps_chan->ps_bufhe = (s_bufhe*)(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi);
1077  l_prev_ok = (ps_chan->l_buf_no == (ps_chan->ps_bufhe->l_buf - 1));
1078  ps_chan->l_buf_no = ps_chan->ps_bufhe->l_buf;
1079  if (ps_chan->ps_bufhe->i_type == 2000) { /* file header */
1080  printf("Unsolicited file header found!\n");
1081  ps_chan->l_io_buf_posi += ps_chan->l_buf_size;
1082  ps_chan->l_buf_posi = ps_chan->l_io_buf_posi;
1083  ps_chan->l_buf_lmt = ps_chan->l_io_buf_posi;
1084  } else {
1085  l_used = ps_chan->ps_bufhe->l_free[2]; // large buffers HE, Oct 2007
1086  if (ps_chan->ps_bufhe->l_dlen <= MAX__DLEN) {
1087  l_used = ps_chan->ps_bufhe->i_used;
1088  }
1089  ps_chan->l_buf_posi = ps_chan->l_io_buf_posi + sizeof(s_bufhe);
1090  ps_chan->l_buf_lmt = ps_chan->l_buf_posi + l_used * 2;
1091  ps_chan->l_io_buf_posi += ps_chan->l_buf_size;
1092  }
1093  } /* end of read file while loop */
1094 
1095  /* now, ps_chan->l_buf_posi points to start of an event or spanned event */
1096  ps_chan->ps_ve10_1 = (s_ve10_1*)(ps_chan->pc_io_buf + ps_chan->l_buf_posi);
1097  if ((ps_chan->l_evt_buf_posi != 0)
1098  || ((ps_chan->ps_bufhe->h_end == 1)
1099  && (ps_chan->l_first_buf == 1))) { /* if this is a spanned part of an event */
1100  ps_chan->l_buf_posi += sizeof(s_evhe);
1101  ps_chan->l_frag_len = ps_chan->ps_ve10_1->l_dlen * 2;
1102  } else { /* if this is a real start of an event */
1103  ps_chan->l_frag_len = (ps_chan->ps_ve10_1->l_dlen - 4) * 2 + sizeof(s_ve10_1);
1104  }
1105 
1106  if (ps_chan->l_frag_len + ps_chan->l_buf_posi > ps_chan->l_buf_lmt) {
1107  return (GETEVT__FRAGMENT);
1108  }
1109 
1110  /* if ps_chan->l_buf_posi is not start of an event and ps_chan->l_first_buf =1 *
1111  * then skip to next event */
1112  if ((ps_chan->ps_bufhe->h_end == 1) && ((ps_chan->l_first_buf == 1) || (l_prev_ok == 0))) {
1113  /* if the first buffer is spanned at begin, then skip */
1114  ps_chan->l_first_buf = 0; /* 24-Apr-1996 */
1115  l_prev_ok = 1; /* 2001 HE */
1116  ps_chan->l_evt_buf_posi = 0; /* 2001 HE stuff in event buffer obsolete */
1117  ps_chan->l_buf_posi += ps_chan->l_frag_len;
1118  /* now,ps_chan->l_buf_posi points to start of an event or spanned event */
1119  continue; /* continue "while" loop */
1120  }
1121 
1122  ps_chan->l_first_buf = 0;
1123 
1124  /* if ps_chan->l_buf_posi is start of an event and the event is not *
1125  * spanned, then return pointer */
1126  if ((ps_chan->l_evt_buf_posi == 0)
1127  && ((ps_chan->ps_bufhe->h_begin == 0)
1128  || ((ps_chan->l_buf_posi + ps_chan->l_frag_len) < ps_chan->l_buf_lmt))) {
1129  *ppl_buffer = (INTS4*)(ps_chan->pc_io_buf + ps_chan->l_buf_posi);
1130  ps_chan->l_buf_posi += ps_chan->l_frag_len;
1131  ps_chan->l_evt_buf_posi = 0;
1132  if (ppl_goobuf) {
1133  *ppl_goobuf = (INTS4*)(ps_chan->ps_bufhe);
1134  }
1135  return (GETEVT__SUCCESS);
1136  }
1137 
1138  /* 2001 HE if we start a spanned event, save buffer header of first buffer */
1139  if (ps_chan->l_evt_buf_posi == 0) {
1140  memcpy((CHARS*)&ps_chan->s_bufhe_1, (CHARS*)ps_chan->ps_bufhe, sizeof(s_bufhe));
1141  ps_bufhe_cur = (s_bufhe*)&ps_chan->s_bufhe_1;
1142  }
1143  /* copy the part of this event which in this buffer into *
1144  * internal buffer, data will be moved by realloc function */
1145  if (ps_chan->l_evt_buf_size < ps_chan->l_evt_buf_posi + ps_chan->l_frag_len) {
1146  ps_chan->l_evt_buf_size = ps_chan->l_evt_buf_posi + ps_chan->l_frag_len;
1147  if ((ps_chan->pc_evt_buf = realloc(ps_chan->pc_evt_buf, ps_chan->l_evt_buf_size)) == NULL) {
1148  printf("Memory allocation error\n");
1149  exit(2);
1150  }
1151  }
1152  memcpy(ps_chan->pc_evt_buf + ps_chan->l_evt_buf_posi,
1153  ps_chan->pc_io_buf + ps_chan->l_buf_posi,
1154  ps_chan->l_frag_len);
1155  ps_chan->l_buf_posi += ps_chan->l_frag_len;
1156  if (ps_chan->l_evt_buf_posi == 0) {}
1157  ps_chan->l_evt_buf_posi += ps_chan->l_frag_len;
1158 
1159  if ((ps_chan->ps_bufhe->h_begin != 1) || (ps_chan->l_buf_posi < ps_chan->l_buf_lmt)) {
1160  /* change event header's l_dlen */
1161  ((s_ve10_1*)(ps_chan->pc_evt_buf))->l_dlen = ps_chan->l_evt_buf_posi / 2 - 4;
1162  *ppl_buffer = (INTS4*)(ps_chan->pc_evt_buf);
1163  if (ppl_goobuf) {
1164  *ppl_goobuf = (INTS4*)(ps_bufhe_cur);
1165  }
1166  return (GETEVT__SUCCESS);
1167  }
1168  } /* if this event spanned to next buffer, then loop */
1169 } /* end of f_evt_get_event */
1170 
1171 /*1+ C Main ******************+****************************************/
1172 /*+ Module : f_evt_get_close */
1173 /*--------------------------------------------------------------------*/
1174 /*+ CALLING : f_evt_get_close( s_evt_channel &s_chan) */
1175 /*--------------------------------------------------------------------*/
1176 /* */
1177 /*+ PURPOSE : f_evt_get_close closes event stream of specified */
1178 /* channel. */
1179 /*+ ARGUMENTS : */
1180 /*+ s_chan : Input channel from open. */
1181 /*+ Return type : int. */
1182 /*+ Status codes: */
1183 /*- GETEVT__SUCCESS : success. */
1184 /*- GETEVT__CLOSE_ERR : close server or file error */
1185 /*+ Declaration : */
1186 /* INTS4 f_evt_get_close(s_evt_channel *); */
1187 /*+ FUNCTION : Closes the specified input channel. */
1188 /*1- C Main ******************+****************************************/
1190 {
1191  INTS4 l_close_failure;
1192  // INTS4 l_status;
1193 
1194  // DABC
1195  if (ps_chan->pLmd != NULL) {
1196  if (ps_chan->l_server_type == GETEVT__TRANS) {
1197  fLmdCloseMbs(ps_chan->pLmd);
1198  } else if (ps_chan->l_server_type == GETEVT__STREAM) {
1199  fLmdCloseMbs(ps_chan->pLmd);
1200  } else if (ps_chan->l_server_type == GETEVT__FILE) {
1201  fLmdGetClose(ps_chan->pLmd);
1202  }
1203  free(ps_chan->pLmd);
1204  ps_chan->pLmd = NULL;
1205  return GETEVT__SUCCESS;
1206  }
1207  // -- DABC
1208  l_close_failure = 0;
1209  if (ps_chan->l_channel_no >= 0) {
1210  switch (ps_chan->l_server_type) {
1211  case GETEVT__FILE:
1212  if (close(ps_chan->l_channel_no) == -1) {
1213  l_close_failure = 1;
1214  }
1215  if (ps_chan->pc_io_buf != NULL) {
1216  free(ps_chan->pc_io_buf);
1217  }
1218  if (ps_chan->pc_evt_buf != NULL) {
1219  free(ps_chan->pc_evt_buf);
1220  }
1221  break;
1222  case GETEVT__STREAM:
1223  /* disconnect with stream server */
1224  f_stc_write("CLOSE", 6, ps_chan->l_channel_no);
1225  if (f_stc_discclient(ps_chan->l_channel_no) != STC__SUCCESS) {
1226  l_close_failure = 1;
1227  }
1228  if (f_stc_close(&s_tcpcomm_st_evt) != STC__SUCCESS) {
1229  l_close_failure = 1;
1230  }
1231  if (ps_chan->pc_io_buf != NULL) {
1232  free(ps_chan->pc_io_buf);
1233  }
1234  if (ps_chan->pc_evt_buf != NULL) {
1235  free(ps_chan->pc_evt_buf);
1236  }
1237  break;
1238  case GETEVT__TRANS:
1239  /* disconnect with stream server */
1240  if (f_stc_discclient(ps_chan->l_channel_no) != STC__SUCCESS) {
1241  l_close_failure = 1;
1242  }
1243  if (f_stc_close(&s_tcpcomm_st_evt) != STC__SUCCESS) {
1244  l_close_failure = 1;
1245  }
1246  if (ps_chan->pc_io_buf != NULL) {
1247  free(ps_chan->pc_io_buf);
1248  }
1249  if (ps_chan->pc_evt_buf != NULL) {
1250  free(ps_chan->pc_evt_buf);
1251  }
1252  break;
1253  case GETEVT__REVSERV:
1254  case GETEVT__EVENT:
1255  // if(f_evcli_close(ps_chan)!=STC__SUCCESS) { l_close_failure=1; }
1256  break;
1257  case GETEVT__RFIO:
1258  RFIO_close(ps_chan->l_channel_no);
1259  ps_chan->l_channel_no = -1;
1260  if (ps_chan->pc_io_buf != NULL) {
1261  free(ps_chan->pc_io_buf);
1262  }
1263  if (ps_chan->pc_evt_buf != NULL) {
1264  free(ps_chan->pc_evt_buf);
1265  }
1266  break;
1267  default:
1268  l_close_failure = 1;
1269  } /* end of switch */
1270  ps_chan->pc_io_buf = NULL;
1271  ps_chan->l_channel_no = -1;
1272  if (l_close_failure == 1) {
1273  return (GETEVT__CLOSE_ERR);
1274  }
1275  return GETEVT__SUCCESS;
1276  }
1277  return GETEVT__SUCCESS;
1278 
1279 } /* end of f_evt_get_close */
1280 
1281 /*1+ C Main ****************+******************************************/
1282 /*+ Module : f_evt_put_open */
1283 /*--------------------------------------------------------------------*/
1284 /*+ CALLING : f_evt_put_open(char *c_file[], long l_size, long l_stream, long l_type, */
1285 /* long l_subtype, s_evt_channel *ps_chan, char *ps_filhe) */
1286 /*--------------------------------------------------------------------*/
1287 /* */
1288 /*+ PURPOSE : f_evt_put_open opens an event output stream. */
1289 /*+ ARGUMENTS : */
1290 /*+ c_file : Name of file. */
1291 /*+ l_size : Size of output buffers in bytes. */
1292 /*+ l_stream : Number of buffers with spanning events. */
1293 /*+ l_type : Buffer type number */
1294 /*+ l_subtype : Buffer subtype number */
1295 /*+ ps_chan : Address of channel structure which will be returned.*/
1296 /*+ ps_filhe : Address of user specified file header */
1297 /*+ Return type : int. */
1298 /*+ Status codes: */
1299 /*- PUTEVT__SUCCESS : success. */
1300 /*- PUTEVT__FILE_EXIST: file already exists. */
1301 /*- PUTEVT__FAILURE : failure. */
1302 /*+ Declaration : */
1303 /* INTS4 f_evt_put_open( */
1304 /* CHARS *,INTS4,INTS4,INTS4,INTS4,s_evt_channel *,CHARS *); */
1305 /*+ FUNCTION : Opens the output channel and save context in */
1306 /* s_evt_channel structure. */
1307 /*+ NOTE : Up to four output channels can be opened. */
1308 /* User Example : In m_lea_user.c */
1309 /*1- C Main ****************+******************************************/
1311  INTS4 l_size,
1312  INTS4 l_stream,
1313  INTS4 l_type,
1314  INTS4 l_subtype,
1315  s_evt_channel* ps_chan,
1316  CHARS* ps_filhe)
1317 {
1318  s_filhe* ps_file_head;
1319  INTS4 l_write_size;
1320  INTS4 l_status; //,ll;
1321  time_t s_timet;
1322  struct timeb s_timeb;
1323  /* because "timeb" is not "typedef", so we must use "struct" */
1324  CHARS c_mode[80];
1325  CHARS c_file[256], *pc_temp;
1326  struct s_varstr
1327  {
1328  INTS2 i_varstr;
1329  CHARS c_varstr[128];
1330  } /* s_varstr_file*/;
1331 
1332  // DABC
1333  ps_chan->pLmd = NULL;
1334  if (l_stream == 0) {
1335  ps_chan->pLmd = fLmdAllocateControl();
1336  l_status = fLmdPutOpen(ps_chan->pLmd, pc_file, NULL, l_size, LMD__NO_OVERWRITE, LMD__INDEX, LMD__LARGE_FILE);
1337  return (l_status);
1338  }
1339  // -- DABC
1340 
1341  ps_chan->l_first_put = 1;
1342 
1343  ps_chan->l_buf_size = l_size;
1344  ps_chan->l_bufs_in_stream = l_stream;
1345  ps_chan->l_buf_type = l_type;
1346  ps_chan->l_buf_subtype = l_subtype;
1347  ps_chan->l_io_buf_size = ps_chan->l_buf_size * ps_chan->l_bufs_in_stream;
1348  if ((ps_chan->pc_io_buf = malloc(ps_chan->l_io_buf_size)) == NULL) {
1349  printf("Memory allocation error\n");
1350  exit(2);
1351  }
1352 
1353  size_t len = strlen(pc_file);
1354  if (len < sizeof(c_file)) {
1355  strncpy(c_file, pc_file, len);
1356  } else {
1357  strncpy(c_file, pc_file, sizeof(c_file) - 1);
1358  }
1359  if (strlen(c_file) < 5) {
1360  strcat(c_file, ".lmd");
1361  } else {
1362  pc_temp = (CHARS*)&c_file[strlen(c_file) - 4];
1363  if ((strcmp(pc_temp, ".LMD") != 0) && (strcmp(pc_temp, ".lmd") != 0)) {
1364  strcat(c_file, ".lmd");
1365  }
1366  }
1367  if ((ps_chan->l_channel_no = open(c_file, PUT__OPEN_APD_FLAG)) != -1) {
1368  return (PUTEVT__FILE_EXIST);
1369  } else {
1370  if ((ps_chan->l_channel_no = open(c_file, PUT__CRT_FLAG, DEF_FILE_ACCE)) == -1) {
1371  return (PUTEVT__FAILURE);
1372  }
1373  /* open OK */
1374  else {
1375  /* output file header */
1376  ps_file_head = (s_filhe*)ps_chan->pc_io_buf;
1377  /* if user specify file header */
1378  if (ps_filhe != NULL) {
1379  memcpy(ps_file_head, ps_filhe, ps_chan->l_buf_size);
1380  } else {
1381  memset(ps_file_head, '\0', ps_chan->l_buf_size);
1382  sprintf(ps_file_head->filhe_run, "Pid %d", getpid());
1383  ps_file_head->filhe_run_l = strlen(ps_file_head->filhe_run);
1384  }
1385  ps_file_head->filhe_dlen = ps_chan->l_buf_size / 2;
1386  ps_file_head->filhe_subtype = 1;
1387  ps_file_head->filhe_type = 2000;
1388  ftime(&s_timeb);
1389  ps_file_head->filhe_stime[0] = (INTS4)s_timeb.time;
1390  ps_file_head->filhe_stime[1] = (INTS4)s_timeb.millitm;
1391  ps_file_head->filhe_free[0] = 1;
1392  ps_file_head->filhe_file_l = strlen(c_file); /* not include \0 */
1393  strcpy(ps_file_head->filhe_file, c_file);
1394  char* username = getenv("USER");
1395  if (username) {
1396  size_t len = strlen(username);
1397  // maximum length for user array in s_filhe is 30
1398  if (len < sizeof(ps_file_head->filhe_user)) {
1399  strncpy(ps_file_head->filhe_user, username, len); /* user name */
1400  } else {
1401  strncpy(ps_file_head->filhe_user, username, sizeof(ps_file_head->filhe_user) - 1); /* user name */
1402  }
1403  }
1404  ps_file_head->filhe_user_l = strlen(ps_file_head->filhe_user);
1405 
1406  /* get calendar time
1407  The returned string from ctime is of the format
1408  Www Mmm dd hh:mm:ss yyyy
1409  The string is followed by a new-line character ('\n') and terminated with a null-character.
1410  The strinh has a length of 25 characters
1411  */
1412  time(&s_timet);
1413  char* temptime = ctime(&s_timet);
1414 
1415  if (strlen(temptime) < sizeof(c_mode)) {
1416  strncpy(c_mode, temptime, sizeof(c_mode) - 1);
1417  } else {
1418  return (PUTEVT__FAILURE);
1419  }
1420 
1421  if (strlen(&c_mode[4]) < sizeof(ps_file_head->filhe_time)) {
1422  strncpy(ps_file_head->filhe_time, &c_mode[4], sizeof(ps_file_head->filhe_time) - 1);
1423  } else {
1424  return (PUTEVT__FAILURE);
1425  }
1426 
1427  // Remove the terminating new-line character
1428  ps_file_head->filhe_time[20] = ' ';
1429  l_write_size = write(ps_chan->l_channel_no, (CHARS*)ps_file_head, ps_chan->l_buf_size);
1430  if (l_write_size == -1) {
1431  return (PUTEVT__WRERR);
1432  }
1433  if (l_write_size != ps_chan->l_buf_size) {
1434  return (PUTEVT__TOOBIG);
1435  }
1436  return (PUTEVT__SUCCESS);
1437  }
1438  }
1439 } /* end of f_evt_put_open */
1440 
1441 /*1+ C Main ****************+******************************************/
1442 /*+ Module : f_evt_put_event */
1443 /*--------------------------------------------------------------------*/
1444 /*+ CALLING : f_evt_put_event(s_evt_channel *ps_chan, long &la_evt_buf[]) */
1445 /*--------------------------------------------------------------------*/
1446 /* */
1447 /*+ PURPOSE : f_evt_put_event outputs event */
1448 /*+ ARGUMENTS : */
1449 /*+ ps_chan : Address of channel structure as returned from */
1450 /* f_evt_put_open. */
1451 /*+ la_evt_buf : event data array. Standard GSI event structure. */
1452 /*+ Return type : int. */
1453 /*+ Status codes: */
1454 /*- PUTEVT__SUCCESS : success. */
1455 /*- PUTEVT__WRERR : read server or file error */
1456 /*+ Declaration : */
1457 /* INTS4 f_evt_put_event(s_evt_channel *, INTS4 *); */
1458 /*+ FUNCTION : Copies current event into output buffer. Writes */
1459 /* buffer to file, when full. */
1460 /*1- C Main ****************+******************************************/
1461 
1463 {
1464  INTS4 l_evt_buf_posi;
1465  INTS4 l_buf_remain_size; /* net free space of I/O buffer */
1466  INTS4 l_write_size, l_temp, l_free;
1467  INTS4 l_status;
1468  CHARS* pc_addr;
1469  s_bufhe* ps_bufhe;
1470 
1471  // DABC
1472  if (ps_chan->pLmd != NULL) {
1473  l_status = fLmdPutElement(ps_chan->pLmd, (sMbsHeader*)pl_evt_buf);
1474  return (l_status);
1475  }
1476  // -- DABC
1477  if (ps_chan->l_first_put == 1) {
1478  ps_chan->l_first_put = 0;
1479  ps_chan->l_io_buf_posi = 0;
1480  ps_chan->l_buf_no = 1;
1481  }
1482  l_evt_buf_posi = 0;
1483  ps_chan->l_evt_size = (((s_ve10_1*)(pl_evt_buf))->l_dlen) * 2 + sizeof(s_evhe);
1484 
1485  /* get l_buf_remain_size is available size in stream */
1486  l_buf_remain_size = ps_chan->l_io_buf_size - ps_chan->l_io_buf_posi;
1487  l_temp = (l_buf_remain_size / ps_chan->l_buf_size) * (sizeof(s_bufhe) + sizeof(s_evhe));
1488  l_buf_remain_size -= l_temp; /* minus space occupied by buffer header and spanned event header */
1489 
1490  if (ps_chan->l_evt_size > l_buf_remain_size) { /* output this i/o buffer */
1491  memset(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi, '\0', ps_chan->l_io_buf_size - ps_chan->l_io_buf_posi);
1492  /* clear rest of this GOOSY buffer */
1493  l_temp = (ps_chan->l_io_buf_posi / ps_chan->l_buf_size) * ps_chan->l_buf_size;
1494  if (ps_chan->l_io_buf_posi % ps_chan->l_buf_size == 0) {
1495  l_temp -= ps_chan->l_buf_size;
1496  }
1497  /* l_temp points to start of last GOOSY buf in i/o buf area */
1498  ps_chan->ps_bufhe = (s_bufhe*)(ps_chan->pc_io_buf + l_temp);
1499  if (l_temp == 0) {
1500  ps_chan->ps_bufhe->h_end = 0;
1501  } /* first buf in stream, no end */
1502  else {
1503  ps_chan->ps_bufhe->h_end = ((s_bufhe*)((CHARS*)ps_chan->ps_bufhe - ps_chan->l_buf_size))->h_begin;
1504  }
1505  ps_chan->ps_bufhe->h_begin = 0; /* evt has end, so not spanned to next buf */
1506  for (l_temp = 0; l_temp < ps_chan->l_io_buf_size; l_temp += ps_chan->l_buf_size) {
1507  pc_addr = ps_chan->pc_io_buf + l_temp;
1508  ps_bufhe = (s_bufhe*)pc_addr;
1509  if (ps_bufhe->l_evt > 0) { /* do not write empty buffers */
1510  l_write_size = write(ps_chan->l_channel_no, pc_addr, ps_chan->l_buf_size);
1511  if (l_write_size == -1) {
1512  return (PUTEVT__WRERR);
1513  }
1514  if (l_write_size != ps_chan->l_buf_size) {
1515  return (PUTEVT__TOOBIG);
1516  }
1517  } else {
1518  ps_chan->l_buf_no--;
1519  } /* decrement buffer number for not written buffer */
1520  } /* buffer output loop */
1521  ps_chan->l_io_buf_posi = 0;
1522  ps_chan->ps_bufhe = (s_bufhe*)ps_chan->pc_io_buf;
1523  /* get l_buf_remain_size of new stream */
1524  l_buf_remain_size = ps_chan->l_io_buf_size;
1525  l_temp = (l_buf_remain_size / ps_chan->l_buf_size) * (sizeof(s_bufhe) + sizeof(s_evhe));
1526  l_buf_remain_size -= l_temp; /* minus space occupied by buf header and spanned event header */
1527  l_buf_remain_size += sizeof(s_evhe); /* 1st buf always no span */
1528  /* if event can not fit in a new stream, then error */
1529  if (ps_chan->l_evt_size > l_buf_remain_size) {
1530  return (PUTEVT__TOO_SMALLS);
1531  }
1532  }
1533 
1534  if (ps_chan->l_io_buf_posi == 0) {
1535  f_evt_ini_bufhe(ps_chan);
1536  } /* init all buffer headers */
1537 
1538  /* write event into i/o buf till end of event, change ps_chan->l_io_buf_posi */
1539  while (l_evt_buf_posi < ps_chan->l_evt_size) {
1540  ps_chan->ps_ve10_1 = (s_ve10_1*)(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi);
1541  if ((ps_chan->l_io_buf_posi % ps_chan->l_buf_size) == 0) {
1542  ps_chan->l_io_buf_posi += sizeof(s_bufhe);
1543  ps_chan->ps_ve10_1 = (s_ve10_1*)(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi); /* behind header */
1544  if (l_evt_buf_posi != 0) {
1545  ps_chan->l_io_buf_posi += sizeof(s_evhe); /* behind element header */
1546  l_write_size = (ps_chan->l_io_buf_posi / ps_chan->l_buf_size) * ps_chan->l_buf_size;
1547  /* l_write_size points to start of this GOOSY buf in i/o buf area */
1548  ps_chan->ps_bufhe = (s_bufhe*)(ps_chan->pc_io_buf + l_write_size);
1549  ps_chan->ps_bufhe->i_used += sizeof(s_evhe) / 2;
1550  }
1551  }
1552  l_write_size = (ps_chan->l_io_buf_posi / ps_chan->l_buf_size) * ps_chan->l_buf_size;
1553  /* l_write_size points to start of this GOOSY buf in i/o buf area */
1554  ps_chan->ps_bufhe = (s_bufhe*)(ps_chan->pc_io_buf + l_write_size);
1555  l_write_size = l_write_size + ps_chan->l_buf_size - ps_chan->l_io_buf_posi;
1556  /* l_write_size is remain free space in this GOOSY buf */
1557  if (l_write_size > (ps_chan->l_evt_size - l_evt_buf_posi)) {
1558  l_write_size = (ps_chan->l_evt_size - l_evt_buf_posi);
1559  }
1560  memcpy(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi, (CHARS*)pl_evt_buf + l_evt_buf_posi, l_write_size);
1561  ps_chan->ps_bufhe->l_evt++; /* number of fragments */
1562  ps_chan->l_io_buf_posi += l_write_size;
1563  l_evt_buf_posi += l_write_size;
1564  ps_chan->ps_bufhe->i_used += l_write_size / 2;
1565  /* if the remain free space of GOOSY buffer does not large enough *
1566  * to hold even a event header, then turn to next GOOSY buffer and *
1567  * fill buffer header. or 8 long words aligned ???????? */
1568  l_free = (ps_chan->l_io_buf_posi / ps_chan->l_buf_size) * ps_chan->l_buf_size;
1569  /* l_free points to start of this GOOSY buf in i/o buf area */
1570  l_free = l_free + ps_chan->l_buf_size - ps_chan->l_io_buf_posi;
1571  if (l_free == ps_chan->l_buf_size) {
1572  l_free = 0;
1573  }
1574  /* l_free is remain free space in this GOOSY buf */
1575  if (l_free < sizeof(s_ve10_1)) {
1576  ps_chan->l_io_buf_posi += l_free;
1577  }
1578  /* change spanned evt header l_dlen */
1579  if (l_evt_buf_posi != l_write_size) {
1580  ps_chan->ps_ve10_1->l_dlen = l_write_size / 2;
1581  ps_chan->ps_ve10_1->i_subtype = ps_chan->l_buf_subtype;
1582  ps_chan->ps_ve10_1->i_type = ps_chan->l_buf_type;
1583  } else {
1584  ps_chan->ps_ve10_1->l_dlen = (l_write_size - sizeof(s_evhe)) / 2;
1585  } /* header of first frag */
1586  if ((ps_chan->l_io_buf_posi % ps_chan->l_buf_size) == 0) {
1587  if (l_evt_buf_posi < ps_chan->l_evt_size) {
1588  ps_chan->ps_bufhe->h_begin = 1;
1589  }
1590  if (ps_chan->l_io_buf_posi > ps_chan->l_buf_size) { /* so first buf in stream */
1591  ps_chan->ps_bufhe->h_end = ((s_bufhe*)((CHARS*)ps_chan->ps_bufhe - ps_chan->l_buf_size))->h_begin;
1592  }
1593  ps_chan->ps_bufhe->l_free[1] = ((s_ve10_1*)(pl_evt_buf))->l_dlen; /* lgth of last event in buf */
1594  } /* end of processs prev buffer head and ... */
1595 
1596  } /* end of write event into i/o buf till end of event */
1597  return (PUTEVT__SUCCESS);
1598 } /* end of f_evt_put_event */
1599 
1600 /*1+ C Main ****************+******************************************/
1601 /*+ Module : f_evt_put_buffer */
1602 /*--------------------------------------------------------------------*/
1603 /*+ CALLING : f_evt_put_buffer(s_evt_channel *ps_chan, s_bufhe *ps_bufhe) */
1604 /*--------------------------------------------------------------------*/
1605 /* */
1606 /*+ PURPOSE : f_evt_put_buffer outputs buffer */
1607 /*+ ARGUMENTS : */
1608 /*+ ps_chan : Address of channel structure as returned from */
1609 /* f_evt_put_open. */
1610 /*+ ps_bufhe : Buffer. */
1611 /*+ Return type : int. */
1612 /*+ Status codes: */
1613 /*- PUTEVT__SUCCESS : success. */
1614 /*- PUTEVT__WRERR : read server or file error */
1615 /*+ Declaration : */
1616 /* INTS4 f_evt_put_buffer(s_evt_channel *, s_bufhe *); */
1617 /*+ FUNCTION : Writes buffer to file. */
1618 /*1- C Main ****************+******************************************/
1619 
1621 {
1622  INTS4 l_write_size; //, l_temp, l_free;
1623  INTS4 l_status;
1624  // CHARS* pc_addr;
1625 
1626  // DABC
1627  if (ps_chan->pLmd != NULL) {
1628  l_status = fLmdPutBuffer(ps_chan->pLmd, (sMbsHeader*)(ps_bufhe + 1), ps_bufhe->l_evt);
1629  return (l_status);
1630  }
1631  // -- DABC
1632  ps_chan->l_io_buf_posi = ps_chan->l_buf_size;
1633  ps_chan->l_io_buf_size = ps_chan->l_buf_size;
1634  l_write_size = write(ps_chan->l_channel_no, (CHARS*)ps_bufhe, ps_chan->l_buf_size);
1635  if (l_write_size == -1) {
1636  return (PUTEVT__WRERR);
1637  }
1638  if (l_write_size != ps_chan->l_buf_size) {
1639  return (PUTEVT__TOOBIG);
1640  }
1641  return PUTEVT__SUCCESS;
1642 } /* end of f_evt_put_buffer */
1643 
1644 /*1+ C Main ****************+******************************************/
1645 /*+ Module : f_evt_put_close */
1646 /*--------------------------------------------------------------------*/
1647 /*+ CALLING : f_evt_put_close(s_evt_channel *ps_chan) */
1648 /*--------------------------------------------------------------------*/
1649 /* */
1650 /*+ PURPOSE : f_evt_put_close closes specified channel. */
1651 /*+ ARGUMENTS : */
1652 /*+ channel : Channel number. */
1653 /*+ Return type : int. */
1654 /*+ Status codes: */
1655 /*- PUTEVT__SUCCESS : success. */
1656 /*- PUTEVT__FAILURE : failure. */
1657 /*- PUTEVT__CLOSE_ERR : close server or file error */
1658 /*+ Declaration : */
1659 /* INTS4 f_evt_put_close(s_evt_channel *); */
1660 /*+ FUNCTION : Closes the specified output channel after writing */
1661 /* last buffer. */
1662 /*1- C Main ****************+******************************************/
1663 
1665 {
1666  INTS4 l_write_size, l_temp, l_temp2;
1667  INTS4 l_status;
1668  // CHARS* pc_addr;
1669 
1670  // DABC
1671  if (ps_chan->pLmd != NULL) {
1672  l_status = fLmdPutClose(ps_chan->pLmd);
1673  return (l_status);
1674  }
1675  // -- DABC
1676  if (ps_chan->l_first_put == 1) {
1677  goto g_close;
1678  }
1679 
1680  /* if not the end of GOOSY buf, fill buffer header */
1681  if ((ps_chan->l_io_buf_posi % ps_chan->l_buf_size) != 0) {
1682  l_temp = (ps_chan->l_io_buf_posi / ps_chan->l_buf_size) * ps_chan->l_buf_size;
1683  /* l_temp points to start of this GOOSY buf in i/o buf area */
1684  memset(
1685  ps_chan->pc_io_buf + ps_chan->l_io_buf_posi, '\0', l_temp + ps_chan->l_buf_size - ps_chan->l_io_buf_posi);
1686  /* clear rest of this GOOSY buffer */
1687  ps_chan->ps_bufhe = (s_bufhe*)(ps_chan->pc_io_buf + l_temp);
1688 
1689  ps_chan->ps_bufhe->l_dlen = (ps_chan->l_buf_size - sizeof(s_bufhe)) / 2;
1690  ps_chan->ps_bufhe->h_begin = 0; /* evt has end, so not spanned to next buf */
1691  if (l_temp == 0) { /* so fisrt buf in stream */
1692  ps_chan->ps_bufhe->h_end = 0;
1693  } else
1694  ps_chan->ps_bufhe->h_end = ((s_bufhe*)((CHARS*)ps_chan->ps_bufhe - ps_chan->l_buf_size))->h_begin;
1695  ps_chan->ps_bufhe->i_used = (ps_chan->l_io_buf_posi % ps_chan->l_buf_size - sizeof(s_bufhe)) / 2;
1696  } /* end of process of infilished buffer header */
1697 
1698  /* if at the end of io buf, need not flush */
1699  if (ps_chan->l_io_buf_posi != ps_chan->l_io_buf_size) {
1700  if ((ps_chan->l_io_buf_posi % ps_chan->l_buf_size) != 0)
1701  l_write_size = (ps_chan->l_io_buf_posi / ps_chan->l_buf_size + 1) * ps_chan->l_buf_size;
1702  else {
1703  l_write_size = ps_chan->l_io_buf_posi;
1704  }
1705 
1706  for (l_temp = 0; l_temp < l_write_size; l_temp += ps_chan->l_buf_size) {
1707  /* why use this loop instead of write the whole io_buf out? because in
1708  VMS side, the record can only be l_buf_size big, not l_write_size big */
1709  l_temp2 = write(ps_chan->l_channel_no, ps_chan->pc_io_buf + l_temp, ps_chan->l_buf_size);
1710  if (l_temp2 == -1) {
1711  return (PUTEVT__WRERR);
1712  }
1713  if (l_temp2 != ps_chan->l_buf_size) {
1714  return (PUTEVT__TOOBIG);
1715  }
1716  }
1717  } /* end of flush */
1718 
1719 g_close:
1720  free(ps_chan->pc_io_buf);
1721  /* free io buffer Memory */
1722 
1723  l_status = close(ps_chan->l_channel_no);
1724  ps_chan->l_channel_no = -1;
1725  if (l_status == -1) {
1726  return (PUTEVT__CLOSE_ERR);
1727  } else {
1728  return PUTEVT__SUCCESS;
1729  }
1730 } /* end of f_evt_put_close */
1731 
1732 /*1+ C Main ****************+******************************************/
1733 /*+ Module : f_evt_error */
1734 /*--------------------------------------------------------------------*/
1735 /*+ CALLING : f_evt_error( long l_error , char &c_string[], long l_out ) */
1736 /*--------------------------------------------------------------------*/
1737 /* */
1738 /*+ PURPOSE : f_evt_error displays error messages. */
1739 /*+ ARGUMENTS : */
1740 /*+ l_error : The error id as returned from other calls */
1741 /*+ c_string : The string into f_evt_error() copies the message....*/
1742 /*+ l_out : specifies the output device for the error message. */
1743 /*- out = 1 : error message is copied to string. */
1744 /*- out = 0 : error message is printed on terminal. */
1745 /*+ Return type : int (longword). */
1746 /*+ Status codes: */
1747 /*- GETEVT__SUCCESS : success. */
1748 /*- GETEVT__FAILURE : failure */
1749 /*+ Declaration : */
1750 /* INTS4 f_evt_error( INTS4 , CHARS * , INTS4 ); */
1751 /*+ FUNCTION : f_evt_error displays the error message for the */
1752 /* error id ( l_error ). If out = 1 the error */
1753 /* message is copied into string, else */
1754 /* f_evt_error prints the message on terminal. */
1755 /*1- C Main ******************+****************************************/
1756 INTS4 f_evt_error(INTS4 l_error, CHARS* pc_dest, INTS4 l_out)
1757 {
1758  CHARS c_line[80];
1759 
1760  switch (l_error) {
1761  case GETEVT__NOFILE:
1762  sprintf(c_line, "-I-f_evt: no such input file");
1763  break;
1764  case GETEVT__NOTAGFILE:
1765  sprintf(c_line, "-I-f_evt: no such tag file");
1766  break;
1767  case GETEVT__NOTAG:
1768  sprintf(c_line, "-I-f_evt: no such event tag");
1769  break;
1770  case GETEVT__TIMEOUT:
1771  sprintf(c_line, "-I-f_evt: time out");
1772  break;
1773  case GETEVT__NOSERVER:
1774  sprintf(c_line, "-I-f_evt: no such server");
1775  break;
1776  case GETEVT__RDERR:
1777  sprintf(c_line, "-I-f_evt: read server error");
1778  break;
1779  case GETEVT__TAGRDERR:
1780  sprintf(c_line, "-I-f_evt: read tag file error");
1781  break;
1782  case GETEVT__TAGWRERR:
1783  sprintf(c_line, "-I-f_evt: write tag file error");
1784  break;
1785  case GETEVT__FRAGMENT:
1786  sprintf(c_line, "-I-f_evt: data format error");
1787  break;
1788  case GETEVT__NOMORE:
1789  sprintf(c_line, "-I-f_evt: no more event");
1790  break;
1791  case GETEVT__CLOSE_ERR:
1792  sprintf(c_line, "-I-f_evt: close server error");
1793  break;
1794  case GETEVT__FAILURE:
1795  sprintf(c_line, "-I-f_evt: failure");
1796  break;
1797  case GETEVT__NOCHANNEL:
1798  sprintf(c_line, "-I-f_evt: too many channels");
1799  break;
1800  case GETEVT__NOLMDFILE:
1801  sprintf(c_line, "-I-f_evt: input file is no LMD file");
1802  break;
1803  case GETEVT__SUCCESS:
1804  sprintf(c_line, "-I-f_evt: success");
1805  break;
1806  case PUTEVT__FILE_EXIST:
1807  sprintf(c_line, "-I-f_evt: output file already exist");
1808  break;
1809  case PUTEVT__WRERR:
1810  sprintf(c_line, "-I-f_evt: write file error");
1811  break;
1812  case PUTEVT__TOOBIG:
1813  sprintf(c_line, "-I-f_evt: output file too large");
1814  break;
1815  case PUTEVT__TOO_SMALLS:
1816  sprintf(c_line, "-I-f_evt: event can not fit in a stream");
1817  break;
1818  case PUTEVT__CLOSE_ERR:
1819  sprintf(c_line, "-I-f_evt: close output error");
1820  break;
1821  case PUTEVT__FAILURE:
1822  sprintf(c_line, "-I-f_evt: output failure");
1823  break;
1824  case PUTEVT__NOCHANNEL:
1825  sprintf(c_line, "-I-f_evt: too many channels");
1826  break;
1827  default:
1828  sprintf(c_line, "-I-f_evt: unknown message id %d", l_error);
1829  if (l_out == 0) {
1830  printf("%s\n", c_line);
1831  }
1832  if (l_out == 1) {
1833  strcpy(pc_dest, c_line);
1834  }
1835  return GETEVT__FAILURE;
1836  } /* end switch( l_error ) */
1837 
1838  if (l_out == 0) {
1839  printf("%s\n", c_line);
1840  }
1841  if (l_out == 1) {
1842  strcpy(pc_dest, c_line);
1843  }
1844 
1845  return GETEVT__SUCCESS;
1846 } /* end of f_evt_error */
1847 
1848 /*1+ C Main ****************+******************************************/
1849 /*+ Module : f_evt_get_buffer */
1850 /*--------------------------------------------------------------------*/
1851 /*+ CALLING : f_evt_get_buffer(s_evt_channel &s_chan, INTS4 *pl_buffer) */
1852 /*--------------------------------------------------------------------*/
1853 /* */
1854 /*+ PURPOSE : f_evt_get_buffer read one buffer from server into */
1855 /* user buffer. */
1856 /*+ ARGUMENTS : */
1857 /*+ s_chan : structure s_evt_channel. */
1858 /*+ pl_buffer : Address of user buffer */
1859 /*+ Return type : int. */
1860 /*+ Status codes: */
1861 /*- GETEVT__SUCCESS : success. */
1862 /*- GETEVT__FAILURE : failure */
1863 /*- GETEVT__RDERR : read server or file error */
1864 /*- GETEVT__NOMORE : No more events. */
1865 /*- GETEVT__TIMEOUT : when enabled by f_evt_timeout */
1866 /*+ Declaration : */
1867 /* INTS4 f_evt_get_buffer(s_evt_channel *, INTS4 *); */
1868 /*1- C Main ****************+******************************************/
1870 {
1871  INTS4 l_temp;
1872  CHARS* pc_temp;
1873  INTS4 l_status;
1874 
1875  pc_temp = (CHARS*)ps_chan->pc_io_buf;
1876  switch (ps_chan->l_server_type) {
1877  case GETEVT__FILE:
1878  l_temp = read(ps_chan->l_channel_no, ps_buffer, ps_chan->l_buf_size);
1879  if (l_temp == 0)
1880  /* if end of file, then exit */
1881  {
1882  ps_chan->l_io_buf_size = 0;
1883  return (GETEVT__NOMORE);
1884  } else if (l_temp == -1) {
1885  ps_chan->l_io_buf_size = 0;
1886  return (GETEVT__RDERR);
1887  }
1888  break;
1889  case GETEVT__RFIO:
1890  l_temp = RFIO_read(ps_chan->l_channel_no, (CHARS*)ps_buffer, ps_chan->l_buf_size);
1891  if (l_temp == 0) {
1892  ps_chan->l_io_buf_size = 0;
1893  return (GETEVT__NOMORE);
1894  } else if (l_temp == -1) {
1895  ps_chan->l_io_buf_size = 0;
1896  return (GETEVT__RDERR);
1897  }
1898  break;
1899  case GETEVT__STREAM:
1900  if (ps_chan->l_stream_bufs == 0)
1901  if (f_stc_write("GETEVT", 7, ps_chan->l_channel_no) != STC__SUCCESS) {
1902  return (GETEVT__FAILURE);
1903  }
1904 
1905  l_status = f_stc_read(ps_buffer, ps_chan->l_buf_size, ps_chan->l_channel_no, ps_chan->l_timeout);
1906  if (l_status == STC__TIMEOUT) {
1907  return (GETEVT__TIMEOUT);
1908  }
1909  if (l_status != STC__SUCCESS) {
1910  return (GETEVT__RDERR);
1911  }
1912  ps_chan->l_stream_bufs++;
1913  if (ps_chan->l_stream_bufs == ps_chan->l_bufs_in_stream) {
1914  ps_chan->l_stream_bufs = 0;
1915  }
1916  break;
1917  case GETEVT__TRANS:
1918  l_status = f_stc_read(ps_buffer, ps_chan->l_buf_size, ps_chan->l_channel_no, ps_chan->l_timeout);
1919  if (l_status == STC__TIMEOUT) {
1920  return (GETEVT__TIMEOUT);
1921  }
1922  if (l_status != STC__SUCCESS) {
1923  return (GETEVT__RDERR);
1924  }
1925  break;
1926  case GETEVT__REVSERV:
1927  case GETEVT__EVENT:
1928  default:
1929  return (GETEVT__FAILURE);
1930  } /* end of switch */
1931 
1932  /* swap */
1933  if (((s_bufhe*)(ps_buffer))->l_free[0] != 1) {
1934  f_evt_swap((CHARS*)ps_buffer, ps_chan->l_buf_size);
1935  }
1936 
1937  return (GETEVT__SUCCESS);
1938 } /* end of f_evt_get_buffer */
1939 /*1+ C Main ****************+******************************************/
1940 /*+ Module : f_evt_skip_buffer */
1941 /*--------------------------------------------------------------------*/
1942 /*+ CALLING : f_evt_skip_buffer(s_evt_channel &s_chan, INTS4 l_buffer) */
1943 /*--------------------------------------------------------------------*/
1944 /* */
1945 /*+ PURPOSE : Skip buffers in file. */
1946 /*+ ARGUMENTS : */
1947 /*+ s_chan : structure s_evt_channel. */
1948 /*+ l_buffer : buffers to skip */
1949 /*+ Return type : int. */
1950 /*+ Status codes: */
1951 /*- GETEVT__SUCCESS : success. */
1952 /*- GETEVT__FAILURE : failure */
1953 /*- GETEVT__RDERR : read server or file error */
1954 /*- GETEVT__NOMORE : No more events. */
1955 /*- GETEVT__TIMEOUT : when enabled by f_evt_timeout */
1956 /*+ Declaration : */
1957 /* INTS4 f_evt_skip_buffer(s_evt_channel *, INTS4); */
1958 /*1- C Main ****************+******************************************/
1960 {
1961  INTS4 l_temp;
1962  CHARS* pc_temp;
1963  // INTS4 l_status,ii;
1964 
1965  pc_temp = (CHARS*)ps_chan->pc_io_buf;
1966  switch (ps_chan->l_server_type) {
1967  case GETEVT__FILE:
1968  l_temp = lseek(ps_chan->l_channel_no, l_buffer * ps_chan->l_buf_size, SEEK_CUR);
1969  if (l_temp == -1)
1970  /* if end of file, then exit */
1971  {
1972  return (GETEVT__NOMORE);
1973  }
1974  break;
1975  case GETEVT__RFIO:
1976  l_temp = RFIO_lseek(ps_chan->l_channel_no, l_buffer * ps_chan->l_buf_size, SEEK_CUR);
1977  if (l_temp == -1) {
1978  return (GETEVT__NOMORE);
1979  }
1980  break;
1981  case GETEVT__STREAM:
1982  case GETEVT__TRANS:
1983  case GETEVT__REVSERV:
1984  case GETEVT__EVENT:
1985  default:
1986  return (GETEVT__FAILURE);
1987  } /* end of switch */
1988 
1989  ps_chan->l_first_get = 1; /* so we will first call f_getevt_get */
1990  return (GETEVT__SUCCESS);
1991 } /* end of f_evt_skip_buffer */
1992 
1993 /*1+ C Main ****************+******************************************/
1994 /*+ Module : f_evt_timeout */
1995 /*--------------------------------------------------------------------*/
1996 /*+ CALLING : f_evt_timeout(s_evt_channel *ps_chan, seconds) */
1997 /*--------------------------------------------------------------------*/
1998 /* */
1999 /*+ PURPOSE : Set a timeout for TCP read operations */
2000 /*+ ARGUMENTS : */
2001 /*+ ps_chan : Address of channel structure. */
2002 /*+ seconds : -1: wait (default) */
2003 /* >0: if after n seconds no data arrived, */
2004 /* read functions return GETEVT__TIMEOUT. */
2005 /*+ Return type : INTS4 */
2006 /*+ Declaration : */
2007 /* INTS4 f_evt_timeout(s_evt_channel *, INTS4 ); */
2008 /*+ FUNCTION : Set a timeout for TCP read operations. */
2009 /* The calls of f_evt_get_event, f_evt_get_buffer */
2010 /* will return GETEVT__TIMEOUT when seconds have been */
2011 /* set to positive value. */
2012 /*1- C Main ****************+******************************************/
2014 {
2015  ps_chan->l_timeout = l_sec;
2016  return (GETEVT__SUCCESS);
2017 } /* end of f_evt_timeout */
2018 
2019 /*1- C Main ****************+******************************************/
2020 /*+ Module : f_evt_swap */
2021 /*--------------------------------------------------------------------*/
2022 /*+ CALLING : sts = f_evt_swap(char &c_source[], long l_length) */
2023 /*--------------------------------------------------------------------*/
2024 /* */
2025 /*+ PURPOSE : Long word byte swap. */
2026 /*+ ARGUMENTS : */
2027 /*+ c_source : source data array. */
2028 /*+ l_length : length (in bytes) */
2029 /*+ FUNCTION : Long word byte swap. Works on the source field. */
2030 /*+ Return type : int */
2031 /*+ Status codes: bit 0: success */
2032 /*1- C Procedure ***********+******************************************/
2033 INTS4 f_evt_swap(CHARS* pc_source, INTS4 l_length)
2034 {
2035  CHARS *p_s, *p_d;
2036  INTS4 l_save;
2037  CHARS ch_temp;
2038 
2039  if (l_length % 4 == 2) {
2040  ch_temp = *(pc_source + l_length - 2);
2041  *(pc_source + l_length - 2) = *(pc_source + l_length - 1);
2042  *(pc_source + l_length - 1) = ch_temp;
2043  }
2044  l_length = (l_length / 4) * 4; /* aligned */
2045  for (p_d = pc_source, p_s = (CHARS*)&l_save; p_d < pc_source + l_length;) {
2046  l_save = *((INTS4*)p_d);
2047  p_s += 4; /* increment source */
2048  *(p_d++) = *(--p_s);
2049  *(p_d++) = *(--p_s);
2050  *(p_d++) = *(--p_s);
2051  *(p_d++) = *(--p_s);
2052  }
2053  return (0);
2054 } /* end of f_evt_swap */
2055 /*1- C Main ****************+******************************************/
2056 /*+ Module : f_evt_swap */
2057 /*--------------------------------------------------------------------*/
2058 /*+ CALLING : sts = f_evt_swap_filhe(s_filhe *) */
2059 /*--------------------------------------------------------------------*/
2060 /* */
2061 /*+ PURPOSE : Swap file header. */
2062 /*+ ARGUMENTS : */
2063 /*+ c_source : source data array. */
2064 /*+ l_length : length (in bytes) */
2065 /*+ FUNCTION : Long word byte swap. Works on the source field. */
2066 /*+ Return type : int */
2067 /*+ Status codes: bit 0: success */
2068 /*1- C Procedure ***********+******************************************/
2070 {
2071  // CHARS* p_s, * p_d;
2072  INTS4 ii;
2073  INTS2* pi;
2074  s_filhe* ps_filhe;
2075 
2076  f_evt_swap((CHARS*)ps_bufhe, 48);
2077  if (ps_bufhe->i_type == 2000) {
2078  ps_filhe = (s_filhe*)ps_bufhe;
2079  f_evt_swap((CHARS*)&ps_filhe->filhe_lines, 4); /* number of comment lines ps_filhe->filhe_lines*/
2080  ps_filhe->filhe_label_l = ps_filhe->filhe_label_l >> 8;
2081  ps_filhe->filhe_file_l = ps_filhe->filhe_file_l >> 8;
2082  ps_filhe->filhe_user_l = ps_filhe->filhe_user_l >> 8;
2083  ps_filhe->filhe_run_l = ps_filhe->filhe_run_l >> 8;
2084  ps_filhe->filhe_exp_l = ps_filhe->filhe_exp_l >> 8;
2085  pi = (INTS2*)&ps_filhe->s_strings;
2086  for (ii = 0; ii < ps_filhe->filhe_lines; ii++) {
2087  *pi = *pi >> 8;
2088  pi += 40;
2089  }
2090  }
2091 
2092  return (0);
2093 } /* end of f_evt_swap */
2094 
2095 /*1- C Main ****************+******************************************/
2096 /*+ Module : f_evt_get_buffer_ptr */
2097 /*--------------------------------------------------------------------*/
2098 /*+ CALLING : f_evt_get_buffer_ptr(s_evt_channel &s_chan) */
2099 /*--------------------------------------------------------------------*/
2100 /* */
2101 /*+ PURPOSE : f_evt_get_buffer_ptr returns buffer pointer */
2102 /*+ ARGUMENTS : */
2103 /*+ s_chan : structure s_evt_channel. */
2104 /*+ Return type : int. */
2105 /*+ Status codes: */
2106 /*1- C Main ****************+******************************************/
2107 CHARS* f_evt_get_buffer_ptr(s_evt_channel* ps_chan) { return (ps_chan->pc_io_buf); }
2108 /*1- C Main ****************+******************************************/
2109 /*+ Module : f_evt_get_newbuf */
2110 /*--------------------------------------------------------------------*/
2111 /*+ CALLING : f_evt_get_newbuf(s_evt_channel &s_chan) */
2112 /*--------------------------------------------------------------------*/
2113 /* */
2114 /*+ PURPOSE : f_evt_get_newbuf read a buffer from server */
2115 /*+ ARGUMENTS : */
2116 /*+ s_chan : structure s_evt_channel. */
2117 /*+ Return type : int. */
2118 /*+ Status codes: */
2119 /*- GETEVT__SUCCESS : success. */
2120 /*- GETEVT__FAILURE : failure */
2121 /*- GETEVT__RDERR : read server or file error */
2122 /*- GETEVT__NOMORE : No more events. */
2123 /*- GETEVT__TIMEOUT : when enabled by f_evt_timeout */
2124 /*1- C Main ****************+******************************************/
2126 {
2127  INTS4 l_temp;
2128  CHARS* pc_temp;
2129  INTS4 l_status;
2130 
2131  pc_temp = (CHARS*)ps_chan->pc_io_buf;
2132  switch (ps_chan->l_server_type) {
2133  case GETEVT__FILE:
2134  while (1) {
2135  l_temp = read(ps_chan->l_channel_no, pc_temp, ps_chan->l_io_buf_size);
2136  if (l_temp == 0) {
2137  return (GETEVT__NOMORE);
2138  }
2139  if (l_temp == -1) {
2140  return (GETEVT__RDERR);
2141  }
2142  if (l_temp < ps_chan->l_io_buf_size) {
2143  return (GETEVT__RDERR);
2144  }
2145  break; /* skip out while(1) */
2146  } /* end of while(1) */
2147  break;
2148  case GETEVT__STREAM:
2149  if (f_stc_write("GETEVT", 7, ps_chan->l_channel_no) != STC__SUCCESS) {
2150  return (GETEVT__FAILURE);
2151  }
2152 
2153  for (l_temp = 0; l_temp < ps_chan->l_bufs_in_stream; l_temp++) {
2154  l_status = f_stc_read(pc_temp, ps_chan->l_buf_size, ps_chan->l_channel_no, ps_chan->l_timeout);
2155  if (l_status == STC__TIMEOUT) {
2156  return (GETEVT__TIMEOUT);
2157  }
2158  if (l_status != STC__SUCCESS) {
2159  return (GETEVT__RDERR);
2160  }
2161  pc_temp += ps_chan->l_buf_size;
2162  }
2163  l_temp = ((s_bufhe*)(ps_chan->pc_io_buf))->l_evt;
2164  if (((s_bufhe*)(ps_chan->pc_io_buf))->l_free[0] != 1) { // swap
2165  f_evt_swap((CHARS*)&l_temp, 4);
2166  }
2167  if (l_temp < 0) { // server will shutdown
2168  printf("**** I-f_evt: Stream server request for disconnect!\n");
2169  f_evt_get_close(ps_chan);
2170  return (GETEVT__RDERR);
2171  }
2172  /* if first buffer is empty, all are empty */
2173  if (((s_bufhe*)(ps_chan->pc_io_buf))->l_evt == 0) {
2174  return (GETEVT__TIMEOUT);
2175  }
2176  break;
2177  case GETEVT__TRANS:
2178  l_status = f_stc_read(pc_temp, ps_chan->l_buf_size, ps_chan->l_channel_no, ps_chan->l_timeout);
2179  l_temp = ((s_bufhe*)(ps_chan->pc_io_buf))->l_evt;
2180  if (((s_bufhe*)(ps_chan->pc_io_buf))->l_free[0] != 1) { // swap
2181  f_evt_swap((CHARS*)&l_temp, 4);
2182  }
2183  if (l_temp < 0) { // server will shutdown
2184  printf("**** I-f_evt: Transport server request for disconnect!\n");
2185  f_evt_get_close(ps_chan);
2186  return (GETEVT__RDERR);
2187  }
2188  if (l_status == STC__TIMEOUT) {
2189  return (GETEVT__TIMEOUT);
2190  }
2191  if (l_status != STC__SUCCESS) {
2192  return (GETEVT__RDERR);
2193  }
2194  break;
2195  case GETEVT__RFIO:
2196  l_temp = RFIO_read(ps_chan->l_channel_no, pc_temp, ps_chan->l_io_buf_size);
2197  if (l_temp == 0) {
2198  return (GETEVT__NOMORE);
2199  }
2200  if (l_temp == -1) {
2201  return (GETEVT__RDERR);
2202  }
2203  if (l_temp < ps_chan->l_io_buf_size) {
2204  return (GETEVT__RDERR);
2205  }
2206  break;
2207  case GETEVT__REVSERV:
2208  case GETEVT__EVENT:
2209  default:
2210  return (GETEVT__FAILURE);
2211  } /* end of switch */
2212 
2213  if (((s_bufhe*)(ps_chan->pc_io_buf))->l_free[0] != 1) { // swap
2214  f_evt_swap(ps_chan->pc_io_buf, ps_chan->l_io_buf_size);
2215  }
2216 
2217  return (GETEVT__SUCCESS);
2218 } /* end of f_evt_get_newbuf */
2219 
2220 /*1- C Main ****************+******************************************/
2221 /*+ Module : f_evt_check_buf */
2222 /*--------------------------------------------------------------------*/
2223 /*+ CALLING : f_evt_check_buf(CHARS *pc_head, INTS4 *pl_goosybuf, */
2224 /* INTS4 *pl_swap, INTS4 *pl_filehead) */
2225 /*--------------------------------------------------------------------*/
2226 /* */
2227 /*+ PURPOSE : check a buffer is a file header or GOOSY buffer, */
2228 /* and whether it's swaped. */
2229 /*+ ARGUMENTS : */
2230 /*+ pc_head : pointer to the buffer to be checked. */
2231 /*+ pl_goosybuf : 1 is goosy buffer, 0 is header */
2232 /*+ pl_swap : 1 means different endian, 0 means same endian. */
2233 /*+ pl_filehead : 1 means first buffer is header */
2234 /*+ Return type : void */
2235 /*+ Status codes: */
2236 /*+ FUNCTION : check a buffer is a file header or GOOSY buffer, */
2237 /* and whether it's swaped. */
2238 /*1- C Main ****************+******************************************/
2239 INTS4 f_evt_check_buf(CHARS* pc_head, INTS4* pl_size, INTS4* pl_is_goosybuf, INTS4* pl_swap, INTS4* pl_filehead)
2240 {
2241  INTU4 l_size;
2242 
2243  *pl_is_goosybuf = 0;
2244  *pl_filehead = 0;
2245  *pl_size = 0;
2246  *pl_swap = 0;
2247  /* first check if it's file header */
2248  if ((((s_filhe*)(pc_head))->filhe_subtype == 1) && (((s_filhe*)(pc_head))->filhe_type == 2000)) {
2249  *pl_swap = 0;
2250  *pl_is_goosybuf = 0;
2251  *pl_filehead = 1;
2252  l_size = ((s_bufhe*)(pc_head))->l_dlen * 2;
2253  if (l_size % 512 > 0) {
2254  l_size += sizeof(s_bufhe);
2255  }
2256  if ((l_size >> 24) > 0) {
2257  *pl_size = 0;
2258  } else {
2259  *pl_size = l_size;
2260  }
2261  return (PUTEVT__SUCCESS);
2262  }
2263  f_evt_swap(pc_head, sizeof(s_filhe));
2264  if ((((s_filhe*)(pc_head))->filhe_subtype == 1) && (((s_filhe*)(pc_head))->filhe_type == 2000)) {
2265  *pl_swap = 1;
2266  *pl_is_goosybuf = 0;
2267  *pl_filehead = 1;
2268  l_size = ((s_bufhe*)(pc_head))->l_dlen * 2;
2269  if (l_size % 512 > 0) {
2270  l_size += sizeof(s_bufhe);
2271  }
2272  if ((l_size >> 24) > 0) {
2273  *pl_size = 0;
2274  } else {
2275  *pl_size = l_size;
2276  }
2277  return (PUTEVT__SUCCESS);
2278  }
2279 
2280  /* if not file header, check if it's goosy buffer header */
2281  l_size = ((s_bufhe*)(pc_head))->l_dlen * 2;
2282  if (l_size % 512 > 0) {
2283  l_size += sizeof(s_bufhe);
2284  }
2285  if ((((l_size >> 24) == 0) && (l_size > 0)) && (((s_bufhe*)(pc_head))->h_begin < 2)
2286  && (((s_bufhe*)(pc_head))->h_begin >= 0) && (((s_bufhe*)(pc_head))->h_end < 2)
2287  && (((s_bufhe*)(pc_head))->h_end >= 0) &&
2288  //(((s_bufhe *)(pc_head))->i_used<=(MAX_BUF_LGTH-sizeof(s_bufhe))/2)&&
2289  ((((s_bufhe*)(pc_head))->i_used > 0) || (((s_bufhe*)(pc_head))->l_free[2] > 0)) &&
2290  //(((s_bufhe *)(pc_head))->i_used>0)&&
2291  ((((s_bufhe*)(pc_head))->l_free[0] == 1) || (((s_bufhe*)(pc_head))->l_free[0] == 0) ||
2292  /* above, because some old lsm file forgot to set this bit, so it's zero */
2293  (((s_bufhe*)(pc_head))->l_free[0] == 256 * 256 * 256))) {
2294  *pl_swap = 1; /* !!!, because already swaped once */
2295  *pl_is_goosybuf = 1;
2296  *pl_size = l_size;
2297  return (PUTEVT__SUCCESS);
2298  }
2299  f_evt_swap(pc_head, sizeof(s_filhe));
2300  l_size = ((s_bufhe*)(pc_head))->l_dlen * 2 + sizeof(s_bufhe);
2301  if ((((l_size >> 24) == 0) && (l_size > 0)) && (((s_bufhe*)(pc_head))->l_dlen > 0)
2302  && (((s_bufhe*)(pc_head))->h_begin < 2) && (((s_bufhe*)(pc_head))->h_begin >= 0)
2303  && (((s_bufhe*)(pc_head))->h_end < 2) && (((s_bufhe*)(pc_head))->h_end >= 0) &&
2304  //(((s_bufhe *)(pc_head))->i_used<=(MAX_BUF_LGTH-sizeof(s_bufhe))/2)&&
2305  ((((s_bufhe*)(pc_head))->i_used > 0) || (((s_bufhe*)(pc_head))->l_free[2] > 0)) &&
2306  //(((s_bufhe *)(pc_head))->i_used>0)&&
2307  ((((s_bufhe*)(pc_head))->l_free[0] == 1) || (((s_bufhe*)(pc_head))->l_free[0] == 0) ||
2308  /* above, because some old lsm file forgot to set this bit, so it's zero */
2309  (((s_bufhe*)(pc_head))->l_free[0] == 256 * 256 * 256))) {
2310  *pl_swap = 0; /* !!!, because already swaped 2 times */
2311  *pl_is_goosybuf = 1;
2312  *pl_size = l_size;
2313  return (PUTEVT__SUCCESS);
2314  }
2315  return (PUTEVT__SUCCESS);
2316 } /* end of f_evt_check_buf */
2317 
2318 /*1- C Main ****************+******************************************/
2319 /*+ Module : f_evt_ini_bufhe */
2320 /*--------------------------------------------------------------------*/
2321 /*+ CALLING : f_evt_ini_bufhe(s_evt_channel *ps_chan) */
2322 /*--------------------------------------------------------------------*/
2323 /* */
2324 /*+ PURPOSE : pre-fill each GOOSY buffer header in a stream */
2325 /*+ ARGUMENTS : */
2326 /*+ ps_chan : Address of channel structure. */
2327 /*+ Return type : void . */
2328 /*1- C Main ****************+******************************************/
2330 {
2331  INTS4 l_temp;
2332  struct timeb s_timeb;
2333 
2334  /* because "timeb" is not "typedef", so we must use "struct" */
2335 
2336  for (l_temp = 0; l_temp < ps_chan->l_io_buf_size; l_temp += ps_chan->l_buf_size) {
2337  ps_chan->ps_bufhe = (s_bufhe*)(ps_chan->pc_io_buf + l_temp);
2338  ps_chan->ps_bufhe->l_dlen = (ps_chan->l_buf_size - sizeof(s_bufhe)) / 2;
2339  ps_chan->ps_bufhe->i_subtype = ps_chan->l_buf_subtype;
2340  ps_chan->ps_bufhe->i_type = ps_chan->l_buf_type;
2341  ps_chan->ps_bufhe->h_begin = 0;
2342  ps_chan->ps_bufhe->h_end = 0;
2343  ps_chan->ps_bufhe->i_used = 0;
2344  ps_chan->ps_bufhe->l_buf = ps_chan->l_buf_no;
2345  ps_chan->l_buf_no++;
2346  ps_chan->ps_bufhe->l_evt = 0;
2347  ps_chan->ps_bufhe->l_current_i = 0;
2348  ftime(&s_timeb);
2349  ps_chan->ps_bufhe->l_time[0] = (INTS4)s_timeb.time;
2350  ps_chan->ps_bufhe->l_time[1] = (INTS4)s_timeb.millitm;
2351  ps_chan->ps_bufhe->l_free[0] = 1; /* for swap flag */
2352  ps_chan->ps_bufhe->l_free[1] = 0;
2353  ps_chan->ps_bufhe->l_free[2] = 0;
2354  ps_chan->ps_bufhe->l_free[3] = 0;
2355  }
2356  return (PUTEVT__SUCCESS);
2357 } /* end of f_evt_ini_bufhe */
2358 
2359 /*1+ C Main ****************+******************************************/
2360 /*+ Module : f_evt_cre_tagfile */
2361 /*--------------------------------------------------------------------*/
2362 /*+ CALLING : f_evt_cre_tagfile(lmdfile,tagfile,filter) */
2363 /*--------------------------------------------------------------------*/
2364 /* */
2365 /*+ PURPOSE : Create a tag file from lmd file */
2366 /*+ ARGUMENTS : */
2367 /*+ lmdfile : LMD file name */
2368 /*+ tagfile : tag file name */
2369 /*+ filter : optional function for filter */
2370 /*+ Return type : INTS4 . */
2371 /*+ Status codes: */
2372 /*- GETEVT__SUCCESS : success. */
2373 /*- GETEVT__NOFILE : file does not exist. */
2374 /*- GETEVT__TAGWRERR : tag file write error. */
2375 /*- GETEVT__RDERR : lmd read error. */
2376 /*+ Declaration : */
2377 /* INTS4 f_evt_cre_tagfile(CHARS *,CHARS *, INTS4 (*)()); */
2378 /*+ FUNCTION : Create a tag file from lmd file */
2379 /*+ filter : The filter function is called at the beginning */
2380 /* with a NULL as argument, then for each event */
2381 /* with the event pointer. Returning 0 skips the event, */
2382 /* 1 takes the event into the tag file. */
2383 /* Different tag files can be created from one lmd file. */
2384 /*1- C Main ****************+******************************************/
2386 {
2387  /* take it */
2388  return (1);
2389  // if(ps_ve10_1 != NULL)
2390  //{
2391  // printf("Filter %9d Type/Subtype %5d %5d Length %5d[w] Trigger %2d\n",
2392  // ps_ve10_1->l_count,
2393  // ps_ve10_1->i_type,
2394  // ps_ve10_1->i_subtype,
2395  // ps_ve10_1->l_dlen,
2396  // ps_ve10_1->i_trigger);
2397  // if(ps_ve10_1->l_count%2) return(1); /* take it */
2398  // else return(0); /* skip it */
2399  //}
2400  // else printf("Initialized filter function\n");
2401  // return(0);
2402 }
2403 INTS4 f_evt_cre_tagfile(CHARS* pc_lmd, CHARS* pc_tag, INTS4 (*e_filter)())
2404 {
2405  INTS4 ii, l_take_it, l_temp, l_chan, l_out, l_file_pos = 0, l_bufnr = 0, l_events = 0;
2406  INTS4 l_firste, *pl, l_len, l_last = -1, l_lin = 0, l_fragsize; //, la_head[2];
2407  INTS4 l_swap, l_swap_head, l_is_goosybuf, l_filehead = 0, l_size, l_size_head, l_dummy, l_evsize,
2408  l_evt_buf_size = 0;
2409  // INTS2* pi;
2410  INTU4 *ps, *pd;
2411  CHARS /*c_lmd[128], c_tag[128], *pc_temp,*/* pc_evt_buf = NULL;
2412  s_ve10_1* ps_ve10_1;
2413  s_bufhe* ps_bufhe;
2414  s_taghe s_taghe;
2415  memset(&s_taghe, 0, sizeof(s_taghe));
2416  s_tag s_tag;
2417  int val;
2418 
2419  ps_bufhe = (s_bufhe*)c_temp;
2420  printf("LMD file %s, TAG file %s\n", pc_lmd, pc_tag);
2421  /* get file attributes */
2422  if ((l_chan = open(pc_lmd, GET__OPEN_FLAG)) == -1) {
2423  return (GETEVT__NOFILE);
2424  }
2425  /* read first 512 bytes */
2426  if (read(l_chan, c_temp, MIN_BUF_LGTH) != MIN_BUF_LGTH) {
2427  close(l_chan);
2428  return (GETEVT__RDERR);
2429  }
2430  /* check for file header, return size and swap */
2431  f_evt_check_buf(c_temp, &l_size_head, &l_is_goosybuf, &l_swap_head, &l_filehead);
2432  if (((l_is_goosybuf == 0) & (l_filehead == 0)) | (l_size_head == 0)) {
2433  printf("LMD format error: swap=%d, header=%d, isLMD=%d, size=%d\n",
2434  l_swap_head,
2435  l_filehead,
2436  l_is_goosybuf,
2437  l_size_head);
2438  close(l_chan);
2439  return (GETEVT__NOLMDFILE);
2440  }
2441  /* read file header and first buffer and check for goosy header */
2442  if (l_filehead == 1) {
2443  val = lseek(l_chan, 0, SEEK_SET); /* rewind file */
2444  if (-1 == val) {
2445  close(l_chan);
2446  return (GETEVT__FAILURE);
2447  }
2448  if (read(l_chan, c_temp, l_size_head) != l_size_head) {
2449  close(l_chan);
2450  return (GETEVT__RDERR);
2451  }
2452  if (read(l_chan, c_temp, MIN_BUF_LGTH) != MIN_BUF_LGTH) {
2453  close(l_chan);
2454  return (GETEVT__RDERR);
2455  }
2456  f_evt_check_buf(c_temp, &l_size, &l_is_goosybuf, &l_swap, &l_dummy);
2457  if ((l_is_goosybuf == 0) | (l_size != l_size_head) | (l_swap != l_swap_head)) {
2458  printf("LMD format error: swap=%d, isLMD=%d, size=%d\n", l_swap, l_is_goosybuf, l_size);
2459  close(l_chan);
2460  return (GETEVT__NOLMDFILE);
2461  }
2462  } /* check buffer behind header */
2463  else {
2464  l_size = 0;
2465  l_swap = 0;
2466  }
2467 
2468  val = lseek(l_chan, 0, SEEK_SET); /* rewind file */
2469  if (-1 == val) {
2470  close(l_chan);
2471  return (GETEVT__FAILURE);
2472  }
2473 
2474  printf("Buffer swap %d, File header %d, LMD buffer %d, size %d[b]\n", l_swap, l_filehead, l_is_goosybuf, l_size);
2475 
2476  /* found file header, skip */
2477  if (l_filehead == 1) {
2478  if (read(l_chan, c_temp, l_size) != l_size) {
2479  close(l_chan);
2480  return (GETEVT__RDERR);
2481  }
2482  l_file_pos += l_size;
2483  l_bufnr++;
2484  }
2485 
2486  /* Open and create tag file */
2487  if ((l_out = open(pc_tag, PUT__CRT_FLAG, DEF_FILE_ACCE)) == -1) {
2488  close(l_chan);
2489  return (GETEVT__NOFILE);
2490  }
2491  ssize_t wcount = write(l_out, (CHARS*)&s_taghe, sizeof(s_taghe));
2492  if (wcount != sizeof(s_taghe)) {
2493  close(l_chan);
2494  close(l_out);
2495  return (GETEVT__TAGWRERR);
2496  }
2497 
2498  /* Initialize filter function */
2499  // if(e_filter != NULL) { ii=(*e_filter)(NULL); }
2500 
2501  while (read(l_chan, c_temp, l_size) == l_size) {
2502  l_file_pos = l_bufnr * l_size;
2503  if (l_swap) {
2504  f_evt_swap(c_temp, l_size);
2505  }
2506  if ((ps_bufhe->h_end) & (ps_bufhe->h_begin) & (ps_bufhe->l_evt == 1)) {
2507  /* only fragment, next buffer */
2508  /* printf("Event fragment skipped Buffer %6d\n",l_bufnr);*/
2509  l_bufnr++;
2510  continue;
2511  }
2512  ps_ve10_1 = (s_ve10_1*)(ps_bufhe + 1);
2513  pl = (INTS4*)ps_ve10_1;
2514  l_file_pos += sizeof(s_bufhe);
2515  /* skip fragment at begin */
2516  if (ps_bufhe->h_end) {
2517  l_len = (*pl >> 1) + 2;
2518  l_file_pos += (l_len << 2); /* bytes */
2519  ps_ve10_1 = (s_ve10_1*)(pl + l_len);
2520  pl = (INTS4*)ps_ve10_1;
2521  ps_bufhe->l_evt--;
2522  /* printf("Event fragment end skipped Buffer %6d\n",l_bufnr);*/
2523  }
2524  for (ii = 1; ii < ps_bufhe->l_evt; ii++) { /* except last element */
2525  /*printf("Event %10d pos %10d Buffer %6d\n",ps_ve10_1->l_count, l_file_pos,l_bufnr);*/
2526  if (e_filter != NULL) {
2527  l_take_it = (*e_filter)(ps_ve10_1);
2528  } else {
2529  l_take_it = 1;
2530  }
2531  l_len = (*pl >> 1) + 2;
2532  s_tag.l_event = ps_ve10_1->l_count;
2533  s_tag.l_offset = l_file_pos;
2534  if (l_take_it) {
2535  l_events++;
2536  if (ps_ve10_1->l_count != (l_last + 1)) {
2537  l_lin++;
2538  if (l_lin == 1) {
2539  l_firste = ps_ve10_1->l_count;
2540  }
2541  /* printf("New event number offset %d, index %d\n",ps_ve10_1->l_count,l_events);*/
2542  }
2543  l_last = ps_ve10_1->l_count;
2544  if (write(l_out, (CHARS*)&s_tag, sizeof(s_tag)) != sizeof(s_tag)) {
2545  close(l_chan);
2546  close(l_out);
2547  return (GETEVT__TAGWRERR);
2548  }
2549  }
2550  l_file_pos += (l_len << 2); /* bytes */
2551  ps_ve10_1 = (s_ve10_1*)(pl + l_len);
2552  pl = (INTS4*)ps_ve10_1;
2553  }
2554 
2555  if (ps_bufhe->l_evt > 0) {
2556  if (ps_ve10_1->l_dlen < 4)
2557  printf("Buffer %d Event fragment %10d dlen %d ts %d %d trigger %d\n",
2558  l_bufnr,
2559  ps_ve10_1->l_count,
2560  ps_ve10_1->l_dlen,
2561  ps_ve10_1->i_type,
2562  ps_ve10_1->i_subtype,
2563  ps_ve10_1->i_trigger);
2564  s_tag.l_event = ps_ve10_1->l_count;
2565  if (ps_bufhe->h_begin) {
2566  /* copy event to event buffer for filter function */
2567  if (e_filter != NULL) {
2568  /* is event buffer big enough? */
2569  l_evsize = ps_bufhe->l_free[1] + 4; /* total words */
2570  if (l_evt_buf_size < l_evsize * 2) {
2571  if (pc_evt_buf != NULL) {
2572  free(pc_evt_buf);
2573  }
2574  l_evt_buf_size = l_evsize * 2;
2575  pc_evt_buf = (CHARS*)malloc(l_evt_buf_size);
2576  }
2577  if (NULL == pc_evt_buf) {
2578  l_evt_buf_size = l_evsize * 2;
2579  pc_evt_buf = (CHARS*)malloc(l_evt_buf_size);
2580  }
2581  /* copy event fragment to buffer */
2582  ps = (INTU4*)ps_ve10_1;
2583  pd = (INTU4*)pc_evt_buf;
2584  l_fragsize = ps_ve10_1->l_dlen + 4; /* fragment size + header [w] */
2585  for (ii = 0; ii < l_fragsize / 2; ii++) {
2586  *pd++ = *ps++;
2587  }
2588  /* loop over fragments */
2589  while (l_fragsize < l_evsize) {
2590  if (read(l_chan, c_temp, l_size) != l_size) {
2591  break;
2592  }
2593  l_bufnr++;
2594  if (l_swap) {
2595  f_evt_swap(c_temp, l_size);
2596  }
2597  ps = (INTU4*)(ps_bufhe + 1); /* behind buffer header, fragment header */
2598  l_temp = (INTS4)*ps; /* fragment size [w] */
2599  ps += 2; /* to data behind fragment header */
2600  for (ii = 0; ii < l_temp / 2; ii++) {
2601  *pd++ = *ps++;
2602  }
2603  l_fragsize += l_temp;
2604  }
2605  if (l_fragsize < l_evsize) {
2606  break;
2607  }
2608  ps_ve10_1 = (s_ve10_1*)pc_evt_buf;
2609  ps_ve10_1->l_dlen = l_evsize - 4;
2610  /* rewind last buffer, will be read again by while loop */
2611  l_bufnr--;
2612  val = lseek(l_chan, -l_size, SEEK_CUR);
2613  if (-1 == val) {
2614  free(pc_evt_buf);
2615  ps_ve10_1 = NULL;
2616  pd = NULL;
2617  close(l_chan);
2618  close(l_out);
2619  return (GETEVT__FAILURE);
2620  }
2621  }
2622  s_tag.l_offset = -l_file_pos;
2623  /* printf("Event %10d pos %10d Buffer -%6d\n",ps_ve10_1->l_count, l_file_pos,l_bufnr);*/
2624  } else {
2625  s_tag.l_offset = l_file_pos;
2626  /* printf("Event %10d pos %10d Buffer %6d\n",ps_ve10_1->l_count, l_file_pos,l_bufnr);*/
2627  }
2628  if (e_filter != NULL) {
2629  l_take_it = (*e_filter)(ps_ve10_1);
2630  } else {
2631  l_take_it = 1;
2632  }
2633  if (l_take_it) {
2634  l_events++;
2635  if (ps_ve10_1->l_count != (l_last + 1)) {
2636  l_lin++;
2637  if (l_lin == 1) {
2638  l_firste = ps_ve10_1->l_count;
2639  }
2640  /* printf("New event number offset %d, index %d\n",ps_ve10_1->l_count,l_events);*/
2641  }
2642  l_last = ps_ve10_1->l_count;
2643  if (write(l_out, (CHARS*)&s_tag, sizeof(s_tag)) != sizeof(s_tag)) {
2644  close(l_chan);
2645  close(l_out);
2646  return (GETEVT__TAGWRERR);
2647  }
2648  }
2649  }
2650  l_bufnr++;
2651  /* if(l_bufnr > 3) break; */
2652  }
2653  val = lseek(l_out, 0, SEEK_SET); /* rewind file */
2654  if (-1 == val) {
2655  close(l_chan);
2656  close(l_out);
2657  return (GETEVT__FAILURE);
2658  }
2659  s_taghe.l_endian = 1;
2660  s_taghe.l_version = 1;
2661  s_taghe.l_bufsize = l_size;
2662  s_taghe.l_buffers = l_bufnr;
2663  s_taghe.l_events = l_events;
2664  s_taghe.l_filesize = sizeof(s_tag) * l_events;
2665  s_taghe.l_linear = (l_lin == 1);
2666  s_taghe.l_first = l_firste;
2667  s_taghe.l_last = s_tag.l_event;
2668  if (write(l_out, (CHARS*)&s_taghe, sizeof(s_taghe)) != sizeof(s_taghe)) {
2669  close(l_chan);
2670  close(l_out);
2671  return (GETEVT__TAGWRERR);
2672  }
2673  close(l_chan);
2674  close(l_out);
2675  printf("Buffers %6d, of size %d, Events %10d, first %d, last %d ",
2676  s_taghe.l_buffers,
2677  s_taghe.l_bufsize,
2678  s_taghe.l_events,
2679  s_taghe.l_first,
2680  s_taghe.l_last);
2681  if (s_taghe.l_linear) {
2682  printf("linear\n");
2683  } else {
2684  printf("not linear\n");
2685  }
2686 
2687  return (GETEVT__SUCCESS);
2688 } /* end of f_evt_cre_tagfile */
2689 /*1+ C Main ****************+******************************************/
2690 /*+ Module : f_evt_get_tagopen */
2691 /*--------------------------------------------------------------------*/
2692 /*+ CALLING : f_evt_get_tagopen(channel,tagfile,lmdfile,header,print) */
2693 /*--------------------------------------------------------------------*/
2694 /* */
2695 /*+ PURPOSE : Open tag and lmd file */
2696 /*+ ARGUMENTS : */
2697 /*+ channel : s_evt_channel* , must be allocated. */
2698 /*+ tagfile : Name of tag file */
2699 /*+ filename : LMD file name */
2700 /*+ header : address of CHARS pointer. If it is not NULL, then */
2701 /* try to return file header or other information */
2702 /* about server. If it is NULL, then returns nothing. */
2703 /*+ print : Print flag: 1=verbose */
2704 /*+ Return type : INTS4 . */
2705 /*+ Status codes: */
2706 /*- GETEVT__SUCCESS : success. */
2707 /*- GETEVT__NOFILE : file does not exist. */
2708 /*- GETEVT__TAGRDERR : tag file read error. */
2709 /*- GETEVT__RDERR : read server error. */
2710 /*+ Declaration : */
2711 /* INTS4 f_evt_get_tagopen( */
2712 /* s_evt_channel *,CHARS *,CHARS *,CHARS **,INTS4); */
2713 /*+ FUNCTION : Open tag file and lmd file. If no tag file is found, */
2714 /* a standard f_evt_get_open is called. In this case */
2715 /* following tag functions call standard f_evt_get functions. */
2716 /*1- C Main ****************+******************************************/
2717 INTS4 f_evt_get_tagopen(s_evt_channel* ps_chan, CHARS* pc_tag, CHARS* pc_lmd, CHARS** ps_head, INTS4 l_prihe)
2718 {
2719  INTS4 /*l_temp,*/ ii;
2720  s_bufhe* ps_bufhe;
2721 
2722  ps_chan->ps_tag = NULL; /* tagfile buffer */
2723  ps_chan->ps_taghe = NULL;
2724  if (l_prihe) {
2725  printf("LMD file %s, TAG file %s\n", pc_lmd, pc_tag);
2726  }
2727 
2728  /* if tag file name not specified, do not try to open it SL:11.11.09*/
2729  if ((pc_tag == 0) || (*pc_tag == 0)) {
2730  ps_chan->l_tagfile_no = -1;
2731  } else {
2732  ps_chan->l_tagfile_no = open(pc_tag, GET__OPEN_FLAG);
2733  }
2734 
2735  /* open tag file and read header */
2736  if (ps_chan->l_tagfile_no == -1) {
2737  /* no tag file, use normal open */
2738  /*=============================================*/
2739  ii = f_evt_get_open(GETEVT__FILE, pc_lmd, ps_chan, (CHARS**)&ps_bufhe, 0, 0);
2740  *ps_head = (CHARS*)ps_bufhe;
2741  if (ii != GETEVT__SUCCESS) {
2742  printf("Error opening input file %s\n", pc_lmd);
2743  return (ii);
2744  }
2745  /* printf("No tag file for %s\n",pc_lmd); */
2746  if (l_prihe) {
2747  ii = f_evt_type(ps_bufhe, NULL, 0, 0, 0, 0);
2748  }
2749  return (GETEVT__SUCCESS);
2750  }
2751  /*=============================================*/
2752  /* tag file found */
2753  ps_chan->ps_taghe = (s_taghe*)malloc(sizeof(s_taghe));
2754  if (read(ps_chan->l_tagfile_no, (CHARS*)ps_chan->ps_taghe, sizeof(s_taghe)) != sizeof(s_taghe)) {
2755  free(ps_chan->ps_taghe);
2756  ps_chan->ps_taghe = NULL;
2757  close(ps_chan->l_tagfile_no);
2758  return (GETEVT__TAGRDERR);
2759  }
2760  if (NULL == ps_chan->ps_taghe) {
2761  close(ps_chan->l_tagfile_no);
2762  return (GETEVT__TAGRDERR);
2763  }
2764  if (ps_chan->ps_taghe->l_endian != 1) {
2765  ps_chan->l_tagswap = 1;
2766  }
2767  if (ps_chan->l_tagswap) {
2768  f_evt_swap((CHARS*)ps_chan->ps_taghe, sizeof(s_taghe));
2769  }
2770  if (l_prihe) {
2771  printf("Buffers %6d, of size %d, Events %10d, first %d, last %d",
2772  ps_chan->ps_taghe->l_buffers,
2773  ps_chan->ps_taghe->l_bufsize,
2774  ps_chan->ps_taghe->l_events,
2775  ps_chan->ps_taghe->l_first,
2776  ps_chan->ps_taghe->l_last);
2777  if (ps_chan->ps_taghe->l_linear) {
2778  printf(" linear\n");
2779  } else {
2780  printf(" not linear\n");
2781  }
2782  }
2783  if ((ps_chan->l_channel_no = open(pc_lmd, GET__OPEN_FLAG)) == -1) {
2784  if (ps_chan->ps_taghe != NULL) {
2785  free(ps_chan->ps_taghe);
2786  }
2787  ps_chan->ps_taghe = NULL;
2788  close(ps_chan->l_tagfile_no);
2789  return (GETEVT__NOFILE);
2790  }
2791  /* read buffer header to check if we have to swap */
2792  ps_bufhe = (s_bufhe*)c_temp;
2793  if (ps_chan->ps_taghe->l_bufsize >= 0 && ps_chan->ps_taghe->l_bufsize < sizeof(c_temp)) {
2794  if (read(ps_chan->l_channel_no, c_temp, ps_chan->ps_taghe->l_bufsize) != ps_chan->ps_taghe->l_bufsize) {
2795  if (ps_chan->ps_taghe != NULL) {
2796  free(ps_chan->ps_taghe);
2797  }
2798  ps_chan->ps_taghe = NULL;
2799  if (ps_chan->ps_tag != NULL) {
2800  free(ps_chan->ps_tag);
2801  }
2802  ps_chan->ps_tag = NULL;
2803  close(ps_chan->l_tagfile_no);
2804  close(ps_chan->l_channel_no);
2805  return (GETEVT__RDERR);
2806  }
2807  }
2808  if (ps_chan->ps_taghe != NULL) {
2809  if (ps_chan->ps_taghe->l_linear == 0) {
2810  if (ps_chan->ps_taghe->l_filesize >= 0 && ps_chan->ps_taghe->l_filesize < sizeof(s_tag)) {
2811  ps_chan->ps_tag = (s_tag*)malloc(ps_chan->ps_taghe->l_filesize);
2812  if (read(ps_chan->l_tagfile_no, (CHARS*)ps_chan->ps_tag, ps_chan->ps_taghe->l_filesize)
2813  != ps_chan->ps_taghe->l_filesize) {
2814  if (ps_chan->ps_taghe != NULL) {
2815  free(ps_chan->ps_taghe);
2816  }
2817  ps_chan->ps_taghe = NULL;
2818  if (ps_chan->ps_tag != NULL) {
2819  free(ps_chan->ps_tag);
2820  }
2821  ps_chan->ps_tag = NULL;
2822  close(ps_chan->l_tagfile_no);
2823  close(ps_chan->l_channel_no);
2824  return (GETEVT__TAGRDERR);
2825  }
2826  }
2827  }
2828  }
2829  if (ps_bufhe->l_free[0] != 1) {
2830  ps_chan->l_lmdswap = 1;
2831  }
2832  if (ps_chan->l_lmdswap) {
2833  f_evt_swap_filhe(ps_bufhe);
2834  }
2835  if (l_prihe) {
2836  ii = f_evt_type(ps_bufhe, NULL, 0, 0, 0, 0);
2837  }
2838  if (ps_head != NULL) {
2839  if (ps_bufhe->i_type == 2000) {
2840  *ps_head = (CHARS*)ps_bufhe;
2841  }
2842  }
2843  ps_chan->l_evt_buf_size = 0;
2844  ps_chan->pc_evt_buf = NULL;
2845  ps_chan->l_io_buf_posi = 0; /* keeps index of last event */
2846  return (GETEVT__SUCCESS);
2847 }
2848 
2849 /*1+ C Main ****************+******************************************/
2850 /*+ Module : f_evt_get_tagnext */
2851 /*--------------------------------------------------------------------*/
2852 /*+ CALLING : f_evt_get_tagnext(channel,skip,**event) */
2853 /*--------------------------------------------------------------------*/
2854 /* */
2855 /*+ PURPOSE : Get tagged event from lmd file */
2856 /*+ ARGUMENTS : */
2857 /*+ channel : s_evt_channel* , must be allocated. */
2858 /*+ event : address of pointer. If it is not NULL, then */
2859 /* return event pointer. */
2860 /*+ Return type : INTS4 . */
2861 /*+ Status codes: */
2862 /*- GETEVT__SUCCESS : success. */
2863 /*- GETEVT__NOFILE : file does not exist. */
2864 /*- GETEVT__TAGRDERR : tag file read error. */
2865 /*- GETEVT__RDERR : lmd read error. */
2866 /*- GETEVT__FRAGMENT : Event fragment found. */
2867 /*- GETEVT__NOMORE : No more events. */
2868 /*+ Declaration : */
2869 /* INTS4 f_evt_get_tagnext(s_evt_channel *,INTS4,INTS4 **); */
2870 /*+ FUNCTION : Get next event at current position, either in tag */
2871 /* file, or in lmd file. Optional <skip> events are skipped. */
2872 /*1- C Main ****************+******************************************/
2873 INTS4 f_evt_get_tagnext(s_evt_channel* ps_chan, INTS4 l_skip, INTS4** pl_event)
2874 {
2875  INTS4 ii = 0, *pl = NULL, kk;
2876  /* no tagfile */
2877  /*=============================================*/
2878  if (ps_chan->ps_taghe == NULL) {
2879  for (kk = 0; kk <= l_skip; kk++) {
2880  ii = f_evt_get_event(ps_chan, (INTS4**)&pl, NULL);
2881  if (ii != GETEVT__SUCCESS) {
2882  break;
2883  }
2884  }
2885  }
2886  /*=============================================*/
2887  else {
2888  /* l_io_buf_posi is the index of last event */
2889  ii = f_evt_get_tagevent(ps_chan, ps_chan->l_io_buf_posi + l_skip + 1, 1, (INTS4**)&pl);
2890  }
2891  if (pl_event != NULL) {
2892  *pl_event = pl;
2893  }
2894  return (ii);
2895 }
2896 /*1+ C Main ****************+******************************************/
2897 /*+ Module : f_evt_get_tagevent */
2898 /*--------------------------------------------------------------------*/
2899 /*+ CALLING : f_evt_get_tagevent(*channel, value, mode, **event) */
2900 /*--------------------------------------------------------------------*/
2901 /* */
2902 /*+ PURPOSE : Get tagged event from lmd file */
2903 /*+ ARGUMENTS : */
2904 /*+ channel : s_evt_channel* , must be allocated. */
2905 /*+ value : event number or index */
2906 /*+ mode : 0: value is number, 1: value is index */
2907 /*+ event : address of pointer. If it is not NULL, then */
2908 /* return event pointer. */
2909 /*+ Return type : INTS4 . */
2910 /*+ Status codes: */
2911 /*- GETEVT__SUCCESS : success. */
2912 /*- GETEVT__TAGRDERR : tag file read error. */
2913 /*- GETEVT__RDERR : lmd read error. */
2914 /*- GETEVT__FRAGMENT : Event fragment found. */
2915 /*- GETEVT__NOMORE : No more events. */
2916 /*- GETEVT__NOTAG : Specified event not in tag file. */
2917 /*+ Declaration : */
2918 /* INTS4 f_evt_get_tagevent( */
2919 /* s_evt_channel *,INTS4,INTS4,INTS4 **); */
2920 /*+ FUNCTION : Get tag event. If no tag file is there, skip <value> */
2921 /* events, or look for event number <value> */
2922 /*1- C Main ****************+******************************************/
2923 INTS4 f_evt_get_tagevent(s_evt_channel* ps_chan, INTS4 l_value, INTS4 l_type, INTS4** pl_event)
2924 {
2925  INTS4 /*l_temp,l_chan,l_tag,*/ ii, kk, /*lb,l_tagbufs,l_tagrest,*/ l_evt, l_off, l_typ, l_val, l_evsize, l_fragsize;
2926  INTS4 la_head[2], *pl;
2927  CHARS* pc;
2928  s_ve10_1* ps_ve10_1;
2929  s_bufhe* ps_bufhe;
2930  s_tag* ps_tag;
2931  s_tag s_tag_l;
2932  // s_filhe* ps_filhe;
2933  // s_evt_channel s_chan_i;
2934 
2935  l_typ = l_type;
2936  l_val = l_value;
2937  /* no tagfile */
2938  /*=============================================*/
2939  if (ps_chan->ps_taghe == NULL) {
2940  /* search by index */
2941  if (l_type == 1) {
2942  if (l_val == 0) {
2943  l_val = 1;
2944  }
2945  for (kk = 0; kk < l_val; kk++) {
2946  ii = f_evt_get_event(ps_chan, (INTS4**)&pl, NULL);
2947  if (ii != GETEVT__SUCCESS) {
2948  return (ii);
2949  }
2950  }
2951  *pl_event = pl;
2952  } else { /* search by event number */
2953  ii = f_evt_get_event(ps_chan, (INTS4**)&ps_ve10_1, NULL);
2954  if (ii != GETEVT__SUCCESS) {
2955  return (ii);
2956  }
2957  while (ps_ve10_1->l_count != l_val) {
2958  ii = f_evt_get_event(ps_chan, (INTS4**)&ps_ve10_1, NULL);
2959  if (ii != GETEVT__SUCCESS) {
2960  return (ii);
2961  }
2962  }
2963  *pl_event = (INTS4*)ps_ve10_1;
2964  }
2965  return (GETEVT__SUCCESS);
2966  }
2967  /*=============================================*/
2968  ps_ve10_1 = (s_ve10_1*)c_temp;
2969  ps_bufhe = (s_bufhe*)c_temp;
2970 
2971  /* linear==1 means that event numbers are subsequent. */
2972  /* in that case we can calculate index from number */
2973  if (ps_chan->ps_taghe->l_linear) {
2974  if (l_typ == 0) {
2975  l_val = l_value - ps_chan->ps_taghe->l_first + 1;
2976  }
2977  l_typ = 1;
2978  }
2979  /* search by index or by value */
2980  if (l_typ == 1) {
2981  if (l_val > ps_chan->ps_taghe->l_events) {
2982  printf("Event index %d not found\n", l_val);
2983  return (GETEVT__NOTAG);
2984  }
2985  if (l_val == 0) {
2986  l_val = 1;
2987  }
2988  ps_tag = (s_tag*)&s_tag_l;
2989  int val =
2990  lseek(ps_chan->l_tagfile_no, (l_val - 1) * sizeof(s_tag) + sizeof(s_taghe), SEEK_SET); /* set file offset*/
2991  if (-1 == val) {
2992  return (GETEVT__TAGRDERR);
2993  }
2994  if (read(ps_chan->l_tagfile_no, (CHARS*)ps_tag, sizeof(s_tag)) != sizeof(s_tag)) {
2995  return (GETEVT__TAGRDERR);
2996  }
2997  if (ps_chan->l_tagswap) {
2998  f_evt_swap((CHARS*)ps_tag, sizeof(s_tag));
2999  }
3000  ps_chan->l_io_buf_posi = l_val; /* keeps index */
3001  } else {
3002  ps_tag = ps_chan->ps_tag;
3003  for (ii = 1; ii <= ps_chan->ps_taghe->l_events; ii++) {
3004  if (ps_tag->l_event == l_val) {
3005  break;
3006  }
3007  ps_tag++;
3008  }
3009  if (ps_tag->l_event != l_val) {
3010  printf("Event number %d not found\n", l_val);
3011  return (GETEVT__NOTAG);
3012  }
3013  ps_chan->l_io_buf_posi = ii; /* keeps index of last event */
3014  }
3015  /* now we have the requested event in ps_tag */
3016  l_off = ps_tag->l_offset;
3017  if (l_off < 0) {
3018  l_off = ((-l_off) / ps_chan->ps_taghe->l_bufsize) * ps_chan->ps_taghe->l_bufsize;
3019  }
3020  l_evt = ps_tag->l_event;
3021  /* full event in buffer, read */
3022  int val;
3023  if (ps_tag->l_offset > 0) {
3024  ps_ve10_1 = (s_ve10_1*)c_temp;
3025  val = lseek(ps_chan->l_channel_no, l_off, SEEK_SET); /* set file offset*/
3026  if (-1 == val) {
3027  return (GETEVT__TAGRDERR);
3028  }
3029  if (read(ps_chan->l_channel_no, c_temp, 8) != 8) {
3030  return (GETEVT__RDERR);
3031  }
3032  if (ps_chan->l_lmdswap) {
3033  f_evt_swap(c_temp, 8);
3034  }
3035  if (read(ps_chan->l_channel_no, (CHARS*)&c_temp[8], ps_ve10_1->l_dlen * 2) != ps_ve10_1->l_dlen * 2) {
3036  return (GETEVT__RDERR);
3037  }
3038  if (ps_chan->l_lmdswap) {
3039  f_evt_swap((CHARS*)&c_temp[8], ps_ve10_1->l_dlen * 2);
3040  }
3041  /*ii=f_evt_type(NULL,(s_evhe *)ps_ve10_1,-1,0,1,1);*/
3042  } else
3043  /* spanning event begin, read to event buffer */
3044  {
3045  val = lseek(ps_chan->l_channel_no, l_off, SEEK_SET); /* set file offset to buffer begin */
3046  if (-1 == val) {
3047  return (GETEVT__TAGRDERR);
3048  }
3049  if (read(ps_chan->l_channel_no, c_temp, sizeof(s_bufhe)) != sizeof(s_bufhe)) {
3050  return (GETEVT__RDERR);
3051  }
3052  if (ps_chan->l_lmdswap) {
3053  f_evt_swap(c_temp, sizeof(s_bufhe));
3054  }
3055  /* is event buffer big enough? */
3056  l_evsize = ps_bufhe->l_free[1] + 4; /* total words */
3057  if (ps_chan->l_evt_buf_size < l_evsize * 2) {
3058  if (ps_chan->pc_evt_buf != NULL) {
3059  free(ps_chan->pc_evt_buf);
3060  }
3061  ps_chan->l_evt_buf_size = l_evsize * 2;
3062  ps_chan->pc_evt_buf = (CHARS*)malloc(ps_chan->l_evt_buf_size);
3063  }
3064  l_fragsize = 0;
3065  ps_ve10_1 = (s_ve10_1*)ps_chan->pc_evt_buf;
3066  val = lseek(ps_chan->l_channel_no, -ps_tag->l_offset, SEEK_SET); /* set file offset*/
3067  if (-1 == val) {
3068  return (GETEVT__TAGRDERR);
3069  }
3070  if (read(ps_chan->l_channel_no, ps_chan->pc_evt_buf, 8) != 8) {
3071  return (GETEVT__RDERR);
3072  }
3073  if (ps_chan->l_lmdswap) {
3074  f_evt_swap(ps_chan->pc_evt_buf, 8);
3075  }
3076  pc = ps_chan->pc_evt_buf + 8;
3077  /* read fragment */
3078  if (read(ps_chan->l_channel_no, pc, ps_ve10_1->l_dlen * 2) != ps_ve10_1->l_dlen * 2) {
3079  return (GETEVT__RDERR);
3080  }
3081  l_fragsize += ps_ve10_1->l_dlen + 4; /* fragment size + header [w] */
3082  pc += ps_ve10_1->l_dlen * 2; /* next in event buffer */
3083  ps_ve10_1->l_dlen = l_evsize - 4; /* set correct event size */
3084  /* loop over fragments */
3085  while (l_fragsize < l_evsize) {
3086  l_off += ps_chan->ps_taghe->l_bufsize; /* next buffer absolut address */
3087  val = lseek(ps_chan->l_channel_no, l_off + sizeof(s_bufhe), SEEK_SET); /* set file offset*/
3088  if (-1 == val) {
3089  return (GETEVT__TAGRDERR);
3090  }
3091  if (read(ps_chan->l_channel_no, (CHARS*)&la_head, 8) != 8) {
3092  return (GETEVT__RDERR);
3093  }
3094  if (ps_chan->l_lmdswap) {
3095  f_evt_swap((CHARS*)&la_head, 8);
3096  }
3097  if (read(ps_chan->l_channel_no, pc, la_head[0] * 2) != la_head[0] * 2) {
3098  return (GETEVT__RDERR);
3099  }
3100  pc += la_head[0] * 2;
3101  l_fragsize += la_head[0];
3102  }
3103  /* now swap whole event in buffer */
3104  pc = ps_chan->pc_evt_buf + 8; /* header already swapped */
3105  ps_ve10_1 = (s_ve10_1*)ps_chan->pc_evt_buf;
3106  if (ps_chan->l_lmdswap) {
3107  f_evt_swap(pc, ps_ve10_1->l_dlen * 2);
3108  }
3109  }
3110  *pl_event = (INTS4*)ps_ve10_1;
3111  return (GETEVT__SUCCESS);
3112 } /* end of f_evt_ini_bufhe */
3113 
3114 /*1+ C Main ****************+******************************************/
3115 /*+ Module : f_evt_get_tagclose */
3116 /*--------------------------------------------------------------------*/
3117 /*+ CALLING : f_evt_get_tagclose(s_evt_channel) */
3118 /*--------------------------------------------------------------------*/
3119 /* */
3120 /*+ PURPOSE : Close tag and lmd file, cleanup s_evt_channel */
3121 /*+ ARGUMENTS : */
3122 /*+ channel : s_evt_channel* , must be allocated. */
3123 /*+ Return type : INTS4 */
3124 /*+ Declaration : */
3125 /* INTS4 f_evt_get_tagclose(s_evt_channel *); */
3126 /*+ FUNCTION : Create a tag file from lmd file */
3127 /*1- C Main ****************+******************************************/
3129 {
3130  /*=============================================*/
3131  if (ps_chan->ps_taghe == NULL) {
3132  f_evt_get_close(ps_chan);
3133  return (GETEVT__SUCCESS);
3134  }
3135  /*=============================================*/
3136  free(ps_chan->ps_taghe);
3137  if (ps_chan->ps_tag != NULL) {
3138  free(ps_chan->ps_tag);
3139  }
3140  if (ps_chan->pc_evt_buf != NULL) {
3141  free(ps_chan->pc_evt_buf);
3142  }
3143  close(ps_chan->l_tagfile_no);
3144  close(ps_chan->l_channel_no);
3145  memset((void*)ps_chan, 0, sizeof(s_evt_channel)); /* clear memory */
3146  return (GETEVT__SUCCESS);
3147 }
3149 {
3150  s_evt_channel* x;
3151  x = (s_evt_channel*)malloc(sizeof(s_evt_channel));
3152  memset(x, 0, sizeof(s_evt_channel));
3153  return (x);
3154 }
INTS4 l_buf_type
Definition: f_evt.h:57
#define GETEVT__NOSERVER
Definition: f_evt.h:135
INTS2 filhe_file_l
Definition: s_filhe.h:65
INTS4 l_last
Definition: f_evt.h:42
INTS4 f_evt_rev_port(INTS4 l_port)
Definition: f_evt.c:513
CHARS h_end
Definition: s_bufhe.h:43
#define PUTEVT__SUCCESS
Definition: f_evt.h:146
#define GETEVT__SUCCESS
Definition: f_evt.h:130
CHARS filhe_file[86]
Definition: s_filhe.h:66
INTS4 l_channel_no
Definition: f_evt.h:59
#define LMD__SUCCESS
Definition: fLmd.h:19
#define PUTEVT__CLOSE_ERR
Definition: f_evt.h:151
s_tag * ps_tag
Definition: f_evt.h:89
INTS4 f_evt_get_subevent(s_ve10_1 *ps_ve10_1, INTS4 l_subevent, INTS4 **pl_se, INTS4 **pl_d, INTS4 *pl_lwords)
Definition: f_evt.c:228
INTS4 f_stc_close(struct s_tcpcomm *ps_tcp)
Definition: f_stccomm.c:1095
for(Int_t i=0;i< 100;i++)
CHARS * pc_evt_buf
Definition: f_evt.h:80
INTS4 f_evt_type(s_bufhe *ps_bufhe, s_evhe *ps_evhe, INTS4 l_subid, INTS4 l_long, INTS4 l_hex, INTS4 l_data)
Definition: f_evt.c:306
INTS4 f_evt_tag_filter(s_ve10_1 *ps_ve10_1)
Definition: f_evt.c:2385
INTS4 l_first_buf
Definition: f_evt.h:77
#define GETLMD__EOFILE
Definition: fLmd.h:24
short INTS2
Definition: typedefs.h:19
#define LMD__INDEX
Definition: sMbs.h:20
INTS4 l_buffers
Definition: f_evt.h:38
#define LMD__LARGE_FILE
Definition: sMbs.h:22
INTS4 f_evt_get_close(s_evt_channel *ps_chan)
Definition: f_evt.c:1189
#define GETEVT__TIMEOUT
Definition: f_evt.h:139
INTS4 l_lmdswap
Definition: f_evt.h:87
CHARS h_subcrate
Definition: s_ves10_1.h:35
sLmdControl * pLmd
Definition: f_evt.h:90
CHARS filhe_run[66]
Definition: s_filhe.h:71
INTS4 l_evt
Definition: s_bufhe.h:46
#define GETLMD__NOMORE
Definition: fLmd.h:25
INTS4 f_evt_get_open(INTS4 l_mode, CHARS *pc_server, s_evt_channel *ps_chan, CHARS **ps_info, INTS4 l_sample, INTS4 l_param)
Definition: f_evt.c:555
s_bufhe s_bufhe_1
Definition: f_evt.h:82
INTS4 f_evt_get_buffer(s_evt_channel *ps_chan, INTS4 *ps_buffer)
Definition: f_evt.c:1869
INTS2 filhe_used
Definition: s_filhe.h:56
#define LMD__NO_OVERWRITE
Definition: sMbs.h:29
CHARS string[78]
Definition: s_filhe.h:17
INTS4 l_free[4]
Definition: s_bufhe.h:49
#define LMD__NO_INDEX
Definition: sMbs.h:28
INTS4 l_first_get
Definition: f_evt.h:75
CHARS filhe_exp[66]
Definition: s_filhe.h:73
INTS4 f_evt_timeout(s_evt_channel *ps_chan, INTS4 l_sec)
Definition: f_evt.c:2013
#define MAX_BUF_LGTH
Definition: f_evt.h:28
#define GETEVT__NOCHANNEL
Definition: f_evt.h:138
s_bufhe * ps_bufhe
Definition: f_evt.h:81
#define GETEVT__NOFILE
Definition: f_evt.h:134
#define PORT__TRANSPORT
Definition: portnum_def.h:13
INTS4 l_buf_size
Definition: f_evt.h:60
INTS4 f_evt_put_event(s_evt_channel *ps_chan, INTS4 *pl_evt_buf)
Definition: f_evt.c:1462
Definition: s_evhe.h:27
#define PUTEVT__TOOBIG
Definition: f_evt.h:149
s_evt_channel * f_evt_control()
Definition: f_evt.c:3148
#define GETEVT__FAILURE
Definition: f_evt.h:131
INTS2 filhe_subtype
Definition: s_filhe.h:55
#define GETEVT__TAGWRERR
Definition: f_evt.h:143
INTS2 i_subtype
Definition: s_bufhe.h:41
INTS4 l_bufs_in_stream
Definition: f_evt.h:64
INTS2 i_type
Definition: s_ve10_1.h:32
#define GETEVT__NOLMDFILE
Definition: f_evt.h:144
uint32_t fLmdGetMbsEvent(sLmdControl *pLmdControl, sMbsHeader **event)
Definition: fLmd.c:463
INTS4 filhe_lines
Definition: s_filhe.h:74
INTS2 i_procid
Definition: s_ves10_1.h:34
#define RFIO_lseek
Definition: f_evt.c:21
INTS2 i_type
Definition: s_evhe.h:30
INTS4 l_first_put
Definition: f_evt.h:76
parOut open("mq_ex_params.root")
#define RFIO_open
Definition: f_evt.c:18
INTS4 f_evt_error(INTS4 l_error, CHARS *pc_dest, INTS4 l_out)
Definition: f_evt.c:1756
#define GETEVT__EVENT
Definition: f_evt.h:122
uint32_t fLmdGetElement(sLmdControl *pLmdControl, uint32_t iEvent, sMbsHeader **event)
Definition: fLmd.c:792
#define GETEVT__NOMORE
Definition: f_evt.h:133
#define MIN_BUF_LGTH
Definition: f_evt.h:29
INTS4 l_filesize
Definition: f_evt.h:40
#define PUTEVT__FAILURE
Definition: f_evt.h:148
INTS4 l_buf_no
Definition: f_evt.h:61
s_taghe * ps_taghe
Definition: f_evt.h:88
INTS2 filhe_type
Definition: s_filhe.h:54
CHARS * pc_io_buf
Definition: f_evt.h:79
#define STC__TIMEOUT
Definition: f_stccomm.h:373
INTS4 f_stc_discclient(INTS4 i_channel)
Definition: f_stccomm.c:1175
CHARS * f_evt_get_buffer_ptr(s_evt_channel *ps_chan)
Definition: f_evt.c:2107
#define GETEVT__NOTAGFILE
Definition: f_evt.h:140
uint32_t fLmdInitMbs(sLmdControl *pLmdControl, char *Nodename, uint32_t iMaxBytes, uint32_t iBuffers, uint32_t iStreams, uint32_t iPort, uint32_t iTimeout)
Definition: fLmd.c:405
uint32_t fLmdPutClose(sLmdControl *pLmdControl)
Definition: fLmd.c:307
uint32_t fLmdGetClose(sLmdControl *pLmdControl)
Definition: fLmd.c:912
INTS4 l_evt_buf_posi
Definition: f_evt.h:69
INTS4 f_stc_connectserver(CHARS *c_node, INTS4 l_port, INTS4 *pi_channel, struct s_tcpcomm *ps_client)
Definition: f_stccomm.c:486
INTS4 l_offset
Definition: f_evt.h:49
INTS2 i_type
Definition: s_bufhe.h:40
unsigned int INTU4
Definition: typedefs.h:22
#define GETEVT__NOTAG
Definition: f_evt.h:141
#define STC__SUCCESS
Definition: f_stccomm.h:368
#define MAX_LONG
Definition: f_evt.h:31
struct cv_string s_strings[30]
Definition: s_filhe.h:75
INTS4 l_dlen
Definition: s_bufhe.h:39
INTS4 l_dlen
Definition: s_ve10_1.h:31
#define GETEVT__RDERR
Definition: f_evt.h:136
INTS2 filhe_user_l
Definition: s_filhe.h:67
#define GETEVT__TRANS
Definition: f_evt.h:121
INTS4 l_stream_bufs
Definition: f_evt.h:65
INTS4 l_first
Definition: f_evt.h:41
INTS4 f_evt_get_newbuf(s_evt_channel *)
Definition: f_evt.c:2125
INTS4 l_tagswap
Definition: f_evt.h:86
INTS4 l_dlen
Definition: s_ves10_1.h:31
#define LMD__BUFFER
Definition: sMbs.h:24
#define LMD__TIMEOUT
Definition: fLmd.h:30
int INTS4
Definition: typedefs.h:21
CHARS h_begin
Definition: s_bufhe.h:44
INTS4 f_evt_check_buf(CHARS *, INTS4 *, INTS4 *, INTS4 *, INTS4 *)
Definition: f_evt.c:2239
INTS4 filhe_dlen
Definition: s_filhe.h:53
INTS4 f_evt_get_tagnext(s_evt_channel *ps_chan, INTS4 l_skip, INTS4 **pl_event)
Definition: f_evt.c:2873
INTS4 l_tagfile_no
Definition: f_evt.h:85
INTS4 f_evt_ini_bufhe(s_evt_channel *ps_chan)
Definition: f_evt.c:2329
#define GETEVT__FRAGMENT
Definition: f_evt.h:132
INTS4 l_version
Definition: f_evt.h:36
#define PUTEVT__TOO_SMALLS
Definition: f_evt.h:150
INTS2 i_subtype
Definition: s_evhe.h:31
#define PUTEVT__WRERR
Definition: f_evt.h:152
CHARS filhe_label[30]
Definition: s_filhe.h:64
CHARS filhe_time[24]
Definition: s_filhe.h:69
CHARS filhe_user[30]
Definition: s_filhe.h:68
Definition: f_evt.h:33
#define RFIO_close
Definition: f_evt.c:19
INTS4 l_endian
Definition: f_evt.h:35
#define GETEVT__REVSERV
Definition: f_evt.h:123
INTS4 l_evt_size
Definition: f_evt.h:70
uint32_t fLmdGetOpen(sLmdControl *pLmdControl, char *Filename, sMbsFileHeader *pBuffHead, uint32_t iBytes, uint32_t iUseOffset)
Definition: fLmd.c:566
INTS4 f_evt_cre_tagfile(CHARS *pc_lmd, CHARS *pc_tag, INTS4(*e_filter)())
Definition: f_evt.c:2403
INTS2 i_trigger
Definition: s_ve10_1.h:35
#define PUTEVT__FILE_EXIST
Definition: f_evt.h:147
INTS2 filhe_label_l
Definition: s_filhe.h:63
CHARS c_channel[128]
Definition: f_evt.h:84
INTS4 f_evt_put_buffer(s_evt_channel *ps_chan, s_bufhe *ps_bufhe)
Definition: f_evt.c:1620
#define PORT__EVENT_SERV
Definition: portnum_def.h:16
INTS4 l_events
Definition: f_evt.h:39
INTS4 l_frag_len
Definition: f_evt.h:78
INTS4 l_bufsize
Definition: f_evt.h:37
uint32_t fLmdCloseMbs(sLmdControl *pLmdControl)
Definition: fLmd.c:442
INTS4 f_evt_skip_buffer(s_evt_channel *ps_chan, INTS4 l_buffer)
Definition: f_evt.c:1959
#define PUTEVT__NOCHANNEL
Definition: f_evt.h:153
INTS4 l_timeout
Definition: f_evt.h:71
INTS4 f_evt_get_tagopen(s_evt_channel *ps_chan, CHARS *pc_tag, CHARS *pc_lmd, CHARS **ps_head, INTS4 l_prihe)
Definition: f_evt.c:2717
#define MAX__DLEN
Definition: s_bufhe.h:16
INTS4 f_ut_utime(INTS4, INTS4, CHARS *)
Definition: f_ut_utime.c:63
INTS4 f_evt_get_tagevent(s_evt_channel *ps_chan, INTS4 l_value, INTS4 l_type, INTS4 **pl_event)
Definition: f_evt.c:2923
INTS4 f_stc_write(INTS1 *p_buffer, INTS4 i_buflen, INTS4 i_channel)
Definition: f_stccomm.c:324
sLmdControl * fLmdAllocateControl()
Definition: fLmd.c:1017
INTS4 l_io_buf_size
Definition: f_evt.h:66
INTS2 filhe_run_l
Definition: s_filhe.h:70
INTS4 f_evt_put_close(s_evt_channel *ps_chan)
Definition: f_evt.c:1664
INTS4 l_current_i
Definition: s_bufhe.h:47
INTS4 f_evt_swap(CHARS *pc_source, INTS4 l_length)
Definition: f_evt.c:2033
INTS4 l_buf_posi
Definition: f_evt.h:62
#define PORT__STREAM_SERV
Definition: portnum_def.h:15
uint32_t fLmdPutElement(sLmdControl *pLmdControl, sMbsHeader *pHeader)
Definition: fLmd.c:190
Definition: f_evt.h:46
#define LMD__TYPE_FILE_HEADER_101_1
Definition: sMbs.h:11
#define RFIO_read
Definition: f_evt.c:20
INTS2 filhe_exp_l
Definition: s_filhe.h:72
INTS4 filhe_stime[2]
Definition: s_filhe.h:61
INTS4 l_dlen
Definition: s_evhe.h:29
uint32_t fLmdPutOpen(sLmdControl *pLmdControl, char *Filename, sMbsFileHeader *pBuffHead, uint32_t iBytes, uint32_t iOver, uint32_t iUseOffset, uint32_t iLargeFile)
Definition: fLmd.c:110
INTS2 i_type
Definition: s_ves10_1.h:32
INTS4 l_buf
Definition: s_bufhe.h:45
struct s_tcpcomm * pTCP
Definition: fLmd.h:59
INTS4 l_time[2]
Definition: s_bufhe.h:48
INTS4 f_stc_read(INTS1 *p_buffer, INTS4 i_buflen, INTS4 i_channel, INTS4 i_timeout)
Definition: f_stccomm.c:123
#define GETLMD__NOBUFFER
Definition: fLmd.h:26
INTS4 l_linear
Definition: f_evt.h:43
#define GETEVT__TAGRDERR
Definition: f_evt.h:142
INTS4 l_event
Definition: f_evt.h:48
INTS2 i_subtype
Definition: s_ve10_1.h:33
INTS4 f_evt_put_open(CHARS *pc_file, INTS4 l_size, INTS4 l_stream, INTS4 l_type, INTS4 l_subtype, s_evt_channel *ps_chan, CHARS *ps_filhe)
Definition: f_evt.c:1310
char CHARS
Definition: typedefs.h:15
INTS4 filhe_free[4]
Definition: s_filhe.h:62
INTS4 l_buf_subtype
Definition: f_evt.h:58
INTS4 f_evt_get_tagclose(s_evt_channel *ps_chan)
Definition: f_evt.c:3128
INTS4 f_evt_get_event(s_evt_channel *ps_chan, INTS4 **ppl_buffer, INTS4 **ppl_goobuf)
Definition: f_evt.c:987
INTS2 i_used
Definition: s_bufhe.h:42
uint32_t fLmdPutBuffer(sLmdControl *pLmdControl, sMbsHeader *pHeader, uint32_t Items)
Definition: fLmd.c:257
#define GETEVT__FILE
Definition: f_evt.h:119
s_ve10_1 * ps_ve10_1
Definition: f_evt.h:83
#define GETEVT__RFIO
Definition: f_evt.h:124
INTS2 i_subtype
Definition: s_ves10_1.h:33
#define GETEVT__STREAM
Definition: f_evt.h:120
INTS4 f_evt_swap_filhe(s_bufhe *)
Definition: f_evt.c:2069
#define GETEVT__CLOSE_ERR
Definition: f_evt.h:137
INTS4 l_count
Definition: s_ve10_1.h:36
INTS4 l_io_buf_posi
Definition: f_evt.h:67
INTS4 l_evt_buf_size
Definition: f_evt.h:68
CHARS h_control
Definition: s_ves10_1.h:36
INTS4 l_server_type
Definition: f_evt.h:56
INTS4 l_buf_lmt
Definition: f_evt.h:63