~ubuntu-branches/ubuntu/utopic/esorex/utopic-proposed

« back to all changes in this revision

Viewing changes to src/er_plugin.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2011-11-25 12:08:00 UTC
  • Revision ID: package-import@ubuntu.com-20111125120800-hb8qatpoxkhv0fne
Tags: upstream-3.9.0
ImportĀ upstreamĀ versionĀ 3.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: er_plugin.c,v 1.97 2010/05/19 08:20:52 kbanse Exp $
 
2
 *
 
3
 *   This file is part of the ESO Common Pipeline Library
 
4
 *   Copyright (C) 2001-2009 European Southern Observatory
 
5
 *
 
6
 *   This program is free software; you can redistribute it and/or modify
 
7
 *   it under the terms of the GNU General Public License as published by
 
8
 *   the Free Software Foundation; either version 2 of the License, or
 
9
 *   (at your option) any later version.
 
10
 *
 
11
 *   This program is distributed in the hope that it will be useful,
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *   GNU General Public License for more details.
 
15
 *
 
16
 *   You should have received a copy of the GNU General Public License
 
17
 *   along with this program; if not, write to the Free Software
 
18
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
 
 
21
/*
 
22
 * $Author: kbanse $
 
23
 * $Date: 2010/05/19 08:20:52 $
 
24
 * $Revision: 1.97 $
 
25
 * $Name: esorex-3_9_0 $
 
26
 */
 
27
 
 
28
#ifdef HAVE_CONFIG_H
 
29
#   include <config.h>
 
30
#endif
 
31
 
 
32
#include <stdio.h>
 
33
#include <string.h>
 
34
#include <stdlib.h>
 
35
#include <ctype.h>
 
36
#include <time.h>
 
37
#include <unistd.h>
 
38
#include <errno.h>
 
39
#include <dirent.h>
 
40
#include <sys/types.h>
 
41
#include <sys/stat.h>
 
42
 
 
43
#include <wordexp.h>
 
44
 
 
45
#include <cpl_dfs.h>
 
46
#include <cpl.h>
 
47
 
 
48
#include "ltdl.h"
 
49
 
 
50
#include "er_main.h"
 
51
#include "er_help.h"
 
52
#include "er_stringarray.h"
 
53
#include "er_fileutils.h"
 
54
#include "er_paramutils.h"
 
55
#include "er_params.h"
 
56
#include "er_plugin.h"
 
57
#include "er_pluginlist.h"
 
58
#include "er_paf.h"
 
59
#include "er_stringutils.h"
 
60
 
 
61
static char trace_msg[80];
 
62
static cpl_frameset *holdme;
 
63
 
 
64
#define SIZE_A  2048
 
65
#define SIZE_B  128
 
66
#define SIZE_C  1024
 
67
#define SIZE_D  1024
 
68
 
 
69
#define Xspace(x)  (x == ' ') || (x == '\t') || (x == '\n') 
 
70
#define Xnospace(x)  (x != ' ') && (x != '\t')  && (x != '\n')
 
71
 
 
72
 
 
73
/**
 
74
 * @defgroup esorex_plugin_process EsoRex Plugin Processing Functions
 
75
 *
 
76
 * EsoRex Plugin Processing Functions
 
77
 *
 
78
 */
 
79
 
 
80
/**@{*/
 
81
/*
 
82
 
 
83
*/
 
84
 
 
85
/**********************************************************************/
 
86
/**
 
87
 * @brief  Sets the message severity level for the terminal
 
88
 * @param  param_list  A list of the command line parameters
 
89
 * @param  flag  = 1, get level for logging
 
90
 *               = 2, get level for terminal messages
 
91
 * 
 
92
 * This function takes the list of all the command line parameters, and
 
93
 * checks for the existence of one to set the terminal message reporting
 
94
 * level. If it exists, the log-level is set to the requested value.
 
95
 *
 
96
 */
 
97
/**********************************************************************/
 
98
 
 
99
cpl_msg_severity message_severity (cpl_parameterlist *param_list, int flag)
 
100
 
 
101
{
 
102
const char *msg_level_value = NULL;
 
103
 
 
104
cpl_parameter *p;
 
105
 
 
106
 
 
107
 
 
108
ER_TRACE(__func__);
 
109
 
 
110
if (param_list == NULL) return (CPL_MSG_OFF);
 
111
 
 
112
 
 
113
if (flag == 1)
 
114
   p = cpl_parameterlist_find (param_list, PACKAGE_RESOURCE ".log-level");
 
115
else
 
116
   p = cpl_parameterlist_find (param_list, PACKAGE_RESOURCE ".msg-level");
 
117
 
 
118
msg_level_value = cpl_parameter_get_string (p);
 
119
ForceNull(msg_level_value)
 
120
 
 
121
if (msg_level_value != NULL)
 
122
   {
 
123
   if (strcmp (msg_level_value, "debug") == 0)
 
124
      return CPL_MSG_DEBUG;
 
125
       
 
126
   else if (strcmp (msg_level_value, "info") == 0)
 
127
      return CPL_MSG_INFO;
 
128
       
 
129
   else if (strcmp (msg_level_value, "warning") == 0)
 
130
      return CPL_MSG_WARNING;
 
131
       
 
132
   else if (strcmp (msg_level_value, "error") == 0)
 
133
      return CPL_MSG_ERROR;
 
134
       
 
135
   else if (strcmp (msg_level_value, "off") == 0)
 
136
      return CPL_MSG_OFF;
 
137
       
 
138
   else
 
139
      {
 
140
      char  tmp[16];
 
141
 
 
142
      if (flag == 1)
 
143
         (void) strcpy(tmp,"logfile");
 
144
      else
 
145
         (void) strcpy(tmp,"terminal");
 
146
      cpl_msg_error 
 
147
          (er_func, "%s messaging level '%s' is not recognized", tmp,msg_level_value);
 
148
      }
 
149
   }
 
150
 
 
151
return (CPL_MSG_OFF);
 
152
 
 
153
}                               /* End of message_severity() */
 
154
/*
 
155
 
 
156
*/
 
157
 
 
158
int  add_size(const char *path, double *fsz)
 
159
{
 
160
struct stat buf;
 
161
 
 
162
 
 
163
if (stat(path,&buf) == -1)              /* get file structure info */
 
164
   {
 
165
   return(-1);
 
166
   }
 
167
 
 
168
*fsz += (double) buf.st_size;           /* add current files size in bytes */
 
169
 
 
170
/*
 
171
printf("file: %s has size %d bytes => total size = %13.6g\n",path,buf.st_size,*fsz); 
 
172
*/
 
173
 
 
174
return(0);
 
175
}
 
176
 
 
177
int  mysscanf (char *myline, char *path, char * tag,char * group)
 
178
{
 
179
int   iout, keep;
 
180
register int i;
 
181
 
 
182
char  cc;
 
183
char  *mpt[3];
 
184
 
 
185
 
 
186
 
 
187
mpt[0] = path;
 
188
mpt[1] = tag;
 
189
mpt[2] = group;
 
190
 
 
191
i = -1;
 
192
keep = 0;
 
193
 
 
194
for (iout=1; iout <4; iout++)
 
195
   {
 
196
   while(1)                             /* find blank space */
 
197
      {
 
198
      cc = myline[++i];
 
199
      if (cc == '\0') 
 
200
         {
 
201
         (void) strcpy(mpt[iout-1],&myline[keep]);
 
202
         return iout; 
 
203
         }
 
204
      if (Xspace(cc)) break; 
 
205
      }
 
206
 
 
207
   myline[i] = '\0';
 
208
   (void) strcpy(mpt[iout-1],&myline[keep]);
 
209
 
 
210
   if (iout == 3) return iout;          /* not more than 3 items... */
 
211
 
 
212
   while(1)                             /* find non-space char */
 
213
      {
 
214
      cc = myline[++i];
 
215
      if (cc == '\0') return iout; 
 
216
      if (Xnospace(cc)) break; 
 
217
      }
 
218
 
 
219
   keep = i;
 
220
   }
 
221
return iout;
 
222
}
 
223
/*
 
224
 
 
225
*/
 
226
 
 
227
/**********************************************************************/
 
