lib.h revision 136643
1/* $FreeBSD: head/usr.sbin/pkg_install/lib/lib.h 136643 2004-10-18 05:34:54Z obrien $ */
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 we put logging information by default, else ${PKG_DBDIR} if set */
61#define DEF_LOG_DIR	"/var/db/pkg"
62/* just in case we change the environment variable name */
63#define PKG_DBDIR	"PKG_DBDIR"
64/* macro to get name of directory where we put logging information */
65#define LOG_DIR		(getenv(PKG_DBDIR) ? getenv(PKG_DBDIR) : DEF_LOG_DIR)
66
67/* The names of our "special" files */
68#define CONTENTS_FNAME		"+CONTENTS"
69#define COMMENT_FNAME		"+COMMENT"
70#define DESC_FNAME		"+DESC"
71#define INSTALL_FNAME		"+INSTALL"
72#define POST_INSTALL_FNAME	"+POST-INSTALL"
73#define DEINSTALL_FNAME		"+DEINSTALL"
74#define POST_DEINSTALL_FNAME	"+POST-DEINSTALL"
75#define REQUIRE_FNAME		"+REQUIRE"
76#define REQUIRED_BY_FNAME	"+REQUIRED_BY"
77#define DISPLAY_FNAME		"+DISPLAY"
78#define MTREE_FNAME		"+MTREE_DIRS"
79
80#if defined(__FreeBSD_version) && __FreeBSD_version >= 500036
81#define INDEX_FNAME		"INDEX-5"
82#else
83#define INDEX_FNAME		"INDEX"
84#endif
85
86#define CMD_CHAR		'@'	/* prefix for extended PLIST cmd */
87
88/* The name of the "prefix" environment variable given to scripts */
89#define PKG_PREFIX_VNAME	"PKG_PREFIX"
90
91/*
92 * Version of the package tools - increase only when some
93 * functionality used by bsd.port.mk is changed, added or removed
94 */
95#define PKG_INSTALL_VERSION	20040629
96
97#define PKG_WRAPCONF_FNAME	"/var/db/pkg_install.conf"
98#define main(argc, argv)	real_main(argc, argv)
99
100/* Version numbers to assist with changes in package file format */
101#define PLIST_FMT_VER_MAJOR	1
102#define PLIST_FMT_VER_MINOR	1
103
104enum _plist_t {
105    PLIST_FILE, PLIST_CWD, PLIST_CMD, PLIST_CHMOD,
106    PLIST_CHOWN, PLIST_CHGRP, PLIST_COMMENT, PLIST_IGNORE,
107    PLIST_NAME, PLIST_UNEXEC, PLIST_SRC, PLIST_DISPLAY,
108    PLIST_PKGDEP, PLIST_CONFLICTS, PLIST_MTREE, PLIST_DIR_RM,
109    PLIST_IGNORE_INST, PLIST_OPTION, PLIST_ORIGIN, PLIST_DEPORIGIN
110};
111typedef enum _plist_t plist_t;
112
113enum _match_t {
114    MATCH_ALL, MATCH_EXACT, MATCH_GLOB, MATCH_NGLOB, MATCH_EREGEX, MATCH_REGEX
115};
116typedef enum _match_t match_t;
117
118/* Types */
119typedef unsigned int Boolean;
120
121struct _plist {
122    struct _plist *prev, *next;
123    char *name;
124    Boolean marked;
125    plist_t type;
126};
127typedef struct _plist *PackingList;
128
129struct _pack {
130    struct _plist *head, *tail;
131    const char *name;
132    const char *origin;
133    int fmtver_maj, fmtver_mnr;
134};
135typedef struct _pack Package;
136
137struct reqr_by_entry {
138    STAILQ_ENTRY(reqr_by_entry) link;
139    char pkgname[PATH_MAX];
140};
141STAILQ_HEAD(reqr_by_head, reqr_by_entry);
142
143/* Prototypes */
144/* Misc */
145int		vsystem(const char *, ...);
146char		*vpipe(const char *, ...);
147void		cleanup(int);
148char		*make_playpen(char *, off_t);
149char		*where_playpen(void);
150void		leave_playpen(void);
151off_t		min_free(const char *);
152
153/* String */
154char 		*get_dash_string(char **);
155char		*copy_string(const char *);
156char		*copy_string_adds_newline(const char *);
157Boolean		suffix(const char *, const char *);
158void		nuke_suffix(char *);
159void		str_lowercase(char *);
160char		*strconcat(const char *, const char *);
161char		*get_string(char *, int, FILE *);
162
163/* File */
164Boolean		fexists(const char *);
165Boolean		isdir(const char *);
166Boolean		isemptydir(const char *fname);
167Boolean		isemptyfile(const char *fname);
168Boolean         isfile(const char *);
169Boolean		isempty(const char *);
170Boolean		issymlink(const char *);
171Boolean		isURL(const char *);
172char		*fileGetURL(const char *, const char *);
173char		*fileFindByPath(const char *, const char *);
174char		*fileGetContents(const char *);
175void		write_file(const char *, const char *);
176void		copy_file(const char *, const char *, const char *);
177void		move_file(const char *, const char *, const char *);
178void		copy_hierarchy(const char *, const char *, Boolean);
179int		delete_hierarchy(const char *, Boolean, Boolean);
180int		unpack(const char *, const char *);
181void		format_cmd(char *, int, const char *, const char *, const char *);
182
183/* Msg */
184void		upchuck(const char *);
185void		barf(const char *, ...);
186void		whinge(const char *, ...);
187Boolean		y_or_n(Boolean, const char *, ...);
188
189/* Packing list */
190PackingList	new_plist_entry(void);
191PackingList	last_plist(Package *);
192PackingList	find_plist(Package *, plist_t);
193char		*find_plist_option(Package *, const char *name);
194void		plist_delete(Package *, Boolean, plist_t, const char *);
195void		free_plist(Package *);
196void		mark_plist(Package *);
197void		csum_plist_entry(char *, PackingList);
198void		add_plist(Package *, plist_t, const char *);
199void		add_plist_top(Package *, plist_t, const char *);
200void		delete_plist(Package *pkg, Boolean all, plist_t type, const char *name);
201void		write_plist(Package *, FILE *);
202void		read_plist(Package *, FILE *);
203int		plist_cmd(const char *, char **);
204int		delete_package(Boolean, Boolean, Package *);
205Boolean 	make_preserve_name(char *, int, const char *, const char *);
206
207/* For all */
208int		pkg_perform(char **);
209int		real_main(int, char **);
210
211/* Query installed packages */
212char		**matchinstalled(match_t, char **, int *);
213char		**matchbyorigin(const char *, int *);
214int		isinstalledpkg(const char *name);
215int		pattern_match(match_t MatchType, char *pattern, const char *pkgname);
216
217/* Dependencies */
218int		sortdeps(char **);
219int		chkifdepends(const char *, const char *);
220int		requiredby(const char *, struct reqr_by_head **, Boolean, Boolean);
221
222/* Version */
223int		verscmp(Package *, int, int);
224int		version_cmp(const char *, const char *);
225
226/* Externs */
227extern Boolean	Quiet;
228extern Boolean	Verbose;
229extern Boolean	Fake;
230extern Boolean  Force;
231extern int	AutoAnswer;
232
233#endif /* _INST_LIB_LIB_H_ */
234