Fix MD5sum.str () formatting bug
[cygwin-setup.git] / inilex.ll
blob86100ae2c3a7663840e826b0f07f2ce809f19726
1 %{
2 /*
3  * Copyright (c) 2000, Red Hat, Inc.
4  *
5  *     This program is free software; you can redistribute it and/or modify
6  *     it under the terms of the GNU General Public License as published by
7  *     the Free Software Foundation; either version 2 of the License, or
8  *     (at your option) any later version.
9  *
10  *     A copy of the GNU General Public License can be found at
11  *     http://www.gnu.org/
12  *
13  * Written by DJ Delorie <dj@cygnus.com>
14  * Maintained by Robert Collins <rbtcollins@hotmail.com>
15  *
16  */
18 /* tokenize the setup.ini files.  We parse a string which we've
19    previously downloaded.  The program must call ini_init() to specify
20    that string. */
22 #include "win32.h"
23 #include <string.h>
25 #include "ini.h"
26 #include "iniparse.hh"
27 #include "String++.h"
28 #include "IniParseFeedback.h"
29 #include "sha2.h"
31 #define YY_READ_BUF_SIZE 65536
32 #define YY_INPUT(buf,result,max_size) { result = ini_getchar(buf, max_size); }
34 static int ini_getchar(char *buf, int max_size);
35 static void ignore_line (void);
39 /*%option debug */
40 %option nounput
41 %option noyywrap
42 %option yylineno
43 %option never-interactive
45 %x descriptionstate
46 %x eolstate
48 STR     [!a-zA-Z0-9_./:\+~-]+
52 [0-9a-f]{32} {
53     yylval = (char *) new unsigned char[16];
54     memset (yylval, 0, 16);
55     int i, j;
56     unsigned char v1, v2;
57     for (i = 0, j = 0; i < 32; i += 2, ++j)
58       {
59         v1 = hexnibble((unsigned char) yytext[i+0]);
60         v2 = hexnibble((unsigned char) yytext[i+1]);
61         ((unsigned char *) yylval) [j] = nibbled1(v1, v2);
62       }
63     return MD5;
66 [0-9a-f]{128} {
67     yylval = (char *) new unsigned char[SHA512_DIGEST_LENGTH];
68     memset (yylval, 0, SHA512_DIGEST_LENGTH);
69     int i, j;
70     unsigned char v1, v2;
71     for (i = 0, j = 0; i < SHA512_BLOCK_LENGTH; i += 2, ++j)
72       {
73         v1 = hexnibble((unsigned char) yytext[i+0]);
74         v2 = hexnibble((unsigned char) yytext[i+1]);
75         ((unsigned char *) yylval) [j] = nibbled1(v1, v2);
76       }
77     return SHA512;
80 [a-zA-Z0-9_-]{86} {
81     /* base64url as defined in RFC4648 */
82     yylval = (char *) new unsigned char[SHA512_DIGEST_LENGTH];
83     memset (yylval, 0, SHA512_DIGEST_LENGTH);
84     int i, j;
85     unsigned char v1, v2, v3, v4;
86     for (i = 0, j = 0; i < 4*(SHA512_DIGEST_LENGTH/3); i += 4, j += 3)
87       {
88         v1 = b64url(((unsigned char) yytext[i+0]));
89         v2 = b64url(((unsigned char) yytext[i+1]));
90         v3 = b64url(((unsigned char) yytext[i+2]));
91         v4 = b64url(((unsigned char) yytext[i+3]));
92         ((unsigned char *) yylval) [j+0] = b64d1(v1, v2, v3, v4);
93         ((unsigned char *) yylval) [j+1] = b64d2(v1, v2, v3, v4);
94         ((unsigned char *) yylval) [j+2] = b64d3(v1, v2, v3, v4);
95       }
96     v1 = b64url((unsigned char) yytext[i+0]);
97     v2 = b64url((unsigned char) yytext[i+1]);
98     v3 = 0;
99     v4 = 0;
100     ((unsigned char *) yylval) [j+0] = b64d1(v1, v2, v3, v4);
101     return SHA512B64URL;
104 \"[^"]*\"               { yylval = new char [strlen (yytext+1) + 1];
105                           strcpy (yylval, yytext+1);
106                           yylval[strlen (yylval)-1] = 0;
107                           return STRING; }
109 "setup-timestamp:"      return SETUP_TIMESTAMP;
110 "setup-version:"        return SETUP_VERSION;
111 "arch:"                 return ARCH;
112 "release:"              return RELEASE;
113 "Package:"              return PACKAGENAME;
114 [vV]"ersion:"           return PACKAGEVERSION;
115 "install:"|"Filename:"  return INSTALL;
116 "source:"               return SOURCE;
117 "sdesc:"                return SDESC;
118 "ldesc:"                return LDESC;
119 "message:"              return MESSAGE;
120 "autodep:"              return AUTODEP;
121 "Description:"          BEGIN (descriptionstate); return DESCTAG;
122 "Size:"                 return FILESIZE;
123 "MD5sum:"               return MD5LINE;
124 "SHA512:"               return SHA512LINE;
125 "Installed-Size:"       return INSTALLEDSIZE;
126 "Maintainer:"           BEGIN (eolstate); return MAINTAINER;
127 "Architecture:"         return ARCHITECTURE;
128 "Source:"               return SOURCEPACKAGE;
129 "Binary:"               return BINARYPACKAGE;
130 "Build-Depends:"        return BUILDDEPENDS;
131 "Build-Depends-Indep:"  return BUILDDEPENDS; /* technicallyincorrect :[ */
132 "Standards-Version:"    return STANDARDSVERSION; 
133 "Format:"               return FORMAT;
134 "Directory:"            return DIRECTORY;
135 "Files:"                return FILES;
137 "category:"|"Section:"  return CATEGORY;
138 "Priority:"             return PRIORITY;
139 "requires:"             return REQUIRES;
140 "Depends:"              return DEPENDS;
141 "Pre-Depends:"          return PREDEPENDS;
142 "Recommends:"           return RECOMMENDS;
143 "Suggests:"             return SUGGESTS;
144 "Conflicts:"            return CONFLICTS;
145 "Replaces:"             return REPLACES;
146 "Provides:"             return PROVIDES;
148 "apath:"                return APATH;
149 "ppath:"                return PPATH;
151 "include-setup:"        return INCLUDE_SETUP;
153 "download-url:"         return DOWNLOAD_URL;
155 ^{STR}":"               ignore_line ();
157 "[curr]"                return T_CURR;
158 "[test]"                return T_TEST;
159 "[exp]"                 return T_TEST;
160 "[prev]"                return T_PREV;
162 "("                     return OPENBRACE;
163 ")"                     return CLOSEBRACE;
164 "["                     return OPENSQUARE;
165 "]"                     return CLOSESQUARE;
166 "<<"                    return LT;
167 ">>"                    return GT;
168 ">="                    return GTEQUAL;
169 "<="                    return LTEQUAL;
170 ">"                     return GT;
171 "<"                     return LT;
172 "="                     return EQUAL;
173 \,                      return COMMA;
174 "|"                     return OR;
175 "@"                     return AT;
177 {STR}"@"{STR}           { yylval = new char [strlen(yytext) + 1];
178                           strcpy (yylval, yytext);
179                           return EMAIL; }
180 {STR}                   { yylval = new char [strlen(yytext) + 1];
181                           strcpy (yylval, yytext);
182                           return STRING; }
184 [ \t\r]+                /* do nothing */;
186 ^"#".*\n                /* do nothing */;
187 <descriptionstate>[^\n]+        { yylval = new char [strlen(yytext) + 1];
188                                   strcpy (yylval, yytext);
189                                   return STRTOEOL; }
190 <descriptionstate>\n    { return NL; }
191 <descriptionstate>"\n"+ {BEGIN(INITIAL); return PARAGRAPH;}
192 <eolstate>[^\n]+        {return STRING; }
193 <eolstate>\n            {BEGIN(INITIAL); return NL; }
195 \n                      { return NL; }
196 .                       { return *yytext;}
200 #include "io_stream.h"
202 static io_stream *input_stream = 0;
203 extern IniDBBuilder *iniBuilder;
204 static IniParseFeedback *iniFeedback;
205 std::string current_ini_name, yyerror_messages;
206 int yyerror_count;
208 void
209 ini_init(io_stream *stream, IniDBBuilder *aBuilder, IniParseFeedback &aFeedback)
211   input_stream = stream;
212   iniBuilder = aBuilder;
213   iniFeedback = &aFeedback;
214   YY_FLUSH_BUFFER;
215   yylineno = 1;
216   yyerror_count = 0;
217   yyerror_messages.clear ();
220 static int
221 ini_getchar(char *buf, int max_size)
223   if (input_stream)
224     {
225       ssize_t len = input_stream->read (buf, max_size);
226       if (len < 1)
227         {
228           len = 0;
229           input_stream = 0;
230         }
231       else
232         iniFeedback->progress (input_stream->tell(), input_stream->get_size());
233       return len;
234     }
235   return 0;
238 static void
239 ignore_line ()
241   char c;
242   while ((c = yyinput ()))
243     {
244       if (c == EOF)
245         return;
246       if (c == '\n')
247         return;
248     }
252 yyerror (const std::string& s)
254   char tmp[16];
255   sprintf (tmp, "%d", yylineno - (!!YY_AT_BOL ()));
256   
257   std::string e = current_ini_name + " line " + tmp + ": " + s;
258   
259   if (!yyerror_messages.empty ())
260     yyerror_messages += "\n";
262   yyerror_messages += e;
263   // OutputDebugString (e.c_str ());
264   yyerror_count++;
265   /* TODO: is return 0 correct? */
266   return 0;