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-03-16 23:53:23 -06:00
|
|
|
"file";
|
2009-01-09 16:16:31 -07:00
|
|
|
"flag";
|
2009-03-18 15:09:16 -06:00
|
|
|
"fmt";
|
2009-01-09 16:16:31 -07:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07: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
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
type rotate13 struct {
|
|
|
|
source reader;
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
func newRotate13(source reader) *rotate13 {
|
2009-03-03 09:39:12 -07:00
|
|
|
return &rotate13{source}
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
func (r13 *rotate13) 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-15 18:54:07 -07:00
|
|
|
func (r13 *rotate13) String() string {
|
2009-01-09 16:16:31 -07:00
|
|
|
return r13.source.String()
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-01-20 20:32:36 -07:00
|
|
|
// end of rotate13 implementation
|
2008-09-15 12:48:37 -06:00
|
|
|
|
2009-01-15 18:54:07 -07:00
|
|
|
func cat(r reader) {
|
2008-09-15 12:48:37 -06:00
|
|
|
const NBUF = 512;
|
|
|
|
var buf [NBUF]byte;
|
|
|
|
|
2009-01-09 16:16:31 -07:00
|
|
|
if *rot13_flag {
|
2009-01-15 18:54:07 -07:00
|
|
|
r = newRotate13(r)
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
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-03-18 15:09:16 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", r.String(), er.String());
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Exit(1);
|
2008-09-15 12:48:37 -06:00
|
|
|
case nr == 0: // EOF
|
|
|
|
return;
|
|
|
|
case nr > 0:
|
2009-03-16 23:53:23 -06:00
|
|
|
nw, ew := file.Stdout.Write(buf[0:nr]);
|
2008-09-15 12:48:37 -06:00
|
|
|
if nw != nr {
|
2009-03-18 15:09:16 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "error writing from %s: %s\n", r.String(), ew.String());
|
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 {
|
2009-03-16 23:53:23 -06:00
|
|
|
cat(file.Stdin);
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-01-09 16:16:31 -07:00
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
2009-03-16 23:53:23 -06:00
|
|
|
f, err := file.Open(flag.Arg(i), 0, 0);
|
|
|
|
if f == nil {
|
2009-03-18 15:09:16 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "can't open %s: error %s\n", flag.Arg(i), err);
|
2009-01-16 15:58:14 -07:00
|
|
|
sys.Exit(1);
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
2009-03-16 23:53:23 -06:00
|
|
|
cat(f);
|
|
|
|
f.Close();
|
2008-09-15 12:48:37 -06:00
|
|
|
}
|
|
|
|
}
|