1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
@echo off
setlocal enabledelayedexpansion
set PACKAGE_NAME="telegram-cli"
set PACKAGE_TARNAME="telegram-cli"
set PACKAGE_VERSION="1.0"
set PACKAGE_STRING="!PACKAGE_TARNAME:~1,-1! %PACKAGE_VERSION:~1%"
set PACKAGE_BUGREPORT=""
set PACKAGE_URL=""
set SRC_FOLDER=..\..
set SOURCE_LIST=()
set AUTO_FOLDER=%SRC_FOLDER%\auto
set BIN_FOLDER=bin
set LIB_FOLDER=lib
set DEFAULT_OUT_FOLDER=build
set OUT_PATH=
set GEN_TOOL=generate.exe
set PARSER_TOOL=%SRC_FOLDER%\tl-parser\contrib\vs\build\Release\tl-parser.exe
set CONFIG_HEADER_IN=%SRC_FOLDER%\config.h.in
set CONFIG_HEADER=%SRC_FOLDER%\config.h
set /p SCHEME_TL=<%SRC_FOLDER%\scheme.tl
set SCHEME_TL=%SRC_FOLDER%\%SCHEME_TL%
set /p ENCRYPTED_SCHEME_TL=<%SRC_FOLDER%\encrypted_scheme.tl
set ENCRYPTED_SCHEME_TL=%SRC_FOLDER%\%ENCRYPTED_SCHEME_TL%
set BINLOG_TL=%SRC_FOLDER%\binlog.tl
set MTPROTO_TL=%SRC_FOLDER%\mtproto.tl
set APPEND_TL=%SRC_FOLDER%\append.tl
set CONSTANTS_HEADER=constants.h
::#################################################################################################################################################
:parse_args
if not "%1"=="" (
if "%1"=="-o" (
set OUT_PATH=%2
)
if "%1"=="--gen-config" (
set /A GEN_CONFIG=1
)
if "%1"=="--gen-scheme" (
set /A GEN_SCHEME=1
)
if "%1"=="-debug" (
set /A DEBUG=1
set PARSER_TOOL=%PARSER_TOOL:Release=Debug%
)
if "%1"=="--parser-path" (
set PARSER_TOOL=%2
)
if "%1"=="--help" (
echo TGL Config
echo Usage:
echo --gen-config generate config.h
echo --gen-scheme generate Telegram API scheme headers and source files
echo --parser-path path to tl-parser.exe
echo -debug debuge mode
exit /b 0
)
shift
goto :parse_args
)
if {%OUT_PATH%}=={} (
set OUT_PATH=%DEFAULT_OUT_FOLDER%
)
::Make output folder if not exists
if not exist %OUT_PATH% mkdir %OUT_PATH%
::Copy source files to the output folder
for %%i in %SOURCE_LIST% do (
if not exist %OUT_PATH%\%%i (
echo |set /p=Copying file: %%i
copy %%i %OUT_PATH%\%%i 1>NUL && (
echo Done
) || (
echo Failed
)
)
)
::Generate config.h
if defined GEN_CONFIG (
if not exist %CONFIG_HEADER% call :gen_config %CONFIG_HEADER%
)
::Generate Telegram API scheme source files
if defined GEN_SCHEME (
set /a ERR_ENC=0
::Check if tl-parser.exe exists
if not exist %PARSER_TOOL% (
echo Error: Cannot find %PARSER_TOOL%
exit /b 1
)
::Check if generate.exe exists
set "GEN_TOOL=%OUT_PATH%\%BIN_FOLDER%\%GEN_TOOL%"
if not exist !GEN_TOOL! (
echo Error: Cannot find !GEN_TOOL!
exit /b 1
)
::Make auto folder
if not exist %AUTO_FOLDER% mkdir %AUTO_FOLDER%
::Generate scheme.tl
echo |set /p=Generating scheme.tl
type %SCHEME_TL% %ENCRYPTED_SCHEME_TL% %ENCRYPTED_SCHEME% %BINLOG_TL% %MTPROTO_TL% %APPEND_TL% 2>NUL > %AUTO_FOLDER%\scheme.tl
echo Done
::Generate scheme2.tl
echo |set /p=Generating scheme2.tl
%PARSER_TOOL% -E %AUTO_FOLDER%\scheme.tl 2> %AUTO_FOLDER%\scheme2.tl
if not !ERRORLEVEL!==0 (
echo Failed
exit /b 1
) else (
echo Done
)
::Generate scheme.tlo
echo |set /p=Generating scheme.tlo
"%PARSER_TOOL%" -e "%AUTO_FOLDER%\scheme.tlo" %AUTO_FOLDER%\scheme.tl
if not !ERRORLEVEL!==0 (
echo Failed
exit /b 1
) else (
echo Done
)
::Generate constants.h
echo |set /p=Generating constants.h
call :gen_constants %AUTO_FOLDER%\scheme2.tl > %AUTO_FOLDER%\constants.h
if not !ERRORLEVEL!==0 (
echo Failed
exit /b 1
) else (
echo Done
)
::Generate auto-*.h
echo |set /p=Generating auto-skip.h
!GEN_TOOL! -g skip-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-skip.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-skip.h del /q /f %AUTO_FOLDER%\auto-skip.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-fetch.h
!GEN_TOOL! -g fetch-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-fetch.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-fetch.h del /q /f %AUTO_FOLDER%\auto-fetch.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-store.h
!GEN_TOOL! -g store-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-store.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-store.h del /q /f %AUTO_FOLDER%\auto-store.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-autocomplete.h
!GEN_TOOL! -g autocomplete-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-autocomplete.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-autocomplete.h del /q /f %AUTO_FOLDER%\auto-autocomplete.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-types.h
!GEN_TOOL! -g types-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-types.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-types.h del /q /f %AUTO_FOLDER%\auto-types.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-fetch-ds.h
!GEN_TOOL! -g fetch-ds-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-fetch-ds.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-fetch-ds.h del /q /f %AUTO_FOLDER%\auto-fetch-ds.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-free-ds.h
!GEN_TOOL! -g free-ds-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-free-ds.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-free-ds.h del /q /f %AUTO_FOLDER%\auto-free-ds.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-store-ds.h
!GEN_TOOL! -g store-ds-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-store-ds.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-store-ds.h del /q /f %AUTO_FOLDER%\auto-store-ds.h
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-print-ds.h
!GEN_TOOL! -g print-ds-header %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-print-ds.h
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-print-ds.h del /q /f %AUTO_FOLDER%\auto-print-ds.h
echo Failed
) else (
echo Done
)
::Generate auto-*.c
echo |set /p=Generating auto-skip.c
!GEN_TOOL! -g skip %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-skip.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-skip.c del /q /f %AUTO_FOLDER%\auto-skip.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-fetch.c
!GEN_TOOL! -g fetch %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-fetch.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-fetch.c del /q /f %AUTO_FOLDER%\auto-fetch.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-store.c
!GEN_TOOL! -g store %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-store.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-store.c del /q /f %AUTO_FOLDER%\auto-store.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-autocomplete.c
!GEN_TOOL! -g autocomplete %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-autocomplete.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-autocomplete.c del /q /f %AUTO_FOLDER%\auto-autocomplete.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-types.c
!GEN_TOOL! -g types %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-types.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-types.c del /q /f %AUTO_FOLDER%\auto-types.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-fetch-ds.c
!GEN_TOOL! -g fetch-ds %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-fetch-ds.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-fetch-ds.c del /q /f %AUTO_FOLDER%\auto-fetch-ds.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-free-ds.c
!GEN_TOOL! -g free-ds %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-free-ds.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-free-ds.c del /q /f %AUTO_FOLDER%\auto-free-ds.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-store-ds.c
!GEN_TOOL! -g store-ds %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-store-ds.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-store-ds.c del /q /f %AUTO_FOLDER%\auto-store-ds.c
echo Failed
) else (
echo Done
)
echo |set /p=Generating auto-print-ds.c
!GEN_TOOL! -g print-ds %AUTO_FOLDER%\scheme.tlo > %AUTO_FOLDER%\auto-print-ds.c
if not !ERRORLEVEL!==0 (
set /a ERR_ENC=!ERRORLEVEL!
if exist %AUTO_FOLDER%\auto-print-ds.c del /q /f %AUTO_FOLDER%\auto-print-ds.c
echo Failed
) else (
echo Done
)
set /a ERRORLEVEL=!ERR_ENC!
)
::Script End
exit /b !ERRORLEVEL!
::#################################################################################################################################################
:: Script Functions
::#################################################################################################################################################
:gen_config <output folder>
:rem Copy config.h.in to the output folder
echo |set /p=Copying file: %CONFIG_HEADER_in%
copy %CONFIG_HEADER_IN% %1 1>NUL && (
echo Done
) || (
echo Failed
exit /B %ERRORLEVEL%
)
echo |set /p=Configurating file: config.h
call :find_and_replace %1 "#undef PACKAGE_BUGREPORT" "#define PACKAGE_BUGREPORT %PACKAGE_BUGREPORT%"
call :find_and_replace %1 "#undef PACKAGE_NAME" "#define PACKAGE_NAME %PACKAGE_NAME%"
call :find_and_replace %1 "#undef PACKAGE_STRING" "#define PACKAGE_STRING %PACKAGE_STRING%"
call :find_and_replace %1 "#undef PACKAGE_TARNAME" "#define PACKAGE_TARNAME %PACKAGE_TARNAME%"
call :find_and_replace %1 "#undef PACKAGE_URL" "#define PACKAGE_URL %PACKAGE_URL%"
call :find_and_replace %1 "#undef PACKAGE_VERSION" "#define PACKAGE_VERSION %PACKAGE_VERSION%"
call :find_and_replace %1 "#undef EVENT_V2" "#define EVENT_V2 1"
call :find_and_replace %1 "#undef HAVE_FCNTL_H" "#define HAVE_FCNTL_H 1"
call :find_and_replace %1 "#undef HAVE_INTTYPES_H" "#define HAVE_INTTYPES_H 1"
call :find_and_replace %1 "#undef HAVE_LIBM" "#define HAVE_LIBM 1"
call :find_and_replace %1 "#undef HAVE_LIBZ" "#define HAVE_LIBZ 1"
call :find_and_replace %1 "#undef HAVE_MALLOC_H" "#define HAVE_MALLOC_H 1"
call :find_and_replace %1 "#undef HAVE_MALLOC" "#define HAVE_MALLOC 1"
call :find_and_replace %1 "#undef HAVE_MEMMOVE" "#define HAVE_MEMMOVE 1"
call :find_and_replace %1 "#undef HAVE_MEMORY_H" "#define HAVE_MEMORY_H 1"
call :find_and_replace %1 "#undef HAVE_MEMSET" "#define HAVE_MEMSET 1"
call :find_and_replace %1 "#undef HAVE_MKDIR" "#define HAVE_MKDIR 1"
call :find_and_replace %1 "#undef HAVE_REALLOC" "#define HAVE_REALLOC 1"
call :find_and_replace %1 "#undef HAVE_SELECT" "#define HAVE_SELECT 1"
call :find_and_replace %1 "#undef HAVE_SOCKET" "#define HAVE_SOCKET 1"
call :find_and_replace %1 "#undef HAVE_STDINT_H" "#define HAVE_STDINT_H 1"
call :find_and_replace %1 "#undef HAVE_STDLIB_H" "#define HAVE_STDLIB_H 1"
call :find_and_replace %1 "#undef HAVE_STRDUP" "#define HAVE_STRDUP 1"
call :find_and_replace %1 "#undef HAVE_STRING_H" "#define HAVE_STRING_H 1"
call :find_and_replace %1 "#undef HAVE_STRNDUP" "#define HAVE_STRNDUP 1"
call :find_and_replace %1 "#undef HAVE_SYS_STAT_H" "#define HAVE_SYS_STAT_H 1"
call :find_and_replace %1 "#undef HAVE_SYS_TYPES_H" "#define HAVE_SYS_TYPES_H 1"
call :find_and_replace %1 "#undef STDC_HEADERS" "#define STDC_HEADERS 1"
call :find_and_replace %1 "#undef gid_t" "#define gid_t int"
call :find_and_replace %1 "#undef uid_t" "#define uid_t int"
call :find_and_replace %1 "#undef inline" "#define inline __inline"
call :find_and_replace %1 "#undef" "// #undef"
if %ERRORLEVEL% == 0 echo Done
if not %ERRORLEVEL% == 0 (
echo Failed
if exist %1\%CONFIG_HEADER% del /q /f %1\%CONFIG_HEADER%
exit /b 1
)
exit /B 0
:gen_constants <input file>
echo /*
echo This file is part of telegram-client.
echo.
echo Telegram-client is free software: you can redistribute it and/or modify
echo it under the terms of the GNU General Public License as published by
echo the Free Software Foundation, either version 2 of the License, or
echo (at your option) any later version.
echo.
echo Telegram-client is distributed in the hope that it will be useful,
echo but WITHOUT ANY WARRANTY; without even the implied warranty of
echo MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
echo GNU General Public License for more details.
echo.
echo You should have received a copy of the GNU General Public License
echo along with this telegram-client. If not, see ^<http://www.gnu.org/licenses/^>.
echo.
echo Copyright Vitaly Valtman 2013
echo */
echo #ifndef CONSTANTS_H
echo #define CONSTANTS_H
setlocal EnableDelayedExpansion
for /F "tokens=1" %%A in (%1) do (
call :split %%A # a
if %ERRORLEVEL% == 0 (
call :sub a[0]
if not "!h[%a[1]%]!"=="" (
echo ERROR: Duplicate magic %a[1]% for define %a[0]% and !h[%a[1]%]! 1>&2
exit /b 1
)
set h[!a[1]!]=!a[0]!
echo #define CODE_!a[0]! 0x!a[1]!
)
)
endlocal
echo #endif
exit /b0
:find_and_replace <file> <findString> <replaceString>
set file_path=%1
set find_str=%2
set "find_str=!find_str:~1,-1!"
set replace_str=%3 %4
set "replace_str=!replace_str:~1,-2!"
for /f "delims=" %%i in ('type "%file_path%" ^| find /n /v "" ^& break ^> "%file_path%" ') do (
set "line=%%i"
setlocal enableDelayedExpansion
set "line=!line:*]%find_str%=%replace_str%!"
set "line=!line:*]=!"
>>%file_path% echo(!line!
endlocal
)
exit /B %ERRORLEVEL%
:split <string> <delim> <resultVar>
for /F "tokens=1,2 delims=%2" %%a in ("%1") do (
if "%%a"=="" exit /b 1
set %3[0]=%%a
if "%%b"=="" exit /b 1
set %3[1]=%%b
)
exit /b0
:sub <string>
set /A len= 0
call :strlen %%1 len
(
setlocal EnableDelayedExpansion
set "substr="
for /l %%a IN (0,1,!len!) DO (
set Char=!%1:~%%a,1!
if "!Char!" EQU "A" SET Char=_a
if "!Char!" EQU "B" SET Char=_b
if "!Char!" EQU "C" SET Char=_c
if "!Char!" EQU "D" SET Char=_d
if "!Char!" EQU "E" SET Char=_e
if "!Char!" EQU "F" SET Char=_f
if "!Char!" EQU "G" SET Char=_g
if "!Char!" EQU "H" SET Char=_h
if "!Char!" EQU "I" SET Char=_i
if "!Char!" EQU "J" SET Char=_j
if "!Char!" EQU "K" SET Char=_k
if "!Char!" EQU "L" SET Char=_l
if "!Char!" EQU "M" SET Char=_m
if "!Char!" EQU "N" SET Char=_n
if "!Char!" EQU "O" SET Char=_o
if "!Char!" EQU "P" SET Char=_p
if "!Char!" EQU "Q" SET Char=_q
if "!Char!" EQU "R" SET Char=_r
if "!Char!" EQU "S" SET Char=_s
if "!Char!" EQU "T" SET Char=_t
if "!Char!" EQU "U" SET Char=_u
if "!Char!" EQU "V" SET Char=_v
if "!Char!" EQU "W" SET Char=_w
if "!Char!" EQU "X" SET Char=_x
if "!Char!" EQU "Y" SET Char=_y
if "!Char!" EQU "Z" SET Char=_z
if "!Char!" EQU "." SET Char=_
set substr=!substr!!Char!
)
)
(
endlocal
set "%~1=%substr%"
exit /b
)
:strlen <stringVar> <resultVar>
(
setlocal EnableDelayedExpansion
set "s=!%~1!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~2=%len%"
exit /b
)
|