메뉴 건너뛰기

NEOS 예제 코드

주의 : 본 기능은 NEOS V5에만 적용됩니다.

 

NEOS 쉘 명령어 추가시 아규먼트와 함께 옵션을 추가하여 명령어를 구현하는 예제이다.

 

#include <sys/types.h>
#include <sys/param.h>
#include <sys/shell.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
void ShellSample3Cmd(int argn, char *argv[]);
void ShellSample3CmdUsage(void);
 
static ShellCmd TestCommand = \
    SHELL_CMD_ENTRY("test3", ShellSample3Cmd, "test3 [- amsd?] arg1 arg2","test command: four arithmetic operation");

 
void ShellAddUserCommand(void)
{
    int ix;
 
    /* attach test commands */
    ShellAddGroup("test");
 
    ShellAddCmd("test", &TestCommand);
  
}
 
/**
 * ShellSample3Cmd
 *
 * DESCRIPTION
 * This function will show how to use options in shell command.
 * In this example, we made the four fundamental arithmetic operations.
 *
 * argv[0] is command.
 * argv[1] is option, which is operator.
 * argv[2], argv[3] is argument of arithmetic operation.
 * For example, with this example,
 *   in command line,
 *   -> test3 -a 1 2
 * means "1 + 2" operation.
 *
 * PARAMETERS
 *  argn - [in]  Number of argument
 *  argv - [in]  Command line array
 *
 */
 
void
ShellSample3Cmd(int argn, char *argv[])
{
    float a1, a2, res;
    getopt_d_t  optd;
    UInt32 ch;
    char op ;
 
    if (argn < 4)
    {
        ShellSample3CmdUsage();
        return;
    }
 
    a1 = (float)strtoul(argv[2], NULL, 10);
    a2 = (float)strtoul(argv[3], NULL, 10);
 
    /* Parse options */
    getopt_init(&optd);
    while ((ch = getopt_r(argn, argv, "amsd?", &optd)) != ERROR)
    {
 
        switch (ch)
        {
        case 'a': //addition
            res = a1 + a2;
            op = '+';
            break;
        case 'm': //multiplication
            res = a1 * a2;
            op = 'x';
            break;
        case 's': //subtraction
            res = a1 - a2;
            op = '-';
            break;
        case 'd': //division
            if(a2)
            {
                res = a1/a2;
                op = '/';
                break;
            }
            else
            {
                printk("Cannot divide by 0.\n\r");
                return;
            }
        case '?':
            Shellample3CmdUsage();
            break;
        default:
            ShellSample3CmdUsage();
            return;
        }
    }
 
    printf("Sample Command 3: the four fundamental arithmetic operation.\n\r");
    printf(" argument 1: %.2f\n\r", a1);
    printf(" argument 2: %.2f\n\r", a2);
    printf(" %.2f %c %.2f is %.2f\n\r", a1, op, a2, res);
}
 
 
/**
 * ShellSample3CmdUsage - print out the usage
 *
 * DESCRIPTION
 *  This function prints out the usage.
 *
 */
 
void
ShellSample3CmdUsage(void)
{
    printf("usage: test3 [- amsd?] arg1 arg2\n");
}
 
위로