1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
|
/* ftnpp.c: Fortran Preprocessor.
This reads in a Fortran source file, interpolating INCLUDE files
and #include files, and passing the result through CPP (the
preprocessor defined in config.h). If no source file specified,
reads stdin.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include "ftnchek.h"
PROTO(int main, ( int argc, char *argv[] ));
PROTO(PRIVATE FILE *open_infile, ( char *s ));
PROTO(PRIVATE FILE *open_pipeout, ( char *prog, char *args[] ));
PROTO(PRIVATE FILE *open_incfile, ( char *s ));
PROTO(PRIVATE VOID read_srcfile,( FILE *fd ));
PROTO(PRIVATE char *see_include_line, ( char *line ));
PROTO(PRIVATE FILE* find_include, ( char **fname ));
PROTO(PRIVATE FILE * fopen_with_path, ( char *inc_path, char **fname ));
PROTO(PRIVATE VOID append_include_path, ( char *new_path ));
char line[MAXLINE+1]; /* input line buffer */
IncludePathNode *include_path_list; /* header to the include-path list */
FILE *input_fd, *list_fd;
char *current_filename;
int strip_comments = TRUE; /* turned off by -C flag */
int print_line_nums = TRUE; /* turned off by -P flag */
/* Macro to add another element to growing array
of char *
*/
#define add_to_list(NUM,PTR,STR) \
NUM++; \
PTR = (char **)realloc(PTR,NUM*sizeof(char *)); \
if(PTR == (char **)NULL) { \
fprintf(stderr,"Cannot allocate memory\n"); \
exit(1); \
} \
PTR[NUM-1] = STR
int
#if HAVE_STDC
main(int argc, char **argv)
#else /* K&R style */
main(argc,argv)
int argc;
char *argv[];
#endif /* HAVE_STDC */
{
int iarg, ifile;
char **filelist = (char **) NULL;
char **cpp_arglist = (char **) NULL;
int num_files=0;
int num_cpp_args=0;
char *stdin_filelist[]={ "" }; /* for use when input is stdin */
include_path_list = (IncludePathNode*) NULL;
for(iarg=1; iarg<argc; iarg++) {
int i;
/* Process any flags. "-" is a file arg. */
if(argv[iarg][0] == '-' && argv[iarg][1] != '\0') {
switch(argv[iarg][1]) {
/* -C (keep comments) */
case 'C':
strip_comments = FALSE;
break;
/* -I includepath */
case 'I':
i = 2;
if(argv[iarg][i] == '\0') {/* handle separated args */
iarg++;
i = 0;
if( iarg == argc ) {
fprintf(stderr,"Missing include file after -I\n");
break;
}
}
append_include_path(&argv[iarg][i]);
break;
/* -P suppress '#-lines' */
case 'P':
print_line_nums = FALSE;
break;
case 'D': /* -D and -U recognized but not processed */
case 'U':
if(argv[iarg][2] == '\0') {/* handle separated args */
iarg++;
if( iarg == argc ) {
fprintf(stderr,"Missing macro name after %s\n",
argv[iarg-1]);
}
}
break;
default: /* Ignore unrecognized flags */
break;
}
}
/* Save any file arguments in list */
else {
add_to_list(num_files,filelist,argv[iarg]);
}
}
/* Now go back over args, copying all the ones
that we pass to cpp into cpp_arglist. First,
put in argv[0] = name of cpp.
*/
add_to_list(num_cpp_args,cpp_arglist,CPP);
for(iarg=1; iarg<argc; iarg++) {
/* Process any flags */
if(argv[iarg][0] == '-' && argv[iarg][1] != '\0') {
/* Stop copying after seeing -- */
if(strcmp(argv[iarg],"--") == 0)
break;
add_to_list(num_cpp_args,cpp_arglist,argv[iarg]);
switch(argv[iarg][1]) {
/* options with arguments */
case 'D':
case 'I':
case 'U':
if(argv[iarg][2] == '\0') {/* handle separated args */
if( ++iarg < argc ) {
add_to_list(num_cpp_args,cpp_arglist,argv[iarg]);
}
}
break;
/* options without arguments */
default:
break;
}
}
}
/* Check for proper invocation */
if( num_files > 2 ) {
fprintf(stderr,"Usage: ftnpp [options] [infile [outfile]]\n");
exit(1);
}
/* If outfile specified, pass it to cpp,
and give cpp the "-" argument for infile
to be stdin.
*/
if( num_files == 2 ) {
add_to_list(num_cpp_args,cpp_arglist,"-");
add_to_list(num_cpp_args,cpp_arglist,filelist[1]);
num_files = 1; /* remove outfile from list */
}
/* Terminate arg list with a NULL as
required by execv().
*/
add_to_list(num_cpp_args,cpp_arglist,(char *)NULL);
#ifdef DEBUG_CPP_ARGS
for(iarg=0; iarg<num_cpp_args; iarg++)
printf("<%s>\n",cpp_arglist[iarg]);
#endif
/* If no file args or infile is "-", use
stdin for infile.
*/
if( num_files == 0 || strcmp(filelist[0],"-") == 0 ) {
num_files = 1;
filelist = stdin_filelist;
}
/* Read the infile */
for(ifile=0; ifile<num_files; ifile++) {
input_fd = open_infile(filelist[ifile]);
if( input_fd == (FILE *)NULL ) {
fprintf(stderr,"\nError opening source file ");
perror(filelist[ifile]);
}
else {
list_fd = open_pipeout(CPP,cpp_arglist);
if( list_fd == (FILE *)NULL ) {
fprintf(stderr,"\nError forking ");
perror(CPP);
}
else {
read_srcfile(input_fd);
pclose(list_fd);
}
fclose(input_fd);
}
}
return 0;
}
PRIVATE VOID
#if HAVE_STDC
read_srcfile(FILE *fd)
#else /* K&R style */
read_srcfile(fd)
FILE *fd;
#endif /* HAVE_STDC */
{
char *incname;
FILE *incfd;
char *parent_filename = current_filename;
int line_num = 1;
if(print_line_nums) {
fprintf(list_fd,"# %d \"%s\"\n",line_num,current_filename);
}
while( fgets(line,MAXLINE+1,fd) != NULL ) {
line_num++;
/* Suppress f77 comments. We do not
suppress ! comments since they need
more context to be sure about them.
*/
if( line[0] == 'C' || line[0] == 'c' || line[0] == '*' ) {
if( strip_comments )
fputs("\n",list_fd);
else
fputs(line,list_fd);
}
/* If line has an include directive, open
the file. If can't open: just pass it on.
*/
else if( ((incname = see_include_line(line)) == (char *)NULL) ||
((incfd = open_incfile(incname)) == (FILE *)NULL) ) {
fputs(line,list_fd);
}
else {
/* Read the include file. */
read_srcfile(incfd);
pclose(incfd);
current_filename = parent_filename; /* restore clobbered value */
/* Print a # line directive to say we're
back in the original file.
*/
if(print_line_nums) {
fprintf(list_fd,"# %d \"%s\"\n",line_num,current_filename);
}
}
}
}
/* Open source file, record its name.
*/
PRIVATE FILE *
#if HAVE_STDC
open_infile(char *infile)
#else /* K&R style */
open_infile(infile)
char *infile;
#endif /* HAVE_STDC */
{
FILE *fd;
/* If file is null string, then it's stdin.
*/
if( infile[0] == '\0' ) {
fd = stdin;
}
else {
fd = fopen(infile,"r");
}
if(fd != NULL)
current_filename = infile;
return fd;
}
PRIVATE FILE *
#if HAVE_STDC
open_pipeout(char *prog, char *args[])
#else /* K&R style */
open_pipeout(prog, args)
char *prog;
char *args[];
#endif /* HAVE_STDC */
{
int pfd[2]; /* Pipe file descriptors */
FILE *pipeout=NULL; /* File pointer to pipe write end */
pid_t child_pid;
if( pipe(pfd) != 0 ) { /* Open the parent -> child pipe */
perror("pipe open failed in open_pipeout");
return pipeout;
}
if( (child_pid = fork()) != 0 ) { /* Start up a child process */
if( child_pid == -1) { /* Fork failed */
perror("fork failed in open_pipeout");
exit(1);
}
/* Child process starts here */
(void) close(pfd[1]); /* Close the writing end of pipe */
if( dup2(pfd[0],0) == -1) { /* Redirect child's stdin to come from pipe */
perror("failed to redirect stdin to pipe in open_pipeout");
exit(1);
}
if(execv(prog,args) == -1) {
fprintf(stderr,"open_pipeout failed to exec ");
perror(prog);
exit(1);
}
} /* Child process ends here */
else { /* Parent process continues here */
(void) close(pfd[0]); /* Close reading end of pipe */
pipeout = fdopen(pfd[1],"w"); /* Open file ptr to pipe */
}
return pipeout;
}
/* Recognize either Fortran INCLUDE statement
or cpp #include line. Return pointer to
the name of the include-file.
*/
PRIVATE char *
#if HAVE_STDC
see_include_line(char *line)
#else /* K&R style */
see_include_line(line)
char *line;
#endif /* HAVE_STDC */
{
char *incfile = (char *)NULL; /* name of include file */
/* Skip over leading space. Also skip over
any '#' sign of a cpp style include.
*/
while( *line != '\0' && (isspace(*line) || (*line == '#')) ) {
++line;
}
if( strncasecmp(line,"include",7) == 0 ) {
int delim;
line += 7; /* skip over "include" */
while( *line != '\0' && isspace(*line) ) {
++line; /* skip over following space */
}
/* See if next thing is a quote mark */
if( *line == '\'' || *line == '"' ) {
delim = *line; /* Remember which kind of quote it is */
++line;
incfile = malloc(strlen(line));
if( incfile != (char *)NULL ) {
/* Copy up to next quote mark. If filename
has a quote mark, too bad.
*/
char *p = incfile;
while( *line != '\0' && *line != delim ) {
*p++ = *line++;
}
/* If closing quote not found, back out. */
if( *line != delim ) {
free(incfile);
incfile = (char *)NULL;
}
else {
*p++ = '\0';
}
}
}
}
return incfile;
}
PRIVATE FILE *
#if HAVE_STDC
open_incfile(char *infile)
#else /* K&R style */
open_incfile(infile)
char *infile;
#endif /* HAVE_STDC */
{
return find_include(&infile);
}
PRIVATE FILE*
#if HAVE_STDC
find_include(char **fname) /* looks for file locally or in include dir */
/* If found, fname is returned with full path*/
#else /* K&R style */
find_include(fname) /* looks for file locally or in include dir */
char **fname; /* If found, fname is returned with full path*/
#endif /* HAVE_STDC */
{
FILE *fp;
char *env_include_var;
IncludePathNode *p;
char *path_end=(char *)NULL;
int fname_path_absolute=FALSE;
/* Look first for bare filename. If it is an
absolute path, then it must be found as it is.
But if it is not absolute and the current source
filename is qualified with a path, evaluate
the include filename relative to that path.
Note: if OS is not included in the stanzas below,
the default behavior is to assume the include
file name is not an absolute path, and the
current file name is not qualified by a path.
*/
/* Still to do:
==> Handle MacOS.
*/
#ifdef UNIX
fname_path_absolute = ((*fname)[0] == '/');
#endif
/* VMS test is kinda simplistic: it just looks for
a colon, as in 'SOME_LOGNAME:INCFILE.H', or else
a left bracket not followed by a '-' or '.' which
are the two ways I know of to do relative paths.
I would appreciate hearing from somebody who knows
a way to do this more surely.
*/
#ifdef VMS
{
char *lbracket;
fname_path_absolute = ( strchr(*fname,':') != NULL
|| ((lbracket=strchr(*fname,'[')) != NULL &&
(lbracket[1] != '-' && lbracket[1] != '.') ) );
}
#endif
/* MSDOS test looks for forms like A:path or
\path or /path (the last since some
development environments support / as path
separator.)
*/
#ifdef MSDOS
fname_path_absolute = ((isalpha((*fname)[0]) && (*fname)[1] == ':')
|| (*fname)[0] == '\\' || (*fname)[0] == '/');
#endif
if(fname_path_absolute) { /* include filename is an absolute path */
return open_infile(*fname);
}
/* Now look for a path qualifying source file name */
#ifdef UNIX
path_end = strrchr(current_filename,'/');
#endif
#ifdef VMS
path_end = strrchr(current_filename,']');
#endif
#ifdef MSDOS /* look for either \ or / at end. */
path_end = strrchr(current_filename,'\\');
if(path_end == (char *)NULL)
path_end = strrchr(current_filename,'/');
#endif
if( path_end == (char *)NULL ) {
if( (fp=open_infile(*fname)) != (FILE *)NULL) /* Not qualified by a path */
return fp;
}
else /* Input file name is qualified by a path */
{
char *local_path;
#ifdef VMS
++path_end; /* need to retain the ']' */
#endif
/* Get a copy of the local path */
if( (local_path=malloc(path_end-current_filename+1))
== (char *)NULL ) {
(void)fprintf(stderr,"Cannot allocate memory for include file path\n");
return (FILE *)NULL;
}
strncpy(local_path,current_filename,path_end-current_filename);
local_path[path_end-current_filename] = '\0';
fp = fopen_with_path(local_path,fname);
(void)free(local_path);
if( fp != (FILE *)NULL ) {
return fp;
}
}
/* If not found, look in directories given
by include_path_list from -include options */
for(p=include_path_list; p!=NULL; p=p->link) {
if( (fp=fopen_with_path(p->include_path,fname)) != (FILE *)NULL)
return fp;
}
/* If not found, look in directory given by
env variable ENV_INCLUDE_VAR (e.g. set by
% setenv INCLUDE ~/myinclude ) */
if( (env_include_var=getenv(ENV_INCLUDE_VAR)) != NULL) {
if( (fp=fopen_with_path(env_include_var,fname)) != (FILE *)NULL)
return fp;
}
/* Still not found: look in systemwide
default directory */
#ifdef DEFAULT_INCLUDE_DIR
if( (fp=fopen_with_path(DEFAULT_INCLUDE_DIR,fname)) != NULL)
return fp;
#endif/* DEFAULT_INCLUDE_DIR */
/* Not found anywhere: fail */
return (FILE *)NULL;
}/*find_include*/
/* Routine to open file with name given by include_path
followed by fname. If successful, fname is replaced
by pointer to full name. */
PRIVATE FILE *
#if HAVE_STDC
fopen_with_path(char *inc_path, char **fname)
#else /* K&R style */
fopen_with_path(inc_path,fname)
char *inc_path, **fname;
#endif /* HAVE_STDC */
{
FILE *fp;
char *tmpname; /* holds name with path prepended */
if( (tmpname = malloc(strlen(inc_path)+strlen(*fname)+2))
== (char *)NULL ) {
(void)fprintf(stderr,"Cannot allocate memory for include file path\n");
return (FILE *)NULL;
}
(void)strcpy(tmpname,inc_path);
/* Add "/" or "\" if not provided */
#ifdef UNIX
if(tmpname[strlen(tmpname)-1] != '/')
(void)strcat(tmpname,"/");
#endif
#ifdef MSDOS
if(tmpname[strlen(tmpname)-1] != '\\')
(void)strcat(tmpname,"\\");
#endif
(void)strcat(tmpname,*fname);
if( (fp=open_infile(tmpname)) != (FILE *)NULL) {
/* Found: save new name in permanent space */
*fname = tmpname;
}
return fp;
}/*fopen_with_path*/
/* Add an include directory path to list of paths */
PRIVATE VOID
#if HAVE_STDC
append_include_path(char *new_path)
#else /* K&R style */
append_include_path(new_path)
char *new_path;
#endif /* HAVE_STDC */
{
IncludePathNode *new_path_node, *p;
if((new_path_node=(IncludePathNode *)malloc(sizeof(IncludePathNode)))
==(IncludePathNode *)NULL) {
(void)fprintf(stderr,"malloc error getting path list\n");
}
else {
new_path_node->link = (IncludePathNode *)NULL;
new_path_node->include_path = new_path;
/* Append the new node at end of list */
if((p=include_path_list) == (IncludePathNode *)NULL)
include_path_list = new_path_node;
else {
while(p->link != (IncludePathNode *)NULL)
p = p->link;
p->link = new_path_node;
}
}
}
|