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