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*55870Scael.\" @(#)getcap.3 5.2 (Berkeley) 08/06/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 52*55870Scaelfrom the database specified by the 53*55870Scael.Dv NULL 54*55870Scaelterminated file array 5555869Sbostic.Fa db_array 5655869Sbosticand returns a pointer to a 57*55870Scael.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 69*55870Scael.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 75*55870Scael.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 89*55870Scaelis 90*55870Scael.Dv NULL , 91*55870Scaelthe current entry is removed from the database. 9255869SbosticOn success 0 is returned and \-1 on failure. 9355869Sbostic.Pp 9455869Sbostic.Nm Cgetmatch 9555869Sbosticwill return 0 if 9655869Sbostic.Fa name 9755869Sbosticis one of the names of the capability record 9855869Sbostic.Fa buf , 9955869Sbostic\-1 if 10055869Sbosticnot. 10155869Sbostic.Pp 10255869Sbostic.Nm Cgetcap 10355869Sbosticsearches the capability record 10455869Sbostic.Fa buf 10555869Sbosticfor the capability 10655869Sbostic.Fa cap 10755869Sbosticwith type 10855869Sbostic.Fa type . 10955869SbosticA 11055869Sbostic.Fa type 11155869Sbosticis specified using any single character. If a colon (`:') is used, an 11255869Sbosticuntyped capability will be searched for (see below for explanation of 11355869Sbostictypes). A pointer to the value of 11455869Sbostic.Fa cap 11555869Sbosticin 11655869Sbostic.Fa buf 117*55870Scaelis returned on success, 118*55870Scael.Dv NULL 119*55870Scaelif the requested capability couldn't be 120*55870Scaelfound. The end of the capability value is signaled by a `:' or 121*55870Scael.Tn ASCII 122*55870Scael.Dv NUL 12355869Sbostic(see below for capability database syntax). 12455869Sbostic.Pp 12555869Sbostic.Nm Cgetnum 12655869Sbosticretrieves the value of the numeric capability 12755869Sbostic.Fa cap 12855869Sbosticfrom the capability record pointed to by 12955869Sbostic.Fa buf . 13055869SbosticThe numeric value is returned in the 13155869Sbostic.Ft long 13255869Sbosticpointed to by 13355869Sbostic.Fa num . 13455869Sbostic0 is returned on success, \-1 if the requested numeric capability couldn't 13555869Sbosticbe found. 13655869Sbostic.Pp 13755869Sbostic.Nm Cgetstr 13855869Sbosticretrieves the value of the string capability 13955869Sbostic.Fa cap 14055869Sbosticfrom the capability record pointed to by 14155869Sbostic.Fa buf . 142*55870ScaelA pointer to a decoded, 143*55870Scael.Dv NUL 144*55870Scaelterminated, 145*55870Scael.Xr malloc Ns \&'d 14655869Sbosticcopy of the string is returned in the 14755869Sbostic.Ft char * 14855869Sbosticpointed to by 14955869Sbostic.Fa str . 15055869SbosticThe number of characters in the decoded string not including the trailing 151*55870Scael.Dv NUL 152*55870Scaelis returned on success, \-1 if the requested string capability couldn't 15355869Sbosticbe found, \-2 if a system error was encountered (storage allocation 15455869Sbosticfailure). 15555869Sbostic.Pp 15655869Sbostic.Nm Cgetustr 15755869Sbosticis identical to 15855869Sbostic.Nm cgetstr 15955869Sbosticexcept that it does not expand special characters, but rather returns each 16055869Sbosticcharacter of the capability string literally. 16155869Sbostic.Pp 16255869Sbostic.Nm Cgetfirst , 16355869Sbostic.Nm cgetnext , 16455869Sbosticand 16555869Sbostic.Nm cgetclose 16655869Sbosticcomprise a function group that provides for sequential 167*55870Scaelaccess of the 168*55870Scael.Dv NULL 169*55870Scaelpointer terminated array of file names, 17055869Sbostic.Fa db_array . 17155869Sbostic.Nm Cgetfirst 17255869Sbosticreturns the first record in the database and resets the access 17355869Sbosticto the first record. 17455869Sbostic.Nm Cgetnext 17555869Sbosticreturns the next record in the database with respect to the 17655869Sbosticrecord returned by the previous 17755869Sbostic.Nm cgetfirst 17855869Sbosticor 17955869Sbostic.Nm cgetnext 18055869Sbosticcall. If there is no such previous call, the first record in the database is 18155869Sbosticreturned. 18255869Sbostic.Nm Cgetclose 18355869Sbosticcloses the access and frees any memory and file descriptors being used. 18455869SbosticEach record is returned in a 185*55870Scael.Xr malloc Ns \&'d 18655869Sbosticcopy pointed to by 18755869Sbostic.Fa buf . 188*55870Scael.Ic Tc 18955869Sbosticexpansion is done (see 190*55870Scael.Ic tc= 19155869Sbosticcomments below). 19255869SbosticUpon completion of the database 0 is returned, 1 is returned upon successful 19355869Sbosticreturn of record with possibly more remaining (we haven't reached the end of 19455869Sbosticthe database yet), \-1 is returned if an system error occured, and \-2 19555869Sbosticis returned if a potential reference loop is detected (see 196*55870Scael.Ic tc= 19755869Sbosticcomments below). 19855869SbosticUpon completion of database (0 return) the database is closed. 19955869Sbostic.Sh CAPABILITY DATABASE SYNTAX 200*55870ScaelCapability databases are normally 201*55870Scael.Tn ASCII 202*55870Scaeland may be edited with standard 20355869Sbostictext editors. Blank lines and lines beginning with a `#' are comments 20455869Sbosticand are ignored. Lines ending with a `\|\e' indicate that the next line 20555869Sbosticis a continuation of the current line; the `\|\e' and following newline 20655869Sbosticare ignored. Long lines are usually continued onto several physical 20755869Sbosticlines by ending each line except the last with a `\|\e'. 20855869Sbostic.Pp 20955869SbosticCapability databases consist of a series of records, one per logical 21055869Sbosticline. Each record contains a variable number of `:'-separated fields 21155869Sbostic(capabilities). Empty fields consisting entirely of white space 21255869Sbosticcharacters (spaces and tabs) are ignored. 21355869Sbostic.Pp 21455869SbosticThe first capability of each record specifies its names, separated by `|' 21555869Sbosticcharacters. These names are used to reference records in the database. 21655869SbosticBy convention, the last name is usually a comment and is not intended as 21755869Sbostica lookup tag. For example, the 218*55870Scael.Em vt100 21955869Sbosticrecord from the 22055869Sbostic.Nm termcap 22155869Sbosticdatabase begins: 22255869Sbostic.Pp 22355869Sbostic.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:" 22455869Sbostic.Pp 22555869Sbosticgiving four names that can be used to access the record. 22655869Sbostic.Pp 22755869SbosticThe remaining non-empty capabilities describe a set of (name, value) 22855869Sbosticbindings, consisting of a names optionally followed by a typed values: 229*55870Scael.Bl -column "nameTvalue" 230*55870Scael.It name Ta "typeless [boolean] capability" 231*55870Scael.Em name No "is present [true]" 232*55870Scael.It name Ns Em \&T Ns value Ta capability 233*55870Scael.Pq Em name , \&T 234*55870Scaelhas value 235*55870Scael.Em value 236*55870Scael.It name@ Ta "no capability" Em name No exists 237*55870Scael.It name Ns Em T Ns \&@ Ta capability 238*55870Scael.Pq Em name , T 239*55870Scaeldoes not exist 240*55870Scael.El 24155869Sbostic.Pp 24255869SbosticNames consist of one or more characters. Names may contain any character 24355869Sbosticexcept `:', but it's usually best to restrict them to the printable 24455869Sbosticcharacters and avoid use of graphics like `#', `=', `%', `@', etc. Types 24555869Sbosticare single characters used to separate capability names from their 24655869Sbosticassociated typed values. Types may be any character except a `:'. 24755869SbosticTypically, graphics like `#', `=', `%', etc. are used. Values may be any 24855869Sbosticnumber of characters and may contain any character except `:'. 24955869Sbostic.Sh CAPABILITY DATABASE SEMANTICS 25055869SbosticCapability records describe a set of (name, value) bindings. Names may 25155869Sbostichave multiple values bound to them. Different values for a name are 25255869Sbosticdistinguished by their 25355869Sbostic.Fa types . 25455869Sbostic.Nm Cgetcap 25555869Sbosticwill return a pointer to a value of a name given the capability name and 25655869Sbosticthe type of the value. 25755869Sbostic.Pp 25855869SbosticThe types `#' and `=' are conventionally used to denote numeric and 25955869Sbosticstring typed values, but no restriction on those types is enforced. The 26055869Sbosticfunctions 26155869Sbostic.Nm cgetnum 26255869Sbosticand 26355869Sbostic.Nm cgetstr 26455869Sbosticcan be used to implement the traditional syntax and semantics of `#' 26555869Sbosticand `='. 26655869SbosticTypeless capabilities are typically used to denote boolean objects with 26755869Sbosticpresence or absence indicating truth and false values respectively. 26855869SbosticThis interpretation is conveniently represented by: 26955869Sbostic.Pp 27055869Sbostic.Dl "(getcap(buf, name, ':') != NULL)" 27155869Sbostic.Pp 27255869SbosticA special capability, 273*55870Scael.Ic tc= name , 27455869Sbosticis used to indicate that the record specified by 27555869Sbostic.Fa name 27655869Sbosticshould be substituted for the 277*55870Scael.Ic tc 27855869Sbosticcapability. 279*55870Scael.Ic Tc 28055869Sbosticcapabilities may interpolate records which also contain 281*55870Scael.Ic tc 28255869Sbosticcapabilities and more than one 283*55870Scael.Ic tc 28455869Sbosticcapability may be used in a record. A 285*55870Scael.Ic tc 28655869Sbosticexpansion scope (i.e., where the argument is searched for) contains the 28755869Sbosticfile in which the 288*55870Scael.Ic tc 28955869Sbosticis declared and all subsequent files in the file array. 29055869Sbostic.Pp 29155869SbosticWhen a database is searched for a capability record, the first matching 29255869Sbosticrecord in the search is returned. When an record is scanned for a 29355869Sbosticcapability, the first matching capability is returned; the capability 294*55870Scael.Ic :nameT@: 29555869Sbosticwill hide any following definition of a value of type 296*55870Scael.Em T 29755869Sbosticfor 29855869Sbostic.Fa name ; 29955869Sbosticand the capability 300*55870Scael.Ic :name@: 30155869Sbosticwill prevent any following values of 30255869Sbostic.Fa name 30355869Sbosticfrom being seen. 30455869Sbostic.Pp 30555869SbosticThese features combined with 306*55870Scael.Ic tc 30755869Sbosticcapabilities can be used to generate variations of other databases and 30855869Sbosticrecords by either adding new capabilities, overriding definitions with new 30955869Sbosticdefinitions, or hiding following definitions via `@' capabilities. 31055869Sbostic.Sh EXAMPLES 31155869Sbostic.Bd -unfilled -offset indent 31255869Sbosticexample\||\|an example of binding multiple values to names:\e 31355869Sbostic :foo%bar:foo^blah:foo@:\e 31455869Sbostic :abc%xyz:abc^frap:abc$@:\e 31555869Sbostic :tc=more: 31655869Sbostic.Ed 31755869Sbostic.Pp 31855869SbosticThe capability foo has two values bound to it (bar of type `%' and blah of 31955869Sbostictype `^') and any other value bindings are hidden. The capability abc 32055869Sbosticalso has two values bound but only a value of type `$' is prevented from 32155869Sbosticbeing defined in the capability record more. 32255869Sbostic.Pp 32355869Sbostic.Bd -unfilled -offset indent 32455869Sbosticfile1: 32555869Sbostic new\||\|new_record\||\|a modification of "old":\e 32655869Sbostic :fript=bar:who-cares@:tc=old:blah:tc=extensions: 32755869Sbosticfile2: 32855869Sbostic old\||\|old_record\||\|an old database record:\e 32955869Sbostic :fript=foo:who-cares:glork#200: 33055869Sbostic.Ed 33155869Sbostic.Pp 33255869SbosticThe records are extracted by calling 33355869Sbostic.Nm cgetent 33455869Sbosticwith file1 preceding file2. 33555869SbosticIn the capability record new in file1, fript=bar overrides the definition 33655869Sbosticof fript=foo interpolated from the capability record old in file2, 33755869Sbosticwho-cares@ prevents the definition of any who-cares definitions in old 33855869Sbosticfrom being seen, glork#200 is inherited from old, and blah and anything 33955869Sbosticdefined by the record extensions is added to those definitions in old. 34055869SbosticNote that the position of the fript=bar and who-cares@ definitions before 34155869Sbostictc=old is important here. If they were after, the definitions in old 34255869Sbosticwould take precedence. 34355869Sbostic.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS 34455869SbosticTwo types are predefined by 34555869Sbostic.Nm cgetnum 34655869Sbosticand 34755869Sbostic.Nm cgetstr : 348*55870Scael.Bl -column "nameXnumber" 349*55870Scael.Sm off 350*55870Scael.It Em name No \&# Em number Ta numeric 351*55870Scaelcapability 352*55870Scael.Em name 353*55870Scaelhas value 354*55870Scael.Em number 355*55870Scael.It Em name No = Em string Ta "string capability" 356*55870Scael.Em name 357*55870Scaelhas value 358*55870Scael.Em string 359*55870Scael.It Em name No \&#@ Ta "the numeric capability" 360*55870Scael.Em name 361*55870Scaeldoes not exist 362*55870Scael.It Em name No \&=@ Ta "the string capability" 363*55870Scael.Em name 364*55870Scaeldoes not exist 365*55870Scael.El 36655869Sbostic.Pp 36755869SbosticNumeric capability values may be given in one of three numeric bases. 36855869SbosticIf the number starts with either 369*55870Scael.Ql 0x 37055869Sbosticor 371*55870Scael.Ql 0X 37255869Sbosticit is interpreted as a hexadecimal number (both upper and lower case a-f 37355869Sbosticmay be used to denote the extended hexadecimal digits). 37455869SbosticOtherwise, if the number starts with a 375*55870Scael.Ql 0 37655869Sbosticit is interpreted as an octal number. 37755869SbosticOtherwise the number is interpreted as a decimal number. 37855869Sbostic.Pp 379*55870ScaelString capability values may contain any character. Non-printable 380*55870Scael.Dv ASCII 38155869Sbosticcodes, new lines, and colons may be conveniently represented by the use 38255869Sbosticof escape sequences: 383*55870Scael.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)" 38455869Sbostic^X ('\fIX\fP' & 037) control-\fIX\fP 38555869Sbostic\e\|b, \e\|B (ASCII 010) backspace 38655869Sbostic\e\|t, \e\|T (ASCII 011) tab 38755869Sbostic\e\|n, \e\|N (ASCII 012) line feed (newline) 38855869Sbostic\e\|f, \e\|F (ASCII 014) form feed 38955869Sbostic\e\|r, \e\|R (ASCII 015) carriage return 39055869Sbostic\e\|e, \e\|E (ASCII 027) escape 39155869Sbostic\e\|c, \e\|C (:) colon 39255869Sbostic\e\|\e (\e\|) back slash 39355869Sbostic\e\|^ (^) caret 39455869Sbostic\e\|\fInnn\fP (ASCII octal \fInnn\fP) 395*55870Scael.El 39655869Sbostic.Pp 39755869SbosticA `\|\e' may be followed by up to three octal digits directly specifies 398*55870Scaelthe numeric code for a character. The use of 399*55870Scael.Tn ASCII 400*55870Scael.Dv NUL Ns s , 401*55870Scaelwhile easily 40255869Sbosticencoded, causes all sorts of problems and must be used with care since 403*55870Scael.Dv NUL Ns s 404*55870Scaelare typically used to denote the end of strings; many applications 405*55870Scaeluse `\e\|200' to represent a 406*55870Scael.Dv NUL . 40755869Sbostic.Sh DIAGNOSTICS 40855869Sbostic.Nm Cgetent , 40955869Sbostic.Nm cgetset , 41055869Sbostic.Nm cgetmatch , 41155869Sbostic.Nm cgetnum , 41255869Sbostic.Nm cgetstr , 41355869Sbostic.Nm cgetustr , 41455869Sbostic.Nm cgetfirst , 41555869Sbosticand 41655869Sbostic.Nm cgetnext 41755869Sbosticreturn a a value greater than or equal to 0 on success and a value less 41855869Sbosticthan 0 on failure. 41955869Sbostic.Nm Cgetcap 420*55870Scaelreturns a character pointer on success and a 421*55870Scael.Dv NULL 422*55870Scaelon failure. 42355869Sbostic.Pp 42455869Sbostic.Nm Cgetent , 42555869Sbosticand 42655869Sbostic.Nm cgetseq 42755869Sbosticmay fail and set 42855869Sbostic.Va errno 42955869Sbosticfor any of the errors specified for the library functions: 43055869Sbostic.Xr fopen 2 , 43155869Sbostic.Xr fclose 2 , 43255869Sbostic.Xr open 2 , 43355869Sbosticand 43455869Sbostic.Xr close 2 . 43555869Sbostic.Pp 43655869Sbostic.Nm Cgetent , 43755869Sbostic.Nm cgetset , 43855869Sbostic.Nm cgetstr , 43955869Sbosticand 44055869Sbostic.Nm cgetustr 44155869Sbosticmay fail and set 44255869Sbostic.Va errno 44355869Sbosticas follows: 44455869Sbostic.Bl -tag -width Er 44555869Sbostic.It Bq Er ENOMEM 44655869SbosticNo memory to allocate. 44755869Sbostic.El 44855869Sbostic.Sh SEE ALSO 44955869Sbostic.Xr malloc 3 45055869Sbostic.Sh BUGS 45155869SbosticColons (`:') can't be used in names, types, or values. 45255869Sbostic.Pp 45355869SbosticThere are no checks for 454*55870Scael.Ic tc= name 45555869Sbosticloops in 45655869Sbostic.Nm cgetent . 457