1 /* oidm.c - object identifier macro routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
28 static LDAP_STAILQ_HEAD(OidMacroList, slap_oid_macro) om_list
29 = LDAP_STAILQ_HEAD_INITIALIZER(om_list);
31 /* Replace an OID Macro invocation with its full numeric OID.
32 * If the macro is used with "macroname:suffix" append ".suffix"
40 /* OID macros must start alpha */
41 if ( OID_LEADCHAR( *oid ) ) {
45 LDAP_STAILQ_FOREACH( om, &om_list, som_next ) {
46 BerVarray names = om->som_names;
52 for( ; !BER_BVISNULL( names ) ; names++ ) {
53 int pos = dscompare(names->bv_val, oid, ':');
56 int suflen = strlen(oid + pos);
57 char *tmp = SLAP_MALLOC( om->som_oid.bv_len
60 Debug( LDAP_DEBUG_ANY,
61 "oidm_find: SLAP_MALLOC failed", 0, 0, 0 );
64 strcpy(tmp, om->som_oid.bv_val);
66 suflen = om->som_oid.bv_len;
68 strcpy(tmp+suflen, oid+pos+1);
81 while( !LDAP_STAILQ_EMPTY( &om_list )) {
82 om = LDAP_STAILQ_FIRST( &om_list );
83 LDAP_STAILQ_REMOVE_HEAD( &om_list, som_next );
85 ber_bvarray_free(om->som_names);
86 ber_bvarray_free(om->som_subs);
87 free(om->som_oid.bv_val);
107 fprintf( stderr, "%s: line %d: too many arguments\n",
109 usage: fprintf( stderr, "\tObjectIdentifier <name> <oid>\n");
113 oid = oidm_find( argv[1] );
117 "ObjectIdentifier \"%s\" previously defined \"%s\"",
118 fname, lineno, argv[1], oid );
122 om = (OidMacro *) SLAP_CALLOC( sizeof(OidMacro), 1 );
124 Debug( LDAP_DEBUG_ANY, "parse_oidm: SLAP_CALLOC failed", 0, 0, 0 );
128 om->som_names = NULL;
130 ber_str2bv( argv[1], 0, 1, &bv );
131 ber_bvarray_add( &om->som_names, &bv );
132 ber_str2bv( argv[2], 0, 1, &bv );
133 ber_bvarray_add( &om->som_subs, &bv );
134 om->som_oid.bv_val = oidm_find( argv[2] );
136 if (!om->som_oid.bv_val) {
137 fprintf( stderr, "%s: line %d: OID %s not recognized\n",
138 fname, lineno, argv[2] );
142 if (om->som_oid.bv_val == argv[2]) {
143 om->som_oid.bv_val = ch_strdup( argv[2] );
146 om->som_oid.bv_len = strlen( om->som_oid.bv_val );
148 om->som_flags |= SLAP_OM_HARDCODE;
150 LDAP_STAILQ_INSERT_TAIL( &om_list, om, som_next );
151 if ( rom ) *rom = om;
155 void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
159 struct berval *bva = NULL, idx;
163 start = LDAP_STAILQ_FIRST( &om_list );
165 /* count the result size */
167 for ( om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
168 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) continue;
169 for ( j=0; !BER_BVISNULL(&om->som_names[j]); j++ );
171 if ( om == end ) break;
176 bva = ch_malloc( (num+1) * sizeof(struct berval) );
177 BER_BVZERO( bva+num );
183 for ( i=0,om=start; om; om=LDAP_STAILQ_NEXT(om, som_next)) {
184 if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) continue;
185 for ( j=0; !BER_BVISNULL(&om->som_names[j]); i++,j++ ) {
187 idx.bv_len = sprintf(idx.bv_val, "{%d}", i );
189 bva[i].bv_len = idx.bv_len + om->som_names[j].bv_len +
190 om->som_subs[j].bv_len + 1;
191 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
192 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
193 ptr = lutil_strcopy( ptr, om->som_names[j].bv_val );
195 strcpy( ptr, om->som_subs[j].bv_val );
198 if ( om == end ) break;