home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / cli_util / man.lha / Man / Man.c < prev    next >
C/C++ Source or Header  |  1993-07-27  |  9KB  |  324 lines

  1. /*
  2. Auto:        sc <file> LINK NODEBUG NOSTRCONS OPT SINT NOSTACKCHECK DATA=NEAR STARTUP=cres PARM=REGISTER UTILLIB NOSTKCHK
  3. */
  4.  
  5. /* $Revision Header built automatically *************** (do not edit) ************
  6. **
  7. ** © Copyright by GuntherSoft
  8. **
  9. ** File             : SnakeSYS:CPrgs/Utils/Man.c
  10. ** Created on       : Friday, 16.07.93 18:00:23
  11. ** Created by       : Kai Iske
  12. ** Current revision : V1.2
  13. **
  14. **
  15. ** Purpose
  16. ** -------
  17. **   - This is a man program, which may scan different directories
  18. **     for man-pages. These directories are set within an ENV-VAR
  19. **     called MANPATHS. Additionally a VIEWER may be set using
  20. **     ENV-VAR MANVIEW (for plain ASCII) or MANVIEWAG (for
  21. **     AMIGAGUIDE). THIS ONE`S PURE AND IN THE PUBLIC DOMAIN
  22. **
  23. ** Revision V1.2
  24. ** --------------
  25. ** created on Tuesday, 27.07.93 15:20:51  by  Kai Iske.   LogMessage :
  26. **   - Used MAN as a keyword for the template which prevented MAN
  27. **     to accept inputs like "man man".
  28. **
  29. ** Revision V1.1
  30. ** --------------
  31. ** created on Monday, 26.07.93 17:42:32  by  Kai Iske.   LogMessage :
  32. **   - Accidentially called Exit() instead of exit(), which
  33. **     prevented the program to pass the cleanup code of SAS.
  34. **     So a lock to the directory was kept and the shell could
  35. **     never been left.............
  36. **
  37. ** Revision V1.0
  38. ** --------------
  39. ** created on Friday, 16.07.93 18:00:23  by  Kai Iske.   LogMessage :
  40. **  -*-  changed on Saturday, 17.07.93 16:30:41  by  Kai Iske.   LogMessage :
  41. **   - Man now searches for files that end up with .doc/.man/.guide
  42. **     If a .guide file is found, the second Viewer (MANVIEWAG)
  43. **     will be used to display the AmigaGuide viewer. Otherwise
  44. **     the normal viewer (MANVIEW) will be used
  45. **  -*-  created on Friday, 16.07.93 18:00:23  by  Kai Iske.   LogMessage :
  46. **     --- Initial release ---
  47. **
  48. *********************************************************************************/
  49. #define REVISION "1.2"
  50. #define REVDATE  "27.07.93"
  51. #define REVTIME  "15:20:51"
  52. #define AUTHOR   "Kai Iske"
  53. #define VERNUM   1
  54. #define REVNUM   2
  55.  
  56. #include    <string.h>
  57. #include    <stdlib.h>
  58. #include    <exec/types.h>
  59. #include    <exec/memory.h>
  60. #include    <exec/execbase.h>
  61. #include    <proto/exec.h>
  62. #include    <proto/dos.h>
  63. #include    <dos/dos.h>
  64. #include    <dos/exall.h>
  65. #include    <dos/dostags.h>
  66.  
  67.  
  68. extern struct SysBase *SysBase;
  69. extern struct WBStartUp *_WBenchMsg;
  70.  
  71.  
  72. /**********************************************************************/
  73. /*                             Prototypes                             */
  74. /**********************************************************************/
  75. char    *GetDir(char *NewName, char *OldName);
  76.  
  77.  
  78.  
  79. /**********************************************************************/
  80. /*                           Version-String                           */
  81. /**********************************************************************/
  82. static char *Version = "$VER: Man "REVISION" ("REVDATE")\0";
  83.  
  84.  
  85. /**********************************************************************/
  86. /*                 Template for Command-Line parsing                  */
  87. /**********************************************************************/
  88. static char *Template    = "MANPAGE/A";
  89. enum {MAN_ARG};
  90.  
  91.  
  92.  
  93. /**********************************************************************/
  94. /*                       This is the main part                        */
  95. /**********************************************************************/
  96. void main(void)
  97. {
  98.     char    ManPaths[512];
  99.     char    ViewCmd[512];
  100.     char    ViewCmdAG[512];
  101.     char    CheckDir[512];
  102.     char    SearchName[512];
  103.     char    FileName[130];
  104.     struct    RDArgs        *RDArgs    = NULL;
  105.     struct    ExAllControl    *EAC    = NULL;
  106.     APTR    *Args;
  107.     APTR    EAB            = NULL;
  108.     char    *DirOffset        = ManPaths;
  109.     char    Pattern[270];
  110.     BPTR    TestLock;
  111.     BPTR    OutHandle;
  112.     BOOL    Found            = FALSE,
  113.         GoOn;
  114.  
  115.         // Get Out Handle
  116.  
  117.     OutHandle = Output();
  118.  
  119.         // Check System we`re running on
  120.         // Whoops, System checks are "done" by startup code anyway ;)
  121.  
  122.         // Don`t start from WB
  123.  
  124.     if(_WBenchMsg)
  125.         exit(0);
  126.  
  127.  
  128.         // Get buffer for Commandline parsing
  129.  
  130.     if((Args = AllocVec((MAN_ARG + 1)*sizeof(ULONG), MEMF_CLEAR)))
  131.     {
  132.             // Get structure for ExAll()
  133.  
  134.         if((EAC = AllocDosObject(DOS_EXALLCONTROL, NULL)))
  135.         {
  136.                 // Get buffer for ExAll()
  137.  
  138.             if((EAB = AllocVec(sizeof(struct ExAllData)*20, MEMF_CLEAR)))
  139.             {
  140.                     // Try to parse commandline
  141.  
  142.                 if((RDArgs = ReadArgs(Template, (LONG *)Args, NULL)))
  143.                 {
  144.                         // Try to get MANPATHS Env-Variable
  145.  
  146.                     if(GetVar("MANPATHS", ManPaths, 256, GVF_GLOBAL_ONLY) >= 1)
  147.                     {
  148.                             // Try to get MANVIEW Env-Variable
  149.  
  150.                         if(GetVar("MANVIEW", ViewCmd, 256, GVF_GLOBAL_ONLY) >= 1)
  151.                         {
  152.                                 // Try to get MANVIEWAG Env-Variable
  153.  
  154.                             if(GetVar("MANVIEWAG", ViewCmdAG, 256, GVF_GLOBAL_ONLY) >= 1)
  155.                             {
  156.                                     // Set pattern for ExAll() search
  157.  
  158.                                 strcpy(SearchName, Args[MAN_ARG]);
  159.                                 strcat(SearchName, "(.doc|.man|.guide)");
  160.  
  161.                                     // Parse the pattern
  162.  
  163.                                 if(ParsePatternNoCase(SearchName, Pattern, 270) != -1)
  164.                                 {
  165.  
  166.                                         // Loop for all dirs and wait until file has been found
  167.  
  168.                                     while(!Found && DirOffset)
  169.                                     {
  170.                                             // Extract next directory from list
  171.  
  172.                                         DirOffset = GetDir(CheckDir, DirOffset);
  173.  
  174.                                             // Add "/" if neither "/", nor ":" end the directory
  175.  
  176.                                         if((CheckDir[strlen(CheckDir) - 1] != '/') && (CheckDir[strlen(CheckDir) - 1] != ':'))
  177.                                             strcat(CheckDir, "/");
  178.  
  179.                                             // Directory available ???
  180.  
  181.                                         if((TestLock = Lock(CheckDir, ACCESS_READ)))
  182.                                         {
  183.                                                 // Fill in ExAll structure
  184.  
  185.                                             EAC->eac_LastKey    = 0;
  186.                                             EAC->eac_MatchString    = Pattern;
  187.                                             EAC->eac_MatchFunc    = NULL;
  188.  
  189.                                             do
  190.                                             {
  191.                                                     // Do the scanning
  192.  
  193.                                                 GoOn = ExAll(TestLock, EAB, (108*20), ED_NAME, EAC);
  194.  
  195.                                                     // Error occured ???
  196.  
  197.                                                 if((!GoOn) && (IoErr() != ERROR_NO_MORE_ENTRIES))
  198.                                                     PrintFault(IoErr(), "Man ");
  199.  
  200.                                                     // End of dir reached ;
  201.  
  202.                                                 if(EAC->eac_Entries == 0)
  203.                                                     GoOn = FALSE;
  204.                                                 else
  205.                                                 {
  206.                                                         // Get name of file
  207.  
  208.                                                     strcpy(FileName, ((struct ExAllData *)EAB)->ed_Name);
  209.  
  210.                                                         // Check extension to decide whether it`s an AmigaGuide file
  211.  
  212.                                                     if(strcmp(&FileName[strlen(FileName) - 6], ".guide"))
  213.                                                         strcpy(SearchName, ViewCmd);
  214.                                                     else
  215.                                                         strcpy(SearchName, ViewCmdAG);
  216.  
  217.                                                         // Append directory and FileName
  218.  
  219.                                                     strcat(SearchName, " ");
  220.                                                     strcat(SearchName, CheckDir);
  221.  
  222.                                                         // Copy Filename to place where the last quote used to reside
  223.  
  224.                                                     strcat(SearchName, FileName);
  225.  
  226.  
  227.                                                         // Set flag that we`ve found the file
  228.  
  229.                                                     Found = TRUE;
  230.                                                 }
  231.                                             } while(GoOn);
  232.  
  233.                                                 // Unlock Directory
  234.  
  235.                                             UnLock(TestLock);
  236.                                         }
  237.                                         else
  238.                                         {
  239.                                                 // Display message
  240.  
  241.                                             FPuts(OutHandle, "Skipping directory; not existent : ");
  242.                                             FPuts(OutHandle, CheckDir);
  243.                                             FPuts(OutHandle, "\n");
  244.                                         }
  245.                                     }
  246.                                 }
  247.                                 else
  248.                                     PrintFault(IoErr(), "Man ");
  249.                             }
  250.                             else
  251.                                 PrintFault(IoErr(), "MANVIEWAG ");
  252.                         }
  253.                         else
  254.                             PrintFault(IoErr(), "MANVIEW ");
  255.                     }
  256.                     else
  257.                         PrintFault(IoErr(), "MANPATHS ");
  258.                 }
  259.                 else
  260.                     PrintFault(IoErr(), "Man ");
  261.             }
  262.             else
  263.                 FPuts(OutHandle, "Could not allocate buffer for ExAll()\n");
  264.         }
  265.         else
  266.             FPuts(OutHandle, "Could not allocate structure for ExAll()\n");
  267.     }
  268.     else
  269.         FPuts(OutHandle, "Could not allocate buffer for CommandLine Parsing\n");
  270.  
  271.     if(Args && RDArgs && !Found)
  272.         strcpy(FileName, Args[MAN_ARG]);
  273.  
  274.     if(EAC)
  275.         FreeDosObject(DOS_EXALLCONTROL, (void *)EAC);
  276.  
  277.     if(EAB)
  278.         FreeVec(EAB);
  279.  
  280.     if(RDArgs)
  281.         FreeArgs(RDArgs);
  282.  
  283.     if(Args)
  284.         FreeVec(Args);
  285.  
  286.     if(Found)
  287.     {
  288.         SystemTags(SearchName, TAG_DONE);
  289.         exit(0);
  290.     }
  291.     else if(Args && RDArgs)
  292.     {
  293.         FPuts(OutHandle, "Man-Pages not found for : ");
  294.         FPuts(OutHandle, FileName);
  295.         FPuts(OutHandle, "\n");
  296.         exit(10);
  297.     }
  298. }
  299.  
  300.  
  301.  
  302.  
  303.  
  304. /**********************************************************************/
  305. /*          Get portions from the Env-Var -> Next directory           */
  306. /**********************************************************************/
  307. char *GetDir(char *NewDir, char *OldDir)
  308. {
  309.     while(*OldDir == ' ')
  310.         OldDir++;
  311.  
  312.     while((*OldDir != '\n') && (*OldDir != '\0') && (*OldDir != '|') && (*OldDir != ','))
  313.         *NewDir++ = *OldDir++;
  314.  
  315.     *NewDir        = '\0';
  316.  
  317.     if(*OldDir == '\n' || *OldDir == '\0')
  318.         OldDir = NULL;
  319.     else
  320.         OldDir++;
  321.  
  322.     return(OldDir);
  323. }
  324.