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
|
|
|
"os";
|
|
|
|
"io";
|
|
|
|
"flag";
|
|
|
|
"fmt";
|
|
|
|
"tabwriter";
|
2008-11-18 19:44:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2008-11-19 17:49:50 -07:00
|
|
|
tabwidth = flag.Int("tabwidth", 4, nil, "tab width");
|
2008-11-20 17:26:43 -07:00
|
|
|
usetabs = flag.Bool("usetabs", false, nil, "align with tabs instead of blanks");
|
2008-11-18 19:44:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2008-11-19 17:49:50 -07:00
|
|
|
func Error(format string, params ...) {
|
|
|
|
fmt.printf(format, params);
|
2008-11-18 19:44:17 -07:00
|
|
|
sys.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-20 18:39:41 -07:00
|
|
|
func Untab(name string, src *os.FD, 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 {
|
|
|
|
Error("error while processing %s (%v)", name, err);
|
|
|
|
}
|
|
|
|
//dst.Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2008-11-19 17:49:50 -07:00
|
|
|
flag.Parse();
|
2008-11-20 17:26:43 -07:00
|
|
|
dst := tabwriter.New(os.Stdout, int(tabwidth.IVal()), 1, usetabs.BVal());
|
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 {
|
|
|
|
Error("could not open %s (%v)\n", name, err);
|
|
|
|
}
|
|
|
|
Untab(name, src, dst);
|
|
|
|
src.Close(); // ignore errors
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no files => use stdin
|
2008-11-19 17:49:50 -07:00
|
|
|
Untab("/dev/stdin", os.Stdin, dst);
|
2008-11-18 19:44:17 -07:00
|
|
|
}
|
|
|
|
}
|