mksysnum.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //
  5. // Generate system call table for DragonFly, NetBSD,
  6. // FreeBSD, OpenBSD or Darwin from master list
  7. // (for example, /usr/src/sys/kern/syscalls.master or
  8. // sys/syscall.h).
  9. // +build ignore
  10. package main
  11. import (
  12. "bufio"
  13. "fmt"
  14. "io"
  15. "io/ioutil"
  16. "net/http"
  17. "os"
  18. "regexp"
  19. "strings"
  20. )
  21. var (
  22. goos, goarch string
  23. )
  24. // cmdLine returns this programs's commandline arguments
  25. func cmdLine() string {
  26. return "go run mksysnum.go " + strings.Join(os.Args[1:], " ")
  27. }
  28. // buildTags returns build tags
  29. func buildTags() string {
  30. return fmt.Sprintf("%s,%s", goarch, goos)
  31. }
  32. func checkErr(err error) {
  33. if err != nil {
  34. fmt.Fprintf(os.Stderr, "%v\n", err)
  35. os.Exit(1)
  36. }
  37. }
  38. // source string and substring slice for regexp
  39. type re struct {
  40. str string // source string
  41. sub []string // matched sub-string
  42. }
  43. // Match performs regular expression match
  44. func (r *re) Match(exp string) bool {
  45. r.sub = regexp.MustCompile(exp).FindStringSubmatch(r.str)
  46. if r.sub != nil {
  47. return true
  48. }
  49. return false
  50. }
  51. // fetchFile fetches a text file from URL
  52. func fetchFile(URL string) io.Reader {
  53. resp, err := http.Get(URL)
  54. checkErr(err)
  55. defer resp.Body.Close()
  56. body, err := ioutil.ReadAll(resp.Body)
  57. checkErr(err)
  58. return strings.NewReader(string(body))
  59. }
  60. // readFile reads a text file from path
  61. func readFile(path string) io.Reader {
  62. file, err := os.Open(os.Args[1])
  63. checkErr(err)
  64. return file
  65. }
  66. func format(name, num, proto string) string {
  67. name = strings.ToUpper(name)
  68. // There are multiple entries for enosys and nosys, so comment them out.
  69. nm := re{str: name}
  70. if nm.Match(`^SYS_E?NOSYS$`) {
  71. name = fmt.Sprintf("// %s", name)
  72. }
  73. if name == `SYS_SYS_EXIT` {
  74. name = `SYS_EXIT`
  75. }
  76. return fmt.Sprintf(" %s = %s; // %s\n", name, num, proto)
  77. }
  78. func main() {
  79. // Get the OS (using GOOS_TARGET if it exist)
  80. goos = os.Getenv("GOOS_TARGET")
  81. if goos == "" {
  82. goos = os.Getenv("GOOS")
  83. }
  84. // Get the architecture (using GOARCH_TARGET if it exists)
  85. goarch = os.Getenv("GOARCH_TARGET")
  86. if goarch == "" {
  87. goarch = os.Getenv("GOARCH")
  88. }
  89. // Check if GOOS and GOARCH environment variables are defined
  90. if goarch == "" || goos == "" {
  91. fmt.Fprintf(os.Stderr, "GOARCH or GOOS not defined in environment\n")
  92. os.Exit(1)
  93. }
  94. file := strings.TrimSpace(os.Args[1])
  95. var syscalls io.Reader
  96. if strings.HasPrefix(file, "http://") {
  97. // Download syscalls.master file
  98. syscalls = fetchFile(file)
  99. } else {
  100. syscalls = readFile(file)
  101. }
  102. var text, line string
  103. s := bufio.NewScanner(syscalls)
  104. for s.Scan() {
  105. t := re{str: line}
  106. if t.Match(`^(.*)\\$`) {
  107. // Handle continuation
  108. line = t.sub[1]
  109. line += strings.TrimLeft(s.Text(), " \t")
  110. } else {
  111. // New line
  112. line = s.Text()
  113. }
  114. t = re{str: line}
  115. if t.Match(`\\$`) {
  116. continue
  117. }
  118. t = re{str: line}
  119. switch goos {
  120. case "dragonfly":
  121. if t.Match(`^([0-9]+)\s+STD\s+({ \S+\s+(\w+).*)$`) {
  122. num, proto := t.sub[1], t.sub[2]
  123. name := fmt.Sprintf("SYS_%s", t.sub[3])
  124. text += format(name, num, proto)
  125. }
  126. case "freebsd":
  127. if t.Match(`^([0-9]+)\s+\S+\s+(?:NO)?STD\s+({ \S+\s+(\w+).*)$`) {
  128. num, proto := t.sub[1], t.sub[2]
  129. name := fmt.Sprintf("SYS_%s", t.sub[3])
  130. text += format(name, num, proto)
  131. }
  132. case "openbsd":
  133. if t.Match(`^([0-9]+)\s+STD\s+(NOLOCK\s+)?({ \S+\s+\*?(\w+).*)$`) {
  134. num, proto, name := t.sub[1], t.sub[3], t.sub[4]
  135. text += format(name, num, proto)
  136. }
  137. case "netbsd":
  138. if t.Match(`^([0-9]+)\s+((STD)|(NOERR))\s+(RUMP\s+)?({\s+\S+\s*\*?\s*\|(\S+)\|(\S*)\|(\w+).*\s+})(\s+(\S+))?$`) {
  139. num, proto, compat := t.sub[1], t.sub[6], t.sub[8]
  140. name := t.sub[7] + "_" + t.sub[9]
  141. if t.sub[11] != "" {
  142. name = t.sub[7] + "_" + t.sub[11]
  143. }
  144. name = strings.ToUpper(name)
  145. if compat == "" || compat == "13" || compat == "30" || compat == "50" {
  146. text += fmt.Sprintf(" %s = %s; // %s\n", name, num, proto)
  147. }
  148. }
  149. case "darwin":
  150. if t.Match(`^#define\s+SYS_(\w+)\s+([0-9]+)`) {
  151. name, num := t.sub[1], t.sub[2]
  152. name = strings.ToUpper(name)
  153. text += fmt.Sprintf(" SYS_%s = %s;\n", name, num)
  154. }
  155. default:
  156. fmt.Fprintf(os.Stderr, "unrecognized GOOS=%s\n", goos)
  157. os.Exit(1)
  158. }
  159. }
  160. err := s.Err()
  161. checkErr(err)
  162. fmt.Printf(template, cmdLine(), buildTags(), text)
  163. }
  164. const template = `// %s
  165. // Code generated by the command above; see README.md. DO NOT EDIT.
  166. // +build %s
  167. package unix
  168. const(
  169. %s)`