7a77ed70483d8365eedec19cc784bfaa0255736e
[bcm963xx.git] / userapps / opensource / atm2684 / atm / lane / lane.c
1 /*
2  * Lan Emulation Server
3  *
4  * $Id: lane.c,v 1.21 1995/11/15 08:27:11 carnil Exp $
5  *
6  */
7
8 /* System includes */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <getopt.h>
13
14 /* Local includes */
15 #include "units.h"
16 #include "load.h"
17 #include "dump.h"
18 #include "mem.h"
19 #include "connect.h"
20 #include "events.h"
21 #include "timers.h"
22
23 /* Type definitions */
24
25 /* Local function prototypes */
26 static void main_init1(void);
27 static void main_release(void);
28 static void parse_args(int argc, char **argv);
29 static void usage(void);
30 static int dump_handler(const Event_t *event, void *funcdata);
31 static int exit_handler(const Event_t *event, void *funcdata);
32
33 /* Data */
34 static const char *rcsid = "$Id: lane.c,v 1.21 1995/11/15 08:27:11 carnil Exp $";
35 const Unit_t main_unit = {
36   "main",
37   NULL,
38   main_init1,
39   NULL,
40   main_release
41 };
42
43 static const char *progname;
44 char *var_file = NULL;
45
46 /* Functions */
47
48 /* Initialization for data that needs other units */
49 static void
50 main_init1(void)
51 {
52   set_var_str(&main_unit, "version", rcsid);
53   add_event_handler(CE_DUMP, &dump_handler, "dump_handler", NULL);
54   add_event_handler(CE_EXIT, &exit_handler, "exit_handler", NULL);
55   Debug_unit(&main_unit, "Initialized.");
56 }
57
58 static void 
59 main_release(void)
60 {
61   if (var_file) {
62     mem_free(&main_unit, var_file);
63     var_file = NULL;
64   }
65 }
66
67 /* Main loop */
68 int
69 main(int argc, char **argv)
70 {
71   short do_restart = 0;
72   const Event_t *event;
73   const Unit_t **units;
74
75   /* Debugging facility */
76   dump_type = DT_STDERR;
77
78   while (1) {
79     /* Call phase 0 initializers */
80     Debug_unit(&main_unit, "Calling phase 0 initializers");
81     FOR_ALL_UNITS(units) {
82       if ((*units)->init0 != NULL) {
83         Debug_unit(&main_unit, "Initializing %s", (*units)->name);
84         (*((*units)->init0))();
85       }
86     }
87
88     /* Get flags from command line */
89     parse_args(argc, argv);
90
91     /* Call phase 1 initializers */
92     Debug_unit(&main_unit, "Calling phase 1 initializers");
93     FOR_ALL_UNITS(units) {
94       if ((*units)->init1 != NULL) {
95         Debug_unit(&main_unit, "Initializing %s", (*units)->name);
96         (*((*units)->init1))();
97       }
98     }
99
100     do_restart = 0;
101
102     while (!do_restart) {
103       event = event_get_next();
104       if (dispatch_handlers(event) == 1) {
105         continue;
106       }
107       switch (event->type) {
108       case CE_RESTART:
109         do_restart = 1;
110         break;
111       case CE_EXIT:
112         break;
113       case CE_DUMP:
114         break;
115       case CE_SVC_OPEN:
116         break;
117       case CE_SVC_CLOSE:
118         break;
119       case CE_DATA:
120         break;
121       case CE_TIMER:
122         break;
123       }
124       mem_free(&main_unit, event);
125     }
126     /* Restart */
127     Debug_unit(&main_unit, "Releasing %d units", num_units);
128     FOR_ALL_UNITS_REV(units) {
129       if ((*units)->release != NULL) {
130         Debug_unit(&main_unit, "Releasing %s", (*units)->name);
131         (*((*units)->release))();
132       }
133     }
134   }
135   return 0;
136 }
137
138 /* Process CE_DUMP events */
139 static int
140 dump_handler(const Event_t *event, void *funcdata)
141 {
142   const Unit_t **units;
143
144   mem_free(&main_unit, event);
145   FOR_ALL_UNITS(units) {
146     if ((*units)->dump != NULL) {
147       Debug_unit(&main_unit, "Dumping %s", (*units)->name);
148       (*((*units)->dump))();
149     }
150   }
151   return 1;
152 }
153
154 /* Process CE_EXIT events */
155 static int
156 exit_handler(const Event_t *event, void *funcdata)
157 {
158   const Unit_t **units;
159
160   mem_free(&main_unit, event);
161   Debug_unit(&main_unit, "Releasing %d units", num_units);
162   FOR_ALL_UNITS_REV(units) {
163     if ((*units)->release != NULL) {
164       Debug_unit(&main_unit, "Releasing %s", (*units)->name);
165       (*((*units)->release))();
166     }
167   }
168   exit(0);
169 }
170
171 static void
172 parse_args(int argc, char **argv)
173 {
174   int i = 0;
175   const Unit_t *unit, **units;
176   
177   progname = argv[0];
178   while(i!=-1) {
179     i = getopt(argc, argv, "d:m:f:");
180     switch(i) {
181     case 'd':
182       if (strcmp(optarg, "all") == 0) {
183         FOR_ALL_UNITS(units) {
184           set_var_bool(*units, "debug", BL_TRUE);
185         }
186       } else {
187         unit = find_unit(optarg);
188         if (unit) {
189           set_var_bool(unit, "debug", BL_TRUE);
190         } else {
191           dump_printf(EL_ERROR, "Unknown module name: %s", optarg);
192           usage();
193         }
194       }
195       break;
196     case 'm':
197       if (strcmp(optarg, "all") == 0) {
198         FOR_ALL_UNITS(units) {
199           set_var_bool(*units, "memdebug", BL_TRUE);
200         }
201       } else {
202         unit = find_unit(optarg);
203         if (unit) {
204           set_var_bool(unit, "memdebug", BL_TRUE);
205         } else {
206           dump_printf(EL_ERROR, "Unknown module name: %s", optarg);
207           usage();
208         }
209       }
210       break;
211     case 'f':
212       var_file = mem_alloc(&main_unit, strlen(optarg)+1);
213       strcpy(var_file, optarg);
214       dump_printf(EL_NOTE, "Configuration file: %s\n", var_file);
215       break;
216     case -1:
217       break;
218     default:
219       usage();
220       return;
221     }
222   }
223   if (argc != optind) usage();
224   if (!var_file) {
225     var_file = mem_alloc(&main_unit, strlen(DEFAULT_CFG_FILE)+1);
226     strcpy(var_file, DEFAULT_CFG_FILE);
227     dump_printf(EL_NOTE, "Configuration file: %s\n", var_file);
228   }    
229 }
230
231 static void
232 usage(void)
233 {
234     dump_printf(EL_ERROR, "Usage:");
235     dump_printf(EL_ERROR, "%s [-d module]... [-m module]...[-f conf_file]", progname);
236     exit(1);
237 }
238