diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-09-07 14:29:08 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-09-07 15:16:03 -0600 |
commit | da5d3607c7e51043f425dac87ac723395e092551 (patch) | |
tree | a5d2934b90aa59718bb7503a763fdb17d2487ec0 | |
parent | 48674e9aa96643e960c54f211bab256047a3d88d (diff) |
execing included gophermaps
-rw-r--r-- | gopher/gophermap/extended.go | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/gopher/gophermap/extended.go b/gopher/gophermap/extended.go index fed8dfc..8b04104 100644 --- a/gopher/gophermap/extended.go +++ b/gopher/gophermap/extended.go @@ -2,10 +2,12 @@ package gophermap import ( "bufio" + "bytes" "errors" "io" "net/url" "os" + "os/exec" "path/filepath" "sort" "strconv" @@ -25,16 +27,16 @@ type ExtendedMapDocument struct { } // ParseExtended parses a gophermap document including gophernicus extensions. -func ParseExtended(input io.Reader, location *url.URL) (ExtendedMapDocument, error) { +func ParseExtended(input io.Reader, location *url.URL) (*ExtendedMapDocument, error) { rdr := bufio.NewReader(input) - doc := ExtendedMapDocument{Location: location} + doc := &ExtendedMapDocument{Location: location} outer: for num := 1; ; num += 1 { line, err := rdr.ReadString('\n') isEOF := errors.Is(err, io.EOF) if err != nil && !isEOF { - return doc, err + return nil, err } line = strings.TrimRight(line, "\r\n") @@ -164,7 +166,7 @@ type FileSystemSettings struct { } // Compatible builds a standards-compliant gophermap from the current extended menu. -func (edoc ExtendedMapDocument) Compatible(cwd string, settings FileSystemSettings) (gopher.MapDocument, string, error) { +func (edoc *ExtendedMapDocument) Compatible(cwd string, settings FileSystemSettings) (gopher.MapDocument, string, error) { doc := gopher.MapDocument{} title := "" @@ -212,7 +214,7 @@ func (edoc ExtendedMapDocument) Compatible(cwd string, settings FileSystemSettin case VHostListType: return nil, "", errors.New("Virtual host listings '%' are not supported") case InclusionType: - subEdoc, err := openExtended(item.Selector, edoc.Location) + subEdoc, err := openExtended(item.Selector, edoc.Location, settings) if err != nil { return nil, "", err } @@ -241,14 +243,40 @@ func (edoc ExtendedMapDocument) Compatible(cwd string, settings FileSystemSettin return doc, title, nil } -func openExtended(path string, location *url.URL) (ExtendedMapDocument, error) { +func openExtended(path string, location *url.URL, settings FileSystemSettings) (*ExtendedMapDocument, error) { file, err := os.Open(path) if err != nil { - return ExtendedMapDocument{}, err + return nil, err } defer func() { _ = file.Close() }() + if settings.Exec { + info, err := file.Stat() + if err != nil { + return nil, err + } + m := info.Mode() + if m.IsRegular() && m&5 == 5 { + cmd := exec.Command(path) + cmd.Env = []string{} + cmd.Dir = filepath.Dir(path) + + outbuf := &bytes.Buffer{} + cmd.Stdout = outbuf + + if err := cmd.Run(); err != nil { + var exErr *exec.ExitError + if errors.As(err, &exErr) { + return nil, fmt.Errorf("Inclusion exec returned exit code %d", exErr.ExitCode()) + } + return nil, err + } + + return ParseExtended(outbuf, location) + } + } + return ParseExtended(file, location) } |