📄 00000017.htm
字号:
终端设为非规范模式,设置波特率和其他一些终端标志。注意各种PostScript打印 <BR>机的这些设置并不都一样。检查你的打印机手册确定它的设置。(数据的位数可能 <BR>是7位或8位,起始位、停止位的数目以及奇偶校验等都可能因打印机而异。) <BR>_______________________________________________________________________ <BR>_______ <BR>#include "lprps.h" <BR>#include <fcntl.h> <BR>#include <termios.h> <BR>static int block_flag = 1; /* default is blocking I/O */ <BR>void <BR>set_block(void) /* turn off nonblocking flag */ <BR>{ /* called only by block_write() below */ <BR> int val; <BR> if (block_flag == 0) { <BR> if ( (val = fcntl(psfd, F_GETFL, 0)) < 0) <BR> log_sys("set_block: fcntl F_GETFL error"); <BR> val &= ~O_NONBLOCK; <BR> if (fcntl(psfd, F_SETFL, val) < 0) <BR> log_sys("set_block: fcntl F_SETFL error"); <BR> block_flag = 1; <BR> } <BR>} <BR>void <BR>set_nonblock(void) /* set descriptor nonblocking */ <BR>{ <BR> int val; <BR> if (block_flag) { <BR> if ( (val = fcntl(psfd, F_GETFL, 0)) < 0) <BR> log_sys("set_nonblock: fcntl F_GETFL error"); <BR> val |= O_NONBLOCK; <BR> if (fcntl(psfd, F_SETFL, val) < 0) <BR> log_sys("set_nonblock: fcntl F_SETFL error"); <BR> block_flag = 0; <BR> } <BR>} <BR>void <BR>block_write(const char *buf, int n) <BR>{ <BR> set_block(); <BR> if (write(psfd, buf, n) != n) <BR> log_sys("block_write: write error"); <BR>} <BR>void <BR>tty_flush(void) /* flush (empty) tty input and output queues */ <BR>{ <BR> if (tcflush(psfd, TCIOFLUSH) < 0) <BR> log_sys("tty_flush: tcflush error"); <BR>} <BR>void <BR>tty_open(void) <BR>{ <BR> struct termios term; <BR> if ( (psfd = open(DEF_DEVICE, O_RDWR)) < 0) <BR> log_sys("tty_open: open error"); <BR> if (tcgetattr(psfd, &term) < 0) /* fetch attributes */ <BR> log_sys("tty_open: tcgetattr error"); <BR> term.c_cflag = CS8 | /* 8-bit data */ <BR> CREAD | /* enable receiv <BR>r */ <BR> CLOCAL; /* ignore modem <BR>tatus lines */ <BR> /* no pa <BR>ity, 1 stop bit */ <BR> term.c_oflag &= ~OPOST; /* turn off post processing */ <BR> term.c_iflag = IXON | IXOFF | /* Xon/Xoff flow control */ <BR> IGNBRK | /* ignore breaks <BR>*/ <BR> ISTRIP | /* strip input t <BR> 7 bits */ <BR> IGNCR; /* ignore receiv <BR>d CR */ <BR> term.c_lflag = 0; /* everything off in local flag: <BR> disables canonical mo <BR>e, disables <BR> signal generation, di <BR>ables echo */ <BR> term.c_cc[VMIN] = 1; /* 1 byte at a time, no timer */ <BR> term.c_cc[VTIME] = 0; <BR> cfsetispeed(&term, DEF_BAUD); <BR> cfsetospeed(&term, DEF_BAUD); <BR> if (tcsetattr(psfd, TCSANOW, &term) < 0) /* set attributes */ <BR> log_sys("tty_open: tcsetattr error"); <BR>} <BR>_______________________________________________________________________ <BR>_______ <BR>程序17.5 终端函数 <BR> 这个程序处理两个信号:SIGINT和SIGALRM。处理SIGINT是对BSD假脱机系统调 <BR>用的任何一种过滤器的要求。如果打印机作业被lprm(1)命令删除,那么这个信号 <BR>被发送给过滤器。我们使用SIGALRM来设置超时,对这两个信号用类似的方式处理 <BR>:我们提供了set_XXX函数来建立信号处理器,clear_XXX函数来取消这个信号处理 <BR>器。如果有信号传送给这个进程,信号处理器在设置一个全局的标记intr_flag和 <BR>alrm_flag后返回。程序的其它部分可在适当的时间来检测这些标记。有一个明显 <BR>的时间是在I/O函数返回错误EINTR时,该程序然后调用handle_intr或者handle_a <BR>lrm来处理这种情况,调用signal_intr函数(程序10.13)来中断一个慢速的系统 <BR>调用。程序17.6是处理SIGINT信号的interrupt.c文件。 <BR> 当一个中断发生时,我们必须发送PostScript的中断字符(Control-C)给打 <BR>印机,接着发送一个文件终止符(EOF)。这通常引起PostScript解释器终止它正在 <BR>解释的程序。然后我们等待从打印机返回的EOF(我们稍后将描述proc_upto_eof函 <BR>数)。我们读取最后的页码,写下记帐记录,然后就可以终止了。 <BR>_______________________________________________________________________ <BR>______ <BR>#include "lprps.h" <BR>static void <BR>sig_int(int signo) /* SIGINT handler */ <BR>{ <BR> intr_flag = 1; <BR> return; <BR>} <BR>/* This function is called after SIGINT has been delivered, <BR> * and the main loop has recognized it. (It not called as <BR> * a signal handler, set_intr() above is the handler.) */ <BR>void <BR>handle_intr(void) <BR>{ <BR> char c; <BR> intr_flag = 0; <BR> clear_intr(); /* turn signal off */ <BR> set_alrm(30); /* 30 second timeout to interrupt printer */ <BR> tty_flush(); /* discard any queued output */ <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -