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 */
21 /* #define BUFSIZE 16384 */
23 #define NEWSTRING 31 /* Character signalling start of new doc string */
25 #define ENDSTRING "\"\n"
26 #define LINEBEGIN " \""
27 #define LINEBREAK ENDSTRING LINEBEGIN
29 /* some brain-dead headers define this ... */
32 enum boolean { FALSE, TRUE };
35 /***********************/
36 /* buffer pseudo-class */
37 /***********************/
39 typedef struct _buffer
41 size_t index; /* current position in buf[] */
42 size_t size; /* size of buf */
46 #define BUF_NULL {0, 0, NULL}
48 int buf_init (buffer_struct *buffer, size_t size);
49 void buf_free (buffer_struct *buffer);
50 void buf_clear (buffer_struct *buffer);
51 int buf_putc (buffer_struct *buffer, int c);
52 int buf_print (buffer_struct *buffer, const char *s);
55 /********************/
56 /* global variables */
57 /********************/
61 buffer_struct buf = BUF_NULL;
64 void scan_file (enum boolean package);
65 void initialize (void);
66 void clean_exit (int status);
67 void buf_putc_safe (int c);
68 void buf_print_safe (const char *s);
69 void terminate_string (void);
71 main (int argc, char *argv[])
74 enum boolean package = FALSE; /* TRUE if scanning add-on package */
80 /* If first two args are -o FILE, output to FILE. */
82 if (argc > i + 1 && strcmp (argv[i], "-o") == 0) {
83 outfile = fopen (argv[++i], "w");
86 /* ...Or if args are -a FILE, append to FILE. */
87 if (argc > i + 1 && strcmp (argv[i], "-a") == 0) {
88 outfile = fopen (argv[++i], "a");
92 fprintf (stderr, "Unable to open output file %s\n", argv[--i]);
96 if (argc > i && !strcmp (argv[i], "-p")) {
101 infile = fopen (argv[i], "r");
103 fprintf (stderr, "Unable to open input file %s\n", argv[i]);
108 clean_exit (EXIT_SUCCESS);
112 void scan_file (enum boolean package)
114 register int c; /* Character read in */
116 fprintf (outfile, "###############\n");
117 fprintf (outfile, "# DOC strings #\n");
118 fprintf (outfile, "###############\n");
120 while (c = getc (infile), !feof (infile)) {
121 if (c == NEWSTRING) {
122 /* If a string was being processed, terminate it. */
126 /* Skip function or variable name. */
131 /* Begin a new string. */
132 fprintf (outfile, "msgid \"");
133 buf_print_safe ("msgstr \"");
137 /* Peek at next character. */
141 /* For add-on (i.e., non-preloaded) documentation, ignore the last
142 carriage return of a string. */
143 if (!(package && c == NEWSTRING)) {
144 fprintf (outfile, LINEEND);
145 buf_print_safe (LINEEND);
148 /* If not end of string, continue it on the next line. */
149 if (c != NEWSTRING) {
150 fprintf (outfile, LINEBREAK);
151 buf_print_safe (LINEBREAK);
156 /* If character is \ or ", precede it by a backslash. */
157 if (c == '\\' || c == '\"') {
158 putc ('\\', outfile);
159 buf_putc_safe ('\\');
170 /* initialize sets up the global variables.
172 void initialize (void)
174 if (buf_init (&buf, BUFSIZE) != 0)
175 clean_exit (EXIT_FAILURE);
179 /* clean_exit returns any resources and terminates the program.
180 An error message is printed if status is EXIT_FAILURE.
182 void clean_exit (int status)
191 if (status == EXIT_FAILURE)
192 fprintf (stderr, "make-po abnormally terminated\n");
197 /* buf_putc_safe writes the character c on the global buffer buf,
198 checking to make sure that the operation was successful.
200 void buf_putc_safe (int c)
204 status = buf_putc (&buf, c);
206 clean_exit (EXIT_FAILURE);
210 /* buf_putc_safe writes the string s on the global buffer buf,
211 checking to make sure that the operation was successful.
213 void buf_print_safe (const char *s)
217 status = buf_print (&buf, s);
219 clean_exit (EXIT_FAILURE);
223 /* terminate_string terminates the current doc string and outputs the buffer.
225 void terminate_string (void)
227 fprintf (outfile, ENDSTRING);
229 /* Make the "translation" different from the original string. */
230 buf_print_safe ("_X");
232 buf_print_safe (ENDSTRING);
233 fprintf (outfile, "%s", buf.buf);
238 /*********************************/
239 /* buffer pseudo-class functions */
240 /*********************************/
242 /* buf_init initializes a buffer to the specified size.
243 It returns non-zero if the attempt fails.
245 int buf_init (buffer_struct *buffer, size_t size)
247 buffer->buf = malloc (size);
248 if (buffer->buf == NULL)
257 /* buf_free releases the memory allocated for the buffer.
259 void buf_free (buffer_struct *buffer)
266 /* buf_clear resets a buffer to an empty string.
268 void buf_clear (buffer_struct *buffer)
271 buffer->buf[0] = '\0';
275 /* buf_putc writes the character c on the buffer.
276 It returns the character written, or EOF for error.
278 int buf_putc (buffer_struct *buffer, int c)
280 if (buffer->index >= buffer->size)
283 buffer->buf[buffer->index++] = c;
288 /* buf_print writes the string s on the buffer.
289 It returns the number of characters written, or negative if an error occurred.
291 int buf_print (buffer_struct *buffer, const char *s)
296 if (buffer->index + len >= buffer->size)
299 sprintf (&(buffer->buf[buffer->index]), s);
300 buffer->index += len;