1 /* $NetBSD: infokey.c,v 1.3 2008/09/02 08:00:24 christos Exp $ */
3 /* infokey.c -- compile ~/.infokey to ~/.info.
4 Id: infokey.c,v 1.9 2004/12/14 00:15:36 karl Exp
6 Copyright (C) 1999, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 Written by Andrew Bettison <andrewb@zip.com.au>. */
30 static char *program_name
= "infokey";
32 /* Non-zero means print version info only. */
33 static int print_version_p
= 0;
35 /* Non-zero means print a short description of the options. */
36 static int print_help_p
= 0;
38 /* String specifying the source file. This is set by the user on the
39 command line, or a default is used. */
40 static char *input_filename
= (char *) NULL
;
42 /* String specifying the name of the file to output to. This is
43 set by the user on the command line, or a default is used. */
44 static char *output_filename
= (char *) NULL
;
46 /* Structure describing the options that Infokey accepts. We pass this
47 structure to getopt_long (). If you add or otherwise change this
48 structure, you must also change the string which follows it. */
49 static struct option long_options
[] =
51 {"output", 1, 0, 'o'},
52 {"help", 0, &print_help_p
, 1},
53 {"version", 0, &print_version_p
, 1},
57 /* String describing the shorthand versions of the long options found above. */
58 static char *short_options
= "o:";
60 /* Structure for holding the compiled sections. */
70 unsigned char data
[INFOKEY_MAX_SECTIONLEN
];
73 /* Some "forward" declarations. */
74 static char *mkpath (const char *dir
, const char *file
);
75 static int compile (FILE *fp
, const char *filename
, struct sect
*sections
);
76 static int write_infokey_file (FILE *fp
, struct sect
*sections
);
77 static void syntax_error (const char *filename
,
78 unsigned int linenum
, const char *fmt
,
79 const void *a1
, const void *a2
, const void *a3
, const void *a4
);
80 static void error_message (int error_code
, const char *fmt
,
81 const void *a1
, const void *a2
, const void *a3
, const void *a4
);
82 static void suggest_help (void);
83 static void short_help (void);
86 /* **************************************************************** */
88 /* Main Entry Point to the Infokey Program */
90 /* **************************************************************** */
93 main (int argc
, char **argv
)
95 int getopt_long_index
; /* Index returned by getopt_long (). */
98 /* Set locale via LC_ALL. */
99 setlocale (LC_ALL
, "");
103 /* Set the text message domain. */
104 bindtextdomain (PACKAGE
, LOCALEDIR
);
105 textdomain (PACKAGE
);
110 int option_character
;
112 option_character
= getopt_long
113 (argc
, argv
, short_options
, long_options
, &getopt_long_index
);
115 /* getopt_long () returns EOF when there are no more long options. */
116 if (option_character
== EOF
)
119 /* If this is a long option, then get the short version of it. */
120 if (option_character
== 0 && long_options
[getopt_long_index
].flag
== 0)
121 option_character
= long_options
[getopt_long_index
].val
;
123 /* Case on the option that we have received. */
124 switch (option_character
)
129 /* User is specifying the name of a file to output to. */
132 free (output_filename
);
133 output_filename
= xstrdup (optarg
);
142 /* If the user specified --version, then show the version and exit. */
145 printf ("%s (GNU %s) %s\n", program_name
, PACKAGE
, VERSION
);
147 printf (_ ("Copyright (C) %s Free Software Foundation, Inc.\n\
148 There is NO warranty. You may redistribute this software\n\
149 under the terms of the GNU General Public License.\n\
150 For more information about these matters, see the files named COPYING.\n"),
155 /* If the `--help' option was present, show the help and exit. */
162 /* If there is one argument remaining, it is the name of the input
164 if (optind
== argc
- 1)
167 free (input_filename
);
168 input_filename
= xstrdup (argv
[optind
]);
170 else if (optind
!= argc
)
172 error_message (0, _("incorrect number of arguments"),
173 NULL
, NULL
, NULL
, NULL
);
178 /* Use default filenames where none given. */
182 homedir
= getenv ("HOME");
188 input_filename
= mkpath (homedir
, INFOKEY_SRCFILE
);
189 if (!output_filename
)
190 output_filename
= mkpath (homedir
, INFOKEY_FILE
);
197 static struct sect sections
[3];
199 /* Open the input file. */
200 inf
= fopen (input_filename
, "r");
203 error_message (errno
, _("cannot open input file `%s'"),
204 input_filename
, NULL
, NULL
, NULL
);
208 /* Compile the input file to its verious sections, then write the
209 section data to the output file. */
211 if (compile (inf
, input_filename
, sections
))
213 /* Open the output file. */
214 outf
= fopen (output_filename
, FOPEN_WBIN
);
217 error_message (errno
, _("cannot create output file `%s'"),
218 output_filename
, NULL
, NULL
, NULL
);
222 /* Write the contents of the output file and close it. If there is
223 an error writing to the file, delete it and exit with a failure
226 if (!write_infokey_file (outf
, sections
))
228 error_message (errno
, _("error writing to `%s'"),
229 output_filename
, NULL
, NULL
, NULL
);
232 if (fclose (outf
) == EOF
)
234 error_message (errno
, _("error closing output file `%s'"),
235 output_filename
, NULL
, NULL
, NULL
);
240 unlink (output_filename
);
245 /* Close the input file. */
253 mkpath (const char *dir
, const char *file
)
257 p
= xmalloc (strlen (dir
) + 1 + strlen (file
) + 2);
265 /* Compilation - the real work.
269 The source file is a line-based text file with the following
278 ^a invalid # just beep
285 ^a echo-area-beg-of-line
286 ^e echo-area-end-of-line
287 \kr echo-area-forward
288 \kl echo-area-backward
289 \kh echo-area-beg-of-line
290 \ke echo-area-end-of-line
296 Lines starting with '#' are comments, and are ignored. Blank
297 lines are ignored. Each section is introduced by one of the
304 The sections may occur in any order. Each section may be
305 omitted completely. If the 'info' section is the first in the
306 file, its '#info' line may be omitted.
308 The 'info' and 'echo-area' sections
309 -----------------------------------
310 Each line in the 'info' or 'echo-area' sections has the
313 key-sequence SPACE action-name [ SPACE [ # comment ] ] \n
315 Where SPACE is one or more white space characters excluding
316 newline, "action-name" is the name of a GNU Info command,
317 "comment" is any sequence of characters excluding newline, and
318 "key-sequence" is a concatenation of one or more key definitions
319 using the following syntax:
321 1. A carat ^ followed by one character indicates a single
324 2. A backslash \ followed by one, two, or three octal
325 digits indicates a single character having that ASCII
328 3. \n indicates a single NEWLINE;
329 \e indicates a single ESC;
330 \r indicates a single CR;
331 \t indicates a single TAB;
332 \b indicates a single BACKSPACE;
334 4. \ku indicates the Up Arrow key;
335 \kd indicates the Down Arrow key;
336 \kl indicates the Left Arrow key;
337 \kr indicates the Right Arrow key;
338 \kP indicates the Page Up (PRIOR) key;
339 \kN indicates the Page Down (NEXT) key;
340 \kh indicates the Home key;
341 \ke indicates the End key;
342 \kx indicates the DEL key;
343 \k followed by any other character indicates a single
344 control-K, and the following character is interpreted
345 as in rules 1, 2, 3, 5 and 6.
347 5. \m followed by any sequence defined in rules 1, 2, 3, 4
348 or 6 indicates the "Meta" modification of that key.
350 6. A backslash \ followed by any character not described
351 above indicates that character itself. In particular:
352 \\ indicates a single backslash \,
353 \ (backslash-space) indicates a single space,
354 \^ indicates a single caret ^,
356 If the following line:
360 occurs anywhere in an 'info' or 'echo-area' section, that
361 indicates to GNU Info to suppress all of its default key
362 bindings in that context.
366 Each line in the 'var' section has the following syntax:
368 variable-name = value \n
370 Where "variable-name" is the name of a GNU Info variable and
371 "value" is the value that GNU Info will assign to that variable
372 when commencing execution. There must be no white space in the
373 variable name, nor between the variable name and the '='. All
374 characters immediately following the '=', up to but not
375 including the terminating newline, are considered to be the
376 value that will be assigned. In other words, white space
377 following the '=' is not ignored.
380 static int add_to_section (struct sect
*s
, const char *str
, unsigned int len
);
381 static int lookup_action (const char *actname
);
383 /* Compile the input file into its various sections. Return true if no
384 error was encountered.
387 compile (FILE *fp
, const char *filename
, struct sect
*sections
)
391 unsigned int lnum
= 0;
394 /* This parser is a true state machine, with no sneaky fetching
395 of input characters inside the main loop. In other words, all
396 state is fully represented by the following variables:
414 state
= start_of_line
;
415 enum sect_e section
= info
;
424 seqstate
; /* used if state == get_keyseq */
426 char ocnt
= 0; /* used if state == get_keyseq && seqstate == octal */
428 /* Data is accumulated in the following variables. The code
429 avoids overflowing these strings, and throws an error
430 where appropriate if a string limit is exceeded. These string
431 lengths are arbitrary (and should be large enough) and their
432 lengths are not hard-coded anywhere else, so increasing them
433 here will not break anything. */
436 unsigned int clen
= 0;
438 unsigned int slen
= 0;
440 unsigned int alen
= 0;
442 unsigned int varlen
= 0;
444 unsigned int vallen
= 0;
448 if (slen < sizeof seq) \
449 seq[slen++] = meta ? Meta(c) : (c); \
452 syntax_error(filename, lnum, _("key sequence too long"), \
453 NULL, NULL, NULL, NULL); \
459 sections
[info
].cur
= 1;
460 sections
[info
].data
[0] = 0;
461 sections
[ea
].cur
= 1;
462 sections
[ea
].data
[0] = 0;
463 sections
[var
].cur
= 0;
465 while (!error
&& (rescan
|| (c
= fgetc (fp
)) != EOF
))
473 state
= start_of_comment
;
493 case start_of_comment
:
495 state
= in_line_comment
;
497 case in_line_comment
:
500 state
= start_of_line
;
501 comment
[clen
] = '\0';
502 if (strcmp (comment
, "info") == 0)
504 else if (strcmp (comment
, "echo-area") == 0)
506 else if (strcmp (comment
, "var") == 0)
508 else if (strcmp (comment
, "stop") == 0
509 && (section
== info
|| section
== ea
))
510 sections
[section
].data
[0] = 1;
512 else if (clen
< sizeof comment
- 1)
516 case in_trailing_comment
:
518 state
= start_of_line
;
525 if (c
== '\n' || isspace (c
))
531 syntax_error (filename
, lnum
, _("missing key sequence"),
532 NULL
, NULL
, NULL
, NULL
);
547 case '0': case '1': case '2': case '3':
548 case '4': case '5': case '6': case '7':
578 seqstate
= special_key
;
581 /* Backslash followed by any other char
582 just means that char. */
592 case '0': case '1': case '2': case '3':
593 case '4': case '5': case '6': case '7':
595 oval
= oval
* 8 + c
- '0';
605 if (seqstate
!= octal
)
611 syntax_error (filename
, lnum
,
612 _("NUL character (\\000) not permitted"),
613 NULL
, NULL
, NULL
, NULL
);
623 case 'u': To_seq (SK_UP_ARROW
); break;
624 case 'd': To_seq (SK_DOWN_ARROW
); break;
625 case 'r': To_seq (SK_RIGHT_ARROW
); break;
626 case 'l': To_seq (SK_LEFT_ARROW
); break;
627 case 'U': To_seq (SK_PAGE_UP
); break;
628 case 'D': To_seq (SK_PAGE_DOWN
); break;
629 case 'h': To_seq (SK_HOME
); break;
630 case 'e': To_seq (SK_END
); break;
631 case 'x': To_seq (SK_DELETE
); break;
632 default: To_seq (SK_LITERAL
); rescan
= 1; break;
639 To_seq (CONTROL (c
));
642 syntax_error (filename
, lnum
,
643 (char *) _("NUL character (^%c) not permitted"),
644 (void *)((intptr_t)c
), NULL
, NULL
, NULL
);
653 if (isspace (c
) && c
!= '\n')
659 if (c
== '\n' || isspace (c
))
667 syntax_error (filename
, lnum
, (char *) _("missing action name"),
668 (void *)((intptr_t)c
), NULL
, NULL
, NULL
);
674 a
= lookup_action (act
);
679 if (!(add_to_section (§ions
[section
], seq
, slen
)
680 && add_to_section (§ions
[section
], "", 1)
681 && add_to_section (§ions
[section
], &av
, 1)))
683 syntax_error (filename
, lnum
, _("section too long"),
684 NULL
, NULL
, NULL
, NULL
);
690 syntax_error (filename
, lnum
, _("unknown action `%s'"),
691 act
, NULL
, NULL
, NULL
);
696 else if (alen
< sizeof act
- 1)
700 syntax_error (filename
, lnum
, _("action name too long"),
701 NULL
, NULL
, NULL
, NULL
);
708 state
= in_trailing_comment
;
710 state
= start_of_line
;
711 else if (!isspace (c
))
713 syntax_error (filename
, lnum
,
714 _("extra characters following action `%s'"),
715 act
, NULL
, NULL
, NULL
);
725 syntax_error (filename
, lnum
, _("missing variable name"),
726 NULL
, NULL
, NULL
, NULL
);
732 else if (c
== '\n' || isspace (c
))
734 syntax_error (filename
, lnum
,
735 _("missing `=' immediately after variable name"),
736 NULL
, NULL
, NULL
, NULL
);
739 else if (varlen
< sizeof varn
)
743 syntax_error (filename
, lnum
, _("variable name too long"),
744 NULL
, NULL
, NULL
, NULL
);
752 state
= start_of_line
;
753 if (!(add_to_section (§ions
[section
], varn
, varlen
)
754 && add_to_section (§ions
[section
], "", 1)
755 && add_to_section (§ions
[section
], val
, vallen
)
756 && add_to_section (§ions
[section
], "", 1)))
758 syntax_error (filename
, lnum
, _("section too long"),
759 NULL
, NULL
, NULL
, NULL
);
763 else if (vallen
< sizeof val
)
767 syntax_error (filename
, lnum
, _("value too long"),
768 NULL
, NULL
, NULL
, NULL
);
785 /* Add some characters to a section's data. Return true if all the
786 characters fit, or false if the section's size limit was exceeded.
789 add_to_section (struct sect
*s
, const char *str
, unsigned int len
)
791 if (s
->cur
+ len
> sizeof s
->data
)
793 strncpy ((char *) s
->data
+ s
->cur
, str
, len
);
798 /* Translate from an action name to its numeric code. This uses the
799 auto-generated array in key.c.
802 lookup_action (const char *actname
)
806 if (strcmp ("invalid", actname
) == 0)
808 for (i
= 0; function_key_array
[i
].name
!= NULL
; i
++)
809 if (strcmp (function_key_array
[i
].name
, actname
) == 0)
810 return function_key_array
[i
].code
;
814 /* Put an integer to an infokey file.
815 Integers are stored as two bytes, low order first,
816 in radix INFOKEY_RADIX.
819 putint (int i
, FILE *fp
)
821 return fputc (i
% INFOKEY_RADIX
, fp
) != EOF
822 && fputc ((i
/ INFOKEY_RADIX
) % INFOKEY_RADIX
, fp
) != EOF
;
825 /* Write an entire section to an infokey file. If the section is
826 empty, simply omit it.
829 putsect (struct sect
*s
, int code
, FILE *fp
)
833 return fputc (code
, fp
) != EOF
834 && putint (s
->cur
, fp
)
835 && fwrite (s
->data
, s
->cur
, 1, fp
) == 1;
838 /* Write an entire infokey file, given an array containing its sections.
841 write_infokey_file (FILE *fp
, struct sect
*sections
)
843 /* Get rid of sections with no effect. */
844 if (sections
[info
].cur
== 1 && sections
[info
].data
[0] == 0)
845 sections
[info
].cur
= 0;
846 if (sections
[ea
].cur
== 1 && sections
[ea
].data
[0] == 0)
847 sections
[ea
].cur
= 0;
849 /* Write all parts of the file out in order (no lseeks),
850 checking for errors all the way. */
851 return fputc (INFOKEY_MAGIC_S0
, fp
) != EOF
852 && fputc (INFOKEY_MAGIC_S1
, fp
) != EOF
853 && fputc (INFOKEY_MAGIC_S2
, fp
) != EOF
854 && fputc (INFOKEY_MAGIC_S3
, fp
) != EOF
855 && fputs (VERSION
, fp
) != EOF
856 && fputc ('\0', fp
) != EOF
857 && putsect (§ions
[info
], INFOKEY_SECTION_INFO
, fp
)
858 && putsect (§ions
[ea
], INFOKEY_SECTION_EA
, fp
)
859 && putsect (§ions
[var
], INFOKEY_SECTION_VAR
, fp
)
860 && fputc (INFOKEY_MAGIC_E0
, fp
) != EOF
861 && fputc (INFOKEY_MAGIC_E1
, fp
) != EOF
862 && fputc (INFOKEY_MAGIC_E2
, fp
) != EOF
863 && fputc (INFOKEY_MAGIC_E3
, fp
) != EOF
;
867 /* Error handling. */
869 /* Give the user a "syntax error" message in the form
870 progname: "filename", line N: message
873 error_message (int error_code
, const char *fmt
,
874 const void *a1
, const void *a2
, const void *a3
, const void *a4
)
876 fprintf (stderr
, "%s: ", program_name
);
877 fprintf (stderr
, fmt
, a1
, a2
, a3
, a4
);
879 fprintf (stderr
, " - %s", strerror (error_code
));
880 fprintf (stderr
, "\n");
883 /* Give the user a generic error message in the form
887 syntax_error (const char *filename
,
888 unsigned int linenum
, const char *fmt
,
889 const void *a1
, const void *a2
, const void *a3
, const void *a4
)
891 fprintf (stderr
, "%s: ", program_name
);
892 fprintf (stderr
, _("\"%s\", line %u: "), filename
, linenum
);
893 fprintf (stderr
, fmt
, a1
, a2
, a3
, a4
);
894 fprintf (stderr
, "\n");
897 /* Produce a gentle rtfm. */
901 fprintf (stderr
, _("Try --help for more information.\n"));
904 /* Produce a scaled down description of the available options to Info. */
909 Usage: %s [OPTION]... [INPUT-FILE]\n\
911 Compile infokey source file to infokey file. Reads INPUT-FILE (default\n\
912 $HOME/.infokey) and writes compiled key file to (by default) $HOME/.info.\n\
915 --output FILE output to FILE instead of $HOME/.info\n\
916 --help display this help and exit.\n\
917 --version display version information and exit.\n\
921 Email bug reports to bug-texinfo@gnu.org,\n\
922 general questions and discussion to help-texinfo@gnu.org.\n\
923 Texinfo home page: http://www.gnu.org/software/texinfo/"));