/***********************************************************************
* Copyright (C) 1999 Winfried Beer                                     *
*                                                                      *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or    *
* (at your option) any later version.                                  *
*                                                                      *
* This program is distributed in the hope that it will be useful,      *
* but WITHOUT ANY WARRANTY; without even the implied warranty of       *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
* GNU General Public License for more details.                         *
*                                                                      *
* You should have received a copy of the GNU General Public License    *
* along with this program; if not, write to the Free Software          *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
*                                                                      *
***********************************************************************/

/***********************************************************************
* linebreak.c                                                          *
************************************************************************
* TASK:                                                                *
*   Break long lines. (e.g. for use with sed or grep)                  *
*                                                                      *
************************************************************************
* AUTHOR:  W. Beer, Apr 1999                                           *
*                                                                      *
* HISTORY:                                                             *
*   13-APR-1999: first version                                         *
*                                                                      *
*                                                                      *
************************************************************************
* USER HINTS:                                                          *
*                                                                      *
* PROGRAMMER HINTS:                                                    *
*                                                                      *
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define MAXLEN 512+1

int debug=0;

/**********************************************************************/
int main(int argc, char **argv)
{
  int f,len,i;
  char line[MAXLEN];
  FILE *fp;
  long int linecnt;
  int maxrlen;

  fp=stdin;
  for (f=1; f<argc; f++){
    if ( strcmp(argv[f],"-d") == 0 ) {debug=1;}
    else if ( strlen(argv[f])>0 && argv[f][0]!='-') {
      fp=fopen(argv[f], "r"); 
    }
  }

    if (fp!=NULL) {
      linecnt=0;
      maxrlen=MAXLEN;
      while(!feof(fp)) {
        if (fgets(line, maxrlen, fp)) {
          maxrlen=MAXLEN;

          len=strlen(line); /* shorten MS-DOS linebreak cr/lf  to only lf */
          if (len>=2 && line[len-2]=='\r') {
            line[len-2]='\n';
            line[len-1]='\0';
          }

          len=strlen(line);
          if (len>0) {
            if (line[len-1]!='\n' && (len>MAXLEN-3)) { /* forced line break */
              for (i=len-1; (i>0) && (i>MAXLEN-40); i--) {
                if (line[i]==' ') { 
                  line[i]='\n';
                  maxrlen=maxrlen-((len-1)-i);
                  i=0;
                  if (debug) {fprintf (stderr, "Break line %ld.\n", linecnt);}
                }
              }
              if (i!=-1) {
                if (debug) {fprintf (stderr, "Break word in line %ld.\n" , linecnt);}  /* break within word */
                line[len]='\n'; 
                line[len+1]='\0';
              }
            }

            fputs(line, stdout);
            len=strlen(line);
            if (len>0 && line[len-1]=='\n') linecnt++; /* count only full lines */
          }
        } /*  {fprintf (stderr, "Read error.\n"); return 2;} */
      }
      fclose(fp);
    }else{
      fprintf (stderr, "Can't open file %s.\n",argv[f]);
    }

  return 0;
}


