home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 214_01 / xshowfns.c < prev    next >
Text File  |  1979-12-31  |  16KB  |  937 lines

  1. /*XSHOWFNS.C   VERS:- 01.00  DATE:- 09/26/86  TIME:- 09:37:48 PM */
  2. /*
  3. %CC1 $1.C 
  4. %CLINK XSHOWR XSHOWFNS WILDEXP -F CHARFUN STRFUN -S -E5500
  5. */
  6. /* 
  7. Description:
  8.  
  9. Functions for XSHOWR.C
  10.  
  11.  
  12. By J.A. Rupley, Tucson, Arizona
  13. Coded for BDS C compiler, version 1.50a
  14. */
  15.  
  16. #include <bdscio.h>
  17.  
  18. #include <xshow.h>
  19.  
  20.  
  21.         /*page eject*/
  22.  
  23. int down_block()
  24. {
  25.     gotop_display(0);
  26.     backup(2);
  27.     if (disp_pntr > mid_addr)
  28.         disp_pntr = mid_addr;
  29.     else
  30.     {
  31.         if (prev_block())
  32.         {
  33.             disp_pntr = start_addr;
  34.             return -1;
  35.         }
  36.         else
  37.             disp_pntr++;
  38.     }
  39.     disp_skip(0);
  40.     return 0;
  41. }
  42.  
  43.  
  44. int up_block()
  45. {
  46.     gotop_display(0);
  47.     if (disp_pntr < mid_addr && end_addr > mid_addr)
  48.         disp_pntr = mid_addr;
  49.     else
  50.         if (next_block())
  51.         {
  52.             while (!disp_skip(0))
  53.                 ;
  54.             if (backup(TLENGTH))
  55.                 disp_pntr = start_addr;
  56.             return -1;
  57.         }
  58.     disp_skip(0);
  59.     return 0;
  60. }
  61.  
  62.  
  63.         /*page eject*/
  64.  
  65. int gotop_display(display)
  66. int display;
  67. {
  68.     if (!top_flag)
  69.         backup(TLENGTH);
  70.     if (display)
  71.         return disp_screen();
  72.     top_flag = 1;
  73.     return 0;
  74. }
  75.  
  76.  
  77. int gobot_display(display)
  78. int display;
  79. {
  80.     int temp;
  81.  
  82.     if (top_flag)
  83.     {
  84.         temp = TLENGTH;
  85.         while (temp--)
  86.             disp_skip(0);
  87.     }
  88.     if (display)
  89.         return disp_screen();
  90.     top_flag = 0;
  91.     return 0;
  92. }
  93.  
  94.  
  95. int disp_screen()
  96. {
  97.     int i, temp;
  98.  
  99.     CLEAR;
  100.     for (temp = -TLENGTH; temp; temp++)
  101.         if (disp_skip(1))
  102.             break;
  103.     CUR_BOTTOM;
  104.     top_flag = 0;
  105.     return temp;
  106. }
  107.  
  108.  
  109. int backup(lines)
  110. int lines;
  111. {
  112.     char c;
  113.  
  114.     lines++;
  115.     while (lines--)
  116.     {
  117.         c = 0;
  118.         while (c != '\n')
  119.         {
  120.             c = (*disp_pntr & 0x7f);
  121.             if (pntr_change(-1))
  122.                 return -1;
  123.         }
  124.     }
  125.     disp_skip(0);
  126.     return 0;
  127. }
  128.  
  129.  
  130. int disp_skip(display)
  131. int display;
  132. {
  133.     int charcount;
  134.     int temp;
  135.     char c;
  136.  
  137.     c = charcount = 0;
  138.     while (c != '\n')
  139.     {
  140.         if ((c = (*disp_pntr & 0x7f)) >= 0x20)
  141.         {
  142.             if (display && ++charcount > tabcount
  143.                 && charcount <= (tabcount + TWIDTH))
  144.                 b_putchar(c);
  145.         }
  146.         else
  147.             {
  148.             switch (c)
  149.             {
  150.             case CPMEOF :
  151.                 return -1;
  152.             case '\n' :
  153.                 if (display)
  154.                     b_putchar('\n');
  155.             case '\r' :
  156.                 charcount = 0;
  157.                 if (display)
  158.                     b_putchar('\r');
  159.                 break;
  160.             case '\t' :
  161.                 if (display)
  162.                 {
  163.                     temp = (TAB_SPACES - charcount % TAB_SPACES);
  164.                     while (temp--)
  165.                         if (++charcount > tabcount && charcount <= (tabcount + TWIDTH))
  166.                             b_putchar(SPACE);
  167.                 }
  168.                 break;
  169.             default :
  170.                 if (display && ++charcount > tabcount
  171.                     && charcount <= (tabcount + TWIDTH - 1))
  172.                 {
  173.                     b_putchar('^');
  174.                     ++charcount;
  175.                     b_putchar(c += 0x40);
  176.                 }
  177.             }
  178.         }
  179.         if (pntr_change(1))
  180.             return -1;
  181.     }
  182.     return 0;
  183. }
  184.  
  185.  
  186.         /*page eject*/
  187.  
  188. int load_buffer()
  189. {
  190.     int temp;
  191.  
  192.     seek(fd, 0, 0);
  193.     if ((temp = read(fd, start_addr, NSECS * 2)) == ERROR)
  194.         error("Read error in load_buffer().");
  195.  
  196.     start_flag = 1;
  197.     disp_pntr = start_addr;
  198.     disp_sec = temp;
  199.     end_set(temp);
  200.     return 0;
  201. }
  202.  
  203.  
  204. int next_block()
  205. {
  206.     int temp;
  207.  
  208.     if (end_flag)
  209.         return -1;
  210.  
  211.     movmem(mid_addr, start_addr, NSECS * SECSIZ);
  212.  
  213.     seek(fd, disp_sec, 0);
  214.     if ((temp = read(fd, mid_addr, NSECS)) == ERROR)
  215.         error("Read error in next_block().");
  216.  
  217.     start_flag = 0;
  218.     disp_pntr = mid_addr;
  219.     disp_sec += temp;
  220.     end_set(temp + NSECS);
  221.     return 0;
  222. }
  223.  
  224.  
  225. int prev_block()
  226. {
  227.     int temp;
  228.  
  229.     if (start_flag)
  230.         return -1;
  231.  
  232.     if ((temp = disp_sec % NSECS) == 0)
  233.         temp = NSECS * 3;
  234.     else
  235.         temp += NSECS * 2;
  236.  
  237.     if (temp >= disp_sec)
  238.     {
  239.         load_buffer();
  240.         disp_pntr = mid_addr - 1;
  241.         return 0;
  242.     }
  243.  
  244.     movmem(start_addr, mid_addr, NSECS * SECSIZ);
  245.  
  246.     seek(fd, (disp_sec -= temp), 0);
  247.     if ((read(fd, start_addr, NSECS)) == ERROR)
  248.         error("Read error in prev_block().");
  249.  
  250.     start_flag = 0;
  251.     disp_sec += 2 * NSECS;
  252.     disp_pntr = mid_addr - 1;
  253.     end_set(2 * NSECS);
  254.     return 0;
  255. }
  256.  
  257.  
  258. void end_set(offset)
  259. int offset;
  260. {
  261.     char *temp_pntr;
  262.  
  263.     end_addr = start_addr + offset * SECSIZ;
  264.     *end_addr = CPMEOF;
  265.  
  266.     if (disp_sec != end_sec)
  267.         end_flag = 0;
  268.     else
  269.         {
  270.         if (offset)
  271.             temp_pntr = end_addr - SECSIZ;
  272.         else
  273.             temp_pntr = start_addr;
  274.         while (*temp_pntr != CPMEOF)
  275.             temp_pntr++;
  276.         if (*--temp_pntr != '\n')
  277.         {
  278.             *++temp_pntr = '\n';
  279.             *++temp_pntr = CPMEOF;
  280.             end_addr = temp_pntr;
  281.         }
  282.         end_flag = 1;
  283.     }
  284. }
  285.  
  286.  
  287.         /* page eject*/
  288.  
  289. int change_load(n)
  290. int n;
  291. {
  292.     int temp;
  293.  
  294.     end_flag = 0;
  295.  
  296.     if ((n < 0) && ((temp = disp_sec % NSECS) != 0))
  297.     {
  298.         temp = disp_sec - temp;
  299.         n++;
  300.     }
  301.     else
  302.         temp = disp_sec;
  303.  
  304.     if ((temp += n * NSECS) >= end_sec)
  305.     {
  306.         if ((end_sec % NSECS) == 0)
  307.             disp_sec = end_sec - NSECS * 2;
  308.         else
  309.             disp_sec = end_sec - NSECS - end_sec % NSECS;
  310.         if (disp_sec <= 0)
  311.             load_buffer();
  312.         else
  313.         {
  314.             seek(fd, disp_sec, 0);
  315.             next_block();
  316.             next_block();
  317.             disp_skip(0);
  318.             if ((temp - end_sec) < NSECS)
  319.                 return 1;
  320.         }
  321.         while (!up_block())
  322.             ;
  323.         return 1;
  324.     }
  325.     else
  326.         if ((temp -= 2 * NSECS) >= NSECS)
  327.     {
  328.         seek(fd, (disp_sec = temp), 0);
  329.         next_block();
  330.         next_block();
  331.     }
  332.     else
  333.     {
  334.         load_buffer();
  335.         if (temp < 0)
  336.             return -1;
  337.         else
  338.             disp_pntr = mid_addr;
  339.     }
  340.     disp_skip(0);
  341.     return 0;
  342. }
  343.  
  344.  
  345.         /* page eject*/
  346.  
  347. int read_pattern()
  348. {
  349.     int i;
  350.     char *tt;
  351.  
  352.     CUR_UP;
  353.     CUR_UP;
  354.     CUR_UP;
  355.     CUR_UP;
  356.     ERA_EOP;
  357.     printf("\n:::Old decoded pattern: %s", pattern);
  358.     printf("\n:::Old input string...: %s", old_pattern);
  359.     printf("\n:::Enter string + -cr-: ");
  360.     if (strlen(gets(input_pattern)))
  361.     {
  362.         strcpy(old_pattern, input_pattern);
  363.         upper_case = 0;
  364.         if (!pat_decode(input_pattern, pattern))
  365.             return -1;
  366.         for (tt = pattern; upper_case && *tt; tt++)
  367.             *tt = toupper(*tt);
  368.     }
  369.     else
  370.     {
  371.         printf("\n\nDo you want to abort?  Y / (N = any other char):");
  372.         if (toupper(b_getchar()) == 'Y')
  373.             return -1;
  374.     }
  375.     return 0;
  376. }
  377.  
  378.  
  379. void srch_display()
  380. {
  381.     p_srch1 = f_pntr;
  382.     p_srch2 = n_pntr;
  383.     backup(10);
  384.     if (disp_sec < f_sec)
  385.     {
  386.         p_srch1 += NSECS * SECSIZ;
  387.         p_srch2 += NSECS * SECSIZ;
  388.     }
  389.     srch_out = 2;
  390.     REV_ON;
  391.     disp_screen();
  392.     srch_out = 0;
  393.     REV_OFF;
  394. }
  395.  
  396.  
  397. int pat_decode(pat, str)
  398. char *pat;
  399. char *str;
  400. {
  401.     int c;
  402.     char *bad;
  403.  
  404.     j = i = 0;
  405.     while (c = pat[i++])
  406.     {
  407.         if (c == '!')
  408.         {
  409.             for (c = pat[i++], bad = "*?!"; *bad; bad++)
  410.                 if (*bad == c)
  411.                     error("Bad pattern after !.");
  412.             decode(c, pat, str, 1);
  413.         }
  414.         else
  415.             decode(c, pat, str, 0);
  416.     }
  417.     str[j] = 0;
  418.     return j;
  419. }
  420.  
  421.  
  422. void decode(c, pat, str, not_flag)
  423. int c, not_flag;
  424. char *pat, *str;
  425. {
  426.     switch (c)
  427.     {
  428.     case '$' :
  429.         upper_case = TRUE;
  430.         break;
  431.     case '*' :
  432.         str[j++] = SKIPANY;
  433.         break;
  434.     case '?' :
  435.         str[j++] = SKIPONE;
  436.         break;
  437.     case '@' :
  438.         str[j++] = ANYALPHA;
  439.         break;
  440.     case '#' :
  441.         str[j++] = ANYNUMBER;
  442.         break;
  443.     case '^' :
  444.         c = pat[i++];
  445.         if (c > 0x7f || c <= 0x40)
  446.             error("Illegal ^ character.");
  447.         str[j++] = toupper(c) - 0x40;
  448.         break;
  449.     case '\\' :
  450.         c = pat[i++];
  451.         if (c == 0)
  452.             error("\\ requires character.");
  453.         switch (toupper(c))
  454.         {
  455.         case 'N' :
  456.             c = '\n';
  457.             break;
  458.         case 'F' :
  459.             c = '\f';
  460.             break;
  461.         case 'R' :
  462.             c = '\r';
  463.             break;
  464.         case 'T' :
  465.             c = '\t';
  466.             break;
  467.         case 'B' :
  468.             c = '\b';
  469.             break;
  470.         }
  471.     default :
  472.         str[j++] = c;
  473.     }
  474.     if (not_flag)
  475.         str[j - 1] |= 0x80;
  476. }
  477.  
  478.  
  479. void set_srch(to, from)
  480. int to, from;
  481. {
  482.     char *temp_pntr;
  483.     int temp_sec;
  484.  
  485.     switch (from)
  486.     {
  487.     case SRCH_CHAR :
  488.         temp_sec = disp_sec;
  489.         temp_pntr = disp_pntr;
  490.         break;
  491.     case FIRST :
  492.         temp_sec = f_sec;
  493.         temp_pntr = f_pntr;
  494.         break;
  495.     case NEXT :
  496.         temp_sec = n_sec;
  497.         temp_pntr = n_pntr;
  498.         break;
  499.     }
  500.  
  501.     switch (to)
  502.     {
  503.     case SRCH_CHAR :
  504.         disp_sec = temp_sec;
  505.         change_load(0);
  506.         disp_pntr = temp_pntr;
  507.         break;
  508.     case FIRST :
  509.         f_sec = temp_sec;
  510.         f_pntr = temp_pntr;
  511.         break;
  512.     case NEXT :
  513.         n_sec = temp_sec;
  514.         n_pntr = temp_pntr;
  515.         break;
  516.     }
  517. }
  518.  
  519.  
  520.         /* page eject*/
  521.  
  522. int find()
  523. {
  524.     int temp;
  525.  
  526.     i = 0;
  527.     j = 0;
  528.     set_srch(SRCH_CHAR, FIRST);
  529.     temp = PAUSE_COUNT * 1000;
  530.  
  531.     switch (setjmp(jbufsrch))
  532.     {
  533.     case NOTFOUND :
  534.         return NOTFOUND;
  535.     case FOUND :
  536.         set_srch(NEXT, SRCH_CHAR);
  537.         return FOUND;
  538.     }
  539.  
  540. loop :
  541.     if (!temp--)
  542.     {
  543.