printf.exe: Arithmetic and Programming

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
lazna
Posts: 62
Joined: 27 Dec 2012 10:54

Re: printf.exe: Show formatted output AND evaluate arithmetic expressions!

#16 Post by lazna » 05 Aug 2022 13:08

Each data source (DS) in RRD file is represented by each column of `rrdtool fetch`output. There could be #IND string only in SOME columns on some line, not on ALL of them. Number of DS may differ across various RRD database files. So never could expect how many columns will be on fprint.exe input, but fprint translating expression MUST be aware of input format.

Therefor prior translating, querying RRD file by `rrdtool info` for list of its data sources, counting them and then assemble fprint translating expression accordingly. There are fixed 10 parameters with untranslated numbers at the and of fprint line, because batch scripts replace non-existing variable by nothing and i am trying keep it simple.

NOTE: Not only professional programmers dealing with windows shell scripts, but uneducated people sometimes too. For those `scientific notation`could be something like chinese chars or hieroglyphs. If I wrote human readable, I mean ALL HUMAN READABLE, not only enough educated ones...

BTW: for same reason is for me very hard to read documentation writen by higly educated people. No excuses, just sidenote..

lazna
Posts: 62
Joined: 27 Dec 2012 10:54

Re: printf.exe: Show formatted output AND evaluate arithmetic expressions!

#17 Post by lazna » 06 Aug 2022 02:41

Problem solved!

IN the for loop definition "tokens=1-5" remains from previous version, but now it process more tokens. This cause by later variable expansion the non existent tokens was replaced by its names instead it remain empty. It working now

Final version published in Cacti forum https://forums.cacti.net/viewtopic.php?t=62271#p288538

Thanks to Antonio for its patientce.

BTW: One more question: Why printf in case of problem does not report error message (plus set errorlevel) and terminate liek other consoel programs, but crash so windows display GUI msg it must be clicked by mouse?

lazna
Posts: 62
Joined: 27 Dec 2012 10:54

Re: printf.exe: Show formatted output AND evaluate arithmetic expressions!

#18 Post by lazna » 07 Aug 2022 04:17

One more note:

It seem to me, first use of fprint.exe on windows batch file somehow 'stop interpreting' ANSI sequences. All sequences after first run (it looping multiple time in my script) are not interpreted, but just displayed on screen. Could someone verify its not a problem only on my machine, please?

EDIT: It does work when prepend "cmd /C" just before printf command, so this could be a workaround

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: printf.exe: Arithmetic and Programming

#19 Post by Aacini » 21 Aug 2023 20:06

