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