lib.h revision 194497
1/* $FreeBSD: head/usr.sbin/pkg_install/lib/lib.h 194497 2009-06-19 17:07:38Z brian $ */
2
3/*
4 * FreeBSD install - a package for the installation and maintainance
5 * of non-core utilities.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * Jordan K. Hubbard
17 * 18 July 1993
18 *
19 * Include and define various things wanted by the library routines.
20 *
21 */
22
23#ifndef _INST_LIB_LIB_H_
24#define _INST_LIB_LIB_H_
25
26/* Includes */
27#include <sys/param.h>
28#include <sys/file.h>
29#include <sys/stat.h>
30#include <sys/queue.h>
31#include <ctype.h>
32#include <dirent.h>
33#include <stdarg.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39/* Macros */
40#define SUCCESS	(0)
41#define	FAIL	(-1)
42
43#ifndef TRUE
44#define TRUE	(1)
45#endif
46
47#ifndef FALSE
48#define FALSE	(0)
49#endif
50
51#define YES		2
52#define NO		1
53
54/* Usually "rm", but often "echo" during debugging! */
55#define REMOVE_CMD	"/bin/rm"
56
57/* Usually "rm", but often "echo" during debugging! */
58#define RMDIR_CMD	"/bin/rmdir"
59
60/* Where the ports lives by default */
61#define DEF_PORTS_DIR   "/usr/ports"
62/* just in case we change the environment variable name */
63#define PORTSDIR    "PORTSDIR"
64/* macro to get name of directory where the ports lives */
65#define PORTS_DIR       (getenv(PORTSDIR) ? getenv(PORTSDIR) : DEF_PORTS_DIR)
66
67/* Where we put logging information by default, else ${PKG_DBDIR} if set */
68#define DEF_LOG_DIR	"/var/db/pkg"
69/* just in case we change the environment variable name */
70#define PKG_DBDIR	"PKG_DBDIR"
71/* macro to get name of directory where we put logging information */
72#define LOG_DIR		(getenv(PKG_DBDIR) ? getenv(PKG_DBDIR) : DEF_LOG_DIR)
73
74/* The names of our "special" files */
75#define CONTENTS_FNAME		"+CONTENTS"
76#define COMMENT_FNAME		"+COMMENT"
77#define DESC_FNAME		"+DESC"
78#define INSTALL_FNAME		"+INSTALL"
79#define POST_INSTALL_FNAME	"+POST-INSTALL"
80#define DEINSTALL_FNAME		"+DEINSTALL"
81#define POST_DEINSTALL_FNAME	"+POST-DEINSTALL"
82#define REQUIRE_FNAME		"+REQUIRE"
83#define REQUIRED_BY_FNAME	"+REQUIRED_BY"
84#define DISPLAY_FNAME		"+DISPLAY"
85#define MTREE_FNAME		"+MTREE_DIRS"
86
87#if defined(__FreeBSD_version) && __FreeBSD_version >= 800000
88#define INDEX_FNAME		"INDEX-8"
89#elif defined(__FreeBSD_version) && __FreeBSD_version >= 700000
90#define INDEX_FNAME		"INDEX-7"
91#elif defined(__FreeBSD_version) && __FreeBSD_version >= 600000
92#define INDEX_FNAME		"INDEX-6"
93#elif defined(__FreeBSD_version) && __FreeBSD_version >= 500036
94#define INDEX_FNAME		"INDEX-5"
95#else
96#define INDEX_FNAME		"INDEX"
97#endif
98
99#define CMD_CHAR		'@'	/* prefix for extended PLIST cmd */
100
101/* The name of the "prefix" environment variable given to scripts */
102#define PKG_PREFIX_VNAME	"PKG_PREFIX"
103
104/*
105 * Version of the package tools - increase only when some
106 * functionality used by bsd.port.mk is changed, added or removed
107 */
108#define PKG_INSTALL_VERSION	20090519
109
110#define PKG_WRAPCONF_FNAME	"/var/db/pkg_install.conf"
111#define main(argc, argv)	real_main(argc, argv)
112
113/* Version numbers to assist with changes in package file format */
114#define PLIST_FMT_VER_MAJOR	1
115#define PLIST_FMT_VER_MINOR	1
116
117enum _plist_t {
118    PLIST_FILE, PLIST_CWD, PLIST_CMD, PLIST_CHMOD,
119    PLIST_CHOWN, PLIST_CHGRP, PLIST_COMMENT, PLIST_IGNORE,
120    PLIST_NAME, PLIST_UNEXEC, PLIST_SRC, PLIST_DISPLAY,
121    PLIST_PKGDEP, PLIST_CONFLICTS, PLIST_MTREE, PLIST_DIR_RM,
122    PLIST_IGNORE_INST, PLIST_OPTION, PLIST_ORIGIN, PLIST_DEPORIGIN,
123    PLIST_NOINST
124};
125typedef enum _plist_t plist_t;
126
127enum _match_t {
128    MATCH_ALL, MATCH_EXACT, MATCH_GLOB, MATCH_NGLOB, MATCH_EREGEX, MATCH_REGEX
129};
130typedef enum _match_t match_t;
131
132/* Types */
133typedef unsigned int Boolean;
134
135struct _plist {
136    struct _plist *prev, *next;
137    char *name;
138    Boolean marked;
139    plist_t type;
140};
141typedef struct _plist *PackingList;
142
143struct _pack {
144    struct _plist *head, *tail;
145    const char *name;
146    const char *origin;
147    int fmtver_maj, fmtver_mnr;
148};
149typedef struct _pack Package;
150
151struct reqr_by_entry {
152    STAILQ_ENTRY(reqr_by_entry) link;
153    char pkgname[PATH_MAX];
154};
155STAILQ_HEAD(reqr_by_head, reqr_by_entry);
156
157/* Prototypes */
158/* Misc */
159int		vsystem(const char *, ...);
160char		*vpipe(const char *, ...);
161void		cleanup(int);
162const char	*make_playpen(char *, off_t);
163char		*where_playpen(void);
164int		leave_playpen(void);
165off_t		min_free(const char *);
166
167/* String */
168char 		*get_dash_string(char **);
169char		*copy_string(const char *);
170char		*copy_string_adds_newline(const char *);
171Boolean		suffix(const char *, const char *);
172void		nuke_suffix(char *);
173void		str_lowercase(char *);
174char		*strconcat(const char *, const char *);
175char		*get_string(char *, int, FILE *);
176
177/* File */
178Boolean		fexists(const char *);
179Boolean		isdir(const char *);
180Boolean		isemptydir(const char *fname);
181Boolean		isemptyfile(const char *fname);
182Boolean		isfile(const char *);
183Boolean		isempty(const char *);
184Boolean		issymlink(const char *);
185Boolean		isURL(const char *);
186const char	*fileGetURL(const char *, const char *, int);
187char		*fileFindByPath(const char *, const char *);
188char		*fileGetContents(const char *);
189void		write_file(const char *, const char *);
190void		copy_file(const char *, const char *, const char *);
191void		move_file(const char *, const char *, const char *);
192void		copy_hierarchy(const char *, const char *, Boolean);
193int		delete_hierarchy(const char *, Boolean, Boolean);
194int		unpack(const char *, const char *);
195void		format_cmd(char *, int, const char *, const char *, const char *);
196
197/* Msg */
198void		upchuck(const char *);
199void		barf(const char *, ...);
200void		whinge(const char *, ...);
201Boolean		y_or_n(Boolean, const char *, ...);
202
203/* Packing list */
204PackingList	new_plist_entry(void);
205PackingList	last_plist(Package *);
206PackingList	find_plist(Package *, plist_t);
207char		*find_plist_option(Package *, const char *name);
208void		plist_delete(Package *, Boolean, plist_t, const char *);
209void		free_plist(Package *);
210void		mark_plist(Package *);
211void		csum_plist_entry(char *, PackingList);
212void		add_plist(Package *, plist_t, const char *);
213void		add_plist_top(Package *, plist_t, const char *);
214void		delete_plist(Package *pkg, Boolean all, plist_t type, const char *name);
215void		write_plist(Package *, FILE *);
216void		read_plist(Package *, FILE *);
217int		plist_cmd(const char *, char **);
218int		delete_package(Boolean, Boolean, Package *);
219Boolean 	make_preserve_name(char *, int, const char *, const char *);
220
221/* For all */
222int		pkg_perform(char **);
223int		real_main(int, char **);
224
225/* Query installed packages */
226char		**matchinstalled(match_t, char **, int *);
227char		**matchbyorigin(const char *, int *);
228char		***matchallbyorigin(const char **, int *);
229int		isinstalledpkg(const char *name);
230int		pattern_match(match_t MatchType, char *pattern, const char *pkgname);
231
232/* Dependencies */
233int		sortdeps(char **);
234int		chkifdepends(const char *, const char *);
235int		requiredby(const char *, struct reqr_by_head **, Boolean, Boolean);
236
237/* Version */
238int		verscmp(Package *, int, int);
239int		version_cmp(const char *, const char *);
240
241/* Externs */
242extern Boolean	Quiet;
243extern Boolean	Fake;
244extern Boolean  Force;
245extern int	AutoAnswer;
246extern int	Verbose;
247
248#endif /* _INST_LIB_LIB_H_ */
249