Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / syslog / main.c
blobae4be68a669497c25d45a4345c038135206a29ae
1 /* SysLog main file */
3 #if !defined(__AROS__)
4 #define __NOLIBBASE__
5 #endif
7 #include <proto/exec.h>
8 #include <proto/dos.h>
9 #include <proto/intuition.h>
10 #include <proto/muimaster.h>
11 #include <proto/locale.h>
12 #include <clib/alib_protos.h>
14 #include <libraries/mui.h>
16 #include <proto/miami.h>
17 #include <proto/socket.h>
19 #define ioctl IoctlSocket
21 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include <sys/time.h>
25 #include <signal.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 #include <netinet/ip_icmp.h>
31 #include <netinet/ip_var.h>
32 #include <netdb.h>
33 #include <syslog.h>
35 #include "hooks.h"
36 #include "main.h"
37 #include "colorlist.h"
40 extern struct Library *UtilityBase;
42 #if !defined(__AROS__)
43 extern struct Library *SysBase,
44 *DOSBase;
45 #endif
47 struct Library *MUIMasterBase,
48 *GfxBase;
50 #if !defined(__AROS__)
51 struct Library *IntuitionBase,
52 *LocaleBase;
53 #endif
55 Object *App, *Win, *SWin, *List, *DbgLevel;
57 struct MsgPort *Port;
59 struct Locale *Locale;
61 STRPTR DebugLevels[] = {"emergency", "alerts", "critical", "errors", "warnings", "notice", "info", "debug", "off", NULL};
63 BOOL Setup(void)
65 if (!(GfxBase = OpenLibrary("graphics.library", 0))) return FALSE;
66 if (!(IntuitionBase = OpenLibrary("intuition.library", 0))) return FALSE;
67 if (!(MUIMasterBase = OpenLibrary("muimaster.library", 0))) return FALSE;
68 if (!(LocaleBase = OpenLibrary("locale.library", 0))) return FALSE;
69 if (!(ColorList = CreateColorListClass())) return FALSE;
70 Locale = OpenLocale(NULL);
71 if (FindPort("SysLog")) return FALSE;
72 return TRUE;
75 void Cleanup(void)
77 CloseLocale(Locale);
78 if (ColorList) MUI_DeleteCustomClass(ColorList);
79 if (LocaleBase) CloseLibrary(LocaleBase);
80 if (MUIMasterBase) CloseLibrary(MUIMasterBase);
81 if (IntuitionBase) CloseLibrary(IntuitionBase);
82 if (GfxBase) CloseLibrary(GfxBase);
85 #define MUIA_InnerSpacing(a) MUIA_InnerLeft, a,\
86 MUIA_InnerRight, a,\
87 MUIA_InnerTop, a,\
88 MUIA_InnerBottom, a
90 Object *BuildSettingsWindow(void)
92 Object *win;
94 win = MUI_NewObject(MUIC_Window,
95 MUIA_Window_Title, (ULONG)"SysLog settings",
96 MUIA_Window_ID, 0x4D444C50,
97 MUIA_Window_RootObject, (ULONG)MUI_NewObject(MUIC_Group,
98 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Group,
99 MUIA_Group_Columns, 2,
100 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Text,
101 MUIA_Text_Contents, (ULONG)"\33rColor of error messages",
102 MUIA_Weight, 0,
103 TAG_END),
104 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Poppen,
105 MUIA_FixWidthTxt, (ULONG)"6EDA",
106 MUIA_InnerSpacing(1),
107 MUIA_ObjectID, 1,
108 MUIA_UserData, PREFS_POPPEN_ERRORS,
109 MUIA_Window_Title, (ULONG)"Error messages",
110 TAG_END),
111 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Text,
112 MUIA_Text_Contents, (ULONG)"\33rColor of important information",
113 MUIA_Weight, 0,
114 TAG_END),
115 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Poppen,
116 MUIA_FixWidthTxt, (ULONG)"6EDA",
117 MUIA_InnerSpacing(1),
118 MUIA_ObjectID, 2,
119 MUIA_UserData, PREFS_POPPEN_IMPORTANT,
120 MUIA_Window_Title, (ULONG)"Important information",
121 TAG_END),
122 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Text,
123 MUIA_Text_Contents, (ULONG)"\33rColor of debug information",
124 MUIA_Weight, 0,
125 TAG_END),
126 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Poppen,
127 MUIA_FixWidthTxt, (ULONG)"6EDA",
128 MUIA_InnerSpacing(1),
129 MUIA_ObjectID, 3,
130 MUIA_UserData, PREFS_POPPEN_OTHERS,
131 MUIA_Window_Title, (ULONG)"Debug information",
132 TAG_END),
133 TAG_END),
134 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Group,
135 MUIA_Group_Horiz, TRUE,
136 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Text,
137 MUIA_Text_Contents, (ULONG)"Save",
138 MUIA_Text_PreParse, (ULONG)"\33c",
139 MUIA_Text_HiChar, 's',
140 MUIA_ControlChar, 's',
141 MUIA_InputMode, MUIV_InputMode_RelVerify,
142 MUIA_Background, MUII_ButtonBack,
143 MUIA_Frame, MUIV_Frame_Button,
144 MUIA_Font, MUIV_Font_Button,
145 MUIA_UserData, PREFS_BUTTON_SAVE,
146 TAG_END),
147 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Text,
148 MUIA_Text_Contents, (ULONG)"Use",
149 MUIA_Text_PreParse, (ULONG)"\33c",
150 MUIA_Text_HiChar, 'u',
151 MUIA_ControlChar, 'u',
152 MUIA_InputMode, MUIV_InputMode_RelVerify,
153 MUIA_Background, MUII_ButtonBack,
154 MUIA_Frame, MUIV_Frame_Button,
155 MUIA_Font, MUIV_Font_Button,
156 MUIA_UserData, PREFS_BUTTON_USE,
157 TAG_END),
158 MUIA_Group_Child, (ULONG)MUI_NewObject(MUIC_Text,
159 MUIA_Text_Contents, (ULONG)"Cancel",
160 MUIA_Text_PreParse, (ULONG)"\33c",
161 MUIA_Text_HiChar, 'c',
162 MUIA_ControlChar, 'c',
163 MUIA_InputMode, MUIV_InputMode_RelVerify,
164 MUIA_Background, MUII_ButtonBack,
165 MUIA_Frame, MUIV_Frame_Button,
166 MUIA_Font, MUIV_Font_Button,
167 MUIA_UserData, PREFS_BUTTON_CANCEL,
168 TAG_END),
169 TAG_END),
170 TAG_END),
171 TAG_END);
173 return win;
177 void MainLoop(Object *win)
179 ULONG signals, portmask;
181 SetAttrs(win, MUIA_Window_Open, TRUE, TAG_END);
182 portmask = 1 << Port->mp_SigBit;
184 while (DoMethod(App, MUIM_Application_NewInput, (ULONG)&signals) != (ULONG)MUIV_Application_ReturnID_Quit)
186 if (signals)
188 signals = Wait(signals | portmask | SIGBREAKF_CTRL_C);
189 if (signals & portmask)
191 struct SysLogPacket *slp;
193 while (slp = (struct SysLogPacket*)GetMsg(Port))
195 if (slp->Level != LOG_CLOSE)
197 DoMethod(List, MUIM_List_InsertSingle, (ULONG)slp, MUIV_List_Insert_Bottom);
198 DoMethod(List, MUIM_List_Jump, MUIV_List_Jump_Bottom, TAG_END);
200 ReplyMsg(&slp->Msg);
203 if (signals & SIGBREAKF_CTRL_C) break;
207 SetAttrs(win, MUIA_Window_Open, FALSE, TAG_END);
208 return;
212 /* Settings hook */
213 #if !defined(__AROS__)
214 LONG SettingsHook(void)
216 #else
217 AROS_UFH3(
218 void, SettingsHook,
219 AROS_UFHA(struct Hook *, hook, A0),
220 AROS_UFHA(APTR *, obj, A2),
221 AROS_UFHA(APTR, param, A1)
224 AROS_USERFUNC_INIT
225 #endif
227 // Get data
228 SetAttrs(Win, MUIA_Window_Sleep, TRUE, TAG_END);
229 MainLoop(SWin);
230 SetAttrs(Win, MUIA_Window_Sleep, FALSE, TAG_END);
231 #if !defined(__AROS__)
232 return 0;
233 #else
234 AROS_USERFUNC_EXIT
235 #endif
239 #if !defined(__AROS__)
240 struct EmulLibEntry g_SettingsHook = {TRAP_LIB, 0, (void (*)(void))SettingsHook};
241 struct Hook h_SettingsHook = {{NULL, NULL}, (HOOKFUNC)&g_SettingsHook, NULL, NULL};
242 #else
243 struct Hook h_SettingsHook;
244 #endif
246 Object* CreateMenu(void)
248 Object *o;
250 o = MUI_NewObject(MUIC_Menustrip,
251 MUIA_Family_Child, (ULONG)MUI_NewObject(MUIC_Menu,
252 MUIA_Menu_Title, (ULONG)"Project",
253 MUIA_Family_Child, (ULONG)MUI_NewObject(MUIC_Menuitem,
254 MUIA_Menuitem_Title, (ULONG)"Clear",
255 MUIA_Menuitem_Shortcut, (ULONG)"C",
256 MUIA_UserData, MENU_CLEAR,
257 TAG_END),
258 /* MUIA_Family_Child, (ULONG)MUI_NewObject(MUIC_Menuitem,
259 MUIA_Menuitem_Title, (ULONG)"Save as...",
260 MUIA_Menuitem_Shortcut, (ULONG)"S",
261 MUIA_UserData, MENU_SAVEAS,
262 TAG_END),*/
263 MUIA_Family_Child, (ULONG)MUI_NewObject(MUIC_Menuitem,
264 MUIA_Menuitem_Title, (ULONG)"Settings...",
265 MUIA_UserData, MENU_SETTINGS,
266 TAG_END),
267 MUIA_Family_Child, (ULONG)MUI_NewObject(MUIC_Menuitem,
268 MUIA_Menuitem_Title, MENUBAR,
269 TAG_END),
270 MUIA_Family_Child, (ULONG)MUI_NewObject(MUIC_Menuitem,
271 MUIA_Menuitem_Title, (ULONG)"Quit",
272 MUIA_Menuitem_Shortcut, (ULONG)"Q",
273 MUIA_UserData, MENU_QUIT,
274 TAG_END),
275 TAG_END),
276 TAG_END);
278 return o;
282 BOOL BuildGui(void)
284 Object *lv;
286 App = MUI_NewObject(MUIC_Application,
287 MUIA_Application_Author, (ULONG)"Grzegorz \"Krashan\" Kraszewski, Pavel \"Sonic\" Fedin, Nick \"Kalamatee\" Andrews",
288 MUIA_Application_Base, (ULONG)"SysLog",
289 MUIA_Application_Copyright, (ULONG)"(c) 2005 by Grzegorz Kraszewski, Pavel Fedin, Nick Andrews",
290 MUIA_Application_Description, (ULONG)"AROSTCP syslog viewer",
291 MUIA_Application_SingleTask, TRUE,
292 MUIA_Application_Title, (ULONG)"SysLog",
293 MUIA_Application_Version, (ULONG)"$VER: SysLog 1.2 (15.11.2005)\n",
294 MUIA_Application_Menustrip, (ULONG)CreateMenu(),
295 MUIA_Application_Window, (ULONG)(Win = MUI_NewObject(MUIC_Window,
296 MUIA_Window_Title, (ULONG)"AROSTCP syslog viewer",
297 MUIA_Window_ScreenTitle, (ULONG)"SysLog 1.2 (c) 2005-2007 Grzegorz Kraszewski, Pavel Fedin, Nick Andrews",
298 MUIA_Window_ID, 0x4D444C47,
299 MUIA_Window_RootObject, (ULONG)MUI_NewObject(MUIC_Group,
300 MUIA_Group_Child, (ULONG)(lv = MUI_NewObject(MUIC_Listview,
301 MUIA_Listview_List, (ULONG)(List = NewObject(ColorList->mcc_Class, NULL,
302 MUIA_Frame, MUIV_Frame_ReadList,
303 MUIA_List_Format, (ULONG)"BAR,BAR,BAR,",
304 MUIA_List_Title, TRUE,
305 TAG_END)),
306 MUIA_Listview_Input, FALSE,
307 TAG_END)),
308 MUIA_Group_Child, MUI_NewObject(MUIC_Group,
309 MUIA_Group_Horiz, TRUE,
310 MUIA_Group_Child, MUI_NewObject(MUIC_Text,
311 MUIA_Text_SetMax, TRUE,
312 MUIA_Text_Contents, (ULONG)"Debug level",
313 TAG_END),
314 MUIA_Group_Child, DbgLevel = MUI_NewObject(MUIC_Cycle,
315 MUIA_Cycle_Entries, (ULONG)DebugLevels,
316 MUIA_Weight, 1,
317 TAG_END),
318 MUIA_Group_Child, MUI_NewObject(MUIC_Rectangle,
319 TAG_END),
320 TAG_END),
321 TAG_END),
322 MUIA_Window_DefaultObject, (ULONG)lv,
323 TAG_END)),
324 MUIA_Application_Window, SWin = BuildSettingsWindow(),
325 TAG_END);
327 if (App) return TRUE;
328 else return FALSE;
332 void SetNotifications (void)
334 #if defined(__AROS__)
335 h_SettingsHook.h_Entry = ( HOOKFUNC )SettingsHook;
336 #endif
337 DoMethod(Win, MUIM_Notify, MUIA_Window_CloseRequest, MUIV_EveryTime, (ULONG)App, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
338 DoMethod(App, MUIM_Notify, MUIA_Application_MenuAction, MENU_QUIT, (ULONG)App, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
339 /*DoMethod(App, MUIM_Notify, MUIA_Application_MenuAction, MENU_SAVEAS, (ULONG)List, 2, MUIM_CallHook, (ULONG)&h_SaveAsHook);*/
340 DoMethod(App, MUIM_Notify, MUIA_Application_MenuAction, MENU_SETTINGS, (ULONG)App, 2, MUIM_CallHook, (ULONG)&h_SettingsHook);
341 DoMethod(App, MUIM_Notify, MUIA_Application_MenuAction, MENU_CLEAR,
342 (ULONG)List, 1, MUIM_List_Clear);
344 /* close prefs window */
346 DoMethod(SWin, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, App, 2,
347 MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
348 DoMethod(findobj(SWin, PREFS_BUTTON_CANCEL), MUIM_Notify, MUIA_Pressed,
349 FALSE, App, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
350 DoMethod(findobj(SWin, PREFS_BUTTON_USE), MUIM_Notify, MUIA_Pressed,
351 FALSE, App, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
352 DoMethod(findobj(SWin, PREFS_BUTTON_SAVE), MUIM_Notify, MUIA_Pressed,
353 FALSE, App, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
355 /* saving prefs */
357 DoMethod(findobj(SWin, PREFS_BUTTON_USE), MUIM_Notify, MUIA_Pressed,
358 FALSE, App, 2, MUIM_Application_Save, MUIV_Application_Save_ENV);
359 DoMethod(findobj(SWin, PREFS_BUTTON_SAVE), MUIM_Notify, MUIA_Pressed,
360 FALSE, App, 2, MUIM_Application_Save, MUIV_Application_Save_ENV);
361 DoMethod(findobj(SWin, PREFS_BUTTON_SAVE), MUIM_Notify, MUIA_Pressed,
362 FALSE, App, 2, MUIM_Application_Save, MUIV_Application_Save_ENVARC);
364 /* list redisplay */
366 DoMethod(findobj(SWin, PREFS_BUTTON_USE), MUIM_Notify, MUIA_Pressed,
367 FALSE, List, 1, CLL_ChangePens);
368 DoMethod(findobj(SWin, PREFS_BUTTON_SAVE), MUIM_Notify, MUIA_Pressed,
369 FALSE, List, 1, CLL_ChangePens);
374 int main(void)
376 struct Library *MiamiBase;
377 if (Setup())
379 if (Port = CreateMsgPort())
381 struct Message *m;
383 Port->mp_Node.ln_Name = "SysLog";
384 Port->mp_Node.ln_Pri = 12;
385 AddPort(Port);
386 MiamiBase = OpenLibrary("miami.library",13);
387 if (MiamiBase) {
388 SetSysLogPort();
389 CloseLibrary(MiamiBase);
391 if (BuildGui())
393 DoMethod(App, MUIM_Application_Load, MUIV_Application_Load_ENV);
394 SetNotifications();
395 MainLoop(Win);
396 MUI_DisposeObject(App);
398 RemPort(Port);
399 MiamiBase = OpenLibrary("miami.library",13);
400 if (MiamiBase) {
401 SetSysLogPort();
402 CloseLibrary(MiamiBase);
404 while (m = GetMsg(Port)) ReplyMsg(m);
405 DeleteMsgPort(Port);
408 else Printf("Setup() failed.\n");
409 Cleanup();
410 return 0;