github.com/core-coin/go-core/v2@v2.1.9/cmd/cvm/disasm.go (about)

     1  // Copyright 2023 by the Authors
     2  // This file is part of go-core.
     3  //
     4  // go-core is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-core is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-core. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"strings"
    24  
    25  	"gopkg.in/urfave/cli.v1"
    26  
    27  	"github.com/core-coin/go-core/v2/core/asm"
    28  )
    29  
    30  var disasmCommand = cli.Command{
    31  	Action:    disasmCmd,
    32  	Name:      "disasm",
    33  	Usage:     "disassembles cvm binary",
    34  	ArgsUsage: "<file>",
    35  }
    36  
    37  func disasmCmd(ctx *cli.Context) error {
    38  	var in string
    39  	switch {
    40  	case len(ctx.Args().First()) > 0:
    41  		fn := ctx.Args().First()
    42  		input, err := ioutil.ReadFile(fn)
    43  		if err != nil {
    44  			return err
    45  		}
    46  		in = string(input)
    47  	case ctx.GlobalIsSet(InputFlag.Name):
    48  		in = ctx.GlobalString(InputFlag.Name)
    49  	default:
    50  		return errors.New("Missing filename or --input value")
    51  	}
    52  
    53  	code := strings.TrimSpace(in)
    54  	fmt.Printf("%v\n", code)
    55  	return asm.PrintDisassembled(code)
    56  }