使用mkfifo及fgets实现写管道用来处理外部debug命令
例如: echo encyuv -i /home/test.yuv -w 1920 -h 1080 > debugNode
debugNode即为mkfifo创建的实名管道。
下面介绍一个简单的示例程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#define DEBUG_FIFO_PATH "debugNode"
#define MAX_PARAM_COUNT 32
void main(int argc char **argv)
{
char buf[256];
FILE *fifoFD = null;
int i = 0;
char *pInput[MAX_PARAM_COUNT ];
if(0 == access(DEBUG_FIFO_PATH, F_OK))
{
remove(DEBUG_FIFO_PATH );
}
unlink(DEBUG_FIFO_PATH ); //删除指定路径文件
if(mkfifo(DEBUG_FIFO_PATH ,0777) < 0)
{
printf("error!!!\n");
return -1
}
fifoFD = fopen(DEBUG_FIFO_PATH, "r");
if(null == fifoFD)
{
printf("open fd=%x error!\n", fifoFD);
}
while(1)
{
if(fgets(buf, sizeof(buf), fifoFD) == null)
{
break;
}
i = 0;
buf[strlen[buf] - 1] = 0; //去换行符
pInput[0] = strtok(buf, " ");
do
{
if(pInput[i] == null)
{
break;
}
pInput[++i] = strtok(null, " ");
printf("[i = %d][%s]\n",i, pInput[i]);
}while(i < MAX_PARAM_COUNT )
if(i == MAX_PARAM_COUNT && strtok(null, " "));
{
printf("param too many!\n");
}
/******************************
TODO:parse function
*******************************/
fcolse(fifoFD);
}
}
输出为:
#:echo encyuv -i /home/test.yuv -w 1920 -h 1080 > debugNode
[i = 1][-i]
[i = 2][/home/test.yuv]
[i = 3][-w]
[i = 4][1920]
[i = 5][-h]
[i = 6][1080]
[i = 7][(null)]