/*****************************************************************************/ /* cli.c Command-line wasDOC. VERSION HISTORY --------------- 21-FEB-2019 MGD initial */ /*****************************************************************************/ #include #include "wasdoc.h" int CliChunk, CliDoOutput, CliInsight; char *CliDocDir, *CliMain, *CliOutput; extern int dbug, isCgi, isCgiPlus; extern char CopyrightDate [], DefaultType [], SoftwareID [], Utility []; /*****************************************************************************/ /* Command-line usage. */ void CliDoc (int argc, char *argv[]) { int chunk, cnt, error, retval; char *cptr, *dptr, *sptr, *zptr; struct wasdoc_st *docptr; if (cptr = getenv ("WASDOC$DBUG")) if (!(dbug = atoi(cptr))) dbug = 1; CliOptions (argc, argv); if (!CliDocDir) CliDocDir = getenv("PATH"); if (!CliDocDir) CliDocDir = "[]"; if (retval = CliCheckDir (CliDocDir)) exit (retval); docptr = InitDoc (NULL, NULL, NULL, CliDocDir); docptr->isStatic = 1; docptr->chunked = CliChunk; if (CliInsight < 0) /* if negative then force insights to document */ docptr->insight = docptr->insight2doc = -CliInsight; else docptr->insight = CliInsight; wasDocStatistics (docptr, 1); if (CliOutput) { zptr = (sptr = docptr->output) + sizeof(docptr->output)-1; for (cptr = CliOutput; *cptr && sptr < zptr; *sptr++ = *cptr++); *sptr = '\0'; /* parse out the file from the directory elements */ for (cptr = sptr = dptr = docptr->output; *cptr; cptr++); while (cptr > sptr && *cptr != '/' && *cptr != ']' && *(cptr-1) != '^') cptr--; if (*cptr == '/' || *cptr == ']') dptr = ++cptr; /* buffer just the file name (sans directory, sans any type) */ zptr = (sptr = docptr->oname) + sizeof(docptr->oname)-1; while (*cptr && *cptr != '.' && !MATCH2 (cptr-1, "^.") && sptr < zptr) *sptr++ = *cptr++; *sptr = '\0'; /* now just terminate ->output at the end of the directory element */ *dptr = '\0'; if (retval = CliCheckDir (CliDocDir)) exit (retval); } if (docptr->insight) { fprintf (stdout, "%%WASDOC-I-INSIGHT, %d\n", docptr->insight); for (cnt = 0; cnt < argc; cnt++) fprintf (stdout, "%s ", argv[cnt]); fprintf (stdout, "\n%s %s\n", docptr->dname, docptr->oname); } error = ProcessDirectory (docptr); if (error) exit (vaxc$errno); if (!docptr->tlength) exit (SS$_ABORT); if (dbug>1) fputs (docptr->text, stdout); if (error = renderParse (docptr)) { if (CliDoOutput) if (docptr->hlength) wasDocWrite (docptr, docptr->html, docptr->hlength); fprintf (stdout, "%%%s-E-RENDER, %s\n", Utility, strerror(error)); exit (SS$_ABORT | STS$M_INHIB_MSG); } if (!docptr->hlength) exit (SS$_ABORT); wasDocStatistics (docptr, 0); // wasDocHtmlPrint (docptr, "\n", docptr->statistics); if (docptr->chunked) { for (chunk = 0; chunk <= docptr->chunkTotal; chunk++) { docptr->pchunk = chunk; error = wasDocChunkOut (docptr); if (error) break; } if (CliMain && *(cptr = CliMain)) { zptr = (sptr = docptr->oname) + sizeof(docptr->oname)-1; while (*cptr && *cptr != '.' && !MATCH2 (cptr-1, "^.") && sptr < zptr) *sptr++ = *cptr++; *sptr = '\0'; /* get the zeroeth chunk (again) */ docptr->pchunk = -1; error = wasDocChunkOut (docptr); } } else error = wasDocWrite (docptr, docptr->html, docptr->hlength); exit (error ? SS$_ABORT : 1); } /*****************************************************************************/ /* */ int CliWriteFile ( struct wasdoc_st *docptr, char *data, int length ) { static char pname [256]; static FILE *ofptr; int retval = 0; char *cptr, *sptr, *zptr; char fname [sizeof(pname)]; if (length < 0) { /* close the file */ fclose (ofptr); pname[0] = '\0'; return (0); } zptr = (sptr = fname) + sizeof(fname)-16; for (cptr = docptr->output; *cptr && sptr < zptr; *sptr++ = *cptr++); for (cptr = docptr->oname; *cptr && sptr < zptr; *sptr++ = *cptr++); if (docptr->chunked && docptr->pchunk >= 0) sprintf (sptr, "%03.3d.html", docptr->pchunk); else sprintf (sptr, ".html"); if (dbug>1) dbugThis (FI_LI, "CliWriteFile() |%s|%s|%s|\n", docptr->oname, fname, pname); if (pname[0] && strcmp (fname, pname)) { fclose (ofptr); pname[0] = '\0'; } if (!pname[0]) { if ((ofptr = fopen (fname, "w")) == NULL) { retval = vaxc$errno; fprintf (stdout, "%%%s-E-OUTPUT, %s\n", Utility, fname); exit (retval); } strcpy (pname, fname); } if (length > 0) retval = fwrite (data, length, 1, ofptr); else retval = fflush (ofptr); return (retval ? 0 : vaxc$errno); } /*****************************************************************************/ /* CLI a bit VMS-like. */ void CliOptions (int argc, char *argv[]) { int cnt, nomain = 0; char *cptr; for (cnt = 1; cnt < argc; cnt++) { if (!strncasecmp (argv[cnt], "/chunked", 6)) CliChunk = 1; else if (!strncasecmp (argv[cnt], "/dbug=", 6)) dbug = atoi(argv[cnt]+6); else if (!strncasecmp (argv[cnt], "/dbug", 5)) dbug = 99; else if (!strncasecmp (argv[cnt], "/main", 4)) { for (cptr = argv[cnt]; *cptr && *cptr != '='; cptr++); if (*cptr) CliMain = cptr + 1; } else if (!strncasecmp (argv[cnt], "/nomain", 6)) nomain = 1; else if (!strncasecmp (argv[cnt], "/output=", 4)) { for (cptr = argv[cnt]; *cptr && *cptr != '='; cptr++); if (*cptr) CliOutput = cptr + 1; else CliDoOutput = 1; } else if (!strncasecmp (argv[cnt], "/insight", 4)) { for (cptr = argv[cnt]; *cptr && *cptr != '='; cptr++); if (*cptr) CliInsight = atoi(cptr+1); else CliInsight = -1; } else if (!strncasecmp (argv[cnt], "/version", 4)) { fprintf (stdout, "%%%s-I-VERSION, %s\n", Utility, SoftwareID); exit (1); } else if (*argv[cnt] == '/') { fprintf (stdout, "%%%s-E-IVQUAL, unrecognized qualifier\n \\%s\\\n", Utility, argv[cnt]); exit(0x10000002); } else CliDocDir = argv[cnt]; } if (nomain) CliMain = NULL; else if (!CliMain) CliMain = CliOutput; } /*****************************************************************************/ /* Return 0 if a dir, ENOTDIR if not a dir, or some other errno. */ int CliCheckDir (char *path) { struct stat st; if (stat (path, &st) == 0) if (!(st.st_mode & S_IFDIR)) return (ENOTDIR); return (errno); } /*****************************************************************************/