PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/OpenSLP_0-8-0/openslp.contrib/slptool/slptool.c

#
C | 350 lines | 246 code | 44 blank | 60 comment | 48 complexity | 6834ac10ae3013ac7eff56dfd7aa100e MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /***************************************************************************/
  2. /* */
  3. /* Project: OpenSLP command line UA wrapper */
  4. /* */
  5. /* File: slptool_main.c */
  6. /* */
  7. /* Abstract: Main implementation for slpua */
  8. /* */
  9. /* Requires: OpenSLP installation */
  10. /* */
  11. /* Copyright (c) 1995, 1999 Caldera Systems, Inc. */
  12. /* */
  13. /* This program is free software; you can redistribute it and/or modify it */
  14. /* under the terms of the GNU Lesser General Public License as published */
  15. /* by the Free Software Foundation; either version 2.1 of the License, or */
  16. /* (at your option) any later version. */
  17. /* */
  18. /* This program is distributed in the hope that it will be useful, */
  19. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  20. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  21. /* GNU Lesser General Public License for more details. */
  22. /* */
  23. /* You should have received a copy of the GNU Lesser General Public */
  24. /* License along with this program; see the file COPYING. If not, */
  25. /* please obtain a copy from http://www.gnu.org/copyleft/lesser.html */
  26. /* */
  27. /*-------------------------------------------------------------------------*/
  28. /* */
  29. /* Please submit patches to maintainer of http://www.openslp.org */
  30. /* */
  31. /***************************************************************************/
  32. #include "slptool.h"
  33. /*=========================================================================*/
  34. SLPBoolean mySrvTypeCallback( SLPHandle hslp,
  35. const char* srvtypes,
  36. SLPError errcode,
  37. void* cookie )
  38. /*=========================================================================*/
  39. {
  40. char* cpy;
  41. char* slider1;
  42. char* slider2;
  43. if(errcode == SLP_OK && *srvtypes)
  44. {
  45. cpy = strdup(srvtypes);
  46. if(cpy)
  47. {
  48. slider1 = slider2 = cpy;
  49. while(slider1 = strchr(slider2,','))
  50. {
  51. *slider1 = 0;
  52. printf("%s\n",slider2);
  53. slider1 ++;
  54. slider2 = slider1;
  55. }
  56. /* print the final itam */
  57. printf("%s\n",slider2);
  58. free(cpy);
  59. }
  60. }
  61. return SLP_TRUE;
  62. }
  63. /*=========================================================================*/
  64. void FindSrvTypes(SLPToolCommandLine* cmdline)
  65. /*=========================================================================*/
  66. {
  67. SLPError result;
  68. SLPHandle hslp;
  69. if(SLPOpen(cmdline->lang,SLP_FALSE,&hslp) == SLP_OK)
  70. {
  71. if(cmdline->cmdparam1)
  72. {
  73. result = SLPFindSrvTypes(hslp,
  74. cmdline->cmdparam1,
  75. cmdline->scopes,
  76. mySrvTypeCallback,
  77. 0);
  78. }
  79. else
  80. {
  81. result = SLPFindSrvTypes(hslp,
  82. "*",
  83. cmdline->scopes,
  84. mySrvTypeCallback,
  85. 0);
  86. }
  87. if(result != SLP_OK)
  88. {
  89. printf("errorcode: %i",result);
  90. }
  91. SLPClose(hslp);
  92. }
  93. }
  94. /*=========================================================================*/
  95. SLPBoolean myAttrCallback(SLPHandle hslp,
  96. const char* attrlist,
  97. SLPError errcode,
  98. void* cookie )
  99. /*=========================================================================*/
  100. {
  101. if(errcode == SLP_OK)
  102. {
  103. printf("%s\n",attrlist);
  104. }
  105. return SLP_TRUE;
  106. }
  107. /*=========================================================================*/
  108. void FindAttrs(SLPToolCommandLine* cmdline)
  109. /*=========================================================================*/
  110. {
  111. SLPError result;
  112. SLPHandle hslp;
  113. if(SLPOpen(cmdline->lang,SLP_FALSE,&hslp) == SLP_OK)
  114. {
  115. result = SLPFindAttrs(hslp,
  116. cmdline->cmdparam1,
  117. cmdline->scopes,
  118. cmdline->cmdparam2,
  119. myAttrCallback,
  120. 0);
  121. if(result != SLP_OK)
  122. {
  123. printf("errorcode: %i",result);
  124. }
  125. SLPClose(hslp);
  126. }
  127. }
  128. /*=========================================================================*/
  129. SLPBoolean mySrvUrlCallback( SLPHandle hslp,
  130. const char* srvurl,
  131. unsigned short lifetime,
  132. SLPError errcode,
  133. void* cookie )
  134. /*=========================================================================*/
  135. {
  136. if(errcode == SLP_OK)
  137. {
  138. printf("%s,%i\n",srvurl,lifetime);
  139. }
  140. return SLP_TRUE;
  141. }
  142. /*=========================================================================*/
  143. void FindSrvs(SLPToolCommandLine* cmdline)
  144. /*=========================================================================*/
  145. {
  146. SLPError result;
  147. SLPHandle hslp;
  148. if(SLPOpen(cmdline->lang,SLP_FALSE,&hslp) == SLP_OK)
  149. {
  150. result = SLPFindSrvs(hslp,
  151. cmdline->cmdparam1,
  152. cmdline->scopes,
  153. cmdline->cmdparam2,
  154. mySrvUrlCallback,
  155. 0);
  156. if(result != SLP_OK)
  157. {
  158. printf("errorcode: %i",result);
  159. }
  160. SLPClose(hslp);
  161. }
  162. }
  163. /*=========================================================================*/
  164. int ParseCommandLine(int argc,char* argv[], SLPToolCommandLine* cmdline)
  165. /* Returns Zero on success. Non-zero on error.
  166. /*=========================================================================*/
  167. {
  168. int i;
  169. if(argc < 2)
  170. {
  171. /* not enough arguments */
  172. return 1;
  173. }
  174. for (i=1;i<argc;i++)
  175. {
  176. if( strcasecmp(argv[i],"-s") == 0 ||
  177. strcasecmp(argv[i],"--scopes") == 0 )
  178. {
  179. i++;
  180. if(i < argc)
  181. {
  182. cmdline->scopes = argv[i];
  183. }
  184. else
  185. {
  186. return 1;
  187. }
  188. }
  189. else if( strcasecmp(argv[i],"-l") == 0 ||
  190. strcasecmp(argv[i],"--lang") == 0 )
  191. {
  192. i++;
  193. if(i < argc)
  194. {
  195. cmdline->lang = argv[i];
  196. }
  197. else
  198. {
  199. return 1;
  200. }
  201. }
  202. else if(strcasecmp(argv[i],"findsrvs") == 0)
  203. {
  204. cmdline->cmd = FINDSRVS;
  205. /* service type */
  206. i++;
  207. if(i < argc)
  208. {
  209. cmdline->cmdparam1 = argv[i];
  210. }
  211. else
  212. {
  213. return 1;
  214. }
  215. /* (optional) filter */
  216. i++;
  217. if(i < argc)
  218. {
  219. cmdline->cmdparam2 = argv[i];
  220. }
  221. break;
  222. }
  223. else if(strcasecmp(argv[i],"findattrs") == 0)
  224. {
  225. cmdline->cmd = FINDATTRS;
  226. /* url or service type */
  227. i++;
  228. if(i < argc)
  229. {
  230. cmdline->cmdparam1 = argv[i];
  231. }
  232. else
  233. {
  234. return 1;
  235. }
  236. /* (optional) attrids */
  237. i++;
  238. if(i < argc)
  239. {
  240. cmdline->cmdparam2 = argv[i];
  241. }
  242. }
  243. else if(strcasecmp(argv[i],"findsrvtypes") == 0)
  244. {
  245. cmdline->cmd = FINDSRVTYPES;
  246. /* (optional) naming authority */
  247. i++;
  248. if(i < argc)
  249. {
  250. cmdline->cmdparam1 = argv[i];
  251. }
  252. }
  253. else
  254. {
  255. return 1;
  256. }
  257. }
  258. return 0;
  259. }
  260. /*=========================================================================*/
  261. void DisplayUsage()
  262. /*=========================================================================*/
  263. {
  264. printf("Usage: slptool [options] command-and-arguments \n");
  265. printf(" options may be:\n");
  266. printf(" -s (or --scope) followed by a comma separated list of scopes\n");
  267. printf(" -l (or --language) followed by a language tag\n");
  268. printf(" command-and-arguments may be:\n");
  269. printf(" findsrvs service-type [filter]\n");
  270. printf(" findattrs url [attrids]\n");
  271. printf(" findsrvtypes [authority]\n");
  272. }
  273. /*=========================================================================*/
  274. int main(int argc, char* argv[])
  275. /*=========================================================================*/
  276. {
  277. int result;
  278. SLPToolCommandLine cmdline;
  279. /* zero out the cmdline */
  280. memset(&cmdline,0,sizeof(cmdline));
  281. /* Parse the command line */
  282. if(ParseCommandLine(argc,argv,&cmdline) == 0)
  283. {
  284. switch(cmdline.cmd)
  285. {
  286. case FINDSRVS:
  287. FindSrvs(&cmdline);
  288. break;
  289. case FINDATTRS:
  290. FindAttrs(&cmdline);
  291. break;
  292. case FINDSRVTYPES:
  293. FindSrvTypes(&cmdline);
  294. break;
  295. case GETPROPERTY:
  296. // GetProperty(&cmdline);
  297. break;
  298. }
  299. }
  300. else
  301. {
  302. DisplayUsage();
  303. result = 1;
  304. }
  305. return 0;
  306. }