在 OS X 10.5 上使用 readline 的 rl_insert_text

发布于 2024-07-23 06:22:23 字数 1089 浏览 7 评论 0原文

因此,我尝试使用 readline 将一些默认文本填充到用户输入中,但无法使其在 OSX 10.5 上工作:

// rl_insert_text_ex.c
// gcc -o rl_insert_text_ex rl_insert_text_ex.c -lreadline
#include <stdio.h>
#include <readline/readline.h>

int my_startup_hook(void) {
  return rl_insert_text("ponycorns");
}
int main(int argc, char *argv[]) {
  char *line;
  rl_startup_hook = (Function*) my_startup_hook;
  line = readline("What's your favorite mythical animal? ");
  if (NULL == line || '\0' == *line) {
    printf("Nothing given... :(\n");
  }
  else {
    printf("That's funny, I love %s too!\n", line);
  }
  return 0;
}

此代码甚至无法在 10.4 上编译(没有定义 _rl_insert_text在 10.4 上,这有点令人失望),但可以在 10.5 上编译。 但是,rl_insert_text() 生成的文本永远不会显示在屏幕上,也不会作为用户输入返回。 正在使用回调,并且 rl_insert_text() 返回正确的值(谢谢 printf),所以我不确定这里发生了什么。

我检查了 /usr/include/readline/readline.h,并且 rl_insert_text() 位于:

/* supported functions */

下,这令人困惑:

/*
 * The following is not implemented
 */

所以我是 SOL,还是我只是这样做错误的?

So, I'm trying to stuff some default text into a user input using readline, and having trouble getting it to work on OSX 10.5:

// rl_insert_text_ex.c
// gcc -o rl_insert_text_ex rl_insert_text_ex.c -lreadline
#include <stdio.h>
#include <readline/readline.h>

int my_startup_hook(void) {
  return rl_insert_text("ponycorns");
}
int main(int argc, char *argv[]) {
  char *line;
  rl_startup_hook = (Function*) my_startup_hook;
  line = readline("What's your favorite mythical animal? ");
  if (NULL == line || '\0' == *line) {
    printf("Nothing given... :(\n");
  }
  else {
    printf("That's funny, I love %s too!\n", line);
  }
  return 0;
}

This code doesn't even compile on 10.4 (no definition for _rl_insert_text on 10.4, which is a bit of a bummer), but does compile on 10.5. However, the rl_insert_text()'d text is never shown to screen, nor returned as user input. The callback is being used and rl_insert_text() returns the proper value, (thank you, printf), so I'm not sure what's going on here.

I checked /usr/include/readline/readline.h, and rl_insert_text() is under:

/* supported functions */

which is confusingly under:

/*
 * The following is not implemented
 */

So am I SOL, or am I just doing it wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2024-07-30 06:22:23

不幸的是,你可能会运气不好,至少 OS X 中包含的 readline 库是这样。由于许可证兼容性问题,Apple 使用 libedit,它(显然)提供了不完整的 readline 模拟。 (此库在 OS X 附带的 readline.h 中以“editline”名称进行记录。)

GNU Readline 库(“一个真正的”readline 库)遵循 GPL,它(作为 Copyleft 许可证)不能很好地与非完全开源的代码兼容。 如果归结为 (A) 开源所有 Xcode、OS X 等,或者 (B) 使用你真正喜欢使用的仿制品,Apple(像大多数公司一样)总是会选择B. 很遗憾,但这就是生活。

就我个人而言,我认为这是 GPL 代码在某种程度上成为这片土地上的祸害的原因之一,因为在“将其粘在人身上”的行为中,它通常也会向购买软件的大众扣留代码。 {BSD,MIT,Apache} 风格的许可证更有利于在闭源系统中使用,并且仍然允许商业实体贡献补丁等。我的猜测是 libedit 还没有得到足够的重视并得到妥善修复。 社区补丁肯定会受到欢迎,尽管如果我们可以使用代码而不需要自己破解它会更好......;-)

顺便说一句,同样的事情也适用于其他 GPL 项目 - 只要 {git, Mercurial,bazaar} 仍受 GPL 约束,请不要等待 Apple 在 Xcode 中集成它们。 :-(

更新: 新的 Xcode 4 提供了 git 支持。好极了!我的理解是,这是由于新的插件架构将 GPL 代码与主 Xcode 代码库隔离开来。然而,我强调,对于应该让所有人受益的代码来说,copyleft 许可证仍然是错误的解决方案,显然有些人不同意(你是一个匿名的反对者),但事实是 GPL 也可以限制自由 — 通常它与闭源/专有软件通常不同,但 GPL 是在防止非法使用源代码方面也非常有效......区别在于道德优越感。

Unfortunately, you may be out of luck, at least with the readline library included in OS X. Due to license compatibility issues, Apple uses libedit, which (apparently) provides incomplete readline emulation. (This library is documented with the name "editline" in the readline.h included with OS X.)

GNU Readline Library (the "one true" readline library) is under GPL, which (being a copyleft license) does not play well with code that is not entirely open-source. If it comes down to (A) open-sourcing all of Xcode, OS X, etc. or (B) using a knock-off of what you're really like to use, Apple (like most companies) is always going to choose B. It's a bummer, but that's life.

Personally, I think this is one reason that GPL'd code is somewhat of a blight on the land, since in the act of "sticking it to the man", it often also withholds the code from the masses who purchase software. The {BSD,MIT,Apache}-style licenses are much more conducive to use in closed-source systems, and still allow commercial entities to contribute back patches, etc. My guess is that libedit hasn't received enough attention to be fixed properly. Community patches would certainly be welcome, although it's so much nicer if we can use code without having to hack on it ourselves... ;-)

BTW, the same thing applies to other GPL projects — as long as {git,mercurial,bazaar} remains under GPL, don't hold your breath for Apple to ship integration for them in Xcode. :-(

UPDATE: The new Xcode 4 offers git support. Huzzah! My understanding is that this is due to the new plugin architecture which isolates GPL'd code from the main Xcode codebase. However, I emphasize that copyleft licenses are still the wrong solution for code that should benefit everyone. Obviously some people don't agree (you're a pal, anonymous downvoter) but the fact is that GPL can restrict freedoms too — usually its different ones than closed-source/proprietary software generally does, but GPL is also quite effective at preventing illegal use of source code... The difference is a feeling of moral superiority.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文