2008-11-18 19:44:17 -07:00
|
|
|
// 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 (
|
2008-11-19 17:49:50 -07:00
|
|
|
"flag";
|
|
|
|
"fmt";
|
2009-04-16 21:52:37 -06:00
|
|
|
"io";
|
|
|
|
"os";
|
2008-11-19 17:49:50 -07:00
|
|
|
"tabwriter";
|
2008-11-18 19:44:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2009-01-09 14:42:46 -07:00
|
|
|
tabwidth = flag.Int("tabwidth", 4, "tab width");
|
|
|
|
usetabs = flag.Bool("usetabs", false, "align with tabs instead of blanks");
|
2008-11-18 19:44:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-01-15 16:14:19 -07:00
|
|
|
func error(format string, params ...) {
|
|
|
|
fmt.Printf(format, params);
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1);
|
2008-11-18 19:44:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
func untab(name string, src *os.File, dst *tabwriter.Writer) {
|
2008-11-19 17:49:50 -07:00
|
|
|
n, err := io.Copy(src, dst);
|
2008-11-18 19:44:17 -07:00
|
|
|
if err != nil {
|
2009-01-15 16:14:19 -07:00
|
|
|
error("error while processing %s (%v)", name, err);
|
2008-11-18 19:44:17 -07:00
|
|
|
}
|
|
|
|
//dst.Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2008-11-19 17:49:50 -07:00
|
|
|
flag.Parse();
|
2008-11-21 10:35:49 -07:00
|
|
|
padchar := byte(' ');
|
2009-01-09 14:42:46 -07:00
|
|
|
if *usetabs {
|
2008-11-21 10:35:49 -07:00
|
|
|
padchar = '\t';
|
|
|
|
}
|
2009-03-10 17:31:19 -06:00
|
|
|
dst := tabwriter.NewWriter(os.Stdout, *tabwidth, 1, padchar, 0);
|
2008-11-19 17:49:50 -07:00
|
|
|
if flag.NArg() > 0 {
|
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
|
|
|
name := flag.Arg(i);
|
|
|
|
src, err := os.Open(name, os.O_RDONLY, 0);
|
2008-11-18 19:44:17 -07:00
|
|
|
if err != nil {
|
2009-01-15 16:14:19 -07:00
|
|
|
error("could not open %s (%v)\n", name, err);
|
2008-11-18 19:44:17 -07:00
|
|
|
}
|
2009-01-15 16:14:19 -07:00
|
|
|
untab(name, src, dst);
|
2008-11-18 19:44:17 -07:00
|
|
|
src.Close(); // ignore errors
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no files => use stdin
|
2009-01-15 16:14:19 -07:00
|
|
|
untab("/dev/stdin", os.Stdin, dst);
|
2008-11-18 19:44:17 -07:00
|
|
|
}
|
|
|
|
}
|