// #5979 func (s *DockerSuite) TestEventsRedirectStdout(c *check.C) { since := daemonTime(c).Unix() dockerCmd(c, "run", "busybox", "true") file, err := ioutil.TempFile("", "") c.Assert(err, checker.IsNil, check.Commentf("could not create temp file")) defer os.Remove(file.Name()) command := fmt.Sprintf("%s events --since=%d --until=%d > %s", dockerBinary, since, daemonTime(c).Unix(), file.Name()) _, tty, err := pty.Open() c.Assert(err, checker.IsNil, check.Commentf("Could not open pty")) cmd := exec.Command("sh", "-c", command) cmd.Stdin = tty cmd.Stdout = tty cmd.Stderr = tty c.Assert(cmd.Run(), checker.IsNil, check.Commentf("run err for command %q", command)) scanner := bufio.NewScanner(file) for scanner.Scan() { for _, ch := range scanner.Text() { c.Assert(unicode.IsControl(ch), checker.False, check.Commentf("found control character %v", []byte(string(ch)))) } } c.Assert(scanner.Err(), checker.IsNil, check.Commentf("Scan err for command %q", command)) }
func doIPTables(args ...interface{}) error { flatArgs := flatten(args, nil) output, err := exec.Command("iptables", flatArgs...).CombinedOutput() switch errt := err.(type) { case nil: case *exec.ExitError: if !errt.Success() { // sanitize iptables output limit := 200 sanOut := strings.Map(func(ch rune) rune { if limit == 0 { return -1 } limit-- if unicode.IsControl(ch) { ch = ' ' } return ch }, string(output)) return ipTablesError{ cmd: strings.Join(flatArgs, " "), output: sanOut, } } default: return err } return nil }
func quoteTo(buf *bytes.Buffer, s string) { buf.Grow(2 + len(s)) buf.WriteByte('"') for i, c := range s { switch { case unicode.IsControl(c): if s, ok := ctrlMap[c]; ok { buf.WriteString(s) } else { fmt.Fprintf(buf, "\\u%04x", c) } case c == '"', c == '\\', c == '\'': buf.WriteByte('\\') buf.WriteRune(c) case c <= unicode.MaxASCII: buf.WriteRune(c) case c == unicode.ReplacementChar: // In correctly-encoded UTF-8, we should never see a replacement // char. Some text in the wild has valid Unicode characters that // aren't UTF-8, and this case lets us be more forgiving of those. fmt.Fprintf(buf, "\\u%04x", s[i]) case c <= 0xffff: fmt.Fprintf(buf, "\\u%04x", c) default: fmt.Fprintf(buf, "\\U%08x", c) } } buf.WriteByte('"') }
// CharType returns a string representing the unicode type of a rune func CharType(r rune) string { switch { case unicode.IsLetter(r): return "letter" case unicode.IsSpace(r): return "space" case unicode.IsPunct(r): return "punct" case unicode.IsNumber(r): return "number" case unicode.IsSymbol(r): return "symbol" case unicode.IsMark(r): return "mark" case unicode.IsDigit(r): return "digit" case unicode.IsPrint(r): return "print" case unicode.IsControl(r): return "control" case unicode.IsGraphic(r): return "graphic" default: return "invalid" } }
// CharCount scans a *bufio.Reader and returns a map of the counts of its // Unicode character types. func CharCount(in *bufio.Reader) map[string]int { counts := make(map[string]int) // counts of Unicode character types for { r, n, err := in.ReadRune() // returns rune, nbytes, error if err == io.EOF { break } if err != nil { fmt.Fprintf(os.Stderr, "charcount: %v\n", err) os.Exit(1) } switch { case r == unicode.ReplacementChar && n == 1: counts["invalid"]++ case unicode.IsControl(r): counts["control"]++ case unicode.IsLetter(r): counts["letter"]++ case unicode.IsMark(r): counts["mark"]++ case unicode.IsNumber(r): counts["number"]++ case unicode.IsPunct(r): counts["punct"]++ case unicode.IsSpace(r): counts["space"]++ case unicode.IsSymbol(r): counts["symbol"]++ } } return counts }
// Some Simple Item Key Validations func isKeyValid(key string) error { // Valid Unicode if !utf8.ValidString(key) { return ErrKeyContainsNonUnicode } // No Slashes if strings.Contains(key, "/") { return ErrKeyContainsSlash } for _, rune := range key { // No White Space if unicode.IsSpace(rune) { return ErrKeyContainsWhiteSpace } // No Control Characters if unicode.IsControl(rune) { return ErrKeyContainsControlChar } } // return an empty error on success return nil }
func lexString(l *lexer) stateFn { l.next() for { switch r := l.next(); { case r == '"': l.emit(itemString) return lexText case r == '\\': if l.accept(`"\/bfnrt`) { break } else if r := l.next(); r == 'u' { for i := 0; i < 4; i++ { if !l.accept(hexdigit) { return l.errorf("expected 4 hexadecimal digits") } } } else { return l.errorf("unsupported escape character") } case unicode.IsControl(r): return l.errorf("cannot contain control characters in strings") case r == eof: return l.errorf("unclosed string") } } }
// Stat calculates statistics for all runes read from r. func (m *Main) Stat(r io.RuneReader) (Stats, error) { var stats Stats for { // Read next character. ch, sz, err := r.ReadRune() if err == io.EOF { break } else if err != nil { return stats, err } // Calculate stats. stats.TotalN++ if unicode.IsControl(ch) { stats.ControlN++ } if unicode.IsDigit(ch) { stats.DigitN++ } if unicode.IsGraphic(ch) { stats.GraphicN++ } if unicode.IsLetter(ch) { stats.LetterN++ } if unicode.IsLower(ch) { stats.LowerN++ } if unicode.IsMark(ch) { stats.MarkN++ } if unicode.IsNumber(ch) { stats.NumberN++ } if unicode.IsPrint(ch) { stats.PrintN++ } if unicode.IsPunct(ch) { stats.PunctN++ } if unicode.IsSpace(ch) { stats.SpaceN++ } if unicode.IsSymbol(ch) { stats.SymbolN++ } if unicode.IsTitle(ch) { stats.TitleN++ } if unicode.IsUpper(ch) { stats.UpperN++ } if sz > 1 { stats.MultiByteN++ } } return stats, nil }
func EventLoop(s *selecta.Search) { for !s.Done { switch ev := termbox.PollEvent(); ev.Type { case termbox.EventKey: switch ev.Key { case termbox.KeyCtrlC: termbox.Close() os.Exit(0) case termbox.KeyBackspace, termbox.KeyBackspace2: s.Backspace() case termbox.KeyEnter: s.Done = true case termbox.KeyCtrlN: s.Down() case termbox.KeyCtrlP: s.Up() case termbox.KeyCtrlW: s.DeleteWord() default: char := rune(ev.Ch) if !unicode.IsControl(char) { s.AppendQuery(string(char)) } else if ev.Key == termbox.KeySpace { s.AppendQuery(" ") } } termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) DrawApp(s) } } }
func incrementCount(r rune, counts map[int]int) { switch { case unicode.IsControl(r): counts[isControl]++ case unicode.IsNumber(r): counts[isNumber]++ case unicode.IsDigit(r): counts[isDigit]++ case unicode.IsLetter(r): counts[isLetter]++ case unicode.IsMark(r): counts[isMark]++ case unicode.IsPunct(r): counts[isPunct]++ case unicode.IsSpace(r): counts[isSpace]++ case unicode.IsSymbol(r): counts[isSymbol]++ case unicode.IsPrint(r): counts[isPrint]++ case unicode.IsGraphic(r): counts[isGraphic]++ } }
func removeDross(s string) string { return strings.Map(func(r rune) rune { if r < 32 || r > 0x7f || unicode.IsControl(r) { return -1 } return r }, s) }
// isWord will return true if the rune is not a special character, and can only be a word character. func isWord(ch rune) bool { return !isNewLine(ch) && !isWhiteSpace(ch) && !isInLine(ch) && !isLineStart(ch) && !isBlock(ch) && !unicode.IsControl(ch) && !strings.ContainsRune("\\|];:", ch) }
func (self *Scanner) scanString() (*Token, error) { var ( buf bytes.Buffer screen bool stop bool ) // skip first quote char := self.read() if char != '"' { // todo err here? self.unread() return &Token{STRING, buf.String(), bytes.Runes(buf.Bytes())}, nil } buf.WriteRune(char) for !stop { char := self.read() if unicode.IsControl(char) { self.unread() stop = true continue } if screen { screen = false switch char { case '"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u': buf.WriteRune(char) default: return nil, fmt.Errorf("unexpected end of input: %q", string(char)) } continue } switch { case char == '\\': screen = true buf.WriteRune(char) case char == '"': buf.WriteRune(char) stop = true case char == eof: stop = true default: buf.WriteRune(char) } } return &Token{STRING, buf.String(), bytes.Runes(buf.Bytes())}, nil }
// Given two strings, compute a score representing whether the internal // boundary falls on logical boundaries // Scores range from 6 (best) to 0 (worst). func semanticScore(one, two string) (score int) { if one == "" || two == "" { // Edges are the best return 6 } // Each port of this function behaves slightly differently due to // subtle differences in each language's definition of things like // 'whitespace'. Since this function's purpose is largely cosmetic, // the choice has been made to use each language's native features // rather than force total conformity. r1 := lastRune(one) r2 := firstRune(two) nonAlphaNum1 := !unicode.IsLetter(r1) && !unicode.IsDigit(r1) nonAlphaNum2 := !unicode.IsLetter(r2) && !unicode.IsDigit(r2) space1 := nonAlphaNum1 && unicode.IsSpace(r1) space2 := nonAlphaNum2 && unicode.IsSpace(r2) lineBreak1 := space1 && unicode.IsControl(r1) lineBreak2 := space2 && unicode.IsControl(r2) blankLine1 := lineBreak1 && blankLineEnd.MatchString(one) blankLine2 := lineBreak2 && blankLineStart.MatchString(two) switch { case blankLine1 || blankLine2: // blank lines score = 5 case lineBreak1 || lineBreak2: // line breaks score = 4 case nonAlphaNum1 && !space1 && space2: // end of sentences score = 3 case space1 || space2: // whitespace score = 2 case nonAlphaNum1 || nonAlphaNum2: // non-alphanumeric score = 1 } return }
// hasContent indicates if a string has any character that's not a whitespace or control character func hasContent(text string) bool { if len(text) == 0 { return false } for _, r := range text { if !unicode.IsSpace(r) && !unicode.IsControl(r) { return true } } return false }
func main() { counts := make(map[rune]int) // counts of Unicode characters var utflen [utf8.UTFMax]int // count of lengths of UTF-8 encodings invalid := 0 // count of invalid UTF-8 characters cats := make(map[string]int) // counts of Unicode categories // In a terminal, use CTRL+Z at line start to signal EOF with ENTER. in := bufio.NewReader(os.Stdin) for { r, n, err := in.ReadRune() // returns rune, nbytes, error if err == io.EOF { break } if err != nil { fmt.Fprintf(os.Stderr, "charcount: %v\n", err) os.Exit(1) } if r == unicode.ReplacementChar && n == 1 { invalid++ continue } switch { case unicode.IsLetter(r): cats["letter"]++ case unicode.IsDigit(r): cats["digit"]++ case unicode.IsControl(r): cats["control"]++ case unicode.IsMark(r): cats["mark"]++ case unicode.IsPunct(r): cats["punct"]++ case unicode.IsSymbol(r): cats["symbol"]++ } counts[r]++ utflen[n-1]++ } fmt.Printf("rune\tcount\n") for c, n := range counts { fmt.Printf("%q\t%d\n", c, n) } fmt.Print("\nlen\tcount\n") for i, n := range utflen { fmt.Printf("%d\t%d\n", i+1, n) } fmt.Print("\ncat\tcount\n") for s, n := range cats { fmt.Printf("%v\t%d\n", s, n) } fmt.Printf("\n%d invalid UTF-8 characters\n", invalid) }
func main() { in := bufio.NewReader(os.Stdin) counts := make(map[string]int) // counts of Unicode character types var utflen [utf8.UTFMax + 1]int // count of lengths of UTF-8 encodings invalid := 0 // count of invalid UTF-8 characters for { r, n, err := in.ReadRune() // returns rune, nbytes, error if err == io.EOF { break } if err != nil { fmt.Fprintf(os.Stderr, "charcount: %v\n", err) os.Exit(1) } if r == unicode.ReplacementChar && n == 1 { invalid++ continue } switch { case unicode.IsControl(r): counts["control"]++ case unicode.IsLetter(r): counts["letter"]++ case unicode.IsMark(r): counts["mark"]++ case unicode.IsNumber(r): counts["number"]++ case unicode.IsPunct(r): counts["punct"]++ case unicode.IsSpace(r): counts["space"]++ case unicode.IsSymbol(r): counts["symbol"]++ } utflen[n]++ } fmt.Printf("rune\tcount\n") for c, n := range counts { fmt.Printf("%q\t%d\n", c, n) } fmt.Print("\nlen\tcount\n") for i, n := range utflen { if i > 0 { fmt.Printf("%d\t%d\n", i, n) } } if invalid > 0 { fmt.Printf("\n%d invalid UTF-8 characters\n", invalid) } }
func sanitizeIPTablesOutput(output []byte) string { limit := 200 return strings.Map(func(ch rune) rune { if limit == 0 { return -1 } limit-- if unicode.IsControl(ch) { ch = ' ' } return ch }, string(output)) }
func (w *Writer) writeLabel(s string) error { if s == "" { return ErrLabelInvalid } var err error for _, c := range s { if c == ':' || c == w.Delimiter || c == '\n' || unicode.IsControl(c) || !unicode.IsPrint(c) { return ErrLabelInvalid } if _, err = w.w.WriteRune(c); err != nil { return err } } return nil }
func StringTransform(s string) string { v := make([]rune, 0, len(s)) for i, r := range s { if r == utf8.RuneError { _, size := utf8.DecodeRuneInString(s[i:]) if size == 1 { continue } } //check unicode chars if !unicode.IsControl(r) { v = append(v, r) } } s = string(v) return strings.TrimSpace(s) }
func (r *Reader) parseLabel() (string, bool, error) { r.label.Reset() for { r1, err := r.readRune() if err != nil { return "", false, err } else if r1 == ':' { return strings.TrimSpace(r.label.String()), false, nil } else if r1 == '\n' { return "", true, nil } else if r1 == '\t' { return "", false, nil // no label } else if unicode.IsControl(r1) || !unicode.IsPrint(r1) { return "", false, errors.New(fmt.Sprintf("line %d: invalid rune at label", r.line)) } r.label.WriteRune(r1) } panic("unreachable") }
func isSpecialChar(s string) (t bool, err error) { r := []rune(s) l := len(r) if l == 0 { return } if l != 1 { err = fmt.Errorf("string must contains only one character, got %s", s) return } if unicode.IsPunct(r[0]) || unicode.IsSymbol(r[0]) || unicode.IsSpace(r[0]) || unicode.IsControl(r[0]) { t = true return } return }
// #5979 func TestEventsRedirectStdout(t *testing.T) { since := time.Now().Unix() cmd(t, "run", "busybox", "true") defer deleteAllContainers() file, err := ioutil.TempFile("", "") if err != nil { t.Fatalf("could not create temp file: %v", err) } defer os.Remove(file.Name()) command := fmt.Sprintf("%s events --since=%d --until=%d > %s", dockerBinary, since, time.Now().Unix(), file.Name()) _, tty, err := pty.Open() if err != nil { t.Fatalf("Could not open pty: %v", err) } cmd := exec.Command("sh", "-c", command) cmd.Stdin = tty cmd.Stdout = tty cmd.Stderr = tty if err := cmd.Run(); err != nil { t.Fatalf("run err for command %q: %v", command, err) } scanner := bufio.NewScanner(file) for scanner.Scan() { for _, c := range scanner.Text() { if unicode.IsControl(c) { t.Fatalf("found control character %v", []byte(string(c))) } } } if err := scanner.Err(); err != nil { t.Fatalf("Scan err for command %q: %v", command, err) } logDone("events - redirect stdout") }
func (l *Lexer) Lex() error { for !l.isEOF() { switch { case isNumber(l.current()), (l.current() == '+' || l.current() == '-') && l.hasNext() && isNumber(l.peek(1)): err := l.lexNumber() if err != nil { return err } case (l.current() == '>' || l.current() == '<' || l.current() == '!') && l.hasNext() && l.peek(1) == '=': l.consume() l.consume() l.addToken(Identifier) case l.current() == '_', l.current() == '+', l.current() == '-', l.current() == '*', l.current() == '/', l.current() == '=', l.current() == '>', l.current() == '<': l.consume() l.addToken(Identifier) case l.current() == '&' && l.hasNext() && IsIdentifierStart(l.peek(1)): l.lexIdentifierOrKeyword(false) case l.current() == ':' && l.hasNext() && IsIdentifierStart(l.peek(1)): l.lexIdentifierOrKeyword(true) case IsIdentifierStart(l.current()): l.lexIdentifierOrKeyword(false) case l.current() == '"': err := l.lexString() if err != nil { return err } case l.current() == '(', l.current() == ')', l.current() == '\'': l.lexSeparator() case l.current() == ';': l.lexComment() case l.current() < ' ', unicode.IsControl(l.current()), unicode.IsSpace(l.current()): l.ignore(1) default: return NewSyntaxError(l.newPos(), "unexpected character '%c'", l.current()) } } return nil }
func selectdoc(files []string, da *DocselArgs) string { dcmd := exec.Command("dmenu", "-nb", da.nb, "-nf", da.nf, "-sf", da.sf, "-sb", da.sb, "-fn", da.fn) din, _ := dcmd.StdinPipe() dout, _ := dcmd.StdoutPipe() rbuf := make([]byte, 512) dcmd.Start() for _, doc := range getfilenames(files) { din.Write([]byte(doc)) din.Write([]byte{'\n'}) } din.Close() dout.Read(rbuf) dcmd.Wait() doc := strings.TrimFunc(string(rbuf), func(r int) bool { return unicode.IsControl(r) || unicode.IsSpace(r) }) return getfilefromname(files, doc) }
// Decode decodes one ANSI terminal command from s. // // s should be connected to a client program that expects an // ANSI terminal on the other end. It will push bytes to us that we are meant // to intepret as terminal control codes, or text to place onto the terminal. // // This Command alone does not actually update the terminal. You need to pass // it to VT100.Process(). // // You should not share s with any other reader, because it could leave // the stream in an invalid state. func Decode(s io.RuneScanner) (Command, error) { r, size, err := s.ReadRune() if err != nil { return nil, err } if r == unicode.ReplacementChar && size == 1 { return nil, fmt.Errorf("non-utf8 data from reader") } if r == escape || r == monogramCsi { // At beginning of escape sequence. s.UnreadRune() return scanEscapeCommand(s) } if unicode.IsControl(r) { return controlCommand(r), nil } return runeCommand(r), nil }
func cleanControl(in []rune) []rune { var b []rune = nil for s := range in { if unicode.IsControl(in[s]) && !unicode.IsSpace(in[s]) { if b == nil { b = make([]rune, 0, len(in)) b = append(b, in[:s]...) } } else { if b != nil { b = append(b, in[s]) } } s++ } if b == nil { return in } return b }
// IsValidUnixUser returns true if passed string can be used // to create a valid UNIX user func IsValidUnixUser(u string) bool { /* See http://www.unix.com/man-page/linux/8/useradd: On Debian, the only constraints are that usernames must neither start with a dash ('-') nor contain a colon (':') or a whitespace (space: ' ', end of line: '\n', tabulation: '\t', etc.). Note that using a slash ('/') may break the default algorithm for the definition of the user's home directory. */ if len(u) > maxUsernameLen || len(u) == 0 || u[0] == '-' { return false } if strings.ContainsAny(u, ":/") { return false } for _, r := range u { if unicode.IsSpace(r) || unicode.IsControl(r) { return false } } return true }
func print_rune_types(char rune) { if unicode.IsControl(char) { fmt.Println(" Control") } if unicode.IsDigit(char) { fmt.Println(" Digit") } if unicode.IsGraphic(char) { fmt.Println(" Graphic") } if unicode.IsLetter(char) { fmt.Println(" Letter") } if unicode.IsLower(char) { fmt.Println(" Lower") } if unicode.IsMark(char) { fmt.Println(" Mark") } if unicode.IsPrint(char) { fmt.Println(" Print") } if unicode.IsPunct(char) { fmt.Println(" Punct") } if unicode.IsSpace(char) { fmt.Println(" Space") } if unicode.IsSymbol(char) { fmt.Println(" Symbol") } if unicode.IsTitle(char) { fmt.Println(" Title") } if unicode.IsUpper(char) { fmt.Println(" Upper") } }
func main() { fmt.Println(unicode.IsGraphic('1')) // true: 1은 화면에 표시되는 숫자이므로 true fmt.Println(unicode.IsGraphic('a')) // true: a는 화면에 표시되는 문자이므로 true fmt.Println(unicode.IsGraphic('한')) // true: '한'은 화면에 표시되는 문자이므로 true fmt.Println(unicode.IsGraphic('漢')) // true: '漢'은 화면에 표시되는 문자이므로 true fmt.Println(unicode.IsGraphic('\n')) // false: \n 화면에 표시되는 문자가 아니므로 false fmt.Println(unicode.IsLetter('a')) // true: a는 문자이므로 true fmt.Println(unicode.IsLetter('1')) // false: 1은 문자가 아니므로 false fmt.Println(unicode.IsDigit('1')) // true: 1은 숫자이므로 true fmt.Println(unicode.IsControl('\n')) // true: \n은 제어 문자이므로 true fmt.Println(unicode.IsMark('\u17c9')) // true: \u17c9는 마크이므로 true fmt.Println(unicode.IsPrint('1')) // true: 1은 Go 언어에서 표시할 수 있으므로 true fmt.Println(unicode.IsPunct('.')) // true: .은 문장 부호이므로 true fmt.Println(unicode.IsSpace(' ')) // true: ' '는 공백이므로 true fmt.Println(unicode.IsSymbol('♥')) // true: ♥는 심볼이므로 true fmt.Println(unicode.IsUpper('A')) // true: A는 대문자이므로 true fmt.Println(unicode.IsLower('a')) // true: a는 소문자이므로 true }