为什么我的GNAT出色的文件描述符不起作用? [英] Why isn't my GNAT's standout file descriptor working?

查看:64
本文介绍了为什么我的GNAT出色的文件描述符不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个小项目的一部分,我正在Ada中编写一个shell。这样,当我研究系统调用时,我了解到有三种方法可以实现。

As part of a little project, I'm writing a shell in Ada. As such, when I was investigating the system calls, I learned that there are three ways to do it.


  1. POSIX系统调用可能是最不可靠的。

  2. 将参数传递给C的system(),我本来不是真正想要做的,因为这是关于编写Ada和 not C中的模拟器。

  3. 使用GNAT的运行时库。

  1. The POSIX system calls, which are probably the least reliable.
  2. Passing the arguments along to C's system(), which I didn't really want to do, since this was about writing the emulator in Ada and not C.
  3. Using GNAT's runtime libraries.

我选择了最后一个选项,因为这是最像Ada一样的选择。我在RosettaCode 此处上找到了一个代码段。在将 cmd.exe更改为 ls并删除了第二个参数定义之后,我复制并粘贴并编译了它。但是,当我运行可执行文件时,什么也没有发生。外壳程序直接回到提示。我已经在两台不同的计算机上对此进行了测试,其中一台运行Fedora 21,另一台运行Debian Jessie。这是我做过的测试:

I chose to go for the last option, considering this to be the most "Ada-like" of the choices. I found a code snippet on RosettaCode here. I copied and pasted it and compiled it after changing the "cmd.exe" to "ls" and removing the second argument definition. However, nothing happens when I run the executable. The shell just goes right back to the prompt. I have tested this on two different computers, one running Fedora 21, the other Debian Jessie. Here's what I've done to test it:


  • 看是否缺少参数字符串导致了

  • 检查GNAT库中是否有任何文件描述符被错误命名

  • 将stderr和stdin都重定向到stdout只是为了查看GNAT是否将其转储到错误的FD。

  • 仔细查看了System.OS_lib库文件,似乎没有任何理由。

  • 将其谷歌搜索,但是GNAT在GCC网站上的主页非常

  • Seen if lacking an arguments string caused it
  • Checked if any of the file descriptors in GNAT's libraries are mis-named
  • Redirected both stderr and stdin to stdout just to see if GNAT was dumping them to the wrong FD anyway.
  • Looked thoroughly through the System.OS_lib library file, and there seems to be no reason.
  • Googled it, but GNAT's own page on the GCC website is very poorly documented.

目前,我在准备外壳时使用C.Interface系统,但我不满意这个。我是Ada的新手,现在只花了一个月左右的时间,所以如果这里有某种Ada的智慧可以帮助我不去做。

For now I'm using the C.Interface system in the preparation of my shell, but I'm dissatisfied with this. I'm new to Ada and have only been tinkering with it for a month or so now, so if there's some kind of Ada wisdom here that would help I'm not in on it.

更新:我尝试使用绝对路径(到/ usr / bin和/ bin位置)运行它,但是它不起作用。有趣的是,操作系统返回的结果代码为1,但我不知道这意味着什么。快速搜索表明它适用于所有常规错误,而另一个站点则表明适用于不正确的功能。

UPDATE: I have tried running it with absolute path, both to /usr/bin and /bin locations, and it doesn't work. Interestingly, the result code returned by the operating system is 1, but I don't know what that means. A quick search suggests that it's for "all general errors", and another site suggests that it's for "incorrect functions".

推荐答案

我不得不微调RosettaCode示例,以便在Debian Linux上运行 / bin / ls ,但它确实按预期运行...

I had to tweak the RosettaCode example a little to run /bin/ls on Debian Linux, but it does run as expected...

with Ada.Text_IO;     use Ada.Text_IO;
with Gnat.OS_Lib;   use Gnat.OS_Lib;

procedure Execute_Synchronously is
   Result    : Integer;
   Arguments : Argument_List :=
                 (  1=> new String'("-al")
                 );
begin
   Spawn
   (  Program_Name           => "/bin/ls",
      Args                   => Arguments,
      Output_File_Descriptor => Standout,
      Return_Code            => Result
   );
   for Index in Arguments'Range loop
      Free (Arguments (Index)); 
   end loop;
end Execute_Synchronously;

更改:


  1. 我的Gnat(Debian Jessie的FSF Gnat 4.92)警告了 System.OS_Lib ,建议使用 Gnat.OS_Lib 。 (只是将其重命名为System.OS_Lib...。为什么?

    System.OS_Lib注释:

  1. my Gnat (FSF Gnat 4.92 from Debian Jessie) warned about System.OS_Lib, recommending Gnat.OS_Lib instead. (Which simply renames System.OS_Lib .... why???
    System.OS_Lib comments:




-注意:此软件包位于系统层次结构中,因此可以直接

-由其他预定义的软件包使用。用户可以通过
访问此软件包
-在GNAT.OS_Lib中重命名此软件包(文件g-os_lib.ads)。

-- Note: this package is in the System hierarchy so that it can be directly
-- be used by other predefined packages. User access to this package is via
-- a renaming of this package in GNAT.OS_Lib (file g-os_lib.ads).




  1. 程序名称(包括路径)。

  2. 自变量。我第一次运行它时,它会显示 ls本身的详细信息,因为它具有自己的名称名称作为第一个参数,因此我删除了该名称,改为查看当前目录。

注意:


  1. 有关可用子程序及其参数的最佳信息通常在软件包规范本身的 adainclude文件夹中:这是 / usr / lib /gcc/x86_64-linux-gnu/4.9/adainclude 在我的Debian安装中,找到system.ads 会找到你的。特定文件为: s-os_lib.ads 用于 System.OS_Lib ,用于导出Spawn和Standout,以及 a-textio.ads 表示Ada.Text_IO。

  2. 突出显示不是首选方式访问Standard Output的方法:它是文件描述符(整数),首选方法是 Ada.Text_IO Standard_Output 函数>返回一个文件。但是, Spawn 似乎没有重载,它需要一个文件(我也不希望在这个低级库中有一个),因此这里使用低级文件描述符

  1. the best information ot the available subprograms and their arguments is usually in the package specs themselves in the "adainclude" folder : this is /usr/lib/gcc/x86_64-linux-gnu/4.9/adainclude on my Debian installation, locate system.ads will find yours. The specific files are: s-os_lib.ads for System.OS_Lib which exports Spawn and Standout, and a-textio.ads for Ada.Text_IO.
  2. Standout is not the preferred way of accessing Standard Output : it's a file descriptor (integer), the preferred way would be the Standard_Output function from Ada.Text_IO which returns a File. However there doesn't seem to be an overload for Spawn which takes a File (nor would I expect one in this low level library) so the lower level file descriptor is used here.

这篇关于为什么我的GNAT出色的文件描述符不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