home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / tde221.zip / TDECFG.C < prev    next >
C/C++ Source or Header  |  1993-04-01  |  19KB  |  631 lines

  1. /*
  2.  * A configuration utility was written to customize the tde executable file.
  3.  * You only need one file to run tde - the executable.  No configuration files
  4.  * to worry about.  It doesn't take a lot of time to figure out the offsets of
  5.  * the variables to initialize in tde.exe, so it's fairly easy to do.
  6.  *
  7.  * Program name:  tdecfg
  8.  * Author:        Frank Davis
  9.  * Date:          October 5, 1991
  10.  *
  11.  * This program is released into the public domain.  You may distribute
  12.  * it freely, Frank Davis
  13.  */
  14.  
  15.  
  16. /********    EXTREMELY IMPORTANT   ************/
  17. /*
  18.  * If you modify tde and you want to use this configuration utility, it is your
  19.  * responsibility to find the offsets of the variables in your new executable.
  20.  *
  21.  */
  22. /*******     EXTREMELY IMPORTANT   ************/
  23.  
  24.  
  25. #include <bios.h>
  26. #include <dos.h>
  27. #include <io.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #include "tdecfg.h"
  33.  
  34.  
  35. struct vcfg cfg;                /* video stuff */
  36. FILE *tde_exe;                  /* FILE pointer to tde.exe */
  37.  
  38.  
  39. struct screen cfg_choice[] = {
  40.    {5,25,"1.  Change colors" },
  41.    {7,25,"2.  Redefine keys" },
  42.    {9,25,"3.  Install new help screen" },
  43.   {11,25,"4.  Set default modes" },
  44.   {13,25,"5.  Install permanent macro file" },
  45.   {15,25,"6.  Read in a configuration file" },
  46.   {17,25,"7.  Exit" },
  47.  {20,20,"Please enter choice: " },
  48.   {0,0,NULL}
  49. };
  50.  
  51.  
  52. char *greatest_composer_ever = "W. A. Mozart, 1756-1791";
  53.  
  54.  
  55. /*
  56.  * Name:    main
  57.  * Date:    October 5, 1991
  58.  * Notes:   Strategy is fairly straight forward -  1) initialize all the
  59.  *          variables  2) show the user a color sample  3) make the changes
  60.  *          permanent if desired.
  61.  */
  62. void main( int argc, char *argv[] )
  63. {
  64. int rc;
  65. int c;
  66. char fname[82];
  67.  
  68.    puts( "\nEnter tde executable file name (<Enter> = \"tde.exe\")  :" );
  69.    gets( fname );
  70.  
  71.    if (strlen(fname) == 0)
  72.       strcpy( fname, "tde.exe" );
  73.  
  74.    if ((rc = access( fname, EXIST )) != 0) {
  75.       puts( "\nFile not found." );
  76.       exit( 1 );
  77.    } else if ((tde_exe = fopen( fname, "r+b" )) == NULL ) {
  78.       puts( "\nCannot open executable file." );
  79.       exit( 2 );
  80.    }
  81.  
  82.    video_config( );
  83.    cls( );
  84.    show_box( 0, 0, cfg_choice, NORMAL );
  85.    for (rc=0; rc != 1;) {
  86.       xygoto( 42, 20 );
  87.       c = getkey( );
  88.       while (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' &&
  89.              c != '6' && c != '7')
  90.          c = getkey( );
  91.       switch (c) {
  92.          case '1' :
  93.             tdecolor( );
  94.             show_box( 0, 0, cfg_choice, NORMAL );
  95.             break;
  96.          case '2' :
  97.             tdekeys( );
  98.             show_box( 0, 0, cfg_choice, NORMAL );
  99.             break;
  100.          case '3' :
  101.             tdehelp( );
  102.             show_box( 0, 0, cfg_choice, NORMAL );
  103.             break;
  104.          case '4' :
  105.             tdemodes( );
  106.             show_box( 0, 0, cfg_choice, NORMAL );
  107.             break;
  108.          case '5' :
  109.             tdemacro( );
  110.             show_box( 0, 0, cfg_choice, NORMAL );
  111.             break;
  112.          case '6' :
  113.             tdecfgfile( );
  114.             show_box( 0, 0, cfg_choice, NORMAL );
  115.             break;
  116.          case '7' :
  117.             rc = 1;
  118.             break;
  119.       }
  120.    }
  121.    fcloseall( );
  122.    puts( " " );
  123.    puts( " " );
  124. }
  125.  
  126.  
  127. /*
  128.  * Name:    xygoto
  129.  * Date:    July 21, 1991
  130.  * Notes:   Use the video interrupt to set the cursor.
  131.  */
  132. void xygoto( int col, int row )
  133. {
  134. union REGS inregs, outregs;
  135.  
  136.    inregs.h.ah = 2;
  137.    inregs.h.bh = 0;
  138.    inregs.h.dh = row;
  139.    inregs.h.dl = col;
  140.    int86( VIDEO_INT, &inregs, &outregs );
  141. }
  142.  
  143.  
  144. /*
  145.  * Name:    video_config
  146.  * Date:    July 21, 1991
  147.  * Notes:   See main.c for more info.
  148.  */
  149. void video_config( void )
  150. {
  151. #pragma pack( 1 )    /* Use pragma to force packing on byte boundaries. */
  152.  
  153. struct LOWMEMVID
  154. {
  155.    char     vidmode;           /* 0x449 */
  156.    unsigned scrwid;            /* 0x44A */
  157.    unsigned scrlen;            /* 0x44C */
  158.    unsigned scroff;            /* 0x44E */
  159.    struct   LOCATE
  160.    {
  161.       unsigned char col;
  162.       unsigned char row;
  163.    } csrpos[8];                /* 0x450 */
  164.    struct   CURSIZE
  165.    {
  166.       unsigned char end;
  167.       unsigned char start;
  168.    } csrsize;                  /* 0x460 */
  169.    char      page;             /* 0x462 */
  170.    unsigned  addr_6845;        /* 0x463 */
  171.    char      crt_mode_set;     /* 0x465 */
  172.    char      crt_palette;      /* 0x466 */
  173.    char      system_stuff[29]; /* 0x467 */
  174.    char      rows;             /* 0x484 */
  175.    unsigned  points;           /* 0x485 */
  176.    char      ega_info;         /* 0x487 */
  177.    char      info_3;           /* 0x488 */
  178. } vid;
  179. struct LOWMEMVID _far *pvid = &vid;
  180.  
  181. #pragma pack( )    /* revert to previously defined pack pragma. */
  182.  
  183. union REGS in, out;
  184. unsigned char temp, active_display;
  185.  
  186.    /* Move system information into uninitialized structure variable. */
  187.    movedata( 0, 0x449, FP_SEG( pvid ), FP_OFF( pvid ), sizeof( vid ) );
  188.  
  189.    cfg.rescan = FALSE;
  190.    in.x.ax =  0x1a00;
  191.    int86( VIDEO_INT, &in, &out );
  192.    temp = out.h.al;
  193.    active_display = out.h.bl;
  194.    if (temp == 0x1a && (active_display == 7 || active_display == 8))
  195.       cfg.adapter = VGA;
  196.    else {
  197.       in.h.ah =  0x12;
  198.       in.h.bl =  0x10;
  199.       int86( VIDEO_INT, &in, &out );
  200.       if (out.h.bl != 0x10) {         /* EGA */
  201.          if (vid.ega_info & 0x08) {
  202.             if (vid.addr_6845 == 0x3d4)
  203.                cfg.adapter = CGA;
  204.             else
  205.                cfg.adapter = MDA;
  206.          } else
  207.             cfg.adapter = EGA;
  208.       } else if (vid.addr_6845 == 0x3d4)
  209.          cfg.adapter = CGA;
  210.       else
  211.          cfg.adapter = MDA;
  212.    }
  213.  
  214.    if (cfg.adapter == CGA)
  215.       cfg.rescan = TRUE;
  216.  
  217.    cfg.mode = vid.vidmode;
  218.    if (vid.addr_6845 == 0x3D4) {
  219.       cfg.color = TRUE;
  220.       FP_SEG( cfg.videomem ) = 0xb800;
  221.    } else {
  222.       cfg.color = FALSE;
  223.       FP_SEG( cfg.videomem ) = 0xb000;
  224.    }
  225.    FP_OFF( cfg.videomem ) = 0x0000;
  226.    if (cfg.color == TRUE)
  227.       cfg.attr = COLOR_ATTR;
  228.    else
  229.       cfg.attr = MONO_ATTR;
  230.  
  231.    cfg.overscan = vid.crt_palette;
  232. }
  233.  
  234.  
  235. /*
  236.  * Name:    getkey
  237.  * Date:    July 21, 1991
  238.  * Notes:   Waits for keyboard input and returns the key pressed by the user.
  239.  */
  240. int getkey( void )
  241. {
  242. unsigned key;
  243. unsigned lo;
  244.  
  245.    /*
  246.     *  _bios_keybrd == int 16.  It returns the scan code in ah, hi part of key,
  247.     *  and the ascii key code in al, lo part of key.
  248.     */
  249.    key = _bios_keybrd( 0 );
  250.    lo = key & 0X00FF;
  251.    lo = (int)((lo == 0) ? (((key & 0XFF00) >> 8) + 256) : lo);
  252.    return( lo );
  253. }
  254.  
  255.  
  256. /*
  257.  * Name:    s_output
  258.  * Date:    July 21, 1991
  259.  * Passed:  s:     the string to display
  260.  *          line:  line number to begin display
  261.  *          col:   column number to begin display
  262.  *          attr:  color to display string
  263.  * Notes:   See tdeasm.c for more info
  264.  */
  265. void s_output( char far *s, int line, int col, int attr )
  266. {
  267. int far *screen_ptr;
  268. int max_col;
  269. int off;
  270.  
  271.    max_col = 80;
  272.    screen_ptr = cfg.videomem;
  273.    off = line * 160 + col * 2;
  274.  
  275.    ASSEMBLE {
  276.         push    ds              /* MUST save ds */
  277.         push    di              /* save di on stack */
  278.         push    si              /* save si on stack */
  279.  
  280.         mov     bx, WORD PTR attr               /* keep attribute in bx */
  281.         mov     cx, WORD PTR col                /* put cols in cx */
  282.         mov     dx, WORD PTR max_col            /* keep max_col in dx */
  283.         mov     di, WORD PTR screen_ptr         /* load offset of screen ptr */
  284.         add     di, WORD PTR off
  285.         mov     ax, WORD PTR screen_ptr+2       /* load segment of screen ptr */
  286.         mov     es, ax
  287.         mov     si, WORD PTR s  /* load offset of string ptr */
  288.         or      si, si          /* is it == NULL? */
  289.         je      getout          /* yes, no output needed */
  290.         mov     ax, WORD PTR s+2        /* load segment of string ptr */
  291.         or      ax, ax          /* is pointer == NULL? */
  292.         je      getout          /* yes, no output needed */
  293.         mov     ds, ax          /* load segment of text in ds */
  294.         mov     ah, bl          /* put attribute in AH */
  295.    }
  296. top:
  297.  
  298.    ASSEMBLE {
  299.         cmp     cx, dx          /* col < max_cols? */
  300.         jge     getout          /* no, thru with line */
  301.         lodsb                   /* get next char in string - put in al */
  302.         or      al, al          /* is it '\0' */
  303.         je      getout          /* yes, end of string */
  304.         stosw                   /* else show attr + char on screen (ah + al) */
  305.         inc     cx              /* col++ */
  306.         jmp     SHORT top       /* get another character */
  307.    }
  308. getout:
  309.  
  310.    ASSEMBLE {
  311.         pop     si              /* get back si */
  312.         pop     di              /* get back di */
  313.         pop     ds              /* get back ds */
  314.    }
  315. }
  316.  
  317.  
  318. /*
  319.  * Name:    hlight_line
  320.  * Date:    July 21, 1991
  321.  * Passed:  x:     column to begin hi lite
  322.  *          y:     line to begin hi lite
  323.  *          lgth:  number of characters to hi lite
  324.  *          attr:  attribute color
  325.  * Notes:   The attribute byte is the hi byte.
  326.  */
  327. void hlight_line( int x, int y, int lgth, int attr )
  328. {
  329. int off, far *pointer;
  330.  
  331.    pointer = cfg.videomem;
  332.    off = y * 160 + 2 * x + 1;  /* add one - so it points to attribute byte */
  333.    ASSEMBLE {
  334.         push    di              /* save es */
  335.  
  336.         mov     cx, lgth        /* number of characters to change color */
  337.  
  338.         mov     di, WORD PTR pointer    /* get destination - video memory */
  339.         add     di, off                 /* add offset */
  340.         mov     ax, WORD PTR pointer+2
  341.         mov     es, ax
  342.         mov     ax, attr        /* attribute */
  343.    }
  344. lite_len:
  345.  
  346.    ASSEMBLE {
  347.         stosb                   /* store a BYTE */
  348.         inc     di              /* skip over character to next attribute */
  349.         loop    lite_len        /* change next attribute */
  350.         pop     di              /* restore di */
  351.    }
  352. }
  353.  
  354.  
  355. /*
  356.  * Name:    scroll_window
  357.  * Date:    October 5, 1991
  358.  * Passed:  lines:  number of lines to scroll
  359.  *          r1:     row scroll starts on
  360.  *          c1:     column scroll start on
  361.  *          r2:     ending row of scroll
  362.  *          c2:     ending column of scroll
  363.  *          attr:   color of scroll
  364.  * Notes:   Clear a window using color.  Use standard BIOS call.  See
  365.  *          any technical reference for more info on VIDEO BIOS services.
  366.  */
  367. void scroll_window( int lines, int r1, int c1, int r2, int c2, int attr )
  368. {
  369. char rah, ral;
  370.  
  371.    ASSEMBLE {
  372.         mov     ax, lines
  373.         cmp     ax, 0           /* if line < 0  - scroll window down */
  374.         jge     a1
  375.  
  376.         neg     ax                      /* make lines positive */
  377.         mov     BYTE PTR ral, al        /* save number of lines */
  378.         mov     BYTE PTR rah, 7         /* function 7 -  scroll window down */
  379.         dec     r2                      /* decrement row 2 */
  380.         jmp     SHORT a2
  381.    }
  382. a1:
  383.  
  384.    ASSEMBLE {
  385.         mov     BYTE PTR ral, al        /* number of lines to scroll */
  386.         mov     BYTE PTR rah, 6         /* function 6 - scroll window up */
  387.    }
  388. a2:
  389.  
  390.    ASSEMBLE {
  391.         mov     ax, WORD PTR r1         /* get starting row */
  392.         mov     ch, al                  /* put it in ch */
  393.         mov     ax, WORD PTR c1         /* get starting column */
  394.         mov     cl, al                  /* put it in cl */
  395.         mov     ax, WORD PTR r2         /* get ending row */
  396.         mov     dh, al                  /* put it in dh */
  397.         mov     ax, WORD PTR c2         /* get ending column */
  398.         mov     dl, al                  /* put it in cl */
  399.         mov     ax, WORD PTR attr       /* get attribute */
  400.         mov     bh, al                  /* put it in bh */
  401.         mov     ah, BYTE PTR rah        /* get function number */
  402.         mov     al, BYTE PTR ral        /* get number of lines */
  403.         push    bp                      /* ** in some early BIOS versions ** */
  404.         int     VIDEO_INT               /* ** u must save bp              ** */
  405.         pop     bp
  406.    }
  407. }
  408.  
  409.  
  410. /*
  411.  * Name:    cls
  412.  * Date:    October 5, 1991
  413.  * Notes:   Clear screen using color.
  414.  */
  415. void cls( void )
  416. {
  417.    scroll_window( 0, 0, 0, 24, 79, NORMAL );
  418. }
  419.  
  420.  
  421. /*
  422.  * Name:    show_box
  423.  * Date:    October 5, 1991
  424.  * Passed:  x:     number of lines to scroll
  425.  *          y:     row scroll starts on
  426.  *          p:     column scroll start on
  427.  *          attr:  ending row of scroll
  428.  * Notes:   Easy way to show text on screen.  Based on the display techiniques
  429.  *          in a very good morse code practice program, sorry I don't know
  430.  *          the author's name.  Several morse code pratice programs have been
  431.  *          written, but I am refering to the one written in C and released
  432.  *          around 1984-1986.  Seems like the author was living in California
  433.  *          at that time.
  434.  */
  435. void show_box( int x, int y, struct screen *p, int  attr )
  436. {
  437.  
  438.    while (p->text) {
  439.       s_output( p->text, p->row+y, p->col+x, attr );
  440.       p++;
  441.    }
  442. }
  443.  
  444.  
  445. /*
  446.  * Name:    make_window
  447.  * Date:    October 5, 1991
  448.  * Passed:  col:     upper left column to begin window
  449.  *          row:     upper left row to begin window
  450.  *          width:   number of columns in window
  451.  *          height:  number of rows in window
  452.  *          attr:    color of window border
  453.  * Notes:   Draw the outline of the window then clear all contents.
  454.  *          The begin and ending column have to incremented so when the
  455.  *          window is cleared the borders are not wiped out.
  456.  */
  457. void make_window( int col, int row, int width, int height, int attr )
  458. {
  459.    buf_box( col++, row++, width, height, attr );
  460.    clear_window( col, row, width-2, height-2 );
  461. }
  462.  
  463.  
  464. /*
  465.  * Name:    buf_box
  466.  * Date:    October 5, 1991
  467.  * Passed:  col:     upper left column to begin window
  468.  *          row:     upper left row to begin window
  469.  *          width:   number of columns in window
  470.  *          height:  number of rows in window
  471.  *          attr:    color of window border
  472.  * Notes:   Draw the outline of the window.  See tdecfg.h for the outline
  473.  *          characters.
  474.  */
  475. void buf_box( int col, int row, int width, int height, int attr )
  476. {
  477. int  i;
  478. int  row_count;
  479. char string[82];
  480.  
  481.    if (height > 0 && width > 0 && height < 25 && width < 81) {
  482.       row_count = 1;
  483.       string[0]= U_LEFT;
  484.       for (i=1; i<width-1; i++)
  485.          string[i] = HOR_LINE;
  486.       string[i++] = U_RIGHT; string[i] = '\0';
  487.       s_output( string, row, col, attr );
  488.       ++row_count;
  489.       ++row;
  490.  
  491.       if (row_count < height) {
  492.          string[0] = VER_LINE;
  493.          string[1] = '\0';
  494.          for (i=1; i<height-1; i++) {
  495.             s_output( string, row, col, attr );
  496.             s_output( string, row, col+width-1, attr );
  497.             ++row;
  498.             ++row_count;
  499.          }
  500.       }
  501.  
  502.       if (row_count <= height) {
  503.          string[0] = L_LEFT;
  504.          for (i=1; i<width-1; i++)
  505.             string[i] = HOR_LINE;
  506.          string[i++] = L_RIGHT; string[i] = '\0';
  507.          s_output( string, row, col, attr );
  508.       }
  509.    }
  510. }
  511.  
  512.  
  513. /*
  514.  * Name:    clear_window
  515.  * Date:    October 5, 1991
  516.  * Passed:  col:     upper left column to begin window
  517.  *          row:     upper left row to begin window
  518.  *          width:   number of columns in window
  519.  *          height:  number of rows in window
  520.  * Notes:   Clear the insides of the window.
  521.  */
  522. void clear_window( int col, int row, int width, int height )
  523. {
  524. /*   scroll_window( height, row, col, row+height-1, col+width-1, NORMAL ); */
  525.    scroll_window( 0, row, col, row+height-1, col+width-1, NORMAL );
  526. }
  527.  
  528.  
  529. /*
  530.  * Name:    window_control
  531.  * Date:    October 5, 1991
  532.  * Passed:  window_ptr: pointer to pointer of window (head of window stack)
  533.  *          action:     are SAVEing or RESTOREing the contents of a window
  534.  *          col:        upper left column of window
  535.  *          row:        upper left row of window
  536.  *          width:      number of characters in window
  537.  *          height:     number of rows in window
  538.  * Notes:   Save or restore the contents of the screen under a pop-up or
  539.  *          pull-down window.  Use a stack to store the windows so an unlimited
  540.  *          number of windows may be saved and restored.
  541.  */
  542. void window_control( WINDOW **window_ptr, int action, int col, int row,
  543.                      int width, int height )
  544. {
  545. WINDOW  *p;
  546. size_t  store_me;
  547.  
  548.    if (action == SAVE) {
  549.       p = (WINDOW *)malloc( sizeof(WINDOW) );
  550.       if (p != NULL) {
  551.          p->n = NULL;
  552.  
  553.          /*
  554.           * push a window on the stack
  555.           */
  556.          if (*window_ptr != NULL)
  557.             p->n = *window_ptr;
  558.          *window_ptr = p;
  559.          store_me = (width * height) * sizeof( int );
  560.          p->buf = (int *)malloc( store_me );
  561.          save_window( p->buf, col, row, width, height );
  562.       }
  563.    } else if (action == RESTORE) {
  564.       if (*window_ptr != NULL) {
  565.          p = *window_ptr;
  566.          restore_window( p->buf, col, row, width, height );
  567.  
  568.          /*
  569.           * pop a window off the stack
  570.           */
  571.          *window_ptr = p->n;
  572.          free( p->buf );
  573.          free( p );
  574. }  }  }
  575.  
  576.  
  577. /*
  578.  * Name:    save_window
  579.  * Date:    October 5, 1991
  580.  * Passed:  destination: pointer to store contents of screen
  581.  *          col:         upper left column of window
  582.  *          row:         upper left row of window
  583.  *          width:       number of characters in window
  584.  *          height:      number of rows in window
  585.  * Notes:   Do an optimal save.  Save only the contents of the screen that the
  586.  *          window writes over.  Some algorithms save the entire contents of
  587.  *          the screen - wasteful.
  588.  */
  589. void save_window( int *destination, int col, int row, int width, int height )
  590. {
  591. int i, j, offset;
  592. int far *pointer;
  593.  
  594.    pointer = cfg.videomem;
  595.    offset = row * 80 + col;
  596.    pointer += offset;
  597.    for (i=0; i < height; i++) {
  598.       for (j=0; j < width; j++)
  599.          *destination++ = *(pointer + j);
  600.       pointer += 80;
  601.    }
  602. }
  603.  
  604.  
  605. /*
  606.  * Name:    restore_window
  607.  * Date:    October 5, 1991
  608.  * Passed:  source:      pointer of source of contents of screen
  609.  *          col:         upper left column of window
  610.  *          row:         upper left row of window
  611.  *          width:       number of characters in window
  612.  *          height:      number of rows in window
  613.  * Notes:   Do an optimal restore.  Restore only the contents of the screen
  614.  *          that the window writes over.  Some algorithms restore the entire
  615.  *          contents of the screen - wasteful.
  616.  */
  617. void restore_window( int *source, int col, int row, int width, int height )
  618. {
  619. int i, j, offset;
  620. int far *pointer;
  621.  
  622.    pointer = cfg.videomem;
  623.    offset = row * 80 + col;
  624.    pointer += offset;
  625.    for (i=0; i < height; i++) {
  626.       for (j=0; j < width; j++)
  627.          *(pointer + j) = *source++;
  628.       pointer += 80;
  629.    }
  630. }
  631.