1*4887Schin /*********************************************************************** 2*4887Schin * * 3*4887Schin * This software is part of the ast package * 4*4887Schin * Copyright (c) 1992-2007 AT&T Knowledge Ventures * 5*4887Schin * and is licensed under the * 6*4887Schin * Common Public License, Version 1.0 * 7*4887Schin * by AT&T Knowledge Ventures * 8*4887Schin * * 9*4887Schin * A copy of the License is available at * 10*4887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 11*4887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12*4887Schin * * 13*4887Schin * Information and Software Systems Research * 14*4887Schin * AT&T Research * 15*4887Schin * Florham Park NJ * 16*4887Schin * * 17*4887Schin * Glenn Fowler <gsf@research.att.com> * 18*4887Schin * David Korn <dgk@research.att.com> * 19*4887Schin * * 20*4887Schin ***********************************************************************/ 21*4887Schin #pragma prototyped 22*4887Schin /* 23*4887Schin * David Korn 24*4887Schin * AT&T Bell Laboratories 25*4887Schin * 26*4887Schin * fold 27*4887Schin */ 28*4887Schin 29*4887Schin static const char usage[] = 30*4887Schin "[-?\n@(#)$Id: fold (AT&T Research) 2004-11-18 $\n]" 31*4887Schin USAGE_LICENSE 32*4887Schin "[+NAME?fold - fold lines]" 33*4887Schin "[+DESCRIPTION?\bfold\b is a filter that folds lines from its input, " 34*4887Schin "breaking the lines to have a maximum of \awidth\a column " 35*4887Schin "positions (or bytes if the \b-b\b option is specified). Lines " 36*4887Schin "are broken by the insertion of a newline character such that " 37*4887Schin "each output line is the maximum width possible that does not " 38*4887Schin "exceed the specified number of column positions, (or bytes). A line " 39*4887Schin "will not be broken in the middle of a character.] " 40*4887Schin "[+?Unless the \b-b\b option is specified, the following will be treated " 41*4887Schin "specially:]{" 42*4887Schin "[+carriage-return?The current count of line width will be set " 43*4887Schin "to zero. \bfold\b will not insert a newline immediately " 44*4887Schin "before or after a carriage-return.]" 45*4887Schin "[+backspace?If positive, the current count of line width will be " 46*4887Schin "decremented by one. \bfold\b will not insert a newline " 47*4887Schin "immediately before or after a backspace.]" 48*4887Schin "[+tab?Each tab character encountered will advance the column " 49*4887Schin "position to the next tab stop. Tab stops are at each " 50*4887Schin "column position \an\a, where \an\a modulo 8 equals 1.]" 51*4887Schin "}" 52*4887Schin "[+?If no \afile\a is given, or if the \afile\a is \b-\b, \bfold\b " 53*4887Schin "reads from standard input. The start of the file is defined " 54*4887Schin "as the current offset.]" 55*4887Schin 56*4887Schin "[b:bytes?Count bytes rather than columns so that each carriage-return, " 57*4887Schin "backspace, and tab counts as 1.]" 58*4887Schin "[c:continue?Emit \atext\a at line splits.]:[text:='\\n']" 59*4887Schin "[d:delimiter?Break at \adelim\a boundaries.]:[delim]" 60*4887Schin "[s:spaces?Break at word boundaries. If the line contains any blanks, " 61*4887Schin "(spaces or tabs), within the first \awidth\a column positions or " 62*4887Schin "bytes, the line is broken after the last blank meeting the " 63*4887Schin "\awidth\a constraint.]" 64*4887Schin "[w:width]#[width:=80?Use a maximum line length of \awidth\a columns " 65*4887Schin "instead of the default.]" 66*4887Schin "\n" 67*4887Schin "\n[file ...]\n" 68*4887Schin "\n" 69*4887Schin "[+EXIT STATUS?]{" 70*4887Schin "[+0?All files processed successfully.]" 71*4887Schin "[+>0?An error occurred.]" 72*4887Schin "}" 73*4887Schin "[+SEE ALSO?\bpaste\b(1)]" 74*4887Schin ; 75*4887Schin 76*4887Schin 77*4887Schin #include <cmd.h> 78*4887Schin 79*4887Schin #define WIDTH 80 80*4887Schin #define TABSIZE 8 81*4887Schin 82*4887Schin #define T_EOF 1 83*4887Schin #define T_NL 2 84*4887Schin #define T_BS 3 85*4887Schin #define T_TAB 4 86*4887Schin #define T_SP 5 87*4887Schin #define T_RET 6 88*4887Schin 89*4887Schin static void fold(Sfio_t *in, Sfio_t *out, register int width, const char *cont, size_t contsize, char *cols) 90*4887Schin { 91*4887Schin register char *cp, *first; 92*4887Schin register int n, col=0, x=0; 93*4887Schin register char *last_space=0; 94*4887Schin cols[0] = 0; 95*4887Schin for (;;) 96*4887Schin { 97*4887Schin if (!(cp = sfgetr(in,'\n',0))) 98*4887Schin { 99*4887Schin if (!(cp = sfgetr(in,'\n',-1)) || (n = sfvalue(in)) <= 0) 100*4887Schin break; 101*4887Schin x = cp[--n]; 102*4887Schin cp[n] = '\n'; 103*4887Schin } 104*4887Schin /* special case -b since no column adjustment is needed */ 105*4887Schin if(cols['\b']==0 && (n=sfvalue(in))<=width) 106*4887Schin { 107*4887Schin sfwrite(out,cp,n); 108*4887Schin continue; 109*4887Schin } 110*4887Schin first = cp; 111*4887Schin col = 0; 112*4887Schin last_space = 0; 113*4887Schin for(;;) 114*4887Schin { 115*4887Schin while((n=cols[*(unsigned char*)cp++])==0); 116*4887Schin while((cp-first) > (width-col)) 117*4887Schin { 118*4887Schin if(last_space) 119*4887Schin col = last_space - first; 120*4887Schin else 121*4887Schin col = width-col; 122*4887Schin sfwrite(out,first,col); 123*4887Schin first += col; 124*4887Schin col = 0; 125*4887Schin last_space = 0; 126*4887Schin if(cp>first+1 || (n!=T_NL && n!=T_BS)) 127*4887Schin sfwrite(out, cont, contsize); 128*4887Schin } 129*4887Schin switch(n) 130*4887Schin { 131*4887Schin case T_NL: 132*4887Schin if(x) 133*4887Schin *(cp-1) = x; 134*4887Schin break; 135*4887Schin case T_RET: 136*4887Schin col = 0; 137*4887Schin continue; 138*4887Schin case T_BS: 139*4887Schin if((cp+(--col)-first)>0) 140*4887Schin col--; 141*4887Schin continue; 142*4887Schin case T_TAB: 143*4887Schin n = (TABSIZE-1) - (cp+col-1-first)&(TABSIZE-1); 144*4887Schin col +=n; 145*4887Schin if((cp-first) > (width-col)) 146*4887Schin { 147*4887Schin sfwrite(out,first,(--cp)-first); 148*4887Schin sfwrite(out, cont, contsize); 149*4887Schin first = cp; 150*4887Schin col = TABSIZE-1; 151*4887Schin last_space = 0; 152*4887Schin continue; 153*4887Schin } 154*4887Schin if(cols[' ']) 155*4887Schin last_space = cp; 156*4887Schin continue; 157*4887Schin case T_SP: 158*4887Schin last_space = cp; 159*4887Schin continue; 160*4887Schin default: 161*4887Schin continue; 162*4887Schin } 163*4887Schin break; 164*4887Schin } 165*4887Schin sfwrite(out,first,cp-first); 166*4887Schin } 167*4887Schin } 168*4887Schin 169*4887Schin int 170*4887Schin b_fold(int argc, char *argv[], void* context) 171*4887Schin { 172*4887Schin register int n, width=WIDTH; 173*4887Schin register Sfio_t *fp; 174*4887Schin register char *cp; 175*4887Schin char *cont="\n"; 176*4887Schin size_t contsize = 1; 177*4887Schin char cols[1<<CHAR_BIT]; 178*4887Schin 179*4887Schin cmdinit(argc, argv, context, ERROR_CATALOG, 0); 180*4887Schin memset(cols, 0, sizeof(cols)); 181*4887Schin cols['\t'] = T_TAB; 182*4887Schin cols['\b'] = T_BS; 183*4887Schin cols['\n'] = T_NL; 184*4887Schin cols['\r'] = T_RET; 185*4887Schin for (;;) 186*4887Schin { 187*4887Schin switch (optget(argv, usage)) 188*4887Schin { 189*4887Schin case 0: 190*4887Schin break; 191*4887Schin case 'b': 192*4887Schin cols['\r'] = cols['\b'] = 0; 193*4887Schin cols['\t'] = cols[' ']; 194*4887Schin continue; 195*4887Schin case 'c': 196*4887Schin contsize = stresc(cont = strdup(opt_info.arg)); 197*4887Schin continue; 198*4887Schin case 'd': 199*4887Schin if (n = *opt_info.arg) 200*4887Schin cols[n] = T_SP; 201*4887Schin continue; 202*4887Schin case 's': 203*4887Schin cols[' '] = T_SP; 204*4887Schin if(cols['\t']==0) 205*4887Schin cols['\t'] = T_SP; 206*4887Schin continue; 207*4887Schin case 'w': 208*4887Schin if ((width = opt_info.num) <= 0) 209*4887Schin error(2, "%d: width must be positive", opt_info.num); 210*4887Schin continue; 211*4887Schin case ':': 212*4887Schin error(2, "%s", opt_info.arg); 213*4887Schin continue; 214*4887Schin case '?': 215*4887Schin error(ERROR_usage(2), "%s", opt_info.arg); 216*4887Schin continue; 217*4887Schin } 218*4887Schin break; 219*4887Schin } 220*4887Schin argv += opt_info.index; 221*4887Schin argc -= opt_info.index; 222*4887Schin if(error_info.errors) 223*4887Schin error(ERROR_usage(2),"%s", optusage(NiL)); 224*4887Schin if(cp = *argv) 225*4887Schin argv++; 226*4887Schin do 227*4887Schin { 228*4887Schin if(!cp || streq(cp,"-")) 229*4887Schin fp = sfstdin; 230*4887Schin else if(!(fp = sfopen(NiL,cp,"r"))) 231*4887Schin { 232*4887Schin error(ERROR_system(0),"%s: cannot open",cp); 233*4887Schin error_info.errors = 1; 234*4887Schin continue; 235*4887Schin } 236*4887Schin fold(fp,sfstdout,width,cont,contsize,cols); 237*4887Schin if(fp!=sfstdin) 238*4887Schin sfclose(fp); 239*4887Schin } 240*4887Schin while(cp= *argv++); 241*4887Schin return(error_info.errors); 242*4887Schin } 243