PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C | 381 lines | 270 code | 49 blank | 62 comment | 55 complexity | 9ecb80957901b2310a7f90a7d2799515 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. void FindScopes(SLPToolCommandLine* cmdline)
  165. /*=========================================================================*/
  166. {
  167. SLPError result;
  168. SLPHandle hslp;
  169. char* scopes;
  170. if(SLPOpen(cmdline->lang,SLP_FALSE,&hslp) == SLP_OK)
  171. {
  172. result = SLPFindScopes(hslp,&scopes);
  173. if(result == SLP_OK)
  174. {
  175. printf("%s\n",scopes);
  176. SLPFree(scopes);
  177. }
  178. SLPClose(hslp);
  179. }
  180. }
  181. /*=========================================================================*/
  182. int ParseCommandLine(int argc,char* argv[], SLPToolCommandLine* cmdline)
  183. /* Returns Zero on success. Non-zero on error.
  184. /*=========================================================================*/
  185. {
  186. int i;
  187. if(argc < 2)
  188. {
  189. /* not enough arguments */
  190. return 1;
  191. }
  192. for (i=1;i<argc;i++)
  193. {
  194. if( strcasecmp(argv[i],"-s") == 0 ||
  195. strcasecmp(argv[i],"--scopes") == 0 )
  196. {
  197. i++;
  198. if(i < argc)
  199. {
  200. cmdline->scopes = argv[i];
  201. }
  202. else
  203. {
  204. return 1;
  205. }
  206. }
  207. else if( strcasecmp(argv[i],"-l") == 0 ||
  208. strcasecmp(argv[i],"--lang") == 0 )
  209. {
  210. i++;
  211. if(i < argc)
  212. {
  213. cmdline->lang = argv[i];
  214. }
  215. else
  216. {
  217. return 1;
  218. }
  219. }
  220. else if(strcasecmp(argv[i],"findsrvs") == 0)
  221. {
  222. cmdline->cmd = FINDSRVS;
  223. /* service type */
  224. i++;
  225. if(i < argc)
  226. {
  227. cmdline->cmdparam1 = argv[i];
  228. }
  229. else
  230. {
  231. return 1;
  232. }
  233. /* (optional) filter */
  234. i++;
  235. if(i < argc)
  236. {
  237. cmdline->cmdparam2 = argv[i];
  238. }
  239. break;
  240. }
  241. else if(strcasecmp(argv[i],"findattrs") == 0)
  242. {
  243. cmdline->cmd = FINDATTRS;
  244. /* url or service type */
  245. i++;
  246. if(i < argc)
  247. {
  248. cmdline->cmdparam1 = argv[i];
  249. }
  250. else
  251. {
  252. return 1;
  253. }
  254. /* (optional) attrids */
  255. i++;
  256. if(i < argc)
  257. {
  258. cmdline->cmdparam2 = argv[i];
  259. }
  260. }
  261. else if(strcasecmp(argv[i],"findsrvtypes") == 0)
  262. {
  263. cmdline->cmd = FINDSRVTYPES;
  264. /* (optional) naming authority */
  265. i++;
  266. if(i < argc)
  267. {
  268. cmdline->cmdparam1 = argv[i];
  269. }
  270. }
  271. else if(strcasecmp(argv[i],"findscopes") == 0)
  272. {
  273. cmdline->cmd = FINDSCOPES;
  274. }
  275. else
  276. {
  277. return 1;
  278. }
  279. }
  280. return 0;
  281. }
  282. /*=========================================================================*/
  283. void DisplayUsage()
  284. /*=========================================================================*/
  285. {
  286. printf("Usage: slptool [options] command-and-arguments \n");
  287. printf(" options may be:\n");
  288. printf(" -s (or --scope) followed by a comma separated list of scopes\n");
  289. printf(" -l (or --language) followed by a language tag\n");
  290. printf(" command-and-arguments may be:\n");
  291. printf(" findsrvs service-type [filter]\n");
  292. printf(" findattrs url [attrids]\n");
  293. printf(" findsrvtypes [authority]\n");
  294. printf(" findscopes\n");
  295. }
  296. /*=========================================================================*/
  297. int main(int argc, char* argv[])
  298. /*=========================================================================*/
  299. {
  300. int result;
  301. SLPToolCommandLine cmdline;
  302. /* zero out the cmdline */
  303. memset(&cmdline,0,sizeof(cmdline));
  304. /* Parse the command line */
  305. if(ParseCommandLine(argc,argv,&cmdline) == 0)
  306. {
  307. switch(cmdline.cmd)
  308. {
  309. case FINDSRVS:
  310. FindSrvs(&cmdline);
  311. break;
  312. case FINDATTRS:
  313. FindAttrs(&cmdline);
  314. break;
  315. case FINDSRVTYPES:
  316. FindSrvTypes(&cmdline);
  317. break;
  318. case FINDSCOPES:
  319. FindScopes(&cmdline);
  320. break;
  321. case GETPROPERTY:
  322. // GetProperty(&cmdline);
  323. break;
  324. }
  325. }
  326. else
  327. {
  328. DisplayUsage();
  329. result = 1;
  330. }
  331. return 0;
  332. }