<commit_before>package shell

import (
	flag "../../appletflag"
	"../../common"
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"
)

func Main() {
	flag.Parse()
	call := flag.Args()
	var in *common.BufferedReader
	interactive := true
	if len(call) > 1 {
		call = call[0:1]
	}
	if len(call) == 1 {
		f, e := os.Open(call[0])
		if e != nil {
			log.Fatalf("Could not open input file %s: %s\n", call[1], e)
		}
		defer f.Close()
		in = common.NewBufferedReader(f)
		interactive = false
	} else {
		in = common.NewBufferedReader(os.Stdin)
	}

	var e error
	var line string
	for e == nil {
		if interactive {
			fmt.Print("> ")
		}
		line, e = in.ReadWholeLine()
		if e != nil {
			log.Fatalf("Could not read line: %s\n", e)
		}
		if isComment(line) {
			continue
		}
		params, ce := common.Parameterize(line)
		if ce != nil {
			common.DumpError(ce)
			continue
		}
		ce = execute(params)
		if ce != nil {
			common.DumpError(ce)
			continue
		}
	}
	return
}

func isComment(line string) bool {
	line = strings.TrimSpace(line)
	return strings.HasPrefix(line, "#")
}

func execute(cmd []string) error {
	if len(cmd) == 0 {
		return nil
	}
	if isBuiltIn(cmd[0]) {
		builtin := Builtins[cmd[0]]
		return builtin(cmd)
	} else {
		cmd := exec.Command(cmd[0], cmd[1:]...)
		cmd.Stdout = os.Stdout
		cmd.Stdin = os.Stdin
		cmd.Stderr = os.Stderr
		e := cmd.Run()
		return e
	}
	return nil
}
<commit_msg>Expand environment variables in shell<commit_after>package shell

import (
	flag "../../appletflag"
	"../../common"
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"
)

func Main() {
	flag.Parse()
	call := flag.Args()
	var in *common.BufferedReader
	interactive := true
	if len(call) > 1 {
		call = call[0:1]
	}
	if len(call) == 1 {
		f, e := os.Open(call[0])
		if e != nil {
			log.Fatalf("Could not open input file %s: %s\n", call[1], e)
		}
		defer f.Close()
		in = common.NewBufferedReader(f)
		interactive = false
	} else {
		in = common.NewBufferedReader(os.Stdin)
	}

	var e error
	var line string
	for e == nil {
		if interactive {
			fmt.Print("> ")
		}
		line, e = in.ReadWholeLine()
		if e != nil {
			log.Fatalf("Could not read line: %s\n", e)
		}
		if isComment(line) {
			continue
		}
		params, ce := common.Parameterize(line)
		if ce != nil {
			common.DumpError(ce)
			continue
		}
		params = expandEnvs(params)
		ce = execute(params)
		if ce != nil {
			common.DumpError(ce)
			continue
		}
	}
	return
}

// Replace environment variables with the content
func expandEnvs(params []string) []string {
	for i, param := range params {
		if len(param) > 1 && strings.HasPrefix(param, "$") {
			params[i] = os.Getenv(param[1:])
		}
	}
	return params
}

func isComment(line string) bool {
	line = strings.TrimSpace(line)
	return strings.HasPrefix(line, "#")
}

func execute(cmd []string) error {
	if len(cmd) == 0 {
		return nil
	}
	if isBuiltIn(cmd[0]) {
		builtin := Builtins[cmd[0]]
		return builtin(cmd)
	} else {
		cmd := exec.Command(cmd[0], cmd[1:]...)
		cmd.Stdout = os.Stdout
		cmd.Stdin = os.Stdin
		cmd.Stderr = os.Stderr
		e := cmd.Run()
		return e
	}
	return nil
}

{'fraction_non_alphanumeric': 0.1641644908616188, 'fraction_numerical': 0.007832898172323759, 'mean_word_length': 2.9599483204134365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '732402', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1223, 'n_words': 299}