home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 181_01 / sp.c < prev    next >
Text File  |  1985-12-13  |  16KB  |  452 lines

  1. /*
  2. *****************************************************************************
  3.  
  4.    IBM Graphics Printer Setup Program - Requires ANSI.SYS to operate
  5.  
  6.  
  7.    Future enhancements: 
  8.       Add a locate() function to operate just like color().  It will run
  9.          slower and require two printf's, but will be more modular,
  10.          and will convert more easily to using BIOS external functions.
  11.       Change paint_page_1 to return two things: the option chosen (so that
  12.          it can be passed to paint_page_2), and an indicator to tell whether
  13.          or not to call paint_page_2.
  14.  
  15. *****************************************************************************
  16. */
  17.  
  18. /*
  19. *****************************************************************************
  20.  
  21.    C header files
  22.    These identify library routines.  LINT_ARGS must be defined first.
  23.  
  24. *****************************************************************************
  25. */
  26.  
  27. #define LINT_ARGS = 1      /* value doesn't matter, only definition does   */
  28.  
  29. #include <conio.h>         /* for console i/o                              */
  30. #include <dos.h>           /* for DOS interrupt calls                      */
  31. #include <stdio.h>         /* for standard i/o                             */
  32. #include <string.h>        /* for string functions                         */
  33.  
  34. /*
  35. *****************************************************************************
  36.  
  37.    Macros
  38.  
  39. *****************************************************************************
  40. */
  41.  
  42. /* This macro returns the number of elements of its array argument         */
  43.  
  44. #define SIZE(x)         ( sizeof(x) / sizeof( x[0] ) )
  45.  
  46. /* These macros validate the parameters to the color function              */
  47.  
  48. #define VALID_FG(X)  ( ( X <= 7 && X > 0 ) ? (X) : (7) )
  49. #define VALID_BG(X)  ( ( X <= 7 && X > 0 ) ? (X) : (0) )
  50.  
  51. /*
  52. *****************************************************************************
  53.  
  54.    Constants and global variables
  55.  
  56. *****************************************************************************
  57. */
  58.  
  59. #define CHAR_H         "H"           /* ANSI command for cursor placement  */
  60. #define CHAR_m         "m"           /* ANSI command for setting color     */
  61. #define ESCAPE_BRACKET "\x1b["       /* First part of ANSI screen command  */
  62. #define SEMICOLON      ";"
  63. #define CLEAR_SCREEN   "\x1b[2J"     /* ANSI clear screen                  */
  64. #define BLACK          0             /* ANSI black                         */
  65. #define RED            1             /* ANSI red                           */
  66. #define GREEN          2             /* ANSI green                         */
  67. #define YELLOW         3             /* ANSI yellow                        */
  68. #define BLUE           4             /* ANSI blue                          */
  69. #define MAGENTA        5             /* ANSI magenta (purple)              */
  70. #define CYAN           6             /* ANSI cyan (lt blue)                */
  71. #define WHITE          7             /* ANSI white                         */
  72. #define MAX_HISTORY   18             /* The max nbr characters history kept*/
  73.                                      /*    so that option_history_prt      */
  74.                                      /*    string is not busted            */
  75. #define MAX_HISTORY_PRT  25          /* The max size of the history string */
  76.  
  77. char *option_hist_loc[] = { "24", "43" };
  78. char option_history[25] = "";
  79. char two_spaces[3]      = "  ";
  80. char option_history_prt[26] = "";
  81.  
  82. /*
  83. *****************************************************************************
  84.  
  85.    main() - Beginning of the code 
  86.  
  87. *****************************************************************************
  88. */
  89.  
  90. main()
  91.  
  92. {
  93.  
  94.    int option;
  95.    int repaint_page_1 = 1;
  96.  
  97.    color( WHITE, BLACK );
  98.    option = paint_page_1( repaint_page_1 );
  99.    repaint_page_1 = 0;
  100.  
  101.    while ( option ) {
  102.       if ( option == 14 || option == 15 ) {
  103.          paint_page_2( option );
  104.          repaint_page_1 = 1;
  105.       }
  106.       option = paint_page_1( repaint_page_1 );
  107.       repaint_page_1 = 0;
  108.    }
  109.  
  110.    return;
  111. }
  112.  
  113. /*
  114. ******************************************************************************
  115.  
  116.  paint_page_1()   Displays the text for option page
  117.  
  118. ******************************************************************************
  119. */
  120.  
  121. int paint_page_1(repaint_page_1)
  122.  
  123. int repaint_page_1;
  124.  
  125. {
  126.    register int i;
  127.    register int j;
  128.  
  129.    static char option_resp[5];       /* holds keyboard response             */
  130.    static int  nbr_cur_options = 20; /* number options currently            */
  131.    static int  option;               /* option chosen                       */
  132.    static char *cmp_ptr;             /* pointer to command chosen           */
  133.    static char *temp_ptr;            /* use when shrinking history          */
  134.  
  135.    static char *option_resp_loc[] = { "24", "18" };
  136.    static char *input_resp;
  137.  
  138.    static char *pg1_row_loc[] = {
  139.        "2",  "4",  "4",
  140.        "6",  "6",  "7",  "7",  "8",  "8",  "9",  "9",
  141.       "10", "10", "11", "11", "12", "12", "13", "13", "14", "14", "15", "15",
  142.       "20", "22", "24", "24", ""
  143.    };
  144.  
  145.    static char *pg1_col_loc[] = {
  146.       "20",  "8", "39",
  147.       "10", "40", "10", "40", "10", "40", "10", "40",
  148.       "10", "40", "10", "40", "10", "40", "10", "40", "10", "40",  "9", "40",
  149.        "8", "10", "10", "25"
  150.    };
  151.  
  152.    static char *pg1_opt_msg[] = {
  153.       "IBM Graphics Printer Setup Program",
  154.       "Opt   Description",
  155.       "Opt   Description",
  156.       "1 = 1/8\x22 Spacing",
  157.       "11 = Start Double Strike",
  158.       "2 = 1/6\x22 Spacing",
  159.       "12 = End Double Strike",
  160.       "3 = 6 Spaces Over Perforation",
  161.       "13 = FF (Hold Cmds till done)",
  162.       "4 = Cancel Skip Perforation",
  163.       "14 = Variable Line Feeding",
  164.       "5 = Start Compressed",
  165.       "15 = Variable Perf Skipping",
  166.       "6 = End Compressed",
  167.       "16 = 1/8\x22, Skip 6, Compressed",
  168.       "7 = Start Emphasized",
  169.       "17 = 1/6\x22, No Skip, No Compressed",
  170.       "8 = End Emphasized",
  171.       "18 = Reset (Sets Top of Form",
  172.       "9 = Start Unidirectional",
  173.       "19 = Start Correspondence Quality",
  174.       "10 = End Unidirectional",
  175.       "20 = Stop Correspondence Quality",
  176.       "Please Enter Options One At A Time, Each Followed By An Enter <\xc4\xd9",
  177.       "(To Terminate Program, Press Enter By Itself)",
  178.       "Option:",
  179.       "Previous Choices:"
  180.    };
  181.  
  182.    static char *pg1_prn_cmds[] = {
  183.       "0",
  184.       "1\x1b0",
  185.       "1\x1b3\x24",
  186.       "1\x1bN\x06",
  187.       "1\x1bO",
  188.       "1\x0f",
  189.       "1\x12",
  190.       "1\x1bE",
  191.       "1\x1bF",
  192.       "1\x1bU\x01",
  193.       "1\x1bU\x00",
  194.       "1\x1bG",
  195.       "1\x1bH",
  196.       "1\x0c",
  197.       "0",
  198.       "0",
  199.       "1\x1b0\x0f\x1bN\x06",
  200.       "1\x1b3\x24\x12\x1bO",
  201.       "0",
  202.       "1\x1bX1",
  203.       "1\x1bXO"
  204.    };
  205.  
  206.    if ( repaint_page_1 ) {
  207.       printf ("%s",CLEAR_SCREEN);
  208.       color( GREEN, BLACK );
  209.       for ( i = 0 ; i <= 2 ; i++ )
  210.          printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, pg1_row_loc[i], SEMICOLON,
  211.                                   pg1_col_loc[i], CHAR_H, pg1_opt_msg[i] );
  212.       color( YELLOW, BLACK );
  213.       for ( j = 0 ; j <= 19 ; j++ ) {
  214.          printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, pg1_row_loc[i], SEMICOLON,
  215.                                   pg1_col_loc[i], CHAR_H, pg1_opt_msg[i] );
  216.          i++;
  217.       }
  218.       color( CYAN, BLACK );
  219.       while ( *pg1_row_loc[i] ) {
  220.          printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, pg1_row_loc[i], SEMICOLON,
  221.                                     pg1_col_loc[i], CHAR_H, pg1_opt_msg [i] );
  222.          i++;
  223.       }
  224.    }
  225.  
  226.    /* print the option history                                             */
  227.    printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, option_hist_loc[0], 
  228.                             SEMICOLON, option_hist_loc[1], 
  229.                             CHAR_H, option_history_prt );
  230.  
  231.    /* clear out the option input field                                     */
  232.    printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, option_resp_loc[0], 
  233.                             SEMICOLON, option_resp_loc[1], CHAR_H, two_spaces );
  234.  
  235.    /* position the cursor for option input                                 */
  236.    printf ( "%s%s%s%s%s", ESCAPE_BRACKET, option_resp_loc[0], 
  237.                           SEMICOLON, option_resp_loc[1], CHAR_H );
  238.  
  239.    /* get up to 2 characters, and convert to integer,                      */
  240.    option_resp[0] = 3;              /* max nbr input chars, including <CR> */
  241.    input_resp = cgets( option_resp );
  242.    option = atoi( input_resp );
  243.    /* process the option if it is valid                                    */
  244.    if ( option > 0 && option <= nbr_cur_options ) {
  245.       /* add a comma unless the string is null to start with               */
  246.       if ( *option_history != '\0' ) {
  247.          strcat ( option_history, "," );
  248.       }
  249.       /* add this input to the history                                     */
  250.       manage_history( input_resp );
  251.       /* if first byte of command is one, then execute the command         */
  252.       cmp_ptr = pg1_prn_cmds[option];
  253.       if ( strncmp( cmp_ptr, "1", 1 ) == 0 ) {
  254.          cmp_ptr++;
  255.          fputs( cmp_ptr, stdprn );
  256.       }
  257.    }
  258.    return( option );
  259. }
  260.  
  261. /*
  262. ******************************************************************************
  263.  
  264.  paint_page_2()   Displays the text for auxiliary option page
  265.  
  266. ******************************************************************************
  267. */
  268.  
  269. int paint_page_2( option )
  270.  
  271. int option;
  272.  
  273. {
  274.    register int i;
  275.    register int j;
  276.  
  277.    static char amount_resp[6];       /* holds keyboard response             */
  278.    static char option_string[5];     /* holds converted option              */
  279.    static int  amount;               /* integer amount entered              */
  280.  
  281.    static char *amount_resp_loc[] = { "24", "18" };
  282.    static char *option_cur_loc[]  = { "18", "51" };
  283.    static char *amount_resp_ptr;
  284.    static char *temp_ptr;            /* use when shrinking history          */
  285.    static char *cmp_ptr;             /* pointer to command chosen           */
  286.  
  287.    static char *pg2_row_loc[] = {
  288.        "2",  "4",  "5",  "6",  "7",  "9", "10", "11", "12", "18",
  289.       "20", "22", "24", "24", ""
  290.    };
  291.  
  292.    static char *pg2_col_loc[] = {
  293.       "20",  "5", "10", "10", "10",  "5", "10", "10", "10", "10", "5",
  294.       "10", "10", "25"
  295.    };
  296.  
  297.    static char *pg2_opt_msg[] = {
  298.       "Auxiliary Input Screen",
  299.       "For Variable Line Feeding: (Option 14)",
  300.       "Enter a number between 1 and 255.  The line feed will be adjusted",
  301.       "to become the entered number / 216 of an inch.  For example, for",
  302.       "1/4\x22 spacing enter 54 (54/216 = 1/4).",
  303.       "For Variable Skipping Over Perforation: (Option 15)",
  304.       "Enter a number between 1 and 127.  The number of lines skipped",
  305.       "is subtracted from the number of lines on the page to determine",
  306.       "the number of lines to print before the skip is performed.",
  307.       "The Option Currently Being Processed is:",
  308.       "Please Enter The Number Desired, Following It By An Enter <\xc4\xd9",
  309.       "(To Return To The Option Screen, Press Enter By Itself)",
  310.       "Amount:",
  311.       "Previous Choices:"
  312.    };
  313.  
  314.    static char *pg2_prn_cmds[] = {
  315.       "0",
  316.       "0",
  317.       "0",
  318.       "0",
  319.       "0",
  320.       "0",
  321.       "0",
  322.       "0",
  323.       "0",
  324.       "0",
  325.       "0",
  326.       "0",
  327.       "0",
  328.       "0",
  329.       "1\x1b3\xff",
  330.       "1\x1bN\x7f",
  331.       "0",
  332.       "0",
  333.       "0",
  334.       "0",
  335.       "0"
  336.    };
  337.  
  338.    itoa( option, option_string, 10 );
  339.    printf ( "%s", CLEAR_SCREEN );
  340.    color( GREEN, BLACK );
  341.    for ( i = 0 ; i <= 0 ; i++ ) {
  342.       printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, pg2_row_loc[i], SEMICOLON,
  343.                                pg2_col_loc[i], CHAR_H, pg2_opt_msg[i] );
  344.    }
  345.    color( YELLOW, BLACK );
  346.    for ( j = 0 ; j <= 8 ; j++ ) {
  347.       printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, pg2_row_loc[i], SEMICOLON,
  348.                                pg2_col_loc[i], CHAR_H, pg2_opt_msg[i] );
  349.       i++;
  350.    }
  351.    printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, option_cur_loc[0], SEMICOLON,
  352.                             option_cur_loc[1], CHAR_H, option_string );
  353.    color( CYAN, BLACK );
  354.    while ( *pg2_row_loc[i] ) {
  355.       printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, pg2_row_loc[i], SEMICOLON,
  356.                                pg2_col_loc[i], CHAR_H, pg2_opt_msg [i] );
  357.       i++;
  358.    }
  359.  
  360.    /* print the option history                                             */
  361.    printf ( "%s%s%s%s%s%s", ESCAPE_BRACKET, option_hist_loc[0], 
  362.                             SEMICOLON, option_hist_loc[1], 
  363.                             CHAR_H, option_history_prt );
  364.  
  365.    printf ( "%s%s%s%s%s", ESCAPE_BRACKET, amount_resp_loc[0], 
  366.                           SEMICOLON, amount_resp_loc[1], CHAR_H );
  367.  
  368.    amount_resp[0] = 4;              /* max nbr input chars, including <CR> */
  369.    amount_resp_ptr = cgets( amount_resp );
  370.    amount = atoi( amount_resp_ptr );
  371.    /* process the amount if it is valid                                    */
  372.    if ( amount > 0 && amount < 256 ) {
  373.       /* add a comma since string should not be null from option logic     */
  374.       strcat ( option_history, "," );
  375.       /* if too much history, save most recent choices                     */
  376.       manage_history( amount_resp_ptr );
  377.       /* if first byte of command is zero, then execute the command        */
  378.       *( pg2_prn_cmds[option] + 3 ) = (char)amount;
  379.       cmp_ptr = pg2_prn_cmds[option];
  380.       if ( strncmp( cmp_ptr, "1", 1 ) == 0 ) {
  381.          cmp_ptr++;
  382.          fputs( cmp_ptr, stdprn );
  383.       }
  384.    }
  385.    return;
  386. }
  387.  
  388. /*
  389. ******************************************************************************
  390.  
  391.  color()   Sets the screen color with ANSI.SYS
  392.  
  393. ******************************************************************************
  394. */
  395.  
  396. color( fg, bg )
  397. int fg, bg;
  398.  
  399. {
  400.    static char *fg_ansi[] = {
  401.       "30", "31", "32", "33", "34", "35", "36", "37"
  402.    };
  403.  
  404.    static char *bg_ansi[] = {
  405.       "40", "41", "42", "43", "44", "45", "46", "47"
  406.    };
  407.  
  408.    fg = VALID_FG( fg );
  409.    bg = VALID_BG( bg );
  410.  
  411.    printf ( "%s%s%s%s%s", ESCAPE_BRACKET, fg_ansi[fg], SEMICOLON,
  412.                           bg_ansi[bg], CHAR_m );
  413.    return;
  414. }
  415.  
  416. /*
  417. ******************************************************************************
  418.  
  419. manage_history()  Keeps the most recent options and auxiliary input
  420.  
  421.    The 'cutting' off of the first option must occur as many times as is
  422.       necessary to get the length down under the stated amount.  This is
  423.       because we could be removing only two characters and adding three.
  424.       Done over and over, the result is that we bust the string.
  425.  
  426. ******************************************************************************
  427. */
  428.  
  429. manage_history(input_ptr)
  430. char *input_ptr;
  431.  
  432. {
  433.    register int j;
  434.    char *temp_ptr;
  435.  
  436.    /* Cut oldest items off the history list till it is short enough        */
  437.    while ( strlen( option_history ) > MAX_HISTORY ) {
  438.       temp_ptr = strchr( option_history, ',' );
  439.       temp_ptr++;
  440.       for ( j = 0 ; ( option_history[j] = *temp_ptr ) != '\0' ; temp_ptr++, j++ )
  441.          ;                             
  442.    }
  443.    /* add most recent response to history string                           */
  444.    strcat ( option_history, input_ptr );
  445.    strcpy( option_history_prt, option_history );
  446.    /* fill out the print string to cover up anthing left on the string     */
  447.    while( strlen( option_history_prt ) < MAX_HISTORY_PRT ) {
  448.       strcat( option_history_prt, " " );
  449.    }
  450.    return;
  451. }
  452.