home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_01 / ST_GUIDE.LZH / STG-UTIL / PC_CONV / PC_CONV.C next >
C/C++ Source or Header  |  1994-04-27  |  10KB  |  511 lines

  1. #line 1/*ACE 4 0344 */
  2.  
  3. extern char *suffix(), *strupr(), *strdup();
  4. extern void *malloc();
  5. typedef unsigned char uchar;
  6. typedef unsigned int uint;
  7.  
  8. extern long gemdos();
  9. #define  Cconws(s)          gemdos(9,s)
  10. #define  Dsetdrv(d)         gemdos(14,d)
  11. #define  Dsetpath(path)     gemdos(59,path)
  12. #define  Fcreate(fn,mode)   gemdos(60,fn,mode)
  13. #define  Fopen(fn,mode)     gemdos(61,fn,mode)
  14. #define  Fclose(h)          gemdos(62,h)
  15. #define  Fread(h,cnt,buf)   gemdos(63,h,cnt,buf)
  16. #define  Fwrite(h,cnt,buf)  gemdos(64,h,cnt,buf)
  17. #define  Fseek(where,h,how) gemdos(66,where,h,how)
  18.  
  19. #define NL  "\r\n"
  20.  
  21. int     inhandle;           // Handle der aktuellen Eingabedatei
  22. uchar   inbuf[8192];        // Dateibuffer fuer Eingabe
  23. uchar   *inptr;
  24. long    inlen;
  25. int     outhandle;          // Handle der Ausgabedatei
  26. uchar   outbuf[4096];       // Dateibuffer fuer Ausgabe
  27. uchar   *outptr;
  28. int     Autoref = 1;        // Autoreferenzer machen lassen
  29. int     NewFormat;          // 1: recompiliert durch HELPDISC
  30. uint    maxlines = 0;
  31. int     HasExternals;       // Anzahl externe Referenzen
  32. uint    Line;               // Zeilennummer der Ausgabe
  33. uint    total;              // Anzahl Ausgabezeilen
  34. int     ExtCnt;             // Zähler für Extensions
  35. long    Syms;               // Anzahl nodes/alabels
  36. char    outfile[128];       // Name der ursprünglichen Ausgabedatei
  37. char    *ExtPage;
  38.  
  39.  
  40. /*
  41.  * Fehler melden und terminieren
  42.  */
  43. Message(str1, str2)
  44. char *str1, *str2;
  45. {
  46.     Cconws(str1);
  47.     if (str2) Cconws(str2);
  48.     Cconws(NL);
  49. }
  50.  
  51.  
  52. error(s1, s2)
  53. char *s1, *s2;
  54. {
  55.     Cconws(NL);
  56.     Message(s1, s2);
  57.     if (outhandle>0) Fclose(outhandle);
  58.     if (inhandle>0) Fclose(inhandle);
  59.     _exit(1);
  60. }
  61.  
  62.  
  63. /*
  64.  * Ausgabedatei oeffnen
  65.  */
  66. Wopen(char *outfile)
  67. {
  68.     if ((outhandle=Fcreate(outfile, 0)) > 5)
  69.         outptr = outbuf;
  70.     else
  71.         error("can't open outfile", 0L);
  72.     Line = 0;
  73. }
  74.  
  75.  
  76. /*
  77.  * Datei zum Lesen oeffnen
  78.  */
  79. Ropen(char *file)
  80. {
  81.     if ((inhandle=Fopen(file, 0))<5) // Datei oeffnen
  82.         error("can't open ", file);
  83.  
  84.     fputs("@options -s");               // kein Zeilenumbruch
  85.     if (!Autoref) fputs("a");           // kein Autoreferenzer
  86.     if (NewFormat) fputs("n");          // "pageN" nicht in Index
  87.     fputs(NL NL);
  88.     fputs("@node Main" NL
  89.           " Dieser Text wurde automatisch in das ST-Guide Format" NL
  90.           " konvertiert. Auf dieser Seite sollte eigentlich ein" NL
  91.           " Inhaltsverzeichnis stehen..." NL
  92.           "@endnode" NL
  93.          );
  94.  
  95.     inlen = -1;
  96.     inptr = inbuf;                      // Lesezeiger init.
  97. }
  98.  
  99.  
  100. /*
  101.  * Datei schliessen, dabei evtl. Buffer flushen
  102.  */
  103. xclose(handle)
  104. int handle;
  105. {
  106.     long len;
  107.  
  108.     if (handle == outhandle) {
  109.         if (outptr != outbuf) {
  110.             len = (long)(outptr-outbuf);
  111.             if (Fwrite(handle, len, outbuf) != len)
  112.                 error("write error", 0L);
  113.         }
  114.     }
  115.  
  116.     Fclose(handle);
  117. }
  118.  
  119.  
  120. int fgets(line)
  121. register uchar *line;
  122. {
  123.     register uchar  *p = inptr;
  124.     int             rv = 1, cnt=0;
  125.  
  126.     do {
  127.         if (inlen <= 0L) {
  128.             inlen = Fread(inhandle, 8192L, inbuf);
  129.             Cconws(".");
  130.             if (inlen < 0L)                 // Fehler?
  131.                 error("read error", 0L);
  132.  
  133.             if (inlen == 0L) {              // nix gelesen?
  134.                 rv = 0;                     // DateiEnde
  135.                 break;
  136.             }
  137.             inptr = p = inbuf;
  138.         }
  139.         if (*p == 10) {             // NL
  140.             --inlen;
  141.             ++p;
  142.             break;
  143.         }
  144.  
  145.         if (*p != 13) {             // CR nicht kopieren
  146.             *line++ = *p;
  147.             ++cnt;
  148.         }
  149.  
  150.         --inlen;                    // einer weniger
  151.         ++p;
  152.     } while (cnt<255);
  153.  
  154.     *line = 0;                      // Zeile abschliessen
  155.     inptr = p;                      // Pos. naechstes Zeichen
  156.     return(rv||cnt);                // und fertig
  157. }
  158.  
  159.  
  160. fputs(line)
  161. register uchar  *line;
  162. {
  163.     register uchar  *p = outptr;
  164.  
  165.     while (*line) {
  166.         if (*line == '\n') ++Line;
  167.         if ( (long)(p-outbuf) == 4096L) {
  168.             if (Fwrite(outhandle, 4096L, outbuf) != 4096L)
  169.                 error("Write error", 0L);
  170.             p = outptr = outbuf;
  171.         }
  172.         *p++ = *line++;
  173.     }
  174.     outptr = p;
  175. }
  176.  
  177.  
  178. KonvKey(buf)
  179. register uchar  *buf;
  180. {
  181.     if (strcmp(buf, "Copyright") == 0)
  182.         strcpy(buf, "Help");
  183.     else if (strcmp(buf, "Help") == 0)
  184.         strcpy(buf, "Help 2");
  185.     else if (strcmp(buf, "Main") == 0)
  186.         strcpy(buf, "main 2");
  187. }
  188.  
  189.  
  190. uchar *GetName(src, buf)
  191. register uchar  *src;
  192. uchar           *buf;
  193. {
  194.     register uchar  *dst = buf;
  195.  
  196.     while (*src && *src != '"') ++src;          // Anfang <node>
  197.     ++src;
  198.     do {
  199.         *dst++ = *src++;
  200.         if (dst[-1] == '\\')                    // maskiertes Zeichen
  201.             *dst++ = *src++;
  202.     } while (*src && *src != '"');              // Ende <node>
  203.     *dst = 0;
  204.     KonvKey(buf);
  205.     return(++src);
  206. }
  207.  
  208.  
  209. NewPage(uchar *line)
  210. {
  211.     uchar           buf[255];
  212.     register uchar  *src = line;
  213.     int             cnt;
  214.  
  215.     src = GetName(src+7, buf);
  216.  
  217.     if (!NewFormat && strcmp(buf, "Index") == 0) {      // dieser Index ist
  218.         while (fgets(line)) {                           // Blödsinn
  219.             if (strncmp(line, "\\end", 4) == 0) break;
  220.         }
  221.         return;
  222.     }
  223.     fputs("@node \"");
  224.     fputs(buf);
  225.     fputs("\"");
  226.     if (NewFormat)
  227.         cnt=0;
  228.     else {
  229.         fputs(NL);
  230.         cnt=1;
  231.     }
  232.  
  233. again:
  234.     ++Syms;
  235.     while (*src) {
  236.         if (*src == ',') {
  237.             if (NewFormat==0) {
  238.                 fgets(line);
  239.                 src = line;
  240.             }
  241.             src = GetName(src, buf);
  242.             if (cnt++ == 0) {
  243.                 fputs(" \"");
  244.                 fputs(buf);
  245.                 fputs("\"" NL);
  246.             }
  247.             fputs("@symbol \"");
  248.             fputs(buf);
  249.             fputs("\"" NL);
  250.             goto again;
  251.         }
  252.         ++src;
  253.     }
  254.     if (cnt==0) fputs(NL);
  255. }
  256.  
  257.  
  258. uchar *MakeLink(dst, node, key)
  259. register uchar  *dst;
  260. uchar           *node, *key;
  261. {
  262.     strcpy(dst, "@{\"");
  263.     strcat(dst, node);
  264.     strcat(dst, "\" link \"");
  265.     strcat(dst, key);
  266.     strcat(dst, "\"}");
  267.     while (*dst++);
  268.     return(--dst);
  269. }
  270.  
  271.  
  272. EndNode()
  273. {
  274.     char buf[128];
  275.  
  276.     fputs("@endnode" NL NL);
  277.     if (maxlines && Line>maxlines) {
  278.         strcpy(buf, outfile);
  279.         itoa(ExtCnt++, suffix(buf)+1, 10);
  280.         xclose(outhandle);
  281.         total += Line;
  282.         Wopen(buf);
  283.         Cconws(NL);
  284.         Cconws(" --> ");
  285.         Cconws(buf);
  286.         Cconws(NL);
  287.     }
  288. }
  289.  
  290.  
  291. ConvLine(uchar *buf)
  292. {
  293.     uchar           line[255], alias[80];
  294.     register uchar  *src = buf, *dst = line;
  295.     uchar           *key;
  296.     int             EndFlag=0;
  297.  
  298.     while (*src) {
  299.         if (*src == '\\') {
  300.             if (src[1] == '\\') {
  301.                 *dst++ = *src++;
  302.                 goto copy;
  303.             }
  304.             if (src[1] == '#') {
  305.                 src += 2;
  306.                 key = src;
  307.                 while (*src) {
  308.                     if (*src == '\\' && src[1] == '#') break;
  309.                     ++src;
  310.                 }
  311.                 if (*src == 0) {
  312.                     Message(NL "-> ", buf);
  313.                     error("syntax error.", 0L);
  314.                 }
  315.                 *src = 0;
  316.                 strcpy(alias, key);
  317.                 KonvKey(alias);
  318.                 if (strcmp(key, alias) == 0 && Autoref) {
  319.                     /*
  320.                      * das Link lassen wir vom Autoreferenzer
  321.                      * machen
  322.                      */
  323.                     strcpy(dst, alias);
  324.                     while (*dst++);
  325.                     --dst;
  326.                 }
  327.                 else {
  328.                     dst = MakeLink(dst, key, alias);
  329.                 }
  330.                 *src = '\\';
  331.                 src += 2;
  332.             }
  333.             else if (strncmp(src+1, "link", 4) == 0) {
  334.                 /*
  335.                  * \link ("<node>")<text>\#
  336.                  */
  337.                 src = GetName(src+4, alias);    // <node> holen
  338.                 while (*src++ != ')');          // Anfang <text>
  339.                 key = src;                      // merken
  340.                 while (*src) {
  341.                     if (*src == '\\' && src[1] == '#') {
  342.                         *src = 0;
  343.                         if (strcmp(alias, ExtPage) == 0)
  344.                             ++HasExternals;
  345.                         dst = MakeLink(dst, key, alias);
  346.                         *src = '\\';
  347.                         src += 2;
  348.                         break;
  349.                     }
  350.                     ++src;
  351.                 }
  352.             }
  353.             else if (strncmp(src+1, "end", 3) == 0) {
  354.                 if (NewFormat)
  355.                     EndFlag=1;
  356.                 else
  357.                     EndNode();
  358.                 goto ende;
  359.             }
  360.             else
  361.                 *dst++ = *src++;        // Backslash kopieren
  362.         }
  363.         else if (*src == '@') {         // schon maskieren
  364.             *dst++ = '@';
  365.             goto copy;
  366.         }
  367.         else
  368. copy:
  369.             *dst++ = *src++;
  370.     }
  371. ende:
  372.     *dst++ = 13;
  373.     *dst++ = 10;
  374.     *dst = 0;
  375.     fputs(line);
  376.     if (EndFlag) EndNode();
  377. }
  378.  
  379.  
  380. AddIncludes(file, cnt)
  381. char    *file;
  382. int     cnt;
  383. {
  384.     char    buf[128];
  385.     int     i;
  386.  
  387.     strcpy(buf, file);
  388.     strcpy(suffix(buf), ".STG");
  389.     outhandle = Fopen(buf, 1);
  390.     outptr = outbuf;
  391.     Fseek(0L, outhandle, 2);
  392.  
  393.     for (i=0; i<cnt; i++) {
  394.         strcpy(buf, file);
  395.         itoa(i, suffix(buf)+1, 10);
  396.         fputs(NL "@include ");
  397.         fputs(buf);
  398.     }
  399.     fputs(NL);
  400.     xclose(outhandle);
  401. }
  402.  
  403.  
  404. extern int  _argc;
  405. extern char **_argv;
  406. _main()
  407. {
  408.     main(_argc, _argv);
  409. }
  410.  
  411.  
  412. main(argc, argv)
  413. int argc;
  414. char **argv;
  415. {
  416.     char    buf[255], *p;
  417.     int     val;
  418.  
  419.     Message("PC-Conv V(" __DATE__ "): PureC-Help --> ST-Guide sources" NL
  420.             "        Written by Holger Weets using SOZOBON-C V2.00x10" NL
  421.             , 0L);
  422.  
  423.     if (argc < 2) {
  424. error:
  425.         Cconws("usage: PC-Conv [+-anmN] file1 [file2 ...]" NL
  426.                "       a: explicit (-) or automatic (+) links" NL
  427.                "       n: HELP_RC (-) or new HELPDISC (+) format" NL
  428.                "       mN: max N lines per output file" NL
  429.                "       <fileN> must be ASCII" NL
  430.               );
  431.         _exit(1);
  432.     }
  433.  
  434.     /*
  435.      * Ausgabedatei bestimmen
  436.      */
  437.     do {
  438.         ++argv;
  439.         strupr(p = *argv);                  // falls aus CLI
  440.         if (*p == '-' || *p == '+') {
  441.             do {
  442.                 switch (*p) {
  443.                     case '+':   val = 1;                break;
  444.                     case '-':   val = 0;                break;
  445.                     case 'A':   Autoref = val;          break;
  446.                     case 'M':   maxlines = atoi(p+1);   goto next;
  447.                     case 'N':   NewFormat = val;        break;
  448.                     default:    goto error;
  449.                 }
  450.                 ++p;
  451.             } while (*p);
  452. next:
  453.         }
  454.         else {
  455.             strcpy(buf, p);
  456.             strcpy(suffix(buf), ".STG");
  457.  
  458.             Cconws(NL);
  459.             Cconws(p);
  460.             Cconws(" --> ");
  461.             Cconws(buf);
  462.             Cconws(NL);
  463.  
  464.             Wopen(buf);
  465.             strcpy(outfile, buf);
  466.             if (NewFormat)
  467.                 ExtPage = "page-1";
  468.             else
  469.                 ExtPage = "%%GLOBAL%%";
  470.             ExtCnt=0;
  471.             total = 0;
  472.             Syms=0;
  473.             HasExternals = 0;
  474.             Ropen(*argv);
  475.             while (fgets(buf)) {
  476.                 if (strncmp(buf, "screen", 6) == 0)
  477.                     NewPage(buf);
  478.                 else
  479.                     ConvLine(buf);
  480.             }
  481.             ltoa(Syms, buf, 10);
  482.             Message(NL "total symbols      : ", buf);
  483.             if (HasExternals) {
  484.                 itoa(HasExternals, buf, 10);
  485.                 Message("external references: ", buf);
  486.                 fputs("@node \"");
  487.                 fputs(ExtPage);
  488.                 fputs("\"" NL
  489.                       " Referenz zu externer Datei." NL
  490.                       " Bitte von Hand vervollständigen." NL
  491.                       "@endnode" NL NL
  492.                      );
  493.             }
  494.             total += Line;
  495.             itoa(total, buf, 10);
  496.             Message("total lines        : ", buf);
  497.             xclose(inhandle);
  498.             xclose(outhandle);
  499.             if (ExtCnt) {
  500.                 AddIncludes(*argv, ExtCnt);
  501.             }
  502.         }
  503.     } while (--argc>1);
  504.     _exit(0);
  505. }
  506.  
  507.  
  508.  
  509.  
  510.  
  511.