首页 > 解决方案 > 如何防止Expect输出CRLF?

问题描述

它来自操作系统分配,为 Xv6 添加系统调用(https://github.com/remzi-arpacidusseau/ostep-projects,initial-xv6)。我确保我的代码正确并设置了环境,但就是无法通过测试。跑步diff回报

1c1
< XV6_TEST_OUTPUT 200000
---
> XV6_TEST_OUTPUT 200000

并且od我发现答案应该\n用作换行符,而在我的输出中,它是\r\n.

我花了相当长的时间来明确这\r不是 Xv6 中的 printf 或 qemu 引入的。测试是使用 Expect 脚本完成的,这就是我认为可能有问题的地方。

我发现了一个相关的问题,但似乎没有给出可行的解决方案: \r 来自哪里?

这个问题似乎并不常见,我的大多数同学都能通过测试。所以我想知道如何防止\r输出?

期望脚本:

#! /usr/bin/env expect

proc shutdown {} {
    # send command to halt qemu (ctrl-a x)
    # https://stackoverflow.com/questions/27050473/how-to-send-ctrl-a-then-d-in-expect
    send "\x01"; send "x"
    # make sure to wait for it all to stop
    # (without this, script was terminating before qemu quit -> bad)
    expect eof
}

# turn off timeout (perhaps make this flexible later)
set timeout -1

# build and launch xv6 on qemu
spawn make [lindex $argv 0] -f [lindex $argv 1] qemu-nox

trap {
    shutdown
    exit 0
} SIGINT

# wait for initial prompt
expect "init: starting sh\r"
expect "$ "

# send command as per command line
send "[lindex $argv 2]\r"

# wait for it to be done
expect "$ "

# shutdown qemu properly (avoid runaways)
shutdown

运行测试的命令:

cd src; ../../tester/run-xv6-command.exp CPUS=1 Makefile.test test_1 | grep XV6_TEST_OUTPUT; cd ..

测试1.c

#include "types.h"
#include "stat.h"
#include "user.h"

int
main(int argc, char *argv[]) {
  int x1 = getreadcount();
  int x2 = getreadcount();
  char buf[100];
  (void) read(4, buf, 1);
  int x3 = getreadcount();
  int i;
  for (i = 0; i < 1000; i++) {
    (void) read(4, buf, 1);
  }
  int x4 = getreadcount();
  printf(1, "XV6_TEST_OUTPUT %d %d %d\n", x2-x1, x3-x2, x4-x3);
  exit();
}

标签: bashexpectline-breakscarriage-returnxv6

解决方案


推荐阅读