228
/**  
 
229
 * @brief
 
230
 *   Create a new frame set from a @em set @em of @em frames file.
 
231
 * 
 
232
 * @param name  Input file path.
 
233
 * @param set   Frame set to be updated with the contents of @em name.
 
234
 * 
 
235
 * @return Pointer to the newly created frame set if @em set was @c NULL,
 
236
 *   or the updated set @em set. In case an error occurred the return value
 
237
 *   is @c NULL.
 
238
 *
 
239
 * The function reads the given input file @em filename and either, 
 
240
 * if @c NULL is passed for @em set, creates a new frame set from its
 
241
 * contents, or updates @em set, if @em set points to an already existing   
 
242
 * frame set.
 
243
 *
 
244
 * @note
 
245
 *   The current format of the @em set @em of @em frames file is as follows:
 
246
 *     - One frame description per line.
 
247
 *     - Each frame description consists of an absolute file path followed
 
248
 *       by the frame tag and, optionally, the group the frame belongs to.
 
249
 *     - Currently the only defined group tags are RAW, CALIB and PRODUCT.
 
250
 *     - The individual fields are separated by whitespace.
 
251
 *     - Blank lines are ignored.
 
252
 *     - Lines that begin with a hash (#) are treated as comment lines.
 
253
 */
 
254
/**********************************************************************/
 
255
 
 
256
cpl_frameset *er_frameset_load
 
257
(const char *name, cpl_frameset * set, int flag_check_sof_exist)
 
258
{
 
259
FILE *fp;
 
260
 
 
261
char  *line, *path, *group, *tag, *xpath;
 
262
char  estr[240];
 
263
register char  cc;
 
264
 
 
265
int line_number = 0, created = 0;
 
266
int  ii, mlen, n, no_comnt;
 
267
 
 
268
int load_sucesfull = 1;
 
269
 
 
270
cpl_frame *frame = NULL;
 
271
 
 
272
cpl_msg_severity msg_level;
 
273
 
 
274
 
 
275
 
 
276
ER_TRACE(__func__);
 
277
 
 
278
 
 
279
if (name == NULL)
 
280
   {
 
281
   cpl_msg_error (er_func, "No SOF name provided, when one was expected.");
 
282
   return NULL;
 
283
   }
 
284
 
 
285
if (!(fp = fopen (name, "r")))
 
286
   {
 
287
   cpl_msg_error (er_func, "Unable to open SOF file '%s'", name);
 
288
   return NULL;
 
289
   }
 
290
 
 
291
/* If set was NULL, create a new frame set that we can fill. */
 
292
 
 
293
if (!set)
 
294
   {
 
295
   set = cpl_frameset_new ();
 
296
   created = 1;
 
297
 
 
298
   if (ER_TRACEFLAG)
 
299
      {
 
300
      (void) sprintf(trace_msg,"new frameset created for SOF %48.48s",name);
 
301
      ER_TRACEX(trace_msg)
 
302
      }
 
303
   }
 
304
 
 
305
 
 
306
line = (char *) cpl_calloc((size_t) SIZE_C, (size_t) 1);
 
307
path = (char *) cpl_calloc((size_t) SIZE_C, (size_t) 1);
 
308
xpath = (char *) cpl_calloc((size_t) SIZE_C, (size_t) 1);
 
309
group = (char *) cpl_calloc((size_t) SIZE_C, (size_t) 1);
 
310
tag = (char *) cpl_calloc((size_t) SIZE_C, (size_t) 1);
 
311
 
 
312
msg_level = cpl_msg_get_level();          /* get the message level */
 
313
 
 
314
 
 
315
/* Loop over all the lines in the set-of-frames file */
 
316
 
 
317
while( fgets (line, (SIZE_C-1), fp))
 
318
   {
 
319
   line_number++;
 
320
   no_comnt = 0;                /* check for comments + empty records */
 
321
   line[SIZE_C-1] = '\0';
 
322
   mlen = (int)strlen(line);
 
323
 
 
324
  /* printf("line no. %d has length %d\n",line_number,mlen);  */
 
325
 
 
326
   for (ii=0; ii<mlen; ii++)
 
327
      {
 
328
      cc = line[ii];
 
329
      if (Xnospace(cc))                 /* look for 1st non-blank char */
 
330
         {
 
331
         if (cc != '#') no_comnt = 1;   /* # indicates comment line */
 
332
         break;
 
333
         }
 
334
      }
 
335
 
 
336
   if (no_comnt == 1)
 
337
      {
 
338
      n = mysscanf (&line[ii], xpath, tag, group);        /* split up line */
 
339
 
 
340
      if (ER_TRACEFLAG) 
 
341
          {
 
342
         printf("mysscanf returns %d\n",n);
 
343
         printf("path: %s\ntag: %s, group: %s\n",xpath,tag,group);
 
344
         }
 
345
 
 
346
      if (n >= 1)
 
347
         {
 
348
         if (xpath[0] == '$')           /* check for env. variables */
 
349
            {
 
350
            int   wer;
 
351
            char  *wp;
 
352
            wordexp_t  wresult;
 
353
 
 
354
            wer = wordexp(xpath,&wresult,0);    /* expand the $VAR/file */
 
355
            if (wer == 0)
 
356
               {
 
357
               wp = *wresult.we_wordv;             /* use 1st and only result word */
 
358
               if (wp != NULL)
 
359
                  (void) strcpy(path,wp);
 
360
               else
 
361
                  wer = 1;
 
362
               wordfree(&wresult);
 
363
               }
 
364
 
 
365
            if (wer != 0)
 
366
               {
 
367
               (void) snprintf(estr,(size_t)238,"Expansion of %s failed...",xpath);
 
368
               cpl_msg_error (er_func, estr);
 
369
               goto dealloc;
 
370
               }
 
371
            }
 
372
         else
 
373
            {
 
374
            (void) strcpy(path,xpath);
 
375
            }
 
376
 
 
377
         if (!(fileutils_file_exists (path)))   /* Ensure that the path exists */
 
378
            {                                   /* Invalid file name? */
 
379
 
 
380
            /* allow a single FITS file instead of SOF */
 
381
 
 
382
            if (strncmp(path,"SIMPLE",6) == 0)
 
383
               {                                /* it's a single FITS file */
 
384
               n = (int) strlen(name);
 
385
               if (n > (SIZE_C-1)) 
 
386
                  {
 
387
                  n = SIZE_C - 1;
 
388
                  (void) strncpy(path,name,(size_t)n);
 
389
                  path[n] = '\0';
 
390
                  cpl_msg_warning (er_func, "FITS file name truncated to %d chars", n);
 
391
                  }
 
392
               else
 
393
                  {
 
394
                  (void) strcpy(path,name);
 
395
                  }
 
396
               (void) strcpy(tag,"COMMAND_LINE");
 
397
               n = -1;                          /* to remember that later on */
 
398
               }
 
399
            else                                /* Yes, bad filename */ 
 
400
               {
 
401
               if (flag_check_sof_exist)
 
402
                   {
 
403
                   cpl_msg_error (er_func,
 
404
                        "The input data file '%s', specified on line %d of the SOF "
 
405
                        "file, '%s', does not exist...", path, line_number, name);
 
406
                   load_sucesfull = 0;
 
407
                   }
 
408
               else
 
409
                  {
 
410
                  cpl_msg_debug (er_func,
 
411
                       "The input data file '%s', specified on line %d of the SOF "
 
412
                       "file, '%s', does not exist...", path, line_number, name);
 
413
                  }
 
414
               }
 
415
            }
 
416
 
 
417
         frame = cpl_frame_new ();                      /* allocate a new frame */
 
418
         cpl_frame_set_filename (frame, path);          /* and provide the filename */
 
419
 
 
420
         if (n == 1)                            /* no tag in SOF line */
 
421
            cpl_frame_set_tag (frame, "");
 
422
         else
 
423
            cpl_frame_set_tag (frame, tag);
 
424
 
 
425
         /* Set the group component of the frame (or set a default) */
 
426
         if (n > 2)                             /* so, omitted for single FITS file */
 
427
            {
 
428
            cpl_frame_group grp;
 
429
 
 
430
            if (!strcmp (group, CPL_FRAME_GROUP_RAW_ID))
 
431
               grp = CPL_FRAME_GROUP_RAW;
 
432
            else
 
433
               {
 
434
               if (!strcmp (group, CPL_FRAME_GROUP_CALIB_ID))
 
435
                  grp = CPL_FRAME_GROUP_CALIB;
 
436
               else
 
437
                  {
 
438
                  if (!strcmp (group, CPL_FRAME_GROUP_PRODUCT_ID))
 
439
                     grp = CPL_FRAME_GROUP_PRODUCT;
 
440
                  else
 
441
                     grp = CPL_FRAME_GROUP_NONE;
 
442
                  }
 
443
               }
 
444
 
 
445
            cpl_frame_set_group (frame, grp);
 
446
            }
 
447
 
 
448
         cpl_frameset_insert (set, frame);
 
449
         if (n == -1) goto dealloc;                     /* was single FITS file */
 
450
         }
 
451
 
 
452
      else
 
453
         {                      /* Invalid frame description. */
 
454
         if (msg_level == CPL_MSG_DEBUG)
 
455
            {
 
456
            cpl_msg_debug (er_func,
 
457
                 "Invalid frame description '%s', specified on line %d of the SOF "
 
458
                 "file, '%s'.", path, line_number, name);
 
459
            } 
 
460
         } 
 
461
      }   
 
462
 
 
463
   }                            /* End of while() */
 
464
 
 
465
 
 
466
dealloc:                                /* Deallocate resources */
 
467
cpl_free (line);
 
468
cpl_free (path);
 
469
cpl_free (xpath);
 
470
cpl_free (group);
 
471
cpl_free (tag);
 
472
fclose (fp);
 
473
 
 
474
 
 
475
if (ER_TRACEFLAG)
 
476
   {
 
477
   const char  *gp, *tp;
 
478
 
 
479
   ii = cpl_frameset_get_size(set);
 
480
   (void) sprintf(trace_msg,"size of frameset = %d",ii);
 
481
   ER_TRACEX(trace_msg)
 
482
   if (ii > 0)
 
483
      {
 
484
      frame = cpl_frameset_get_first(set);
 
485
      while (frame != NULL)
 
486
         {
 
487
         gp = cpl_frame_get_filename(frame);
 
488
         tp = cpl_frame_get_tag(frame);
 
489
         (void) sprintf(trace_msg,"name: %60.60s, %s",gp,tp);
 
490
         ER_TRACEX(trace_msg)
 
491
   
 
492
         frame = cpl_frameset_get_next(set);
 
493
         }
 
494
      }
 
495
   }
 
496
 
 
497
/* Check if the loading was sucesfully */
 
498
if(!load_sucesfull)
 
499
    {
 
500
    if(created)
 
501
        cpl_frameset_delete(set);
 
502
    return NULL;
 
503
    }
 
504
 
 
505
/* Return the pointer to the frameset */
 
506
return set;
 
507
 
 
508
}                               /* End of er_frameset_load() */
 
