1 /* common definitions for 'patch' */ 2 3 /* Copyright (C) 1986, 1988 Larry Wall 4 5 Copyright (C) 1990-1993, 1997-1999, 2002-2003, 2006, 2009-2012 Free Software 6 Foundation, Inc. 7 8 This program is free software: you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation, either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20 21 #ifndef DEBUGGING 22 #define DEBUGGING 1 23 #endif 24 25 #include <config.h> 26 27 #include <assert.h> 28 #include <stdbool.h> 29 #include <stdio.h> 30 #include <sys/types.h> 31 #include <time.h> 32 33 #include <sys/stat.h> 34 35 #include <limits.h> 36 37 #if HAVE_INTTYPES_H 38 # include <inttypes.h> 39 #elif HAVE_STDINT_H 40 # include <stdint.h> 41 #endif 42 #include <intprops.h> 43 44 #include <ctype.h> 45 /* CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given 46 as an argument to <ctype.h> macros like 'isspace'. */ 47 #if STDC_HEADERS 48 #define CTYPE_DOMAIN(c) 1 49 #else 50 #define CTYPE_DOMAIN(c) ((unsigned) (c) <= 0177) 51 #endif 52 #ifndef ISSPACE 53 #define ISSPACE(c) (CTYPE_DOMAIN (c) && isspace (c)) 54 #endif 55 56 #ifndef ISDIGIT 57 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9) 58 #endif 59 60 #include <progname.h> 61 62 /* handy definitions */ 63 64 #define strEQ(s1,s2) (!strcmp(s1, s2)) 65 #define strnEQ(s1,s2,l) (!strncmp(s1, s2, l)) 66 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) 67 68 /* typedefs */ 69 70 typedef off_t lin; /* must be signed */ 71 72 #define LINENUM_MIN TYPE_MINIMUM (lin) 73 #define LINENUM_MAX TYPE_MAXIMUM (lin) 74 75 /* globals */ 76 77 XTERN char *buf; /* general purpose buffer */ 78 XTERN size_t bufsize; /* allocated size of buf */ 79 80 XTERN bool using_plan_a; /* try to keep everything in memory */ 81 82 XTERN char *inname; 83 XTERN char *outfile; 84 XTERN int inerrno; 85 XTERN int invc; 86 XTERN struct stat instat; 87 XTERN bool dry_run; 88 XTERN bool posixly_correct; 89 90 XTERN char const *origprae; 91 XTERN char const *origbase; 92 XTERN char const *origsuff; 93 94 XTERN char const * TMPINNAME; 95 XTERN char const * TMPOUTNAME; 96 XTERN char const * TMPPATNAME; 97 XTERN char const * TMPEDNAME; 98 99 XTERN bool TMPINNAME_needs_removal; 100 XTERN bool TMPOUTNAME_needs_removal; 101 XTERN bool TMPPATNAME_needs_removal; 102 XTERN bool TMPEDNAME_needs_removal; 103 104 #ifdef DEBUGGING 105 XTERN int debug; 106 #else 107 # define debug 0 108 #endif 109 XTERN bool force; 110 XTERN bool batch; 111 XTERN bool noreverse; 112 XTERN bool reverse; 113 XTERN enum { DEFAULT_VERBOSITY, SILENT, VERBOSE } verbosity; 114 XTERN bool skip_rest_of_patch; 115 XTERN int strippath; 116 XTERN bool canonicalize_ws; 117 XTERN int patch_get; 118 XTERN bool set_time; 119 XTERN bool set_utc; 120 XTERN bool follow_symlinks; 121 122 enum diff 123 { 124 NO_DIFF, 125 CONTEXT_DIFF, 126 NORMAL_DIFF, 127 ED_DIFF, 128 NEW_CONTEXT_DIFF, 129 UNI_DIFF, 130 GIT_BINARY_DIFF 131 }; 132 133 XTERN enum diff diff_type; 134 135 XTERN char *revision; /* prerequisite revision, if any */ 136 137 #ifndef __attribute__ 138 /* The __attribute__ feature is available in gcc versions 2.5 and later. 139 The __-protected variants of the attributes 'format' and 'printf' are 140 accepted by gcc versions 2.6.4 (effectively 2.7) and later. 141 We enable __attribute__ only if these are supported too, because 142 gnulib and libintl do '#define printf __printf__' when they override 143 the 'printf' function. */ 144 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) 145 # define __attribute__(Spec) /* empty */ 146 # endif 147 #endif 148 149 void fatal_exit (int) __attribute__ ((noreturn)); 150 151 #include <errno.h> 152 #if !STDC_HEADERS && !defined errno 153 extern int errno; 154 #endif 155 156 #include <string.h> 157 #include <stdlib.h> 158 #include <unistd.h> 159 160 #if HAVE_FSEEKO 161 typedef off_t file_offset; 162 # define file_seek fseeko 163 # define file_tell ftello 164 #else 165 typedef long file_offset; 166 # define file_seek fseek 167 # define file_tell ftell 168 #endif 169 170 #if ! (HAVE_GETEUID || defined geteuid) 171 # if ! (HAVE_GETUID || defined getuid) 172 # define geteuid() (-1) 173 # else 174 # define geteuid() getuid () 175 # endif 176 #endif 177 178 #include <fcntl.h> 179 180 #ifdef HAVE_SETMODE_DOS 181 XTERN int binary_transput; /* O_BINARY if binary i/o is desired */ 182 #else 183 # define binary_transput 0 184 #endif 185 186 /* Disable the CR stripping heuristic? */ 187 XTERN bool no_strip_trailing_cr; 188 189 #ifndef NULL_DEVICE 190 #define NULL_DEVICE "/dev/null" 191 #endif 192 193 #ifndef TTY_DEVICE 194 #define TTY_DEVICE "/dev/tty" 195 #endif 196 197 /* Output stream state. */ 198 struct outstate 199 { 200 FILE *ofp; 201 bool after_newline; 202 bool zero_output; 203 }; 204 205 /* offset in the input and output at which the previous hunk matched */ 206 XTERN lin in_offset; 207 XTERN lin out_offset; 208 209 /* how many input lines have been irretractably output */ 210 XTERN lin last_frozen_line; 211 212 bool copy_till (struct outstate *, lin); 213 bool similar (char const *, size_t, char const *, size_t) _GL_ATTRIBUTE_PURE; 214 215 #ifdef ENABLE_MERGE 216 enum conflict_style { MERGE_MERGE, MERGE_DIFF3 }; 217 XTERN enum conflict_style conflict_style; 218 219 bool merge_hunk (int hunk, struct outstate *, lin where, bool *); 220 #else 221 # define merge_hunk(hunk, outstate, where, somefailed) false 222 #endif 223