esdlcmd_shell.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2013 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include <stdio.h>
  14. #include "jlog.hpp"
  15. #include "jfile.hpp"
  16. #include "jargv.hpp"
  17. #include "jprop.hpp"
  18. #include "esdlcmd.hpp"
  19. #include "esdlcmd_core.hpp"
  20. //=========================================================================================
  21. #ifdef _WIN32
  22. #include "process.h"
  23. #endif
  24. int EsdlCMDShell::callExternal(ArgvIterator &iter)
  25. {
  26. const char *argv[100];
  27. StringBuffer cmdstr("esdl-");
  28. cmdstr.append(cmd.str());
  29. int i=0;
  30. argv[i++]=cmdstr.str();
  31. if (optHelp)
  32. argv[i++]="--help";
  33. for (; !iter.done(); iter.next())
  34. argv[i++]=iter.query();
  35. argv[i]=NULL;
  36. //TODO - add common routine or use existing in jlib
  37. #ifdef _WIN32
  38. if (_spawnvp(_P_WAIT, cmdstr.str(), const_cast<char **>(argv))==-1)
  39. #else
  40. // First try in same dir as the esdl executable
  41. StringBuffer local;
  42. splitFilename(queryCurrentProcessPath(), &local, &local, NULL, NULL);
  43. local.append(cmdstr);
  44. if (execvp(local.str(), const_cast<char **>(argv))!=-1)
  45. return 0;
  46. // If not found, try the path
  47. if (errno!=ENOENT || execvp(cmdstr.str(), const_cast<char **>(argv))==-1)
  48. #endif
  49. {
  50. switch(errno)
  51. {
  52. case ENOENT:
  53. fprintf(stderr, "esdl '%s' command not found\n", cmd.str());
  54. usage();
  55. return 1;
  56. default:
  57. fprintf(stderr, "esdl '%s' command error %d\n", cmd.str(), errno);
  58. return 1;
  59. }
  60. }
  61. return 0;
  62. }
  63. int EsdlCMDShell::processCMD(ArgvIterator &iter)
  64. {
  65. Owned<IEsdlCommand> c = factory(cmd.get());
  66. if (!c)
  67. {
  68. if (cmd.length())
  69. {
  70. if (runExternals)
  71. return callExternal(iter);
  72. fprintf(stderr, "esdl '%s' command not found\n", cmd.str());
  73. }
  74. usage();
  75. return 1;
  76. }
  77. if (optHelp)
  78. {
  79. c->usage();
  80. return 0;
  81. }
  82. if (!c->parseCommandLineOptions(iter))
  83. return 0;
  84. if (!c->finalizeOptions(globals))
  85. return 0;
  86. return c->processCMD();
  87. }
  88. void EsdlCMDShell::finalizeOptions(IProperties *globals)
  89. {
  90. }
  91. int EsdlCMDShell::run()
  92. {
  93. try
  94. {
  95. if (!parseCommandLineOptions(args))
  96. return 1;
  97. finalizeOptions(globals);
  98. return processCMD(args);
  99. }
  100. catch (IException *E)
  101. {
  102. StringBuffer m;
  103. fputs(E->errorMessage(m).newline().str(), stderr);
  104. E->Release();
  105. return 2;
  106. }
  107. #ifndef _DEBUG
  108. catch (...)
  109. {
  110. ERRLOG("Unexpected exception\n");
  111. return 4;
  112. }
  113. #endif
  114. return 0;
  115. }
  116. //=========================================================================================
  117. bool EsdlCMDShell::parseCommandLineOptions(ArgvIterator &iter)
  118. {
  119. if (iter.done())
  120. {
  121. usage();
  122. return false;
  123. }
  124. bool boolValue;
  125. for (; !iter.done(); iter.next())
  126. {
  127. const char * arg = iter.query();
  128. if (iter.matchFlag(optHelp, ESDLARG_HELP) || iter.matchFlag(optHelp, ESDLOPT_HELP)) //users expect --help to work too
  129. continue;
  130. if (*arg!='-')
  131. {
  132. cmd.set(arg);
  133. iter.next();
  134. break;
  135. }
  136. if (iter.matchFlag(boolValue, ESDLOPT_VERSION))
  137. {
  138. fprintf(stdout, "%s\n", hpccBuildTag);
  139. return false;
  140. }
  141. StringAttr tempArg;
  142. if (iter.matchOption(tempArg, "-brk"))
  143. {
  144. #if defined(_WIN32) && defined(_DEBUG)
  145. unsigned id = atoi(tempArg.str());
  146. if (id == 0)
  147. DebugBreak();
  148. else
  149. _CrtSetBreakAlloc(id);
  150. #endif
  151. }
  152. }
  153. return true;
  154. }
  155. //=========================================================================================
  156. void EsdlCMDShell::usage()
  157. {
  158. fprintf(stdout,"\nUsage:\n"
  159. " esdl <command> [<args>]\n\n"
  160. "Commonly used commands:\n"
  161. " xml Generate XML from ESDL definition.\n"
  162. " ecl Generate ECL from ESDL definition.\n"
  163. " xsd Generate XSD from ESDL definition.\n"
  164. " wsdl Generate WSDL from ESDL definition.\n"
  165. " java Generate Java code from ESDL definition.\n"
  166. " cpp Generate C++ code from ESDL definition.\n"
  167. " publish Publish ESDL Definition for ESP use.\n"
  168. " list-definitions List all ESDL definitions.\n"
  169. " get-definition Get ESDL definition.\n"
  170. " delete Delete ESDL Definition.\n"
  171. " bind-service Configure ESDL based service on target ESP (with existing ESP Binding).\n"
  172. " list-bindings List all ESDL bindings.\n"
  173. " unbind-service Remove ESDL based service binding on target ESP.\n"
  174. " bind-method Configure method associated with existing ESDL binding.\n"
  175. " unbind-method Remove method associated with existing ESDL binding.\n"
  176. " get-binding Get ESDL binding.\n"
  177. " bind-log-transform Configure log transform associated with existing ESDL binding.\n"
  178. " unbind-log-transform Remove log transform associated with existing ESDL binding.\n"
  179. " monitor Generate ECL code for result monitoring / differencing\n"
  180. " monitor-template Generate a template for use with 'monitor' command\n"
  181. ""
  182. "\nRun 'esdl help <command>' for more information on a specific command\n"
  183. "\nRun 'esdl --version' to get version information\n\n"
  184. );
  185. }