xref: /original-bsd/lib/libc/gen/getcap.3 (revision deff14a8)
1.\" Copyright (c) 1992, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Casey Leedom of Lawrence Livermore National Laboratory.
6.\"
7.\" %sccs.include.redist.roff%
8.\"
9.\"	@(#)getcap.3	8.4 (Berkeley) 05/13/94
10.\"
11.Dd ""
12.Dt GETCAP 3
13.Os
14.Sh NAME
15.Nm cgetent ,
16.Nm cgetset ,
17.Nm cgetmatch ,
18.Nm cgetcap ,
19.Nm cgetnum ,
20.Nm cgetstr ,
21.Nm cgetustr ,
22.Nm cgetfirst ,
23.Nm cgetnext ,
24.Nm cgetclose
25.Nd capability database access routines
26.Sh SYNOPSIS
27.Fd #include <stdlib.h>
28.Ft int
29.Fn cgetent "char **buf" "char **db_array" "char *name"
30.Ft int
31.Fn cgetset "char *ent"
32.Ft int
33.Fn cgetmatch "char *buf" "char *name"
34.Ft char *
35.Fn cgetcap "char *buf" "char *cap" "char type"
36.Ft int
37.Fn cgetnum "char *buf" "char *cap" "long *num"
38.Ft int
39.Fn cgetstr "char *buf" "char *cap" "char **str"
40.Ft int
41.Fn cgetustr "char *buf" "char *cap" "char **str"
42.Ft int
43.Fn cgetfirst "char **buf" "char **db_array"
44.Ft int
45.Fn cgetnext "char **buf" "char **db_array"
46.Ft int
47.Fn cgetclose "void"
48.Sh DESCRIPTION
49.Fn Cgetent
50extracts the capability
51.Fa name
52from the database specified by the
53.Dv NULL
54terminated file array
55.Fa db_array
56and returns a pointer to a
57.Xr malloc Ns \&'d
58copy of it in
59.Fa buf .
60.Nm Cgetent
61will first look for files ending in
62.Nm .db
63(see
64.Xr cap_mkdb 1)
65before accessing the ASCII file.
66.Fa Buf
67must be retained through all subsequent calls to
68.Fn cgetmatch ,
69.Fn cgetcap ,
70.Fn cgetnum ,
71.Fn cgetstr ,
72and
73.Fn cgetustr ,
74but may then be
75.Xr free Ns \&'d.
76On success 0 is returned, 1 if the returned
77record contains an unresolved
78.Nm tc
79expansion,
80\-1 if the requested record couldn't be found,
81\-2 if a system error was encountered (couldn't open/read a file, etc.) also
82setting
83.Va errno ,
84and \-3 if a potential reference loop is detected (see
85.Ic tc=
86comments below).
87.Pp
88.Nm Cgetset
89enables the addition of a character buffer containing a single capability
90record entry
91to the capability database.
92Conceptually, the entry is added as the first ``file'' in the database, and
93is therefore searched first on the call to
94.Nm cgetent .
95The entry is passed in
96.Fa ent .
97If
98.Fa ent
99is
100.Dv NULL ,
101the current entry is removed from the database.
102.Nm Cgetset
103must precede the database traversal.  It must be called before the
104.Nm cgetent
105call. If a sequential access is being performed (see below), it must be called
106before the first sequential access call (
107.Nm cgetfirst
108or
109.Nm cgetnext
110), or be directly preceded by a
111.Nm cgetclose
112call.
113On success 0 is returned and \-1 on failure.
114.Pp
115.Nm Cgetmatch
116will return 0 if
117.Fa name
118is one of the names of the capability record
119.Fa buf ,
120\-1 if
121not.
122.Pp
123.Nm Cgetcap
124searches the capability record
125.Fa buf
126for the capability
127.Fa cap
128with type
129.Fa type .
130A
131.Fa type
132is specified using any single character.  If a colon (`:') is used, an
133untyped capability will be searched for (see below for explanation of
134types).  A pointer to the value of
135.Fa cap
136in
137.Fa buf
138is returned on success,
139.Dv NULL
140if the requested capability couldn't be
141found.  The end of the capability value is signaled by a `:' or
142.Tn ASCII
143.Dv NUL
144(see below for capability database syntax).
145.Pp
146.Nm Cgetnum
147retrieves the value of the numeric capability
148.Fa cap
149from the capability record pointed to by
150.Fa buf .
151The numeric value is returned in the
152.Ft long
153pointed to by
154.Fa num .
1550 is returned on success, \-1 if the requested numeric capability couldn't
156be found.
157.Pp
158.Nm Cgetstr
159retrieves the value of the string capability
160.Fa cap
161from the capability record pointed to by
162.Fa buf .
163A pointer to a decoded,
164.Dv NUL
165terminated,
166.Xr malloc Ns \&'d
167copy of the string is returned in the
168.Ft char *
169pointed to by
170.Fa str .
171The number of characters in the decoded string not including the trailing
172.Dv NUL
173is returned on success, \-1 if the requested string capability couldn't
174be found, \-2 if a system error was encountered (storage allocation
175failure).
176.Pp
177.Nm Cgetustr
178is identical to
179.Nm cgetstr
180except that it does not expand special characters, but rather returns each
181character of the capability string literally.
182.Pp
183.Nm Cgetfirst ,
184.Nm cgetnext ,
185comprise a function group that provides for sequential
186access of the
187.Dv NULL
188pointer terminated array of file names,
189.Fa db_array .
190.Nm Cgetfirst
191returns the first record in the database and resets the access
192to the first record.
193.Nm Cgetnext
194returns the next record in the database with respect to the
195record returned by the previous
196.Nm cgetfirst
197or
198.Nm cgetnext
199call.  If there is no such previous call, the first record in the database is
200returned.
201Each record is returned in a
202.Xr malloc Ns \&'d
203copy pointed to by
204.Fa buf .
205.Ic Tc
206expansion is done (see
207.Ic tc=
208comments below).
209Upon completion of the database 0 is returned,  1 is returned upon successful
210return of record with possibly more remaining (we haven't reached the end of
211the database yet), 2 is returned if the record contains an unresolved
212.Nm tc
213expansion, \-1 is returned if an system error occurred, and \-2
214is returned if a potential reference loop is detected (see
215.Ic tc=
216comments below).
217Upon completion of database (0 return) the database is closed.
218.Pp
219.Nm Cgetclose
220closes the sequential access and frees any memory and file descriptors
221being used.  Note that it does not erase the buffer pushed by a call to
222.Nm cgetset .
223.Sh CAPABILITY DATABASE SYNTAX
224Capability databases are normally
225.Tn ASCII
226and may be edited with standard
227text editors.  Blank lines and lines beginning with a `#' are comments
228and are ignored.  Lines ending with a `\|\e' indicate that the next line
229is a continuation of the current line; the `\|\e' and following newline
230are ignored.  Long lines are usually continued onto several physical
231lines by ending each line except the last with a `\|\e'.
232.Pp
233Capability databases consist of a series of records, one per logical
234line.  Each record contains a variable number of `:'-separated fields
235(capabilities).  Empty fields consisting entirely of white space
236characters (spaces and tabs) are ignored.
237.Pp
238The first capability of each record specifies its names, separated by `|'
239characters.  These names are used to reference records in the database.
240By convention, the last name is usually a comment and is not intended as
241a lookup tag.  For example, the
242.Em vt100
243record from the
244.Nm termcap
245database begins:
246.Pp
247.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:"
248.Pp
249giving four names that can be used to access the record.
250.Pp
251The remaining non-empty capabilities describe a set of (name, value)
252bindings, consisting of a names optionally followed by a typed values:
253.Bl -column "nameTvalue"
254.It name Ta "typeless [boolean] capability"
255.Em name No "is present [true]"
256.It name Ns Em \&T Ns value Ta capability
257.Pq Em name , \&T
258has value
259.Em value
260.It name@ Ta "no capability" Em name No exists
261.It name Ns Em T Ns \&@ Ta capability
262.Pq Em name , T
263does not exist
264.El
265.Pp
266Names consist of one or more characters.  Names may contain any character
267except `:', but it's usually best to restrict them to the printable
268characters and avoid use of graphics like `#', `=', `%', `@', etc.  Types
269are single characters used to separate capability names from their
270associated typed values.  Types may be any character except a `:'.
271Typically, graphics like `#', `=', `%', etc. are used.  Values may be any
272number of characters and may contain any character except `:'.
273.Sh CAPABILITY DATABASE SEMANTICS
274Capability records describe a set of (name, value) bindings.  Names may
275have multiple values bound to them.  Different values for a name are
276distinguished by their
277.Fa types .
278.Nm Cgetcap
279will return a pointer to a value of a name given the capability name and
280the type of the value.
281.Pp
282The types `#' and `=' are conventionally used to denote numeric and
283string typed values, but no restriction on those types is enforced.  The
284functions
285.Nm cgetnum
286and
287.Nm cgetstr
288can be used to implement the traditional syntax and semantics of `#'
289and `='.
290Typeless capabilities are typically used to denote boolean objects with
291presence or absence indicating truth and false values respectively.
292This interpretation is conveniently represented by:
293.Pp
294.Dl "(getcap(buf, name, ':') != NULL)"
295.Pp
296A special capability,
297.Ic tc= name ,
298is used to indicate that the record specified by
299.Fa name
300should be substituted for the
301.Ic tc
302capability.
303.Ic Tc
304capabilities may interpolate records which also contain
305.Ic tc
306capabilities and more than one
307.Ic tc
308capability may be used in a record.  A
309.Ic tc
310expansion scope (i.e., where the argument is searched for) contains the
311file in which the
312.Ic tc
313is declared and all subsequent files in the file array.
314.Pp
315When a database is searched for a capability record, the first matching
316record in the search is returned.  When a record is scanned for a
317capability, the first matching capability is returned; the capability
318.Ic :nameT@:
319will hide any following definition of a value of type
320.Em T
321for
322.Fa name ;
323and the capability
324.Ic :name@:
325will prevent any following values of
326.Fa name
327from being seen.
328.Pp
329These features combined with
330.Ic tc
331capabilities can be used to generate variations of other databases and
332records by either adding new capabilities, overriding definitions with new
333definitions, or hiding following definitions via `@' capabilities.
334.Sh EXAMPLES
335.Bd -unfilled -offset indent
336example\||\|an example of binding multiple values to names:\e
337	:foo%bar:foo^blah:foo@:\e
338	:abc%xyz:abc^frap:abc$@:\e
339	:tc=more:
340.Ed
341.Pp
342The capability foo has two values bound to it (bar of type `%' and blah of
343type `^') and any other value bindings are hidden.  The capability abc
344also has two values bound but only a value of type `$' is prevented from
345being defined in the capability record more.
346.Pp
347.Bd -unfilled -offset indent
348file1:
349 	new\||\|new_record\||\|a modification of "old":\e
350		:fript=bar:who-cares@:tc=old:blah:tc=extensions:
351file2:
352	old\||\|old_record\||\|an old database record:\e
353		:fript=foo:who-cares:glork#200:
354.Ed
355.Pp
356The records are extracted by calling
357.Nm cgetent
358with file1 preceding file2.
359In the capability record new in file1, fript=bar overrides the definition
360of fript=foo interpolated from the capability record old in file2,
361who-cares@ prevents the definition of any who-cares definitions in old
362from being seen, glork#200 is inherited from old, and blah and anything
363defined by the record extensions is added to those definitions in old.
364Note that the position of the fript=bar and who-cares@ definitions before
365tc=old is important here.  If they were after, the definitions in old
366would take precedence.
367.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS
368Two types are predefined by
369.Nm cgetnum
370and
371.Nm cgetstr :
372.Bl -column "nameXnumber"
373.Sm off
374.It Em name No \&# Em number Ta numeric
375capability
376.Em name
377has value
378.Em number
379.It Em name No = Em string Ta "string capability"
380.Em name
381has value
382.Em string
383.It Em name No \&#@ Ta "the numeric capability"
384.Em name
385does not exist
386.It Em name No \&=@ Ta "the string capability"
387.Em name
388does not exist
389.El
390.Pp
391Numeric capability values may be given in one of three numeric bases.
392If the number starts with either
393.Ql 0x
394or
395.Ql 0X
396it is interpreted as a hexadecimal number (both upper and lower case a-f
397may be used to denote the extended hexadecimal digits).
398Otherwise, if the number starts with a
399.Ql 0
400it is interpreted as an octal number.
401Otherwise the number is interpreted as a decimal number.
402.Pp
403String capability values may contain any character.  Non-printable
404.Dv ASCII
405codes, new lines, and colons may be conveniently represented by the use
406of escape sequences:
407.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)"
408^X	('\fIX\fP' & 037)	control-\fIX\fP
409\e\|b, \e\|B	(ASCII 010)	backspace
410\e\|t, \e\|T	(ASCII 011)	tab
411\e\|n, \e\|N	(ASCII 012)	line feed (newline)
412\e\|f, \e\|F	(ASCII 014)	form feed
413\e\|r, \e\|R	(ASCII 015)	carriage return
414\e\|e, \e\|E	(ASCII 027)	escape
415\e\|c, \e\|C	(:)	colon
416\e\|\e	(\e\|)	back slash
417\e\|^	(^)	caret
418\e\|\fInnn\fP	(ASCII octal \fInnn\fP)
419.El
420.Pp
421A `\|\e' may be followed by up to three octal digits directly specifies
422the numeric code for a character.  The use of
423.Tn ASCII
424.Dv NUL Ns s ,
425while easily
426encoded, causes all sorts of problems and must be used with care since
427.Dv NUL Ns s
428are typically used to denote the end of strings; many applications
429use `\e\|200' to represent a
430.Dv NUL .
431.Sh DIAGNOSTICS
432.Nm Cgetent ,
433.Nm cgetset ,
434.Nm cgetmatch ,
435.Nm cgetnum ,
436.Nm cgetstr ,
437.Nm cgetustr ,
438.Nm cgetfirst ,
439and
440.Nm cgetnext
441return a value greater than or equal to 0 on success and a value less
442than 0 on failure.
443.Nm Cgetcap
444returns a character pointer on success and a
445.Dv NULL
446on failure.
447.Pp
448.Nm Cgetent ,
449and
450.Nm cgetseq
451may fail and set
452.Va errno
453for any of the errors specified for the library functions:
454.Xr fopen 2 ,
455.Xr fclose 2 ,
456.Xr open 2 ,
457and
458.Xr close 2 .
459.Pp
460.Nm Cgetent ,
461.Nm cgetset ,
462.Nm cgetstr ,
463and
464.Nm cgetustr
465may fail and set
466.Va errno
467as follows:
468.Bl -tag -width Er
469.It Bq Er ENOMEM
470No memory to allocate.
471.El
472.Sh SEE ALSO
473.Xr cap_mkdb 1 ,
474.Xr malloc 3
475.Sh BUGS
476Colons (`:') can't be used in names, types, or values.
477.Pp
478There are no checks for
479.Ic tc= name
480loops in
481.Nm cgetent .
482.Pp
483The buffer added to the database by a call to
484.Nm cgetset
485is not unique to the database but is rather prepended to any database used.
486