[BACK]Return to fsplit.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / fsplit

Diff for /src/usr.bin/fsplit/Attic/fsplit.c between version 1.4 and 1.5

version 1.4, 1998/11/16 06:21:58 version 1.5, 1999/12/06 00:34:26
Line 40 
Line 40 
 static char copyright[] =  static char copyright[] =
 "@(#) Copyright (c) 1983, 1993\n\  "@(#) Copyright (c) 1983, 1993\n\
         The Regents of the University of California.  All rights reserved.\n";          The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */  #endif                          /* not lint */
   
 #ifndef lint  #ifndef lint
 /*static char sccsid[] = "from: @(#)fsplit.c    8.1 (Berkeley) 6/6/93";*/  /*static char sccsid[] = "from: @(#)fsplit.c    8.1 (Berkeley) 6/6/93";*/
 static char rcsid[] = "$OpenBSD$";  static char rcsid[] = "$OpenBSD$";
 #endif /* not lint */  #endif                          /* not lint */
   
 #include <ctype.h>  #include <ctype.h>
 #include <stdio.h>  #include <stdio.h>
Line 55 
Line 55 
 #include <sys/stat.h>  #include <sys/stat.h>
 #include <err.h>  #include <err.h>
   
 void    badparms __P(());  void badparms __P(());
 void    get_name __P((char *, int));  void get_name __P((char *, int));
 int     lname __P((char *));  int lname __P((char *));
 int     getline __P((void));  int getline __P((void));
 int     lend __P((void));  int lend __P((void));
 int     scan_name __P((char *, char *));  int scan_name __P((char *, char *));
 int     saveit __P((char *));  int saveit __P((char *));
   
 /*  /*
  *      usage:          fsplit [-e efile] ... [file]   *      usage:          fsplit [-e efile] ... [file]
Line 77 
Line 77 
  *      If -e option is used, then only those subprograms named in the -e   *      If -e option is used, then only those subprograms named in the -e
  *              option are split off; e.g.:   *              option are split off; e.g.:
  *                      fsplit -esub1 -e sub2 prog.f   *                      fsplit -esub1 -e sub2 prog.f
  *              isolates sub1 and sub2 in sub1.f and sub2.f.  The space   *              isolates sub1 and sub2 in sub1.f and sub2.f.  The space
  *              after -e is optional.   *              after -e is optional.
  *   *
  *      Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.   *      Modified Feb., 1983 by Jerry Berkman, Computing Services, U.C. Berkeley.
Line 89 
Line 89 
  */   */
   
 #define BSZ 512  #define BSZ 512
 char buf[BSZ];  char    buf[BSZ];
 FILE *ifp;  FILE   *ifp;
 char    x[]="zzz000.f",  char    x[] = "zzz000.f", mainp[] = "main000.f", blkp[] = "blkdta000.f";
         mainp[]="main000.f",  char   *look(), *skiplab(), *functs();
         blkp[]="blkdta000.f";  
 char *look(), *skiplab(), *functs();  
   
 #define TRUE 1  #define TRUE 1
 #define FALSE 0  #define FALSE 0
 int     extr = FALSE,  int     extr = FALSE, extrknt = -1, extrfnd[100];
         extrknt = -1,  char    extrbuf[1000], *extrnames[100];
         extrfnd[100];  
 char    extrbuf[1000],  
         *extrnames[100];  
 struct stat sbuf;  struct stat sbuf;
   
 #define trim(p) while (*p == ' ' || *p == '\t') p++  #define trim(p) while (*p == ' ' || *p == '\t') p++
   
 int  int
 main(argc, argv)  main(argc, argv)
 char **argv;          char  **argv;
 {  {
         register FILE *ofp;     /* output file */          register FILE *ofp;     /* output file */
         register int rv;        /* 1 if got card in output file, 0 otherwise */          register int rv;        /* 1 if got card in output file, 0 otherwise */
         register char *ptr;          register char *ptr;
         int nflag,              /* 1 if got name of subprog., 0 otherwise */          int     nflag,          /* 1 if got name of subprog., 0 otherwise */
                 retval,                  retval, i;
                 i;          char    name[20], *extrptr = extrbuf;
         char name[20],  
                 *extrptr = extrbuf;  
   
         /*  scan -e options */          /* scan -e options */
         while ( argc > 1  && argv[1][0] == '-' && argv[1][1] == 'e') {          while (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') {
                 extr = TRUE;                  extr = TRUE;
                 ptr = argv[1] + 2;                  ptr = argv[1] + 2;
                 if(!*ptr) {                  if (!*ptr) {
                         argc--;                          argc--;
                         argv++;                          argv++;
                         if(argc <= 1) badparms();                          if (argc <= 1)
                                   badparms();
                         ptr = argv[1];                          ptr = argv[1];
                 }                  }
                 extrknt = extrknt + 1;                  extrknt = extrknt + 1;
                 extrnames[extrknt] = extrptr;                  extrnames[extrknt] = extrptr;
                 extrfnd[extrknt] = FALSE;                  extrfnd[extrknt] = FALSE;
                 while(*ptr) *extrptr++ = *ptr++;                  while (*ptr)
                           *extrptr++ = *ptr++;
                 *extrptr++ = 0;                  *extrptr++ = 0;
                 argc--;                  argc--;
                 argv++;                  argv++;
Line 141 
Line 136 
   
         if (argc > 2)          if (argc > 2)
                 badparms();                  badparms();
         else if (argc == 2) {  
                 if ((ifp = fopen(argv[1], "r")) == NULL)  
                         err(1, argv[1]);  
         }  
         else          else
                 ifp = stdin;                  if (argc == 2) {
     for(;;) {                          if ((ifp = fopen(argv[1], "r")) == NULL)
         /* look for a temp file that doesn't correspond to an existing file */                                  err(1, argv[1]);
         get_name(x, 3);  
         ofp = fopen(x, "w");  
         nflag = 0;  
         rv = 0;  
         while (getline() > 0) {  
                 rv = 1;  
                 fprintf(ofp, "%s", buf);  
                 if (lend())             /* look for an 'end' statement */  
                         break;  
                 if (nflag == 0)         /* if no name yet, try and find one */  
                         nflag = lname(name);  
         }  
         fclose(ofp);  
         if (rv == 0) {                  /* no lines in file, forget the file */  
                 unlink(x);  
                 retval = 0;  
                 for ( i = 0; i <= extrknt; i++ )  
                         if(!extrfnd[i]) {  
                                 retval = 1;  
                                 warnx("%s not found", extrnames[i]);  
                         }  
                 exit( retval );  
         }  
         if (nflag) {                    /* rename the file */  
                 if(saveit(name)) {  
                         if (stat(name, &sbuf) < 0 ) {  
                                 link(x, name);  
                                 unlink(x);  
                                 printf("%s\n", name);  
                                 continue;  
                         } else if (strcmp(name, x) == 0) {  
                                 printf("%s\n", x);  
                                 continue;  
                         }  
                         printf("%s already exists, put in %s\n", name, x);  
                         continue;  
                 } else                  } else
                           ifp = stdin;
           for (;;) {
                   /* look for a temp file that doesn't correspond to an existing
                    * file */
                   get_name(x, 3);
                   ofp = fopen(x, "w");
                   nflag = 0;
                   rv = 0;
                   while (getline() > 0) {
                           rv = 1;
                           fprintf(ofp, "%s", buf);
                           if (lend())     /* look for an 'end' statement */
                                   break;
                           if (nflag == 0) /* if no name yet, try and find one */
                                   nflag = lname(name);
                   }
                   fclose(ofp);
                   if (rv == 0) {  /* no lines in file, forget the file */
                         unlink(x);                          unlink(x);
                           retval = 0;
                           for (i = 0; i <= extrknt; i++)
                                   if (!extrfnd[i]) {
                                           retval = 1;
                                           warnx("%s not found", extrnames[i]);
                                   }
                           exit(retval);
                   }
                   if (nflag) {    /* rename the file */
                           if (saveit(name)) {
                                   if (stat(name, &sbuf) < 0) {
                                           link(x, name);
                                           unlink(x);
                                           printf("%s\n", name);
                                           continue;
                                   } else
                                           if (strcmp(name, x) == 0) {
                                                   printf("%s\n", x);
                                                   continue;
                                           }
                                   printf("%s already exists, put in %s\n", name, x);
                                   continue;
                           } else
                                   unlink(x);
                         continue;                          continue;
                   }
                   if (!extr)
                           printf("%s\n", x);
                   else
                           unlink(x);
         }          }
         if(!extr)  
                 printf("%s\n", x);  
         else  
                 unlink(x);  
     }  
 }  }
   
 void  void
Line 204 
Line 201 
   
 int  int
 saveit(name)  saveit(name)
 char *name;          char   *name;
 {  {
         int i;          int     i;
         char    fname[50],          char    fname[50], *fptr = fname;
                 *fptr = fname;  
   
         if(!extr) return(1);          if (!extr)
         while(*name) *fptr++ = *name++;                  return (1);
           while (*name)
                   *fptr++ = *name++;
         *--fptr = 0;          *--fptr = 0;
         *--fptr = 0;          *--fptr = 0;
         for ( i=0 ; i<=extrknt; i++ )          for (i = 0; i <= extrknt; i++)
                 if( strcmp(fname, extrnames[i]) == 0 ) {                  if (strcmp(fname, extrnames[i]) == 0) {
                         extrfnd[i] = TRUE;                          extrfnd[i] = TRUE;
                         return(1);                          return (1);
                 }                  }
         return(0);          return (0);
 }  }
   
 void  void
 get_name(name, letters)  get_name(name, letters)
 char *name;          char   *name;
 int letters;          int     letters;
 {  {
         register char *ptr;          register char *ptr;
   
Line 236 
Line 234 
                                 break;                                  break;
                         *ptr = '0';                          *ptr = '0';
                 }                  }
                 if(ptr < name + letters)                  if (ptr < name + letters)
                         errx(1, "ran out of file names");                          errx(1, "ran out of file names");
         }          }
 }  }
