埃菲尔:如何进行系统调用?

时间:2019-07-22 23:38:56

标签: eiffel

我想用bzip2 myFile.bz2解压缩文件,我必须为此使用哪个类?

我试图在base kernel documentation中找到它,这对我来说最有意义,却没有找到

2 个答案:

答案 0 :(得分:1)

我在带有管道的linux上使用的最终解决方案如下:

feature -- Commands

    piped_command_result (a_command: STRING): detachable PROCESS_COMMAND_RESULT
            -- https://groups.google.com/forum/#!topic/eiffel-users/O9KEtBSPrf4
        local
            l_cmd: READABLE_STRING_32
            l_args: ARRAYED_LIST [READABLE_STRING_32]
            l_proc: like {BASE_PROCESS_FACTORY}.process_launcher
            l_err, l_res: STRING
            l_err_spec, l_res_spec: SPECIAL [NATURAL_8]
        do
            create l_res.make (0)
            create l_err.make (0)
            create l_res_spec.make_filled (0, 1024)
            create l_err_spec.make_filled (0, 1024)

            l_cmd := (create {EXECUTION_ENVIRONMENT}).default_shell
            if l_cmd.is_empty then
            l_cmd := {STRING_32} "/bin/bash" -- or either "/bin/sh"
            end
            create l_args.make (2)
            l_args.extend ("-c")
            l_args.extend (a_command)

            l_proc := (create {BASE_PROCESS_FACTORY}).process_launcher (l_cmd, l_args, Void)
            l_proc.set_hidden (True)
            l_proc.set_separate_console (False)
            l_proc.redirect_output_to_stream
            l_proc.redirect_error_to_stream

            l_proc.launch
            check
                process_launched: l_proc.launched
            then
                -- read output
                from
                until
                    l_proc.has_output_stream_closed or l_proc.has_output_stream_error
                loop
                    l_proc.read_output_to_special (l_res_spec)
                    append_special_of_natural_8_to_string_8 (l_res_spec, l_res)
                end
                -- read error
                from
                until
                    l_proc.has_error_stream_closed or l_proc.has_error_stream_error
                loop
                    l_proc.read_error_to_special (l_err_spec)
                    append_special_of_natural_8_to_string_8 (l_err_spec, l_err)
                end

                l_proc.wait_for_exit
                create Result.make (l_proc.exit_code, l_res, l_err)
            end
        ensure
            instance_free: Class
        end

feature {NONE} -- Implementation

    append_special_of_natural_8_to_string_8 (spec: SPECIAL [NATURAL_8]; a_output: STRING)
        local
            i,n: INTEGER
        do
            from
                i := spec.lower
                n := spec.upper
            until
                i > n
            loop
                a_output.append_code (spec[i])
                i := i + 1
            end
        ensure
            instance_free: Class
        end

Credits to google groups eiffel users

请注意,对于其他用户(例如由apache启动的应用),您必须检查环境变量和$PATH才能正常工作!

答案 1 :(得分:0)

这有效:

    make
        local
            l_env:EXECUTION_ENVIRONMENT
        do
            create l_env
            l_env.system ("bzip2 test.txt")
        end