home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 287.lha / TY_v1.3 / src / suppcom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  8.3 KB  |  272 lines

  1. /*************************************************************************
  2.  ***                            suppcom.c                (JJB TEMPLAR) ***
  3.  *** Date begun: 11/8/89 - copied from hardcopy of old prim.c          ***
  4.  *** Last modified: 27/8/89.                                           ***
  5.  *************************************************************************/
  6. /*** The pattern matching algorithm used is the Knuth-Morris-Pratt one ***
  7.  *** as detailed in:-                                                  ***
  8.  ***    "Computer Algorithms: Introduction to Design and Analysis"     ***
  9.  ***            by Sara Baase.                                         ***
  10.  *************************************************************************/
  11.  
  12. #include "less.h"
  13. #include "position.h"
  14. #include "screen.h"
  15.  
  16. #include <intuition/intuition.h>
  17. #include "iconify.h"
  18.  
  19. extern struct Window    *Window;
  20. extern struct NewWindow NewWindow;
  21. extern UWORD far    image10[420];
  22. extern int          fast_line,twiddle,back_scroll,top_scroll,quit_at_eof,
  23.                     squeeze,scroll_bar;
  24. extern int          has_resized;
  25. extern BPTR         file;
  26.  
  27. extern char *line;
  28.  
  29. char    *modedata[] = {
  30. "\n    Ty 1.3 User Changeable Modes:\n\n",
  31. "    (0) Cancel ........ don't change any modes.\n\n",
  32. "    (1) Fast line ..... check for ~` or control characters.\n",
  33. "    (2) Twiddle ....... output a ~ for lines beyond EOF.\n",
  34. "    (3) Back scroll ... allow backwards scrolling.\n",
  35. "    (4) Top scroll .... draw from top of screen.\n",
  36. "    (5) Quit at EOF ... quit after two attempts to pass EOF.\n",
  37. "    (6) Squeeze ....... squeeze multiple blank lines together.\n",
  38. "    (7) Scroll bar .... activate vertical scroll bar.\n\n\n\n\n\n\n\n",
  39. "    Sorry about the cruddy numbered menu system, but this'll be made a\n",
  40. "    requester... eventually.\n",
  41. "                                (SELECT NUMBER)",
  42. NULL};
  43. static LONG marks[10];  /* The table of marks. */
  44.  
  45. struct Image    iconimg = {0,0,70,42,2,image10,3,0,NULL};
  46.  
  47. #define PATLEN   20
  48. char    pat[PATLEN + 3];    /* Two characters extra */
  49. char    tup[PATLEN + 3];    /* Copy of pat, but all upper case */
  50. int     flink[PATLEN + 3];
  51. int     pat_opt;            /* Should be set by option reader whenever a
  52.                              * new pattern is specified. */
  53.  
  54. int     toupp(ch) /*=====================================================*/
  55. register int    ch;
  56. {
  57.     if ((ch >= 'a') && (ch <= 'z')) ch += 'A' - 'a';
  58.     return(ch);
  59. }
  60.  
  61. static void doflink() /*=================================================*/
  62. {                     /* Set up flink[], using pat[] as pattern.         */
  63. register int    i,j;
  64. register char   *cp = pat,*tp = tup;
  65.     while (*cp) *tp++ = toupp(*cp++);
  66.     *tp = 0;                        /* Terminate string! */
  67.     flink[0] = -1;   i = 1;
  68.     while (tup[i]) {
  69.         j = flink[i-1];
  70.         while ((j != -1) && (tup[j] != tup[i-1]) && (tup[j] != '?')) j = flink[j];
  71.         flink[i] = j + 1;
  72.         i++;
  73.     }
  74.     pat_opt = 0;    /* Turn off pattern option flag */
  75. }
  76.  
  77. static int  match(buf) /*================================================*/
  78. register char *buf;
  79. {
  80. register char   *p = tup;
  81. register int    j;
  82.     j = 0;
  83.     while (*buf) {
  84.         while ((j != -1) && (p[j] != toupp(*buf)) && (p[j] != '?')) j = flink[j];
  85.         if (!p[j+1]) return(1);
  86.         else {  buf++;  j++; }
  87.     }
  88.     return(0);
  89. }
  90.  
  91. void    search_forw(f) /*================================================*/
  92. int     f;
  93. {
  94. LONG    pos,lpos;
  95.     if ((f || !pat[0]) &&
  96.         (!(f = r_string("Forward Search",pat,PATLEN+1)) || !pat[0])) return;
  97.  
  98.     if (position(TOP) == NULL_POSITION) pos = 0;
  99.     else pos = position(TOP_PLUS_ONE);
  100.  
  101.     if (pos == NULL_POSITION) {
  102.         error("Nothing to search!",0);
  103.         return;
  104.     }
  105.  
  106.     if (f || pat_opt) doflink();
  107.     while (1) {
  108.         lpos = pos;
  109.         pos = forw_raw_line(pos);
  110.  
  111.         if (pos == NULL_POSITION) {
  112.             error("Pattern not found",0);
  113.             return;
  114.         }
  115.  
  116.         if (match(line)) break;
  117.     }
  118.  
  119.     jump_loc(lpos);
  120. }
  121.  
  122. void    search_back(f) /*================================================*/
  123. int     f;
  124. {
  125. LONG    pos,lpos;
  126.     if ((f || !pat[0]) &&
  127.         (!(f = r_string("Backward Search",pat,PATLEN+1)) || !pat[0])) return;
  128.         /* The above sets f to 1 if user entered pattern, if f was zero */
  129.  
  130.     if (position(TOP) == NULL_POSITION) pos = 0;
  131.     else pos = position(TOP);
  132.  
  133.     if (pos == NULL_POSITION) {
  134.         error("Nothing to search!",0);
  135.         return;
  136.     }
  137.  
  138.     if (f || pat_opt) doflink();
  139.     while (1) {
  140.         pos = back_raw_line(pos);
  141.         lpos = pos;
  142.  
  143.         if (pos == NULL_POSITION) {
  144.             error("Pattern not found",0);
  145.             return;
  146.         }
  147.  
  148.         if (match(line)) break;
  149.     }
  150.  
  151.     jump_loc(lpos);
  152. }
  153.  
  154. void    doiconify() /*===================================================*/
  155. {
  156. static int      success = 1;
  157. static UWORD    x,y;        /* Initialised to zero */
  158. register int    temp;
  159.  
  160.     if (success) {
  161.         NewWindow.LeftEdge = Window->LeftEdge;  /* Save window dimensions */
  162.         NewWindow.TopEdge = Window->TopEdge;
  163.         NewWindow.Height = Window->Height;
  164.         NewWindow.Width = Window->Width;
  165.         NewWindow.Title = Window->Title;
  166.  
  167.         ttclose();                          /* Close Window and console */
  168.  
  169.         success = iconify(&x,&y,iconimg.Width,iconimg.Height,NULL,(APTR)&iconimg,ICON_IMAGE);
  170.  
  171.         ttopen();                        /* Get back window and console */
  172.         temp = top_scroll;  top_scroll = 1;
  173.         repaint();
  174.         top_scroll = temp;
  175.         if (!success) error("Iconify failed!",0);
  176.     }
  177.     else {
  178.         error("Iconify disabled.",0);
  179.     }
  180. }
  181.  
  182. void    print() /*=======================================================*/
  183. {
  184. UBYTE   buf[512];       /* Buffer on the fly, as it were... */
  185. register ULONG  oldpos; /* Seek back to initial file pos?   */
  186. register BPTR   prt;
  187. register int    size;
  188.     if (!file || !r_bool("Really print file?")) return;
  189.     if (!(prt = Open("PRT:",MODE_NEWFILE))) {
  190.         error("Failed to open printer!",0);
  191.         return;
  192.     }
  193.     oldpos = Seek(file,0,OFFSET_BEGINNING);
  194.     while ((size = Read(file,buf,512)) > 0) {
  195.         if (Write(prt,buf,size) != size) {
  196.             error("Printing error!",0);
  197.             break;
  198.         }
  199.     }
  200.     Close(prt);
  201.     Seek(file,oldpos,OFFSET_BEGINNING);
  202. }
  203.  
  204. void    init_mark() /*===================================================*/
  205. {                   /* Clear all marks.                                  */
  206. register int    i;
  207.     for (i = 0;  i < 10;  i++) marks[i] = NULL_POSITION;
  208. }
  209.  
  210. static int  badmark(c) /*================================================*/
  211. register int    c;     /* See if c is a valid mark char (a - z).         */
  212. {
  213.     if ((c < 0x01) || (c > 0x0a)) {     /* RAWKEY */
  214.         error("Must be a digit in [0,9]!",0);
  215.         return(1);
  216.     }
  217.     return(0);
  218. }
  219.  
  220. static int  getmark(cp) /*===============================================*/
  221. register char   *cp;
  222. {
  223. register int    c;
  224.     SetWindowTitles(Window,cp,(char *)-1L);
  225.     c = getc();
  226.     return((badmark(c))? -1: c - 1);   /* mark '0' actually pos 9 in table,
  227.                  * since RAWKEY goes 1,2,...,9,0 instead of 0,1,...,8,9. */
  228. }
  229.  
  230. void    setmark() /*=====================================================*/
  231. {
  232. register int    c;
  233.     if ((c = getmark("Mark: set mark [0-9] ")) >= 0)
  234.         marks[c] = position(TOP);
  235. }
  236.  
  237. void    gomark() /*======================================================*/
  238. {
  239. register int    c;
  240.     if ((c = getmark("Mark: go to mark [0-9] ")) < 0)
  241.         return;    /* Failed */
  242.     if (marks[c] == NULL_POSITION) error("Mark not set",0);
  243.     else jump_loc(marks[c]);
  244. }
  245.  
  246. void    domode(on) /*====================================================*/
  247. register int    on;
  248. {
  249. register char   **ch = modedata;
  250.  
  251.     SetWindowTitles(Window,(on)?"Mode ON":"Mode OFF",(char *)-1L);
  252.     clear();
  253.     while (*ch) puts(*ch++);
  254.     flush();
  255.  
  256.     switch (getc()) {
  257.         case (0x01): fast_line = on;        break;
  258.         case (0x02): twiddle = on;          break;
  259.         case (0x03): back_scroll = on * 30; break;
  260.         case (0x04): top_scroll = on;       break;
  261.         case (0x05): quit_at_eof = on;      break;
  262.         case (0x06): squeeze = on;          break;
  263.         case (0x07): scroll_bar = on;
  264.                      setbar(2);
  265.                      break;
  266.     }
  267.     if (has_resized) {
  268.         resize();
  269.         has_resized = 0;
  270.     }
  271. }
  272.