2008-09-15 12:48:37 -06: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 (
|
2009-01-09 16:16:31 -07:00
|
|
|
"fd";
|
|
|
|
"flag";
|
|
|
|
"os";
|
2008-09-15 12:48:37 -06:00
|
|
|
)
|
|
|
|
|
2009-01-09 16:16:31 -07:00
|
|
|
var rot13_flag = flag.Bool("rot13", false, "rot13 the input")
|
2008-09-15 12:48:37 -06:00
|
|
|
|
2008-09-16 12:00:11 -06:00
|
|
|
func rot13(b byte) byte {
|
2008-09-15 12:48:37 -06:00
|
|
|
if 'a' <= b && b <= 'z' {
|
|
|
|
b = 'a' + ((b - 'a') + 13) % 26;
|
|
|
|
}
|
|
|
|
if 'A' <= b && b <= 'Z' {
|
|
|
|
b = 'A' + ((b - 'A') + 13) % 26
|
|
|
|
}
|
2008-09-16 12:00:11 -06:00
|
|
|
return b
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Reader interface {
|
2009-01-09 16:16:31 -07:00
|
|
|
Read(b []byte) (ret int, err *os.Error);
|
|
|
|
String() string;
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Rot13 struct {
|
|
|
|
source Reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRot13(source Reader) *Rot13 {
|
2009-01-09 16:16:31 -07:00
|
|
|
return &Rot13{source}
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-01-09 16:16:31 -07:00
|
|
|
func (r13 *Rot13) Read(b []byte) (ret int, err *os.Error) {
|
2008-09-15 12:48:37 -06:00
|
|
|
r, e := r13.source.Read(b);
|
2009-01-09 16:16:31 -07:00
|
|
|
for i := 0; i < r; i++ {
|
2008-09-15 12:48:37 -06:00
|
|
|
b[i] = rot13(b[i])
|
|
|
|
}
|
|
|
|
return r, e
|
|
|
|
}
|
|
|
|
|
2009-01-09 16:16:31 -07:00
|
|
|
func (r13 *Rot13) String() string {
|
|
|
|
return r13.source.String()
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
// end of Rot13 implementation
|
|
|
|
|
|
|
|
func cat(r Reader) {
|
|
|
|
const NBUF = 512;
|
|
|
|
var buf [NBUF]byte;
|
|
|
|
|
2009-01-09 16:16:31 -07:00
|
|
|
if *rot13_flag {
|
2008-09-15 12:48:37 -06:00
|
|
|
r = NewRot13(r)
|
|
|
|
}
|
|
|
|
for {
|
2009-01-06 16:49:27 -07:00
|
|
|
switch nr, er := r.Read(buf); {
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr < 0:
|
2009-01-09 16:16:31 -07:00
|
|
|
print("error reading from ", r.String(), ": ", er.String(), "\n");
|
2008-09-15 12:48:37 -06:00
|
|
|
sys.exit(1);
|
|
|
|
case nr == 0: // EOF
|
|
|
|
return;
|
|
|
|
case nr > 0:
|
2009-01-09 16:16:31 -07:00
|
|
|
nw, ew := fd.Stdout.Write(buf[0:nr]);
|
2008-09-15 12:48:37 -06:00
|
|
|
if nw != nr {
|
2009-01-09 16:16:31 -07:00
|
|
|
print("error writing from ", r.String(), ": ", ew.String(), "\n");
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2009-01-09 16:16:31 -07:00
|
|
|
flag.Parse(); // Scans the arg list and sets up flags
|
|
|
|
if flag.NArg() == 0 {
|
|
|
|
cat(fd.Stdin);
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-01-09 16:16:31 -07:00
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
|
|
|
file, err := fd.Open(flag.Arg(i), 0, 0);
|
|
|
|
if file == nil {
|
|
|
|
print("can't open ", flag.Arg(i), ": error ", err, "\n");
|
2008-09-15 12:48:37 -06:00
|
|
|
sys.exit(1);
|
|
|
|
}
|
2009-01-09 16:16:31 -07:00
|
|
|
cat(file);
|
|
|
|
file.Close();
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|