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

📄 sptest.c

📁 操作系统SunOS 4.1.3版本的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
**	description	The Sundiag environmental variables "SERIAL_PORTS_n"*			are parsed into the source and destination fields.*			Example:**				SERIAL_PORTS_1 = a**			expands to:**				source         destination*				------         -----------*				/dev/ttya	/dev/ttya**			and*				SERIAL_PORTS_1 = a-b**			expands to:**				source         destination*				------         -----------*				/dev/ttya      /dev/ttyb**//* * syspars program */syspars(source, destination)    char           *source, *destination;      /* ptrs source and destination					        * buffers */{    int             i;			       /* general purpose loop					        * variable */    unsigned char   port, *ptr;		       /* port character and pointer */    /*     * Initialize     */    srcbuf = source;			       /* global ptr to source line */    desbuf = destination;		       /* global ptr to destination					        * buffer */    /*     * get Sundiag ports from "SERIAL_PORTS_n" envirmental variables.     */    for (i = 1; i < 100; i++) {	sprintf(serial_ports, "SERIAL_PORTS_%d", i);	if (getenv(serial_ports)) {	    strcpy(port_to_test, "/dev/tty");	    strcat(port_to_test, (getenv(serial_ports)));	    send_message(0, DEBUG, ports_test_msg, serial_ports, port_to_test);	    port_ptr = port_to_test;	    while ((*srcbuf++ = *port_ptr++) != '\0') {		if (*(port_ptr - 1) == '-') {		    (*(srcbuf - 1)) = '\0';		    *desbuf--;		    if (*(desbuf - 1) != 'y')			*desbuf--;		    while (*port_ptr != '\0') {			*desbuf++ = *port_ptr++;		    }		    break;		} else {		    *desbuf++ = (*(port_ptr - 1));		}	    }	    *desbuf++ = '\0';		       /* mark end of device */	    /*	     * Check source port for device 0-3, if yes change 'tty' to	     * 'ttys'	     */	    if (*(srcbuf - 3) == 'y') {		port = (*(srcbuf - 2));		switch (port) {		case '0':		case '1':		case '2':		case '3':		    (*(srcbuf - 2)) = 's';		    (*(srcbuf - 1)) = port;		    *srcbuf++ = '\0';	       /* mark end of device */		    break;		}	    }	    /*	     * Check destination port for device 0-3, if yes change 'tty' to	     * 'ttys'	     */	    if (*(desbuf - 3) == 'y') {		port = (*(desbuf - 2));		switch (port) {		case '0':		case '1':		case '2':		case '3':		    (*(desbuf - 2)) = 's';		    (*(desbuf - 1)) = port;		    *desbuf++ = '\0';	       /* mark end of device */		    break;		}	    }	} else {	    if (i == 1) 		send_message(-NO_SD_PORTS_SELECTED, ERROR, no_ports_msg);	    break;	}    } /* end of for */}/********************************************************************************	name		"expars.c"**	synopsis	status = expars(source, destination, limit)**			status	=>	0 = success, non-zero = error**			source	=>	set of arguments or ranges to be*					parsed and expanded**			destination =>	address of buffer to hold expanded*					list of arguments**			limit	=>	size of expand buffer****	description	A single string (usually a command line argument) is*			is parsed and expanded according to its content. If*			a range is given, a table of device names is*			 constructed.  The same applies to a a list.*			  Construction  uses the full root, replacing*			each instance with the number of bytes given in the*			range or list.  Example:**				"/dev/tty00-3"**			expands to:**				/dev/tty00*				/dev/tty01*				/dev/tty02*				/dev/tty03**			and*				"/dev/tty00,2,7,11"**			expands to:**				/dev/tty00*				/dev/tty02*				/dev/tty07*				/dev/tty11***			conditions can be combined:**				"/dev/tty00-4,8,a,c-e"**			expands to:***				/dev/tty00*				/dev/tty01*				/dev/tty03*				/dev/tty04*				/dev/tty08*				/dev/tty0a*				/dev/tty0c*				/dev/tty0d*				/dev/tty0e**//* * expars program */expars(compact, expand, limit)    char           *compact, *expand;  /* ptrs source and expand buffers */    int             limit;	       /* size of expand buffer */{    int             i;		       /* general purpose loop variable */    int             status = 0;	       /* error status */    unsigned char   c, *ptr;	       /* general character and pointer */    bleft = limit - 2;		       /* set bytes left less a margin */    srcbuf = compact;		       /* global ptr to source line */    exbuf = expand;		       /* global ptr to expand buffer */    root = expand;		       /* intial root name at expand buffer */    *expand = '\0';		       /* set "null" expand buffer */    /*     * Parse source line     */    while ((*exbuf++ = *srcbuf++) != '\0') {	if (--bleft == 0)	    status = -1;	      /* prevent buffer overflow */	switch (*(srcbuf - 1)) {	case '-':		       /* range list */	    status = exrange();	    break;	case ',':		       /* item list */	    status = exlist();	    break;	}	if (status)	    break;		       /* error, stop parsing */    }    if (!status)	*exbuf = '\0';		       /* mark end of buffer */    return (status);		       /* exit with status */}/* * Expand range */exrange(){    int             upsize;		       /* size of upper limit */    *(exbuf - 1) = '\0';		       /* tag end root name					        * (overwrite '-') */    for (upsize = 0; isalnum(*srcbuf++); upsize++)	; 				/* get size of upper limit */    if (!upsize)	return (-1);		       /* no upper limit, return error */    while ((strncmp(srcbuf - 1 - upsize, exbuf - 1 - upsize, upsize)) > 0) {	/* expand until ranges meet */	while ((*exbuf++ = *root++) != '\0') {	    /* copy previous root name */	    if (--bleft == 0)		return (-1);		       /* prevent buffer overflow */	}	strinc(exbuf - 1 - upsize);	       /* yet upper limit, increment */    }    srcbuf--;		       /* end of upper range in source line */    exbuf--;		       /* end of latest name in buffer */    return(0);		       /* return success */}/* * Expand list */exlist(){    int             newsize;		/* size of new item */    *(exbuf - 1) = '\0';		/* tag end root name(overwrite ',') */    for (newsize = 0; (isalnum(*srcbuf++)); newsize++)	; 				/* get size of new item */    if (!newsize)	return (-1);		       /* no new item, return error */    while ((*exbuf++ = *root++) != '\0') { /* copy previous root name */	if (--bleft == 0)	    return (-1);	       /* prevent buffer overflow */    }    srcbuf -= newsize + 1;	       /* point at new item replacement */    exbuf -= newsize + 1;	       /* point at old part to replace */    while (newsize--) 	*exbuf++ = *srcbuf++;	       /* replace old part of item name */    return(0);			       /* return success */}/* * Increment a string */strinc(ptr)    char           *ptr;		       /* string to increment */{    int             size;		       /* size of string */    for (size = 0; *ptr++ != '\0'; size++)	;    					/* measure string */    ptr--;				       /* back up to end of string */    while (size--) {	ptr--;				       /* back up to next character */	(*ptr)++;			       /* increment last character */	switch (*ptr) {		/* check for "rollover" of * range */	case '9' + 1:	       /* end of numeric range? */	    *ptr = '0';	       /* "roll over" for carry */	    break;	       /* try again on next char */	case 'Z' + 1:	       /* end of upper case alpha range? */	    *ptr = 'A';	       /* "roll over" for carry */	    break;	       /* try again on next char */	case 'z' + 1:	       /* end of lower case range? */	    *ptr = 'a';	       /* "roll over" for carry */	    break;	       /* try again on next char */	default:	    return (0);	       /* done, exit */	}    }    return (-1);	       /* no more places for carry, * error */}receive_timeout(){    strcpy(device, receive_dev);    send_message(-NOT_READY, ERROR, no_response_msg, device);}clean_up(){    if (fcntl(srcfd, F_GETFL, 0) != -1) {    	if (srcfd > 0) {	    if (swapflag > 0) {		if (ioctl(srcfd, TIOCNXCL, 0) == -1)			    send_message(0, DEBUG, no_release_msg,"S",test_name,srcfd);	    }	    if (swapflag > 1) {		srclist.sg_flags = srcsav;     /* restore original tty mode */		if (ioctl(srcfd, TIOCSETP, &srclist) == -1)			    send_message(0, DEBUG, no_restore_msg,"S",test_name,srcfd);	    }	    send_message(0, DEBUG, close_xmit_msg, xmit_dev);	}    }    if (rcvfd > 0 && rcvfd != srcfd) {	if (fcntl(rcvfd, F_GETFL, 0) != -1) {			    if (swapflag > 2) {		if (ioctl(rcvfd, TIOCNXCL, 0) == -1)			    send_message(0, DEBUG, no_release_msg,"R",test_name,rcvfd);	    }	    if (swapflag > 3) {		rcvlist.sg_flags = rcvsav;     /* restore original tty mode */		if (ioctl(rcvfd, TIOCSETP, &rcvlist) == -1)		    send_message(0, DEBUG, no_restore_msg,"R",test_name,rcvfd);	    }	}    }}openfiles(srcdev, rcvdev, srcfdes, rcvfdes)    unsigned char	*srcdev, *rcvdev;    int		        *srcfdes, *rcvfdes;{    if ((*srcfdes = open(srcdev, O_RDWR, 7)) == -1) {	perror(perror_msg);	exit(DEV_NOT_OPEN);    }    send_message(0, DEBUG, open_port_msg, "transmit", srcdev, *srcfdes);    if ( !(strcmp(srcdev, rcvdev)) ) {	*rcvfdes = *srcfdes;    } else if ((*rcvfdes = open(rcvdev, O_RDWR, 7)) == -1) {	perror(perror_msg);	exit(DEV_NOT_OPEN);    }    if (strcmp(srcdev, rcvdev)) 	send_message(0, DEBUG, open_port_msg, "receive", rcvdev, *rcvfdes);    srcfd = *srcfdes;    rcvfd = *rcvfdes;}closefiles(srcfdes, rcvfdes)    int	srcfdes, rcvfdes;{    if (close(srcfdes) == -1) 	send_message(0, DEBUG, no_close_msg, test_name, srcfdes);    send_message(0, DEBUG, close_port_msg, "transmit", srcfdes);    if (srcfdes != rcvfdes) {	if (close(rcvfdes) == -1) 	    send_message(0, DEBUG, no_close_msg, test_name, rcvfdes);        if (srcfdes != rcvfdes)            send_message(0, DEBUG, close_port_msg, "receive", rcvfdes);    }}routine_usage(){    printf("\Routine specific arguments [defaults]:\n\device = serial port(s) to test [none]\n\	Supported devices:\n\		/dev/ttya, /dev/ttyb\n\		/dev/ttys0 to /dev/ttys3\n\		/dev/tty00 to /dev/tty3f\n\		/dev/ttyh0 to /dev/ttyhf\n\		/dev/ttyi0 to /dev/ttyif\n\		/dev/ttyj0 to /dev/ttyjf\n\		/dev/ttyk0 to /dev/ttykf\n\	Supported keywords:\n\		{board.}double8 - first 8 ports, double loop\n\		{board.}double12 - 12 ports 4-16, double loop\n\		{board.}double14 - first 14 ports, double loop\n\		{board.}pairs - 16 ports, double loop\n\	The keywords may be prefixed with the board, 0-3 or h-k. [0.]:\n"	);}process_sptest_args(argv, arrcount)char    *argv[];int     arrcount;{    int	i, match = FALSE;    if (strncmp(argv[arrcount], "D=", 2) == 0) {        match = TRUE;        strcpy(sundiag_device, argv[arrcount]+2);        device_name = sundiag_device;    } else if (!strncmp(argv[arrcount], "/dev/tty", 8) && !keyword) {	match = TRUE;        if (!device_1) {            device_1 = TRUE;            if (expars(argv[arrcount], srcbufm, BUF_SIZE - 1))                printf("%s: %s unable to expand the source argument.\n",                               test_name, argv[arrcount]);            if (expars(argv[arrcount], rcvbufm, BUF_SIZE - 1))                printf("%s: %s unable to expand the receiver argument.\n",                               test_name, argv[arrcount]);	    send_message(0, VERBOSE, "Testing %s.", argv[arrcount]);        } else {            if (!device_2) {                device_2 = TRUE;                if (expars(argv[arrcount], rcvbufm, BUF_SIZE - 1))                    printf("%s: %s unable to expand the receiver argument.\n",                           test_name, argv[arrcount]);		send_message(0, VERBOSE, "Testing %s.",argv[arrcount]);            } else                match = FALSE;        }    }    if (!match && !device_1 && !keyword) {        for (i = 0; *(pkg[i].key) != '\0'; i++) {            if (!strcmp(argv[arrcount], pkg[i].key)) {                if (expars(pkg[i].src, srcbufm, BUF_SIZE - 1))                    send_message(0, DEBUG, no_expand_msg, "S", 		                 test_name, pkg[i].src);                if (expars(pkg[i].rcv, rcvbufm, BUF_SIZE - 1))                    send_message(0, DEBUG, no_expand_msg, "R", 		                 test_name, pkg[i].rcv);                keyword = TRUE;                match = TRUE;		send_message(0, VERBOSE, "Testing %s.",argv[arrcount]);                break;            }        }    }    return (match);} 

⌨️ 快捷键说明

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