📄 disptmpl.c
字号:
/*
* Copyright (c) 1993, 1994 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*
* disptmpl.c: display template library routines for LDAP clients
* 7 March 1994 by Mark C Smith
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#ifdef MACOS
#include "macos.h"
#else /* MACOS */
#if defined(DOS) || defined(WIN32) /* marcd */
#include <malloc.h>
#ifdef DOS
#include "msdos.h"
#endif
#else /* DOS */
#include <sys/types.h>
#include <sys/file.h>
#ifndef VMS
#include <unistd.h>
#endif /* VMS */
#endif /* DOS */
#endif /* MACOS */
#include "lber.h"
#include "ldap.h"
#include "disptmpl.h"
#ifndef NEEDPROTOS
static void free_disptmpl();
static int read_next_tmpl();
int next_line_tokens();
void free_strarray();
#else /* !NEEDPROTOS */
static void free_disptmpl( struct ldap_disptmpl *tmpl );
static int read_next_tmpl( char **bufp, long *blenp,
struct ldap_disptmpl **tmplp, int dtversion );
int next_line_tokens( char **bufp, long *blenp, char ***toksp );
void free_strarray( char **sap );
#endif /* !NEEDPROTOS */
static char *tmploptions[] = {
"addable", "modrdn",
"altview",
NULL
};
static unsigned long tmploptvals[] = {
LDAP_DTMPL_OPT_ADDABLE, LDAP_DTMPL_OPT_ALLOWMODRDN,
LDAP_DTMPL_OPT_ALTVIEW,
};
static char *itemtypes[] = {
"cis", "mls", "dn",
"bool", "jpeg", "jpegbtn",
"fax", "faxbtn", "audiobtn",
"time", "date", "url",
"searchact", "linkact", "adddnact",
"addact", "verifyact", "mail",
NULL
};
static unsigned long itemsynids[] = {
LDAP_SYN_CASEIGNORESTR, LDAP_SYN_MULTILINESTR, LDAP_SYN_DN,
LDAP_SYN_BOOLEAN, LDAP_SYN_JPEGIMAGE, LDAP_SYN_JPEGBUTTON,
LDAP_SYN_FAXIMAGE, LDAP_SYN_FAXBUTTON, LDAP_SYN_AUDIOBUTTON,
LDAP_SYN_TIME, LDAP_SYN_DATE, LDAP_SYN_LABELEDURL,
LDAP_SYN_SEARCHACTION, LDAP_SYN_LINKACTION, LDAP_SYN_ADDDNACTION,
LDAP_SYN_ADDDNACTION, LDAP_SYN_VERIFYDNACTION,LDAP_SYN_RFC822ADDR,
};
static char *itemoptions[] = {
"ro", "sort",
"1val", "hide",
"required", "hideiffalse",
NULL
};
static unsigned long itemoptvals[] = {
LDAP_DITEM_OPT_READONLY, LDAP_DITEM_OPT_SORTVALUES,
LDAP_DITEM_OPT_SINGLEVALUED, LDAP_DITEM_OPT_HIDEIFEMPTY,
LDAP_DITEM_OPT_VALUEREQUIRED, LDAP_DITEM_OPT_HIDEIFFALSE,
};
#define ADDEF_CONSTANT "constant"
#define ADDEF_ADDERSDN "addersdn"
int
ldap_init_templates( char *file, struct ldap_disptmpl **tmpllistp )
{
FILE *fp;
char *buf;
long rlen, len;
int rc, eof;
*tmpllistp = NULLDISPTMPL;
if (( fp = fopen( file, "r" )) == NULL ) {
return( LDAP_TMPL_ERR_FILE );
}
if ( fseek( fp, 0L, SEEK_END ) != 0 ) { /* move to end to get len */
fclose( fp );
return( LDAP_TMPL_ERR_FILE );
}
len = ftell( fp );
if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { /* back to start of file */
fclose( fp );
return( LDAP_TMPL_ERR_FILE );
}
if (( buf = malloc( (size_t)len )) == NULL ) {
fclose( fp );
return( LDAP_TMPL_ERR_MEM );
}
rlen = fread( buf, 1, (size_t)len, fp );
eof = feof( fp );
fclose( fp );
if ( rlen != len && !eof ) { /* error: didn't get the whole file */
free( buf );
return( LDAP_TMPL_ERR_FILE );
}
rc = ldap_init_templates_buf( buf, rlen, tmpllistp );
free( buf );
return( rc );
}
int
ldap_init_templates_buf( char *buf, long buflen,
struct ldap_disptmpl **tmpllistp )
{
int rc, version;
char **toks;
struct ldap_disptmpl *prevtmpl, *tmpl;
*tmpllistp = prevtmpl = NULLDISPTMPL;
if ( next_line_tokens( &buf, &buflen, &toks ) != 2 ||
strcasecmp( toks[ 0 ], "version" ) != 0 ) {
free_strarray( toks );
return( LDAP_TMPL_ERR_SYNTAX );
}
version = atoi( toks[ 1 ] );
free_strarray( toks );
if ( version != LDAP_TEMPLATE_VERSION ) {
return( LDAP_TMPL_ERR_VERSION );
}
while ( buflen > 0 && ( rc = read_next_tmpl( &buf, &buflen, &tmpl,
version )) == 0 && tmpl != NULLDISPTMPL ) {
if ( prevtmpl == NULLDISPTMPL ) {
*tmpllistp = tmpl;
} else {
prevtmpl->dt_next = tmpl;
}
prevtmpl = tmpl;
}
if ( rc != 0 ) {
ldap_free_templates( *tmpllistp );
}
return( rc );
}
void
ldap_free_templates( struct ldap_disptmpl *tmpllist )
{
struct ldap_disptmpl *tp, *nexttp;
if ( tmpllist != NULL ) {
for ( tp = tmpllist; tp != NULL; tp = nexttp ) {
nexttp = tp->dt_next;
free_disptmpl( tp );
}
}
}
static void
free_disptmpl( struct ldap_disptmpl *tmpl )
{
if ( tmpl != NULL ) {
if ( tmpl->dt_name != NULL ) {
free( tmpl->dt_name );
}
if ( tmpl->dt_pluralname != NULL ) {
free( tmpl->dt_pluralname );
}
if ( tmpl->dt_iconname != NULL ) {
free( tmpl->dt_iconname );
}
if ( tmpl->dt_authattrname != NULL ) {
free( tmpl->dt_authattrname );
}
if ( tmpl->dt_defrdnattrname != NULL ) {
free( tmpl->dt_defrdnattrname );
}
if ( tmpl->dt_defaddlocation != NULL ) {
free( tmpl->dt_defaddlocation );
}
if ( tmpl->dt_oclist != NULL ) {
struct ldap_oclist *ocp, *nextocp;
for ( ocp = tmpl->dt_oclist; ocp != NULL; ocp = nextocp ) {
nextocp = ocp->oc_next;
free_strarray( ocp->oc_objclasses );
free( ocp );
}
}
if ( tmpl->dt_adddeflist != NULL ) {
struct ldap_adddeflist *adp, *nextadp;
for ( adp = tmpl->dt_adddeflist; adp != NULL; adp = nextadp ) {
nextadp = adp->ad_next;
if( adp->ad_attrname != NULL ) {
free( adp->ad_attrname );
}
if( adp->ad_value != NULL ) {
free( adp->ad_value );
}
free( adp );
}
}
if ( tmpl->dt_items != NULL ) {
struct ldap_tmplitem *rowp, *nextrowp, *colp, *nextcolp;
for ( rowp = tmpl->dt_items; rowp != NULL; rowp = nextrowp ) {
nextrowp = rowp->ti_next_in_col;
for ( colp = rowp; colp != NULL; colp = nextcolp ) {
nextcolp = colp->ti_next_in_row;
if ( colp->ti_attrname != NULL ) {
free( colp->ti_attrname );
}
if ( colp->ti_label != NULL ) {
free( colp->ti_label );
}
if ( colp->ti_args != NULL ) {
free_strarray( colp->ti_args );
}
free( colp );
}
}
}
free( tmpl );
}
}
struct ldap_disptmpl *
ldap_first_disptmpl( struct ldap_disptmpl *tmpllist )
{
return( tmpllist );
}
struct ldap_disptmpl *
ldap_next_disptmpl( struct ldap_disptmpl *tmpllist,
struct ldap_disptmpl *tmpl )
{
return( tmpl == NULLDISPTMPL ? tmpl : tmpl->dt_next );
}
struct ldap_disptmpl *
ldap_name2template( char *name, struct ldap_disptmpl *tmpllist )
{
struct ldap_disptmpl *dtp;
for ( dtp = ldap_first_disptmpl( tmpllist ); dtp != NULLDISPTMPL;
dtp = ldap_next_disptmpl( tmpllist, dtp )) {
if ( strcasecmp( name, dtp->dt_name ) == 0 ) {
return( dtp );
}
}
return( NULLDISPTMPL );
}
struct ldap_disptmpl *
ldap_oc2template( char **oclist, struct ldap_disptmpl *tmpllist )
{
struct ldap_disptmpl *dtp;
struct ldap_oclist *oclp;
int i, j, needcnt, matchcnt;
if ( tmpllist == NULL || oclist == NULL || oclist[ 0 ] == NULL ) {
return( NULLDISPTMPL );
}
for ( dtp = ldap_first_disptmpl( tmpllist ); dtp != NULLDISPTMPL;
dtp = ldap_next_disptmpl( tmpllist, dtp )) {
for ( oclp = dtp->dt_oclist; oclp != NULLOCLIST;
oclp = oclp->oc_next ) {
needcnt = matchcnt = 0;
for ( i = 0; oclp->oc_objclasses[ i ] != NULL; ++i ) {
for ( j = 0; oclist[ j ] != NULL; ++j ) {
if ( strcasecmp( oclist[ j ], oclp->oc_objclasses[ i ] )
== 0 ) {
++matchcnt;
}
}
++needcnt;
}
if ( matchcnt == needcnt ) {
return( dtp );
}
}
}
return( NULLDISPTMPL );
}
struct ldap_tmplitem *
ldap_first_tmplrow( struct ldap_disptmpl *tmpl )
{
return( tmpl->dt_items );
}
struct ldap_tmplitem *
ldap_next_tmplrow( struct ldap_disptmpl *tmpl, struct ldap_tmplitem *row )
{
return( row == NULLTMPLITEM ? row : row->ti_next_in_col );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -