G:/ScriptBasic/source/extensions/cio/interface.c

Go to the documentation of this file.
00001 /* FILE: cio.c
00002 
00003 This file implements the cio module for ScriptBasic. This module is
00004 to handle console input and output directly.
00005 
00006 --GNU LGPL
00007 This library is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU Lesser General Public
00009 License as published by the Free Software Foundation; either
00010 version 2.1 of the License, or (at your option) any later version.
00011 
00012 This library is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 Lesser General Public License for more details.
00016 
00017 You should have received a copy of the GNU Lesser General Public
00018 License along with this library; if not, write to the Free Software
00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 This line tells the Perl script setup.pl that this file is to be compiled
00022 under NT or other WIN32 flavours and should not try to compile it under
00023 UNIX.
00024 
00025 NTLIBS:
00026 */
00027 #ifdef WIN32
00028 #include <conio.h>
00029 #include <stdio.h>
00030 #include <windows.h>
00031 #include <winbase.h>
00032 #include <winuser.h>
00033 #endif
00034 #include "../../basext.h"
00035 
00046 besSUB_SHUTDOWN
00047   return 0;
00048 besEND
00049 
00050 besSUB_START
00051   return COMMAND_ERROR_SUCCESS;
00052 besEND
00053 
00054 static HANDLE GetConsoleHandle(){
00055   HANDLE hConsole;
00056 
00057   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
00058   if( hConsole == NULL ){
00059     AllocConsole();
00060     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
00061     }
00062   return hConsole;
00063   }
00064 
00065 #define GET_CONSOLE_HANDLE   hConsole = GetConsoleHandle();\
00066   if( hConsole == NULL )return 0;
00067 
00076 besFUNCTION(gotoxy)
00077 #ifdef WIN32
00078   COORD lp;
00079   long x,y;
00080   HANDLE hConsole;
00081   int iError;
00082 
00083   if( besARGNR < 2 )return COMMAND_ERROR_FEW_ARGS;
00084 
00085   iError = besGETARGS "ii",&x,&y besGETARGE
00086   lp.X = (short)x; lp.Y = (short)y;
00087   if( iError )return iError;
00088   GET_CONSOLE_HANDLE
00089   SetConsoleCursorPosition(hConsole,lp);
00090 #else
00091   return COMMAND_ERROR_NOTIMP;
00092 #endif
00093 besEND
00094 
00102 besFUNCTION(sbkbhit)
00103 #ifdef WIN32
00104   besALLOC_RETURN_LONG;
00105   LONGVALUE(besRETURNVALUE) = _kbhit() ? -1 : 0 ;
00106 #else
00107   return COMMAND_ERROR_NOTIMP;
00108 #endif
00109 besEND
00110 
00119 besFUNCTION(sbgetch)
00120 #ifdef WIN32
00121   besALLOC_RETURN_LONG;
00122   LONGVALUE(besRETURNVALUE) = _getch();
00123 #else
00124   return COMMAND_ERROR_NOTIMP;
00125 #endif
00126 besEND
00127 
00136 besFUNCTION(sbgetche)
00137 #ifdef WIN32
00138   besALLOC_RETURN_LONG;
00139   LONGVALUE(besRETURNVALUE) = _getche();
00140 #else
00141   return COMMAND_ERROR_NOTIMP;
00142 #endif
00143 besEND
00144 
00153 besFUNCTION(sbdetach)
00154 #ifdef WIN32
00155   FreeConsole();
00156 #else
00157   return COMMAND_ERROR_NOTIMP;
00158 #endif
00159 besEND
00160 
00167 besFUNCTION(sbgettitle)
00168 #ifdef WIN32
00169   char szB[256];
00170   long i;
00171 
00172   i = GetConsoleTitle(szB,256);
00173   if( i > 0 ){
00174     besALLOC_RETURN_STRING(i);
00175     memcpy(STRINGVALUE(besRETURNVALUE),szB,i);
00176     }else
00177     besRETURNVALUE = NULL;
00178 #else
00179   return COMMAND_ERROR_NOTIMP;
00180 #endif
00181 besEND
00182 
00189 besFUNCTION(sbsettitle)
00190 #ifdef WIN32
00191   char *pszT;
00192   int iError;
00193 
00194   if( besARGNR < 1 )return COMMAND_ERROR_FEW_ARGS;
00195 
00196   iError = besGETARGS "z",&pszT besGETARGE
00197   if( iError )return iError;
00198   SetConsoleTitle(pszT);
00199   besFREE(pszT);
00200 #else
00201   return COMMAND_ERROR_NOTIMP;
00202 #endif
00203 besEND
00204 
00205 #ifdef WIN32
00206 static int SetConBreak(int i){
00207   HANDLE hConsole;
00208   DWORD Mode;
00209 
00210   GET_CONSOLE_HANDLE
00211   if( GetConsoleMode(hConsole,&Mode) ){
00212     if( i ){
00213       Mode |= ENABLE_PROCESSED_INPUT;
00214       SetConsoleCtrlHandler(NULL,TRUE);
00215       }
00216     else{
00217       Mode &= ~ ENABLE_PROCESSED_INPUT;
00218       SetConsoleCtrlHandler(NULL,FALSE);
00219       }
00220     SetConsoleMode(hConsole,Mode);
00221     }
00222   return 0;
00223   }
00224 #endif
00225 
00233 besFUNCTION(sbnobreak)
00234 #ifdef WIN32
00235   SetConBreak(1);
00236 #else
00237   return COMMAND_ERROR_NOTIMP;
00238 #endif
00239 besEND
00240 
00249 besFUNCTION(sbbreak)
00250 #ifdef WIN32
00251   SetConBreak(0);
00252 #else
00253   return COMMAND_ERROR_NOTIMP;
00254 #endif
00255 besEND
00256 
00257 static int GetCSBI(PCONSOLE_SCREEN_BUFFER_INFO q){
00258   HANDLE hConsole;
00259 
00260   GET_CONSOLE_HANDLE
00261   GetConsoleScreenBufferInfo(hConsole,q);
00262   return 0;
00263   }
00264 
00265 #define GET_CSBI(PAR) \
00266   CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;\
00267   GetCSBI(&ConsoleScreenBufferInfo);\
00268   besALLOC_RETURN_LONG;\
00269   LONGVALUE(besRETURNVALUE) = ConsoleScreenBufferInfo.PAR;
00270 
00281 besFUNCTION(sbsizx)
00282 #ifdef WIN32
00283   GET_CSBI(dwSize.X)
00284 #else
00285   return COMMAND_ERROR_NOTIMP;
00286 #endif
00287 besEND
00288 
00299 besFUNCTION(sbsizy)
00300 #ifdef WIN32
00301   GET_CSBI(dwSize.Y)
00302 #else
00303   return COMMAND_ERROR_NOTIMP;
00304 #endif
00305 besEND
00306 
00314 besFUNCTION(sbcurx)
00315 #ifdef WIN32
00316   GET_CSBI(dwCursorPosition.X)
00317 #else
00318   return COMMAND_ERROR_NOTIMP;
00319 #endif
00320 besEND
00321 
00329 besFUNCTION(sbcury)
00330 #ifdef WIN32
00331   GET_CSBI(dwCursorPosition.Y)
00332 #else
00333   return COMMAND_ERROR_NOTIMP;
00334 #endif
00335 besEND
00336 
00345 besFUNCTION(sbscrx)
00346 #ifdef WIN32
00347   CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
00348   GetCSBI(&ConsoleScreenBufferInfo);
00349   besALLOC_RETURN_LONG;
00350   LONGVALUE(besRETURNVALUE) = ConsoleScreenBufferInfo.srWindow.Right -
00351                               ConsoleScreenBufferInfo.srWindow.Left + 1 ;
00352 #else
00353   return COMMAND_ERROR_NOTIMP;
00354 #endif
00355 besEND
00356 
00365 besFUNCTION(sbscry)
00366 #ifdef WIN32
00367   CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
00368   GetCSBI(&ConsoleScreenBufferInfo);
00369   besALLOC_RETURN_LONG;
00370   LONGVALUE(besRETURNVALUE) = ConsoleScreenBufferInfo.srWindow.Bottom -
00371                               ConsoleScreenBufferInfo.srWindow.Top + 1;
00372 #else
00373   return COMMAND_ERROR_NOTIMP;
00374 #endif
00375 besEND
00376 
00385 besFUNCTION(sbmaxx)
00386 #ifdef WIN32
00387   HANDLE hConsole;
00388   COORD crd;
00389 
00390   GET_CONSOLE_HANDLE
00391   crd = GetLargestConsoleWindowSize(hConsole);
00392 
00393   besALLOC_RETURN_LONG;
00394   LONGVALUE(besRETURNVALUE) = crd.X;
00395 #else
00396   return COMMAND_ERROR_NOTIMP;
00397 #endif
00398 besEND
00399 
00408 besFUNCTION(sbmaxy)
00409 #ifdef WIN32
00410   HANDLE hConsole;
00411   COORD crd;
00412 
00413   GET_CONSOLE_HANDLE
00414   crd = GetLargestConsoleWindowSize(hConsole);
00415 
00416   besALLOC_RETURN_LONG;
00417   LONGVALUE(besRETURNVALUE) = crd.Y;
00418 #else
00419   return COMMAND_ERROR_NOTIMP;
00420 #endif
00421 besEND
00422 
00430 besFUNCTION(sbsetcw)
00431 #ifdef WIN32
00432   HANDLE hConsole;
00433   int iError;
00434   long x,y,yb;
00435   SMALL_RECT sr;
00436   COORD crd;
00437 
00438   if( besARGNR < 3 )return COMMAND_ERROR_FEW_ARGS;
00439 
00440   iError = besGETARGS "iii",&x,&y,&yb besGETARGE
00441   if( iError )return iError;
00442   crd.X = (short)x;
00443   crd.Y = (short)yb;
00444   
00445   GET_CONSOLE_HANDLE
00446   SetConsoleScreenBufferSize(hConsole,crd);
00447 
00448   sr.Left   = 0;
00449   sr.Top    = 0;
00450   sr.Right  = (short)x;
00451   sr.Bottom = (short)yb;
00452   SetConsoleWindowInfo(hConsole,1,&sr);
00453 #else
00454   return COMMAND_ERROR_NOTIMP;
00455 #endif
00456 besEND
00457 
00466 besFUNCTION(sbsetcur)
00467 #ifdef WIN32
00468   HANDLE hConsole;
00469   int iError;
00470   long size;
00471   CONSOLE_CURSOR_INFO  cci;
00472 
00473   if( besARGNR < 1 )return COMMAND_ERROR_FEW_ARGS;
00474 
00475   iError = besGETARGS "i",&size besGETARGE
00476   if( iError )return iError;
00477   cci.dwSize = size;
00478   cci.bVisible = size ? 1 :0;
00479   GET_CONSOLE_HANDLE
00480   SetConsoleCursorInfo(hConsole,&cci);
00481 #else
00482   return COMMAND_ERROR_NOTIMP;
00483 #endif
00484 besEND
00528 besFUNCTION(sbsetcol)
00529 #ifdef WIN32
00530   HANDLE hConsole;
00531   int iError;
00532   long color;
00533 
00534   if( besARGNR < 1 )return COMMAND_ERROR_FEW_ARGS;
00535 
00536   iError = besGETARGS "i",&color besGETARGE
00537   if( iError )return iError;
00538   GET_CONSOLE_HANDLE
00539   SetConsoleTextAttribute(hConsole,(WORD)color);
00540 #else
00541   return COMMAND_ERROR_NOTIMP;
00542 #endif
00543 besEND
00544 
00545 #define PERR(X,Y) if( !X )return 0
00546 static int cls( HANDLE hConsole ){
00547     COORD coordScreen = { 0, 0 };    /* here's where we'll home the
00548                                         cursor */
00549     BOOL bSuccess;
00550     DWORD cCharsWritten;
00551     CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
00552     DWORD dwConSize;                 /* number of character cells in
00553                                         the current buffer */
00554 
00555     /* get the number of character cells in the current buffer */
00556 
00557     bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
00558     PERR( bSuccess, "GetConsoleScreenBufferInfo" );
00559     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
00560 
00561     /* fill the entire screen with blanks */
00562 
00563     bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
00564        dwConSize, coordScreen, &cCharsWritten );
00565     PERR( bSuccess, "FillConsoleOutputCharacter" );
00566 
00567     /* get the current text attribute */
00568 
00569     bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
00570     PERR( bSuccess, "ConsoleScreenBufferInfo" );
00571 
00572     /* now set the buffer's attributes accordingly */
00573 
00574     bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
00575        dwConSize, coordScreen, &cCharsWritten );
00576     PERR( bSuccess, "FillConsoleOutputAttribute" );
00577 
00578     /* put the cursor at (0, 0) */
00579 
00580     bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
00581     PERR( bSuccess, "SetConsoleCursorPosition" );
00582     return 0;
00583  }
00584 
00592 besFUNCTION(sbcls)
00593 #ifdef WIN32
00594   HANDLE hConsole;
00595 
00596   GET_CONSOLE_HANDLE
00597   return cls(hConsole);
00598 #else
00599   return COMMAND_ERROR_NOTIMP;
00600 #endif
00601 besEND
00602 
00603 
00604 
00605 besVERSION_NEGOTIATE
00606 
00607   return (int)INTERFACE_VERSION;
00608 
00609 besEND
00610 
00611 besSUB_FINISH
00612 
00613 besEND
00614 
00615 SLFST CIO_SLFST[] ={
00616 
00617 { "shutmodu" , shutmodu },
00618 { "bootmodu" , bootmodu },
00619 { "gotoxy" , gotoxy },
00620 { "sbkbhit" , sbkbhit },
00621 { "sbgetch" , sbgetch },
00622 { "sbgetche" , sbgetche },
00623 { "sbdetach" , sbdetach },
00624 { "sbgettitle" , sbgettitle },
00625 { "sbsettitle" , sbsettitle },
00626 { "sbnobreak" , sbnobreak },
00627 { "sbbreak" , sbbreak },
00628 { "sbsizx" , sbsizx },
00629 { "sbsizy" , sbsizy },
00630 { "sbcurx" , sbcurx },
00631 { "sbcury" , sbcury },
00632 { "sbscrx" , sbscrx },
00633 { "sbscry" , sbscry },
00634 { "sbmaxx" , sbmaxx },
00635 { "sbmaxy" , sbmaxy },
00636 { "sbsetcw" , sbsetcw },
00637 { "sbsetcur" , sbsetcur },
00638 { "sbsetcol" , sbsetcol },
00639 { "sbcls" , sbcls },
00640 { "versmodu" , versmodu },
00641 { "finimodu" , finimodu },
00642 { NULL , NULL }
00643   };

Generated on Sun Mar 12 23:56:28 2006 for ScriptBasic by  doxygen 1.4.6-NO