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