509
/*
 
510
 
 
511
*/
 
512
 
 
513
/**********************************************************************/
 
514
/**
 
515
 * @brief Process the Plugin
 
516
 *
 
517
 * @param frameset         Set of frames to be moved
 
518
 * @param output_prefix    Character string with the prefix to be used
 
519
 *
 
520
 * @returns 0 
 
521
 *
 
522
 * This function updates the DATAMD5 keyword for any product already created
 
523
 * in the case a recipe returned an error status (failed)
 
524
 *
 
525
 */
 
526
/**********************************************************************/
 
527
 
 
528
int upda_products (cpl_frameset * frameset)
 
529
 
 
530
 
 
531
{
 
532
cpl_error_code reto;
 
533
 
 
534
 
 
535
 
 
536
ER_TRACE(__func__);
 
537
 
 
538
 
 
539
reto = cpl_dfs_update_product_header(frameset);
 
540
if (reto != CPL_ERROR_NONE)
 
541
   {
 
542
   cpl_msg_error (er_func, "could not update the product header...");
 
543
   }
 
544
 
 
545
return 0;
 
546
}
 
547
 
 
548
/*
 
549
 
 
550
*/
 
551
 
 
552
/**********************************************************************/
 
553
/**
 
554
 * @brief Process the Plugin
 
555
 *
 
556
 * @param plugin_name       Name of Plugin to process
 
557
 * @param frameset         Set of frames to be moved
 
558
 * @param output_directory Character string with the output directory
 
559
 * @param output_prefix    Character string with the prefix to be used
 
560
 * @param flag_noprefix    Boolean indicating if prefix/number system 
 
561
 *                         should be ignored, in which case the recipe
 
562
 *                         output filenames are used.
 
563
 * @param flag_readonly    Boolean indicating whether or not output files
 
564
 *                         should be set to oct(444) for Paranal.
 
565
 *
 
566
 * @returns 0 if successfull, !=0 otherwise
 
567
 *
 
568
 * This function moves the products from the temporary working
 
569
 * directory, as used by the recipe, to the output directory requested 
 
570
 * by the user.
 
571
 *
 
572
 * This function also sets the PIPEFILE keyword in the FITS header if
 
573
 * appropriate. If this cannot be done, or the FITS card has 
 
574
 * insufficient size to then a warning is printed.
 
575
 */
 
576
/**********************************************************************/
 
577
 
 
578
/* int move_products (char * plugin_name, */
 
579
int move_products (cpl_frameset * frameset,
 
580
             char * output_directory,
 
581
             char * output_prefix,
 
582
             char * link_directory,
 
583
             int flag_noprefix, int flag_nolink, int flag_readonly)
 
