/*****************************************************************************/ /* cli.c Command-line wasDOC. VERSION HISTORY --------------- 21-FEB-2019 MGD initial */ /*****************************************************************************/ #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; char *cptr, *fptr, *sptr, *zptr; struct wasdoc_st *docptr; if (cptr = getenv ("WASDOC$DBUG")) if (!(dbug = atoi(cptr))) dbug = 1; CliOptions (argc, argv); if (!(cptr = CliDocDir)) cptr = getenv("PATH"); docptr = InitDoc (NULL, NULL, NULL, cptr); 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); 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 = fptr = docptr->output; *cptr; cptr++); while (cptr > sptr && *cptr != '/' && *cptr != ']' && *(cptr-1) != '^') cptr--; if (*cptr == '/' || *cptr == ']') fptr = ++cptr; /* buffer just the file name (sans directory, sans type) */ zptr = (sptr = docptr->oname) + sizeof(docptr->oname)-1; while (*cptr && *cptr != '.' && !MATCH2 (cptr-1, "^.") && sptr < zptr) *sptr++ = *cptr++; *sptr = '\0'; /* terminate any directory element */ if (fptr == docptr->output) strcpy (fptr, "[]"); 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 < 1) { /* 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 (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(cptr+6); else if (!strncasecmp (argv[cnt], "/dbug", 6)) dbug = 1; 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; } /*****************************************************************************/