| 1 | /* Parse printf format string. |
| 2 | Copyright (C) 1999, 2002-2003, 2005, 2007 Free Software Foundation, Inc. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2, or (at your option) |
| 7 | any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License along |
| 15 | with this program; if not, write to the Free Software Foundation, |
| 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 17 | |
| 18 | #ifndef _PRINTF_PARSE_H |
| 19 | #define _PRINTF_PARSE_H |
| 20 | |
| 21 | /* This file can be parametrized with the following macros: |
| 22 | ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. |
| 23 | STATIC Set to 'static' to declare the function static. */ |
| 24 | |
| 25 | #include "printf-args.h" |
| 26 | |
| 27 | |
| 28 | /* Flags */ |
| 29 | #define FLAG_GROUP 1 /* ' flag */ |
| 30 | #define FLAG_LEFT 2 /* - flag */ |
| 31 | #define FLAG_SHOWSIGN 4 /* + flag */ |
| 32 | #define FLAG_SPACE 8 /* space flag */ |
| 33 | #define FLAG_ALT 16 /* # flag */ |
| 34 | #define FLAG_ZERO 32 |
| 35 | |
| 36 | /* arg_index value indicating that no argument is consumed. */ |
| 37 | #define ARG_NONE (~(size_t)0) |
| 38 | |
| 39 | /* xxx_directive: A parsed directive. |
| 40 | xxx_directives: A parsed format string. */ |
| 41 | |
| 42 | /* A parsed directive. */ |
| 43 | typedef struct |
| 44 | { |
| 45 | const char* dir_start; |
| 46 | const char* dir_end; |
| 47 | int flags; |
| 48 | const char* width_start; |
| 49 | const char* width_end; |
| 50 | size_t width_arg_index; |
| 51 | const char* precision_start; |
| 52 | const char* precision_end; |
| 53 | size_t precision_arg_index; |
| 54 | char conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */ |
| 55 | size_t arg_index; |
| 56 | } |
| 57 | char_directive; |
| 58 | |
| 59 | /* A parsed format string. */ |
| 60 | typedef struct |
| 61 | { |
| 62 | size_t count; |
| 63 | char_directive *dir; |
| 64 | size_t max_width_length; |
| 65 | size_t max_precision_length; |
| 66 | } |
| 67 | char_directives; |
| 68 | |
| 69 | #if ENABLE_UNISTDIO |
| 70 | |
| 71 | /* A parsed directive. */ |
| 72 | typedef struct |
| 73 | { |
| 74 | const uint8_t* dir_start; |
| 75 | const uint8_t* dir_end; |
| 76 | int flags; |
| 77 | const uint8_t* width_start; |
| 78 | const uint8_t* width_end; |
| 79 | size_t width_arg_index; |
| 80 | const uint8_t* precision_start; |
| 81 | const uint8_t* precision_end; |
| 82 | size_t precision_arg_index; |
| 83 | uint8_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */ |
| 84 | size_t arg_index; |
| 85 | } |
| 86 | u8_directive; |
| 87 | |
| 88 | /* A parsed format string. */ |
| 89 | typedef struct |
| 90 | { |
| 91 | size_t count; |
| 92 | u8_directive *dir; |
| 93 | size_t max_width_length; |
| 94 | size_t max_precision_length; |
| 95 | } |
| 96 | u8_directives; |
| 97 | |
| 98 | /* A parsed directive. */ |
| 99 | typedef struct |
| 100 | { |
| 101 | const uint16_t* dir_start; |
| 102 | const uint16_t* dir_end; |
| 103 | int flags; |
| 104 | const uint16_t* width_start; |
| 105 | const uint16_t* width_end; |
| 106 | size_t width_arg_index; |
| 107 | const uint16_t* precision_start; |
| 108 | const uint16_t* precision_end; |
| 109 | size_t precision_arg_index; |
| 110 | uint16_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */ |
| 111 | size_t arg_index; |
| 112 | } |
| 113 | u16_directive; |
| 114 | |
| 115 | /* A parsed format string. */ |
| 116 | typedef struct |
| 117 | { |
| 118 | size_t count; |
| 119 | u16_directive *dir; |
| 120 | size_t max_width_length; |
| 121 | size_t max_precision_length; |
| 122 | } |
| 123 | u16_directives; |
| 124 | |
| 125 | /* A parsed directive. */ |
| 126 | typedef struct |
| 127 | { |
| 128 | const uint32_t* dir_start; |
| 129 | const uint32_t* dir_end; |
| 130 | int flags; |
| 131 | const uint32_t* width_start; |
| 132 | const uint32_t* width_end; |
| 133 | size_t width_arg_index; |
| 134 | const uint32_t* precision_start; |
| 135 | const uint32_t* precision_end; |
| 136 | size_t precision_arg_index; |
| 137 | uint32_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */ |
| 138 | size_t arg_index; |
| 139 | } |
| 140 | u32_directive; |
| 141 | |
| 142 | /* A parsed format string. */ |
| 143 | typedef struct |
| 144 | { |
| 145 | size_t count; |
| 146 | u32_directive *dir; |
| 147 | size_t max_width_length; |
| 148 | size_t max_precision_length; |
| 149 | } |
| 150 | u32_directives; |
| 151 | |
| 152 | #endif |
| 153 | |
| 154 | |
| 155 | /* Parses the format string. Fills in the number N of directives, and fills |
| 156 | in directives[0], ..., directives[N-1], and sets directives[N].dir_start |
| 157 | to the end of the format string. Also fills in the arg_type fields of the |
| 158 | arguments and the needed count of arguments. */ |
| 159 | #if ENABLE_UNISTDIO |
| 160 | extern int |
| 161 | ulc_printf_parse (const char *format, char_directives *d, arguments *a); |
| 162 | extern int |
| 163 | u8_printf_parse (const uint8_t *format, u8_directives *d, arguments *a); |
| 164 | extern int |
| 165 | u16_printf_parse (const uint16_t *format, u16_directives *d, |
| 166 | arguments *a); |
| 167 | extern int |
| 168 | u32_printf_parse (const uint32_t *format, u32_directives *d, |
| 169 | arguments *a); |
| 170 | #else |
| 171 | # ifdef STATIC |
| 172 | STATIC |
| 173 | # else |
| 174 | extern |
| 175 | # endif |
| 176 | int printf_parse (const char *format, char_directives *d, arguments *a); |
| 177 | #endif |
| 178 | |
| 179 | #endif /* _PRINTF_PARSE_H */ |