home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / printimage_394.lzh / 'Liner / Source / arexx.c next >
C/C++ Source or Header  |  1990-10-28  |  14KB  |  527 lines

  1. #include "globals.h"
  2. #include "minrexx.h"
  3. #include "ARexxWdw.h"
  4.  
  5. extern struct RxsLib *RexxSysBase;
  6. struct RexxMsg *LinerRexxMsg;
  7. int LinerDispatch();
  8.  
  9. /*Function definitions private to arexx.c*/
  10. void DoOpen(),DoSave(),DoDoubClick(),DoSetTitleMsg();
  11. void DoGetStats(),DoPrintDisk(),DoGetChar(),DoGetWord(),DoGetLineText();
  12. void DoCursorRight(),DoCursorLeft(),DoDoubSpacing(),DoInterlaced();
  13. void DoIcons(),DoScrnSize(),DoStartingLevel(),DoLoadPrefs(),DoARexxSearch();
  14. void DoInsertText();
  15. void DoGetLine(),DoStartLine(),DoEndLine(),DoPushIn(),DoPullOut();
  16. void DoFakeReturn(),SpecReplyToRexx();
  17. void DoGetFilename(),DoPutPrefs();
  18.  
  19. struct rexxCommandList rcl[]= /*The list of all the ARexx commands and*/
  20. {        /*their associated functions*/
  21.    { "open" , (APTR)&DoOpen},
  22.    { "save" , (APTR)&DoSave},
  23.    { "getfilename" , (APTR)&DoGetFilename},
  24.    { "new" , (APTR)&NewAll},
  25.    { "printprinter", (APTR)&HandlePrintPrinter},
  26.    { "printdisk" , (APTR)&DoPrintDisk},
  27.    { "quit" , (APTR)&CloseStuff },
  28.    { "doubclick" , (APTR) &DoDoubClick},
  29.    { "settitlemsg" , (APTR) &DoSetTitleMsg},
  30.    { "getstats" , (APTR) &DoGetStats},
  31.    { "getchar" , (APTR) &DoGetChar},
  32.    { "getword" , (APTR) &DoGetWord},
  33.    { "getlinetext" , (APTR) &DoGetLineText},
  34.    { "cut" , (APTR) &HandleCut},
  35.    { "copy" , (APTR) &HandleCopy},
  36.    { "paste" , (APTR) &HandlePaste},
  37.    { "erase" , (APTR) &HandleErase},
  38.    { "cursorup" , (APTR) &CursorUp},
  39.    { "cursordown" , (APTR) &CursorDown},
  40.    { "cursorright" , (APTR) &DoCursorRight},
  41.    { "cursorleft" , (APTR) &DoCursorLeft},
  42.    { "totop" , (APTR) &JumpToTop},
  43.    { "tobottom" , (APTR) &JumpToBottom},
  44.    { "upscreen" , (APTR) &WholeScreenUp},
  45.    { "downscreen" , (APTR) &WholeScreenDown},
  46.    { "doubspacing" , (APTR) &DoDoubSpacing},
  47.    { "interlaced" , (APTR) &DoInterlaced},
  48.    { "icons" , (APTR) &DoIcons },
  49.    { "scrnsize" , (APTR)&DoScrnSize},
  50.    { "startinglevel" , (APTR)&DoStartingLevel},
  51.    { "loadprefs" , (APTR)&HandleReloadPrefs},
  52.    { "sveprefs" , (APTR)&DoPutPrefs},
  53.    { "search" , (APTR)&DoARexxSearch},
  54.    { "instext" , (APTR)&DoInsertText},
  55.    { "getline" , (APTR)&DoGetLine},
  56.    { "fakereturn" , (APTR)&DoFakeReturn},
  57.    { "fakebs" , (APTR)&HandleBS},
  58.    { "pushin" , (APTR)&DoPushIn},
  59.    { "pullout" , (APTR)&DoPullOut},
  60.    { "startline" , (APTR)&DoStartLine},
  61.    { "endline" , (APTR) &DoEndLine},
  62.    { NULL , NULL}
  63. };
  64.  
  65. void OpenARexx() /*Open the ARexx port*/
  66. {
  67.    ARexxSigBit=upRexxPort("liner",rcl,"liner",&LinerDispatch);
  68. }
  69.  
  70. void CloseARexx() /*Close the ARexx port*/
  71. {
  72.    dnRexxPort();
  73. }
  74.  
  75. void CheckRexxPort() /*Check the ARexx port for commands*/
  76. {
  77.    dispRexxPort();
  78. }
  79.  
  80. int LinerDispatch(msg,rcl,p) /*The minrexx userdisp function*/
  81. struct RexxMsg *msg;
  82. struct rexxCommandList *rcl;
  83. char *p;
  84. {
  85.    
  86.    LinerRexxMsg=(struct RexxMsg *)msg;
  87.    
  88.    ((int(*)())(rcl->userdata))(p); /*Call the function associated with the */
  89.                            /*ARexx command received*/
  90.    
  91.       /*If the function is one of those that returns something to ARexx*/
  92.    if(strcmp(rcl->name,"open")==0 || strcmp(rcl->name,"printdisk")==0 ||
  93.          strcmp(rcl->name,"getlinetext")==0 || strcmp(rcl->name,"getline")==0 ||
  94.          strcmp(rcl->name,"getchar")==0 || strcmp(rcl->name,"linersearch")==0 ||
  95.          strcmp(rcl->name,"getstats")==0 || strcmp(rcl->name,"getword")==0 ||
  96.          strcmp(rcl->name,"startinglevel")==0||strcmp(rcl->name,"fakereturn")==0
  97.          || strcmp(rcl->name,"pushin") || strcmp(rcl->name,"pullout") ||
  98.          strcmp(rcl->name,"getfilename") || strcmp(rcl->name,"sveprefs") )
  99.       return(1);  /*Tell minrexx not to replyRexxCmd()*/
  100.    else
  101.       return(0);
  102. }   
  103.  
  104. void DoOpen(p)   /*Open a file under ARexx control*/
  105. char *p;
  106. {
  107.   
  108.    /*Setup the filename*/
  109.    strcpy(FileName,&p[1]);
  110.    if(ReadItemList(FileName,TRUE))  /*Do the read*/
  111.    {
  112.       Modified=FALSE;
  113.       TitleErrorCancel();
  114.       SplitFilename(FileName,SFileName,SDirName);
  115.       replyRexxCmd(LinerRexxMsg,0,0,"true");
  116.    }
  117.    else
  118.       replyRexxCmd(LinerRexxMsg,0,0,"false");
  119. }
  120.  
  121. void DoSave(p)   /*Save a file under ARexx*/
  122. char *p;
  123. {
  124.    strcpy(FileName,&p[1]);
  125.    SplitFilename(FileName,SFileName,SDirName);
  126.    Save();  /*Write the file out*/
  127. }
  128.  
  129. void DoGetFilename()
  130. {
  131.    char filename[183];
  132.    
  133.    strcpy(filename,SDirName);
  134.    if(filename[strlen(filename)-1]!=':' && filename[strlen(filename)-1]!=NULL)
  135.       strcat(filename,"/");   /*Append a / if not at the root*/
  136.    strcat(filename,SFileName);
  137.    replyRexxCmd(LinerRexxMsg,0,0,filename);
  138. }
  139.  
  140. void DoPrintDisk(p)  /*Print a file to disk*/
  141. char *p;
  142. {
  143.    SplitFilename(++p,PFileName,PDirName);
  144.    SpecReplyToRexx(HandlePrintDisk(TRUE,p),TRUE);
  145. }
  146.  
  147. void DoDoubClick(p)  /*Fake a double click (good for highlighting blocks)*/
  148. char *p;
  149. {
  150.    p++;
  151.    PtrY=CurY;
  152.    if(strcmp(p,"true")==0) /*if the click is meant to be outside a line*/
  153.       PtrX=1;
  154.    else
  155.       PtrX=CurX;
  156.    HandleInvs();
  157.    BLastX=CurX;
  158. }
  159.  
  160. void DoSetTitleMsg(p) /*Print message in 'p' in title bar*/
  161. char *p;
  162. {
  163.    TitleError(&p[1]);
  164. }
  165.  
  166. void DoGetStats()  /*Tell ARexx the CurrentItem's level, number, and */
  167. {                    /*whether or not it's a continuation*/
  168.    char buffer1[7];
  169.    static char buffer2[12];
  170.    
  171.    stci_d(buffer2,CurrentItem->Level); /*Convert the numbers into text*/
  172.    strcat(buffer2," ");
  173.    stci_d(buffer1,CurrentItem->ItemNumber);
  174.    strcat(buffer2,buffer1);
  175.    if(CurrentItem->cont)
  176.       strcat(buffer2," true");   /*true==continuation*/
  177.    else
  178.       strcat(buffer2," false");
  179.       
  180.    replyRexxCmd(LinerRexxMsg,0,0,buffer2);
  181.    
  182.       
  183. }
  184.  
  185. void DoGetChar() /*Send the character under the cursor to ARexx*/
  186. {
  187.    char letter[2];
  188.    
  189.    letter[1]=NULL;
  190.    letter[0]=CurrentItem->Text[PosInText(CurrentItem->Level)];/*Get the letter*/
  191.    replyRexxCmd(LinerRexxMsg,0,0,letter);
  192. }
  193.  
  194. void DoGetWord() /*Send the word under the cursor to ARexx*/
  195. {
  196.    static result[80];
  197.    BYTE c;
  198.    
  199.    for(c=0;c<80;result[c++]=NULL); /*Clear the string (weird things happen*/
  200.                      /*if I don't, and I don't know why...)*/
  201.    strcpy(result,"");
  202.    GetWord(result,CurrentItem);
  203.    replyRexxCmd(LinerRexxMsg,0,0,result);
  204. }
  205.  
  206. void DoGetLineText() /*Send the current line's text to ARexx*/
  207. {
  208.    static buffer[100];
  209.    strcpy(buffer,CurrentItem->Text);
  210.    replyRexxCmd(LinerRexxMsg,0,0,buffer);
  211. }
  212.  
  213. void DoCursorRight()   /*Move the cursor to the right*/
  214. {
  215.    CursorRight(0);
  216. }
  217.  
  218. void DoCursorLeft() /*Move the cursor to the left*/
  219. {
  220.    CursorLeft(0);
  221. }
  222.  
  223. void DoDoubSpacing(p) /*Set the spacing*/
  224. char *p;
  225. {
  226.    p++;
  227.    if(strcmp(p,"true")==0) /*Set double spacing*/
  228.    {
  229.       if(!prefs.DS)  /*If not double spaced*/
  230.       {
  231.          DoubleSpacing();
  232.          ModifyMenus(32+StartingLevel); /*Change menus appropriately*/
  233.       }
  234.    }
  235.    else  /*Unset double spacing*/
  236.       if(prefs.DS) /*If double spaced*/
  237.       {
  238.          DoubleSpacing();
  239.          ModifyMenus(StartingLevel);
  240.       }
  241. }
  242.  
  243. void DoInterlaced(p) /*Determine whether the screen is/isn't interlaced*/
  244. char *p;
  245. {
  246.    p++;
  247.    
  248.    if(!prefs.Laced)   /*If interlaced*/
  249.    {
  250.       if(strcmp(p,"true")==0) /*Switch to non-interlaced*/
  251.       {
  252.          HandleInterlaced();
  253.          ModifyOtherMenus(1);
  254.       }
  255.    }
  256.    else
  257.       if(strcmp(p,"false")==0)   /*Switch to interlaced*/
  258.       {
  259.          HandleInterlaced();
  260.          ModifyOtherMenus(1);
  261.       }
  262.    
  263. }
  264.  
  265. void DoIcons(p)   /*Set program to create/not create icons*/
  266. char *p;
  267. {
  268.    p++;
  269.    if(((prefs.Icons) && (strcmp(p,"false")==0)) ||
  270.          ((!prefs.Icons) && (strcmp(p,"true")==0) ) )
  271.    {
  272.       ModifyOtherMenus(2); /*Modify the menu flag*/
  273.       HandleIcons();
  274.    }
  275. }
  276.  
  277. void DoScrnSize(p)   /*Set the screen size (workbench or standard)*/
  278. char *p;
  279. {
  280.    p++;
  281.    
  282.    if( ( (prefs.Screen == 0) && (strcmp(p,"workbench")==0) ) ||
  283.          ( (prefs.Screen == 1) && (strcmp(p,"standard")==0) ) )
  284.    {
  285.       ModifyOtherMenus(3); /*Modify the menu flag*/
  286.       SwapScrnSize();      /*Change the screen size*/
  287.    }
  288. }
  289.  
  290. void DoStartingLevel(p) /*Set the starting level*/
  291. char *p;
  292. {
  293.    int level;
  294.    p++;
  295.    p[1]=NULL;
  296.    stcd_i(p,&level); /*Get the level (as a number)*/
  297.    if(level < 0 || level > 5)
  298.       SpecReplyToRexx(FALSE);
  299.    if(level == StartingLevel)
  300.       SpecReplyToRexx(TRUE);
  301.       
  302.    if(prefs.DS)   /*If double spaced*/
  303.       level+=32;  /*Alter level for ModifyMenus()*/
  304.    ModifyMenus(level);
  305.    StartingLevel=level; /*Record the new starting level*/
  306.    Refresh();  /*And update the display*/
  307.       SpecReplyToRexx(TRUE);
  308.    
  309. }
  310.  
  311. void DoARexxSearch(p)   /*Do a search/replace*/
  312. char *p;
  313. {
  314.    BYTE cse,partial,repl,c,i,old;
  315.    char search[80],replace[80];
  316.    search[0]=replace[0]=NULL;
  317.    
  318.    for(c=0;c<80;search[c++]=NULL)
  319.       replace[c]=NULL;
  320.    
  321.    cse=Interp(p[1]);
  322.    partial=Interp(p[2]);
  323.    repl=Interp(p[3]);
  324.    
  325.    if(!(p[4]==NULL || p[5]==NULL)) /*If there is a word to search for*/
  326.    {     /*use it.  Otherwise, use what the user has specified*/
  327.       for(c=5,i=0;p[c] != NULL && p[c]!=' ';i++,c++);
  328.       strncpy(search,&p[5],i);
  329.       if(p[c]!=NULL && p[c+1]!=NULL)
  330.       {
  331.          for(old=++c,i=0;p[c]!=NULL && p[c]!=' ';++i,c++);
  332.          strncpy(replace,&p[old],i);
  333.       }
  334.    }
  335.    
  336.    /*Set the appropriate search variables*/
  337.    ModifyParams(search,replace,cse,partial,repl);
  338.    if(DoSearch(TRUE,TRUE))  /*Do the actual search*/
  339.       strcpy(search,"true");
  340.    else
  341.       strcpy(search,"false");
  342.       
  343.    /*Give ARexx the result*/
  344.    replyRexxCmd(LinerRexxMsg,0,0,search);
  345.  
  346. }
  347.  
  348. Interp(p)   /*Used by DoARexxSearch.  Determines if a parameter send by ARexx*/
  349. char p;  /*specified setting a search variable to TRUE, FALSE, or leaving it*/
  350. {        /*unchanged*/
  351.    switch(p)
  352.    {
  353.       case 't':
  354.       case 'T':
  355.          return(TRUE);
  356.       case 'f':
  357.       case 'F':
  358.          return(FALSE);
  359.       case 'u':
  360.       case 'U':
  361.          return(100);   /*Unchanged*/
  362.    }
  363.    return(100);   /*If none of the above, something's screwy.  Best to not*/
  364. }                    /*change anything*/
  365.  
  366. void DoInsertText(p) /*Insert a text string into the line after the cursor*/
  367. char *p;
  368. {
  369.    char WorkString[160];
  370.    UBYTE TempX;
  371.    
  372.    WorkString[0]=NULL;
  373.    
  374.    strcpy(WorkString,CurrentItem->Text);
  375.    WorkString[PosInText(CurrentItem->Level)]=NULL;
  376.    strcat(WorkString,&p[1]);
  377.    strcat(WorkString,&CurrentItem->Text[PosInText(CurrentItem->Level)]);
  378.    WorkString[MaxLen(CurrentItem->Level)]=NULL;
  379.    strcpy(CurrentItem->Text,WorkString);
  380.    TempX=CurX;
  381.    PlotCursor(1,CurY);
  382.    PrintItem(CurrentItem);
  383.    PlotCursor(TempX,CurY);
  384.  
  385. }
  386.  
  387. void DoGetLine() /*Return an entire line to ARexx (incl. line number)*/
  388. {
  389.    static buffer[100];  /*static to make sure it doesn't disappear before*/
  390.  
  391.    MakeTextLine(buffer,CurrentItem); /*ARexx gets it*/
  392.    buffer[strlen(CurrentItem->Text)-1]=NULL;
  393.    replyRexxCmd(LinerRexxMsg,0,0,buffer);
  394. }
  395.  
  396. void DoFakeReturn(p) /*Let ARexx push the return button (i.e. create a line)*/
  397. char *p;
  398. {
  399.    BYTE stat;
  400.    p++;
  401.    if(strcmp(p,"shift")==0)
  402.       stat=HandleReturn(3);
  403.    else
  404.       stat=HandleReturn(0);
  405.    SpecReplyToRexx(stat);
  406. }
  407.  
  408. void DoStartLine()
  409. {
  410.    CursorLeft(8); /*Fake a SHIFT-CURSOR-LEFT*/
  411. }
  412.  
  413. void DoEndLine()
  414. {
  415.    CursorRight(8); /*Fake a SHIFT-CURSOR-RIGHT*/
  416. }
  417.  
  418. void DoPushIn()   /*Fake a TAB*/
  419. {
  420.    SpecReplyToRexx(HandleTAB(0));
  421. }
  422.  
  423. void DoPullOut()  /*Fake a SHIFT-TAB*/
  424. {
  425.    SpecReplyToRexx(HandleTAB(3));
  426. }
  427.  
  428. void DoPutPrefs()
  429. {
  430.    SpecReplyToRexx(PutPrefs("liner:liner.prefs",TRUE));
  431. }
  432.  
  433. void SpecReplyToRexx(stat) /*Send a 'true' or 'false' to ARexx*/
  434. BYTE stat;
  435. {
  436.    if(stat)
  437.       replyRexxCmd(LinerRexxMsg,0,0,"true");
  438.    else
  439.       replyRexxCmd(LinerRexxMsg,0,0,"false");
  440. }
  441.  
  442. void GetWord(result,item) /*Get the word under the cursor*/
  443. char *result;
  444. struct LineItem *item;
  445. {
  446.    BYTE start,end,pos;
  447.    
  448.    pos=PosInText(CurrentItem->Level);
  449.    if(pos>=strlen(CurrentItem->Text))
  450.    {
  451.       strcpy(result,"");
  452.       return;
  453.    }
  454.    
  455.    start=pos;
  456.    if(pos!=0)
  457.       for(start--;CurrentItem->Text[start]!=' ' && start >= 0;start--);
  458.       
  459.    if(CurrentItem->Text[pos]!=NULL) /*Not at the end of the line...*/
  460.       for(end=pos;CurrentItem->Text[end]!=' ' && CurrentItem->Text[end]!=NULL;
  461.             end++);
  462.             
  463.    strncpy(result,&CurrentItem->Text[start+1],end-start);
  464. }
  465.  
  466. void EngageMacro(name) /*Start an external ARexx macro ('ENGAGE!' - J-LP :-)*/
  467. char *name;
  468. {  
  469.    asyncRexxCmd(name);
  470. }
  471.  
  472. void GetMacroInfo() /*Open the window to get the macro names*/
  473. {
  474.    struct Window *MacroWdw;
  475.    struct IntuiMessage *mesg;
  476.    struct Gadget *gadg;
  477.    char Name[4][80],Macro[4][80];
  478.    BYTE c;
  479.    
  480.    for(c=0;c<4;c++) /*Store the Names and Macro filename in case CANCEL*/
  481.    {                 /*gets pushed*/
  482.       strcpy(Name[c],prefs.Name[c]);
  483.       strcpy(Macro[c],prefs.Macro[c]);
  484.    }
  485.    
  486.    NewWindowStructure1.Screen=(struct Screen *) Screen;
  487.    MacroWdw=(struct Window *)OpenWindow(&NewWindowStructure1);
  488.    if(MacroWdw==NULL)
  489.    {
  490.       Leave(0,"Couldn't open the window!");
  491.       return;
  492.    }
  493.       
  494.    PrintIText(MacroWdw->RPort,&IntuiTextList1,0,0); /*Print the text*/
  495.    
  496.    Wait(1<<MacroWdw->UserPort->mp_SigBit);   /*Wait for Done to be pressed*/
  497.    
  498.    mesg=(struct IntuiMessage *)GetMsg(MacroWdw->UserPort);
  499.    gadg=(APTR)mesg->IAddress;
  500.    ReplyMsg(mesg);
  501.    
  502.    if(gadg->GadgetID == 10) /*Cancel was pressed*/
  503.       for(c=0;c<4;c++)
  504.       {
  505.          strcpy(prefs.Name[c],Name[c]);
  506.          strcpy(prefs.Macro[c],Macro[c]);
  507.       }
  508.    else  /*OK*/
  509.       CopyPrefsToMenus();
  510.    
  511.    CloseWindow(MacroWdw);
  512. }
  513.  
  514. void SplitFilename(Filename,file,dir) /*Split a full filename into */
  515. char *Filename,*file,*dir; /*path, filename, and extension*/
  516. {
  517.    char path[150];
  518.    char ext[150];
  519.    
  520.    strsfn(Filename,dir,path,file,ext);
  521.    strcat(dir,path);
  522.    strcpy(file,&Filename[strlen(dir)+((Filename[strlen(dir)]=='/') ? 1 : 0)]);
  523. /*   strcat(file,ext); */
  524. }
  525.  
  526. /*End of arexx.c*/
  527.