/Common/Cmdline.c

https://github.com/pcmac77/truecrypt-android · C · 244 lines · 188 code · 39 blank · 17 comment · 53 complexity · 1685d31d34b0900021d88b519b353d9a MD5 · raw file

  1. /*
  2. Legal Notice: Some portions of the source code contained in this file were
  3. derived from the source code of Encryption for the Masses 2.02a, which is
  4. Copyright (c) 1998-2000 Paul Le Roux and which is governed by the 'License
  5. Agreement for Encryption for the Masses'. Modifications and additions to
  6. the original source code (contained in this file) and all other portions
  7. of this file are Copyright (c) 2003-2009 TrueCrypt Developers Association
  8. and are governed by the TrueCrypt License 3.0 the full text of which is
  9. contained in the file License.txt included in TrueCrypt binary and source
  10. code distribution packages. */
  11. #include "Tcdefs.h"
  12. #include <malloc.h>
  13. #include <ctype.h>
  14. #include "Cmdline.h"
  15. #include "Resource.h"
  16. #include "Crypto.h"
  17. #include "Apidrvr.h"
  18. #include "Dlgcode.h"
  19. #include "Language.h"
  20. /* Except in response to the WM_INITDIALOG message, the dialog box procedure
  21. should return nonzero if it processes the message, and zero if it does
  22. not. - see DialogProc */
  23. BOOL CALLBACK CommandHelpDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  24. {
  25. if (lParam); /* remove warning */
  26. if (wParam); /* remove warning */
  27. switch (msg)
  28. {
  29. case WM_INITDIALOG:
  30. {
  31. char * tmp = err_malloc(8192);
  32. char tmp2[MAX_PATH * 2];
  33. argumentspec *as;
  34. int i;
  35. LocalizeDialog (hwndDlg, "IDD_COMMANDHELP_DLG");
  36. as = (argumentspec*) lParam;
  37. *tmp = 0;
  38. strcpy (tmp, "Command line options:\n\n");
  39. for (i = 0; i < as->arg_cnt; i ++)
  40. {
  41. if (!as->args[i].Internal)
  42. {
  43. sprintf(tmp2, "%s\t%s\n", as->args[i].short_name, as->args[i].long_name);
  44. strcat(tmp,tmp2);
  45. }
  46. }
  47. SetWindowText (GetDlgItem (hwndDlg, IDC_COMMANDHELP_TEXT), (char*) tmp);
  48. return 1;
  49. }
  50. case WM_COMMAND:
  51. EndDialog (hwndDlg, IDOK);
  52. return 1;
  53. case WM_CLOSE:
  54. EndDialog (hwndDlg, 0);
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. int Win32CommandLine (char *lpszCommandLine, char ***lpszArgs)
  60. {
  61. int argumentCount;
  62. int i;
  63. LPWSTR *arguments = CommandLineToArgvW (GetCommandLineW(), &argumentCount);
  64. if (!arguments)
  65. {
  66. handleWin32Error (NULL);
  67. return 0;
  68. }
  69. --argumentCount;
  70. if (argumentCount < 1)
  71. {
  72. LocalFree (arguments);
  73. return 0;
  74. }
  75. *lpszArgs = malloc (sizeof (char *) * argumentCount);
  76. if (!*lpszArgs)
  77. AbortProcess ("OUTOFMEMORY");
  78. for (i = 0; i < argumentCount; ++i)
  79. {
  80. size_t argLen = wcslen (arguments[i + 1]);
  81. char *arg = malloc (argLen + 1);
  82. if (!arg)
  83. AbortProcess ("OUTOFMEMORY");
  84. if (argLen > 0)
  85. {
  86. int len = WideCharToMultiByte (CP_ACP, 0, arguments[i + 1], -1, arg, argLen + 1, NULL, NULL);
  87. if (len == 0)
  88. {
  89. handleWin32Error (NULL);
  90. AbortProcessSilent();
  91. }
  92. }
  93. else
  94. arg[0] = 0;
  95. (*lpszArgs)[i] = arg;
  96. }
  97. LocalFree (arguments);
  98. return argumentCount;
  99. }
  100. int GetArgSepPosOffset (char *lpszArgument)
  101. {
  102. if (lpszArgument[0] == '/')
  103. return 1;
  104. else if (lpszArgument[0] == '-' && lpszArgument[1] == '-')
  105. return 2;
  106. else if (lpszArgument[0] == '-')
  107. return 1;
  108. else
  109. return 0;
  110. }
  111. int GetArgumentID (argumentspec *as, char *lpszArgument, int *nArgPos)
  112. {
  113. char szTmp[MAX_PATH * 2];
  114. int i;
  115. i = strlen (lpszArgument);
  116. szTmp[i] = 0;
  117. while (--i >= 0)
  118. {
  119. szTmp[i] = (char) tolower (lpszArgument[i]);
  120. }
  121. for (i = 0; i < as->arg_cnt; i++)
  122. {
  123. size_t k;
  124. k = strlen (as->args[i].long_name);
  125. if (memcmp (as->args[i].long_name, szTmp, k * sizeof (char)) == 0)
  126. {
  127. int x;
  128. for (x = i + 1; x < as->arg_cnt; x++)
  129. {
  130. size_t m;
  131. m = strlen (as->args[x].long_name);
  132. if (memcmp (as->args[x].long_name, szTmp, m * sizeof (char)) == 0)
  133. {
  134. break;
  135. }
  136. }
  137. if (x == as->arg_cnt)
  138. {
  139. if (strlen (lpszArgument) != k)
  140. *nArgPos = k;
  141. else
  142. *nArgPos = 0;
  143. return as->args[i].Id;
  144. }
  145. }
  146. }
  147. for (i = 0; i < as->arg_cnt; i++)
  148. {
  149. size_t k;
  150. if (as->args[i].short_name[0] == 0)
  151. continue;
  152. k = strlen (as->args[i].short_name);
  153. if (memcmp (as->args[i].short_name, szTmp, k * sizeof (char)) == 0)
  154. {
  155. int x;
  156. for (x = i + 1; x < as->arg_cnt; x++)
  157. {
  158. size_t m;
  159. if (as->args[x].short_name[0] == 0)
  160. continue;
  161. m = strlen (as->args[x].short_name);
  162. if (memcmp (as->args[x].short_name, szTmp, m * sizeof (char)) == 0)
  163. {
  164. break;
  165. }
  166. }
  167. if (x == as->arg_cnt)
  168. {
  169. if (strlen (lpszArgument) != k)
  170. *nArgPos = k;
  171. else
  172. *nArgPos = 0;
  173. return as->args[i].Id;
  174. }
  175. }
  176. }
  177. return -1;
  178. }
  179. int GetArgumentValue (char **lpszCommandLineArgs, int nArgPos, int *nArgIdx,
  180. int nNoCommandLineArgs, char *lpszValue, int nValueSize)
  181. {
  182. *lpszValue = 0;
  183. if (nArgPos)
  184. {
  185. /* Handles the case of no space between parameter code and
  186. value */
  187. strncpy (lpszValue, &lpszCommandLineArgs[*nArgIdx][nArgPos], nValueSize);
  188. lpszValue[nValueSize - 1] = 0;
  189. return HAS_ARGUMENT;
  190. }
  191. else if (*nArgIdx + 1 < nNoCommandLineArgs)
  192. {
  193. int x = GetArgSepPosOffset (lpszCommandLineArgs[*nArgIdx + 1]);
  194. if (x == 0)
  195. {
  196. /* Handles the case of space between parameter code
  197. and value */
  198. strncpy (lpszValue, &lpszCommandLineArgs[*nArgIdx + 1][x], nValueSize);
  199. lpszValue[nValueSize - 1] = 0;
  200. (*nArgIdx)++;
  201. return HAS_ARGUMENT;
  202. }
  203. }
  204. return HAS_NO_ARGUMENT;
  205. }