@matheusbsbs / matheus-bagliano / commits / 4050c0df8e
Fork 0

Clean up compatibility definitions.

This attempts to clean up the way various compatibility functions are defined and used. - A new header file, git-compat-util.h, is introduced. This looks at various NO_XXX and does necessary function name replacements, equivalent of -Dstrcasestr=gitstrcasestr in the Makefile. - Those function name replacements are removed from the Makefile. - Common features such as usage(), die(), xmalloc() are moved from cache.h to git-compat-util.h; cache.h includes git-compat-util.h itself. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 5, 2005 at 11:54 UTC 4050c0df8ec7043315e2192695de432875239775
8 files changed +129 -118
Makefile
+6 -6
@@ -162,7 +162,7 @@ LIB_FILE=libgit.a
162 LIB_H = \
163 blob.h cache.h commit.h count-delta.h csum-file.h delta.h \
164 diff.h epoch.h object.h pack.h pkt-line.h quote.h refs.h \
165 - run-command.h strbuf.h tag.h tree.h
165 + run-command.h strbuf.h tag.h tree.h git-compat-util.h
166
167 DIFF_OBJS = \
168 diff.o diffcore-break.o diffcore-order.o diffcore-pathspec.o \
@@ -320,15 +320,15 @@ ifdef NEEDS_NSL
320 SIMPLE_LIB += -lnsl
321 endif
322 ifdef NO_STRCASESTR
323 - COMPAT_CFLAGS += -Dstrcasestr=gitstrcasestr -DNO_STRCASESTR=1
323 + COMPAT_CFLAGS += -DNO_STRCASESTR
324 COMPAT_OBJS += compat/strcasestr.o
325 endif
326 ifdef NO_SETENV
327 - COMPAT_CFLAGS += -Dsetenv=gitsetenv -DNO_SETENV=1
327 + COMPAT_CFLAGS += -DNO_SETENV
328 COMPAT_OBJS += compat/setenv.o
329 endif
330 ifdef NO_MMAP
331 - COMPAT_CFLAGS += -Dmmap=gitfakemmap -Dmunmap=gitfakemunmap -DNO_MMAP
331 + COMPAT_CFLAGS += -DNO_MMAP
332 COMPAT_OBJS += compat/mmap.o
333 endif
334 ifdef NO_IPV6
@@ -363,9 +363,9 @@ all: $(ALL_PROGRAMS)
363 all:
364 $(MAKE) -C templates
365
366 -git$(X): git.c $(COMPAT_OBJS) Makefile
366 +git$X: git.c $(LIB_FILE) Makefile
367 $(CC) -DGIT_EXEC_PATH='"$(bindir)"' -DGIT_VERSION='"$(GIT_VERSION)"' \
368 - $(CFLAGS) $(COMPAT_CFLAGS) -o $@ $(filter %.c,$^) $(filter %.o,$^)
368 + $(CFLAGS) $(COMPAT_CFLAGS) -o $@ $(filter %.c,$^) $(LIB_FILE)
369
370 $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
371 rm -f $@
cache.h
+1 -95
@@ -1,23 +1,7 @@
1 #ifndef CACHE_H
2 #define CACHE_H
3
4 -#include <unistd.h>
5 -#include <stdio.h>
6 -#include <sys/stat.h>
7 -#include <fcntl.h>
8 -#include <stddef.h>
9 -#include <stdlib.h>
10 -#include <stdarg.h>
11 -#include <string.h>
12 -#include <errno.h>
13 -#include <limits.h>
14 -#ifndef NO_MMAP
15 -#include <sys/mman.h>
16 -#endif
17 -#include <sys/param.h>
18 -#include <netinet/in.h>
19 -#include <sys/types.h>
20 -#include <dirent.h>
4 +#include "git-compat-util.h"
5
6 #include SHA1_HEADER
7 #include <zlib.h>
@@ -36,15 +20,6 @@
20 #define DTYPE(de) DT_UNKNOWN
21 #endif
22
39 -#ifdef __GNUC__
40 -#define NORETURN __attribute__((__noreturn__))
41 -#else
42 -#define NORETURN
43 -#ifndef __attribute__
44 -#define __attribute__(x)
45 -#endif
46 -#endif
47 -
23 /*
24 * Intensive research over the course of many years has shown that
25 * port 9418 is totally unused by anything else. Or
@@ -250,11 +225,6 @@ extern const char *resolve_ref(const char *path, unsigned char *sha1, int);
225 extern int create_symref(const char *git_HEAD, const char *refs_heads_master);
226 extern int validate_symref(const char *git_HEAD);
227
253 -/* General helper functions */
254 -extern void usage(const char *err) NORETURN;
255 -extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
256 -extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
257 -
228 extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
229 extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);
230
@@ -272,30 +242,6 @@ extern int setup_ident(void);
242 extern const char *git_author_info(void);
243 extern const char *git_committer_info(void);
244
275 -static inline void *xmalloc(size_t size)
276 -{
277 - void *ret = malloc(size);
278 - if (!ret)
279 - die("Out of memory, malloc failed");
280 - return ret;
281 -}
282 -
283 -static inline void *xrealloc(void *ptr, size_t size)
284 -{
285 - void *ret = realloc(ptr, size);
286 - if (!ret)
287 - die("Out of memory, realloc failed");
288 - return ret;
289 -}
290 -
291 -static inline void *xcalloc(size_t nmemb, size_t size)
292 -{
293 - void *ret = calloc(nmemb, size);
294 - if (!ret)
295 - die("Out of memory, calloc failed");
296 - return ret;
297 -}
298 -
245 struct checkout {
246 const char *base_dir;
247 int base_dir_len;
@@ -373,20 +319,6 @@ extern void packed_object_info_detail(struct pack_entry *, char *, unsigned long
319 /* Dumb servers support */
320 extern int update_server_info(int);
321
376 -#ifdef NO_MMAP
377 -
378 -#ifndef PROT_READ
379 -#define PROT_READ 1
380 -#define PROT_WRITE 2
381 -#define MAP_PRIVATE 1
382 -#define MAP_FAILED ((void*)-1)
383 -#endif
384 -
385 -extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
386 -extern int gitfakemunmap(void *start, size_t length);
387 -
388 -#endif
389 -
322 typedef int (*config_fn_t)(const char *, const char *);
323 extern int git_default_config(const char *, const char *);
324 extern int git_config_from_file(config_fn_t fn, const char *);
@@ -404,31 +336,5 @@ extern char git_default_name[MAX_GITNAME];
336 #define MAX_ENCODING_LENGTH 64
337 extern char git_commit_encoding[MAX_ENCODING_LENGTH];
338
407 -/* Sane ctype - no locale, and works with signed chars */
408 -#undef isspace
409 -#undef isdigit
410 -#undef isalpha
411 -#undef isalnum
412 -#undef tolower
413 -#undef toupper
414 -extern unsigned char sane_ctype[256];
415 -#define GIT_SPACE 0x01
416 -#define GIT_DIGIT 0x02
417 -#define GIT_ALPHA 0x04
418 -#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
419 -#define isspace(x) sane_istest(x,GIT_SPACE)
420 -#define isdigit(x) sane_istest(x,GIT_DIGIT)
421 -#define isalpha(x) sane_istest(x,GIT_ALPHA)
422 -#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
423 -#define tolower(x) sane_case((unsigned char)(x), 0x20)
424 -#define toupper(x) sane_case((unsigned char)(x), 0)
425 -
426 -static inline int sane_case(int x, int high)
427 -{
428 - if (sane_istest(x, GIT_ALPHA))
429 - x = (x & ~0x20) | high;
430 - return x;
431 -}
432 -
339 extern int copy_fd(int ifd, int ofd);
340 #endif /* CACHE_H */
compat/mmap.c
+1 -1
@@ -2,7 +2,7 @@
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 -#include "../cache.h"
5 +#include "../git-compat-util.h"
6
7 void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
8 {
compat/strcasestr.c
+1 -2
@@ -1,5 +1,4 @@
1 -#include <string.h>
2 -#include <ctype.h>
1 +#include "../git-compat-util.h"
2
3 char *gitstrcasestr(const char *haystack, const char *needle)
4 {
git-compat-util.h new
+113
@@ -0,0 +1,113 @@
1 +#ifndef GIT_COMPAT_UTIL_H
2 +#define GIT_COMPAT_UTIL_H
3 +
4 +#include <unistd.h>
5 +#include <stdio.h>
6 +#include <sys/stat.h>
7 +#include <fcntl.h>
8 +#include <stddef.h>
9 +#include <stdlib.h>
10 +#include <stdarg.h>
11 +#include <string.h>
12 +#include <errno.h>
13 +#include <limits.h>
14 +#include <sys/param.h>
15 +#include <netinet/in.h>
16 +#include <sys/types.h>
17 +#include <dirent.h>
18 +
19 +#ifdef __GNUC__
20 +#define NORETURN __attribute__((__noreturn__))
21 +#else
22 +#define NORETURN
23 +#ifndef __attribute__
24 +#define __attribute__(x)
25 +#endif
26 +#endif
27 +
28 +/* General helper functions */
29 +extern void usage(const char *err) NORETURN;
30 +extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
31 +extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
32 +
33 +#ifdef NO_MMAP
34 +
35 +#ifndef PROT_READ
36 +#define PROT_READ 1
37 +#define PROT_WRITE 2
38 +#define MAP_PRIVATE 1
39 +#define MAP_FAILED ((void*)-1)
40 +#endif
41 +
42 +#define mmap gitfakemmap
43 +#define munmap gitfakemunmap
44 +extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
45 +extern int gitfakemunmap(void *start, size_t length);
46 +
47 +#else /* NO_MMAP */
48 +
49 +#include <sys/mman.h>
50 +
51 +#endif /* NO_MMAP */
52 +
53 +#ifdef NO_SETENV
54 +#define setenv gitsetenv
55 +extern int gitsetenv(const char *, const char *, int);
56 +#endif
57 +
58 +#ifdef NO_STRCASESTR
59 +#define strcasestr gitstrcasestr
60 +extern char *gitstrcasestr(const char *haystack, const char *needle);
61 +#endif
62 +
63 +static inline void *xmalloc(size_t size)
64 +{
65 + void *ret = malloc(size);
66 + if (!ret)
67 + die("Out of memory, malloc failed");
68 + return ret;
69 +}
70 +
71 +static inline void *xrealloc(void *ptr, size_t size)
72 +{
73 + void *ret = realloc(ptr, size);
74 + if (!ret)
75 + die("Out of memory, realloc failed");
76 + return ret;
77 +}
78 +
79 +static inline void *xcalloc(size_t nmemb, size_t size)
80 +{
81 + void *ret = calloc(nmemb, size);
82 + if (!ret)
83 + die("Out of memory, calloc failed");
84 + return ret;
85 +}
86 +
87 +/* Sane ctype - no locale, and works with signed chars */
88 +#undef isspace
89 +#undef isdigit
90 +#undef isalpha
91 +#undef isalnum
92 +#undef tolower
93 +#undef toupper
94 +extern unsigned char sane_ctype[256];
95 +#define GIT_SPACE 0x01
96 +#define GIT_DIGIT 0x02
97 +#define GIT_ALPHA 0x04
98 +#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
99 +#define isspace(x) sane_istest(x,GIT_SPACE)
100 +#define isdigit(x) sane_istest(x,GIT_DIGIT)
101 +#define isalpha(x) sane_istest(x,GIT_ALPHA)
102 +#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
103 +#define tolower(x) sane_case((unsigned char)(x), 0x20)
104 +#define toupper(x) sane_case((unsigned char)(x), 0)
105 +
106 +static inline int sane_case(int x, int high)
107 +{
108 + if (sane_istest(x, GIT_ALPHA))
109 + x = (x & ~0x20) | high;
110 + return x;
111 +}
112 +
113 +#endif
git.c
+6 -9
@@ -8,15 +8,12 @@
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdarg.h>
11 +#include "git-compat-util.h"
12
13 #ifndef PATH_MAX
14 # define PATH_MAX 4096
15 #endif
16
16 -#ifdef NO_SETENV
17 -extern int gitsetenv(const char *, const char *, int);
18 -#endif
19 -
17 static const char git_usage[] =
18 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
19
@@ -156,10 +153,10 @@ static void list_commands(const char *exec_path, const char *pattern)
153 }
154
155 #ifdef __GNUC__
159 -static void usage(const char *exec_path, const char *fmt, ...)
156 +static void cmd_usage(const char *exec_path, const char *fmt, ...)
157 __attribute__((__format__(__printf__, 2, 3), __noreturn__));
158 #endif
162 -static void usage(const char *exec_path, const char *fmt, ...)
159 +static void cmd_usage(const char *exec_path, const char *fmt, ...)
160 {
161 if (fmt) {
162 va_list ap;
@@ -254,12 +251,12 @@ int main(int argc, char **argv, char **envp)
251 else if (!strcmp(arg, "help"))
252 show_help = 1;
253 else if (!show_help)
257 - usage(NULL, NULL);
254 + cmd_usage(NULL, NULL);
255 }
256
257 if (i >= argc || show_help) {
258 if (i >= argc)
262 - usage(exec_path, NULL);
259 + cmd_usage(exec_path, NULL);
260
261 show_man_page(argv[i]);
262 }
@@ -297,7 +294,7 @@ int main(int argc, char **argv, char **envp)
294 execve(git_command, &argv[i], envp);
295
296 if (errno == ENOENT)
300 - usage(exec_path, "'%s' is not a git-command", argv[i]);
297 + cmd_usage(exec_path, "'%s' is not a git-command", argv[i]);
298
299 fprintf(stderr, "Failed to run command '%s': %s\n",
300 git_command, strerror(errno));
mailinfo.c
-4
@@ -10,10 +10,6 @@
10 #include <iconv.h>
11 #include "cache.h"
12
13 -#ifdef NO_STRCASESTR
14 -extern char *gitstrcasestr(const char *haystack, const char *needle);
15 -#endif
16 -
13 static FILE *cmitmsg, *patchfile;
14
15 static int keep_subject = 0;
usage.c
+1 -1
@@ -3,7 +3,7 @@
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 -#include "cache.h"
6 +#include "git-compat-util.h"
7
8 static void report(const char *prefix, const char *err, va_list params)
9 {