annotate lib-src/yow.c @ 10213:dd2102e33b29

(dired-string-replace-match): Moved to dired.el.
author Richard M. Stallman <rms@gnu.org>
date Thu, 22 Dec 1994 04:16:36 +0000
parents aa59550d809f
children 2f57a6fbd720
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
1 /*
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
2 * yow.c
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 *
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4 * Print a quotation from Zippy the Pinhead.
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 * Qux <Kaufman-David@Yale> March 6, 1986
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 *
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
7 * With dynamic memory allocation.
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
10 #include <stdio.h>
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
11 #include <ctype.h>
4702
2641ab559aae Include <src/paths.h>, instead "src/paths.h".
Roland McGrath <roland@gnu.org>
parents: 3591
diff changeset
12 #include <../src/paths.h> /* For PATH_DATA. */
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
13
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
14 #define BUFSIZE 80
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 #define SEP '\0'
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
16
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
17 #ifndef YOW_FILE
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 #define YOW_FILE "yow.lines"
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
19 #endif
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
7820
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
21 #ifdef MSDOS
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
22 #define rootrelativepath(rel) \
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
23 ({\
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
24 static char res[BUFSIZE], *p;\
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
25 strcpy (res, argv[0]);\
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
26 p = res + strlen (res);\
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
27 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
28 strcpy (p + 1, "../");\
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
29 strcpy (p + 4, rel);\
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
30 &res;})
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
31 #endif
128950f29e66 (rootrelativepath) [MSDOS]: Define, expanding to dynamic
Richard M. Stallman <rms@gnu.org>
parents: 4702
diff changeset
32
10198
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
33 char *malloc(), *realloc();
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
34
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
35 void yow();
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
36 void setup_yow();
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
37
9491
dd3b83e4ceb0 Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents: 7820
diff changeset
38 int
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 main (argc, argv)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 int argc;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 char *argv[];
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 FILE *fp;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 char file[BUFSIZ];
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 if (argc > 2 && !strcmp (argv[1], "-f"))
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 strcpy (file, argv[2]);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 else
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 #ifdef vms
443
78165914b12d entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 42
diff changeset
50 sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 #else
443
78165914b12d entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 42
diff changeset
52 sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 #endif
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 if ((fp = fopen(file, "r")) == NULL) {
10198
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
56 fprintf(stderr, "yow: ");
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 perror(file);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 exit(1);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 /* initialize random seed */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 srand((int) (getpid() + time((long *) 0)));
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
64 setup_yow(fp);
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 fclose(fp);
9491
dd3b83e4ceb0 Eliminate some -Wall warnings.
David J. MacKenzie <djm@gnu.org>
parents: 7820
diff changeset
67 return 0;
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
70 static long len = -1;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
71 static long header_len;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
72
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
73 #define AVG_LEN 40 /* average length of a quotation */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
74
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
75 /* Sets len and header_len */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
76 void
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
77 setup_yow(fp)
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
78 FILE *fp;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
79 {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
80 int c;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
81
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
82 /* Get length of file */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
83 /* Because the header (stuff before the first SEP) can be very long,
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
84 * thus biasing our search in favor of the first quotation in the file,
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
85 * we explicitly skip that. */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
86 while ((c = getc(fp)) != SEP) {
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
87 if (c == EOF) {
10198
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
88 fprintf(stderr, "yow: file contains no separators\n");
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
89 exit(2);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
90 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
91 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
92 header_len = ftell(fp);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
93 if (header_len > AVG_LEN)
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
94 header_len -= AVG_LEN; /* allow the first quotation to appear */
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
95
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
96 if (fseek(fp, 0L, 2) == -1) {
10198
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
97 perror("yow");
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
98 exit(1);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
99 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
100 len = ftell(fp) - header_len;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
101 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
102
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
103
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
104 /* go to a random place in the file and print the quotation there */
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 void
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 yow (fp)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 FILE *fp;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 long offset;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 int c, i = 0;
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
111 char *buf;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
112 unsigned int bufsize;
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
114 offset = rand() % len + header_len;
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 if (fseek(fp, offset, 0) == -1) {
10198
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
116 perror("yow");
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 exit(1);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 /* Read until SEP, read next line, print it.
3591
507f64624555 Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents: 443
diff changeset
121 (Note that we will never print anything before the first separator.)
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 If we hit EOF looking for the first SEP, just recurse. */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 while ((c = getc(fp)) != SEP)
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 if (c == EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 return;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 /* Skip leading whitespace, then read in a quotation.
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 If we hit EOF before we find a non-whitespace char, recurse. */
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 while (isspace(c = getc(fp)))
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 ;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 if (c == EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 yow(fp);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 return;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 }
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
137
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
138 bufsize = BUFSIZE;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
139 buf = malloc(bufsize);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
140 if (buf == (char *)0) {
10198
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
141 fprintf(stderr, "yow: virtual memory exhausted\n");
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
142 exit (3);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
143 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
144
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 buf[i++] = c;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 while ((c = getc(fp)) != SEP && c != EOF) {
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 buf[i++] = c;
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
148
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
149 if (i == bufsize-1) {
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 /* Yow! Is this quotation too long yet? */
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
151 bufsize *= 2;
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
152 buf = realloc(buf, bufsize);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
153 if (buf == (char *)0) {
10198
aa59550d809f Include program name in error messages.
David J. MacKenzie <djm@gnu.org>
parents: 9491
diff changeset
154 fprintf(stderr, "yow: virtual memory exhausted\n");
42
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
155 exit (3);
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
156 }
71b3e30e0dd4 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 12
diff changeset
157 }
12
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 buf[i++] = 0;
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 printf("%s\n", buf);
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 }
4f420246a4f7 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162