标题: CME脚本解释器初版 [打印本页]
作者: happy886rr 时间: 2016-10-31 23:11 标题: CME脚本解释器初版
本帖最后由 happy886rr 于 2016-10-31 23:21 编辑
CME is CMD and 易 for me.模仿cmd和易语言的脚本解释器,功能不是很多,只实现了基础的命令。支持中英文编程。
比如CME的注销用户脚本可以这样写复制代码
显示文本复制代码
易代表中文编程,如果不写易,则只识别类批处理风格英文关键词- @echo off
- shutdown L
- pause
复制代码
- @echo off
- REM 移动窗口
- MW 100 300
- sleep 3000
- ps
- kl 1
- pause
复制代码
再如混合脚本- @echo off
- color 1 2 0 8
- REM 查看当前进程
- PS
- SLEEP 1500
- color 4 2 0 8
- cls
- REM 查看磁盘信息
- DISK
- SLEEP 1500
- REM 切换编程语言
- 易
- 颜色 1 4 0 8
- 移窗 500 20
- 标题 CME脚本
- 显示 CME1.0脚本处理器
- 暂停
复制代码
因时间比较紧迫,1.0版的CME暂不开放set、if、for语句、goto语句还有点小问题还在测试。以后将提供兼容50%以上的标准cmd脚本,并提供一个cme编译器。目前支持的命令全部为集成命令。部分函数版权归原作者所有,如bmp、磁盘信息统计、一些windowsAPI函数版权归微软、还有一些参考的函数无法追忆版权归初始作者所有,除此之外剩余的70%的代码版权归本人所有。
部分功能请自行测试,主要支持双语编程,echo的速度比cmd的快一倍。
外链地址,图片存为a.zip解压即是。
源码- /*
- COPYRIGHT@2016~2018 BY HAPPY
- CME Script
- VERSION 1.0
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <locale.h>
- #include <stdbool.h>
- #include <conio.h>
- #include <direct.h>
- #include <tchar.h>
- #include <time.h>
- #include<TlHelp32.h>
-
- #define BUFF_SIZE 4096
- #define CHECK_SIZE 16383
- #define FOLLOW_SIZE 1000
- #define FOLLOW_LINE 3
- #define FOLLOW_WAIT 20
-
- //全局变量
- int fsize=0, position=0, FLAG=255, ECHOFF=0, ERRORLEVEL=0, L_SN=0, E_LANGUAGE=0;
- int LAB[];
- char* RAMread;
- char ARG[16][255];
-
- //类型定义
- typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)();
- PROCGETCONSOLEWINDOW GetConsoleWindow;
-
- //GETOPT参数变量
- int OPTIND=1,OPTOPT;
- char *OPTARG;
-
- //添加关键词条目(请用小写定义),解释时不区分大小写。
- static const char* SENSITIVE_WORDS[] ={"echo", "cls", "pause", "exit", "title", "mode con", "pos", "color", "if", "for", "set", "goto", "set/p", "set/a", "ls", "ps", "kl", "tl", "bmp", "mw", "rem", "@echo off", "mouse", "disk", "shutdown", "#$~*&NULL", "#$~*&NULL", "sleep", "易"};
- static const char* SENSITIVE_CN_WORDS[]={"显示", "清屏", "暂停", "退出", "标题", "窗体", "位置", "颜色", "如果", "循环", "设置", "跳转", "输入", "计算", "目录", "进程", "杀死", "显文", "位图", "移窗", "注释", "关闭回显", "鼠标", "磁盘", "关机", "注销", "重启", "睡眠"};
- //限制关键词数量
- #define SENSITIVE_NUM 29
-
- /***************解析函数群***************/
- //关键词解析函数
- inline int Identify_KeyWords()
- {
- int i, SN;
- if(E_LANGUAGE==1){
- for(SN=0; SN<SENSITIVE_NUM-1; SN++){
- for(i=0; SENSITIVE_CN_WORDS[SN][i]!='\0'; i++){
- if(
- RAMread[position+i] !=SENSITIVE_CN_WORDS[SN][i] &&
- RAMread[position+i]+32!=SENSITIVE_CN_WORDS[SN][i]
-
- ){
- break;
- }
- }
- if(
- (SENSITIVE_CN_WORDS[SN][i]=='\0' && FLAG!=0) &&
- (
- (RAMread[position+i]== ' ') ||
- (RAMread[position+i]== '.') ||
- (RAMread[position+i]== ';') ||
- (RAMread[position+i]=='\r') ||
- (RAMread[position+i]=='\n') ||
- (RAMread[position+i]=='\t') ||
- (RAMread[position+i]== '&') ||
- (position+i==fsize)
- )
- ){
- position+=i;
- return SN;
- }
- }
- return 255;
- }
- for(SN=0; SN<SENSITIVE_NUM; SN++){
- for(i=0; SENSITIVE_WORDS[SN][i]!='\0'; i++){
- if(
- RAMread[position+i] !=SENSITIVE_WORDS[SN][i] &&
- RAMread[position+i]+32!=SENSITIVE_WORDS[SN][i]
-
- ){
- break;
- }
- }
- if(
- (SENSITIVE_WORDS[SN][i]=='\0' && FLAG!=0) &&
- (
- (RAMread[position+i]== ' ') ||
- (RAMread[position+i]== '.') ||
- (RAMread[position+i]== ';') ||
- (RAMread[position+i]=='\r') ||
- (RAMread[position+i]=='\n') ||
- (RAMread[position+i]=='\t') ||
- (RAMread[position+i]== '&') ||
- (position+i==fsize)
- )
- ){
- position+=i;
- return SN;
- }
- }
- return 255;
- }
- //切分解析函数
- inline int Analytic_Spli()
- {
- int i=0;
- for(position++; position<fsize; position++){
- if(
- (RAMread[position]=='\r') ||
- (RAMread[position]=='\n') ||
- (RAMread[position]=='\0') ||
- (RAMread[position]== '&') ||
- (RAMread[position]== '|')
- ){
- break;
- }
- i++;
- }
- return i;
- }
- //命令行参数拆分
- int Split_Argv()
- {
- int i, j=0, L=0, MARK1=0, MARK2=0, AC=0;
- if((L=Analytic_Spli())<=1){return 0;}
- i=position-L;
- while(RAMread[i]==' '||RAMread[i]=='\t'){
- i++;
- }
- for(; i<position; i++){
- if(
- (RAMread[i]!= ' ') &&
- (RAMread[i]!='\t') &&
- (MARK1==0)
- ){
- if(RAMread[i]=='"'){
- i++, MARK2=1;
- }
- MARK1=1;
- }else if(
- (RAMread[i]== ' ')||
- (RAMread[i]=='\t')||
- (RAMread[i]== '"')
- ){
- if(
- (MARK1==1) &&
- (MARK2==0) &&
- (RAMread[i]!='"')
- ){
- ARG[AC][j]='\0';
- j=0, MARK1=0, AC++;
- }
- if(
- (RAMread[i]=='"') &&
- (MARK2==1)
- ){
- MARK2=0;
- }
- }
- if(
- (MARK1==1) &&
- (RAMread[i]!='"')
- ){
- ARG[AC][j++]=RAMread[i];
- }
- }
- return AC+1;
- }
-
- /***************命令函数群***************/
- //打印函数
- inline int Shell_ECHO()
- {
- int i=Analytic_Spli();
- fwrite(RAMread+position-i, i, 1, stdout);
- if(i){fputs("\r\n", stdout);}
- return 0;
- }
- //REM函数
- inline int Shell_REM()
- {
- if(ECHOFF==0){
- position--;
- fputs("REM", stdout);
- Shell_ECHO();
- return 1;
- }
- Analytic_Spli();
- return 0;
- }
- //清屏函数
- int Shell_CLS()
- {
- HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
- COORD coordScreen={0,0};
- DWORD cCharsWritten;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(hConsole, &csbi);
- FillConsoleOutputCharacter(hConsole, (TCHAR)' ', csbi.dwSize.X*csbi.dwSize.Y, coordScreen, &cCharsWritten);
- GetConsoleScreenBufferInfo(hConsole, &csbi);
- FillConsoleOutputAttribute(hConsole, csbi.wAttributes, csbi.dwSize.X*csbi.dwSize.Y, coordScreen, &cCharsWritten);
- SetConsoleCursorPosition(hConsole,coordScreen);
- return 0;
- }
- //暂停函数
- int Shell_PAUSE()
- {
- if(ECHOFF==0){
- if(E_LANGUAGE=0){
- fputs("\r\nPAUSE...", stdout);
- }else{
- fputs("\r\n请按任意键继续...", stdout);
- }
- }
- getch();
- return 0;
- }
- //退出函数
- int Shell_EXIT(int Exit_Code)
- {
- exit(Exit_Code);
- }
- //标题函数
- int Shell_TITLE(char* Str)
- {
- SetConsoleTitle(Str);
- return 0;
- }
- //跳转函数
- int Shell_GOTO(int len)
- {
- int i, j;
- for(j=0; j<L_SN; j++){
- for(i=0; i<len; i++){
- printf("%c#####%c\n",RAMread[position-len+i], RAMread[LAB[j]+i]);
- RAMread[position-len+i]!=RAMread[LAB[j]+i];
- break;
- }
- if(i==len){
- position=RAMread[LAB[j]];
- return 0;
- }
- }
- fputs("Can not find label :" ,stdout);
- fwrite(RAMread+position-len, len, 1, stdout);
- return 1;
- }
- //光标函数
- int Shell_POS(int x,int y)
- {
- COORD pos;
- HANDLE hOutput;
- pos.X=x;
- pos.Y=y;
- hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hOutput,pos);
- return 0;
- }
- //睡眠函数
- int Shell_SLEEP(int t)
- {
- Sleep(t);
- return 0;
- }
- //按键函数
- int Shell_KEY(clock_t delay)
- {
- int i,KEY_V,start=clock();
- do{
- if(kbhit()){
- KEY_V=(int)(getch());
- if(KEY_V<97){KEY_V+=32;}
- return KEY_V;
- }
- for(i=0;i<=50;i++);
- }while((clock()-start)<delay);
- return -1;
- }
- //鼠标点击函数:KEY_V取值1、2对应鼠标左右键点击;当取值为0时则隐藏光标;
- int Shell_MOUSE(int KEY_V)
- {
- if(KEY_V==0){
- CONSOLE_CURSOR_INFO cursor_info={1,0};
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
- return 0;
- }
- HANDLE StdIn=GetStdHandle(STD_INPUT_HANDLE);
- DWORD OrgMode, Res;
- INPUT_RECORD InR;
- GetConsoleMode(StdIn, &OrgMode);
- SetConsoleMode(StdIn, OrgMode | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
- for(;;){
- ReadConsoleInput(StdIn, &InR, 1, &Res);
- if(
- InR.Event.MouseEvent.dwEventFlags ==0 &&
- InR.Event.MouseEvent.dwButtonState==KEY_V
- ){
- SetConsoleMode(StdIn, OrgMode);
- return (InR.Event.MouseEvent.dwMousePosition.Y)*1000+(InR.Event.MouseEvent.dwMousePosition.X);
- }
- }
- }
- //窗口大小
- int Shell_MODE_CON(int x, int y)
- {
-
- HANDLE StdOut=GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO scbi;
- COORD size={x, 300};
- GetConsoleScreenBufferInfo(StdOut, &scbi);
- SetConsoleScreenBufferSize(StdOut, size);
- SMALL_RECT rc={0, 0, x-1, y-1};
- SetConsoleWindowInfo(StdOut, 1, &rc);
- CloseHandle(StdOut);
- return 0;
- }
- //列举文件
- int Shell_LS(char* path)
- {
- CHAR szFilePath[MAX_PATH];
- HANDLE hListFile;
- WIN32_FIND_DATA fileData;
- lstrcpy(szFilePath, path);
- lstrcat(szFilePath, "\\*");
- hListFile = FindFirstFile(szFilePath,&fileData);
- if(hListFile == INVALID_HANDLE_VALUE){return 1;}
- printf("FILE LIST:\r\n");
- do{
- printf("\t%s\r\n", fileData.cFileName );
- }while(FindNextFile(hListFile, &fileData));
- return 0;
- }
- //列举进程
- int Shell_PS()
- {
- HANDLE hSnapshot;
- HANDLE hProcess;
- PROCESSENTRY32 pe32;
- hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
- if( hSnapshot == INVALID_HANDLE_VALUE )
- {
- printf( "CreateToolhelp32Snapshot (of processes) failed\n" );
- return 1;
- }
- pe32.dwSize = sizeof( PROCESSENTRY32 );
- if( !Process32First( hSnapshot, &pe32 ) )
- {
- printf( "Process32First() failed\n" );
- CloseHandle( hSnapshot );
- return 1;
- }
- printf(" [PROCESS] [PID] [PPID] [THRE] [PRI]\r\n---------------------------------------------------------------\r");
- do{
- printf( "\n%25s", pe32.szExeFile );
- printf( "%8u", pe32.th32ProcessID );
- printf( "%8u", pe32.th32ParentProcessID );
- printf( "%8d", pe32.cntThreads );
- printf( "%8d", pe32.pcPriClassBase );
- }while( Process32Next( hSnapshot, &pe32 ) );
- CloseHandle( hSnapshot );
- fputc('\n',stdout);
- return 0;
- }
- //结束进程
- int Shell_KL(int ProcessID)
- {
- HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)ProcessID);
- TerminateProcess(hProcess, 0);
- return 0;
- }
- //字体颜色: 1,2,4,8前景;16,32,64,128后景;
- int Shell_COLOR(unsigned short A,unsigned short B,unsigned short C,unsigned short D)
- {
- HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(handle_out, &csbi);
- SetConsoleTextAttribute(handle_out, A | B | C | D);
- return 0;
- }
- //位图显示
- int Shell_BMP(
-
- char* file , //位图文件;
- int x,int y, //相对窗口起始坐标x,y;
- int W,int H, //要粘贴的宽度高度W,H;
- int X,int Y //相对缓存DC的坐标X,Y;
- )
- {
- HMODULE hKernel32=GetModuleHandle("kernel32");
- GetConsoleWindow=(PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow");
- HWND cmd=GetConsoleWindow();
- HDC dc=GetDC(cmd);
- HBITMAP hBitmap;
- hBitmap=(HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
- HDC cmdmem=CreateCompatibleDC(dc);
- SelectObject(cmdmem, hBitmap);
- BitBlt(dc, x, y, W, H, cmdmem, X, Y, SRCCOPY);
- return 0;
- }
- //移动窗口
- int Shell_MW(int x, int y)
- {
- HMODULE hKernel32 = GetModuleHandle("kernel32");
- GetConsoleWindow= (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow");
- HWND cmd=GetConsoleWindow();
- SetWindowPos(cmd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE);
- return 0;
- }
- //关机函数S取值r、l、p、空;对应重启、注销、关电源、关机;
- int Shell_SHUTDOWN(char S)
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
- if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)){
- return 1;
- }
- LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
- tkp.PrivilegeCount=1;
- tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
- if(GetLastError() !=ERROR_SUCCESS){
- return 1;
- }
- if (S=='r'||S=='R'){
- ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
- }else if(S=='l'||S=='L'){
- ExitWindowsEx(EWX_LOGOFF | EWX_FORCE, 0);
- }else if(S=='p'||S=='P'){
- ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0);
- }else{
- ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
- }
- return 0;
- }
- //磁盘信息
- void utoiRightJustified(TCHAR* szLeft, TCHAR* szRight, unsigned uVal)
- {
- TCHAR* szCur = szRight;
- int nComma = 0;
- if (uVal) {
- while (uVal && (szCur >= szLeft)) {
- if (nComma == 3) {
- *szCur = ',';
- nComma = 0;
- }else {
- *szCur = (uVal % 10) | 0x30;
- uVal /= 10;
- ++nComma;
- }
- --szCur;
- }
- } else {
- *szCur = '0';
- --szCur;
- }
-
- if (uVal) {
- szCur = szLeft;
- while (szCur <= szRight) {
- *szCur = '*';
- ++szCur;
- }
- }
- }
- int Shell_DISK(){
- DWORD dwDrive;
- INT nDrive;
- char dName[4] ;
- char* Driver;
- long GB = 1024*1024*1024;
- ULARGE_INTEGER Free ;
- ULARGE_INTEGER Total ;
- ULARGE_INTEGER TotalFree ;
- dwDrive = GetLogicalDrives();
- printf(
- " ------------------------------------\n"
- "│DRIVE│FREE CAPACITY│TOTAL CAPACITY│ DISK\n"
- " ------------------------------------\n"
- " ------------------------------------\n"
- );
- for ( nDrive = 0 ; nDrive < 26 ; nDrive++ ){
- if ( dwDrive & (1 << nDrive) ){
- sprintf(dName,"%c:",(nDrive + 'A'));
- Driver =(char*)dName;
- if(GetDiskFreeSpaceEx(Driver, &Free, &Total, &TotalFree)){
- printf("│ %s │ %3I64u GB │ %3I64u GB │\n",Driver,Free.QuadPart/GB,Total.QuadPart/GB);
- }
- }
- }
- printf(" ------------------------------------\n");
- TCHAR g_szBorder[] = _T(" ------------------------------------------------------------------------\n");
- TCHAR g_szTitle1[] = _T("│DRIVE│TOTAL CLUSTERS│AVAIL CLUSTERS│SECTORS / CLUSTER│BYTES / SECTOR│\n");
- TCHAR g_szTitle2[] = _T(" ------------------------------------------------------------------------\n ------------------------------------------------------------------------\n");
- TCHAR g_szLine[] = _T("│ : │ │ │ │ │\n");
- TCHAR szMsg[4200];
- struct _diskfree_t df = {0};
- ULONG uDriveMask = _getdrives();
- unsigned uErr, uLen, uDrive;
- printf(g_szBorder);
- printf(g_szTitle1);
- printf(g_szTitle2);
- for (uDrive=1; uDrive<=26; ++uDrive) {
- if (uDriveMask & 1) {
- uErr = _getdiskfree(uDrive, &df);
- memcpy(szMsg, g_szLine, sizeof(g_szLine));
- szMsg[3] = uDrive + 'A' - 1;
- char lp[5] = "C://";
- lp [0] = uDrive + 'A' - 1;
- if (uErr == 0) {
- utoiRightJustified(szMsg+8, szMsg+19, df.total_clusters);
- utoiRightJustified(szMsg+23, szMsg+34, df.avail_clusters);
- utoiRightJustified(szMsg+38, szMsg+52, df.sectors_per_cluster);
- utoiRightJustified(szMsg+56, szMsg+67, df.bytes_per_sector);
- } else {
- uLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, uErr, 0, szMsg+8, 4100, NULL);
- szMsg[uLen+6] = ' ';
- szMsg[uLen+7] = ' ';
- szMsg[uLen+8] = ' ';
- }
- printf(szMsg);
- }
- uDriveMask >>= 1;
- if (!uDriveMask)
- break;
- }
- printf(g_szBorder);
- return 0;
- }
-
- /***************业务函数群***************/
- //参数解析函数
- int GETOPT(int nargc, char *nargv[], char *ostr)
- {
- static char* place="";
- static char* lastostr=(char *) 0;
- register char* oli;
- char* index();
- if(ostr!=lastostr){
- lastostr=ostr;
- place="";
- }
- if(!*place){
- if(
- (OPTIND>=nargc) ||
- (*(place=nargv[OPTIND])!='/')||
- (! *++place)
- ){
- place="";
- return(EOF);
- }
- if (*place == '/' && place[1] == 0){
- ++OPTIND;
- return(EOF);
- }
- }
- if ((OPTOPT=(int)*place++)==(int)':' || !(oli=strchr(ostr, OPTOPT))){
- if(!*place){++OPTIND;}
- }
- if (*++oli!=':'){
- OPTARG=NULL;
- if(!*place){++OPTIND;}
- }
- else{
- if(*place){
- OPTARG=place;
- }else if(nargc<=++OPTIND){
- place="";
- }else{
- OPTARG=nargv[OPTIND];
- }
- place="";
- ++OPTIND;
- }
- return(OPTOPT);
- }
- char* UnicodeToANSI(const wchar_t* Str)
- {
- int L=WideCharToMultiByte(CP_ACP, 0, Str, -1, NULL, 0, NULL, NULL);
- char* Out=(char *)calloc(L+1, sizeof(char));
- WideCharToMultiByte(CP_ACP, 0, Str, -1, Out, L, NULL, NULL);
- return Out;
- }
- wchar_t* UTF8ToUnicode(const char* Str)
- {
- int L=MultiByteToWideChar(CP_UTF8, 0, Str,-1, NULL, 0);
- wchar_t* Out=(wchar_t *)calloc(L+1, sizeof(wchar_t));
- MultiByteToWideChar(CP_UTF8, 0, Str, -1, (LPWSTR)Out, L);
- return Out;
- }
- wchar_t* BIG5ToUnicode(const char* Str)
- {
- int L=MultiByteToWideChar(950, 0, Str,-1, NULL, 0);
- wchar_t* Out=(wchar_t *)calloc(L+1, sizeof(wchar_t));
- MultiByteToWideChar(950, 0, Str, -1, (LPWSTR)Out, L);
- return Out;
- }
- bool isUTF8(const char* Str)
- {
- if(!Str){
- return false;
- }
- const unsigned char* bytes=(const unsigned char *)Str;
- while(*bytes){
- if(
- (
- bytes[0]<=0x7F ||
- bytes[0]==0x09 ||
- bytes[0]==0x0A ||
- bytes[0]==0x0D ||
- (0x20<=bytes[0] && bytes[0]<=0x7E)
- )
- ){
- bytes+=1;
- continue;
- }
- if(
- (
- (0xC2<=bytes[0] && bytes[0]<=0xDF) &&
- (0x80<=bytes[1] && bytes[1]<=0xBF)
- )
- ){
- bytes+=2;
- continue;
- }
- if(
- (
- (bytes[0]==0xE0) &&
- (0xA0<=bytes[1] && bytes[1]<=0xBF) &&
- (0x80<=bytes[2] && bytes[2]<=0xBF)
- ) ||
- (
- (
- (0xE1<=bytes[0] && bytes[0]<=0xEC)||
- bytes[0]==0xEE ||
- bytes[0]==0xEF
- ) &&
- (0x80<=bytes[1] && bytes[1]<=0xBF) &&
- (0x80<=bytes[2] && bytes[2]<=0xBF)
- ) ||
- (
- (bytes[0]==0xED) &&
- (0x80<=bytes[1] && bytes[1]<=0x9F) &&
- (0x80<=bytes[2] && bytes[2]<=0xBF)
- )
- ){
- bytes+=3;
- continue;
- }
- if(
- (
- (bytes[0]==0xF0) &&
- (0x90<=bytes[1] && bytes[1]<=0xBF) &&
- (0x80<=bytes[2] && bytes[2]<=0xBF) &&
- (0x80<=bytes[3] && bytes[3]<=0xBF)
- ) ||
- (
- (0xF1<=bytes[0] && bytes[0]<=0xF3) &&
- (0x80<=bytes[1] && bytes[1]<=0xBF) &&
- (0x80<=bytes[2] && bytes[2]<=0xBF) &&
- (0x80<=bytes[3] && bytes[3]<=0xBF)
- ) ||
- (
- (bytes[0]==0xF4) &&
- (0x80<=bytes[1] && bytes[1]<=0x8F) &&
- (0x80<=bytes[2] && bytes[2]<=0xBF) &&
- (0x80<=bytes[3] && bytes[3]<=0xBF)
- )
- ){
- bytes+=4;
- continue;
- }
- return false;
- }
- return true;
- }
- bool isGB2312(const char* Str)
- {
- if(!Str){
- return false;
- }
- const unsigned char* bytes=(const unsigned char *)Str;
- while(*bytes){
- if(
- (
- bytes[0]<=0x7F ||
- bytes[0]==0x09 ||
- bytes[0]==0x0A ||
- bytes[0]==0x0D ||
- (0x20<=bytes[0] && bytes[0]<=0x7E)
- )
- ){
- bytes+=1;
- continue;
- }
- if(
- (0xA1<=bytes[0] && bytes[0]<=0xF7) &&
- (0xA1<=bytes[1] && bytes[1]<=0xFE)
- ){
- bytes+=2;
- continue;
- }
- return false;
- }
- return true;
- }
- bool isBIG5(const char* Str)
- {
- if(!Str){
- return false;
- }
- const unsigned char* bytes=(const unsigned char *)Str;
- while(*bytes){
- if(
- (
- bytes[0]<=0x7F ||
- bytes[0]==0x09 ||
- bytes[0]==0x0A ||
- bytes[0]==0x0D ||
- (0x20<=bytes[0] && bytes[0]<=0x7E)
- )
- ){
- bytes+=1;
- continue;
- }
- if(
- (0xA1<=bytes[0] && bytes[0]<=0xF9) &&
- (
- (0x40<=bytes[1] && bytes[1]<=0x7E) ||
- (0xA1<=bytes[1] && bytes[1]<=0xFE)
- )
- ){
- bytes+=2;
- continue;
- }
- return false;
- }
- return true;
- }
- int CheckBom(FILE* fp)
- {
- unsigned char* buf=(unsigned char*)calloc(3,sizeof(unsigned char));
- unsigned char* buf2;
- fseeko64(fp, (__int64)0, SEEK_SET);
- fread(buf, sizeof(unsigned char), 3, fp);
- if(buf[0]==0xEF && buf[1]==0xBB && buf[2]==0xBF){return 3;}
- else if(buf[0]==0xFF && buf[1]==0xFE){return 5;}
- else if(buf[0]==0xFE && buf[1]==0xFF){return 6;}
- else{
- fseeko64(fp, (__int64)0, SEEK_SET);
- buf2=(unsigned char*)calloc(CHECK_SIZE,sizeof(unsigned char));
- fread(buf2, sizeof(unsigned char), CHECK_SIZE, fp);
- if(isUTF8(buf2)){
- return 2;
- }else if(isGB2312(buf2)){
- return 1;
- }else if(isBIG5(buf2)){
- return 4;
- }
- }
- return 1;
- }
- void Help_Information_Tl(FILE* stream, int a)
- {
- fprintf(stream,
- ">>>------------------------------------------------------------\n"
- "DISPLAYS THE CONTENTS OF A TEXT FILE\n"
- "VERSION 2.0\n"
- "tl [file] [-n&num1,num2]|[-p&per1,per2]|[-i]|[-d&num]\n"
- "---------------------------------------------------------------\n\n"
- " -h Show help information\n"
- " -n Read lines from num1 to num2\n"
- " -p Read lines from percent1 to percent2\n"
- " -i Show file's information\n"
- " -d Detection latest num lines \n"
- "---------------------------------------------------------------\n"
- );
- return;
- }
- int Getkey(int N,int T)
- {
- int i,KEY_V,start=clock();
- do{
- if(kbhit()){
- KEY_V=(int)(getch());
- if(KEY_V<97){KEY_V+=32;}
- return KEY_V;
- }
- for(i=0;i<=N;i++);
- }while((clock()-start)<T);
- return -1;
- }
- int CountLines(FILE* fp)
- {
- int i=0;
- char* line=(char *)malloc(BUFF_SIZE*sizeof(char));
- while(!feof(fp)){
- fgets(line, BUFF_SIZE, fp);
- i++;
- }
- return i;
- }
- void File_Information(FILE* fp, char* fname)
- {
- fseeko64(fp, (__int64)0, SEEK_END);
- __int64 fsize=ftello64(fp);
- fseeko64(fp, (__int64)0, SEEK_SET);
- int linenum=CountLines(fp);
- fprintf(stdout,
- "FILE NAME : %s\n"
- "FILE SIZE : %I64d\n"
- "FILE LINES: %d\n"
- ,fname, fsize, linenum
- );
- }
- int DisplayLine(FILE* fp, int flag, int N1, int N2, __int64 F1, __int64 F2)
- {
- int i=0, n=0, BOM=0, EN=0;
- BOM=CheckBom(fp);
- if(BOM==1 || BOM==2 || BOM==4){
- EN=0;
- }else if(BOM==5 || BOM==6){
- EN=2;
- }else if(BOM==3){
- EN=3;
- }
- if (flag==0){
- fseeko64(fp, (__int64)EN, SEEK_SET);
- }else if(flag==1||flag==3){
- __int64 FD=(N1*BUFF_SIZE>F2)?F2:N1*BUFF_SIZE;
- fseeko64(fp, -FD, SEEK_END);
- N1=CountLines(fp)-N1+1;
- fseeko64(fp, -FD, SEEK_END);
- }else if(flag==2){
- fseeko64(fp, F1, SEEK_SET);
- }
- if(BOM<5){
- char* Line=(char *)malloc(BUFF_SIZE*sizeof(char));
- while(!feof(fp)||flag==3){
- memset(Line, 0, BUFF_SIZE*sizeof(char));
- if(!fgets(Line, BUFF_SIZE, fp)){Sleep(1);}
- i++;
- if( ((N1<=i) && (i<=N2) && (F1<=ftello64(fp)) && (ftello64(fp)<=F2))||(flag==3) ){
- switch(BOM){
- case 1: //Ansi行显
- fputs(Line, stdout);
- break;
- case 2: //Utf8无BOM行显
- case 3: //Utf8行显
- fputs(UnicodeToANSI(UTF8ToUnicode(Line)), stdout);
- break;
- case 4: //Big5行显
- fputs(UnicodeToANSI(BIG5ToUnicode(Line)), stdout);
- break;
- }
- }else if((i>N2) && (ftello64(fp)>=F2)){
- break;
- }
- }
- }else if(BOM==5){ //Unicode行显
- wchar_t* LineW=(wchar_t *)calloc(BUFF_SIZE, sizeof(wchar_t));
- while(!feof(fp)||(flag==3)){
- memset(LineW, 0, BUFF_SIZE*sizeof(wchar_t));
- if(!fgetws(LineW, BUFF_SIZE, fp)){Sleep(1);}
- i++;
- if( ((N1<=i) && (i<=N2) && (F1<=ftello64(fp)) && (ftello64(fp)<=F2))||(flag==3) ){
- fputs(UnicodeToANSI(LineW), stdout);
- }else if((i>N2) && (ftello64(fp)>=F2)){
- break;
- }
- }
- }else if(BOM==6){ //Unicode big endian行显
- wchar_t* LineW=(wchar_t *)calloc(BUFF_SIZE, sizeof(wchar_t));
- while(!feof(fp)||(flag==3)){
- memset(LineW, 0, BUFF_SIZE*sizeof(wchar_t));
- if(!fgets(LineW, BUFF_SIZE, fp)){Sleep(1);}
- i++;
- if( ((N1<=i) && (i<=N2) && (F1<=ftello64(fp)) && (ftello64(fp)<=F2))||(flag==3) ){
- for(n=0;LineW[n]!=0x0000;n++){
- LineW[n]=(LineW[n]&0x00FF)<<8|(LineW[n]&0xFF00)>>8;
- }
- fputs(UnicodeToANSI(LineW), stdout);
- }else if((i>N2) && (ftello64(fp)>=F2)){
- break;
- }
- }
- }
- fflush(stdout);
- return 0;
- }
- //TL函数入口
- int Shell_TL(int argc)
- {
- int N1=1, N2=2147483631, FLAG=0, i;
- __int64 fsize=0;
- float P1=0.0, P2=1.0;
- FILE* fp;
- char* delims;
- if((argc==2) && (ARG[1][0]=='-')){
- switch(ARG[1][1]){
- case 'H':
- case 'h':
- Help_Information_Tl(stdout, 0);
- return 1;
- case 'N':
- case 'n':
- delims=(ARG[1]+2);
- N1=atoi(strtok(delims, ","));
- N2=atoi(strtok(NULL, ","));
- if(N2==0){N2=2147483631;}
- if((N1>N2) || (N1<0) || (N2<0)){Help_Information_Tl(stderr, 1);return 1;}
- break;
- case 'P':
- case 'p':
- delims=(ARG[1]+2);
- P1=atof(strtok(delims, ","));
- P2=atof(strtok(NULL, ","));
- if((P1>=P2) || (P1>1.0) || (P2>1.0) || (P1<0.0) || (P2<0.0) ){Help_Information_Tl(stderr, 1);return 1;}
- FLAG=2;
- break;
- case 'I':
- case 'i':
- if( (fp=fopen64(ARG[0], "rb"))==NULL ){fputs("Read failed.", stdout);return 3;}
- File_Information(fp, ARG[0]);
- fclose(fp);
- return 0;
- case 'D':
- case 'd':
- delims=(ARG[1]+2);
- N1=abs(atoi(delims)), N1=(N1>FOLLOW_SIZE)?1000:N1, FLAG=1;
- if(ARG[1][2]=='\0'){N1=FOLLOW_LINE,FLAG=3;}
- break;
- default:
- Help_Information_Tl(stderr, 2);return 1;
- }
- }else if(argc!=1){
- Help_Information_Tl(stderr, 3);return 1;
- }
- if( (fp=fopen64(ARG[0], "rb"))==NULL ){
- fputs("Read failed.", stdout);
- return 3;
- }
- if(FLAG==1){
-
- do{
- fseeko64(fp, (__int64)0, SEEK_END);
- if( fsize!=ftello64(fp)){
- fsize =ftello64(fp);
- system("cls");
- DisplayLine(fp, 1, N1, 2147483631, 0, fsize);
- }
- }while(Getkey(64,FOLLOW_WAIT)!=113);
- fputs("\n", stdout);
- fclose(fp);
- return 0;
- }
- fseeko64(fp, (__int64)0, SEEK_END);
- fsize=ftello64(fp);
- DisplayLine(fp, FLAG, N1, N2, fsize*P1, fsize*P2);
- fclose(fp);
- return 0;
- }
- //帮助信息
- void Help_Information(FILE* stream, int Exit_Code)
- {
- fprintf(stream,
- "==================================================\n"
- "CME [VERSION 1.0]\n"
- "==================================================\n"
- " [ECHO] Display string\n"
- " [CLS] Clear screen\n"
- " [PAUSE] Time out\n"
- " [EXIT ] Drop out\n"
- " [TITLE] Set the title\n"
- " [MODE CON] Window settings\n"
- " [POS] Set the Cursor position\n"
- " [COLOR] Font color\n"
- " [GOTO] Tag jumps\n"
- " [LS] List the specified directory files\n"
- " [PS] List the processes\n"
- " [KL] End a process\n"
- " [CAT] View a txt file\n"
- " [BMP] Displays a bitmap\n\n"
- "==================================================\n"
- " 1029TE0\n"
- );
- exit(Exit_Code);
- }
- //脚本解释函数
- int CME()
- { int i=0;
- while(i++<fsize){
- if(
- (RAMread[i]== ' ') ||
- (RAMread[i]=='\r') ||
- (RAMread[i]=='\n') ||
- (RAMread[i]=='\t') ||
- (RAMread[i]== '&') ||
- (RAMread[i]== '|') ||
- (RAMread[i]=='\0')
- ){
- continue;
- }
- if(RAMread[i]==':'){
- i+=Analytic_Spli();
- LAB[L_SN++]=i;
- }
- }
- position=0;
- while(position<fsize){
- if(
- (RAMread[position]== ' ') ||
- (RAMread[position]=='\r') ||
- (RAMread[position]=='\n') ||
- (RAMread[position]=='\t') ||
- (RAMread[position]== '&') ||
- (RAMread[position]== '|') ||
- (RAMread[position]=='\0')
- ){
- position++;FLAG=255;
- if(RAMread[position]==':'){
- Analytic_Spli();
- LAB[L_SN++]=position;
- }
- continue;
- }
- FLAG=Identify_KeyWords(position);
- switch(FLAG){
- case 255:
- printf("ERROR [At this time should not have \"");
- fwrite(RAMread+position, 12, 1, stdout);
- fputs(" \"]\n", stdout);
- Shell_PAUSE();
- return 1;
- case 0:
- Shell_ECHO();
- break;
- case 1:
- Shell_CLS();
- break;
- case 2:
- Shell_PAUSE();
- break;
- case 3:
- Shell_EXIT(0);
- break;
- case 4:
- if(Split_Argv()==1){
- Shell_TITLE(ARG[0]);
- }
- break;
- case 5:
- if(Split_Argv()==2){
- Shell_MODE_CON(atoi(ARG[0]), atoi(ARG[1]));
- }
- break;
- case 6:
- if(Split_Argv()==2){
- Shell_POS(atoi(ARG[0]), atoi(ARG[1]));
- }
- break;
- case 7:
- if(Split_Argv()==4){
- Shell_COLOR(atoi(ARG[0]), atoi(ARG[1]), atoi(ARG[2]), atoi(ARG[3]));
- }else{
- Shell_COLOR(1,2,4,8);
- }
- break;
- case 8:
- //Shell_IF();
- break;
- case 9:
- //Shell_FOR();
- break;
- case 10:
- //Shell_SET();
- break;
- case 11:
- //Shell_GOTO(Analytic_Spli());
- break;
- case 12:
- //Shell_SET/P();
- break;
- case 13:
- //Shell_SET/A();
- break;
- case 14:
- if(Split_Argv()==1){
- Shell_LS(ARG[0]);
- }else{
- Shell_LS(".");
- }
- break;
- case 15:
- Shell_PS();
- break;
- case 16:
- if(Split_Argv()==1){
- Shell_KL(atoi(ARG[0]));
- }
- break;
- case 17:
- Shell_TL(Split_Argv());
- break;
- case 18:
- if(Split_Argv()==7){
- Shell_BMP(ARG[0], atoi(ARG[1]), atoi(ARG[2]), atoi(ARG[3]), atoi(ARG[4]), atoi(ARG[5]), atoi(ARG[6]));
- }else{
- Shell_BMP(ARG[0], 100, 100, 300, 300, 0, 0);
- }
- break;
- case 19:
- if(Split_Argv()==2){
- Shell_MW(atoi(ARG[0]), atoi(ARG[1]));
- }
- break;
- case 20:
- Shell_REM();
- break;
- case 21:
- ECHOFF=1;
- break;
- case 22:
- if(Split_Argv()==1){
- Shell_MOUSE(atoi(ARG[0]));
- }
- break;
- case 23:
- Shell_DISK();
- break;
- case 24:
- if(Split_Argv()==0){
- Shell_SHUTDOWN('\0');
- }else{
- Shell_SHUTDOWN(ARG[0][0]);
- }
- break;
- case 25:
- if(Split_Argv()==0){
- Shell_SHUTDOWN('L');
- }
- break;
- case 26:
- if(Split_Argv()==0){
- Shell_SHUTDOWN('R');
- }
- break;
- case 27:
- if(Split_Argv()==1){
- Shell_SLEEP(atoi(ARG[0]));
- }
- break;
- case 28:
- E_LANGUAGE=1;
- break;
- }
- }
- return 0;
- }
-
- /*************MAIN主函数入口*************/
- int main(int argc, char** argv)
- {
- if(argc!=2){Help_Information(stderr, 2);}
- Shell_TITLE("CME Script");
- FILE* fp;
- if( (fp=fopen(argv[1], "rb"))==NULL ){
- fputs("Read Error.\n", stdout);
- }
- //测量尺寸
- fseek(fp, 0, SEEK_END);
- if( (fsize=ftell(fp))>2*1024*1024 ){
- fputs("The file is too large, out of memory.\n", stdout);
- return 1;
- }
- fseek(fp, 0, SEEK_SET);
- //压入内存
- RAMread=(char*)calloc(fsize, sizeof(char));
- fread(RAMread, fsize, 1, fp);
- fclose(fp);
- //解析脚本
- CME();
- free(RAMread);
- return 0;
- }
复制代码
作者: yishuigege 时间: 2016-11-1 09:03
为楼主喝彩!
作者: codegay 时间: 2016-11-1 15:49
这么NB是要上天啊。
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |