unexpand.c revision 28456
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1980, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 3. All advertising materials mentioning features or use of this software 141556Srgrimes * must display the following acknowledgement: 151556Srgrimes * This product includes software developed by the University of 161556Srgrimes * California, Berkeley and its contributors. 171556Srgrimes * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 321556Srgrimes */ 331556Srgrimes 341556Srgrimes#ifndef lint 351556Srgrimesstatic const char copyright[] = 361556Srgrimes"@(#) Copyright (c) 1980, 1993\n\ 371556Srgrimes The Regents of the University of California. All rights reserved.\n"; 3827958Ssteve#endif /* not lint */ 391556Srgrimes 401556Srgrimes#ifndef lint 4127967Ssteve#if 0 4227967Sstevestatic char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93"; 4327967Ssteve#endif 4427967Sstevestatic const char rcsid[] = 4527967Ssteve "$Id$"; 4627967Ssteve#endif /* not lint */ 4727958Ssteve 4850471Speter/* 4927967Ssteve * unexpand - put tabs into a file replacing blanks 501556Srgrimes */ 511556Srgrimes#include <err.h> 521556Srgrimes#include <stdio.h> 531556Srgrimes 541556Srgrimeschar genbuf[BUFSIZ]; 551556Srgrimeschar linebuf[BUFSIZ]; 561556Srgrimesint all; 571556Srgrimes 581556Srgrimesstatic void usage __P((void)); 591556Srgrimesvoid tabify __P((char)); 6050050Ssheldonh 6150050Ssheldonhvoid 621556Srgrimesmain(argc, argv) 631556Srgrimes int argc; 641556Srgrimes char *argv[]; 6561289Sache{ 6661268Sjoe register char *cp; 6761289Sache 6861289Sache argc--, argv++; 6961268Sjoe if (argc > 0 && argv[0][0] == '-') { 701556Srgrimes if (strcmp(argv[0], "-a") != 0) 711556Srgrimes usage(); 721556Srgrimes all++; 731556Srgrimes argc--, argv++; 7450050Ssheldonh } 7550050Ssheldonh do { 7650050Ssheldonh if (argc > 0) { 7750050Ssheldonh if (freopen(argv[0], "r", stdin) == NULL) 7850050Ssheldonh err(1, "%s", argv[0]); 7950051Ssheldonh argc--, argv++; 8050050Ssheldonh } 811556Srgrimes while (fgets(genbuf, BUFSIZ, stdin) != NULL) { 8237932Shoek for (cp = linebuf; *cp; cp++) 831556Srgrimes continue; 841556Srgrimes if (cp > linebuf) 851556Srgrimes cp[-1] = 0; 861556Srgrimes tabify(all); 871556Srgrimes printf("%s", linebuf); 881556Srgrimes } 891556Srgrimes } while (argc > 0); 901556Srgrimes exit(0); 911556Srgrimes} 921556Srgrimes 931556Srgrimesstatic void 941556Srgrimesusage() 951556Srgrimes{ 961556Srgrimes fprintf(stderr, "usage: unexpand [-a] file ...\n"); 972889Spst exit(1); 981556Srgrimes} 991556Srgrimes 1001556Srgrimesvoid 1011556Srgrimestabify(c) 1021556Srgrimes char c; 10350050Ssheldonh{ 10449373Ssheldonh register char *cp, *dp; 10535373Sdes register int dcol; 10635417Sdes int ocol; 1071556Srgrimes 1081556Srgrimes ocol = 0; 1091556Srgrimes dcol = 0; 1101556Srgrimes cp = genbuf, dp = linebuf; 1111556Srgrimes for (;;) { 1121556Srgrimes switch (*cp) { 1131556Srgrimes 1141556Srgrimes case ' ': 11520417Ssteve dcol++; 11661268Sjoe break; 11761178Sjoe 11861271Sjoe case '\t': 11961271Sjoe dcol += 8; 12061271Sjoe dcol &= ~07; 12161271Sjoe break; 12261268Sjoe 1231556Srgrimes default: 12417852Sadam while (((ocol + 8) &~ 07) <= dcol) { 12517852Sadam if (ocol + 1 == dcol) 1261556Srgrimes break; 1271556Srgrimes *dp++ = '\t'; 1281556Srgrimes ocol += 8; 1291556Srgrimes ocol &= ~07; 1301556Srgrimes } 1311556Srgrimes while (ocol < dcol) { 1321556Srgrimes *dp++ = ' '; 1331556Srgrimes ocol++; 1341556Srgrimes } 1351556Srgrimes if (*cp == 0 || c == 0) { 13661271Sjoe strcpy(dp, cp); 13761271Sjoe return; 13861271Sjoe } 13961271Sjoe *dp++ = *cp; 14061271Sjoe ocol++, dcol++; 14161271Sjoe } 14211808Sache cp++; 14311808Sache } 1441556Srgrimes} 1451556Srgrimes