📄 filecom.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.24 06/05/06 */ /* */ /* FILE COMMANDS MODULE */ /*******************************************************//*************************************************************//* Purpose: Contains the code for file commands including *//* batch, dribble-on, dribble-off, save, load, bsave, and *//* bload. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* Bebe Ly *//* *//* Revision History: *//* *//* 6.24: Added environment parameter to GenClose. *//* Added environment parameter to GenOpen. *//* *//* Renamed BOOLEAN macro type to intBool. *//* *//*************************************************************/#define _FILECOM_SOURCE_#include <stdio.h>#define _STDIO_INCLUDED_#include <string.h>#include "setup.h"#include "argacces.h"#include "constrct.h"#include "commline.h"#include "cstrcpsr.h"#include "envrnmnt.h"#include "extnfunc.h"#include "memalloc.h"#include "prcdrfun.h"#include "router.h"#include "strngrtr.h"#include "sysdep.h"#include "utility.h"#include "filecom.h"#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE#include "bsave.h"#include "bload.h"#endif/***************//* STRUCTURES *//***************/struct batchEntry { int batchType; void *inputSource; char *theString; struct batchEntry *next; };/***************//* DEFINITIONS *//***************/#define FILE_BATCH 0#define STRING_BATCH 1#define BUFFER_SIZE 120#define FILECOM_DATA 14struct fileCommandData { #if DEBUGGING_FUNCTIONS FILE *DribbleFP; char *DribbleBuffer; size_t DribbleCurrentPosition; size_t DribbleMaximumPosition; int (*DribbleStatusFunction)(void *,int);#endif int BatchType; void *BatchSource; char *BatchBuffer; size_t BatchCurrentPosition; size_t BatchMaximumPosition; struct batchEntry *TopOfBatchList; struct batchEntry *BottomOfBatchList; };#define FileCommandData(theEnv) ((struct fileCommandData *) GetEnvironmentData(theEnv,FILECOM_DATA))/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if DEBUGGING_FUNCTIONS static int FindDribble(void *,char *); static int GetcDribble(void *,char *); static int UngetcDribble(void *,int,char *); static int ExitDribble(void *,int); static int PrintDribble(void *,char *,char *); static void PutcDribbleBuffer(void *,int);#endif static int FindBatch(void *,char *); static int GetcBatch(void *,char *); static int UngetcBatch(void *,int,char *); static int ExitBatch(void *,int); static void AddBatch(void *,int,void *,int,char *); static void DeallocateFileCommandData(void *);/***************************************//* FileCommandDefinitions: Initializes *//* file commands. *//***************************************/globle void FileCommandDefinitions( void *theEnv) { AllocateEnvironmentData(theEnv,FILECOM_DATA,sizeof(struct fileCommandData),DeallocateFileCommandData);#if ! RUN_TIME#if DEBUGGING_FUNCTIONS EnvDefineFunction2(theEnv,"batch",'b',PTIEF BatchCommand,"BatchCommand","11k"); EnvDefineFunction2(theEnv,"batch*",'b',PTIEF BatchStarCommand,"BatchStarCommand","11k"); EnvDefineFunction2(theEnv,"dribble-on",'b',PTIEF DribbleOnCommand,"DribbleOnCommand","11k"); EnvDefineFunction2(theEnv,"dribble-off",'b',PTIEF DribbleOffCommand,"DribbleOffCommand","00"); EnvDefineFunction2(theEnv,"save",'b',PTIEF SaveCommand,"SaveCommand","11k");#endif EnvDefineFunction2(theEnv,"load",'b',PTIEF LoadCommand,"LoadCommand","11k"); EnvDefineFunction2(theEnv,"load*",'b',PTIEF LoadStarCommand,"LoadStarCommand","11k");#if BLOAD_AND_BSAVE InitializeBsaveData(theEnv); EnvDefineFunction2(theEnv,"bsave",'b', PTIEF BsaveCommand,"BsaveCommand","11k");#endif#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE InitializeBloadData(theEnv); EnvDefineFunction2(theEnv,"bload",'b',PTIEF BloadCommand,"BloadCommand","11k");#endif#endif } /******************************************************//* DeallocateFileCommandData: Deallocates environment *//* data for file commands. *//******************************************************/static void DeallocateFileCommandData( void *theEnv) { struct batchEntry *theEntry, *nextEntry; theEntry = FileCommandData(theEnv)->TopOfBatchList; while (theEntry != NULL) { nextEntry = theEntry->next; if (theEntry->batchType == FILE_BATCH) { GenClose(theEnv,(FILE *) FileCommandData(theEnv)->TopOfBatchList->inputSource); } else { rm(theEnv,theEntry->theString,strlen(theEntry->theString) + 1); } rtn_struct(theEnv,batchEntry,theEntry); theEntry = nextEntry; } if (FileCommandData(theEnv)->BatchBuffer != NULL) { rm(theEnv,FileCommandData(theEnv)->BatchBuffer,FileCommandData(theEnv)->BatchMaximumPosition); }#if DEBUGGING_FUNCTIONS if (FileCommandData(theEnv)->DribbleBuffer != NULL) { rm(theEnv,FileCommandData(theEnv)->DribbleBuffer,FileCommandData(theEnv)->DribbleMaximumPosition); } if (FileCommandData(theEnv)->DribbleFP != NULL) { GenClose(theEnv,FileCommandData(theEnv)->DribbleFP); }#endif } #if DEBUGGING_FUNCTIONS/*****************************************************//* FindDribble: Find routine for the dribble router. *//*****************************************************/#if IBM_TBC#pragma argsused#endifstatic int FindDribble( void *theEnv, char *logicalName) {#if MAC_MCW || IBM_MCW || MAC_XCD#pragma unused(theEnv)#endif if ( (strcmp(logicalName,"stdout") == 0) || (strcmp(logicalName,"stdin") == 0) || (strcmp(logicalName,WPROMPT) == 0) || (strcmp(logicalName,WTRACE) == 0) || (strcmp(logicalName,WERROR) == 0) || (strcmp(logicalName,WWARNING) == 0) || (strcmp(logicalName,WDISPLAY) == 0) || (strcmp(logicalName,WDIALOG) == 0) ) { return(TRUE); } return(FALSE); }/*******************************************************//* PrintDribble: Print routine for the dribble router. *//*******************************************************/static int PrintDribble( void *theEnv, char *logicalName, char *str) { int i; /*======================================*/ /* Send the output to the dribble file. */ /*======================================*/ for (i = 0 ; str[i] != EOS ; i++) { PutcDribbleBuffer(theEnv,str[i]); } /*===========================================================*/ /* Send the output to any routers interested in printing it. */ /*===========================================================*/ EnvDeactivateRouter(theEnv,"dribble"); EnvPrintRouter(theEnv,logicalName,str); EnvActivateRouter(theEnv,"dribble"); return(1); }/*****************************************************//* GetcDribble: Getc routine for the dribble router. *//*****************************************************/static int GetcDribble( void *theEnv, char *logicalName) { int rv; /*===========================================*/ /* Deactivate the dribble router and get the */ /* character from another active router. */ /*===========================================*/ EnvDeactivateRouter(theEnv,"dribble"); rv = EnvGetcRouter(theEnv,logicalName); EnvActivateRouter(theEnv,"dribble"); /*==========================================*/ /* Put the character retrieved from another */ /* router into the dribble buffer. */ /*==========================================*/ PutcDribbleBuffer(theEnv,rv); /*=======================*/ /* Return the character. */ /*=======================*/ return(rv); }/***********************************************************//* PutcDribbleBuffer: Putc routine for the dribble router. *//***********************************************************/static void PutcDribbleBuffer( void *theEnv, int rv) { /*===================================================*/ /* Receiving an end-of-file character will cause the */ /* contents of the dribble buffer to be flushed. */ /*===================================================*/ if (rv == EOF) { if (FileCommandData(theEnv)->DribbleCurrentPosition > 0) { fprintf(FileCommandData(theEnv)->DribbleFP,"%s",FileCommandData(theEnv)->DribbleBuffer); FileCommandData(theEnv)->DribbleCurrentPosition = 0; FileCommandData(theEnv)->DribbleBuffer[0] = EOS; } } /*===========================================================*/ /* If we aren't receiving command input, then the character */ /* just received doesn't need to be placed in the dribble */ /* buffer--It can be written directly to the file. This will */ /* occur for example when the command prompt is being */ /* printed (the CommandBufferInputCount variable will be -1 */ /* because command input has not been receivied yet). Before */ /* writing the character to the file, the dribble buffer is */ /* flushed. */ /*===========================================================*/ else if (RouterData(theEnv)->CommandBufferInputCount < 0) { if (FileCommandData(theEnv)->DribbleCurrentPosition > 0) { fprintf(FileCommandData(theEnv)->DribbleFP,"%s",FileCommandData(theEnv)->DribbleBuffer); FileCommandData(theEnv)->DribbleCurrentPosition = 0; FileCommandData(theEnv)->DribbleBuffer[0] = EOS; } fputc(rv,FileCommandData(theEnv)->DribbleFP); } /*=====================================================*/ /* Otherwise, add the character to the dribble buffer. */ /*=====================================================*/ else { FileCommandData(theEnv)->DribbleBuffer = ExpandStringWithChar(theEnv,rv,FileCommandData(theEnv)->DribbleBuffer, &FileCommandData(theEnv)->DribbleCurrentPosition, &FileCommandData(theEnv)->DribbleMaximumPosition, FileCommandData(theEnv)->DribbleMaximumPosition+BUFFER_SIZE); } }/*********************************************************//* UngetcDribble: Ungetc routine for the dribble router. *//*********************************************************/static int UngetcDribble( void *theEnv, int ch, char *logicalName) { int rv; /*===============================================*/ /* Remove the character from the dribble buffer. */ /*===============================================*/ if (FileCommandData(theEnv)->DribbleCurrentPosition > 0) FileCommandData(theEnv)->DribbleCurrentPosition--; FileCommandData(theEnv)->DribbleBuffer[FileCommandData(theEnv)->DribbleCurrentPosition] = EOS; /*=============================================*/ /* Deactivate the dribble router and pass the */ /* ungetc request to the other active routers. */ /*=============================================*/ EnvDeactivateRouter(theEnv,"dribble"); rv = EnvUngetcRouter(theEnv,ch,logicalName); EnvActivateRouter(theEnv,"dribble"); /*==========================================*/ /* Return the result of the ungetc request. */ /*==========================================*/ return(rv); }/*****************************************************//* ExitDribble: Exit routine for the dribble router. *//*****************************************************/#if IBM_TBC#pragma argsused#endifstatic int ExitDribble( void *theEnv, int num) {#if MAC_MCW || IBM_MCW || MAC_XCD#pragma unused(num)#endif if (FileCommandData(theEnv)->DribbleCurrentPosition > 0) { fprintf(FileCommandData(theEnv)->DribbleFP,"%s",FileCommandData(theEnv)->DribbleBuffer); } if (FileCommandData(theEnv)->DribbleFP != NULL) GenClose(theEnv,FileCommandData(theEnv)->DribbleFP); return(1); }/******************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -