xref: /csrg-svn/lib/libc/gen/getcap.3 (revision 67135)
161111Sbostic.\" Copyright (c) 1992, 1993
261111Sbostic.\"	The Regents of the University of California.  All rights reserved.
355869Sbostic.\"
455869Sbostic.\" This code is derived from software contributed to Berkeley by
555869Sbostic.\" Casey Leedom of Lawrence Livermore National Laboratory.
655869Sbostic.\"
755869Sbostic.\" %sccs.include.redist.roff%
855869Sbostic.\"
9*67135Smckusick.\"	@(#)getcap.3	8.4 (Berkeley) 05/13/94
1055869Sbostic.\"
1155869Sbostic.Dd ""
1255869Sbostic.Dt GETCAP 3
1355869Sbostic.Os
1455869Sbostic.Sh NAME
1555869Sbostic.Nm cgetent ,
1655869Sbostic.Nm cgetset ,
1755869Sbostic.Nm cgetmatch ,
1855869Sbostic.Nm cgetcap ,
1955869Sbostic.Nm cgetnum ,
2055869Sbostic.Nm cgetstr ,
2155869Sbostic.Nm cgetustr ,
2255869Sbostic.Nm cgetfirst ,
2355869Sbostic.Nm cgetnext ,
2455869Sbostic.Nm cgetclose
2555869Sbostic.Nd capability database access routines
2655869Sbostic.Sh SYNOPSIS
2755869Sbostic.Fd #include <stdlib.h>
2855869Sbostic.Ft int
2955869Sbostic.Fn cgetent "char **buf" "char **db_array" "char *name"
3055869Sbostic.Ft int
3155869Sbostic.Fn cgetset "char *ent"
3255869Sbostic.Ft int
3355869Sbostic.Fn cgetmatch "char *buf" "char *name"
3455869Sbostic.Ft char *
3555869Sbostic.Fn cgetcap "char *buf" "char *cap" "char type"
3655869Sbostic.Ft int
3755869Sbostic.Fn cgetnum "char *buf" "char *cap" "long *num"
3855869Sbostic.Ft int
3955869Sbostic.Fn cgetstr "char *buf" "char *cap" "char **str"
4055869Sbostic.Ft int
4155869Sbostic.Fn cgetustr "char *buf" "char *cap" "char **str"
4255869Sbostic.Ft int
4355869Sbostic.Fn cgetfirst "char **buf" "char **db_array"
4455869Sbostic.Ft int
4555869Sbostic.Fn cgetnext "char **buf" "char **db_array"
4655869Sbostic.Ft int
4755869Sbostic.Fn cgetclose "void"
4855869Sbostic.Sh DESCRIPTION
4955869Sbostic.Fn Cgetent
50*67135Smckusickextracts the capability
5155869Sbostic.Fa name
5255870Scaelfrom the database specified by the
5355870Scael.Dv NULL
5455870Scaelterminated file array
5555869Sbostic.Fa db_array
5655869Sbosticand returns a pointer to a
5755870Scael.Xr malloc Ns \&'d
5855869Sbosticcopy of it in
5955886Selan.Fa buf .
6055886Selan.Nm Cgetent
6155886Selanwill first look for files ending in
6255886Selan.Nm .db
6355886Selan(see
6455886Selan.Xr cap_mkdb 1)
6555886Selanbefore accessing the ASCII file.
6655869Sbostic.Fa Buf
6755869Sbosticmust be retained through all subsequent calls to
6855869Sbostic.Fn cgetmatch ,
6955869Sbostic.Fn cgetcap ,
7055869Sbostic.Fn cgetnum ,
7155869Sbostic.Fn cgetstr ,
7255869Sbosticand
7355869Sbostic.Fn cgetustr ,
7455869Sbosticbut may then be
7555870Scael.Xr free Ns \&'d.
7655886SelanOn success 0 is returned, 1 if the returned
7755886Selanrecord contains an unresolved
7855886Selan.Nm tc
7955886Selanexpansion,
8055886Selan\-1 if the requested record couldn't be found,
8155869Sbostic\-2 if a system error was encountered (couldn't open/read a file, etc.) also
8255869Sbosticsetting
8355869Sbostic.Va errno ,
8455869Sbosticand \-3 if a potential reference loop is detected (see
8555870Scael.Ic tc=
8655869Sbosticcomments below).
8755869Sbostic.Pp
8855869Sbostic.Nm Cgetset
8955869Sbosticenables the addition of a character buffer containing a single capability
9055869Sbosticrecord entry
9155869Sbosticto the capability database.
9255869SbosticConceptually, the entry is added as the first ``file'' in the database, and
9355869Sbosticis therefore searched first on the call to
9455869Sbostic.Nm cgetent .
9555869SbosticThe entry is passed in
9655869Sbostic.Fa ent .
9755869SbosticIf
9855869Sbostic.Fa ent
9955870Scaelis
10055870Scael.Dv NULL ,
10155877Selanthe current entry is removed from the database.
10255877Selan.Nm Cgetset
10355877Selanmust precede the database traversal.  It must be called before the
10455877Selan.Nm cgetent
10555877Selancall. If a sequential access is being performed (see below), it must be called
10655877Selanbefore the first sequential access call (
10755877Selan.Nm cgetfirst
10855877Selanor
10955877Selan.Nm cgetnext
11055877Selan), or be directly preceded by a
11155877Selan.Nm cgetclose
11255877Selancall.
11355869SbosticOn success 0 is returned and \-1 on failure.
11455869Sbostic.Pp
11555869Sbostic.Nm Cgetmatch
11655869Sbosticwill return 0 if
11755869Sbostic.Fa name
11855869Sbosticis one of the names of the capability record
11955869Sbostic.Fa buf ,
12055869Sbostic\-1 if
12155869Sbosticnot.
12255869Sbostic.Pp
12355869Sbostic.Nm Cgetcap
12455869Sbosticsearches the capability record
12555869Sbostic.Fa buf
12655869Sbosticfor the capability
12755869Sbostic.Fa cap
12855869Sbosticwith type
12955869Sbostic.Fa type .
13055869SbosticA
13155869Sbostic.Fa type
13255869Sbosticis specified using any single character.  If a colon (`:') is used, an
13355869Sbosticuntyped capability will be searched for (see below for explanation of
13455869Sbostictypes).  A pointer to the value of
13555869Sbostic.Fa cap
13655869Sbosticin
13755869Sbostic.Fa buf
13855870Scaelis returned on success,
13955870Scael.Dv NULL
14055870Scaelif the requested capability couldn't be
14155870Scaelfound.  The end of the capability value is signaled by a `:' or
14255870Scael.Tn ASCII
14355870Scael.Dv NUL
14455869Sbostic(see below for capability database syntax).
14555869Sbostic.Pp
14655869Sbostic.Nm Cgetnum
14755869Sbosticretrieves the value of the numeric capability
14855869Sbostic.Fa cap
14955869Sbosticfrom the capability record pointed to by
15055869Sbostic.Fa buf .
15155869SbosticThe numeric value is returned in the
15255869Sbostic.Ft long
15355869Sbosticpointed to by
15455869Sbostic.Fa num .
15555869Sbostic0 is returned on success, \-1 if the requested numeric capability couldn't
15655869Sbosticbe found.
15755869Sbostic.Pp
15855869Sbostic.Nm Cgetstr
15955869Sbosticretrieves the value of the string capability
16055869Sbostic.Fa cap
16155869Sbosticfrom the capability record pointed to by
16255869Sbostic.Fa buf .
16355870ScaelA pointer to a decoded,
16455870Scael.Dv NUL
16555870Scaelterminated,
16655870Scael.Xr malloc Ns \&'d
16755869Sbosticcopy of the string is returned in the
16855869Sbostic.Ft char *
16955869Sbosticpointed to by
17055869Sbostic.Fa str .
17155869SbosticThe number of characters in the decoded string not including the trailing
17255870Scael.Dv NUL
17355870Scaelis returned on success, \-1 if the requested string capability couldn't
17455869Sbosticbe found, \-2 if a system error was encountered (storage allocation
17555869Sbosticfailure).
17655869Sbostic.Pp
17755869Sbostic.Nm Cgetustr
17855869Sbosticis identical to
17955869Sbostic.Nm cgetstr
18055869Sbosticexcept that it does not expand special characters, but rather returns each
18155869Sbosticcharacter of the capability string literally.
18255869Sbostic.Pp
18355869Sbostic.Nm Cgetfirst ,
18455869Sbostic.Nm cgetnext ,
18555869Sbosticcomprise a function group that provides for sequential
18655870Scaelaccess of the
18755870Scael.Dv NULL
18855870Scaelpointer terminated array of file names,
18955869Sbostic.Fa db_array .
19055869Sbostic.Nm Cgetfirst
19155869Sbosticreturns the first record in the database and resets the access
19255869Sbosticto the first record.
19355869Sbostic.Nm Cgetnext
19455869Sbosticreturns the next record in the database with respect to the
19555869Sbosticrecord returned by the previous
19655869Sbostic.Nm cgetfirst
19755869Sbosticor
19855869Sbostic.Nm cgetnext
19955869Sbosticcall.  If there is no such previous call, the first record in the database is
20055869Sbosticreturned.
20155869SbosticEach record is returned in a
20255870Scael.Xr malloc Ns \&'d
20355869Sbosticcopy pointed to by
20455869Sbostic.Fa buf .
20555870Scael.Ic Tc
20655869Sbosticexpansion is done (see
20755870Scael.Ic tc=
20855869Sbosticcomments below).
20955869SbosticUpon completion of the database 0 is returned,  1 is returned upon successful
21055869Sbosticreturn of record with possibly more remaining (we haven't reached the end of
21155886Selanthe database yet), 2 is returned if the record contains an unresolved
21255886Selan.Nm tc
21366943Sbosticexpansion, \-1 is returned if an system error occurred, and \-2
21455869Sbosticis returned if a potential reference loop is detected (see
21555870Scael.Ic tc=
21655869Sbosticcomments below).
21755869SbosticUpon completion of database (0 return) the database is closed.
21855877Selan.Pp
21955877Selan.Nm Cgetclose
22055877Selancloses the sequential access and frees any memory and file descriptors
22155877Selanbeing used.  Note that it does not erase the buffer pushed by a call to
22255877Selan.Nm cgetset .
22355869Sbostic.Sh CAPABILITY DATABASE SYNTAX
22455870ScaelCapability databases are normally
22555870Scael.Tn ASCII
22655870Scaeland may be edited with standard
22755869Sbostictext editors.  Blank lines and lines beginning with a `#' are comments
22855869Sbosticand are ignored.  Lines ending with a `\|\e' indicate that the next line
22955869Sbosticis a continuation of the current line; the `\|\e' and following newline
23055869Sbosticare ignored.  Long lines are usually continued onto several physical
23155869Sbosticlines by ending each line except the last with a `\|\e'.
23255869Sbostic.Pp
23355869SbosticCapability databases consist of a series of records, one per logical
23455869Sbosticline.  Each record contains a variable number of `:'-separated fields
23555869Sbostic(capabilities).  Empty fields consisting entirely of white space
23655869Sbosticcharacters (spaces and tabs) are ignored.
23755869Sbostic.Pp
23855869SbosticThe first capability of each record specifies its names, separated by `|'
23955869Sbosticcharacters.  These names are used to reference records in the database.
24055869SbosticBy convention, the last name is usually a comment and is not intended as
24155869Sbostica lookup tag.  For example, the
24255870Scael.Em vt100
24355869Sbosticrecord from the
24455869Sbostic.Nm termcap
24555869Sbosticdatabase begins:
24655869Sbostic.Pp
24755869Sbostic.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:"
24855869Sbostic.Pp
24955869Sbosticgiving four names that can be used to access the record.
25055869Sbostic.Pp
25155869SbosticThe remaining non-empty capabilities describe a set of (name, value)
25255869Sbosticbindings, consisting of a names optionally followed by a typed values:
25355870Scael.Bl -column "nameTvalue"
25455870Scael.It name Ta "typeless [boolean] capability"
25555870Scael.Em name No "is present [true]"
25655870Scael.It name Ns Em \&T Ns value Ta capability
25755870Scael.Pq Em name , \&T
25855870Scaelhas value
25955870Scael.Em value
26055870Scael.It name@ Ta "no capability" Em name No exists
26155870Scael.It name Ns Em T Ns \&@ Ta capability
26255870Scael.Pq Em name , T
26355870Scaeldoes not exist
26455870Scael.El
26555869Sbostic.Pp
26655869SbosticNames consist of one or more characters.  Names may contain any character
26755869Sbosticexcept `:', but it's usually best to restrict them to the printable
26855869Sbosticcharacters and avoid use of graphics like `#', `=', `%', `@', etc.  Types
26955869Sbosticare single characters used to separate capability names from their
27055869Sbosticassociated typed values.  Types may be any character except a `:'.
27155869SbosticTypically, graphics like `#', `=', `%', etc. are used.  Values may be any
27255869Sbosticnumber of characters and may contain any character except `:'.
27355869Sbostic.Sh CAPABILITY DATABASE SEMANTICS
27455869SbosticCapability records describe a set of (name, value) bindings.  Names may
27555869Sbostichave multiple values bound to them.  Different values for a name are
27655869Sbosticdistinguished by their
27755869Sbostic.Fa types .
27855869Sbostic.Nm Cgetcap
27955869Sbosticwill return a pointer to a value of a name given the capability name and
28055869Sbosticthe type of the value.
28155869Sbostic.Pp
28255869SbosticThe types `#' and `=' are conventionally used to denote numeric and
28355869Sbosticstring typed values, but no restriction on those types is enforced.  The
28455869Sbosticfunctions
28555869Sbostic.Nm cgetnum
28655869Sbosticand
28755869Sbostic.Nm cgetstr
28855869Sbosticcan be used to implement the traditional syntax and semantics of `#'
28955869Sbosticand `='.
29055869SbosticTypeless capabilities are typically used to denote boolean objects with
29155869Sbosticpresence or absence indicating truth and false values respectively.
29255869SbosticThis interpretation is conveniently represented by:
29355869Sbostic.Pp
29455869Sbostic.Dl "(getcap(buf, name, ':') != NULL)"
29555869Sbostic.Pp
29655869SbosticA special capability,
29755870Scael.Ic tc= name ,
29855869Sbosticis used to indicate that the record specified by
29955869Sbostic.Fa name
30055869Sbosticshould be substituted for the
30155870Scael.Ic tc
30255869Sbosticcapability.
30355870Scael.Ic Tc
30455869Sbosticcapabilities may interpolate records which also contain
30555870Scael.Ic tc
30655869Sbosticcapabilities and more than one
30755870Scael.Ic tc
30855869Sbosticcapability may be used in a record.  A
30955870Scael.Ic tc
31055869Sbosticexpansion scope (i.e., where the argument is searched for) contains the
31155869Sbosticfile in which the
31255870Scael.Ic tc
31355869Sbosticis declared and all subsequent files in the file array.
31455869Sbostic.Pp
31555869SbosticWhen a database is searched for a capability record, the first matching
31665099Smckusickrecord in the search is returned.  When a record is scanned for a
31755869Sbosticcapability, the first matching capability is returned; the capability
31855870Scael.Ic :nameT@:
31955869Sbosticwill hide any following definition of a value of type
32055870Scael.Em T
32155869Sbosticfor
32255869Sbostic.Fa name ;
32355869Sbosticand the capability
32455870Scael.Ic :name@:
32555869Sbosticwill prevent any following values of
32655869Sbostic.Fa name
32755869Sbosticfrom being seen.
32855869Sbostic.Pp
32955869SbosticThese features combined with
33055870Scael.Ic tc
33155869Sbosticcapabilities can be used to generate variations of other databases and
33255869Sbosticrecords by either adding new capabilities, overriding definitions with new
33355869Sbosticdefinitions, or hiding following definitions via `@' capabilities.
33455869Sbostic.Sh EXAMPLES
33555869Sbostic.Bd -unfilled -offset indent
33655869Sbosticexample\||\|an example of binding multiple values to names:\e
33755869Sbostic	:foo%bar:foo^blah:foo@:\e
33855869Sbostic	:abc%xyz:abc^frap:abc$@:\e
33955869Sbostic	:tc=more:
34055869Sbostic.Ed
34155869Sbostic.Pp
34255869SbosticThe capability foo has two values bound to it (bar of type `%' and blah of
34355869Sbostictype `^') and any other value bindings are hidden.  The capability abc
34455869Sbosticalso has two values bound but only a value of type `$' is prevented from
34555869Sbosticbeing defined in the capability record more.
34655869Sbostic.Pp
34755869Sbostic.Bd -unfilled -offset indent
34855869Sbosticfile1:
34955869Sbostic 	new\||\|new_record\||\|a modification of "old":\e
35055869Sbostic		:fript=bar:who-cares@:tc=old:blah:tc=extensions:
35155869Sbosticfile2:
35255869Sbostic	old\||\|old_record\||\|an old database record:\e
35355869Sbostic		:fript=foo:who-cares:glork#200:
35455869Sbostic.Ed
35555869Sbostic.Pp
35655869SbosticThe records are extracted by calling
35755869Sbostic.Nm cgetent
35855869Sbosticwith file1 preceding file2.
35955869SbosticIn the capability record new in file1, fript=bar overrides the definition
36055869Sbosticof fript=foo interpolated from the capability record old in file2,
36155869Sbosticwho-cares@ prevents the definition of any who-cares definitions in old
36255869Sbosticfrom being seen, glork#200 is inherited from old, and blah and anything
36355869Sbosticdefined by the record extensions is added to those definitions in old.
36455869SbosticNote that the position of the fript=bar and who-cares@ definitions before
36555869Sbostictc=old is important here.  If they were after, the definitions in old
36655869Sbosticwould take precedence.
36755869Sbostic.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS
36855869SbosticTwo types are predefined by
36955869Sbostic.Nm cgetnum
37055869Sbosticand
37155869Sbostic.Nm cgetstr :
37255870Scael.Bl -column "nameXnumber"
37355870Scael.Sm off
37455870Scael.It Em name No \&# Em number Ta numeric
37555870Scaelcapability
37655870Scael.Em name
37755870Scaelhas value
37855870Scael.Em number
37955870Scael.It Em name No = Em string Ta "string capability"
38055870Scael.Em name
38155870Scaelhas value
38255870Scael.Em string
38355870Scael.It Em name No \&#@ Ta "the numeric capability"
38455870Scael.Em name
38555870Scaeldoes not exist
38655870Scael.It Em name No \&=@ Ta "the string capability"
38755870Scael.Em name
38855870Scaeldoes not exist
38955870Scael.El
39055869Sbostic.Pp
39155869SbosticNumeric capability values may be given in one of three numeric bases.
39255869SbosticIf the number starts with either
39355870Scael.Ql 0x
39455869Sbosticor
39555870Scael.Ql 0X
39655869Sbosticit is interpreted as a hexadecimal number (both upper and lower case a-f
39755869Sbosticmay be used to denote the extended hexadecimal digits).
39855869SbosticOtherwise, if the number starts with a
39955870Scael.Ql 0
40055869Sbosticit is interpreted as an octal number.
40155869SbosticOtherwise the number is interpreted as a decimal number.
40255869Sbostic.Pp
40355870ScaelString capability values may contain any character.  Non-printable
40455870Scael.Dv ASCII
40555869Sbosticcodes, new lines, and colons may be conveniently represented by the use
40655869Sbosticof escape sequences:
40755870Scael.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)"
40855869Sbostic^X	('\fIX\fP' & 037)	control-\fIX\fP
40955869Sbostic\e\|b, \e\|B	(ASCII 010)	backspace
41055869Sbostic\e\|t, \e\|T	(ASCII 011)	tab
41155869Sbostic\e\|n, \e\|N	(ASCII 012)	line feed (newline)
41255869Sbostic\e\|f, \e\|F	(ASCII 014)	form feed
41355869Sbostic\e\|r, \e\|R	(ASCII 015)	carriage return
41455869Sbostic\e\|e, \e\|E	(ASCII 027)	escape
41555869Sbostic\e\|c, \e\|C	(:)	colon
41655869Sbostic\e\|\e	(\e\|)	back slash
41755869Sbostic\e\|^	(^)	caret
41855869Sbostic\e\|\fInnn\fP	(ASCII octal \fInnn\fP)
41955870Scael.El
42055869Sbostic.Pp
42155869SbosticA `\|\e' may be followed by up to three octal digits directly specifies
42255870Scaelthe numeric code for a character.  The use of
42355870Scael.Tn ASCII
42455870Scael.Dv NUL Ns s ,
42555870Scaelwhile easily
42655869Sbosticencoded, causes all sorts of problems and must be used with care since
42755870Scael.Dv NUL Ns s
42855870Scaelare typically used to denote the end of strings; many applications
42955870Scaeluse `\e\|200' to represent a
43055870Scael.Dv NUL .
43155869Sbostic.Sh DIAGNOSTICS
43255869Sbostic.Nm Cgetent ,
43355869Sbostic.Nm cgetset ,
43455869Sbostic.Nm cgetmatch ,
43555869Sbostic.Nm cgetnum ,
43655869Sbostic.Nm cgetstr ,
43755869Sbostic.Nm cgetustr ,
43855869Sbostic.Nm cgetfirst ,
43955869Sbosticand
44055869Sbostic.Nm cgetnext
44165099Smckusickreturn a value greater than or equal to 0 on success and a value less
44255869Sbosticthan 0 on failure.
44355869Sbostic.Nm Cgetcap
44455870Scaelreturns a character pointer on success and a
44555870Scael.Dv NULL
44655870Scaelon failure.
44755869Sbostic.Pp
44855869Sbostic.Nm Cgetent ,
44955869Sbosticand
45055869Sbostic.Nm cgetseq
45155869Sbosticmay fail and set
45255869Sbostic.Va errno
45355869Sbosticfor any of the errors specified for the library functions:
45455869Sbostic.Xr fopen 2 ,
45555869Sbostic.Xr fclose 2 ,
45655869Sbostic.Xr open 2 ,
45755869Sbosticand
45855869Sbostic.Xr close 2 .
45955869Sbostic.Pp
46055869Sbostic.Nm Cgetent ,
46155869Sbostic.Nm cgetset ,
46255869Sbostic.Nm cgetstr ,
46355869Sbosticand
46455869Sbostic.Nm cgetustr
46555869Sbosticmay fail and set
46655869Sbostic.Va errno
46755869Sbosticas follows:
46855869Sbostic.Bl -tag -width Er
46955869Sbostic.It Bq Er ENOMEM
47055869SbosticNo memory to allocate.
47155869Sbostic.El
47255869Sbostic.Sh SEE ALSO
47355886Selan.Xr cap_mkdb 1 ,
47455869Sbostic.Xr malloc 3
47555869Sbostic.Sh BUGS
47655869SbosticColons (`:') can't be used in names, types, or values.
47755869Sbostic.Pp
47855869SbosticThere are no checks for
47955870Scael.Ic tc= name
48055869Sbosticloops in
48155869Sbostic.Nm cgetent .
48255877Selan.Pp
48355877SelanThe buffer added to the database by a call to
48455877Selan.Nm cgetset
48555877Selanis not unique to the database but is rather prepended to any database used.
486