4e257d2002-05-08Martin Nilsson // -*- Pike -*- // // YP (NIS) Functionality for Pike // Per Hedbor & Henrik Grubbström 1996-1997 //
69b2971997-06-09Henrik Grubbström (Grubba) 
a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
bca3001997-06-09Henrik Grubbström (Grubba) inherit @module@;
249dc31999-08-24Fredrik Hübinette (Hubbe) #if constant(@module@.Domain)
bca3001997-06-09Henrik Grubbström (Grubba) // Usual nicknames constant nicknames = ([
20ef1c1997-06-09Henrik Grubbström (Grubba)  "passwd":"passwd.byname", "group":"group.byname", "networks":"networks.byaddr", "hosts":"hosts.byname", "protocols":"protocols.bynumber", "services":"services.byname", "aliases":"mail.aliases",
bca3001997-06-09Henrik Grubbström (Grubba)  "ethers":"ethers.byname" ]);
69b2971997-06-09Henrik Grubbström (Grubba) 
8333902001-04-26Henrik Grubbström (Grubba) //! Network Information Service aka YP map.
63d9661997-06-09Henrik Grubbström (Grubba) class Map {
1c6b121997-06-10Henrik Grubbström (Grubba)  object(Domain) domain;
69b2971997-06-09Henrik Grubbström (Grubba)  string ypmap;
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Create a new YP-map object. //! //! @[map] is the YP-map to bind to. This may be a nickname, such as
8333902001-04-26Henrik Grubbström (Grubba)  //! @tt{passwd@} instead of just @tt{passwd.byname@}.
e7820e2001-01-05Henrik Grubbström (Grubba)  //! //! If @[domain] is not specified, the default domain will be used. //! //! @note //! If there is no YP server available for the domain, this //! function call will block until there is one. If no server appears //! in about ten minutes or so, an error will be returned. The timeout //! is not configurable. //! void create(string map, string|void domain)
69b2971997-06-09Henrik Grubbström (Grubba)  {
e7820e2001-01-05Henrik Grubbström (Grubba)  if (domain) {
7bffb72002-09-21Martin Stjernholm  this_program::domain = Domain(domain);
69b2971997-06-09Henrik Grubbström (Grubba)  } else {
7bffb72002-09-21Martin Stjernholm  this_program::domain = Domain();
69b2971997-06-09Henrik Grubbström (Grubba)  } ypmap = map;
bca3001997-06-09Henrik Grubbström (Grubba)  array err; do { // Force an error if the map does not exist. err = catch{
7bffb72002-09-21Martin Stjernholm  this_program::domain->match(ypmap, "foo");
bca3001997-06-09Henrik Grubbström (Grubba)  return; }; } while (ypmap = nicknames[ypmap]); throw(err);
69b2971997-06-09Henrik Grubbström (Grubba)  }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Search for the key @[key]. If there is no @[key] in the map, 0 (zero) //! will be returned, otherwise the string matching the key will //! be returned. //! //! @note //! @[key] must match exactly, no pattern matching of any kind is done. //!
69b2971997-06-09Henrik Grubbström (Grubba)  string match(string key) { return(domain->match(ypmap, key)); } string `[](string key) {
7d014c1997-06-09Henrik Grubbström (Grubba)  return(domain->match(ypmap, key));
69b2971997-06-09Henrik Grubbström (Grubba)  }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Returns the whole map as a mapping. mapping(string:string) all()
69b2971997-06-09Henrik Grubbström (Grubba)  { return(domain->all(ypmap)); }
7a74cc2014-08-26Per Hedbor 
e7820e2001-01-05Henrik Grubbström (Grubba)  mapping(string:string) cast_to_mapping()
69b2971997-06-09Henrik Grubbström (Grubba)  { return(domain->all(ypmap)); }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Call a function for each entry in the map. //!
56cd002001-10-28Martin Nilsson  //! For each entry in the map, call the function @[fun].
e7820e2001-01-05Henrik Grubbström (Grubba)  //! //! The function will be called like
cbe8c92003-04-07Martin Nilsson  //! @expr{void fun(string key, string value)@}.
e7820e2001-01-05Henrik Grubbström (Grubba)  //! void map(function(string,string:void) fun)
69b2971997-06-09Henrik Grubbström (Grubba)  {
daab992001-01-05Martin Nilsson  domain->map(ypmap, fun);
69b2971997-06-09Henrik Grubbström (Grubba)  }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Return the server that provides this map.
69b2971997-06-09Henrik Grubbström (Grubba)  string server() {
fdd03d1997-06-09Henrik Grubbström (Grubba)  return(domain->server(ypmap));
69b2971997-06-09Henrik Grubbström (Grubba)  }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Return the order number for this map.
69b2971997-06-09Henrik Grubbström (Grubba)  int order() { return(domain->order(ypmap)); }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Return the number of entries in this map.
bca3001997-06-09Henrik Grubbström (Grubba)  int _sizeof()
69b2971997-06-09Henrik Grubbström (Grubba)  {
bca3001997-06-09Henrik Grubbström (Grubba)  return(sizeof(indices(domain->all(ypmap))));
69b2971997-06-09Henrik Grubbström (Grubba)  }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Return the keys of the map.
bca3001997-06-09Henrik Grubbström (Grubba)  array(string) _indices()
69b2971997-06-09Henrik Grubbström (Grubba)  { return(indices(domain->all(ypmap))); }
e7820e2001-01-05Henrik Grubbström (Grubba)  //! Return the values of the map.
bca3001997-06-09Henrik Grubbström (Grubba)  array(string) _values()
69b2971997-06-09Henrik Grubbström (Grubba)  {
7d014c1997-06-09Henrik Grubbström (Grubba)  return(values(domain->all(ypmap))); }
e7820e2001-01-05Henrik Grubbström (Grubba) 
7a74cc2014-08-26Per Hedbor  //! Convert the map to a mapping protected mapping cast(string type)
7d014c1997-06-09Henrik Grubbström (Grubba)  {
e673da2014-08-18Martin Nilsson  if( type=="mapping" ) return cast_to_mapping(); return UNDEFINED;
69b2971997-06-09Henrik Grubbström (Grubba)  }
8333902001-04-26Henrik Grubbström (Grubba) }
249dc31999-08-24Fredrik Hübinette (Hubbe) 
ca7f901999-08-25Henrik Grubbström (Grubba) #endif