home *** CD-ROM | disk | FTP | other *** search
/ ftp.rarlab.com / 2014.05.ftp.rarlab.com.tar / ftp.rarlab.com / rar / unrar_vms_alpha-3.6.5.zip / consio.cxx < prev    next >
C/C++ Source or Header  |  2006-06-27  |  6KB  |  295 lines

  1. #include "rar.hpp"
  2.  
  3. #ifndef GUI
  4. #include "log.cpp"
  5. #endif
  6.  
  7. #if !defined(GUI) && !defined(SILENT)
  8. static void RawPrint(char *Msg,MESSAGE_TYPE MessageType);
  9. #endif
  10.  
  11. static MESSAGE_TYPE MsgStream=MSG_STDOUT;
  12. static bool Sound=false;
  13. const int MaxMsgSize=2*NM+2048;
  14.  
  15. void InitConsoleOptions(MESSAGE_TYPE MsgStream,bool Sound)
  16. {
  17.   ::MsgStream=MsgStream;
  18.   ::Sound=Sound;
  19. }
  20.  
  21. #if !defined(GUI) && !defined(SILENT)
  22. void mprintf(const char *fmt,...)
  23. {
  24.   if (MsgStream==MSG_NULL || MsgStream==MSG_ERRONLY)
  25.     return;
  26.   safebuf char Msg[MaxMsgSize];
  27.   va_list argptr;
  28.   va_start(argptr,fmt);
  29.   vsprintf(Msg,fmt,argptr);
  30.   RawPrint(Msg,MsgStream);
  31.   va_end(argptr);
  32. }
  33. #endif
  34.  
  35.  
  36. #if !defined(GUI) && !defined(SILENT)
  37. void eprintf(const char *fmt,...)
  38. {
  39.   if (MsgStream==MSG_NULL)
  40.     return;
  41.   safebuf char Msg[MaxMsgSize];
  42.   va_list argptr;
  43.   va_start(argptr,fmt);
  44.   vsprintf(Msg,fmt,argptr);
  45.   RawPrint(Msg,MSG_STDERR);
  46.   va_end(argptr);
  47. }
  48. #endif
  49.  
  50.  
  51. #if !defined(GUI) && !defined(SILENT)
  52. void RawPrint(char *Msg,MESSAGE_TYPE MessageType)
  53. {
  54.   File OutFile;
  55.   switch(MessageType)
  56.   {
  57.     case MSG_STDOUT:
  58.       OutFile.SetHandleType(FILE_HANDLESTD);
  59.       break;
  60.     case MSG_STDERR:
  61.     case MSG_ERRONLY:
  62.       OutFile.SetHandleType(FILE_HANDLEERR);
  63.       break;
  64.     default:
  65.       return;
  66.   }
  67. #ifdef _WIN_32
  68.   CharToOem(Msg,Msg);
  69.  
  70.   char OutMsg[MaxMsgSize],*OutPos=OutMsg;
  71.   for (int I=0;Msg[I]!=0;I++)
  72.   {
  73.     if (Msg[I]=='\n' && (I==0 || Msg[I-1]!='\r'))
  74.       *(OutPos++)='\r';
  75.     *(OutPos++)=Msg[I];
  76.   }
  77.   *OutPos=0;
  78.   strcpy(Msg,OutMsg);
  79. #endif
  80. #if defined(_UNIX) || defined(_EMX)
  81.   char OutMsg[MaxMsgSize],*OutPos=OutMsg;
  82.   for (int I=0;Msg[I]!=0;I++)
  83.     if (Msg[I]!='\r')
  84.       *(OutPos++)=Msg[I];
  85.   *OutPos=0;
  86.   strcpy(Msg,OutMsg);
  87. #endif
  88.  
  89.   OutFile.Write(Msg,strlen(Msg));
  90. //  OutFile.Flush();
  91. }
  92. #endif
  93.  
  94.  
  95. #ifndef SILENT
  96. void Alarm()
  97. {
  98. #ifndef SFX_MODULE
  99.   if (Sound)
  100.     putchar('\007');
  101. #endif
  102. }
  103. #endif
  104.  
  105.  
  106.  
  107. #ifndef SILENT
  108. #ifndef GUI
  109. void GetPasswordText(char *Str,int MaxLength)
  110. {
  111. #ifdef _WIN_32
  112.   HANDLE hConIn=GetStdHandle(STD_INPUT_HANDLE);
  113.   HANDLE hConOut=GetStdHandle(STD_OUTPUT_HANDLE);
  114.   DWORD ConInMode,ConOutMode;
  115.   DWORD Read=0;
  116.   GetConsoleMode(hConIn,&ConInMode);
  117.   GetConsoleMode(hConOut,&ConOutMode);
  118.   SetConsoleMode(hConIn,ENABLE_LINE_INPUT);
  119.   SetConsoleMode(hConOut,ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT);
  120.   ReadConsole(hConIn,Str,MaxLength-1,&Read,NULL);
  121.   Str[Read]=0;
  122.   OemToChar(Str,Str);
  123.   SetConsoleMode(hConIn,ConInMode);
  124.   SetConsoleMode(hConOut,ConOutMode);
  125. #elif defined(_EMX) || defined(_BEOS) || defined(__sparc) || defined(sparc) || defined (__VMS)
  126.   fgets(Str,MaxLength-1,stdin);
  127. #else
  128.   strncpy(Str,getpass(""),MaxLength-1);
  129. #endif
  130.   RemoveLF(Str);
  131. }
  132. #endif
  133. #endif
  134.  
  135.  
  136. #if !defined(GUI) && !defined(SILENT)
  137. unsigned int GetKey()
  138. {
  139. #ifdef SILENT
  140.   return(0);
  141. #else
  142.   char Str[80];
  143. #ifdef __GNUC__
  144.   fgets(Str,sizeof(Str),stdin);
  145.   return(Str[0]);
  146. #else
  147.   File SrcFile;
  148.   SrcFile.SetHandleType(FILE_HANDLESTD);
  149.   SrcFile.Read(Str,sizeof(Str));
  150.   return(Str[0]);
  151. #endif
  152. #endif
  153. }
  154. #endif
  155.  
  156.  
  157. #ifndef SILENT
  158. bool GetPassword(PASSWORD_TYPE Type,const char *FileName,char *Password,int MaxLength)
  159. {
  160.   Alarm();
  161.   while (true)
  162.   {
  163.     char PromptStr[256];
  164. #if defined(_EMX) || defined(_BEOS)
  165.     strcpy(PromptStr,St(MAskPswEcho));
  166. #else
  167.     strcpy(PromptStr,St(MAskPsw));
  168. #endif
  169.     if (Type!=PASSWORD_GLOBAL)
  170.     {
  171.       strcat(PromptStr,St(MFor));
  172.       strcat(PromptStr,PointToName(FileName));
  173.     }
  174.     eprintf("\n%s: ",PromptStr);
  175.     GetPasswordText(Password,MaxLength);
  176.     if (*Password==0 && Type==PASSWORD_GLOBAL)
  177.       return(false);
  178.     if (Type==PASSWORD_GLOBAL)
  179.     {
  180.       strcpy(PromptStr,St(MReAskPsw));
  181.       eprintf(PromptStr);
  182.       char CmpStr[256];
  183.       GetPasswordText(CmpStr,sizeof(CmpStr));
  184.       if (*CmpStr==0 || strcmp(Password,CmpStr)!=0)
  185.       {
  186.         strcpy(PromptStr,St(MNotMatchPsw));
  187. /*
  188. #ifdef _WIN_32
  189.         CharToOem(PromptStr,PromptStr);
  190. #endif
  191. */
  192.         eprintf(PromptStr);
  193.         memset(Password,0,MaxLength);
  194.         memset(CmpStr,0,sizeof(CmpStr));
  195.         continue;
  196.       }
  197.       memset(CmpStr,0,sizeof(CmpStr));
  198.     }
  199.     break;
  200.   }
  201.   return(true);
  202. }
  203. #endif
  204.  
  205.  
  206. #if !defined(GUI) && !defined(SILENT)
  207. int Ask(const char *AskStr)
  208. {
  209.   const int MaxItems=10;
  210.   char Item[MaxItems][40];
  211.   int ItemKeyPos[MaxItems],NumItems=0;
  212.  
  213.   for (const char *NextItem=AskStr;NextItem!=NULL;NextItem=strchr(NextItem+1,'_'))
  214.   {
  215.     char *CurItem=Item[NumItems];
  216.     strncpy(CurItem,NextItem+1,sizeof(Item[0]));
  217.     char *EndItem=strchr(CurItem,'_');
  218.     if (EndItem!=NULL)
  219.       *EndItem=0;
  220.     int KeyPos=0,CurKey;
  221.     while ((CurKey=CurItem[KeyPos])!=0)
  222.     {
  223.       bool Found=false;
  224.       for (int I=0;I<NumItems && !Found;I++)
  225.         if (loctoupper(Item[I][ItemKeyPos[I]])==loctoupper(CurKey))
  226.           Found=true;
  227.       if (!Found && CurKey!=' ')
  228.         break;
  229.       KeyPos++;
  230.     }
  231.     ItemKeyPos[NumItems]=KeyPos;
  232.     NumItems++;
  233.   }
  234.  
  235.   for (int I=0;I<NumItems;I++)
  236.   {
  237.     eprintf(I==0 ? (NumItems>4 ? "\n":" "):", ");
  238.     int KeyPos=ItemKeyPos[I];
  239.     for (int J=0;J<KeyPos;J++)
  240.       eprintf("%c",Item[I][J]);
  241.     eprintf("[%c]%s",Item[I][KeyPos],&Item[I][KeyPos+1]);
  242.   }
  243.   eprintf(" ");
  244.   int Ch=GetKey();
  245. #if defined(_WIN_32)
  246.   OemToCharBuff((LPCSTR)&Ch,(LPTSTR)&Ch,1);
  247. #endif
  248.   Ch=loctoupper(Ch);
  249.   for (int I=0;I<NumItems;I++)
  250.     if (Ch==Item[I][ItemKeyPos[I]])
  251.       return(I+1);
  252.   return(0);
  253. }
  254. #endif
  255.  
  256.  
  257. int KbdAnsi(char *Addr,int Size)
  258. {
  259.   int RetCode=0;
  260. #ifndef GUI
  261.   for (int I=0;I<Size;I++)
  262.     if (Addr[I]==27 && Addr[I+1]=='[')
  263.     {
  264.       for (int J=I+2;J<Size;J++)
  265.       {
  266.         if (Addr[J]=='\"')
  267.           return(2);
  268.         if (!isdigit(Addr[J]) && Addr[J]!=';')
  269.           break;
  270.       }
  271.       RetCode=1;
  272.     }
  273. #endif
  274.   return(RetCode);
  275. }
  276.  
  277.  
  278. void OutComment(char *Comment,int Size)
  279. {
  280. #ifndef GUI
  281.   if (KbdAnsi(Comment,Size)==2)
  282.     return;
  283.   const int MaxOutSize=0x400;
  284.   for (int I=0;I<Size;I+=MaxOutSize)
  285.   {
  286.     char Msg[MaxOutSize+1];
  287.     int CopySize=Min(MaxOutSize,Size-I);
  288.     strncpy(Msg,Comment+I,CopySize);
  289.     Msg[CopySize]=0;
  290.     mprintf("%s",Msg);
  291.   }
  292.   mprintf("\n");
  293. #endif
  294. }
  295.