1 /* Generate .po file from doc-string file.
3 Scan specified doc-string file, creating .po format messages for processing
4 with msgfmt. The results go to standard output or to a file specified
5 with -a or -o (-a to append, -o to start from nothing).
7 Kludge to make up for shortcoming in make-docfile and Snarf-documentation:
8 If arg before input filename is -p, we are scanning an add-on
9 package, which requires slightly different processing.
16 #define EXIT_SUCCESS 0
17 #define EXIT_FAILURE 1
20 /* #define BUFSIZE 8192 */
22 #define NEWSTRING 31 /* Character signalling start of new doc string */
24 #define ENDSTRING "\"\n"
25 #define LINEBEGIN " \""
26 #define LINEBREAK ENDSTRING LINEBEGIN
28 /* some brain-dead headers define this ... */
31 enum boolean { FALSE, TRUE };
34 /***********************/
35 /* buffer pseudo-class */
36 /***********************/
38 typedef struct _buffer
40 size_t index; /* current position in buf[] */
41 size_t size; /* size of buf */
45 #define BUF_NULL {0, 0, NULL}
47 int buf_init (buffer_struct *buffer, size_t size);
48 void buf_free (buffer_struct *buffer);
49 void buf_clear (buffer_struct *buffer);
50 int buf_putc (buffer_struct *buffer, int c);
51 int buf_print (buffer_struct *buffer, const char *s);
54 /********************/
55 /* global variables */
56 /********************/
60 buffer_struct buf = BUF_NULL;
63 void scan_file (enum boolean package);
64 void initialize (void);
65 void clean_exit (int status);
66 void buf_putc_safe (int c);
67 void buf_print_safe (const char *s);
68 void terminate_string (void);
70 main (int argc, char *argv[])
73 enum boolean package = FALSE; /* TRUE if scanning add-on package */
79 /* If first two args are -o FILE, output to FILE. */
81 if (argc > i + 1 && strcmp (argv[i], "-o") == 0) {
82 outfile = fopen (argv[++i], "w");
85 /* ...Or if args are -a FILE, append to FILE. */
86 if (argc > i + 1 && strcmp (argv[i], "-a") == 0) {
87 outfile = fopen (argv[++i], "a");
91 fprintf (stderr, "Unable to open output file %s\n", argv[--i]);
95 if (argc > i && !strcmp (argv[i], "-p")) {
100 infile = fopen (argv[i], "r");
102 fprintf (stderr, "Unable to open input file %s\n", argv[i]);
107 clean_exit (EXIT_SUCCESS);
111 void scan_file (enum boolean package)
113 register int c; /* Character read in */
115 fprintf (outfile, "###############\n");
116 fprintf (outfile, "# DOC strings #\n");
117 fprintf (outfile, "###############\n");
119 while (c = getc (infile), !feof (infile)) {
120 if (c == NEWSTRING) {
121 /* If a string was being processed, terminate it. */
125 /* Skip function or variable name. */
130 /* Begin a new string. */
131 fprintf (outfile, "msgid \"");
132 buf_print_safe ("msgstr \"");
136 /* Peek at next character. */
140 /* For add-on (i.e., non-preloaded) documentation, ignore the last
141 carriage return of a string. */
142 if (!(package && c == NEWSTRING)) {
143 fprintf (outfile, LINEEND);
144 buf_print_safe (LINEEND);
147 /* If not end of string, continue it on the next line. */
148 if (c != NEWSTRING) {
149 fprintf (outfile, LINEBREAK);
150 buf_print_safe (LINEBREAK);
155 /* If character is \ or ", precede it by a backslash. */
156 if (c == '\\' || c == '\"') {
157 putc ('\\', outfile);
158 buf_putc_safe ('\\');
169 /* initialize sets up the global variables.
171 void initialize (void)
173 if (buf_init (&buf, BUFSIZE) != 0)
174 clean_exit (EXIT_FAILURE);
178 /* clean_exit returns any resources and terminates the program.
179 An error message is printed if status is EXIT_FAILURE.
181 void clean_exit (int status)
190 if (status == EXIT_FAILURE)
191 fprintf (stderr, "make-po abnormally terminated\n");
196 /* buf_putc_safe writes the character c on the global buffer buf,
197 checking to make sure that the operation was successful.
199 void buf_putc_safe (int c)
203 status = buf_putc (&buf, c);
205 clean_exit (EXIT_FAILURE);
209 /* buf_putc_safe writes the string s on the global buffer buf,
210 checking to make sure that the operation was successful.
212 void buf_print_safe (const char *s)
216 status = buf_print (&buf, s);
218 clean_exit (EXIT_FAILURE);
222 /* terminate_string terminates the current doc string and outputs the buffer.
224 void terminate_string (void)
226 fprintf (outfile, ENDSTRING);
228 /* Make the "translation" different from the original string. */
229 buf_print_safe ("_X");
231 buf_print_safe (ENDSTRING);
232 fprintf (outfile, "%s", buf.buf);
237 /*********************************/
238 /* buffer pseudo-class functions */
239 /*********************************/
241 /* buf_init initializes a buffer to the specified size.
242 It returns non-zero if the attempt fails.
244 int buf_init (buffer_struct *buffer, size_t size)
246 buffer->buf = malloc (size);
247 if (buffer->buf == NULL)
256 /* buf_free releases the memory allocated for the buffer.
258 void buf_free (buffer_struct *buffer)
265 /* buf_clear resets a buffer to an empty string.
267 void buf_clear (buffer_struct *buffer)
270 buffer->buf[0] = '\0';
274 /* buf_putc writes the character c on the buffer.
275 It returns the character written, or EOF for error.
277 int buf_putc (buffer_struct *buffer, int c)
279 if (buffer->index >= buffer->size)
282 buffer->buf[buffer->index++] = c;
287 /* buf_print writes the string s on the buffer.
288 It returns the number of characters written, or negative if an error occurred.
290 int buf_print (buffer_struct *buffer, const char *s)
295 if (buffer->index + len >= buffer->size)
298 sprintf (&(buffer->buf[buffer->index]), s);
299 buffer->index += len;