示例#1
0
// The following command `remotectl -profile test.sh -list` would result in
// len(args) <= i returning false.
func parseArgs(args []string) (query, cmd string) {
	i := flag.NFlag() + 1

	cmd = strings.Join(flag.Args(), " ")

	if len(args) > i && args[i] != "--" {
		query = flag.Args()[0]
		cmd = strings.Join(flag.Args()[1:], " ")
	}

	return query, cmd
}
示例#2
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "%s", Help)
		os.Exit(1)
	}
	flag.Parse()

	if *constVersion {
		fmt.Printf("Unicode Version: %s\n", unicode.Version)
		os.Exit(0)
	} else if *version {
		fmt.Printf("%s\n", Version)
		os.Exit(0)
	}

	if !(*printBytes || *printChars || *printLines || *printWords || *printLineLength) {
		*printLines = true
		*printBytes = true
		*printWords = true
	}

	// This is a gross attempt to simulate this...
	// (print_lines + print_words + print_chars +
	//	print_bytes + print_linelength) == 1
	//
	// Since Go can't add booleans (e.g. false + true == 1)
	// and checking that *only* one of 5 bool variables would be sloppy,
	// we check the number of set flags and the remaining non-'print' flags
	// which is a much smaller set of conditions to check
	//
	// 1 flag and it's --files0-from
	if (flag.NFlag() == 1 && *filesFrom == "" && *tabWidth == 8) ||
		// 2 flags and one's *filesFrom OR *tabWidth
		(flag.NFlag() == 2 && (*filesFrom != "" || *tabWidth != 8)) ||
		// 3 flags and two are *filesFrom AND *tabWidth
		(flag.NFlag() == 3 && *filesFrom != "" && *tabWidth != 8) {

		printOne = true
	}

	var (
		ok         = 0           // Return status.
		files      = flag.Args() // List of files.
		numFiles   = len(files)  // Number of files to wc.
		reasonable = true        // Can we read file list into memory?
		stdin      = true        // Are we reading from stdin?
		size       int64
	)

	if *filesFrom != "" {
		// Cannot specify files with --files0-from.
		if flag.NArg() > 0 {
			fatal.Fatalln("file operands cannot be combined with --files0-from")
		}

		// --files0-from is small enough to fit into RAM.
		if reasonable, size = isReasonable(*filesFrom); reasonable {
			files, numFiles = getFileList(*filesFrom, size)
			stdin = false
		}
	}

	fs := getFileStatus(numFiles, files)
	numberWidth = findNumberWidth(numFiles, fs)

	if !reasonable {
		var (
			file *os.File
			err  error
		)

		file = os.Stdin
		if *filesFrom != "-" {
			file, err = os.Open(*filesFrom)
		}

		if err != nil {
			fatal.Fatalf("cannot open: %s\n", *filesFrom)
		}
		defer file.Close()

		r := bufio.NewReader(file)
		i := 0
		for {
			fname, err := r.ReadString(NullByte)
			if err != nil {
				if err != io.EOF && i < 1 {
					fatal.Fatalln(err)
				}
				break
			}

			if len(fname) > 1 {
				// Trim trailing null-byte.
				if fname[len(fname)-1] == NullByte {
					fname = fname[:len(fname)-1]
				}
			} else {
				log.Fatalf("invalid zero-length file name at position: %d\n", i)
			}

			ok ^= wcFile(fname, nil)
			i++
		}

		numFiles = i
	} else {
		if stdin { // We're reading from Stdin.
			ok ^= wcFile("", fs[0])
		} else {
			for i, v := range files {
				ok ^= wcFile(v, fs[i])
			}
		}
	}

	if numFiles > 1 {
		writeCounts(totalLines, totalWords,
			totalChars, totalBytes, maxLineLength, "total")
	}

	// Return status.
	os.Exit(ok)
}
示例#3
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "%s", Help)
		os.Exit(0)
	}
	flag.Parse()

	p := profile.Start(profile.CPUProfile)

	if *constVersion {
		fmt.Printf("Unicode Version: %s\n", unicode.Version)
		os.Exit(0)
	}
	if *version {
		fmt.Printf("%s\n", Version)
		os.Exit(0)
	}

	if !(*printBytes || *printChars || *printLines || *printWords || *printLineLength) {
		*printLines = true
		*printBytes = true
		*printWords = true
	}

	// This is a gross attempt to simulate this...
	// (print_lines + print_words + print_chars +
	//	print_bytes + print_linelength) == 1
	//
	// Since Go can't add booleans (e.g. false + true == 1)
	// and checking that *only* one of 5 bool variables would be sloppy,
	// we check the number of set flags and the remaining non-'print' flags
	// which is a much smaller set of conditions to check
	//
	//	I could just use a loop, but this also removes a branch soooo...
	printOne =
		// 1 flag and it's not *filesFrom OR *tabWidth
		((flag.NFlag() == 1 && *filesFrom == "" && *tabWidth == 8) ||
			// 2 flags and one is *filesFrom OR *tabWidth
			(flag.NFlag() == 2 && (*filesFrom != "" || *tabWidth != 8)) ||
			// 3 flags and two are *filesFrom AND *tabWidth
			(flag.NFlag() == 3 && *filesFrom != "" && *tabWidth != 8))

	var (
		ok         int           // Return status.
		files      = flag.Args() // List of files.
		numFiles   = len(files)  // Number of files to wc.
		reasonable = true        // Can we read file list into memory?
		size       int64
	)

	if *filesFrom != "" {
		// Cannot specify files with --files0-from.
		if flag.NArg() > 0 {
			fatal("file operands cannot be combined with --files0-from")
		}

		// --files0-from is small enough to fit into RAM.
		if reasonable, size = isReasonable(*filesFrom); reasonable {
			files, numFiles = getFileList(*filesFrom, size)
		}
	}

	fs := getFileStatus(files)
	numberWidth = findNumberWidth(fs)

	if reasonable {
		for i, file := range files {
			ok ^= wcFile(file, fs[i])
		}
	} else {
		var err error

		file := os.Stdin
		if *filesFrom != "-" {
			file, err = os.Open(*filesFrom)
		}

		if err != nil {
			fatal("cannot open %q for reading: No such file or directory", *filesFrom)
		}
		defer file.Close()

		s := bufio.NewScanner(file)
		s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
			if atEOF && len(data) == 0 {
				return 0, nil, nil
			}
			if i := bytes.IndexByte(data, NullByte); i >= 0 {
				// We have a full newline-terminated line.
				return i + 1, dropNullByte(numFiles, data[0:i]), nil
			}
			// If we're at EOF, we have a final, non-terminated line. Return it.
			if atEOF {
				return len(data), dropNullByte(numFiles, data), nil
			}
			// Request more data.
			return 0, nil, nil
		})
		for ; s.Scan(); numFiles++ {
			ok ^= wcFile(s.Text(), fstatus{})
		}
		if err := s.Err(); err != nil {
			fatal("%v", err)
		}
	}

	if numFiles > 1 {
		writeCounts(totalLines, totalWords,
			totalChars, totalBytes, maxLineLength, "total")
	}

	p.Stop()

	// Return status.
	os.Exit(ok)
}
示例#4
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "%s\n", HELP)
		return
	}
	flag.Parse()

	uname := GenInfo()

	if *version {
		fmt.Printf("%s\n", VERSION)
		return
	}
	if flag.NFlag() == 0 {
		fmt.Printf("%s\n", uname.kname)
		return
	}
	if *all {
		r := fmt.Sprintf("%s %s %s %s %s %s %s", uname.kname, uname.node, uname.release, uname.kversion, uname.machine, uname.processor, uname.os)
		fmt.Println(r)
		return
	}
	if *kernelName {
		if GOOS == "linux" {
			fmt.Printf("%s ", "Linux")
		} else {
			fmt.Printf("%s ", GOOS)
		}
	}
	if *nodeName {
		hn, err := os.Hostname()
		if err == nil {
			fmt.Printf("%s ", hn)
		}
	}
	if *release {
		fmt.Printf("%s ", uname.release)
	}
	if *kernelVersion {
		fmt.Printf("%s ", uname.kversion)
	}
	if *machine {
		fmt.Printf("%s ", uname.machine)
	}
	if *processor {
		if uname.processor == "" {
			fmt.Printf("%s ", UNKNOWN)
		} else {
			fmt.Printf("%s ", uname.processor)
		}
	}
	if *hwPlatform {
		if uname.hardware == "" {
			fmt.Printf("%s ", UNKNOWN)
		} else {
			fmt.Printf("%s ", uname.hardware)
		}
	}
	if *operatingSystem {
		if uname.os == "linux" {
			fmt.Printf("%s ", "GNU/Linux")
		} else {
			fmt.Printf("%s ", uname.os)
		}
	}
	fmt.Println()
}
示例#5
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "%s", Help)
		os.Exit(1)
	}
	flag.Parse()

	if *constVersion {
		fmt.Printf("Unicode Version: %s\n", unicode.Version)
		os.Exit(0)
	} else if *version {
		fmt.Printf("%s\n", Version)
		os.Exit(0)
	}

	if !(*printBytes || *printChars || *printLines || *printWords || *printLineLength) {
		*printLines = true
		*printBytes = true
		*printWords = true
	}

	// This is a gross attempt to simulate this...
	// (print_lines + print_words + print_chars +
	//	print_bytes + print_linelength) == 1
	//
	// Since Go can't add booleans (e.g. false + true == 1)
	// and checking that *only* one of 5 bool variables would be sloppy,
	// we check the number of set flags and the remaining non-'print' flags
	// which is a much smaller set of conditions to check
	//
	// 1 flag and it's --files0-from
	if (flag.NFlag() == 1 && *filesFrom == "" && *tabWidth == 8) ||
		// 2 flags and one's *filesFrom OR *tabWidth
		(flag.NFlag() == 2 && (*filesFrom != "" || *tabWidth != 8)) ||
		// 3 flags and two are *filesFrom AND *tabWidth
		(flag.NFlag() == 3 && *filesFrom != "" && *tabWidth != 8) {

		printOne = true
	}

	var (
		ok         = 0           // dictates return status
		total      bool          // print totals?
		files      = flag.Args() // list of files
		numFiles   = len(files)  // number of files to wc()
		reasonable = true        // can we read file list into memory?
	)

	if *filesFrom != "" {
		// cannot specify files with --files0-from
		if flag.NArg() > 0 {
			fatal.Fatalln("file operands cannot be combined with --files0-from")
		}

		// is small enough to fit into RAM
		if good, stat := isReasonable(*filesFrom); good {
			reasonable = true
			files = getFileList(*filesFrom, stat)
			numFiles = len(files)

			if numFiles == 0 {
				fatal.Fatalln("--files0-from contained no usable files")
			}
		} else {
			reasonable = false
		}
		// stdin
	} else if numFiles == 0 {
		files = nil
		numFiles = 1
	}

	fs := getFileStatus(numFiles, files)
	numberWidth = findNumberWidth(numFiles, fs)

	if !reasonable {
		var (
			fi  *os.File
			err error
		)

		if *filesFrom == "-" {
			fi = os.Stdin
		} else {
			fi, err = os.Open(*filesFrom)
		}
		if err != nil {
			fatal.Fatalf("cannot open: %s", *filesFrom)
		}
		defer fi.Close()

		r := bufio.NewReader(fi)
		i := 0
		for {
			fname, err := r.ReadString(NullByte)
			if err != nil {
				if err == io.EOF {
					if i > 0 {
						break
					}
				} else {
					fatal.Fatalln(err)
				}
			}

			if len(fname) > 1 {
				// \0
				if fname[len(fname)-1] == NullByte {
					fname = fname[:len(fname)-1]
				}
			}

			ok ^= wcFile(fname, nil)
			i++
		}

		if i > 1 {
			total = true
		}
	} else {
		// stdin
		if files == nil {
			ok ^= wcFile("", fs[0])
		} else {
			for i, v := range files {
				ok ^= wcFile(v, fs[i])
			}
		}
	}

	if numFiles > 1 {
		total = true
	}

	if total {
		writeCounts(totalLines, totalWords,
			totalChars, totalBytes, maxLineLength, "total")
	}

	// return status
	os.Exit(ok)
}