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



15
16
17
18
# File 'lib/seccomp-tools/cli/asm.rb', line 15

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

Instance Method Details

#handlevoid

This method returns an undefined value.

Handle options.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/seccomp-tools/cli/asm.rb', line 41

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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/seccomp-tools/cli/asm.rb', line 22

def parser
  @parser ||= OptionParser.new do |opt|
    opt.banner = usage
    opt.on('-o', '--output FILE', 'Output result into 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