├── LICENSE ├── Make.cmd ├── PRIVACY ├── auto_drop.c ├── docs ├── README.md ├── _config.yml ├── _layouts │ └── default.html ├── index.md ├── tinyTerm-0.png └── tinyTerm.mp4 ├── host.c ├── res ├── TL1.ico ├── resource.h ├── tiny.manifest └── tinyTerm.rc ├── scripts ├── README.md └── scp_to_folder.js ├── ssh2.c ├── term.c ├── tiny.c └── tiny.h /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Make.cmd: -------------------------------------------------------------------------------- 1 | rc res\tinyTerm.rc 2 | 3 | cl /c -O1 /GL /MT /DUNICODE /I../%Platform%/include %1 %2 %3 %4 %5 %6 %7 %8 %9 4 | 5 | link /LTCG /NXCOMPAT /DYNAMICBASE /NODEFAULTLIB:libucrt.lib ucrt.lib tiny.obj term.obj host.obj ssh2.obj auto_drop.obj res\tinyTerm.res user32.lib gdi32.lib comdlg32.lib comctl32.lib ole32.lib shell32.lib ws2_32.lib winmm.lib ntdll.lib bcrypt.lib crypt32.lib shlwapi.lib Advapi32.lib ../%Platform%/lib/libssh2.lib /out:tinyTerm_%Platform%.exe 6 | -------------------------------------------------------------------------------- /PRIVACY: -------------------------------------------------------------------------------- 1 | Privacy Policy 2 | 3 | tinyTerm does not collect any user data, except when logs are reqeusted for bug reports, 4 | debug logs are used for trouble shooting purpose only, data is never shared with external 5 | parties unless explicitly requested by the user. 6 | 7 | tinyTerm is hosted on GitHub and Microsoft Store, the use of those sites are governed by 8 | privacy policy of each site, author of tinyTerm does not track or store data of users 9 | except to fulfill the user requested actions, or to aid in fixing technical problems. 10 | 11 | -------------------------------------------------------------------------------- /auto_drop.c: -------------------------------------------------------------------------------- 1 | // 2 | // "$Id: auto_drop.c 17850 2020-06-27 23:05:10 $" 3 | // 4 | // tinyTerm -- A minimal serail/telnet/ssh/sftp terminal emulator 5 | // 6 | // auto_drop.c implements COM interfaces for 7 | // edit control autocompletion and text window drop target 8 | // 9 | // Copyright 2018-2019 by Yongchao Fan. 10 | // 11 | // This library is free software distributed under GNU GPL 3.0, 12 | // see the license at: 13 | // 14 | // https://github.com/yongchaofan/tinyTerm/blob/master/LICENSE 15 | // 16 | // Please report all bugs and problems on the following page: 17 | // 18 | // https://github.com/yongchaofan/tinyTerm/issues/new 19 | // 20 | #define INITGUID 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | // 28 | //impletemention of IEnumString interface, 29 | //provided to IAutoCompelte for command history 30 | // 31 | typedef struct CAutoEnumString { 32 | IEnumStringVtbl *lpVtbl; 33 | LPOLESTR * m_arString; 34 | unsigned int m_Size; 35 | unsigned int m_iterCur; 36 | unsigned int m_count; 37 | unsigned int m_rtrvCur; 38 | int m_Ref; 39 | } CAutoEnumString; 40 | 41 | void CAutoEnumString_Construct(CAutoEnumString* this); 42 | void CAutoEnumString_Destruct(CAutoEnumString* this); 43 | int CAutoEnumString_AddString(CAutoEnumString* this, LPOLESTR lpszStr); 44 | 45 | STDMETHODIMP_(ULONG) CAutoEnumString_AddRef(IEnumString *this) 46 | { 47 | CAutoEnumString *self = (CAutoEnumString *)this; 48 | return ++self->m_Ref; 49 | } 50 | 51 | STDMETHODIMP_(ULONG) CAutoEnumString_Release(IEnumString *this) 52 | { 53 | CAutoEnumString *self = (CAutoEnumString *)this; 54 | return --self->m_Ref; 55 | } 56 | 57 | STDMETHODIMP CAutoEnumString_QueryInterface(IEnumString *this, 58 | REFIID iid, void **ppv) 59 | { 60 | CAutoEnumString *self = (CAutoEnumString *)this; 61 | if ( IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumString) ){ 62 | *ppv = (IEnumString *) this ; 63 | self->m_Ref ++; 64 | } 65 | else{ 66 | *ppv = NULL; 67 | return E_NOINTERFACE ; 68 | } 69 | return S_OK; 70 | } 71 | 72 | STDMETHODIMP CAutoEnumString_Next(IEnumString *this, ULONG celt, 73 | LPOLESTR *rgelt, ULONG *pceltFetched) 74 | { 75 | CAutoEnumString *self = (CAutoEnumString *)this; 76 | if (rgelt == NULL || (celt != 1 && pceltFetched == NULL)) return E_POINTER; 77 | if (self->m_iterCur == self->m_count) return E_FAIL; 78 | 79 | ULONG nFetched = 0; 80 | while( self->m_iterCurm_count && celt>0) { 81 | (*rgelt) = (LPOLESTR)CoTaskMemAlloc(sizeof(OLECHAR)*256); 82 | if ( *rgelt ) { 83 | wcscpy(*rgelt, self->m_arString[self->m_iterCur++]); 84 | celt--; nFetched++; rgelt++; 85 | } 86 | else break; 87 | } 88 | if (pceltFetched != NULL) *pceltFetched = nFetched; 89 | 90 | return (celt>0)? S_FALSE:S_OK; 91 | } 92 | 93 | STDMETHODIMP CAutoEnumString_Skip(IEnumString *this, ULONG celt) 94 | { 95 | CAutoEnumString *self = (CAutoEnumString *)this; 96 | if (self->m_count - self->m_iterCur < celt) { 97 | self->m_iterCur = self->m_count; 98 | return S_FALSE; 99 | } 100 | self->m_iterCur += celt; 101 | return S_OK; 102 | } 103 | 104 | STDMETHODIMP CAutoEnumString_Reset(IEnumString *this) 105 | { 106 | CAutoEnumString *self = (CAutoEnumString *)this; 107 | self->m_iterCur = 0; 108 | return S_OK; 109 | } 110 | 111 | STDMETHODIMP CAutoEnumString_Clone(IEnumString *this, IEnumString** ppEnum) 112 | { 113 | return E_POINTER; 114 | } 115 | 116 | IEnumStringVtbl CAutoEnumStringVtbl = { 117 | CAutoEnumString_QueryInterface, 118 | &CAutoEnumString_AddRef, 119 | &CAutoEnumString_Release, 120 | CAutoEnumString_Next, 121 | CAutoEnumString_Skip, 122 | CAutoEnumString_Reset, 123 | CAutoEnumString_Clone, 124 | }; 125 | 126 | void CAutoEnumString_Construct(CAutoEnumString* this) 127 | { 128 | this->lpVtbl = &CAutoEnumStringVtbl; 129 | this->m_arString = (LPOLESTR *)malloc(256*sizeof(LPOLESTR)); 130 | this->m_Size = 256; 131 | this->m_iterCur = 0; 132 | this->m_count = 0; 133 | this->m_rtrvCur = 0; 134 | } 135 | void CAutoEnumString_Destruct(CAutoEnumString* this) 136 | { 137 | if ( this->m_count>0 ) 138 | for (int i=0; im_count; i++) free(this->m_arString[i]); 139 | if ( this->m_arString ) free(this->m_arString); 140 | } 141 | int CAutoEnumString_AddString(CAutoEnumString* this, LPOLESTR lpszStr) 142 | { 143 | int cur; 144 | for ( cur=0; curm_count; cur++ ) { 145 | int cmp = wcscmp(this->m_arString[cur], lpszStr); 146 | if ( cmp == 0 ) return 0; 147 | if ( cmp > 0 ) break; 148 | } 149 | if ( ++this->m_count==this->m_Size ) { 150 | int size = this->m_Size*2; 151 | LPOLESTR * arString = realloc(this->m_arString, size*sizeof(LPOLESTR)); 152 | if ( arString != NULL ) { 153 | this->m_arString=arString; 154 | this->m_Size=size; 155 | } 156 | else 157 | this->m_count--; 158 | } 159 | for ( int i=this->m_count-1; i>=cur; i-- ) 160 | this->m_arString[i+1] = this->m_arString[i]; 161 | this->m_arString[cur] = _wcsdup(lpszStr); 162 | this->m_rtrvCur = cur; 163 | 164 | return 1; 165 | } 166 | int CAutoEnumString_DelString(CAutoEnumString* this, LPOLESTR lpszStr) 167 | { 168 | for ( int cur=0; curm_count; cur++ ) { 169 | if ( wcscmp(this->m_arString[cur], lpszStr) == 0 ) { 170 | free(this->m_arString[cur]); 171 | this->m_arString[cur] = _wcsdup(L""); 172 | return this->m_rtrvCur = cur; 173 | } 174 | } 175 | return 0; 176 | } 177 | LPOLESTR CAutoEnumString_prevString(CAutoEnumString* this) 178 | { 179 | if ( this->m_count>0 && this->m_rtrvCur>0) 180 | return this->m_arString[this->m_rtrvCur--]; 181 | else 182 | return NULL; 183 | } 184 | LPOLESTR CAutoEnumString_firstString(CAutoEnumString* this) 185 | { 186 | this->m_rtrvCur = 0; 187 | if ( this->m_count>0 ) 188 | return this->m_arString[0]; 189 | else 190 | return NULL; 191 | } 192 | LPOLESTR CAutoEnumString_nextString(CAutoEnumString* this) 193 | { 194 | if ( this->m_rtrvCur < this->m_count-1 ) 195 | return this->m_arString[++this->m_rtrvCur]; 196 | else 197 | return NULL; 198 | } 199 | // 200 | // AutoComplete implementation 201 | // 202 | static CAutoEnumString cmdHistory; 203 | static IEnumString *penum =NULL; 204 | static IAutoComplete *pauto = NULL; 205 | 206 | void autocomplete_Init(HWND hwndCmd) 207 | { 208 | CoCreateInstance( &CLSID_AutoComplete, NULL, CLSCTX_INPROC_SERVER, 209 | &IID_IAutoComplete,(LPVOID *) &pauto ); 210 | 211 | if ( pauto ) { 212 | IAutoComplete2 *pauto2 = NULL; 213 | pauto->lpVtbl->QueryInterface(pauto,&IID_IAutoComplete2,(PVOID*)&pauto2); 214 | if ( pauto2 ) { 215 | pauto2->lpVtbl->SetOptions(pauto2, ACO_AUTOSUGGEST|ACO_AUTOAPPEND); 216 | pauto2->lpVtbl->Release(pauto2); 217 | } 218 | CAutoEnumString_Construct( &cmdHistory ); 219 | cmdHistory.lpVtbl->QueryInterface( (IEnumString *)&cmdHistory, 220 | &IID_IEnumString, (PVOID*)&penum); 221 | if ( penum ) 222 | pauto->lpVtbl->Init(pauto, hwndCmd, (IUnknown *)penum, NULL, NULL); 223 | } 224 | } 225 | int autocomplete_Add(LPOLESTR cmd) 226 | { 227 | if ( pauto && penum && cmd ) { 228 | if ( *cmd ) 229 | return CAutoEnumString_AddString(&cmdHistory, cmd); 230 | } 231 | return 0; 232 | } 233 | int autocomplete_Del(LPOLESTR cmd) 234 | { 235 | if ( pauto && penum && cmd ) { 236 | if ( *cmd ) 237 | return CAutoEnumString_DelString(&cmdHistory, cmd); 238 | } 239 | return 0; 240 | } 241 | LPOLESTR autocomplete_First( void ) 242 | { 243 | return CAutoEnumString_firstString(&cmdHistory); 244 | } 245 | LPOLESTR autocomplete_Prev( void ) 246 | { 247 | return CAutoEnumString_prevString(&cmdHistory); 248 | } 249 | LPOLESTR autocomplete_Next( void ) 250 | { 251 | return CAutoEnumString_nextString(&cmdHistory); 252 | } 253 | void autocomplete_Destroy() 254 | { 255 | if ( pauto ) pauto->lpVtbl->Release(pauto); 256 | if ( penum ) penum->lpVtbl->Release(penum); 257 | CAutoEnumString_Destruct(&cmdHistory); 258 | } 259 | 260 | // 261 | // DropTarget implementation 262 | // 263 | typedef struct { 264 | IDataObject ido; 265 | int ref_count; 266 | FORMATETC *m_pFormatEtc; 267 | STGMEDIUM *m_pStgMedium; 268 | LONG m_nNumFormats; 269 | LONG m_lRefCount; 270 | } WB_IDataObject; 271 | 272 | typedef struct { 273 | IEnumFORMATETC ief; 274 | int ref_count; 275 | int ix; 276 | LONG m_lRefCount; 277 | ULONG m_nIndex; 278 | ULONG m_nNumFormats; 279 | FORMATETC * m_pFormatEtc; 280 | } WB_IEnumFORMATETC; 281 | 282 | typedef struct { 283 | IDropSource ids; 284 | LONG m_lRefCount; 285 | } WB_IDropSource; 286 | 287 | typedef struct { 288 | IDropTarget idt; 289 | LONG m_lRefCount; 290 | HWND m_hWnd; 291 | BOOL m_fAllowDrop; 292 | IDataObject *m_pDataObject; 293 | } WB_IDropTarget; 294 | 295 | typedef struct WB_IDataObjectVtbl 296 | { 297 | BEGIN_INTERFACE 298 | 299 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )( 300 | WB_IDataObject __RPC_FAR * This, 301 | REFIID riid, 302 | void __RPC_FAR *__RPC_FAR *ppvObject // [iid_is][out] 303 | ); 304 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )( 305 | WB_IDataObject __RPC_FAR * This); 306 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )( 307 | WB_IDataObject __RPC_FAR * This); 308 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *GetData )( 309 | WB_IDataObject __RPC_FAR * This, 310 | FORMATETC __RPC_FAR *pformatetcIn, // [unique][in] 311 | STGMEDIUM __RPC_FAR *pmedium // [out] 312 | ); 313 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *GetDataHere )( 314 | WB_IDataObject __RPC_FAR * This, 315 | FORMATETC __RPC_FAR *pformatetc, // [unique][in] 316 | STGMEDIUM __RPC_FAR *pmedium // [out] 317 | ); 318 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryGetData )( 319 | WB_IDataObject __RPC_FAR * This, 320 | FORMATETC __RPC_FAR *pformatetc // [unique][in] 321 | ); 322 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *GetCanonicalFormatEtc )( 323 | WB_IDataObject __RPC_FAR * This, 324 | FORMATETC __RPC_FAR *pformatectIn, // [unique][in] 325 | FORMATETC __RPC_FAR *pformatetcOut // [out] 326 | ); 327 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *SetData )( 328 | WB_IDataObject __RPC_FAR * This, 329 | FORMATETC __RPC_FAR *pformatetc, // [unique][in] 330 | STGMEDIUM __RPC_FAR *pmedium, // [unique][in] 331 | BOOL fRelease 332 | ); 333 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *EnumFormatEtc )( 334 | WB_IDataObject __RPC_FAR * This, 335 | DWORD dwDirection, 336 | IEnumFORMATETC __RPC_FAR *__RPC_FAR *ppenumFormatEtc// [out] 337 | ); 338 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *DAdvise )( 339 | WB_IDataObject __RPC_FAR * This, 340 | FORMATETC __RPC_FAR *pformatetc, 341 | DWORD advf, 342 | IAdviseSink __RPC_FAR *pAdvSink, // [unique][in] 343 | DWORD __RPC_FAR *pdwConnection // [out] 344 | ); 345 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *DUnadvise )( 346 | WB_IDataObject __RPC_FAR * This, 347 | DWORD dwConnection 348 | ); 349 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *EnumDAdvise )( 350 | WB_IDataObject __RPC_FAR * This, 351 | IEnumSTATDATA __RPC_FAR *__RPC_FAR *ppenumAdvise // [out] 352 | ); 353 | 354 | END_INTERFACE 355 | } WB_IDataObjectVtbl; 356 | 357 | typedef struct WB_IEnumFORMATETCVtbl 358 | { 359 | BEGIN_INTERFACE 360 | 361 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )( 362 | WB_IEnumFORMATETC __RPC_FAR * This, 363 | REFIID riid, 364 | void __RPC_FAR *__RPC_FAR *ppvObject // [iid_is][out] 365 | ); 366 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )( 367 | WB_IEnumFORMATETC __RPC_FAR * This 368 | ); 369 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )( 370 | WB_IEnumFORMATETC __RPC_FAR * This 371 | ); 372 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Next )( 373 | WB_IEnumFORMATETC __RPC_FAR * This, 374 | ULONG celt, 375 | FORMATETC __RPC_FAR *rgelt, // [length_is][size_is][out] 376 | ULONG __RPC_FAR *pceltFetched // [out] 377 | ); 378 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Skip )( 379 | WB_IEnumFORMATETC __RPC_FAR * This, 380 | ULONG celt 381 | ); 382 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Reset )( 383 | WB_IEnumFORMATETC __RPC_FAR * This 384 | ); 385 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Clone )( 386 | WB_IEnumFORMATETC __RPC_FAR * This, 387 | WB_IEnumFORMATETC __RPC_FAR *__RPC_FAR *ppenum // [out] 388 | ); 389 | 390 | END_INTERFACE 391 | } WB_IEnumFORMATETCVtbl; 392 | 393 | typedef struct WB_IDropSourceVtbl 394 | { 395 | BEGIN_INTERFACE 396 | 397 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )( 398 | WB_IDropSource __RPC_FAR * This, 399 | REFIID riid, 400 | void __RPC_FAR *__RPC_FAR *ppvObject // [iid_is][out] 401 | ); 402 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )( 403 | WB_IDropSource __RPC_FAR * This 404 | ); 405 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )( 406 | WB_IDropSource __RPC_FAR * This 407 | ); 408 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryContinueDrag )( 409 | WB_IDropSource __RPC_FAR * This, 410 | BOOL fEscapePressed, 411 | DWORD grfKeyState 412 | ); 413 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *GiveFeedback )( 414 | WB_IDropSource __RPC_FAR * This, 415 | DWORD dwEffect 416 | ); 417 | 418 | END_INTERFACE 419 | } WB_IDropSourceVtbl; 420 | 421 | typedef struct WB_IDropTargetVtbl 422 | { 423 | BEGIN_INTERFACE 424 | 425 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )( 426 | WB_IDropTarget __RPC_FAR * This, 427 | REFIID riid, 428 | void __RPC_FAR *__RPC_FAR *ppvObject // [iid_is][out] 429 | ); 430 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )( 431 | WB_IDropTarget __RPC_FAR * This 432 | ); 433 | ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )( 434 | WB_IDropTarget __RPC_FAR * This 435 | ); 436 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *DragEnter )( 437 | WB_IDropTarget __RPC_FAR * This, 438 | WB_IDataObject __RPC_FAR *pDataObj, // [unique][in] 439 | DWORD grfKeyState, 440 | POINTL pt, 441 | DWORD __RPC_FAR *pdwEffect // [out][in] 442 | ); 443 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *DragOver )( 444 | WB_IDropTarget __RPC_FAR * This, 445 | DWORD grfKeyState, 446 | POINTL pt, 447 | DWORD __RPC_FAR *pdwEffect // [out][in] 448 | ); 449 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *DragLeave )( 450 | WB_IDropTarget __RPC_FAR * This 451 | ); 452 | HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Drop )( 453 | WB_IDropTarget __RPC_FAR * This, 454 | IDataObject __RPC_FAR *pDataObj, // [unique][in] 455 | DWORD grfKeyState, 456 | POINTL pt, 457 | DWORD __RPC_FAR *pdwEffect // [out][in] 458 | ); 459 | 460 | END_INTERFACE 461 | } WB_IDropTargetVtbl; 462 | 463 | typedef WB_IDataObject* LPWBDATAOBJECT; 464 | typedef WB_IEnumFORMATETC* LPWBFORMATETC; 465 | WB_IEnumFORMATETC *WB_IEnumFORMATETC_new (UINT, FORMATETC *); 466 | 467 | // 468 | // DropEffect private helper routine 469 | // 470 | static DWORD DropEffect(DWORD grfKeyState, POINTL pt, DWORD dwAllowed) 471 | { 472 | DWORD dwEffect = 0; 473 | if(dwAllowed & DROPEFFECT_MOVE) dwEffect = DROPEFFECT_MOVE; 474 | if(dwAllowed & DROPEFFECT_COPY) dwEffect = DROPEFFECT_COPY; 475 | if ( grfKeyState & MK_SHIFT ) dwEffect = dwAllowed & DROPEFFECT_MOVE; 476 | if ( grfKeyState & MK_CONTROL ) dwEffect = dwAllowed & DROPEFFECT_COPY; 477 | 478 | return dwEffect; 479 | } 480 | 481 | // 482 | // IUnknown::AddRef 483 | // 484 | static ULONG STDMETHODCALLTYPE idroptarget_addref (WB_IDropTarget* This) 485 | { 486 | return InterlockedIncrement(&This->m_lRefCount); 487 | } 488 | 489 | // 490 | // IUnknown::QueryInterface 491 | // 492 | static HRESULT STDMETHODCALLTYPE 493 | idroptarget_queryinterface (WB_IDropTarget *This, 494 | REFIID riid, 495 | LPVOID *ppvObject) 496 | { 497 | *ppvObject = NULL; 498 | if ( IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDropTarget) ) 499 | { 500 | idroptarget_addref (This); 501 | *ppvObject = This; 502 | return S_OK; 503 | } 504 | else 505 | return E_NOINTERFACE; 506 | } 507 | // 508 | // IUnknown::Release 509 | // 510 | static ULONG STDMETHODCALLTYPE 511 | idroptarget_release (WB_IDropTarget* This) 512 | { 513 | LONG count = InterlockedDecrement(&This->m_lRefCount); 514 | 515 | if(count == 0) { 516 | LocalFree(This); 517 | return 0; 518 | } 519 | else 520 | return count; 521 | } 522 | // 523 | // IDropTarget::DragEnter 524 | // 525 | static HRESULT STDMETHODCALLTYPE idroptarget_dragenter( WB_IDropTarget *This, 526 | WB_IDataObject *pDataObject, 527 | DWORD grfKeyState, 528 | POINTL pt, DWORD *pdwEffect) 529 | { 530 | // does the dataobject contain data we want? 531 | FORMATETC fmtetc = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; 532 | // does the data object support CF_TEXT using a HGLOBAL? 533 | HRESULT rc=pDataObject->ido.lpVtbl->QueryGetData((LPDATAOBJECT)pDataObject, 534 | &fmtetc); 535 | This->m_fAllowDrop = rc==S_OK ? TRUE : FALSE; 536 | 537 | if(This->m_fAllowDrop) { 538 | // get the dropeffect based on keyboard state 539 | *pdwEffect = DropEffect(grfKeyState, pt, *pdwEffect); 540 | SetFocus(This->m_hWnd); 541 | } 542 | else { 543 | *pdwEffect = DROPEFFECT_NONE; 544 | } 545 | return S_OK; 546 | } 547 | 548 | // 549 | // IDropTarget::DragOver 550 | // 551 | static HRESULT STDMETHODCALLTYPE idroptarget_dragover( WB_IDropTarget* This, 552 | DWORD grfKeyState, 553 | POINTL pt, 554 | DWORD *pdwEffect ) 555 | { 556 | if(This->m_fAllowDrop) { 557 | *pdwEffect = DropEffect(grfKeyState, pt, *pdwEffect); 558 | } 559 | else { 560 | *pdwEffect = DROPEFFECT_NONE; 561 | } 562 | return S_OK; 563 | } 564 | 565 | // 566 | // IDropTarget::DragLeave 567 | // 568 | static HRESULT STDMETHODCALLTYPE idroptarget_dragleave( WB_IDropTarget* This ) 569 | { 570 | return S_OK; 571 | } 572 | 573 | // 574 | // IDropTarget::Drop 575 | // 576 | void ( *dropHandler )(char *); 577 | static HRESULT STDMETHODCALLTYPE idroptarget_drop( WB_IDropTarget* This, 578 | IDataObject * pDataObject, 579 | DWORD grfKeyState, 580 | POINTL pt, DWORD *pdwEffect) 581 | { 582 | *pdwEffect = DROPEFFECT_NONE; 583 | 584 | if(This->m_fAllowDrop) { 585 | *pdwEffect = DropEffect(grfKeyState, pt, *pdwEffect); 586 | FORMATETC fmtetc = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; 587 | // See if the dataobject contains any TEXT stored as a HGLOBAL 588 | if(pDataObject->lpVtbl->QueryGetData(pDataObject, &fmtetc) == S_OK) { 589 | STGMEDIUM stgmed; 590 | if(pDataObject->lpVtbl->GetData(pDataObject,&fmtetc,&stgmed)==S_OK) 591 | { 592 | PVOID data = GlobalLock(stgmed.hGlobal);//lock to access HGLOBAL 593 | dropHandler(strdup((char *)data)); 594 | GlobalUnlock(stgmed.hGlobal); 595 | ReleaseStgMedium(&stgmed); 596 | } 597 | } 598 | } 599 | return S_OK; 600 | } 601 | 602 | static WB_IDropTargetVtbl idt_vtbl = { 603 | idroptarget_queryinterface, 604 | idroptarget_addref, 605 | idroptarget_release, 606 | idroptarget_dragenter, 607 | idroptarget_dragover, 608 | idroptarget_dragleave, 609 | idroptarget_drop 610 | }; 611 | 612 | // 613 | // Constructor for the CDropTarget class 614 | // 615 | WB_IDropTarget * WB_IDropTarget_new(HWND hwnd) 616 | { 617 | WB_IDropTarget *result; 618 | 619 | result = (WB_IDropTarget *)LocalAlloc(LPTR, sizeof(WB_IDropTarget)); 620 | result->idt.lpVtbl = (IDropTargetVtbl*)&idt_vtbl; 621 | result->m_lRefCount = 1; 622 | result->m_hWnd = hwnd; 623 | result->m_fAllowDrop = FALSE; 624 | 625 | return result; 626 | } 627 | HRESULT CreateDropTarget(HWND hwnd, WB_IDropTarget **ppDropTarget) 628 | { 629 | if(ppDropTarget == 0) return E_INVALIDARG; 630 | *ppDropTarget = WB_IDropTarget_new(hwnd); 631 | return (*ppDropTarget) ? S_OK : E_OUTOFMEMORY; 632 | } 633 | 634 | void RegisterDropWindow(HWND hwnd, WB_IDropTarget **ppDropTarget) 635 | { 636 | WB_IDropTarget *pDropTarget; 637 | CreateDropTarget(hwnd, &pDropTarget); // acquire a strong lock 638 | CoLockObjectExternal((struct IUnknown*)pDropTarget, TRUE, FALSE); 639 | RegisterDragDrop(hwnd, (LPDROPTARGET)pDropTarget); 640 | *ppDropTarget = pDropTarget; 641 | } 642 | 643 | void UnregisterDropWindow(HWND hwnd, IDropTarget *pDropTarget) 644 | { 645 | RevokeDragDrop(hwnd); // remove the strong lock 646 | CoLockObjectExternal((struct IUnknown*)pDropTarget, FALSE, TRUE); 647 | pDropTarget->lpVtbl->Release(pDropTarget); // release our own reference 648 | } 649 | 650 | static IDropTarget *pDropTarget; 651 | void drop_Init( HWND hwnd, void (*handler)(char*) ) 652 | { 653 | dropHandler = handler; 654 | RegisterDropWindow(hwnd, (WB_IDropTarget **)&pDropTarget); 655 | } 656 | void drop_Destroy( HWND hwnd ) 657 | { 658 | UnregisterDropWindow(hwnd, pDropTarget); 659 | } -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # [tinyTerm](http://yongchaofan.github.io/tinyTerm) 2 | 3 | [![Build Status](https://travis-ci.org/pages-themes/minimal.svg?branch=master)](https://travis-ci.org/pages-themes/minimal) 4 | 5 | *Minimalist terminal emulator, designed by network engineer for network engineers, with unique features for effeciency and effectiveness when managing network devices like routers, switches, transponders and ROADMs through command line interface.* 6 | 7 | ![Thumbnail of minimal](tinyTerm-0.png) 8 | 9 | 10 | ## Project philosophy 11 | tinyTerm is intended to be small, simple and scriptable, x64 executable is only 244KB, source code consist of 6 files totaling 150KB, ~6000 sloc, libssh2 is the only external library required to build on Windows 7/10. 12 | 13 | User interface design is minimal, main menu shares title bar space, scrollbar hidden until user trys to scroll back, only one dialog for making connections, 14 | 15 | 16 | ## Building 17 | Makefiles are provided for building with MSYS2+MingW64/32, also a cmd file for building with Visual Studio building tools. 18 | 19 | Makefile building 32bit tinyTerm.exe for Windows 7/10 using wincng crypto 20 | Make.cmd command for Visual Studio 2019 build tool 21 | 22 | ### Librarys 23 | libssh2 using release 1.9.0 with pull request #397 for full support of WinCNG crypto functions 24 | ./configure --with-crypto=wincng 25 | make install 26 | 27 | mbedTLS Since Windows XP doesn't have WinCNG support, external crypto library has to be used, 28 | download mbedtls-2.7.9, "make WINDOWS_BUILD=1 no_test install", then build libssh2 29 | ./configure --with-crypto=mbedtls 30 | make install 31 | 32 | ### Soruce files 33 | tiny.h header file for all function definitions 34 | tiny.c winmain and UI functions 35 | term.c simple xterm compatible terminal implementation 36 | host.c serial and telnet host implementation, plus http/ftp/tftp servers 37 | ssh2.c ssh/sftp/netconf host implementation based on libssh2 38 | auto_drop.c COM wrapper for auto completion and drag&drop function 39 | 40 | 41 | ## Contributing 42 | Interested in contributing to tinyTerm? I'd love your help. tinyTerm is made an open source project for users and developers to contribute and make it better together. See [the CONTRIBUTING file](docs/CONTRIBUTING.md) for instructions on how to contribute. 43 | 44 | 45 | ## Roadmap 46 | See the [open issues](https://github.com/zoudaokou/tinyTerm/issues) for a list of proposed features (and known issues). 47 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | show_downloads: true 3 | github: 4 | store_url: https://www.microsoft.com/store/apps/9NXGN9LJTL05 5 | zip_url: https://github.com/yongchaofan/tinyTerm/releases/download/1.9.8/tinyTerm-1.9.8.zip 6 | description: simple, small and scriptable terminal emulator 7 | -------------------------------------------------------------------------------- /docs/_layouts/default.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {% if site.google_analytics %} 6 | 7 | 13 | {% endif %} 14 | 15 | 16 | {% seo %} 17 | 18 | 19 | 20 | 21 | 22 | 23 | Skip to the content. 24 | 25 | 35 | 36 |
37 | {{ content }} 38 | 39 | 45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | 3 | tinyTerm started as a simple telnet client used to work with telecom transport equipment through command line interface(TL1). Over time it has grown to a full function terminal emulator supporting serial, telnet, ssh, sftp and netconf connections with xterm emulation, plus unique features like: **command autocompletion**, **scp integration**, **batch automation**, and **scripting**. 4 | 5 | Windows 10 user should install from Microsoft Store, users on older versions of Windows, or prefer portable apps download exe files. 6 |

