/* Output routines for ed-script format.12Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995, 1998, 2001, 20043Free Software Foundation, Inc.45This file is part of GNU DIFF.67GNU DIFF is free software; you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation; either version 2, or (at your option)10any later version.1112GNU DIFF is distributed in the hope that it will be useful,13but WITHOUT ANY WARRANTY; without even the implied warranty of14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15GNU General Public License for more details.1617You should have received a copy of the GNU General Public License18along with this program; see the file COPYING.19If not, write to the Free Software Foundation,2059 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */2122#include "diff.h"2324static void print_ed_hunk (struct change *);25static void print_rcs_hunk (struct change *);26static void pr_forward_ed_hunk (struct change *);2728/* Print our script as ed commands. */2930void31print_ed_script (struct change *script)32{33print_script (script, find_reverse_change, print_ed_hunk);34}3536/* Print a hunk of an ed diff */3738static void39print_ed_hunk (struct change *hunk)40{41lin f0, l0, f1, l1;42enum changes changes;4344#ifdef DEBUG45debug_script (hunk);46#endif4748/* Determine range of line numbers involved in each file. */49changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);50if (!changes)51return;5253begin_output ();5455/* Print out the line number header for this hunk */56print_number_range (',', &files[0], f0, l0);57fprintf (outfile, "%c\n", change_letter[changes]);5859/* Print new/changed lines from second file, if needed */60if (changes != OLD)61{62lin i;63for (i = f1; i <= l1; i++)64{65if (files[1].linbuf[i][0] == '.' && files[1].linbuf[i][1] == '\n')66{67/* The file's line is just a dot, and it would exit68insert mode. Precede the dot with another dot, exit69insert mode, remove the extra dot, and then resume70insert mode. */71fprintf (outfile, "..\n.\ns/.//\na\n");72}73else74print_1_line ("", &files[1].linbuf[i]);75}7677fprintf (outfile, ".\n");78}79}8081/* Print change script in the style of ed commands,82but print the changes in the order they appear in the input files,83which means that the commands are not truly useful with ed. */8485void86pr_forward_ed_script (struct change *script)87{88print_script (script, find_change, pr_forward_ed_hunk);89}9091static void92pr_forward_ed_hunk (struct change *hunk)93{94lin i, f0, l0, f1, l1;9596/* Determine range of line numbers involved in each file. */97enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);98if (!changes)99return;100101begin_output ();102103fprintf (outfile, "%c", change_letter[changes]);104print_number_range (' ', files, f0, l0);105fprintf (outfile, "\n");106107/* If deletion only, print just the number range. */108109if (changes == OLD)110return;111112/* For insertion (with or without deletion), print the number range113and the lines from file 2. */114115for (i = f1; i <= l1; i++)116print_1_line ("", &files[1].linbuf[i]);117118fprintf (outfile, ".\n");119}120121/* Print in a format somewhat like ed commands122except that each insert command states the number of lines it inserts.123This format is used for RCS. */124125void126print_rcs_script (struct change *script)127{128print_script (script, find_change, print_rcs_hunk);129}130131/* Print a hunk of an RCS diff */132133static void134print_rcs_hunk (struct change *hunk)135{136lin i, f0, l0, f1, l1;137long int tf0, tl0, tf1, tl1;138139/* Determine range of line numbers involved in each file. */140enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);141if (!changes)142return;143144begin_output ();145146translate_range (&files[0], f0, l0, &tf0, &tl0);147148if (changes & OLD)149{150fprintf (outfile, "d");151/* For deletion, print just the starting line number from file 0152and the number of lines deleted. */153fprintf (outfile, "%ld %ld\n", tf0, tf0 <= tl0 ? tl0 - tf0 + 1 : 1);154}155156if (changes & NEW)157{158fprintf (outfile, "a");159160/* Take last-line-number from file 0 and # lines from file 1. */161translate_range (&files[1], f1, l1, &tf1, &tl1);162fprintf (outfile, "%ld %ld\n", tl0, tf1 <= tl1 ? tl1 - tf1 + 1 : 1);163164/* Print the inserted lines. */165for (i = f1; i <= l1; i++)166print_1_line ("", &files[1].linbuf[i]);167}168}169170171