1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
/* 
 * $Id: module.pmod.in,v 1.13 2001/01/05 21:38:10 nilsson Exp $ 
 * 
 * YP (NIS) Functionality for Pike 
 * 
 * Per Hedbor & Henrik Grubbström 1996-1997 
 */ 
 
#pike __REAL_VERSION__ 
inherit @module@; 
 
#if constant(@module@.Domain) 
 
// Usual nicknames 
constant nicknames = ([ 
  "passwd":"passwd.byname", 
  "group":"group.byname", 
  "networks":"networks.byaddr", 
  "hosts":"hosts.byname", 
  "protocols":"protocols.bynumber", 
  "services":"services.byname", 
  "aliases":"mail.aliases", 
  "ethers":"ethers.byname" 
]); 
 
class Map { 
  object(Domain) domain; 
  string ypmap; 
 
  //! Create a new YP-map object. 
  //! 
  //! @[map] is the YP-map to bind to. This may be a nickname, such as 
  //! @tt{passwd} instead of just @tt{passwd.byname}. 
  //! 
  //! 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) 
  { 
    if (domain) { 
      local::domain = Domain(domain); 
    } else { 
      local::domain = Domain(); 
    } 
    ypmap = map; 
    array err; 
    do { 
      // Force an error if the map does not exist. 
      err = catch{ 
        local::domain->match(ypmap, "foo"); 
        return; 
      }; 
    } while (ypmap = nicknames[ypmap]); 
    throw(err); 
  } 
 
  //! 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. 
  //! 
  string match(string key) 
  { 
    return(domain->match(ypmap, key)); 
  } 
  string `[](string key) 
  { 
    return(domain->match(ypmap, key)); 
  } 
 
  //! Returns the whole map as a mapping. 
  mapping(string:string) all() 
  { 
    return(domain->all(ypmap)); 
  } 
  mapping(string:string) cast_to_mapping() 
  { 
    return(domain->all(ypmap)); 
  } 
 
  //! Call a function for each entry in the map. 
  //! 
  //! For each entry in the map, call the function @[fun()]. 
  //! 
  //! The function will be called like 
  //!   @tt{void fun(string key, string value)@}. 
  //! 
  void map(function(string,string:void) fun) 
  { 
    domain->map(ypmap, fun); 
  } 
 
  //! Return the server that provides this map. 
  string server() 
  { 
    return(domain->server(ypmap)); 
  } 
 
  //! Return the order number for this map. 
  int order() 
  { 
    return(domain->order(ypmap)); 
  } 
 
  //! Return the number of entries in this map. 
  int _sizeof() 
  { 
    return(sizeof(indices(domain->all(ypmap)))); 
  } 
 
  //! Return the keys of the map. 
  array(string) _indices() 
  { 
    return(indices(domain->all(ypmap))); 
  } 
 
  //! Return the values of the map. 
  array(string) _values() 
  { 
    return(values(domain->all(ypmap))); 
  } 
 
  mixed cast(string type) 
  { 
    switch(type) { 
    case "mapping": 
      return(cast_to_mapping()); 
    default: 
      throw(({ "Yp.YpDomain: Cast to " + type + " not supported.\n", 
                 backtrace() })); 
    } 
  } 
}; 
 
#endif