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

desktop.c: purge strncpy() and replace with custom pstrcpy() #20

Merged
merged 1 commit into from
Jun 18, 2024

Conversation

01micko
Copy link
Contributor

@01micko 01micko commented Jun 15, 2024

fixes #19

desktop.c Outdated Show resolved Hide resolved
@01micko 01micko marked this pull request as draft June 15, 2024 11:10
@01micko 01micko closed this Jun 16, 2024
@01micko 01micko reopened this Jun 16, 2024
@01micko
Copy link
Contributor Author

01micko commented Jun 16, 2024

I think I'm going to have another go. Will force push soon, took a completely different approach from anything suggested.

@01micko 01micko changed the title desktop.c: purge strncpy() and replace with snprintf() desktop.c: purge strncpy() and replace with custom pstrcpy() Jun 16, 2024
@01micko 01micko marked this pull request as ready for review June 16, 2024 07:01
@01micko
Copy link
Contributor Author

01micko commented Jun 16, 2024

I stole pstrcpy() from qemu, written by Fabrice Bellard,

Edit to add: I placed a comment before the function linking to qemu and of course giving credit

@01micko 01micko marked this pull request as draft June 17, 2024 02:18
@01micko
Copy link
Contributor Author

01micko commented Jun 17, 2024

I'm going to add another test using 'zh_HK.utf8' - t1002.t - not to be committed but patched in to the source to test out wide char support in debuild. That's why I marked again as WIP.

@01micko
Copy link
Contributor Author

01micko commented Jun 17, 2024

   dh_auto_test
	make -j6 check
make[1]: Entering directory '/home/mick/Packages/labwc/labwc-menu-generator-0.1.0'
rm -f -r test-results
*** prove ***
t1000.t .. ok
t1001.t .. ok
t1002.t .. ok
All tests successful.
Files=3, Tests=3,  0 wallclock secs ( 0.01 usr  0.00 sys +  0.03 cusr  0.01 csys =  0.05 CPU)
Result: PASS
make clean-except-prove-cache
rm -f -r 'trash directory'.* test-results
make[1]: Leaving directory '/home/mick/Packages/labwc/labwc-menu-generator-0.1.0'

