20 static const char *ego = "<unset>";
22 static const char *bkp = 0;
24 static unsigned flags = 0;
25 #define F_MIDLINETABS 1u
29 #define F_UNTABIFY 16u
33 static void usage(FILE *fp)
34 { fprintf(fp, "Usage: %s [-cmtuv] [-i[BKP]] [FILE...]\n\n", ego); }
36 static char *augment(const char *name, const char *suffix)
38 size_t n = strlen(name), nn = strlen(suffix);
39 char *p = malloc(n + nn + 1);
42 fprintf(stderr, "%s: Out of memory!\n", ego);
46 memcpy(p + n, suffix, nn + 1);
50 static FILE *freshname(const char *name, char **newname, mode_t mode)
58 for (i = 0; i < 32767; i++) {
59 sprintf(buf, ".new%d", i);
60 if ((n = augment(name, buf)) == 0)
62 if ((fd = open(n, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
63 if (errno == EEXIST) {
67 fprintf(stderr, "%s: Can't create new file for `%s': %s\n",
68 ego, name, strerror(errno));
73 fprintf(stderr, "%s: Can't find new file to update `%s'\n", ego, name);
78 fprintf(stderr, "%s: Can't set permissions on `%s': %s\n",
79 ego, n, strerror(errno));
82 if ((fp = fdopen(fd, "w")) == 0) {
83 fprintf(stderr, "%s: fdopen on `%s' failed: %s\n",
84 ego, n, strerror(errno));
103 #define BUF_INIT { 0, 0, 0 }
105 static void reset(buf *b) { b->n = 0; }
107 static int put(buf *b, int ch)
117 b->b = realloc(b->b, w);
120 fprintf(stderr, "%s: Not enough memory for buffer!\n", ego);
129 #define TABSTOP(n) (((n) + 8u) & ~7u)
131 static int space(const char *name)
133 static buf b = BUF_INIT;
134 FILE *fin, *fout = stdout;
135 char *newname = 0, *oldname = 0;
136 int rc = TROUBLE, status = OK;
138 unsigned nsp = 0, nwsp = 0, hpos = 0, ohpos = 0, nhpos = 0, nl = 1;
141 #define f_warnspacetab 2u
143 #define f_warntabs 8u
144 #define f_warnspaces 16u
147 #define f_forced 128u
148 unsigned f = f_newline | (flags & F_TABIFY ? f_tabify : 0);
151 if (strcmp(name, "-") == 0) {
152 if (flags & F_INPLACE) {
153 fprintf(stderr, "%s: Can't modify stdin in-place.\n", ego);
158 if ((fin = fopen(name, "r")) == 0) {
159 fprintf(stderr, "%s: Failed to open file `%s': %s.\n",
160 ego, name, strerror(errno));
163 else if (flags & F_INPLACE) {
165 if (stat(name, &st)) {
166 fprintf(stderr, "%s: Can't stat `%s': %s.\n",
167 ego, name, strerror(errno));
170 if ((fout = freshname(name, &newname, st.st_mode)) == 0)
181 nsp++; nwsp++; hpos++;
182 if (put(&b, ' ')) goto done_2;
185 if (flags & F_UNTABIFY) {
186 if ((flags & F_VERBOSE) && !(f & f_warntabs)) {
187 fprintf(stderr, "%s:%u: found tab\n", name, nl);
191 } else if (((flags & F_MIDLINETABS) || (f & f_newline)) && nsp) {
192 if ((flags & F_VERBOSE) && !(f & f_warnspacetab)) {
193 fprintf(stderr, "%s:%u: space followed by tab\n", name, nl);
197 f |= f_tabify | f_forced;
200 nsp = 0; nwsp++; hpos = TABSTOP(hpos);
201 if (put(&b, '\t')) goto done_2;
204 if (nwsp || !(f & f_newline)) {
205 if (flags & F_VERBOSE)
206 fprintf(stderr, "%s:%u: file ends in mid-line\n", name, nl);
208 if (fout) putc('\n', fout);
214 if (flags & F_VERBOSE)
215 fprintf(stderr, "%s:%u: trailing whitespace\n", name, nl);
218 if (fout) putc('\n', fout);
220 nsp = nwsp = hpos = ohpos = 0; nl++;
222 f &= ~(f_tab | f_warnspacetab | f_warntabs | f_warnspaces);
223 if (flags & F_TABIFY)
231 if (flags & F_UNTABIFY) {
232 if (fout) for (; ohpos < hpos; ohpos++) putc(' ', fout);
233 } else if ((f & f_tabify) &&
234 ((hpos - ohpos >= (last == '.' || last == ':' ?
236 (f & (f_tab | f_newline)))) {
239 nhpos = TABSTOP(ohpos);
240 if (nhpos > hpos) break;
241 if (fout) putc('\t', fout);
242 if ((flags & F_VERBOSE) && (flags & F_TABIFY) &&
243 i < b.n && b.b[i] != '\t' &&
244 !(f & (f_warnspaces | f_forced))) {
245 fprintf(stderr, "%s:%u: spaces could be turned into tabs\n",
253 for (; ohpos < hpos; ohpos++) putc(' ', fout);
255 for (i = 0; i < b.n; i++) putc(b.b[i], fout);
258 f &= ~(f_newline | f_tab | f_forced);
259 if (!(flags & F_TABIFY) || !(flags & F_MIDLINETABS)) f &= ~f_tabify;
261 hpos++; ohpos = hpos;
262 if (fout) putc(ch, fout);
263 if (ch != '"' && ch != '\'')
271 fprintf(stderr, "%s: Error reading `%s': %s\n",
272 ego, name, strerror(errno));
277 if (fflush(fout) || ferror(fout)) f |= f_bad;
278 if (fout != stdout && fclose(fout)) f |= f_bad;
281 fprintf(stderr, "%s: Error writing `%s': %s\n",
282 ego, newname, strerror(errno));
287 if (flags & F_INPLACE) {
289 if ((oldname = augment(name, bkp)) == 0)
291 if (rename(name, oldname)) {
292 fprintf(stderr, "%s: Failed to back up `%s' as `%s': %s\n",
293 ego, name, oldname, strerror(errno));
297 if (rename(newname, name)) {
298 if (oldname) rename(oldname, name);
299 fprintf(stderr, "%s: Failed to install `%s' as `%s': %s\n",
300 ego, newname, name, strerror(errno));
308 if (oldname) free(oldname);
314 if (fout && fout != stdout) fclose(fout);
320 static int manysetp(unsigned f) { return (!!(f & (f - 1))); }
322 int main(int argc, char *argv[])
327 if ((ego = strrchr(argv[0], '/')) == 0)
333 if ((i = getopt(argc, argv, "h" "cmtuv" "i::")) < 0)
337 printf("%s -- remove extraneous spaces from files\n\n", ego);
340 -h Print this help text\n\
341 -c Check files for badness, but don't produce other output\n\
342 -m Fix spaces followed by tabs in mid-line\n\
343 -t Tabify file completely\n\
344 -u Untabify file completely\n\
345 -i[BKP] Modify files in place; leave FILEBKP as copy of old FILE\n\
353 flags |= F_MIDLINETABS;
372 if (flags & F_BOGUS) {
376 if (manysetp(flags & (F_CHECK | F_INPLACE))) {
377 fprintf(stderr, "%s: Options -c and -i are mutually exclusive.\n", ego);
380 if (manysetp(flags & (F_TABIFY | F_UNTABIFY))) {
381 fprintf(stderr, "%s: Options -t and -u are mutually exclusive.\n", ego);
385 if (optind == argc) {
387 fprintf(stderr, "%s: No options given and stdin is a terminal.\n",
392 } else for (i = optind; i < argc; i++) {
394 if (st > rc) rc = st;
396 if (rc == BADNESS && !(flags & F_CHECK))