ReactOS  0.4.15-dev-1207-g698a8e6
unattended.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Applications Manager
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Functions to parse command-line flags and process them
5  * COPYRIGHT: Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
6  * Copyright 2020 He Yang (1160386205@qq.com)
7  */
8 
9 #include "rapps.h"
10 #include "unattended.h"
11 #include "winmain.h"
12 #include <setupapi.h>
13 #include <conutils.h>
14 
15 BOOL MatchCmdOption(LPWSTR argvOption, LPCWSTR szOptToMacth)
16 {
17  WCHAR FirstCharList[] = { L'-', L'/' };
18 
19  for (UINT i = 0; i < _countof(FirstCharList); i++)
20  {
21  if (argvOption[0] == FirstCharList[i])
22  {
23  return StrCmpIW(argvOption + 1, szOptToMacth) == 0;
24  }
25  }
26  return FALSE;
27 }
28 
30 {
31  // First, try to attach to our parent's console
33  {
34  // Did we already have a console?
36  {
37  // No, try to open a new one
38  AllocConsole();
39  }
40  }
41  ConInitStdStreams(); // Initialize the Console Standard Streams
42 }
43 
44 
45 BOOL HandleInstallCommand(LPWSTR szCommand, int argcLeft, LPWSTR * argvLeft)
46 {
47  if (argcLeft == 0)
48  {
51  return FALSE;
52  }
53 
55 
56  for (int i = 0; i < argcLeft; i++)
57  {
58  PkgNameList.Add(argvLeft[i]);
59  }
60 
61  CAvailableApps apps;
62  apps.UpdateAppsDB();
64 
66  if (arrAppInfo.GetSize() > 0)
67  {
68  DownloadListOfApplications(arrAppInfo, TRUE);
69  return TRUE;
70  }
71  else
72  {
73  return FALSE;
74  }
75 }
76 
77 BOOL HandleSetupCommand(LPWSTR szCommand, int argcLeft, LPWSTR * argvLeft)
78 {
79  if (argcLeft != 1)
80  {
83  return FALSE;
84  }
85 
87  HINF InfHandle = SetupOpenInfFileW(argvLeft[0], NULL, INF_STYLE_WIN4, NULL);
88  if (InfHandle == INVALID_HANDLE_VALUE)
89  {
90  return FALSE;
91  }
92 
94  if (SetupFindFirstLineW(InfHandle, L"RAPPS", L"Install", &Context))
95  {
96  WCHAR szPkgName[MAX_PATH];
97  do
98  {
99  if (SetupGetStringFieldW(&Context, 1, szPkgName, _countof(szPkgName), NULL))
100  {
101  PkgNameList.Add(szPkgName);
102  }
103  } while (SetupFindNextLine(&Context, &Context));
104  }
105  SetupCloseInfFile(InfHandle);
106 
107  CAvailableApps apps;
108  apps.UpdateAppsDB();
110 
112  if (arrAppInfo.GetSize() > 0)
113  {
114  DownloadListOfApplications(arrAppInfo, TRUE);
115  return TRUE;
116  }
117  else
118  {
119  return FALSE;
120  }
121 }
122 
124 {
125  LPCWSTR lpszSearch = (LPCWSTR)param;
126  if (!SearchPatternMatch(Info->m_szName, lpszSearch) &&
127  !SearchPatternMatch(Info->m_szDesc, lpszSearch))
128  {
129  return TRUE;
130  }
131 
132  ConPrintf(StdOut, L"%s (%s)\n", Info->m_szName.GetString(), Info->m_szPkgName.GetString());
133  return TRUE;
134 }
135 
136 BOOL HandleFindCommand(LPWSTR szCommand, int argcLeft, LPWSTR *argvLeft)
137 {
138  if (argcLeft < 1)
139  {
141  return FALSE;
142  }
143 
144  CAvailableApps apps;
145  apps.UpdateAppsDB();
146 
147  for (int i = 0; i < argcLeft; i++)
148  {
150  apps.Enum(ENUM_ALL_AVAILABLE, CmdFindAppEnum, argvLeft[i]);
151  ConPrintf(StdOut, L"\n");
152  }
153 
154  return TRUE;
155 }
156 
157 BOOL HandleInfoCommand(LPWSTR szCommand, int argcLeft, LPWSTR *argvLeft)
158 {
159  if (argcLeft < 1)
160  {
162  return FALSE;
163  }
164 
165  CAvailableApps apps;
166  apps.UpdateAppsDB();
168 
169  for (int i = 0; i < argcLeft; i++)
170  {
171  CAvailableApplicationInfo *AppInfo = apps.FindAppByPkgName(argvLeft[i]);
172  if (!AppInfo)
173  {
175  }
176  else
177  {
179  // TODO: code about extracting information from CAvailableApplicationInfo (in appview.cpp, class CAppRichEdit)
180  // is in a mess. It should be refactored, and should not placed in class CAppRichEdit.
181  // and the code here should reused that code after refactor.
182 
183  ConPuts(StdOut, AppInfo->m_szName);
184 
185  if (AppInfo->m_szVersion)
186  {
188  ConPuts(StdOut, AppInfo->m_szVersion);
189  }
190 
191  if (AppInfo->m_szLicense)
192  {
194  ConPuts(StdOut, AppInfo->m_szLicense);
195  }
196 
197  if (AppInfo->m_szSize)
198  {
200  ConPuts(StdOut, AppInfo->m_szSize);
201  }
202 
203  if (AppInfo->m_szUrlSite)
204  {
206  ConPuts(StdOut, AppInfo->m_szUrlSite);
207  }
208 
209  if (AppInfo->m_szDesc)
210  {
212  ConPuts(StdOut, AppInfo->m_szDesc);
213  }
214 
215  if (AppInfo->m_szUrlDownload)
216  {
218  ConPuts(StdOut, AppInfo->m_szUrlDownload);
219  }
220 
221  ConPrintf(StdOut, L"\n");
222  }
223  ConPrintf(StdOut, L"\n");
224  }
225  return TRUE;
226 }
227 
229 {
230  ConPrintf(StdOut, L"\n");
232  ConPrintf(StdOut, L"\n\n");
233 
235  ConPrintf(StdOut, L"%ls\n", UsageString);
236 }
237 
238 BOOL ParseCmdAndExecute(LPWSTR lpCmdLine, BOOL bIsFirstLaunch, int nCmdShow)
239 {
240  INT argc;
241  LPWSTR *argv = CommandLineToArgvW(lpCmdLine, &argc);
242 
243  if (!argv)
244  {
245  return FALSE;
246  }
247 
248  if (argc == 1) // RAPPS is launched without options
249  {
250  // Check for if rapps MainWindow is already launched in another process
251  HANDLE hMutex;
252 
254  if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
255  {
256  /* If already started, it is found its window */
257  HWND hWindow = FindWindowW(szWindowClass, NULL);
258 
259  /* Activate window */
260  ShowWindow(hWindow, SW_SHOWNORMAL);
261  SetForegroundWindow(hWindow);
262  return FALSE;
263  }
264 
265  if (SettingsInfo.bUpdateAtStart || bIsFirstLaunch)
267 
268  MainWindowLoop(nCmdShow);
269 
270  if (hMutex)
272 
273  return TRUE;
274  }
275 
277  {
278  return HandleInstallCommand(argv[1], argc - 2, argv + 2);
279  }
280  else if (MatchCmdOption(argv[1], CMD_KEY_SETUP))
281  {
282  return HandleSetupCommand(argv[1], argc - 2, argv + 2);
283  }
284 
286 
288  {
289  return HandleFindCommand(argv[1], argc - 2, argv + 2);
290  }
291  else if (MatchCmdOption(argv[1], CMD_KEY_INFO))
292  {
293  return HandleInfoCommand(argv[1], argc - 2, argv + 2);
294  }
296  {
298  return TRUE;
299  }
300  else
301  {
302  // unrecognized/invalid options
305  return FALSE;
306  }
307 }
#define IDS_AINFO_VERSION
Definition: resource.h:161
BOOL HandleInfoCommand(LPWSTR szCommand, int argcLeft, LPWSTR *argvLeft)
Definition: unattended.cpp:157
#define IDS_CMD_FIND_RESULT_FOR
Definition: resource.h:227
BOOL WINAPI SetupFindFirstLineW(IN HINF InfHandle, IN PCWSTR Section, IN PCWSTR Key, IN OUT PINFCONTEXT Context)
Definition: infsupp.c:54
BOOL bUpdateAtStart
Definition: settings.h:8
static int argc
Definition: ServiceArgs.c:12
const WCHAR UsageString[]
Definition: unattended.h:11
BOOL WINAPI AllocConsole(VOID)
Definition: console.c:74
#define CloseHandle
Definition: compat.h:598
ATL::CStringW m_szLicense
Definition: available.h:49
BOOL Add(const T &t)
Definition: atlsimpcoll.h:58
#define CMD_KEY_INFO
Definition: unattended.h:6
#define INF_STYLE_WIN4
Definition: infsupp.h:41
#define CMD_KEY_SETUP
Definition: unattended.h:4
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define IDS_APPTITLE
Definition: resource.h:3
#define TRUE
Definition: types.h:120
#define IDS_CMD_PACKAGE_INFO
Definition: resource.h:229
#define CMD_KEY_INSTALL
Definition: unattended.h:3
#define IDS_AINFO_LICENSE
Definition: resource.h:165
#define IDS_AINFO_URLSITE
Definition: resource.h:164
#define CALLBACK
Definition: compat.h:35
INT ConPuts(IN PCON_STREAM Stream, IN PCWSTR szStr)
Definition: outstream.c:427
ATL::CStringW m_szUrlSite
Definition: available.h:52
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
CAvailableApplicationInfo * FindAppByPkgName(const ATL::CStringW &szPkgName) const
Definition: available.cpp:573
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define IDS_CMD_INVALID_OPTION
Definition: resource.h:226
HINF WINAPI SetupOpenInfFileW(PCWSTR name, PCWSTR class, DWORD style, UINT *error)
Definition: parser.c:1229
#define argv
Definition: mplay32.c:18
#define CMD_KEY_HELP
Definition: unattended.h:7
static BOOL UpdateAppsDB()
Definition: available.cpp:409
int GetSize() const
Definition: atlsimpcoll.h:104
int32_t INT
Definition: typedefs.h:58
struct TraceInfo Info
VOID WINAPI SetupCloseInfFile(IN HINF InfHandle)
Definition: infsupp.c:43
BOOL SearchPatternMatch(LPCWSTR szHaystack, LPCWSTR szNeedle)
Definition: misc.cpp:526
VOID MainWindowLoop(INT nShowCmd)
Definition: gui.cpp:862
ATL::CStringW m_szDesc
Definition: available.h:50
const TCHAR szWindowClass[]
Definition: magnifier.c:28
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define ConInitStdStreams()
Definition: stream.h:128
#define FALSE
Definition: types.h:117
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
ATL::CStringW m_szUrlDownload
Definition: available.h:53
unsigned int BOOL
Definition: ntddk_ex.h:94
ATL::CStringW m_szSize
Definition: available.h:51
LPWSTR *WINAPI CommandLineToArgvW(LPCWSTR lpCmdline, int *numargs)
Definition: shell32_main.c:79
smooth NULL
Definition: ftsmooth.c:416
#define IDS_CMD_NEED_PARAMS
Definition: resource.h:225
BOOL HandleSetupCommand(LPWSTR szCommand, int argcLeft, LPWSTR *argvLeft)
Definition: unattended.cpp:77
HANDLE hMutex
Definition: mutex.c:11
#define CMD_KEY_FIND
Definition: unattended.h:5
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
ATL::CSimpleArray< CAvailableApplicationInfo > FindAppsByPkgNameList(const ATL::CSimpleArray< ATL::CStringW > &arrAppsNames) const
Definition: available.cpp:594
void InitRappsConsole()
Definition: unattended.cpp:29
SETTINGS_INFO SettingsInfo
Definition: winmain.cpp:20
BOOL Enum(INT EnumType, AVAILENUMPROC lpEnumProc, PVOID param)
Definition: available.cpp:448
__wchar_t WCHAR
Definition: xmlstorage.h:180
HWND WINAPI FindWindowW(_In_opt_ LPCWSTR, _In_opt_ LPCWSTR)
ATL::CStringW m_szVersion
Definition: available.h:48
#define _countof(array)
Definition: sndvol32.h:68
GLfloat param
Definition: glext.h:5796
#define MAX_PATH
Definition: compat.h:34
#define IDS_CMD_NEED_FILE_NAME
Definition: resource.h:224
BOOL MatchCmdOption(LPWSTR argvOption, LPCWSTR szOptToMacth)
Definition: unattended.cpp:15
#define IDS_CMD_NEED_PACKAGE_NAME
Definition: resource.h:223
VOID PrintHelpCommand()
Definition: unattended.cpp:228
#define CMD_KEY_HELP_ALT
Definition: unattended.h:8
BOOL HandleInstallCommand(LPWSTR szCommand, int argcLeft, LPWSTR *argvLeft)
Definition: unattended.cpp:45
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
static const WCHAR L[]
Definition: oid.c:1250
INT __cdecl ConPrintf(IN PCON_STREAM Stream, IN PCWSTR szStr,...)
Definition: outstream.c:520
ATL::CStringW m_szName
Definition: available.h:46
static BOOL ForceUpdateAppsDB()
Definition: available.cpp:442
#define IDS_AINFO_SIZE
Definition: resource.h:163
BOOL CALLBACK CmdFindAppEnum(CAvailableApplicationInfo *Info, BOOL bInitialCheckState, PVOID param)
Definition: unattended.cpp:123
#define IDS_AINFO_URLDOWNLOAD
Definition: resource.h:166
#define SW_SHOWNORMAL
Definition: winuser.h:764
HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW(IN LPSECURITY_ATTRIBUTES lpMutexAttributes OPTIONAL, IN BOOL bInitialOwner, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:576
HRESULT GetString(REFGUID guidKey, [out, size_is(cchBufSize)] LPWSTR pwszValue, UINT32 cchBufSize, [in, out, ptr] UINT32 *pcchLength)
unsigned int UINT
Definition: ndis.h:50
#define ATTACH_PARENT_PROCESS
Definition: wincon.h:21
BOOL WINAPI SetupFindNextLine(IN PINFCONTEXT ContextIn, OUT PINFCONTEXT ContextOut)
Definition: infsupp.c:80
BOOL DownloadListOfApplications(const ATL::CSimpleArray< CAvailableApplicationInfo > &AppsList, BOOL bIsModal)
Definition: loaddlg.cpp:1000
#define IDS_CMD_USAGE
Definition: resource.h:222
BOOL WINAPI AttachConsole(IN DWORD dwProcessId)
Definition: console.c:144
#define StdOut
Definition: stream.h:82
struct tagContext Context
Definition: acpixf.h:1034
BOOL WINAPI SetForegroundWindow(_In_ HWND)
BOOL HandleFindCommand(LPWSTR szCommand, int argcLeft, LPWSTR *argvLeft)
Definition: unattended.cpp:136
#define IDS_CMD_PACKAGE_NOT_FOUND
Definition: resource.h:228
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define IDS_AINFO_DESCRIPTION
Definition: resource.h:162
int WINAPI StrCmpIW(LPCWSTR lpszStr, LPCWSTR lpszComp)
Definition: string.c:353
INT __cdecl ConResMsgPrintf(IN PCON_STREAM Stream, IN DWORD dwFlags, IN UINT uID,...)
Definition: outstream.c:1461
BOOL ParseCmdAndExecute(LPWSTR lpCmdLine, BOOL bIsFirstLaunch, int nCmdShow)
Definition: unattended.cpp:238
BOOL WINAPI SetupGetStringFieldW(IN PINFCONTEXT Context, IN ULONG FieldIndex, OUT PWSTR ReturnBuffer, IN ULONG ReturnBufferSize, OUT PULONG RequiredSize)
Definition: infsupp.c:184