annotate lib-src/make-po.c @ 347:7c94d56991e1 r21-1-3

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