1 /* $Id$
2    Written 1999 by Tobias Ernst and released do the Public Domain.
3    This file is part of NLTOOLS, the nodelist processor of the Husky fidonet
4    software project.
5 
6    Fidouserlist compiler.
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 
13 #include <fidoconf/fidoconf.h>
14 #include <huskylib/huskylib.h>
15 #include <huskylib/log.h>
16 
17 #include "ulc.h"
18 #if !(defined(_MSC_VER) && (_MSC_VER >= 1200))
19 # include <huskylib/dirlayer.h>
20 #endif
21 #include "nlstring.h"
22 #include "nlfind.h"
23 #include "version.h"
24 
process(s_fidoconfig * config)25 int process( s_fidoconfig * config )
26 {
27   unsigned int i;
28   int rv = 0;
29   char *fidouserlist, *nodelist;
30   FILE *fin, *fout;
31   fin = fout = NULL;
32 
33   if( config->fidoUserList == NULL )
34   {
35     w_log( LL_CRIT, "No fido user list configured in fidoconfig." );
36     return 8;
37   }
38 
39   if( config->nodelistDir == NULL )
40   {
41     w_log( LL_CRIT, "No nodelist directory configured in fidoconfig." );
42     return 8;
43   }
44 
45 
46   if( config->nodelistCount < 1 )
47   {
48     w_log( LL_CRIT, "No nodelist configured in fidoconfig." );
49     return 8;
50   }
51 
52   fidouserlist = malloc( strlen( config->nodelistDir ) + strlen( config->fidoUserList ) + 1 );
53   if( fidouserlist == NULL )
54   {
55     w_log( LL_CRIT, "Out of memory." );
56     return 8;
57   }
58 
59   strcpy( fidouserlist, config->nodelistDir );
60   strcat( fidouserlist, config->fidoUserList );
61 
62 
63   fout = fopen( fidouserlist, "w+b" );
64   if( fout == NULL )
65   {
66     w_log( LL_CRIT, "Cannot open %s: %s", fidouserlist, strerror( errno ) );
67     free( fidouserlist );
68     return 8;
69   }
70 
71   w_log( LL_INFO, "Building %s", fidouserlist );
72 
73   for( i = 0; i < config->nodelistCount; i++ )
74   {
75     nodelist = findNodelist( config, i );
76 
77     if( nodelist == NULL )
78     {
79       w_log( LL_ALERT, "No instance of nodelist %s found.", config->nodelists[i].nodelistName );
80       if( rv < 4 )
81         rv = 4;
82     }
83     else
84     {
85       w_log( LL_INFO, "Using %s", nodelist );
86 
87       fin = fopen( nodelist, "rb" );
88       if( fin == NULL )
89       {
90         w_log( LL_ERROR, "Error opening %s: %s", nodelist, strerror( errno ) );
91         if( rv < 8 )
92           rv = 8;
93       }
94       else
95       {
96         int format;
97 
98         switch ( config->nodelists[i].format )
99         {
100         case points4d:
101           format = F_POINTS4D;
102           break;
103         case points24:
104           format = F_POINTS24;
105           break;
106         case fts5000:
107         default:
108           format = F_NODELIST;
109         }
110 
111         if( !ul_compile( fin, fout, format, config->nodelists[i].defaultZone ) )
112         {
113           w_log( LL_ERROR, "Error during compile" );
114           if( rv < 8 )
115             rv = 8;
116         }
117       }
118     }
119     free( nodelist );
120   }
121 
122   w_log( LL_INFO, "Sorting" );
123   if( !ul_sort( fout ) )
124   {
125     w_log( LL_ERROR, "Error while sorting" );
126     if( rv < 8 )
127       rv = 8;
128   }
129 
130   if( fin )
131     fclose( fin );
132   fclose( fout );
133   return rv;
134 }
135 
usage(void)136 void usage( void )
137 {
138   printf( "Usage:\n"
139           "ulc [-hvq] [-c config]\n"
140           "where\n"
141           "      -h         print this help and exit\n"
142           "      -v         print version information\n"
143           "      -q         quiet mode\n" "      -c config  specify fidoconfig file\n" );
144 }
145 
main(int argc,char ** argv)146 int main( int argc, char **argv )
147 {
148   s_fidoconfig *config;
149   int rv, i, flag_quiet = 0;
150   char *versionStr, *configfile = NULL;
151   versionStr = GenVersionStr( "ulc", VER_MAJOR, VER_MINOR, VER_PATCH, VER_BRANCH, cvs_date );
152   for( i = 1; i < argc; i++ )
153   {
154     int j, plen;
155     if( argv[i][0] == '-' )
156     {
157       int plen = sstrlen( argv[i] );
158       for( j = 1; j < plen; j++ )
159         switch ( argv[i][j] )
160         {
161         case 'h':
162           printversion(  );
163           usage(  );
164           return 0;
165         case 'v':
166           printversion(  );
167           return 0;
168         case 'c':
169           if( plen > ++j )
170           { configfile = argv[i] + j;
171             j=plen;
172           }
173           else if( argc > ++i )
174             configfile = argv[i];
175           else
176           {
177             w_log( LL_ERR, "Fatal: parameter after -c is required\n" );
178             return 1;
179           }
180           break;
181         case 'q':
182           flag_quiet = 1;
183         }
184     }
185   }
186 
187   if( !flag_quiet )
188     printversion(  );
189   config = readConfig( configfile );
190   if( config != NULL )
191   {
192     {
193       char errloglevels[3] = { LL_ERR, LL_CRIT, '\0' };
194       initLog( config->logFileDir, config->logEchoToScreen, config->loglevels,
195                flag_quiet? errloglevels : config->screenloglevels );
196     }
197     openLog( LOGNAME, versionStr );
198     w_log( LL_START, "Start" );
199     rv = process( config );
200     w_log( LL_STOP, "End" );
201     closeLog(  );
202     disposeConfig( config );
203     return rv;
204   }
205   else
206   {
207     w_log( LL_ERR, "Cannot open fidoconfig.\n" );
208     return 8;
209   }
210 }
211