4747 libcPath = "libc.so"
4848)
4949
50+ var (
51+ regexpComma = regexp .MustCompile (`\s*,\s*` )
52+ regexpParamKV = regexp .MustCompile (`^(\S*) (\S*)$` )
53+ regexpSys = regexp .MustCompile (`^\/\/sys\t` )
54+ regexpSysNonblock = regexp .MustCompile (`^\/\/sysnb\t` )
55+ regexpSysDeclaration = regexp .MustCompile (`^\/\/sys(nb)?\t(\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$` )
56+ regexpPointer = regexp .MustCompile (`^\*` )
57+ regexpSlice = regexp .MustCompile (`^\[](.*)` )
58+ regexpDragonflyExtp = regexp .MustCompile (`^(?i)extp(read|write)` )
59+ regexpSyscallName = regexp .MustCompile (`([a-z])([A-Z])` )
60+ )
61+
5062// cmdLine returns this programs's commandline arguments
5163func cmdLine () string {
5264 return "go run mksyscall.go " + strings .Join (os .Args [1 :], " " )
@@ -75,12 +87,12 @@ func parseParamList(list string) []string {
7587 if list == "" {
7688 return []string {}
7789 }
78- return regexp . MustCompile ( `\s*,\s*` ) .Split (list , - 1 )
90+ return regexpComma .Split (list , - 1 )
7991}
8092
8193// parseParam splits a parameter into name and type
8294func parseParam (p string ) Param {
83- ps := regexp . MustCompile ( `^(\S*) (\S*)$` ) .FindStringSubmatch (p )
95+ ps := regexpParamKV .FindStringSubmatch (p )
8496 if ps == nil {
8597 fmt .Fprintf (os .Stderr , "malformed parameter: %s\n " , p )
8698 os .Exit (1 )
@@ -138,15 +150,15 @@ func main() {
138150 s := bufio .NewScanner (file )
139151 for s .Scan () {
140152 t := s .Text ()
141- nonblock := regexp . MustCompile ( `^\/\/sysnb\t` ) .FindStringSubmatch (t )
142- if regexp . MustCompile ( `^\/\/sys\t` ) .FindStringSubmatch (t ) == nil && nonblock == nil {
153+ nonblock := regexpSysNonblock .FindStringSubmatch (t )
154+ if regexpSys .FindStringSubmatch (t ) == nil && nonblock == nil {
143155 continue
144156 }
145157
146158 // Line must be of the form
147159 // func Open(path string, mode int, perm int) (fd int, errno error)
148160 // Split into name, in params, out params.
149- f := regexp . MustCompile ( `^\/\/sys(nb)?\t(\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$` ) .FindStringSubmatch (t )
161+ f := regexpSysDeclaration .FindStringSubmatch (t )
150162 if f == nil {
151163 fmt .Fprintf (os .Stderr , "%s:%s\n malformed //sys declaration\n " , path , t )
152164 os .Exit (1 )
@@ -184,7 +196,7 @@ func main() {
184196 n := 0
185197 for _ , param := range in {
186198 p := parseParam (param )
187- if regexp . MustCompile ( `^\*` ) .FindStringSubmatch (p .Type ) != nil {
199+ if regexpPointer .FindStringSubmatch (p .Type ) != nil {
188200 args = append (args , "uintptr(unsafe.Pointer(" + p .Name + "))" )
189201 } else if p .Type == "string" && errvar != "" {
190202 text += fmt .Sprintf ("\t var _p%d *byte\n " , n )
@@ -198,7 +210,7 @@ func main() {
198210 text += fmt .Sprintf ("\t _p%d, _ = BytePtrFromString(%s)\n " , n , p .Name )
199211 args = append (args , fmt .Sprintf ("uintptr(unsafe.Pointer(_p%d))" , n ))
200212 n ++
201- } else if regexp . MustCompile ( `^\[\](.*)` ) .FindStringSubmatch (p .Type ) != nil {
213+ } else if regexpSlice .FindStringSubmatch (p .Type ) != nil {
202214 // Convert slice into pointer, length.
203215 // Have to be careful not to take address of &a[0] if len == 0:
204216 // pass dummy pointer in that case.
@@ -218,7 +230,7 @@ func main() {
218230 args = append (args , fmt .Sprintf ("uintptr(%s)" , p .Name ))
219231 }
220232 } else if p .Type == "int64" && * dragonfly {
221- if regexp . MustCompile ( `^(?i)extp(read|write)` ) .FindStringSubmatch (funct ) == nil {
233+ if regexpDragonflyExtp .FindStringSubmatch (funct ) == nil {
222234 args = append (args , "0" )
223235 }
224236 if endianness == "big-endian" {
@@ -278,7 +290,7 @@ func main() {
278290 // System call number.
279291 if sysname == "" {
280292 sysname = "SYS_" + funct
281- sysname = regexp . MustCompile ( `([a-z])([A-Z])` ) .ReplaceAllString (sysname , `${1}_$2` )
293+ sysname = regexpSyscallName .ReplaceAllString (sysname , `${1}_$2` )
282294 sysname = strings .ToUpper (sysname )
283295 }
284296
0 commit comments