cleanconfig.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/Dict.h"
  16. #include "benc/serialization/BencSerializer.h"
  17. #include "benc/serialization/json/JsonBencSerializer.h"
  18. #include "memory/MallocAllocator.h"
  19. #include "io/Reader.h"
  20. #include "io/FileReader.h"
  21. #include "io/Writer.h"
  22. #include "io/FileWriter.h"
  23. #include <unistd.h>
  24. int main(int argc, char** argv)
  25. {
  26. if (isatty(STDIN_FILENO)) {
  27. printf("Usage: %s < cjdroute.conf > compliant.json\n", argv[0]);
  28. printf("Cjdns accepts configuration which is not valid json but only outputs json\n"
  29. "which is valid. This tool deserialies and reserialized a conf file or other "
  30. "json file.\n");
  31. printf("In honor of thefinn93, thanks for all of your hard work helping people.\n");
  32. return 0;
  33. }
  34. struct Allocator* allocator = MallocAllocator_new(1<<20);
  35. struct Reader* stdinReader = FileReader_new(stdin, allocator);
  36. Dict config;
  37. if (JsonBencSerializer_get()->parseDictionary(stdinReader, allocator, &config)) {
  38. fprintf(stderr, "Failed to parse configuration.\n");
  39. return -1;
  40. }
  41. struct Writer* stdoutWriter = FileWriter_new(stdout, allocator);
  42. JsonBencSerializer_get()->serializeDictionary(stdoutWriter, &config);
  43. printf("\n");
  44. Allocator_free(allocator);
  45. }