Class: SeccompTools::CLI::Asm

Inherits:
Base
  • Object
show all
Defined in:
lib/seccomp-tools/cli/asm.rb

Overview

Handle 'asm' command.

Constant Summary collapse

SUMMARY =

Summary of this command.

'Seccomp bpf assembler.'
USAGE =

Usage of this command.

"asm - #{SUMMARY}\n\nUsage: seccomp-tools asm IN_FILE [options]".freeze

Instance Attribute Summary

Attributes inherited from Base

#argv, #option

Instance Method Summary collapse

Constructor Details

#initializeAsm

Instantiate an SeccompTools::CLI::Asm object, defaulting the output format to :inspect.

Takes the same arguments as Base#initialize.



18
19
20
21
# File 'lib/seccomp-tools/cli/asm.rb', line 18

def initialize(*)
  super
  option[:format] = :inspect
end

Instance Method Details

#handlevoid

This method returns an undefined value.

Assembles the input file and writes the result in the requested format.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/seccomp-tools/cli/asm.rb', line 45

def handle
  return unless super

  option[:ifile] = argv.shift
  return CLI.show(parser.help) if option[:ifile].nil?

  res = SeccompTools::Asm.asm(input, filename: option[:ifile], arch: option[:arch])
  output do
    case option[:format]
    when :inspect then "#{res.inspect}\n"
    when :raw then res
    when :c_array, :carray then "unsigned char bpf[] = {#{res.bytes.join(',')}};\n"
    when :c_source then SeccompTools::Util.template('asm.c').sub('<TO_BE_REPLACED>', res.bytes.join(','))
    when :assembly
      SeccompTools::Util.template("asm.#{option[:arch]}.asm").sub(
        '<TO_BE_REPLACED>',
        res.bytes.map { |b| format('\\\%03o', b) }.join
      )
    end
  end
end

#parserOptionParser

Define option parser.

Returns:

  • (OptionParser)

    The parser of this command's options.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/seccomp-tools/cli/asm.rb', line 26

def parser
  @parser ||= OptionParser.new do |opt|
    opt.banner = usage
    opt.on('-o', '--output FILE', 'Write output to FILE instead of stdout.') do |o|
      option[:ofile] = o
    end

    opt.on('-f', '--format FORMAT', %i[inspect raw c_array carray c_source assembly],
           'Output format. FORMAT can only be one of <inspect|raw|c_array|c_source|assembly>.',
           'Default: inspect') do |f|
             option[:format] = f
           end

    option_arch(opt)
  end
end