584
 
 
585
{
 
586
cpl_frame *frame = NULL;                /* The current frame being processed */
 
587
 
 
588
int filenum = 0;                        /* Number of the output product */
 
589
int ii, move_err;               
 
590
 
 
591
const char *input_name = NULL;          /* Name of the input file */
 
592
const char *output_dir = NULL;          /* Name of the output directory */
 
593
const char *link_dir = NULL;            /* Name of the symbolic link dir. */
 
594
const char *cpp;
 
595
 
 
596
char *output_name = NULL;               /* Name of the output file */
 
597
char *suffix = NULL;                    /* The file suffix */
 
598
char current_dir[FILEMAX];         /* Name of current (work) directory */
 
599
 
 
600
 
 
601
ER_TRACE(__func__);
 
602
 
 
603
 
 
604
/* allocate space */
 
605
 
 
606
output_name = (char *) cpl_malloc((size_t) SIZE_A);
 
607
 
 
608
current_dir[0] = '\0';
 
609
 
 
610
 
 
611
/* Start by getting the "cleaned" output directory name */
 
612
 
 
613
output_dir = 
 
614
   er_fileutils_dot_replace (er_fileutils_tilde_replace (output_directory));
 
615
link_dir = er_fileutils_tilde_replace (link_directory);
 
616
 
 
617
if (link_dir == NULL)
 
618
   {
 
619
   cpl_msg_warning (er_func, "missing directory for symbolic link ignored...");
 
620
   }
 
621
 
 
622
/* Determine the name of the current directory */
 
623
 
 
624
(void) getcwd (current_dir, (size_t) FILEMAX);
 
625
 
 
626
if (ER_TRACEFLAG)
 
627
   {
 
628
   (void) sprintf(trace_msg, "output_directory = %s>",output_directory);
 
629
   ER_TRACEX(trace_msg)
 
630
   (void) sprintf(trace_msg, "output_dir = %s>",output_dir);
 
631
   ER_TRACEX(trace_msg)
 
632
   (void) sprintf(trace_msg, "current_dir = %s>",current_dir);
 
633
   ER_TRACEX(trace_msg)
 
634
   (void) sprintf(trace_msg, "link_dir = %s>",link_dir);
 
635
   ER_TRACEX(trace_msg)
 
636
   }
 
637
 
 
638
ii = upda_products(frameset);           /* MD5 errors ignored at the moment... */
 
639
holdme = frameset;
 
640
 
 
641
 
 
642
/* Loop through the frameset */
 
643
 
 
644
move_err = 0;                           /* init possible error from moving files */
 
645
frame = cpl_frameset_get_first (frameset);
 
646
while (frame != NULL)
 
647
   {
 
648
   if (cpl_frame_get_group (frame) == CPL_FRAME_GROUP_PRODUCT)
 
649
      {
 
650
 
 
651
      /* Get the filename as specified by the frame */
 
652
      input_name = cpl_frame_get_filename (frame);
 
653
 
 
654
      /* Temporarily remove the suffix (find the first '.' and replace it) */
 
655
      suffix = strrchr (input_name, '.');
 
656
      if (suffix) *suffix = '\0';
 
657
 
 
658
      /* Generate the output filename (keep original if this is suppressed) */
 
659
      if (flag_noprefix != 0)
 
660
         {
 
661
         (void) strcpy (output_name, input_name);
 
662
         }
 
663
      else
 
664
         {
 
665
         (void) sprintf (output_name, "%s_%04d", output_prefix, filenum);
 
666
         }
 
667
 
 
668
      /* Now restore the suffix (note difference between suffix and *suffix) */
 
669
      if (suffix) *suffix = '.';
 
670
      if (suffix) (void) strcat (output_name, suffix);
 
671
 
 
672
      /* Check if any move-command is required */
 
673
 
 
674
         cpp = cpl_frame_get_filename (frame);
 
675
         if (cpp == NULL)
 
676
            {
 
677
            cpl_msg_error (er_func, "cpl_frame_get_filename failed");
 
678
            move_err = 63;
 
679
            goto report_products;
 
680
            }
 
681
 
 
682
      if (((strcmp (current_dir, output_dir) == 0) ||
 
683
           (strcmp (".", output_directory) == 0)) &&                    /* same dir */
 
684
          (strcmp (cpp, output_name) == 0))     /* same file */
 
685
         {
 
686
         /* No move. Report that the product has been created */
 
687
         cpl_msg_info (er_func, "Created product %s (in place)", output_name);
 
688
         }
 
689
 
 
690
      else
 
691
         {                                              /* Create the full path */
 
692
         char  tmp[SIZE_A];
 
693
 
 
694
         if (ER_TRACEFLAG)
 
695
            {
 
696
            (void) sprintf(trace_msg, "input name = %s>",cpp);
 
697
            ER_TRACEX(trace_msg)
 
698
            (void) sprintf(trace_msg, "output_name = %s>",output_name);
 
699
            ER_TRACEX(trace_msg)
 
700
            }
 
701
 
 
702
         if (output_name[0] == '\0')
 
703
            (void) strcpy(tmp,"dummy_output");
 
704
         else
 
705
            (void) strcpy(tmp,output_name);
 
706
 
 
707
         (void) strcpy (output_name, output_dir);
 
708
         ii = (int) strlen(output_name) - 1;
 
709
         if (output_name[ii] != '/') output_name[++ii] = '/';
 
710
         (void) strcpy (&output_name[ii+1],tmp);
 
711
 
 
712
         if (ER_TRACEFLAG)
 
713
            {
 
714
            (void) sprintf(trace_msg, "input = %s",cpp);
 
715
            ER_TRACEX(trace_msg)
 
716
            (void) sprintf(trace_msg, "final output_name = %s",output_name);
 
717
            ER_TRACEX(trace_msg)
 
718
            }
 
719
             
 
720
/*  
 
721
         rename() has problems:
 
722
         a) it happily overwrites read-only files...
 
723
         b) it does not work across different partitions (that's documented)
 
724
         so we cannot simply do:
 
725
 
 
726
         ret_val = rename (cpp, output_name);
 
727
*/
 
728
 
 
729
         ii = fileutils_move (cpp, output_name);   
 
730
         if (ii != 0)
 
731
            {                           /* also that failed - so, no way... */
 
732
            if (ii == 99)
 
733
               {                        /* different name, but same inode ... */
 
734
               cpl_msg_info (er_func, "Created product %s (in place)", output_name);
 
735
               }
 
736
            else
 
737
               {
 
738
               cpl_msg_error (er_func, "Unable to move product file to final "
 
739
                        "output location (%s)", output_name);
 
740
               move_err = 61;
 
741
               (void) sprintf(trace_msg, "fileutils_move returned: %d",ii);
 
742
               ER_TRACEX(trace_msg)
 
743
               goto report_products;
 
744
               }
 
745
            }
 
746
         else
 
747
            {
 
748
            /* Update the name in the sof */ 
 
749
            cpl_frame_set_filename(frame, output_name);
 
750
            
 
751
            cpl_msg_info (er_func, "Created product %s", output_name);
 
752
            }
 
753
         }
 
754
 
 
755
      /* If required, generate the symbolic links for the archive system */
 
756
      if (flag_nolink == 0) 
 
757
         {
 
758
         if (er_fileutils_link (link_dir, output_name) != EXIT_SUCCESS)
 
759
            {
 
760
            cpl_msg_error (er_func,
 
761
                           "Unable to create symbolic link for %s in "
 
762
                           "directory %s", output_name, link_dir);
 
763
            move_err = 62;
 
764
            goto report_products;
 
765
            }
 
766
         }
 
767
 
 
768
      /* If required, lock the file permissions to read-only */
 
769
      if (flag_readonly != 0) chmod (output_name, 0444);
 
770
 
 
771
      filenum++;                /* Increment the product number */
 
772
      }                   
 
773
 
 
774
   /* Get the next frame in the list */
 
775
 
 
776
   frame = cpl_frameset_get_next (frameset);
 
777
   }                            /* End of (while) loop through all frames in frameset */
 
778
 
 
779
 
 
780
/* Report how many products were created (getting the plural right!) */
 
781
 
 
782
report_products:
 
783
cpl_msg_info (er_func, "%d product%s created", filenum, (filenum == 1) ? "" : "s");
 
784
cpl_free (output_name);                         /* free the allocated memory */
 
785
 
 
786
return move_err;
 
787
}                            
 
788
/*
 
789
 
 
790
*/
 
791
 
 
792
/**********************************************************************/
 
793
/**
 
794
 * @brief Process the Plugin
 
795
 *
 
796
 * @param caller_parameters Caller parameters
 
797
 * @param plugin_name       Name of Plugin to process
 
798
 * @param sof_filename_list List of strings with SOF filenames
 
799
 * @param argc              Count of remaining arguments from cmdl
 
800
 * @param argv              Handle to remaining arguments from cmdl
 
801
 *
 
802
 * @returns 0 if successfull, !=0 otherwise
 
803
 *
 
804
 * Processes a Plugin.
 
805
 * Write more here...
 
806
 *
 
807
 */
 
808
/**********************************************************************/
 
809
 
 
810
int plugin_process_plugin (cpl_parameterlist * caller_parameters,
 
811
            char * plugin_name,
 
812
            er_stringarray_t * sof_filename_list,
 
813
            int argc, char * argv[])
 