Stable release: 1.9.9

7 |

Download: Microsoft Store 8 | 32-bit: tinyTerm.exe 9 | 64-bit: tinyTerm64.exe

10 |

License: GPL 3.0

written in C with Win32 API and [libssh2](http://libssh2.org) 11 | 15 | 16 | --- 17 | 18 | ## Usage notes 19 | 20 | > ### Terminal Emulation 21 | > Each time a connection is made using the connect dialog, an entry will be added to the Term menu, simply select the menu entry to make the same connection again. 22 | > 23 | > When local edit is enabled, user is presented with a "tinyTerm >" prompt, simply type commands like "telnet 192.168.1.1" or "ssh admin@172.16.1.1" to make connection, or type commands like "ipconfig", "ping 192.168.1.1", "tracert google.com" to execute 24 | > 25 | > For serial connections, available serial ports will be auto detected and added to the ports dropdown list in connection dialog. 26 | > 27 | > for netconf connections, typing netconf messages is possible but not really practical, it's better to use a text or xml editor to compose the messages and then drag&drop to the terminal window. 28 | > 29 | > Press and drag left mouse button to select text, left double click to select a word, middle click to paste selected text without copying to clipboard, right click to get context menu for copy, paste, copy all and paste selection(i.e. middle click). 30 | > 31 | > Scroll buffer holds 16k lines of text, use pageup key or mouse wheel to scroll back, scrollbar will appear when scrolled back, and will hide when scrolled all the way down. 32 | > 33 | > ### Command Autocompletion 34 | > When local edit mode is enabled, key presses are not sent to remote host until "Enter" or "Tab" key is pressed, and the input is auto completed using command history, every command typed in local edit mode is added to command history to complete future inputs. Command history is saved to tinyTerm.hist at exit, then loaded into memory at the next start of tinyTerm. 35 | > 36 | > Command history file is saved as %USERPROFILE%\documents\tinyTerm\tinyTerm.hist by default, copy tinyTerm.hist to the same folder as tinyTerm.exe for portable use. Since the command history file is just a plain text file, user can edit the file outside of tinyTerm to put additional commands in the list for command auto-completion. For example put all command TL1 commands in the history list to use as a dictionary. 37 | > 38 | > Command history file is not only used for auto completion, user interface options like window size, font size and font face, local edit mode, transparency are saved in the history file on exit, and loaded from the history file at next start. Plus the added Term and Script menu entries are saved in the history file too, so they can be restored at sebsequent sessions. 39 | > 40 | > ### Batch Automation 41 | > To automate the execution of batch commands, simply drag and drop a list of commands from text editor, or select "Run..." from Script menu and select a text file with all the commands to be executed, tinyTerm send one command at a time, wait for prompt string before sending the next command, to avoid overflowing the receive buffer of the remote host or network device. 42 | > 43 | > Most command line interface system uses a prompt string to tell user it’s ready for the next command, for example "> " or "$ " used by Cisco routers. tinyTerm will auto detect the prompt string used by remote host when user is typing commands interactively, and use the detected prompt string during scripting. Additionally, prompt string can be set in the script using special command "!Prompt {str}", refer to appendix A for details and other special commands supported for scripting. 44 | > 45 | > ### SCP integration 46 | > When a SSH or SFTP session is established in tinyTerm, simply drag and drop files to the terminal window will cause those files to be transfered to remote host using SCP or SFTP put, remote files will be created in the current directory. 47 | > 48 | > To copy file from server to a local folder, select the filename in the terminal window, then chose "scp_to_folder.js" from script menu. 49 | > 50 | > ### xmodem support 51 | > When a SERIAL sission is established, simply drag and drop a file to the terminal window will cause the file been sent using xmodem protocol, only send function of the original xmodem protocol is supported, CRC optional, xmodem-1K is not supported. This is added to 1.1 release for to support bootstraping of MCUs on embeded system like Ardiuno 52 | > 53 | > ### FTPd/TFTPd/HTTPd 54 | > A built in FTP server can be used for simple file transfer tasks, like software download to network devices. Only one user name "tiny" is allowed to login, with password "term". For security, user session to the FTP server is timed out in 1 minute without action, and FTP server will time out in 15 minutes without active connection. 55 | > 56 | > Similarly a built in TFTP server can be used for file transfer with simpler devices like cable modems. TFTP server times out after 5 minutes. 57 | > 58 | > A built in HTTP server is started as soon as tinyTerm is started, for the first instance of tinyTerm running, HTTPd listens at 127.0.0.1:8080, the second instance listens at 127.0.0.1:8081, the third instance listens at 127.0.0.1:8082, so on and so forth. Since it's listening on 127.0.0.1 only, the HTTPd will only accept connections from local machine, for the purpose of scripting. 59 | > 60 | 61 | ___ 62 | 63 | ## Scripting interface 64 | 65 | The Built in HTTP server will accept GET request from local machine, which means any program running on the same machine, be it a browser or a javascript or any other script, can connect to tinyTerm and request either a file or the result of a command, 66 | 67 | for example: 68 | 69 | - http://127.0.0.1:8080/tinyTerm.html return tinyTerm.html from current working folder 70 | - http://127.0.0.1:8080/?ls%20-al return the result of "ls -al" from remote host 71 | - http://127.0.0.1:8080/?!Selection return current selected text from scroll back buffer 72 | 73 | Notice the "!" just before "Selection" in the last example, when a command is started with "!", it's being executed by tinyTerm instead of sent to remote host, There are about 30 tinyTerm commands supported for the purpose of making connections, setting options, sending commands, scp files, turning up ssh2 tunnels, see appendix for the list. 74 | 75 | The script scp_to_folder.js referenced in the trailer, is a perfect example of scripting tinyTerm 76 | 77 | ```js 78 | // Javascript to download a highlighted file via scp. 79 | var xml = new ActiveXObject("Microsoft.XMLHTTP"); 80 | var port = "8080/?"; 81 | if ( WScript.Arguments.length>0 ) port = WScript.Arguments(0)+"/?"; 82 | var filename = term("!Selection"); 83 | var objShell = new ActiveXObject("Shell.Application") 84 | var objFolder = objShell.BrowseForFolder(0, "Destination", 0x11, "") 85 | if ( objFolder ) term("!scp :"+filename+" "+objFolder.Self.path); 86 | 87 | function term( cmd ) 88 | { 89 | xml.Open ("GET", "http://127.0.0.1:"+port+cmd, false); 90 | xml.Send(); 91 | return xml.responseText; 92 | } 93 | ``` 94 | 95 | 96 | 97 | ## Appendix. list of supported tinyTerm commands: 98 | These commands can be used programatically for scripting or interactively in local edit mode. 99 | 100 | ### Connection 101 | !com3:9600,n,8,1 connect to serial port com3 with settings 9600,n,8,1 102 | !telnet 192.168.1.1 telnet to 192.168.1.1 103 | !ssh pi@piZero:2222 ssh to host piZero port 2222 with username pi 104 | !sftp -P 2222 jun01 sftp to host jun01 port 2222 105 | !netconf rtr1 netconf to port 830(default) of host rtr1 106 | !disconn disconnect from current connection 107 | !Find {string} search for {string} in scroll back buffer 108 | 109 | ### Automation 110 | !Clear set clear scroll back buffer 111 | !Prompt $%20 set command prompt to “$ “, for CLI script 112 | !Timeout 30 set time out to 30 seconds for CLI script 113 | !Wait 10 wait 10 seconds during execution of CLI script 114 | !Loop 5 repeat the list of commands 5 times 115 | !Waitfor 100% wait for “100%” from host during execution of CLI script 116 | !Log test.log start/stop logging with log file test.log 117 | 118 | ### Scripting 119 | !Disp test case #1 display “test case #1” in terminal window 120 | !Send exit send “exit” to host 121 | !Recv get all text received since last Send/RecV 122 | !Selection get current selected text 123 | 124 | ### Options 125 | ~TermSize 100x40 set terminal size to 100 cols x 40 rows 126 | ~Transparency 192 set window transparency level to 192/255 127 | ~LocalEdit Enable local edit 128 | ~FontFace Consolas set font face to “Consolas” 129 | ~FontSize 18 set font size to 18 130 | 131 | ### Extras 132 | !Ftpd c:/tmp start/stop ftp server using c:/tmp as root directory 133 | !Tftpd c:/tmp start/stop tftp server using c:/tmp as root directory 134 | 135 | !scp tt.txt :t1.txt secure copy local file tt.txt to remote host as t1.txt 136 | !scp :*.txt d:/ secure copy remote files *.txt to local d:/ 137 | 138 | !tun 127.0.0.1:2222 127.0.0.1:22 139 | start ssh2 tunnel from localhost port 2222 to remote host port 22 140 | !tun list all ssh2 tunnels 141 | !tun 3256 close ssh2 tunnel number 3256 142 | -------------------------------------------------------------------------------- /docs/tinyTerm-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yongchaofan/tinyTerm/a25433640dd526779d61a9be4497133b5eb4926b/docs/tinyTerm-0.png -------------------------------------------------------------------------------- /docs/tinyTerm.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yongchaofan/tinyTerm/a25433640dd526779d61a9be4497133b5eb4926b/docs/tinyTerm.mp4 -------------------------------------------------------------------------------- /host.c: -------------------------------------------------------------------------------- 1 | // 2 | // "$Id: host.c 31224 2020-08-23 15:05:10 $" 3 | // 4 | // tinyTerm -- A minimal serail/telnet/ssh/sftp terminal emulator 5 | // 6 | // host.c is the host communication implementation 7 | // serial communication is based on WIN32 API. 8 | // telnet communication is based on socket API 9 | // 10 | // Copyright 2018-2020 by Yongchao Fan. 11 | // 12 | // This library is free software distributed under GNU GPL 3.0, 13 | // see the license at: 14 | // 15 | // https://github.com/yongchaofan/tinyTerm/blob/master/LICENSE 16 | // 17 | // Please report all bugs and problems on the following page: 18 | // 19 | // https://github.com/yongchaofan/tinyTerm/issues/new 20 | // 21 | #include "tiny.h" 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | DWORD WINAPI stdio(void *pv); 28 | DWORD WINAPI serial(void *pv); 29 | DWORD WINAPI telnet(void *pv); 30 | DWORD WINAPI ssh(void *pv); 31 | DWORD WINAPI sftp(void *pv); 32 | void stdio_Close(HOST *ph); 33 | 34 | void host_Construct(HOST *ph ) 35 | { 36 | ph->sock = 0; 37 | ph->type = NONE; 38 | ph->status=IDLE; 39 | ph->homedir[0]=0; 40 | ssh2_Construct(ph); 41 | } 42 | void host_Open(HOST *ph, char *port) 43 | { 44 | if (ph->status==IDLE ) 45 | { 46 | if ( port!=NULL ) 47 | { 48 | ph->cmdline[0] = 0; 49 | if ( strncmp(port, "netconf", 7)==0 ) 50 | strcpy(ph->cmdline, "ssh -s "); 51 | strncat(ph->cmdline, port, 248); 52 | ph->cmdline[255] = 0; 53 | } 54 | port = ph->cmdline; 55 | 56 | LPTHREAD_START_ROUTINE reader = stdio; 57 | if ( strnicmp(port, "com",3)==0 ) 58 | reader = serial; 59 | else if ( strncmp(port, "telnet", 6)==0 ) { 60 | reader = telnet; 61 | } 62 | else if ( strncmp(port, "ssh", 3)==0 ) { 63 | reader = ssh; 64 | } 65 | else if ( strncmp(port, "sftp", 4)==0 ){ 66 | reader = sftp; 67 | } 68 | 69 | CreateThread(NULL, 0, reader, ph, 0, NULL); 70 | } 71 | else { 72 | if ( strnicmp(port, "disconn", 7)==0 ) host_Close(ph); 73 | } 74 | } 75 | void host_Send(HOST *ph, char *buf, int len) 76 | { 77 | DWORD dwWrite; 78 | switch (ph->type ) { 79 | case STDIO: if ( *buf==3 && len==1 ) //CTRL+C 80 | stdio_Close(ph); 81 | else 82 | WriteFile(ph->hStdioWrite, buf, len, &dwWrite, NULL); 83 | break; 84 | case SERIAL:WriteFile(ph->hSerial, buf, len, &dwWrite, NULL); break; 85 | case TELNET:if (ph->sock>0 ) send(ph->sock, buf, len, 0); break; 86 | case SSH: 87 | case SFTP: 88 | case NETCONF: ssh2_Send(ph, buf, len); break; 89 | default: if (ph->status!=IDLE ) ssh2_Send(ph, buf, len); 90 | } 91 | } 92 | void host_Send_Size(HOST *ph, int w, int h) 93 | { 94 | if (ph->type==SSH ) ssh2_Size(ph, w, h); 95 | } 96 | int host_Status(HOST *ph ) 97 | { 98 | return ph->status; 99 | } 100 | int host_Type(HOST *ph) 101 | { 102 | return ph->type; 103 | } 104 | void host_Close(HOST *ph) 105 | { 106 | switch (ph->type ) { 107 | case STDIO: stdio_Close(ph); break; 108 | case SERIAL:SetEvent(ph->hExitEvent); break; 109 | case TELNET:closesocket(ph->sock); break; 110 | case SFTP: sftp_Close(ph); break; 111 | case SSH: 112 | case NETCONF:ssh2_Close(ph); break; 113 | } 114 | } 115 | void host_Destory() 116 | { 117 | } 118 | 119 | /***************************Serial*****************************/ 120 | const char SOH = 0x01; 121 | const char STX = 0x02; 122 | const char EOT = 0x04; 123 | const char ACK = 0x06; 124 | const char NAK = 0x15; 125 | static unsigned char xmodem_buf[133]; 126 | static unsigned char xmodem_blk; 127 | static int xmodem_timeout; 128 | static FILE *xmodem_fp; 129 | static BOOL bXmodem, xmodem_crc, xmodem_started; 130 | void block_crc() 131 | { 132 | unsigned short crc = 0; 133 | for ( int i=3; i<131; i++ ) { 134 | crc = crc ^ xmodem_buf[i] << 8; 135 | for ( int j=0; j<8; j++ ) { 136 | if (crc & 0x8000) 137 | crc = crc << 1 ^ 0x1021; 138 | else 139 | crc = crc << 1; 140 | } 141 | } 142 | xmodem_buf[131] = (crc>>8) & 0xff; 143 | xmodem_buf[132] = crc & 0xff; 144 | } 145 | void xmodem_block(HOST *ph) 146 | { 147 | xmodem_buf[0] = SOH; 148 | xmodem_buf[1] = ++xmodem_blk; 149 | xmodem_buf[2] = 255-xmodem_blk; 150 | int cnt = fread( xmodem_buf+3, 1, 128, xmodem_fp); 151 | if ( cnt <= 0 ) { 152 | xmodem_buf[0] = EOT; 153 | fclose(xmodem_fp); 154 | } 155 | if ( cnt>0 && cnt<128 ) 156 | for ( int i=cnt+3; i<131; i++ ) xmodem_buf[i] = 0; 157 | if ( xmodem_crc ) { 158 | block_crc(); 159 | } 160 | else { 161 | unsigned char chksum = 0; 162 | for ( int i=3; i<131; i++ ) chksum += xmodem_buf[i]; 163 | xmodem_buf[131] = chksum; 164 | } 165 | } 166 | void xmodem_send(HOST *ph) 167 | { 168 | xmodem_started = TRUE; 169 | if ( xmodem_buf[0]==EOT ) 170 | host_Send(ph, (char *)xmodem_buf, 1); 171 | else 172 | host_Send(ph, (char *)xmodem_buf, xmodem_crc?133:132); 173 | } 174 | void xmodem_recv(HOST *ph, char op) 175 | { 176 | switch( op ) { 177 | case 0: //nothing received,resend every 10 seconds 178 | if ( ++xmodem_timeout%10000==0 && xmodem_started ) { 179 | xmodem_send(ph); 180 | term_Disp(ph->term, "R"); 181 | } 182 | if ( xmodem_timeout>60000 ) { //timeout after 60 seconds 183 | bXmodem = FALSE; 184 | fclose(xmodem_fp); 185 | term_Disp(ph->term, "Aborted\r\n"); 186 | } 187 | break; 188 | case 0x06: xmodem_timeout = 0; //ACK 189 | if ( xmodem_buf[0] == EOT ) { 190 | term_Disp(ph->term, "Completed\r\n"); 191 | bXmodem = FALSE; 192 | return; 193 | } 194 | xmodem_block(ph); 195 | xmodem_send(ph); 196 | if ( xmodem_blk==0 ) term_Disp(ph->term, "."); 197 | break; 198 | case 0x15: term_Disp(ph->term, "N"); //NAK 199 | xmodem_send(ph); 200 | break; 201 | case 'C': term_Disp(ph->term, "CRC"); //start CRC 202 | xmodem_crc = TRUE; 203 | block_crc(); 204 | xmodem_send(ph); 205 | break; 206 | } 207 | } 208 | void xmodem_init(HOST *ph, FILE *fp) 209 | { 210 | xmodem_fp = fp; 211 | bXmodem = TRUE; 212 | xmodem_crc = FALSE; 213 | xmodem_started = FALSE; 214 | xmodem_timeout = 0; 215 | xmodem_blk = 0; 216 | xmodem_block(ph); 217 | term_Disp(ph->term, "xmodem"); 218 | } 219 | DWORD WINAPI serial(void *pv) 220 | { 221 | HOST *ph = (HOST *)pv; 222 | char port[256] = "\\\\.\\"; 223 | strcat( port, ph->cmdline); 224 | 225 | char *p = strchr( port, ':'); 226 | if ( p!=NULL ) *p++ = 0; 227 | if ( p==NULL || *p==0 ) p = "9600,n,8,1"; 228 | 229 | ph->hSerial = CreateFileA( port, GENERIC_READ|GENERIC_WRITE, 230 | 0, NULL, OPEN_EXISTING, 0, NULL); 231 | if (ph->hSerial==INVALID_HANDLE_VALUE ) { 232 | term_Disp(ph->term, "Couldn't open serial port\r\n"); 233 | goto comm_close; 234 | } 235 | COMMTIMEOUTS timeouts = { 1, 0, 1, 0, 0 }; 236 | //ReadIntervalTimeout = 1 237 | //ReadTotalTimeoutMultiplier = 0 238 | //ReadTotalTimeoutConstant = 1 239 | //WriteTotalTimeoutMultiplier = 0 240 | //WriteTotalTimeoutConstant = 0 241 | if ( SetCommTimeouts(ph->hSerial, &timeouts)==0 ) { 242 | term_Disp(ph->term, "couldn't set serial port timeout\r\n"); 243 | CloseHandle(ph->hSerial); 244 | goto comm_close; 245 | } 246 | SetupComm(ph->hSerial, 4096, 1024); //comm buffer sizes 247 | 248 | DCB dcb; // comm port settings 249 | memset(&dcb, 0, sizeof(dcb)); 250 | dcb.DCBlength = sizeof(dcb); 251 | BuildCommDCBA(p, &dcb); 252 | if ( SetCommState(ph->hSerial, &dcb )==0 ) { 253 | term_Disp(ph->term, "Invalid serial port settings\r\n"); 254 | CloseHandle(ph->hSerial); 255 | goto comm_close; 256 | } 257 | 258 | ph->hostname = ph->cmdline; 259 | ph->type = SERIAL; 260 | ph->status=CONNECTED; 261 | bXmodem = FALSE; 262 | term_Title(ph->term, ph->cmdline); 263 | term_Disp(ph->term, "connected\r\n"); 264 | ph->hExitEvent = CreateEventA( NULL, TRUE, FALSE, "COM exit"); 265 | while ( WaitForSingleObject(ph->hExitEvent, 0) == WAIT_TIMEOUT ) { 266 | char buf[256]; 267 | DWORD dwCCH; 268 | if ( ReadFile(ph->hSerial, buf, 256, &dwCCH, NULL ) ) { 269 | if ( bXmodem ) { 270 | char op = 0; 271 | if ( dwCCH>0 ) op = buf[dwCCH-1]; 272 | xmodem_recv(ph, op); 273 | continue; 274 | } 275 | if ( dwCCH > 0 ) 276 | term_Parse(ph->term, buf, dwCCH); 277 | else 278 | Sleep(1);//give WriteFile a chance to complete 279 | } 280 | else 281 | if ( !ClearCommError(ph->hSerial, NULL, NULL) ) break; 282 | } 283 | term_Error(ph->term, "Disconnected"); 284 | CloseHandle(ph->hExitEvent); 285 | CloseHandle(ph->hSerial); 286 | ph->type = NONE; 287 | ph->status=IDLE; 288 | 289 | comm_close: 290 | term_Title(ph->term, ""); 291 | return 1; 292 | } 293 | 294 | /***************Telnet*******************************/ 295 | int host_tcp(HOST *ph ) 296 | { 297 | int rc = 0; 298 | struct addrinfo *ainfo; 299 | if ( getaddrinfo(ph->hostname, NULL, NULL, &ainfo)!=0 ) { 300 | term_Disp(ph->term, "invalid ip address or hostname\r\n"); 301 | return -1; 302 | } 303 | ((struct sockaddr_in *)(ainfo->ai_addr))->sin_port = htons(ph->port); 304 | 305 | ph->sock= socket(ainfo->ai_family, SOCK_STREAM, 0); 306 | term_Disp(ph->term, "Trying..."); 307 | if ( connect(ph->sock, ainfo->ai_addr, ainfo->ai_addrlen)==SOCKET_ERROR ) { 308 | switch ( WSAGetLastError() ) { 309 | case WSAEHOSTUNREACH: 310 | case WSAENETUNREACH: 311 | term_Error(ph->term, "host unreachable"); break; 312 | case WSAECONNRESET: 313 | term_Error(ph->term, "connection reset"); break; 314 | case WSAETIMEDOUT: 315 | term_Error(ph->term, "connection timeout");break; 316 | case WSAECONNREFUSED: 317 | term_Error(ph->term, "connection refused");break; 318 | default: 319 | term_Error(ph->term, "connection failure"); 320 | } 321 | closesocket(ph->sock); 322 | ph->sock = 0; 323 | rc = -1; 324 | } 325 | freeaddrinfo(ainfo); 326 | 327 | return rc; 328 | } 329 | DWORD WINAPI telnet(void *pv) 330 | { 331 | HOST *ph = (HOST *)pv; 332 | char port[256]; 333 | strcpy( port, ph->cmdline+7); 334 | ph->hostname = port; 335 | ph->port = 23; 336 | char *p=strchr(port, ':'); 337 | char *q=strrchr(port, ':'); 338 | if ( p!=NULL && p==q ) { 339 | *p++=0; 340 | ph->port=atoi(p); 341 | } 342 | 343 | ph->status=CONNECTING; 344 | term_Title(ph->term, ph->hostname); 345 | if ( host_tcp(ph)!=-1 ) 346 | { 347 | ph->type=TELNET; 348 | ph->status=CONNECTED; 349 | term_Disp(ph->term, "connected\r\n"); 350 | 351 | char buf[4096]; 352 | int cnt; 353 | while ( (cnt=recv(ph->sock, buf, 4096, 0)) > 0 ) { 354 | term_Parse(ph->term, buf, cnt); 355 | } 356 | closesocket(ph->sock); 357 | ph->sock = 0; 358 | 359 | ph->type = NONE; 360 | term_Error(ph->term, "Disconnected"); 361 | } 362 | 363 | ph->status=IDLE; 364 | term_Title(ph->term, ""); 365 | return 1; 366 | } 367 | 368 | /***************************STDIO*******************************/ 369 | static PROCESS_INFORMATION piStd; 370 | DWORD WINAPI stdio( void *pv) 371 | { 372 | HOST *ph = (HOST *)pv; 373 | HANDLE Stdin_Rd, Stdin_Wr ; 374 | HANDLE Stdout_Rd, Stdout_Wr, Stderr_Wr; 375 | memset(&piStd, 0, sizeof(PROCESS_INFORMATION)); 376 | 377 | SECURITY_ATTRIBUTES saAttr; 378 | saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 379 | saAttr.bInheritHandle = TRUE; //Set the bInheritHandle flag 380 | saAttr.lpSecurityDescriptor = NULL; //so pipe handles are inherited 381 | 382 | CreatePipe(&Stdout_Rd, &Stdout_Wr, &saAttr, 0);//pipe for child's STDOUT 383 | SetHandleInformation(Stdout_Rd, HANDLE_FLAG_INHERIT, 0); 384 | // Ensure the read handle to the pipe for STDOUT is not inherited 385 | CreatePipe(&Stdin_Rd, &Stdin_Wr, &saAttr, 0); //pipe for child's STDIN 386 | SetHandleInformation(Stdin_Wr, HANDLE_FLAG_INHERIT, 0); 387 | // Ensure the write handle to the pipe for STDIN is not inherited 388 | DuplicateHandle(GetCurrentProcess(),Stdout_Wr, 389 | GetCurrentProcess(),&Stderr_Wr,0, 390 | TRUE,DUPLICATE_SAME_ACCESS); 391 | DuplicateHandle(GetCurrentProcess(),Stdout_Rd, 392 | GetCurrentProcess(),&ph->hStdioRead,0, 393 | TRUE,DUPLICATE_SAME_ACCESS); 394 | DuplicateHandle(GetCurrentProcess(),Stdin_Wr, 395 | GetCurrentProcess(),&ph->hStdioWrite,0, 396 | TRUE,DUPLICATE_SAME_ACCESS); 397 | CloseHandle(Stdin_Wr); 398 | CloseHandle(Stdout_Rd); 399 | 400 | struct _STARTUPINFOA siStartInfo; 401 | memset(&siStartInfo, 0, sizeof(STARTUPINFO)); // Set STARTUPINFO 402 | siStartInfo.cb = sizeof(STARTUPINFO); 403 | siStartInfo.hStdError = Stderr_Wr; 404 | siStartInfo.hStdOutput = Stdout_Wr; 405 | siStartInfo.hStdInput = Stdin_Rd; 406 | siStartInfo.dwFlags |= STARTF_USESTDHANDLES; 407 | 408 | if ( CreateProcessA(NULL, // Create the child process. 409 | ph->cmdline, // command line 410 | NULL, // process security attributes 411 | NULL, // primary thread security attributes 412 | TRUE, // handles are inherited 413 | CREATE_NO_WINDOW,// creation flags 414 | NULL, // use parent's environment 415 | NULL, // use parent's current directory 416 | &siStartInfo, // STARTUPINFO pointer 417 | &piStd) ) { // receives PROCESS_INFORMATION 418 | CloseHandle(Stdin_Rd); 419 | CloseHandle(Stdout_Wr); 420 | CloseHandle(Stderr_Wr); 421 | 422 | ph->type = STDIO; 423 | ph->status=CONNECTED; 424 | while ( TRUE ) { 425 | DWORD dwCCH; 426 | char buf[4096]; 427 | if ( ReadFile(ph->hStdioRead, buf, 4096, &dwCCH, NULL) > 0 ) { 428 | if ( dwCCH > 0 ) 429 | term_Parse(ph->term, buf, dwCCH); 430 | else 431 | Sleep(1); 432 | } 433 | else 434 | break; 435 | } 436 | ph->status=IDLE; 437 | ph->type = NONE; 438 | } 439 | else 440 | term_Disp(ph->term, "Invalid command\r\n"); 441 | 442 | CloseHandle(ph->hStdioRead); 443 | CloseHandle(ph->hStdioWrite); 444 | term_Title(ph->term, ""); 445 | return 1; 446 | } 447 | void stdio_Close(HOST *ph) 448 | { 449 | if ( WaitForSingleObject(piStd.hProcess, 100)==WAIT_TIMEOUT ) 450 | TerminateProcess(piStd.hProcess,0); 451 | CloseHandle(piStd.hThread); 452 | CloseHandle(piStd.hProcess); 453 | } 454 | 455 | /**********************************HTTPd*******************************/ 456 | extern TERM *pt; 457 | const char *RFC1123FMT="%a, %d %b %Y %H:%M:%S GMT"; 458 | const char *exts[]={".txt", 459 | ".htm", ".html", 460 | ".js", 461 | ".jpg", ".jpeg", 462 | ".png", 463 | ".css" 464 | }; 465 | const char *mime[]={"text/plain", 466 | "text/html", "text/html", 467 | "text/javascript", 468 | "image/jpeg", "image/jpeg", 469 | "image/png", 470 | "text/css" 471 | }; 472 | const char HEADER[]="HTTP/1.1 200 Ok\nServer: tinyTerm\n\ 473 | Access-Control-Allow-Origin: *\nContent-Type: text/plain\n\ 474 | Cache-Control: no-cache\nContent-length: %d\n\n"; 475 | 476 | void httpFile( int s1, char *file) 477 | { 478 | char reply[4096], timebuf[128]; 479 | int len, i, j; 480 | time_t now; 481 | 482 | now = time(NULL); 483 | strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime( &now )); 484 | 485 | struct stat sb; 486 | if ( stat( file, &sb )==-1 || strstr(file, ".." )!=NULL ) { 487 | len=sprintf(reply, "HTTP/1.1 404 not found\nDate: %s\n", timebuf); 488 | len+=sprintf(reply+len, "Server: tinyTerm\nConnection: close\n"); 489 | len+=sprintf(reply+len, "Content-Type: text/html\nContent-Length: 14"); 490 | len+=sprintf(reply+len, "\n\nfile not found"); 491 | send(s1, reply, len, 0); 492 | return; 493 | } 494 | 495 | FILE *fp = fopen( file, "rb"); 496 | if ( fp!=NULL ) { 497 | len=sprintf(reply, "HTTP/1.1 200 Ok\nDate: %s\n", timebuf); 498 | len+=sprintf(reply+len, "Server: tinyTerm\nConnection: close\n"); 499 | 500 | const char *filext=file+strlen(file)-1; 501 | while ( *filext!='.' ) filext--; 502 | for ( i=0, j=0; j<8; j++ ) if ( strcmp(filext, exts[j])==0 ) i=j; 503 | len+=sprintf(reply+len, "Content-Type: %s\n", mime[i]); 504 | 505 | long filesize = sb.st_size; 506 | len+=sprintf(reply+len, "Content-Length: %ld\n", filesize); 507 | strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime( &sb.st_mtime )); 508 | len+=sprintf(reply+len, "Last-Modified: %s\n\n", timebuf); 509 | 510 | send(s1, reply, len, 0); 511 | while ( (len=fread(reply, 1, 4096, fp))>0 ) 512 | if ( send(s1, reply, len, 0)==-1) break; 513 | fclose(fp); 514 | } 515 | } 516 | int url_decode(char *url) 517 | { 518 | char *p = url, *q = url; 519 | while ( *p ) { 520 | if ( *p=='%' && isdigit(p[1]) ) { 521 | int a; 522 | sscanf( ++p, "%02x", &a); 523 | *(++p) = a; 524 | } 525 | *q++ = *p++; 526 | } 527 | *q = 0; 528 | return q-url; 529 | } 530 | 531 | DWORD WINAPI httpd( void *pv ) 532 | { 533 | char buf[4096], *cmd, *reply; 534 | struct sockaddr_in cltaddr; 535 | int addrsize=sizeof(cltaddr), cmdlen, replen; 536 | SOCKET http_s0 = *(SOCKET *)pv; 537 | 538 | while ( TRUE ) { 539 | SOCKET http_s1 = accept(http_s0, (struct sockaddr*)&cltaddr, &addrsize); 540 | if ( http_s1 == INVALID_SOCKET ) break; 541 | cmdlen=recv(http_s1, buf, 4095, 0); 542 | if ( cmdlen>0 ) { 543 | if ( strncmp(buf, "GET /", 5)!=0 ) {//TCP connection 544 | term_Send(pt, buf, cmdlen); 545 | do { 546 | if ( host_Status(pt->host)==CONNECTED ) { 547 | replen = term_Recv(pt, &reply); 548 | if ( replen > 0 ) send(http_s1, reply, replen, 0); 549 | } 550 | struct timeval tv = { 0, 100 }; //tv_sec=0, tv_usec=100 551 | FD_SET readset; 552 | FD_ZERO(&readset); 553 | FD_SET(http_s1, &readset); 554 | if ( select(1, &readset, NULL, NULL, &tv)>0 ) { 555 | cmdlen = recv(http_s1, buf, 4095, 0); 556 | if ( cmdlen>0 ) 557 | term_Send(pt, buf, cmdlen); 558 | } 559 | } while (cmdlen>0); 560 | } 561 | else { //HTTP connection 562 | do { 563 | buf[cmdlen] = 0; 564 | cmd = buf+5; 565 | char *p=strchr(cmd, ' '); 566 | if ( p!=NULL ) *p = 0; 567 | url_decode(cmd); 568 | 569 | if ( *cmd=='?' ) { //get CGI, cmd+1 points to command 570 | replen = term_Cmd(pt, cmd+1, &reply); 571 | int len = sprintf(buf, HEADER, replen); 572 | send(http_s1, buf, len, 0); 573 | if ( replen>0 ) send(http_s1, reply, replen, 0); 574 | } 575 | else { //get file, cmd points to filename 576 | httpFile(http_s1, cmd); 577 | } 578 | cmdlen = recv(http_s1, buf, 4095, 0); 579 | } while ( cmdlen>0); 580 | } 581 | } 582 | shutdown(http_s1, SD_SEND); 583 | closesocket(http_s1); 584 | } 585 | return 0; 586 | } 587 | int http_Svr(char *intf) 588 | { 589 | static SOCKET http_s0=INVALID_SOCKET; 590 | 591 | if ( http_s0 !=INVALID_SOCKET ) { 592 | closesocket(http_s0); 593 | http_s0 = INVALID_SOCKET; 594 | return 0; 595 | } 596 | 597 | http_s0 = socket(AF_INET, SOCK_STREAM, 0); 598 | if ( http_s0==INVALID_SOCKET ) return 0; 599 | 600 | struct sockaddr_in svraddr; 601 | int addrsize=sizeof(svraddr); 602 | memset(&svraddr, 0, addrsize); 603 | svraddr.sin_family=AF_INET; 604 | svraddr.sin_addr.s_addr=inet_addr(intf); 605 | int p; 606 | for ( p=8080; p<8099; p++ ) { 607 | svraddr.sin_port=htons(p); 608 | if ( bind(http_s0, (struct sockaddr*)&svraddr, addrsize)!=SOCKET_ERROR ) { 609 | if ( listen(http_s0, 1)!=SOCKET_ERROR){ 610 | DWORD dwThreadId; 611 | CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)httpd, 612 | &http_s0, 0, &dwThreadId); 613 | return p; 614 | } 615 | } 616 | } 617 | closesocket(http_s0); 618 | http_s0 = INVALID_SOCKET; 619 | return 0; 620 | } 621 | /**********************************FTPd*******************************/ 622 | static SOCKET ftp_s0 = INVALID_SOCKET; 623 | static SOCKET ftp_s1 = INVALID_SOCKET; 624 | int sock_select( SOCKET s, int secs ) 625 | { 626 | struct timeval tv = { 0, 0 }; 627 | tv.tv_sec = secs; 628 | FD_SET svrset; 629 | FD_ZERO(&svrset); 630 | FD_SET(s, &svrset); 631 | return select(1, &svrset, NULL, NULL, &tv); 632 | } 633 | void sock_send(char *reply) 634 | { 635 | send(ftp_s1, reply, strlen(reply), 0); 636 | } 637 | BOOL is_directory(char *root) 638 | { 639 | struct stat sb; 640 | if ( stat(root, &sb)==-1 ) { 641 | term_Print(pt, "FTPD: %s doesn't exist\n", root); 642 | return FALSE; 643 | } 644 | if ( (sb.st_mode&S_IFMT)!=S_IFDIR ) { 645 | term_Print(pt, "FTPD: %s is not a directory\n", root); 646 | return FALSE; 647 | } 648 | return TRUE; 649 | } 650 | DWORD WINAPI ftpd(LPVOID p) 651 | { 652 | unsigned long dwIp; 653 | unsigned short wPort; 654 | unsigned int c[6], ii[2]; 655 | char *param=0, szBuf[32768]; 656 | char fn[MAX_PATH], workDir[MAX_PATH], rootDir[MAX_PATH]; 657 | BOOL bPassive; 658 | 659 | SOCKET s2=-1, s3=-1; // s2 data connection, s3 data listen 660 | struct sockaddr_in svraddr, clientaddr; // for data connection 661 | int addrsize=sizeof(clientaddr); 662 | 663 | strcpy( rootDir, (char*)p); 664 | term_Print(pt, "FTPd started at %s\r\n", rootDir); 665 | 666 | int ret0, ret1; 667 | while( (ret0=sock_select(ftp_s0, 900)) == 1 ) { 668 | strcpy(workDir, "\\");; 669 | ftp_s1 = accept( ftp_s0, (struct sockaddr*)&clientaddr, &addrsize); 670 | if ( ftp_s1 ==INVALID_SOCKET ) continue; 671 | 672 | sock_send("220 Welcome\n"); 673 | getpeername(ftp_s1, (struct sockaddr *)&clientaddr, &addrsize); 674 | term_Print(pt, "FTPd: connected from %s\n", 675 | inet_ntoa(clientaddr.sin_addr)); 676 | 677 | FILE *fp; 678 | bPassive = FALSE; 679 | BOOL bUser=FALSE, bPass=FALSE; 680 | while ( (ret1=sock_select(ftp_s1, 300)) == 1 ) { 681 | int cnt=recv(ftp_s1, szBuf, 1024, 0); 682 | if ( cnt<=0 ) { 683 | term_Disp(pt, "FTPd: client disconnected\n"); 684 | break; 685 | } 686 | szBuf[cnt--]=0; 687 | term_Disp(pt, szBuf); 688 | while (szBuf[cnt]=='\r' || szBuf[cnt]=='\n' ) szBuf[cnt--]=0; 689 | if ( (param=strchr(szBuf, ' '))!=NULL ) 690 | *param++=0; 691 | else 692 | param = szBuf+cnt+1; 693 | 694 | // *** Process FTP commands *** 695 | if (stricmp("user", szBuf) == 0){ 696 | sock_send("331 Password required\n"); 697 | bUser = strncmp(param, "tiny", 4 )==0 ? TRUE : FALSE; 698 | continue; 699 | } 700 | if (stricmp("pass", szBuf) == 0){ 701 | bPass = bUser && strncmp(param, "term", 4)==0; 702 | sock_send(bPass ? "230 Logged in okay\n": 703 | "530 Login incorrect\n"); 704 | continue; 705 | } 706 | if ( !bPass ) { 707 | sock_send("530 Login required\n"); 708 | continue; 709 | } 710 | strcpy(fn, rootDir); 711 | strcat(fn, workDir); 712 | if ( workDir[strlen(workDir)-1]!='\\' ) strcat(fn, "\\"); 713 | strcat(fn, param); 714 | 715 | if (stricmp("syst", szBuf) ==0 ){ 716 | sock_send("215 tinyTerm ftpd\n"); 717 | } 718 | else if(stricmp("port", szBuf) == 0){ 719 | sscanf(param, "%d,%d,%d,%d,%d,%d", 720 | &c[0], &c[1], &c[2], &c[3], &c[4], &c[5]); 721 | ii[0] = c[1]+(c[0]<<8); 722 | ii[1] = c[3]+(c[2]<<8); 723 | wPort = c[5]+(c[4]<<8); 724 | dwIp = ii[1]+(ii[0]<<16); 725 | clientaddr.sin_addr.s_addr = htonl(dwIp); 726 | clientaddr.sin_port=htons(wPort); 727 | sock_send("200 PORT command successful\n"); 728 | } 729 | else if(stricmp("type", szBuf) == 0){ 730 | sock_send("200 Type can only be binary\n"); 731 | } 732 | else if(stricmp("pwd", szBuf) == 0 || stricmp("xpwd", szBuf) == 0){ 733 | sprintf( szBuf, "257 \"%s\" is current directory\n", workDir); 734 | sock_send(szBuf); 735 | } 736 | else if(stricmp("cwd", szBuf) == 0){ 737 | char *pPart, fullDir[MAX_PATH]; 738 | if ( GetFullPathNameA(fn, MAX_PATH, fullDir, &pPart)==0 ) { 739 | sock_send("550 Invalid path\n"); 740 | continue; 741 | } 742 | if ( strncmp(rootDir, fullDir, strlen(rootDir))!=0 ) { 743 | sock_send("550 Invalid path\n"); 744 | continue; 745 | } 746 | if ( !is_directory( fullDir ) ) { 747 | sock_send("550 No such directory\n"); 748 | continue; 749 | } 750 | sock_send("250 CWD sucessful\n"); 751 | strcpy(workDir, fullDir+strlen(rootDir)); 752 | } 753 | else if(stricmp("cdup", szBuf) == 0) { 754 | char *p = strrchr(workDir, '\\'); 755 | if( p!=NULL && p!=workDir ) { 756 | *p = 0; 757 | sock_send("250 CWD sucessful\n"); 758 | } 759 | else 760 | sock_send("550 Invalid path\n"); 761 | } 762 | else if(stricmp("pasv", szBuf)==0 || stricmp("epsv", szBuf)==0 ){ 763 | getsockname(ftp_s1, (struct sockaddr *)&svraddr, &addrsize); 764 | svraddr.sin_port = 0; 765 | s3 = socket(AF_INET, SOCK_STREAM, 0); 766 | bind(s3, (struct sockaddr *)&svraddr, addrsize); 767 | listen(s3, 1); 768 | 769 | getsockname(s3, (struct sockaddr *)&svraddr, &addrsize); 770 | dwIp = svraddr.sin_addr.s_addr; 771 | wPort = svraddr.sin_port; 772 | 773 | if ( *szBuf=='p' || *szBuf=='P' ) { 774 | ii[1]=HIWORD(dwIp); ii[0]=LOWORD(dwIp); 775 | c[1]=HIBYTE(ii[0]); c[0]=LOBYTE(ii[0]); 776 | c[3]=HIBYTE(ii[1]); c[2]=LOBYTE(ii[1]); 777 | c[5]=HIBYTE(wPort); c[4]=LOBYTE(wPort); 778 | sprintf(szBuf, "227 PASV Mode (%d,%d,%d,%d,%d,%d)\n", 779 | c[0], c[1], c[2], c[3], c[4], c[5]); 780 | } 781 | else sprintf(szBuf, "229 EPSV Mode (|||%d|)\n", ntohs(wPort)); 782 | sock_send(szBuf); 783 | bPassive=TRUE; 784 | } 785 | else if( stricmp("nlst", szBuf)==0 || stricmp("list", szBuf)==0 ){ 786 | struct _finddata_t ffblk; 787 | if ( *(param+strlen(param)-1)=='/') strcat(fn, "*.*"); 788 | if ( *param==0 ) strcat(fn, "\\*.*"); 789 | intptr_t hFile = _findfirst(fn, &ffblk); 790 | if ( hFile==-1 ) { 791 | sock_send("550 No such file or directory\n"); 792 | continue; 793 | } 794 | sock_send("150 Opening ASCII connection for list\n"); 795 | if ( bPassive ) { 796 | s2 = accept(s3, (struct sockaddr*)&clientaddr, &addrsize); 797 | } 798 | else { 799 | s2 = socket(AF_INET, SOCK_STREAM, 0); 800 | connect(s2, (struct sockaddr*)&clientaddr, 801 | sizeof(clientaddr)); 802 | } 803 | BOOL bNlst = szBuf[0]=='n' || szBuf[0]=='N'; 804 | do { 805 | if ( ffblk.name[0]=='.' ) continue; 806 | if ( bNlst ) { 807 | sprintf(szBuf, "%s\r\n", ffblk.name); 808 | } 809 | else { 810 | char buf[256]; 811 | strcpy(buf, ctime(&ffblk.time_write)); 812 | buf[24]=0; 813 | if ( ffblk.attrib&_A_SUBDIR ) 814 | sprintf(szBuf, "%s %s %s\r\n", buf+4, 815 | " ", ffblk.name); 816 | else 817 | sprintf(szBuf, "%s % 12ld %s\r\n", buf+4, 818 | ffblk.size, ffblk.name); 819 | } 820 | send(s2, szBuf, strlen(szBuf), 0); 821 | } while ( _findnext(hFile, &ffblk)==0); 822 | _findclose(hFile); 823 | sock_send("226 Transfer complete.\n"); 824 | closesocket(s2); 825 | } 826 | else if(stricmp("stor", szBuf) == 0){ 827 | fp = NULL; 828 | if ( strstr(param, ".." )==NULL ) 829 | fp = fopen_utf8(fn, "wb"); 830 | if(fp == NULL){ 831 | sock_send("550 Unable write file\n"); 832 | continue; 833 | } 834 | sock_send("150 Opening BINARY data connection\n"); 835 | if ( bPassive ) { 836 | s2 = accept(s3, (struct sockaddr*)&clientaddr, &addrsize); 837 | } 838 | else { 839 | s2 = socket(AF_INET, SOCK_STREAM, 0); 840 | connect(s2, (struct sockaddr *)&clientaddr, 841 | sizeof(clientaddr)); 842 | } 843 | unsigned long lSize = 0; 844 | unsigned int nLen=0, nCnt=0; 845 | do { 846 | nLen = recv(s2, szBuf, 32768, 0); 847 | if ( nLen>0 ) { 848 | lSize += nLen; 849 | fwrite(szBuf, nLen, 1, fp); 850 | } 851 | if ( ++nCnt==256 ) { 852 | term_Print(pt, "\r%lu bytes received", lSize); 853 | nCnt = 0; 854 | } 855 | }while ( nLen!=0); 856 | fclose(fp); 857 | term_Print(pt, "\r%lu bytes received\n", lSize); 858 | sock_send("226 Transfer complete\n"); 859 | closesocket(s2); 860 | } 861 | else if(stricmp("retr", szBuf) == 0){ 862 | fp = NULL; 863 | if ( strstr(param, ".." )==NULL ) 864 | fp = fopen_utf8(fn, "rb"); 865 | if(fp == NULL) { 866 | sock_send("550 Unable to read file\n"); 867 | continue; 868 | } 869 | sock_send("150 Opening BINARY data connection\n"); 870 | if ( bPassive ) { 871 | s2 = accept(s3, (struct sockaddr*)&clientaddr, &addrsize); 872 | } 873 | else { 874 | s2 = socket(AF_INET, SOCK_STREAM, 0); 875 | connect(s2, (struct sockaddr *)&clientaddr, 876 | sizeof(clientaddr)); 877 | } 878 | unsigned long lSize = 0; 879 | unsigned int nLen=0, nCnt=0; 880 | do { 881 | nLen = fread(szBuf, 1, 32768, fp); 882 | if ( send(s2, szBuf, nLen, 0) == 0) break; 883 | lSize += nLen; 884 | if ( ++nCnt==32 ) { 885 | term_Print(pt, "\r%lu bytes sent", lSize); 886 | nCnt = 0; 887 | } 888 | } 889 | while ( nLen==32768); 890 | fclose(fp); 891 | term_Print(pt, "\r%lu bytes sent\n", lSize); 892 | sock_send("226 Transfer complete\n"); 893 | closesocket(s2); 894 | } 895 | else if(stricmp("size", szBuf) == 0){ 896 | struct _finddata_t ffblk; 897 | intptr_t hFile = _findfirst(fn, &ffblk); 898 | if ( hFile!=-1 ) 899 | { 900 | sprintf(szBuf, "213 %lu\n", ffblk.size); 901 | sock_send(szBuf); 902 | _findclose(hFile); 903 | } 904 | else 905 | sock_send("550 No such file or directory\n"); 906 | } 907 | else if(stricmp("mdtm", szBuf) == 0) { 908 | struct _finddata_t ffblk; 909 | intptr_t hFile = _findfirst(fn, &ffblk); 910 | if ( hFile!=-1 ) 911 | { 912 | struct tm *t_mod = localtime( &ffblk.time_write); 913 | sprintf(szBuf, "213 %4d%02d%02d%02d%02d%02d\n", 914 | t_mod->tm_year+1900, t_mod->tm_mon+1, 915 | t_mod->tm_mday, t_mod->tm_hour, 916 | t_mod->tm_min, t_mod->tm_sec); 917 | sock_send(szBuf); 918 | _findclose(hFile); 919 | } 920 | else 921 | sock_send("550 No such file or directory\n"); 922 | } 923 | else if(stricmp("quit", szBuf) == 0){ 924 | sock_send("221 Bye!\n"); 925 | break; 926 | } 927 | else { 928 | sock_send("500 Command not supported\n"); 929 | } 930 | } 931 | if( ret1 == 0 ) { 932 | sock_send("500 Timeout\n"); 933 | term_Disp(pt, "FTPd: client timed out\n"); 934 | } 935 | closesocket(ftp_s1); 936 | } 937 | term_Disp(pt, ret0==0? "FTPd timed out\n" : "FTPd stopped\n"); 938 | closesocket(ftp_s0); 939 | ftp_s0 = INVALID_SOCKET; 940 | ftpd_quit(); 941 | return 0; 942 | } 943 | BOOL ftp_Svr(char *root) 944 | { 945 | if ( root==NULL ) { 946 | if ( ftp_s0 != INVALID_SOCKET ) { 947 | closesocket(ftp_s1); 948 | closesocket(ftp_s0); 949 | ftp_s0 = INVALID_SOCKET; 950 | } 951 | } 952 | else { 953 | if ( is_directory( root ) ) { 954 | struct sockaddr_in serveraddr; 955 | int addrsize=sizeof(serveraddr); 956 | ftp_s0 = socket(AF_INET, SOCK_STREAM, 0); 957 | memset(&serveraddr, 0, addrsize); 958 | serveraddr.sin_family=AF_INET; 959 | serveraddr.sin_addr.s_addr=ADDR_ANY; 960 | serveraddr.sin_port=htons(21); 961 | if ( bind(ftp_s0, (struct sockaddr*)&serveraddr, 962 | addrsize) != SOCKET_ERROR ) { 963 | if ( listen(ftp_s0, 1) != SOCKET_ERROR ) { 964 | DWORD dwId; 965 | CreateThread( NULL, 0, ftpd, (LPVOID)root, 0, &dwId); 966 | return TRUE; 967 | } 968 | } 969 | else 970 | term_Disp(pt, "FTPD: Couldn't bind to TCP port 21\n"); 971 | } 972 | } 973 | return FALSE; 974 | } 975 | 976 | static SOCKET tftp_s0=INVALID_SOCKET, tftp_s1; 977 | void tftp_Read( FILE *fp ) 978 | { 979 | char dataBuf[516], ackBuf[516]; 980 | unsigned short nCnt, nRetry=0; 981 | int nLen, len; 982 | 983 | nCnt=1; 984 | len = nLen = fread(dataBuf+4, 1, 512 , fp); 985 | do { 986 | dataBuf[0]=0; dataBuf[1]=3; 987 | dataBuf[2]=(nCnt>>8)&0xff; dataBuf[3]=nCnt&0xff; 988 | send(tftp_s1, dataBuf, nLen+4, 0); 989 | if ( nCnt%512==0 ) term_Disp(pt, "#"); 990 | if ( sock_select( tftp_s1, 5 ) == 1 ) { 991 | if ( recv(tftp_s1, ackBuf, 516, 0)==SOCKET_ERROR ) break; 992 | if ( ackBuf[1]==4 && ackBuf[2]==dataBuf[2] && 993 | ackBuf[3]==dataBuf[3]) { 994 | nRetry=0; 995 | nCnt++; 996 | len = nLen; 997 | nLen = fread(dataBuf+4, 1, 512 , fp); 998 | } 999 | else if ( ++nRetry==5 ) break; 1000 | } 1001 | else if ( ++nRetry==5 ) break; 1002 | } while ( len==512); 1003 | if ( nCnt>=512 ) term_Disp(pt, "\n"); 1004 | } 1005 | void tftp_Write( FILE *fp ) 1006 | { 1007 | char dataBuf[516], ackBuf[516]; 1008 | unsigned short ntmp, nCnt=0, nRetry=0; 1009 | int nLen=516; 1010 | while ( nLen > 0 ) { 1011 | ackBuf[0]=0; ackBuf[1]=4; 1012 | ackBuf[2]=(nCnt>>8)&0xff; ackBuf[3]=nCnt&0xff; 1013 | send(tftp_s1, ackBuf, 4, 0); 1014 | if ( nLen!=516 ) break; 1015 | if ( sock_select( tftp_s1, 5) == 1 ) { 1016 | nLen = recv(tftp_s1, dataBuf, 516, 0); 1017 | if ( nLen == SOCKET_ERROR ) break; 1018 | ntmp=(unsigned char)dataBuf[2]; 1019 | ntmp=(ntmp<<8)+(unsigned char)dataBuf[3]; 1020 | if ( dataBuf[1]==3 && ntmp==nCnt+1 ) { 1021 | fwrite(dataBuf+4, 1, nLen-4, fp); 1022 | nRetry=0; 1023 | nCnt++; 1024 | if ( nCnt%512==0 ) term_Disp(pt, "#"); 1025 | } 1026 | else if ( ++nRetry==5 ) break; 1027 | } 1028 | else if ( ++nRetry==5 ) break; 1029 | } 1030 | if ( nCnt>=512 ) term_Disp(pt, "\n"); 1031 | } 1032 | DWORD WINAPI tftpd(LPVOID p) 1033 | { 1034 | char dataBuf[516]; 1035 | struct sockaddr_in clientaddr; 1036 | int addrsize=sizeof(clientaddr); 1037 | 1038 | char rootDir[MAX_PATH], fn[MAX_PATH]; 1039 | strcpy( rootDir, (char *)p); 1040 | term_Print(pt, "TFTPd started at %s\r\n", rootDir); 1041 | strcat(rootDir, "\\"); 1042 | 1043 | int ret; 1044 | while ( (ret=sock_select( tftp_s0, 300 )) == 1 ) { 1045 | ret = recvfrom(tftp_s0, dataBuf, 516, 0, 1046 | (struct sockaddr *)&clientaddr, &addrsize); 1047 | if ( ret==SOCKET_ERROR ) break; 1048 | connect(tftp_s1, (struct sockaddr *)&clientaddr, addrsize); 1049 | if ( dataBuf[1]==1 || dataBuf[1]==2 ) { 1050 | BOOL bRead = dataBuf[1]==1; 1051 | term_Print(pt, "TFTPd: %cRQ %s from %s\n", bRead?'R':'W', 1052 | dataBuf+2, inet_ntoa(clientaddr.sin_addr)); 1053 | strcpy(fn, rootDir); 1054 | strcat(fn, dataBuf+2); 1055 | FILE *fp = NULL; 1056 | if ( strstr(fn, "..")==NULL ) 1057 | fp = fopen_utf8(fn, bRead?"rb":"wb"); 1058 | if ( fp == NULL ) { 1059 | dataBuf[3]=dataBuf[1]; dataBuf[0]=0; 1060 | dataBuf[1]=5; dataBuf[2]=0; 1061 | int len = sprintf( dataBuf+4, "Couldn't open %s", fn); 1062 | send(tftp_s1, dataBuf, len+4, 0); 1063 | } 1064 | else { 1065 | bRead ? tftp_Read(fp) : tftp_Write(fp); 1066 | fclose(fp); 1067 | } 1068 | } 1069 | } 1070 | term_Disp(pt, ret==0 ? "TFTPD timed out\n" : "TFTPd stopped\n"); 1071 | closesocket( tftp_s1); 1072 | closesocket(tftp_s0); 1073 | tftp_s0 = INVALID_SOCKET; 1074 | tftpd_quit(); 1075 | return 0; 1076 | } 1077 | BOOL tftp_Svr( char *root ) 1078 | { 1079 | struct sockaddr_in svraddr; 1080 | int addrsize=sizeof(svraddr); 1081 | 1082 | if ( root==NULL ) { 1083 | if ( tftp_s0 != INVALID_SOCKET ) { 1084 | closesocket(tftp_s1); 1085 | closesocket(tftp_s0); 1086 | tftp_s0 = INVALID_SOCKET; 1087 | } 1088 | } 1089 | else { 1090 | if ( is_directory(root) ) { 1091 | tftp_s0 = socket(AF_INET, SOCK_DGRAM, 0); 1092 | tftp_s1 = socket(AF_INET, SOCK_DGRAM, 0); 1093 | if ( tftp_s0==INVALID_SOCKET || tftp_s1==INVALID_SOCKET ) { 1094 | term_Disp(pt, "Couldn't create sockets for TFTPd\n"); 1095 | return FALSE; 1096 | } 1097 | memset(&svraddr, 0, addrsize); 1098 | svraddr.sin_family=AF_INET; 1099 | svraddr.sin_addr.s_addr=ADDR_ANY; 1100 | svraddr.sin_port=htons(69); 1101 | if ( bind(tftp_s0, (struct sockaddr*)&svraddr, 1102 | addrsize)!=SOCKET_ERROR ) { 1103 | svraddr.sin_port=0; 1104 | if ( bind(tftp_s1, (struct sockaddr *)&svraddr, 1105 | addrsize)!=SOCKET_ERROR ) { 1106 | DWORD dwId; 1107 | CreateThread(NULL, 0, tftpd, (LPVOID)root, 0, &dwId); 1108 | return TRUE; 1109 | } 1110 | } 1111 | else 1112 | term_Disp(pt, "Couldn't bind TFTPd port\n"); 1113 | } 1114 | } 1115 | return FALSE; 1116 | } -------------------------------------------------------------------------------- /res/TL1.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yongchaofan/tinyTerm/a25433640dd526779d61a9be4497133b5eb4926b/res/TL1.ico -------------------------------------------------------------------------------- /res/resource.h: -------------------------------------------------------------------------------- 1 | // 2 | // "$Id: resource.h 1459 2020-10-10 21:05:10 $" 3 | // 4 | // tinyTerm -- A minimal serail/telnet/ssh/sftp terminal emulator 5 | // 6 | // resource.h defines IDs for resources in tinyTerm.rc 7 | // 8 | // Copyright 2018-2019 by Yongchao Fan. 9 | // 10 | // This library is free software distributed under GNU GPL 3.0, 11 | // see the license at: 12 | // 13 | // https://github.com/yongchaofan/tinyTerm/blob/master/LICENSE 14 | // 15 | // Please report all bugs and problems on the following page: 16 | // 17 | // https://github.com/yongchaofan/tinyTerm/issues/new 18 | // 19 | #define ID_ABOUT 65 20 | #define ID_ECHO 66 21 | #define ID_CONNECT 67 //C 22 | #define ID_DISCONN 68 //D 23 | #define ID_EDIT 69 24 | #define ID_FONT 70 25 | #define ID_DELETE 71 26 | #define ID_MIDDLE 72 27 | #define ID_COPY 73 28 | #define ID_PASTE 74 29 | #define ID_SELALL 75 30 | #define ID_LOGG 76 //L 31 | #define ID_FTPD 77 32 | #define ID_TFTPD 78 33 | #define ID_OPTIONS 79 //O 34 | #define ID_PAUSE 80 //P 35 | #define ID_QUIT 81 //Q 36 | #define ID_RUN 82 //R 37 | #define ID_SCRIPT 83 //S 38 | #define ID_TERM 85 //T 39 | #define ID_TRANSP 84 40 | #define ID_PRIOR 86 41 | #define ID_NEXT 87 42 | #define ID_TAB 88 43 | 44 | #define IDICON_TL1 5129 45 | #define IDMENU_MAIN 5130 46 | #define IDACCEL_MAIN 5131 47 | #define IDD_CONNECT 5132 48 | #define IDD_SCRIPT 5133 49 | #define IDCONNECT 40001 50 | #define IDSTATIC 40002 51 | #define IDPROTO 40003 52 | #define IDPORT 40004 53 | #define IDHOST 40005 54 | #define IDSTATIC1 40006 55 | #define IDSTATIC2 40007 56 | #define IDSTATIC3 40008 57 | #define IDPAUSE 40011 58 | #define IDQUIT 40012 59 | -------------------------------------------------------------------------------- /res/tiny.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 19 | 20 | 21 | 22 | 23 | true/pm 24 | permonitorv2,permonitor 25 | 26 | 27 | -------------------------------------------------------------------------------- /res/tinyTerm.rc: -------------------------------------------------------------------------------- 1 | // 2 | // "$Id: tinyTerm.rc 3088 2021-09-01 19:05:10 $" 3 | // 4 | // tinyTerm -- A minimal serail/telnet/ssh/sftp terminal emulator 5 | // 6 | // tinyTerm.rc is the resource file defining dialog, icon and menus. 7 | // 8 | // Copyright 2018-2021 by Yongchao Fan. 9 | // 10 | // This library is free software distributed under GNU GPL 3.0, 11 | // see the license at: 12 | // 13 | // https://github.com/yongchaofan/tinyTerm/blob/master/LICENSE 14 | // 15 | // Please report all bugs and problems on the following page: 16 | // 17 | // https://github.com/yongchaofan/tinyTerm/issues/new 18 | // 19 | #pragma code_page(65001) 20 | #include 21 | #include 22 | #include "resource.h" 23 | 24 | IDICON_TL1 ICON "TL1.ico" 25 | 26 | IDMENU_MAIN MENU 27 | { 28 | POPUP "&Term" 29 | { 30 | MENUITEM "&Connect...", ID_CONNECT 31 | MENUITEM "&Logging...", ID_LOGG 32 | MENUITEM "&About tinyTerm", ID_ABOUT 33 | MENUITEM SEPARATOR 34 | MENUITEM "&FTPd...", ID_FTPD 35 | MENUITEM "&TFTPd...", ID_TFTPD 36 | } 37 | POPUP "&Script" 38 | { 39 | MENUITEM "&Run...", ID_RUN 40 | MENUITEM SEPARATOR 41 | } 42 | POPUP "&Options" 43 | { 44 | MENUITEM "&Font...", ID_FONT 45 | MENUITEM "Local &Edit\tAlt+E",ID_EDIT 46 | MENUITEM "&Tranparent", ID_TRANSP 47 | MENUITEM "Local Echo", ID_ECHO 48 | } 49 | POPUP "&Context" 50 | { 51 | MENUITEM "&Copy \tAlt+C",ID_COPY 52 | MENUITEM "&Paste \tAlt+V",ID_PASTE 53 | MENUITEM "select &All\tAlt+A",ID_SELALL 54 | MENUITEM "paste &Selection",ID_MIDDLE 55 | } 56 | } 57 | IDACCEL_MAIN ACCELERATORS 58 | { 59 | "a", ID_SELALL, ALT 60 | "c", ID_COPY, ALT 61 | "e", ID_EDIT, ALT 62 | "v", ID_PASTE, ALT 63 | "t", ID_TERM, ALT 64 | "s", ID_SCRIPT, ALT 65 | "o", ID_OPTIONS,ALT 66 | VK_TAB, ID_TAB, VIRTKEY 67 | VK_NEXT, ID_NEXT, VIRTKEY 68 | VK_PRIOR, ID_PRIOR, VIRTKEY 69 | VK_PAUSE, ID_PAUSE, VIRTKEY 70 | VK_DELETE,ID_DELETE,ALT, VIRTKEY 71 | } 72 | 73 | IDD_CONNECT DIALOG 45, 30, 150, 100 74 | CAPTION "Connect Dialog" 75 | { 76 | COMBOBOX IDPROTO, 60, 10, 70, 16, WS_TABSTOP|CBS_DROPDOWNLIST|CBS_HASSTRINGS 77 | LTEXT "Protocol:", IDSTATIC1, 4, 12, 50, 12, SS_RIGHT, WS_EX_LEFT 78 | COMBOBOX IDPORT, 60, 30, 70, 16, WS_TABSTOP|CBS_DROPDOWN|CBS_HASSTRINGS 79 | LTEXT "Port:", IDSTATIC2, 4, 32, 50, 12, SS_RIGHT, WS_EX_LEFT 80 | COMBOBOX IDHOST, 60, 50, 70, 16, WS_TABSTOP|CBS_DROPDOWN|CBS_HASSTRINGS |CBS_AUTOHSCROLL 81 | LTEXT "Host:", IDSTATIC, 4, 52, 50, 12, SS_RIGHT, WS_EX_LEFT 82 | DEFPUSHBUTTON "Connect",IDCONNECT, 80, 75, 50, 16, 0, WS_EX_LEFT 83 | PUSHBUTTON "Cancel", IDCANCEL, 20, 75, 50, 16, 0, WS_EX_LEFT 84 | } 85 | IDD_SCRIPT DIALOG 300, 10, 130, 32 86 | CAPTION "Script Control" 87 | { 88 | PUSHBUTTON "Pause", IDPAUSE, 10, 8, 50, 16, 0, WS_EX_LEFT 89 | PUSHBUTTON "Quit", IDQUIT, 70, 8, 50, 16, 0, WS_EX_LEFT 90 | } 91 | 1 VERSIONINFO 92 | FILEVERSION 1,9,9,0 93 | PRODUCTVERSION 1,9,9,0 94 | FILEOS VOS__WINDOWS32 95 | FILETYPE VFT_APP 96 | FILESUBTYPE VFT2_UNKNOWN 97 | FILEFLAGSMASK 0 98 | FILEFLAGS 0 99 | { 100 | BLOCK "StringFileInfo" 101 | { 102 | BLOCK "040904B0" 103 | { 104 | VALUE "FileDescription", "Tiny Terminal Emulator" 105 | VALUE "LegalCopyright", "©2018-2021 Yongchao Fan" 106 | VALUE "ProductName", "tinyTerm" 107 | VALUE "ProductVersion", "1.9.9.0" 108 | } 109 | } 110 | BLOCK "VarFileInfo" 111 | { 112 | VALUE "Translation", 0x0409, 0x04B0 113 | } 114 | } 115 | CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "tiny.manifest" -------------------------------------------------------------------------------- /scripts/README.md: -------------------------------------------------------------------------------- 1 | This folder holds collection of scripts created to extend function on tinyTerm 2 | first example is scp_to_folder.js 3 | -------------------------------------------------------------------------------- /scripts/scp_to_folder.js: -------------------------------------------------------------------------------- 1 | var xml = new ActiveXObject("Microsoft.XMLHTTP"); 2 | var port = "8080/?"; 3 | if ( WScript.Arguments.length>0 ) port = WScript.Arguments(0)+"/?"; 4 | var filename = term("!Selection"); 5 | var objShell = new ActiveXObject("Shell.Application"); 6 | var objFolder = objShell.BrowseForFolder(0,"Destination folder",0x11,""); 7 | if ( objFolder ) term("!scp :"+filename+" "+objFolder.Self.path); 8 | function term( cmd ) { 9 | xml.Open ("GET", "http://127.0.0.1:"+port+cmd, false); 10 | xml.Send(); 11 | return xml.responseText; 12 | } -------------------------------------------------------------------------------- /term.c: -------------------------------------------------------------------------------- 1 | // 2 | // "$Id: term.c 36942 2020-08-28 15:05:10 $" 3 | // 4 | // tinyTerm -- A minimal serail/telnet/ssh/sftp terminal emulator 5 | // 6 | // term.c is the minimal xterm implementation, only common ESCAPE 7 | // control sequences are supported for apps like top, vi etc. 8 | // 9 | // Copyright 2018-2020 by Yongchao Fan. 10 | // 11 | // This library is free software distributed under GNU GPL 3.0, 12 | // see the license at: 13 | // 14 | // https://github.com/yongchaofan/tinyTerm/blob/master/LICENSE 15 | // 16 | // Please report all bugs and problems on the following page: 17 | // 18 | // https://github.com/yongchaofan/tinyTerm/issues/new 19 | // 20 | #include "tiny.h" 21 | #define isUTF8c(x) (((x)&0xc0)==0x80) 22 | 23 | const unsigned char *vt100_Escape(TERM *pt, const unsigned char *sz, int cnt); 24 | const unsigned char *telnet_Options(TERM *pt, const unsigned char *p, int cnt); 25 | void term_Clear(TERM *pt) 26 | { 27 | memset(pt->buff, 0, BUFFERSIZE); 28 | memset(pt->attr, 0, BUFFERSIZE); 29 | memset(pt->line, 0, MAXLINES*sizeof(int)); 30 | pt->c_attr = 7; 31 | pt->cursor_y = pt->cursor_x = 0; 32 | pt->screen_y = 0; 33 | pt->sel_left = pt->sel_right = 0; 34 | pt->roll_top = 0; 35 | pt->roll_bot = pt->size_y-1; 36 | pt->bAlterScreen = FALSE; 37 | pt->bAppCursor = FALSE; 38 | pt->bBracket = FALSE; 39 | pt->bGraphic = FALSE; 40 | pt->bEscape = FALSE; 41 | pt->bInsert = FALSE; 42 | pt->bTitle = FALSE; 43 | pt->bOriginMode = FALSE; 44 | pt->bWraparound = TRUE; 45 | pt->bCursor = TRUE; 46 | pt->bPrompt = TRUE; 47 | pt->escape_idx = 0; 48 | pt->xmlIndent = 0; 49 | pt->xmlPreviousIsOpen = TRUE; 50 | memset(pt->tabstops, 0, 256); 51 | for ( int i=0; i<256; i+=8 ) pt->tabstops[i]=1; 52 | } 53 | BOOL term_Construct(TERM *pt) 54 | { 55 | pt->size_x=80; 56 | pt->size_y=25; 57 | pt->bLogging=FALSE; 58 | pt->bEcho=FALSE; 59 | pt->title_idx=0; 60 | strcpy(pt->sPrompt, "> "); 61 | pt->iPrompt=2; 62 | pt->iTimeOut=30; 63 | pt->tl1len=0; 64 | pt->tl1text=NULL; 65 | pt->mtx = CreateMutex(NULL, FALSE, L"term parse mutex"); 66 | 67 | pt->buff = (char *)malloc(BUFFERSIZE); 68 | pt->attr = (char *)malloc(BUFFERSIZE); 69 | pt->line = (int * )malloc(MAXLINES*sizeof(int)); 70 | 71 | if ( pt->buff!=NULL && pt->attr!=NULL && pt->line!=NULL ) { 72 | term_Clear(pt); 73 | return TRUE; 74 | } 75 | return FALSE; 76 | } 77 | void term_Destruct(TERM *pt) 78 | { 79 | free(pt->buff); 80 | free(pt->attr); 81 | free(pt->line); 82 | } 83 | void term_Size(TERM *pt, int x, int y) 84 | { 85 | pt->size_x = x; 86 | pt->size_y = y; 87 | pt->roll_top = 0; 88 | pt->roll_bot = pt->size_y-1; 89 | if ( !pt->bAlterScreen ) { 90 | pt->screen_y = max(0, pt->cursor_y-pt->size_y+1); 91 | } 92 | host_Send_Size(pt->host, pt->size_x, pt->size_y); 93 | tiny_Redraw(); 94 | } 95 | void term_nextLine(TERM *pt) 96 | { 97 | pt->line[++pt->cursor_y] = pt->cursor_x; 98 | if (pt->line[pt->cursor_y+1]cursor_x ) 99 | pt->line[pt->cursor_y+1]=pt->cursor_x; 100 | if (pt->screen_y==pt->cursor_y-pt->size_y ) pt->screen_y++; 101 | 102 | if (pt->cursor_x>=BUFFERSIZE-1024 || pt->cursor_y>=MAXLINES-4 ) { 103 | int i, len = pt->line[4096]; 104 | pt->tl1text -= len; 105 | if (pt->tl1textbuff ) { 106 | pt->tl1len -= pt->buff-pt->tl1text; 107 | pt->tl1text = pt->buff; 108 | } 109 | pt->cursor_x -= len; 110 | pt->cursor_y -= 4096; 111 | pt->screen_y -= 4096; 112 | if (pt->screen_y<0 ) pt->screen_y = 0; 113 | memmove(pt->buff, pt->buff+len, BUFFERSIZE-len); 114 | memset(pt->buff+pt->cursor_x, 0, BUFFERSIZE-pt->cursor_x); 115 | memmove(pt->attr, pt->attr+len, BUFFERSIZE-len); 116 | memset(pt->attr+pt->cursor_x, 0, BUFFERSIZE-pt->cursor_x); 117 | for ( i=0; icursor_y+2; i++ ) 118 | pt->line[i] = pt->line[i+4096]-len; 119 | while ( iline[i++] = 0; 120 | } 121 | } 122 | void term_Parse(TERM *pt, const char *buf, int len) 123 | { 124 | const unsigned char *p=(const unsigned char *)buf; 125 | const unsigned char *zz = p+len; 126 | 127 | if ( WaitForSingleObject(pt->mtx, INFINITE)!=WAIT_OBJECT_0 ) return; 128 | if (pt->bLogging ) fwrite( buf, 1, len, pt->fpLogFile); 129 | if (pt->bEscape ) p = vt100_Escape(pt, p, zz-p); 130 | while ( p < zz ) { 131 | unsigned char c = *p++; 132 | if (pt->bTitle ) { 133 | if ( c==0x07 ) { 134 | pt->bTitle = FALSE; 135 | pt->title[pt->title_idx]=0; 136 | tiny_Title(pt->title); 137 | } 138 | else 139 | if (pt->title_idx<63 ) 140 | pt->title[pt->title_idx++] = c; 141 | continue; 142 | } 143 | switch ( c ) { 144 | case 0x00: 145 | case 0x0e: 146 | case 0x0f: break; 147 | case 0x07: tiny_Beep();break; 148 | case 0x08: 149 | if (pt->cursor_x>pt->line[pt->cursor_y] ) { 150 | if ( isUTF8c(pt->buff[pt->cursor_x--]) )//utf8 continuation 151 | while ( isUTF8c(pt->buff[pt->cursor_x]) ) 152 | pt->cursor_x--; 153 | } 154 | break; 155 | case 0x09: { 156 | int l; 157 | do { 158 | pt->attr[pt->cursor_x] = pt->c_attr; 159 | pt->buff[pt->cursor_x++]=' '; 160 | l=pt->cursor_x-pt->line[pt->cursor_y]; 161 | } while ( lsize_x && pt->tabstops[l]==0); 162 | } 163 | break; 164 | case 0x0a: 165 | case 0x0b: 166 | case 0x0c: 167 | if (pt->bAlterScreen || pt->line[pt->cursor_y+2]!=0 ) { 168 | //IND to next line 169 | vt100_Escape(pt, (const unsigned char *)"D", 1); 170 | } 171 | else { //LF and new line 172 | pt->cursor_x = pt->line[pt->cursor_y+1]; 173 | pt->attr[pt->cursor_x] = pt->c_attr; 174 | pt->buff[pt->cursor_x++] = c; 175 | term_nextLine(pt); 176 | } 177 | break; 178 | case 0x0d: 179 | if (pt->cursor_x-pt->line[pt->cursor_y]==pt->size_x+1 && *p!=0x0a) 180 | term_nextLine(pt); //soft line feed 181 | else 182 | pt->cursor_x = pt->line[pt->cursor_y]; 183 | break; 184 | case 0x1b: p = vt100_Escape(pt, p, zz-p); break; 185 | case 0xff: p = telnet_Options(pt, p-1, zz-p+1); break; 186 | case 0xe2: 187 | if (pt->bAlterScreen ) { 188 | c = ' '; //hack utf8 box drawing 189 | if ( *p++==0x94 ) //to make alterscreen easier 190 | { 191 | switch ( *p ) { 192 | case 0x80: 193 | case 0xac: 194 | case 0xb4: 195 | case 0xbc: c='_'; break; 196 | case 0x82: 197 | case 0x94: 198 | case 0x98: 199 | case 0x9c: 200 | case 0xa4: c='|'; break; 201 | } 202 | } 203 | p++; 204 | }//fall through 205 | default: 206 | if (pt->bGraphic ) 207 | switch ( c ) { 208 | case 'q': c='_'; break; 209 | case 'x': 210 | case 't': 211 | case 'u': 212 | case 'm': 213 | case 'j': c='|'; break; 214 | case 'l': 215 | case 'k': 216 | default: c = ' '; 217 | } 218 | if (pt->bInsert ) 219 | vt100_Escape(pt, (const unsigned char *)"[1@", 3); 220 | if (pt->cursor_x-pt->line[pt->cursor_y]>=pt->size_x ) { 221 | int char_cnt=0; 222 | for ( int i=pt->line[pt->cursor_y]; icursor_x; i++ ) 223 | if ( !isUTF8c(pt->buff[i]) ) char_cnt++; 224 | if ( char_cnt==pt->size_x ) { 225 | if (pt->bWraparound )//pt->bAlterScreen 226 | term_nextLine(pt); 227 | else 228 | pt->cursor_x--; //don't overflow in vi 229 | } 230 | } 231 | pt->attr[pt->cursor_x] = pt->c_attr; 232 | pt->buff[pt->cursor_x++] = c; 233 | if (pt->line[pt->cursor_y+1]cursor_x ) 234 | pt->line[pt->cursor_y+1]=pt->cursor_x; 235 | } 236 | } 237 | 238 | if ( !pt->bPrompt && pt->cursor_x>pt->iPrompt ) { 239 | char *p=pt->buff+pt->cursor_x-pt->iPrompt; 240 | if ( strncmp(p, pt->sPrompt, pt->iPrompt)==0 ) pt->bPrompt=TRUE; 241 | pt->tl1len = pt->buff+pt->cursor_x - pt->tl1text; 242 | } 243 | tiny_Redraw(); 244 | ReleaseMutex(pt->mtx); 245 | } 246 | BOOL term_Echo(TERM *pt) 247 | { 248 | pt->bEcho=!pt->bEcho; 249 | return pt->bEcho; 250 | } 251 | void term_Error(TERM *pt, char *error) 252 | { 253 | term_Print(pt, "\r\n\033[31m%s! \033[37m", error); 254 | term_Disp(pt, "Press \033[33mEnter \033[37mto reconnect\r\n"); 255 | } 256 | void term_Title(TERM *pt, char *title) 257 | { 258 | switch ( host_Status(pt->host) ) { 259 | case IDLE: 260 | pt->title[0] = 0; 261 | pt->bEcho = FALSE; 262 | break; 263 | case CONNECTING: 264 | break; 265 | case CONNECTED: 266 | host_Send_Size(pt->host, pt->size_x, pt->size_y); 267 | if ( host_Type(pt->host)==NETCONF ) pt->bEcho = TRUE; 268 | break; 269 | } 270 | strncpy(pt->title, title, 63); 271 | pt->title[63] = 0; 272 | tiny_Title(pt->title); 273 | } 274 | void term_Scroll(TERM *pt, int lines) 275 | { 276 | if ( pt->bAlterScreen ) return; 277 | pt->screen_y -= lines; 278 | if (pt->screen_y<0 || pt->screen_y>pt->cursor_y ) { 279 | pt->screen_y += lines; 280 | return; 281 | } 282 | if ( tiny_Scroll(pt->screen_ycursor_y-pt->size_y+1, 283 | pt->cursor_y, pt->screen_y) ) 284 | pt->screen_y -= lines; //first pageup fix 285 | } 286 | void term_Mouse(TERM *pt, int evt, int x, int y) 287 | { 288 | switch( evt ) { 289 | case DOUBLECLK: 290 | y += pt->screen_y; 291 | pt->sel_left = pt->line[y]+x; 292 | pt->sel_right = pt->sel_left; 293 | while ( --pt->sel_left>pt->line[y] ) 294 | if (pt->buff[pt->sel_left]==0x0a 295 | || pt->buff[pt->sel_left]==0x20 ) { 296 | pt->sel_left++; 297 | break; 298 | } 299 | while ( ++pt->sel_rightline[y+1]) { 300 | if (pt->buff[pt->sel_right]==0x0a 301 | || pt->buff[pt->sel_right]==0x20 ) 302 | break; 303 | } 304 | break; 305 | case LEFTDOWN: 306 | y += pt->screen_y; 307 | pt->sel_left = min(pt->line[y]+x, pt->line[y+1]); 308 | while ( isUTF8c(pt->buff[pt->sel_left]) ) pt->sel_left--; 309 | pt->sel_right = pt->sel_left; 310 | break; 311 | case LEFTDRAG: 312 | if ( y<0 ) { 313 | pt->screen_y += y*2; 314 | if (pt->screen_y<0 ) pt->screen_y = 0; 315 | } 316 | if ( y>pt->size_y) { 317 | pt->screen_y += (y-pt->size_y)*2; 318 | if (pt->screen_y>pt->cursor_y ) pt->screen_y=pt->cursor_y; 319 | } 320 | y += pt->screen_y; 321 | pt->sel_right = min(pt->line[y]+x, pt->line[y+1]); 322 | while ( isUTF8c(pt->buff[pt->sel_right]) ) pt->sel_right++; 323 | break; 324 | case LEFTUP: 325 | if (pt->sel_right!=pt->sel_left ) { 326 | int sel_min = min(pt->sel_left, pt->sel_right); 327 | int sel_max = max(pt->sel_left, pt->sel_right); 328 | pt->sel_left = sel_min; 329 | pt->sel_right = sel_max; 330 | } 331 | else { 332 | pt->sel_left = pt->sel_right = 0; 333 | } 334 | break; 335 | case MIDDLEUP: 336 | if (pt->sel_left!=pt->sel_right ) 337 | term_Send(pt, pt->buff+pt->sel_left, pt->sel_right-pt->sel_left); 338 | break; 339 | } 340 | tiny_Redraw(); 341 | } 342 | void term_Print(TERM *pt, const char *fmt, ...) 343 | { 344 | char buff[4096]; 345 | va_list args; 346 | va_start(args, (char *)fmt); 347 | int len = vsnprintf(buff, 4096, (char *)fmt, args); 348 | va_end(args); 349 | term_Parse(pt, buff, len); 350 | term_Parse(pt, "\033[37m", 5); 351 | } 352 | void term_Disp(TERM *pt, const char *msg ) 353 | { 354 | pt->tl1text = pt->buff+pt->cursor_x; 355 | term_Parse(pt, msg, strlen(msg)); 356 | } 357 | void term_Send(TERM *pt, char *buf, int len) 358 | { 359 | if (pt->bEcho ) term_Parse(pt, buf, len); 360 | if ( host_Status(pt->host)!=IDLE ) 361 | host_Send(pt->host, buf, len); 362 | } 363 | void term_Paste(TERM *pt, char *buf, int len) 364 | { 365 | if (pt->bBracket ) term_Send(pt, "\033[200~", 6); 366 | term_Send(pt, buf, len); 367 | if (pt->bBracket ) term_Send(pt, "\033[201~", 6); 368 | } 369 | int term_Copy(TERM *pt, char **buf) 370 | { 371 | *buf = pt->buff+pt->sel_left; 372 | return pt->sel_right-pt->sel_left; 373 | } 374 | int term_Recv(TERM *pt, char **pTL1text) 375 | { 376 | if ( pTL1text!=NULL ) *pTL1text = pt->tl1text; 377 | int len = pt->buff+pt->cursor_x - pt->tl1text; 378 | pt->tl1text = pt->buff+pt->cursor_x; 379 | return len; 380 | } 381 | void term_Learn_Prompt(TERM *pt) 382 | {//capture prompt for scripting 383 | if (pt->cursor_x>1 ) { 384 | pt->sPrompt[0] = pt->buff[pt->cursor_x-2]; 385 | pt->sPrompt[1] = pt->buff[pt->cursor_x-1]; 386 | pt->sPrompt[2] = 0; 387 | pt->iPrompt = 2; 388 | } 389 | } 390 | char *term_Mark_Prompt(TERM *pt) 391 | { 392 | pt->bPrompt = FALSE; 393 | pt->tl1len = 0; 394 | pt->tl1text = pt->buff+pt->cursor_x; 395 | return pt->tl1text; 396 | } 397 | int term_Waitfor_Prompt(TERM *pt) 398 | { 399 | int oldlen = 0; 400 | for ( int i=0; iiTimeOut*10 && !pt->bPrompt; i++ ) { 401 | if (pt->tl1len>oldlen ) { i=0; oldlen=pt->tl1len; } 402 | Sleep(100); 403 | } 404 | pt->bPrompt = TRUE; 405 | return pt->tl1len; 406 | } 407 | void term_Logg(TERM *pt, char *fn) 408 | { 409 | if (pt->bLogging ) { 410 | fclose(pt->fpLogFile); 411 | pt->bLogging = FALSE; 412 | term_Print(pt, "\n\033[33m logging stopped\n"); 413 | } 414 | else if ( fn!=NULL ) { 415 | if ( *fn==' ' ) fn++; 416 | pt->fpLogFile = fopen_utf8( fn, "wb"); 417 | if (pt->fpLogFile != NULL ) { 418 | pt->bLogging = TRUE; 419 | term_Print(pt, "\n\033[33m%s logging started\n", fn); 420 | } 421 | } 422 | } 423 | 424 | int term_Srch(TERM *pt, char *sstr) 425 | { 426 | int l = strlen(sstr); 427 | char *p = pt->buff+pt->sel_left; 428 | if (pt->sel_left==pt->sel_right ) p = pt->buff+pt->cursor_x; 429 | while ( --p>=pt->buff+l ) { 430 | int i; 431 | for ( i=l-1; i>=0; i--) if ( sstr[i]!=p[i-l] ) break; 432 | if ( i==-1 ) { //found a match 433 | pt->sel_left = p-l-pt->buff; 434 | pt->sel_right = p-pt->buff; 435 | for ( i=pt->screen_y; pt->line[i]>pt->sel_left; i--); 436 | term_Scroll(pt, pt->screen_y-i); 437 | return TRUE; 438 | } 439 | } 440 | return FALSE; 441 | } 442 | int term_TL1(TERM *pt, char *cmd, char **pTl1Text) 443 | { 444 | if ( host_Status(pt->host )!=IDLE ) { //retrieve from NE 445 | term_Mark_Prompt(pt); 446 | int cmdlen = strlen(cmd); 447 | term_Send(pt, cmd, cmdlen); 448 | if ( cmd[cmdlen-1]!='\r' ) term_Send(pt, "\r", 1); 449 | term_Waitfor_Prompt(pt); 450 | } 451 | else { //retrieve from buffer 452 | char *pcursor=pt->buff; //only when retrieve from buffer 453 | pt->tl1len = 0; 454 | pt->buff[pt->cursor_x]=0; 455 | char *p = strstr( pcursor, cmd); 456 | if ( p==NULL ) { pcursor = pt->buff; p = strstr(pcursor, cmd); } 457 | if ( p!=NULL ) { p = strstr( p, "\r\n"); 458 | if ( p!=NULL ) { 459 | pt->tl1text = p+2; 460 | p = strstr(p, "\nM "); 461 | p = strstr(p, pt->sPrompt); 462 | if ( p!=NULL ) { 463 | pcursor = ++p; 464 | pt->tl1len = pcursor - pt->tl1text; 465 | } 466 | } 467 | } 468 | if (pt->tl1len == 0 ) { pt->tl1text = pt->buff+pt->cursor_x; } 469 | } 470 | 471 | if ( pTl1Text!=NULL ) *pTl1Text = pt->tl1text; 472 | return pt->tl1len; 473 | } 474 | int term_Pwd(TERM *pt, char *pwd, int len) 475 | { 476 | char *p1, *p2; 477 | term_TL1(pt,"pwd\r", &p2); 478 | p1 = strchr(p2, 0x0a); 479 | if ( p1!=NULL ) { 480 | p2 = p1+1; 481 | p1 = strchr(p2, 0x0a); 482 | if ( p1!=NULL ) { 483 | if ( len>p1-p2 ) len = p1-p2; 484 | strncpy(pwd, p2, len); 485 | pwd[len]=0; 486 | } 487 | } 488 | else 489 | len = 0; 490 | return len; 491 | } 492 | void escape_space(char *p) 493 | { 494 | do { 495 | p = strchr(p, '\\'); 496 | if ( p!=NULL ) { 497 | if ( p[1]==' ' ) 498 | for ( char *q=p; *q; q++ ) *q=q[1]; 499 | } 500 | } while ( p!=NULL ); 501 | } 502 | int term_Scp(TERM *pt, char *cmd, char **preply) 503 | { 504 | if ( host_Type(pt->host )!=SSH ) return 0; 505 | 506 | for ( char *q=cmd; *q; q++ ) if ( *q=='\\'&&q[1]!=' ' ) *q='/'; 507 | char *p = strchr(cmd, ' '); 508 | while ( p!=NULL ) { 509 | if ( p[-1]!='\\' ) { *p++=0; break; } 510 | p = strchr(p+1, ' '); 511 | } 512 | 513 | char *lpath, *rpath, *reply = term_Mark_Prompt(pt); 514 | term_Learn_Prompt(pt); 515 | if ( *cmd==':' ) { //scp_read 516 | lpath = p; rpath = cmd+1; 517 | escape_space(lpath); 518 | char ls_1[1024]="ls -1 "; 519 | if ( *rpath!='/' && *rpath!='~' ) { 520 | term_Pwd(pt, ls_1+6, 1016); 521 | strcat(ls_1, "/"); 522 | } 523 | strcat(ls_1, rpath); 524 | 525 | if ( strchr(rpath, '*')==NULL 526 | && strchr(rpath, '?')==NULL ) { //rpath is a single file 527 | reply = term_Mark_Prompt(pt); 528 | strcat(ls_1, "\012"); 529 | scp_read(pt->host, lpath, ls_1+6); 530 | } 531 | else { //rpath is a filename pattern 532 | char *rlist, *rfiles; 533 | strcat(ls_1, "\r"); 534 | int len = term_TL1(pt, ls_1, &rlist); 535 | if ( len>0 ) { 536 | rlist[len] = 0; 537 | p = strchr(rlist, 0x0a); 538 | if ( p!=NULL ) { 539 | rfiles = strdup(p+1); 540 | reply = term_Mark_Prompt(pt); 541 | scp_read(pt->host, lpath, rfiles); 542 | free(rfiles); 543 | } 544 | } 545 | } 546 | } 547 | else {//scp_write 548 | lpath = cmd; rpath = p+1; //*p is expected to be ':' here 549 | char lsld[1024]="ls -ld "; 550 | if ( *rpath!='/' && *rpath!='~' ) { //rpath is relative 551 | term_Pwd(pt, lsld+7, 1017); 552 | if ( *rpath ) strcat(lsld, "/"); 553 | } 554 | strcat(lsld, rpath); 555 | strcat(lsld, "\r"); 556 | 557 | if ( lsld[strlen(lsld)-1]!='/' ) { 558 | char *rlist; 559 | if ( term_TL1(pt, lsld, &rlist )>0 ) {//check if rpath is a dir 560 | lsld[strlen(lsld)-1] = 0; 561 | p = strchr(rlist, 0x0a); 562 | if ( p!=NULL ) { //append '/' if yes 563 | if ( p[1]=='d' ) strcat(lsld, "/"); 564 | } 565 | } 566 | } 567 | reply = term_Mark_Prompt(pt); 568 | escape_space(lsld+7); 569 | escape_space(lpath); 570 | scp_write(pt->host, lpath, lsld+7); 571 | } 572 | term_Send(pt, "\r", 1); 573 | if ( preply!=NULL ) *preply = reply; 574 | return term_Waitfor_Prompt(pt); 575 | } 576 | int term_Tun(TERM *pt, char *cmd, char **preply) 577 | { 578 | if ( host_Type(pt->host)!=SSH ) return 0; 579 | char *reply = term_Mark_Prompt(pt); 580 | if ( preply!=NULL ) *preply = reply; 581 | ssh2_Tun(pt->host, cmd); 582 | return term_Waitfor_Prompt(pt); 583 | } 584 | int term_xmodem(TERM *pt, char *fn) 585 | { 586 | if ( host_Type(pt->host)==SERIAL ) { 587 | FILE *fp = fopen(fn, "rb"); 588 | if ( fp!=NULL ) { 589 | term_Disp(pt, "sending "); 590 | term_Disp(pt, fn); 591 | term_Disp(pt, "\n"); 592 | xmodem_init(pt->host, fp); 593 | return 1; 594 | } 595 | } 596 | return 0; 597 | } 598 | int term_Cmd(TERM *pt, char *cmd, char **preply) 599 | { 600 | if ( *cmd!='!' ) return term_TL1(pt, cmd, preply); 601 | 602 | int rc = 0; 603 | if ( strncmp(++cmd, "Clear",5)==0 ) term_Clear(pt); 604 | else if ( strncmp(cmd, "Log", 3)==0 ) term_Logg(pt, cmd+3); 605 | else if ( strncmp(cmd, "Find ",5)==0 ) term_Srch(pt, cmd+5); 606 | else if ( strncmp(cmd, "Disp ",5)==0 ) term_Disp(pt, cmd+5); 607 | else if ( strncmp(cmd, "Send ",5)==0 ) { 608 | term_Mark_Prompt(pt); 609 | term_Send(pt, cmd+5,strlen(cmd+5)); 610 | } 611 | else if ( strncmp(cmd, "Hostname",8)==0) { 612 | if ( preply!=NULL ) { 613 | *preply = host_Status(pt->host)==IDLE ? "":pt->host->hostname; 614 | rc = strlen(*preply); 615 | } 616 | } 617 | else if ( strncmp(cmd,"Selection",9)==0) { 618 | if ( preply!=NULL ) *preply = pt->buff+pt->sel_left; 619 | rc = pt->sel_right-pt->sel_left; 620 | } 621 | else if ( strncmp(cmd, "Recv" ,4)==0 ) rc = term_Recv(pt, preply); 622 | else if ( strncmp(cmd, "Echo", 4)==0 ) rc = term_Echo(pt ) ? 1 : 0; 623 | else if ( strncmp(cmd, "Timeout",7)==0 )pt->iTimeOut = atoi( cmd+8); 624 | else if ( strncmp(cmd, "Prompt",6)==0 ) { 625 | if ( cmd[6]==' ' ) { 626 | strncpy(pt->sPrompt, cmd+7, 31); 627 | pt->sPrompt[31] = 0; 628 | pt->iPrompt = url_decode(pt->sPrompt); 629 | } 630 | else { 631 | term_Learn_Prompt(pt); 632 | } 633 | if ( preply!=NULL ) *preply = pt->sPrompt; 634 | rc = pt->iPrompt; 635 | } 636 | else if ( strncmp(cmd, "Tftpd",5)==0 ) tftp_Svr(cmd+5); 637 | else if ( strncmp(cmd, "Ftpd", 4)==0 ) ftp_Svr(cmd+4); 638 | else if ( strncmp(cmd, "tun", 3)==0 ) rc = term_Tun(pt, cmd+3, preply); 639 | else if ( strncmp(cmd, "scp ", 4)==0 ) rc = term_Scp(pt, cmd+4, preply); 640 | else if ( strncmp(cmd, "xmodem ", 7)==0 ) rc = term_xmodem(pt, cmd+7); 641 | else if ( strncmp(cmd, "Wait ", 5)==0 ) Sleep(atoi(cmd+5)*1000); 642 | else if ( strncmp(cmd, "Waitfor ", 8)==0) { 643 | for ( int i=pt->iTimeOut; i>0; i-- ) { 644 | pt->buff[pt->cursor_x] = 0; 645 | if ( strstr(pt->tl1text, cmd+8)!=NULL ) { 646 | if ( preply!=NULL ) *preply = pt->tl1text; 647 | rc = pt->buff+pt->cursor_x-pt->tl1text; 648 | break; 649 | } 650 | Sleep(1000); 651 | } 652 | } 653 | else { 654 | term_Mark_Prompt(pt); 655 | host_Open(pt->host, cmd); 656 | if ( preply!=NULL ) { 657 | term_Waitfor_Prompt(pt); //added for scripting 658 | *preply = pt->tl1text; 659 | rc = pt->tl1len; 660 | } 661 | } 662 | return rc; 663 | } 664 | void buff_clear(TERM *pt, int offset, int len) 665 | { 666 | memset(pt->buff+offset, ' ', len); 667 | memset(pt->attr+offset, 7, len); 668 | } 669 | void screen_clear(TERM *pt, int m0) 670 | { 671 | /*mostly [2J used after [?1049h to clear screen 672 | and when screen size changed during vi or raspi-config 673 | flashwave TL1 use it without [?1049h for splash screen 674 | freeBSD use it without [?1049h* for top and vi*/ 675 | int lines = pt->size_y; 676 | if ( m0==2 ) pt->screen_y = pt->cursor_y; 677 | if ( m0==1 ) { 678 | lines = pt->cursor_y-pt->screen_y; 679 | buff_clear(pt, pt->line[pt->cursor_y], 680 | pt->cursor_x-pt->line[pt->cursor_y]+1); 681 | pt->cursor_y = pt->screen_y; 682 | } 683 | if ( m0==0 ) { 684 | buff_clear(pt, pt->cursor_x, 685 | pt->line[pt->cursor_y+1]-pt->cursor_x); 686 | lines = pt->size_y+pt->screen_y-pt->cursor_y; 687 | } 688 | pt->cursor_x = pt->line[pt->cursor_y]; 689 | int cy = pt->cursor_y; 690 | for ( int i=0; icursor_x, pt->size_x); 693 | pt->cursor_x += pt->size_x; 694 | term_nextLine(pt); 695 | } 696 | pt->cursor_y = cy; 697 | if ( m0==2 || m0==0 ) pt->screen_y--; 698 | pt->cursor_x = pt->line[pt->cursor_y]; 699 | } 700 | void check_cursor_y(TERM *pt) 701 | { 702 | if (pt->cursor_y < pt->screen_y ) 703 | pt->cursor_y = pt->screen_y; 704 | if (pt->cursor_y > pt->screen_y+pt->size_y-1 ) 705 | pt->cursor_y = pt->screen_y+pt->size_y-1; 706 | if (pt->bOriginMode ) { 707 | if (pt->cursor_y< pt->screen_y+pt->roll_top ) 708 | pt->cursor_y = pt->screen_y+pt->roll_top; 709 | if (pt->cursor_y> pt->screen_y+pt->roll_bot ) 710 | pt->cursor_y = pt->screen_y+pt->roll_bot; 711 | 712 | } 713 | } 714 | const unsigned char *vt100_Escape(TERM *pt, const unsigned char *sz, int cnt) 715 | { 716 | const unsigned char *zz = sz+cnt; 717 | 718 | pt->bEscape = TRUE; 719 | while ( szbEscape ) 720 | { 721 | if ( *sz>31 ) { 722 | pt->escape_code[pt->escape_idx++] = *sz++; 723 | } 724 | else { //handle control character in escape sequence 725 | switch ( *sz++ ) { 726 | case 0x08: //BS 727 | if ( isUTF8c(pt->buff[pt->cursor_x--]) ) 728 | //utf8 continuation byte 729 | while ( isUTF8c(pt->buff[pt->cursor_x]) ) 730 | pt->cursor_x--; 731 | break; 732 | case 0x0b:{ //VT 733 | int x = pt->cursor_x - pt->line[pt->cursor_y]; 734 | pt->cursor_x = pt->line[++pt->cursor_y] + x; 735 | break; 736 | } 737 | case 0x0d: //CR 738 | pt->cursor_x = pt->line[pt->cursor_y]; 739 | } 740 | } 741 | switch(pt->escape_code[0] ) 742 | { 743 | case '[': if ( isalpha(pt->escape_code[pt->escape_idx-1]) 744 | || pt->escape_code[pt->escape_idx-1]=='@' 745 | || pt->escape_code[pt->escape_idx-1]=='`' ) 746 | { 747 | pt->bEscape = FALSE; 748 | int m0=0; //ESC[J == ESC[0J ESC[K==ESC[0K 749 | int n0=1; //ESC[A == ESC[1A 750 | int n1=1; //n1;n0 used by ESC[Ps;PtH and ESC[Ps;Ptr 751 | if ( isdigit(pt->escape_code[1]) ) { 752 | m0 = n0 = atoi(pt->escape_code+1); 753 | if ( n0==0 ) n0 = 1;//ESC[0A == ESC[1A 754 | } 755 | char *p = strchr(pt->escape_code, ';'); 756 | if ( p != NULL ) { 757 | n1 = n0 ; 758 | n0 = atoi(p+1); 759 | if ( n0==0 ) n0=1; //ESC[0;0f == ESC[1;1f 760 | } 761 | int x; 762 | switch (pt->escape_code[pt->escape_idx-1] ) 763 | { 764 | case 'A'://cursor up n0 lines 765 | x = pt->cursor_x - pt->line[pt->cursor_y]; 766 | pt->cursor_y -= n0; 767 | check_cursor_y(pt); 768 | pt->cursor_x = pt->line[pt->cursor_y]+x; 769 | break; 770 | case 'd'://line position absolute 771 | x = pt->cursor_x-pt->line[pt->cursor_y]; 772 | pt->cursor_y = pt->screen_y+n0-1; 773 | check_cursor_y(pt); 774 | pt->cursor_x = pt->line[pt->cursor_y]+x; 775 | break; 776 | case 'e'://line position relative 777 | case 'B'://cursor down n0 lines 778 | x = pt->cursor_x - pt->line[pt->cursor_y]; 779 | pt->cursor_y += n0; 780 | check_cursor_y(pt); 781 | pt->cursor_x = pt->line[pt->cursor_y]+x; 782 | break; 783 | case '`': //character position absolute 784 | case 'G': //cursor to n0th position from left 785 | pt->cursor_x = pt->line[pt->cursor_y]; 786 | //fall through 787 | case 'a'://character position relative 788 | case 'C'://cursor right n0 characters 789 | while ( n0-->0 && 790 | pt->cursor_xline[pt->cursor_y]+pt->size_x-1 ) 791 | { 792 | if ( isUTF8c(pt->buff[++pt->cursor_x]) ) 793 | while ( isUTF8c(pt->buff[++pt->cursor_x])); 794 | } 795 | break; 796 | case 'D'://cursor left n0 characters 797 | while ( n0-->0 && pt->cursor_x>pt->line[pt->cursor_y]) { 798 | if ( isUTF8c(pt->buff[--pt->cursor_x]) ) 799 | while ( isUTF8c(pt->buff[--pt->cursor_x])); 800 | } 801 | break; 802 | case 'E': //cursor to begining of next line n0 times 803 | pt->cursor_y += n0; 804 | check_cursor_y(pt); 805 | pt->cursor_x = pt->line[pt->cursor_y]; 806 | break; 807 | case 'F': //cursor to begining of previous line n0 times 808 | pt->cursor_y -= n0; 809 | check_cursor_y(pt); 810 | pt->cursor_x = pt->line[pt->cursor_y]; 811 | break; 812 | case 'f': //horizontal and vertical position forced 813 | for ( int i=pt->cursor_y+1; iscreen_y+n1; i++ ) 814 | if ( iline[i]cursor_x ) 815 | pt->line[i]=pt->cursor_x; 816 | case 'H': //cursor to line n1, postion n0 817 | if ( !pt->bAlterScreen && n1>pt->size_y ) { 818 | pt->cursor_y = (pt->screen_y++) + pt->size_y; 819 | } 820 | else { 821 | pt->cursor_y = pt->screen_y+n1-1; 822 | if (pt->bOriginMode ) pt->cursor_y+=pt->roll_top; 823 | check_cursor_y(pt); 824 | } 825 | pt->cursor_x = pt->line[pt->cursor_y]; 826 | while ( --n0>0 ) { 827 | pt->cursor_x++; 828 | while ( isUTF8c(pt->buff[pt->cursor_x]) ) pt->cursor_x++; 829 | } 830 | break; 831 | case 'J': //[J kill till end, 1J begining, 2J entire screen 832 | if ( isdigit(pt->escape_code[1]) ) { 833 | screen_clear(pt, m0); 834 | } 835 | else { 836 | pt->line[pt->cursor_y+1] = pt->cursor_x; 837 | for ( int i=pt->cursor_y+2; 838 | i<=pt->screen_y+pt->size_y+1; i++ ) 839 | if ( iline[i] = 0; 840 | } 841 | break; 842 | case 'K': { //[K kill till end, 1K begining, 2K entire line 843 | int i = pt->line[pt->cursor_y]; //setup for m0==2 844 | int j = pt->line[pt->cursor_y+1]; 845 | if ( m0==0 ) i = pt->cursor_x; //change start if m0==0 846 | if ( m0==1 ) j = pt->cursor_x+1; //change stop if m0==1 847 | if ( j>i ) buff_clear(pt, i, j-i); 848 | } 849 | break; 850 | case 'L'://insert lines 851 | if ( n0 > pt->screen_y+pt->roll_bot-pt->cursor_y ) 852 | n0 = pt->screen_y+pt->roll_bot-pt->cursor_y+1; 853 | else { 854 | for ( int i=pt->screen_y+pt->roll_bot; 855 | i>=pt->cursor_y+n0; i--) { 856 | memcpy(pt->buff+pt->line[i], 857 | pt->buff+pt->line[i-n0], pt->size_x); 858 | memcpy(pt->attr+pt->line[i], 859 | pt->attr+pt->line[i-n0], pt->size_x); 860 | } 861 | } 862 | pt->cursor_x = pt->line[pt->cursor_y]; 863 | buff_clear(pt, pt->cursor_x, pt->size_x*n0); 864 | break; 865 | case 'M'://delete lines 866 | if ( n0 > pt->screen_y+pt->roll_bot-pt->cursor_y ) 867 | n0 = pt->screen_y+pt->roll_bot-pt->cursor_y+1; 868 | else { 869 | for ( int i=pt->cursor_y; 870 | i<=pt->screen_y+pt->roll_bot-n0; i++ ) { 871 | memcpy(pt->buff+pt->line[i], 872 | pt->buff+pt->line[i+n0], pt->size_x); 873 | memcpy(pt->attr+pt->line[i], 874 | pt->attr+pt->line[i+n0], pt->size_x); 875 | } 876 | } 877 | pt->cursor_x = pt->line[pt->cursor_y]; 878 | buff_clear(pt, pt->line[pt->screen_y+pt->roll_bot-n0+1], 879 | pt->size_x*n0); 880 | break; 881 | case 'P'://delete n0 characters, fill with space to the right margin 882 | for (int i=pt->cursor_x;iline[pt->cursor_y+1]-n0;i++){ 883 | pt->buff[i]=pt->buff[i+n0]; 884 | pt->attr[i]=pt->attr[i+n0]; 885 | } 886 | buff_clear(pt, pt->line[pt->cursor_y+1]-n0, n0); 887 | if ( !pt->bAlterScreen ) { 888 | pt->line[pt->cursor_y+1]-=n0; 889 | if ( pt->line[pt->cursor_y+1]line[pt->cursor_y] ) 890 | pt->line[pt->cursor_y+1] =pt->line[pt->cursor_y]; 891 | } 892 | break; 893 | case '@'://insert n0 spaces 894 | for (int i=pt->line[pt->cursor_y+1]-n0-1;i>=pt->cursor_x; i--){ 895 | pt->buff[i+n0]=pt->buff[i]; 896 | pt->attr[i+n0]=pt->attr[i]; 897 | } 898 | if ( !pt->bAlterScreen ){ 899 | pt->line[pt->cursor_y+1]+=n0; 900 | if ( pt->line[pt->cursor_y+1]>pt->line[pt->cursor_y] 901 | +pt->size_x ) 902 | pt->line[pt->cursor_y+1] =pt->line[pt->cursor_y] 903 | +pt->size_x; 904 | } 905 | //fall through; 906 | case 'X': //erase n0 characters 907 | buff_clear(pt, pt->cursor_x, n0); 908 | break; 909 | case 'S': // scroll up n0 lines 910 | for ( int i=pt->roll_top; i<=pt->roll_bot-n0; i++ ) { 911 | memcpy(pt->buff+pt->line[pt->screen_y+i], 912 | pt->buff+pt->line[pt->screen_y+i+n0], 913 | pt->size_x); 914 | memcpy(pt->attr+pt->line[pt->screen_y+i], 915 | pt->attr+pt->line[pt->screen_y+i+n0], 916 | pt->size_x); 917 | } 918 | buff_clear(pt, pt->line[pt->screen_y+pt->roll_bot-n0+1], 919 | n0*pt->size_x); 920 | break; 921 | case 'T': // scroll down n0 lines 922 | for ( int i=pt->roll_bot; i>=pt->roll_top+n0; i-- ) { 923 | memcpy(pt->buff+pt->line[pt->screen_y+i], 924 | pt->buff+pt->line[pt->screen_y+i-n0], 925 | pt->size_x); 926 | memcpy(pt->attr+pt->line[pt->screen_y+i], 927 | pt->attr+pt->line[pt->screen_y+i-n0], 928 | pt->size_x); 929 | } 930 | buff_clear(pt, pt->line[pt->screen_y+pt->roll_top], 931 | n0*pt->size_x); 932 | break; 933 | case 'I': //cursor forward n0 tab stops 934 | break; 935 | case 'Z': //cursor backward n0 tab stops 936 | break; 937 | case 'c'://Send Device Attributes 938 | host_Send(pt->host, "\033[?1;0c", 7); //vt100 without options 939 | break; 940 | case 'g': //clear tabstop 941 | if ( m0==0 ) { //clear current tabstop 942 | int l = pt->cursor_x - pt->line[pt->cursor_y]; 943 | pt->tabstops[l] = 0; 944 | } 945 | if ( m0==3 ) { //clear all tabstops 946 | memset(pt->tabstops, 0, 256); 947 | } 948 | break; 949 | case 'h': 950 | if (pt->escape_code[1]=='4' ) pt->bInsert = TRUE; 951 | if (pt->escape_code[1]=='?' ) { 952 | n0 = atoi(pt->escape_code+2); 953 | if ( n0==1 ) pt->bAppCursor = TRUE; 954 | if ( n0==3 ) { 955 | if (pt->size_x!=132 || pt->size_y!=25 ) { 956 | pt->size_x = 132; pt->size_y = 25; 957 | wnd_Size(); 958 | } 959 | screen_clear(pt, 2); 960 | } 961 | if ( n0==6 ) pt->bOriginMode = TRUE; 962 | if ( n0==7 ) pt->bWraparound = TRUE; 963 | if ( n0==25 ) pt->bCursor = TRUE; 964 | if ( n0==2004 ) pt->bBracket = TRUE; 965 | if ( n0==1049 ) { //?1049h alternate screen, 966 | pt->bAlterScreen = TRUE; 967 | screen_clear(pt, 2); 968 | } 969 | } 970 | break; 971 | case 'l': 972 | if (pt->escape_code[1]=='4' ) pt->bInsert = FALSE; 973 | if (pt->escape_code[1]=='?' ) { 974 | n0 = atoi(pt->escape_code+2); 975 | if ( n0==1 ) pt->bAppCursor = FALSE; 976 | if ( n0==3 ) { 977 | if (pt->size_x!=80 || pt->size_y!=25 ) { 978 | pt->size_x = 80; pt->size_y = 25; 979 | wnd_Size(); 980 | } 981 | screen_clear(pt, 2); 982 | } 983 | if ( n0==6 ) pt->bOriginMode = FALSE; 984 | if ( n0==7 ) pt->bWraparound = FALSE; 985 | if ( n0==25 ) pt->bCursor = FALSE; 986 | if ( n0==2004 ) pt->bBracket = FALSE; 987 | if ( n0==1049 ) { //?1049l exit alternate screen, 988 | pt->bAlterScreen = FALSE; 989 | pt->cursor_y = pt->screen_y; 990 | pt->cursor_x = pt->line[pt->cursor_y]; 991 | for ( int i=1; i<=pt->size_y+1; i++ ) 992 | pt->line[pt->cursor_y+i] = 0; 993 | pt->screen_y = pt->cursor_y-pt->size_y+1; 994 | if (pt->screen_y<0 ) pt->screen_y = 0; 995 | } 996 | } 997 | break; 998 | case 'm': { 999 | char *p = pt->escape_code; 1000 | while ( p!=NULL ) { 1001 | m0 = atoi(++p); 1002 | switch ( m0/10 ) { 1003 | case 0: if ( m0==0 ) pt->c_attr = 7; //normal 1004 | if ( m0==1 ) pt->c_attr|= 0x08; //bright 1005 | if ( m0==7 ) pt->c_attr = 0x70; //negative 1006 | break; 1007 | case 2: pt->c_attr = 7; //normal 1008 | break; 1009 | case 3: if ( m0==39 ) m0 = 37; //default foreground 1010 | pt->c_attr = (pt->c_attr&0xf8)+m0%10; 1011 | break; 1012 | case 4: if ( m0==49 ) m0 = 0; //default background 1013 | pt->c_attr = (pt->c_attr&0x0f)+((m0%10)<<4); 1014 | break; 1015 | case 9: pt->c_attr = (pt->c_attr&0xf0)+m0%10+8; 1016 | break; 1017 | case 10:pt->c_attr = (pt->c_attr&0x0f)+((m0%10+8)<<4); 1018 | break; 1019 | } 1020 | p = strchr(p, ';'); 1021 | } 1022 | } 1023 | break; 1024 | case 'r': 1025 | if ( n1==1 && n0==1 ) n0 = pt->size_y; //ESC[r 1026 | pt->roll_top=n1-1; pt->roll_bot=n0-1; 1027 | pt->cursor_y = pt->screen_y; 1028 | if (pt->bOriginMode ) pt->cursor_y+=pt->roll_top; 1029 | pt->cursor_x = pt->line[pt->cursor_y]; 1030 | break; 1031 | case 's': //save cursor 1032 | pt->save_x = pt->cursor_x-pt->line[pt->cursor_y]; 1033 | pt->save_y = pt->cursor_y-pt->screen_y; 1034 | pt->save_attr = pt->c_attr; 1035 | break; 1036 | case 'u': //restore cursor 1037 | pt->cursor_y = pt->save_y+pt->screen_y; 1038 | pt->cursor_x = pt->line[pt->cursor_y]+pt->save_x; 1039 | pt->c_attr = pt->save_attr; 1040 | break; 1041 | } 1042 | } 1043 | break; 1044 | case '7'://save cursor 1045 | pt->save_x = pt->cursor_x-pt->line[pt->cursor_y]; 1046 | pt->save_y = pt->cursor_y-pt->screen_y; 1047 | pt->save_attr = pt->c_attr; 1048 | pt->bEscape = FALSE; 1049 | break; 1050 | case '8': //restore cursor 1051 | pt->cursor_y = pt->save_y+pt->screen_y; 1052 | pt->cursor_x = pt->line[pt->cursor_y]+pt->save_x; 1053 | pt->c_attr = pt->save_attr; 1054 | pt->bEscape = FALSE; 1055 | break; 1056 | case 'F'://cursor to lower left corner 1057 | pt->cursor_y = pt->screen_y+pt->size_y-1; 1058 | pt->cursor_x = pt->line[pt->cursor_y]; 1059 | pt->bEscape = FALSE; 1060 | break; 1061 | case 'E'://NEL, move to next line 1062 | pt->cursor_x = pt->line[++pt->cursor_y]; 1063 | pt->bEscape = FALSE; 1064 | break; 1065 | case 'D'://IND, move/scroll up one line 1066 | if (pt->cursor_y < pt->roll_bot+pt->screen_y ) { 1067 | int x = pt->cursor_x - pt->line[pt->cursor_y]; 1068 | pt->cursor_x = pt->line[++pt->cursor_y] + x; 1069 | } 1070 | else { 1071 | int len = pt->line[pt->screen_y+pt->roll_bot+1] 1072 | -pt->line[pt->screen_y+pt->roll_top+1]; 1073 | int x = pt->cursor_x-pt->line[pt->cursor_y]; 1074 | memcpy( pt->buff+pt->line[pt->screen_y+pt->roll_top], 1075 | pt->buff+pt->line[pt->screen_y+pt->roll_top+1], len); 1076 | memcpy( pt->attr+pt->line[pt->screen_y+pt->roll_top], 1077 | pt->attr+pt->line[pt->screen_y+pt->roll_top+1], len); 1078 | len = pt->line[pt->screen_y+pt->roll_top+1] 1079 | -pt->line[pt->screen_y+pt->roll_top]; 1080 | for ( int i=pt->roll_top+1; i<=pt->roll_bot; i++ ) 1081 | pt->line[pt->screen_y+i] = pt->line[pt->screen_y+i+1]-len; 1082 | buff_clear( pt, pt->line[pt->screen_y+pt->roll_bot], 1083 | pt->line[pt->screen_y+pt->roll_bot+1]- 1084 | pt->line[pt->screen_y+pt->roll_bot]); 1085 | pt->cursor_x = pt->line[pt->cursor_y]+x; 1086 | } 1087 | pt->bEscape = FALSE; 1088 | break; 1089 | case 'M'://RI, move/scroll down one line 1090 | if (pt->cursor_y > pt->roll_top+pt->screen_y ) { 1091 | int x = pt->cursor_x - pt->line[pt->cursor_y]; 1092 | pt->cursor_x = pt->line[--pt->cursor_y] + x; 1093 | } 1094 | else { 1095 | for ( int i=pt->roll_bot; i>pt->roll_top; i-- ) { 1096 | memcpy(pt->buff+pt->line[pt->screen_y+i], 1097 | pt->buff+pt->line[pt->screen_y+i-1], 1098 | pt->size_x); 1099 | memcpy(pt->attr+pt->line[pt->screen_y+i], 1100 | pt->attr+pt->line[pt->screen_y+i-1], 1101 | pt->size_x); 1102 | } 1103 | buff_clear(pt, pt->line[pt->screen_y+pt->roll_top], 1104 | pt->size_x); 1105 | } 1106 | pt->bEscape = FALSE; 1107 | break; 1108 | case 'H': 1109 | pt->tabstops[pt->cursor_x-pt->line[pt->cursor_y]]=1; 1110 | pt->bEscape = FALSE; 1111 | break; 1112 | case ']': 1113 | if (pt->escape_code[pt->escape_idx-1]==';' ) { 1114 | if (pt->escape_code[1]=='0' ) { 1115 | pt->bTitle = TRUE; 1116 | pt->title_idx = 0; 1117 | } 1118 | pt->bEscape = FALSE; 1119 | } 1120 | break; 1121 | case '(': 1122 | case ')': 1123 | if (pt->escape_code[1]=='B'||pt->escape_code[1]=='0' ) { 1124 | pt->bGraphic = (pt->escape_code[1]=='0'); 1125 | pt->bEscape = FALSE; 1126 | } 1127 | break; 1128 | case '#': //#8 alignment test, fill screen with 'E' 1129 | if (pt->escape_idx==2 ) { 1130 | if (pt->escape_code[1]=='8' ) 1131 | memset(pt->buff+pt->line[pt->screen_y], 'E', 1132 | pt->size_x*pt->size_y); 1133 | pt->bEscape = FALSE; 1134 | } 1135 | break; 1136 | default: pt->bEscape = FALSE; 1137 | } 1138 | if (pt->escape_idx==31 ) pt->bEscape = FALSE; 1139 | if ( !pt->bEscape ) 1140 | { 1141 | pt->escape_idx=0; 1142 | memset(pt->escape_code, 0, 32); 1143 | } 1144 | } 1145 | return sz; 1146 | } 1147 | void term_Parse_XML(TERM *pt, const char *msg, int len) 1148 | { 1149 | const char *p=msg, *q; 1150 | const char spaces[256]="\r\n \ 1151 | "; 1152 | if ( strncmp(msg, "xmlIndent = 0; 1154 | pt->xmlPreviousIsOpen = TRUE; 1155 | } 1156 | while ( *p!=0 && *p!='<' ) p++; 1157 | if ( p>msg ) term_Parse(pt, msg, p-msg); 1158 | while ( *p!=0 && p]]>", 6)==0 ) { 1162 | term_Parse(pt, "]]>]]>\n\033[37m", 12); 1163 | p+=6; 1164 | } 1165 | } 1166 | else if ( *p=='<' ) {//tag 1167 | if ( p[1]=='/' ) { 1168 | if ( !pt->xmlPreviousIsOpen ) { 1169 | pt->xmlIndent -= 2; 1170 | term_Parse(pt, spaces, pt->xmlIndent); 1171 | } 1172 | pt->xmlPreviousIsOpen = FALSE; 1173 | } 1174 | else { 1175 | if (pt->xmlPreviousIsOpen ) pt->xmlIndent+=2; 1176 | term_Parse(pt, spaces, pt->xmlIndent); 1177 | pt->xmlPreviousIsOpen = TRUE; 1178 | } 1179 | term_Parse(pt, "\033[32m", 5); 1180 | q = strchr(p, '>'); 1181 | if ( q==NULL ) q = p+strlen(p); 1182 | char *r = strchr(p, ' '); 1183 | if ( r!=NULL && r", 6); 1191 | p = q; 1192 | if ( *q=='>' ) { 1193 | p++; 1194 | if ( q[-1]=='/' ) pt->xmlPreviousIsOpen = FALSE; 1195 | } 1196 | } 1197 | else {//data 1198 | term_Parse(pt, "\033[33m", 5); 1199 | q = strchr(p, '<'); 1200 | if ( q==NULL ) q = p+strlen(p); 1201 | term_Parse(pt, p, q-p); 1202 | p = q; 1203 | } 1204 | } 1205 | } 1206 | #define TNO_IAC 0xff 1207 | #define TNO_DONT 0xfe 1208 | #define TNO_DO 0xfd 1209 | #define TNO_WONT 0xfc 1210 | #define TNO_WILL 0xfb 1211 | #define TNO_SUB 0xfa 1212 | #define TNO_SUBEND 0xf0 1213 | #define TNO_ECHO 0x01 1214 | #define TNO_AHEAD 0x03 1215 | #define TNO_STATUS 0x05 1216 | #define TNO_WNDSIZE 0x1f 1217 | #define TNO_TERMTYPE 0x18 1218 | #define TNO_NEWENV 0x27 1219 | //UCHAR NEGOBEG[]={0xff, 0xfb, 0x03, 0xff, 0xfd, 0x03, 0xff, 0xfd, 0x01}; 1220 | unsigned char TERMTYPE[]={//vt100 1221 | 0xff, 0xfa, 0x18, 0x00, 0x76, 0x74, 0x31, 0x30, 0x30, 0xff, 0xf0 1222 | }; 1223 | const unsigned char *telnet_Options(TERM *pt, const unsigned char *p, int cnt) 1224 | { 1225 | const unsigned char *q = p+cnt; 1226 | while ( *p==0xff && pbEcho = TRUE; 1239 | } 1240 | term_Send(pt, (char *)negoreq, 3); 1241 | p+=3; 1242 | break; 1243 | case TNO_WILL: 1244 | negoreq[1]=TNO_DONT; negoreq[2]=p[2]; 1245 | if ( p[2]==TNO_ECHO || p[2]==TNO_AHEAD ) { 1246 | negoreq[1]=TNO_DO; 1247 | if ( p[2]==TNO_ECHO ) pt->bEcho = FALSE; 1248 | } 1249 | term_Send(pt, (char*)negoreq, 3); 1250 | p+=3; 1251 | break; 1252 | case TNO_SUB: 1253 | negoreq[1]=TNO_SUB; negoreq[2]=p[2]; 1254 | if ( p[2]==TNO_TERMTYPE ) { 1255 | term_Send(pt, (char *)TERMTYPE, sizeof(TERMTYPE)); 1256 | } 1257 | if ( p[2]==TNO_NEWENV ) { 1258 | term_Send(pt, (char*)negoreq, 6); 1259 | } 1260 | while ( *p!=0xff && p 22 | #include 23 | #include 24 | #include 25 | #define ID_SCRIPT0 1000 26 | #define ID_CONNECT0 2000 27 | //#define WM_DPICHANGED 0x02E0 28 | //#define SM_CXPADDEDBORDER 92 29 | 30 | int dpi = 96; 31 | int titleHeight; 32 | int fontSize = 16; 33 | WCHAR fontFace[32] = L"Consolas"; 34 | WCHAR wndTitle[256] = L" Term Script Options "; 35 | const char TINYTERM[]="\r\033[32mtinyTerm> \033[37m"; 36 | const char WELCOME[]="\r\n\n\ 37 | \ttinyTerm is a simple, small and scriptable terminal emulator,\r\n\n\ 38 | \ta serial/telnet/ssh/sftp/netconf client with unique features:\r\n\n\n\ 39 | \t * small portable exe less than 250KB\r\n\n\ 40 | \t * command history and autocompletion\r\n\n\ 41 | \t * text based batch command automation\r\n\n\ 42 | \t * drag and drop to send files via scp or xmodem\r\n\n\ 43 | \t * scripting interface at xmlhttp://127.0.0.1:%d\r\n\n\n\ 44 | \thttps://yongchaofan.github.io/tinyTerm\r\n\n\ 45 | \tVerision 1.9.9 ©2018-2021 Yongchao Fan\r\n\n"; 46 | 47 | const COLORREF COLORS[16] = { 48 | RGB(0,0,0), RGB(192,0,0), RGB(0,192,0), RGB(192,192,0), 49 | RGB(32,96,240), RGB(192,0,192), RGB(0,192,192), RGB(192,192,192), 50 | RGB(0,0,0), RGB(240,0,0), RGB(0,240,0), RGB(240,240,0), 51 | RGB(32,96,240), RGB(240,0,240), RGB(0,240,240), RGB(240,240,240) 52 | }; 53 | static HINSTANCE hInst; 54 | static HBRUSH dwBkBrush; 55 | static HWND hwndTerm, hwndCmd; 56 | static HWND hwndScriptDlg=NULL; //script control dialog window 57 | static RECT termRect, wndRect; 58 | static HFONT hTermFont; 59 | static HMENU hMainMenu, hMenu[4]; 60 | const int TTERM=0, SCRIPT=1, OPTION=2, CONTEX=3; 61 | int menuX[4]; 62 | 63 | TERM *pt; 64 | HOST *ph; 65 | 66 | static int iFontHeight, iFontWidth; 67 | static int iTransparency = 255; 68 | static int iConnectCount=0, iScriptCount=0, httport; 69 | static BOOL bFocus=TRUE, bLocalEdit=FALSE, bScrollbar=FALSE; 70 | static BOOL bScriptRun=FALSE, bScriptPause=FALSE; 71 | static BOOL bFTPd=FALSE, bTFTPd=FALSE; 72 | 73 | void LoadDict(); 74 | void SaveDict(); 75 | void OpenScript(WCHAR *wfn); 76 | void DropScript(char *tl1s); 77 | void DropFiles(HDROP hDrop); 78 | void DropXmodem(HDROP hDrop); 79 | 80 | int wchar_to_utf8(WCHAR *wbuf, int wcnt, char *buf, int cnt) 81 | { 82 | return WideCharToMultiByte(CP_UTF8, 0, wbuf, wcnt, buf, cnt, NULL, NULL); 83 | } 84 | int utf8_to_wchar(const char *buf, int cnt, WCHAR *wbuf, int wcnt) 85 | { 86 | return MultiByteToWideChar(CP_UTF8, 0, buf, cnt, wbuf, wcnt); 87 | } 88 | FILE * fopen_utf8(const char *fn, const char *mode) 89 | { 90 | WCHAR wfn[MAX_PATH], wmode[4]; 91 | utf8_to_wchar(fn, strlen(fn)+1, wfn, MAX_PATH); 92 | utf8_to_wchar(mode, strlen(mode)+1, wmode, 4); 93 | return _wfopen(wfn, wmode); 94 | } 95 | int stat_utf8(const char *fn, struct _stat *buffer) 96 | { 97 | WCHAR wfn[MAX_PATH]; 98 | utf8_to_wchar(fn, strlen(fn)+1, wfn, MAX_PATH); 99 | return _wstat(wfn, buffer); 100 | } 101 | 102 | WCHAR *fileDialog( WCHAR *szFilter, DWORD dwFlags ) 103 | { 104 | static WCHAR wname[MAX_PATH]; 105 | BOOL ret = FALSE; 106 | OPENFILENAME ofn; 107 | 108 | wname[0]=0; 109 | memset(&ofn, 0, sizeof(OPENFILENAME)); 110 | ofn.lStructSize = sizeof(OPENFILENAME); 111 | ofn.hwndOwner = hwndTerm; 112 | ofn.lpstrFile = wname; 113 | ofn.nMaxFile = MAX_PATH-1; 114 | ofn.lpstrFilter = szFilter; 115 | ofn.nFilterIndex = 1; 116 | ofn.lpstrFileTitle = NULL; 117 | ofn.nMaxFileTitle = 0; 118 | ofn.lpstrInitialDir = NULL; 119 | ofn.Flags = dwFlags | OFN_NOCHANGEDIR; 120 | if ( dwFlags&OFN_OVERWRITEPROMPT ) 121 | ret = GetSaveFileName(&ofn); 122 | else 123 | ret = GetOpenFileName(&ofn); 124 | return ret ? wname : NULL; 125 | } 126 | char *getFolderName(WCHAR *wtitle) 127 | { 128 | static BROWSEINFO bi; 129 | static char szFolder[MAX_PATH]; 130 | static WCHAR wfolder[MAX_PATH]; 131 | WCHAR szDispName[MAX_PATH]; 132 | LPITEMIDLIST pidl; 133 | 134 | memset(&bi, 0, sizeof(BROWSEINFO)); 135 | bi.hwndOwner = 0; 136 | bi.pidlRoot = NULL; 137 | bi.pszDisplayName = szDispName; 138 | bi.lpszTitle = wtitle; 139 | bi.ulFlags = BIF_RETURNONLYFSDIRS; 140 | bi.lpfn = NULL; 141 | bi.lParam = 0; 142 | pidl = SHBrowseForFolder(&bi); 143 | if ( pidl != NULL ) 144 | if ( SHGetPathFromIDList(pidl, wfolder) ) 145 | { 146 | wchar_to_utf8(wfolder, -1, szFolder, MAX_PATH); 147 | return szFolder; 148 | } 149 | return NULL; 150 | } 151 | BOOL fontDialog() 152 | { 153 | LOGFONT lf; 154 | CHOOSEFONT cf; 155 | ZeroMemory(&cf, sizeof(cf)); 156 | cf.lStructSize = sizeof (cf); 157 | cf.hwndOwner = hwndTerm; 158 | cf.lpLogFont = &lf; 159 | cf.Flags = CF_SCREENFONTS|CF_FIXEDPITCHONLY|CF_INITTOLOGFONTSTRUCT; 160 | if ( GetObject(hTermFont, sizeof(lf), &lf)==0 ) ZeroMemory(&lf, sizeof(lf)); 161 | 162 | if ( ChooseFont(&cf) ) 163 | { 164 | DeleteObject(hTermFont); 165 | hTermFont = CreateFontIndirect(&lf); 166 | SendMessage( hwndCmd, WM_SETFONT, (WPARAM)hTermFont, TRUE ); 167 | fontSize = lf.lfHeight; 168 | if ( fontSize<0 ) fontSize = -fontSize; 169 | wcscpy(fontFace, lf.lfFaceName); 170 | return TRUE; 171 | } 172 | return FALSE; 173 | } 174 | void menu_Add(WCHAR *wcmd) 175 | { 176 | if ( wcsncmp(wcmd, L"com", 3)==0 || 177 | wcsncmp(wcmd, L"ssh", 3)==0 || 178 | wcsncmp(wcmd, L"sftp", 4)==0 || 179 | wcsncmp(wcmd, L"telnet",6)==0 || 180 | wcsncmp(wcmd, L"netconf",7)==0 ) 181 | AppendMenu( hMenu[TTERM], 0, ID_CONNECT0+iConnectCount++, wcmd); 182 | if ( wcsncmp(wcmd, L"script ", 7)==0 ) 183 | AppendMenu( hMenu[SCRIPT], 0, ID_SCRIPT0+iScriptCount++, wcmd+7); 184 | } 185 | void menu_Del(HMENU menu, int pos) 186 | { 187 | int id = GetMenuItemID(menu, pos); 188 | if ( id>=ID_SCRIPT0 ) { 189 | WCHAR wcmd[264] = L"!script "; 190 | GetMenuString(menu, pos, wcmd+(idstatus!=IDLE ) { 251 | cmd[cnt-1] = '\r'; 252 | term_Send(pt, cmd, cnt); 253 | } 254 | else { 255 | if ( *cmd ) { 256 | term_Print(pt, "\033[33m%s\r\n", cmd); 257 | host_Open(ph, cmd); 258 | } 259 | else 260 | host_Open(ph, NULL); 261 | } 262 | } 263 | } 264 | WNDPROC wpOrigCmdProc; 265 | LRESULT APIENTRY CmdEditProc(HWND hwnd, UINT uMsg, 266 | WPARAM wParam, LPARAM lParam) 267 | { 268 | WCHAR wcmd[256]; 269 | if ( uMsg==WM_KEYDOWN ) 270 | { 271 | if ( GetKeyState(VK_CONTROL) & 0x8000 ) //CTRL+ 272 | { 273 | char cmd = 0; 274 | if ( wParam==54 ) cmd = 30; //^ 275 | if ( wParam>64 && wParam<91 ) cmd = wParam-64; //A-Z 276 | if ( wParam>218&&wParam<222 ) cmd = wParam-192; //[\] 277 | if ( cmd ) { 278 | term_Send(pt, &cmd, 1); 279 | return 1; 280 | } 281 | } 282 | else 283 | { 284 | switch ( wParam ) { 285 | case VK_UP: 286 | cmd_Disp(autocomplete_Prev()); break; 287 | case VK_DOWN: 288 | cmd_Disp(autocomplete_Next()); break; 289 | case VK_BACK: 290 | if ( GetWindowText(hwndCmd, wcmd, 256)==0 ) { 291 | term_Send(pt, "\b", 1); 292 | return 1; 293 | } 294 | break; 295 | case VK_RETURN: 296 | if ( GetWindowText(hwndCmd, wcmd, 256)>=0 ) 297 | cmd_Enter(wcmd); 298 | break; 299 | } 300 | } 301 | } 302 | return CallWindowProc(wpOrigCmdProc, hwnd, uMsg, wParam, lParam); 303 | } 304 | 305 | char *tiny_Gets(char *prompt, BOOL bEcho) 306 | { 307 | return ssh2_Gets(ph, prompt, bEcho); 308 | } 309 | static BOOL redraw_pending=FALSE; 310 | void tiny_Redraw() 311 | { 312 | redraw_pending = TRUE; 313 | } 314 | void tiny_Title(char *buf) 315 | { 316 | utf8_to_wchar(buf, -1, wndTitle+50, 200); 317 | SetWindowText(hwndTerm, wndTitle); 318 | MENUITEMINFOA menuitem = { sizeof(MENUITEMINFOA) }; 319 | GetMenuItemInfoA(hMenu[0], ID_CONNECT, FALSE, &menuitem); 320 | if ( ph->status==IDLE ) 321 | { 322 | if ( bLocalEdit ) term_Disp(pt, TINYTERM); 323 | menuitem.dwTypeData = "Connect..."; 324 | } 325 | else { 326 | menuitem.dwTypeData = "Disonnect"; 327 | } 328 | menuitem.fMask = MIIM_TYPE | MIIM_DATA; 329 | SetMenuItemInfoA(hMenu[0], ID_CONNECT, FALSE, &menuitem); 330 | menu_Check(ID_ECHO, pt->bEcho); 331 | } 332 | BOOL tiny_Scroll(BOOL bShowScroll, int cy, int sy) 333 | { 334 | BOOL rt = FALSE; 335 | if ( bShowScroll ) { 336 | SetScrollRange(hwndTerm, SB_VERT, 0, cy, TRUE); 337 | SetScrollPos(hwndTerm, SB_VERT, sy, TRUE); 338 | if ( !bScrollbar ) { 339 | bScrollbar = TRUE; 340 | ShowScrollBar(hwndTerm, SB_VERT, TRUE); 341 | rt = TRUE; //first pageup fix 342 | } 343 | } 344 | else { 345 | if ( bScrollbar ) { 346 | bScrollbar = FALSE; 347 | ShowScrollBar(hwndTerm, SB_VERT, FALSE); 348 | } 349 | } 350 | tiny_Redraw(); 351 | return rt; 352 | } 353 | void tiny_Beep() 354 | { 355 | PlaySound(L"Default Beep", NULL, SND_ALIAS|SND_ASYNC); 356 | } 357 | //wnd_Size: adjust window size when fontface/fontsize or size_x/size_y changed 358 | void wnd_Size() 359 | { 360 | HDC hdc; 361 | TEXTMETRIC tm; 362 | hdc = GetDC(hwndTerm); 363 | SelectObject(hdc, hTermFont); 364 | GetTextMetrics(hdc, &tm); 365 | ReleaseDC(hwndTerm, hdc); 366 | iFontHeight = tm.tmHeight; 367 | iFontWidth = tm.tmAveCharWidth; 368 | GetWindowRect( hwndTerm, &wndRect ); 369 | int x = wndRect.left; 370 | int y = wndRect.top; 371 | wndRect.right = x + iFontWidth*pt->size_x; 372 | wndRect.bottom = y + iFontHeight*pt->size_y; 373 | AdjustWindowRect(&wndRect, WS_TILEDWINDOW, FALSE); 374 | MoveWindow( hwndTerm, x, y, wndRect.right-wndRect.left, 375 | wndRect.bottom-wndRect.top, TRUE ); 376 | } 377 | void font_Size() 378 | { 379 | DeleteObject(hTermFont); 380 | hTermFont = CreateFont(fontSize*dpi/96,0,0,0,FW_MEDIUM,FALSE,FALSE,FALSE, 381 | DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, 382 | DEFAULT_QUALITY, FIXED_PITCH, fontFace); 383 | SendMessage( hwndCmd, WM_SETFONT, (WPARAM)hTermFont, TRUE ); 384 | wnd_Size(); 385 | } 386 | 387 | void tiny_Paint(HDC hDC, RECT rcPaint) 388 | { 389 | WCHAR wbuf[1024]; 390 | RECT text_rect = {0, 0, 0, 0}; 391 | SelectObject(hDC, hTermFont); 392 | int y = pt->screen_y; 393 | int sel_min = min(pt->sel_left, pt->sel_right); 394 | int sel_max = max(pt->sel_left, pt->sel_right); 395 | int dx, dy=rcPaint.top; 396 | 397 | redraw_pending = FALSE; 398 | for ( int l=dy/iFontHeight; lsize_y; l++ ) { 399 | dx = 0; 400 | int i = pt->line[y+l]; 401 | while ( iline[y+l+1] ) { 402 | BOOL utf8 = FALSE; 403 | int j = i; 404 | while ( pt->attr[j]==pt->attr[i] ) { 405 | if ( (pt->buff[j]&0xc0)==0xc0 ) utf8 = TRUE; 406 | if ( ++j==pt->line[y+l+1] ) break; 407 | if ( j==sel_min || j==sel_max ) break; 408 | } 409 | if ( i>=sel_min&&iattr[i]&0x0f]); 415 | SetBkColor(hDC, COLORS[(pt->attr[i]>>4)&0x0f]); 416 | } 417 | int len = j-i; 418 | if ( pt->buff[j-1]==0x0a ) len--; //remove unprintable 0x0a for XP 419 | if ( utf8 ) { 420 | int cnt = utf8_to_wchar(pt->buff+i, len, wbuf, 1024); 421 | TextOutW(hDC, dx, dy, wbuf, cnt); 422 | DrawText(hDC, wbuf, cnt, &text_rect, DT_CALCRECT|DT_NOPREFIX); 423 | dx += text_rect.right; 424 | } 425 | else { 426 | TextOutA(hDC, dx, dy, pt->buff+i, len); 427 | dx += iFontWidth*len; 428 | } 429 | i=j; 430 | } 431 | if ( dx < termRect.right ) { 432 | RECT fillRect; 433 | fillRect.top = dy; 434 | fillRect.bottom = dy+iFontHeight; 435 | fillRect.left = dx; 436 | fillRect.right = termRect.right; 437 | FillRect(hDC, &fillRect, dwBkBrush); 438 | } 439 | dy += iFontHeight; 440 | } 441 | 442 | int cnt = utf8_to_wchar(pt->buff+pt->line[pt->cursor_y], 443 | pt->cursor_x-pt->line[pt->cursor_y], wbuf, 1024); 444 | if ( cnt>0 ) 445 | DrawText(hDC, wbuf, cnt, &text_rect, DT_CALCRECT|DT_NOPREFIX); 446 | else 447 | text_rect.right = 0;//DrawText won't work when wbuf is zero length 448 | 449 | if ( bLocalEdit && !pt->bAlterScreen && host_Status(ph)!=AUTHENTICATING ) { 450 | MoveWindow(hwndCmd, text_rect.right, 451 | (pt->cursor_y-pt->screen_y)*iFontHeight, 452 | termRect.right-text_rect.right, iFontHeight, TRUE); 453 | SetFocus(hwndCmd); 454 | } 455 | else { 456 | MoveWindow(hwndCmd, 0, 0, 1, 1, TRUE); 457 | SetFocus(hwndTerm); 458 | SetCaretPos(text_rect.right+1, 459 | (pt->cursor_y-pt->screen_y)*iFontHeight+iFontHeight*3/4); 460 | if ( pt->bCursor && bFocus) 461 | ShowCaret(hwndTerm); 462 | else 463 | HideCaret(hwndTerm); 464 | } 465 | } 466 | const WCHAR *PROTOCOLS[]={L"Serial ", L"telnet ", L"ssh ", L"sftp ", L"netconf "}; 467 | const WCHAR *PORTS[] ={L"2024", L"23", L"22", L"22", L"830"}; 468 | const WCHAR *SETTINGS[] ={L"9600,n,8,1", L"19200,n,8,1", L"38400,n,8,1", 469 | L"57600,n,8,1", L"115200,n,8,1"}; 470 | void get_serial_ports(HWND hwndPort) 471 | { 472 | for ( int i=1; i<32; i++ ) { 473 | WCHAR port[32]; 474 | wsprintf(port, L"\\\\.\\COM%d", i); 475 | HANDLE hPort = CreateFile(port, GENERIC_READ, 0, NULL, 476 | OPEN_EXISTING, 0, NULL); 477 | if ( hPort != INVALID_HANDLE_VALUE ) { 478 | ComboBox_AddString(hwndPort,port+4); 479 | CloseHandle( hPort ); 480 | } 481 | } 482 | } 483 | static WCHAR last_host[128] = L"192.168.1.1"; 484 | void get_hosts(HWND hwndHost) 485 | { 486 | ComboBox_AddString(hwndHost, last_host); 487 | for ( int id=ID_CONNECT0; idstatus==IDLE ) 651 | DialogBox(hInst, MAKEINTRESOURCE(IDD_CONNECT), 652 | hwndTerm, (DLGPROC)ConnectProc); 653 | else 654 | host_Close(ph); 655 | break; 656 | case ID_LOGG: 657 | if ( !pt->bLogging ) { 658 | WCHAR *wfn = fileDialog(L"logfile\0*.log\0All\0*.*\0\0", 659 | OFN_PATHMUSTEXIST|OFN_NOREADONLYRETURN|OFN_OVERWRITEPROMPT); 660 | if ( wfn!=NULL ) { 661 | char fn[MAX_PATH]; 662 | wchar_to_utf8(wfn, wcslen(wfn)+1, fn, MAX_PATH); 663 | term_Logg(pt, fn); 664 | } 665 | } 666 | else 667 | term_Logg( pt, NULL ); 668 | menu_Check( ID_LOGG, pt->bLogging ); 669 | break; 670 | case ID_SELALL: 671 | pt->sel_left = 0; 672 | pt->sel_right = pt->cursor_x; 673 | tiny_Redraw(); 674 | break; 675 | case ID_COPY: 676 | if ( OpenClipboard(hwndTerm) ) { 677 | EmptyClipboard(); 678 | char *ptr; 679 | int len = term_Copy(pt, &ptr); 680 | HANDLE hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (len+1)*2); 681 | if ( hglbCopy!=NULL && len>0) { 682 | WCHAR *wbuf = GlobalLock(hglbCopy); 683 | len = utf8_to_wchar(ptr, len, wbuf, len); 684 | wbuf[len] = 0; 685 | GlobalUnlock(hglbCopy); 686 | SetClipboardData(CF_UNICODETEXT, hglbCopy); 687 | } 688 | CloseClipboard(); 689 | } 690 | break; 691 | case ID_PASTE: 692 | if ( OpenClipboard(hwndTerm) ) { 693 | HANDLE hglb = GetClipboardData(CF_UNICODETEXT); 694 | WCHAR *ptr = (WCHAR *)GlobalLock(hglb); 695 | if (ptr != NULL) { 696 | int len = wchar_to_utf8(ptr, -1, NULL, 0); 697 | char *p = (char *)malloc(len); 698 | if ( p!=NULL ) { 699 | wchar_to_utf8(ptr, -1, p, len); 700 | term_Paste(pt, p, len); 701 | free(p); 702 | } 703 | GlobalUnlock(hglb); 704 | } 705 | CloseClipboard(); 706 | } 707 | break; 708 | case ID_MIDDLE: 709 | term_Mouse(pt, MIDDLEUP, 0, 0); 710 | break; 711 | case ID_DELETE: { 712 | WCHAR wcmd[256]; 713 | if ( GetWindowText(hwndCmd, wcmd, 256)>0 ) { 714 | autocomplete_Del(wcmd); 715 | cmd_Disp(autocomplete_Next()); 716 | } 717 | break; 718 | } 719 | case ID_TAB: 720 | if ( bLocalEdit ) { 721 | char cmd[256]; 722 | WCHAR wcmd[256]; 723 | Edit_ReplaceSel(hwndCmd, L""); 724 | GetWindowText(hwndCmd, wcmd, 256); 725 | SetWindowText(hwndCmd, L""); 726 | int cnt = wchar_to_utf8(wcmd, -1, cmd, 256); 727 | if ( cnt>0 ) term_Send(pt, cmd, cnt-1); 728 | } 729 | term_Send(pt, "\t", 1); 730 | break; 731 | case ID_PRIOR: term_Scroll(pt, pt->size_y-1); break; 732 | case ID_NEXT: term_Scroll(pt, 1-pt->size_y); break; 733 | case ID_ECHO: menu_Check(ID_ECHO, term_Echo(pt)); break; 734 | case ID_EDIT: 735 | bLocalEdit = !bLocalEdit; 736 | menu_Check(ID_EDIT, bLocalEdit); 737 | if ( bLocalEdit && ph->status==IDLE ) term_Disp(pt, TINYTERM); 738 | tiny_Redraw(); 739 | break; 740 | case ID_TRANSP: 741 | if ( lParam>0 && lParam<256 ) 742 | iTransparency = lParam; 743 | else 744 | iTransparency = (iTransparency==255) ? 224 : 255; 745 | SetLayeredWindowAttributes(hwndTerm, 0, iTransparency, LWA_ALPHA); 746 | menu_Check( ID_TRANSP, iTransparency!=255 ); 747 | break; 748 | case ID_FONT: 749 | if ( fontDialog() ) wnd_Size(); 750 | break; 751 | case ID_FTPD: 752 | bFTPd = ftp_Svr(bFTPd?NULL:getFolderName(L"Choose root directory")); 753 | menu_Check( ID_FTPD, bFTPd ); 754 | break; 755 | case ID_TFTPD: 756 | bTFTPd = tftp_Svr(bTFTPd?NULL:getFolderName(L"Choose root directory")); 757 | menu_Check( ID_TFTPD, bTFTPd ); 758 | break; 759 | case ID_RUN: { 760 | WCHAR *wfn=fileDialog(L"Script\0*.html;*.js;*.vbs;*.txt\0All\0*.*\0\0", 761 | OFN_FILEMUSTEXIST); 762 | if ( wfn!=NULL ) { 763 | WCHAR wcwd[MAX_PATH]; 764 | _wgetcwd(wcwd, MAX_PATH); 765 | int len = wcslen(wcwd); 766 | if ( wcsncmp(wcwd, wfn, len)==0 ) wfn+=len+1; 767 | OpenScript(wfn); 768 | } 769 | break; 770 | } 771 | case ID_PAUSE: 772 | show_script_dialog(); 773 | break; 774 | case ID_QUIT: 775 | hide_script_dialog(); 776 | break; 777 | case ID_TERM: 778 | menu_Popup(TTERM); 779 | break; 780 | case ID_SCRIPT: 781 | menu_Popup(SCRIPT); 782 | break; 783 | case ID_OPTIONS: 784 | menu_Popup(OPTION); 785 | break; 786 | default: 787 | if ( wParam>=ID_SCRIPT0 && wParam=ID_CONNECT0 && wParamstatus==IDLE ) {//press Enter to reconnect 880 | if ( (wParam&0xff)==0x0d ) host_Open(ph, NULL); 881 | } 882 | else { 883 | if ( (wParam>>8)==0 ) { 884 | char key = wParam&0xff; 885 | term_Send(pt, &key, 1); 886 | } 887 | else { 888 | char utf8[6], ho = wParam>>8; 889 | if ( (ho&0xF8)!=0xD8 ) { 890 | int c = wchar_to_utf8((WCHAR *)&wParam, 1, utf8, 6); 891 | if ( c>0 ) term_Send(pt, utf8, c); 892 | } 893 | else { 894 | if ( (ho&0xDC)==0xD8 ) 895 | wm_chars[0] = wParam; //high surrogate word 896 | else 897 | wm_chars[1] = wParam; //low surrogate word 898 | if ( wm_chars[1]!=0 && wm_chars[0]!=0 ) { 899 | int c = wchar_to_utf8(wm_chars, 2, utf8, 6); 900 | if ( c>0 ) term_Send(pt, utf8, c); 901 | wm_chars[0] = 0; 902 | wm_chars[1] = 0; 903 | } 904 | } 905 | } 906 | } 907 | break; 908 | case WM_KEYDOWN: 909 | switch( wParam ) { 910 | case VK_DELETE:term_Send(pt, "\177",1); break; 911 | case VK_UP: term_Send(pt, pt->bAppCursor?"\033OA":"\033[A",3); break; 912 | case VK_DOWN: term_Send(pt, pt->bAppCursor?"\033OB":"\033[B",3); break; 913 | case VK_RIGHT: term_Send(pt, pt->bAppCursor?"\033OC":"\033[C",3); break; 914 | case VK_LEFT: term_Send(pt, pt->bAppCursor?"\033OD":"\033[D",3); break; 915 | case VK_HOME: term_Send(pt, pt->bAppCursor?"\033OH":"\033[H",3); break; 916 | case VK_END: term_Send(pt, pt->bAppCursor?"\033OF":"\033[F",3); break; 917 | } 918 | if ( bScrollbar ) 919 | term_Scroll(pt, pt->screen_y-(pt->cursor_y-pt->size_y+1)); 920 | break; 921 | case WM_VSCROLL: 922 | switch ( LOWORD (wParam) ) 923 | { 924 | case SB_LINEUP: term_Scroll(pt, 1); break; 925 | case SB_LINEDOWN: term_Scroll(pt, -1); break; 926 | case SB_PAGEUP: term_Scroll(pt, pt->size_y-1); break; 927 | case SB_PAGEDOWN: term_Scroll(pt, 1-pt->size_y); break; 928 | case SB_THUMBTRACK: { 929 | SCROLLINFO si; 930 | si.cbSize = sizeof (si); 931 | si.fMask = SIF_ALL; 932 | GetScrollInfo (hwnd, SB_VERT, &si); 933 | term_Scroll(pt, si.nPos-si.nTrackPos); 934 | } 935 | } 936 | break; 937 | case WM_MOUSEWHEEL: 938 | term_Scroll(pt, GET_WHEEL_DELTA_WPARAM(wParam)/40); 939 | break; 940 | case WM_LBUTTONDBLCLK: 941 | term_Mouse(pt, DOUBLECLK, GET_X_LPARAM(lParam)/iFontWidth, 942 | (GET_Y_LPARAM(lParam)+2)/iFontHeight); 943 | break; 944 | case WM_LBUTTONDOWN: 945 | term_Mouse(pt, LEFTDOWN, GET_X_LPARAM(lParam)/iFontWidth, 946 | (GET_Y_LPARAM(lParam)+2)/iFontHeight); 947 | SetCapture(hwnd); 948 | break; 949 | case WM_MOUSEMOVE: 950 | if ( MK_LBUTTON&wParam ) { 951 | term_Mouse(pt, LEFTDRAG, GET_X_LPARAM(lParam)/iFontWidth, 952 | (GET_Y_LPARAM(lParam)+2)/iFontHeight); 953 | } 954 | break; 955 | case WM_LBUTTONUP: 956 | term_Mouse(pt, LEFTUP, GET_X_LPARAM(lParam)/iFontWidth, 957 | (GET_Y_LPARAM(lParam)+2)/iFontHeight); 958 | 959 | ReleaseCapture(); 960 | break; 961 | case WM_MBUTTONUP: 962 | term_Mouse(pt, MIDDLEUP, GET_X_LPARAM(lParam)/iFontWidth, 963 | (GET_Y_LPARAM(lParam)+2)/iFontHeight); 964 | break; 965 | case WM_CONTEXTMENU: 966 | TrackPopupMenu(hMenu[CONTEX], TPM_LEFTBUTTON, GET_X_LPARAM(lParam), 967 | GET_Y_LPARAM(lParam), 0, hwndTerm, NULL); 968 | break; 969 | case WM_NCLBUTTONDOWN: { 970 | int y = GET_Y_LPARAM(lParam)-wndRect.top; 971 | if ( y>0 && ymenuX[0] && x0 && ymenuX[i] && xtype==SSH || ph->type==SFTP ) 995 | DropFiles((HDROP)wParam); 996 | if ( ph->type==SERIAL ) 997 | DropXmodem((HDROP)wParam); 998 | break; 999 | case WM_CTLCOLOREDIT: 1000 | SetTextColor((HDC)wParam, COLORS[3]); 1001 | SetBkColor((HDC)wParam, COLORS[0]); 1002 | return (LRESULT)dwBkBrush; //must return brush for update 1003 | case WM_CLOSE: 1004 | if ( ph->status!=IDLE ) { 1005 | if ( MessageBox(hwnd, L"Disconnect and quit?", 1006 | L"tinyTerm", MB_YESNO)==IDNO ) break; 1007 | host_Close(ph); 1008 | while ( ph->status!=IDLE ) Sleep(100); 1009 | } 1010 | if ( pt->bLogging ) term_Logg(pt, NULL); 1011 | SaveDict(); 1012 | autocomplete_Destroy(); 1013 | DestroyMenu(hMainMenu); 1014 | DeleteObject(dwBkBrush); 1015 | DragAcceptFiles(hwnd, FALSE); 1016 | drop_Destroy(hwnd); 1017 | DestroyWindow(hwnd); 1018 | break; 1019 | case WM_DESTROY: 1020 | PostQuitMessage(0); 1021 | break; 1022 | case WM_COMMAND: 1023 | case WM_SYSCOMMAND: 1024 | if ( menu_Command(wParam, lParam) ) return 1; 1025 | default: return DefWindowProc(hwnd,msg,wParam,lParam); 1026 | } 1027 | return 0; 1028 | } 1029 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 1030 | LPSTR lpCmdLine, INT nCmdShow) 1031 | { 1032 | WNDCLASSEX wc; 1033 | wc.cbSize = sizeof(wc); 1034 | wc.style = CS_DBLCLKS; 1035 | wc.cbClsExtra = 0; 1036 | wc.cbWndExtra = 0; 1037 | wc.lpfnWndProc = &MainWndProc; 1038 | wc.hInstance = hInstance; 1039 | wc.hIcon = 0; 1040 | wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDICON_TL1)); 1041 | wc.hCursor = LoadCursor(NULL,IDC_ARROW); 1042 | wc.hbrBackground = 0; 1043 | wc.lpszClassName = L"TWnd"; 1044 | wc.lpszMenuName = 0; 1045 | if ( !RegisterClassEx(&wc) ) return 0; 1046 | hInst = hInstance; 1047 | 1048 | OleInitialize(NULL); 1049 | WSADATA wsaData; 1050 | WSAStartup(MAKEWORD(2, 2), &wsaData); 1051 | libssh2_init(0); 1052 | httport = http_Svr("127.0.0.1"); 1053 | 1054 | TERM term; 1055 | HOST host; 1056 | pt = &term; 1057 | ph = &host; 1058 | if ( !term_Construct(pt) ) return 0; 1059 | host_Construct(ph); 1060 | term.host = ph; 1061 | host.term = pt; 1062 | 1063 | HDC sysDC = GetDC(0); 1064 | dpi = GetDeviceCaps(sysDC, LOGPIXELSX); 1065 | ReleaseDC(0, sysDC); 1066 | dwBkBrush = (HBRUSH)GetStockObject(BLACK_BRUSH); 1067 | hMainMenu = LoadMenu( hInst, MAKEINTRESOURCE(IDMENU_MAIN)); 1068 | for ( int i=0; i<4; i++ ) hMenu[i] = GetSubMenu(hMainMenu, i); 1069 | hTermFont = CreateFont(fontSize*dpi/96, 0, 0, 0, 1070 | FW_MEDIUM, FALSE, FALSE, FALSE, 1071 | DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, 1072 | DEFAULT_QUALITY, FIXED_PITCH, fontFace); 1073 | hwndTerm = CreateWindowEx(WS_EX_LAYERED,L"TWnd", wndTitle, 1074 | WS_TILEDWINDOW|WS_VSCROLL, 1075 | CW_USEDEFAULT, CW_USEDEFAULT, 1076 | CW_USEDEFAULT, CW_USEDEFAULT, 1077 | NULL, NULL, hInst, NULL ); 1078 | ShowScrollBar(hwndTerm, SB_VERT, FALSE); 1079 | 1080 | HACCEL haccel = LoadAccelerators(hInst, MAKEINTRESOURCE(IDACCEL_MAIN)); 1081 | MSG msg; 1082 | while ( GetMessage(&msg, NULL, 0, 0) ) 1083 | { 1084 | if ( IsWindow(hwndScriptDlg) && IsDialogMessage(hwndScriptDlg, &msg) ) 1085 | continue; 1086 | if (!TranslateAccelerator(hwndTerm, haccel, &msg)) 1087 | { 1088 | TranslateMessage(&msg); 1089 | DispatchMessage(&msg); 1090 | } 1091 | } 1092 | 1093 | term_Destruct(pt); 1094 | http_Svr("127.0.0.1"); 1095 | libssh2_exit(); 1096 | WSACleanup(); 1097 | OleUninitialize(); 1098 | 1099 | return 0; 1100 | } 1101 | const char *DICTFILE="tinyTerm.hist"; 1102 | void LoadDict( ) 1103 | { 1104 | FILE *fp = fopen(DICTFILE, "r"); 1105 | if ( fp==NULL ) { 1106 | if ( _chdir(getenv("USERPROFILE"))==0 ) { 1107 | _mkdir("Documents\\tinyTerm"); 1108 | _chdir("Documents\\tinyTerm"); 1109 | fp = fopen(DICTFILE, "r"); 1110 | } 1111 | } 1112 | if ( fp!=NULL ) { 1113 | char cmd[256]; 1114 | while ( fgets(cmd, 256, fp)!=NULL ) { 1115 | cmd[255] = 0; 1116 | cmd[strcspn(cmd, "\n")] = 0; //remove trailing new line 1117 | if ( *cmd=='~' ) { 1118 | if (strncmp(cmd,"~Transparency",13)==0) { 1119 | iTransparency = atoi(cmd+14); 1120 | if ( iTransparency!=255 ) 1121 | CheckMenuItem(hMainMenu, ID_TRANSP, 1122 | MF_BYCOMMAND|MF_CHECKED); 1123 | } 1124 | else if ( strncmp(cmd, "~LocalEdit", 10)==0) { 1125 | bLocalEdit = TRUE; 1126 | menu_Check(ID_EDIT, TRUE); 1127 | } 1128 | else if ( strncmp(cmd, "~FontSize", 9)==0 ) { 1129 | fontSize = atoi(cmd+9); 1130 | } 1131 | else if ( strncmp(cmd, "~FontFace", 9)==0 ) { 1132 | utf8_to_wchar(cmd+10, -1, fontFace, 32); 1133 | fontFace[31] = 0; 1134 | } 1135 | else if ( strncmp(cmd, "~TermSize", 9)==0 ) { 1136 | char *p = strchr(cmd+9, 'x'); 1137 | if ( p!=NULL ) { 1138 | pt->size_x = atoi(cmd+9); 1139 | pt->size_y = atoi(p+1); 1140 | } 1141 | } 1142 | } 1143 | else { 1144 | WCHAR wcmd[256]; 1145 | utf8_to_wchar(cmd, -1, wcmd, 256); 1146 | if( autocomplete_Add(wcmd) ) 1147 | if ( *cmd=='!' ) menu_Add(wcmd+1); 1148 | } 1149 | } 1150 | fclose( fp ); 1151 | } 1152 | } 1153 | void SaveDict( ) 1154 | { 1155 | FILE *fp = fopen(DICTFILE, "w"); 1156 | if ( fp!=NULL ) { 1157 | if ( fontSize!=16 ) 1158 | fprintf(fp, "~FontSize %d\n", fontSize); 1159 | if ( wcscmp(fontFace, L"Consolas")!=0 ) { 1160 | char fontface[32]; 1161 | wchar_to_utf8(fontFace, -1, fontface, 32); 1162 | fprintf(fp, "~FontFace %s\n", fontface); 1163 | } 1164 | if ( bLocalEdit ) 1165 | fprintf(fp, "~LocalEdit\n"); 1166 | if ( pt->size_x!=80 || pt->size_y!=25 ) 1167 | fprintf(fp, "~TermSize %dx%d\n", pt->size_x, pt->size_y); 1168 | if ( iTransparency!=255 ) 1169 | fprintf(fp, "~Transparency %d\n", iTransparency); 1170 | WCHAR *wp = autocomplete_First(); 1171 | while ( wp!=NULL ) { 1172 | char cmd[256]; 1173 | int n = wchar_to_utf8(wp, -1, cmd, 256); 1174 | if ( n>3 && *wp!='~' ) fprintf(fp, "%s\n", cmd); 1175 | wp = autocomplete_Next(); 1176 | } 1177 | fclose( fp ); 1178 | } 1179 | } 1180 | 1181 | DWORD WINAPI uploader(void *files) //upload files through scp or sftp 1182 | { 1183 | if ( bScriptRun ) { 1184 | MessageBox(hwndTerm, L"another script is running", L"Script", 1185 | MB_OK|MB_ICONSTOP); 1186 | free((char *)files); 1187 | return 0; 1188 | } 1189 | 1190 | for ( char *q=files; *q; q++ ) if ( *q=='\\' ) *q='/'; 1191 | term_Learn_Prompt(pt); 1192 | 1193 | char rdir[1024]; 1194 | if ( ph->type==SSH ) { 1195 | term_Pwd(pt, rdir, 1022); 1196 | rdir[1023] = 0; 1197 | strcat(rdir, "/"); 1198 | } 1199 | else 1200 | term_Disp(pt, "\n"); 1201 | 1202 | bScriptRun=TRUE; bScriptPause = FALSE; 1203 | PostMessage(hwndTerm, WM_COMMAND, ID_PAUSE, 0); 1204 | 1205 | char *p1=(char *)files, *p; 1206 | while ( bScriptRun && (p=p1)!=NULL ) { 1207 | if ( bScriptPause ) { Sleep(100); continue; } 1208 | if ( *p==0 ) break; 1209 | p1 = strchr(p, 0x0a); 1210 | if ( p1!=NULL ) *p1++=0; 1211 | if ( ph->type==SSH ) 1212 | scp_write(ph, p, rdir); 1213 | else 1214 | sftp_put(ph, p, ph->realpath); 1215 | } 1216 | bScriptRun = bScriptPause = FALSE; 1217 | PostMessage(hwndTerm, WM_COMMAND, ID_QUIT, 0); 1218 | 1219 | term_Send(pt, "\r", 1); 1220 | free((char *)files); 1221 | return 0; 1222 | } 1223 | void DropFiles(HDROP hDrop) 1224 | { 1225 | WCHAR wname[MAX_PATH]; 1226 | int n = DragQueryFile(hDrop, -1, NULL, 0); 1227 | char *files = (char*)malloc(n*1024); 1228 | if ( files==NULL ) return; 1229 | 1230 | int len = 0; 1231 | for ( int i=0; i0 ) { 1263 | if ( (--iWaitCnt)%10==0 ) { 1264 | WCHAR wait[16]; 1265 | wsprintf(wait, L"Wait %d ", iWaitCnt/10); 1266 | cmd_Disp(iWaitCnt==0 ? L"": wait); 1267 | } 1268 | } 1269 | if ( iWaitCnt || bScriptPause ) { Sleep(100); continue; } 1270 | 1271 | p1=strchr(p0, 0x0a); 1272 | int len = ( p1==NULL ? strlen(p0) : (p1++)-p0 ); 1273 | 1274 | if ( strncmp(p0, "!Wait ", 6)==0 ) { 1275 | iWaitCnt = atoi(p0+6)*10; 1276 | } 1277 | else if ( strncmp(p0, "!Loop ", 6)==0 ) { 1278 | if ( iLoopCnt<0 ) iLoopCnt = atoi(p0+6); 1279 | if ( --iLoopCnt>0 ) p1 = (char*)cmds; 1280 | } 1281 | else if ( *p0=='!' ) { 1282 | char cmd[256], *reply; 1283 | strncpy(cmd, p0, len); 1284 | if ( cmd[len-1]=='\r' ) cmd[len-1]=0; 1285 | term_Cmd(pt, cmd, &reply); 1286 | } 1287 | else { 1288 | if ( ph->status==IDLE ) { 1289 | term_Parse(pt, p0, len+1); 1290 | } 1291 | else if ( len>0 && *p0!='#' ) { 1292 | term_Mark_Prompt(pt); 1293 | term_Send(pt, p0, len); 1294 | term_Waitfor_Prompt(pt); 1295 | } 1296 | } 1297 | p0 = p1; 1298 | } 1299 | if ( iWaitCnt>0 ) cmd_Disp(L""); 1300 | bScriptRun = bScriptPause = FALSE; 1301 | PostMessage(hwndTerm, WM_COMMAND, ID_QUIT, 0); 1302 | 1303 | free((char *)cmds); 1304 | return 0; 1305 | } 1306 | void DropScript(char *cmds) 1307 | { 1308 | if ( ph->type==NETCONF ) { 1309 | term_Send(pt, cmds, strlen(cmds)); 1310 | free(cmds); 1311 | } 1312 | else { 1313 | term_Learn_Prompt(pt); 1314 | CreateThread(NULL, 0, scripter, (void *)cmds, 0, NULL); 1315 | } 1316 | } 1317 | void OpenScript(WCHAR *wfn) 1318 | { 1319 | WCHAR wport[MAX_PATH]; 1320 | wsprintf(wport, L"!script %s", wfn); 1321 | if ( autocomplete_Add(wport) ) menu_Add(wport+1); 1322 | 1323 | term_Learn_Prompt(pt); 1324 | int len = wcslen(wfn); 1325 | if ( wcscmp(wfn+len-5, L".html")==0) { 1326 | wsprintf(wport, L"http://127.0.0.1:%d/%s", httport, wfn); 1327 | ShellExecute(NULL, L"Open", wport, NULL, NULL, SW_SHOW); 1328 | } 1329 | else { 1330 | wsprintf(wport, L"%d", httport); 1331 | ShellExecute(NULL, L"Open", wfn, wport, NULL, SW_SHOW); 1332 | } 1333 | } -------------------------------------------------------------------------------- /tiny.h: -------------------------------------------------------------------------------- 1 | // 2 | // "$Id: tiny.h 5703 2020-08-31 12:05:10 $" 3 | // 4 | // tinyTerm -- A minimal serail/telnet/ssh/sftp terminal emulator 5 | // 6 | // tiny.h has function declarations for all .c files. 7 | // 8 | // Copyright 2018-2020 by Yongchao Fan. 9 | // 10 | // This library is free software distributed under GNU GPL 3.0, 11 | // see the license at: 12 | // 13 | // https://github.com/yongchaofan/tinyTerm/blob/master/LICENSE 14 | // 15 | // Please report all bugs and problems on the following page: 16 | // 17 | // https://github.com/yongchaofan/tinyTerm/issues/new 18 | // 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #define MAXLINES 16384 25 | #define BUFFERSIZE 16384*64 26 | 27 | struct Tunnel 28 | { 29 | int socket; 30 | char *localip; 31 | char *remoteip; 32 | unsigned short localport; 33 | unsigned short remoteport; 34 | LIBSSH2_CHANNEL *channel; 35 | struct Tunnel *next; 36 | struct tagHOST *host; 37 | }; 38 | 39 | typedef struct tagHOST { 40 | int type; 41 | int status; 42 | 43 | char cmdline[256]; 44 | char tunline[256]; 45 | SOCKET sock; //for tcp/ssh/sftp reader 46 | short port; 47 | HANDLE hExitEvent, hSerial; //for serial reader 48 | HANDLE hStdioRead, hStdioWrite; //for stdio reader 49 | 50 | char *subsystem; 51 | char *username; 52 | char *password; 53 | char *passphrase; 54 | char *hostname; 55 | char homedir[MAX_PATH]; 56 | char keys[256]; 57 | int cursor; 58 | BOOL bReturn, bPassword, bGets; 59 | 60 | HANDLE mtx; //ssh2 reading/writing mutex 61 | LIBSSH2_SESSION *session; 62 | LIBSSH2_CHANNEL *channel; 63 | HANDLE mtx_tun; //tunnel list add/delete mutex 64 | struct Tunnel *tunnel_list; 65 | 66 | LIBSSH2_SFTP *sftp; //sftp host 67 | char homepath[MAX_PATH]; 68 | char realpath[MAX_PATH]; 69 | int sftp_running; 70 | 71 | struct tagTERM *term; 72 | } HOST; 73 | 74 | typedef struct tagTERM { 75 | char *buff, *attr, c_attr, save_attr; 76 | int *line; 77 | int size_x, size_y; 78 | int cursor_x, cursor_y; 79 | int screen_y; 80 | int sel_left, sel_right; 81 | BOOL bLogging, bEcho, bCursor, bAlterScreen; 82 | BOOL bAppCursor, bGraphic, bEscape, bTitle, bInsert; 83 | BOOL bBracket, bOriginMode, bWraparound;//bracketed paste mode 84 | int save_x, save_y; 85 | int roll_top, roll_bot; 86 | HANDLE mtx; //term parse mutex 87 | 88 | char title[64]; 89 | int title_idx; 90 | FILE *fpLogFile; 91 | 92 | BOOL bPrompt; 93 | char sPrompt[32]; 94 | int iPrompt, iTimeOut; 95 | char *tl1text; 96 | int tl1len; 97 | 98 | int escape_idx; 99 | char escape_code[32]; 100 | char tabstops[256]; 101 | 102 | int xmlIndent; 103 | BOOL xmlPreviousIsOpen; 104 | 105 | HOST *host; 106 | } TERM; 107 | enum hostStatus {IDLE, CONNECTING, AUTHENTICATING, CONNECTED}; 108 | enum hostType {NONE, STDIO, SERIAL, TELNET, SSH, SFTP, NETCONF}; 109 | enum mouseEvents {DOUBLECLK, RIGHTCLK, LEFTDOWN, LEFTDRAG, LEFTUP, MIDDLEUP}; 110 | 111 | /****************auto_drop.c*************/ 112 | void drop_Init(HWND hwnd, void (*handler)(char *)); 113 | void drop_Destroy(HWND hwnd); 114 | int autocomplete_Init(HWND hWnd); 115 | int autocomplete_Destroy(); 116 | int autocomplete_Add(WCHAR *cmd); 117 | int autocomplete_Del(WCHAR *cmd); 118 | WCHAR *autocomplete_First(); 119 | WCHAR *autocomplete_Next(); 120 | WCHAR *autocomplete_Prev(); 121 | 122 | /****************host.c****************/ 123 | void host_Construct(HOST *ph); 124 | void host_Open(HOST *ph, char *port); 125 | void host_Send_Size(HOST *ph, int w, int h); 126 | void host_Send(HOST *ph, char *buf, int len); 127 | void host_Close(HOST *ph); 128 | int host_Type(HOST *ph); 129 | int host_Status(HOST *ph); 130 | int host_tcp(HOST *ph); 131 | void xmodem_init(HOST *ph, FILE *fp); 132 | 133 | int url_decode(char *url); 134 | int http_Svr(char *intf); 135 | BOOL ftp_Svr(char *root); 136 | BOOL tftp_Svr(char *root); 137 | 138 | /****************ssh2.c****************/ 139 | void ssh2_Construct(HOST *ph); 140 | void ssh2_Size(HOST *ph, int w, int h); 141 | void ssh2_Send(HOST *ph, char *buf, int len); 142 | char *ssh2_Gets(HOST *ph, char *prompt, BOOL bEcho); 143 | void ssh2_Close(HOST *ph); 144 | void ssh2_Tun(HOST *ph, char *cmd); 145 | void scp_read(HOST *ph, char *lpath, char *rfiles); 146 | void scp_write(HOST *ph, char *lpath, char *rpath); 147 | void sftp_put(HOST *ph, char *src, char *dst); 148 | void sftp_Close(HOST *ph); 149 | 150 | /****************term.c****************/ 151 | void host_callback( void *term, char *buf, int len); 152 | BOOL term_Construct(TERM *pt); 153 | void term_Destruct(TERM *pt); 154 | void term_Size(TERM *pt, int x, int y); 155 | void term_Title(TERM *pt, char *title); 156 | void term_Error(TERM *pt, char *error); 157 | void term_Scroll(TERM* pt, int lines); 158 | void term_Mouse(TERM *pt, int evt, int x, int y); 159 | void term_Print(TERM *pt, const char *fmt, ...); 160 | void term_Parse(TERM *pt, const char *buf, int len); 161 | void term_Parse_XML(TERM *pt, const char *xml, int len); 162 | 163 | BOOL term_Echo(TERM *pt); 164 | void term_Logg(TERM *pt, char *fn); 165 | void term_Save(TERM *pt, char *fn); 166 | void term_Disp(TERM *pt, const char *buf); 167 | void term_Send(TERM *pt, char *buf, int len); 168 | void term_Paste(TERM *pt, char *buf, int len); 169 | int term_Copy(TERM *pt, char **buf); 170 | int term_Recv(TERM *pt, char **preply); 171 | int term_Srch(TERM *pt, char *sstr); 172 | 173 | void term_Learn_Prompt(TERM *pt); 174 | char *term_Mark_Prompt(TERM *pt); 175 | int term_Waitfor_Prompt(TERM *pt); 176 | int term_Pwd(TERM *pt, char *buf, int len); 177 | int term_Scp(TERM *pt, char *cmd, char **preply); 178 | int term_Tun(TERM *pt, char *cmd, char **preply); 179 | int term_Cmd(TERM *pt, char *cmd, char **preply); 180 | 181 | /****************tiny.c****************/ 182 | int utf8_to_wchar(const char *buf, int cnt, WCHAR *wbuf, int wcnt); 183 | int wchar_to_utf8(WCHAR *wbuf, int wcnt, char *buf, int cnt); 184 | int stat_utf8(const char *fn, struct _stat *buffer); 185 | FILE *fopen_utf8(const char *fn, const char *mode); 186 | 187 | void cmd_Disp_utf8(char *buf); 188 | void ftpd_quit(); 189 | void tftpd_quit(); 190 | void tiny_Beep(); 191 | void wnd_Size(); //resize window when font or term size changes 192 | void tiny_Redraw(); //redraw term window 193 | void tiny_Title(char *buf); 194 | BOOL tiny_Scroll(BOOL bShowScroll, int cy, int sy);//return if scrollbar is shown 195 | char *tiny_Gets(char *prompt, BOOL bEcho); 196 | --------------------------------------------------------------------------------