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