814
 
 
815
{
 
816
const char *val_output_mask,
 
817
           *val_link_dir, 
 
818
           *val_log_dir, 
 
819
           *val_log_file,
 
820
           *val_output_dir, *cpp;
 
821
const char *val_string_tmp = NULL;
 
822
const char *cdescr;
 
823
 
 
824
const char *val_paf_config;
 
825
 
 
826
char **val_recipe_dirs = NULL,
 
827
     *log_file_name_full = NULL;
 
828
 
 
829
char *cptr, 
 
830
     *plugin_conf_file_global=NULL,
 
831
     *plugin_conf_file_local=NULL,
 
832
     library_path[MAXSTRLENCONF];
 
833
 
 
834
char *val_create = NULL, *help_str = NULL;
 
835
 
 
836
int e_code = 0, e_code2 = 0, i = 0, ii = 0;
 
837
int countr, n, sz;
 
838
 
 
839
int flag_time_plugin = 0, 
 
840
    flag_plugin_found = 0,
 
841
    flag_help = 0, 
 
842
    flag_man_page = 0,
 
843
    flag_create_config = 0, 
 
844
    flag_params = 0,
 
845
    flag_readonly = 0, 
 
846
    flag_noprefix = 0, 
 
847
    f_val_unload_plugin = 1,            /* default is TRUE */
 
848
    flag_nolink = 0;
 
849
unsigned long  uil;
 
850
 
 
851
double plugin_time_start=0.0, plugin_time_end=0.0;
 
852
 
 
853
cpl_msg_severity msg_level = CPL_MSG_ERROR,
 
854
                 msg_sev_logfile, msg_sev_terminal;
 
855
cpl_parameter *p = NULL;
 
856
cpl_plugin *tplugin = NULL;
 
857
lt_dlhandle module = NULL;
 
858
cpl_plugin_func plugin_func_init = NULL,
 
859
                plugin_func_exec = NULL, 
 
860
                plugin_func_deinit = NULL;
 
861
cpl_recipe *trecipe = NULL;
 
862
cpl_recipe2 *t2recipe = NULL;
 
863
 
 
864
cpl_error_code log_err;
 
865
 
 
866
er_stringarray_t *list_of_pllib_names = NULL;
 
867
 
 
868
 
 
869
 
 
870
 
 
871
ER_TRACE(__func__);
 
872
 
 
873
 
 
874
library_path[0] = '\0';
 
875
 
 
876
/* check, if we want to take times */
 
877
p = cpl_parameterlist_find (caller_parameters, PACKAGE_RESOURCE ".time");
 
878
flag_time_plugin = cpl_parameter_get_bool (p);
 
879
if (flag_time_plugin != 0)                      /* --time option */
 
880
   {
 
881
   plugin_time_start = cpl_test_get_walltime();
 
882
   }
 
883
 
 
884
msg_sev_logfile = message_severity (caller_parameters,1);
 
885
msg_sev_terminal = message_severity (caller_parameters,2);
 
886
 
 
887
p = cpl_parameterlist_find (caller_parameters, PACKAGE_RESOURCE ".log-dir");
 
888
val_log_dir = cpl_parameter_get_string (p);
 
889
ForceNull(val_log_dir)
 
890
 
 
891
p = cpl_parameterlist_find (caller_parameters, PACKAGE_RESOURCE ".log-file");
 
892
val_log_file = cpl_parameter_get_string (p); 
 
893
ForceNull(val_log_file)
 
894
log_file_name_full = 
 
895
    fileutils_create_fqfname ((char *) val_log_dir, (char *) val_log_file);
 
896
 
 
897
 
 
898
/* CPL messaging should already be active, but reset the defaults */
 
899
 
 
900
cpl_msg_set_domain (PACKAGE);
 
901
cpl_msg_set_time_off ();
 
902
cpl_msg_set_domain_on ();
 
903
cpl_msg_set_component_off ();
 
904
log_err = cpl_msg_set_log_level (msg_sev_logfile);
 
905
if (log_err != CPL_ERROR_NONE)
 
906
   {
 
907
   (void) printf ("WARNING : EsoRex is unable to establish the message log "
 
908
                  "(Error = %d)\n", log_err);
 
909
   (void) printf ("          (Check write permission for temporary files.)\n");
 
910
   }
 
911
 
 
912
/* always save used library versions in logfile (only) */
 
913
 
 
914
cpl_msg_set_level (CPL_MSG_OFF);
 
915
cdescr = cpl_get_description(CPL_DESCRIPTION_DEFAULT);
 
916
 
 
917
(void) cpl_msg_info(er_func,"Libraries used: %s\n",cdescr);
 
918
cpl_msg_set_level (msg_sev_terminal);
 
919
 
 
920
 
 
921
/* look for recipe directory */
 
922
 
 
923
p = cpl_parameterlist_find (caller_parameters, PACKAGE_RESOURCE ".recipe-dir");
 
924
val_string_tmp = cpl_parameter_get_string (p);
 
925
ForceNull(val_string_tmp)
 
926
if (val_string_tmp != NULL)
 
927
   val_recipe_dirs = cx_strsplit (val_string_tmp, ":", -1);
 
928
 
 
929
if (ER_TRACEFLAG)
 
930
   {
 
931
   (void) sprintf(trace_msg,"plugin_process_plugin: recipe-dir = %s",val_string_tmp);
 
932
   ER_TRACEX(trace_msg)
 
933
   }
 
934
 
 
935
 
 
936
/* get the plugin  */
 
937
 
 
938
if (ER_TRACEFLAG)
 
939
   {
 
940
   (void) sprintf(trace_msg,"plugin_name = %s]",plugin_name);
 
941
   ER_TRACEX(trace_msg)
 
942
   }
 
943
 
 
944
if ((plugin_name[0] == ' ') || (plugin_name[0] == '\0'))
 
945
   sz = 0;
 
946
else
 
947
   sz = 1;
 
948
 
 
949
if ((val_recipe_dirs != NULL) && (sz > 0))
 
950
   {
 
951
   list_of_pllib_names = er_pluginlist_create_list (val_recipe_dirs);
 
952
 
 
953
   sz = er_pluginlist_get_libpath (list_of_pllib_names, plugin_name, library_path);
 
954
   if (sz > 0)
 
955
      {
 
956
      tplugin = er_pluginlist_get_plugin (library_path, plugin_name, &module);
 
957
      if (tplugin != NULL)
 
958
         {
 
959
         flag_plugin_found = 1;
 
960
         goto next_step;
 
961
         }
 
962
      }
 
963
   cpl_msg_error (er_func, "Unable to find recipe '%s'."
 
964
                 " Check that the recipe is in the path specified by the"
 
965
                 " '--recipe-dir' option.", plugin_name);
 
966
   e_code = CPL_ERROR_INCOMPATIBLE_INPUT;
 
967
   goto cleanup;                                /* we cannot do anything */
 
968
   }
 
969
 
 
970
 
 
971
/* ------------------------------------- */
 
972
/* here we process the plugin (if found) */
 
973
/* ------------------------------------- */
 
974
 
 
975
next_step:
 
976
 
 
977
/*
 
978
printf("none = %d, recipe = %d, recipe_v2 = %d\n",CPL_PLUGIN_TYPE_NONE,CPL_PLUGIN_TYPE_RECIPE,CPL_PLUGIN_TYPE_RECIPE_V2);
 
979
*/
 
980
 
 
981
 
 
982
uil = cpl_plugin_get_type(tplugin);
 
983
 
 
984
/*  initialize the plugin/recipe structure: trecipe->parameters is handled by plugin  */
 
985
 
 
986
if (uil < 2)
 
987
   {
 
988
   n = sizeof (cpl_recipe);
 
989
   trecipe = (cpl_recipe *) cpl_calloc (1,(size_t) n);
 
990
   }
 
991
else
 
992
   {
 
993
   n = sizeof (cpl_recipe2);
 
994
   t2recipe = (cpl_recipe2 *) cpl_calloc (1,(size_t) n);
 
995
   trecipe = &t2recipe->base;
 
996
   }
 
997
 
 
998
/*
 
999
printf
 
1000
("trying to support version 2 recipes ...\nplugin type: %d, size of recipe = %d\n",
 
1001
uil,n);
 
1002
*/
 
1003
 
 
1004
trecipe->frames = cpl_frameset_new (); 
 
1005
 
 
1006
 
 
1007
if (flag_plugin_found != 0)                     /*  = TRUE */
 
1008
   {
 
1009
   cpl_plugin_copy ((cpl_plugin *) & trecipe->interface, tplugin);
 
1010
 
 
1011
 
 
1012
   /*  Run Plugin Initialization...  */
 
1013
 
 
1014
   plugin_func_init = cpl_plugin_get_init ((cpl_plugin *) trecipe);
 
1015
   if (plugin_func_init != NULL)
 
1016
      {
 
1017
      cpl_msg_set_domain (cpl_plugin_get_name ((cpl_plugin *) trecipe));
 
1018
      e_code = plugin_func_init ((cpl_plugin *) trecipe);
 
1019
      cpl_msg_set_domain (PACKAGE);
 
1020
      if (e_code != 0) 
 
1021
         {
 
1022
         cpl_msg_error (er_func, "Init of recipe failed...");
 
1023
         goto plugin_deinit;
 
1024
         }
 
1025
      }
 
1026
 
 
1027
   /* loop through all parameters in the list */
 
1028
 
 
1029
   p = cpl_parameterlist_get_first (trecipe->parameters);
 
1030
   while (p != NULL)
 
1031
      {                                                 /* Set the tag */
 
1032
      char recip_def[] = "recipe default";
 
1033
      char  *myptr;
 
1034
 
 
1035
      myptr = recip_def;
 
1036
      er_manage_sources(1,cpl_parameter_get_name(p),&myptr);
 
1037
 
 
1038
      /* Get the next parameter in the list */
 
1039
      p = cpl_parameterlist_get_next (trecipe->parameters);
 
1040
      }
 
1041
 
 
1042
 
 
1043
   cpp = cpl_plugin_get_name ((cpl_plugin *) trecipe);
 
1044
 
 
1045
#ifdef USE_GLOBAL_PLUGIN_RC /* Parse any global plugin configuration file */
 
1046
 
 
1047
   cptr = getenv ("HOME");
 
1048
   if (cptr == NULL)
 
1049
      n = 0;
 
1050
   else
 
1051
      n = (int) strlen(cptr);
 
1052
   if (cpp != NULL) n += (int)strlen(cpp);
 
1053
 
 
1054
   n = n + ((int)strlen(cpp)) +
 
1055
       ((int) strlen(GLOBAL_RC_DIR)) + ((int) strlen(GLOBAL_RC_EXTENSION));
 
1056
   n += 4;                                      /* for the additionial const chars */
 
1057
 
 
1058
   plugin_conf_file_global = (char *) cpl_malloc((size_t) n);
 
1059
   if ( plugin_conf_file_global == NULL)
 
1060
      {
 
1061
      cpl_msg_error (er_func, 
 
1062
                     "Could not allocate %d bytes for plugin_conf_file_global",n);
 
1063
      e_code = CPL_ERROR_ILLEGAL_OUTPUT;
 
1064
      goto plugin_deinit;
 
1065
      }
 
1066
 
 
1067
 
 
1068
   if (cptr == NULL)
 
1069
      (void) strcpy(plugin_conf_file_global, "/");
 
1070
   else
 
1071
      {
 
1072
      (void) strcpy(plugin_conf_file_global,cptr);
 
1073
      (void) strcat(plugin_conf_file_global, "/");
 
1074
      }
 
1075
   (void) strcat(plugin_conf_file_global, GLOBAL_RC_DIR);
 
1076
   (void) strcat(plugin_conf_file_global, "/");
 
1077
   if (cpp != NULL) (void) strcat(plugin_conf_file_global, cpp);
 
1078
   (void) strcat(plugin_conf_file_global, GLOBAL_RC_EXTENSION);
 
1079
 
 
1080
   if (e_code == 0)
 
1081
      {
 
1082
      if (fileutils_file_exists(plugin_conf_file_global) != 0)
 
1083
         {
 
1084
         e_code = params_parse_config_file (trecipe->parameters,
 
1085
                                            plugin_conf_file_global);
 
1086
         if (e_code != 0) goto plugin_deinit;
 
1087
         }
 
1088
      }
 
1089
#endif
 
1090
 
 
1091
#ifdef USE_LOCAL_PLUGIN_RC /* Parse any local plugin configuration file */
 
1092
 
 
1093
   if (cpp != NULL) 
 
1094
      n = (int)strlen(cpp);
 
1095
   else
 
1096
      n = 0;
 
1097
   n = n + ((int) strlen(LOCAL_RC_PATH)) + ((int) strlen(GLOBAL_RC_EXTENSION));
 
1098
   n += 4;                                 /* for the additionial const chars */
 
1099
 
 
1100
   plugin_conf_file_local = (char *) cpl_malloc((size_t) n);
 
1101
   if ( plugin_conf_file_local == NULL)
 
1102
      {
 
1103
      cpl_msg_error (er_func,
 
1104
                     "Could not allocate %d bytes for plugin_conf_file_local",n);
 
1105
      e_code = CPL_ERROR_ILLEGAL_OUTPUT;
 
1106
      goto plugin_deinit;
 
1107
      }
 
1108
 
 
1109
   void) strcpy(plugin_conf_file_local,LOCAL_RC_PATH);
 
1110
   (void) strcat(plugin_conf_file_local, "/");
 
1111
   if (cpp != NULL) (void) strcat(plugin_conf_file_local, cpp);
 
1112
   (void) strcat(plugin_conf_file_local, GLOBAL_RC_EXTENSION);
 
1113
 
 
1114
   if (e_code == 0)
 
1115
      {
 
1116
      if (fileutils_file_exists(plugin_conf_file_local) != 0)
 
1117
         {
 
1118
         e_code = params_parse_config_file (trecipe->parameters,
 
1119
                                            plugin_conf_file_local);
 
1120
         if (e_code != 0) goto plugin_deinit;
 
1121
         }
 
1122
      }
 
1123
#endif
 
1124
 
 
1125
   /* Parse any specifically specified plugin configuration file */
 
1126
   if (e_code == 0)
 
1127
      {
 
1128
      p = cpl_parameterlist_find (caller_parameters,
 
1129
                                  PACKAGE_RESOURCE ".recipe-config");
 
1130
      val_string_tmp = cpl_parameter_get_string (p);
 
1131
 
 
1132
      if ((val_string_tmp != (char *) 0) && (strlen (val_string_tmp) > 0))
 
1133
         e_code = params_parse_config_file (trecipe->parameters, val_string_tmp);
 
1134
 
 
1135
      }
 
1136
 
 
1137
   if (e_code == 0)
 
1138
      {
 
1139
      char *str_tmp = NULL;
 
1140
 
 
1141
      e_code = params_parse_config_commandline 
 
1142
                      (trecipe->parameters, plugin_name,
 
1143
                       sof_filename_list, argc, argv, 0);
 
1144
      if (e_code != 0) goto plugin_deinit;
 
1145
 
 
1146
      params_parse_config_postprocess (trecipe->parameters);
 
1147
      ii = er_stringarray_size(sof_filename_list);
 
1148
 
 
1149
      for (i=0; i<ii; i++)
 
1150
         {
 
1151
         int flag_check_sof_exist;
 
1152
         /* Get the check input files parameter */
 
1153
         p = cpl_parameterlist_find (caller_parameters,
 
1154
                 PACKAGE_RESOURCE ".check-sof-exist");
 
1155
         flag_check_sof_exist = cpl_parameter_get_bool (p);
 
1156
 
 
1157
         str_tmp = er_stringarray_get (sof_filename_list, i);
 
1158
         if (er_frameset_load (str_tmp, trecipe->frames, flag_check_sof_exist) == NULL)
 
1159
            {
 
1160
            cpl_msg_error (er_func, "Problem occurred loading frameset "
 
1161
                           "from the SOF file '%s'.\n"
 
1162
                           "If you want to ignore errors of missing files in "
 
1163
                           "the sof, set '--check-sof-exist=false'.", str_tmp);
 
1164
 
 
1165
            e_code = CPL_ERROR_FILE_NOT_FOUND;
 
1166
            }
 
1167
         }
 
1168
      }
 
1169
 
 
1170
   /* Check if we want to create the plugin (recipe) configuration file */
 
1171
 
 
1172
   p = cpl_parameterlist_find (caller_parameters, PACKAGE_RESOURCE ".create-config");
 
1173
   if (cpl_parameter_get_default_flag (p))
 
1174
      {
 
1175
      val_create = cpl_parameter_get_string (p);
 
1176
      ForceNull(val_create)
 
1177
      if (val_create != NULL)                      /* --create-config=bla.bla  found */
 
1178
         {                 /* handle FALSE and TRUE from boolean history of this param */
 
1179
         if (strcmp(val_create,"TRUE") == 0)
 
1180
            {
 
1181
            flag_create_config = 1;
 
1182
            }
 
1183
         else if (strcmp(val_create,"FALSE") != 0)
 
1184
            {
 
1185
            flag_create_config = 11;             /* indicates filename is given */
 
1186
            }
 
1187
         }
 
1188
      else                                 /* --create-config  is interpreted as ...=TRUE */
 
1189
         {
 
1190
         flag_create_config = 1;
 
1191
         }
 
1192
      }
 
1193
   if (flag_create_config == 11)
 
1194
      {
 
1195
      er_help_create_config (flag_create_config,val_create, caller_parameters, trecipe->parameters);
 
1196
      e_code = -99999;
 
1197
      }
 
1198
   else if (flag_create_config == 1)
 
1199
      {
 
1200
      er_help_create_config (flag_create_config, plugin_name, caller_parameters, trecipe->parameters);
 
1201
      e_code = -99999;
 
1202
      }
 
1203
 
 
1204
   /* Check if we want to display the plugin help */
 
1205
 
 
1206
   p = cpl_parameterlist_find (caller_parameters, PACKAGE_RESOURCE ".man-page");
 
1207
   flag_man_page = cpl_parameter_get_bool (p);
 
1208
   if (flag_man_page != 0)
 
1209
      {
 
1210
      er_help_manpage (trecipe);
 
1211
      e_code = -99999;
 
1212
      }
 
1213
 
 
1214
   /* Check if we want to display the plugin help */
 
1215
 
 
1216
   p = cpl_parameterlist_find (caller_parameters, PACKAGE_RESOURCE ".help");
 
1217
   flag_help = cpl_parameter_get_bool (p);
 
1218
   if (flag_help != 0)
 
1219
      {
 
1220
      int  h_size = 512;
 
1221
      char tmp_buf[512];
 
1222
 
 
1223
      help_str = (char *) cpl_malloc((size_t) h_size);
 
1224
      if (help_str == NULL)
 
1225
         {
 
1226
         cpl_msg_error (er_func, "Could not allocate %d bytes for help_str",h_size);
 
1227
         e_code = CPL_ERROR_ILLEGAL_OUTPUT;
 
1228
         goto plugin_deinit;
 
1229
         }
 
1230
 
 
1231
 
 
1232
      (void) strcpy (help_str, "Recipe: ");
 
1233
      countr = 9;
 
1234
      cpp = cpl_plugin_get_name ((cpl_plugin *) trecipe);
 
1235
      n = (int) strlen(cpp);
 
1236
      countr += (5 + n);
 
1237
      if (countr > h_size) 
 
1238
         {                                      /* allocate more space */
 
1239
         h_size = countr + 120;
 
1240
         er_enlarge(er_func,&help_str,h_size);
 
1241
         }
 
1242
      (void) strcat (help_str, cpp);
 
1243
      (void) strcat (help_str, " -- ");
 
1244
 
 
1245
      cpp = cpl_plugin_get_synopsis ((cpl_plugin *) trecipe);
 
1246
      n = (int) strlen(cpp) + countr;
 
1247
      if (n > h_size) 
 
1248
         {                                      /* allocate more space */
 
1249
         h_size = n + 120;
 
1250
         er_enlarge(er_func,&help_str,h_size);
 
1251
         }
 
1252
      (void) strcat (help_str, cpp);
 
1253
      printf ("%s\n\n", er_strutils_split (help_str, 2, er_strutils_termwidth ()));
 
1254
 
 
1255
      msg_level = cpl_msg_get_level();          /* test, if we are in debug mode */
 
1256
      if (msg_level == CPL_MSG_DEBUG)
 
1257
         {
 
1258
         (void) strcpy (help_str, "Library: ");
 
1259
         countr = 12;                           /* length of above */
 
1260
         n = countr + (int) strlen(library_path);
 
1261
         if (n > h_size)
 
1262
            {                                           /* allocate more space */
 
1263
            h_size = n + 120;
 
1264
            er_enlarge(er_func,&help_str,h_size);
 
1265
            }
 
1266
 
 
1267
         (void) strcat (help_str, library_path);
 
1268
         printf ("%s\n\n", er_strutils_split (help_str, 2, er_strutils_termwidth ()));
 
1269
         }
 
1270
 
 
1271
      /* Display the actual help for the plugin */
 
1272
 
 
1273
      er_help_display (plugin_name, trecipe->parameters);
 
1274
 
 
1275
      /* Explain why the esorex help doesn't appear */
 
1276
 
 
1277
      (void) strcpy (tmp_buf,"For help on the options of " PACKAGE
 
1278
                     " itself, please use the command '" PACKAGE " --help' "
 
1279
                     "(that is, without specifying any recipe name). "
 
1280
                     "For more information about the recipe, one can also use "
 
1281
                     "the command '" PACKAGE " --man-page ");
 
1282
      countr = (int) strlen(tmp_buf);
 
1283
 
 
1284
      cpp = cpl_plugin_get_name ((cpl_plugin *) trecipe);
 
1285
      n = (int) strlen(cpp) + countr + 4;
 
1286
 
 
1287
      if (n > h_size) 
 
1288
         {
 
1289
         h_size = n;
 
1290
         er_enlarge("plugin_process_plugin",&help_str,h_size);
 
1291
         }
 
1292
 
 
1293
      (void) strcpy (help_str, tmp_buf);
 
1294
      (void) strcat (help_str, cpp);
 
1295
      (void) strcat (help_str, "'.");
 
1296
      (void) printf 
 
1297
         ("%s\n", er_strutils_split (help_str, 0, er_strutils_termwidth ()));
 
1298
             
 
1299
      goto plugin_deinit;                       /* avoid plugin execution */
 
1300
      }
 
1301
 
 
1302
   /* Check if we want to display the plugin parameters */
 
1303
 
 
1304
   p = cpl_parameterlist_find (caller_parameters,PACKAGE_RESOURCE ".params");
 
1305
   flag_params = cpl_parameter_get_bool (p);
 
1306
   if (flag_params != 0)
 
1307
      {
 
1308
      if (er_paramutils_print_list(trecipe->parameters,"Recipe Parameters") != 0)
 
1309
         {
 
1310
         cpl_msg_error (er_func,"Unable to print the recipe parameter list\n");
 
1311
         e_code = CPL_ERROR_INCOMPATIBLE_INPUT;
 
1312
         }
 
1313
      else
 
1314
         goto plugin_deinit;                    /* avoid plugin execution */
 
1315
      }           
 
1316
 
 
1317
   /* Check if we don't want to cleanup after processing (for debugging) */
 
1318
 
 
1319
   p = cpl_parameterlist_find (caller_parameters,PACKAGE_RESOURCE ".unload-plugin");
 
1320
   f_val_unload_plugin = cpl_parameter_get_bool (p);
 
1321
   /* printf("f_val_unload_plugin = %d\n",f_val_unload_plugin); */
 
1322
 
 
1323
 
 
1324
   /*
 
1325
    *  Run Plugin Execute - if all went well until here...
 
1326
    */
 
1327
 
 
1328
   if (e_code == 0)
 
1329
      {
 
1330
      plugin_func_exec = cpl_plugin_get_exec ((cpl_plugin *) trecipe);
 
1331
 
 
1332
      if (plugin_func_exec != NULL)
 
1333
         {                      /* We have a pointer, so run the plugin */
 
1334
         const char *recipe_name;
 
1335
 
 
1336
         recipe_name = cpl_plugin_get_name ((cpl_plugin *) trecipe);
 
1337
         cpl_msg_set_domain (recipe_name);
 
1338
         e_code = plugin_func_exec ((cpl_plugin *) trecipe);
 
1339
         cpl_msg_set_domain (PACKAGE);
 
1340
         if (e_code != 0)
 
1341
            {
 
1342
            cpl_msg_error (er_func,"Execution of recipe '%s' failed, status = %d",
 
1343
                           recipe_name,e_code);
 
1344
            }
 
1345
         }
 
1346
      else                      /* NULL-pointer, so we simply set an error */
 
1347
         e_code = CPL_ERROR_INCOMPATIBLE_INPUT;
 
1348
      }
 
1349
 
 
1350
   if (e_code == 0)                     /* successful execution of recipe */
 
1351
      {
 
1352
      p = cpl_parameterlist_find (caller_parameters, 
 
1353
                                  PACKAGE_RESOURCE ".output-dir");
 
1354
      val_output_dir = cpl_parameter_get_string (p);
 
1355
      ForceNull(val_output_dir)
 
1356
 
 
1357
      p = cpl_parameterlist_find (caller_parameters, 
 
1358
                                  PACKAGE_RESOURCE ".link-dir");
 
1359
      val_link_dir = cpl_parameter_get_string (p);
 
1360
      ForceNull(val_link_dir)
 
1361
 
 
1362
      p = cpl_parameterlist_find (caller_parameters,
 
1363
                                  PACKAGE_RESOURCE ".output-prefix");
 
1364
      val_output_mask = cpl_parameter_get_string (p);
 
1365
      ForceNull(val_output_mask)
 
1366
 
 
1367
      p = cpl_parameterlist_find (caller_parameters,
 
1368
                                  PACKAGE_RESOURCE ".output-readonly");
 
1369
      flag_readonly = cpl_parameter_get_bool (p);
 
1370
 
 
1371
      p = cpl_parameterlist_find (caller_parameters,
 
1372
                                  PACKAGE_RESOURCE ".suppress-prefix");
 
1373
      flag_noprefix = cpl_parameter_get_bool (p);
 
1374
 
 
1375
      p = cpl_parameterlist_find (caller_parameters,
 
1376
                                  PACKAGE_RESOURCE ".suppress-link");
 
1377
      flag_nolink = cpl_parameter_get_bool (p);
 
1378
 
 
1379
      /* Move all product files to output directory */
 
1380
 
 
1381
      e_code = move_products (trecipe->frames, (char *)val_output_dir,
 
1382
                   (char *)val_output_mask,
 
1383
                   (char *)val_link_dir, flag_noprefix, flag_nolink, flag_readonly);
 
1384
 
 
1385
      if (e_code != 0) cpl_msg_error (er_func,
 
1386
                        "An error occurred while trying to move the output products");
 
1387
      }
 
1388
 
 
1389
   else                 /* recipe failed, only get correct MD5 sum */
 
1390
      {                 /* for all product files created anyway */
 
1391
      if (e_code != -99999) ii = upda_products (trecipe->frames);
 
1392
      }
 
1393
 
 
1394
   /* Run PAF creation */
 
1395
   p = cpl_parameterlist_find (caller_parameters,
 
1396
                               PACKAGE_RESOURCE ".paf-config");
 
1397
   val_paf_config = cpl_parameter_get_string (p);
 
1398
   ForceNull(val_paf_config)
 
1399
   if(val_paf_config != NULL)
 
1400
       {
 
1401
       if (e_code == 0)    /* successful execution of recipe and product move*/
 
1402
          {
 
1403
               e_code = er_create_recipe_pafs(trecipe->frames,
 
1404
                                              trecipe->interface.name,
 
1405
                                              val_paf_config);
 
1406
               if (e_code != 0)
 
1407
                  {
 
1408
                  cpl_msg_error (er_func,"Cannot create paf files, status = %d",
 
1409
                                 e_code);
 
1410
                  }
 
1411
          }
 
1412
       else
 
1413
          {
 
1414
          cpl_msg_warning (er_func,"Writing of paf files omitted"
 
1415
                                   " due to previous errors");
 
1416
          }
 
1417
       }
 
1418
 
 
1419
 
 
1420
   /* Run Plugin Deinitialisation...  */
 
1421
 
 
1422
  plugin_deinit:
 
1423
   if (f_val_unload_plugin == 0)        /* we want to keep all memory */
 
1424
      {
 
1425
      cpl_msg_info(er_func,
 
1426
          "The plugin info will not be removed after execution!");
 
1427
      goto final_end;                   /* as it is for debugging! */
 
1428
      }
 
1429
 
 
1430
   plugin_func_deinit = cpl_plugin_get_deinit ((cpl_plugin *) trecipe);
 
1431
 
 
1432
   if (plugin_func_deinit != NULL)
 
1433
      {
 
1434
      cpl_msg_set_domain (cpl_plugin_get_name ((cpl_plugin *) trecipe));
 
1435
      e_code2 = plugin_func_deinit ((cpl_plugin *) trecipe);
 
1436
      cpl_msg_set_domain (PACKAGE);
 
1437
      }
 
1438
 
 
1439
   if (e_code2 == 0)
 
1440
      {
 
1441
      /* For now deinitialization is empty */
 
1442
      }
 
1443
 
 
1444
   /* If out main err.code is "OKAY", then use the deinit one, */
 
1445
   /* (otherwise, preserve the original error). */
 
1446
 
 
1447
   if (e_code == 0) e_code = e_code2;
 
1448
   }
 
