annotate lib-src/make-po.c @ 2621:a5158a1f89a4

[xemacs-hg @ 2005-02-28 22:51:03 by viteno] Update xemacs_extra_name.
author viteno
date Mon, 28 Feb 2005 22:51:03 +0000
parents 576fb035e263
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
1 /* Generate .po file from doc-string file.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
2
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
3 Scan specified doc-string file, creating .po format messages for processing
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
4 with msgfmt. The results go to standard output or to a file specified
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
5 with -a or -o (-a to append, -o to start from nothing).
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
6
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
7 Kludge to make up for shortcoming in make-docfile and Snarf-documentation:
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
8 If arg before input filename is -p, we are scanning an add-on
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
9 package, which requires slightly different processing.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
10 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
11
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
12 #include <stdio.h>
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
13 #include <stdlib.h>
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
14
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
15 #ifndef EXIT_SUCCESS
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
16 #define EXIT_SUCCESS 0
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
17 #define EXIT_FAILURE 1
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
18 #endif
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
19
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
20 /* #define BUFSIZE 8192 */
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 70
diff changeset
21 /* #define BUFSIZE 16384 */
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 70
diff changeset
22 #define BUFSIZE 32768
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
23 #define NEWSTRING 31 /* Character signalling start of new doc string */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
24 #define LINEEND "\\n"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
25 #define ENDSTRING "\"\n"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
26 #define LINEBEGIN " \""
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
27 #define LINEBREAK ENDSTRING LINEBEGIN
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
28
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
29 /* some brain-dead headers define this ... */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
30 #undef FALSE
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
31 #undef TRUE
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
32 enum boolean { FALSE, TRUE };
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
33
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
34
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
35 /***********************/
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
36 /* buffer pseudo-class */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
37 /***********************/
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
38
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
39 typedef struct _buffer
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
40 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
41 size_t index; /* current position in buf[] */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
42 size_t size; /* size of buf */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
43 char *buf;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
44 } buffer_struct;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
45
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
46 #define BUF_NULL {0, 0, NULL}
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
47
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
48 int buf_init (buffer_struct *buffer, size_t size);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
49 void buf_free (buffer_struct *buffer);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
50 void buf_clear (buffer_struct *buffer);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
51 int buf_putc (buffer_struct *buffer, int c);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
52 int buf_print (buffer_struct *buffer, const char *s);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
53
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
54
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
55 /********************/
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
56 /* global variables */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
57 /********************/
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
58
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
59 FILE *infile = NULL;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
60 FILE *outfile = NULL;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
61 buffer_struct buf = BUF_NULL;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
62
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
63
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
64 void scan_file (enum boolean package);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
65 void initialize (void);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
66 void clean_exit (int status);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
67 void buf_putc_safe (int c);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
68 void buf_print_safe (const char *s);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
69 void terminate_string (void);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
71 main (int argc, char *argv[])
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
72 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
73 register int i;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
74 enum boolean package = FALSE; /* TRUE if scanning add-on package */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
75
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
76 initialize ();
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
77
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
78 outfile = stdout;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
79
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
80 /* If first two args are -o FILE, output to FILE. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
81 i = 1;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
82 if (argc > i + 1 && strcmp (argv[i], "-o") == 0) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
83 outfile = fopen (argv[++i], "w");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
84 ++i;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
85 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
86 /* ...Or if args are -a FILE, append to FILE. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
87 if (argc > i + 1 && strcmp (argv[i], "-a") == 0) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
88 outfile = fopen (argv[++i], "a");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
89 ++i;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
90 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
91 if (!outfile) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
92 fprintf (stderr, "Unable to open output file %s\n", argv[--i]);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
93 return 1;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
94 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
95
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
96 if (argc > i && !strcmp (argv[i], "-p")) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
97 package = TRUE;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
98 ++i;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
99 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
100
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
101 infile = fopen (argv[i], "r");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
102 if (!infile) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
103 fprintf (stderr, "Unable to open input file %s\n", argv[i]);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
104 return 1;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
105 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
106
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
107 scan_file (package);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
108 clean_exit (EXIT_SUCCESS);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
109 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
110
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
111
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
112 void scan_file (enum boolean package)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
113 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
114 register int c; /* Character read in */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
115
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
116 fprintf (outfile, "###############\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
117 fprintf (outfile, "# DOC strings #\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
118 fprintf (outfile, "###############\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
119
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
120 while (c = getc (infile), !feof (infile)) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
121 if (c == NEWSTRING) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
122 /* If a string was being processed, terminate it. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
123 if (buf.index > 0)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
124 terminate_string ();
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
125
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
126 /* Skip function or variable name. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
127 while (c != '\n')
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
128 c = getc (infile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
129 c = getc (infile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
130
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
131 /* Begin a new string. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
132 fprintf (outfile, "msgid \"");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
133 buf_print_safe ("msgstr \"");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
134 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
135
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
136 if (c == '\n') {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
137 /* Peek at next character. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
138 c = getc (infile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
139 ungetc (c, infile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
140
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
141 /* For add-on (i.e., non-preloaded) documentation, ignore the last
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
142 carriage return of a string. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
143 if (!(package && c == NEWSTRING)) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
144 fprintf (outfile, LINEEND);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
145 buf_print_safe (LINEEND);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
146 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
147
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
148 /* If not end of string, continue it on the next line. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
149 if (c != NEWSTRING) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
150 fprintf (outfile, LINEBREAK);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
151 buf_print_safe (LINEBREAK);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
152 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
153 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
154 else {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
155
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
156 /* If character is \ or ", precede it by a backslash. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
157 if (c == '\\' || c == '\"') {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
158 putc ('\\', outfile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
159 buf_putc_safe ('\\');
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
160 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
161
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
162 putc (c, outfile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
163 buf_putc_safe (c);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
164 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
165 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
166 terminate_string ();
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
167 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
168
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
169
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
170 /* initialize sets up the global variables.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
171 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
172 void initialize (void)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
173 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
174 if (buf_init (&buf, BUFSIZE) != 0)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
175 clean_exit (EXIT_FAILURE);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
176 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
177
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
178
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
179 /* clean_exit returns any resources and terminates the program.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
180 An error message is printed if status is EXIT_FAILURE.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
181 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
182 void clean_exit (int status)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
183 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
184 if (buf.size > 0)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
185 buf_free (&buf);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
186 if (outfile)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
187 fclose (outfile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
188 if (infile)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
189 fclose (infile);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
190
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
191 if (status == EXIT_FAILURE)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
192 fprintf (stderr, "make-po abnormally terminated\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
193 exit (status);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
194 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
195
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
196
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
197 /* buf_putc_safe writes the character c on the global buffer buf,
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
198 checking to make sure that the operation was successful.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
199 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
200 void buf_putc_safe (int c)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
201 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
202 register int status;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
203
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
204 status = buf_putc (&buf, c);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
205 if (status == EOF)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
206 clean_exit (EXIT_FAILURE);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
207 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
208
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
209
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
210 /* buf_putc_safe writes the string s on the global buffer buf,
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
211 checking to make sure that the operation was successful.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
212 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
213 void buf_print_safe (const char *s)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
214 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
215 register int status;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
216
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
217 status = buf_print (&buf, s);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
218 if (status < 0)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
219 clean_exit (EXIT_FAILURE);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
220 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
221
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
222
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
223 /* terminate_string terminates the current doc string and outputs the buffer.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
224 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
225 void terminate_string (void)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
226 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
227 fprintf (outfile, ENDSTRING);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
228
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
229 /* Make the "translation" different from the original string. */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
230 buf_print_safe ("_X");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
231
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
232 buf_print_safe (ENDSTRING);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
233 fprintf (outfile, "%s", buf.buf);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
234 buf_clear (&buf);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
235 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
236
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
237
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
238 /*********************************/
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
239 /* buffer pseudo-class functions */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
240 /*********************************/
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
241
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
242 /* buf_init initializes a buffer to the specified size.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
243 It returns non-zero if the attempt fails.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
244 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
245 int buf_init (buffer_struct *buffer, size_t size)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
246 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
247 buffer->buf = malloc (size);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
248 if (buffer->buf == NULL)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
249 return 1;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
250
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
251 buffer->size = size;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
252 buf_clear (buffer);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
253 return 0;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
254 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
255
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
256
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
257 /* buf_free releases the memory allocated for the buffer.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
258 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
259 void buf_free (buffer_struct *buffer)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
260 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
261 free (buffer->buf);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
262 buffer->size = 0;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
263 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
264
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
265
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
266 /* buf_clear resets a buffer to an empty string.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
267 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
268 void buf_clear (buffer_struct *buffer)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
269 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
270 buffer->index = 0;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
271 buffer->buf[0] = '\0';
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
272 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
273
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
274
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
275 /* buf_putc writes the character c on the buffer.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
276 It returns the character written, or EOF for error.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
277 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
278 int buf_putc (buffer_struct *buffer, int c)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
279 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
280 if (buffer->index >= buffer->size)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
281 return EOF;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
282
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
283 buffer->buf[buffer->index++] = c;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
284 return c;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
285 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
286
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
287
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
288 /* buf_print writes the string s on the buffer.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
289 It returns the number of characters written, or negative if an error occurred.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
290 */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
291 int buf_print (buffer_struct *buffer, const char *s)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
292 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
293 register int len;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
294
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
295 len = strlen (s);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
296 if (buffer->index + len >= buffer->size)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
297 return -1;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
298
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
299 sprintf (&(buffer->buf[buffer->index]), s);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
300 buffer->index += len;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
301 return len;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
302 }