modernc.org/99c@v1.0.1-0.20181109153923-a9e8197063d9/99run/main.go (about)

     1  // Copyright 2017 The 99c 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 99run executes binary programs produced by the 99c compiler.
     6  //
     7  // Usage
     8  //
     9  // To execute a compiled binary named a.out
    10  //
    11  //	99run a.out [arguments]
    12  //
    13  // Installation
    14  //
    15  // To install or update
    16  //
    17  //      $ go get [-u] modernc.org/99c/99run
    18  //
    19  // Online documentation: [godoc.org/modernc.org/99c/99run](http://godoc.org/modernc.org/99c/99run)
    20  //
    21  // Changelog
    22  //
    23  // 2017-01-07: Initial public release.
    24  package main
    25  
    26  import (
    27  	"bufio"
    28  	"fmt"
    29  	"os"
    30  
    31  	"modernc.org/virtual"
    32  )
    33  
    34  func exit(code int, msg string, arg ...interface{}) {
    35  	if msg != "" {
    36  		fmt.Fprintf(os.Stderr, os.Args[0]+": "+msg, arg...)
    37  	}
    38  	os.Exit(code)
    39  }
    40  
    41  func main() {
    42  	if len(os.Args) < 2 {
    43  		exit(2, "invalid arguments %v\n", os.Args)
    44  	}
    45  
    46  	bin, err := os.Open(os.Args[1])
    47  	if err != nil {
    48  		exit(1, "%v\n", err)
    49  	}
    50  
    51  	var b virtual.Binary
    52  	if _, err := b.ReadFrom(bufio.NewReader(bin)); err != nil {
    53  		exit(1, "%v\n", err)
    54  	}
    55  
    56  	code, err := virtual.Exec(&b, os.Args[1:], os.Stdin, os.Stdout, os.Stderr, 0, 8<<20, "")
    57  	if err != nil {
    58  		if code == 0 {
    59  			code = 1
    60  		}
    61  		exit(code, "%v\n", err)
    62  	}
    63  
    64  	exit(code, "")
    65  }