1449
 
 
1450
 
 
1451
if ((flag_time_plugin != 0) && (flag_plugin_found != 0))
 
1452
   {
 
1453
   double fsz = 0.0;
 
1454
   int infile_count = 0;
 
1455
   cpl_frame *frame = NULL;     
 
1456
   const char *input_name = NULL;  
 
1457
 
 
1458
   plugin_time_end = cpl_test_get_walltime();
 
1459
   if (plugin_time_end > plugin_time_start)
 
1460
      {
 
1461
      plugin_time_end -= plugin_time_start;             /* reuse for time difference */
 
1462
      }
 
1463
   else
 
1464
      {
 
1465
      plugin_time_end = -1.0;
 
1466
      }
 
1467
 
 
1468
   /* loop again over frameset - now, all tags should be set by the recipe */
 
1469
 
 
1470
   frame = cpl_frameset_get_first (holdme);
 
1471
   while (frame != NULL)
 
1472
      {
 
1473
      if (cpl_frame_get_group (frame) == CPL_FRAME_GROUP_RAW)
 
1474
         {                       /* Get the filename as specified by the frame */
 
1475
         input_name = cpl_frame_get_filename (frame);  
 
1476
         if (add_size (input_name,&fsz) != 0)           /* accumulate size (in bytes) of raw input frames */
 
1477
            {
 
1478
            cpl_msg_warning("could not get size of %s\n",input_name);
 
1479
            }
 
1480
         else
 
1481
            {
 
1482
            infile_count ++;
 
1483
            }
 
1484
         }
 
1485
      frame = cpl_frameset_get_next (holdme);
 
1486
      }
 
1487
 
 
1488
   if ((plugin_time_end > 0.0) && (fsz > 0.0))
 
1489
      {
 
1490
      cpl_msg_info (er_func, "Recipe operation(s) took %14.3g seconds to complete.",
 
1491
                 plugin_time_end);
 
1492
      }
 
1493
   /*
 
1494
   else
 
1495
      {
 
1496
      cpl_msg_info (er_func, "No timing function [gettimeofday()] available...\n"); 
 
1497
      }
 
1498
   */
 
1499
 
 
1500
   if (fsz > 0.0)
 
1501
      {
 
1502
      fsz *= 0.000001;
 
1503
      if (infile_count > 1)
 
1504
         cpl_msg_info (er_func, "Total size of %d raw input frames  = %8.2f MB\n",infile_count,fsz);
 
1505
      else
 
1506
         cpl_msg_info (er_func, "Size of single raw input frame  = %8.2f MB\n",fsz);
 
1507
 
 
1508
      if (plugin_time_end > 0.0)
 
1509
         {
 
1510
         fsz /= plugin_time_end;
 
1511
         cpl_msg_info (er_func, "=> processing rate of %8.2f MB/sec \n",fsz);
 
1512
         }
 
1513
      }
 
1514
   /*
 
1515
   else
 
1516
      {
 
1517
      cpl_msg_info (er_func, "No size information obtained for raw input frames(s)...\n");
 
1518
      }
 
1519
      */
 
1520
   }
 
