home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff241.lzh / Man / man.c < prev    next >
C/C++ Source or Header  |  1989-08-27  |  8KB  |  267 lines

  1. /**********************************************************************\
  2.  *                              Man V1.2                              *
  3.  *                 Copyright (C) 1989 by G. Glendown                  *
  4.  *                                                                    *
  5.  * This program and all documentation is placed in the public domain. *
  6.  *No fee may be charged except for media and copying.  ArcPrep may be *
  7.  * included on Fred Fish's distribution disk.  Any other PD-collector *
  8.  * will have to get my permission prior to putting Man on his disks.  *
  9.  *     Any net service may distribute Man, as long as no fee for      *
  10.  *downloading is charged (execpt normal line costs).  You may make any*
  11.  * changes to Man, but I would appreciate it if you could send me the *
  12.  *  new sources (except conversions to Lattice-C or Modula-II) or at  *
  13.  *              least a short note about what you did...              *
  14.  *                                                                    *
  15.  * German PD dealers:                                                 *
  16.  *             Please read the file 'GermanDistribution'              *
  17. \**********************************************************************/
  18.  
  19.  
  20. /* ts=4 */
  21. /*
  22.     *    Man    V1.2
  23.     *
  24.     *     Unix-style manual command controlled completely by 
  25.     *     the file "man:manuals"
  26.     *
  27.     * USAGE:
  28.     *
  29.     *    man <manual> [-vTextViewer] [-pPath] [-f]
  30.     *
  31. */
  32.  
  33. #include <stdio.h>
  34.  
  35. #define LINE    512
  36. #define    MAXCOM    16            /* Maximum size of command name */
  37. #define    EMPTY    "                "
  38. #define FNPOS    20            /* start position of command line/filename */
  39. #define MANFILE    "man:manuals"
  40.  
  41. /* Uncomment this if you don't have my library with a new PrintF... */
  42. /*#define PrintF printf*/
  43. int cnt,fc;
  44.  
  45. FILE *fh,*fopen();
  46. char *fgets();
  47.  
  48. char line[LINE],w[MAXCOM<<1];
  49. char path[11][128];
  50. char cmmnd[128]={"run less "};
  51. int Status=0;
  52. #define CUSTOMVIEW    (1<<0)
  53. #define FIRSTONLY    (1<<1)
  54. #define DISPLAYPATHS    (1<<2)
  55. int pt;
  56.  
  57. char *name;
  58.  
  59. /**********************************************************************\
  60.  *                                main                                *
  61.  *                                                                    *
  62.  * Does all scanning of the CLI parameters, prints the paths defined  *
  63.  *                   and checks the 'manuals'-file.                   *
  64. \**********************************************************************/
  65.  
  66. main(argc,argv)
  67. int argc;
  68. char *argv[];
  69. {
  70. char    *c;
  71. int t;
  72.     if (argc<=1) 
  73.         PrintF("Use '%s man' to see usage of %s\n",argv[0],argv[0]),exit(0L);
  74.     
  75.     cnt=0;
  76.  
  77.     name=(char *)NULL;
  78.  
  79.     for (t=1;t<argc;t++) {
  80.         if (argv[t][0]=='-') {
  81.             switch (toupper(argv[t][1])) {
  82.                 case 'V':
  83.                     strcpy(&(cmmnd[4]),&(argv[t][2]));
  84.                     strcat(cmmnd," \0");
  85.                     Status|=CUSTOMVIEW;
  86.                     break;
  87.                 case 'P':
  88.                     strcpy(path[pt],&argv[t][2]);
  89.                     pt++;
  90.                     break;
  91.                 case 'F':
  92.                     Status|=FIRSTONLY;
  93.                     break;
  94.                 case 'D':
  95.                     Status|=DISPLAYPATHS;
  96.                     break;
  97.                 default:
  98.                     PrintF("Illegal option '%c' ignored\n",argv[t][1]);
  99.                     break;
  100.             }
  101.         }
  102.         else
  103.             name=argv[t];
  104.     }
  105.  
  106.     if ((name==(char *)NULL)&&(!(Status&DISPLAYPATHS)))
  107.         PrintF("No manual specified!\n"),exit(0L);
  108.  
  109.     if (fh=fopen(MANFILE,"r"))
  110.     {
  111.         SearchStart(fh);
  112.         if (Status&DISPLAYPATHS) {
  113.             PrintF("Man-Paths:\n");
  114.             for (t=0;t<pt;t++)
  115.                 PrintF("\t%s\n",path[t]);
  116.             PrintF("File-viewer: '%s'\n",cmmnd);
  117.             fclose(fh);
  118.             exit(0L);
  119.         }
  120.         strncpy(w,name,MAXCOM);
  121.         strcat(w,EMPTY);
  122.         do {
  123.             if (!(c=fgets(line,LINE,fh)))
  124.                 PrintF("Oops! Manual-file corrupt!\n"),ex(name);
  125.             if (strncmp(line,w,MAXCOM)==0) {
  126.                 cnt++;
  127.                 fc++;
  128.                 DisplayFile(&line[FNPOS]);
  129.                 if (Status&FIRSTONLY) 
  130.                     fclose(fh),
  131.                     exit(0L);
  132.             }
  133.             if (strncmp(line,"END_MAN",7)==0) ex(name);
  134.         }
  135.         while (1);
  136.     }
  137.     ex();
  138. }
  139.  
  140. /**********************************************************************\
  141.  *                                 ex                                 *
  142.  *  This routine looks through all defined paths to find a file that  *
  143.  *                   will match the filename given.                   *
  144.  **********************************************************************
  145.  * Parameters:                                                        *
  146.  * - name of the manual file  (char *)                                *
  147. \**********************************************************************/
  148.  
  149. ex(t)
  150. char *t;
  151. {
  152. int g;
  153.     if (cnt==0) {
  154.         if (Found("man:",t)) {
  155.             strcpy(line,"man:");
  156.             strcat(line,t);
  157.             DisplayFile(line);
  158.             if (Status&FIRSTONLY) goto TheEnd;
  159.         }
  160.         for (g=0;g<pt;g++) {
  161.             if (Found(path[g],t)) {
  162.                 strcpy(line,path[g]);
  163.                 strcat(line,t);
  164.                 DisplayFile(line);
  165.                 if (Status&FIRSTONLY) g=pt;
  166.             }
  167.         }
  168.     }
  169.  
  170.     if (!fc) PrintF("No manuals available!\n");
  171.  
  172. TheEnd:
  173.     if (fh) fclose(fh);
  174.     exit();
  175. }
  176.  
  177. /**********************************************************************\
  178.  *                               Found                                *
  179.  *  This routine checks, if the file is accessable in the directory   *
  180.  *                        given as a parameter                        *
  181.  **********************************************************************
  182.  * Parameters:                                                        *
  183.  * - path which is to be searched  (char *)                           *
  184.  * - filename to look for (char *)                                    *
  185.  **********************************************************************
  186.  * Result:                                                            *
  187.  *  >1< if file is found                                              *
  188.  *  >0< file not found                                                *
  189. \**********************************************************************/
  190.  
  191. int Found(p,n)
  192. int *p,*n;
  193. {
  194. char f[256];
  195. struct FileLock *l,*Lock();
  196.     strcpy(f,p);
  197.     strcat(f,n);
  198.     l=Lock(f,ACCESS_READ);
  199.     if (l) UnLock(l),fc++;
  200.     if (l) return(1);
  201.     else return(0);
  202. }
  203.  
  204. /**********************************************************************\
  205.  *                            DisplayFile                             *
  206.  * This routine will call either the file viewer, or it will execute  *
  207.  *                       the command supplied.                        *
  208.  **********************************************************************
  209.  * Parameters:                                                        *
  210.  * - line  includes either the filename or the line to be executed    *
  211. \**********************************************************************/
  212.  
  213. DisplayFile(f)
  214. char *f;
  215. {
  216. int t;
  217. char exec[256];
  218.     t=0;
  219.     if (*f=='!')
  220.         Execute(&f[1],0L,0L);
  221.     else {
  222.         do {
  223.             t++;
  224.         } while ((f[t]!=' ')&&(f[t]!='\n'));
  225.         strcpy(exec,cmmnd);
  226.         strncat(exec,f,t);
  227.         PrintF("%s - ",f);
  228.         Execute(exec,0L,0L);
  229.     }
  230. }
  231.  
  232. /**********************************************************************\
  233.  *                            SearchStart                             *
  234.  *This routine scans the 'manuals'-file for the PATH and VIEW commands*
  235.  *             and tries to find the 'START_MAN'-command.             *
  236.  **********************************************************************
  237.  * Parameters:                                                        *
  238.  * - filepointer to the 'manuals'-file.                               *
  239. \**********************************************************************/
  240.  
  241. SearchStart(fh)
  242. struct FileHandle *fh;
  243. {
  244. char line[LINE],*c;
  245.     for (;;) {
  246.         if (c=fgets(line,LINE,fh)) {
  247.             if (strncmp(line,"START_MAN",9)==0) return;
  248.             if (strncmp(line,"VIEW:",5)==0) {
  249.                 if ((Status&CUSTOMVIEW)==0) {
  250.                     strcpy(cmmnd,&line[5]);
  251.                     cmmnd[strlen(cmmnd)-1]=' ';
  252.                 }
  253.             }
  254.             if (strncmp(line,"PATH ",5)==0) {
  255.                 if (pt<10) {
  256.                     strcpy(path[pt],&line[5]);
  257.                     path[pt][strlen(path[pt])-1]='\0';
  258.                     pt++;
  259.                 }
  260.                 else
  261.                     PrintF("Too many paths in 'man:manuals' - file!\n");
  262.             }
  263.         }
  264.     }
  265. }
  266.  
  267.