xref: /freebsd-src/contrib/libdiff/include/diff_output.h (revision 4e859e67dde2cb4a583d340b27793a255f62f53e)
159c8e88eSDag-Erling Smørgrav /* Diff output generators and invocation shims. */
259c8e88eSDag-Erling Smørgrav /*
359c8e88eSDag-Erling Smørgrav  * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
459c8e88eSDag-Erling Smørgrav  *
559c8e88eSDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
659c8e88eSDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
759c8e88eSDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
859c8e88eSDag-Erling Smørgrav  *
959c8e88eSDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1059c8e88eSDag-Erling Smørgrav  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1159c8e88eSDag-Erling Smørgrav  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1259c8e88eSDag-Erling Smørgrav  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1359c8e88eSDag-Erling Smørgrav  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1459c8e88eSDag-Erling Smørgrav  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1559c8e88eSDag-Erling Smørgrav  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1659c8e88eSDag-Erling Smørgrav  */
1759c8e88eSDag-Erling Smørgrav 
1859c8e88eSDag-Erling Smørgrav struct diff_input_info {
1959c8e88eSDag-Erling Smørgrav 	const char *left_path;
2059c8e88eSDag-Erling Smørgrav 	const char *right_path;
2159c8e88eSDag-Erling Smørgrav 
2259c8e88eSDag-Erling Smørgrav 	/* Set by caller of diff_output_* functions. */
2359c8e88eSDag-Erling Smørgrav 	int flags;
2459c8e88eSDag-Erling Smørgrav #define DIFF_INPUT_LEFT_NONEXISTENT	0x00000001
2559c8e88eSDag-Erling Smørgrav #define DIFF_INPUT_RIGHT_NONEXISTENT	0x00000002
2659c8e88eSDag-Erling Smørgrav };
2759c8e88eSDag-Erling Smørgrav 
2859c8e88eSDag-Erling Smørgrav struct diff_output_info {
2959c8e88eSDag-Erling Smørgrav 	/*
3059c8e88eSDag-Erling Smørgrav 	 * Byte offset to each line in the generated output file.
3159c8e88eSDag-Erling Smørgrav 	 * The total number of lines in the file is line_offsets.len - 1.
3259c8e88eSDag-Erling Smørgrav 	 * The last offset in this array corresponds to end-of-file.
3359c8e88eSDag-Erling Smørgrav 	 */
3459c8e88eSDag-Erling Smørgrav 	ARRAYLIST(off_t) line_offsets;
3559c8e88eSDag-Erling Smørgrav 	/*
3659c8e88eSDag-Erling Smørgrav 	 * Type (i.e., context, minus, plus) of each line generated by the diff.
3759c8e88eSDag-Erling Smørgrav 	 * nb. 0x00 to 0x3b reserved for client-defined line types.
3859c8e88eSDag-Erling Smørgrav 	 */
3959c8e88eSDag-Erling Smørgrav 	ARRAYLIST(uint8_t) line_types;
4059c8e88eSDag-Erling Smørgrav #define DIFF_LINE_HUNK		0x3c
4159c8e88eSDag-Erling Smørgrav #define DIFF_LINE_MINUS		0x3d
4259c8e88eSDag-Erling Smørgrav #define DIFF_LINE_PLUS		0x3e
4359c8e88eSDag-Erling Smørgrav #define DIFF_LINE_CONTEXT	0x3f
4459c8e88eSDag-Erling Smørgrav #define DIFF_LINE_NONE		0x40  /* binary or no EOF newline msg, etc. */
4559c8e88eSDag-Erling Smørgrav };
4659c8e88eSDag-Erling Smørgrav 
4759c8e88eSDag-Erling Smørgrav void diff_output_info_free(struct diff_output_info *output_info);
4859c8e88eSDag-Erling Smørgrav 
4959c8e88eSDag-Erling Smørgrav struct diff_chunk_context {
5059c8e88eSDag-Erling Smørgrav 	struct diff_range chunk;
5159c8e88eSDag-Erling Smørgrav 	struct diff_range left, right;
5259c8e88eSDag-Erling Smørgrav };
5359c8e88eSDag-Erling Smørgrav 
5459c8e88eSDag-Erling Smørgrav int diff_output_plain(struct diff_output_info **output_info, FILE *dest,
5559c8e88eSDag-Erling Smørgrav 			const struct diff_input_info *info,
5659c8e88eSDag-Erling Smørgrav 			const struct diff_result *result,
5759c8e88eSDag-Erling Smørgrav 			int hunk_headers_only);
5859c8e88eSDag-Erling Smørgrav int diff_output_unidiff(struct diff_output_info **output_info,
5959c8e88eSDag-Erling Smørgrav 			FILE *dest, const struct diff_input_info *info,
6059c8e88eSDag-Erling Smørgrav 			const struct diff_result *result,
6159c8e88eSDag-Erling Smørgrav 			unsigned int context_lines);
6259c8e88eSDag-Erling Smørgrav int diff_output_edscript(struct diff_output_info **output_info,
6359c8e88eSDag-Erling Smørgrav 			 FILE *dest, const struct diff_input_info *info,
6459c8e88eSDag-Erling Smørgrav 			 const struct diff_result *result);
6559c8e88eSDag-Erling Smørgrav int diff_chunk_get_left_start(const struct diff_chunk *c,
6659c8e88eSDag-Erling Smørgrav 			      const struct diff_result *r,
6759c8e88eSDag-Erling Smørgrav 			      int context_lines);
6859c8e88eSDag-Erling Smørgrav int diff_chunk_get_left_end(const struct diff_chunk *c,
6959c8e88eSDag-Erling Smørgrav 			    const struct diff_result *r,
7059c8e88eSDag-Erling Smørgrav 			    int context_lines);
7159c8e88eSDag-Erling Smørgrav int diff_chunk_get_right_start(const struct diff_chunk *c,
7259c8e88eSDag-Erling Smørgrav 			       const struct diff_result *r,
7359c8e88eSDag-Erling Smørgrav 			       int context_lines);
7459c8e88eSDag-Erling Smørgrav int diff_chunk_get_right_end(const struct diff_chunk *c,
7559c8e88eSDag-Erling Smørgrav 			     const struct diff_result *r,
7659c8e88eSDag-Erling Smørgrav 			     int context_lines);
7759c8e88eSDag-Erling Smørgrav off_t diff_chunk_get_left_start_pos(const struct diff_chunk *c);
7859c8e88eSDag-Erling Smørgrav off_t diff_chunk_get_right_start_pos(const struct diff_chunk *c);
7959c8e88eSDag-Erling Smørgrav struct diff_chunk *diff_chunk_get(const struct diff_result *r, int chunk_idx);
8059c8e88eSDag-Erling Smørgrav int diff_chunk_get_left_count(struct diff_chunk *c);
8159c8e88eSDag-Erling Smørgrav int diff_chunk_get_right_count(struct diff_chunk *c);
8259c8e88eSDag-Erling Smørgrav void diff_chunk_context_get(struct diff_chunk_context *cc,
8359c8e88eSDag-Erling Smørgrav 				 const struct diff_result *r,
8459c8e88eSDag-Erling Smørgrav 				 int chunk_idx, int context_lines);
8559c8e88eSDag-Erling Smørgrav void diff_chunk_context_load_change(struct diff_chunk_context *cc,
8659c8e88eSDag-Erling Smørgrav 				    int *nchunks_used,
8759c8e88eSDag-Erling Smørgrav 				    struct diff_result *result,
8859c8e88eSDag-Erling Smørgrav 				    int start_chunk_idx,
8959c8e88eSDag-Erling Smørgrav 				    int context_lines);
9059c8e88eSDag-Erling Smørgrav 
9159c8e88eSDag-Erling Smørgrav struct diff_output_unidiff_state;
9259c8e88eSDag-Erling Smørgrav struct diff_output_unidiff_state *diff_output_unidiff_state_alloc(void);
9359c8e88eSDag-Erling Smørgrav void diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state);
9459c8e88eSDag-Erling Smørgrav void diff_output_unidiff_state_free(struct diff_output_unidiff_state *state);
9559c8e88eSDag-Erling Smørgrav int diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
9659c8e88eSDag-Erling Smørgrav 			  struct diff_output_unidiff_state *state,
9759c8e88eSDag-Erling Smørgrav 			  const struct diff_input_info *info,
9859c8e88eSDag-Erling Smørgrav 			  const struct diff_result *result,
9959c8e88eSDag-Erling Smørgrav 			  const struct diff_chunk_context *cc);
10059c8e88eSDag-Erling Smørgrav int diff_output_chunk_left_version(struct diff_output_info **output_info,
10159c8e88eSDag-Erling Smørgrav 			       FILE *dest,
10259c8e88eSDag-Erling Smørgrav 			       const struct diff_input_info *info,
10359c8e88eSDag-Erling Smørgrav 			       const struct diff_result *result,
10459c8e88eSDag-Erling Smørgrav 			       const struct diff_chunk_context *cc);
10559c8e88eSDag-Erling Smørgrav int diff_output_chunk_right_version(struct diff_output_info **output_info,
10659c8e88eSDag-Erling Smørgrav 				FILE *dest,
10759c8e88eSDag-Erling Smørgrav 				const struct diff_input_info *info,
10859c8e88eSDag-Erling Smørgrav 				const struct diff_result *result,
10959c8e88eSDag-Erling Smørgrav 				const struct diff_chunk_context *cc);
11059c8e88eSDag-Erling Smørgrav 
11159c8e88eSDag-Erling Smørgrav const char *diff_output_get_label_left(const struct diff_input_info *info);
11259c8e88eSDag-Erling Smørgrav const char *diff_output_get_label_right(const struct diff_input_info *info);
113*4e859e67SDag-Erling Smørgrav 
114*4e859e67SDag-Erling Smørgrav void diff_output_set_colors(bool _color,
115*4e859e67SDag-Erling Smørgrav 			    const char *_del_code,
116*4e859e67SDag-Erling Smørgrav 			    const char *_add_code);
117