]> git.sur5r.net Git - openldap/blob - contrib/whois++/template.c
Update for Alpha3 from -devel as of OPENLDAP_DEVEL_981116.
[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 #include <stdlib.h>
33
34
35 void
36 showTemplate( char *template )
37 {
38         char    filename[MAXPATHLEN], buffer[BUFSIZ];
39         FILE    *description;
40         int     i;
41
42         if ( template == NULL || *template == '\0' )
43                 return;
44         sprintf( filename, "%s/templates/%s", configDir, template );
45         if ( ( description = fopen( filename, "r" ) ) == NULL ) 
46                 printFormatted( lineLength, TRUE, stdout,
47                         "Cannot find template %s", template );
48         else {
49                 while ( fgets( buffer, BUFSIZ, description ) != NULL ) {
50                         i = strlen( buffer );
51                         while ( i-- > 0 &&
52                                 ( buffer[i] == '\n' || buffer[i] == '\r' ) )
53                                 buffer[i] = '\0';
54                         printFormatted( lineLength, FALSE, stdout,
55                                 " %s", buffer );
56                 }
57                 fclose( description );
58         }
59 }
60
61 void
62 listTemplates( char *query )
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 **
93 specifyAttributes( char *objectClass )
94 {
95         FILE    *description;
96         char    filename[MAXPATHLEN], buffer[BUFSIZ];
97         char    **attributes;
98         int     max = ATTRIBUTE_INCREMENT;
99         int     i, number = 0;
100
101         if ( objectClass == NULL || *objectClass == '\0' )
102                 return NULL;
103         sprintf( filename, "%s/templates/%s", configDir,
104                 lowerCase( objectClass ) );
105         if ( ( description = fopen( filename, "r" ) ) == NULL ) 
106                 return NULL;
107         if ( ( attributes = (char **)malloc( max*sizeof(char *) ) ) == NULL ) {
108                 printFormatted( lineLength, TRUE, stdout,
109                         "Error while attempting to create attribute list - %s",
110                         strerror( errno ) );
111                 return NULL;
112         }
113         while ( fgets( buffer, BUFSIZ, description ) != NULL ) {
114                 i = strlen( buffer );
115                 while ( i-- > 0 && ( buffer[i] == '\n' || buffer[i] == '\r' ) )
116                         buffer[i] = '\0';
117                 attributes[number++] = strdup( buffer );
118                 if ( number == max ) {
119                         max += ATTRIBUTE_INCREMENT;
120                         if ( ( attributes = (char **)realloc( attributes, max*sizeof(char *)) ) == NULL ) {
121                                 printFormatted( lineLength, TRUE, stdout,
122                                         "Error while attempting to extend attribute list - %s",
123                                         strerror( errno ) );
124                                 return NULL;
125                         }
126                 }
127         }
128         attributes[number] = NULL;
129         fclose( description );
130         return attributes;
131 }
132
133 char *
134 templateToObjectClass( char *template )
135 {
136         int     i;
137
138         if ( template == NULL || *template == '\0' ) {
139                 printFormatted( lineLength, TRUE, stdout,
140                         "Unrecognised template" );
141                 return "unrecognised";
142         }
143         for ( i = 0; i < numberOfTemplates; i++ )
144                 if ( EQ( template, templateTranslationTable[i].key ) )
145                         return templateTranslationTable[i].value;
146         printFormatted( lineLength, TRUE, stdout,
147                 "Template (%s) not recognised, assuming that it is already an objectClass",
148                 template );
149         return template;
150 }
151
152 char *
153 objectClassToTemplate( char *objectClass )
154 {
155         int     i;
156
157         if ( objectClass == NULL || *objectClass == '\0' ) {
158                 printFormatted( lineLength, TRUE, stdout,
159                         "Unrecognised template" );
160                 return "unrecognised";
161         }
162         for ( i = 0; i < numberOfTemplates; i++ )
163                 if ( EQ( objectClass, templateTranslationTable[i].value ) )
164                         return templateTranslationTable[i].key;
165         return objectClass;
166 }