/lwraft/tools/vdcschema/parseargs.c

https://github.com/vmware/lightwave · C · 268 lines · 214 code · 40 blank · 14 comment · 36 complexity · 5456a337bf1ae002fe8e9a443ef92cec MD5 · raw file

  1. /*
  2. * Copyright © 2016 VMware, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the “License”); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an “AS IS” BASIS, without
  10. * warranties or conditions of any kind, EITHER EXPRESS OR IMPLIED. See the
  11. * License for the specific language governing permissions and limitations
  12. * under the License.
  13. */
  14. #include "includes.h"
  15. static
  16. DWORD
  17. _DeleteArgAt(
  18. PSTR* ppszArgs,
  19. DWORD dwDelIdx
  20. )
  21. {
  22. DWORD dwError = 0;
  23. DWORD i = 0;
  24. if (!ppszArgs)
  25. {
  26. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  27. BAIL_ON_VMDIR_ERROR(dwError);
  28. }
  29. VMDIR_SAFE_FREE_MEMORY(ppszArgs[dwDelIdx]);
  30. for (i = dwDelIdx; ppszArgs[i+1]; i++)
  31. {
  32. ppszArgs[i] = ppszArgs[i+1];
  33. ppszArgs[i+1] = NULL;
  34. }
  35. error:
  36. return dwError;
  37. }
  38. static
  39. DWORD
  40. _ParseConn(
  41. PSTR* ppszArgs,
  42. PVDC_SCHEMA_CONN* ppConn
  43. )
  44. {
  45. DWORD dwError = 0;
  46. DWORD i = 0;
  47. PSTR* ppszArg = NULL;
  48. PVDC_SCHEMA_CONN pConn = NULL;
  49. if (!ppszArgs || !ppConn)
  50. {
  51. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  52. BAIL_ON_VMDIR_ERROR(dwError);
  53. }
  54. dwError = VdcSchemaConnInit(&pConn);
  55. BAIL_ON_VMDIR_ERROR(dwError);
  56. i = 1;
  57. while (ppszArgs[i])
  58. {
  59. if (VmDirStringCompareA("--domain", ppszArgs[i], TRUE) == 0)
  60. {
  61. ppszArg = &pConn->pszDomain;
  62. }
  63. else if (VmDirStringCompareA("--host", ppszArgs[i], TRUE) == 0)
  64. {
  65. ppszArg = &pConn->pszHostName;
  66. }
  67. else if (VmDirStringCompareA("--login", ppszArgs[i], TRUE) == 0)
  68. {
  69. ppszArg = &pConn->pszUserName;
  70. }
  71. else if (VmDirStringCompareA("--passwd", ppszArgs[i], TRUE) == 0)
  72. {
  73. ppszArg = &pConn->pszPassword;
  74. }
  75. else
  76. {
  77. i++;
  78. continue;
  79. }
  80. if (!ppszArgs[i+1])
  81. {
  82. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  83. BAIL_ON_VMDIR_ERROR(dwError);
  84. }
  85. dwError = VmDirAllocateStringA(ppszArgs[i+1], ppszArg);
  86. BAIL_ON_VMDIR_ERROR(dwError);
  87. dwError = _DeleteArgAt(ppszArgs, i+1);
  88. BAIL_ON_VMDIR_ERROR(dwError);
  89. dwError = _DeleteArgAt(ppszArgs, i);
  90. BAIL_ON_VMDIR_ERROR(dwError);
  91. }
  92. dwError = VdcSchemaConnValidateAndSetDefault(pConn);
  93. BAIL_ON_VMDIR_ERROR(dwError);
  94. *ppConn = pConn;
  95. cleanup:
  96. return dwError;
  97. error:
  98. VdcSchemaFreeConn(pConn);
  99. goto cleanup;
  100. }
  101. static
  102. DWORD
  103. _ParseOpParam(
  104. PSTR* ppszArgs,
  105. PVDC_SCHEMA_OP_PARAM* ppOpParam
  106. )
  107. {
  108. DWORD dwError = 0;
  109. PVDC_SCHEMA_OP_PARAM pOpParam = NULL;
  110. if (!ppszArgs || !ppOpParam)
  111. {
  112. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  113. BAIL_ON_VMDIR_ERROR(dwError);
  114. }
  115. dwError = VdcSchemaOpParamInit(&pOpParam);
  116. BAIL_ON_VMDIR_ERROR(dwError);
  117. if (VmDirStringCompareA("get-supported-syntaxes", ppszArgs[0], TRUE) == 0)
  118. {
  119. pOpParam->opCode = OP_GET_SUPPORTED_SYNTAXES;
  120. }
  121. else if (VmDirStringCompareA("patch-schema-defs", ppszArgs[0], TRUE) == 0)
  122. {
  123. pOpParam->opCode = OP_PATCH_SCHEMA_DEFS;
  124. }
  125. else
  126. {
  127. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  128. BAIL_ON_VMDIR_ERROR(dwError);
  129. }
  130. dwError = _DeleteArgAt(ppszArgs, 0);
  131. BAIL_ON_VMDIR_ERROR(dwError);
  132. while (ppszArgs[0])
  133. {
  134. if (pOpParam->opCode == OP_PATCH_SCHEMA_DEFS &&
  135. VmDirStringCompareA("--file", ppszArgs[0], TRUE) == 0 &&
  136. ppszArgs[1])
  137. {
  138. dwError = VmDirAllocateStringA(ppszArgs[1], &pOpParam->pszFileName);
  139. BAIL_ON_VMDIR_ERROR(dwError);
  140. dwError = _DeleteArgAt(ppszArgs, 1);
  141. BAIL_ON_VMDIR_ERROR(dwError);
  142. dwError = _DeleteArgAt(ppszArgs, 0);
  143. BAIL_ON_VMDIR_ERROR(dwError);
  144. }
  145. else if (pOpParam->opCode == OP_PATCH_SCHEMA_DEFS &&
  146. VmDirStringCompareA("--dryrun", ppszArgs[0], TRUE) == 0)
  147. {
  148. pOpParam->bDryrun = TRUE;
  149. dwError = _DeleteArgAt(ppszArgs, 0);
  150. BAIL_ON_VMDIR_ERROR(dwError);
  151. }
  152. else
  153. {
  154. // Unrecognizable argument detected
  155. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  156. BAIL_ON_VMDIR_ERROR(dwError);
  157. }
  158. }
  159. *ppOpParam = pOpParam;
  160. cleanup:
  161. return dwError;
  162. error:
  163. VdcSchemaFreeOpParam(pOpParam);
  164. goto cleanup;
  165. }
  166. DWORD
  167. VdcSchemaParseArgs(
  168. int argc,
  169. char* argv[],
  170. PVDC_SCHEMA_CONN* ppConn,
  171. PVDC_SCHEMA_OP_PARAM* ppOpParam
  172. )
  173. {
  174. DWORD dwError = 0;
  175. int i = 0;
  176. PSTR* ppszArgs = NULL;
  177. PVDC_SCHEMA_CONN pConn = NULL;
  178. PVDC_SCHEMA_OP_PARAM pOpParam = NULL;
  179. if (argc < 4 || !ppConn || !ppOpParam)
  180. {
  181. dwError = VMDIR_ERROR_INVALID_PARAMETER;
  182. BAIL_ON_VMDIR_ERROR(dwError);
  183. }
  184. dwError = VmDirAllocateMemory(sizeof(PSTR) * argc, (PVOID*)&ppszArgs);
  185. BAIL_ON_VMDIR_ERROR(dwError);
  186. for (i = 1; i < argc; i++)
  187. {
  188. dwError = VmDirAllocateStringA(argv[i], &ppszArgs[i-1]);
  189. BAIL_ON_VMDIR_ERROR(dwError);
  190. }
  191. dwError = _ParseConn(ppszArgs, &pConn);
  192. BAIL_ON_VMDIR_ERROR(dwError);
  193. dwError = _ParseOpParam(ppszArgs, &pOpParam);
  194. BAIL_ON_VMDIR_ERROR(dwError);
  195. *ppConn = pConn;
  196. *ppOpParam = pOpParam;
  197. cleanup:
  198. VmDirFreeStrArray(ppszArgs);
  199. return dwError;
  200. error:
  201. VdcSchemaFreeConn(pConn);
  202. VdcSchemaFreeOpParam(pOpParam);
  203. VdcSchemaShowUsage();
  204. goto cleanup;
  205. }
  206. VOID
  207. VdcSchemaShowUsage(
  208. VOID
  209. )
  210. {
  211. printf( "Usage: vdcschema { arguments }\n"
  212. "\n"
  213. "Arguments:\n"
  214. "\n"
  215. "\tget-supported-syntaxes\n"
  216. "\t --domain <domain-name>\n"
  217. "\t [ --host <host-name> ]\n"
  218. "\t [ --login <user-name> ]\n"
  219. "\t [ --passwd <password> ]\n"
  220. "\n"
  221. "\tpatch-schema-defs\n"
  222. "\t --file <filename>\n"
  223. "\t --domain <domain-name>\n"
  224. "\t [ --host <host-name> ]\n"
  225. "\t [ --login <user-name> ]\n"
  226. "\t [ --passwd <password> ]\n"
  227. "\t [ --dryrun ]\n");
  228. }