fs_getopt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/fs_getopt.c
  19. * @brief helper functions for command-line argument processing
  20. * @author Igor Wronsky, Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_fs_service.h"
  24. #include "fs_api.h"
  25. /* ******************** command-line option parsing API ******************** */
  26. /**
  27. * Command-line option parser function that allows the user
  28. * to specify one or more '-k' options with keywords. Each
  29. * specified keyword will be added to the URI. A pointer to
  30. * the URI must be passed as the "scls" argument.
  31. *
  32. * @param ctx command line processor context
  33. * @param scls must be of type "struct GNUNET_FS_Uri **"
  34. * @param option name of the option (typically 'k')
  35. * @param value command line argument given
  36. * @return GNUNET_OK on success
  37. */
  38. int
  39. GNUNET_FS_getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext
  40. *ctx, void *scls, const char *option,
  41. const char *value)
  42. {
  43. struct GNUNET_FS_Uri **uri = scls;
  44. struct GNUNET_FS_Uri *u = *uri;
  45. char *val;
  46. size_t slen;
  47. if (u == NULL)
  48. {
  49. u = GNUNET_new (struct GNUNET_FS_Uri);
  50. *uri = u;
  51. u->type = GNUNET_FS_URI_KSK;
  52. u->data.ksk.keywordCount = 0;
  53. u->data.ksk.keywords = NULL;
  54. }
  55. else
  56. {
  57. GNUNET_assert (u->type == GNUNET_FS_URI_KSK);
  58. }
  59. slen = strlen (value);
  60. if (slen == 0)
  61. return GNUNET_SYSERR; /* cannot be empty */
  62. if (value[0] == '+')
  63. {
  64. /* simply preserve the "mandatory" flag */
  65. if (slen < 2)
  66. return GNUNET_SYSERR; /* empty keywords not allowed */
  67. if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
  68. {
  69. /* remove the quotes, keep the '+' */
  70. val = GNUNET_malloc (slen - 1);
  71. val[0] = '+';
  72. memcpy (&val[1], &value[2], slen - 3);
  73. val[slen - 2] = '\0';
  74. }
  75. else
  76. {
  77. /* no quotes, just keep the '+' */
  78. val = GNUNET_strdup (value);
  79. }
  80. }
  81. else
  82. {
  83. if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
  84. {
  85. /* remove the quotes, add a space */
  86. val = GNUNET_malloc (slen);
  87. val[0] = ' ';
  88. memcpy (&val[1], &value[1], slen - 2);
  89. val[slen - 1] = '\0';
  90. }
  91. else
  92. {
  93. /* add a space to indicate "not mandatory" */
  94. val = GNUNET_malloc (slen + 2);
  95. strcpy (val, " ");
  96. strcat (val, value);
  97. }
  98. }
  99. GNUNET_array_append (u->data.ksk.keywords, u->data.ksk.keywordCount, val);
  100. return GNUNET_OK;
  101. }
  102. /**
  103. * Command-line option parser function that allows the user to specify
  104. * one or more '-m' options with metadata. Each specified entry of
  105. * the form "type=value" will be added to the metadata. A pointer to
  106. * the metadata must be passed as the "scls" argument.
  107. *
  108. * @param ctx command line processor context
  109. * @param scls must be of type "struct GNUNET_MetaData **"
  110. * @param option name of the option (typically 'k')
  111. * @param value command line argument given
  112. * @return GNUNET_OK on success
  113. */
  114. int
  115. GNUNET_FS_getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext
  116. *ctx, void *scls, const char *option,
  117. const char *value)
  118. {
  119. struct GNUNET_CONTAINER_MetaData **mm = scls;
  120. enum EXTRACTOR_MetaType type;
  121. const char *typename;
  122. const char *typename_i18n;
  123. struct GNUNET_CONTAINER_MetaData *meta;
  124. char *tmp;
  125. meta = *mm;
  126. if (meta == NULL)
  127. {
  128. meta = GNUNET_CONTAINER_meta_data_create ();
  129. *mm = meta;
  130. }
  131. /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded
  132. * commandline arguments, so that the following line is not needed.
  133. */
  134. /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/
  135. tmp = GNUNET_strdup (value);
  136. type = EXTRACTOR_metatype_get_max ();
  137. while (type > 0)
  138. {
  139. type--;
  140. typename = EXTRACTOR_metatype_to_string (type);
  141. typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
  142. if ((strlen (tmp) >= strlen (typename) + 1) &&
  143. (tmp[strlen (typename)] == ':') &&
  144. (0 == strncmp (typename, tmp, strlen (typename))))
  145. {
  146. GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
  147. EXTRACTOR_METAFORMAT_UTF8,
  148. "text/plain",
  149. &tmp[strlen (typename) + 1],
  150. strlen (&tmp[strlen (typename) + 1]) +
  151. 1);
  152. GNUNET_free (tmp);
  153. tmp = NULL;
  154. break;
  155. }
  156. if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
  157. (tmp[strlen (typename_i18n)] == ':') &&
  158. (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
  159. {
  160. GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
  161. EXTRACTOR_METAFORMAT_UTF8,
  162. "text/plain",
  163. &tmp[strlen (typename_i18n) + 1],
  164. strlen (&tmp
  165. [strlen (typename_i18n) + 1]) +
  166. 1);
  167. GNUNET_free (tmp);
  168. tmp = NULL;
  169. break;
  170. }
  171. }
  172. if (tmp != NULL)
  173. {
  174. GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
  175. EXTRACTOR_METATYPE_UNKNOWN,
  176. EXTRACTOR_METAFORMAT_UTF8, "text/plain",
  177. tmp, strlen (tmp) + 1);
  178. GNUNET_free (tmp);
  179. printf (_
  180. ("Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
  181. value);
  182. }
  183. return GNUNET_OK;
  184. }
  185. /* end of fs_getopt.c */