1 static char *sccsid = "@(#)cb.c 4.4 (Berkeley) 02/09/87"; 2 #include <stdio.h> 3 int slevel[10]; 4 int clevel = 0; 5 int spflg[20][10]; 6 int sind[20][10]; 7 int siflev[10]; 8 int sifflg[10]; 9 int iflev = 0; 10 int ifflg = -1; 11 int level = 0; 12 int ind[10] = { 13 0,0,0,0,0,0,0,0,0,0 }; 14 int eflg = 0; 15 int paren = 0; 16 int pflg[10] = { 17 0,0,0,0,0,0,0,0,0,0 }; 18 char lchar; 19 char pchar; 20 int aflg = 0; 21 int ct; 22 int stabs[20][10]; 23 int qflg = 0; 24 char *wif[] = { 25 "if",0}; 26 char *welse[] = { 27 "else",0}; 28 char *wfor[] = { 29 "for",0}; 30 char *wds[] = { 31 "case","default",0}; 32 int j = 0; 33 char string[200]; 34 char cc; 35 int sflg = 1; 36 int peek = -1; 37 int tabs = 0; 38 int lastchar; 39 int c; 40 int getstr(); 41 main(argc,argv) int argc; 42 char argv[]; 43 { 44 while((c = getch()) != EOF){ 45 switch(c){ 46 case ' ': 47 case '\t': 48 if(lookup(welse) == 1){ 49 gotelse(); 50 if(sflg == 0 || j > 0)string[j++] = c; 51 puts(); 52 sflg = 0; 53 continue; 54 } 55 if(sflg == 0 || j > 0)string[j++] = c; 56 continue; 57 case '\n': 58 if((eflg = lookup(welse)) == 1)gotelse(); 59 puts(); 60 printf("\n"); 61 sflg = 1; 62 if(eflg == 1){ 63 pflg[level]++; 64 tabs++; 65 } 66 else 67 if(pchar == lchar) 68 aflg = 1; 69 continue; 70 case '{': 71 if(lookup(welse) == 1)gotelse(); 72 siflev[clevel] = iflev; 73 sifflg[clevel] = ifflg; 74 iflev = ifflg = 0; 75 clevel++; 76 if(sflg == 1 && pflg[level] != 0){ 77 pflg[level]--; 78 tabs--; 79 } 80 string[j++] = c; 81 puts(); 82 getnl(); 83 puts(); 84 printf("\n"); 85 tabs++; 86 sflg = 1; 87 if(pflg[level] > 0){ 88 ind[level] = 1; 89 level++; 90 slevel[level] = clevel; 91 } 92 continue; 93 case '}': 94 clevel--; 95 if((iflev = siflev[clevel]-1) < 0)iflev = 0; 96 ifflg = sifflg[clevel]; 97 if(pflg[level] >0 && ind[level] == 0){ 98 tabs -= pflg[level]; 99 pflg[level] = 0; 100 } 101 puts(); 102 tabs--; 103 ptabs(); 104 if((peek = getch()) == ';'){ 105 printf("%c;",c); 106 peek = -1; 107 } 108 else printf("%c",c); 109 getnl(); 110 puts(); 111 printf("\n"); 112 sflg = 1; 113 if(clevel < slevel[level])if(level > 0)level--; 114 if(ind[level] != 0){ 115 tabs -= pflg[level]; 116 pflg[level] = 0; 117 ind[level] = 0; 118 } 119 continue; 120 case '"': 121 case '\'': 122 string[j++] = c; 123 while((cc = getch()) != c){ 124 string[j++] = cc; 125 if(cc == '\\'){ 126 string[j++] = getch(); 127 } 128 if(cc == '\n'){ 129 puts(); 130 sflg = 1; 131 } 132 } 133 string[j++] = cc; 134 if(getnl() == 1){ 135 lchar = cc; 136 peek = '\n'; 137 } 138 continue; 139 case ';': 140 string[j++] = c; 141 puts(); 142 if(pflg[level] > 0 && ind[level] == 0){ 143 tabs -= pflg[level]; 144 pflg[level] = 0; 145 } 146 getnl(); 147 puts(); 148 printf("\n"); 149 sflg = 1; 150 if(iflev > 0) 151 if(ifflg == 1){iflev--; 152 ifflg = 0; 153 } 154 else iflev = 0; 155 continue; 156 case '\\': 157 string[j++] = c; 158 string[j++] = getch(); 159 continue; 160 case '?': 161 qflg = 1; 162 string[j++] = c; 163 continue; 164 case ':': 165 string[j++] = c; 166 if(qflg == 1){ 167 qflg = 0; 168 continue; 169 } 170 if(lookup(wds) == 0){ 171 sflg = 0; 172 puts(); 173 } 174 else{ 175 tabs--; 176 puts(); 177 tabs++; 178 } 179 if((peek = getch()) == ';'){ 180 printf(";"); 181 peek = -1; 182 } 183 getnl(); 184 puts(); 185 printf("\n"); 186 sflg = 1; 187 continue; 188 case '/': 189 string[j++] = c; 190 if((peek = getch()) != '*')continue; 191 string[j++] = peek; 192 peek = -1; 193 comment(); 194 continue; 195 case ')': 196 paren--; 197 string[j++] = c; 198 puts(); 199 if(getnl() == 1){ 200 peek = '\n'; 201 if(paren != 0)aflg = 1; 202 else if(tabs > 0){ 203 pflg[level]++; 204 tabs++; 205 ind[level] = 0; 206 } 207 } 208 continue; 209 case '#': 210 string[j++] = c; 211 while((cc = getch()) != '\n')string[j++] = cc; 212 string[j++] = cc; 213 sflg = 0; 214 puts(); 215 sflg = 1; 216 continue; 217 case '(': 218 string[j++] = c; 219 paren++; 220 if(lookup(wfor) == 1){ 221 while((c = getstr()) != ';'); 222 ct=0; 223 cont: 224 while((c = getstr()) != ')'){ 225 if(c == '(') ct++; 226 } 227 if(ct != 0){ 228 ct--; 229 goto cont; 230 } 231 paren--; 232 puts(); 233 if(getnl() == 1){ 234 peek = '\n'; 235 pflg[level]++; 236 tabs++; 237 ind[level] = 0; 238 } 239 continue; 240 } 241 if(lookup(wif) == 1){ 242 puts(); 243 stabs[clevel][iflev] = tabs; 244 spflg[clevel][iflev] = pflg[level]; 245 sind[clevel][iflev] = ind[level]; 246 iflev++; 247 ifflg = 1; 248 } 249 continue; 250 default: 251 string[j++] = c; 252 if(c != ',')lchar = c; 253 } 254 } 255 } 256 ptabs(){ 257 int i; 258 for(i=0; i < tabs; i++)printf("\t"); 259 } 260 getch(){ 261 if(peek < 0 && lastchar != ' ' && lastchar != '\t')pchar = lastchar; 262 lastchar = (peek<0) ? getc(stdin):peek; 263 peek = -1; 264 return(lastchar); 265 } 266 puts(){ 267 if(j > 0){ 268 if(sflg != 0){ 269 ptabs(); 270 sflg = 0; 271 if(aflg == 1){ 272 aflg = 0; 273 if(tabs > 0)printf(" "); 274 } 275 } 276 string[j] = '\0'; 277 printf("%s",string); 278 j = 0; 279 } 280 else{ 281 if(sflg != 0){ 282 sflg = 0; 283 aflg = 0; 284 } 285 } 286 } 287 lookup(tab) 288 char *tab[]; 289 { 290 char r; 291 int l,kk,k,i; 292 if(j < 1)return(0); 293 kk=0; 294 while(string[kk] == ' ')kk++; 295 for(i=0; tab[i] != 0; i++){ 296 l=0; 297 for(k=kk;(r = tab[i][l++]) == string[k] && r != '\0';k++); 298 if(r == '\0' && (string[k] < 'a' || string[k] > 'z' || k >= j))return(1); 299 } 300 return(0); 301 } 302 getstr(){ 303 char ch; 304 beg: 305 if((ch = string[j++] = getch()) == '\\'){ 306 string[j++] = getch(); 307 goto beg; 308 } 309 if(ch == '\'' || ch == '"'){ 310 while((cc = string[j++] = getch()) != ch)if(cc == '\\')string[j++] = getch(); 311 goto beg; 312 } 313 if(ch == '\n'){ 314 puts(); 315 aflg = 1; 316 goto beg; 317 } 318 else return(ch); 319 } 320 gotelse(){ 321 tabs = stabs[clevel][iflev]; 322 pflg[level] = spflg[clevel][iflev]; 323 ind[level] = sind[clevel][iflev]; 324 ifflg = 1; 325 } 326 getnl(){ 327 while((peek = getch()) == '\t' || peek == ' '){ 328 string[j++] = peek; 329 peek = -1; 330 } 331 if((peek = getch()) == '/'){ 332 peek = -1; 333 if((peek = getch()) == '*'){ 334 string[j++] = '/'; 335 string[j++] = '*'; 336 peek = -1; 337 comment(); 338 } 339 else string[j++] = '/'; 340 } 341 if((peek = getch()) == '\n'){ 342 peek = -1; 343 return(1); 344 } 345 return(0); 346 } 347 comment(){ 348 int i = j; 349 350 while ((c = getch()) != EOF) { 351 string[j++] = c; 352 switch(c) { 353 case '/': 354 if (j > i + 1 && string[j-2] == '*') 355 return; 356 break; 357 case '\n': 358 puts(); 359 i = 0; 360 sflg = 1; 361 break; 362 } 363 } 364 } 365