home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / calculat / rpn30src.zip / RPNINST.C < prev    next >
C/C++ Source or Header  |  1990-05-30  |  11KB  |  416 lines

  1. /*
  2. | rpninst.c
  3. |
  4. | Install new colors into the screens.
  5. | 90/05/24, for v3.0
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <sys\stat.h>
  11. #include <io.h>
  12. #include <conio.h>
  13. #include <string.h>
  14. #include "display.h"
  15. #include "rpnio.h"
  16. #include "dispscrn.h"
  17. #include "helpscrn.h"
  18.  
  19. #define BUF_LENGTH 4096
  20.  
  21. #define DIALOG_L (H_RIGHT+6)
  22. #define DIALOG_R (H_RIGHT+6+26)
  23. #define DIALOG_T 16
  24. #define DIALOG_B 25
  25.  
  26.  
  27. static int handle;
  28.  
  29. char foremask = 0x0f;
  30.  
  31. char fr_buf[FRAME_SIZE];
  32. char h1_buf[HELP_SIZE];
  33. char h2_buf[HELP_SIZE];
  34.  
  35.  
  36. static char scrn_choice[] =
  37.     "╔════════════════════════╗\r\n"
  38.     "║  rpninst  " __DATE__
  39.  
  40.                    "  ║\r\n"
  41.     "║                        ║\r\n"
  42.     "║  Press  D (Display),   ║\r\n"
  43.     "║    H (Help),  or  Q    ║\r\n"
  44.     "║   to color a screen    ║\r\n"
  45.     "║        or Quit         ║\r\n"
  46.     "╚════════════════════════╝\r\n" ;
  47.  
  48. static char help_colors[] =
  49.     "┌────────────────────────┐\r\n"
  50.     "│ Press  F, I, C, T, R,  │\r\n"
  51.     "│  G, B,  S, Q,  or  O:  │\r\n"
  52.     "│  Ftns  Info  Captions  │\r\n"
  53.     "│  Title       Reset     │\r\n"
  54.     "│  backGround  Border    │\r\n"
  55.     "│  Save  Quit  OtherScrn │\r\n"
  56.     "└────────────────────────┘\r\n" ;
  57.  
  58. static char frame_colors[] =
  59.     "┌────────────────────────┐\r\n"
  60.     "│ Press  N,  F,  E,  R,  │\r\n"
  61.     "│  G,  B,  S,  or  Q :   │\r\n"
  62.     "│  Number      Function  │\r\n"
  63.     "│  Exceptions  Reset     │\r\n"
  64.     "│  backGround  Border    │\r\n"
  65.     "│  Save        Quit      │\r\n"
  66.     "└────────────────────────┘\r\n" ;
  67.  
  68. static char savingmsg[] =
  69.     "╔════════════════════════╗\r\n"
  70.     "║                        ║\r\n"
  71.     "║                        ║\r\n"
  72.     "║                        ║\r\n"
  73.     "║   S A V I N G . . .    ║\r\n"
  74.     "║                        ║\r\n"
  75.     "║                        ║\r\n"
  76.     "╚════════════════════════╝\r\n" ;
  77.  
  78.  
  79. /***************************************************************************/
  80.  
  81. int promptfor(char *scrn, int x, int y)
  82. {
  83.     window(DIALOG_L, DIALOG_T, DIALOG_R, DIALOG_B);
  84.     cputs(scrn);
  85.     gotoxy(x, y);
  86.     return getche() ;
  87. }
  88.  
  89. /***************************************************************************/
  90.  
  91. int cmp2(char *src, char *tgt, unsigned len)
  92. {
  93.     char *t;
  94.     for (t = tgt + len; *src == *tgt; src += 2, tgt += 2)
  95.         if (tgt == t)
  96.         return 0;
  97.     return *src - *tgt;        /* lexical "difference" */
  98. }
  99. /***************************************************************************/
  100.  
  101. long lookfrom(char *target, long start, unsigned len)
  102. {
  103.     long posn;
  104.     int n_read;
  105.     char buf[BUF_LENGTH];
  106.     unsigned bp;
  107.  
  108.     while (start > 0) {
  109.     if ( 0 > (posn = lseek(handle, start, SEEK_SET)) ) {
  110.         perror("lookfrom loop");
  111.         exit(4);
  112.     }
  113.     n_read = read(handle, buf, BUF_LENGTH);
  114.     for (bp = 0; (bp+len) < n_read; ++bp)
  115.         if (0 == cmp2(buf+bp, target, len))
  116.         return posn + bp;            /* Found it! */
  117.  
  118.     start -= BUF_LENGTH;
  119.     }
  120.  
  121.     return -1;                        /* No match! */
  122. }
  123. /***************************************************************************/
  124.  
  125. long location(char *tgt, long start, unsigned size, char *msg)
  126. {
  127.     long pos;
  128.     if ( 0 > (pos = lookfrom(tgt, start, size)) )
  129.     if ( 0 > (pos = lookfrom(tgt, start+BUF_LENGTH/2, size)) ) {
  130.         fputs(msg, stderr);
  131.         exit(3);
  132.     }
  133.     return pos;
  134. }
  135. /***************************************************************************/
  136.  
  137. void scribble(char *f, char *p, char *pend, char color)
  138. {
  139.     for ( ; p < pend; ) {
  140.     *f++ = *p++;
  141.     *f++ = color;
  142.     }
  143. }
  144. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  145.  
  146. char numbers[] = "-98765.43210+e88";
  147. char ftns[]    = "Function-names";
  148. char oops[]    = "Exception Messages";
  149. char blanks[]  = "                  ";
  150.  
  151. void scribbleframe(char *frame)
  152. {
  153.     scribble(frame + (D_WIDTH<<1)+4,
  154.          numbers,
  155.          numbers + strlen(numbers), *(frame+(D_WIDTH<<1)+3));
  156.     scribble(frame + (D_WIDTH<<2)+4,
  157.          ftns,
  158.          ftns + strlen(ftns), *(frame+(D_WIDTH<<2)+3));
  159.     scribble(frame + (D_WIDTH<<2)+(D_WIDTH<<1)+4,
  160.          oops,
  161.          oops + strlen(oops), *(frame+(D_WIDTH<<2)+(D_WIDTH<<1)+3));
  162. }
  163. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  164.  
  165. void erase(char *frame)
  166. {
  167.     scribble(frame + (D_WIDTH<<1)+4,
  168.          blanks,
  169.          blanks + strlen(blanks), *(frame+(D_WIDTH<<1)+1));
  170.     scribble(frame + (D_WIDTH<<2)+4,
  171.          blanks,
  172.          blanks + strlen(blanks), *(frame+(D_WIDTH<<2)+1));
  173.     scribble(frame + (D_WIDTH<<2)+(D_WIDTH<<1)+4,
  174.          blanks,
  175.          blanks + strlen(blanks), *(frame+(D_WIDTH<<2)+(D_WIDTH<<1)+1));
  176. }
  177. /***************************************************************************/
  178.  
  179. void bump_bkgnd(char *dest, char *end)
  180. {
  181.     int b;
  182.  
  183.     for (++dest; dest < end; dest +=2) {
  184.     b = (*dest >> 4) & 0x07;
  185.     if (++b > 0x07)
  186.         b = 0;
  187.     *dest = (*dest & 0x8f) | (b << 4);
  188.     }
  189. }
  190. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  191.  
  192. void reset(char *dest, char *src, char *end)
  193. {
  194.     for (++dest, ++src; src < end; dest +=2, src +=2)
  195.     *dest = *src;
  196. }
  197. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  198.  
  199. void bump_vals(char *scrn, char *master, char *mastend, int index)
  200. {
  201.     int c;
  202.     for ( ; master <= mastend; scrn += 2, master +=2)
  203.     if ( (*master & foremask) == index ) {
  204.         c = (*scrn & foremask);
  205.         if ( ++c > 0x0f )
  206.             c = 0;
  207.         *scrn = (*scrn & 0xf0) | c;
  208.     }
  209. }
  210. /***************************************************************************/
  211.  
  212. int do_frame(void)
  213. {
  214.     int c;
  215.     signed char index;
  216.  
  217.     do {
  218.     puttext(H_LEFT, H_TOP, H_LEFT+D_WIDTH-1, H_TOP+D_DEPTH-1, fr_buf);
  219.     c = promptfor(frame_colors, 12, 3);
  220.  
  221.     if ( '\r' == c || 'q' == c || 'Q' == c)
  222.         return 0;
  223.  
  224.     else if (c == 's' || c == 'S')
  225.         return 1;
  226.  
  227.     else if (c == 'r' || c == 'R')
  228.         reset(fr_buf, frame, frame+FRAME_SIZE);
  229.     else if (c == 'g' || c == 'G')
  230.         bump_bkgnd(fr_buf, fr_buf+FRAME_SIZE);
  231.     else {
  232.         switch (c) {
  233.         case 'e':  case 'E':
  234.         index = DOOPS;      break;
  235.         case 'f':    case 'F':
  236.         index = DFTN;          break;
  237.         case 'b':  case 'B':
  238.         index = DBORD;        break;
  239.         case 'n':    case 'N':
  240.         index = DNUM;        break;
  241.         default: index = -1;
  242.         }
  243.         if (index != -1)
  244.         ;
  245.         bump_vals(fr_buf+1, frame+1, frame+FRAME_SIZE, index);
  246.     }
  247.     } while (1);
  248. }
  249. /***************************************************************************/
  250.  
  251. int do_helps(void)
  252. {
  253.     int c, o = 0;
  254.     char *other[2] = { h1_buf, h2_buf };
  255.     signed char index;
  256.  
  257.     do {
  258.     puttext(H_LEFT, H_TOP, H_LEFT+H_WIDTH-1, H_TOP+H_DEPTH-1, other[o]);
  259.  
  260.     c = promptfor(help_colors, 12, 3);
  261.  
  262.     if ( '\r' == c || 'q' == c || 'Q' == c)
  263.         return 0;
  264.  
  265.     else if (c == 's' || c == 'S')
  266.         return 1;
  267.  
  268.     else if (c == 'o' || c == 'O')
  269.         o = !o;
  270.     else if (c == 'r' || c == 'R') {
  271.         reset(h1_buf, help1, help1+HELP_SIZE);
  272.         reset(h2_buf, help2, help2+HELP_SIZE);
  273.     } else if (c == 'g' || c == 'G') {
  274.         bump_bkgnd(h1_buf, h1_buf+HELP_SIZE);
  275.         bump_bkgnd(h2_buf, h2_buf+HELP_SIZE);
  276.     } else {
  277.         switch (c) {
  278.         case 'f':  case 'F':            /* Ftns */
  279.         index = H_TEXT;        break;
  280.         case 'i':  case 'I':            /* Info */
  281.         index = H_NUM;        break;
  282.         case 'c':  case 'C':            /* Caption */
  283.         index = H_LABEL;    break;
  284.         case 't':  case 'T':
  285.         index = H_TITLE;    break;
  286.         case 'b':  case 'B':
  287.         index = H_BORDER;    break;
  288.         default: index = -1;
  289.         }
  290.         if (index != -1) {
  291.         bump_vals(h1_buf+1, help1+1, help1+HELP_SIZE, index);
  292.         bump_vals(h2_buf+1, help2+1, help2+HELP_SIZE, index);
  293.         }
  294.     }
  295.     } while (1);
  296. }
  297.  
  298. /***************************************************************************/
  299.  
  300. static char orig_scrn[ 2*25*80 ];
  301.  
  302. void warn_save(void)
  303. {
  304.     textattr((BLACK<<4) | LIGHTRED);
  305.     window(DIALOG_L, DIALOG_T, DIALOG_R, DIALOG_B);
  306.     cputs(savingmsg);
  307. }
  308.  
  309. /***************************************************************************/
  310.  
  311. int cdecl main(int argc, char **argv)
  312. {
  313.     char msgfr[] = "Can't find Display !\n";
  314.     char msgh1[] = "Can't find Help 1 !\n";
  315.     char msgh2[] = "Can't find Help 2 !\n";
  316.     char progname[80] = "rpn.exe";
  317.     long frpos, h1pos, h2pos;
  318.     int more = 1;
  319.     long start_posn;
  320.     int x, y;
  321.  
  322.     puts("searching EXE file...");
  323.     x = wherex(); y = wherey();
  324.     if (argc == 2)
  325.     strncpy(progname, argv[1], 79);
  326.     handle = open(progname, O_RDWR | O_BINARY);
  327.     if (handle < 1) {
  328.     perror( progname );
  329.     return 1;
  330.     }
  331.  
  332.     if ( 0 > (start_posn = lseek(handle, 0, SEEK_END) - BUF_LENGTH) )
  333.     start_posn = 0;
  334.     frpos = location(frame, start_posn, FRAME_SIZE, msgfr);
  335.     h2pos = location(help2, frpos, HELP_SIZE, msgh2);
  336.     h1pos = location(help1, h2pos, HELP_SIZE, msgh1);
  337.     scribbleframe(frame);
  338.     gettext(1,1,80,25, orig_scrn);
  339.     textattr((BLUE<<4) | WHITE);
  340.  
  341.     do {
  342.     textattr((BLUE<<4) | WHITE);
  343.     switch ( promptfor(scrn_choice, 12, 7) ) {
  344.     case 'q':  case 'Q':
  345.         more = 0;
  346.         break;
  347.  
  348.     case 'd':  case 'D':
  349.         lseek(handle, frpos, 0);
  350.         read(handle, fr_buf, FRAME_SIZE);
  351.         scribbleframe(fr_buf);
  352.         if (do_frame()) {
  353.         warn_save();
  354.         erase(fr_buf);
  355.         lseek(handle, frpos, 0);
  356.         write(handle, fr_buf, FRAME_SIZE);
  357.         scribbleframe(fr_buf);
  358.         }
  359.         break;
  360.  
  361.     case 'h':  case 'H':
  362.         lseek(handle, h1pos, 0);
  363.         read(handle, h1_buf, HELP_SIZE);
  364.         lseek(handle, h2pos, 0);
  365.         read(handle, h2_buf, HELP_SIZE);
  366.         if ( do_helps()) {
  367.         warn_save();
  368.         lseek(handle, h1pos, 0);
  369.         write(handle, h1_buf, HELP_SIZE);
  370.         lseek(handle, h2pos, 0);
  371.         write(handle, h2_buf, HELP_SIZE);
  372.         }
  373.     }
  374.     puttext(1,1,80,25, orig_scrn);
  375.     } while (more);
  376.  
  377.     close(handle);
  378.     puttext(1,1,80,25, orig_scrn);
  379.     window(1,1,80,25);
  380.     gotoxy(x, y);
  381.     puts("rpninst done.");
  382.     return 0;
  383. }
  384. /***************************************************************************/
  385.  
  386. /*************** DEBUG STUFF ******************************************/
  387.  
  388. #ifdef DEBUG
  389. #define DBG_CPRINTF(lst)    { \
  390.     window(DIALOG_L, DIALOG_T, DIALOG_R, DIALOG_B); \
  391.     cprintf lst;             }
  392. #define DBG1_CPRINTF(lst)    { \
  393.     window(DIALOG_L, DIALOG_T, DIALOG_R, DIALOG_B); \
  394.     cprintf lst;             }
  395. #define DBG_GETCH()    getch();
  396.  
  397.     DBG_CPRINTF((
  398.     "╔════════════════════════╗\r\n"
  399.     "║  border %02x  title %02x   ║\r\n"
  400.     "║   label %02x   text %02x   ║\r\n"
  401.     "║     num %02x             ║\r\n"
  402.     "║                        ║\r\n"
  403.     "║    dnum %02x    ftn %02x   ║\r\n"
  404.     "║         oops %02x        ║\r\n"
  405.     "╚════════════════════════╝\r\n",
  406.     NBORDER, NTITLE, NLABEL, NTEXT, NNUM, NDNUM, NFTN, NOOPS ));
  407.     DBG_GETCH();
  408.  
  409.  
  410. #else
  411. #define DBG_CPRINTF(lst)
  412. #define DBG1_CPRINTF(lst)
  413. #define DBG_GETCH()
  414. #endif
  415. /*************** DEBUG STUFF ******************************************/
  416.