/*
 * $Id: system.c,v 1.6 1997/01/03 17:36:43 root Exp root $
 *
 * $Log: system.c,v $
 * Revision 1.6  1997/01/03 17:36:43  root
 * Moved everything bug exit_program() to system.c.
 *
 * Revision 1.5  1996/12/21 00:20:06  root
 * Changed:
 * In network_config_init():
 * compact code to find correct aliases
 *
 * Revision 1.4  1996/12/20 19:34:29  root
 * Changed:
 * In network_config():
 * Label and text widgets are now created in a loop.
 *
 * Revision 1.3  1996/12/20 18:56:20  root
 * Added:
 * network_config_init()
 * network_config_ok()
 * network_config_reset()
 * network_config_cancel()
 *
 * Revision 1.2  1996/12/12 19:02:01  root
 * Added functions network_config() and network_config_cancel()
 *
 * Revision 1.1  1996/11/18 21:39:35  root
 * Initial revision
 *
 */

#include "common.h"
#include "system.h"
#include "util.h"

static int netstat(char **);



static int 
netstat(char **text)
{
  FILE *netstat;
  char buf[BUFSIZ], *ptr;


  if ((netstat = popen("netstat -rn", "r")) != NULL) {
    while ((fgets(buf, BUFSIZ, netstat)) != NULL)
      if (strstr(buf, "Destination") != NULL) {
	ptr = strrchr(buf, '\n');
	*ptr = '\0';
	break;
      }
    
    text[0] = XtMalloc(strlen(buf) + 1);
    strcpy(text[0], buf);
    
    fgets(buf, BUFSIZ, netstat);

    text[1] = XtMalloc(1);

    while ((fgets(buf, BUFSIZ, netstat)) != NULL) {
      text[1] = XtRealloc(text[1], strlen(text[1]) + strlen(buf));
      strcat(text[1], buf);
      handle_pending_x_events();
    }

    pclose(netstat);

    return 0;
  } else
    return PIPE_ERROR;
}



void 
route_table(Widget w, XtPointer client_data, XtPointer call_data)
{
  Widget route_dialog, header, body;
  Widget route_form, messagebox, sep;
  XmString str;
  char *text[2];
  
  watch_cursor(GetTopShell(w), True);

  route_dialog = XtVaCreatePopupShell("route_dialog",
				      xmDialogShellWidgetClass,
				      GetAppShell(w),
				      NULL);
  
  messagebox = XmCreateMessageBox(route_dialog, "messagebox", NULL, 0);
  XtVaSetValues(messagebox,
		XmNmarginWidth, 0,
		XmNtopAttachment, XmATTACH_WIDGET,
		XmNtopWidget, body,
		XmNleftAttachment, XmATTACH_FORM,
		XmNrightAttachment, XmATTACH_FORM,
		NULL);
  
  XtUnmanageChild(XmMessageBoxGetChild(messagebox, 
				       XmDIALOG_HELP_BUTTON));
  XtUnmanageChild(XmMessageBoxGetChild(messagebox, 
				       XmDIALOG_CANCEL_BUTTON));
  
  
  route_form = XtVaCreateWidget ("route_form", xmFormWidgetClass,
				 messagebox, NULL);
  
  header = XtVaCreateManagedWidget("header", xmLabelGadgetClass,
				   route_form,
				   XmNtopAttachment, XmATTACH_FORM,
				   XmNleftAttachment, XmATTACH_FORM,
				   XmNleftOffset, 13,
				   XmNrightAttachment, XmATTACH_FORM,
				   XmNrightOffset, 13,
				   NULL);
  
  sep = XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass,
				route_form,
				XmNtopAttachment, XmATTACH_WIDGET,
				XmNtopWidget, header,
				XmNleftAttachment, XmATTACH_FORM,
				XmNrightAttachment, XmATTACH_FORM,
				NULL);
  
  body = XtVaCreateManagedWidget("body", xmLabelGadgetClass, route_form,
				 XmNalignment, XmALIGNMENT_BEGINNING,
				 XmNtopAttachment, XmATTACH_WIDGET,
				 XmNtopWidget, sep,
				 XmNtopOffset, 5,
				 XmNleftAttachment, XmATTACH_FORM,
				 XmNleftOffset, 13,
				 XmNrightAttachment, XmATTACH_FORM,
				 XmNrightOffset, 13,
				 NULL);
  
  
  netstat(text);
  
  str = XmStringCreateLocalized(text[0]);
  XtVaSetValues(header, XmNlabelString, str, NULL);
  XmStringFree(str);

  str = XmStringCreateLocalized(text[1]);
  XtVaSetValues(body, XmNlabelString, str, NULL);
  XmStringFree(str);

  XtFree(text[0]);
  XtFree(text[1]);

  XtManageChild(route_form);
  XtManageChild(messagebox);

  
  XtPopup(route_dialog, XtGrabNone);
  watch_cursor(GetTopShell(w), False);
}


void 
exit_program(void)
{
  exit(0);
}





  
