mkdict.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* $NetBSD: mkdict.c,v 1.9 2003/08/07 09:37:06 agc Exp $ */
  2. /*-
  3. * Copyright (c) 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Barry Brachman.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #ifndef lint
  34. static const char copyright[] __attribute__((__unused__)) =
  35. "@(#) Copyright (c) 1993\n\
  36. The Regents of the University of California. All rights reserved.\n";
  37. #if 0
  38. static char sccsid[] = "@(#)mkdict.c 8.1 (Berkeley) 6/11/93";
  39. #else
  40. static const char rcsid[] __attribute__((__unused__)) =
  41. "$NetBSD: mkdict.c,v 1.9 2003/08/07 09:37:06 agc Exp $";
  42. #endif
  43. #endif /* not lint */
  44. /*
  45. * Filter out words that:
  46. * 1) Are not completely made up of lower case letters
  47. * 2) Contain a 'q' not immediately followed by a 'u'
  48. * 3) Are less that 3 characters long
  49. * 4) Are greater than MAXWORDLEN characters long
  50. */
  51. #include <ctype.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include "bog.h"
  56. int main(int, char *[]);
  57. int
  58. main(argc, argv)
  59. int argc;
  60. char *argv[];
  61. {
  62. char *p, *q;
  63. int ch, common, nwords;
  64. int current, len, prev, qcount;
  65. char buf[2][MAXWORDLEN + 1];
  66. prev = 0;
  67. current = 1;
  68. buf[prev][0] = '\0';
  69. for (nwords = 1;
  70. fgets(buf[current], MAXWORDLEN + 1, stdin) != NULL; ++nwords) {
  71. if ((p = strchr(buf[current], '\n')) == NULL) {
  72. fprintf(stderr, "word too long: %s\n", buf[current]);
  73. while ((ch = getc(stdin)) != EOF && ch != '\n')
  74. ;
  75. if (ch == EOF)
  76. break;
  77. continue;
  78. }
  79. len = 0;
  80. for (p = buf[current]; *p != '\n'; p++) {
  81. if (!islower(*p))
  82. break;
  83. if (*p == 'q') {
  84. q = p + 1;
  85. if (*q != 'u')
  86. break;
  87. else {
  88. while ((*q = *(q + 1)))
  89. q++;
  90. }
  91. len++;
  92. }
  93. len++;
  94. }
  95. if (*p != '\n' || len < 3 || len > MAXWORDLEN)
  96. continue;
  97. if (argc == 2 && nwords % atoi(argv[1]))
  98. continue;
  99. *p = '\0';
  100. p = buf[current];
  101. q = buf[prev];
  102. qcount = 0;
  103. while ((ch = *p++) == *q++ && ch != '\0')
  104. if (ch == 'q')
  105. qcount++;
  106. common = p - buf[current] - 1;
  107. printf("%c%s", common + qcount, p - 1);
  108. prev = !prev;
  109. current = !current;
  110. }
  111. fprintf(stderr, "%d words\n", nwords);
  112. fflush(stdout);
  113. if (ferror(stdout)) {
  114. perror("error writing standard output");
  115. exit(1);
  116. }
  117. exit(0);
  118. }