xref: /netbsd-src/lib/libc/gen/cgetcap.3 (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1.\"	$NetBSD: cgetcap.3,v 1.3 2005/06/17 02:10:45 enami Exp $
2.\"
3.\" Copyright (c) 1992, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Casey Leedom of Lawrence Livermore National Laboratory.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. Neither the name of the University nor the names of its contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\"	@(#)getcap.3	8.4 (Berkeley) 5/13/94
34.\"
35.Dd April 23, 2004
36.Dt CGETCAP 3
37.Os
38.Sh NAME
39.Nm cgetent ,
40.Nm cgetset ,
41.Nm cgetmatch ,
42.Nm cgetcap ,
43.Nm cgetnum ,
44.Nm cgetstr ,
45.Nm cgetustr ,
46.Nm cgetfirst ,
47.Nm cgetnext ,
48.Nm cgetclose
49.Nd capability database access routines
50.Sh LIBRARY
51.Lb libc
52.Sh SYNOPSIS
53.In stdlib.h
54.Ft int
55.Fn cgetent "char **buf" "const char * const *db_array" "const char *name"
56.Ft int
57.Fn cgetset "const char *ent"
58.Ft int
59.Fn cgetmatch "const char *buf" "const char *name"
60.Ft char *
61.Fn cgetcap "char *buf" "const char *cap" "int type"
62.Ft int
63.Fn cgetnum "char *buf" "const char *cap" "long *num"
64.Ft int
65.Fn cgetstr "char *buf" "const char *cap" "char **str"
66.Ft int
67.Fn cgetustr "char *buf" "const char *cap" "char **str"
68.Ft int
69.Fn cgetfirst "char **buf" "const char * const *db_array"
70.Ft int
71.Fn cgetnext "char **buf" "const char * const *db_array"
72.Ft int
73.Fn cgetclose "void"
74.Sh DESCRIPTION
75.Fn cgetent
76extracts the capability
77.Fa name
78from the database specified by the
79.Dv NULL
80terminated file array
81.Fa db_array
82and returns a pointer to a
83.Xr malloc 3 Ap d
84copy of it in
85.Fa buf .
86.Fn cgetent
87will first look for files ending in
88.Pa .db
89(see
90.Xr cap_mkdb 1 )
91before accessing the
92.Tn ASCII
93file.
94.Pp
95.Fa buf
96must be retained through all subsequent calls to
97.Fn cgetmatch ,
98.Fn cgetcap ,
99.Fn cgetnum ,
100.Fn cgetstr ,
101and
102.Fn cgetustr ,
103but may then be
104.Xr free 3 Ap d .
105.Pp
106On success 0 is returned, 1 if the returned record contains an unresolved
107.Qq tc
108expansion, \-1 if the requested record couldn't be found, \-2 if
109a system error was encountered (couldn't open/read a file, etc.)
110also setting
111.Va errno ,
112and \-3 if a potential reference loop is detected (see
113.Qq tc=name
114comments below).
115.Pp
116.Fn cgetset
117enables the addition of a character buffer containing a single capability
118record entry to the capability database.
119Conceptually, the entry is added as the first
120.Dq file
121in the database, and
122is therefore searched first on the call to
123.Fn cgetent .
124The entry is passed in
125.Fa ent .
126If
127.Fa ent
128is
129.Dv NULL ,
130the current entry is removed from the database.
131.Pp
132.Fn cgetset
133must precede the database traversal.
134It must be called before the
135.Fn cgetent
136call.
137If a sequential access is being performed (see below), it must be called
138before the first sequential access call
139.Po
140.Fn cgetfirst
141or
142.Fn cgetnext
143.Pc ,
144or be directly preceded by a
145.Fn cgetclose
146call.
147On success 0 is returned and \-1 on failure.
148.Pp
149.Fn cgetmatch
150will return 0 if
151.Fa name
152is one of the names of the capability record
153.Fa buf ,
154\-1 if not.
155.Pp
156.Fn cgetcap
157searches the capability record
158.Fa buf
159for the capability
160.Fa cap
161with type
162.Fa type .
163A
164.Fa type
165is specified using any single character.
166If a colon
167.Pq Sq \&:
168is used, an untyped capability will be searched
169for (see below for explanation of types).
170A pointer to the value of
171.Fa cap
172in
173.Fa buf
174is returned on success,
175.Dv NULL
176if the requested capability couldn't be found.
177The end of the capability value is signaled by a
178.Sq \&:
179or
180.Tn ASCII
181.Dv NUL
182(see below for capability database syntax).
183.Pp
184.Fn cgetnum
185retrieves the value of the numeric capability
186.Fa cap
187from the capability record pointed to by
188.Fa buf .
189The numeric value is returned in the
190.Ft long
191pointed to by
192.Fa num .
1930 is returned on success,
194\-1 if the requested numeric capability couldn't be found.
195.Pp
196.Fn cgetstr
197retrieves the value of the string capability
198.Fa cap
199from the capability record pointed to by
200.Fa buf .
201A pointer to a decoded,
202.Dv NUL
203terminated,
204.Xr malloc 3 Ap d
205copy of the string is returned in the
206.Ft char *
207pointed to by
208.Fa str .
209The number of characters in the decoded string not including the trailing
210.Dv NUL
211is returned on success, \-1 if the requested string capability couldn't
212be found, \-2 if a system error was encountered (storage allocation
213failure).
214.Pp
215.Fn cgetustr
216is identical to
217.Fn cgetstr
218except that it does not expand special characters, but rather returns each
219character of the capability string literally.
220.Pp
221.Fn cgetfirst ,
222.Fn cgetnext ,
223comprise a function group that provides for sequential access of the
224.Dv NULL
225pointer terminated array of file names,
226.Fa db_array .
227.Fn cgetfirst
228returns the first record in the database and resets the access
229to the first record.
230.Fn cgetnext
231returns the next record in the database with respect to the
232record returned by the previous
233.Fn cgetfirst
234or
235.Fn cgetnext
236call.
237If there is no such previous call,
238the first record in the database is returned.
239Each record is returned in a
240.Xr malloc 3 Ap d
241copy pointed to by
242.Fa buf .
243.Qq tc
244expansion is done (see
245.Qq tc=name
246comments below).
247.Pp
248Upon completion of the database 0 is returned,  1 is returned upon successful
249return of record with possibly more remaining (we haven't reached the end of
250the database yet), 2 is returned if the record contains an unresolved
251.Qq tc
252expansion, \-1 is returned if an system error occurred, and \-2
253is returned if a potential reference loop is detected (see
254.Qq tc=name
255comments below).
256Upon completion of database (0 return) the database is closed.
257.Pp
258.Fn cgetclose
259closes the sequential access and frees any memory and file descriptors
260being used.
261Note that it does not erase the buffer pushed by a call to
262.Fn cgetset .
263.Sh CAPABILITY DATABASE SYNTAX
264Capability databases are normally
265.Tn ASCII
266and may be edited with standard text editors.
267Blank lines and lines beginning with a
268.Sq #
269are comments and are ignored.
270Lines ending with a
271.Sq \|\e
272indicate that the next line is a continuation
273of the current line; the
274.Sq \|\e
275and following newline are ignored.
276Long lines are usually continued onto several physical
277lines by ending each line except the last with a
278.Sq \|\e .
279.Pp
280Capability databases consist of a series of records, one per logical line.
281Each record contains a variable number of
282.So \&: Sc Ns -separated
283fields (capabilities).
284Empty fields consisting entirely of white space
285characters (spaces and tabs) are ignored.
286.Pp
287The first capability of each record specifies its names, separated by
288.Sq |
289characters.
290These names are used to reference records in the database.
291By convention, the last name is usually a comment and is not intended as
292a lookup tag.
293For example, the
294.Em vt100
295record from the
296.Pa termcap
297database begins:
298.Pp
299.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:"
300.Pp
301giving four names that can be used to access the record.
302.Pp
303The remaining non-empty capabilities describe a set of (name, value)
304bindings, consisting of a name optionally followed by a typed value:
305.Bl -column "nameTvalue"
306.It name Ta "typeless [boolean] capability"
307.Em name No "is present [true]"
308.It name Ns Em \&T Ns value Ta capability
309.Pq Em name , \&T
310has value
311.Em value
312.It name@ Ta "no capability" Em name No exists
313.It name Ns Em T Ns \&@ Ta capability
314.Pq Em name , T
315does not exist
316.El
317.Pp
318Names consist of one or more characters.
319Names may contain any character except
320.Sq \&: ,
321but it's usually best
322to restrict them to the printable characters and avoid use of
323graphics like
324.Sq # ,
325.Sq = ,
326.Sq % ,
327.Sq @ ,
328etc.
329.Pp
330Types are single characters used to separate capability names from
331their associated typed values.
332Types may be any character except a
333.Sq \&: .
334Typically, graphics like
335.Sq # ,
336.Sq = ,
337.Sq % ,
338etc. are used.
339Values may be any number of characters and may contain any character except
340.Sq \&: .
341.Sh CAPABILITY DATABASE SEMANTICS
342Capability records describe a set of (name, value) bindings.
343Names may have multiple values bound to them.
344Different values for a name are distinguished by their
345.Fa types .
346.Fn cgetcap
347will return a pointer to a value of a name given the capability name and
348the type of the value.
349.Pp
350The types
351.Sq #
352and
353.Sq =
354are conventionally used to denote numeric and
355string typed values, but no restriction on those types is enforced.
356The functions
357.Fn cgetnum
358and
359.Fn cgetstr
360can be used to implement the traditional syntax and semantics of
361.Sq #
362and
363.Sq = .
364Typeless capabilities are typically used to denote boolean objects with
365presence or absence indicating truth and false values respectively.
366This interpretation is conveniently represented by:
367.Pp
368.Dl "(getcap(buf, name, ':') != NULL)"
369.Pp
370A special capability,
371.Qq tc=name ,
372is used to indicate that the record specified by
373.Fa name
374should be substituted for the
375.Qq tc
376capability.
377.Qq tc
378capabilities may interpolate records which also contain
379.Qq tc
380capabilities and more than one
381.Qq tc
382capability may be used in a record.
383A
384.Qq tc
385expansion scope (i.e. where the argument is searched for) contains the
386file in which the
387.Qq tc
388is declared and all subsequent files in the file array.
389.Pp
390When a database is searched for a capability record, the first matching
391record in the search is returned.
392When a record is scanned for a capability, the first matching
393capability is returned; the capability
394.Qq :nameT@:
395will hide any following definition of a value of type
396.Em T
397for
398.Fa name ;
399and the capability
400.Qq :name@:
401will prevent any following values of
402.Fa name
403from being seen.
404.Pp
405These features combined with
406.Qq tc
407capabilities can be used to generate variations of other databases and
408records by either adding new capabilities, overriding definitions with new
409definitions, or hiding following definitions via
410.Sq @
411capabilities.
412.Sh EXAMPLES
413.Bd -unfilled -offset indent
414example\||\|an example of binding multiple values to names:\e
415	:foo%bar:foo^blah:foo@:\e
416	:abc%xyz:abc^frap:abc$@:\e
417	:tc=more:
418.Ed
419.Pp
420The capability
421.Em foo
422has two values bound to it
423.Po
424.Em bar
425of type
426.Sq %
427and
428.Em blah
429of type
430.Sq ^
431.Pc
432and any other value bindings are hidden.
433The capability
434.Em abc
435also has two values bound but only a value of type
436.Sq $
437is prevented from
438being defined in the capability record more.
439.Pp
440.Bd -unfilled -offset indent
441file1:
442 	new\||\|new_record\||\|a modification of "old":\e
443		:fript=bar:who-cares@:tc=old:blah:tc=extensions:
444file2:
445	old\||\|old_record\||\|an old database record:\e
446		:fript=foo:who-cares:glork#200:
447.Ed
448.Pp
449The records are extracted by calling
450.Fn cgetent
451with
452.Fa file1
453preceding
454.Fa file2 .
455In the capability record
456.Em new
457in
458.Fa file1 ,
459.Qq fript=bar
460overrides the definition of
461.Qq fript=foo
462interpolated from the capability record
463.Em old
464in
465.Fa file2 ,
466.Qq who-cares@
467prevents the definition of any who-cares definitions in
468.Em old
469from being seen,
470.Qq glork#200
471is inherited from
472.Em old ,
473and
474.Em blah
475and anything defined by the record extensions is added to those
476definitions in
477.Em old .
478Note that the position of the
479.Qq fript=bar
480and
481.Qq who-cares@
482definitions before
483.Qq tc=old
484is important here.
485If they were after, the definitions in
486.Em old
487would take precedence.
488.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS
489Two types are predefined by
490.Fn cgetnum
491and
492.Fn cgetstr :
493.Bl -column "nameXnumber"
494.It Em name Ns \&# Ns Em number Ta numeric
495capability
496.Em name
497has value
498.Em number
499.It Em name Ns = Ns Em string Ta "string capability"
500.Em name
501has value
502.Em string
503.It Em name Ns \&#@ Ns Ta "the numeric capability"
504.Em name
505does not exist
506.It Em name Ns \&=@ Ns Ta "the string capability"
507.Em name
508does not exist
509.El
510.Pp
511Numeric capability values may be given in one of three numeric bases.
512If the number starts with either
513.Ql 0x
514or
515.Ql 0X
516it is interpreted as a hexadecimal number (both upper and lower case a-f
517may be used to denote the extended hexadecimal digits).
518Otherwise, if the number starts with a
519.Ql 0
520it is interpreted as an octal number.
521Otherwise the number is interpreted as a decimal number.
522.Pp
523String capability values may contain any character.
524Non-printable
525.Dv ASCII
526codes, new lines, and colons may be conveniently represented by the use
527of escape sequences:
528.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)"
529^X	('\fIX\fP' \*[Am] 037)	control-\fIX\fP
530\e\|b, \e\|B	(ASCII 010)	backspace
531\e\|t, \e\|T	(ASCII 011)	tab
532\e\|n, \e\|N	(ASCII 012)	line feed (newline)
533\e\|f, \e\|F	(ASCII 014)	form feed
534\e\|r, \e\|R	(ASCII 015)	carriage return
535\e\|e, \e\|E	(ASCII 027)	escape
536\e\|c, \e\|C	(:)	colon
537\e\|\e	(\e\|)	back slash
538\e\|^	(^)	caret
539\e\|\fInnn\fP	(ASCII octal \fInnn\fP)
540.El
541.Pp
542A
543.Sq \|\e
544followed by up to three octal digits directly specifies
545the numeric code for a character.
546The use of
547.Tn ASCII
548.Dv NUL Ns s ,
549while easily
550encoded, causes all sorts of problems and must be used with care since
551.Dv NUL Ns s
552are typically used to denote the end of strings; many applications
553use
554.Sq \e\|200
555to represent a
556.Dv NUL .
557.Sh DIAGNOSTICS
558.Fn cgetent ,
559.Fn cgetset ,
560.Fn cgetmatch ,
561.Fn cgetnum ,
562.Fn cgetstr ,
563.Fn cgetustr ,
564.Fn cgetfirst ,
565and
566.Fn cgetnext
567return a value greater than or equal to 0 on success and a value less
568than 0 on failure.
569.Fn cgetcap
570returns a character pointer on success and a
571.Dv NULL
572on failure.
573.Pp
574.Fn cgetclose ,
575.Fn cgetent ,
576.Fn cgetfirst ,
577and
578.Fn cgetnext
579may fail and set
580.Va errno
581for any of the errors specified for the library functions:
582.Xr fopen 3 ,
583.Xr fclose 3 ,
584.Xr open 2 ,
585and
586.Xr close 2 .
587.Pp
588.Fn cgetent ,
589.Fn cgetset ,
590.Fn cgetstr ,
591and
592.Fn cgetustr
593may fail and set
594.Va errno
595as follows:
596.Bl -tag -width Er
597.It Bq Er ENOMEM
598No memory to allocate.
599.El
600.Sh SEE ALSO
601.Xr cap_mkdb 1 ,
602.Xr malloc 3
603.Sh BUGS
604Colons
605.Pq Sq \&:
606can't be used in names, types, or values.
607.Pp
608There are no checks for
609.Qq tc=name
610loops in
611.Fn cgetent .
612.Pp
613The buffer added to the database by a call to
614.Fn cgetset
615is not unique to the database but is rather prepended to any database used.
616