Board logo

标题: CME脚本解释器初版 [打印本页]

作者: happy886rr    时间: 2016-10-31 23:11     标题: CME脚本解释器初版

本帖最后由 happy886rr 于 2016-10-31 23:21 编辑

CME is CMD and 易 for me.模仿cmd和易语言的脚本解释器,功能不是很多,只实现了基础的命令。支持中英文编程。
比如CME的注销用户脚本可以这样写
  1. 注销
  2. 暂停
复制代码
显示文本
  1. 显文 "要显示的.txt"
  2. 暂停
复制代码
易代表中文编程,如果不写易,则只识别类批处理风格英文关键词
  1. @echo off
  2. shutdown L
  3. pause
复制代码
  1. @echo off
  2. REM 移动窗口
  3. MW 100 300
  4. sleep 3000
  5. ps
  6. kl 1
  7. pause
复制代码
再如混合脚本
  1. @echo off
  2. color 1 2 0 8
  3. REM 查看当前进程
  4. PS
  5. SLEEP 1500
  6. color 4 2 0 8
  7. cls
  8. REM 查看磁盘信息
  9. DISK
  10. SLEEP 1500
  11. REM 切换编程语言
  12. 颜色 1 4 0 8
  13. 移窗 500 20
  14. 标题 CME脚本
  15. 显示 CME1.0脚本处理器
  16. 暂停
复制代码
因时间比较紧迫,1.0版的CME暂不开放set、if、for语句、goto语句还有点小问题还在测试。以后将提供兼容50%以上的标准cmd脚本,并提供一个cme编译器。目前支持的命令全部为集成命令。部分函数版权归原作者所有,如bmp、磁盘信息统计、一些windowsAPI函数版权归微软、还有一些参考的函数无法追忆版权归初始作者所有,除此之外剩余的70%的代码版权归本人所有。
部分功能请自行测试,主要支持双语编程,echo的速度比cmd的快一倍。
外链地址,图片存为a.zip解压即是。

