modernc.org/knuth@v0.0.4/cmd/gomft/main.go (about)

     1  // Copyright 2023 The Knuth Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Command gomft is the MFT program by D. E. Knuth, transpiled to Go.
     6  //
     7  //	http://mirrors.ctan.org/systems/knuth/dist/mfware/mft.web
     8  //
     9  // For more details about the original Pascal program and its usage please see
    10  // the PDF documentation included in the modernc.org/knuth/mft package.
    11  package main // modernc.org/knuth/cmd/gomft
    12  
    13  import (
    14  	"bytes"
    15  	"flag"
    16  	"fmt"
    17  	"io"
    18  	"os"
    19  
    20  	"modernc.org/knuth"
    21  	"modernc.org/knuth/mft"
    22  )
    23  
    24  func fail(rc int, s string, args ...interface{}) {
    25  	fmt.Fprintf(os.Stderr, s, args...)
    26  	os.Exit(rc)
    27  }
    28  
    29  // program MFT( mf_file, change_file, style_file, tex_file, output);
    30  // mf_file:text_file; {primary input}
    31  // change_file:text_file; {updates}
    32  // style_file:text_file; {formatting bootstrap}
    33  // tex_file: text_file;
    34  
    35  // Main executes the mft program using the supplied arguments.
    36  func main() {
    37  	oChange := flag.String("change", "", "apply a change file change file")
    38  	oStyle := flag.String("style", "", "use a named style file instead of the default plain.mft asset")
    39  	flag.Parse()
    40  	nArg := flag.NArg()
    41  	if nArg < 1 || nArg > 2 {
    42  		fail(2, "expected 1 or 2 arguments: mf_file [tex_file]\n")
    43  	}
    44  
    45  	mfFile, err := os.Open(flag.Arg(0))
    46  	if err != nil {
    47  		fail(1, "%s\n", err)
    48  	}
    49  
    50  	defer mfFile.Close()
    51  
    52  	texFile := io.Writer(os.Stdout)
    53  	if nArg == 2 {
    54  		texFile, err := os.Create(flag.Arg(1))
    55  		if err != nil {
    56  			fail(1, "creating %s: %v\n", flag.Arg(1), err)
    57  		}
    58  
    59  		defer func() {
    60  			if err := texFile.Close(); err != nil {
    61  				fail(1, "closing %s: %v\n", flag.Arg(1), err)
    62  			}
    63  		}()
    64  
    65  	}
    66  
    67  	var changeFile, styleFile io.Reader
    68  	switch nm := *oChange; {
    69  	case nm != "":
    70  		f, err := os.Open(nm)
    71  		if err != nil {
    72  			fail(1, "change file: %v", err)
    73  		}
    74  
    75  		defer f.Close()
    76  
    77  		changeFile = f
    78  	default:
    79  		changeFile = bytes.NewBuffer(nil)
    80  	}
    81  	switch nm := *oStyle; {
    82  	case nm != "":
    83  		f, err := os.Open(nm)
    84  		if err != nil {
    85  			fail(1, "style file: %v", err)
    86  		}
    87  
    88  		defer f.Close()
    89  
    90  		styleFile = f
    91  	default:
    92  		styleFile, err = knuth.Open("TeXinputs:plain.mft", nil)
    93  		if err != nil {
    94  			fail(1, "style file: %v", err)
    95  		}
    96  	}
    97  
    98  	if err = mft.Main(mfFile, changeFile, styleFile, texFile, os.Stdout, os.Stderr); err != nil {
    99  		fail(1, "FAIL: %s\n", err)
   100  	}
   101  }