1 /* $OpenBSD: unexpand.c,v 1.9 2003/07/10 00:06:51 david Exp $ */ 2 /* $NetBSD: unexpand.c,v 1.5 1994/12/24 17:08:05 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static char copyright[] = 35 "@(#) Copyright (c) 1980, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93"; 42 #endif 43 static char rcsid[] = "$OpenBSD: unexpand.c,v 1.9 2003/07/10 00:06:51 david Exp $"; 44 #endif /* not lint */ 45 46 /* 47 * unexpand - put tabs into a file replacing blanks 48 */ 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 53 char genbuf[BUFSIZ]; 54 char linebuf[BUFSIZ]; 55 int all; 56 57 void tabify(char); 58 59 int 60 main(int argc, char *argv[]) 61 { 62 char *cp; 63 64 argc--, argv++; 65 if (argc > 0 && argv[0][0] == '-') { 66 if (strcmp(argv[0], "-a") != 0) { 67 fprintf(stderr, "usage: unexpand [-a] [file ...]\n"); 68 exit(1); 69 } 70 all++; 71 argc--, argv++; 72 } 73 do { 74 if (argc > 0) { 75 if (freopen(argv[0], "r", stdin) == NULL) { 76 perror(argv[0]); 77 exit(1); 78 } 79 argc--, argv++; 80 } 81 while (fgets(genbuf, BUFSIZ, stdin) != NULL) { 82 for (cp = linebuf; *cp; cp++) 83 continue; 84 if (cp > linebuf) 85 cp[-1] = 0; 86 tabify(all); 87 printf("%s", linebuf); 88 } 89 } while (argc > 0); 90 exit(0); 91 } 92 93 void 94 tabify(char c) 95 { 96 char *cp, *dp; 97 int dcol; 98 int ocol; 99 size_t len; 100 101 ocol = 0; 102 dcol = 0; 103 cp = genbuf; 104 dp = linebuf; 105 len = sizeof linebuf; 106 107 for (;;) { 108 switch (*cp) { 109 110 case ' ': 111 dcol++; 112 break; 113 114 case '\t': 115 dcol += 8; 116 dcol &= ~07; 117 break; 118 119 default: 120 while (((ocol + 8) &~ 07) <= dcol) { 121 if (ocol + 1 == dcol) 122 break; 123 if (len > 1) { 124 *dp++ = '\t'; 125 len--; 126 } 127 ocol += 8; 128 ocol &= ~07; 129 } 130 while (ocol < dcol) { 131 if (len > 1) { 132 *dp++ = ' '; 133 len--; 134 } 135 ocol++; 136 } 137 if (*cp == 0 || c == 0) { 138 strlcpy(dp, cp, len); 139 return; 140 } 141 *dp++ = *cp; 142 len--; 143 ocol++; 144 dcol++; 145 } 146 cp++; 147 } 148 } 149