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