1. Trang chủ
  2. » Công Nghệ Thông Tin

Linux programming unleash phần 2 pdf

84 300 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 84
Dung lượng 6,75 MB

Nội dung

Programmers often need to quickly identify differences between two files, or to merge two files together. The GNU project’s diff and patch programs provide these facilities. The first part of this chapter shows you how to create diffs, files that express the differ- ences between two source code files. The second part illustrates using diffs to create source code patches in an automatic fashion. Comparing Files The diff command is one of a suite of commands that compares files. It is the one on which we will focus, but first we briefly introduce the cmp command. Then, we cover the other two commands, diff3 and sdiff, in the following sections. Understanding the cmp Command The cmp command compares two files, showing the offset and line numbers where they differ. Optionally, cmp displays differing characters side-by-side. Invoke cmp as follows: $ cmp [options] file1 [file2] A hyphen (-) may be substituted for file1 or file2, so cmp may be used in a pipeline. If one filename is omitted, cmp assumes standard input. The options include the following: • -c| print-chars Print the first characters encountered that differ • -I N| ignore-initial=N Ignore any difference encountered in the first N bytes • -l| verbose Print the offsets of differing characters in decimal format and the their values in octal format • -s| silent| quiet Suppress all output, returning only an exit code. 0 means no difference, 1 means one or more differences, 2 means an error occurred. • -v| version Print cmp’s version information From a programmer’s perspective, cmp is not terribly useful. Listings 6.1 and 6.2 show two versions of Proverbs 3, verses 5 and 6. The acronyms JPS and NIV stand for Jewish Publication Society and New International Version, respectively. LISTING 6.1 JPS VERSION OF PROVERBS 3:5-6 Trust in the Lord with all your heart, And do not rely on your own understanding. In all your ways acknowledge Him, And He will make your paths smooth. The Linux Programming Toolkit P ART I 86 0872316072 CH06 7/26/99 1:42 PM Page 86 LISTING 6.2 NIV VERSION OF PROVERBS 3:5-6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make your paths straight. A bare cmp produces the following: $ cmp jps niv jps niv differ: char 38, line 1 Helpful, yes? We see that the first difference occurs at byte 38 one line 1. Adding the -c option, cmp reports: $ cmp -c jps niv jps niv differ: char 38, line 1 is 54 , 12 ^J Now we know that the differing character is decimal 52, a control character, in this case. Replacing -c with -l produces the following: $ cmp -l jps niv 38 54 12 39 12 141 40 101 156 41 156 144 42 144 40 43 40 154 148 157 164 149 164 56 150 150 12 151 56 12 cmp: EOF on niv The first column of the preceding listing shows the character number where cmp finds a difference, the second column lists the character from the first file, and the third column the character from the second file. Note that the second to last line of the output ( 151 56 12) may not appear on some Red Hat systems. Character 38, for exam- ple, is octal 54, a comma (,) in the file jps, while it is octal 12, a newline, in the file niv. Only part of the output is shown to save space. Finally, combining -c and -l yields the following: $ cmp -cl jps niv 38 54 , 12 ^J 39 12 ^J 141 a 40 101 A 156 n 41 156 n 144 d 42 144 d 40 Comparing and Merging Source Files C HAPTER 6 87 6 COMPARING AND MERGING SOURCE FILES 0872316072 CH06 7/26/99 1:42 PM Page 87 43 40 154 l 148 157 o 164 t 149 164 t 56 . 150 150 h 12 ^J 151 56 . 12 ^J cmp: EOF on niv Using -cl results in more immediately readable output, in that you can see both the encoded characters and their human-readable translations for each character that differs. Understanding the diff Command The diff command shows the differences between two files, or between two identically named files in separate directories. You can direct diff, using command line options, to format its output in any of several formats. The patch program, discussed in the section “Preparing Source Code Patches” later in this chapter, reads this output and uses it to re- create one of the files used to create the diff. As the authors of the diff manual say, “If you think of diff as subtracting one file from another to produce their difference, you can think of patch as adding the difference to one file to reproduce the other.” Because this book attempts to be practical, I will focus on diff’s usage from a program- mer’s perspective, ignoring many of its options and capabilities. While comparing files may seem an uninteresting subject, the technical literature devoted to the subject is extensive. For a complete listing of diff’s options and some of the theory behind file comparisons, see the diff info page (info diff). The general syntax of the diff command is diff [options] file1 file2 diff operates by attempting to find large sequences of lines common to file1 and file2, interrupted by groups of differing lines, called hunks. Two identical files, there- fore, will have no hunks and two complete different files result in one hunk consisting of all the lines from both files. Also bear in mind that diff performs a line-by-line compari- son of two files, as opposed to cmp, which performs a character-by-character comparison. diff produces several different output formats. I will discuss each them in the following sections. The Normal Output Format If we diff Listings 6.1 and 6.2 (jps and niv, respectively, on the CD-ROM), the output is as follows: $ diff jps niv 1,4c1,4 The Linux Programming Toolkit P ART I 88 0872316072 CH06 7/26/99 1:42 PM Page 88 < Trust in the Lord with all your heart, < And do not rely on your own understanding. < In all your ways acknowledge Him, < And He will make your paths smooth. > Trust in the Lord with all your heart > and lean not on your own understanding; > in all your ways acknowledge him, > and he will make your paths straight. The output is in normal format, showing only the lines that differ, uncluttered by context. This output is the default in order to comply with Posix standards. Normal format is rarely used for distributing software patches; nevertheless, here is a brief description of the output, or hunk format. The general normal hunk format is as follows: change_command < file1 line < file1 line > file2 line > file2 line change_command takes the form of a line number or a comma- separated range of lines from file1, a one character command, and a line number or comma-separated range of lines from file2. The character will be one of the following: • a—add • d—delete • c—change The change command is actually the ed command to execute to transform file1 into file2. Looking at the hunk above, to convert jps to niv, we would have to change lines 1–4 of jps to lines 1–4 of niv. The Context Output Format As noted in the preceding section, normal hunk format is rarely used to distribute soft- ware patches. Rather, the “context” or “unified” hunk formats diff produces are the pre- ferred formats to patches. To generate context diffs, use the -c, —context=[NUM], or -C NUM options to diff. So-called “context diffs” show differing lines surrounded by NUM lines of context, so you can more clearly understand the changes between files. Listings 6.3 and 6.4 illustrate the context diff format using a simple bash shell script that changes the signature files appended to the bottom of email and Usenet posts. (No line numbers were inserted into these listings in order to prevent confusion with the line num- bers that diff produces.) Comparing and Merging Source Files C HAPTER 6 89 6 COMPARING AND MERGING SOURCE FILES 0872316072 CH06 7/26/99 1:42 PM Page 89 LISTING 6.3 sigrot.1 #!/usr/local/bin/bash # sigrot.sh # Version 1.0 # Rotate signatures # Suitable to be run via cron ############################# sigfile=signature old=$(cat num) let new=$(expr $old+1) if [ -f $sigfile.$new ]; then cp $sigfile.$new .$sigfile echo $new > num else cp $sigfile.1 .$sigfile echo 1 > num fi LISTING 6.4 sigrot.2 #!/usr/local/bin/bash # sigrot.sh # Version 2.0 # Rotate signatures # Suitable to be run via cron ############################# sigfile=signature srcdir=$HOME/doc/signatures srcfile=$srcdir/$sigfile old=$(cat $srcdir/num) let new=$(expr $old+1) if [ -f $srcfile.$new ]; then cp $srcfile.$new $HOME/.$sigfile echo $new > $srcdir/num else cp $srcfile.1 $HOME/.$sigfile echo 1 > $srcdir/num fi Context hunk format takes the following form: *** file1 file1_timestamp file2 file2_timestamp The Linux Programming Toolkit P ART I 90 0872316072 CH06 7/26/99 1:42 PM Page 90 *************** *** file1_line_range **** file1 line file1 line file2_line_range file2 line file2 line The first three lines identify the files compared and separate this information from the rest of the output, which is one or more hunks of differences. Each hunk shows one area where the files differ, surrounded (by default) by two line of context (where the files are the same). Context lines begin with two spaces and differing lines begin with a !, +, or -, followed by one space, illustrating the difference between the files. A + indicates a line in the file2 that does not exist in file1, so in a sense, a + line was added to file1 to cre- ate file2. A - marks a line in file1 that does not appear in file2, suggesting a subtrac- tion operation. A ! indicates a line that was changed between file1 and file2; for each line or group of lines from file1 marked with !, a corresponding line or group of lines from file2 is also marked with a !. To generate a context diff, execute a command similar to the following: $ diff -C 1 sigrot.1 sigrot.2 The hunks look like the following: *** sigrot.1 Sun Mar 14 22:41:34 1999 sigrot.2 Mon Mar 15 00:17:40 1999 **** 2,4 **** # sigrot.sh ! # Version 1.0 # Rotate signatures 2,4 # sigrot.sh ! # Version 2.0 # Rotate signatures *************** *** 8,19 **** sigfile=signature ! old=$(cat num) let new=$(expr $old+1) ! if [ -f $sigfile.$new ]; then ! cp $sigfile.$new .$sigfile ! echo $new > num else ! cp $sigfile.1 .$sigfile ! echo 1 > num Comparing and Merging Source Files C HAPTER 6 91 6 COMPARING AND MERGING SOURCE FILES 0872316072 CH06 7/26/99 1:42 PM Page 91 fi 8,21 sigfile=signature + srcdir=$HOME/doc/signatures + srcfile=$srcdir/$sigfile ! old=$(cat $srcdir/num) let new=$(expr $old+1) ! if [ -f $srcfile.$new ]; then ! cp $srcfile.$new $HOME/.$sigfile ! echo $new > $srcdir/num else ! cp $srcfile.1 $HOME/.$sigfile ! echo 1 > $srcdir/num fi************** The Linux Programming Toolkit P ART I 92 NOTE To shorten the display, -C 1 was used to indicate that only a single line of con- text should be displayed. The patch command requires at least two lines of con- text to function properly. So when you generate context diffs to distribute as software patches, request at least two lines of context. The output shows two hunks, one covering lines 2–4 in both files, the other covering lines 8–19 in sigrot.1 and lines 8–21 in sigrot.2. In the first hunk, the differing lines are marked with a ! in the first column. The change is minimal, as you can see, merely an incremented version number. In the second hunk, there are many more changes, and two lines were added to sigrot.2, indicated by the +. Each change and addition in both hunks is surrounded by a single line of context. The Unified Output Format Unified format is a modified version of context format that suppresses the display of repeated context lines and compacts the output in other ways as well. Unified format begins with a header identifying the files compared file1 file1_timestamp +++ file2 file2_timestamp followed by one or more hunks in the form @@ file1_range file2_range @@ line_from_either_file line_from_either_file 0872316072 CH06 7/26/99 1:42 PM Page 92 Context lines begin with a single space and differing lines begin with a + or a -, indicat- ing that a line was added or removed at this location with respect to file1. The follow- ing listing was generated with the command diff –U 1 sigrot.1 sigrot.2. sigrot.1 Sun Mar 14 2:41:34 1999 +++ sigrot.2 Mon Mar 15 00:17:40 1999 @@ -2,3 +2,3 @@ # sigrot.sh -# Version 1.0 +# Version 2.0 # Rotate signatures @@ -8,12 +8,14 @@ sigfile=signature +srcdir=$HOME/doc/signatures +srcfile=$srcdir/$sigfile -old=$(cat num) +old=$(cat $srcdir/num) let new=$(expr $old+1) -if [ -f $sigfile.$new ]; then - cp $sigfile.$new .$sigfile - echo $new > num +if [ -f $srcfile.$new ]; then + cp $srcfile.$new $HOME/.$sigfile + echo $new > $srcdir/num else - cp $sigfile.1 .$sigfile - echo 1 > num + cp $srcfile.1 $HOME/.$sigfile + echo 1 > $srcdir/num fi As you can see, the unified format’s output is much more compact, but just as easy to understand without repeated context lines cluttering the display. Again, we have two hunks. The first hunk consists of lines 2–3 in both files, the second lines 8–12 in sigrot.1 and lines 8–14 of sigrot.2. The first hunk says “delete ‘# Version 1.0’ from file1 and add ‘# Version 2.0’ to file1 to create file2.” The second hunk has three sim- ilar sets of additions and deletions, plus a simple addition of two lines at the top of the hunk. As useful and compact as the unified format is, however, there is a catch: only GNU diff generates unified diffs and only GNU patch understands the unified format. So, if you are distributing software patches to systems that do not or may not use GNU diff and GNU patch, don’t use unified format. Use the standard context format. Comparing and Merging Source Files C HAPTER 6 93 6 COMPARING AND MERGING SOURCE FILES 0872316072 CH06 7/26/99 1:42 PM Page 93 Additional diff Features In addition to the normal, context, and unified formats we have discussed, diff can also produce side-by-side comparisons, ed scripts for modifying or converting files, and an RCS-compatible output format, and it contains a sophisticated ability to merge files using an if-then-else format. To generate side-by-side output, use diff’s -y or side- by-side options. Note, however, that the output will be wider than usual and long lines will be truncated. To generate ed scripts, use the -e or ed options. For information about diff’s RCS and if-then-else capabilities, see the documentation—they are not dis- cussed in this book because they are esoteric and not widely used. diff Command-Line Options Like most GNU programs, diff sports a bewildering array of options to fine tune its behavior. Table 6.1 summarizes some of these options. For a complete list of all options, use the command diff help. TABLE 6.1 SELECTED diff OPTIONS Option Meaning binary Read and write data in binary mode -c|-C NUM| context=NUM Produce context format output, displaying NUM lines of context -t| expand-tabs Expand tabs to spaces in the output -i| ignore-case Ignore case changes, treating upper- and lowercase letters the same -H| speed-large-files Modify diff’s handling of large files -w| ignore-all-space Ignore whitespace when comparing lines -I REGEXP| ignore- matching-lines=REGEXP Ignore lines that insert or delete lines that match the regular expression REGEXP -B| ignore-blank-lines Ignore changes that insert or delete blank lines -b| ignore-space-change Ignore changes in the amount of whitespace -l| paginate Paginate the output by passing it through pr -p| show-c-function Show the C function in which a change occurs -q| brief Only report if files differ, do not output the dif- ferences -a| text Treat all files as text, even if they appear to be binary, and perform a line-by-line comparison The Linux Programming Toolkit P ART I 94 0872316072 CH06 7/26/99 1:42 PM Page 94 Option Meaning -u|-U NUM| unified=NUM Produce unified format output, displaying NUM lines of context -v| version Print diff’s version number -y| side-by-side Produce side-by-side format output Understanding the diff3 Command diff3 shows its usefulness when two people change a common file. It compares the two sets of changes, creates a third file containing the merged output, and indicates conflicts between the changes. diff3’s syntax is: diff3 [options] myfile oldfile yourfile oldfile is the common ancestor from which myfile and yourfile were derived. Listing 6.5 introduces sigrot.3. It is the same as sigrot.1, except that we added a return statement at the end of the script. LISTING 6.5 sigrot.3 #!/usr/local/bin/bash # sigrot.sh # Version 3.0 # Rotate signatures # Suitable to be run via cron ############################# sigfile=signature old=$(cat num) let new=$(expr $old+1) if [ -f $sigfile.$new ]; then cp $sigfile.$new .$sigfile echo $new > num else cp $sigfile.1 .$sigfile echo 1 > num fi return 0 Predictably, diff3’s output is more complex because it must juggle three input files. diff3 only displays lines that vary between the files. Hunks in which all three input files Comparing and Merging Source Files C HAPTER 6 95 6 COMPARING AND MERGING SOURCE FILES 0872316072 CH06 7/26/99 1:42 PM Page 95 [...]... essential part of your Linux software development toolkit 6 COMPARING AND MERGING SOURCE FILES Summary 101 08 723 160 72 CH06 7 /26 /99 1: 42 PM Page 1 02 1 02 09 723 160 72 CH07 7 /26 /99 1:41 PM Page 103 Version Control with RCS CHAPTER 7 by Kurt Wall IN THIS CHAPTER • Terminology 104 • Basic RCS Usage • rcsdiff 105 110 • Other RCS Commands 113 09 723 160 72 CH07 7 /26 /99 1:41 PM Page 104 104 The Linux Programming Toolkit... rcsmerge, and rlog 10 723 160 72 CH08 7 /26 /99 2: 38 PM Page 115 Creating Programs in Emacs CHAPTER 8 by Kurt Wall and Mark Watson IN THIS CHAPTER • Introduction to Emacs 116 • Features Supporting Programming 125 • Automating Development with Emacs Lisp 1 32 10 723 160 72 CH08 7 /26 /99 2: 38 PM Page 116 116 The Linux Programming Toolkit PART I Emacs provides a rich, highly configurable programming environment... $Id: prn_env.c,v 1.3 1999/01/06 03: 12: 22 kwall Exp $ 11d10 < static char rcsid[] = ➥“$Id: prn_env.c,v 1.1 1999/01/06 03:04:40 Exp kwall $\n”; 21 a21 > sleep(5); Next, compare 1 .2 to 1.3: $ rcsdiff -r1 .2 -r1.3 prn_env.c =================================================================== RCS file: RCS/prn_env.c,v retrieving revision 1 .2 retrieving revision 1.3 diff -r1 .2 -r1.3 1c1 < /* $Id: prn_env.c,v... lines srcdir=$HOME/doc/signatures srcfile=$srcdir/$sigfile from sigrot .2 must be appended after line 8 of sigrot.1 and sigrot.3 Similarly, to obtain sigrot.1 from sigrot .2, line 10 from sigrot.1 must be changed to line 12 from sigrot.1 08 723 160 72 CH06 7 /26 /99 1: 42 PM Page 97 Comparing and Merging Source Files CHAPTER 6 $ diff3 -m sigrot .2 sigrot.1 sigrot.3 > sigrot.merged merges the files, marks conflicting... You should now have three revisions of prn_env.c in the repository The general format for comparing specific file revisions using rcsdiff is rcsdiff [ -rFILE1 [ -rFILE2 ] ] FILENAME 09 723 160 72 CH07 7 /26 /99 1:41 PM Page 1 12 1 12 The Linux Programming Toolkit PART I First, compare revision 1.1 to the working file: $ rcsdiff -r1.1 prn_env.c ===================================================================... type, you should get a screen that looks like Figure 8.1, Figure 8 .2, or Figure 8.3 FIGURE 8.1 Menu bar Emacs on a text mode console Editing window 8 Status bar Minibuffer Menu bar Emacs, with Athena (X) support Editing window Status bar Minibuffer CREATING PROGRAMS IN EMACS FIGURE 8 .2 10 723 160 72 CH08 7 /26 /99 2: 38 PM Page 118 118 The Linux Programming Toolkit PART I FIGURE 8.3 XEmacs has an attractive... your Alt key works like the Meta (M-) key, M-n, where n is some digit, works as a multiplier, too CREATING PROGRAMS IN EMACS • C-p—Moves the cursor to the previous line 8 10 723 160 72 CH08 7 /26 /99 2: 38 PM Page 120 120 The Linux Programming Toolkit PART I Inserting Text Emacs editing is simple: just start typing Each character you type is inserted at the “point,” which, in most cases, is the cursor In... example, $ diff3 sigrot .2 sigrot.1 sigrot.3 yields (output truncated to conserve space): ==== 1:3c # Version 2. 0 2: 3c # Version 1.0 3:3c # Version 3.0 ====1 1:9,10c srcdir=$HOME/doc/signatures srcfile=$srcdir/$sigfile 2: 8a 3:8a ====1 1:12c old=$(cat $srcdir/num) 2: 10c 3:10c old=$(cat num) The first hunk is a three-way hunk The other hunks are two-way hunks To obtain sigrot .2 from sigrot.1 or sigrot.3,...08 723 160 72 CH06 7 /26 /99 1: 42 PM Page 96 96 The Linux Programming Toolkit PART I are different are called three-way hunks; two-way hunks occur when only two of the three files differ Three-way hunks are indicated with ====, while two-way hunks add a 1, 2, or 3 at the end to indicate which of the files is different After this header,... marker lines and the lines indicating the 1.0 and 2. 0 versions . file2 file2_timestamp The Linux Programming Toolkit P ART I 90 08 723 160 72 CH06 7 /26 /99 1: 42 PM Page 90 *************** *** file1_line_range **** file1 line file1 line file2_line_range file2 line file2. your Linux software development toolkit. Comparing and Merging Source Files C HAPTER 6 101 6 COMPARING AND MERGING SOURCE FILES 08 723 160 72 CH06 7 /26 /99 1: 42 PM Page 101 1 02 08 723 160 72 CH06 7 /26 /99. 6 .2 lists commonly used patch options. For complete details, try patch help or the patch info pages. The Linux Programming Toolkit P ART I 98 08 723 160 72 CH06 7 /26 /99 1: 42 PM Page 98 TABLE 6.2

Ngày đăng: 12/08/2014, 21:20

TỪ KHÓA LIÊN QUAN