Annotation of 43BSD/contrib/spms/src/bin/phelp/Phelp.c, revision 1.1.1.1

1.1       root        1: static char *rcsid = "$Header$";
                      2: /*
                      3:  * phelp - on-line help for a project
                      4:  *
                      5:  * Author: Peter J. Nicklin
                      6:  */
                      7: #include <sys/types.h>
                      8: #include <sys/dir.h>
                      9: #include <stdio.h>
                     10: #include "getarg.h"
                     11: #include "macro.h"
                     12: #include "null.h"
                     13: #include "path.h"
                     14: #include "phelp.h"
                     15: #include "spms.h"
                     16: #include "yesno.h"
                     17: 
                     18: char *PGN = "phelp";                   /* program name */
                     19: char PHELP_CMD[PATHSIZE];              /* help command file pathname */
                     20: char PHELP_HELP[PATHSIZE];             /* help introduction file pathname */
                     21: int INTERACTIVE;                       /* phelp interactive? */
                     22: int HELPLEVEL;                         /* help hierarchy level */
                     23: 
                     24: main(argc, argv)
                     25:        int argc;
                     26:        char **argv;
                     27: {
                     28:        extern int PPDEBUG;             /* project pathname debug flag */
                     29:        char *getcwp();                 /* get current working project */
                     30:        char *pathcat();                /* pathname concatenation */
                     31:        char *ppathname = CURPROJECT;   /* project pathname */
                     32:        int isintract();                /* find out if interactive */
                     33:        int printhelp();                /* print a help file */
                     34:        int printtopic();               /* print topic file and index */
                     35:        int processtopic();             /* process help topic on stack */
                     36:        int status = 0;                 /* exit status */
                     37:        void gettopic();                /* get next help topic from stdin */
                     38:        void printnotopics();           /* print "no topics" error message */
                     39:        void prompt();                  /* prompt for a topic */
                     40:        void puttopic();                /* add help topic to request queue */
                     41: 
                     42:        {
                     43:        register char *s;               /* option pointer */
                     44:        while (--argc > 0 && **++argv == '-')
                     45:                {
                     46:                for (s = argv[0]+1; *s != '\0'; s++)
                     47:                        switch (*s)
                     48:                                {
                     49:                                case 'D':
                     50:                                        PPDEBUG = YES;
                     51:                                        break;
                     52:                                case 'P':
                     53:                                        ppathname = GETARG(s);
                     54:                                        if (*ppathname == '\0')
                     55:                                                {
                     56:                                                warn("missing project name");
                     57:                                                status = 1;
                     58:                                                }
                     59:                                        goto endfor;
                     60:                                default:
                     61:                                        warn("bad option -%c", *s);
                     62:                                        status = 1;
                     63:                                        goto endfor;
                     64:                                }
                     65:                endfor: continue;
                     66:                }
                     67:        }
                     68:        if (status == 1)
                     69:                fatal("usage: phelp [-P projectname] [topic [subtopic ...]]");
                     70: 
                     71:        if (getcwp() == NULL)
                     72:                fatal("There is no project environment");
                     73: 
                     74:        if (chghelp(ppathname) == NO)
                     75:                exit(1);
                     76: 
                     77:        pathcat(PHELP_CMD, SPMSLIB, PHELP_CMD_FILE);
                     78:        pathcat(PHELP_HELP, SPMSLIB, PHELP_HELP_FILE);
                     79: 
                     80:        INTERACTIVE = isintract();
                     81:        
                     82:        if (argc == 0 || (argc == 1 && PHELP))
                     83:                {                       /* general introduction */
                     84:                if (printtopic(PHELP_HELP, CURDIR) > 1)
                     85:                        {
                     86:                        printnotopics(ppathname);
                     87:                        exit(1);
                     88:                        }
                     89:                }
                     90:        else    {                       /* command line arguments */
                     91:                while (argc-- > 0)
                     92:                        puttopic(*argv++);
                     93:                status = processtopic();
                     94:                }
                     95:        if (INTERACTIVE == YES)         /* further help topics */
                     96:                while (HELPLEVEL >= 0)
                     97:                        {
                     98:                        prompt();
                     99:                        gettopic();
                    100:                        status = processtopic();
                    101:                        }
                    102:        
                    103:        exit(status);
                    104: }
                    105: 
                    106: 
                    107: 
                    108: /*
                    109:  * chghelp() changes to another help hierarchy. Returns YES if
                    110:  * successful, otherwise NO.
                    111:  */
                    112: chghelp(ppathname)
                    113:        char *ppathname;                /* project pathname */
                    114: {
                    115:        char helpath[PATHSIZE];         /* help directory pathname */
                    116:        char *oldcwp;                   /* old current working project */
                    117:        char *pathcat();                /* pathname concatenation */
                    118:        char *strsav();                 /* save a string somewhere */
                    119:        int chproject();                /* change project */
                    120:        void printnotopics();           /* print "no topics" error message */
                    121: 
                    122:        oldcwp = strsav(getcwp());
                    123:        if (chproject(ppathname) == NO)
                    124:                {
                    125:                free(oldcwp);
                    126:                return(NO);
                    127:                }
                    128:        pathcat(helpath, getcwp(), "help");
                    129:        if (!CHDIR(helpath))
                    130:                {
                    131:                printnotopics(ppathname);
                    132:                return(NO);
                    133:                }
                    134:        return(YES);
                    135: }
                    136: 
                    137: 
                    138: 
                    139: /*
                    140:  * mkndir() makes a directory name by appending ".d" to a topic name.
                    141:  */
                    142: char *
                    143: mkndir(basename)
                    144:        char *basename;                 /* topic name */
                    145: {
                    146:        static char dirnam[MAXNAMLEN];  /* directory name buffer */
                    147:        char *strcpy();                 /* string copy */
                    148:        char *strcat();                 /* string concatenation */
                    149: 
                    150:        strcpy(dirnam, basename);
                    151:        strcat(dirnam, ".d");
                    152:        return(dirnam);
                    153: }
                    154: 
                    155: 
                    156: 
                    157: /*
                    158:  * printhelp() prints a help file on output stream ofp. Returns YES if
                    159:  * file can be opened and read, otherwise NO.
                    160:  */
                    161: printhelp(helpfile, ofp)
                    162:        char *helpfile;                 /* file containing help information */
                    163:        register FILE *ofp;             /* output stream */
                    164: {
                    165:        register FILE *ifp;             /* input file stream */
                    166:        register int c;                 /* current character */
                    167:        FILE *fopen();                  /* open file */
                    168: 
                    169:        if ((ifp = fopen(helpfile, "r")) == NULL)
                    170:                return(NO);
                    171:        if ((c = getc(ifp)) != EOF)
                    172:                {
                    173:                putc(c, ofp);
                    174:                while ((c = getc(ifp)) != EOF)
                    175:                        putc(c, ofp);
                    176:                fclose(ifp);
                    177:                return(YES);
                    178:                }
                    179:        else    {
                    180:                fclose(ifp);
                    181:                return(NO);
                    182:                }
                    183: }
                    184: 
                    185: 
                    186: 
                    187: /* printtopic() prints a help topic plus a list of available subtopics. The
                    188:  * output is piped through more(1) if phelp is interactive. Returns 0 if
                    189:  * topic and subtopics printed; 1 if subtopics printed only; 2 if file printed
                    190:  * only; 3 if nothing printed.
                    191:  */
                    192: printtopic(topic, subtopicdir)
                    193:        char *topic;                    /* name of topic file to be printed */
                    194:        char *subtopicdir;              /* name of subtopic directory */
                    195: {
                    196:        FILE *popen();                  /* open pipe for writing */
                    197:        FILE *ofp;                      /* output stream */
                    198:        int mkindex();                  /* make topic index */
                    199:        int printhelp();                /* print help file */
                    200:        int status = 0;                 /* printhelp status */
                    201:        void printindex();              /* print topic index */
                    202: 
                    203:        if (INTERACTIVE == NO || (ofp = popen("more -d", "w")) == NULL)
                    204:                ofp = stdout;
                    205:        if (printhelp(topic, ofp) == NO)
                    206:                status = 1;
                    207:        if (mkindex(subtopicdir) == YES)
                    208:                printindex(ofp);
                    209:        else
                    210:                status += 2;
                    211:        if (ofp != stdout)
                    212:                pclose(ofp);
                    213:        return(status);
                    214: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.