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

📄 tclunixtest.c

📁 linux系统下的音频通信
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  * tclUnixTest.c -- * *	Contains platform specific test commands for the Unix platform. * * Copyright (c) 1996 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * * SCCS: @(#) tclUnixTest.c 1.5 97/10/31 17:23:42 */#include "tclInt.h"#include "tclPort.h"/* * The headers are needed for the testalarm command that verifies the * use of SA_RESTART in signal handlers. */#include <signal.h>#include <sys/resource.h>/* * The following macros convert between TclFile's and fd's.  The conversion * simple involves shifting fd's up by one to ensure that no valid fd is ever * the same as NULL.  Note that this code is duplicated from tclUnixPipe.c */#define MakeFile(fd) ((TclFile)((fd)+1))#define GetFd(file) (((int)file)-1)/* * The stuff below is used to keep track of file handlers created and * exercised by the "testfilehandler" command. */typedef struct Pipe {    TclFile readFile;		/* File handle for reading from the				 * pipe.  NULL means pipe doesn't exist yet. */    TclFile writeFile;		/* File handle for writing from the				 * pipe. */    int readCount;		/* Number of times the file handler for				 * this file has triggered and the file				 * was readable. */    int writeCount;		/* Number of times the file handler for				 * this file has triggered and the file				 * was writable. */} Pipe;#define MAX_PIPES 10static Pipe testPipes[MAX_PIPES];/* * The stuff below is used by the testalarm and testgotsig ommands. */static char *gotsig = "0";/* * Forward declarations of procedures defined later in this file: */static void		TestFileHandlerProc _ANSI_ARGS_((ClientData clientData,			    int mask));static int		TestfilehandlerCmd _ANSI_ARGS_((ClientData dummy,			    Tcl_Interp *interp, int argc, char **argv));static int		TestfilewaitCmd _ANSI_ARGS_((ClientData dummy,			    Tcl_Interp *interp, int argc, char **argv));static int		TestfindexecutableCmd _ANSI_ARGS_((ClientData dummy,			    Tcl_Interp *interp, int argc, char **argv));static int		TestgetopenfileCmd _ANSI_ARGS_((ClientData dummy,			    Tcl_Interp *interp, int argc, char **argv));int			TclplatformtestInit _ANSI_ARGS_((Tcl_Interp *interp));static int		TestalarmCmd _ANSI_ARGS_((ClientData dummy,			    Tcl_Interp *interp, int argc, char **argv));static int		TestgotsigCmd _ANSI_ARGS_((ClientData dummy,			    Tcl_Interp *interp, int argc, char **argv));static void 		AlarmHandler _ANSI_ARGS_(());/* *---------------------------------------------------------------------- * * TclplatformtestInit -- * *	Defines commands that test platform specific functionality for *	Unix platforms. * * Results: *	A standard Tcl result. * * Side effects: *	Defines new commands. * *---------------------------------------------------------------------- */intTclplatformtestInit(interp)    Tcl_Interp *interp;		/* Interpreter to add commands to. */{    Tcl_CreateCommand(interp, "testfilehandler", TestfilehandlerCmd,            (ClientData) 0, (Tcl_CmdDeleteProc *) NULL);    Tcl_CreateCommand(interp, "testfilewait", TestfilewaitCmd,            (ClientData) 0, (Tcl_CmdDeleteProc *) NULL);    Tcl_CreateCommand(interp, "testfindexecutable", TestfindexecutableCmd,            (ClientData) 0, (Tcl_CmdDeleteProc *) NULL);    Tcl_CreateCommand(interp, "testgetopenfile", TestgetopenfileCmd,            (ClientData) 0, (Tcl_CmdDeleteProc *) NULL);    Tcl_CreateCommand(interp, "testalarm", TestalarmCmd,            (ClientData) 0, (Tcl_CmdDeleteProc *) NULL);    Tcl_CreateCommand(interp, "testgotsig", TestgotsigCmd,            (ClientData) 0, (Tcl_CmdDeleteProc *) NULL);    return TCL_OK;}/* *---------------------------------------------------------------------- * * TestfilehandlerCmd -- * *	This procedure implements the "testfilehandler" command. It is *	used to test Tcl_CreateFileHandler, Tcl_DeleteFileHandler, and *	TclWaitForFile. * * Results: *	A standard Tcl result. * * Side effects: *	None. * *---------------------------------------------------------------------- */static intTestfilehandlerCmd(clientData, interp, argc, argv)    ClientData clientData;		/* Not used. */    Tcl_Interp *interp;			/* Current interpreter. */    int argc;				/* Number of arguments. */    char **argv;			/* Argument strings. */{    Pipe *pipePtr;    int i, mask, timeout;    static int initialized = 0;    char buffer[4000];    TclFile file;    /*     * NOTE: When we make this code work on Windows also, the following     * variable needs to be made Unix-only.     */        if (!initialized) {	for (i = 0; i < MAX_PIPES; i++) {	    testPipes[i].readFile = NULL;	}	initialized = 1;    }    if (argc < 2) {	Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],                " option ... \"", (char *) NULL);        return TCL_ERROR;    }    pipePtr = NULL;    if (argc >= 3) {	if (Tcl_GetInt(interp, argv[2], &i) != TCL_OK) {	    return TCL_ERROR;	}	if (i >= MAX_PIPES) {	    Tcl_AppendResult(interp, "bad index ", argv[2], (char *) NULL);	    return TCL_ERROR;	}	pipePtr = &testPipes[i];    }    if (strcmp(argv[1], "close") == 0) {	for (i = 0; i < MAX_PIPES; i++) {	    if (testPipes[i].readFile != NULL) {		TclpCloseFile(testPipes[i].readFile);		testPipes[i].readFile = NULL;		TclpCloseFile(testPipes[i].writeFile);		testPipes[i].writeFile = NULL;	    }	}    } else if (strcmp(argv[1], "clear") == 0) {	if (argc != 3) {	    Tcl_AppendResult(interp, "wrong # arguments: should be \"",                    argv[0], " clear index\"", (char *) NULL);	    return TCL_ERROR;	}	pipePtr->readCount = pipePtr->writeCount = 0;    } else if (strcmp(argv[1], "counts") == 0) {	char buf[30];		if (argc != 3) {	    Tcl_AppendResult(interp, "wrong # arguments: should be \"",                    argv[0], " counts index\"", (char *) NULL);	    return TCL_ERROR;	}	sprintf(buf, "%d %d", pipePtr->readCount, pipePtr->writeCount);	Tcl_SetResult(interp, buf, TCL_VOLATILE);    } else if (strcmp(argv[1], "create") == 0) {	if (argc != 5) {	    Tcl_AppendResult(interp, "wrong # arguments: should be \"",                    argv[0], " create index readMode writeMode\"",                    (char *) NULL);	    return TCL_ERROR;	}	if (pipePtr->readFile == NULL) {	    if (!TclpCreatePipe(&pipePtr->readFile, &pipePtr->writeFile)) {		Tcl_AppendResult(interp, "couldn't open pipe: ",			Tcl_PosixError(interp), (char *) NULL);		return TCL_ERROR;	    }#ifdef O_NONBLOCK	    fcntl(GetFd(pipePtr->readFile), F_SETFL, O_NONBLOCK);	    fcntl(GetFd(pipePtr->writeFile), F_SETFL, O_NONBLOCK);#else	    Tcl_SetResult(interp, "can't make pipes non-blocking",		    TCL_STATIC);	    return TCL_ERROR;#endif	}	pipePtr->readCount = 0;	pipePtr->writeCount = 0;	if (strcmp(argv[3], "readable") == 0) {	    Tcl_CreateFileHandler(GetFd(pipePtr->readFile), TCL_READABLE,		    TestFileHandlerProc, (ClientData) pipePtr);	} else if (strcmp(argv[3], "off") == 0) {	    Tcl_DeleteFileHandler(GetFd(pipePtr->readFile));	} else if (strcmp(argv[3], "disabled") == 0) {	    Tcl_CreateFileHandler(GetFd(pipePtr->readFile), 0,		    TestFileHandlerProc, (ClientData) pipePtr);	} else {	    Tcl_AppendResult(interp, "bad read mode \"", argv[3], "\"",		    (char *) NULL);	    return TCL_ERROR;	}	if (strcmp(argv[4], "writable") == 0) {	    Tcl_CreateFileHandler(GetFd(pipePtr->writeFile), TCL_WRITABLE,		    TestFileHandlerProc, (ClientData) pipePtr);	} else if (strcmp(argv[4], "off") == 0) {	    Tcl_DeleteFileHandler(GetFd(pipePtr->writeFile));	} else if (strcmp(argv[4], "disabled") == 0) {	    Tcl_CreateFileHandler(GetFd(pipePtr->writeFile), 0,		    TestFileHandlerProc, (ClientData) pipePtr);	} else {	    Tcl_AppendResult(interp, "bad read mode \"", argv[4], "\"",		    (char *) NULL);	    return TCL_ERROR;	}    } else if (strcmp(argv[1], "empty") == 0) {	if (argc != 3) {	    Tcl_AppendResult(interp, "wrong # arguments: should be \"",                    argv[0], " empty index\"", (char *) NULL);	    return TCL_ERROR;	}        while (read(GetFd(pipePtr->readFile), buffer, 4000) > 0) {            /* Empty loop body. */        }    } else if (strcmp(argv[1], "fill") == 0) {	if (argc != 3) {	    Tcl_AppendResult(interp, "wrong # arguments: should be \"",                    argv[0], " empty index\"", (char *) NULL);	    return TCL_ERROR;	}	memset((VOID *) buffer, 'a', 4000);        while (write(GetFd(pipePtr->writeFile), buffer, 4000) > 0) {            /* Empty loop body. */        }    } else if (strcmp(argv[1], "fillpartial") == 0) {	char buf[30];		if (argc != 3) {	    Tcl_AppendResult(interp, "wrong # arguments: should be \"",                    argv[0], " empty index\"", (char *) NULL);	    return TCL_ERROR;	}	memset((VOID *) buffer, 'b', 10);	sprintf(buf, "%d", write(GetFd(pipePtr->writeFile), buffer, 10));	Tcl_SetResult(interp, buf, TCL_VOLATILE);    } else if (strcmp(argv[1], "oneevent") == 0) {	Tcl_DoOneEvent(TCL_FILE_EVENTS|TCL_DONT_WAIT);    } else if (strcmp(argv[1], "wait") == 0) {	if (argc != 5) {	    Tcl_AppendResult(interp, "wrong # arguments: should be \"",                    argv[0], " wait index readable/writable timeout\"",                    (char *) NULL);	    return TCL_ERROR;	}	if (pipePtr->readFile == NULL) {	    Tcl_AppendResult(interp, "pipe ", argv[2], " doesn't exist",		    (char *) NULL);	    return TCL_ERROR;	}	if (strcmp(argv[3], "readable") == 0) {	    mask = TCL_READABLE;	    file = pipePtr->readFile;

⌨️ 快捷键说明

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