2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
24 static buffer_t *buffer_merge(buffer_t *A, buffer_t **B)
31 static inline int to_control(int c) {
32 return (toupper((unsigned char)c) - 'A' + 1) & 0x7f;
35 static buffer_t *buffer_escape(buffer_t *buf, const char *s, ssize_t len)
43 buffer_addch(buf, to_control(s[1]));
44 buffer_add(buf, s + 2, len - 2);
49 int i = (hexval(s[1]) << 4) | hexval(s[2]);
52 buffer_add(buf, s + 3, len - 3);
58 case '0': case '1': case '2': case '3':
60 int i = (octval(s[0]) << 6) | (octval(s[1]) << 3) | octval(s[2]);
63 buffer_add(buf, s + 3, len - 3);
69 case 'e': buffer_addch(buf, '\e'); return buf;
70 case 'f': buffer_addch(buf, '\f'); return buf;
71 case 'n': buffer_addch(buf, '\n'); return buf;
72 case 'r': buffer_addch(buf, '\r'); return buf;
73 case 't': buffer_addch(buf, '\t'); return buf;
74 case 'v': buffer_addch(buf, '\v'); return buf;
80 buffer_add(buf, s, len);
87 %token_type { segment }
89 %extra_argument { struct rcstate *state }
91 /****************************************************************************/
93 /****************************************************************************/
95 escnl ::= BSLASH NL . { state->linenum++; }
99 sp ::= sp escnl SPACE .
101 non_nl ::= ATOM|SPACE|BQUOTE|BSLASH|SHARP|DOLLAR|DQUOTE|EQUAL|LBRACE|QUOTE|RBRACE .
103 non_nl_star ::= non_nl_star non_nl .
105 to_eol_aux ::= NL . { state->linenum++; }
106 to_eol_aux ::= SHARP non_nl_star NL . { state->linenum++; }
107 to_eol ::= to_eol_aux .
108 to_eol ::= sp to_eol_aux .
110 /****************************************************************************/
111 /* Our macro tokens */
112 /****************************************************************************/
114 /* '-quoted tokens */
115 %type sqtok { buffer_t* }
116 %destructor sqtok { buffer_delete(&$$); }
117 sqtok(Z) ::= . { Z = buffer_new(); }
118 sqtok(Z) ::= sqtok(A) escnl . { Z = A; }
119 sqtok(Z) ::= sqtok(A) BSLASH BSLASH|QUOTE(B) . { buffer_addch(Z = A, *B.s); }
120 sqtok(Z) ::= sqtok(A) BSLASH ATOM|SPACE|BQUOTE|SHARP|DOLLAR|DQUOTE|EQUAL|LBRACE|RBRACE(B) . {
121 buffer_addch(Z = A, '\\');
122 buffer_add(Z, B.s, B.len);
124 sqtok(Z) ::= sqtok(A) ATOM|SPACE|BQUOTE|SHARP|DOLLAR|DQUOTE|EQUAL|LBRACE|RBRACE(B) . {
125 buffer_add(Z = A, B.s, B.len);
128 /* "-quoted tokens */
129 %type dqtok { buffer_t* }
130 %destructor dqtok { buffer_delete(&$$); }
131 dqtok(Z) ::= . { Z = buffer_new(); }
132 dqtok(Z) ::= dqtok(A) escnl . { Z = A; }
133 dqtok(Z) ::= dqtok(A) BSLASH non_nl(B) . { Z = buffer_escape(A, B.s, B.len); }
134 dqtok(Z) ::= dqtok(A) ATOM|SPACE|SHARP|EQUAL|LBRACE|RBRACE(B) . {
135 buffer_add(Z = A, B.s, B.len);
137 dqtok(Z) ::= dqtok(A) DOLLAR ATOM(B) . {
142 dqtok(Z) ::= dqtok(A) DOLLAR LBRACE ATOM(B) RBRACE . {
148 /* unquoted tokens */
149 %type uqtok { buffer_t* }
150 %destructor uqtok { buffer_delete(&$$); }
151 uqtok(Z) ::= ATOM|EQUAL|LBRACE|RBRACE(A) . { buffer_add(Z = buffer_new(), A.s, A.len); }
152 uqtok(Z) ::= BSLASH non_nl(A) . { buffer_add(Z = buffer_new(), A.s, A.len); }
153 uqtok(Z) ::= QUOTE sqtok(A) QUOTE . { Z = A; }
154 uqtok(Z) ::= DQUOTE dqtok(A) DQUOTE . { Z = A; }
156 /* token accumulator */
157 %type token { buffer_t* }
158 %destructor token { buffer_delete(&$$); }
159 token(Z) ::= uqtok(A) . { Z = A; }
160 token(Z) ::= token(A) escnl . { Z = A; }
161 token(Z) ::= token(A) uqtok(B) . { Z = buffer_merge(A, &B); }
163 /****************************************************************************/
165 /****************************************************************************/
167 %type simple_list { string_list_t * }
168 %destructor simple_list { string_list_wipe(&$$); }
170 simple_list(L) ::= simple_list(A) sp token(B) . {
171 L = string_item_new();
172 L->data = buffer_unwrap(&B);
177 /****************************************************************************/
178 /* Entry point : the rc lines */
179 /****************************************************************************/
183 rclines ::= rclines rcline .
185 rcline ::= ALTERNATIVE_ORDER simple_list(L) to_eol . {
186 string_list_append(&AlternativeOrderList, string_list_rev(L));
188 rcline ::= AUTO_VIEW simple_list(L) to_eol . {
189 string_list_append(&AutoViewList, string_list_rev(L));
191 rcline ::= HDR_ORDER simple_list(L) to_eol . {
192 string_list_append(&HeaderOrderList, string_list_rev(L));
194 rcline ::= MIME_LOOKUP simple_list(L) to_eol . {
195 string_list_append(&MimeLookupList, string_list_rev(L));
199 /* vim: set indentexpr= cin: */