<commit_before>package main

// An interactive client for 9P servers.

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"path"
	"strings"
	"go9p.googlecode.com/hg/p"
	"go9p.googlecode.com/hg/p/clnt"
)

var addr = flag.String("addr", "127.0.0.1:5640", "network address")
var ouser = flag.String("user", "", "user to connect as")
var cmdfile = flag.String("file", "", "read commands from file")
var prompt = flag.String("prompt", "9p> ", "prompt for interactive client")
var debug = flag.Bool("d", false, "enable debugging (fcalls)")
var debugall = flag.Bool("D", false, "enable debugging (raw packets)")

var cwd = "/"
var cfid *clnt.Fid

type Cmd struct {
	fun  func(c *clnt.Clnt, s []string)
	help string
}

var cmds map[string]*Cmd

func init() {
	cmds = make(map[string]*Cmd)
	cmds["write"] = &Cmd{cmdwrite, "write file string [...]\t«write the unmodified string to file, create file if necessary»"}
	cmds["echo"] = &Cmd{cmdecho, "echo file string [...]\t«echo string to file (newline appended)»"}
	cmds["stat"] = &Cmd{cmdstat, "stat file [...]\t«stat file»"}
	cmds["ls"] = &Cmd{cmdls, "ls [-l] file [...]\t«list contents of directory or file»"}
	cmds["cd"] = &Cmd{cmdcd, "cd dir\t«change working directory»"}
	cmds["cat"] = &Cmd{cmdcat, "cat file [...]\t«print the contents of file»"}
	cmds["mkdir"] = &Cmd{cmdmkdir, "mkdir dir [...]\t«create dir on remote server»"}
	cmds["get"] = &Cmd{cmdget, "get file [local]\t«get file from remote server»"}
	cmds["put"] = &Cmd{cmdput, "put file [remote]\t«put file on the remote server as 'file'»"}
	cmds["pwd"] = &Cmd{cmdpwd, "pwd\t«print working directory»"}
	cmds["rm"] = &Cmd{cmdrm, "rm file [...]\t«remove file from remote server»"}
	cmds["help"] = &Cmd{cmdhelp, "help [cmd]\t«print available commands or help on cmd»"}
	cmds["quit"] = &Cmd{cmdquit, "quit\t«exit»"}
	cmds["exit"] = &Cmd{cmdquit, "exit\t«quit»"}
}

// normalize user-supplied path. path starting with '/' is left untouched, otherwise is considered
// local from cwd
func normpath(s string) string {
	if s[0] == '/' {
		return path.Clean(s)
	}
	return path.Clean(cwd + "/" + s)
}

func b(mode uint32, s uint8) string {
	var bits = []string{"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"}
	return bits[(mode>>s)&7]
}

// Convert file mode bits to string representation
func modetostr(mode uint32) string {
	d := "-"
	if mode&p.DMDIR != 0 {
		d = "d"
	} else if mode&p.DMAPPEND != 0 {
		d = "a"
	}
	return fmt.Sprintf("%s%s%s%s", d, b(mode, 6), b(mode, 3), b(mode, 0))
}

// Write the string s to remote file f. Create f if it doesn't exist
func writeone(c *clnt.Clnt, f, s string) {
	fname := normpath(f)
	file, err := c.FOpen(fname, p.OWRITE|p.OTRUNC)
	if err != nil {
		file, err = c.FCreate(fname, 0666, p.OWRITE)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error opening %s: %s\n", fname, err.Error)
			return<commit_msg>fixed a bug that caused a runtime fault if 'cd' was issued without any arguments.<commit_after>package main

// An interactive client for 9P servers.

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"path"
	"strings"
	"go9p.googlecode.com/hg/p"
	"go9p.googlecode.com/hg/p/clnt"
)

var addr = flag.String("addr", "127.0.0.1:5640", "network address")
var ouser = flag.String("user", "", "user to connect as")
var cmdfile = flag.String("file", "", "read commands from file")
var prompt = flag.String("prompt", "9p> ", "prompt for interactive client")
var debug = flag.Bool("d", false, "enable debugging (fcalls)")
var debugall = flag.Bool("D", false, "enable debugging (raw packets)")

var cwd = "/"
var cfid *clnt.Fid

type Cmd struct {
	fun  func(c *clnt.Clnt, s []string)
	help string
}

var cmds map[string]*Cmd

func init() {
	cmds = make(map[string]*Cmd)
	cmds["write"] = &Cmd{cmdwrite, "write file string [...]\t«write the unmodified string to file, create file if necessary»"}
	cmds["echo"] = &Cmd{cmdecho, "echo file string [...]\t«echo string to file (newline appended)»"}
	cmds["stat"] = &Cmd{cmdstat, "stat file [...]\t«stat file»"}
	cmds["ls"] = &Cmd{cmdls, "ls [-l] file [...]\t«list contents of directory or file»"}
	cmds["cd"] = &Cmd{cmdcd, "cd dir\t«change working directory»"}
	cmds["cat"] = &Cmd{cmdcat, "cat file [...]\t«print the contents of file»"}
	cmds["mkdir"] = &Cmd{cmdmkdir, "mkdir dir [...]\t«create dir on remote server»"}
	cmds["get"] = &Cmd{cmdget, "get file [local]\t«get file from remote server»"}
	cmds["put"] = &Cmd{cmdput, "put file [remote]\t«put file on the remote server as 'file'»"}
	cmds["pwd"] = &Cmd{cmdpwd, "pwd\t«print working directory»"}
	cmds["rm"] = &Cmd{cmdrm, "rm file [...]\t«remove file from remote server»"}
	cmds["help"] = &Cmd{cmdhelp, "help [cmd]\t«print available commands or help on cmd»"}
	cmds["quit"] = &Cmd{cmdquit, "quit\t«exit»"}
	cmds["exit"] = &Cmd{cmdquit, "exit\t«quit»"}
}

// normalize user-supplied path. path starting with '/' is left untouched, otherwise is considered
// local from cwd
func normpath(s string) string {
	if len(s) > 0 {
		if s[0] == '/' {
			return path.Clean(s)
		}
		return path.Clean(cwd + "/" + s)
	}
	return "/"
}

func b(mode uint32, s uint8) string {
	var bits = []string{"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"}
	return bits[(mode>>s)&7]
}

// Convert file mode bits to string representation
func modetostr(mode uint32) string {
	d := "-"
	if mode&p.DMDIR != 0 {
		d = "d"
	} else if mode&p.DMAPPEND != 0 {
		d = "a"
	}
	return fmt.Sprintf("%s%s%s%s", d, b(mode, 6), b(mode, 3), b(mode, 0))
}

// Write the string s to remote file f. Create f if it doesn't exist
func writeone(c *clnt.Clnt, f, s string) {
	fname := normpath(f)
	file, err := c.FOpen(fname, p.OWRITE|p.OTRUNC)
	if err != nil {
		file, err = c.FCreate(fname, 0666, p.OWRITE)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error opening %s: %s\n", fname, err.Error)
			return
{'fraction_non_alphanumeric': 0.2002422983731395, 'fraction_numerical': 0.010557286258220838, 'mean_word_length': 4.0339721254355405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5356827', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2242, 'n_words': 743}