main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "config.h"
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <iconv.h>
  7. #include <string.h>
  8. #include <argp.h>
  9. #include "debug.h"
  10. #include "gl_list.h"
  11. #include "gl_array_list.h"
  12. #include "doc_stream.h"
  13. #include "doc_ref.h"
  14. #include "print.h"
  15. #include "doc_elt.h"
  16. #include "org_document.h"
  17. //#include "doc_elt_ops.h"
  18. #include "org_parser.h"
  19. //#include "smerger.h"
  20. struct arguments;
  21. typedef struct arguments arguments;
  22. static error_t parse_opt (int key, char *arg, struct argp_state *state);
  23. static void arguments_set_default (arguments *arguments);
  24. const char *argp_program_version =
  25. "org-merge-driver 0.1\n\
  26. Copywrite (C) 2012 Free Software Foundation, Inc\n\
  27. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
  28. This is free software: you are free to change and redistribute it.\n\
  29. There is NO WARRANTY, to the extent permitted by law.";
  30. const char *argp_program_bug_address =
  31. "<youngar17@gmail.com>";
  32. static char doc [] =
  33. "org-merge-driver\n\
  34. A tool for performing 3 way merges of org-mode formatted plain text documents.";
  35. static char args_doc[] = "ANCESTOR LOCAL REMOTE [OUT]";
  36. static struct argp_option options[] = {
  37. {"todo", 't', "STATE", 0, "Specify an accepted todo state" },
  38. {"priority", 'p', "PRIORITY", 0, "Specify an accepted prority"},
  39. {"tabwidth", 'W', "WIDTH", 0, "The width of tabs in spaces"},
  40. {"notabs", 'N', 0, 0, "Use only spaces in the output"},
  41. {"usetabs", 'T', 0, 0, "Use tabs in the output"},
  42. {"rmargin", 'm', "COLUMN", 0, "Set the right margin of headings"},
  43. { 0 }
  44. };
  45. struct arguments
  46. {
  47. char *paths[4];
  48. gl_list_t todo_states;
  49. gl_list_t priorities;
  50. bool verbose;
  51. size_t tab_width;
  52. bool use_tabs;
  53. size_t rmargin;
  54. };
  55. static struct argp argp = { options, parse_opt, args_doc, doc };
  56. main (int argc, char *argv[])
  57. {
  58. debug_msg (MAIN, 5, "Begin\n");
  59. error_t exit_status = EXIT_SUCCESS;
  60. /* Parse Arguments */
  61. struct arguments arguments;
  62. error_t error = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  63. /* Configure the contexts using arguments */
  64. struct parse_ctxt parse_ctxt;
  65. parse_ctxt_init (&parse_ctxt);
  66. if (gl_list_size (arguments.todo_states) > 0)
  67. {
  68. parse_ctxt.todo_states = arguments.todo_states;
  69. }
  70. parse_ctxt_set_defaults (&parse_ctxt);
  71. struct merge_ctxt loc_merge_ctxt;
  72. merge_ctxt_init (&loc_merge_ctxt);
  73. struct merge_ctxt rem_merge_ctxt;
  74. merge_ctxt_init (&rem_merge_ctxt);
  75. if (gl_list_size (arguments.priorities) > 0)
  76. {
  77. loc_merge_ctxt.priorities = arguments.priorities;
  78. rem_merge_ctxt.priorities = arguments.priorities;
  79. }
  80. merge_ctxt_set_defaults (&loc_merge_ctxt);
  81. merge_ctxt_set_defaults (&rem_merge_ctxt);
  82. struct print_ctxt print_ctxt;
  83. print_ctxt_init (&print_ctxt);
  84. print_ctxt.tab_width = arguments.tab_width;
  85. print_ctxt.use_tabs = arguments.use_tabs;
  86. print_ctxt.rmargin = arguments.rmargin;
  87. print_ctxt_set_defaults (&print_ctxt);
  88. /* Open output file */
  89. FILE * out = NULL;
  90. if (arguments.paths[3] != NULL)
  91. {
  92. out = fopen ( arguments.paths[3], "w");
  93. if (out == NULL)
  94. {
  95. printf ("Could not open OUT %s\nOutputting to stdin\n", arguments.paths[3]);
  96. }
  97. }
  98. if (out == NULL)
  99. {
  100. debug_msg (MAIN, 4, "Setting output to stdout\n");
  101. out = stdout;
  102. }
  103. FILE *anc_file = fopen ( arguments.paths[0], "r");
  104. if (anc_file != NULL)
  105. {
  106. debug_msg (MAIN, 4, "File 1 opened\n");
  107. FILE *loc_file = fopen (arguments.paths[1], "r");
  108. if (loc_file != NULL)
  109. {
  110. debug_msg (MAIN, 4, "File 2 opened\n");
  111. FILE *rem_file = fopen (arguments.paths[2], "r");
  112. if (rem_file != NULL)
  113. {
  114. debug_msg (MAIN, 4, "File 3 opened\n");
  115. debug_msg (MAIN, 4, "Parsing Files\n\n");
  116. org_document *anc = org_parse_file_stream (anc_file, ANC_SRC, &parse_ctxt);
  117. org_document *loc = org_parse_file_stream (loc_file, LOC_SRC, &parse_ctxt);
  118. org_document *rem = org_parse_file_stream (rem_file, REM_SRC, &parse_ctxt);
  119. debug_msg (MAIN, 4, "Merging Files\n\n");
  120. debug_msg (MAIN, 4, "Merging anc and loc\n");
  121. org_document_merge (anc, loc, &loc_merge_ctxt);
  122. debug_msg (MAIN, 4, "Merging anc and rem\n");
  123. org_document_merge (anc, rem, &rem_merge_ctxt);
  124. org_document_check_for_loop (anc);
  125. debug_msg (MAIN, 3, "Printing\n\n");
  126. org_document_print (anc, &print_ctxt, out);
  127. fclose (rem_file);
  128. }
  129. else
  130. {
  131. printf ("Could not open REMOTE %s\n", arguments.paths[2]);
  132. }
  133. fclose (loc_file);
  134. }
  135. else
  136. {
  137. printf ("Could not open LOCAL %s\n", arguments.paths[1]);
  138. }
  139. fclose(anc_file);
  140. }
  141. else
  142. {
  143. printf ("Could not open ANCESTOR %s\n", arguments.paths[0]);
  144. }
  145. if (out != stdout)
  146. {
  147. fclose (out);
  148. }
  149. /* Exit */
  150. debug_msg (MAIN, 5, "Return =%d\n", exit_status);
  151. return exit_status;
  152. }
  153. void
  154. arguments_set_default (arguments *arguments)
  155. {
  156. debug_msg (MAIN, 5, "Begin\n");
  157. arguments->paths[0] = NULL;
  158. arguments->paths[1] = NULL;
  159. arguments->paths[2] = NULL;
  160. arguments->paths[3] = NULL;
  161. arguments->todo_states =
  162. gl_list_nx_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
  163. arguments->priorities =
  164. gl_list_nx_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
  165. arguments->verbose = 0;
  166. arguments->tab_width = 8;
  167. arguments->use_tabs = false;
  168. arguments->rmargin = 77;
  169. debug_msg (MAIN, 5, "Return\n");
  170. return;
  171. }
  172. static error_t
  173. parse_opt (int key, char *arg, struct argp_state *state)
  174. {
  175. struct arguments *arguments = state->input;
  176. error_t retvalue = 0;
  177. debug_msg (MAIN, 3, "opt key=%c, arg=%s\n", key, arg);
  178. switch (key)
  179. {
  180. case ARGP_KEY_INIT:
  181. /* Initialize defaults */
  182. arguments_set_default (arguments);
  183. break;
  184. case ARGP_KEY_NO_ARGS:
  185. argp_usage (state);
  186. break;
  187. case 'v':
  188. arguments->verbose = true;
  189. break;
  190. case 't':
  191. gl_list_nx_add_last (arguments->todo_states, arg);
  192. break;
  193. case 'p':
  194. gl_list_nx_add_last (arguments->priorities, arg);
  195. break;
  196. case 'W':
  197. {
  198. int tab_width = atoi (arg);
  199. if (tab_width > 0)
  200. {
  201. arguments->tab_width = tab_width;
  202. }
  203. else
  204. {
  205. argp_error (state, "Specified tab width must be a positive integer.\n");
  206. }
  207. }
  208. break;
  209. case 'N':
  210. arguments->use_tabs = false;
  211. break;
  212. case 'T':
  213. arguments->use_tabs = true;
  214. break;
  215. case 'm':
  216. {
  217. int rmargin = atoi (arg);
  218. if (rmargin > 0)
  219. {
  220. arguments->rmargin = rmargin;
  221. }
  222. else
  223. {
  224. argp_error (state, "Specified right margin column must be a positive interger.\n");
  225. }
  226. }
  227. break;
  228. case ARGP_KEY_ARG:
  229. if (state->arg_num < 5)
  230. {
  231. arguments->paths[state->arg_num] = arg;
  232. }
  233. else
  234. {
  235. argp_error (state, "Too many arguments.\n");
  236. }
  237. break;
  238. case ARGP_KEY_END:
  239. if (state->arg_num < 2)
  240. {
  241. argp_error (state, "Too few arguments.\n");
  242. }
  243. break;
  244. default:
  245. retvalue = ARGP_ERR_UNKNOWN;
  246. }
  247. return retvalue;
  248. }