home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk457.lzh / QuickReq / QuickReq.c < prev    next >
C/C++ Source or Header  |  1991-02-14  |  15KB  |  546 lines

  1. /*  QuickReq V2.0   (c) 1990,1991 by Markus Aalto
  2.  
  3.     QuickReq.c  :   14.12.90 - 09.01.91
  4.  
  5. */
  6.  
  7. #include "exec/types.h"
  8. #include "exec/memory.h"
  9. #include "intuition/intuition.h"
  10. #include "intuition/screens.h"
  11. #include "intuition/intuitionbase.h"
  12. #include "graphics/text.h"
  13. #include "graphics/gfxbase.h"
  14. #include "graphics/gfx.h"
  15. #include "stdio.h"
  16. #include "string.h"
  17. #include "stdlib.h"
  18. #include "ctype.h"
  19. #include "proto/intuition.h"
  20. #include "proto/exec.h"
  21. #include "proto/dos.h"
  22.  
  23. /* No CTRL-C checking */
  24. #ifdef LATTICE
  25. int CXBRK(VOID) {return(0);}
  26. #endif
  27.  
  28. /* Protos for my own functions */
  29. int ParseCommands(int, register char *, char **), ReadNumber(char **);
  30. char *AllocNormMem(int);
  31. VOID main(int, char **), Print(char *), ReadRedirFile(char **);
  32. VOID CleanUp(BOOL , char *,ULONG ), PrintUsage(VOID);
  33. USHORT CheckParameters(int , char **), TestWidth(struct IntuiText *);
  34. struct IntuiText *MakeIntuText(char *, USHORT, USHORT);
  35.  
  36. #define MINWINDOWWIDTH 86
  37. #define MINWINDOWHEIGHT 54
  38. #define INTUILIB    "intuition.library"
  39. #define GFXLIB      "graphics.library"
  40. #define IPOINT ((struct IntuiText *)-1)
  41. #define MAXARG 32              /* maximum command line arguments */
  42. #define QUOTE  '"'
  43.  
  44. struct  IntuitionBase *IntuitionBase;
  45. struct  GfxBase *GfxBase;
  46. struct  Remember *RememberKey;
  47. char    *text[3];
  48. USHORT  ForceWidth, ForceHeight, PenNumber;
  49. ULONG   MaxCols;
  50. BOOL    CENTER_ON = FALSE;
  51. BOOL    BEEP_ON = FALSE;
  52. BOOL    LARGE_ON = FALSE;
  53. BOOL    FWIDTH_ON = FALSE;
  54. BOOL    FHEIGHT_ON = FALSE;
  55. BOOL    OWNPEN_ON = FALSE;
  56.  
  57. /*  Well, This is very important */
  58.  
  59. char UsageString[] = {
  60.     'Q','u','i','c','k','R','e','q',' ','V','2','.','0',' ',169,' ','1','9',
  61.     '9','1',' ','b','y',' ',0x9b,'1','m','M','a','r','k','u','s',' ','A','a',
  62.     'l','t','o',0x9b,'0','m','\n',0
  63. };
  64.  
  65. /*  This is something I may choose not to be defined here because of KS2.0 and
  66.     it's switchable and scalable fonts, but for now I just do it in easy way.
  67. */
  68. struct TextAttr MyTopaz8 = {
  69.     "topaz.font",
  70.     8, FS_NORMAL, FPF_ROMFONT
  71. };
  72.  
  73. VOID main(int argc, char *argv[])
  74. {
  75.     struct  Screen Buffer, *MyScreen;
  76.     struct  IntuiText *TempI, *Intu1, *Intu2, *Intu3;
  77.     ULONG   ScreenSuccess, Success, error, IntuLock;
  78.     USHORT  Width, MaxHeight, MaxWidth, text1width, text2width,
  79.             text3width, textheight, i;
  80.  
  81.     /* Just for sure */
  82.     error = 0;
  83.     RememberKey = NULL;
  84.     MaxCols = 1;
  85.  
  86.     IntuitionBase = (struct IntuitionBase *)OpenLibrary(INTUILIB,33L);
  87.     if(IntuitionBase == NULL) {
  88.         CleanUp(FALSE,"Sorry, Can't open intuition.library V33 or later!\n",20);
  89.     }
  90.  
  91.     GfxBase = (struct GfxBase *)OpenLibrary(GFXLIB,33L);
  92.     if(GfxBase == NULL) {
  93.         CleanUp(FALSE,"Sorry, Can't open graphics.library V33 or later!\n",20);
  94.     }
  95.  
  96.     ScreenSuccess = GetScreenData((char *)&Buffer,sizeof(struct Screen),
  97.                                                        WBENCHSCREEN,NULL);
  98.     if(ScreenSuccess == 0L) {
  99.         CleanUp(FALSE,"Can't get Screen information!\n",20);
  100.     }
  101.  
  102.     for(i=0; i < ((&(Buffer.BitMap))->Depth); i++) MaxCols *= 2;
  103.  
  104.     error = CheckParameters(argc, argv);
  105.     if(error != 0) {
  106.         CleanUp(TRUE,NULL,error);
  107.     }
  108.  
  109.     if(text[0] == NULL) {
  110.         CleanUp(FALSE,NULL,0);
  111.     }
  112.     else if(text[1] == NULL) {
  113.         ReadRedirFile(argv);
  114.         if(text[0] == NULL) {
  115.             CleanUp(FALSE,NULL,0);
  116.         }
  117.         else if(text[1] == NULL) {
  118.             CleanUp(FALSE,"Can't open redirection file!\n",10);
  119.         }
  120.         else if(text[2] == NULL) {
  121.             text[2] = text[1];
  122.             text[1] = NULL;
  123.         }
  124.     }
  125.     else if(text[2] == NULL) {
  126.         text[2] = text[1];
  127.         text[1] = NULL;
  128.     }
  129.  
  130.     MaxHeight = Buffer.Height;
  131.     MaxWidth = Buffer.Width;
  132.  
  133.     if(!LARGE_ON) {
  134.         if(MaxHeight > GfxBase->NormalDisplayRows) {
  135.             MaxHeight = GfxBase->NormalDisplayRows;
  136.         }
  137.         if(MaxWidth > GfxBase->NormalDisplayColumns) {
  138.             MaxWidth = GfxBase->NormalDisplayColumns;
  139.         }
  140.     }
  141.  
  142.     if(FWIDTH_ON) {
  143.         if((ForceWidth > MINWINDOWWIDTH) && (ForceWidth <= MaxWidth)) {
  144.             MaxWidth = ForceWidth;
  145.         }
  146.         else {
  147.             Print("Illegal window size!\n");
  148.             FWIDTH_ON = FALSE;
  149.         }
  150.     }
  151.     if(FHEIGHT_ON) {
  152.         if((ForceHeight > MINWINDOWHEIGHT) && (ForceHeight <= MaxHeight)) {
  153.             MaxHeight = ForceHeight;
  154.         }
  155.         else {
  156.             FHEIGHT_ON = FALSE;
  157.             Print("Illegal window size!\n");
  158.         }
  159.     }
  160.  
  161.     MaxHeight -= 46;
  162.     MaxWidth -= 38;
  163.  
  164.     Intu1 = MakeIntuText(text[0],(USHORT)(MaxHeight/9),(USHORT)(MaxWidth/8));
  165.     Intu2 = MakeIntuText(text[1],1,(USHORT)((MaxWidth-48)/16));
  166.     Intu3 = MakeIntuText(text[2],1,(USHORT)((MaxWidth-48)/16));
  167.     if((Intu1 == IPOINT) || (Intu2 == IPOINT) || (Intu3 == IPOINT)) {
  168.         CleanUp(FALSE,"Out of memory!\n",20);
  169.     }
  170.  
  171.     if(FWIDTH_ON) {
  172.         Width = ForceWidth;
  173.     }
  174.     else {
  175.         text1width = TestWidth(Intu1);
  176.         text2width = TestWidth(Intu2)+3;
  177.         text3width = TestWidth(Intu3)+3;
  178.         Width = text2width+text3width;
  179.         if(text1width > Width) Width = text1width;
  180.         Width = Width*8+38;
  181.     }
  182.  
  183.     textheight = 0;
  184.     TempI = Intu1;
  185.     while(TempI != NULL) {
  186.         textheight++;
  187.         if(CENTER_ON) {
  188.             TempI->LeftEdge += (Width - 38 - strlen(TempI->IText)*8)/2;
  189.         }
  190.         TempI = TempI->NextText;
  191.     }
  192.     if(FHEIGHT_ON) {
  193.         textheight = ForceHeight;
  194.     }
  195.     else {
  196.         textheight = textheight*9+46;
  197.     }
  198.  
  199.     if(BEEP_ON) {
  200.         IntuLock = LockIBase(0);
  201.         MyScreen = IntuitionBase->ActiveScreen;
  202.         UnlockIBase(IntuLock);
  203.         DisplayBeep(MyScreen);   /* Flash the Active Screen, before activating
  204.                                     AutoRequest */
  205.     }
  206.  
  207.     Success = (USHORT)AutoRequest(NULL,Intu1,Intu2,Intu3,0,0,Width,textheight);
  208.     if(Success == 0) error = 5;
  209.  
  210.     CleanUp(TRUE, NULL, error);
  211. }
  212.  
  213. VOID CleanUp(BOOL SUCCESS, char *Text,ULONG Error)
  214. {
  215.     if(!SUCCESS) {
  216.         PrintUsage();
  217.         Print(Text);
  218.     }
  219.  
  220.     FreeRemember(&RememberKey,TRUE);
  221.     if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  222.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  223.  
  224.     XCEXIT(Error);
  225. }
  226.  
  227. VOID PrintUsage()
  228. {
  229.     Print(UsageString);
  230.     Print("USAGE: QuickReq\t<Question> [<True>] <False> [OPTIONS] or\n");
  231.     Print("\t\t<Filename> [OPTIONS]\n");
  232.     Print("OPTIONS:\n\t-C  = Text centering\n");
  233.     Print("\t-B  = DisplayBeep\n");
  234.     Print("\t-L  = Large view\n");
  235.     Print("\t-W# = Width\n");
  236.     Print("\t-H# = Height\n");
  237.     Print("\t-P# = Pen color\n\n");
  238. }
  239.  
  240.  
  241. VOID ReadRedirFile(char *argv[])
  242. {
  243.     ULONG MyLock, FileSize, Success, error;
  244.     char *line;
  245.     struct FileInfoBlock *FIB;
  246.     int argc;
  247.  
  248.     MyLock = Lock(text[0],ACCESS_READ);
  249.     if(MyLock == NULL) {
  250.         CleanUp(FALSE,"Can't open redirection file!\n",IoErr());
  251.     }
  252.     FIB = (struct FileInfoBlock *)AllocNormMem(sizeof(struct FileInfoBlock));
  253.     if(FIB == NULL) {
  254.         CleanUp(FALSE,"Out of memory!\n",10);
  255.     }
  256.     Success = Examine(MyLock,FIB);
  257.     error = IoErr();
  258.     UnLock(MyLock);
  259.     if(Success == 0) {
  260.         CleanUp(FALSE,"File error!\n",error);
  261.     }
  262.     else if(FIB->fib_DirEntryType > 0) {
  263.         CleanUp(FALSE,"Object wrong type!\n",ERROR_OBJECT_WRONG_TYPE);
  264.     }
  265.     FileSize = FIB->fib_Size;
  266.     if(FileSize != 0) {
  267.         line = (char *)AllocNormMem(FileSize);
  268.         if(line == NULL) {
  269.             CleanUp(FALSE,"Out of memory!\n",10);
  270.         }
  271.         MyLock = Open(text[0],MODE_OLDFILE);
  272.         if(MyLock == NULL) {
  273.             CleanUp(FALSE,"File error!\n",IoErr());
  274.         }
  275.         Success = Read(MyLock,line,FileSize);
  276.         if(Success == -1) {
  277.             error = IoErr();
  278.             Close(MyLock);
  279.             CleanUp(FALSE,"File error!\n",error);
  280.         }
  281.         Close(MyLock);
  282.     }
  283.     else line = NULL;
  284.  
  285.     argc = ParseCommands(1, line, argv);
  286.     if(argc > MAXARG) {
  287.         CleanUp(FALSE,NULL,1);
  288.     }
  289.     error = CheckParameters(argc, argv);
  290.     if(error != 0) {
  291.         CleanUp(TRUE,NULL,error);
  292.     }
  293. }
  294.  
  295. USHORT TestWidth(struct IntuiText *MyIText)
  296. {
  297.     USHORT width, widest = 0;
  298.  
  299.     while(MyIText != NULL) {
  300.         width = strlen(MyIText->IText);
  301.         if(width > widest) widest = width;
  302.         MyIText = MyIText->NextText;
  303.     }
  304.     return(widest);
  305. }
  306.  
  307. VOID Print(char *String)
  308. {
  309.     ULONG output;
  310.  
  311.     output = Output();
  312.     if(output != NULL) {
  313.         Write(output,String,strlen(String));
  314.     }
  315. }
  316.  
  317. USHORT CheckParameters(int argc, char *argv[])
  318. {
  319.     char *NextStr =  NULL;
  320.     int count, textcount;
  321.     char Currentchar;
  322.  
  323.     count = 0;
  324.     textcount = 0;
  325.  
  326.     while(count < argc-1) {
  327.         NextStr = argv[count+1];
  328.         if(*NextStr == '-') {
  329.             NextStr++;
  330.             while((*NextStr != '\0') && (!isspace((char)*NextStr))) {
  331.                 Currentchar = toupper(*NextStr);
  332.                 if(Currentchar == 'C') {
  333.                     CENTER_ON = TRUE;
  334.                 }
  335.                 else if(Currentchar == 'B') {
  336.                     BEEP_ON = TRUE;
  337.                 }
  338.                 else if(Currentchar == 'L') {
  339.                     LARGE_ON = TRUE;
  340.                 }
  341.                 else if(Currentchar == 'W') {
  342.                     NextStr++;
  343.                     ForceWidth = ReadNumber(&NextStr);
  344.                     FWIDTH_ON = TRUE;
  345.                 }
  346.                 else if(Currentchar == 'H') {
  347.                     NextStr++;
  348.                     ForceHeight = ReadNumber(&NextStr);
  349.                     FHEIGHT_ON = TRUE;
  350.                 }
  351.                 else if(Currentchar == 'P') {
  352.                     NextStr++;
  353.                     PenNumber = ReadNumber(&NextStr);
  354.                     OWNPEN_ON = TRUE;
  355.                 }
  356.                 else {
  357.                     PrintUsage();
  358.                     Print("Unknown option ");
  359.                     Print(argv[count+1]);
  360.                     Print(" !\n");
  361.                     return(10);
  362.                 }
  363.                 NextStr++;
  364.             }
  365.         }
  366.         else if((textcount == 0) && (*NextStr == '?')) {
  367.             PrintUsage();
  368.             return(1);
  369.         }
  370.         else if(textcount <= 2) {
  371.             text[textcount] = NextStr;
  372.             textcount++;
  373.         }
  374.         else {
  375.             PrintUsage();
  376.             Print("Too many arguments!\n");
  377.             return(10);
  378.         }
  379.         count++;
  380.     }
  381.     return(0L);
  382. }
  383.  
  384. int ReadNumber(char *line[])
  385. {
  386.     char *temp;
  387.     USHORT Number = 0;
  388.  
  389.     temp = line[0];
  390.     while((*temp != ' ') && (*temp != '\0') && (isdigit(*temp))) {
  391.         Number = Number*10+(*temp - '0');
  392.         temp++;
  393.     }
  394.     line[0] = temp-1;
  395.     return(Number);
  396. }
  397.  
  398. struct IntuiText *MakeIntuText(char *text, USHORT height, USHORT width)
  399. {
  400.     struct IntuiText *FirstText, *CurrentText, *OldText = 0;
  401.     USHORT curheight = 0, curwidth, i;
  402.     char *writechar, *readchar, *oldreadchar;
  403.  
  404.     if(text == NULL) return(NULL);
  405.  
  406.     FirstText = (struct IntuiText *)AllocNormMem(sizeof(struct IntuiText));
  407.     if(FirstText == NULL) {
  408.         return(IPOINT);
  409.     }
  410.  
  411.     writechar = (char *)AllocNormMem(height*(width+1));
  412.     if(writechar == NULL) {
  413.         return(IPOINT);
  414.     }
  415.  
  416.     CurrentText = FirstText;
  417.     readchar = text;
  418.  
  419.     while((CurrentText != NULL) && (curheight < height)) {
  420.         if(OldText != NULL) {
  421.             OldText->NextText = CurrentText;
  422.         }
  423.         if(OWNPEN_ON) {
  424.             CurrentText->FrontPen = PenNumber % MaxCols;
  425.         }
  426.         else {
  427.             CurrentText->FrontPen = AUTOFRONTPEN;
  428.         }
  429.         if(CurrentText->FrontPen == AUTOBACKPEN) {
  430.             CurrentText->BackPen = (PenNumber+1) % MaxCols;
  431.         }
  432.         else {
  433.             CurrentText->BackPen = AUTOBACKPEN;
  434.         }
  435.         CurrentText->DrawMode = AUTODRAWMODE;
  436.         CurrentText->LeftEdge = AUTOLEFTEDGE;
  437.         CurrentText->TopEdge = AUTOTOPEDGE + (curheight * 9);
  438.         CurrentText->ITextFont = &MyTopaz8;
  439.         CurrentText->IText = (UBYTE *)writechar;
  440.  
  441.         oldreadchar = readchar;
  442.         curwidth = 0;
  443.         while((*readchar != '\\') && (curwidth < width)) {
  444.             if((*readchar == 0x0a) || (*readchar == 0x09)) {
  445.                 *readchar = ' ';
  446.             }
  447.             *writechar++ = *readchar;
  448.             if(*readchar++ == '\0') return(FirstText);
  449.             curwidth++;
  450.         }
  451.         if(*readchar == '\\') {
  452.             *writechar++ = '\0';
  453.             readchar++;
  454.         }
  455.         else if(curwidth >= width) {
  456.             i = 0;
  457.             while((*readchar != ' ') && (readchar != oldreadchar)) {
  458.                 --readchar;
  459.                 i++;
  460.             }
  461.             if(*readchar == ' ') {
  462.                 writechar -= i;
  463.                 *writechar++ = '\0';
  464.                 readchar++;
  465.             }
  466.             else {
  467.                 readchar += i;
  468.                 *writechar++ = '\0';
  469.             }
  470.         }
  471.         curheight++;
  472.         OldText = CurrentText;
  473.         CurrentText = (struct IntuiText *)AllocNormMem(sizeof(struct IntuiText));
  474.         if(CurrentText == NULL) {
  475.             return(IPOINT);
  476.         }
  477.     }
  478.     return(FirstText);
  479. }
  480.  
  481. char *AllocNormMem(int Size)
  482. {
  483.     char *MemPointer;
  484.  
  485.     MemPointer = (char *)AllocRemember(&RememberKey, Size,MEMF_PUBLIC|MEMF_CLEAR);
  486.  
  487.     return(MemPointer);
  488. }
  489.  
  490. /* ------------------------------------------ */
  491. /*   These are my modified startup routines   */
  492. /* ------------------------------------------ */
  493.  
  494. VOID _main(line)
  495. register char *line;
  496. {
  497.     int argc = 0;                    /* arg count */
  498.     char *argv[MAXARG];              /* arg pointers */
  499.  
  500.     argc = ParseCommands(0, line, argv);
  501.     if(argc > MAXARG) {
  502.         PrintUsage();
  503.         XCEXIT(1);
  504.     }
  505.  
  506.     main(argc, argv);
  507.     XCEXIT(0);
  508. }
  509.  
  510. /* I have separated this to own routine, because I also use this to parse
  511.    redirection file.
  512. */
  513.  
  514. int ParseCommands(int Start, register char *line, char *args[])
  515. {
  516.     register char **pargv;
  517.     int argc;
  518.  
  519.     argc = Start;
  520.  
  521.     while (argc < MAXARG)
  522.     {
  523.         while (isspace(*line))  line++;
  524.         if (*line == '\0')      break;
  525.         pargv = &args[argc++];
  526.         if (*line == QUOTE)
  527.             {
  528.             *pargv = ++line;  /* ptr inside quoted string */
  529.             while ((*line != '\0') && (*line != QUOTE)) line++;
  530.             if (*line == '\0') {
  531.                 return(MAXARG+1);
  532.             }
  533.             else                *line++ = '\0';  /* terminate arg */
  534.         }
  535.         else            /* non-quoted arg */
  536.         {
  537.             *pargv = line;
  538.             while ((*line != '\0') && (!isspace(*line))) line++;
  539.             if (*line == '\0')  break;
  540.             else                *line++ = '\0';  /* terminate arg */
  541.         }
  542.     }  /* while */
  543.  
  544.     return(argc);
  545. }
  546.