home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / amiga / programm / 17982 < prev    next >
Encoding:
Text File  |  1992-12-31  |  8.1 KB  |  276 lines

  1. Path: sparky!uunet!cs.utexas.edu!sun-barr!male.EBay.Sun.COM!exodus.Eng.Sun.COM!pepper.Eng.Sun.COM!cmcmanis
  2. From: cmcmanis@pepper.Eng.Sun.COM (Chuck McManis)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Console RAW mode.
  5. Date: 31 Dec 1992 07:37:13 GMT
  6. Organization: Sun
  7. Lines: 264
  8. Message-ID: <lk58p9INN3kd@exodus.Eng.Sun.COM>
  9. References: <1992Dec17.051157.23971@serval.net.wsu.edu> <1992Dec17.232419.22268@bernina.ethz.ch>
  10. NNTP-Posting-Host: pepper
  11.  
  12. Some very old code that did this at one time for SAS/C programs.
  13.  
  14. Newsgroups: comp.sys.amiga
  15. Subject: Re: MsgPorts, DOS, ACTION_SCREEN_MODE help needed
  16. Summary: 
  17. Expires: 
  18. References: <378NU105451@NDSUVM1>
  19. Sender: 
  20. Reply-To: cmcmanis@sun.UUCP (Chuck McManis)
  21. Followup-To: 
  22. Distribution: 
  23. Organization: Sun Microsystems, Mountain View
  24. Keywords: 
  25.  
  26. In article <378NU105451@NDSUVM1> NU105451@NDSUVM1.BITNET (Walter Reed) writes:
  27. >I am working on a version of MicroEmacs that would support either opening
  28. >a new window or useing the current window and setting its mode to RAW.
  29.  
  30. The following code I cooked up (pun intended) to emulate the curses functions
  31. for putting the console into raw() or cooked() (they call it noraw()) modes.
  32. Note that it depends on knowing how the structure FILE * stores the internal
  33. amiga file handle. If someone would port it to Aztec C and post it that would
  34. be appreciated. Anyway, it's short so I'll include it here. It's a shar
  35. file so cut off the message and feed it to the unix shell, or the Amiga
  36. shar program (shar -u) available on one of the Fish disks.
  37.  
  38. --Chuck
  39. #       This is a shell archive.
  40. #       Remove everything above and including the cut line.
  41. #       Then run the rest of the file through sh.
  42. #----cut here-----cut here-----cut here-----cut here----#
  43. #!/bin/sh
  44. # shar:    Shell Archiver
  45. #       Run the following text with /bin/sh to create:
  46. #       testraw.c
  47. #       raw.c
  48. #       sendpacket.c
  49. # This archive created: Tue Jun 16 01:47:01 1987
  50. cat << \SHAR_EOF > testraw.c
  51. /*
  52.  *       testraw.c
  53.  *
  54.  *   This program shows how to use the functions raw() and cooked() to
  55.  * control the way characters are read from a Level 2 file pointer.
  56.  * These are only useful if the file pointer points at an instance of
  57.  * the console.device, which means one of 'stdin', 'stdout', or 'stderr'
  58.  * or a console opened with fp = fopen("CON:x/y/wid/len/Title","w+");
  59.  * like this example does. 
  60.  *
  61.  * Written : 16-Jun-87 By Chuck McManis, do with it what you will.
  62.  *
  63.  */
  64.  
  65. #include <stdio.h>
  66. #ifndef u_char
  67. #define u_char  unsigned char
  68. #endif
  69.  
  70. void main()
  71.  
  72. {
  73.   FILE  *win;
  74.   char  c;
  75.   long  i;
  76.  
  77.   printf("This program shows how to use raw mode file pointers.\n");
  78.   printf("First we open the window...\n");
  79.   win = fopen("CON:10/10/400/100/Test Console","w+");
  80.   setnbf(win); /* This is IMPORTANT, set file pointer to 'unbuffered' */
  81.   fprintf(win,"Using the default mode, type some characters ... \n");  
  82.   i = 0;
  83.   while ((c = fgetc(win)) != 'Q') {
  84.     i = (i + 1) % 25;
  85.     if (i == 0) printf("\n");
  86.     printf(" %02x",(u_char) c);
  87.   }
  88.   printf("\n************\n");
  89.   fprintf(win,"Now switching to 'raw' mode ...\n");
  90.   if (raw(win) != 0) perror("raw");
  91.   i = 0;
  92.   while ((c = fgetc(win)) != 'Q') {
  93.     i = (i + 1) % 25;
  94.     if (i == 0) printf("\n");
  95.     printf(" %02x",(u_char) c);
  96.   }
  97.   printf("\n************\n");
  98.   fprintf(win,"Now back to 'cooked' mode ... \n");
  99.   if (cooked(win) != 0) perror("cooked");
  100.   i = 0;
  101.   while ((c = fgetc(win)) != 'Q') {
  102.     i = (i + 1) % 25;
  103.     if (i == 0) printf("\n");
  104.     printf(" %02x",(u_char) c); 
  105.   }
  106. }
  107. SHAR_EOF
  108. cat << \SHAR_EOF > raw.c 
  109. /* 
  110.  *      raw.c
  111.  *
  112.  *    This is a routine for setting a given stream to raw or cooked mode.
  113.  * This is useful when you are using Lattice C to produce programs that
  114.  * want to read single characters with the "getch()" or "fgetc" call.
  115.  *
  116.  * Written : 18-Jun-87 By Chuck McManis. 
  117.  *           If you use it I would appreciate credit for it somewhere.
  118.  */
  119. #include <exec/types.h>
  120. #include <libraries/dos.h>
  121. #include <libraries/dosextens.h>
  122. #include <stdio.h>
  123. #include <ios1.h>
  124. #include <error.h>
  125.  
  126. /* New Packet in 1.2 */
  127. #define ACTION_SCREEN_MODE      994L
  128.  
  129. extern  int     errno;          /* The error variable */
  130.  
  131. /*
  132.  * Function raw() - Convert the specified file pointer to 'raw' mode. This
  133.  * only works on TTY's and essentially keeps DOS from translating keys for
  134.  * you, also (BIG WIN) it means getch() will return immediately rather than
  135.  * wait for a return. You lose editing features though.
  136.  */
  137. long
  138. raw(fp)
  139.  
  140. FILE *fp;
  141.  
  142. {
  143.   struct MsgPort        *mp; /* The File Handle message port */
  144.   struct FileHandle     *afh;
  145.   struct UFB            *ufb;
  146.   long                  Arg[1],res;
  147.  
  148.   ufb = (struct UFB *) chkufb(fileno(fp));  /* Step one, get the file handle */
  149.   afh = (struct FileHandle *)(ufb->ufbfh); 
  150.  
  151.   if (!IsInteractive(afh)) {    /* Step two, check to see if it's a console */
  152.     errno = ENOTTY;
  153.     return(-1);
  154.   }
  155.                               /* Step three, get it's message port. */
  156.   mp  = ((struct FileHandle *)(BADDR(afh)))->fh_Type;
  157.   Arg[0] = -1L;
  158.   res = SendPacket(mp,ACTION_SCREEN_MODE,Arg,1); /* Put it in RAW: mode */
  159.   if (res == 0) {
  160.     errno = ENXIO;
  161.     return(-1);
  162.   }
  163.   return(0);
  164. }
  165.  
  166. /*
  167.  * Function - cooked() this function returns the designate file pointer to
  168.  * it's normal, wait for a <CR> mode. This is exactly like raw() except that
  169.  * it sends a 0 to the console to make it back into a CON: from a RAW:
  170.  */
  171.  
  172. long
  173. cooked(fp)
  174.  
  175. FILE *fp;
  176.  
  177. {
  178.   struct MsgPort        *mp; /* The File Handle message port */
  179.   struct FileHandle     *afh;
  180.   struct UFB            *ufb;
  181.   long                  Arg[1],res;
  182.  
  183.   ufb = (struct UFB *) chkufb(fileno(fp));
  184.   afh = (struct FileHandle *)(ufb->ufbfh);
  185.   if ( ! IsInteractive(afh)) {
  186.     errno = ENOTTY;
  187.     return(-1);
  188.   }
  189.   mp  = ((struct FileHandle *)(BADDR(afh)))->fh_Type;
  190.   Arg[0] = 0;
  191.   res = SendPacket(mp,ACTION_SCREEN_MODE,Arg,1);
  192.   if (res == 0) {
  193.     errno = ENXIO;
  194.     return(-1);
  195.   }
  196.   return(0);
  197. }
  198. SHAR_EOF
  199. cat << \SHAR_EOF > sendpacket.c
  200. /*
  201.  *      Sendpacket.c 
  202.  *
  203.  *  An invaluable addition to your Amiga.lib file. This code sends a packet
  204.  * the given message port. This makes working around DOS lots easier.
  205.  * 
  206.  * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
  207.  * however that you may wish to add it to Amiga.Lib, to do so, compile it
  208.  * and say 'oml lib:amiga.lib -r sendpacket.o' 
  209.  */
  210.  
  211. #include <exec/types.h>
  212. #include <exec/ports.h>
  213. #include <exec/memory.h>
  214. #include <libraries/dos.h>
  215. #include <libraries/dosextens.h>
  216.  
  217. /*
  218.  * Function - SendPacket written by Phil Lindsay, Carolyn Scheppner, and
  219.  * Andy Finkel. This function will send a packet of the given type to the
  220.  * Message Port supplied.
  221.  */
  222.  
  223. long
  224. SendPacket(pid,action,args,nargs)
  225.  
  226. struct MsgPort *pid;  /* process indentifier ... (handlers message port ) */
  227. long action,          /* packet type ... (what you want handler to do )   */
  228.      args[],          /* a pointer to a argument list */
  229.      nargs;           /* number of arguments in list  */
  230. {
  231.   struct MsgPort        *replyport;
  232.   struct StandardPacket *packet;
  233.  
  234.   long  count, *pargs, res1;
  235.  
  236.   replyport = (struct MsgPort *) CreatePort(NULL,0);
  237.   if(!replyport) return(0);
  238.  
  239.   /* Allocate space for a packet, make it public and clear it */
  240.   packet = (struct StandardPacket *) 
  241.     AllocMem((long)sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR);
  242.   if(!packet) {
  243.     DeletePort(replyport);
  244.     return(0);
  245.   }
  246.  
  247.   packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);
  248.   packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);
  249.   packet->sp_Pkt.dp_Port         = replyport;
  250.   packet->sp_Pkt.dp_Type         = action;
  251.  
  252.   /* copy the args into the packet */
  253.   pargs = &(packet->sp_Pkt.dp_Arg1);       /* address of first argument */
  254.   for(count=0;count < nargs;count++) 
  255.     pargs[count]=args[count];
  256.  
  257.   PutMsg(pid,packet); /* send packet */
  258.  
  259.   WaitPort(replyport);
  260.   GetMsg(replyport); 
  261.  
  262.   res1 = packet->sp_Pkt.dp_Res1;
  263.  
  264.   FreeMem(packet,(long)sizeof(struct StandardPacket));
  265.   DeletePort(replyport); 
  266.  
  267.   return(res1);
  268. }
  269. SHAR_EOF
  270. #       End of shell archive
  271. exit 0
  272. --
  273. --Chuck McManis                Mr. NIS+                Sunsoft
  274. uucp: {anywhere}!sun!cmcmanis   BIX: <none>   Internet: cmcmanis@Eng.Sun.COM
  275. These opinions are my own and no one elses, but you knew that didn't you.
  276.