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
70typedef 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
77XTERN char *buf;			/* general purpose buffer */
78XTERN size_t bufsize;			/* allocated size of buf */
79
80XTERN bool using_plan_a;		/* try to keep everything in memory */
81
82XTERN char *inname;
83XTERN char *outfile;
84XTERN int inerrno;
85XTERN int invc;
86XTERN struct stat instat;
87XTERN bool dry_run;
88XTERN bool posixly_correct;
89
90XTERN char const *origprae;
91XTERN char const *origbase;
92XTERN char const *origsuff;
93
94XTERN char const * TMPINNAME;
95XTERN char const * TMPOUTNAME;
96XTERN char const * TMPPATNAME;
97
98XTERN bool TMPINNAME_needs_removal;
99XTERN bool TMPOUTNAME_needs_removal;
100XTERN bool TMPPATNAME_needs_removal;
101
102#ifdef DEBUGGING
103XTERN int debug;
104#else
105# define debug 0
106#endif
107XTERN bool force;
108XTERN bool batch;
109XTERN bool noreverse;
110XTERN bool reverse;
111XTERN enum { DEFAULT_VERBOSITY, SILENT, VERBOSE } verbosity;
112XTERN bool skip_rest_of_patch;
113XTERN int strippath;
114XTERN bool canonicalize_ws;
115XTERN int patch_get;
116XTERN bool set_time;
117XTERN bool set_utc;
118XTERN bool follow_symlinks;
119
120enum diff
121  {
122    NO_DIFF,
123    CONTEXT_DIFF,
124    NORMAL_DIFF,
125    ED_DIFF,
126    NEW_CONTEXT_DIFF,
127    UNI_DIFF,
128    GIT_BINARY_DIFF
129  };
130
131XTERN enum diff diff_type;
132
133XTERN char *revision;			/* prerequisite revision, if any */
134
135#ifndef __attribute__
136/* The __attribute__ feature is available in gcc versions 2.5 and later.
137   The __-protected variants of the attributes 'format' and 'printf' are
138   accepted by gcc versions 2.6.4 (effectively 2.7) and later.
139   We enable __attribute__ only if these are supported too, because
140   gnulib and libintl do '#define printf __printf__' when they override
141   the 'printf' function.  */
142# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
143#  define __attribute__(Spec)   /* empty */
144# endif
145#endif
146
147void fatal_exit (int) __attribute__ ((noreturn));
148
149#include <errno.h>
150#if !STDC_HEADERS && !defined errno
151extern int errno;
152#endif
153
154#include <string.h>
155#include <stdlib.h>
156#include <unistd.h>
157
158#if HAVE_FSEEKO
159  typedef off_t file_offset;
160# define file_seek fseeko
161# define file_tell ftello
162#else
163  typedef long file_offset;
164# define file_seek fseek
165# define file_tell ftell
166#endif
167
168#if ! (HAVE_GETEUID || defined geteuid)
169# if ! (HAVE_GETUID || defined getuid)
170#  define geteuid() (-1)
171# else
172#  define geteuid() getuid ()
173# endif
174#endif
175
176#include <fcntl.h>
177
178#ifdef HAVE_SETMODE_DOS
179  XTERN int binary_transput;	/* O_BINARY if binary i/o is desired */
180#else
181# define binary_transput 0
182#endif
183
184/* Disable the CR stripping heuristic?  */
185XTERN bool no_strip_trailing_cr;
186
187#ifndef NULL_DEVICE
188#define NULL_DEVICE "/dev/null"
189#endif
190
191#ifndef TTY_DEVICE
192#define TTY_DEVICE "/dev/tty"
193#endif
194
195/* Output stream state.  */
196struct outstate
197{
198  FILE *ofp;
199  bool after_newline;
200  bool zero_output;
201};
202
203/* offset in the input and output at which the previous hunk matched */
204XTERN lin in_offset;
205XTERN lin out_offset;
206
207/* how many input lines have been irretractably output */
208XTERN lin last_frozen_line;
209
210bool copy_till (struct outstate *, lin);
211bool similar (char const *, size_t, char const *, size_t) _GL_ATTRIBUTE_PURE;
212
213#ifdef ENABLE_MERGE
214enum conflict_style { MERGE_MERGE, MERGE_DIFF3 };
215XTERN enum conflict_style conflict_style;
216
217bool merge_hunk (int hunk, struct outstate *, lin where, bool *);
218#else
219# define merge_hunk(hunk, outstate, where, somefailed) false
220#endif
221