func decode_stdin(encs []mojibake.Encoding) int { decoder, err := mojibake.NewDecoder(os.Stdout, encs...) if err != nil && !*flag_force { fmt.Fprintf(os.Stderr, "unmoji: %v\n", err) return 1 } io.Copy(decoder, os.Stdin) err = decoder.Close() if err != nil && !*flag_force { fmt.Fprintf(os.Stderr, "unmoji: %v\n", err) return 1 } return 0 }
func decode_args(encs []mojibake.Encoding) int { if flag.NArg() == 0 { return 0 } buf := new(bytes.Buffer) dec, err := mojibake.NewDecoder(buf, encs...) if err != nil { fmt.Fprintf(os.Stderr, "unmoji: %v\n", err) return 1 } decoded := make([]string, 0, flag.NArg()) args := flag.Args() // need this in case any of them error and -f is given // so we can selectively include input filenames input := make([]string, 0, len(args)) for _, garbled := range args { r := strings.NewReader(garbled) io.Copy(dec, r) _, err := dec.Flush() if err != nil { if *flag_force { continue } fmt.Fprintf(os.Stderr, "unmoji: %v\n", err) dec.Close() return 1 } p := buf.Next(buf.Len()) input = append(input, garbled) decoded = append(decoded, string(p)) } dec.Close() if !*flag_rename { for _, s := range decoded { fmt.Println(s) } return 0 } if !*flag_really { for i, filename := range input { fmt.Printf("\"%s\" → \"%s\"\n", filename, decoded[i]) } return 0 } for i, filename := range input { if filename == decoded[i] && !*flag_force { fmt.Fprintf(os.Stderr, "unmoji: rename \"%s\": source and destination are the same\n", filename) return 1 } err := os.Rename(filename, decoded[i]) if err != nil && !*flag_force { fmt.Fprintf(os.Stderr, "unmoji: %v\n", err) return 1 } } return 0 }