1521
 
 
1522
/* Terminate CPL messaging */
 
1523
 
 
1524
cleanup:
 
1525
/* currently cpl_msg_stop_log () always returns CPL_ERROR_NONE ... 080613 */
 
1526
if (cpl_msg_stop_log () != CPL_ERROR_NONE)
 
1527
   cpl_msg_error (er_func, "An error was encountered while closing the logfile");
 
1528
   
 
1529
if (flag_plugin_found != 0)
 
1530
   {                                            /*  Move log file...  */
 
1531
   char  tmpbuf[FILEMAX+12];
 
1532
 
 
1533
   (void) getcwd (tmpbuf, (size_t) FILEMAX);
 
1534
   (void) strcat (tmpbuf, "/.logfile");
 
1535
 
 
1536
   if (fileutils_file_is_there(tmpbuf) != 0)
 
1537
      {                                         /* .logfile exists ... */
 
1538
      char  dstbuf[FILEMAX+2];
 
1539
 
 
1540
      (void) strcpy (dstbuf, val_log_dir);
 
1541
      (void) strcat (dstbuf, "/");
 
1542
      (void) strcat (dstbuf, val_log_file);
 
1543
      n = fileutils_copy(tmpbuf,dstbuf);  /* copy .logfile to "real" log file */
 
1544
      if (n < 0)
 
1545
         {
 
1546
         (void) printf("we could not copy .logfile to %s (err-code = %d)\n",
 
1547
                       dstbuf,n);
 
1548
         }
 
1549
      else
 
1550
         {
 
1551
         if (n == 0) (void) unlink(tmpbuf);     /* get rid of .logfile */
 
1552
         }
 
1553
      }
 
1554
   }
 
1555
 
 
1556
cpl_msg_set_component_off ();
 
1557
cpl_msg_set_time_off ();
 
1558
cpl_msg_set_domain_off ();
 
1559
 
 
1560
 
 
1561
/* free memory again */
 
1562
 
 
1563
if (help_str != NULL) cpl_free (help_str);
 
1564
 
 
1565
if (log_file_name_full != NULL) cpl_free (log_file_name_full);
 
1566
if (plugin_conf_file_global != NULL) cpl_free (plugin_conf_file_global);
 
1567
if (plugin_conf_file_local != NULL) cpl_free (plugin_conf_file_local);
 
1568
 
 
1569
if (val_recipe_dirs != NULL) cx_strfreev (val_recipe_dirs);
 
1570
if (list_of_pllib_names != NULL) er_stringarray_delete (list_of_pllib_names);
 
1571
 
 
1572
if (tplugin != NULL) cpl_plugin_delete (tplugin);
 
1573
 
 
1574
#ifdef STATIC_LINK
 
1575
#else
 
1576
if (module != NULL) lt_dlclose (module);
 
1577
#endif
 
1578
 
 
1579
if (t2recipe != NULL)
 
1580
   {                                    /* we have a version 2 recipe! */
 
1581
   cpl_frameset_delete (trecipe->frames);
 
1582
   cpl_plugin_delete ((cpl_plugin *) t2recipe); 
 
1583
   }
 
1584
else if (trecipe != NULL)
 
1585
   {                                    /* trecipe->parameters handled by plugin */
 
1586
   cpl_frameset_delete (trecipe->frames);
 
1587
   cpl_plugin_delete ((cpl_plugin *) trecipe); 
 
1588
   }
 
1589
 
 
1590
final_end:
 
1591
if (e_code == -99999) e_code = 0;
 
1592
return e_code;
 
1593
}                               /* End of plugin_process_plugin() */
 
1594
 
 
1595
/*
 
1596
 
 
1597
*/
 
1598
 
 
1599
/**********************************************************************/
 
1600
/**
 
1601
 * @brief  Enlarge memory buffer
 
1602
 * @param  func        name of caller
 
1603
 * @param  mem_pntr    addr. of pointer to allocated memory buffer
 
1604
 * @param  mem_size    new (increased) size for memory buffer
 
1605
 *
 
1606
 * @returns 0 if successfull, !=0 otherwise
 
1607
 * 
 
1608
 * This function frees the currently allocated space and uses the pointer
 
1609
 * 'mem_pntr' to point to newly allocated memory
 
1610
 */
 
1611
/**********************************************************************/
 
1612
 
 
1613
void er_enlarge (const char *fn, char **pptr, int msize)
 
1614
 
 
1615
{
 
1616
void  *newptr, *ptr;
 
1617
 
 
1618
 
 
1619
ptr = (void *) *pptr;                   /* get address to free */
 
1620
 
 
1621
newptr = cpl_realloc(ptr,(size_t) msize);
 
1622
if (newptr == NULL)                     /* couldn't get the memory... */
 
1623
   {
 
1624
   cpl_msg_error (fn, "Could not allocate %d bytes - fatal error", msize);
 
1625
   exit (EXIT_FAILURE);
 
1626
   }
 
1627
 
 
1628
*pptr = (char *) newptr;
 
1629
}
 
1630
 
 
1631
/**@}*/
 
1632
 
 
1633
/* End of file */
 
1634
 
 
1635