如何从C函数创建Shell命令 [英] How create shell commands from C functions

查看:77
本文介绍了如何从C函数创建Shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道如何使用一组C函数并将它们转换为shell / bash命令。

Wondering how to take a set of C functions and turn them into shell/bash commands.

所以说我有一组简单的C函数

So say I have a simple set of C functions

int
fn1() {
  // some C code for function 1.
}

int
fn2() {
  // some C code for function 2.
}

int
fn3() {
  // some C code for function 3.
}

我想然后以某种方式创建CLI命令,以便可以在终端上使用它们。

I would like to then somehow create CLI commands so I can use them from the terminal.

$ fn1 <param> <param> ...
$ fn2 ...
$ fn3 ...

不确定执行此操作的过程。如果我需要以某种方式重写shell脚本中的所有函数接口,然后以某种方式调用C函数,则类似于(bash脚本):

Not sure what the process is of doing this. If I need to somehow rewrite all the function interfaces in shell script, and then call of to a C function in some way, sort of like this (bash script):

fn1() {
  callc mylib/fn1 $1 $2
}

fn2() {
  ...
}

...

或者如果我能以某种方式转换它们通过将C函数自动分离为单个文件 fn1.c fn2.c 等自动将其转换为Shell脚本,以及以某种方式使用 source〜/ .bash_profile 类型的东西将它们加载到外壳中。

Or if I can just somehow convert each C function into a shell script automatically by separating them into individual files fn1.c, fn2.c, etc. and somehow loading them into the shell with source ~/.bash_profile type of thing.

任何帮助,谢谢。

推荐答案

还是老套了,也许:编写C代码检查其调用方式(原始代码的第0个arg)命令行参数),然后根据该名称调用正确的C函数。需要将这样的C程序编译为单个可执行文件,然后创建指向基本应用程序的符号链接,其中符号链接是所需功能的名称。除了在此处将工件(可执行文件和符号链接)安装到$ PATH中的目录外,无需任何外壳程序代码。

Or go old-school, perhaps: write C code to examine how it was invoked (0th arg from original command-line arguments) and invoke the right C function based on that name. Requires compiling such a C program to a single executable, then creating symbolic links to the base application where the symlinks are the names of the functions of interest. No shell code needed beyond installing the artifacts here - executable and symlinks - into a directory in your $PATH.

示例。如果以下代码名为toybox.c,并且〜/ bin存在并且在用户的$ PATH中,请使用类似以下内容的代码:

Example. If the following code is name toybox.c, and ~/bin exists and is in the user's $PATH, use something like:

$ cc -o ~/bin/toybox toybox.c
$ ln -s toybox ~/bin/fn1
$ ln -s toybox ~/bin/fn2
$ ln -s toybox ~/bin/fn3

简单测试-仅显示支架在适当位置。

Simple tests - only shows that the scaffolding is in place.

$ fn1
fn1 invoked - no arguments.
$ fn3 1 2 'a b c'
fn3 invoked - arguments:
  1 - '1'
  2 - '2'
  3 - 'a b c'

toybox.c的来源可能如下:

The source for toybox.c might look like:

#include <string.h>
#include <libgen.h>
#include <stdio.h>

struct name2func {
    const char *name;
    int (*func)(int ac, char *const av[]);
};

void
fn_debug(const char *fn, int ac, char *const av[])
{
    int n;

    printf("%s invoked - ", fn);

    if (ac <= 0) {
        printf("no arguments.\n");
    } else {
        printf("arguments:\n");
        for (n = 0; n < ac; n++) {
            printf("  %d - '%s'\n", n + 1, av[n]);
        }
    }
}

int
fn1(int ac, char *const av[])
{
    fn_debug("fn1", ac, av);
    /* some C code for function 1. */
    return 0;
}

int
fn2(int ac, char *const av[])
{
    fn_debug("fn2", ac, av);
    /* some C code for function 2. */
    return 0;
}

int
fn3(int ac, char *const av[])
{
    fn_debug("fn3", ac, av);
    /* some C code for function 3. */
    return 0;
}

/*
 * Establish a crude symbol table after function definitions: size of
 * the name2func array (i.e., its number of elements) is available via the
 * sizeof builtin.
 */

struct name2func n2f[] = {
  { "fn1", fn1 },
  { "fn2", fn2 },
  { "fn3", fn3 }
};

int
dispatch(const char *func_name, int ac, char *const av[])
{
    size_t n;

    /* linear search ok for small # of funcs */

    for (n = 0; n < sizeof n2f / sizeof n2f[0]; n++) {
        if (strcmp(func_name, n2f[n].name) == 0) {
            return (*n2f[n].func)(ac, av);
        }
    }

    fprintf(stderr, "%s: unsupported\n", func_name);
    return 1;
}

int
main(int argc, char *const argv[])
{
    /*
     * using POSIX basename(3) to create, say, "fn1" from
     * a full-path invocation like "/my/odd/dir/fn1".
     */
    char *fnbase = basename(argv[0]);

    if (fnbase == 0) {
        perror("basename");
        return 1;
    }

    return dispatch(fnbase, argc - 1, argv + 1);
}

这篇关于如何从C函数创建Shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