summaryrefslogtreecommitdiff
path: root/usr/gri/pretty/untab.go
blob: a2232b35172d0e65773f72789b588e5fdcfb64fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"os";
	"io";
	"flag";
	"fmt";
	"tabwriter";
)


var (
	usetabs = flag.Bool("usetabs", false, nil, "align with tabs instead of blanks");
	tabwidth = flag.Int("tabwidth", 4, nil, "tab width");
)


func Error(format string, params ...) {
	fmt.printf(format, params);
	sys.exit(1);
}


func Untab(name string, src *os.FD, dst *tabwriter.TabWriter) {
	n, err := io.Copy(src, dst);
	if err != nil {
		Error("error while processing %s (%v)", name, err);
	}
	//dst.Flush();
}


func main() {
	flag.Parse();
	dst := tabwriter.MakeTabWriter(os.Stdout, usetabs.BVal(), int(tabwidth.IVal()));
	if flag.NArg() > 0 {
		for i := 0; i < flag.NArg(); i++ {
			name := flag.Arg(i);
			src, err := os.Open(name, os.O_RDONLY, 0);
			if err != nil {
				Error("could not open %s (%v)\n", name, err);
			}
			Untab(name, src, dst);
			src.Close();  // ignore errors
		}
	} else {
		// no files => use stdin
		Untab("/dev/stdin", os.Stdin, dst);
	}
}