From da5d3607c7e51043f425dac87ac723395e092551 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Thu, 7 Sep 2023 14:29:08 -0600 Subject: execing included gophermaps --- gopher/gophermap/extended.go | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'gopher/gophermap') 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) } -- cgit v1.2.3