lib.h revision 98766
1/* $FreeBSD: head/usr.sbin/pkg_install/lib/lib.h 98766 2002-06-24 16:03:24Z markm $ */
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	"rm"
56
57/* Usually "rm", but often "echo" during debugging! */
58#define RMDIR_CMD	"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#define CMD_CHAR		'@'	/* prefix for extended PLIST cmd */
81
82/* The name of the "prefix" environment variable given to scripts */
83#define PKG_PREFIX_VNAME	"PKG_PREFIX"
84
85/* Version numbers to assist with changes in package file format */
86#define PLIST_FMT_VER_MAJOR	1
87#define PLIST_FMT_VER_MINOR	1
88
89enum _plist_t {
90    PLIST_FILE, PLIST_CWD, PLIST_CMD, PLIST_CHMOD,
91    PLIST_CHOWN, PLIST_CHGRP, PLIST_COMMENT, PLIST_IGNORE,
92    PLIST_NAME, PLIST_UNEXEC, PLIST_SRC, PLIST_DISPLAY,
93    PLIST_PKGDEP, PLIST_MTREE, PLIST_DIR_RM, PLIST_IGNORE_INST,
94    PLIST_OPTION, PLIST_ORIGIN, PLIST_DEPORIGIN
95};
96typedef enum _plist_t plist_t;
97
98enum _match_t {
99    MATCH_ALL, MATCH_EXACT, MATCH_GLOB, MATCH_REGEX
100};
101typedef enum _match_t match_t;
102
103/* Types */
104typedef unsigned int Boolean;
105
106struct _plist {
107    struct _plist *prev, *next;
108    char *name;
109    Boolean marked;
110    plist_t type;
111};
112typedef struct _plist *PackingList;
113
114struct _pack {
115    struct _plist *head, *tail;
116    char *name;
117    char *origin;
118    int fmtver_maj, fmtver_mnr;
119};
120typedef struct _pack Package;
121
122struct reqr_by_entry {
123    STAILQ_ENTRY(reqr_by_entry) link;
124    char pkgname[PATH_MAX];
125};
126STAILQ_HEAD(reqr_by_head, reqr_by_entry);
127
128/* Prototypes */
129/* Misc */
130int		vsystem(const char *, ...);
131char		*vpipe(const char *, ...);
132void		cleanup(int);
133char		*make_playpen(char *, off_t);
134char		*where_playpen(void);
135void		leave_playpen(void);
136off_t		min_free(const char *);
137
138/* String */
139char 		*get_dash_string(char **);
140char		*copy_string(const char *);
141Boolean		suffix(const char *, const char *);
142void		nuke_suffix(char *);
143void		str_lowercase(char *);
144char		*strconcat(const char *, const char *);
145char		*get_string(char *, int, FILE *);
146
147/* File */
148Boolean		fexists(const char *);
149Boolean		isdir(const char *);
150Boolean		isemptydir(const char *fname);
151Boolean		isemptyfile(const char *fname);
152Boolean         isfile(const char *);
153Boolean		isempty(const char *);
154Boolean		issymlink(const char *);
155Boolean		isURL(const char *);
156char		*fileGetURL(const char *, const char *);
157char		*fileFindByPath(const char *, const char *);
158char		*fileGetContents(const char *);
159void		write_file(const char *, const char *);
160void		copy_file(const char *, const char *, const char *);
161void		move_file(const char *, const char *, const char *);
162void		copy_hierarchy(const char *, const char *, Boolean);
163int		delete_hierarchy(const char *, Boolean, Boolean);
164int		unpack(const char *, const char *);
165void		format_cmd(char *, const char *, const char *, const char *);
166
167/* Msg */
168void		upchuck(const char *);
169void		barf(const char *, ...);
170void		whinge(const char *, ...);
171Boolean		y_or_n(Boolean, const char *, ...);
172
173/* Packing list */
174PackingList	new_plist_entry(void);
175PackingList	last_plist(Package *);
176PackingList	find_plist(Package *, plist_t);
177char		*find_plist_option(Package *, const char *name);
178void		plist_delete(Package *, Boolean, plist_t, const char *);
179void		free_plist(Package *);
180void		mark_plist(Package *);
181void		csum_plist_entry(char *, PackingList);
182void		add_plist(Package *, plist_t, const char *);
183void		add_plist_top(Package *, plist_t, const char *);
184void		delete_plist(Package *pkg, Boolean all, plist_t type, const char *name);
185void		write_plist(Package *, FILE *);
186void		read_plist(Package *, FILE *);
187int		plist_cmd(const char *, char **);
188int		delete_package(Boolean, Boolean, Package *);
189Boolean 	make_preserve_name(char *, int, const char *, const char *);
190
191/* For all */
192int		pkg_perform(char **);
193
194/* Query installed packages */
195char		**matchinstalled(match_t, char **, int *);
196char		**matchbyorigin(const char *, int *);
197int		isinstalledpkg(const char *name);
198
199/* Dependencies */
200int		sortdeps(char **);
201int		chkifdepends(const char *, const char *);
202int		requiredby(const char *, struct reqr_by_head **, Boolean, Boolean);
203
204/* Version */
205int		verscmp(Package *, int, int);
206const char	*version_of(const char *, int *, int *);
207int		version_cmp(const char *, const char *);
208
209/* Externs */
210extern Boolean	Verbose;
211extern Boolean	Fake;
212extern Boolean  Force;
213extern int	AutoAnswer;
214
215#endif /* _INST_LIB_LIB_H_ */
216