源码
  1. /*
  2. COPYRIGHT@2016~2018 BY HAPPY
  3. CME Script
  4. VERSION 1.0
  5. */
  6. #include   <stdio.h>
  7. #include  <stdlib.h>
  8. #include  <string.h>
  9. #include <windows.h>
  10. #include  <locale.h>
  11. #include <stdbool.h>
  12. #include   <conio.h>
  13. #include  <direct.h>
  14. #include   <tchar.h>
  15. #include    <time.h>
  16. #include<TlHelp32.h>
  17. #define BUFF_SIZE 4096
  18. #define CHECK_SIZE 16383
  19. #define FOLLOW_SIZE 1000
  20. #define FOLLOW_LINE 3
  21. #define FOLLOW_WAIT 20
  22. //全局变量
  23. int fsize=0, position=0, FLAG=255, ECHOFF=0, ERRORLEVEL=0, L_SN=0, E_LANGUAGE=0;
  24. int LAB[];
  25. char* RAMread;
  26. char ARG[16][255];
  27. //类型定义
  28. typedef HWND (WINAPI *PROCGETCONSOLEWINDOW)();
  29. PROCGETCONSOLEWINDOW GetConsoleWindow;
  30. //GETOPT参数变量
  31. int OPTIND=1,OPTOPT;
  32. char *OPTARG;
  33. //添加关键词条目(请用小写定义),解释时不区分大小写。
  34. 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", "易"};
  35. static const char* SENSITIVE_CN_WORDS[]={"显示", "清屏", "暂停", "退出", "标题", "窗体", "位置", "颜色", "如果", "循环", "设置", "跳转", "输入", "计算", "目录", "进程", "杀死", "显文", "位图", "移窗", "注释", "关闭回显", "鼠标", "磁盘", "关机", "注销", "重启", "睡眠"};
  36. //限制关键词数量                        
  37. #define SENSITIVE_NUM 29
  38. /***************解析函数群***************/
  39. //关键词解析函数
  40. inline int Identify_KeyWords()
  41. {
  42. int i, SN;
  43. if(E_LANGUAGE==1){
  44. for(SN=0; SN<SENSITIVE_NUM-1; SN++){
  45. for(i=0; SENSITIVE_CN_WORDS[SN][i]!='\0'; i++){
  46. if(
  47. RAMread[position+i]   !=SENSITIVE_CN_WORDS[SN][i] &&
  48. RAMread[position+i]+32!=SENSITIVE_CN_WORDS[SN][i]
  49. ){
  50. break;
  51. }
  52. }
  53. if(
  54. (SENSITIVE_CN_WORDS[SN][i]=='\0' && FLAG!=0) &&
  55. (
  56. (RAMread[position+i]== ' ') ||
  57. (RAMread[position+i]== '.') ||
  58. (RAMread[position+i]== ';') ||
  59. (RAMread[position+i]=='\r') ||
  60. (RAMread[position+i]=='\n') ||
  61. (RAMread[position+i]=='\t') ||
  62. (RAMread[position+i]== '&') ||
  63. (position+i==fsize)
  64. )
  65. ){
  66. position+=i;
  67. return SN;
  68. }
  69. }
  70. return 255;
  71. }
  72. for(SN=0; SN<SENSITIVE_NUM; SN++){
  73. for(i=0; SENSITIVE_WORDS[SN][i]!='\0'; i++){
  74. if(
  75. RAMread[position+i]   !=SENSITIVE_WORDS[SN][i] &&
  76. RAMread[position+i]+32!=SENSITIVE_WORDS[SN][i]
  77. ){
  78. break;
  79. }
  80. }
  81. if(
  82. (SENSITIVE_WORDS[SN][i]=='\0' && FLAG!=0) &&
  83. (
  84. (RAMread[position+i]== ' ') ||
  85. (RAMread[position+i]== '.') ||
  86. (RAMread[position+i]== ';') ||
  87. (RAMread[position+i]=='\r') ||
  88. (RAMread[position+i]=='\n') ||
  89. (RAMread[position+i]=='\t') ||
  90. (RAMread[position+i]== '&') ||
  91. (position+i==fsize)
  92. )
  93. ){
  94. position+=i;
  95. return SN;
  96. }
  97. }
  98. return 255;
  99. }
  100. //切分解析函数
  101. inline int Analytic_Spli()
  102. {
  103. int i=0;
  104. for(position++; position<fsize; position++){
  105. if(
  106. (RAMread[position]=='\r') ||
  107. (RAMread[position]=='\n') ||
  108. (RAMread[position]=='\0') ||
  109. (RAMread[position]== '&') ||
  110. (RAMread[position]== '|')
  111. ){
  112. break;
  113. }
  114. i++;
  115. }
  116. return i;
  117. }
  118. //命令行参数拆分
  119. int Split_Argv()
  120. {
  121. int i, j=0, L=0, MARK1=0, MARK2=0, AC=0;
  122. if((L=Analytic_Spli())<=1){return 0;}
  123. i=position-L;
  124. while(RAMread[i]==' '||RAMread[i]=='\t'){
  125. i++;
  126. }
  127. for(; i<position; i++){
  128. if(
  129. (RAMread[i]!= ' ') &&
  130. (RAMread[i]!='\t') &&
  131. (MARK1==0)
  132. ){
  133. if(RAMread[i]=='"'){
  134. i++, MARK2=1;
  135. }
  136. MARK1=1;
  137. }else if(
  138. (RAMread[i]== ' ')||
  139. (RAMread[i]=='\t')||
  140. (RAMread[i]== '"')
  141. ){
  142. if(
  143. (MARK1==1) &&
  144. (MARK2==0) &&
  145. (RAMread[i]!='"')
  146. ){
  147. ARG[AC][j]='\0';
  148. j=0, MARK1=0, AC++;
  149. }
  150. if(
  151. (RAMread[i]=='"') &&
  152. (MARK2==1)
  153. ){
  154. MARK2=0;
  155. }
  156. }
  157. if(
  158. (MARK1==1) &&
  159. (RAMread[i]!='"')
  160. ){
  161. ARG[AC][j++]=RAMread[i];
  162. }
  163. }
  164. return AC+1;
  165. }
  166. /***************命令函数群***************/
  167. //打印函数
  168. inline int Shell_ECHO()
  169. {
  170. int i=Analytic_Spli();
  171. fwrite(RAMread+position-i, i, 1, stdout);
  172. if(i){fputs("\r\n", stdout);}
  173. return 0;
  174. }
  175. //REM函数
  176. inline int Shell_REM()
  177. {
  178. if(ECHOFF==0){
  179. position--;
  180. fputs("REM", stdout);
  181. Shell_ECHO();
  182. return 1;
  183. }
  184. Analytic_Spli();
  185. return 0;
  186. }
  187. //清屏函数
  188. int Shell_CLS()
  189. {
  190. HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
  191. COORD coordScreen={0,0};
  192. DWORD cCharsWritten;
  193. CONSOLE_SCREEN_BUFFER_INFO csbi;
  194. GetConsoleScreenBufferInfo(hConsole, &csbi);
  195. FillConsoleOutputCharacter(hConsole,       (TCHAR)' ', csbi.dwSize.X*csbi.dwSize.Y, coordScreen, &cCharsWritten);
  196. GetConsoleScreenBufferInfo(hConsole, &csbi);
  197. FillConsoleOutputAttribute(hConsole, csbi.wAttributes, csbi.dwSize.X*csbi.dwSize.Y, coordScreen, &cCharsWritten);
  198. SetConsoleCursorPosition(hConsole,coordScreen);
  199. return 0;
  200. }
  201. //暂停函数
  202. int Shell_PAUSE()
  203. {
  204. if(ECHOFF==0){
  205. if(E_LANGUAGE=0){
  206. fputs("\r\nPAUSE...", stdout);
  207. }else{
  208. fputs("\r\n请按任意键继续...", stdout);
  209. }
  210. }
  211. getch();
  212. return 0;
  213. }
  214. //退出函数
  215. int Shell_EXIT(int Exit_Code)
  216. {
  217. exit(Exit_Code);
  218. }
  219. //标题函数
  220. int Shell_TITLE(char* Str)
  221. {
  222. SetConsoleTitle(Str);
  223. return 0;
  224. }
  225. //跳转函数
  226. int Shell_GOTO(int len)
  227. {
  228. int i, j;
  229. for(j=0; j<L_SN; j++){
  230. for(i=0; i<len; i++){
  231. printf("%c#####%c\n",RAMread[position-len+i],  RAMread[LAB[j]+i]);
  232. RAMread[position-len+i]!=RAMread[LAB[j]+i];
  233. break;
  234. }
  235. if(i==len){
  236. position=RAMread[LAB[j]];
  237. return 0;
  238. }
  239. }
  240. fputs("Can not find label :" ,stdout);
  241. fwrite(RAMread+position-len, len, 1, stdout);
  242. return 1;
  243. }
  244. //光标函数
  245. int Shell_POS(int x,int y)
  246. {
  247. COORD pos;
  248. HANDLE hOutput;
  249. pos.X=x;
  250. pos.Y=y;
  251. hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
  252. SetConsoleCursorPosition(hOutput,pos);
  253. return 0;
  254. }
  255. //睡眠函数
  256. int Shell_SLEEP(int t)
  257. {
  258. Sleep(t);
  259. return 0;
  260. }
  261. //按键函数
  262. int Shell_KEY(clock_t delay)
  263. {
  264. int i,KEY_V,start=clock();
  265. do{
  266. if(kbhit()){
  267. KEY_V=(int)(getch());
  268. if(KEY_V<97){KEY_V+=32;}
  269. return KEY_V;
  270. }
  271. for(i=0;i<=50;i++);
  272. }while((clock()-start)<delay);
  273. return -1;
  274. }
  275. //鼠标点击函数:KEY_V取值1、2对应鼠标左右键点击;当取值为0时则隐藏光标;
  276. int Shell_MOUSE(int KEY_V)
  277. {
  278. if(KEY_V==0){
  279.          CONSOLE_CURSOR_INFO cursor_info={1,0};
  280.          SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
  281. return 0;
  282. }
  283. HANDLE StdIn=GetStdHandle(STD_INPUT_HANDLE);
  284. DWORD  OrgMode, Res;
  285. INPUT_RECORD InR;
  286. GetConsoleMode(StdIn, &OrgMode);
  287. SetConsoleMode(StdIn, OrgMode | ENABLE_WINDOW_INPUT |  ENABLE_MOUSE_INPUT);
  288. for(;;){
  289. ReadConsoleInput(StdIn, &InR, 1, &Res);
  290. if(
  291. InR.Event.MouseEvent.dwEventFlags ==0  &&
  292. InR.Event.MouseEvent.dwButtonState==KEY_V
  293. ){
  294. SetConsoleMode(StdIn, OrgMode);
  295. return (InR.Event.MouseEvent.dwMousePosition.Y)*1000+(InR.Event.MouseEvent.dwMousePosition.X);
  296. }
  297. }
  298. }
  299. //窗口大小
  300. int Shell_MODE_CON(int x, int y)
  301. {
  302. HANDLE StdOut=GetStdHandle(STD_OUTPUT_HANDLE);
  303. CONSOLE_SCREEN_BUFFER_INFO scbi;
  304. COORD size={x, 300};
  305. GetConsoleScreenBufferInfo(StdOut, &scbi);
  306. SetConsoleScreenBufferSize(StdOut, size);
  307. SMALL_RECT rc={0, 0, x-1, y-1};
  308. SetConsoleWindowInfo(StdOut, 1, &rc);
  309. CloseHandle(StdOut);
  310. return 0;
  311. }
  312. //列举文件
  313. int Shell_LS(char* path)
  314. {
  315. CHAR szFilePath[MAX_PATH];
  316. HANDLE hListFile;
  317. WIN32_FIND_DATA fileData;
  318. lstrcpy(szFilePath, path);
  319. lstrcat(szFilePath, "\\*");
  320. hListFile = FindFirstFile(szFilePath,&fileData);
  321. if(hListFile == INVALID_HANDLE_VALUE){return 1;}
  322. printf("FILE LIST:\r\n");
  323. do{
  324. printf("\t%s\r\n", fileData.cFileName );
  325. }while(FindNextFile(hListFile, &fileData));
  326. return 0;
  327. }
  328. //列举进程
  329. int Shell_PS()
  330. {
  331. HANDLE hSnapshot;
  332. HANDLE hProcess;
  333. PROCESSENTRY32 pe32;
  334. hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  335. if( hSnapshot == INVALID_HANDLE_VALUE )
  336. {
  337. printf( "CreateToolhelp32Snapshot (of processes) failed\n" );
  338. return 1;
  339. }
  340. pe32.dwSize = sizeof( PROCESSENTRY32 );
  341. if( !Process32First( hSnapshot, &pe32 ) )
  342. {
  343. printf( "Process32First() failed\n" );
  344. CloseHandle( hSnapshot );
  345. return 1;
  346. }
  347. printf("                [PROCESS]   [PID]  [PPID]  [THRE]   [PRI]\r\n---------------------------------------------------------------\r");
  348. do{
  349. printf( "\n%25s", pe32.szExeFile );
  350. printf( "%8u", pe32.th32ProcessID );
  351. printf( "%8u", pe32.th32ParentProcessID );
  352. printf( "%8d", pe32.cntThreads );
  353. printf( "%8d", pe32.pcPriClassBase );
  354. }while( Process32Next( hSnapshot, &pe32 ) );
  355. CloseHandle( hSnapshot );
  356. fputc('\n',stdout);
  357. return 0;
  358. }
  359. //结束进程
  360. int Shell_KL(int ProcessID)
  361. {
  362. HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)ProcessID);
  363. TerminateProcess(hProcess, 0);
  364. return 0;
  365. }
  366. //字体颜色: 1,2,4,8前景;16,32,64,128后景;
  367. int Shell_COLOR(unsigned short A,unsigned short B,unsigned short C,unsigned short D)
  368. {
  369. HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
  370. CONSOLE_SCREEN_BUFFER_INFO csbi;
  371. GetConsoleScreenBufferInfo(handle_out, &csbi);
  372. SetConsoleTextAttribute(handle_out, A | B | C | D);
  373. return 0;
  374. }
  375. //位图显示
  376. int Shell_BMP(
  377. char* file ,         //位图文件;
  378. int x,int y,         //相对窗口起始坐标x,y;
  379. int W,int H,         //要粘贴的宽度高度W,H;
  380. int X,int Y          //相对缓存DC的坐标X,Y;
  381. )
  382. {
  383. HMODULE hKernel32=GetModuleHandle("kernel32");
  384. GetConsoleWindow=(PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow");
  385. HWND cmd=GetConsoleWindow();
  386. HDC dc=GetDC(cmd);
  387. HBITMAP hBitmap;
  388. hBitmap=(HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
  389. HDC cmdmem=CreateCompatibleDC(dc);
  390. SelectObject(cmdmem, hBitmap);
  391. BitBlt(dc, x, y, W, H, cmdmem, X, Y, SRCCOPY);
  392. return 0;
  393. }
  394. //移动窗口
  395. int Shell_MW(int x, int y)
  396. {
  397. HMODULE hKernel32 = GetModuleHandle("kernel32");
  398. GetConsoleWindow= (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow");
  399. HWND cmd=GetConsoleWindow();
  400. SetWindowPos(cmd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE);
  401. return 0;
  402. }
  403. //关机函数S取值r、l、p、空;对应重启、注销、关电源、关机;
  404. int Shell_SHUTDOWN(char S)
  405. {
  406. HANDLE hToken;
  407. TOKEN_PRIVILEGES tkp;
  408. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)){
  409. return 1;
  410. }
  411. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
  412. tkp.PrivilegeCount=1;
  413. tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
  414. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  415. if(GetLastError() !=ERROR_SUCCESS){
  416. return 1;
  417. }
  418. if      (S=='r'||S=='R'){
  419. ExitWindowsEx(EWX_REBOOT   | EWX_FORCE, 0);
  420. }else if(S=='l'||S=='L'){
  421. ExitWindowsEx(EWX_LOGOFF   | EWX_FORCE, 0);
  422. }else if(S=='p'||S=='P'){
  423. ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0);
  424. }else{
  425. ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
  426. }
  427. return 0;
  428. }
  429. //磁盘信息
  430. void utoiRightJustified(TCHAR* szLeft, TCHAR* szRight, unsigned uVal)
  431. {
  432. TCHAR* szCur = szRight;
  433. int nComma = 0;
  434. if (uVal) {
  435. while (uVal && (szCur >= szLeft)) {
  436. if (nComma == 3) {
  437. *szCur = ',';
  438. nComma = 0;
  439. }else {
  440. *szCur = (uVal % 10) | 0x30;
  441. uVal /= 10;
  442. ++nComma;
  443. }
  444. --szCur;
  445. }
  446. } else {
  447. *szCur = '0';
  448. --szCur;
  449. }
  450. if (uVal) {
  451. szCur = szLeft;
  452. while (szCur <= szRight) {
  453. *szCur = '*';
  454. ++szCur;
  455. }
  456. }
  457. }
  458. int Shell_DISK(){
  459. DWORD dwDrive;
  460. INT nDrive;
  461. char dName[4] ;
  462. char* Driver;
  463. long GB = 1024*1024*1024;
  464. ULARGE_INTEGER Free ;
  465. ULARGE_INTEGER Total ;
  466. ULARGE_INTEGER TotalFree ;
  467. dwDrive = GetLogicalDrives();
  468. printf(
  469. "  ------------------------------------\n"
  470. "│DRIVE│FREE CAPACITY│TOTAL CAPACITY│                              DISK\n"
  471. "  ------------------------------------\n"
  472. "  ------------------------------------\n"
  473. );
  474. for ( nDrive = 0 ; nDrive < 26 ; nDrive++ ){
  475. if ( dwDrive & (1 << nDrive) ){
  476. sprintf(dName,"%c:",(nDrive + 'A'));
  477. Driver =(char*)dName;
  478. if(GetDiskFreeSpaceEx(Driver, &Free, &Total, &TotalFree)){
  479. printf("│ %s  │    %3I64u GB   │     %3I64u GB   │\n",Driver,Free.QuadPart/GB,Total.QuadPart/GB);
  480. }
  481. }
  482. }
  483. printf("  ------------------------------------\n");
  484. TCHAR   g_szBorder[] = _T("  ------------------------------------------------------------------------\n");
  485. TCHAR   g_szTitle1[] = _T("│DRIVE│TOTAL CLUSTERS│AVAIL CLUSTERS│SECTORS / CLUSTER│BYTES / SECTOR│\n");
  486. TCHAR   g_szTitle2[] = _T("  ------------------------------------------------------------------------\n  ------------------------------------------------------------------------\n");
  487. TCHAR   g_szLine[]   = _T("│  :  │              │              │                 │              │\n");
  488. TCHAR szMsg[4200];
  489. struct _diskfree_t df = {0};
  490. ULONG uDriveMask = _getdrives();
  491. unsigned uErr, uLen, uDrive;
  492. printf(g_szBorder);
  493. printf(g_szTitle1);
  494. printf(g_szTitle2);
  495. for (uDrive=1; uDrive<=26; ++uDrive) {
  496. if (uDriveMask & 1) {
  497. uErr = _getdiskfree(uDrive, &df);
  498. memcpy(szMsg, g_szLine, sizeof(g_szLine));
  499. szMsg[3] = uDrive + 'A' - 1;
  500. char lp[5] = "C://";
  501. lp [0] = uDrive + 'A' - 1;
  502. if (uErr == 0) {
  503.     utoiRightJustified(szMsg+8,  szMsg+19, df.total_clusters);
  504.     utoiRightJustified(szMsg+23, szMsg+34, df.avail_clusters);
  505.     utoiRightJustified(szMsg+38, szMsg+52, df.sectors_per_cluster);
  506.     utoiRightJustified(szMsg+56, szMsg+67, df.bytes_per_sector);
  507. } else {
  508. uLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, uErr, 0, szMsg+8, 4100, NULL);
  509. szMsg[uLen+6] = ' ';
  510. szMsg[uLen+7] = ' ';
  511. szMsg[uLen+8] = ' ';
  512. }
  513. printf(szMsg);
  514. }
  515. uDriveMask >>= 1;
  516. if (!uDriveMask)
  517. break;
  518. }
  519. printf(g_szBorder);
  520. return 0;
  521. }
  522. /***************业务函数群***************/
  523. //参数解析函数
  524. int GETOPT(int nargc, char *nargv[], char *ostr)
  525. {
  526. static char* place="";
  527. static char* lastostr=(char *) 0;
  528. register char* oli;
  529. char* index();
  530. if(ostr!=lastostr){
  531. lastostr=ostr;
  532. place="";
  533. }
  534. if(!*place){
  535. if(
  536. (OPTIND>=nargc)              ||
  537. (*(place=nargv[OPTIND])!='/')||
  538. (! *++place)
  539. ){
  540. place="";
  541. return(EOF);
  542. }
  543. if (*place == '/' && place[1] == 0){
  544. ++OPTIND;
  545. return(EOF);
  546. }
  547. }
  548. if ((OPTOPT=(int)*place++)==(int)':' || !(oli=strchr(ostr, OPTOPT))){
  549. if(!*place){++OPTIND;}
  550. }
  551. if (*++oli!=':'){
  552. OPTARG=NULL;
  553. if(!*place){++OPTIND;}
  554. }
  555. else{
  556. if(*place){
  557. OPTARG=place;
  558. }else if(nargc<=++OPTIND){
  559. place="";
  560. }else{
  561. OPTARG=nargv[OPTIND];
  562. }
  563. place="";
  564. ++OPTIND;
  565. }
  566. return(OPTOPT);
  567. }
  568. char* UnicodeToANSI(const wchar_t* Str)
  569. {
  570. int L=WideCharToMultiByte(CP_ACP, 0, Str, -1, NULL, 0, NULL, NULL);
  571. char* Out=(char *)calloc(L+1, sizeof(char));
  572. WideCharToMultiByte(CP_ACP, 0, Str, -1, Out, L, NULL, NULL);
  573. return Out;
  574. }
  575. wchar_t* UTF8ToUnicode(const char* Str)
  576. {
  577. int L=MultiByteToWideChar(CP_UTF8, 0, Str,-1, NULL, 0);
  578. wchar_t* Out=(wchar_t *)calloc(L+1, sizeof(wchar_t));
  579. MultiByteToWideChar(CP_UTF8, 0, Str, -1, (LPWSTR)Out, L);
  580. return Out;
  581. }
  582. wchar_t* BIG5ToUnicode(const char* Str)
  583. {
  584. int L=MultiByteToWideChar(950, 0, Str,-1, NULL, 0);
  585. wchar_t* Out=(wchar_t *)calloc(L+1, sizeof(wchar_t));
  586. MultiByteToWideChar(950, 0, Str, -1, (LPWSTR)Out, L);
  587. return Out;
  588. }
  589. bool isUTF8(const char* Str)
  590. {
  591. if(!Str){
  592. return false;
  593. }
  594. const unsigned char* bytes=(const unsigned char *)Str;
  595. while(*bytes){
  596. if(
  597. (
  598. bytes[0]<=0x7F ||
  599. bytes[0]==0x09 ||
  600. bytes[0]==0x0A ||
  601. bytes[0]==0x0D ||
  602. (0x20<=bytes[0] && bytes[0]<=0x7E)
  603. )
  604. ){
  605. bytes+=1;
  606. continue;
  607. }
  608. if(
  609. (
  610. (0xC2<=bytes[0] && bytes[0]<=0xDF) &&
  611. (0x80<=bytes[1] && bytes[1]<=0xBF)
  612. )
  613. ){
  614. bytes+=2;
  615. continue;
  616. }
  617. if(
  618. (
  619.   (bytes[0]==0xE0) &&
  620. (0xA0<=bytes[1] && bytes[1]<=0xBF) &&
  621. (0x80<=bytes[2] && bytes[2]<=0xBF)
  622. ) ||
  623. (
  624. (
  625. (0xE1<=bytes[0] && bytes[0]<=0xEC)||
  626.                    bytes[0]==0xEE ||
  627.                    bytes[0]==0xEF
  628. ) &&
  629. (0x80<=bytes[1] && bytes[1]<=0xBF) &&
  630. (0x80<=bytes[2] && bytes[2]<=0xBF)
  631. ) ||
  632. (
  633.   (bytes[0]==0xED) &&
  634. (0x80<=bytes[1] && bytes[1]<=0x9F) &&
  635. (0x80<=bytes[2] && bytes[2]<=0xBF)
  636. )
  637. ){
  638. bytes+=3;
  639. continue;
  640. }
  641. if(
  642. (
  643.   (bytes[0]==0xF0) &&
  644. (0x90<=bytes[1] && bytes[1]<=0xBF) &&
  645. (0x80<=bytes[2] && bytes[2]<=0xBF) &&
  646. (0x80<=bytes[3] && bytes[3]<=0xBF)
  647. ) ||
  648. (
  649. (0xF1<=bytes[0] && bytes[0]<=0xF3) &&
  650. (0x80<=bytes[1] && bytes[1]<=0xBF) &&
  651. (0x80<=bytes[2] && bytes[2]<=0xBF) &&
  652. (0x80<=bytes[3] && bytes[3]<=0xBF)
  653. ) ||
  654. (
  655.   (bytes[0]==0xF4) &&
  656. (0x80<=bytes[1] && bytes[1]<=0x8F) &&
  657. (0x80<=bytes[2] && bytes[2]<=0xBF) &&
  658. (0x80<=bytes[3] && bytes[3]<=0xBF)
  659. )
  660. ){
  661. bytes+=4;
  662. continue;
  663. }
  664. return false;
  665. }
  666. return true;
  667. }
  668. bool isGB2312(const char* Str)
  669. {
  670. if(!Str){
  671. return false;
  672. }
  673. const unsigned char* bytes=(const unsigned char *)Str;
  674. while(*bytes){
  675. if(
  676. (
  677. bytes[0]<=0x7F ||
  678. bytes[0]==0x09 ||
  679. bytes[0]==0x0A ||
  680. bytes[0]==0x0D ||
  681. (0x20<=bytes[0] && bytes[0]<=0x7E)
  682. )
  683. ){
  684. bytes+=1;
  685. continue;
  686. }
  687. if(
  688. (0xA1<=bytes[0] && bytes[0]<=0xF7) &&
  689. (0xA1<=bytes[1] && bytes[1]<=0xFE)
  690. ){
  691. bytes+=2;
  692. continue;
  693. }
  694. return false;
  695. }
  696. return true;
  697. }
  698. bool isBIG5(const char* Str)
  699. {
  700. if(!Str){
  701. return false;
  702. }
  703. const unsigned char* bytes=(const unsigned char *)Str;
  704. while(*bytes){
  705. if(
  706. (
  707. bytes[0]<=0x7F ||
  708. bytes[0]==0x09 ||
  709. bytes[0]==0x0A ||
  710. bytes[0]==0x0D ||
  711. (0x20<=bytes[0] && bytes[0]<=0x7E)
  712. )
  713. ){
  714. bytes+=1;
  715. continue;
  716. }
  717. if(
  718. (0xA1<=bytes[0] && bytes[0]<=0xF9) &&
  719. (
  720. (0x40<=bytes[1] && bytes[1]<=0x7E) ||
  721. (0xA1<=bytes[1] && bytes[1]<=0xFE)
  722. )
  723. ){
  724. bytes+=2;
  725. continue;
  726. }
  727. return false;
  728. }
  729. return true;
  730. }
  731. int CheckBom(FILE* fp)
  732. {
  733. unsigned char* buf=(unsigned char*)calloc(3,sizeof(unsigned char));
  734. unsigned char* buf2;
  735. fseeko64(fp, (__int64)0, SEEK_SET);
  736. fread(buf, sizeof(unsigned char), 3, fp);
  737.      if(buf[0]==0xEF && buf[1]==0xBB && buf[2]==0xBF){return 3;}
  738. else if(buf[0]==0xFF && buf[1]==0xFE){return 5;}
  739. else if(buf[0]==0xFE && buf[1]==0xFF){return 6;}
  740. else{
  741. fseeko64(fp, (__int64)0, SEEK_SET);
  742. buf2=(unsigned char*)calloc(CHECK_SIZE,sizeof(unsigned char));
  743. fread(buf2, sizeof(unsigned char), CHECK_SIZE, fp);
  744. if(isUTF8(buf2)){
  745. return 2;
  746. }else if(isGB2312(buf2)){
  747. return 1;
  748. }else if(isBIG5(buf2)){
  749. return 4;
  750. }
  751. }
  752. return 1;
  753. }
  754. void Help_Information_Tl(FILE* stream, int a)
  755. {
  756. fprintf(stream,
  757. ">>>------------------------------------------------------------\n"
  758. "DISPLAYS THE CONTENTS OF A TEXT FILE\n"
  759. "VERSION 2.0\n"
  760. "tl  [file] [-n&num1,num2]|[-p&per1,per2]|[-i]|[-d&num]\n"
  761. "---------------------------------------------------------------\n\n"
  762. "    -h  Show help information\n"
  763. "    -n  Read lines from num1 to num2\n"
  764. "    -p  Read lines from percent1 to percent2\n"
  765. "    -i  Show file's information\n"
  766. "    -d  Detection latest num lines \n"
  767. "---------------------------------------------------------------\n"
  768. );
  769. return;
  770. }
  771. int Getkey(int N,int T)
  772. {
  773. int i,KEY_V,start=clock();
  774. do{
  775. if(kbhit()){
  776. KEY_V=(int)(getch());
  777. if(KEY_V<97){KEY_V+=32;}
  778. return KEY_V;
  779. }
  780. for(i=0;i<=N;i++);
  781. }while((clock()-start)<T);
  782. return -1;
  783. }
  784. int CountLines(FILE* fp)
  785. {
  786. int i=0;
  787. char* line=(char *)malloc(BUFF_SIZE*sizeof(char));
  788. while(!feof(fp)){
  789. fgets(line, BUFF_SIZE, fp);
  790. i++;
  791. }
  792. return i;
  793. }
  794. void File_Information(FILE* fp, char* fname)
  795. {
  796. fseeko64(fp, (__int64)0, SEEK_END);
  797. __int64 fsize=ftello64(fp);
  798. fseeko64(fp, (__int64)0, SEEK_SET);
  799. int linenum=CountLines(fp);
  800. fprintf(stdout,
  801. "FILE NAME : %s\n"
  802. "FILE SIZE : %I64d\n"
  803. "FILE LINES: %d\n"
  804. ,fname, fsize, linenum
  805. );
  806. }
  807. int DisplayLine(FILE* fp, int flag, int N1, int N2, __int64 F1, __int64 F2)
  808. {
  809. int i=0, n=0, BOM=0, EN=0;
  810. BOM=CheckBom(fp);
  811. if(BOM==1 || BOM==2 || BOM==4){
  812. EN=0;
  813. }else if(BOM==5 || BOM==6){
  814. EN=2;
  815. }else if(BOM==3){
  816. EN=3;
  817. }
  818. if      (flag==0){
  819. fseeko64(fp, (__int64)EN, SEEK_SET);
  820. }else if(flag==1||flag==3){
  821. __int64 FD=(N1*BUFF_SIZE>F2)?F2:N1*BUFF_SIZE;
  822. fseeko64(fp, -FD, SEEK_END);
  823. N1=CountLines(fp)-N1+1;
  824. fseeko64(fp, -FD, SEEK_END);
  825. }else if(flag==2){
  826. fseeko64(fp,  F1, SEEK_SET);
  827. }
  828. if(BOM<5){
  829. char* Line=(char *)malloc(BUFF_SIZE*sizeof(char));
  830. while(!feof(fp)||flag==3){
  831. memset(Line, 0, BUFF_SIZE*sizeof(char));
  832. if(!fgets(Line, BUFF_SIZE, fp)){Sleep(1);}
  833. i++;
  834. if( ((N1<=i) && (i<=N2) && (F1<=ftello64(fp)) && (ftello64(fp)<=F2))||(flag==3) ){
  835. switch(BOM){
  836. case 1: //Ansi行显
  837. fputs(Line, stdout);
  838. break;
  839. case 2: //Utf8无BOM行显
  840. case 3: //Utf8行显
  841. fputs(UnicodeToANSI(UTF8ToUnicode(Line)), stdout);
  842. break;
  843. case 4: //Big5行显
  844. fputs(UnicodeToANSI(BIG5ToUnicode(Line)), stdout);
  845. break;
  846. }
  847. }else if((i>N2) && (ftello64(fp)>=F2)){
  848. break;
  849. }
  850. }
  851. }else if(BOM==5){                       //Unicode行显
  852. wchar_t* LineW=(wchar_t *)calloc(BUFF_SIZE, sizeof(wchar_t));
  853. while(!feof(fp)||(flag==3)){
  854. memset(LineW, 0, BUFF_SIZE*sizeof(wchar_t));
  855. if(!fgetws(LineW, BUFF_SIZE, fp)){Sleep(1);}
  856. i++;
  857. if( ((N1<=i) && (i<=N2) && (F1<=ftello64(fp)) && (ftello64(fp)<=F2))||(flag==3) ){
  858. fputs(UnicodeToANSI(LineW), stdout);
  859. }else if((i>N2) && (ftello64(fp)>=F2)){
  860. break;
  861. }
  862. }
  863. }else if(BOM==6){                       //Unicode big endian行显
  864. wchar_t* LineW=(wchar_t *)calloc(BUFF_SIZE, sizeof(wchar_t));
  865. while(!feof(fp)||(flag==3)){
  866. memset(LineW, 0, BUFF_SIZE*sizeof(wchar_t));
  867. if(!fgets(LineW, BUFF_SIZE, fp)){Sleep(1);}
  868. i++;
  869. if( ((N1<=i) && (i<=N2) && (F1<=ftello64(fp)) && (ftello64(fp)<=F2))||(flag==3) ){
  870. for(n=0;LineW[n]!=0x0000;n++){
  871. LineW[n]=(LineW[n]&0x00FF)<<8|(LineW[n]&0xFF00)>>8;
  872. }
  873. fputs(UnicodeToANSI(LineW), stdout);
  874. }else if((i>N2) && (ftello64(fp)>=F2)){
  875. break;
  876. }
  877. }
  878. }
  879. fflush(stdout);
  880. return 0;
  881. }
  882. //TL函数入口
  883. int Shell_TL(int argc)
  884. {
  885. int N1=1, N2=2147483631, FLAG=0, i;
  886. __int64 fsize=0;
  887. float P1=0.0, P2=1.0;
  888. FILE* fp;
  889. char* delims;
  890. if((argc==2) && (ARG[1][0]=='-')){
  891. switch(ARG[1][1]){
  892. case 'H':
  893. case 'h':
  894. Help_Information_Tl(stdout, 0);
  895. return 1;
  896. case 'N':
  897. case 'n':
  898. delims=(ARG[1]+2);
  899. N1=atoi(strtok(delims, ","));
  900. N2=atoi(strtok(NULL, ","));
  901. if(N2==0){N2=2147483631;}
  902. if((N1>N2) || (N1<0) || (N2<0)){Help_Information_Tl(stderr, 1);return 1;}
  903. break;
  904. case 'P':
  905. case 'p':
  906. delims=(ARG[1]+2);
  907. P1=atof(strtok(delims, ","));
  908. P2=atof(strtok(NULL, ","));
  909. if((P1>=P2) || (P1>1.0) || (P2>1.0) || (P1<0.0) || (P2<0.0) ){Help_Information_Tl(stderr, 1);return 1;}
  910. FLAG=2;
  911. break;
  912. case 'I':
  913. case 'i':
  914. if( (fp=fopen64(ARG[0], "rb"))==NULL ){fputs("Read failed.", stdout);return 3;}
  915. File_Information(fp, ARG[0]);
  916. fclose(fp);
  917. return 0;
  918. case 'D':
  919. case 'd':
  920. delims=(ARG[1]+2);
  921. N1=abs(atoi(delims)), N1=(N1>FOLLOW_SIZE)?1000:N1, FLAG=1;
  922. if(ARG[1][2]=='\0'){N1=FOLLOW_LINE,FLAG=3;}
  923. break;
  924. default:
  925. Help_Information_Tl(stderr, 2);return 1;
  926. }
  927. }else if(argc!=1){
  928. Help_Information_Tl(stderr, 3);return 1;
  929. }
  930. if( (fp=fopen64(ARG[0], "rb"))==NULL ){
  931. fputs("Read failed.", stdout);
  932. return 3;
  933. }
  934. if(FLAG==1){
  935. do{
  936. fseeko64(fp, (__int64)0, SEEK_END);
  937. if( fsize!=ftello64(fp)){
  938. fsize =ftello64(fp);
  939. system("cls");
  940. DisplayLine(fp, 1, N1, 2147483631, 0, fsize);
  941. }
  942. }while(Getkey(64,FOLLOW_WAIT)!=113);
  943. fputs("\n", stdout);
  944. fclose(fp);
  945. return 0;
  946. }
  947. fseeko64(fp, (__int64)0, SEEK_END);
  948. fsize=ftello64(fp);
  949. DisplayLine(fp, FLAG, N1, N2, fsize*P1, fsize*P2);
  950. fclose(fp);
  951. return 0;
  952. }
  953. //帮助信息
  954. void Help_Information(FILE* stream, int Exit_Code)
  955. {
  956. fprintf(stream,
  957. "==================================================\n"
  958. "CME [VERSION 1.0]\n"
  959. "==================================================\n"
  960. "    [ECHO]      Display string\n"
  961. "    [CLS]       Clear screen\n"
  962. "    [PAUSE]     Time out\n"
  963. "    [EXIT ]     Drop out\n"
  964. "    [TITLE]     Set the title\n"
  965. "    [MODE CON]  Window settings\n"
  966. "    [POS]       Set the Cursor position\n"
  967. "    [COLOR]     Font color\n"
  968. "    [GOTO]      Tag jumps\n"
  969. "    [LS]        List the specified directory files\n"
  970. "    [PS]        List the processes\n"
  971. "    [KL]        End a process\n"
  972. "    [CAT]       View a txt file\n"
  973. "    [BMP]       Displays a bitmap\n\n"
  974. "==================================================\n"
  975. "                                        1029TE0\n"
  976. );
  977. exit(Exit_Code);
  978. }
  979. //脚本解释函数
  980. int CME()
  981. { int i=0;
  982. while(i++<fsize){
  983. if(
  984. (RAMread[i]== ' ') ||
  985. (RAMread[i]=='\r') ||
  986. (RAMread[i]=='\n') ||
  987. (RAMread[i]=='\t') ||
  988. (RAMread[i]== '&') ||
  989. (RAMread[i]== '|') ||
  990. (RAMread[i]=='\0')
  991. ){
  992. continue;
  993. }
  994. if(RAMread[i]==':'){
  995. i+=Analytic_Spli();
  996. LAB[L_SN++]=i;
  997. }
  998. }
  999. position=0;
  1000. while(position<fsize){
  1001. if(
  1002. (RAMread[position]== ' ') ||
  1003. (RAMread[position]=='\r') ||
  1004. (RAMread[position]=='\n') ||
  1005. (RAMread[position]=='\t') ||
  1006. (RAMread[position]== '&') ||
  1007. (RAMread[position]== '|') ||
  1008. (RAMread[position]=='\0')
  1009. ){
  1010. position++;FLAG=255;
  1011. if(RAMread[position]==':'){
  1012. Analytic_Spli();
  1013. LAB[L_SN++]=position;
  1014. }
  1015. continue;
  1016. }
  1017. FLAG=Identify_KeyWords(position);
  1018. switch(FLAG){
  1019. case 255:
  1020. printf("ERROR [At this time should not have \"");
  1021. fwrite(RAMread+position, 12, 1, stdout);
  1022. fputs(" \"]\n", stdout);
  1023. Shell_PAUSE();
  1024. return 1;
  1025. case 0:
  1026. Shell_ECHO();
  1027. break;
  1028. case 1:
  1029. Shell_CLS();
  1030. break;
  1031. case 2:
  1032. Shell_PAUSE();
  1033. break;
  1034. case 3:
  1035. Shell_EXIT(0);
  1036. break;
  1037. case 4:
  1038. if(Split_Argv()==1){
  1039. Shell_TITLE(ARG[0]);
  1040. }
  1041. break;
  1042. case 5:
  1043. if(Split_Argv()==2){
  1044. Shell_MODE_CON(atoi(ARG[0]), atoi(ARG[1]));
  1045. }
  1046. break;
  1047. case 6:
  1048. if(Split_Argv()==2){
  1049. Shell_POS(atoi(ARG[0]), atoi(ARG[1]));
  1050. }
  1051. break;
  1052. case 7:
  1053. if(Split_Argv()==4){
  1054. Shell_COLOR(atoi(ARG[0]), atoi(ARG[1]), atoi(ARG[2]), atoi(ARG[3]));
  1055. }else{
  1056. Shell_COLOR(1,2,4,8);
  1057. }
  1058. break;
  1059. case 8:
  1060. //Shell_IF();
  1061. break;
  1062. case 9:
  1063. //Shell_FOR();
  1064. break;
  1065. case 10:
  1066. //Shell_SET();
  1067. break;
  1068. case 11:
  1069. //Shell_GOTO(Analytic_Spli());
  1070. break;
  1071. case 12:
  1072. //Shell_SET/P();
  1073. break;
  1074. case 13:
  1075. //Shell_SET/A();
  1076. break;
  1077. case 14:
  1078. if(Split_Argv()==1){
  1079. Shell_LS(ARG[0]);
  1080. }else{
  1081. Shell_LS(".");
  1082. }
  1083. break;
  1084. case 15:
  1085. Shell_PS();
  1086. break;
  1087. case 16:
  1088. if(Split_Argv()==1){
  1089. Shell_KL(atoi(ARG[0]));
  1090. }
  1091. break;
  1092. case 17:
  1093. Shell_TL(Split_Argv());
  1094. break;
  1095. case 18:
  1096. if(Split_Argv()==7){
  1097. Shell_BMP(ARG[0], atoi(ARG[1]), atoi(ARG[2]), atoi(ARG[3]), atoi(ARG[4]), atoi(ARG[5]), atoi(ARG[6]));
  1098. }else{
  1099. Shell_BMP(ARG[0], 100, 100, 300, 300, 0, 0);
  1100. }
  1101. break;
  1102. case 19:
  1103. if(Split_Argv()==2){
  1104. Shell_MW(atoi(ARG[0]), atoi(ARG[1]));
  1105. }
  1106. break;
  1107. case 20:
  1108. Shell_REM();
  1109. break;
  1110. case 21:
  1111. ECHOFF=1;
  1112. break;
  1113. case 22:
  1114. if(Split_Argv()==1){
  1115. Shell_MOUSE(atoi(ARG[0]));
  1116. }
  1117. break;
  1118. case 23:
  1119. Shell_DISK();
  1120. break;
  1121. case 24:
  1122. if(Split_Argv()==0){
  1123. Shell_SHUTDOWN('\0');
  1124. }else{
  1125. Shell_SHUTDOWN(ARG[0][0]);
  1126. }
  1127. break;
  1128. case 25:
  1129. if(Split_Argv()==0){
  1130. Shell_SHUTDOWN('L');
  1131. }
  1132. break;
  1133. case 26:
  1134. if(Split_Argv()==0){
  1135. Shell_SHUTDOWN('R');
  1136. }
  1137. break;
  1138. case 27:
  1139. if(Split_Argv()==1){
  1140. Shell_SLEEP(atoi(ARG[0]));
  1141. }
  1142. break;
  1143. case 28:
  1144. E_LANGUAGE=1;
  1145. break;
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. /*************MAIN主函数入口*************/
  1151. int main(int argc, char** argv)
  1152. {
  1153. if(argc!=2){Help_Information(stderr, 2);}
  1154. Shell_TITLE("CME Script");
  1155. FILE* fp;
  1156. if( (fp=fopen(argv[1], "rb"))==NULL ){
  1157. fputs("Read Error.\n", stdout);
  1158. }
  1159. //测量尺寸
  1160. fseek(fp, 0, SEEK_END);
  1161. if( (fsize=ftell(fp))>2*1024*1024 ){
  1162. fputs("The file is too large, out of memory.\n", stdout);
  1163. return 1;
  1164. }
  1165. fseek(fp, 0, SEEK_SET);
  1166. //压入内存
  1167. RAMread=(char*)calloc(fsize, sizeof(char));
  1168. fread(RAMread, fsize, 1, fp);
  1169. fclose(fp);
  1170. //解析脚本
  1171. CME();
  1172. free(RAMread);
  1173. return 0;
  1174. }
复制代码

作者: yishuigege    时间: 2016-11-1 09:03

为楼主喝彩!
作者: codegay    时间: 2016-11-1 15:49


这么NB是要上天啊。




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2