1
// drv_preproc_ifr.cpp,v 1.15 2003/11/04 05:21:30 dhinton Exp
7
Copyright 1992, 1993, 1994 Sun Microsystems, Inc. Printed in the United
8
States of America. All Rights Reserved.
10
This product is protected by copyright and distributed under the following
11
license restricting its use.
13
The Interface Definition Language Compiler Front End (CFE) is made
14
available for your use provided that you include this license and copyright
15
notice on all media and documentation and the software program in which
16
this product is incorporated in whole or part. You may copy and extend
17
functionality (but may not remove functionality) of the Interface
18
Definition Language CFE without charge, but you are not authorized to
19
license or distribute it to anyone else except as part of a product or
20
program developed by you or with the express written consent of Sun
21
Microsystems, Inc. ("Sun").
23
The names of Sun Microsystems, Inc. and any of its subsidiaries or
24
affiliates may not be used in advertising or publicity pertaining to
25
distribution of Interface Definition Language CFE as permitted herein.
27
This license is effective until terminated by Sun for failure to comply
28
with this license. Upon termination, you shall destroy or return all code
29
and documentation for the Interface Definition Language CFE.
31
INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED AS IS WITH NO WARRANTIES OF
32
ANY KIND INCLUDING THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS
33
FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR ARISING FROM A COURSE OF
34
DEALING, USAGE OR TRADE PRACTICE.
36
INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED WITH NO SUPPORT AND WITHOUT
37
ANY OBLIGATION ON THE PART OF Sun OR ANY OF ITS SUBSIDIARIES OR AFFILIATES
38
TO ASSIST IN ITS USE, CORRECTION, MODIFICATION OR ENHANCEMENT.
40
SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES SHALL HAVE NO LIABILITY WITH
41
RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY
42
INTERFACE DEFINITION LANGUAGE CFE OR ANY PART THEREOF.
44
IN NO EVENT WILL SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES BE LIABLE FOR
45
ANY LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
46
DAMAGES, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
48
Use, duplication, or disclosure by the government is subject to
49
restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
50
Technical Data and Computer Software clause at DFARS 252.227-7013 and FAR
53
Sun, Sun Microsystems and the Sun logo are trademarks or registered
54
trademarks of Sun Microsystems, Inc.
58
Mountain View, California 94043
62
SunOS, SunSoft, Sun, Solaris, Sun Microsystems or the Sun logo are
63
trademarks or registered trademarks of Sun Microsystems, Inc.
67
// Pass an IDL file through the C preprocessor
69
#include "drv_extern_ifr.h"
70
#include "be_global.h"
71
#include "be_extern.h"
74
#include "idl_defines.h"
75
#include "global_extern.h"
76
#include "fe_extern.h"
77
#include "utl_string.h"
78
#include "ace/Process_Manager.h"
79
#include "ace/SString.h"
80
#include "ace/Env_Value_T.h"
82
#include "ace/streams.h"
83
#include "ace/OS_NS_stdio.h"
85
ACE_RCSID (IFR_Service,
87
"drv_preproc_ifr.cpp,v 1.15 2003/11/04 05:21:30 dhinton Exp")
89
static long argcount = 0;
90
static long max_argcount = 128;
91
static const char *arglist[128];
92
static const char *output_arg_format = 0;
93
static long output_arg_index = 0;
95
// Push the new CPP location if we got a -Yp argument
97
DRV_cpp_new_location (const char *new_loc)
102
// Push an argument into the arglist
104
DRV_cpp_putarg (const char *str)
106
if (argcount >= max_argcount)
108
ACE_ERROR ((LM_ERROR,
110
idl_global->prog_name (),
111
ACE_TEXT (": More than"),
113
ACE_TEXT ("arguments to preprocessor")));
118
arglist[argcount++] = ACE::strnew (str);
121
// Expand the output argument with the given filename.
123
DRV_cpp_expand_output_arg (const char *filename)
125
if (output_arg_format != 0)
127
delete [] ACE_const_cast (char *, arglist[output_arg_index]);
128
arglist[output_arg_index] = 0;
130
char *output_arg = 0;
132
char [ACE_OS::strlen (output_arg_format)
133
+ ACE_OS::strlen (filename)
136
ACE_OS::sprintf (output_arg,
140
arglist[output_arg_index] = output_arg;
144
// Initialize the cpp argument list.
148
// @@ There are two "one time" memory leaks in this function.
149
// They will not blow off the program but should be fixed at some point.
151
const char *cpp_args;
153
// See if TAO_IDL_PREPROCESSOR is defined.
155
ACE_Env_Value<char*> preprocessor ("TAO_IDL_PREPROCESSOR", (char *) 0);
157
// Set cpp_loc to the built in location, unless it has been overriden by
158
// environment variables.
160
if (preprocessor != 0)
162
cpp_loc = preprocessor;
166
// Check for the deprecated CPP_LOCATION environment variable
167
ACE_Env_Value<char*> cpp_path ("CPP_LOCATION", (char *) 0);
170
ACE_ERROR ((LM_ERROR,
171
ACE_TEXT ("Warning: The environment variable ")
172
ACE_TEXT ("CPP_LOCATION has been deprecated.\n")
173
ACE_TEXT (" Please use TAO_IDL_PREPROCESSOR ")
174
ACE_TEXT ("instead.\n")));
180
cpp_loc = idl_global->cpp_location ();
184
DRV_cpp_putarg (cpp_loc);
185
DRV_cpp_putarg ("-I.");
187
// Added some customizable preprocessor options
189
ACE_Env_Value<char*> args1 ("TAO_IDL_PREPROCESSOR_ARGS", (char *) 0);
197
// Check for the deprecated TAO_IDL_DEFAULT_CPP_FLAGS environment variable
198
ACE_Env_Value<char*> args2 ("TAO_IDL_DEFAULT_CPP_FLAGS", (char *) 0);
201
ACE_ERROR ((LM_ERROR,
203
ACE_TEXT ("Warning: The environment variable "),
204
ACE_TEXT ("TAO_IDL_DEFAULT_CPP_FLAGS has been "),
205
ACE_TEXT ("deprecated.\n"),
206
ACE_TEXT (" Please use "),
207
ACE_TEXT ("TAO_IDL_PREPROCESSOR_ARGS instead.\n")));
213
// If no cpp flag was defined by the user, we define some
214
// platform specific flags here.
217
#if defined (TAO_IDL_PREPROCESSOR_ARGS)
218
cpp_args = TAO_IDL_PREPROCESSOR_ARGS;
219
#elif defined (ACE_CC_PREPROCESSOR_ARGS)
220
cpp_args = ACE_CC_PREPROCESSOR_ARGS;
223
#endif /* TAO_IDL_PREPROCESSOR_ARGS */
225
// So we can find OMG IDL files, such as `orb.idl'.
226
ACE_OS::strcpy (option, "-I");
228
#if defined (TAO_IDL_INCLUDE_DIR)
229
// TAO_IDL_INCLUDE_DIR should be in quotes,
230
// e.g. "/usr/local/include/tao"
232
ACE_OS::strcat (option, TAO_IDL_INCLUDE_DIR);
234
const char* TAO_ROOT = ACE_OS::getenv ("TAO_ROOT");
238
ACE_OS::strcat (option, TAO_ROOT);
239
ACE_OS::strcat (option, "/tao");
243
const char* ACE_ROOT = ACE_OS::getenv ("ACE_ROOT");
247
ACE_OS::strcat (option, ACE_ROOT);
248
ACE_OS::strcat (option, "/TAO/tao");
255
ACE_TEXT ("Note: The environment variables "),
256
ACE_TEXT ("TAO_ROOT and ACE_ROOT are not defined.\n"),
257
ACE_TEXT (" TAO_IDL may not be able to "),
258
ACE_TEXT ("locate orb.idl\n")
261
ACE_OS::strcat (option, ".");
264
#endif /* TAO_IDL_INCLUDE_DIR */
266
DRV_cpp_putarg (option);
270
// Add any flags in cpp_args to cpp's arglist.
271
ACE_ARGV arglist (cpp_args);
273
for (size_t arg_cnt = 0; arg_cnt < (size_t)arglist.argc (); ++arg_cnt)
275
// Check for an argument that specifies the preprocessor's output file.
276
if (ACE_OS::strstr (arglist[arg_cnt], "%s") != 0
277
&& output_arg_format == 0)
279
output_arg_format = ACE::strnew (arglist[arg_cnt]);
280
output_arg_index = argcount;
285
DRV_cpp_putarg (arglist[arg_cnt]);
290
// Lines can be 1024 chars long.
291
#define LINEBUF_SIZE 1024
292
static char drv_line[LINEBUF_SIZE + 1];
294
// Get a line from stdin
296
DRV_get_line (FILE *f)
298
char *l = fgets (drv_line,
308
if (*l == '\0' && feof (f))
328
// Copy from stdin to a file
330
DRV_copy_input (FILE *fin,
332
const char *orig_filename)
334
FILE *f = ACE_OS::fopen (fn, "w");
338
ACE_ERROR ((LM_ERROR,
340
idl_global->prog_name (),
341
ACE_TEXT (": cannot open temp file "),
343
ACE_TEXT (" for writing\n")));
350
ACE_ERROR ((LM_ERROR,
352
idl_global->prog_name (),
353
ACE_TEXT (": cannot open input file\n")));
358
#if !defined (ACE_WIN32)
363
// Convert single \ into double \ otherwise MSVC++ pre-processor
364
// gets awfully confused.
365
char buf[2*MAXPATHLEN];
368
for (const char *s = orig_filename; *s != 0; ++s)
384
#endif /* ! ACE_WIN32 */
386
while (DRV_get_line (fin))
388
// We really need to know whether this line is a "#include
389
// ...". If so, we would like to separate the "file name" and
390
// keep that in the idl_global. We need them to produce
391
// "#include's in the stubs and skeletons.
392
DRV_check_for_include (drv_line);
394
// Print the line to the temp file.
400
// Close the temp file.
404
// Strip down a name to the last component,
405
// i.e. everything after the last '/' or '\' character
407
DRV_stripped_name (char *fn)
420
for (n += l - 1; n >= fn && !slash_found; n--)
422
slash_found = (*n == '/' || *n == '\\');
436
static char tmp_file[128];
437
static char tmp_ifile[128];
439
// Pass input through preprocessor
441
DRV_pre_proc (const char *myfile)
443
long readfromstdin = I_FALSE;
445
// Macro to avoid "warning: unused parameter" type warning.
446
ACE_UNUSED_ARG (readfromstdin);
448
const char* tmpdir = idl_global->temp_dir ();
450
ACE_OS::strcpy (tmp_file, tmpdir);
451
ACE_OS::strcpy (tmp_ifile, tmpdir);
453
ACE_OS::strcat (tmp_file, "idlf_XXXXXX");
454
ACE_OS::strcat (tmp_ifile, "idli_XXXXXX");
456
(void) ACE_OS::mktemp (tmp_file); ACE_OS::strcat (tmp_file, ".cc");
457
(void) ACE_OS::mktemp (tmp_ifile); ACE_OS::strcat (tmp_ifile, ".cc");
461
be_global->filename (ACE_const_cast (char *, myfile));
463
if (strcmp (myfile, "standard input") == 0)
466
UTL_String (tmp_ifile));
467
idl_global->set_filename (tmp);
470
UTL_String (tmp_ifile));
471
idl_global->set_main_filename (tmp);
474
UTL_String (DRV_stripped_name (tmp_ifile)));
475
idl_global->set_stripped_filename (tmp);
478
UTL_String (tmp_ifile));
479
idl_global->set_real_filename (tmp);
481
DRV_copy_input (stdin,
485
idl_global->set_read_from_stdin (I_TRUE);
489
FILE *fd = fopen (myfile, "r");
495
idl_global->set_read_from_stdin (I_FALSE);
498
UTL_String (myfile));
499
idl_global->set_filename (tmp);
502
UTL_String (myfile));
503
idl_global->set_main_filename (tmp);
505
ACE_Auto_String_Free safety (ACE_OS::strdup (myfile));
507
UTL_String (DRV_stripped_name (safety.get ())));
508
idl_global->set_stripped_filename (tmp);
511
UTL_String (tmp_ifile));
512
idl_global->set_real_filename (tmp);
515
// We use ACE instead of the (low level) fork facilities, this also
519
// For complex builds, the default command line buffer size of 1024
520
// is sometimes not enough. We use 4096 here.
521
ACE_Process_Options cpp_options (1, // Inherit environment.
522
TAO_IDL_COMMAND_LINE_BUFFER_SIZE);
524
DRV_cpp_expand_output_arg (tmp_file);
525
DRV_cpp_putarg (tmp_ifile);
526
DRV_cpp_putarg (0); // Null terminate the arglist.
528
cpp_options.command_line (arglist);
530
/// Remove any existing output file.
531
(void) ACE_OS::unlink (tmp_file);
533
ACE_HANDLE fd = ACE_INVALID_HANDLE;
535
if (output_arg_format == 0)
537
// If the following open() fails, then we're either being hit with a
538
// symbolic link attack, or another process opened the file before
540
fd = ACE_OS::open (tmp_file,
541
O_WRONLY | O_CREAT | O_EXCL,
542
ACE_DEFAULT_FILE_PERMS);
544
if (fd == ACE_INVALID_HANDLE)
546
ACE_ERROR ((LM_ERROR,
548
idl_global->prog_name (),
549
ACE_TEXT (": cannot open temp file "),
551
ACE_TEXT (" for writing\n")));
556
cpp_options.set_handles (ACE_INVALID_HANDLE, fd);
559
if (process.spawn (cpp_options) == ACE_INVALID_PID)
561
ACE_ERROR ((LM_ERROR,
563
idl_global->prog_name (),
564
ACE_TEXT (": spawn of "),
566
ACE_TEXT (" failed\n")));
571
if (fd != ACE_INVALID_HANDLE)
573
// Close the output file on the parent process.
574
if (ACE_OS::close (fd) == -1)
576
ACE_ERROR ((LM_ERROR,
578
idl_global->prog_name (),
579
ACE_TEXT (": cannot close temp file"),
581
ACE_TEXT (" on parent\n")));
587
// Remove the null termination and the input file from the arglist,
588
// the next file will the previous args.
591
ACE_exitcode status = 0;
592
if (process.wait (&status) == ACE_INVALID_PID)
594
ACE_ERROR ((LM_ERROR,
596
idl_global->prog_name (),
597
ACE_TEXT (": wait for child process failed\n")));
602
if (WIFEXITED ((status)))
604
// child terminated normally?
605
if (WEXITSTATUS ((status)) != 0)
607
errno = WEXITSTATUS ((status));
609
ACE_ERROR ((LM_ERROR,
611
idl_global->prog_name (),
612
ACE_TEXT (": preprocessor "),
614
ACE_TEXT (" returned with an error\n")));
621
// child didn't call exit(); perhaps it received a signal?
624
ACE_ERROR ((LM_ERROR,
626
idl_global->prog_name (),
627
ACE_TEXT (": preprocessor "),
629
ACE_TEXT (" appears to have been interrupted\n")));
633
// TODO: Manage problems in the pre-processor, in the previous
634
// version the current process would exit if the pre-processor
635
// returned with error.
637
FILE *yyin = ACE_OS::fopen (tmp_file, "r");
641
ACE_ERROR ((LM_ERROR,
643
idl_global->prog_name (),
644
ACE_TEXT (": Could not open cpp output file"),
650
FE_set_yyin (ACE_reinterpret_cast (File *, yyin));
652
if (idl_global->compile_flags() & IDL_CF_ONLY_PREPROC)
654
FILE *preproc = ACE_OS::fopen (tmp_file, "r");
655
char buffer[ACE_MAXLOGMSGLEN];
660
ACE_ERROR ((LM_ERROR,
661
"%s: Could not open cpp output file: %s\n",
662
idl_global->prog_name (),
668
// ACE_DEBUG sends to stderr - we want stdout for this dump
669
// of the preprocessor output. So we modify the singleton that
670
// was created in this process. Since IDL_CF_ONLY_PREPROC causes
671
// an (almost) immediate exit below, we don't have to restore
672
// the singleton's default parameters.
673
ACE_Log_Msg *out = ACE_Log_Msg::instance ();
674
out->msg_ostream (&cout);
675
out->clr_flags (ACE_Log_Msg::STDERR);
676
out->set_flags (ACE_Log_Msg::OSTREAM);
678
while ((bytes = ACE_OS::fread (buffer,
680
ACE_MAXLOGMSGLEN - 1,
686
ACE_DEBUG ((LM_DEBUG,
690
ACE_OS::fclose (preproc);
693
if (ACE_OS::unlink (tmp_ifile) == -1)
695
ACE_ERROR ((LM_ERROR,
697
idl_global->prog_name (),
698
ACE_TEXT (": Could not remove cpp input file"),
704
#if !defined (ACE_WIN32) || defined (ACE_HAS_WINNT4) && (ACE_HAS_WINNT4 != 0)
705
if (ACE_OS::unlink (tmp_file) == -1)
707
ACE_ERROR ((LM_ERROR,
709
idl_global->prog_name (),
710
ACE_TEXT (": Could not remove cpp output file"),
715
#endif /* ACE_HAS_WINNT4 && ACE_HAS_WINNT4 != 0 */
717
if (idl_global->compile_flags() & IDL_CF_ONLY_PREPROC)
723
// We really need to know whether this line is a "#include ...". If
724
// so, we would like to separate the "file name" and keep that in the
725
// idl_global. We need them to produce "#include's in the stubs and
728
DRV_check_for_include (const char* buf)
743
// Skip the tabs and spaces.
744
while (*r == ' ' || *r == '\t')
749
// Probably we are at the word `include`. If not return.
755
// Check whether this word is `include` or no.
756
const char* include_str = "include";
759
ii < strlen ("include") && *r != '\0' && *r != ' ' && *r != '\t';
762
// Return if it doesn't match.
763
if (include_str [ii] != *r)
769
// Next thing is finding the file that has been `#include'd. Skip
770
// all the blanks and tabs and reach the startng " or < character.
771
for (; (*r != '"') && (*r != '<'); r++)
773
if (*r == '\n' || *r == '\0')
779
// Decide on the end char.
790
// Store this position.
793
// Found this in idl.ll. Decides the file to be standard input.
799
// Find the closing " or < character.
800
for (; *r != end_char; r++)
805
// Make a new string for this file name.
813
for (; h != r; fi++, h++)
818
// Terminate the string.
819
file_name [fi] = '\0';
821
// Put Microsoft-style pathnames into a canonical form.
824
for (size_t j = 0; file_name [j] != '\0'; i++, j++)
826
if (file_name [j] == '\\' && file_name [j + 1] == '\\')
831
file_name [i] = file_name [j];
834
// Terminate this string.
835
file_name [i] = '\0';
837
// Store in the idl_global, unless it's "orb.idl" -
838
// we don't want to generate header includes for that.
839
if (ACE_OS::strcmp (file_name, "orb.idl"))
841
idl_global->add_to_included_idl_files (file_name);
845
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
846
template class ACE_Env_Value<char*>;
847
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
848
# pragma instantiate ACE_Env_Value<char*>