/* linebreak.c */ /* text filter to perform line breaking. */ /* to be used as if filter in printcap. */ #include #include #include #include #include #ifdef USEA4 int width = 80, length = 70, indent = 0; #else USEA4 int width = 80, length = 66, indent = 0; #endif USEA4 #define TRUE 1 #define FALSE !TRUE #define MAXBUF 256*8 #define TABSPACING 8 void main(short argc,char *argv[]) { unsigned char oneline[MAXBUF],outline[MAXBUF]; char c; unsigned char *strptr; char number[4]; int tabs; int possible; int i,j,k,d; int theend; extern char *optarg; while (( c = getopt( argc, argv, "w:i:l:6:5:n:h" )) != EOF ) { switch ( c ) { case 'w' : width = atoi( optarg ); #ifdef ZEROWIDTH /* * Some version of lpd pass 0 for the page width. */ if ( width == 0 ) { width = 80; } #endif ZEROWIDTH break; case 'i' : indent = atoi( optarg ); break; } } do{ if(fgets(oneline,MAXBUF,stdin)!=NULL){ tabs=FALSE; for(i=strlen(oneline)-1;i>=0&&!tabs;i--){ if(oneline[i]=='\t') tabs=TRUE; if(oneline[i]=='\n') oneline[i]='\0'; if(oneline[i]=='\r') oneline[i]='\0'; } if(!tabs&&strlen(oneline)<=width){ printf("%s\n",oneline); } else{ if(tabs){ theend=FALSE; d=i=j=0; strptr=oneline; while(!theend){ if(strptr[i]=='\0'||(&strptr[i])>=(&oneline[MAXBUF])){ if(strlen(outline)!=0) printf("%s\n",outline); /* This was the problem */ theend=TRUE; } else{ switch(strptr[i]){ case '\0' : theend=TRUE; outline[i+d]='\0'; if(strlen(outline)!=0){ printf("%s\n",outline); strcpy(outline,""); } break; case '\t' : j=i+d; do{ outline[i+d]=' '; outline[i+d+1]='\0'; j=i+d; d++; if((i+d)==(width+1)){ outline[i+d+1]='\0'; printf("%s\n",outline); strptr=&strptr[i]; d=i=j=0; strcpy(outline,""); } else{ if((i+d)>(width+1)){ outline[j-1]='\0'; printf("%s\n",outline); for(k=0;k(width+1)){ outline[j-1]='\0'; printf("%s\n",outline); for(k=0;kwidth){ possible=FALSE; i=width; while(!possible&&i>0){ if(strptr[i]==' '){ strptr[i]='\0'; printf("%s\n",strptr); strptr=&strptr[i+1]; possible=TRUE; } i--; } if(!possible){ strncpy(outline,strptr,width); outline[width]='\0'; printf("%s\n",outline); strptr=&strptr[width]; possible=TRUE; } } if(strlen(strptr)>0){ printf("%s\n",strptr); } } } } } while(!feof(stdin)); }