]> git.sur5r.net Git - openldap/blob - contrib/whois++/template.c
SASL version check
[openldap] / contrib / whois++ / template.c
1 #if !defined(lint)
2 static char copyright[] = "Copyright 1992 The University of Adelaide";
3 #endif
4
5 /*
6  *                      T E M P L A T E
7  *
8  * Author:      Mark R. Prior
9  *              Communications and Systems Branch
10  *              Information Technology Division
11  *              The University of Adelaide
12  * E-mail:      mrp@itd.adelaide.edu.au
13  * Date:        October 1992
14  * Version:     1.7
15  * Description:
16  *              This module deals with whois++ templates
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that the above copyright notice and this paragraph are
20  * duplicated in all such forms and that any documentation,
21  * advertising materials, and other materials related to such
22  * distribution and use acknowledge that the software was developed
23  * by the University of Adelaide. The name of the University may not
24  * be used to endorse or promote products derived from this software
25  * without specific prior written permission.
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
28  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30
31 #include "whois++.h"
32
33 void    showTemplate( template )
34 char    *template;
35
36 {
37         char    filename[MAXPATHLEN], buffer[BUFSIZ];
38         FILE    *description;
39         int     i;
40
41         if ( template == NULL || *template == '\0' )
42                 return;
43         sprintf( filename, "%s/templates/%s", configDir, template );
44         if ( ( description = fopen( filename, "r" ) ) == NULL ) 
45                 printFormatted( lineLength, TRUE, stdout,
46                         "Cannot find template %s", template );
47         else {
48                 while ( fgets( buffer, BUFSIZ, description ) != NULL ) {
49                         i = strlen( buffer );
50                         while ( i-- > 0 &&
51                                 ( buffer[i] == '\n' || buffer[i] == '\r' ) )
52                                 buffer[i] = '\0';
53                         printFormatted( lineLength, FALSE, stdout,
54                                 " %s", buffer );
55                 }
56                 fclose( description );
57         }
58 }
59
60 void    listTemplates( query )
61 char    *query;
62
63 {
64         char            filename[MAXPATHLEN];
65         DIR             *dir;
66         struct dirent   *entry;
67
68         if ( query == NULL || *query == '\0' ) {
69                 sprintf( filename, "%s/templates", configDir );
70                 if ( ( dir = opendir( filename ) ) == NULL ) {
71                         printFormatted( lineLength, TRUE, stdout,
72                                 "Cannot access template descriptions - %s",
73                                 strerror( errno ) );
74                         return;
75                 }
76                 for ( entry = readdir( dir ); entry != NULL; entry = readdir( dir ) )
77                         if ( !EQ(entry->d_name, "." ) && !EQ(entry->d_name, ".." ) )
78                                 printFormatted( lineLength, FALSE, stdout,
79                                         " %s", lowerCase( entry->d_name ) );
80                 closedir( dir );
81         } else {
82                 sprintf( filename, "%s/templates/%s", configDir, query );
83                 if ( fopen( filename, "r" ) == NULL )
84                         printFormatted( lineLength, TRUE, stdout,
85                                 "No such template (%s)", query );
86                 else
87                         printFormatted( lineLength, FALSE, stdout,
88                                 " %s", query );
89         }
90 }
91
92 char    **specifyAttributes( objectClass )
93 char    *objectClass;
94
95 {
96         FILE    *description;
97         char    filename[MAXPATHLEN], buffer[BUFSIZ];
98         char    **attributes;
99         int     max = ATTRIBUTE_INCREMENT;
100         int     i, number = 0;
101
102         if ( objectClass == NULL || *objectClass == '\0' )
103                 return NULL;
104         sprintf( filename, "%s/templates/%s", configDir,
105                 lowerCase( objectClass ) );
106         if ( ( description = fopen( filename, "r" ) ) == NULL ) 
107                 return NULL;
108         if ( ( attributes = (char **)malloc( max*sizeof(char *) ) ) == NULL ) {
109                 printFormatted( lineLength, TRUE, stdout,
110                         "Error while attempting to create attribute list - %s",
111                         strerror( errno ) );
112                 return NULL;
113         }
114         while ( fgets( buffer, BUFSIZ, description ) != NULL ) {
115                 i = strlen( buffer );
116                 while ( i-- > 0 && ( buffer[i] == '\n' || buffer[i] == '\r' ) )
117                         buffer[i] = '\0';
118                 attributes[number++] = strdup( buffer );
119                 if ( number == max ) {
120                         max += ATTRIBUTE_INCREMENT;
121                         if ( ( attributes = (char **)realloc( attributes, max*sizeof(char *)) ) == NULL ) {
122                                 printFormatted( lineLength, TRUE, stdout,
123                                         "Error while attempting to extend attribute list - %s",
124                                         strerror( errno ) );
125                                 return NULL;
126                         }
127                 }
128         }
129         attributes[number] = NULL;
130         fclose( description );
131         return attributes;
132 }
133
134 char    *templateToObjectClass( template )
135 char    *template;
136
137 {
138         int     i;
139
140         if ( template == NULL || *template == '\0' ) {
141                 printFormatted( lineLength, TRUE, stdout,
142                         "Unrecognised template" );
143                 return "unrecognised";
144         }
145         for ( i = 0; i < numberOfTemplates; i++ )
146                 if ( EQ( template, templateTranslationTable[i].key ) )
147                         return templateTranslationTable[i].value;
148         printFormatted( lineLength, TRUE, stdout,
149                 "Template (%s) not recognised, assuming that it is already an objectClass",
150                 template );
151         return template;
152 }
153
154 char    *objectClassToTemplate( objectClass )
155 char    *objectClass;
156
157 {
158         int     i;
159
160         if ( objectClass == NULL || *objectClass == '\0' ) {
161                 printFormatted( lineLength, TRUE, stdout,
162                         "Unrecognised template" );
163                 return "unrecognised";
164         }
165         for ( i = 0; i < numberOfTemplates; i++ )
166                 if ( EQ( objectClass, templateTranslationTable[i].value ) )
167                         return templateTranslationTable[i].key;
168         return objectClass;
169 }