?? hshell.c
字號(hào):
/* ----------------------------------------------------------- *//* *//* ___ *//* |_| | |_/ SPEECH *//* | | | | \ RECOGNITION *//* ========= SOFTWARE */ /* *//* *//* ----------------------------------------------------------- *//* developed at: *//* *//* Speech Vision and Robotics group *//* Cambridge University Engineering Department *//* http://svr-www.eng.cam.ac.uk/ *//* *//* Entropic Cambridge Research Laboratory *//* (now part of Microsoft) *//* *//* ----------------------------------------------------------- *//* Copyright: Microsoft Corporation *//* 1995-2000 Redmond, Washington USA *//* http://www.microsoft.com *//* *//* 2001-2002 Cambridge University *//* Engineering Department *//* *//* Use of this software is governed by a License Agreement *//* ** See the file License for the Conditions of Use ** *//* ** This banner notice must not be removed ** *//* *//* ----------------------------------------------------------- *//* File: HShell.c: Interface to the Shell *//* ----------------------------------------------------------- */char *hshell_version = "!HVER!HShell: 3.2 [CUED 09/12/02]";char *hshell_vc_id = "$Id: HShell.c,v 1.12 2002/12/19 16:37:11 ge204 Exp $";#include "HShell.h"#ifdef WIN32#include <windows.h>#include <mmsystem.h>#include <fcntl.h>#endif#ifdef UNIX#include <sys/ioctl.h>#endif/* ------------------------ Trace Flags --------------------- */static int trace = 0;#define T_IOP 0002 /* i/o input via FOpen */#define T_EXF 0004 /* extended file name processing *//* --------------------- Global Variables ------------------- */static Boolean infoPrinted = FALSE; /* set when -A -B or -V is used */static Boolean abortOnError = FALSE; /* causes HError to abort */static Boolean printVersionInfo = FALSE; /* request version info */static Boolean showConfig = FALSE; /* show configuration params */static Boolean noNumEscapes = FALSE; /* Prevent writing in \012 format */static Boolean natReadOrder = FALSE; /* Preserve natural mach read order*/static Boolean natWriteOrder = FALSE; /* Preserve natural mach write order*/static Boolean extendedFileNames = TRUE; /* allow extended file names *//* Global variable indicating VAX-order architecture for storing numbers */Boolean vaxOrder = FALSE;#define MAXEFS 5 /* max num ext files to remember */typedef struct { /* extended file name */ char logfile[1024]; /* logical name */ char actfile[1024]; /* actual file name */ long stindex; /* start sample to extract */ long enindex; /* end sample to extract */}ExtFile;static ExtFile extFiles[MAXEFS]; /* circ buf of ext file names */static int extFileNext = 0; /* next slot to save into */static int extFileUsed = 0; /* total ext files in buffer *//* ------------- Extended File Name Handling ---------------- *//* RegisterExtFileName: record details of fn exts if any in circ buffer */static char * RegisterExtFileName(char *s){ char *eq,*rb,*lb,*co; char buf[1024]; ExtFile *p; strcpy(buf,s); eq = strchr(buf,'='); lb = strchr(buf,'['); if (eq == NULL && lb == NULL) return s; if (trace&T_EXF) printf("Ext File Name: %s\n",buf); p = extFiles+extFileNext; ++extFileNext; if (extFileNext==MAXEFS) extFileNext=0; if (extFileUsed < MAXEFS) ++extFileUsed; p->stindex = p->enindex = -1; if (lb!=NULL) { if ((co = strchr(buf,',')) == NULL) HError(5024,"RegisterExtFileName: comma missing in index spec"); if ((rb = strchr(buf,']')) == NULL) HError(5024,"RegisterExtFileName: ] missing in index spec"); *rb = '\0'; p->enindex = atol(co+1); *co = '\0'; p->stindex = atol(lb+1); *lb = '\0'; } if (eq!=NULL) { strcpy(p->actfile,eq+1); *eq = '\0'; strcpy(p->logfile,buf); } else { strcpy(p->logfile,buf); strcpy(p->actfile,buf); } if (trace&T_EXF) { printf("%s=%s", p->logfile, p->actfile); if(p->stindex >=0) printf("[%ld,%ld]", p->stindex, p->enindex); printf("\n"); } return p->logfile;}/* GetFileNameExt: return true if given file has extensions and return the extend info. The problem with this routine is that the logical name can be repeated in the buffer. This is normally handled by comparing the pointer rather than the string itself. However, if the application copies the logical file name, this would break. Hence, if the pointer is not there, the name is searched for going back in time. If the name is found and it occurs more than once, a warning is printed.*/Boolean GetFileNameExt(char *logfn, char *actfn, long *st, long *en){ int i, noccs; ExtFile *p; Boolean found = FALSE; Boolean ambiguous = FALSE; /* First count number of times logfn occurs in buffer */ noccs = 0; for (i=0,p=extFiles; i<extFileUsed; i++,p++){ if (strcmp(logfn,p->logfile) == 0 ) ++noccs; } if (noccs==0) return FALSE; /* Try to find the logfn, by pointer first */ for (i=0,p=extFiles; i<extFileUsed && !found; i++){ if (logfn == p->logfile) found = TRUE; else p++; } if (!found) { /* look for actual name */ if (noccs>1) ambiguous = TRUE; p = extFiles + extFileNext; for (i=0; i<extFileUsed && !found; i++){ if (p==extFiles) p += MAXEFS; else p--; if (strcmp(logfn,p->logfile) == 0 ) found = TRUE; } } if (!found) return FALSE; /* Copy back info and warn if ambiguous */ strcpy(actfn,p->actfile); *st = p->stindex; *en = p->enindex; if (trace&T_EXF) printf("%sFile Ext found: %s=%s[%ld,%ld]\n", (ambiguous) ? "Ambiguous " : "", logfn, actfn, *st, *en); if (ambiguous) HError(-1,"GetFileNameExt: ambiguous extended file name %s=%s[%ld,%ld]", logfn, actfn, *st, *en); return TRUE;}/* --------------------- Version Display -------------------- */typedef struct _VersionEntry{ char *ver; char *sccs; struct _VersionEntry *next;}VersionEntry;static VersionEntry *vInfoHd = NULL; /* head of version info list */static VersionEntry *vInfoTl = NULL; /* tail of version info list *//* EXPORT->Register:module name with HTK version info ver and sccs info */void Register(char *ver, char *sccs){ VersionEntry *v; v = (VersionEntry *)malloc(sizeof(VersionEntry)); v->ver = (char *)malloc(strlen(ver)+1); strcpy(v->ver,ver); v->sccs = (char *)malloc(strlen(sccs)+1); strcpy(v->sccs,sccs); v->next = NULL; if (vInfoTl==NULL) vInfoHd = v; else vInfoTl->next = v; vInfoTl = v;}/* PrVInfo: print version info */static void PrVInfo(char *s,char *sccs){ char buf[MAXSTRLEN]; char *name,*ver,*who,*date,*p; strcpy(buf,s); if ((p=strrchr(buf,']')) == NULL) HError(5070,"PrVInfo: no ']' in %s",s); *p = '\0'; if ((p=strrchr(buf,'[')) == NULL) HError(5070,"PrVInfo: no '[' in %s",s); who = p+1; *p = '\0'; if (strlen(who) <8) HError(5070,"PrVInfo: who/date field too short in %s",s); if ((p=strrchr(who,' ')) == NULL) HError(5070,"PrVInfo: no space in who/date field in %s",s); date = p+1; *p = '\0'; if ((p=strrchr(buf,'!')) == NULL) HError(5070,"PrVInfo: no '!' in %s",s); name = p+1; if ((p=strchr(name,':')) == NULL) HError(5070,"PrVInfo: no ':' in %s",s); ver = p+1; *p = '\0'; while (*ver == ' ') ++ver; if ((p=strchr(ver,' ')) != NULL) *p = '\0'; printf("%-10s %-10s %-6s %-9s : %s\n",name, ver, who, date, sccs);}/* EXPORT->InfoPrinted: true if info printed by Shell */Boolean InfoPrinted(void){ VersionEntry *v; if (printVersionInfo) { printf("\nHTK Version Information\n"); PrVInfo("!HVER!Module: Version [Who Date]","CVS Info"); for (v = vInfoHd; v != NULL; v=v->next) PrVInfo(v->ver,v->sccs); printf("\n"); } return infoPrinted;}/* ------------- Configuration Parameter File Handling --------------- *//* A configuration file consists of a sequence of parameter declarations of the form [USER:]PARAM_NAME = VALUE If included, USER indicates that the parameter is only visible to the module or tool of the same name. VALUE is an integer, float or string. A string is any sequence of nonblank characters, or any sequence inside double quotes.Otherwise, the parameter is global. Unless it appears inside a string, a hash (#) indicates that the rest of the line is a comment.*/typedef struct _ConfigEntry{ ConfParam param; struct _ConfigEntry *next;}ConfigEntry;static int numConfigParms = 0;static ConfigEntry *confList = NULL;static char *cfkmap[] = { "StrCKind","IntCKind","FltCKind","BoolCKind","AnyCKind"};/* ReadConfName: read module or paramname field and cvt to ucase */static Boolean ReadConfName(Source *src, char *s){ int i,c; while (isspace(c=GetCh(src))); if (c == EOF) return FALSE; for (i=0; i<MAXSTRLEN ; i++){ if (c == EOF || isspace(c) || !isalnum(c)){ if (c==':' || c=='=') UnGetCh(c,src); s[i] = '\0'; return TRUE; } s[i] = toupper(c); c = GetCh(src); } return FALSE;}/* FindConfEntry: return entry with given name and user */static ConfigEntry *FindConfEntry(char *user, char *name){ ConfigEntry *e; char *s; for (e=confList; e!= NULL; e=e->next) if (strcmp(e->param.name,name)==0){ s = e->param.user; if (s==NULL?user==NULL:(user!=NULL && strcmp(s,user)==0) ) return e; } return NULL;}/* NumHead: returns TRUE if the first two chars of a string are ('+'|'-') digit | digit */static Boolean NumHead(char *s){ if (*s!='\0') if (isdigit(*s)) return TRUE; if (((*s=='-') || (*s=='+')) && (isdigit(*(s+1)))) return TRUE; return FALSE;}/* ParseInclude: skip comments or return #include argument */static char *ParseComment(Source *src,char *name){ const char comch = '#'; int c; char buf[MAXSTRLEN]; c = GetCh(src); while (c!=EOF && (isspace(c) || c==comch)) { if (c==comch) { src->wasNewline=FALSE; SkipWhiteSpace(src); if(src->wasNewline){ c = GetCh(src); continue; } if (ReadString(src,buf) && (strcmp(buf,"include")==0)) { if (ReadString(src,name)) { SkipLine(src); return name; } } SkipLine(src); } c = GetCh(src); } UnGetCh(c,src); return NULL;}/* ReadConfigFile: read the given configuration file */static ReturnStatus ReadConfigFile(char *fname){ double x; Source src; ConfigEntry *e; Boolean gotParam,hasUser; char c,*s,buf[32],sbuf[MAXSTRLEN]; char user[MAXSTRLEN],name[MAXSTRLEN],value[MAXSTRLEN]; static int recurse = 0; if (recurse++ > 15){ HRError(5050,"ReadConfigFile: max #include depth reached (%s)",fname); recurse--; return(FAIL); } if(InitSource(fname,&src,NoFilter)<SUCCESS){ HRError(5010,"ReadConfigFile: Can't open file %s", fname); return(FAIL); } /* skip comments and parse #include */ while (ParseComment(&src,name)!=NULL) { PathOf(name,sbuf); if (*sbuf=='\0') PathOf(fname,sbuf); strcat(sbuf,name); if(ReadConfigFile(sbuf)<SUCCESS){ recurse--; return(FAIL); } } hasUser=FALSE; gotParam = ReadConfName(&src,name); while (gotParam) { while (isspace(c=GetCh(&src))); if (c==':') { /* user field given */ hasUser = TRUE; strcpy(user,name); if (!ReadConfName(&src,name)){ HRError(5050,"ReadConfigFile: param name expected %s", SrcPosition(src,buf)); recurse--; return(FAIL); } while (isspace(c=GetCh(&src))); } if (c != '='){ HRError(5050,"ReadConfigFile: = expected %s", SrcPosition(src,buf)); recurse--; return(FAIL); } if (!ReadString(&src,value)){ HRError(5050,"ReadConfig: parameter value expected %s", SrcPosition(src,buf)); recurse--; return(FAIL); } e = FindConfEntry(hasUser?user:NULL,name); if (e==NULL){ /* new param */ e = (ConfigEntry *) malloc(sizeof(ConfigEntry)); e->next = confList; confList = e; e->param.seen = FALSE; ++numConfigParms; } if (hasUser){ e->param.user = (char *) malloc(strlen(user)+1); strcpy(e->param.user,user); }else e->param.user = NULL; e->param.name = (char *) malloc(strlen(name)+1); strcpy(e->param.name,name); if (strcmp(value,"T")==0 || strcmp(value,"TRUE")==0) { e->param.kind = BoolCKind; e->param.val.b = TRUE; } else if (strcmp(value,"F")==0 || strcmp(value,"FALSE")==0) { e->param.kind = BoolCKind; e->param.val.b = FALSE; } else if (NumHead(value)){ x = strtod(value,&s); if (s==NULL || *s == '\0'){ if (strchr(value,'.') == NULL){ e->param.kind = IntCKind; e->param.val.i = strtol(value,NULL,0); }else{ e->param.kind = FltCKind; e->param.val.f = x; } } } else { e->param.kind = StrCKind; e->param.val.s = (char *) malloc(strlen(value)+1); strcpy(e->param.val.s,value); } /* skip comments and parse #include */ while (ParseComment(&src,name)!=NULL) { PathOf(name,sbuf); if (*sbuf=='\0') PathOf(fname,sbuf); strcat(sbuf,name); if(ReadConfigFile(sbuf)<SUCCESS){ recurse--; return(FAIL); } } hasUser=FALSE; gotParam = ReadConfName(&src,name); } recurse--; return(SUCCESS);}/* EXPORT PrintConfig: print the current config params */void PrintConfig(void){ ConfigEntry *e; printf("\n"); if (numConfigParms==0) printf("No HTK Configuration Parameters Set\n"); else { printf("HTK Configuration Parameters[%d]\n",numConfigParms); printf(" %-14s %-14s %16s\n","Module/Tool","Parameter","Value"); for (e=confList; e!= NULL; e=e->next){ printf("%c %-14s %-14s ",(e->param.seen?' ':'#'), e->param.user==NULL?"":e->param.user,e->param.name); switch(e->param.kind){ case StrCKind: printf("%16s",e->param.val.s); break; case BoolCKind: printf("%16s",e->param.val.b?"TRUE":"FALSE"); break; case IntCKind: printf("%16d",e->param.val.i); break; case FltCKind: printf("%16f",e->param.val.f); break; } printf("\n"); } } printf("\n");}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -