home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / omgrmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  5.5 KB  |  233 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: omgrmail.c,v 4.2 88/06/22 14:37:57 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/omgrmail.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/omgrmail.c,v $$Revision: 4.2 $";
  12.  
  13. /* check for new mail */
  14.  
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include "term.h"
  20.  
  21. #define MSG_1    "\fLooking for new mail"
  22. #define MSG_2    "\f\007You have new mail"
  23. #define MSG_3    "\freading mail ...\r"
  24. #define MSG_4    "\rChecking for new mail..."
  25.  
  26. #define MAILF        "/usr/spool/mail"    /* spool file */
  27. #define MAIL        "mail"            /* name of mail command */
  28. #define POLL        60            /* polling interval */
  29. #define XPOS        200            /* x start of mail window */
  30. #define YPOS        150            /* y start of mail window */
  31.  
  32. #define PROCESSED    2            /* new mail already processed */
  33.  
  34. #define S(x)            statb.x
  35. #define Isflag(arg,flag)    (!strncmp(arg,flag,strlen(flag)))
  36. #define Max(x,y)        ((x)>(y)?(x):(y))
  37. #define dprintf            if(debug) fprintf
  38.  
  39. #define MENU_COUNT        (sizeof(menu)/sizeof(struct menu_entry))
  40.  
  41. struct menu_entry menu[] = {
  42.     "print","t\r",
  43.     "delete","dt\r",
  44.     "next","n\r",
  45.     "quit","q\r",
  46.     "help","?\r",
  47.     "headers","h *\r",
  48.     "abort","x\r",
  49. };
  50.  
  51. struct    stat statb;            /* spool file status */
  52. char    mail[255];            /* spool file path name */
  53. long omtime=0l;                /* previous file mod. time */
  54. int state = 0;                /* mail & window state */
  55. int poll = POLL;            /* poll interval */
  56. int debug=0;                /* for mgrmail -d >& /dev/tty?? */
  57.  
  58. main(argc,argv)
  59.     char **argv;
  60. {
  61.     register int i;
  62.     int xpos = XPOS;        /* screen position of mail subwindow */
  63.     int ypos = YPOS;
  64.     int font = -1;            /* font to use for mail subwindow */
  65.     int shape = 1;            /* initially reshape window */
  66.     char *command = MAIL;        /* name of readmail command */
  67.  
  68.     char *getenv();
  69.     char *user = getenv("USER");
  70.     char line[80];            /* event input buffer */
  71.  
  72.     int clean(), update();
  73.  
  74.     /* make sure environment is ok */
  75.  
  76.     ckmgrterm( *argv );
  77.  
  78.     if (user==NULL || *user=='\0') {
  79.         fprintf(stderr,"%s: Who are you?\n",argv[0]);
  80.         exit(2);
  81.     }
  82.  
  83.     /* process arguments */
  84.  
  85.     for(i=1;i<argc;i++) {
  86.         if (Isflag(argv[i],"-s"))
  87.             shape = 0;
  88.         else if (Isflag(argv[i],"-d"))
  89.             debug = 1;
  90.         else if (Isflag(argv[i],"-x"))
  91.             xpos = atoi(argv[i]+2);
  92.         else if (Isflag(argv[i],"-y"))
  93.             ypos = atoi(argv[i]+2);
  94.         else if (Isflag(argv[i],"-f"))
  95.             font = atoi(argv[i]+2);
  96.         else if (Isflag(argv[i],"-p"))
  97.             poll  = Max(atoi(argv[i]+2),10);
  98.         else if (Isflag(argv[i],"-M"))
  99.             command  = argv[i]+2;
  100.         else
  101.             usage(argv[0],argv[i]);
  102.     }
  103.     sprintf(mail,"%s/%s",MAILF,user);
  104.  
  105.     /* set up window environment */
  106.  
  107.     m_setup(M_FLUSH);
  108.     m_ttyset();
  109.     m_push(P_MENU|P_EVENT|P_FLAGS);
  110.     dprintf(stderr,"pushing environment\n"); fflush(stderr);
  111.     m_setmode(M_NOWRAP);
  112.  
  113.     signal(SIGTERM,clean);
  114.     signal(SIGINT,clean);
  115.     signal(SIGALRM,update);
  116.  
  117.     m_setmode(M_ACTIVATE);
  118.     if (shape) {
  119.         m_size(strlen(MSG_1),1);
  120.         }
  121.  
  122.     menu_load(1,MENU_COUNT,menu);
  123.     m_selectmenu(1);
  124.     m_setevent(ACTIVATE,"A\r");
  125.     m_setevent(REDRAW,"R\r");
  126.  
  127.     m_clearmode(M_ACTIVATE);
  128.     m_clear();
  129.     m_printstr(MSG_1);
  130.  
  131.     dprintf(stderr,"Starting state 0x%x\n",state); fflush(stderr);
  132.  
  133.     update();
  134.  
  135.     /* wait for an event */
  136.  
  137.     while(1) {
  138.         m_gets(line);
  139.         dprintf(stderr,"state 0x%x line : %c\n",state,*line); fflush(stderr);
  140.         switch(*line) {
  141.             case 'A':    /* window is activated */
  142.                 if (!stat(mail,&statb) && S(st_size))
  143.                     do_mail(command,font,xpos,ypos);
  144.                 state &= ~PROCESSED;
  145.                 update();
  146.                 break;
  147.             case 'R':    /* screen is redrawn */
  148.                 state &= ~PROCESSED;
  149.                 update();
  150.                 break;
  151.         }
  152.     }
  153. }
  154.  
  155. /* run readmail in a subwindow */
  156.  
  157. do_mail(command,font,xpos,ypos)
  158. char *command;
  159. int font,xpos,ypos;
  160.     {
  161.     int x,y,dummy;
  162.     int code;
  163.  
  164.     alarm(0);
  165.     m_push(P_POSITION|P_EVENT|P_FLAGS|P_FONT);
  166.     dprintf(stderr,"doing mail\n"); fflush(stderr);
  167.     if (font != -1)
  168.         m_font(font);
  169.     m_sizeall(xpos,ypos,80,24);
  170.     m_printstr(MSG_3);
  171.     m_ttyreset();
  172.     code = system(command);
  173.     m_printstr(MSG_4);
  174.     sleep(2);    /* for "New mail arrived" message */
  175.     dprintf(stderr,"Readmail completed code %d\n",code); fflush(stderr);
  176.     m_ttyset();
  177.         get_size(&x,&y,&dummy,&dummy);
  178.     m_pop();
  179.     dprintf(stderr,"done with mail\n"); fflush(stderr);
  180.     if (x!=xpos || y!=ypos) {
  181.         m_movewindow(x,y);
  182.         xpos = x;
  183.         dprintf(stderr,"moving mail window\n"); fflush(stderr);
  184.         ypos = y;
  185.     }
  186.     m_clearmode(M_ACTIVATE);
  187.     dprintf(stderr,"window deactivated\n"); fflush(stderr);
  188.     }
  189.  
  190. /* check the spool file for new mail and update message */
  191.  
  192. int
  193. update()
  194. {
  195.     alarm(0);
  196.     dprintf(stderr,"checking mail state 0x%x\n",state); fflush(stderr);
  197.     if (!stat(mail,&statb) && S(st_mtime)>S(st_atime) && S(st_size)) {
  198.         state &= ~PROCESSED;
  199.         if (S(st_mtime) != omtime) {
  200.         dprintf(stderr,"    First time New mail\n"); fflush(stderr);
  201.             m_printstr(MSG_2);
  202.             m_setmode(M_WOB);
  203.             omtime = S(st_mtime);
  204.         }
  205.     }
  206.     else if (!(state&PROCESSED)) {
  207.         dprintf(stderr,"    Clearing new mail\n"); fflush(stderr);
  208.         m_clearmode(M_WOB);
  209.         m_printstr(MSG_1);
  210.         state |= PROCESSED;
  211.     }
  212.     alarm(poll);
  213. }
  214.  
  215. /*    Clean up and exit */
  216.  
  217. clean()
  218. {
  219.     m_popall();
  220.     m_ttyreset();
  221.     exit(1);
  222. }
  223.  
  224. usage(name,error)
  225. char *name, *error;
  226. {
  227.     fprintf(stderr,"Invalid flag: %s\n",error);
  228.     fprintf(stderr,
  229.         "usage: %s -[s|x<pos>|y<pos>|f<font>|p<poll>|M<mail_program>]\n"
  230.         ,name);
  231.     exit(1);
  232. }
  233.