home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / s / seyon197.tz / seyon197 / seyon / SeMisc.c < prev    next >
C/C++ Source or Header  |  1993-02-16  |  7KB  |  348 lines

  1.  
  2. /*
  3.  * This file is part of the Seyon, Copyright (c) 1992-1993 by Muhammad M.
  4.  * Saggaf. All rights reserved.
  5.  *
  6.  * See the file COPYING (1-COPYING) or the manual page seyon(1) for a full
  7.  * statement of rights and permissions for this program.
  8.  */
  9.  
  10. #include <X11/Intrinsic.h>
  11. #include <X11/StringDefs.h>
  12. #include <X11/Xaw/Dialog.h>
  13.  
  14. #include <signal.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17.  
  18. #include "seyon.h"
  19. #include "SeDecl.h"
  20. #include "SeSig.h"
  21.  
  22. extern pid_t    read_pid;
  23.  
  24. char            captureFile[REG_BUF];
  25. Boolean         capture = False;
  26. FILE           *cfp;
  27.  
  28. void
  29.                 SendBreak(),
  30.                 terminal_refresh(),
  31.                 LocalShell(),
  32.                 ToggleCapture(),
  33.                 kill_handler(),
  34.                 DivertFile(),
  35.                 DoDivertFile(),
  36.                 ExecDivertFile(),
  37.                 RunScript(),
  38.                 DoRunScript(),
  39.                 ExecScript();
  40.  
  41. void
  42. top_misc(w)
  43.      Widget          w;
  44. {
  45.   Widget          popup,
  46.                   mBox,
  47.                   uBox,
  48.                   lBox,
  49.                   toggle;
  50.   static Boolean  misc_is_up = False;
  51.  
  52.   ret_if_up(w, inhibit_child);
  53.   ret_if_up(w, misc_is_up);
  54.   misc_is_up = True;
  55.  
  56.   popup = SeAddPopup("misc", w);
  57.   mBox = SeAddPaned("mBox", popup);
  58.   uBox = SeAddBox("uBox", mBox);
  59.   lBox = SeAddBox("lBox", mBox);
  60.  
  61.   SeAddButton("break", uBox, SendBreak);
  62.   SeAddButton("refresh", uBox, terminal_refresh);
  63.   SeAddButton("suspend", uBox, LocalShell);
  64.   toggle = SeAddToggle("capture", uBox, ToggleCapture);
  65.   SeAddButton("divert", uBox, DivertFile);
  66.   SeAddButton("script", uBox, RunScript);
  67.  
  68.   SeAddButtonWCD("dismiss", lBox, DismissPopup2, (XtPointer) & misc_is_up);
  69.  
  70.   SeSetUnsetToggle(toggle, capture);
  71.  
  72.   XtPopup(popup, XtGrabNone);
  73. }
  74.  
  75. void
  76. SendBreak(widget)
  77.      Widget          widget;
  78. {
  79.   ret_if_up(widget, inhibit_child);
  80.   send_break();
  81.   SeyonMessage("BREAK Sent to Remote Host");
  82. }
  83.  
  84. void
  85. terminal_refresh(widget)
  86.      Widget          widget;
  87. {
  88.   ret_if_up(widget, inhibit_child);
  89.   restart_terminal();
  90.   SeyonMessage("Terminal Process Refreshed");
  91. }
  92.  
  93. void
  94. LocalShell(widget)
  95.      Widget          widget;
  96. {
  97.   ret_if_up(widget, inhibit_child);
  98.   exec_cmd("");
  99.   SeyonMessage("Terminal Suspended");
  100. }
  101.  
  102. void
  103. ToggleCapture(widget)
  104.      Widget          widget;
  105. {
  106.   ret_if_up(widget, inhibit_child);
  107.   DoToggleCapture();
  108.  
  109.   SeSetUnsetToggle(widget, capture);
  110.   restart_terminal();
  111. }
  112.  
  113. Boolean
  114. DoToggleCapture()
  115. {
  116.   if (capture) {
  117.     fclose(cfp);
  118.     capture = False;
  119.     SeyonMessage("Capture Turned OFF");
  120.   }
  121.   else {
  122.     if ((cfp = fopen(captureFile, "a")) == NULL) {
  123.       SeyonMessagef("Unable to Open Capture File '%s'", captureFile, "", "");
  124.       return False;
  125.     }
  126.     else {
  127.       capture = True;
  128.       SeyonMessage("Capture Turned ON");
  129.     }
  130.   }
  131.   return True;
  132. }
  133.  
  134. /*
  135.  * divert: upload text file
  136.  */
  137.  
  138. void
  139. DivertFile(widget)
  140.      Widget          widget;
  141. {
  142.   ret_if_up(widget, inhibit_child);
  143.   SePopupDialogGetStringE("divert_name", widget, DoDivertFile, NULL,
  144.               NULL, True);
  145. }
  146.  
  147. void
  148. divert_action_ok(widget)
  149.      Widget          widget;
  150. {
  151.   DoDivertFile(widget);
  152. }
  153.  
  154. void
  155. DoDivertFile(widget)
  156.      Widget          widget;
  157. {
  158.   Widget          dialog = XtParent(widget);
  159.   String          file_name;
  160.  
  161.   file_name = XawDialogGetValueString(dialog);
  162.   DestroyShell(dialog);
  163.  
  164.   ExecDivertFile(XtParent(GetShell(widget)), file_name);
  165. }
  166.  
  167. void
  168. divert_handler(
  169. #if NeedFunctionPrototypes
  170.         int signo,
  171.         XtPointer client_data
  172. #endif
  173. )
  174. {
  175.   int             status;
  176.  
  177.   XoAppIgnoreSignal(app_con, SIGCHLD);
  178.   wait(&status);
  179.  
  180.   switch (WEXITSTATUS(status)) {
  181.   case 0:
  182.     SeyonMessage("Text Upload Successful");
  183.     break;
  184.   case 10:
  185.     SeyonMessage("Text Upload Canceled");
  186.     break;
  187.   }
  188.  
  189.   inhibit_child = False;
  190.   post_process();
  191. }
  192.  
  193. void
  194. killdivert_handler(
  195. #if NeedFunctionPrototypes
  196.             int signo
  197. #endif
  198. )
  199. {
  200.   signal(SIGTERM, SIG_IGN);
  201.  
  202.   if (read_pid) {
  203.     kill(read_pid, SIGTERM);
  204.     wait((int *)0);
  205.   }
  206.   exit(10);
  207. }
  208.  
  209. void
  210. ExecDivertFile(widget, file_name)
  211.      Widget          widget;
  212.      String          file_name;
  213. {
  214.   char            fullname[REG_BUF];
  215.   FILE           *fp;
  216.   int             c;
  217.  
  218.   expand_fname(file_name, fullname);
  219.   if ((fp = fopen(fullname, "r")) == NULL) {
  220.     SeyonMessagef("Unable to Open File '%s'", file_name, "", "");
  221.     SeError("errFileAccess", widget);
  222.     return;
  223.   }
  224.  
  225.   inhibit_child = True;
  226.   SeyonMessagef("Uploading Text File '%s'...", file_name, "", "");
  227.  
  228.   pre_process();
  229.   XoAppAddSignal(app_con, SIGCHLD, divert_handler, NULL);
  230.  
  231.   if ((w_child_pid = se_fork()) == 0) {
  232.     signal(SIGTERM, killdivert_handler);
  233.  
  234.     if ((read_pid = se_fork()) == 0)
  235.       PortToTty();
  236.  
  237.     while ((c = getc(fp)) != EOF) {
  238.       send_tbyte(c);
  239.       if (c == '\r' || c == '\n')
  240.     usleep(MDELAY);
  241.     }
  242.     fclose(fp);
  243.     if (read_pid) {
  244.       kill(read_pid, SIGTERM);
  245.       wait((int *)0);
  246.     }
  247.     exit(0);
  248.   }
  249. }
  250.  
  251. /*
  252.  * run a script
  253.  */
  254.  
  255. void
  256. RunScript(widget, client_data)
  257.      Widget          widget;
  258.      XtPointer       client_data;
  259. {
  260.   ret_if_up(widget, inhibit_child);
  261.   SePopupDialogGetStringE("script_name", widget, DoRunScript, NULL,
  262.               NULL, True);
  263. }
  264.  
  265. void
  266. script_action_ok(widget)
  267.      Widget          widget;
  268. {
  269.   DoRunScript(widget);
  270. }
  271.  
  272. void
  273. DoRunScript(widget)
  274.      Widget          widget;
  275. {
  276.   Widget          dialog = XtParent(widget);
  277.   String          script_name;
  278.  
  279.   script_name = XawDialogGetValueString(dialog);
  280.   DestroyShell(dialog);
  281.  
  282.   linkflag = 0;
  283.   ExecScript(script_name);
  284. }
  285.  
  286. void
  287. script_handler(
  288. #if NeedFunctionPrototypes
  289.         int signo,
  290.         XtPointer client_data
  291. #endif
  292. )
  293. {
  294.   int             status;
  295.  
  296.   XoAppIgnoreSignal(app_con, SIGCHLD);
  297.   wait(&status);
  298.  
  299.   switch (WEXITSTATUS(status)) {
  300.   case 0:
  301.     SeyonMessage("Script Completed");
  302.     break;
  303.   case 1:
  304.     SeyonMessage("Script Execution Failed");
  305.     break;
  306.   case 10:
  307.     SeyonMessage("Script Canceled by User");
  308.     get_modem_attr();
  309.     break;
  310.   }
  311.  
  312.   inhibit_child = False;
  313.   post_process();
  314. }
  315.  
  316. void
  317. killscript_handler(
  318. #if NeedFunctionPrototypes
  319.             int signo
  320. #endif
  321. )
  322. {
  323.   signal(SIGTERM, SIG_IGN);
  324.   put_parameters();
  325.   exit(10);
  326. }
  327.  
  328. void
  329. ExecScript(script_name)
  330.      String          script_name;
  331. {
  332.   int scriptRet;
  333.  
  334.   inhibit_child = True;
  335.   SeyonMessagef("Running Script '%s'...", script_name, "", "");
  336.  
  337.   pre_process();
  338.   XoAppAddSignal(app_con, SIGCHLD, script_handler, NULL);
  339.  
  340.   if ((w_child_pid = se_fork()) == 0) {
  341.     signal(SIGTERM, killscript_handler);
  342.  
  343.     scriptRet = (int)do_script(script_name);
  344.     put_parameters();
  345.     exit(scriptRet ? 0 : 1);
  346.   }
  347. }
  348.