home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / unix / volume03 / la50 < prev    next >
Encoding:
Internet Message Format  |  1988-09-11  |  7.8 KB

  1. From: genrad!decvax!minow (Martin Minow)
  2. Subject: hack to convert nroff underlines to LA50 esc. seq's
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 3, Issue 76
  7. Submitted by: genrad!decvax!minow (Martin Minow)
  8.  
  9.  
  10.  
  11. # This is a shell archive.  Remove anything before this line, then
  12. # unpack it by saving it in a file and typing "sh file".  (Files
  13. # unpacked will be owned by you and have default permissions.)
  14. #
  15. # This archive contains:
  16. # readme.txt ulfix.c Makefile
  17.  
  18. echo x - readme.txt
  19. cat > "readme.txt" << '//E*O*F readme.txt//'
  20. ulfix is a public-domain program that converts nroff-style underlines
  21. and superscripts to the escape sequences understood by Dec LA50
  22. and VT200 series terminals.  It is a quick hack and shouldn't
  23. be taken seriously.
  24.  
  25. Martin Minow
  26. decvax!minow
  27. //E*O*F readme.txt//
  28.  
  29. echo x - ulfix.c
  30. cat > "ulfix.c" << '//E*O*F ulfix.c//'
  31. /*
  32.  * Copy stdin to stdout, converting sequences of _<BS>L... to
  33.  * an escape sequence appropriate for display on a VTxx terminal
  34.  * or LAxx printer.  Also hack nroff superscripts.
  35.  */
  36.  
  37. /*)BUILD
  38.  */
  39.  
  40. #ifdef DOCUMENTATION
  41.  
  42. Title    ulfix    Fix Overstrike Underlining
  43. Index    ulfix    Fix overstrike underlining
  44.  
  45. Usage
  46.  
  47.     ulfix [-e] [-s] [-n] [input [output]]
  48.  
  49. Description
  50.  
  51.     Copy input to output, replacing overstrike underlining
  52.     (such as generated by the Unix nroff formatter) by escape
  53.     sequences appropriate for VT100 (family) terminals
  54.     or LA50 printers.
  55.  
  56.     The -e option generates escape sequences in their
  57.     eight-bit form.  This is the default on P/OS.
  58.  
  59.     The -s option generates escape sequences in the
  60.     seven-bit form.  This may not work on P/OS.
  61.  
  62.     The -n option supresses conversion of superscripts to their
  63.     their Multinational form.  This is needed for display on
  64.     terminals, such as the VT100, which support underscore
  65.     attributes but not Multinational.
  66.  
  67.     Note: the program assumes that Dec Multinational is mapped
  68.     into the right-half of the 8-bit code matrix (this is normal
  69.     for VT200 family terminals).
  70.  
  71.     Missing arguments are replaced by stdin and stdout. An argument
  72.     of '-' may be used for explicit specification of stdin or stdout:
  73.  
  74.         ulfix - foo.out
  75.  
  76.     reads stdin, writing all output to foo.out.
  77.  
  78.     The reverse linefeed and superscript escape sequences generated
  79.     by nroff (<ESC>7, <ESC>8 and <ESC>9) are generally removed.
  80.     The sequence <ESC>8 followed by a sequence of digits from
  81.     the set "123oa." followed by <ESC>9 are replaced by appropriate
  82.     superscripted characters in the Dec Multinational (Latin 1) set.
  83.  
  84. Author
  85.  
  86.     Martin Minow
  87.  
  88. Bugs
  89.  
  90.     Ulfix should be more intelligent about escape sequences.
  91.     Note that it somewhat duplicates functionality in the Unix col,
  92.     colcrt, and more programs.
  93.  
  94. #endif
  95.  
  96. #include    <stdio.h>
  97. #ifdef vms
  98. #include    <ssdef.h>
  99. #include    <stsdef.h>
  100. #include    <errno.h>
  101. #define    IO_SUCCESS    SS$_NORMAL
  102. #define    IO_ERROR    errno
  103. #endif
  104. #ifndef    IO_SUCCESS
  105. #define    IO_SUCCESS    0
  106. #define    IO_ERROR    1
  107. #endif
  108. #define    FALSE    0
  109. #define    TRUE    1
  110. #ifndef    EOS
  111. #define    EOS    '\0'
  112. #endif
  113. #define    ESC    '\033'
  114. #ifdef decus
  115. extern int    $$pos;            /* TRUE on P/OS            */
  116. #endif
  117.  
  118. char        line[513];
  119. char        work[513];
  120. int        eightbit = 0;        /* Assume seven bit        */
  121. int        nosuper = FALSE;
  122. /*
  123.  * super[] is a list of characters that have Multinational superscripts
  124.  * and replace[] is the equivalent Multinational character.
  125.  */
  126. char        super[] =   {  '1',  '2',  '3',  'o',  'a',  '.',  EOS    };
  127. char        replace[] = { 0x39, 0x32, 0x33, 0x3A, 0x2A, 0x37    };
  128.  
  129. char        *csi[] = {
  130.     "\033[", "\233"
  131. };
  132.  
  133. extern char    *strchr();
  134.  
  135. main(argc, argv)
  136. int        argc;
  137. char        *argv[];
  138. {
  139.     register char    *ap;
  140.  
  141. #ifdef vms
  142.     argc = getredirection(argc, argv);
  143. #endif
  144. #ifdef decus
  145.     if ($$pos)
  146.         eightbit = 1;
  147. #endif
  148.     while (argc > 1
  149.         && argv[1][0] == '-'
  150.         && argv[1][1] != EOS) {
  151.         for (ap = &argv[1][1]; *ap != EOS; ap++) {
  152.         switch (*ap) {
  153.         case 'e':
  154.         case 'E':
  155.             eightbit = 1;
  156.             break;
  157.  
  158.         case 'n':
  159.         case 'N':
  160.             nosuper = TRUE;
  161.             break;
  162.  
  163.         case 's':
  164.         case 'S':
  165.             eightbit = 0;
  166.             break;
  167.  
  168.         default:
  169.             fprintf(stderr, "Unknown option '%c' ignored.\n", *ap);
  170.             break;
  171.         }
  172.         }
  173.         argc--;
  174.         argv++;
  175.     }
  176.     if (argc > 1) {
  177.         if (strcmp(argv[1], "-") != 0) {
  178.         if (freopen(argv[1], "r", stdin) == NULL) {
  179.             perror(argv[1]);
  180.             exit(IO_ERROR);
  181.         }
  182.         }
  183.         argc--;
  184.         argv++;
  185.     }
  186.     if (argc > 1) {
  187.         if (strcmp(argv[1], "-") != 0) {
  188. #ifdef vms
  189.         if (freopen(argv[1], "w", stdout,
  190.             "rfm=var", "rat=cr") == NULL) {
  191. #else
  192.         if (freopen(argv[1], "w", stdout) == NULL) {
  193. #endif
  194.             perror(argv[1]);
  195.             exit(IO_ERROR);
  196.         }
  197.         }
  198.         argc--;
  199.         argv++;
  200.     }
  201.     while (fgets(line, sizeof line, stdin) != NULL) {
  202.         if (!nosuper)
  203.         mapsuperscript();
  204.         eatsuperscript();
  205.         eatoverstrike();
  206.         fputs(line, stdout);
  207.     }
  208.     fclose(stdin);
  209.     fclose(stdout);
  210. }
  211.  
  212. mapsuperscript()
  213. /*
  214.  * Convert <ESC>81<ESC>9 to Multinational superscript
  215.  */
  216. {
  217.     register char    *lp;
  218.     register char    *ep;
  219.     register char    *wp;
  220.     char        *xp;
  221.     int        i;        /* Workaround Decus C bug    */
  222.  
  223.     for (lp = line; (lp = strchr(lp, ESC)) != NULL;) {
  224.         if (lp[1] != '8')
  225.         lp++;
  226.         else {
  227.         wp = work;
  228.         for (ep = lp + 2; *ep != EOS && *ep != ESC; ep++) {
  229.             /*
  230.              * Check for underscore in superscript or
  231.              * one of the superscripted Multinationals.
  232.              * If it's something else, drop back to
  233.              * ASCII_G for want of any better ideas.
  234.              */
  235.             if (*ep == '_' && ep[1] == '\b') {
  236.             *wp++ = *ep++;        /* Catch you later    */
  237.             *wp++ = *ep;        /* You too        */
  238.             }
  239.             else if ((xp = strchr(super, *ep)) != NULL) {
  240.             i = xp - &super[0];    /* Don't optimize this    */
  241.             i = replace[i];        /* -- Decus C bug --    */
  242.             if (eightbit)
  243.                 *wp++ = i | 0x80;
  244.             else {
  245.                 *wp++ = ESC;    /* SS2, shift to Multi    */
  246.                 *wp++ = 'N';    /* for one character;    */
  247.                 *wp++ = i;        /* and here it is.    */
  248.             }
  249.             }
  250.             else {            /* Garbage        */
  251.             *wp++ = *ep;
  252.             }
  253.         }
  254.         if (*ep == ESC && ep[1] == '9')
  255.             ep += 2;
  256.         strcpy(wp, ep);
  257.         strcpy(lp, work);
  258.         lp += (wp - work);
  259.         }
  260.     }
  261. }
  262.  
  263. eatsuperscript()
  264. /*
  265.  * Eat superscript escape sequences
  266.  */
  267. {
  268.     register char    *lp;
  269.  
  270.     for (lp = line; (lp = strchr(lp, ESC)) != NULL;) {
  271.         switch (lp[1]) {
  272.         case '7':
  273.         case '8':
  274.         case '9':
  275.         strcpy(lp, lp + 2);    /* Eat this        */
  276.         break;
  277.  
  278.         default:            /* <ESC>N or stranger    */
  279.         lp++;            /* Skip over it        */
  280.         break;
  281.         }
  282.     }
  283. }
  284.  
  285. eatoverstrike()
  286. {
  287.     register char    *lp;
  288.     register char    *ep;
  289.     register char    *wp;
  290.  
  291.     for (lp = line; (lp = strchr(lp, '_')) != NULL;) {
  292.         if (lp[1] == '\b' && lp[2] != '\0') {
  293.         wp = work;
  294.         ep = lp + 2;
  295.         if (*ep == ESC && ep[1] == 'N') {
  296.             *wp++ = *ep++;
  297.             *wp++ = *ep++;
  298.         }
  299.         if (*ep != EOS)
  300.             *wp++ = *ep++;
  301.         while (ep[0] == '_' && ep[1] == '\b') {
  302.             ep += 2;
  303.             if (*ep == ESC && ep[1] == 'N') {
  304.             *wp++ = *ep++;
  305.             *wp++ = *ep++;
  306.             }
  307.             if (*ep != EOS)
  308.             *wp++ = *ep++;
  309.         }
  310.         /*
  311.          * work..wp has the text to be underscored.
  312.          * lp -> start of underscored,
  313.          * ep -> start of text after sequence
  314.          * wp -> free space in work.
  315.          * Note:
  316.          * <ESC>[4m        turn  on underscore
  317.          * <ESC>[0m        turn off underscore
  318.          */
  319.         sprintf(wp, "%s0m%s", csi[eightbit], ep);
  320.         sprintf(lp, "%s4m%s", csi[eightbit], work);
  321.         }
  322.         else {
  323.         lp++;        /* Random '_', skip over it    */
  324.         }
  325.     }
  326. }
  327. //E*O*F ulfix.c//
  328.  
  329. echo x - Makefile
  330. cat > "Makefile" << '//E*O*F Makefile//'
  331. # Unix makefile for ulfix
  332. #
  333. # The redefinition of strchr() is needed for Unix 4.2 bsd
  334. # (and maybe some other Unices).
  335. #
  336. BSDDEFINE = -Dstrchr=index -Dstrrchr=rindex
  337. #
  338. # On certain systems, such as Unix System III, you may need to define
  339. # $(LINTFLAGS) in the make command line to set system-specific lint flags.
  340. #
  341. # DEFINES collects all -D arguments for cc and lint:
  342. #
  343. DEFINES = $(BSDDEFINE)
  344.  
  345. CFLAGS = -O $(DEFINES)
  346.  
  347. #
  348. # ** compile ulfix
  349. #
  350. SRCS = ulfix.c
  351. OBJS = ulfix.o
  352. ulfix: $(OBJS)
  353.     $(CC) $(CFLAGS) $(OBJS) -o ulfix
  354.  
  355. lint:    $(SRCS)
  356.     lint $(LINTFLAGS) $(DEFINES) $(SRCS)
  357.  
  358. #
  359. # ** Remove unneeded files
  360. #
  361. clean:
  362.     rm -f $(OBJS) ulfix
  363.  
  364. ulfix.o    :    ulfix.c
  365.  
  366. //E*O*F Makefile//
  367.  
  368. exit 0
  369.  
  370.