4

我找到了http://support.zeus.com/zws/examples/2005/12/16/hello_world_in_perl_and_c这两个例子正在工作。

现在我为 Ada 尝试了这个,但两天后我就无法完成它。

fcgi_stdio.ads

with Interfaces.C;
with Interfaces.C.Strings;

package fcgi_stdio is
    function FCGI_Accept return Interfaces.C.int;
    pragma Import (C, FCGI_Accept, "FCGI_Accept");

    procedure FCGI_printf (str : Interfaces.C.Strings.chars_ptr);
    pragma Import (C, FCGI_printf, "FCGI_printf");
end fcgi_stdio;

测试.adb

with fcgi_stdio;
with Interfaces.C;
with Interfaces.C.Strings;

procedure Test is
begin
    while Integer (fcgi_stdio.FCGI_Accept) >= 0 loop
        fcgi_stdio.FCGI_printf (Interfaces.C.Strings.New_String ("Content-Type: text/plain" & ASCII.LF & ASCII.LF));
        fcgi_stdio.FCGI_printf (Interfaces.C.Strings.New_String ("Hello World from Ada!" & ASCII.LF));
    end loop;
end Test;

当我在控制台中运行它时,我收到以下错误:

$ ./test
raised STORAGE_ERROR : stack overflow or erroneous memory access

Apache error_log 显示:

Premature end of script headers: test

有谁知道我怎样才能让它工作?

4

1 回答 1

7

在 Mac OS X 上进行实验,似乎问题在于FCGI_printf()varargs 函数。它调用FCGI_fprintf(),也是可变参数:

int FCGI_fprintf(FCGI_FILE *fp, const char *format, ...)
{
    va_list ap;
    int n = 0;
    va_start(ap, format);          <------ crash here

Ada 没有指定可变参数函数的标准方法,GNAT 也没有实现定义的方法。

GNAT文档说解决方案是为 varargs 函数提供一个 C 包装器:

#include <fcgi_stdio.h>
int FCGI_printf_wrapper(const char *msg)
{
  return FCGI_printf(msg);
}

并导入包装器:

procedure FCGI_printf (str : Interfaces.C.Strings.chars_ptr);
pragma Import (C, FCGI_printf, "FCGI_printf_wrapper");

该程序的另一个问题是,在 Ada 中,与 C 和许多其他语言不同,"\n"它不是一种在字符串中插入换行符的方法。尝试

fcgi_stdio.FCGI_printf
  (Interfaces.C.Strings.New_String ("Content-Type: text/plain" 
                                    & ASCII.LF & ASCII.LF));

[13.1.13 编辑]

于 2013-01-12T23:26:30.043 回答