Line 246 
Line 244 
 {  {
         register char *ptr;          register char *ptr;
   
         for (ptr = buf; ptr < &buf[BSZ]; ) {          for (ptr = buf; ptr < &buf[BSZ];) {
                 *ptr = getc(ifp);                  *ptr = getc(ifp);
                 if (feof(ifp))                  if (feof(ifp))
                         return (-1);                          return (-1);
Line 255 
Line 253 
                         return (1);                          return (1);
                 }                  }
         }          }
         while (getc(ifp) != '\n' && feof(ifp) == 0) ;          while (getc(ifp) != '\n' && feof(ifp) == 0);
         warnx("line truncated to %d characters", BSZ);          warnx("line truncated to %d characters", BSZ);
         return (1);          return (1);
 }  }
   
 /* return 1 for 'end' alone on card (up to col. 72),  0 otherwise */  /* return 1 for 'end' alone on card (up to col. 72),  0 otherwise */
 int  int
 lend()  lend()
Line 269 
Line 266 
         if ((p = skiplab(buf)) == 0)          if ((p = skiplab(buf)) == 0)
                 return (0);                  return (0);
         trim(p);          trim(p);
         if (*p != 'e' && *p != 'E') return(0);          if (*p != 'e' && *p != 'E')
                   return (0);
         p++;          p++;
         trim(p);          trim(p);
         if (*p != 'n' && *p != 'N') return(0);          if (*p != 'n' && *p != 'N')
                   return (0);
         p++;          p++;
         trim(p);          trim(p);
         if (*p != 'd' && *p != 'D') return(0);          if (*p != 'd' && *p != 'D')
                   return (0);
         p++;          p++;
         trim(p);          trim(p);
         if (p - buf >= 72 || *p == '\n')          if (p - buf >= 72 || *p == '\n')
                 return (1);                  return (1);
         return (0);          return (0);
 }  }
   /*              check for keywords for subprograms
 /*              check for keywords for subprograms  
                 return 0 if comment card, 1 if found                  return 0 if comment card, 1 if found
                 name and put in arg string. invent name for unnamed                  name and put in arg string. invent name for unnamed
                 block datas and main programs.          */                  block datas and main programs.          */
 int  int
 lname(s)  lname(s)
 char *s;          char   *s;
 {  {
 #       define LINESIZE 80  #define LINESIZE 80
         register char *ptr, *p;          register char *ptr, *p;
         char    line[LINESIZE], *iptr = line;          char    line[LINESIZE], *iptr = line;
   
         /* first check for comment cards */          /* first check for comment cards */
         if(buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*') return(0);          if (buf[0] == 'c' || buf[0] == 'C' || buf[0] == '*')
                   return (0);
         ptr = buf;          ptr = buf;
         while (*ptr == ' ' || *ptr == '\t') ptr++;          while (*ptr == ' ' || *ptr == '\t')
         if(*ptr == '\n') return(0);                  ptr++;
           if (*ptr == '\n')
                   return (0);
   
   
         ptr = skiplab(buf);          ptr = skiplab(buf);
Line 307 
Line 309 
                 return (0);                  return (0);
   
   
         /*  copy to buffer and converting to lower case */          /* copy to buffer and converting to lower case */
         p = ptr;          p = ptr;
         while (*p && p <= &buf[71] ) {          while (*p && p <= &buf[71]) {
            *iptr = isupper(*p) ? tolower(*p) : *p;                  *iptr = isupper(*p) ? tolower(*p) : *p;
            iptr++;                  iptr++;
            p++;                  p++;
         }          }
         *iptr = '\n';          *iptr = '\n';
   
         if ((ptr = look(line, "subroutine")) != 0 ||          if ((ptr = look(line, "subroutine")) != 0 ||
             (ptr = look(line, "function")) != 0 ||              (ptr = look(line, "function")) != 0 ||
             (ptr = functs(line)) != 0) {              (ptr = functs(line)) != 0) {
                 if(scan_name(s, ptr)) return(1);                  if (scan_name(s, ptr))
                 strcpy( s, x);                          return (1);
         } else if((ptr = look(line, "program")) != 0) {                  strcpy(s, x);
                 if(scan_name(s, ptr)) return(1);          } else
                 get_name( mainp, 4);                  if ((ptr = look(line, "program")) != 0) {
                 strcpy( s, mainp);                          if (scan_name(s, ptr))
         } else if((ptr = look(line, "blockdata")) != 0) {                                  return (1);
                 if(scan_name(s, ptr)) return(1);                          get_name(mainp, 4);
                 get_name( blkp, 6);                          strcpy(s, mainp);
                 strcpy( s, blkp);                  } else
         } else if((ptr = functs(line)) != 0) {                          if ((ptr = look(line, "blockdata")) != 0) {
                 if(scan_name(s, ptr)) return(1);                                  if (scan_name(s, ptr))
                 strcpy( s, x);                                          return (1);
         } else {                                  get_name(blkp, 6);
                 get_name( mainp, 4);                                  strcpy(s, blkp);
                 strcpy( s, mainp);                          } else
         }                                  if ((ptr = functs(line)) != 0) {
         return(1);                                          if (scan_name(s, ptr))
                                                   return (1);
                                           strcpy(s, x);
                                   } else {
                                           get_name(mainp, 4);
                                           strcpy(s, mainp);
                                   }
           return (1);
 }  }
   
 int  int
 scan_name(s, ptr)  scan_name(s, ptr)
 char *s, *ptr;          char   *s, *ptr;
 {  {
         char *sptr;          char   *sptr;
   
         /* scan off the name */          /* scan off the name */
         trim(ptr);          trim(ptr);
Line 354 
Line 363 
                 ptr++;                  ptr++;
         }          }
   
         if (sptr == s) return(0);          if (sptr == s)
                   return (0);
   
         *sptr++ = '.';          *sptr++ = '.';
         *sptr++ = 'f';          *sptr++ = 'f';
         *sptr++ = 0;          *sptr++ = 0;
         return(1);          return (1);
 }  }
   
 char *functs(p)  char   *
 char *p;  functs(p)
           char   *p;
 {  {
         register char *ptr;          register char *ptr;
   
 /*      look for typed functions such as: real*8 function,  /*      look for typed functions such as: real*8 function,
                 character*16 function, character*(*) function  */                  character*16 function, character*(*) function  */
   
         if((ptr = look(p,"character")) != 0 ||          if ((ptr = look(p, "character")) != 0 ||
            (ptr = look(p,"logical")) != 0 ||              (ptr = look(p, "logical")) != 0 ||
            (ptr = look(p,"real")) != 0 ||              (ptr = look(p, "real")) != 0 ||
            (ptr = look(p,"integer")) != 0 ||              (ptr = look(p, "integer")) != 0 ||
            (ptr = look(p,"doubleprecision")) != 0 ||              (ptr = look(p, "doubleprecision")) != 0 ||
            (ptr = look(p,"complex")) != 0 ||              (ptr = look(p, "complex")) != 0 ||
            (ptr = look(p,"doublecomplex")) != 0 ) {              (ptr = look(p, "doublecomplex")) != 0) {
                 while ( *ptr == ' ' || *ptr == '\t' || *ptr == '*'                  while (*ptr == ' ' || *ptr == '\t' || *ptr == '*'
                         || (*ptr >= '0' && *ptr <= '9')                      || (*ptr >= '0' && *ptr <= '9')
                         || *ptr == '(' || *ptr == ')') ptr++;                      || *ptr == '(' || *ptr == ')')
                 ptr = look(ptr,"function");                          ptr++;
                 return(ptr);                  ptr = look(ptr, "function");
         }                  return (ptr);
         else          } else
                 return(0);                  return (0);
 }  }
   
 /*      if first 6 col. blank, return ptr to col. 7,  /*      if first 6 col. blank, return ptr to col. 7,
         if blanks and then tab, return ptr after tab,          if blanks and then tab, return ptr after tab,
         else return 0 (labelled statement, comment or continuation */          else return 0 (labelled statement, comment or continuation */
 char *skiplab(p)  char   *
 char *p;  skiplab(p)
           char   *p;
 {  {
         register char *ptr;          register char *ptr;
   
Line 406 
Line 417 
         }          }
         return (ptr);          return (ptr);
 }  }
   
 /*      return 0 if m doesn't match initial part of s;  /*      return 0 if m doesn't match initial part of s;
         otherwise return ptr to next char after m in s */          otherwise return ptr to next char after m in s */
 char *look(s, m)  char   *
 char *s, *m;  look(s, m)
           char   *s, *m;
 {  {
         register char *sp, *mp;          register char *sp, *mp;
   
         sp = s; mp = m;          sp = s;
           mp = m;
         while (*mp) {          while (*mp) {
                 trim(sp);                  trim(sp);
                 if (*sp++ != *mp++)                  if (*sp++ != *mp++)

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5