home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / misc_utils / scan / console.c < prev    next >
C/C++ Source or Header  |  1991-03-09  |  9KB  |  338 lines

  1. /* Console.c */
  2.  
  3. /* [Ed note:  This is a somewhat chopped up and modified version of 
  4.    console.c by  Jim Cooper from AmigaLibDisk 69.  Mostly just chopped out
  5.    some of the graphics routines and added a *VERY* basic capability of 
  6.    determining what the current WB screen size and font are to use as
  7.    the default for the console.  Special thanks to Jim for doing all the 
  8.    dirty work here!   -Dan
  9.  
  10. */
  11.     
  12.  
  13. #include <exec/types.h>
  14. #include <exec/io.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/intuitionbase.h>
  17. #include <devices/conunit.h>
  18. #include <libraries/diskfont.h>
  19. /*#include <stdio.h>*/   /* for debug use */
  20.  
  21. #define EOF -1
  22.  
  23. #define GRAPHICS_REV     33
  24. #define INTUITION_REV     33
  25.  
  26. #define INTUITION_OPEN 1
  27. #define GRAPHICS_OPEN  INTUITION_OPEN << 1
  28. #define DISKFONT_OPEN  GRAPHICS_OPEN  << 1
  29. #define WINDOW_OPEN    DISKFONT_OPEN  << 1
  30. #define READPORT_OPEN  WINDOW_OPEN    << 1
  31. #define CONREADER_OPEN READPORT_OPEN  << 1
  32. #define CONDEVICE_OPEN CONREADER_OPEN << 1
  33.  
  34. #define RP mywindow->RPort    /* make life easy, since I use it a lot */
  35.  
  36. void myexit(), mysetup(), myprintf(), queueread();
  37. int mygetc(), myscanf();
  38. char *mygets();
  39.  
  40. short pagelen;
  41. long GfxBase;
  42. struct IntuitionBase *IntuitionBase;
  43. ULONG DiskfontBase;
  44. extern struct TextFont *OpenDiskFont();
  45. struct TextFont *ConFont = NULL;
  46.  
  47. struct IOStdReq *CreateStdIO();
  48. struct MsgPort *CreatePort();
  49. static struct Screen *wbscreen();
  50. struct Screen *OurS = NULL;    
  51. struct Window *OpenWindow();
  52. struct Window *mywindow;
  53.  
  54.  
  55. static struct IOStdReq *conreader, conrequest;
  56. struct MsgPort *conreadport;
  57.  
  58. extern char WindowTitle[];
  59. static char buff[513];
  60. static char conchar;
  61. static int myflags;
  62.  
  63.  
  64. static struct NewWindow NewWindow = {
  65.    0,1,1,1,-1,-1,        /* left, top, width, height, detail, block pen */ 
  66.    NULL,
  67.    ACTIVATE|WINDOWDRAG|SIMPLE_REFRESH, 
  68.    NULL, NULL, (UBYTE *)WindowTitle,
  69.    NULL, NULL, 0, 0, 0, 0,    /* Min & Max size don't matter with no size gadget */
  70.    WBENCHSCREEN };
  71.  
  72. /* initialize the window so we can read and write to it */
  73. void mysetup()
  74. {
  75.  
  76.    IntuitionBase=(struct IntuitionBase *)
  77.                   OpenLibrary("intuition.library",INTUITION_REV);
  78.     if (IntuitionBase==NULL)
  79.            myexit(1);
  80.  
  81.    myflags |= INTUITION_OPEN;
  82.  
  83.    GfxBase=OpenLibrary("graphics.library",GRAPHICS_REV);
  84.     if (GfxBase==NULL)
  85.            myexit(2);
  86.  
  87.    myflags |= GRAPHICS_OPEN;
  88.  
  89.  
  90.    DiskfontBase = (ULONG) OpenLibrary("diskfont.library", 0L);
  91.     if (DiskfontBase == NULL)
  92.        myexit(9);
  93.  
  94.    myflags |= DISKFONT_OPEN;
  95.  
  96.  
  97.    if ((OurS = wbscreen()) == NULL)
  98.      {
  99.        NewWindow.Width =640;
  100.        NewWindow.Height=199;
  101.      }
  102.    else
  103.      {
  104.        NewWindow.Width = OurS->Width;
  105.        NewWindow.Height= (OurS->Height)-1;
  106.      }
  107.       
  108.    ConFont = OpenDiskFont(OurS->Font);
  109.    
  110.  
  111.    if ((mywindow = OpenWindow(&NewWindow)) == NULL)
  112.       myexit(3);
  113.  
  114.  
  115.    myflags |= WINDOW_OPEN;
  116.  
  117.    pagelen=((mywindow->Height)/ConFont->tf_YSize)-2;
  118. /*   fprintf(stderr,"pagelen = %d, Window = %d, Font Height = %d\n",pagelen, 
  119.            mywindow->Height,ConFont->tf_YSize);*/
  120.  
  121. /* create a port to receive messages about completed operations */
  122.    if ((conreadport = CreatePort("my.con.read", 0)) == NULL)
  123.       myexit(5);
  124.  
  125.    myflags |= READPORT_OPEN;
  126.  
  127. /* construct a request for that port */
  128.    if ((conreader = CreateStdIO(conreadport)) == NULL)
  129.       myexit(6);
  130.  
  131.    myflags |= CONREADER_OPEN;
  132.  
  133.    conreader->io_Data = (APTR)mywindow;
  134.    conreader->io_Length = sizeof(*mywindow);
  135.  
  136.    if (OpenDevice("console.device", 0, conreader, 0) != 0)
  137.       myexit(7);
  138.  
  139.    myflags |= CONDEVICE_OPEN;
  140.  
  141. /* copy so we have a read request structure */
  142.    conrequest.io_Device = conreader->io_Device;
  143.    conrequest.io_Unit = conreader->io_Unit;
  144.  
  145.    queueread();
  146. }
  147.  
  148. static struct Screen
  149. *wbscreen()
  150. {
  151.     register struct Screen    *s;
  152.     Forbid();
  153.     for (s = IntuitionBase->FirstScreen; s ; s = s->NextScreen)
  154.         if ((s->Flags & SCREENTYPE) == WBENCHSCREEN)
  155.             break;
  156.     Permit();
  157.     return (s);
  158. }
  159.  
  160. void myputc(c)    /* write a single character to our window */
  161. char c;
  162. {
  163.    if (mywindow == NULL) mysetup();
  164.  
  165.    conrequest.io_Command = CMD_WRITE;
  166.    conrequest.io_Data = (APTR)&c;
  167.    conrequest.io_Length = 1;
  168.    DoIO(&conrequest);
  169. }
  170.  
  171. void queueread()
  172. {
  173.    conreader->io_Command = CMD_READ;
  174.    conreader->io_Data = (APTR)&conchar;
  175.    conreader->io_Length = 1;
  176.    SendIO(conreader);
  177. }
  178.  
  179. int mygetc()
  180. {
  181.    struct IntuiMessage *GetMsg();
  182.    char c = 0;
  183.  
  184.    if (mywindow == NULL) mysetup();
  185.  
  186. /* if there hasn't been a response to the last read request, wait for it */
  187.    while((GetMsg(conreadport) == NULL)) WaitPort(conreadport);
  188. /* get what the response character was */
  189.    c = conchar;
  190. /* and queue up for another one */
  191.    queueread();
  192.    return((c == 0x1c) ? EOF : c);
  193. }
  194.  
  195. char *mygets()
  196. {
  197.    static char DELCHR[] = "\b \b";    /* String to DELete a CHaRacter */
  198.    register char inchar;
  199.    register int i,j;
  200.    register int count = 0;
  201.    register int ScrCount;
  202.    int LnCount, MaxLnCount = 0;
  203.    int InitCount = 0;
  204.  
  205.    if (mywindow == NULL) mysetup();
  206.  
  207.    myprintf("\x9b6n");            /* <CSI>6n - get current cursor pos */
  208.    while (mygetc() != ';');        /*   returned as "<CSI>row;columnR" */
  209.                     /*   row & column returned as ASCII */
  210.    while ((inchar = mygetc()) != 'R')
  211.       InitCount = (InitCount * 10) + (inchar - '0');
  212.  
  213.    LnCount = ScrCount = InitCount;
  214.  
  215.    myprintf("\x9b q");            /* <CSI><space>q - window status request */
  216.    for (i=0;i<3;i++)            /* returns window bounds in printable */
  217.       while (mygetc() != ';');        /* characters, format:             */
  218.                     /* <CSI>top line,first char,bottom line,last char<space>r */
  219.    while ((inchar = mygetc()) != ' ')
  220.       MaxLnCount = (MaxLnCount * 10) + (inchar - '0');
  221.  
  222.    mygetc();                /* throw away the 'r' */
  223.  
  224.    buff[0] = NULL;
  225.  
  226.    do {
  227.       switch (inchar = mygetc()) {
  228.      case '\b' :            /* Backspace handling */
  229.         if (count == 0)
  230.            break;
  231.         if (buff[--count] == '\t') {
  232.            i = buff[--count];
  233.            ScrCount -= i;
  234.            LnCount = ((LnCount - i) >= 0) ? (MaxLnCount - i) : (LnCount - i);
  235.            for(j=i;j>0;j--)
  236.           myprintf("%s",DELCHR);
  237.         } else {
  238.            ScrCount--;
  239.            LnCount = ((LnCount - 1) >= 0) ? MaxLnCount : 0;
  240.            myprintf("%s", DELCHR);    /* Got to use myprintf to avoid newline */
  241.         }
  242.         break;
  243.      case '\t' :            /* tab character */
  244.         if (count < 256) {
  245.            i = 9-(LnCount%8);
  246.            if ((LnCount + i) > MaxLnCount) {
  247.           i = MaxLnCount - LnCount + 1;
  248.           LnCount = 1;
  249.            } else LnCount += i;
  250.            ScrCount += i;
  251.            buff[count++] = i;
  252.            buff[count++] = '\t';
  253.            for(j=0;j<i;j++) myputc(' ');
  254.         }
  255.         break;
  256.      case '\r' :
  257.         buff[count] = '\r';
  258.         myputc('\n');
  259.         break;
  260.      case 0x18 :            /* This is handling of ^X */
  261.         if (count == 0)
  262.            break;
  263.         myprintf("\x9b0 p");    /* turn cursor off */
  264.         for (; ScrCount > InitCount; ScrCount--)
  265.            myprintf("%s", DELCHR);
  266.         myprintf("\x9b p");        /* turn cursor on */
  267.         count = 0;
  268.         LnCount = InitCount;
  269.         buff[0] = NULL;
  270.         break;
  271.      default :
  272.         if ((count < 256) && (inchar > 31)) {
  273.            myputc(inchar);
  274.            ScrCount++;
  275.            LnCount = ((LnCount + 1) > MaxLnCount) ? 0 : (LnCount + 1);
  276.            buff[count++] = inchar;
  277.         }
  278.       }
  279.    } while (inchar != '\r');
  280.  
  281.    buff[count] = '\0';
  282.    for(i=count-1;i>0;i--)
  283.       if(buff[i] == '\t') {
  284.      for(j=i-1;j<count;j++)
  285.         buff[j] = buff[j+1];
  286.          i--;
  287.       }
  288.    return(buff);
  289. }
  290.  
  291. int myscanf(str,v1,v2,v3,v4,v5,v6,v7,v8,v9)
  292. char *str;
  293. long *v1,*v2,*v3,*v4,*v5,*v6,*v7,*v8,*v9;
  294. {
  295.    return(sscanf(mygets(),str,v1,v2,v3,v4,v5,v6,v7,v8,v9));
  296. }
  297.  
  298. void myputs(str)    /* write a string to our window */
  299. char *str;
  300. {
  301.    if (mywindow == NULL) mysetup();
  302.  
  303.    conrequest.io_Command = CMD_WRITE;
  304.    conrequest.io_Data = (APTR)str;
  305.    conrequest.io_Length = strlen(str);
  306.    DoIO(&conrequest);
  307.  
  308. }
  309.  
  310. /* write a formatted string to our window */
  311. void myprintf(str,v1,v2,v3,v4,v5,v6,v7,v8,v9)
  312. char *str;
  313. long v1,v2,v3,v4,v5,v6,v7,v8,v9;
  314. {
  315.    char buff[256];
  316.  
  317.    if (mywindow == NULL) mysetup();
  318.  
  319.    conrequest.io_Command = CMD_WRITE;
  320.    conrequest.io_Data = (APTR)buff;
  321.    conrequest.io_Length = sprintf(buff,str,v1,v2,v3,v4,v5,v6,v7,v8,v9);
  322.    DoIO(&conrequest);
  323. }
  324.  
  325. /* close up everything and leave the program */
  326. void myexit(code)
  327. int code;
  328. {
  329.    if (myflags && CONDEVICE_OPEN) CloseDevice(conreader);
  330.    if (myflags && CONREADER_OPEN) DeleteStdIO(conreader);
  331.    if (myflags && READPORT_OPEN) DeletePort(conreadport);
  332.    if (myflags && WINDOW_OPEN) CloseWindow(mywindow);
  333.    if (myflags && DISKFONT_OPEN) CloseLibrary(DiskfontBase);
  334.    if (myflags && GRAPHICS_OPEN) CloseLibrary(GfxBase);
  335.    if (myflags && INTUITION_OPEN) CloseLibrary(IntuitionBase);
  336.    _exit(code);
  337. }
  338.