使用 Ctrl+C 中的 SIGINT

发布于 2024-10-02 20:08:24 字数 559 浏览 13 评论 0原文

好吧,所以我使用叹息控制器来解释一些信号,为此目的它是 Ctrl+C,所以当 Ctrl+C 将采取一些操作,一切都很好,但我真正需要的是在输入/输出中不出现 ^C 的情况下发生这种情况,

例如让我们假设我有这段代码,

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void siginthandler(int param)
{
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}

输出将是

^CUser Pressed Ctrl+C

我怎样才能让它简单地是

User Pressed Ctrl+C

alright, so i'm using a sighandler to interpret some signal, for this purpose it is Ctrl+C, so when Ctrl+C is typed some action will be taken, and everything is fine and dandy, but what I really need is for this to happen without ^C appearing in the input/output

for example let's say i have this code

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void siginthandler(int param)
{
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}

the output would be

^CUser pressed Ctrl+C

How could I get this to simply be

User pressed Ctrl+C?

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

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

发布评论

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

评论(3

云归处 2024-10-09 20:08:24

您可以使用curses库的“noecho()”调用来实现这种效果——但您也必须愿意接受(和/或使用)curses终端处理...

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <curses.h>

void siginthandler(int param)
{
  endwin();
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  initscr();
      noecho();
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}

You can achieve this effect using the curses library's "noecho()" call -- but you must be willing to accept (and/or use) curses terminal handling too ...

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <curses.h>

void siginthandler(int param)
{
  endwin();
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  initscr();
      noecho();
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}
嗫嚅 2024-10-09 20:08:24

那很可能不是你的程序这样做的。它可能是终端驱动程序,在这种情况下,您应该使用 ctlechoechoctl 选项查看 stty

您可以使用 stty -a 获取终端设备的所有当前设置。对于这些项目的编程接口,termios 就是这样去。

That is most likely not your program doing that. It's likely to be the terminal drivers, in which case you should look into stty with the ctlecho or echoctl options.

You can use stty -a to get all the current settings for your terminal device. For a programmatic interface to these items, termios is the way to go.

我喜欢麦丽素 2024-10-09 20:08:24

正如其他人所说,这种事情由终端驱动程序控制,通常是使用curses库完成的。也就是说,这里有一个可能可以帮助您上路的技巧:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void siginthandler(int param) {
    printf("User pressed Ctrl+C\n");
    system("stty echo");
    exit(1);
}

int main() {
    system("stty -echo");
    signal(SIGINT, siginthandler);
    while(1);
    return 0;
}

As other have said, this kind of thing falls under the control of your terminal driver and is typically done using the curses library. That said, here is a hack that will probably get you on your way:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void siginthandler(int param) {
    printf("User pressed Ctrl+C\n");
    system("stty echo");
    exit(1);
}

int main() {
    system("stty -echo");
    signal(SIGINT, siginthandler);
    while(1);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文