main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. exit_status = (print_ctxt.conflict_occurred ? -1 : exit_status);
  128. fclose (rem_file);
  129. }
  130. else
  131. {
  132. printf ("Could not open REMOTE %s\n", arguments.paths[2]);
  133. }
  134. fclose (loc_file);
  135. }
  136. else
  137. {
  138. printf ("Could not open LOCAL %s\n", arguments.paths[1]);
  139. }
  140. fclose(anc_file);
  141. }
  142. else
  143. {
  144. printf ("Could not open ANCESTOR %s\n", arguments.paths[0]);
  145. }
  146. if (out != stdout)
  147. {
  148. fclose (out);
  149. }
  150. /* Exit */
  151. debug_msg (MAIN, 5, "Return =%d\n", exit_status);
  152. return exit_status;
  153. }
  154. void
  155. arguments_set_default (arguments *arguments)
  156. {
  157. debug_msg (MAIN, 5, "Begin\n");
  158. arguments->paths[0] = NULL;
  159. arguments->paths[1] = NULL;
  160. arguments->paths[2] = NULL;
  161. arguments->paths[3] = NULL;
  162. arguments->todo_states =
  163. gl_list_nx_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
  164. arguments->priorities =
  165. gl_list_nx_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
  166. arguments->verbose = 0;
  167. arguments->tab_width = 8;
  168. arguments->use_tabs = false;
  169. arguments->rmargin = 77;
  170. debug_msg (MAIN, 5, "Return\n");
  171. return;
  172. }
  173. static error_t
  174. parse_opt (int key, char *arg, struct argp_state *state)
  175. {
  176. struct arguments *arguments = state->input;
  177. error_t retvalue = 0;
  178. debug_msg (MAIN, 3, "opt key=%c, arg=%s\n", key, arg);
  179. switch (key)
  180. {
  181. case ARGP_KEY_INIT:
  182. /* Initialize defaults */
  183. arguments_set_default (arguments);
  184. break;
  185. case ARGP_KEY_NO_ARGS:
  186. argp_usage (state);
  187. break;
  188. case 'v':
  189. arguments->verbose = true;
  190. break;
  191. case 't':
  192. gl_list_nx_add_last (arguments->todo_states, arg);
  193. break;
  194. case 'p':
  195. gl_list_nx_add_last (arguments->priorities, arg);
  196. break;
  197. case 'W':
  198. {
  199. int tab_width = atoi (arg);
  200. if (tab_width > 0)
  201. {
  202. arguments->tab_width = tab_width;
  203. }
  204. else
  205. {
  206. argp_error (state, "Specified tab width must be a positive integer.\n");
  207. }
  208. }
  209. break;
  210. case 'N':
  211. arguments->use_tabs = false;
  212. break;
  213. case 'T':
  214. arguments->use_tabs = true;
  215. break;
  216. case 'm':
  217. {
  218. int rmargin = atoi (arg);
  219. if (rmargin > 0)
  220. {
  221. arguments->rmargin = rmargin;
  222. }
  223. else
  224. {
  225. argp_error (state, "Specified right margin column must be a positive interger.\n");
  226. }
  227. }
  228. break;
  229. case ARGP_KEY_ARG:
  230. if (state->arg_num < 5)
  231. {
  232. arguments->paths[state->arg_num] = arg;
  233. }
  234. else
  235. {
  236. argp_error (state, "Too many arguments.\n");
  237. }
  238. break;
  239. case ARGP_KEY_END:
  240. if (state->arg_num < 2)
  241. {
  242. argp_error (state, "Too few arguments.\n");
  243. }
  244. break;
  245. default:
  246. retvalue = ARGP_ERR_UNKNOWN;
  247. }
  248. return retvalue;
  249. }