home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MXMS_160.LZH / MAXLIB.C < prev    next >
C/C++ Source or Header  |  1991-06-17  |  9KB  |  387 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    Maxlib.c      :Miscellaneous Maximus routines for MaxMail             */
  4. /*                                                                          */
  5. /****************************************************************************/
  6.  
  7. #include "MaxMail.h"
  8. #include "bios.h"
  9.  
  10. int getch(void);
  11.  
  12. extern int CurColor;
  13.  
  14.  
  15. /* Load in the MAXimus PARM file into our global structure */
  16. int read_prm(char *pname)
  17. {
  18.   int prmfile,x;
  19.  
  20.   offsets=NULL;
  21.  
  22.   if ((prmfile=sopen(pname,O_RDONLY | O_BINARY,SH_DENYNO,S_IREAD))==-1)
  23.    return(-1);
  24.  
  25.   x=read(prmfile,(char *)&prm,sizeof(struct m_pointers));
  26.    
  27.   if (prm.id != 'M' || prm.version != CTL_VER)
  28.   {
  29.     printf("\a\a");
  30.     aborterror(FILEOPEN,"Bad PARM file");
  31.   }
  32.  
  33.   x=(int)(fsize(pname)-((int)tell(prmfile)));
  34.   offsets=(char *)malloc(x+10);
  35.   x=read(prmfile,offsets,x);
  36.   close(prmfile);
  37.   return TRUE;
  38. }
  39.  
  40. /* Quick simple method to return size of file without
  41.    opening it */
  42.  
  43. long fsize(char *fname)
  44. {
  45.    struct find_t find;
  46.  
  47.    if(_dos_findfirst(fname,0xffff,&find))
  48.       return(0L);        /* An error occured */
  49.    else return(find.size);
  50. }
  51.  
  52. void get_currents()
  53. {
  54.    unsigned x;
  55.  
  56.    _dos_getdrive(&x);
  57.    sprintf(sdrive,"%c:",'A' + x - 1);
  58.    getcwd(temp,_MAX_DIR - 1);
  59.    strcpy(sdir,temp+2);
  60.    strcat(sdir,"\\");
  61. }
  62.  
  63.  
  64. /* Switch to drive,dir specified */
  65. int switch_dir(char *dname)
  66. {
  67.    unsigned x,y,z;
  68.    char tt[65];
  69.  
  70.    strcpy(tt,dname);
  71.    x = strlen(tt) - 1;
  72.    y = strlen(tt) - 1;
  73.    if (tt[x] == '\\') 
  74.       tt[x] = 0;        /* Nullify trailing \ */
  75.  
  76.    x = (toupper(dname[0])) - 'A' + 1;
  77.    _dos_setdrive(x,&z);        /* Ignore 2nd parm */
  78.    if (tt[y] == ':') {        /* Just a drive letter for temp directory ? */
  79.       _dos_getdrive(&z);
  80.       if (x != z)         /* Change drive did not work */
  81.          return 1;
  82.       else {        /* If it's just a drive, move to root */
  83.          chdir("\\");
  84.          return 0;
  85.       }
  86.    }
  87.    else return(chdir(tt));
  88. }
  89.  
  90. /* Video stuff */
  91.  
  92. void chrout(char ch)
  93. {
  94.    putchar(ch);
  95.  
  96.    if (!IsLocal) 
  97.       FossSendCh(ch);
  98.  
  99. }
  100.  
  101. void strout(char *outmsg)
  102. {
  103.    printf(outmsg);
  104.  
  105.    if (!IsLocal) {
  106.       check_carrier();
  107.       FossSendStr(outmsg);
  108.       check_carrier();
  109.    }
  110. }
  111.  
  112. void cleareol(void)
  113. {
  114.    int x;
  115.  
  116.    x = CurColor;
  117.    setcolor(7);
  118.  
  119.    if (LastUser.ansi) 
  120.       strout("\x1b[K");
  121.    else if (LastUser.avatar) {
  122.       printf("\x1b[K");        /* Always send ansi to local console */
  123.       FossSendStr("\x07");        /* ^G */
  124.    }
  125.    setcolor(x);
  126. }
  127.  
  128. void strin(char *inmsg)
  129. {
  130.    unsigned ch;
  131.    int key,minutes;
  132.    char *p;
  133.  
  134.    p = inmsg;
  135.    cflush();
  136.    if (!IsLocal) {        /* Simulate GETS function for fossil */
  137.       minutes = timeon();        /* Establish a counter */
  138.       while (1) {
  139.          check_carrier();
  140.          while ((ch = FossGetCh()) == 0xFFFF) {
  141.             check_carrier();
  142.             if (timeon() > minutes + IDLETIME)        /* User is idle too long */
  143.                aborterror(USERIDLE,NULL);
  144.          }
  145.          key = ch & 0xff;
  146.          if (key == '\r') {        /* Carraige return */
  147.             break;
  148.          }
  149.          if (key == '\b') {        /* Backspace */
  150.             if (p > inmsg) {
  151.                FossSendCh((byte)key);
  152.                putchar(key);
  153.                FossSendCh(' ');
  154.                putchar(' ');
  155.                FossSendCh((byte) key);
  156.                putchar(key);
  157.                p--;
  158.             }
  159.          }
  160.          else {
  161.             (*p) = (char) key;
  162.             p++;
  163.             FossSendCh((byte) key);
  164.             putchar(key);
  165.          }
  166.          *p = 0;        /* Terminate */
  167.       }
  168.       *(p+1) = 0;
  169.    }
  170.    else gets(inmsg);
  171. }
  172.  
  173. int chrin(void)
  174. {
  175.    unsigned ch;
  176.    int minutes;
  177.  
  178.    cflush();
  179.    if (IsLocal) { 
  180.       ch = _bios_keybrd(_KEYBRD_READ);
  181.       if (ChatOk && ch == 0x2e00) {        /* Chat mode is enabled */
  182.          chat();
  183.          ch = '!';        /* Return saying to restart menu */
  184.       }
  185.       return(ch & 0xff);        /* Return saying to restart menu */
  186.    }
  187.    else {
  188.       minutes = timeon();        /* Establish a counter */
  189.       while ((ch = FossGetCh()) == 0xFFFF) {
  190.          check_carrier();
  191.          if (timeon() > minutes + IDLETIME) {        /* User is idle too long */
  192.             aborterror(USERIDLE,NULL);
  193.          }
  194.       }
  195.       if (ChatOk && ch == 0x2e00) {        /* Chat mode is enabled */
  196.          chat();
  197.          ch = '!';        /* Return saying to restart menu */
  198.       }
  199.       return(ch & 0x00ff);
  200.    }
  201. }
  202.  
  203. int chrinwait(int tminutes)
  204. {
  205.    unsigned ch;
  206.    int minutes;
  207.  
  208.    cflush();
  209.    minutes = timeon();        /* Establish a counter */
  210.    if (IsLocal) {
  211.       while (_bios_keybrd(_KEYBRD_READY)) {
  212.          if (timeon() > minutes + tminutes) {     /* User is idle too long */
  213.             return('\r');
  214.          }
  215.       }
  216.       ch = _bios_keybrd(_KEYBRD_READ);
  217.       if (ChatOk && ch == 0x2e00) {        /* Chat mode is enabled */
  218.          chat();
  219.          ch = '!';        /* Return saying to restart menu */
  220.       }
  221.       return(ch & 0xff);        /* Return saying to restart menu */
  222.    }
  223.    else {
  224.       while ((ch = FossGetCh()) == 0xFFFF) {
  225.          check_carrier();
  226.          if (timeon() > minutes + tminutes) {     /* User is idle too long */
  227.             return('\r');
  228.          }
  229.       }
  230.       if (ChatOk && ch == 0x2e00) {        /* Chat mode is enabled */
  231.          chat();
  232.          ch = '!';        /* Return saying to restart menu */
  233.       }
  234.       return(ch);
  235.    }
  236. }
  237.  
  238. void cflush(void)
  239. {
  240.    while (_bios_keybrd(_KEYBRD_READY))        /* Flush local console */
  241.       getch();
  242.    if (IsLocal)
  243.       return;
  244.    FossPurgeBuff(TRUE);
  245. }
  246.  
  247. int check_carrier(void)
  248. {
  249.    if (IsLocal) 
  250.       return(FALSE);
  251.  
  252.    if ((FossMdmStatus() & prm.carrier_mask) == 0) {        /* Look at DCD(carrier bit) */
  253.       fclose(outfile);
  254.       aborterror(DROPCARRIER,NULL);
  255.    }
  256.    return(FALSE);
  257. }
  258.  
  259. void logit(char *strng,char flag)
  260. {
  261.    FILE *log;
  262.    char ttemp[40];
  263.    char *ttemp1;
  264.    char *month;
  265.    char *day;
  266.  
  267.    if (LogMode == FALSE)
  268.       return;
  269.  
  270.    ttemp1 = (char *) malloc(101);
  271.    if (ttemp1 == NULL) {
  272.       LogMode = FALSE;
  273.       aborterror(BADALLOC,NULL);
  274.    }
  275.  
  276.    strcpy(ttemp, timestring());
  277.    strtok(ttemp," ");               /* Strip Day of week */
  278.    month = strtok(NULL," ");
  279.    day = strtok(NULL," ");
  280.    tm = strtok(NULL," ");
  281.  
  282.    if (Task)
  283.       sprintf(ttemp1,"%c %s %s %s MaxMail{%x}  %s\n",flag,day,month,tm,Task,strng);
  284.    else sprintf(ttemp1,"%c %s %s %s MaxMail  %s\n",flag,day,month,tm,strng);
  285.    if ((log = _fsopen(LogFile,"at+",SH_DENYRW)) == NULL) {
  286.       printf("\a\aError opening log file!\n");
  287.       LogMode = FALSE;
  288.       free(ttemp1);
  289.       return;
  290.    }
  291.    fputs(ttemp1,log);
  292.    free(ttemp1);
  293.    fclose(log);
  294. }
  295.  
  296. char *timestring(void)
  297. {
  298.    time_t     aclock;
  299.    struct tm  *newtime;
  300.  
  301.    time(&aclock);
  302.    newtime = localtime(&aclock);
  303.    return(asctime(newtime));
  304. }
  305.  
  306. void find_class(void)
  307. {
  308.   int x;
  309.  
  310.   for (x=0;x < MAXCLASS;x++)
  311.   {
  312.     if (LastUser.priv==prm.class[x].priv) {
  313.       UserClass=x;
  314.       break;
  315.     }
  316.   }
  317. }
  318.  
  319. void videoline(int row,int col,char *strng)
  320. {
  321.    union REGS r,r1;
  322.    byte scol,srow;
  323.    char *p;
  324.  
  325.    r.h.ah = 03;        /* Get cursor pos */
  326.    r.h.bh = 0;        /* Page 0 */
  327.    p = strng;
  328.  
  329.    int86(0x10,&r,&r);
  330.    scol = r.h.dl;
  331.    srow = r.h.dh;
  332.  
  333.    r.h.ah = 02;        /* Set cursor pos */
  334.    r.h.bh = 0;
  335.    r.h.dh = (byte) row;
  336.    r.h.dl = (byte) col;
  337.    int86(0x10,&r,&r);
  338.  
  339.    r.h.ah = 0x0e;        /* Write character */
  340.    r.h.bh = 0;
  341.    r.h.bl = 0x7d;        /* Black on white */
  342.  
  343.    while (*p) {
  344.       r.h.al = *p;
  345.       int86(0x10,&r,&r1);
  346.       p++;
  347.    }
  348.  
  349.    r.h.ah = 02;        /* Set cursor pos */
  350.    r.h.bh = 0;
  351.    r.h.dh = srow;
  352.    r.h.dl = scol;
  353.    int86(0x10,&r,&r);        /* Restore to original position */
  354. }
  355. /* Clear 25th line */
  356.  
  357. void clearstat(void)
  358. {
  359.    union REGS r;
  360.  
  361.    r.h.ah = 06;        /* Scroll */
  362.    r.h.al = 1;
  363.    r.h.bh = 0;
  364.    r.h.ch = 24;
  365.    r.h.cl = 0;
  366.    r.h.dh = 24;
  367.    r.h.dl = 0;
  368.    int86(0x10,&r,&r);
  369. }
  370.  
  371. void showstat(void)
  372. {
  373.    if (*statline)
  374.       videoline(24,0,statline);
  375. }
  376.  
  377. /* Clear the screen */
  378. void clearscreen(void)
  379. {
  380.    if (LastUser.ansi) 
  381.       strout("\x1b[2J");
  382.    else if (LastUser.avatar) {
  383.       printf("\x1b[2,J");        /* Always send ansi to local console */
  384.       FossSendStr("\x0c");        /* ^L */
  385.    }
  386. }
  387.