Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pimctl output OK via cmdline <> but NOK when integrated in html #235

Open
LouisAtGH opened this issue Nov 16, 2022 · 7 comments
Open

pimctl output OK via cmdline <> but NOK when integrated in html #235

LouisAtGH opened this issue Nov 16, 2022 · 7 comments

Comments

@LouisAtGH
Copy link
Contributor

LouisAtGH commented Nov 16, 2022

Joachim,

I compiled the actual code using freebsd 14-currenct in favor of the actual pfSense development release. Works OK, no problem, apart from an layout issue.

pfsense is integrating the pimctl output in its html screens like this

<?= htmlspecialchars(shell_exec('/usr/local/sbin/pimctl show')); ?>

And pimctl reacts with an output intended for a vt100 command line using control like

ESC[4m 	ESC[24m 	set underline mode
ESC[7m 	ESC[27m 	set inverse/reverse mode
ESC[0m 		       reset all modes (styles and colors)

However that does not fit in an html page :(
(see example below)

To solve this the pmctl headers format should be either OK for both command line and html or there should be an option to select 'the formatting format'

I also have been considering two other options:

  • making some changes to the pimctl code myself (I do not really like that since it is not my code)
  • or writing an php converter routine trying to convert 'vt100' controls to html controls
    To be honest, I am not wild on either of those two ideas

Below an example of the pimctl output as it shows up in the pfsense gui, and below that some compiler warnings I discovered. Not dramatic however hot really OK as well.

Louis

pimctl output as displayed in an html screen

How formatted headers are shown in an html page:

lagg0.5           Up        192.168.5.1               1     30    0  192.168.5.1               1
�[4m                                                                               �[0m
PIM Neighbor Table
�[7mInterface         Address            Priority  Mode  Uptime/Expires                  �[0m
�[4m                                                                               �[0m
Multicast Routing Table
�[7mSource            Group            RP Address       Flags                      �[0m
ANY               239.255.255.250  192.168.14.15    WC RP
192.168.1.2       239.255.255.250  192.168.14.15    SPT CACHE SG

Compiler warnings

configure.ac:8: warning: The macro `AC_CONFIG_HEADER' is obsolete.
configure.ac:8: You should run autoupdate.
configure.ac:51: warning: The macro `AC_HEADER_STDC' is obsolete.
configure.ac:51: You should run autoupdate.
configure.ac:3: warning: name 'aux' is reserved on W32 and DOS platforms
kern.c:212:24: warning: unused parameter 'socket' [-Wunused-parameter]
void k_set_pktinfo(int socket, int val)
kern.c:212:36: warning: unused parameter 'val' [-Wunused-parameter]
void k_set_pktinfo(int socket, int val)
pim.c:447:53: warning: unused parameter 'frag' [-Wunused-parameter]
static int send_frame(char *buf, size_t len, size_t frag, size_t mtu, struct sockaddr *dst, size_t salen)
                                                    ^
pim.c:447:66: warning: unused parameter 'mtu' [-Wunused-parameter]
static int send_frame(char *buf, size_t len, size_t frag, size_t mtu, struct sockaddr *dst, size_t salen)
@troglobit
Copy link
Owner

Edit: changed markup syntax of issue to Markdown

@troglobit
Copy link
Owner

Thank you for the report. See my comments below:

  • At my old job we parsed a lot of command line output, including ANSI control characters. We used Javascript to convert to HTML colors as well as bold/italic/etc. It's not overly difficult.
  • Another way, which would admittedly take longer, but something I've considered myself, is to add a -j option to pimctl for JSON output, similar to what iproute2 in Linux has. I've added the same recently to another project. This would be a LOT easier to parse from Javascript. We only need to agree out the output format. This is in my own interest as well, so I would be able to help out here with actual code changes -- without committing to any timeline.
  • I'd suggest splitting your report into the functional request, and another separate issue for the compiler warnings -- as they are separate issues.

@LouisAtGH
Copy link
Contributor Author

Joachim, pfSense uses PHP, I worked around the issue this way.

I did work around the problem in the following way:

The ANSI controls available in the output where
ESC[4m ESC[24m set underline mode
ESC[7m ESC[27m set inverse/reverse mode
ESC[0m reset all modes (styles and colors)
More info in https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797

With as complicating factor that every modified line was terminated by the generic ESC[0m, which forced me to use a generic html termination code as well.

I defined an string array like this
$dictionary = array(
'#[4m' => '
________________________________________________________________________________________________' ,
'#[24m' => '
' ,
'#[7m' => '' ,
'#[27m' => '
' ,
'#[0m' => '' ,
);
Where the '#' is an replacement for the original ESC-character
( I do not know how to insert an ESC-character in a static string array)

Then the following line did the job


      

@LouisAtGH
Copy link
Contributor Author

Joachim,

What seems minimal to me is that the formatting is symmetrical / consistent. As example

  • starting bold should be terminated with end bold ESC[4m should be followed by ESC[24m and not by ESC[0m !
  • and of course a control starting 1st should be closed last

@troglobit
Copy link
Owner

Sorry, but no. This is not HTML tags, it's ANSI escape sequences, and there's no standard that says one cannot use ESC[0m to disable all formatting. If you go for translating ANSI escape codes you need to handle all these cases. If you do it right, such a function would be useful for parsing output from many other system tools as well.

My recommendation is still to go for an alternate output format, I suggest JSON since that's sort the industry standard at this point.

Btw, I suppose you've seen the pimctl -p option to disable ANSI escape sequences altogether?

@LouisAtGH
Copy link
Contributor Author

Oeps! I did not notice the -p option, but that surely improves the situation! Thanks for the tip !
(the idea is not great, but the result of my workarround is not even bad)

Whatever if you want to convert e.g. the ANSI escapes to for example html layout controls, than you you need not only the start of the style change but also the end.

I can convert ansi "start bold" to the equivalent html command but than I also have to insert the "stop bold"
and it is not a big deal to translate ^ESC[4m^ into start underline and to translate ^ESC[24m^ into stop underline, but there is no way I can do something with ^ESC[0m^. No way to translate that in some equivalent code

@troglobit
Copy link
Owner

State machine. You can always track the latest set of HTML tags you've inserted. It's definitely possible, we did it in javascript for many other applications at my old job, so it should be in other languages as well.

I'm not going to change the ANSI escape sequences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants