WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: wbar.tcz bug and fix  (Read 1671 times)

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: wbar.tcz bug and fix
« Reply #15 on: October 30, 2024, 11:47:40 AM »
How about this?

Code: [Select]
--- a/wbar_update.sh
+++ b/wbar_update.sh
@@ -1,9 +1,11 @@
-#!/bin/sh
+#!/bin/busybox ash
 # (c) Robert Shingledecker 2010
 # Called from desktop.sh to update wbar icons.
+. /etc/init.d/tc-functions
+useBusybox
 
 writeWBARitem() {
-busybox awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
+awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
 BEGIN {
   FS = "="
   name = ""
@@ -19,7 +21,7 @@
     test = match(exec,"%")
     if ( test ) exec = substr(exec,0,test-1)
   } else if ( $1 == "X-FullPathIcon" ) {
-    icon = $2
+    icon = rtrim($2)
   } else if ( $1 == "Terminal" ) {
     terminal = $2
   }
@@ -72,6 +74,7 @@
 FREEDESK=/usr/local/share/applications/"$APPNAME".desktop
 if [ -e "$FREEDESK" ]; then
    ICONCHECK="$(awk 'BEGIN{FS = "="}$1=="X-FullPathIcon"{print $2}' "$FREEDESK")"
+   ICONCHECK=$(trim "$ICONCHECK")
    NAMECHECK="$(awk 'BEGIN{FS = "="}$1=="Name"{print $2; exit 0}' "$FREEDESK")"
    if grep -qw "^t: *${NAMECHECK// /}$" "${TCEDIR}"/xwbar.lst 2>/dev/null; then exit 0; fi
    TARGET="$APPNAME".img
« Last Edit: October 30, 2024, 12:12:42 PM by GNUser »

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: wbar.tcz bug and fix
« Reply #16 on: October 30, 2024, 12:08:13 PM »
Hmm, I don't like using trim/echo here. It does more than just get rid of trailing whitespace. I favor sticking with sed.

Code: [Select]
bruno@x230:~$ trim() { echo $1; }
bruno@x230:~$ var="   abc   def   ghi   "
bruno@x230:~$ var2=$(trim $var)
bruno@x230:~$ echo "$var2"x
abcx

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: wbar.tcz bug and fix
« Reply #17 on: October 30, 2024, 12:12:17 PM »
This is better:
Code: [Select]
bruno@x230:~$ trim() { echo $1; }
bruno@x230:~$ var="   abc   def   ghi   "
bruno@x230:~$ var2=$(trim "$var")
bruno@x230:~$ echo "$var2"x
abc def ghix
I went back and added quotes at the appropriate spot in Reply #15.
Using quotes and echo to get the desired result feels risky. I think trimming ICONCHECK with sed is cleaner.

« Last Edit: October 30, 2024, 12:18:14 PM by GNUser »

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: wbar.tcz bug and fix
« Reply #18 on: October 30, 2024, 12:20:58 PM »
I like this one the best:

Code: [Select]
--- a/wbar_update.sh
+++ b/wbar_update.sh
@@ -1,9 +1,11 @@
-#!/bin/sh
+#!/bin/busybox ash
 # (c) Robert Shingledecker 2010
 # Called from desktop.sh to update wbar icons.
+. /etc/init.d/tc-functions
+useBusybox
 
 writeWBARitem() {
-busybox awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
+awk -v output="$TMP" -v target="$TARGET" -v wbaricons="$TCEWBAR" '
 BEGIN {
   FS = "="
   name = ""
@@ -19,7 +21,7 @@
     test = match(exec,"%")
     if ( test ) exec = substr(exec,0,test-1)
   } else if ( $1 == "X-FullPathIcon" ) {
-    icon = $2
+    icon = rtrim($2)
   } else if ( $1 == "Terminal" ) {
     terminal = $2
   }
@@ -71,7 +73,7 @@
 #
 FREEDESK=/usr/local/share/applications/"$APPNAME".desktop
 if [ -e "$FREEDESK" ]; then
-   ICONCHECK="$(awk 'BEGIN{FS = "="}$1=="X-FullPathIcon"{print $2}' "$FREEDESK")"
+   ICONCHECK="$(awk 'BEGIN{FS = "="}$1=="X-FullPathIcon"{print $2}' "$FREEDESK" | sed -r 's/[ \t]+$//')"
    NAMECHECK="$(awk 'BEGIN{FS = "="}$1=="Name"{print $2; exit 0}' "$FREEDESK")"
    if grep -qw "^t: *${NAMECHECK// /}$" "${TCEDIR}"/xwbar.lst 2>/dev/null; then exit 0; fi
    TARGET="$APPNAME".img

Offline mocore

  • Hero Member
  • *****
  • Posts: 659
  • ~.~
Re: awk trim string - ltrim() + rtrim() = trim()
« Reply #19 on: October 30, 2024, 02:21:42 PM »
Hmm, I don't like using trim/echo here. It does more than just get rid of trailing whitespace. I favor sticking with sed.

i happened to be reading
https://git.altlinux.org/people/legion/packages/?p=libshell.git;a=blob;f=docs/README.md
Quote
   1 # libshell
   2
   3 The libshell is a set of the most commonly-used shell functions. All functions use minimum
   4 of external utilities and written for POSIX shell.
   5
   6 The main idea is to get rid of implementing these functions in shell-scripts again and again,
   7 and also to protect from common mistakes.

... i like the sentiment , and even more the idea of (scripted) modularity   
both seam relevant in the case of this bug !! ...

so ....
 consider these awk functions ( or *irrelevant tangent*  the lily's  :P if you like)
https://gist.github.com/andrewrcollins/1592991 ~  ltrim(), rtrim(), and trim() in awk
Code: [Select]
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s)  { return rtrim(ltrim(s)); }

eg

Code: [Select]
f() { awk '                               
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s)  { return rtrim(ltrim(s)); }
'"$@" ; }

var="   abc   def   ghi   "
echo "$var" | f '{print ">" trim($0) "<"}'
#>abc   def   ghi<

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: wbar.tcz bug and fix
« Reply #20 on: October 30, 2024, 05:51:24 PM »
Hi GNUser
Hmm, I don't like using trim/echo here. It does more than just get rid of trailing whitespace. ...
Quotes can change how variables are treated.
For example:
Code: [Select]
tc@E310:~$ Var1="    Too many   spaces    "
tc@E310:~$ echo "$Var1"
    Too many   spaces
tc@E310:~$ echo $Var1
Too many spaces
With quotes, it's a string. Without quotes, it's a series of fields.
Either way, they all show up.

Here's one that has tripped me up in the past.
This works:
Code: [Select]
tc@E310:~$ for W in $Var1; do echo $W; done
Too
many
spaces

This doesn't:
Code: [Select]
tc@E310:~$ for W in "$Var1"; do echo $W; done
Too many spaces

When you call a function, it looks like this:
Code: [Select]
Function Var1 Var2 Var3 ....
Without quotes, it's treated as Vars separated by whitespace:
Code: [Select]
tc@E310:~$ trim $Var1
Too

If I change this:
Code: [Select]
trim() { echo $1; }To this:
Code: [Select]
trim() { echo $*; }
It works with or without quotes:
Code: [Select]
tc@E310:~$ trim $Var1
Too many spaces
tc@E310:~$ trim "$Var1"
Too many spaces


Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14849
Re: wbar.tcz bug and fix
« Reply #22 on: November 04, 2024, 06:57:22 AM »
Did we come to a conclusion on this?

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: wbar.tcz bug and fix
« Reply #23 on: November 04, 2024, 09:30:53 AM »
Hi Juanito. Folks pointed out 1001 ways to fix the problem, many of which are equally good.

I think the patch in Reply #18 is the most straight-forward. Would you like me to use that patch and submit patched wbar.tcz extensions for TCL15 x86 and x86_64?

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: wbar.tcz bug and fix
« Reply #24 on: November 04, 2024, 09:34:06 AM »
Hi GNUser
Agreed.

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14849
Re: wbar.tcz bug and fix
« Reply #25 on: November 04, 2024, 09:34:24 AM »
Yes please 🙂

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: wbar.tcz bug and fix
« Reply #26 on: November 04, 2024, 10:04:53 AM »
Updated wbar.tcz submitted for TCL15 x86 and x86_64 :)

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14849
Re: wbar.tcz bug and fix
« Reply #27 on: November 04, 2024, 10:54:52 AM »
posted