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

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

version 1.4, 1997/01/26 03:56:52 version 1.5, 1997/01/27 04:06:49
Line 55 
Line 55 
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
   #ifdef  __GNUC__
   #define inline  __inline
   #else   /* !__GNUC__ */
   #define inline
   #endif  /* !__GNUC__ */
   
 /*  /*
  * fmt -- format the concatenation of input files or standard input   * fmt -- format the concatenation of input files or standard input
  * onto standard output.  Designed for use with Mail ~|   * onto standard output.  Designed for use with Mail ~|
Line 90 
Line 96 
 void tabulate __P((char *));  void tabulate __P((char *));
 void leadin __P((void));  void leadin __P((void));
 char *savestr __P((char *));  char *savestr __P((char *));
   char *extstr __P((char *, int *, int));
 int  ispref __P((char *, char *));  int  ispref __P((char *, char *));
 int  ishead __P((char *));  int  ishead __P((char *));
   
Line 163 
Line 170 
 fmt(fi)  fmt(fi)
         FILE *fi;          FILE *fi;
 {  {
         static char *linebuf = 0, *canonb = 0;          static char *linebuf, *canonb;
           static int lbufsize, cbufsize;
         register char *cp, *cp2, cc;          register char *cp, *cp2, cc;
         register int c, col;          register int c, col;
 #define CHUNKSIZE 1024  #define CHUNKSIZE 1024
         static int lbufsize = 0, cbufsize = 0;  
   
         if (center) {          if (center) {
                 if ((linebuf = malloc(BUFSIZ)) == NOSTR)                  register int len;
                         errx(1, "Ran out of memory");  
                   linebuf = extstr(linebuf, &lbufsize, NULL);
                 for (;;) {                  for (;;) {
                         cp = fgets(linebuf, BUFSIZ, fi);                          len = 0;
                         if (!cp)                          for (;;) {
                                   if (!fgets(linebuf + len, lbufsize - len, fi))
                                           break;
                                   len = strlen(linebuf);
                                   if (linebuf[len-1] == '\n' || feof(fi))
                                           break;
                                   linebuf = extstr(linebuf, &lbufsize, NULL);
                           }
                           if (len == 0)
                                 return;                                  return;
                           cp = linebuf;
                         while (*cp && isspace(*cp))                          while (*cp && isspace(*cp))
                                 cp++;                                  cp++;
                         cp2 = cp + strlen(cp) - 1;                          cp2 = cp + strlen(cp) - 1;
Line 201 
Line 218 
                 while (c != '\n' && c != EOF) {                  while (c != '\n' && c != EOF) {
                         if (cp - linebuf >= lbufsize) {                          if (cp - linebuf >= lbufsize) {
                                 int offset = cp - linebuf;                                  int offset = cp - linebuf;
                                 lbufsize += CHUNKSIZE;                                  linebuf = extstr(linebuf, &lbufsize, NULL);
                                 linebuf = realloc(linebuf, lbufsize);  
                                 if(linebuf == 0)  
                                         abort();  
                                 cp = linebuf + offset;                                  cp = linebuf + offset;
                         }                          }
                         if (c == '\b') {                          if (c == '\b') {
Line 246 
Line 260 
                                 col++;                                  col++;
                                 if (cp2 - canonb >= cbufsize) {                                  if (cp2 - canonb >= cbufsize) {
                                         int offset = cp2 - canonb;                                          int offset = cp2 - canonb;
                                         cbufsize += CHUNKSIZE;                                          canonb = extstr(canonb, &cbufsize, NULL);
                                         canonb = realloc(canonb, cbufsize);  
                                         if(canonb == 0)  
                                                 abort();  
                                         cp2 = canonb + offset;                                          cp2 = canonb + offset;
                                 }                                  }
                                 *cp2++ = cc;                                  *cp2++ = cc;
Line 258 
Line 269 
                         do {                          do {
                                 if (cp2 - canonb >= cbufsize) {                                  if (cp2 - canonb >= cbufsize) {
                                         int offset = cp2 - canonb;                                          int offset = cp2 - canonb;
                                         cbufsize += CHUNKSIZE;                                          canonb = extstr(canonb, &cbufsize, NULL);
                                         canonb = realloc(canonb, cbufsize);  
                                         if(canonb == 0)  
                                                 abort();  
                                         cp2 = canonb + offset;                                          cp2 = canonb + offset;
                                 }                                  }
                                 *cp2++ = ' ';                                  *cp2++ = ' ';
Line 313 
Line 321 
         if ((h = ishead(cp)))          if ((h = ishead(cp)))
                 oflush(), mark = lineno;                  oflush(), mark = lineno;
         if (lineno - mark < 3 && lineno - mark > 0)          if (lineno - mark < 3 && lineno - mark > 0)
                 for (hp = &headnames[0]; *hp != (char *) 0; hp++)                  for (hp = &headnames[0]; *hp != NULL; hp++)
                         if (ispref(*hp, cp)) {                          if (ispref(*hp, cp)) {
                                 h = 1;                                  h = 1;
                                 oflush();                                  oflush();
Line 324 
Line 332 
         pfx = np;          pfx = np;
         if (h)          if (h)
                 pack(cp, strlen(cp));                  pack(cp, strlen(cp));
         else    split(cp);          else
                   split(cp);
         if (h)          if (h)
                 oflush();                  oflush();
         lineno++;          lineno++;
Line 341 
Line 350 
         char line[];          char line[];
 {  {
         register char *cp, *cp2;          register char *cp, *cp2;
         char word[BUFSIZ];          static char *word;
           static int wordsize;
         int wordl;              /* LIZ@UOM 6/18/85 */          int wordl;              /* LIZ@UOM 6/18/85 */
   
           if (strlen(line) >= wordsize)
                   word = extstr(word, &wordsize, strlen(line) + 1);
   
         cp = line;          cp = line;
         while (*cp) {          while (*cp) {
                 cp2 = word;                  cp2 = word;
Line 388 
Line 401 
  * there ain't nothing in there yet.  At the bottom of this whole mess,   * there ain't nothing in there yet.  At the bottom of this whole mess,
  * leading tabs are reinserted.   * leading tabs are reinserted.
  */   */
 char    outbuf[BUFSIZ];                 /* Sandbagged output line image */  static char *outbuf;                    /* Sandbagged output line image */
 char    *outp;                          /* Pointer in above */  static int   obufsize;                  /* Size of outbuf */
   static char *outp;                      /* Pointer in above */
   
 /*  /*
  * Initialize the output section.   * Initialize the output section.
Line 439 
Line 453 
          */           */
         s = outp - outbuf;          s = outp - outbuf;
         t = wl + s;          t = wl + s;
           if (t + 1 > obufsize) {
                   outbuf = extstr(outbuf, &obufsize, t + 1);
                   outp = outbuf + s;
           }
         if ((t <= goal_length) ||          if ((t <= goal_length) ||
             ((t <= max_length) && (t - goal_length <= goal_length - s))) {              ((t <= max_length) && (t - goal_length <= goal_length - s))) {
                 /*                  /*
                  * In like flint!                   * In like flint!
                  */                   */
                 for (cp = word; *cp; *outp++ = *cp++);                  for (cp = word; *cp; *outp++ = *cp++)
                           ;
                 return;                  return;
         }          }
         if (s > pfx) {          if (s > pfx) {
                 oflush();                  oflush();
                 leadin();                  leadin();
         }          }
         for (cp = word; *cp; *outp++ = *cp++);          for (cp = word; *cp; *outp++ = *cp++)
                   ;
 }  }
   
 /*  /*
Line 519 
Line 539 
         register int b;          register int b;
         register char *cp;          register char *cp;
   
           if (obufsize == 0 || (outp != NULL && outp - outbuf <= pfx))
                   outbuf = extstr(outbuf, &obufsize, pfx);
         for (b = 0, cp = outbuf; b < pfx; b++)          for (b = 0, cp = outbuf; b < pfx; b++)
                 *cp++ = ' ';                  *cp++ = ' ';
         outp = cp;          outp = cp;
Line 552 
Line 574 
         while (*s1++ == *s2)          while (*s1++ == *s2)
                 ;                  ;
         return (*s1 == '\0');          return (*s1 == '\0');
   }
   
   inline char *
   extstr(str, size, gsize)
           char *str;
           int *size;
           int gsize;
   {
           do {
                   *size += CHUNKSIZE;
           } while (gsize && *size < gsize);
   
           if ((str = realloc(str, *size)) == NULL)
                   errx(1, "Ran out of memory");
   
           return(str);
 }  }

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