gpt4 book ai didi

linux - 使用 nix-shell 或 nix shell 在 Linux 64 上运行 MacOS sed

转载 作者:行者123 更新时间:2023-12-04 14:46:53 24 4
gpt4 key购买 nike

bounty结束了。回答这个问题有资格获得 +50 声望奖励。赏金宽限期将在 22 小时后结束。
potong想提请大家注意这个问题:

Help me help you a solution for this question will provide better answers for this topic.








在 SO 上回答 sed 问题我经常遇到“在 linux 上工作但在 MacOS 上不工作”的问题,是否可以将在 MacOS 上运行的 sed 版本加载到 nix shell 中?
目前使用 flakes 我可以使用以下方法获取最新的 gnus:
nix shell nixpkgs#gnused
有没有办法临时安装 BSD/MacOS 版本的 sed?

最佳答案

不久前我研究了这个问题;也许你可以利用
我发现的。
网络搜索显示对 FreeBSD 的需求很少 sed在 BSD 之外,所以我
决定从
github
并在最近的 Debian 系统上构建和测试它。为此,我创建了
3个文件(如下所列):

  • GET - 支持非 GNU 函数
  • adapt/local.c - 同上头文件
  • adapt/local.h - 下载、生成所需文件、运行 1 或 2 个
    测试套件;请参阅标题和附近目标中的评论Makefile

  • 在我的系统上,可执行文件构建没有错误。测试它使用 test我跑了
    make test | tee multi_test.log | grep '^not ok' | tee multi_test.err.log
    130 次测试中有 127 次成功,3 次失败:
    not ok 69 7.1 # Print and file routines
    not ok 75 7.7 # w results
    not ok 97 8.21 # \ in y command
    我认为,这些差异只不过是可以预期的:#69
    tests/multi_test.sh 的输出差异触发, #75 差异
    sed -n l内容(有限可移植性的测试用例),
    如果使用 /usr/share/dict/words,#97 就会消失在 makefile 中或使用 SHELL := /bin/bash而不是 printf '%s\n' 'a\b(c'在测试脚本中。
    仍然在待办事项 list 上:
  • echo 'a\b(c'Makefile变量是硬编码的,更好地创建
    它们是动态的;然而,名称似乎很少改变
    程序与 *.names发展历程
  • 支持 ATF
    测试来自 sed (当前忽略)
  • 支持在可执行文件中嵌入 ident 字符串 (tests/sed2_test.sh宏当前被忽略)而不是依赖于手册页中的日期
    version
    info
  • __FBSDID 备注 食谱在这里没有以通常的制表符为前缀
    但与 ./Makefile (在行首)充当 >运算符(operator)
    ( make )。
    # desc:
    # Download, build, test FreeBSD sed (dated 2020-06-10) on GNU/Linux
    # compat:
    # dash 0.5.10 GNU Make 4.2.1 GNU wget 1.20 GNU gcc 9.30 man 2.9
    # ref:
    # https://www.freebsd.org/cgi/man.cgi?sed
    # https://github.com/freebsd/freebsd-src/tree/main/usr.bin/sed
    # https://github.com/joshuarubin/wcwidth9
    # files:
    # Makefile adapt/local.c adapt/local.h
    # howto:
    # make download
    # make all
    # (optional) make download.tests download.regress.multitest.out test
    # (optional) cp $(exe) /usr/local/bin/bsdsed
    # (optional) cp $(man.1) /usr/local/man/man1/bsdsed.1
    # note:
    # Mind the $(wgetFlags) and $(CFLAGS)

    SHELL := /bin/sh
    wgetFlags ?= --no-verbose --wait=1
    # $(call wgetCmd,subtarget-name)
    define wgetCmd =
    wget $(wgetFlags) --no-host-directories --directory-prefix=$($1.ldir) \
    -- $(addprefix $($1.url),$($1.names))
    endef
    #
    hdrs := defs.h extern.h
    srcs := misc.c compile.c process.c main.c
    hdrx := wcwidth9.h
    srcx := local.c
    objs := $(patsubst %.c,%.o,$(srcs) $(srcx))
    # test scripts expect an executable named 'sed'
    exe := sed
    man.1 := $(exe).1
    man.ps := $(exe).ps
    binaries := $(exe) $(man.1) $(man.ps)
    #
    subtargets := wcwidth9 sed tests regress.multitest.out
    dnldtargets := $(addprefix download.,$(subtargets))
    #
    wcwidth9.url := https://github.com/joshuarubin/wcwidth9/raw/master/
    wcwidth9.names := $(hdrx)
    wcwidth9.ldir := ./
    #
    sed.url := https://github.com/freebsd/freebsd-src/raw/master/usr.bin/sed/
    sed.names := POSIX sed.1 $(srcs) $(hdrs)
    sed.ldir := ./
    tests.url := $(sed.url)tests/
    tests.names := \
    hanoi.sed inplace_race_test.sh legacy_test.sh math.sed \
    multi_test.sh regress.G.out regress.P.out regress.b2a.out \
    regress.bcb.out regress.c0.out regress.c1.out regress.c2.out \
    regress.c3.out regress.hanoi.out regress.icase1.out \
    regress.icase2.out regress.icase3.out regress.icase4.out \
    regress.in regress.math.out regress.not.out regress.psl.out \
    regress.s3.out regress.s4.out regress.s5.out regress.sg.out \
    regress.sh regress.y.out sed2_test.sh
    tests.ldir := ./tests/
    regress.multitest.out.url := $(sed.url)tests/regress.multitest.out/
    regress.multitest.out.names := \
    1.1 1.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 \
    1.14 1.15 1.16 1.17 1.18 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 \
    2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.20 2.21 \
    2.22 2.23 3.1 3.2 3.3 3.4 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 5.1 \
    5.2 5.3 5.4 5.5 5.6 5.7 5.8 6.1 6.2 6.3 6.4 6.5 6.6 7.1 7.2 \
    7.3 7.4 7.5 7.6 7.7 7.8 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 \
    8.10 8.11 8.12 8.13 8.14 8.15 8.16 8.17 8.18 8.19 8.20 8.21 \
    8.22 8.23 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 9.11 9.12 \
    9.13 9.14 9.15 9.16 9.17 9.18 9.19 9.20 9.21 9.22 9.23 9.24 \
    9.25 9.26 9.27 9.28 9.29 9.30 9.31
    regress.multitest.out.ldir := ./tests/regress.multitest.out/


    .RECIPEPREFIX = >
    .DELETE_ON_ERROR:
    .PHONY: all clean realclean
    all : $(binaries)
    clean : ; rm -f -- $(objs) $(binaries)
    realclean : clean
    > rm -f -- local.c manpage.1 $(foreach T,$(subtargets),$(addprefix $($(T).ldir),$($(T).names)))
    > rmdir --ignore-fail-on-non-empty -- \
    $(patsubst %/,%,$(foreach T,$(subtargets),$(filter-out %. %./,$($(T).ldir))))


    # notonbsd: enable modifications in extern.h and $(srcx)
    # __FBSDID: don't embed RCS ID
    $(objs) : CFLAGS += -Dnotonbsd -D__FBSDID\(s\)=
    $(objs) : $(srcs) $(hdrs)
    extern.h : adapt/local.h
    > grep -q '^.ifdef\s*notonbsd' $@ || { cat $@ $< > $@.tmp && mv -f -- $@.tmp $@ ; }
    local.c : adapt/local.c
    > cp $< $@
    local.o : $(hdrx)
    main.o : CFLAGS += -D__unreachable=__builtin_unreachable

    $(exe) : $(objs)
    > $(LINK.c) -o $@ $^
    > $(if $(DEBUG),,strip $@)

    $(man.1) : manpage.1
    > cp $< $@

    %.ps : %.1
    > man -l -t $< > $@


    .PHONY : download.all $(dnldtargets) download
    download.all : $(dnldtargets)
    $(dnldtargets) :
    > $(call wgetCmd,$(patsubst download.%,%,$@))

    download : download.wcwidth9 download.sed
    > mv -f sed.1 manpage.1
    > touch adapt/local.h


    .PHONY: test
    # ! run after: make download.tests download.regress.multitest.out
    # ! hint: make test | tee multi_test.log | grep '^not ok' | tee multi_test.err.log
    # - set PATH so scripts invoke the new sed executable
    # - run multi_test.sh (requires /usr/share/dict/words regress.multitest.out/*)
    # - optionally run inplace_race_test.sh
    # - ignore legacy_test.sh regress.* (require m4 regress.m4 hanoi.sed math.sed)
    # - ignore sed2_test.sh (requires ATF, cf. https://github.com/freebsd/freebsd-src/tree/main/contrib/atf)
    test : | /usr/share/dict/words
    > cd $(patsubst %/,%,$(tests.ldir)); \
    PATH="..:$$PATH"; \
    $(SHELL) multi_test.sh \
    $(if $(racetest),; $(SHELL) inplace_race_test.sh && rm -f file[0-9] file[0-9].prev)
    .RECIPEPREFIX = >
    /* Support for non-GNU functions, cf. man.freebsd.org */
    #ifdef notonbsd

    #include <err.h>
    #include <limits.h>
    #include <regex.h>
    #include <stdio.h>
    #include <stddef.h>
    #include <string.h>

    #include <stdarg.h>
    #include <errno.h>

    #include "defs.h"
    #include "extern.h"
    #include "wcwidth9.h" /* https://github.com/joshuarubin/wcwidth9 */

    void
    errc(int eval, int code, const char *fmt, ...)
    {
    va_list args;

    va_start(args, fmt);
    errno = code;
    err(eval, fmt, args);
    va_end(args);
    }

    char*
    getprogname()
    {
    return (program_invocation_short_name);
    }

    int
    wcwidth(wchar_t wc)
    {
    return wcwidth9(wc);
    }

    size_t
    strlcpy(char *dst, const char *src, size_t dstsize)
    {
    return snprintf(dst, dstsize, "%s", src);
    }

    size_t
    strlcat(char *dst, const char *src, size_t dstsize)
    {
    int dlen, slen, dslen, addlim;
    dlen = strlen(dst);
    slen = strlen(src);
    dslen = dlen + slen;
    addlim = (dstsize > dslen ? slen : dstsize - dlen - 1);
    if ( addlim > 0 )
    strncat(dst, src, addlim);
    return dslen;
    }

    #endif /* notonbsd */
    ./adapt/local.c
    /* To be appended to extern.h */
    #ifdef notonbsd
    extern char *program_invocation_short_name;
    char *getprogname();
    void errc(int eval, int code, const char *fmt, ...);
    int wcwidth(wchar_t wc);
    size_t strlcpy(char *dst, const char *src, size_t dstsize);
    size_t strlcat(char *dst, const char *src, size_t dstsize);
    #endif /* notonbsd */

    关于linux - 使用 nix-shell 或 nix shell 在 Linux 64 上运行 MacOS sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69863809/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com