]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - tools/tools/fixwhite/fixwhite.c
MFC r298881, 298882, 298883, 298885:
[FreeBSD/stable/10.git] / tools / tools / fixwhite / fixwhite.c
1 /*-
2  * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <ctype.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 static char *queue = NULL;
36 static size_t queuelen = 0, queuesize = 0;
37 static off_t column = 0;
38
39 static void
40 savebyte(char c)
41 {
42
43         if (queuelen >= queuesize) {
44                 queuesize += 128;
45                 queue = realloc(queue, queuesize);
46                 if (queue == NULL) {
47                         perror("malloc");
48                         exit(1);
49                 }
50         }
51         queue[queuelen++] = c;
52
53         switch (c) {
54         case '\n':
55                 column = 0;
56                 break;
57         case ' ':
58                 column++;
59                 break;
60         case '\t':
61                 column = (column / 8 + 1) * 8;
62                 break;
63         }
64 }
65
66 static bool
67 peekbyte(size_t back, char c)
68 {
69
70         return (queuelen >= back && queue[queuelen - back] == c);
71 }
72
73 static void
74 savewhite(char c, bool leading)
75 {
76         off_t ncolumn;
77
78         switch (c) {
79         case '\n':
80                 if (leading) {
81                         /* Remove empty lines before input. */
82                         queuelen = 0;
83                         column = 0;
84                 } else {
85                         /* Remove trailing whitespace. */
86                         while (peekbyte(1, ' ') || peekbyte(1, '\t'))
87                                 queuelen--;
88                         /* Remove redundant empty lines. */
89                         if (peekbyte(2, '\n') && peekbyte(1, '\n'))
90                                 return;
91                         savebyte('\n');
92                 }
93                 break;
94         case ' ':
95                 savebyte(' ');
96                 break;
97         case '\t':
98                 /* Convert preceding spaces to tabs. */
99                 ncolumn = (column / 8 + 1) * 8;
100                 while (peekbyte(1, ' ')) {
101                         queuelen--;
102                         column--;
103                 }
104                 while (column < ncolumn)
105                         savebyte('\t');
106                 break;
107         }
108 }
109
110 static void
111 printwhite(void)
112 {
113         off_t i;
114
115         /* Merge spaces at the start of a sentence to tabs if possible. */
116         if ((column % 8) == 0) {
117                 for (i = 0; i < column; i++)
118                         if (!peekbyte(i + 1, ' '))
119                                 break;
120                 if (i == column) {
121                         queuelen -= column;
122                         for (i = 0; i < column; i += 8)
123                                 queue[queuelen++] = '\t';
124                 }
125         }
126
127         if (fwrite(queue, 1, queuelen, stdout) != queuelen) {
128                 perror("write");
129                 exit(1);
130         }
131         queuelen = 0;
132 }
133
134 static char
135 readchar(void)
136 {
137         int c;
138
139         c = getchar();
140         if (c == EOF && ferror(stdin)) {
141                 perror("read");
142                 exit(1);
143         }
144         return (c);
145 }
146
147 static void
148 writechar(char c)
149 {
150
151         if (putchar(c) == EOF) {
152                 perror("write");
153                 exit(1);
154         }
155         /* XXX: Multi-byte characters. */
156         column++;
157 }
158
159 int
160 main(void)
161 {
162         int c;
163         bool leading = true;
164
165         while ((c = readchar()) != EOF) {
166                 if (isspace(c))
167                         /* Save whitespace. */
168                         savewhite(c, leading);
169                 else {
170                         /* Reprint whitespace and print regular character. */
171                         printwhite();
172                         writechar(c);
173                         leading = false;
174                 }
175         }
176         /* Terminate non-empty files with a newline. */
177         if (!leading)
178                 writechar('\n');
179         return (0);
180 }