I have just released a new version of the printf.exe application. The new version 2.11 of printf.exe includes many new features. Firstly, this is a list of the original printf.exe features as implemented in the current version 2.11:
  • All output is performed via the printf C standard function in the usual way. Allowed control characters in the format string are: \n New line, \r CR, \t TAB, \b BS and \a BELL. For example:

    Code: Select all

    printf "Hello, world!\n"
    
    Data specification formats are %i for integers, %f for floats, %c for characters and %s for strings.
    .
  • The 64-bits integer numbers of previous version were removed; this point allows to write correct arithmetic expressions in a simpler way. Current data types are: 32-bits integer number (with no decimal point), 64-bits floating point number (with decimal point or Exponent of ten), a character enclosed in apostrophes ('C') and a string enclosed "between quotes". A Batch (environment) variable also works as string value. A simple example:

    Code: Select all

    printf "A character: %c\tA string: %s\tAn integer: %i\tA floating point: %f\n"  'X'  "ABC"  1  1.
    
    .
  • Arithmetic operations are evaluated in Reverse Polish Notation. Integer operations. One operand: ! BoolNot, ~ BitNot, _ ChS, $ Sign, ] Sto, [ Rcl, > Dup, < Drop, ++ Inc, -- Dec. Two operands: + Add, - Sub, * Mul, / Div, % Mod, ** Power. << BitSHL, >> BitSHR, & BitAnd, | BitOr, ^ BitXor, <> Exchan. # Random, . Float.
    .
  • Floating Point operations. One operand: CHS, ABS, SIGN, FRAC, INV, SQR, SQRT; LN, LOG, EXP, EXPT; STO, RCL, DUP, DROP. SIN, COS, TAN, ASIN, ACOS, ATAN (rad); DEG, RAD. Special: ZERO, ONE, PI, INT. Two operands: ADD, SUB, MUL, DIV, MOD, POW, XCHG. For example:

    Code: Select all

    printf "The sum of 3 plus 4 is %i\nThe sum of 3. plus 4. is %f\n"  3 4 +  3. 4. ADD
    
The new printf version 2.11 have a lot of new and amazing features. This is a brief list:
  • A few extended operations for stack management, storage registers and arithmetic operations have been added.
  • A new rich set of string management functions: atoi(string), atof(string), len(string), getc(str,pos), putc(str,pos,C), xchc(str,pos,C), dupc(character,N), dups(string,N), revc(string), revs(s1,..,sn,N), gets(str,pos,len), index(str,subs), split(string), join(s1,..,sn,N), shift(s1,..,sn,N), repl(str,s1,s2).
  • An entirely new set of basic and advanced Input/Output operations: OUT, FMT{, FMT}, GETK, GETK?, GETK?:r, maxlen IN, "command" CMD, maxlen IN?, "file" IN{?:n, "file" IN{+?:n, maxlen IN?:n, "file" OUT{:n, "file" OUT{+:n, OUT:n, OUT}:n, OUT}+:n, SEEK#:n:p.
However, the most important feature of the new printf.exe version 2.11 is its programming capabilities. The printf.exe programming scheme is not the very high-level one of modern programming languages; it is a much simpler one that was designed based on a technology that arose together with the Lisp programming language, which established the principles of Structured Programming. The program writing method used in printf.exe is based on only four control elements (Begin, Repeat, Quit and End) and a couple of simple rules about evaluating conditions (Tests), but this method still allows the same constructs of modern structured programming languages ​​to be assembled and used. The scheme below summarizes all operation rules on printf.exe programming:

Image

This programming paradigm makes it possible for non-technical people to understand and practice the process of writing programs; they just need a little interest in math.

All these features are described with detail in the new printf.exe 2.11 User's Manual (now in both English and Spanish) that include a lot of examples on several levels.

I encourage you to download the printf.exe 2.11 package, review the User's Manual, and try each of the included sample programs at least once. You may be surprised!!! :D

printf.exe version 2.11.zip
printf.exe 2.11 with examples, user's manuals and support files
(192.6 KiB) Downloaded 1055 times

Antonio

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: printf.exe: Arithmetic and Programming

#20 Post by Aacini » 24 Aug 2023 06:19

Ops! I just realized that I forgot a couple of images in the printf.zip file 2.11 previously released. :( I added the missing files to the .zip, so please download the file again.

Antonio

PS - Could you please confirm if the printf.exe program is running correctly on your computer? :roll: Thank you so much!

lazna
Posts: 62
Joined: 27 Dec 2012 10:54

Re: printf.exe: Arithmetic and Programming

#21 Post by lazna » 22 Oct 2023 04:30

Just download zip file, extract printf.exe and run it:

:\Users\user>\utils\printf.exe
printf.exe Version 1.0 Copyright (C) 2016-2020, Antonio Perez Ayala

'printfHelp.bat' is not recognized as an internal or external command,
operable program or batch file.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: printf.exe: Arithmetic and Programming

#22 Post by Aacini » 22 Oct 2023 20:28

lazna wrote:
22 Oct 2023 04:30
Just download zip file, extract printf.exe and run it:

:\Users\user>\utils\printf.exe
printf.exe Version 1.0 Copyright (C) 2016-2020, Antonio Perez Ayala

'printfHelp.bat' is not recognized as an internal or external command,
operable program or batch file.

I am afraid the description of your problem is incomplete...

I assumed that you downloaded the .zip file and extracted just printf.exe file from it. The .zip file contains 27 files. Why just download printf.exe?

The Appendix 1: "printf.exe installation and support" of printf.exe User's Manual clearly specify: "download the printf.zip file and extract its contents in a given folder". Then a list of the included files follows. In such a list, this file appears:

Code: Select all

printfHelp.bat			printf.exe on-screen help
I hope this information helps you solve your problem. If not, please describe your situation in more detail...

Antonio

lazna
Posts: 62
Joined: 27 Dec 2012 10:54

Re: printf.exe: Arithmetic and Programming

#23 Post by lazna » 23 Oct 2023 02:18

Aacini wrote:
22 Oct 2023 20:28
I assumed that you downloaded the .zip file and extracted just printf.exe file from it. The .zip file contains 27 files. Why just download printf.exe?
Because I am using it in redistributable package which including many other files, and want to minimize files number to keep whole package better maintained. And because previous version of printf.exe work fine as a single file.

Now I have exctracted whole package and program start normally. Sorry for my ignorance..

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: printf.exe: Arithmetic and Programming

#24 Post by Aacini » 12 May 2024 15:40

A new release of printf.exe application is here!

The new printf.exe version 2.55 adds some new features:
  • A couple small bugs were fixed
  • A new set of very efficient FPU-only operations were added
  • CURS new operation to control the cursor was added
  • FMT{:r option to show text in color was added
Besides, some new example programs were added. For example, the new Mandelbrot Set example program can show the well-known fractal graphic, like this one:

Image

Enjoy it!

printf.exe version 2.55.zip
Last printf.exe version with documentation, examples and support files
(284.29 KiB) Downloaded 607 times

Antonio

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: printf.exe: Arithmetic and Programming

#25 Post by Aacini » 01 Sep 2024 22:18

I just released the new printf.exe version 2.80 with these new features:
  • CURS3..CURS9 change cursor size function added.
  • SYSTEM function to execute cmd.exe commands added.
  • CASE function that convert uppercase/lowercase letters from Code Page 850 added.
  • A new set of Y vs X "reversed" comparison operators added.
  • ]i++ ]i-- [i++ [i-- STOi++ STOi-- RCLi++ RCLi-- Post-increment/decrement in indirect addressing storage registers operations added.
There is also a new example program that implements the Turtle Graphics of Logo programming language. The example program not only contains the code for the printf.exe subroutines that allow you to translate any standard Logo Turtle Graphics program into a printf.exe one; it is also an extensive example that makes it possible to review the graphic of several Logo examples and begin using Turtle Graphics in an interactive environment with on-screen help. The following image is an example of the result of said program:

Image

Besides Turtle Graphics basic commands, there is another example program that implements the LABEL Turtle Graphics feature that allows you to show text messages. The printf.exe LABEL implementation is based on Font Definition Files that you can easily create in order to define your own fonts. The printf.exe package includes two font definition files; the image below show the standard ASCII characters 32-127 of one of such files.

Image

printf version 2.80.zip
(423.25 KiB) Downloaded 716 times
Antonio

PS - I would like to invite you to register yourself as a printf.exe user. See the indications in the Register your copy of printf.exe.html file.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: printf.exe: Arithmetic and Programming

#26 Post by miskox » 02 Sep 2024 04:28

Nice. I wanted to use escape sequence DECDHL—Double-Width, Double-Height Line but it is not supported in Windows.

This:

Code: Select all

ESC#3 Some text - displayed with double height and double width.
ESC#4 Some text - displayed with double height and double width.
Saso

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: printf.exe: Arithmetic and Programming

#27 Post by aGerman » 02 Sep 2024 12:49

Works for me, Saso.

Code: Select all

printf "%c#3Double Height (implies Double Width)\n%c#4Double Height (implies Double Width)\n" 27 27
Tested in the prompt of Windows Terminal. In a batch script you have to double the percent signs as always. And I think to make it also work in a console window, Antonio would have to set ENABLE_VIRTUAL_TERMINAL_PROCESSING for his application (https://learn.microsoft.com/en-us/windo ... onsolemode).

@Antonio, do you think supporting the non-standard \e sequence for an escape character would be reasonable? Some C and C++ compilers do (gcc and clang for example) as well as Linux shells. PowerShell 6.0+ supports it using its `e escape syntax.

Steffen

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: printf.exe: Arithmetic and Programming

#28 Post by Aacini » 02 Sep 2024 21:29

The inclusion of \e as an ESC character is very simple and it will be implemented in the next version of printf.exe.

About ENABLE_VIRTUAL_TERMINAL_PROCESSING: I understand that this mode must be implemented in order to use VT100 Terminal Sequences. What I have not decided yet is the way to do such implementation. I think of three possibilities:

1- Always enable VirtualProcessing when printf.exe starts and reset the original console mode when end.
2- Provide a simple printf.exe command that just enable/disable VirtualProcessing (I don't like this option: it looks like an ugly patch).
3- Provide an advanced command that gives full access to all modes of both input console and output buffer.

The first method is the simplest one, but I don't know if it would introduce some problems in certain programs that don't use VT100 sequences.

IMHO the third method is the "right way" to solve this problem. However, this option is somewhat difficult to use if you just want to enable VirtualProcessing. It also opens the possibility that users could damage the operation of the console if they don't know what they are doing...

What do you think?

Antonio

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: printf.exe: Arithmetic and Programming

#29 Post by miskox » 03 Sep 2024 04:19

aGerman wrote:
02 Sep 2024 12:49
Works for me, Saso.

Code: Select all

printf "%c#3Double Height (implies Double Width)\n%c#4Double Height (implies Double Width)\n" 27 27
Tested in the prompt of Windows Terminal. In a batch script you have to double the percent signs as always. And I think to make it also work in a console window, Antonio would have to set ENABLE_VIRTUAL_TERMINAL_PROCESSING for his application (https://learn.microsoft.com/en-us/windo ... onsolemode).

@Antonio, do you think supporting the non-standard \e sequence for an escape character would be reasonable? Some C and C++ compilers do (ggc and clang for example) as well as Linux shells. PowerShell 6.0+ supports it using its `e escape syntax.

Steffen
Sorry Steffen for not being clear enough: I wanted to use Escape Sequences on their own (without printf) in the command prompt. Now printf would be useful.

Saso

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: printf.exe: Arithmetic and Programming

#30 Post by aGerman » 03 Sep 2024 10:04

@Saso
I thought it was about printf as you asked in this thread.
However, no problem. Just create an escape character and assign it to a variable as you already know:

Code: Select all

for /f %%i in ('echo prompt $E^|cmd') do set "ESC=%%i"
echo %ESC%#3 Some text - displayed with double height and double width.
echo %ESC%#4 Some text - displayed with double height and double width.

@Antonio
If you'd ask me, don't make it an option, just enable it. In fact I'm not aware of any tool where this is optional. Either it is enabled or not. The CMD, PowerShell and Linux Shells on Windows (via WSL) have it enabled. Also, if you run in Windows Terminal (which is the default on Win 11 already) you don't even need to enable it anymore. It's only required for applications running in the console. So this is rather an exception and I hope Microsoft has any good reason for that kind of inconsistency.
Since I don't speak ASM, I've been putting the code into the Compiler Explorer in the hope it's helpful for you:
https://godbolt.org/z/b4rb1f6Ys
Don't worry about the target. If it is anything else than a console (e.g. a redirection to a file or a pipe) Get/SetConsoleMode will just fail. So, let it fail... :wink:

EDIT Oh, I forgot to mention that you don't need to reset the mode.

Steffen

Post Reply