150479Speter/* $FreeBSD$ */
2327Sjkh
3327Sjkh/*
4228990Suqs * FreeBSD install - a package for the installation and maintenance
5327Sjkh * of non-core utilities.
6327Sjkh *
7327Sjkh * Redistribution and use in source and binary forms, with or without
8327Sjkh * modification, are permitted provided that the following conditions
9327Sjkh * are met:
10327Sjkh * 1. Redistributions of source code must retain the above copyright
11327Sjkh *    notice, this list of conditions and the following disclaimer.
12327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
13327Sjkh *    notice, this list of conditions and the following disclaimer in the
14327Sjkh *    documentation and/or other materials provided with the distribution.
15327Sjkh *
16327Sjkh * Jordan K. Hubbard
17327Sjkh * 18 July 1993
18327Sjkh *
19327Sjkh * Include and define various things wanted by the library routines.
20327Sjkh *
21327Sjkh */
22327Sjkh
23327Sjkh#ifndef _INST_LIB_LIB_H_
24327Sjkh#define _INST_LIB_LIB_H_
25327Sjkh
26327Sjkh/* Includes */
2762154Sdes#include <sys/param.h>
2862154Sdes#include <sys/file.h>
2962154Sdes#include <sys/stat.h>
3083663Ssobomax#include <sys/queue.h>
31206043Sflz#include <sys/utsname.h>
3262154Sdes#include <ctype.h>
3362154Sdes#include <dirent.h>
3462154Sdes#include <stdarg.h>
35327Sjkh#include <stdio.h>
36327Sjkh#include <stdlib.h>
37327Sjkh#include <string.h>
38327Sjkh#include <unistd.h>
39327Sjkh
40327Sjkh/* Macros */
41327Sjkh#define SUCCESS	(0)
42327Sjkh#define	FAIL	(-1)
43327Sjkh
44327Sjkh#ifndef TRUE
45327Sjkh#define TRUE	(1)
46327Sjkh#endif
47327Sjkh
48327Sjkh#ifndef FALSE
49327Sjkh#define FALSE	(0)
50327Sjkh#endif
51327Sjkh
521338Sjkh#define YES		2
531338Sjkh#define NO		1
541338Sjkh
55206043Sflz/* Some more stat macros. */
56206043Sflz#define S_IRALL		0000444
57206043Sflz#define S_IWALL		0000222
58206043Sflz#define S_IXALL		0000111
59206043Sflz
60327Sjkh/* Usually "rm", but often "echo" during debugging! */
61131285Seik#define REMOVE_CMD	"/bin/rm"
62327Sjkh
63327Sjkh/* Usually "rm", but often "echo" during debugging! */
64131285Seik#define RMDIR_CMD	"/bin/rmdir"
65327Sjkh
66173514Skrion/* Where the ports lives by default */
67173514Skrion#define DEF_PORTS_DIR   "/usr/ports"
68173514Skrion/* just in case we change the environment variable name */
69173514Skrion#define PORTSDIR    "PORTSDIR"
70173514Skrion/* macro to get name of directory where the ports lives */
71173514Skrion#define PORTS_DIR       (getenv(PORTSDIR) ? getenv(PORTSDIR) : DEF_PORTS_DIR)
72173514Skrion
737937Sjkh/* Where we put logging information by default, else ${PKG_DBDIR} if set */
7481049Ssobomax#define DEF_LOG_DIR	"/var/db/pkg"
757937Sjkh/* just in case we change the environment variable name */
7681049Ssobomax#define PKG_DBDIR	"PKG_DBDIR"
7781049Ssobomax/* macro to get name of directory where we put logging information */
7881049Ssobomax#define LOG_DIR		(getenv(PKG_DBDIR) ? getenv(PKG_DBDIR) : DEF_LOG_DIR)
79327Sjkh
80327Sjkh/* The names of our "special" files */
8117338Sjkh#define CONTENTS_FNAME		"+CONTENTS"
8217338Sjkh#define COMMENT_FNAME		"+COMMENT"
8317338Sjkh#define DESC_FNAME		"+DESC"
8417338Sjkh#define INSTALL_FNAME		"+INSTALL"
8541866Sjkh#define POST_INSTALL_FNAME	"+POST-INSTALL"
8617338Sjkh#define DEINSTALL_FNAME		"+DEINSTALL"
8741866Sjkh#define POST_DEINSTALL_FNAME	"+POST-DEINSTALL"
8817338Sjkh#define REQUIRE_FNAME		"+REQUIRE"
894996Sjkh#define REQUIRED_BY_FNAME	"+REQUIRED_BY"
9017338Sjkh#define DISPLAY_FNAME		"+DISPLAY"
9117338Sjkh#define MTREE_FNAME		"+MTREE_DIRS"
92327Sjkh
9317338Sjkh#define CMD_CHAR		'@'	/* prefix for extended PLIST cmd */
94327Sjkh
951550Sasami/* The name of the "prefix" environment variable given to scripts */
961550Sasami#define PKG_PREFIX_VNAME	"PKG_PREFIX"
97479Sjkh
98103149Ssobomax/*
99196766Sflz * Version of the package tools - increase whenever you make a change
100196766Sflz * in the code that is not cosmetic only.
101103149Ssobomax */
102245828Sbapt#define PKG_INSTALL_VERSION	20130122
103103149Ssobomax
104103149Ssobomax#define PKG_WRAPCONF_FNAME	"/var/db/pkg_install.conf"
105103149Ssobomax#define main(argc, argv)	real_main(argc, argv)
106103149Ssobomax
10784750Ssobomax/* Version numbers to assist with changes in package file format */
10884750Ssobomax#define PLIST_FMT_VER_MAJOR	1
10984750Ssobomax#define PLIST_FMT_VER_MINOR	1
11084750Ssobomax
111327Sjkhenum _plist_t {
112327Sjkh    PLIST_FILE, PLIST_CWD, PLIST_CMD, PLIST_CHMOD,
11317338Sjkh    PLIST_CHOWN, PLIST_CHGRP, PLIST_COMMENT, PLIST_IGNORE,
11417338Sjkh    PLIST_NAME, PLIST_UNEXEC, PLIST_SRC, PLIST_DISPLAY,
115157809Skrion    PLIST_PKGDEP, PLIST_CONFLICTS, PLIST_MTREE, PLIST_DIR_RM,
116147381Skrion    PLIST_IGNORE_INST, PLIST_OPTION, PLIST_ORIGIN, PLIST_DEPORIGIN,
117147381Skrion    PLIST_NOINST
118327Sjkh};
119327Sjkhtypedef enum _plist_t plist_t;
120327Sjkh
12173134Ssobomaxenum _match_t {
122131275Seik    MATCH_ALL, MATCH_EXACT, MATCH_GLOB, MATCH_NGLOB, MATCH_EREGEX, MATCH_REGEX
12373134Ssobomax};
12473134Ssobomaxtypedef enum _match_t match_t;
12573134Ssobomax
126327Sjkh/* Types */
127327Sjkhtypedef unsigned int Boolean;
128327Sjkh
129327Sjkhstruct _plist {
130327Sjkh    struct _plist *prev, *next;
131327Sjkh    char *name;
132327Sjkh    Boolean marked;
133327Sjkh    plist_t type;
134327Sjkh};
135327Sjkhtypedef struct _plist *PackingList;
136327Sjkh
137327Sjkhstruct _pack {
138327Sjkh    struct _plist *head, *tail;
139131280Seik    const char *name;
140131280Seik    const char *origin;
14184750Ssobomax    int fmtver_maj, fmtver_mnr;
142327Sjkh};
143327Sjkhtypedef struct _pack Package;
144327Sjkh
14583663Ssobomaxstruct reqr_by_entry {
14683663Ssobomax    STAILQ_ENTRY(reqr_by_entry) link;
14783663Ssobomax    char pkgname[PATH_MAX];
14883663Ssobomax};
14983663SsobomaxSTAILQ_HEAD(reqr_by_head, reqr_by_entry);
150157809Skrion
151327Sjkh/* Prototypes */
152327Sjkh/* Misc */
153327Sjkhint		vsystem(const char *, ...);
15474699Ssobomaxchar		*vpipe(const char *, ...);
155327Sjkhvoid		cleanup(int);
156194497Sbrianconst char	*make_playpen(char *, off_t);
157383Sjkhchar		*where_playpen(void);
158194497Sbrianint		leave_playpen(void);
15984745Ssobomaxoff_t		min_free(const char *);
160241830Seadlervoid		warnpkgng(void);
161327Sjkh
162327Sjkh/* String */
163327Sjkhchar 		*get_dash_string(char **);
16484745Ssobomaxchar		*copy_string(const char *);
165115325Sliouxchar		*copy_string_adds_newline(const char *);
16684745SsobomaxBoolean		suffix(const char *, const char *);
167327Sjkhvoid		nuke_suffix(char *);
168327Sjkhvoid		str_lowercase(char *);
16984745Ssobomaxchar		*strconcat(const char *, const char *);
17074699Ssobomaxchar		*get_string(char *, int, FILE *);
171327Sjkh
172327Sjkh/* File */
17384745SsobomaxBoolean		fexists(const char *);
17484745SsobomaxBoolean		isdir(const char *);
17584745SsobomaxBoolean		isemptydir(const char *fname);
17684745SsobomaxBoolean		isemptyfile(const char *fname);
177167972SnjlBoolean		isfile(const char *);
17884745SsobomaxBoolean		isempty(const char *);
17984745SsobomaxBoolean		issymlink(const char *);
18084745SsobomaxBoolean		isURL(const char *);
181194497Sbrianconst char	*fileGetURL(const char *, const char *, int);
18284745Ssobomaxchar		*fileFindByPath(const char *, const char *);
18384745Ssobomaxchar		*fileGetContents(const char *);
18484745Ssobomaxvoid		write_file(const char *, const char *);
18584745Ssobomaxvoid		copy_file(const char *, const char *, const char *);
18684745Ssobomaxvoid		move_file(const char *, const char *, const char *);
18784745Ssobomaxvoid		copy_hierarchy(const char *, const char *, Boolean);
18884745Ssobomaxint		delete_hierarchy(const char *, Boolean, Boolean);
18984745Ssobomaxint		unpack(const char *, const char *);
190108778Sjkhvoid		format_cmd(char *, int, const char *, const char *, const char *);
191327Sjkh
192327Sjkh/* Msg */
193327Sjkhvoid		upchuck(const char *);
194327Sjkhvoid		barf(const char *, ...);
195327Sjkhvoid		whinge(const char *, ...);
196327SjkhBoolean		y_or_n(Boolean, const char *, ...);
197327Sjkh
198327Sjkh/* Packing list */
199327SjkhPackingList	new_plist_entry(void);
200327SjkhPackingList	last_plist(Package *);
2011547SjkhPackingList	find_plist(Package *, plist_t);
20284745Ssobomaxchar		*find_plist_option(Package *, const char *name);
20384745Ssobomaxvoid		plist_delete(Package *, Boolean, plist_t, const char *);
204327Sjkhvoid		free_plist(Package *);
205327Sjkhvoid		mark_plist(Package *);
206327Sjkhvoid		csum_plist_entry(char *, PackingList);
20784745Ssobomaxvoid		add_plist(Package *, plist_t, const char *);
20884745Ssobomaxvoid		add_plist_top(Package *, plist_t, const char *);
20984745Ssobomaxvoid		delete_plist(Package *pkg, Boolean all, plist_t type, const char *name);
210327Sjkhvoid		write_plist(Package *, FILE *);
211327Sjkhvoid		read_plist(Package *, FILE *);
21284745Ssobomaxint		plist_cmd(const char *, char **);
2134996Sjkhint		delete_package(Boolean, Boolean, Package *);
21484745SsobomaxBoolean 	make_preserve_name(char *, int, const char *, const char *);
215327Sjkh
216327Sjkh/* For all */
217327Sjkhint		pkg_perform(char **);
218103149Ssobomaxint		real_main(int, char **);
219327Sjkh
22073134Ssobomax/* Query installed packages */
22173134Ssobomaxchar		**matchinstalled(match_t, char **, int *);
22296613Ssobomaxchar		**matchbyorigin(const char *, int *);
223178753Spavchar		***matchallbyorigin(const char **, int *);
22496613Ssobomaxint		isinstalledpkg(const char *name);
225131275Seikint		pattern_match(match_t MatchType, char *pattern, const char *pkgname);
22673134Ssobomax
22774295Ssobomax/* Dependencies */
22874295Ssobomaxint		sortdeps(char **);
22984745Ssobomaxint		chkifdepends(const char *, const char *);
23083663Ssobomaxint		requiredby(const char *, struct reqr_by_head **, Boolean, Boolean);
23174295Ssobomax
23284750Ssobomax/* Version */
23384750Ssobomaxint		verscmp(Package *, int, int);
23498766Smarkmint		version_cmp(const char *, const char *);
23584750Ssobomax
236327Sjkh/* Externs */
237136643Sobrienextern Boolean	Quiet;
238327Sjkhextern Boolean	Fake;
23917338Sjkhextern Boolean  Force;
2401338Sjkhextern int	AutoAnswer;
241159554Sobrienextern int	Verbose;
242327Sjkh
243327Sjkh#endif /* _INST_LIB_LIB_H_ */
244