⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ifdef.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
 * mesg[]. The string in mesg[] is terminated with a '\0' and TRUE is returned to * the caller when we find a newline, EOF, or reach the end of the mesg[] array. * If nothing is available on ttyi we return FALSE if a single process is being * used for reads and writes, while in the two process implementation we force a * one character read. Interactive mode loops here forever, except during start(), * echoing everything that comes back on ttyi to stdout. The performance of a * simple getc/putc loop for interactive mode was unacceptable when run under mux * and has been replaced by more complicated code. When layers wasn't involved * the getc/putc loop worked well. * */    if ( interactive == FALSE )  {	while ( 1 )  {	    while ( nptr < eptr )  {	/* grab characters from tbuf */		*ptr = *nptr++;		if ( *ptr == '\r' ) continue;		if ( *ptr == '\n' || *ptr == '\004' || ptr >= endmesg )  {		    *(ptr+1) = '\0';		    if ( *ptr == '\004' )			strcpy(ptr, "%%[ status: endofjob ]%%\n");		    ptr = mesg;		    return(TRUE);		}   /* End if */		++ptr;	    }	/* End for */	    nptr = eptr = tbuf;	    if ( ioctl(ttyi, FIONREAD, &n) < 0 )		if ( errno == EINTR )		    continue;		else error(FATAL, "ioctl error - FIONREAD");	    if ( n <= 0 )		if ( canwrite == TRUE )		    return(FALSE);	    n = ((n < 1) ? 1 : ((n < sizeof(tbuf)) ? n : sizeof(tbuf)));	    if ( (n = read(ttyi, tbuf, n)) < 0 )		if ( errno == EINTR )		    continue;		else error(FATAL, "error reading line %s", line);	    else eptr = nptr + n;	}   /* End while */    }	/* End if */    if ( canwrite == TRUE )		/* don't block during start() */	return(FALSE);    while ( 1 )  {			/* only interactive mode gets here */	if ( ioctl(ttyi, FIONREAD, &n) < 0 )	    error(FATAL, "ioctl error - FIONREAD");	n = ((n < 1) ? 1 : ((n < sizeof(tbuf)) ? n : sizeof(tbuf)));	if ( (n = read(ttyi, tbuf, n)) < 0 )	    error(FATAL, "error reading line %s", line);	else if ( n == 0 )		/* should not happen */	    error(FATAL, "end of file in interactive mode");	if ( write(1, tbuf, n) != n )	    error(FATAL, "error writing to stdout");    }	/* End while */    return(FALSE);}   /* End of readline */#endif/*****************************************************************************/#ifdef BSD4_2setupline(){    struct sgttyb	sgtty;    static struct tchars	tchar = { '\377',	/* interrupt */					  '\377',	/* quit */					  '\021',	/* start output */					  '\023',	/* stop output */					  '\377',	/* end-of-file */					  '\377'	/* input delimiter */					};    long	lmodes;    int		disc = NTTYDISC;/* * * Line initialization for BSD4_2. As in the System V code, if no line is given * (ie. line == NULL) we continue on as before using stdout as ttyi and ttyo. * */    if ( line == NULL )	ttyi = fileno(stdout);    else if ( (ttyi = open(line, O_RDWR)) == -1 )	error(FATAL, "can't open %s", line);    if ( (ttyo = dup(ttyi)) == -1 )	error(FATAL, "can't dup file descriptor for %s", line);    if (ioctl(ttyi, TIOCSETD, &disc) == -1 )	error(FATAL, "ioctl error - TIOCSETD");    if ( ioctl(ttyi, TIOCGETP, &sgtty) == -1 )	error(FATAL, "ioctl error - TIOCGETP");    if ( ioctl(ttyi, TIOCLGET, &lmodes) == -1 )	error(FATAL, "ioctl error - TIOCLGET");    sgtty.sg_flags &= ~ECHO;    sgtty.sg_flags &= ~CRMOD;    sgtty.sg_flags |= CBREAK;    sgtty.sg_ispeed = baudrate;    sgtty.sg_ospeed = baudrate;    lmodes |= LDECCTQ;    if ( ioctl(ttyi, TIOCSETP, &sgtty) == -1 )	error(FATAL, "ioctl error - TIOCSETP");    if ( ioctl(ttyi, TIOCSETC, &tchar) == -1 )	error(FATAL, "ioctl error - TIOCSETC");    if ( ioctl(ttyi, TIOCLSET, &lmodes) == -1 )	error(FATAL, "ioctl error - TIOCLSET");    fp_ttyi = fdopen(ttyi, "r");}   /* End of setupline *//*****************************************************************************/resetline(){    struct sgttyb	sgtty;/* * * Only used if we're running the program as separate read and write processes. * Called from split() after the initial connection has been made and returns * TRUE if two processes should work. Haven't tested or even compiled the stuff * for separate read and write processes on Berkeley systems - no guarantees * even though we return TRUE! * */    if ( ioctl(ttyi, TIOCGETP, &sgtty) == -1 )	error(FATAL, "ioctl error - TIOCGETP");    sgtty.sg_flags |= TANDEM;    if ( ioctl(ttyi, TIOCSETP, &sgtty) == -1 )	error(FATAL, "ioctl error - TIOCSETP");    return(TRUE);}   /* End of resetline *//*****************************************************************************/setupstdin(mode)    int		mode;			/* what to do with stdin settings */{    struct sgttyb		sgtty;    static int			saved = FALSE;    static struct sgttyb	oldsgtty;/* * * Save (mode = 0), reset (mode = 1), or restore (mode = 2) the tty settings for * stdin. Expect something like raw mode with no echo will be set up. Need to make * sure interrupt and quit still work - they're the only good way to exit when * we're running interactive mode. I haven't tested or even compiled this code * so there are no guarantees. * */    if ( interactive == TRUE )	switch ( mode )  {	    case 0:		if ( isatty(0) != 1 )		    error(FATAL, "stdin not a terminal - can't run interactive mode");		if ( ioctl(0, TIOCGETP, &oldsgtty) == -1 )		    error(FATAL, "can't save terminal settings");		saved = TRUE;		break;	    case 1:		sgtty = oldsgtty;		sgtty.sg_flags &= ~ECHO;		sgtty.sg_flags |= CBREAK;		ioctl(0, TIOCSETP, &sgtty);		break;	    case 2:		if ( saved == TRUE )		    ioctl(0, TIOCSETP, &oldsgtty);		break;	}   /* End switch */}   /* End of setupstdin *//*****************************************************************************/readline(){    int		n;			/* read() return value */    int		ch;			/* for interactive mode *//* * * Reads characters coming back from the printer on ttyo up to a newline (or EOF) * or until no more characters are available. Characters are put in mesg[], the * string is terminated with '\0' when we're done with a line and TRUE is returned * to the caller. If complete line wasn't available FALSE is returned. Interactive * mode should loop here forever, except during start(), echoing characters to * stdout. If it happens to leave FALSE should be returned. Probably should read * everything available on ttyi into a temporary buffer and work from there rather * than reading one character at a time. * */    if ( interactive == FALSE )  {	while ( 1 )  {	    if ( ioctl(ttyi, FIONREAD, &n) < 0 )		if ( errno == EINTR )		    continue;		else error(FATAL, "ioctl error - FIONREAD");	    if ( n <= 0 )		if ( canwrite == TRUE )		    return(FALSE);		else n = 1;	    for ( ; n > 0; n-- )  {		/*if ( read(ttyi, ptr, 1) < 0 )*/		if ( (*ptr = getc(fp_ttyi)) == EOF )		    if ( errno == EINTR )			continue;		    else error(FATAL, "error reading %s", line);		if ( *ptr == '\r' ) continue;		if ( *ptr == '\n' || *ptr == '\004' || ptr >= endmesg )  {		    *(ptr+1) = '\0';		    if ( *ptr == '\004' )			strcpy(ptr, "%%[ status: endofjob ]%%\n");		    ptr = mesg;		    return(TRUE);		}   /* End if */		++ptr;	    }	/* End for */	}   /* End while */    }	/* End if */    if ( canwrite == TRUE )		/* don't block during start() */	return(FALSE);    while ( (ch = getc(fp_ttyi)) != EOF )	putc(ch, stdout);    return(FALSE);}   /* End of readline *//*****************************************************************************//*	@(#)strspn.c	1.2	*//*LINTLIBRARY*//* * Return the number of characters in the maximum leading segment * of string which consists solely of characters from charset. */intstrspn(string, charset)char	*string;register char	*charset;{	register char *p, *q;	for(q=string; *q != '\0'; ++q) {		for(p=charset; *p != '\0' && *p != *q; ++p)			;		if(*p == '\0')			break;	}	return(q-string);}/*	@(#)strpbrk.c	1.2	*//*LINTLIBRARY*//* * Return ptr to first occurance of any character from `brkset' * in the character string `string'; NULL if none exists. */char *strpbrk(string, brkset)register char *string, *brkset;{	register char *p;	do {		for(p=brkset; *p != '\0' && *p != *string; ++p)			;		if(*p != '\0')			return(string);	}	while(*string++);	return((char*)0);}/*	@(#)strtok.c	1.2	*//*	3.0 SID #	1.2	*//*LINTLIBRARY*//* * uses strpbrk and strspn to break string into tokens on * sequentially subsequent calls.  returns NULL when no * non-separator characters remain. * `subsequent' calls are calls with first argument NULL. */extern int strspn();extern char *strpbrk();char *strtok(string, sepset)char	*string, *sepset;{	register char	*p, *q, *r;	static char	*savept;	/*first or subsequent call*/	p = (string == (char*)0)? savept: string;	if(p == 0)		/* return if no tokens remaining */		return((char*)0);	q = p + strspn(p, sepset);	/* skip leading separators */	if(*q == '\0')		/* return if no tokens remaining */		return((char*)0);	if((r = strpbrk(q, sepset)) == (char*)0)	/* move past token */		savept = 0;	/* indicate this is last token */	else {		*r = '\0';		savept = ++r;	}	return(q);}#endif/*****************************************************************************/#ifdef DKHOST#ifndef DKSTREAMSshort	dkrmode[3] = {DKR_TIME, 0, 0};#endifdkhost_connect(){    int		ofd;			/* for saving and restoring stderr */    int		dfd;    int		retrytime = 5;/* * * Tries to connect to a Datakit destination. The extra stuff I've added to save * and later restore stderr is primarily for our spooling setup at Murray Hill. * postio is usually called with stderr directed to a file that will be returned * to the user when the job finishes printing. Problems encountered by dkdial(), * like busy messages, go to stderr but don't belong in the user's mail. They'll * be temporarily directed to the log file. After we've connected stderr will be * restored. * */    if ( *line == '\0' )	error(FATAL, "incomplete Datakit line");    if ( fp_log != NULL && fp_log != stderr )  {	/* redirect dkdial errors */	ofd = dup(2);	close(2);	dup(fileno(fp_log));    }	/* End if */    while ( (dfd = ttyi = dkdial(line)) < 0 )  {	if ( retrytime < 0 )	    error(FATAL, "can't connect to %s", line);	sleep(retrytime++);	if ( retrytime > 60 )	    retrytime = 60;    }	/* End while */    if ( fp_log != NULL && fp_log != stderr )  {	/* restore stderr */	close(2);	dup(ofd);	close(ofd);    }	/* End if */#ifndef DKSTREAMS    if ( ioctl(ttyi, DIOCRMODE, dkrmode) == -1 )	error(FATAL, "ioctl error - DIOCRMODE");#ifdef DIOURPWD    if ( window_size > 0 ) {	short	dkparm[3];	dkparm[0] = dkminor(ttyi);	dkparm[1] = 1;	dkparm[2] = window_size;	if ( ioctl(ttyi, DIOURPWD, dkparm) < 0 || ioctl(ttyi, DIOCFLUSH, 0) < 0 )	    error(NON_FATAL, "WSA failed");    }	/* End if */#endif    line = dtnamer(dkminor(ttyi));    if ( (ttyi = open(line, O_RDWR)) == -1 )	error(FATAL, "can't open %s", line);    close(dfd);#endif}   /* End of dkhost_connect */#endif/*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -