155869Sbostic.\" Copyright (c) 1992 The Regents of the University of California. 255869Sbostic.\" 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*55877Selan.\" @(#)getcap.3 5.3 (Berkeley) 08/07/92 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 5055869Sbosticextracts the capability rec 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 5955869Sbostic.Fa buf . 6055869Sbostic.Fa Buf 6155869Sbosticmust be retained through all subsequent calls to 6255869Sbostic.Fn cgetmatch , 6355869Sbostic.Fn cgetcap , 6455869Sbostic.Fn cgetnum , 6555869Sbostic.Fn cgetstr , 6655869Sbosticand 6755869Sbostic.Fn cgetustr , 6855869Sbosticbut may then be 6955870Scael.Xr free Ns \&'d. 7055869SbosticOn success 0 is returned, \-1 if the requested record couldn't be found, 7155869Sbostic\-2 if a system error was encountered (couldn't open/read a file, etc.) also 7255869Sbosticsetting 7355869Sbostic.Va errno , 7455869Sbosticand \-3 if a potential reference loop is detected (see 7555870Scael.Ic tc= 7655869Sbosticcomments below). 7755869Sbostic.Pp 7855869Sbostic.Nm Cgetset 7955869Sbosticenables the addition of a character buffer containing a single capability 8055869Sbosticrecord entry 8155869Sbosticto the capability database. 8255869SbosticConceptually, the entry is added as the first ``file'' in the database, and 8355869Sbosticis therefore searched first on the call to 8455869Sbostic.Nm cgetent . 8555869SbosticThe entry is passed in 8655869Sbostic.Fa ent . 8755869SbosticIf 8855869Sbostic.Fa ent 8955870Scaelis 9055870Scael.Dv NULL , 91*55877Selanthe current entry is removed from the database. 92*55877Selan.Nm Cgetset 93*55877Selanmust precede the database traversal. It must be called before the 94*55877Selan.Nm cgetent 95*55877Selancall. If a sequential access is being performed (see below), it must be called 96*55877Selanbefore the first sequential access call ( 97*55877Selan.Nm cgetfirst 98*55877Selanor 99*55877Selan.Nm cgetnext 100*55877Selan), or be directly preceded by a 101*55877Selan.Nm cgetclose 102*55877Selancall. 10355869SbosticOn success 0 is returned and \-1 on failure. 10455869Sbostic.Pp 10555869Sbostic.Nm Cgetmatch 10655869Sbosticwill return 0 if 10755869Sbostic.Fa name 10855869Sbosticis one of the names of the capability record 10955869Sbostic.Fa buf , 11055869Sbostic\-1 if 11155869Sbosticnot. 11255869Sbostic.Pp 11355869Sbostic.Nm Cgetcap 11455869Sbosticsearches the capability record 11555869Sbostic.Fa buf 11655869Sbosticfor the capability 11755869Sbostic.Fa cap 11855869Sbosticwith type 11955869Sbostic.Fa type . 12055869SbosticA 12155869Sbostic.Fa type 12255869Sbosticis specified using any single character. If a colon (`:') is used, an 12355869Sbosticuntyped capability will be searched for (see below for explanation of 12455869Sbostictypes). A pointer to the value of 12555869Sbostic.Fa cap 12655869Sbosticin 12755869Sbostic.Fa buf 12855870Scaelis returned on success, 12955870Scael.Dv NULL 13055870Scaelif the requested capability couldn't be 13155870Scaelfound. The end of the capability value is signaled by a `:' or 13255870Scael.Tn ASCII 13355870Scael.Dv NUL 13455869Sbostic(see below for capability database syntax). 13555869Sbostic.Pp 13655869Sbostic.Nm Cgetnum 13755869Sbosticretrieves the value of the numeric capability 13855869Sbostic.Fa cap 13955869Sbosticfrom the capability record pointed to by 14055869Sbostic.Fa buf . 14155869SbosticThe numeric value is returned in the 14255869Sbostic.Ft long 14355869Sbosticpointed to by 14455869Sbostic.Fa num . 14555869Sbostic0 is returned on success, \-1 if the requested numeric capability couldn't 14655869Sbosticbe found. 14755869Sbostic.Pp 14855869Sbostic.Nm Cgetstr 14955869Sbosticretrieves the value of the string capability 15055869Sbostic.Fa cap 15155869Sbosticfrom the capability record pointed to by 15255869Sbostic.Fa buf . 15355870ScaelA pointer to a decoded, 15455870Scael.Dv NUL 15555870Scaelterminated, 15655870Scael.Xr malloc Ns \&'d 15755869Sbosticcopy of the string is returned in the 15855869Sbostic.Ft char * 15955869Sbosticpointed to by 16055869Sbostic.Fa str . 16155869SbosticThe number of characters in the decoded string not including the trailing 16255870Scael.Dv NUL 16355870Scaelis returned on success, \-1 if the requested string capability couldn't 16455869Sbosticbe found, \-2 if a system error was encountered (storage allocation 16555869Sbosticfailure). 16655869Sbostic.Pp 16755869Sbostic.Nm Cgetustr 16855869Sbosticis identical to 16955869Sbostic.Nm cgetstr 17055869Sbosticexcept that it does not expand special characters, but rather returns each 17155869Sbosticcharacter of the capability string literally. 17255869Sbostic.Pp 17355869Sbostic.Nm Cgetfirst , 17455869Sbostic.Nm cgetnext , 17555869Sbosticcomprise a function group that provides for sequential 17655870Scaelaccess of the 17755870Scael.Dv NULL 17855870Scaelpointer terminated array of file names, 17955869Sbostic.Fa db_array . 18055869Sbostic.Nm Cgetfirst 18155869Sbosticreturns the first record in the database and resets the access 18255869Sbosticto the first record. 18355869Sbostic.Nm Cgetnext 18455869Sbosticreturns the next record in the database with respect to the 18555869Sbosticrecord returned by the previous 18655869Sbostic.Nm cgetfirst 18755869Sbosticor 18855869Sbostic.Nm cgetnext 18955869Sbosticcall. If there is no such previous call, the first record in the database is 19055869Sbosticreturned. 19155869SbosticEach record is returned in a 19255870Scael.Xr malloc Ns \&'d 19355869Sbosticcopy pointed to by 19455869Sbostic.Fa buf . 19555870Scael.Ic Tc 19655869Sbosticexpansion is done (see 19755870Scael.Ic tc= 19855869Sbosticcomments below). 19955869SbosticUpon completion of the database 0 is returned, 1 is returned upon successful 20055869Sbosticreturn of record with possibly more remaining (we haven't reached the end of 20155869Sbosticthe database yet), \-1 is returned if an system error occured, and \-2 20255869Sbosticis returned if a potential reference loop is detected (see 20355870Scael.Ic tc= 20455869Sbosticcomments below). 20555869SbosticUpon completion of database (0 return) the database is closed. 206*55877Selan.Pp 207*55877Selan.Nm Cgetclose 208*55877Selancloses the sequential access and frees any memory and file descriptors 209*55877Selanbeing used. Note that it does not erase the buffer pushed by a call to 210*55877Selan.Nm cgetset . 21155869Sbostic.Sh CAPABILITY DATABASE SYNTAX 21255870ScaelCapability databases are normally 21355870Scael.Tn ASCII 21455870Scaeland may be edited with standard 21555869Sbostictext editors. Blank lines and lines beginning with a `#' are comments 21655869Sbosticand are ignored. Lines ending with a `\|\e' indicate that the next line 21755869Sbosticis a continuation of the current line; the `\|\e' and following newline 21855869Sbosticare ignored. Long lines are usually continued onto several physical 21955869Sbosticlines by ending each line except the last with a `\|\e'. 22055869Sbostic.Pp 22155869SbosticCapability databases consist of a series of records, one per logical 22255869Sbosticline. Each record contains a variable number of `:'-separated fields 22355869Sbostic(capabilities). Empty fields consisting entirely of white space 22455869Sbosticcharacters (spaces and tabs) are ignored. 22555869Sbostic.Pp 22655869SbosticThe first capability of each record specifies its names, separated by `|' 22755869Sbosticcharacters. These names are used to reference records in the database. 22855869SbosticBy convention, the last name is usually a comment and is not intended as 22955869Sbostica lookup tag. For example, the 23055870Scael.Em vt100 23155869Sbosticrecord from the 23255869Sbostic.Nm termcap 23355869Sbosticdatabase begins: 23455869Sbostic.Pp 23555869Sbostic.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:" 23655869Sbostic.Pp 23755869Sbosticgiving four names that can be used to access the record. 23855869Sbostic.Pp 23955869SbosticThe remaining non-empty capabilities describe a set of (name, value) 24055869Sbosticbindings, consisting of a names optionally followed by a typed values: 24155870Scael.Bl -column "nameTvalue" 24255870Scael.It name Ta "typeless [boolean] capability" 24355870Scael.Em name No "is present [true]" 24455870Scael.It name Ns Em \&T Ns value Ta capability 24555870Scael.Pq Em name , \&T 24655870Scaelhas value 24755870Scael.Em value 24855870Scael.It name@ Ta "no capability" Em name No exists 24955870Scael.It name Ns Em T Ns \&@ Ta capability 25055870Scael.Pq Em name , T 25155870Scaeldoes not exist 25255870Scael.El 25355869Sbostic.Pp 25455869SbosticNames consist of one or more characters. Names may contain any character 25555869Sbosticexcept `:', but it's usually best to restrict them to the printable 25655869Sbosticcharacters and avoid use of graphics like `#', `=', `%', `@', etc. Types 25755869Sbosticare single characters used to separate capability names from their 25855869Sbosticassociated typed values. Types may be any character except a `:'. 25955869SbosticTypically, graphics like `#', `=', `%', etc. are used. Values may be any 26055869Sbosticnumber of characters and may contain any character except `:'. 26155869Sbostic.Sh CAPABILITY DATABASE SEMANTICS 26255869SbosticCapability records describe a set of (name, value) bindings. Names may 26355869Sbostichave multiple values bound to them. Different values for a name are 26455869Sbosticdistinguished by their 26555869Sbostic.Fa types . 26655869Sbostic.Nm Cgetcap 26755869Sbosticwill return a pointer to a value of a name given the capability name and 26855869Sbosticthe type of the value. 26955869Sbostic.Pp 27055869SbosticThe types `#' and `=' are conventionally used to denote numeric and 27155869Sbosticstring typed values, but no restriction on those types is enforced. The 27255869Sbosticfunctions 27355869Sbostic.Nm cgetnum 27455869Sbosticand 27555869Sbostic.Nm cgetstr 27655869Sbosticcan be used to implement the traditional syntax and semantics of `#' 27755869Sbosticand `='. 27855869SbosticTypeless capabilities are typically used to denote boolean objects with 27955869Sbosticpresence or absence indicating truth and false values respectively. 28055869SbosticThis interpretation is conveniently represented by: 28155869Sbostic.Pp 28255869Sbostic.Dl "(getcap(buf, name, ':') != NULL)" 28355869Sbostic.Pp 28455869SbosticA special capability, 28555870Scael.Ic tc= name , 28655869Sbosticis used to indicate that the record specified by 28755869Sbostic.Fa name 28855869Sbosticshould be substituted for the 28955870Scael.Ic tc 29055869Sbosticcapability. 29155870Scael.Ic Tc 29255869Sbosticcapabilities may interpolate records which also contain 29355870Scael.Ic tc 29455869Sbosticcapabilities and more than one 29555870Scael.Ic tc 29655869Sbosticcapability may be used in a record. A 29755870Scael.Ic tc 29855869Sbosticexpansion scope (i.e., where the argument is searched for) contains the 29955869Sbosticfile in which the 30055870Scael.Ic tc 30155869Sbosticis declared and all subsequent files in the file array. 30255869Sbostic.Pp 30355869SbosticWhen a database is searched for a capability record, the first matching 30455869Sbosticrecord in the search is returned. When an record is scanned for a 30555869Sbosticcapability, the first matching capability is returned; the capability 30655870Scael.Ic :nameT@: 30755869Sbosticwill hide any following definition of a value of type 30855870Scael.Em T 30955869Sbosticfor 31055869Sbostic.Fa name ; 31155869Sbosticand the capability 31255870Scael.Ic :name@: 31355869Sbosticwill prevent any following values of 31455869Sbostic.Fa name 31555869Sbosticfrom being seen. 31655869Sbostic.Pp 31755869SbosticThese features combined with 31855870Scael.Ic tc 31955869Sbosticcapabilities can be used to generate variations of other databases and 32055869Sbosticrecords by either adding new capabilities, overriding definitions with new 32155869Sbosticdefinitions, or hiding following definitions via `@' capabilities. 32255869Sbostic.Sh EXAMPLES 32355869Sbostic.Bd -unfilled -offset indent 32455869Sbosticexample\||\|an example of binding multiple values to names:\e 32555869Sbostic :foo%bar:foo^blah:foo@:\e 32655869Sbostic :abc%xyz:abc^frap:abc$@:\e 32755869Sbostic :tc=more: 32855869Sbostic.Ed 32955869Sbostic.Pp 33055869SbosticThe capability foo has two values bound to it (bar of type `%' and blah of 33155869Sbostictype `^') and any other value bindings are hidden. The capability abc 33255869Sbosticalso has two values bound but only a value of type `$' is prevented from 33355869Sbosticbeing defined in the capability record more. 33455869Sbostic.Pp 33555869Sbostic.Bd -unfilled -offset indent 33655869Sbosticfile1: 33755869Sbostic new\||\|new_record\||\|a modification of "old":\e 33855869Sbostic :fript=bar:who-cares@:tc=old:blah:tc=extensions: 33955869Sbosticfile2: 34055869Sbostic old\||\|old_record\||\|an old database record:\e 34155869Sbostic :fript=foo:who-cares:glork#200: 34255869Sbostic.Ed 34355869Sbostic.Pp 34455869SbosticThe records are extracted by calling 34555869Sbostic.Nm cgetent 34655869Sbosticwith file1 preceding file2. 34755869SbosticIn the capability record new in file1, fript=bar overrides the definition 34855869Sbosticof fript=foo interpolated from the capability record old in file2, 34955869Sbosticwho-cares@ prevents the definition of any who-cares definitions in old 35055869Sbosticfrom being seen, glork#200 is inherited from old, and blah and anything 35155869Sbosticdefined by the record extensions is added to those definitions in old. 35255869SbosticNote that the position of the fript=bar and who-cares@ definitions before 35355869Sbostictc=old is important here. If they were after, the definitions in old 35455869Sbosticwould take precedence. 35555869Sbostic.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS 35655869SbosticTwo types are predefined by 35755869Sbostic.Nm cgetnum 35855869Sbosticand 35955869Sbostic.Nm cgetstr : 36055870Scael.Bl -column "nameXnumber" 36155870Scael.Sm off 36255870Scael.It Em name No \&# Em number Ta numeric 36355870Scaelcapability 36455870Scael.Em name 36555870Scaelhas value 36655870Scael.Em number 36755870Scael.It Em name No = Em string Ta "string capability" 36855870Scael.Em name 36955870Scaelhas value 37055870Scael.Em string 37155870Scael.It Em name No \&#@ Ta "the numeric capability" 37255870Scael.Em name 37355870Scaeldoes not exist 37455870Scael.It Em name No \&=@ Ta "the string capability" 37555870Scael.Em name 37655870Scaeldoes not exist 37755870Scael.El 37855869Sbostic.Pp 37955869SbosticNumeric capability values may be given in one of three numeric bases. 38055869SbosticIf the number starts with either 38155870Scael.Ql 0x 38255869Sbosticor 38355870Scael.Ql 0X 38455869Sbosticit is interpreted as a hexadecimal number (both upper and lower case a-f 38555869Sbosticmay be used to denote the extended hexadecimal digits). 38655869SbosticOtherwise, if the number starts with a 38755870Scael.Ql 0 38855869Sbosticit is interpreted as an octal number. 38955869SbosticOtherwise the number is interpreted as a decimal number. 39055869Sbostic.Pp 39155870ScaelString capability values may contain any character. Non-printable 39255870Scael.Dv ASCII 39355869Sbosticcodes, new lines, and colons may be conveniently represented by the use 39455869Sbosticof escape sequences: 39555870Scael.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)" 39655869Sbostic^X ('\fIX\fP' & 037) control-\fIX\fP 39755869Sbostic\e\|b, \e\|B (ASCII 010) backspace 39855869Sbostic\e\|t, \e\|T (ASCII 011) tab 39955869Sbostic\e\|n, \e\|N (ASCII 012) line feed (newline) 40055869Sbostic\e\|f, \e\|F (ASCII 014) form feed 40155869Sbostic\e\|r, \e\|R (ASCII 015) carriage return 40255869Sbostic\e\|e, \e\|E (ASCII 027) escape 40355869Sbostic\e\|c, \e\|C (:) colon 40455869Sbostic\e\|\e (\e\|) back slash 40555869Sbostic\e\|^ (^) caret 40655869Sbostic\e\|\fInnn\fP (ASCII octal \fInnn\fP) 40755870Scael.El 40855869Sbostic.Pp 40955869SbosticA `\|\e' may be followed by up to three octal digits directly specifies 41055870Scaelthe numeric code for a character. The use of 41155870Scael.Tn ASCII 41255870Scael.Dv NUL Ns s , 41355870Scaelwhile easily 41455869Sbosticencoded, causes all sorts of problems and must be used with care since 41555870Scael.Dv NUL Ns s 41655870Scaelare typically used to denote the end of strings; many applications 41755870Scaeluse `\e\|200' to represent a 41855870Scael.Dv NUL . 41955869Sbostic.Sh DIAGNOSTICS 42055869Sbostic.Nm Cgetent , 42155869Sbostic.Nm cgetset , 42255869Sbostic.Nm cgetmatch , 42355869Sbostic.Nm cgetnum , 42455869Sbostic.Nm cgetstr , 42555869Sbostic.Nm cgetustr , 42655869Sbostic.Nm cgetfirst , 42755869Sbosticand 42855869Sbostic.Nm cgetnext 42955869Sbosticreturn a a value greater than or equal to 0 on success and a value less 43055869Sbosticthan 0 on failure. 43155869Sbostic.Nm Cgetcap 43255870Scaelreturns a character pointer on success and a 43355870Scael.Dv NULL 43455870Scaelon failure. 43555869Sbostic.Pp 43655869Sbostic.Nm Cgetent , 43755869Sbosticand 43855869Sbostic.Nm cgetseq 43955869Sbosticmay fail and set 44055869Sbostic.Va errno 44155869Sbosticfor any of the errors specified for the library functions: 44255869Sbostic.Xr fopen 2 , 44355869Sbostic.Xr fclose 2 , 44455869Sbostic.Xr open 2 , 44555869Sbosticand 44655869Sbostic.Xr close 2 . 44755869Sbostic.Pp 44855869Sbostic.Nm Cgetent , 44955869Sbostic.Nm cgetset , 45055869Sbostic.Nm cgetstr , 45155869Sbosticand 45255869Sbostic.Nm cgetustr 45355869Sbosticmay fail and set 45455869Sbostic.Va errno 45555869Sbosticas follows: 45655869Sbostic.Bl -tag -width Er 45755869Sbostic.It Bq Er ENOMEM 45855869SbosticNo memory to allocate. 45955869Sbostic.El 46055869Sbostic.Sh SEE ALSO 46155869Sbostic.Xr malloc 3 46255869Sbostic.Sh BUGS 46355869SbosticColons (`:') can't be used in names, types, or values. 46455869Sbostic.Pp 46555869SbosticThere are no checks for 46655870Scael.Ic tc= name 46755869Sbosticloops in 46855869Sbostic.Nm cgetent . 469*55877Selan.Pp 470*55877SelanThe buffer added to the database by a call to 471*55877Selan.Nm cgetset 472*55877Selanis not unique to the database but is rather prepended to any database used. 473