My rationale for not using the heap is that continuosly allocating and freeing memory might slow things down enough to be a pain - especially if we use the generator to provide a pipe-menu (which I haven't tried yet but will soon).

If @johanmalm you disagree with my choice I have no problem! You have years of experience and I'm a novice :)


edit for completeness:

t/t1002.t
#!/bin/sh

test_description='parse .desktop files to generate openbox menu'
. ./sharness.sh

test_menu() {
	export XDG_DATA_HOME=${PWD}
	export XDG_DATA_DIRS="bad-location"
	export LABWC_MENU_GENERATOR_DEBUG_FIRST_DIR_ONLY=1
	export LANG="$2"
	cat ../t1002/"$1" >expect
	cp -a ../t1000/applications .
	../../labwc-menu-generator >actual
	test_cmp expect actual
}

test_expect_success 'parse .desktop files with LANG=zh_HK.utf8' '
test_menu "menu-zh.xml" "zh_HK.utf8"
'

test_done
t/t1002/menu-zh.xml
<?xml version="1.0" encoding="UTF-8"?>
<openbox_menu>
<menu id="root-menu" label="root-menu">
  <menu id="Internet" label="互聯網">
    <item label="Avahi SSH Server Browser" icon="network-wired">
      <action name="Execute"><command>/usr/bin/bssh</command></action>
    </item>
    <item label="Avahi VNC Server Browser" icon="network-wired">
      <action name="Execute"><command>/usr/bin/bvnc</command></action>
    </item>
    <item label="Chromium" icon="chromium">
      <action name="Execute"><command>/usr/bin/chromium</command></action>
    </item>
    <item label="Firefox" icon="firefox">
      <action name="Execute"><command>/usr/lib/firefox/firefox</command></action>
    </item>
    <item label="HexChat" icon="io.github.Hexchat">
      <action name="Execute"><command>hexchat --existing</command></action>
    </item>
    <item label="NetSurf Web Browser" icon="netsurf.png">
      <action name="Execute"><command>netsurf</command></action>
    </item>
    <item label="qBittorrent" icon="qbittorrent">
      <action name="Execute"><command>qbittorrent</command></action>
    </item>
    <item label="Vivaldi" icon="vivaldi">
      <action name="Execute"><command>/usr/bin/vivaldi-stable</command></action>
    </item>
  </menu> <!-- Internet -->
  <menu id="Multimedia" label="多媒體">
    <item label="Audacious" icon="audacious">
      <action name="Execute"><command>audacious</command></action>
    </item>
    <item label="mpv Media Player" icon="mpv">
      <action name="Execute"><command>mpv --player-operation-mode=pseudo-gui --</command></action>
    </item>
    <item label="Qt V4L2 test Utility" icon="qv4l2">
      <action name="Execute"><command>qv4l2</command></action>
    </item>
    <item label="Qt V4L2 video capture utility" icon="qvidcap">
      <action name="Execute"><command>qvidcap</command></action>
    </item>
    <item label="VLC media player" icon="vlc">
      <action name="Execute"><command>/usr/bin/vlc --started-from-file</command></action>
    </item>
  </menu> <!-- Multimedia -->
  <menu id="System" label="系統">
    <item label="Alacritty" icon="Alacritty">
      <action name="Execute"><command>alacritty</command></action>
    </item>
    <item label="Avahi Zeroconf Browser" icon="network-wired">
      <action name="Execute"><command>/usr/bin/avahi-discover</command></action>
    </item>
    <item label="File Manager PCManFM" icon="system-file-manager">
      <action name="Execute"><command>pcmanfm</command></action>
    </item>
    <item label="Foot" icon="foot">
      <action name="Execute"><command>foot</command></action>
    </item>
    <item label="Foot Client" icon="foot">
      <action name="Execute"><command>footclient</command></action>
    </item>
    <item label="Foot Server" icon="foot">
      <action name="Execute"><command>foot --server</command></action>
    </item>
    <item label="OpenJDK Java 11 Console" icon="java11-openjdk">
      <action name="Execute"><command>/usr/lib/jvm/java-11-openjdk/bin/jconsole</command></action>
    </item>
    <item label="OpenJDK Java 11 Shell" icon="java11-openjdk">
      <action name="Execute"><command>/usr/lib/jvm/java-11-openjdk/bin/jshell</command></action>
    </item>
    <item label="QTerminal" icon="utilities-terminal">
      <action name="Execute"><command>qterminal</command></action>
    </item>
    <item label="QTerminal drop down" icon="utilities-terminal">
      <action name="Execute"><command>qterminal --drop</command></action>
    </item>
    <item label="Sakura" icon="terminal-tango">
      <action name="Execute"><command>sakura</command></action>
    </item>
    <item label="Tint2" icon="tint2">
      <action name="Execute"><command>tint2</command></action>
    </item>
    <item label="urxvt" icon="utilities-terminal">
      <action name="Execute"><command>urxvt</command></action>
    </item>
    <item label="urxvt (client)" icon="utilities-terminal">
      <action name="Execute"><command>urxvtc</command></action>
    </item>
    <item label="urxvt (tabbed)" icon="utilities-terminal">
      <action name="Execute"><command>urxvt-tabbed</command></action>
    </item>
    <item label="UXTerm" icon="xterm-color_48x48">
      <action name="Execute"><command>uxterm</command></action>
    </item>
    <item label="XTerm" icon="xterm-color_48x48">
      <action name="Execute"><command>xterm</command></action>
    </item>
  </menu> <!-- System -->
  <menu id="Graphics" label="美工繪圖">
    <item label="Flameshot" icon="org.flameshot.Flameshot">
      <action name="Execute"><command>/usr/bin/flameshot</command></action>
    </item>
    <item label="GNU 圖片處理程式" icon="gimp">
      <action name="Execute"><command>gimp-2.10</command></action>
    </item>
    <item label="Image Viewer" icon="gpicview">
      <action name="Execute"><command>gpicview</command></action>
    </item>
    <item label="Inkscape" icon="org.inkscape.Inkscape">
      <action name="Execute"><command>inkscape</command></action>
    </item>
    <item label="MATE Color Selection" icon="gtk-select-color">
      <action name="Execute"><command>mate-color-select</command></action>
    </item>
    <item label="mtPaint" icon="mtpaint">
      <action name="Execute"><command>mtpaint</command></action>
    </item>
    <item label="文件檢視器" icon="org.gnome.Evince">
      <action name="Execute"><command>evince</command></action>
    </item>
  </menu> <!-- Graphics -->
  <menu id="Settings" label="設定">
    <item label="Desktop Preferences" icon="user-desktop">
      <action name="Execute"><command>pcmanfm --desktop-pref</command></action>
    </item>
    <item label="nitrogen" icon="nitrogen">
      <action name="Execute"><command>nitrogen</command></action>
    </item>
    <item label="Preferred Applications" icon="preferences-desktop">
      <action name="Execute"><command>libfm-pref-apps</command></action>
    </item>
    <item label="Text Editor Settings" icon="org.xfce.mousepad">
      <action name="Execute"><command>mousepad --preferences</command></action>
    </item>
    <item label="Tint2 Settings" icon="tint2conf">
      <action name="Execute"><command>tint2conf</command></action>
    </item>
    <item label="wdisplays" icon="network.cycles.wdisplays">
      <action name="Execute"><command>wdisplays</command></action>
    </item>
  </menu> <!-- Settings -->
  <menu id="Office" label="辦公">
    <item label="Gnumeric" icon="gnumeric">
      <action name="Execute"><command>gnumeric</command></action>
    </item>
    <item label="文件檢視器" icon="org.gnome.Evince">
      <action name="Execute"><command>evince</command></action>
    </item>
  </menu> <!-- Office -->
  <menu id="Development" label="開發">
    <item label="CMake" icon="CMakeSetup">
      <action name="Execute"><command>cmake-gui</command></action>
    </item>
    <item label="Geany" icon="geany">
      <action name="Execute"><command>geany</command></action>
    </item>
    <item label="GTK Demo" icon="org.gtk.Demo4">
      <action name="Execute"><command>gtk4-demo</command></action>
    </item>
    <item label="Icon Browser" icon="org.gtk.IconBrowser4">
      <action name="Execute"><command>gtk4-icon-browser</command></action>
    </item>
    <item label="IntelliJ IDEA Community Edition" icon="idea">
      <action name="Execute"><command>/usr/bin/idea</command></action>
    </item>
    <item label="Print Editor" icon="org.gtk.PrintEditor4">
      <action name="Execute"><command>gtk4-print-editor</command></action>
    </item>
    <item label="Widget Factory" icon="org.gtk.WidgetFactory4">
      <action name="Execute"><command>gtk4-widget-factory</command></action>
    </item>
  </menu> <!-- Development -->
  <menu id="Accessories" label="附屬應用">
    <item label="File Manager PCManFM" icon="system-file-manager">
      <action name="Execute"><command>pcmanfm</command></action>
    </item>
    <item label="Image Viewer" icon="gpicview">
      <action name="Execute"><command>gpicview</command></action>
    </item>
    <item label="Leafpad" icon="leafpad">
      <action name="Execute"><command>leafpad</command></action>
    </item>
    <item label="Mousepad" icon="org.xfce.mousepad">
      <action name="Execute"><command>mousepad</command></action>
    </item>
    <item label="nitrogen" icon="nitrogen">
      <action name="Execute"><command>nitrogen</command></action>
    </item>
    <item label="picom" icon="picom">
      <action name="Execute"><command>picom</command></action>
    </item>
    <item label="Sakura" icon="terminal-tango">
      <action name="Execute"><command>sakura</command></action>
    </item>
    <item label="Vim" icon="gvim">
      <action name="Execute"><command>vim</command></action>
    </item>
    <item label="檔案" icon="org.gnome.Nautilus">
      <action name="Execute"><command>nautilus --new-window</command></action>
    </item>
  </menu> <!-- Accessories -->
</menu> <!-- root-menu -->
</openbox_menu>

@01micko 01micko marked this pull request as ready for review June 17, 2024 04:10
@01micko
Copy link
Contributor Author

01micko commented Jun 17, 2024

Demo:

script

lab-gen-menu
#!/bin/sh

# script to generate labwc menu with pipe-menus
# prints to stdout so redirect to menu.xml

# header
printf "%s\n\n%s\n" '<?xml version="1.0" encoding="UTF-8"?>' '<openbox_menu>'
printf "  %s\n" '<menu id="root-menu" label="root-menu">' 
printf "    %s\n" '<item label="Web browser"><action name="Execute" command="vivaldi" /></item>'
printf "    %s\n" '<item label="Terminal"><action name="Execute" command="alacritty" /></item>'
printf "    %s\n" '<item label="File Manager"><action name="Execute" command="thunar" /></item>'
printf "  %s\n" '<menu id="Windows" label="Windows" execute="app-list-cut.sh" /> <!-- pipemenu -->'
printf "  %s\n" '<menu id="Applications" label="Applications" execute="labwc-menu-generator -p" /> <!-- pipemenu -->'
printf "  %s\n" '<separator />'
printf "  %s\n" '<menu id="Exit" label="Exit">'
printf "    %s\n" '<item label="Reconfigure"><action name="Reconfigure" /></item>'
printf "    %s\n" '<item label="Logout"><action name="Execute" command="wlogout" /></item>'
printf "  %s\n" '</menu> <!-- exit-menu -->'
printf "  %s\n" '</menu> <!-- root-menu -->'
printf "%s\n" '</openbox_menu>'
main menu
<?xml version="1.0" encoding="UTF-8"?>

<openbox_menu>
  <menu id="root-menu" label="root-menu">
    <item label="Web browser"><action name="Execute" command="vivaldi" /></item>
    <item label="Terminal"><action name="Execute" command="alacritty" /></item>
    <item label="File Manager"><action name="Execute" command="thunar" /></item>
  <menu id="Windows" label="Windows" execute="app-list-cut.sh" /> <!-- pipemenu -->
  <menu id="Applications" label="Applications" execute="labwc-menu-generator -p" /> <!-- pipemenu -->
  <separator />
  <menu id="Exit" label="Exit">
    <item label="Reconfigure"><action name="Reconfigure" /></item>
    <item label="Logout"><action name="Execute" command="wlogout" /></item>
  </menu> <!-- exit-menu -->
  </menu> <!-- root-menu -->
</openbox_menu>

The 'Windows' pipe-menu is @davidphilipbarr 's.

@johanmalm
Copy link
Collaborator

Sorry. Got distracted for a couple of days.
I think I like pstrcpy but let me have a look tomorrow.

@01micko
Copy link
Contributor Author

01micko commented Jun 17, 2024

All good. No rush 😃

@johanmalm johanmalm merged commit 6f60e87 into labwc:master Jun 18, 2024
3 checks passed
@johanmalm
Copy link
Collaborator

Thanks.

This only happens once, so heap-allocating would be fine if we were worried about truncating... but given the situation, I'm not bothered.

And it's nice to have a touch of "Fabrice Bellard".

@01micko 01micko deleted the purge branch June 18, 2024 21:30
@01micko
Copy link
Contributor Author

01micko commented Jun 18, 2024

Thanks

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

Successfully merging this pull request may close these issues.

desktop.c: warning: ‘__builtin_strncpy’ specified bound 24 equals destination size
3 participants