ppt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* $NetBSD: ppt.c,v 1.16 2004/01/27 20:30:30 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1988, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
  33. The Regents of the University of California. All rights reserved.\n");
  34. #endif /* not lint */
  35. #ifndef lint
  36. #if 0
  37. static char sccsid[] = "@(#)ppt.c 8.1 (Berkeley) 5/31/93";
  38. #else
  39. __RCSID("$NetBSD: ppt.c,v 1.16 2004/01/27 20:30:30 jsm Exp $");
  40. #endif
  41. #endif /* not lint */
  42. #include <err.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <unistd.h>
  47. #define EDGE "___________"
  48. void usage(void);
  49. int main(int, char *[]);
  50. static void putppt(int);
  51. int getppt(const char *);
  52. void
  53. usage(void)
  54. {
  55. extern char *__progname;
  56. fprintf(stderr, "usage: %s [-d] [string ...]\n", __progname);
  57. exit(1);
  58. }
  59. int
  60. main(argc, argv)
  61. int argc;
  62. char **argv;
  63. {
  64. char *p, buf[132];
  65. int c, start, neednl, dflag;
  66. /* Revoke setgid privileges */
  67. setregid(getgid(), getgid());
  68. dflag = 0;
  69. while ((c = getopt(argc, argv, "dh")) != -1)
  70. switch(c) {
  71. case 'd':
  72. dflag = 1;
  73. break;
  74. case 'h':
  75. case '?':
  76. default:
  77. usage();
  78. }
  79. argc -= optind;
  80. argv += optind;
  81. if (dflag) {
  82. if (argc > 0)
  83. usage();
  84. start = 0;
  85. neednl = 0;
  86. while (fgets(buf, sizeof(buf), stdin) != NULL) {
  87. c = getppt(buf);
  88. if (c < 0) {
  89. if (start) {
  90. /* lost sync? */
  91. if (neednl)
  92. putchar('\n');
  93. exit(0);
  94. } else
  95. continue;
  96. }
  97. start = 1;
  98. putchar(c);
  99. neednl = (c != '\n');
  100. }
  101. if (!feof(stdin))
  102. err(1, "fgets");
  103. if (neednl)
  104. putchar('\n');
  105. } else {
  106. (void) puts(EDGE);
  107. if (argc > 0)
  108. while ((p = *argv++)) {
  109. for (; *p; ++p)
  110. putppt((int)*p);
  111. if ((*(argv)))
  112. putppt((int)' ');
  113. }
  114. else while ((c = getchar()) != EOF)
  115. putppt(c);
  116. (void) puts(EDGE);
  117. }
  118. exit(0);
  119. }
  120. static void
  121. putppt(c)
  122. int c;
  123. {
  124. int i;
  125. (void) putchar('|');
  126. for (i = 7; i >= 0; i--) {
  127. if (i == 2)
  128. (void) putchar('.'); /* feed hole */
  129. if ((c&(1<<i)) != 0)
  130. (void) putchar('o');
  131. else
  132. (void) putchar(' ');
  133. }
  134. (void) putchar('|');
  135. (void) putchar('\n');
  136. }
  137. int
  138. getppt(const char *buf)
  139. {
  140. const char *p = strchr(buf, '.');
  141. int c;
  142. if (p == NULL)
  143. return (-1);
  144. c = 0;
  145. if (p[ 3] != ' ')
  146. c |= 0001;
  147. if (p[ 2] != ' ')
  148. c |= 0002;
  149. if (p[ 1] != ' ')
  150. c |= 0004;
  151. if (p[-1] != ' ')
  152. c |= 0010;
  153. if (p[-2] != ' ')
  154. c |= 0020;
  155. if (p[-3] != ' ')
  156. c |= 0040;
  157. if (p[-4] != ' ')
  158. c |= 0100;
  159. if (p[-5] != ' ')
  160. c |= 0200;
  161. return (c);
  162. }