home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / EDITORS / TDE20.ZIP / TDECFG.C < prev    next >
C/C++ Source or Header  |  1992-06-05  |  19KB  |  610 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[30];  /* 0x466 */
  173.    char      rows;             /* 0x484 */
  174.    unsigned  points;           /* 0x485 */
  175.    char      ega_info;         /* 0x487 */
  176.    char      info_3;           /* 0x488 */
  177. } vid;
  178. struct LOWMEMVID _far *pvid = &vid;
  179.  
  180. #pragma pack( )    /* revert to previously defined pack pragma. */
  181.  
  182. union REGS in, out;
  183. unsigned char temp, active_display;
  184.  
  185.    /* Move system information into uninitialized structure variable. */
  186.    movedata( 0, 0x449, FP_SEG( pvid ), FP_OFF( pvid ), sizeof( vid ) );
  187.  
  188.    cfg.rescan = FALSE;
  189.    in.x.ax =  0x1a00;
  190.    int86( VIDEO_INT, &in, &out );
  191.    temp = out.h.al;
  192.    active_display = out.h.bl;
  193.    if (temp == 0x1a && (active_display == 7 || active_display == 8))
  194.       cfg.adapter = VGA;
  195.    else {
  196.       in.h.ah =  0x12;
  197.       in.h.bl =  0x10;
  198.       int86( VIDEO_INT, &in, &out );
  199.       if (out.h.bl != 0x10) {         /* EGA */
  200.          if (vid.ega_info & 0x08) {
  201.             if (vid.addr_6845 == 0x3d4)
  202.                cfg.adapter = CGA;
  203.             else
  204.                cfg.adapter = MDA;
  205.          } else
  206.             cfg.adapter = EGA;
  207.       } else if (vid.addr_6845 == 0x3d4)
  208.          cfg.adapter = CGA;
  209.       else
  210.          cfg.adapter = MDA;
  211.    }
  212.  
  213.    if (cfg.adapter == CGA)
  214.       cfg.rescan = TRUE;
  215.  
  216.    cfg.mode = vid.vidmode;
  217.    if (vid.addr_6845 == 0x3D4) {
  218.       cfg.color = TRUE;
  219.       FP_SEG( cfg.videomem ) = 0xb800;
  220.    } else {
  221.       cfg.color = FALSE;
  222.       FP_SEG( cfg.videomem ) = 0xb000;
  223.    }
  224.    FP_OFF( cfg.videomem ) = 0x0000;
  225.    if (cfg.color == TRUE)
  226.       cfg.attr = COLOR_ATTR;
  227.    else
  228.       cfg.attr = MONO_ATTR;
  229. }
  230.  
  231.  
  232. /*
  233.  * Name:    getkey
  234.  * Date:    July 21, 1991
  235.  * Notes:   Waits for keyboard input and returns the key pressed by the user.
  236.  */
  237. int getkey( void )
  238. {
  239. unsigned key, lo, scan;
  240.  
  241.    /*
  242.     *  _bios_keybrd == int 16.  It returns the scan code in ah, hi part of key,
  243.     *  and the ascii key code in al, lo part of key.
  244.     */
  245.    key = _bios_keybrd( 0 );
  246.    lo = key & 0X00FF;
  247.    lo = (int)((lo == 0) ? (((key & 0XFF00) >> 8) + 256) : lo);
  248.    return( lo );
  249. }
  250.  
  251.  
  252. /*
  253.  * Name:    s_output
  254.  * Date:    July 21, 1991
  255.  * Passed:  s:     the string to display
  256.  *          line:  line number to begin display
  257.  *          col:   column number to begin display
  258.  *          attr:  color to display string
  259.  * Notes:   See tdeasm.c for more info
  260.  */
  261. void s_output( char far *s, int line, int col, int attr )
  262. {
  263. int far *screen_ptr;
  264. int max_col;
  265. int off;
  266.  
  267.    max_col = 80;
  268.    screen_ptr = cfg.videomem;
  269.    off = line * 160 + col * 2;
  270.  
  271.    _asm {
  272.         push    ds              ; MUST save ds
  273.         push    di              ; save di on stack
  274.         push    si              ; save si on stack
  275.  
  276.         mov     bx, WORD PTR attr               ; keep attribute in bx
  277.         mov     cx, WORD PTR col                ; put cols in cx
  278.         mov     dx, WORD PTR max_col            ; keep max_col in dx
  279.         mov     di, WORD PTR screen_ptr         ; load offset of screen ptr
  280.         add     di, WORD PTR off
  281.         mov     ax, WORD PTR screen_ptr+2       ; load segment of screen ptr
  282.         mov     es, ax
  283.         mov     si, WORD PTR s  ; load offset of string ptr
  284.         or      si, si          ; is it == NULL?
  285.         je      getout          ; yes, no output needed
  286.         mov     ax, WORD PTR s+2        ; load segment of string ptr
  287.         or      ax, ax          ; is pointer == NULL?
  288.         je      getout          ; yes, no output needed
  289.         mov     ds, ax          ; load segment of text in ds
  290.         mov     ah, bl          ; put attribute in AH
  291. top:
  292.         cmp     cx, dx          ; col < max_cols?
  293.         jge     getout          ; no, thru with line
  294.         lodsb                   ; get next char in string - put in al
  295.         or      al, al          ; is it '\0'
  296.         je      getout          ; yes, end of string
  297.         stosw                   ; else show attr + char on screen (ah + al)
  298.         inc     cx              ; col++
  299.         jmp     SHORT top       ; get another character
  300. getout:
  301.         pop     si              ; get back si
  302.         pop     di              ; get back di
  303.         pop     ds              ; get back ds
  304.    }
  305. }
  306.  
  307.  
  308. /*
  309.  * Name:    hlight_line
  310.  * Date:    July 21, 1991
  311.  * Passed:  x:     column to begin hi lite
  312.  *          y:     line to begin hi lite
  313.  *          lgth:  number of characters to hi lite
  314.  *          attr:  attribute color
  315.  * Notes:   The attribute byte is the hi byte.
  316.  */
  317. void hlight_line( int x, int y, int lgth, int attr )
  318. {
  319. int off, far *pointer;
  320.  
  321.    pointer = cfg.videomem;
  322.    off = y * 160 + 2 * x + 1;  /* add one - so it points to attribute byte */
  323.    _asm {
  324.         push    di              ; save es
  325.  
  326.         mov     cx, lgth        ; number of characters to change color
  327.  
  328.         mov     di, WORD PTR pointer    ; get destination - video memory
  329.         add     di, off                 ; add offset
  330.         mov     ax, WORD PTR pointer+2  ;
  331.         mov     es, ax
  332.         mov     ax, attr        ; attribute
  333. lite_len:
  334.         stosb                   ; store a BYTE
  335.         inc     di              ; skip over character to next attribute
  336.         loop    lite_len        ; change next attribute
  337.         pop     di              ; restore di
  338.    }
  339. }
  340.  
  341.  
  342. /*
  343.  * Name:    scroll_window
  344.  * Date:    October 5, 1991
  345.  * Passed:  lines:  number of lines to scroll
  346.  *          r1:     row scroll starts on
  347.  *          c1:     column scroll start on
  348.  *          r2:     ending row of scroll
  349.  *          c2:     ending column of scroll
  350.  *          attr:   color of scroll
  351.  * Notes:   Clear a window using color.  Use standard BIOS call.  See
  352.  *          any technical reference for more info on VIDEO BIOS services.
  353.  */
  354. void scroll_window( int lines, int r1, int c1, int r2, int c2, int attr )
  355. {
  356. char rah, ral;
  357.    _asm {
  358.         mov     ax, lines
  359.         cmp     ax, 0           ; if line < 0  - scroll window down
  360.         jge     a1
  361.  
  362.         neg     ax                      ; make lines positive
  363.         mov     BYTE PTR ral, al        ; save number of lines
  364.         mov     BYTE PTR rah, 7         ; function 7 -  scroll window down
  365.         dec     r2                      ; decrement row 2
  366.         jmp     SHORT a2
  367. a1:
  368.         mov     BYTE PTR ral, al        ; number of lines to scroll
  369.         mov     BYTE PTR rah, 6         ; function 6 - scroll window up
  370. a2:
  371.         mov     ax, WORD PTR r1         ; get starting row
  372.         mov     ch, al                  ; put it in ch
  373.         mov     ax, WORD PTR c1         ; get starting column
  374.         mov     cl, al                  ; put it in cl
  375.         mov     ax, WORD PTR r2         ; get ending row
  376.         mov     dh, al                  ; put it in dh
  377.         mov     ax, WORD PTR c2         ; get ending column
  378.         mov     dl, al                  ; put it in cl
  379.         mov     ax, WORD PTR attr       ; get attribute
  380.         mov     bh, al                  ; put it in bh
  381.         mov     ah, BYTE PTR rah        ; get function number
  382.         mov     al, BYTE PTR ral        ; get number of lines
  383.         push    bp                      ; *** in some early versions of ***
  384.         int     VIDEO_INT               ; *** DOS, u must save bp       ***
  385.         pop     bp
  386.    }
  387. }
  388.  
  389.  
  390. /*
  391.  * Name:    cls
  392.  * Date:    October 5, 1991
  393.  * Notes:   Clear screen using color.
  394.  */
  395. void cls( void )
  396. {
  397.    scroll_window( 0, 0, 0, 24, 79, NORMAL );
  398. }
  399.  
  400.  
  401. /*
  402.  * Name:    show_box
  403.  * Date:    October 5, 1991
  404.  * Passed:  x:     number of lines to scroll
  405.  *          y:     row scroll starts on
  406.  *          p:     column scroll start on
  407.  *          attr:  ending row of scroll
  408.  * Notes:   Easy way to show text on screen.  Based on the display techiniques
  409.  *          in a very good morse code practice program, sorry I don't know
  410.  *          the author's name.  Several morse code pratice programs have been
  411.  *          written, but I am refering to the one written in C and released
  412.  *          around 1984-1986.  Seems like the author was living in California
  413.  *          at that time.
  414.  */
  415. void show_box( int x, int y, struct screen *p, int  attr )
  416. {
  417.  
  418.    while (p->text) {
  419.       s_output( p->text, p->row+y, p->col+x, attr );
  420.       p++;
  421.    }
  422. }
  423.  
  424.  
  425. /*
  426.  * Name:    make_window
  427.  * Date:    October 5, 1991
  428.  * Passed:  col:     upper left column to begin window
  429.  *          row:     upper left row to begin window
  430.  *          width:   number of columns in window
  431.  *          height:  number of rows in window
  432.  *          attr:    color of window border
  433.  * Notes:   Draw the outline of the window then clear all contents.
  434.  *          The begin and ending column have to incremented so when the
  435.  *          window is cleared the borders are not wiped out.
  436.  */
  437. void make_window( int col, int row, int width, int height, int attr )
  438. {
  439.    buf_box( col++, row++, width, height, attr );
  440.    clear_window( col, row, width-2, height-2 );
  441. }
  442.  
  443.  
  444. /*
  445.  * Name:    buf_box
  446.  * Date:    October 5, 1991
  447.  * Passed:  col:     upper left column to begin window
  448.  *          row:     upper left row to begin window
  449.  *          width:   number of columns in window
  450.  *          height:  number of rows in window
  451.  *          attr:    color of window border
  452.  * Notes:   Draw the outline of the window.  See tdecfg.h for the outline
  453.  *          characters.
  454.  */
  455. void buf_box( int col, int row, int width, int height, int attr )
  456. {
  457. int i, j, row_count;
  458. char string[82];
  459.  
  460.    if (height > 0 && width > 0 && height < 25 && width < 81) {
  461.       row_count = 1;
  462.       string[0]= U_LEFT;
  463.       for (i=1; i<width-1; i++)
  464.          string[i] = HOR_LINE;
  465.       string[i++] = U_RIGHT; string[i] = '\0';
  466.       s_output( string, row, col, attr );
  467.       ++row_count;
  468.       ++row;
  469.  
  470.       if (row_count < height) {
  471.          string[0] = VER_LINE;
  472.          string[1] = '\0';
  473.          for (i=1; i<height-1; i++) {
  474.             s_output( string, row, col, attr );
  475.             s_output( string, row, col+width-1, attr );
  476.             ++row;
  477.             ++row_count;
  478.          }
  479.       }
  480.  
  481.       if (row_count <= height) {
  482.          string[0] = L_LEFT;
  483.          for (i=1; i<width-1; i++)
  484.             string[i] = HOR_LINE;
  485.          string[i++] = L_RIGHT; string[i] = '\0';
  486.          s_output( string, row, col, attr );
  487.       }
  488.    }
  489. }
  490.  
  491.  
  492. /*
  493.  * Name:    clear_window
  494.  * Date:    October 5, 1991
  495.  * Passed:  col:     upper left column to begin window
  496.  *          row:     upper left row to begin window
  497.  *          width:   number of columns in window
  498.  *          height:  number of rows in window
  499.  * Notes:   Clear the insides of the window.
  500.  */
  501. void clear_window( int col, int row, int width, int height )
  502. {
  503. /*   scroll_window( height, row, col, row+height-1, col+width-1, NORMAL ); */
  504.    scroll_window( 0, row, col, row+height-1, col+width-1, NORMAL );
  505. }
  506.  
  507.  
  508. /*
  509.  * Name:    window_control
  510.  * Date:    October 5, 1991
  511.  * Passed:  window_ptr: pointer to pointer of window (head of window stack)
  512.  *          action:     are SAVEing or RESTOREing the contents of a window
  513.  *          col:        upper left column of window
  514.  *          row:        upper left row of window
  515.  *          width:      number of characters in window
  516.  *          height:     number of rows in window
  517.  * Notes:   Save or restore the contents of the screen under a pop-up or
  518.  *          pull-down window.  Use a stack to store the windows so an unlimited
  519.  *          number of windows may be saved and restored.
  520.  */
  521. void window_control( WINDOW **window_ptr, int action, int col, int row,
  522.                      int width, int height )
  523. {
  524. WINDOW  *p;
  525. size_t  store_me;
  526.  
  527.    if (action == SAVE) {
  528.       p = (WINDOW *)malloc( sizeof(WINDOW) );
  529.       if (p != NULL) {
  530.          p->n = NULL;
  531.  
  532.          /*
  533.           * push a window on the stack
  534.           */
  535.          if (*window_ptr != NULL)
  536.             p->n = *window_ptr;
  537.          *window_ptr = p;
  538.          store_me = (width * height) * sizeof( int );
  539.          p->buf = (int *)malloc( store_me );
  540.          save_window( p->buf, col, row, width, height );
  541.       }
  542.    } else if (action == RESTORE) {
  543.       if (*window_ptr != NULL) {
  544.          p = *window_ptr;
  545.          restore_window( p->buf, col, row, width, height );
  546.  
  547.          /*
  548.           * pop a window off the stack
  549.           */
  550.          *window_ptr = p->n;
  551.          free( p->buf );
  552.          free( p );
  553. }  }  }
  554.  
  555.  
  556. /*
  557.  * Name:    save_window
  558.  * Date:    October 5, 1991
  559.  * Passed:  destination: pointer to store contents of screen
  560.  *          col:         upper left column of window
  561.  *          row:         upper left row of window
  562.  *          width:       number of characters in window
  563.  *          height:      number of rows in window
  564.  * Notes:   Do an optimal save.  Save only the contents of the screen that the
  565.  *          window writes over.  Some algorithms save the entire contents of
  566.  *          the screen - wasteful.
  567.  */
  568. void save_window( int *destination, int col, int row, int width, int height )
  569. {
  570. int i, j, offset;
  571. int far *pointer;
  572.  
  573.    pointer = cfg.videomem;
  574.    offset = row * 80 + col;
  575.    pointer += offset;
  576.    for (i=0; i < height; i++) {
  577.       for (j=0; j < width; j++)
  578.          *destination++ = *(pointer + j);
  579.       pointer += 80;
  580.    }
  581. }
  582.  
  583.  
  584. /*
  585.  * Name:    restore_window
  586.  * Date:    October 5, 1991
  587.  * Passed:  source:      pointer of source of contents of screen
  588.  *          col:         upper left column of window
  589.  *          row:         upper left row of window
  590.  *          width:       number of characters in window
  591.  *          height:      number of rows in window
  592.  * Notes:   Do an optimal restore.  Restore only the contents of the screen
  593.  *          that the window writes over.  Some algorithms restore the entire
  594.  *          contents of the screen - wasteful.
  595.  */
  596. void restore_window( int *source, int col, int row, int width, int height )
  597. {
  598. int i, j, offset;
  599. int far *pointer;
  600.  
  601.    pointer = cfg.videomem;
  602.    offset = row * 80 + col;
  603.    pointer += offset;
  604.    for (i=0; i < height; i++) {
  605.       for (j=0; j < width; j++)
  606.          *(pointer + j) = *source++;
  607.       pointer += 80;
  608.    }
  609. }
  610.