-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc.go
54 lines (45 loc) · 870 Bytes
/
wc.go
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 2019 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a Apache
// license that can be found in the LICENSE file.
//go:generate flex wc.l
// just like Unix wc
package main
//#include "lex.yy.h"
//#include "wc.h"
import "C"
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
)
var (
flagInput = flag.String("f", "", "set input file")
)
func main() {
flag.Parse()
var (
content []byte
err error
)
if *flagInput == "" {
// cat wc.go | go run .
content, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
} else {
// go run . -f wc.go
content, err = ioutil.ReadFile(*flagInput)
if err != nil {
log.Fatal(err)
}
}
C.yy_scan_bytes(
(*C.char)(C.CBytes(content)),
C.yy_size_t(len(content)),
)
C.yylex()
fmt.Printf("%8d%8d%8d\n", C.lines, C.words, C.chars)
}