1*55869Sbostic.\" Copyright (c) 1992 The Regents of the University of California. 2*55869Sbostic.\" All rights reserved. 3*55869Sbostic.\" 4*55869Sbostic.\" This code is derived from software contributed to Berkeley by 5*55869Sbostic.\" Casey Leedom of Lawrence Livermore National Laboratory. 6*55869Sbostic.\" 7*55869Sbostic.\" %sccs.include.redist.roff% 8*55869Sbostic.\" 9*55869Sbostic.\" @(#)getcap.3 5.1 (Berkeley) 08/06/92 10*55869Sbostic.\" 11*55869Sbostic.Dd "" 12*55869Sbostic.Dt GETCAP 3 13*55869Sbostic.Os 14*55869Sbostic.Sh NAME 15*55869Sbostic.Nm cgetent , 16*55869Sbostic.Nm cgetset , 17*55869Sbostic.Nm cgetmatch , 18*55869Sbostic.Nm cgetcap , 19*55869Sbostic.Nm cgetnum , 20*55869Sbostic.Nm cgetstr , 21*55869Sbostic.Nm cgetustr , 22*55869Sbostic.Nm cgetfirst , 23*55869Sbostic.Nm cgetnext , 24*55869Sbostic.Nm cgetclose 25*55869Sbostic.Nd capability database access routines 26*55869Sbostic.Sh SYNOPSIS 27*55869Sbostic.Fd #include <stdlib.h> 28*55869Sbostic.Ft int 29*55869Sbostic.Fn cgetent "char **buf" "char **db_array" "char *name" 30*55869Sbostic.Ft int 31*55869Sbostic.Fn cgetset "char *ent" 32*55869Sbostic.Ft int 33*55869Sbostic.Fn cgetmatch "char *buf" "char *name" 34*55869Sbostic.Ft char * 35*55869Sbostic.Fn cgetcap "char *buf" "char *cap" "char type" 36*55869Sbostic.Ft int 37*55869Sbostic.Fn cgetnum "char *buf" "char *cap" "long *num" 38*55869Sbostic.Ft int 39*55869Sbostic.Fn cgetstr "char *buf" "char *cap" "char **str" 40*55869Sbostic.Ft int 41*55869Sbostic.Fn cgetustr "char *buf" "char *cap" "char **str" 42*55869Sbostic.Ft int 43*55869Sbostic.Fn cgetfirst "char **buf" "char **db_array" 44*55869Sbostic.Ft int 45*55869Sbostic.Fn cgetnext "char **buf" "char **db_array" 46*55869Sbostic.Ft int 47*55869Sbostic.Fn cgetclose "void" 48*55869Sbostic.Sh DESCRIPTION 49*55869Sbostic.Fn Cgetent 50*55869Sbosticextracts the capability rec 51*55869Sbostic.Fa name 52*55869Sbosticfrom the database specified by the NULL terminated file array 53*55869Sbostic.Fa db_array 54*55869Sbosticand returns a pointer to a 55*55869Sbostic.Nm malloc 56*55869Sbostic\'d 57*55869Sbosticcopy of it in 58*55869Sbostic.Fa buf . 59*55869Sbostic.Fa Buf 60*55869Sbosticmust be retained through all subsequent calls to 61*55869Sbostic.Fn cgetmatch , 62*55869Sbostic.Fn cgetcap , 63*55869Sbostic.Fn cgetnum , 64*55869Sbostic.Fn cgetstr , 65*55869Sbosticand 66*55869Sbostic.Fn cgetustr , 67*55869Sbosticbut may then be 68*55869Sbostic.Nm free 69*55869Sbostic\'d. 70*55869SbosticOn success 0 is returned, \-1 if the requested record couldn't be found, 71*55869Sbostic\-2 if a system error was encountered (couldn't open/read a file, etc.) also 72*55869Sbosticsetting 73*55869Sbostic.Va errno , 74*55869Sbosticand \-3 if a potential reference loop is detected (see 75*55869Sbostic.Nm tc= 76*55869Sbosticcomments below). 77*55869Sbostic.Pp 78*55869Sbostic.Nm Cgetset 79*55869Sbosticenables the addition of a character buffer containing a single capability 80*55869Sbosticrecord entry 81*55869Sbosticto the capability database. 82*55869SbosticConceptually, the entry is added as the first ``file'' in the database, and 83*55869Sbosticis therefore searched first on the call to 84*55869Sbostic.Nm cgetent . 85*55869SbosticThe entry is passed in 86*55869Sbostic.Fa ent . 87*55869SbosticIf 88*55869Sbostic.Fa ent 89*55869Sbosticis NULL, the current entry is removed from the database. 90*55869SbosticOn success 0 is returned and \-1 on failure. 91*55869Sbostic.Pp 92*55869Sbostic.Nm Cgetmatch 93*55869Sbosticwill return 0 if 94*55869Sbostic.Fa name 95*55869Sbosticis one of the names of the capability record 96*55869Sbostic.Fa buf , 97*55869Sbostic\-1 if 98*55869Sbosticnot. 99*55869Sbostic.Pp 100*55869Sbostic.Nm Cgetcap 101*55869Sbosticsearches the capability record 102*55869Sbostic.Fa buf 103*55869Sbosticfor the capability 104*55869Sbostic.Fa cap 105*55869Sbosticwith type 106*55869Sbostic.Fa type . 107*55869SbosticA 108*55869Sbostic.Fa type 109*55869Sbosticis specified using any single character. If a colon (`:') is used, an 110*55869Sbosticuntyped capability will be searched for (see below for explanation of 111*55869Sbostictypes). A pointer to the value of 112*55869Sbostic.Fa cap 113*55869Sbosticin 114*55869Sbostic.Fa buf 115*55869Sbosticis returned on success, NULL if the requested capability couldn't be 116*55869Sbosticfound. The end of the capability value is signaled by a `:' or ASCII NUL 117*55869Sbostic(see below for capability database syntax). 118*55869Sbostic.Pp 119*55869Sbostic.Nm Cgetnum 120*55869Sbosticretrieves the value of the numeric capability 121*55869Sbostic.Fa cap 122*55869Sbosticfrom the capability record pointed to by 123*55869Sbostic.Fa buf . 124*55869SbosticThe numeric value is returned in the 125*55869Sbostic.Ft long 126*55869Sbosticpointed to by 127*55869Sbostic.Fa num . 128*55869Sbostic0 is returned on success, \-1 if the requested numeric capability couldn't 129*55869Sbosticbe found. 130*55869Sbostic.Pp 131*55869Sbostic.Nm Cgetstr 132*55869Sbosticretrieves the value of the string capability 133*55869Sbostic.Fa cap 134*55869Sbosticfrom the capability record pointed to by 135*55869Sbostic.Fa buf . 136*55869SbosticA pointer to a decoded, NUL terminated, 137*55869Sbostic.Nm malloc 138*55869Sbostic\'d 139*55869Sbosticcopy of the string is returned in the 140*55869Sbostic.Ft char * 141*55869Sbosticpointed to by 142*55869Sbostic.Fa str . 143*55869SbosticThe number of characters in the decoded string not including the trailing 144*55869SbosticNUL is returned on success, \-1 if the requested string capability couldn't 145*55869Sbosticbe found, \-2 if a system error was encountered (storage allocation 146*55869Sbosticfailure). 147*55869Sbostic.Pp 148*55869Sbostic.Nm Cgetustr 149*55869Sbosticis identical to 150*55869Sbostic.Nm cgetstr 151*55869Sbosticexcept that it does not expand special characters, but rather returns each 152*55869Sbosticcharacter of the capability string literally. 153*55869Sbostic.Pp 154*55869Sbostic.Nm Cgetfirst , 155*55869Sbostic.Nm cgetnext , 156*55869Sbosticand 157*55869Sbostic.Nm cgetclose 158*55869Sbosticcomprise a function group that provides for sequential 159*55869Sbosticaccess of the NULL pointer terminated array of file names, 160*55869Sbostic.Fa db_array . 161*55869Sbostic.Nm Cgetfirst 162*55869Sbosticreturns the first record in the database and resets the access 163*55869Sbosticto the first record. 164*55869Sbostic.Nm Cgetnext 165*55869Sbosticreturns the next record in the database with respect to the 166*55869Sbosticrecord returned by the previous 167*55869Sbostic.Nm cgetfirst 168*55869Sbosticor 169*55869Sbostic.Nm cgetnext 170*55869Sbosticcall. If there is no such previous call, the first record in the database is 171*55869Sbosticreturned. 172*55869Sbostic.Nm Cgetclose 173*55869Sbosticcloses the access and frees any memory and file descriptors being used. 174*55869SbosticEach record is returned in a 175*55869Sbostic.Nm malloc 176*55869Sbostic\'d 177*55869Sbosticcopy pointed to by 178*55869Sbostic.Fa buf . 179*55869Sbostic.Nm Tc 180*55869Sbosticexpansion is done (see 181*55869Sbostic.Nm tc= 182*55869Sbosticcomments below). 183*55869SbosticUpon completion of the database 0 is returned, 1 is returned upon successful 184*55869Sbosticreturn of record with possibly more remaining (we haven't reached the end of 185*55869Sbosticthe database yet), \-1 is returned if an system error occured, and \-2 186*55869Sbosticis returned if a potential reference loop is detected (see 187*55869Sbostic.Nm tc= 188*55869Sbosticcomments below). 189*55869SbosticUpon completion of database (0 return) the database is closed. 190*55869Sbostic.Sh CAPABILITY DATABASE SYNTAX 191*55869SbosticCapability databases are normally ASCII and may be edited with standard 192*55869Sbostictext editors. Blank lines and lines beginning with a `#' are comments 193*55869Sbosticand are ignored. Lines ending with a `\|\e' indicate that the next line 194*55869Sbosticis a continuation of the current line; the `\|\e' and following newline 195*55869Sbosticare ignored. Long lines are usually continued onto several physical 196*55869Sbosticlines by ending each line except the last with a `\|\e'. 197*55869Sbostic.Pp 198*55869SbosticCapability databases consist of a series of records, one per logical 199*55869Sbosticline. Each record contains a variable number of `:'-separated fields 200*55869Sbostic(capabilities). Empty fields consisting entirely of white space 201*55869Sbosticcharacters (spaces and tabs) are ignored. 202*55869Sbostic.Pp 203*55869SbosticThe first capability of each record specifies its names, separated by `|' 204*55869Sbosticcharacters. These names are used to reference records in the database. 205*55869SbosticBy convention, the last name is usually a comment and is not intended as 206*55869Sbostica lookup tag. For example, the 207*55869Sbostic.Nm vt100 208*55869Sbosticrecord from the 209*55869Sbostic.Nm termcap 210*55869Sbosticdatabase begins: 211*55869Sbostic.Pp 212*55869Sbostic.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:" 213*55869Sbostic.Pp 214*55869Sbosticgiving four names that can be used to access the record. 215*55869Sbostic.Pp 216*55869SbosticThe remaining non-empty capabilities describe a set of (name, value) 217*55869Sbosticbindings, consisting of a names optionally followed by a typed values: 218*55869Sbostic.Pp 219*55869Sbostic.Bd -unfilled -offset indent 220*55869Sbostic.ta +\w'nameTvalue 'u 221*55869Sbosticname typeless [boolean] capability \fIname\fP is present [true] 222*55869Sbosticname\fIT\fPvalue capability (\fIname\fP, \fIT\fP) has value \fIvalue\fP 223*55869Sbosticname@ no capability \fIname\fP exists 224*55869Sbosticname\fIT\fP@ capability (\fIname\fP, \fIT\fP) does not exist 225*55869Sbostic.Ed 226*55869Sbostic.Pp 227*55869SbosticNames consist of one or more characters. Names may contain any character 228*55869Sbosticexcept `:', but it's usually best to restrict them to the printable 229*55869Sbosticcharacters and avoid use of graphics like `#', `=', `%', `@', etc. Types 230*55869Sbosticare single characters used to separate capability names from their 231*55869Sbosticassociated typed values. Types may be any character except a `:'. 232*55869SbosticTypically, graphics like `#', `=', `%', etc. are used. Values may be any 233*55869Sbosticnumber of characters and may contain any character except `:'. 234*55869Sbostic.Sh CAPABILITY DATABASE SEMANTICS 235*55869SbosticCapability records describe a set of (name, value) bindings. Names may 236*55869Sbostichave multiple values bound to them. Different values for a name are 237*55869Sbosticdistinguished by their 238*55869Sbostic.Fa types . 239*55869Sbostic.Nm Cgetcap 240*55869Sbosticwill return a pointer to a value of a name given the capability name and 241*55869Sbosticthe type of the value. 242*55869Sbostic.Pp 243*55869SbosticThe types `#' and `=' are conventionally used to denote numeric and 244*55869Sbosticstring typed values, but no restriction on those types is enforced. The 245*55869Sbosticfunctions 246*55869Sbostic.Nm cgetnum 247*55869Sbosticand 248*55869Sbostic.Nm cgetstr 249*55869Sbosticcan be used to implement the traditional syntax and semantics of `#' 250*55869Sbosticand `='. 251*55869SbosticTypeless capabilities are typically used to denote boolean objects with 252*55869Sbosticpresence or absence indicating truth and false values respectively. 253*55869SbosticThis interpretation is conveniently represented by: 254*55869Sbostic.Pp 255*55869Sbostic.Dl "(getcap(buf, name, ':') != NULL)" 256*55869Sbostic.Pp 257*55869SbosticA special capability, 258*55869Sbostic.RI tc= name , 259*55869Sbosticis used to indicate that the record specified by 260*55869Sbostic.Fa name 261*55869Sbosticshould be substituted for the 262*55869Sbostic.Nm tc 263*55869Sbosticcapability. 264*55869Sbostic.Nm Tc 265*55869Sbosticcapabilities may interpolate records which also contain 266*55869Sbostic.Nm tc 267*55869Sbosticcapabilities and more than one 268*55869Sbostic.Nm tc 269*55869Sbosticcapability may be used in a record. A 270*55869Sbostic.Nm tc 271*55869Sbosticexpansion scope (i.e., where the argument is searched for) contains the 272*55869Sbosticfile in which the 273*55869Sbostic.Nm tc 274*55869Sbosticis declared and all subsequent files in the file array. 275*55869Sbostic.Pp 276*55869SbosticWhen a database is searched for a capability record, the first matching 277*55869Sbosticrecord in the search is returned. When an record is scanned for a 278*55869Sbosticcapability, the first matching capability is returned; the capability 279*55869Sbostic.Nm :nameT@: 280*55869Sbosticwill hide any following definition of a value of type 281*55869Sbostic.Nm T 282*55869Sbosticfor 283*55869Sbostic.Fa name ; 284*55869Sbosticand the capability 285*55869Sbostic.Nm :name@: 286*55869Sbosticwill prevent any following values of 287*55869Sbostic.Fa name 288*55869Sbosticfrom being seen. 289*55869Sbostic.Pp 290*55869SbosticThese features combined with 291*55869Sbostic.Nm tc 292*55869Sbosticcapabilities can be used to generate variations of other databases and 293*55869Sbosticrecords by either adding new capabilities, overriding definitions with new 294*55869Sbosticdefinitions, or hiding following definitions via `@' capabilities. 295*55869Sbostic.Sh EXAMPLES 296*55869Sbostic.Bd -unfilled -offset indent 297*55869Sbosticexample\||\|an example of binding multiple values to names:\e 298*55869Sbostic :foo%bar:foo^blah:foo@:\e 299*55869Sbostic :abc%xyz:abc^frap:abc$@:\e 300*55869Sbostic :tc=more: 301*55869Sbostic.Ed 302*55869Sbostic.Pp 303*55869SbosticThe capability foo has two values bound to it (bar of type `%' and blah of 304*55869Sbostictype `^') and any other value bindings are hidden. The capability abc 305*55869Sbosticalso has two values bound but only a value of type `$' is prevented from 306*55869Sbosticbeing defined in the capability record more. 307*55869Sbostic.Pp 308*55869Sbostic.Bd -unfilled -offset indent 309*55869Sbosticfile1: 310*55869Sbostic new\||\|new_record\||\|a modification of "old":\e 311*55869Sbostic :fript=bar:who-cares@:tc=old:blah:tc=extensions: 312*55869Sbosticfile2: 313*55869Sbostic old\||\|old_record\||\|an old database record:\e 314*55869Sbostic :fript=foo:who-cares:glork#200: 315*55869Sbostic.Ed 316*55869Sbostic.Pp 317*55869SbosticThe records are extracted by calling 318*55869Sbostic.Nm cgetent 319*55869Sbosticwith file1 preceding file2. 320*55869SbosticIn the capability record new in file1, fript=bar overrides the definition 321*55869Sbosticof fript=foo interpolated from the capability record old in file2, 322*55869Sbosticwho-cares@ prevents the definition of any who-cares definitions in old 323*55869Sbosticfrom being seen, glork#200 is inherited from old, and blah and anything 324*55869Sbosticdefined by the record extensions is added to those definitions in old. 325*55869SbosticNote that the position of the fript=bar and who-cares@ definitions before 326*55869Sbostictc=old is important here. If they were after, the definitions in old 327*55869Sbosticwould take precedence. 328*55869Sbostic.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS 329*55869SbosticTwo types are predefined by 330*55869Sbostic.Nm cgetnum 331*55869Sbosticand 332*55869Sbostic.Nm cgetstr : 333*55869Sbostic.Pp 334*55869Sbostic.RS 335*55869Sbostic.nf 336*55869Sbostic.ta +\w'name#number 'u 337*55869Sbostic\fIname\fP#\fInumber\fP numeric capability \fIname\fP has value \fInumber\fP 338*55869Sbostic\fIname\fP=\fIstring\fP string capability \fIname\fP has value \fIstring\fP 339*55869Sbostic\fIname\fP#@ the numeric capability \fIname\fP does not exist 340*55869Sbostic\fIname\fP=@ the string capability \fIname\fP does not exist 341*55869Sbostic.DT 342*55869Sbostic.fi 343*55869Sbostic.RE 344*55869Sbostic.Pp 345*55869SbosticNumeric capability values may be given in one of three numeric bases. 346*55869SbosticIf the number starts with either 347*55869Sbostic.Nm 0x 348*55869Sbosticor 349*55869Sbostic.Nm 0X 350*55869Sbosticit is interpreted as a hexadecimal number (both upper and lower case a-f 351*55869Sbosticmay be used to denote the extended hexadecimal digits). 352*55869SbosticOtherwise, if the number starts with a 353*55869Sbostic.Nm 0 354*55869Sbosticit is interpreted as an octal number. 355*55869SbosticOtherwise the number is interpreted as a decimal number. 356*55869Sbostic.Pp 357*55869SbosticString capability values may contain any character. Non-printable ASCII 358*55869Sbosticcodes, new lines, and colons may be conveniently represented by the use 359*55869Sbosticof escape sequences: 360*55869Sbostic.Pp 361*55869Sbostic.RS 362*55869Sbostic.nf 363*55869Sbostic.ta \w'\e\|X, \e\|X 'u +\w'(ASCII octal nnn) 'u 364*55869Sbostic^X ('\fIX\fP' & 037) control-\fIX\fP 365*55869Sbostic\e\|b, \e\|B (ASCII 010) backspace 366*55869Sbostic\e\|t, \e\|T (ASCII 011) tab 367*55869Sbostic\e\|n, \e\|N (ASCII 012) line feed (newline) 368*55869Sbostic\e\|f, \e\|F (ASCII 014) form feed 369*55869Sbostic\e\|r, \e\|R (ASCII 015) carriage return 370*55869Sbostic\e\|e, \e\|E (ASCII 027) escape 371*55869Sbostic\e\|c, \e\|C (:) colon 372*55869Sbostic\e\|\e (\e\|) back slash 373*55869Sbostic\e\|^ (^) caret 374*55869Sbostic\e\|\fInnn\fP (ASCII octal \fInnn\fP) 375*55869Sbostic.DT 376*55869Sbostic.fi 377*55869Sbostic.RE 378*55869Sbostic.Pp 379*55869SbosticA `\|\e' may be followed by up to three octal digits directly specifies 380*55869Sbosticthe numeric code for a character. The use of ASCII NULs, while easily 381*55869Sbosticencoded, causes all sorts of problems and must be used with care since 382*55869SbosticNULs are typically used to denote the end of strings; many applications 383*55869Sbosticuse `\e\|200' to represent a NUL. 384*55869Sbostic.Sh DIAGNOSTICS 385*55869Sbostic.Nm Cgetent , 386*55869Sbostic.Nm cgetset , 387*55869Sbostic.Nm cgetmatch , 388*55869Sbostic.Nm cgetnum , 389*55869Sbostic.Nm cgetstr , 390*55869Sbostic.Nm cgetustr , 391*55869Sbostic.Nm cgetfirst , 392*55869Sbosticand 393*55869Sbostic.Nm cgetnext 394*55869Sbosticreturn a a value greater than or equal to 0 on success and a value less 395*55869Sbosticthan 0 on failure. 396*55869Sbostic.Nm Cgetcap 397*55869Sbosticreturns a character pointer on success and a NULL on failure. 398*55869Sbostic.Pp 399*55869Sbostic.Nm Cgetent , 400*55869Sbosticand 401*55869Sbostic.Nm cgetseq 402*55869Sbosticmay fail and set 403*55869Sbostic.Va errno 404*55869Sbosticfor any of the errors specified for the library functions: 405*55869Sbostic.Xr fopen 2 , 406*55869Sbostic.Xr fclose 2 , 407*55869Sbostic.Xr open 2 , 408*55869Sbosticand 409*55869Sbostic.Xr close 2 . 410*55869Sbostic.Pp 411*55869Sbostic.Nm Cgetent , 412*55869Sbostic.Nm cgetset , 413*55869Sbostic.Nm cgetstr , 414*55869Sbosticand 415*55869Sbostic.Nm cgetustr 416*55869Sbosticmay fail and set 417*55869Sbostic.Va errno 418*55869Sbosticas follows: 419*55869Sbostic.Bl -tag -width Er 420*55869Sbostic.It Bq Er ENOMEM 421*55869SbosticNo memory to allocate. 422*55869Sbostic.El 423*55869Sbostic.Sh SEE ALSO 424*55869Sbostic.Xr malloc 3 425*55869Sbostic.Sh BUGS 426*55869SbosticColons (`:') can't be used in names, types, or values. 427*55869Sbostic.Pp 428*55869SbosticThere are no checks for 429*55869Sbostic.Nm tc= name 430*55869Sbosticloops in 431*55869Sbostic.Nm cgetent . 432