templateindex.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /** --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. --- END COPYRIGHT BLOCK --- */
  6. /*
  7. * templateindex.c -- CGI template indexer -- HTTP gateway
  8. */
  9. #include "dsgw.h"
  10. #if defined( XP_WIN32 )
  11. #include <io.h>
  12. struct dirent {
  13. char d_name[1];
  14. };
  15. #else
  16. #include <dirent.h>
  17. #endif
  18. static void build_index();
  19. #if defined( XP_WIN32 )
  20. char **ds_get_file_list( char *dir )
  21. {
  22. char szWildcardFileSpec[MAX_PATH];
  23. char **ret = NULL;
  24. long hFile;
  25. struct _finddata_t fileinfo;
  26. int nfiles = 0;
  27. if( ( dir == NULL ) || (strlen( dir ) == 0) )
  28. return NULL;
  29. if( ( ret = malloc( sizeof( char * ) ) ) == NULL )
  30. return NULL;
  31. strcpy(szWildcardFileSpec, dir);
  32. strcat(szWildcardFileSpec, "/*");
  33. hFile = _findfirst( szWildcardFileSpec, &fileinfo);
  34. if( hFile == -1 )
  35. return NULL;
  36. if( ( strcmp( fileinfo.name, "." ) != 0 ) &&
  37. ( strcmp( fileinfo.name, ".." ) != 0 ) )
  38. {
  39. ret[ nfiles++ ] = strdup( fileinfo.name );
  40. }
  41. while( _findnext( hFile, &fileinfo ) == 0 )
  42. {
  43. if( ( strcmp( fileinfo.name, "." ) != 0 ) &&
  44. ( strcmp( fileinfo.name, ".." ) != 0 ) )
  45. {
  46. if( ( ret = (char **) realloc( ret, sizeof( char * ) * ( nfiles + 1 ) ) ) != NULL )
  47. ret[ nfiles++ ] = strdup( fileinfo.name);
  48. }
  49. }
  50. _findclose( hFile );
  51. ret[ nfiles ] = NULL;
  52. return ret;
  53. }
  54. #endif ( XP_WIN32 )
  55. main( argc, argv, env )
  56. int argc;
  57. char *argv[];
  58. #ifdef DSGW_DEBUG
  59. char *env[];
  60. #endif
  61. {
  62. int reqmethod;
  63. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_GET );
  64. dsgw_send_header();
  65. #ifdef DSGW_DEBUG
  66. dsgw_logstringarray( "env", env );
  67. #endif
  68. dsgw_html_begin( "Directory Server Gateway Template Indexer", 1 );
  69. build_index();
  70. dsgw_html_end();
  71. exit( 0 );
  72. }
  73. static void
  74. build_index()
  75. {
  76. FILE *htmlfp;
  77. #if !defined( XP_WIN32 )
  78. DIR *dirp;
  79. #endif
  80. struct dirent *dep;
  81. char *path, **argv, *classes, *p, line[ BIG_LINE ];
  82. char **filelist;
  83. int errcount, prefixlen, count, argc, filecount = 0;
  84. path = dsgw_file2path( gc->gc_tmpldir, "" );
  85. #if defined( XP_WIN32 )
  86. if (( filelist = ds_get_file_list( path )) == NULL ) {
  87. #else
  88. if (( dirp = opendir( path )) == NULL ) {
  89. #endif
  90. dsgw_error( DSGW_ERR_OPENDIR, path, DSGW_ERROPT_EXIT, 0, NULL );
  91. }
  92. free( path );
  93. prefixlen = strlen( DSGW_CONFIG_DISPLAYPREFIX );
  94. errcount = count = 0;
  95. dsgw_emitf( "Remove any lines that begin with \"template\" from \n" );
  96. dsgw_emitf( "your dsgw.conf file and add these lines:<BR><PRE>\n" );
  97. #if defined( XP_WIN32 )
  98. while( filelist != NULL && filelist[filecount] != NULL ) {
  99. dep = (struct dirent *)filelist[filecount];
  100. #else
  101. while (( dep = readdir( dirp )) != NULL ) {
  102. #endif
  103. if ( strlen( dep->d_name ) > prefixlen && strncasecmp( dep->d_name,
  104. DSGW_CONFIG_DISPLAYPREFIX, prefixlen ) == 0 && strcmp(
  105. ".html", dep->d_name + strlen( dep->d_name ) - 5 ) == 0 ) {
  106. ++count;
  107. htmlfp = dsgw_open_html_file( dep->d_name, DSGW_ERROPT_EXIT );
  108. while ( dsgw_next_html_line( htmlfp, line )) {
  109. if ( dsgw_parse_line( line, &argc, &argv, 1,
  110. dsgw_simple_cond_is_true, NULL )) {
  111. if ( dsgw_directive_is( line, DRCT_DS_OBJECTCLASS )) {
  112. if (( classes = get_arg_by_name( "value", argc, argv ))
  113. == NULL ) {
  114. dsgw_emitf(
  115. "Missing \"value=objectclass\" on line &lt%s<BR>\n", line+1 );
  116. ++errcount;
  117. continue;
  118. }
  119. dsgw_emitf( "template %.*s",
  120. strlen( dep->d_name ) - prefixlen - 5,
  121. dep->d_name + prefixlen );
  122. for ( ; classes != NULL && *classes != '\0';
  123. classes = p ) {
  124. if (( p = strchr( classes, ',' )) != NULL ) {
  125. *p++ = '\0';
  126. while ( ldap_utf8isspace( p )) {
  127. LDAP_UTF8INC(p);
  128. }
  129. }
  130. dsgw_emitf( " %s", classes );
  131. }
  132. dsgw_emits( "\n" );
  133. }
  134. }
  135. }
  136. fclose( htmlfp );
  137. filecount++;
  138. }
  139. }
  140. #if !defined( XP_WIN32 )
  141. closedir( dirp );
  142. #endif
  143. dsgw_emits( "</PRE><H3>Template indexing " );
  144. if ( errcount == 0 ) {
  145. dsgw_emitf( "complete (%d files).<H3>\n", count );
  146. } else {
  147. dsgw_emitf( "failed (%d errors).<H3>\n", errcount );
  148. }
  149. }