自己写函数
#include <stdio.h>#include <termios.h>
static struct termios initial_settings, new_settings
static int peek_character = -1
void init_keyboard(void)
void close_keyboard(void)
int kbhit(void)
int readch(void)
void init_keyboard()
{
tcgetattr(0,&initial_settings)
new_settings = initial_settings
new_settings.c_lflag |= ICANON
new_settings.c_lflag |= ECHO
new_settings.c_lflag |= ISIG
new_settings.c_cc[VMIN] = 1
new_settings.c_cc[VTIME] = 0
tcsetattr(0, TCSANOW, &new_settings)
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings)
}
int kbhit()
{
unsigned char ch
int nread
if (peek_character != -1) return 1
new_settings.c_cc[VMIN]=0
tcsetattr(0, TCSANOW, &new_settings)
nread = read(0,&ch,1)
new_settings.c_cc[VMIN]=1
tcsetattr(0, TCSANOW, &new_settings)
if(nread == 1)
{
peek_character = ch
return 1
}
return 0
}
int readch()
{
char ch
if(peek_character != -1)
{
ch = peek_character
peek_character = -1
return ch
}
read(0,&ch,1)
return ch
}
int main()
{
init_keyboard()
while(1)
{
kbhit()
printf("\n%d\n", readch())
}
close_keyboard()
return 0
}
/* 一组宏定义,不过此函数没有用 */#define ESIZE 1024
#define INIT register unsigned char *sp=instring
#define GETC() (*sp++)
#define PEEKC() (*sp)
#define UNGETC(c) (--sp)
#define RETURN(c) return c
#define ERROR(c) { regerr(c)return(NULL)}
#include "stdio.h"
#include "string.h"
/* 这是什么头文件?? */
#include "regexp.h"
/* 此文件内定义的全局变量 */
int i
unsigned char *nextpos
static unsigned char lbuf[512], ebuf[ESIZE]
FILE *fp
/* 函数声明 */
int regerr()
/* 可接受命令行参数 */
int main(int argc,char *argv[])
{
if(argc <3) /* 输入的参数不足三个,出错 */
{
/* 向标准出错设备输出信息 */
fprintf(stderr,"Use: %s regular_expr files ..\n",argv[0])
exit(-1)/* 退出程序 */
}
/* 没见过compile函数 */
nextpos = compile(argv[1], ebuf, &ebuf[ESIZE],'\0')
if(nextpos)
for(i=2i<argci++)
{
if((fp=fopen(argv[i],"rb"))==NULL)
printf("%s: read failure.\n",argv[i])
else
/* 没见过step函数 */
while(fgets(lbuf,sizeof(lbuf),fp))
if(step(lbuf,ebuf)) printf("%s: %s",argv[i],lbuf)
fclose(fp)/* 关闭文件 */
}
}
/* 向标准出错设备输出信息,即屏幕 */
int regerr(int c) /* 函数不完整,返回值没用 */
{
fprintf(stderr,"Error %d.\n", c)
}
grep的功能
grep从一个或多个文本文件中查找符合指定范本(正则表达式)的行,并把查找结果输出到指定设备(默认到屏幕)。
正则表达式?还要模式匹配?有点小复杂啊。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)