~sbinet/star-tex

star-tex/cmd/dvi-dump/main.go -rw-r--r-- 2.1 KiB
9bb27e8aSebastien Binet ci: bump to freebsd/latest 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright ©2021 The star-tex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Command dvi-dump displays the content of a DVI file in a human readable
// format or JSON.
package main // import "star-tex.org/x/tex/cmd/dvi-dump"

import (
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	"star-tex.org/x/tex/dvi"
	"star-tex.org/x/tex/kpath"
)

func main() {
	log.SetFlags(0)
	log.SetPrefix("dvi-dump: ")

	var (
		doJSON = flag.Bool("json", false, "enable JSON output")
		texmf  = flag.String("texmf", "", "path to TexMF root")
	)

	flag.Parse()

	if flag.NArg() == 0 {
		flag.Usage()
		log.Fatalf("missing input dvi file")
	}

	xmain(flag.Arg(0), *doJSON, *texmf)
}

func xmain(fname string, doJSON bool, texmf string) {
	f, err := os.Open(fname)
	if err != nil {
		log.Fatalf("could not open DVI file %q: %+v", fname, err)
	}
	defer f.Close()

	ctx := kpath.New()
	if texmf != "" {
		ctx, err = kpath.NewFromFS(os.DirFS(texmf))
		if err != nil {
			log.Fatalf("could not create kpath context: %+v", err)
		}
	}

	switch {
	case doJSON:
		err = jsonProcess(f)
	default:
		err = interp(ctx, os.Stdout, f)
	}
	if err != nil {
		log.Fatalf("could not process DVI file %q: %+v", fname, err)
	}
}

func jsonProcess(f *os.File) error {
	w := os.Stdout
	o := json.NewEncoder(w)

	err := dvi.Dump(f, func(cmd dvi.Cmd) error {
		var v struct {
			Cmd  string  `json:"cmd"`
			Args dvi.Cmd `json:"args,omitempty"`
		}
		v.Cmd = cmd.Name()
		v.Args = cmd
		return o.Encode(v)
	})

	if err != nil {
		return fmt.Errorf("could not read DVI file: %w", err)
	}

	return nil
}

func interp(ctx kpath.Context, w io.Writer, f *os.File) error {
	vm := dvi.NewMachine(dvi.WithContext(ctx), dvi.WithLogOutput(w))
	raw, err := io.ReadAll(f)
	if err != nil {
		return fmt.Errorf("could not read DVI program file: %w", err)
	}

	prog, err := dvi.Compile(raw)
	if err != nil {
		return fmt.Errorf("could not compile DVI program: %w", err)
	}

	err = vm.Run(prog)
	if err != nil {
		return fmt.Errorf("could not interpret DVI program: %w", err)
	}

	return nil
}