From: b Date: Thu, 11 May 2023 23:40:39 +0000 (+0000) Subject: some encoding & IO stuff X-Git-Tag: v1.0.0~1 X-Git-Url: http://bicyclesonthemoon.info/git-projects/?a=commitdiff_plain;h=4d62924a2f1c31bffeb9a6e3365de6aadac3682e;p=botm%2Fcommon-perl some encoding & IO stuff --- diff --git a/botm_common.pm b/botm_common.pm new file mode 100644 index 0000000..a94f664 --- /dev/null +++ b/botm_common.pm @@ -0,0 +1,107 @@ +# Copyright (C) 2023 Balthasar Szczepański +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +package botm_common; + +use strict; +#use warnings; +use utf8; + +use Encode qw(encode decode); + +use Exporter; +use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); + +$VERSION = 1.000000; +@ISA = qw(Exporter); +@EXPORT = (); +@EXPORT_OK = qw(readdatafile); +%EXPORT_TAGS = (); + +sub readdatafile { + (my $path, my $encoding, my $content_only) = @_; + my $file; + my %data; + my $eoh=0; + + if ($encoding eq '') { + $encoding = 'utf8'; + } + if ($content_only) { + $eoh=1; + } + + # check if $path is actually a path or maybe a filehandle + # filehandles are references. + if(ref($path)) { + $file=$path; + unless (seek($file, 0, 0)) { + # return %data; + } + } + else { + unless (open ($file, "<:$encoding", $path)) { + return %data; + } + } + + # The name of header field in previous line. Required for header fields that + # occupy multiple lines. + my $lastname=''; + + while (defined(my $line = <$file>)) { + # $line = decode($encoding, $line); + my $name=''; + my $value=''; + + if ($eoh){ + unless($line eq'') { + $data{'content'} = $data{'content'}.$line; + } + next; + } + + $line =~ s/[\n]$//g; + + # Empty line - end of header. + if ($line eq ''){ + $eoh=1; + } + # Line starts with whitespace. It's a continuation of the previous line. + # Concatenate the field value, separated by newline. + elsif($line =~ /^[ \t](.*)$/){ + if($lastname ne '') { + $data{$lastname} .= "\n".$1; + } + } + # Line starts with a name followed by colon/equal sign. Save the value + elsif ($line =~ /^([ -9;-<>-~]+)((:[ \t])|=)(.*)$/) { + $name = lc($1); + $value = $4; + + $data{$name} = $value; + + $lastname = $name; + } + } + # If argument was a path the file must be closed. + unless (ref($path)) { + close ($file); + } + + return %data; +} + +1 diff --git a/makefile b/makefile new file mode 100644 index 0000000..e54680c --- /dev/null +++ b/makefile @@ -0,0 +1,138 @@ +# "makefile" is automatically generated from "makefile.1.mak" +# +# Copyright (C) 2023 Balthasar Szczepański +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +ifndef TARGET +TARGET = debug +# when want to change target run this first: +# make -B TARGET=target_name makefile +endif + +CHMOD=/usr/bin/chmod +CP =/usr/bin/cp +MKDIR=/usr/bin/mkdir +RM =/usr/bin/rm + +BIN_PATH = /botm/bin/test-post +LIB_PATH = /botm/lib/test-post + + +CONFIGFILE = settings-$(TARGET).txt settings.txt + +# The configuration tool +# http://bicyclesonthemoon.info/git/botm-config +CONFIGURE = /botm/bin/config/configure.pl +CONFIGURE_CMD = $(PERL) $(CONFIGURE) $(CONFIGFILE) + +# keep these 2 lists in the same order!: +GENERATE_FROM=\ +sendpost.1.pl + +TO_GENERATE=\ +sendpost + +DIR=\ +$(BIN_PATH)\ +$(LIB_PATH) + +SETUID= + +EXEC=\ +sendpost + +BIN=\ +$(EXEC) + +LIB=\ +botm_common.pm + +all: $(BIN) + +makefile: makefile.1.mak $(CONFIGFILE) + $(CONFIGURE_CMD) < makefile.1.mak > makefile + + +$(TO_GENERATE): $(GENERATE_FROM) $(CONFIGFILE) + $(CONFIGURE_CMD) --in $(GENERATE_FROM) --out $(TO_GENERATE) + + +exec: $(EXEC) + $(CHMOD) +x $(EXEC) + +setuid: $(SETUID) +# $(CHMOD) u+s,g+s $(SETUID) + + +mktree: + $(MKDIR) -p $(DIR) +# $(CHMOD) g-r,g-w,g-x,o-r,o-w,o-x $(HIDDEN_DIR) + +ifdef BIN_PATH +cp_bin: $(BIN) setuid exec | mktree + $(RM) -f $(BIN_PATH)/* + $(CP) -p $(BIN) $(BIN_PATH) +endif + +ifdef LIB_PATH +cp_lib: $(LIB) | mktree + $(RM) -f $(LIB_PATH)/* + $(CP) -p $(LIB) $(LIB_PATH) +endif + +install: all cp_bin cp_lib + +clean: + $(RM) -f $(TO_GENERATE) + +PHONY: all clean install cp_bin cp_lib + + + +e: +# CC=gcc +# CC2=g++ +# CF=-g -Wall +# # LF=-lIL +# LF2=-lcgi + +# all: posted mpview remove post bot5 upload viewer enable + +# bot5: bot5.c post.awk mpost.awk makefile + # $(CC) $(CF) -o bot5 bot5.c + +# # deliver: deliver.c +# # $(CC) $(CF) -o deliver deliver.c + +# remove: remove.c verify.awk + # $(CC) $(CF) -o remove remove.c $(LF2) + +# mpview: mpview.c mview.awk verify.awk makefile + # $(CC) $(CF) -o mpview mpview.c $(LF2) + +# posted: posted.c posted.awk makefile + # $(CC) $(CF) -o posted posted.c $(LF2) + +# post: post.cpp preview.awk bb2html.awk verify.awk makefile + # $(CC2) $(CF) -o post post.cpp $(LF2) + +# upload: upload.c upload.awk makefile + # $(CC) $(CF) -o upload upload.c $(LF2) + +# viewer: viewer.c makefile + # $(CC) $(CF) -o viewer viewer.c $(LF2) + +# enable: posted mpview remove post upload viewer + # chmod u+s posted mpview remove post upload viewer diff --git a/makefile.1.mak b/makefile.1.mak index 78cc26b..71d5d7d 100644 --- a/makefile.1.mak +++ b/makefile.1.mak @@ -1,5 +1,4 @@ # "makefile" is automatically generated from "makefile.1.mak" -# 08.05.2023 # # Copyright (C) 2023 Balthasar Szczepański # @@ -28,6 +27,7 @@ endif ###MAKE_RM: RM = rm ###MAKE_BIN_PATH: BIN_PATH = /botm/bin/post +###MAKE_LIB_PATH: LIB_PATH = /botm/lib/post CONFIGFILE = settings-$(TARGET).txt settings.txt @@ -46,6 +46,7 @@ sendpost DIR=\ $(BIN_PATH)\ +$(LIB_PATH) SETUID= @@ -55,6 +56,9 @@ sendpost BIN=\ $(EXEC) +LIB=\ +botm_common.pm + all: $(BIN) makefile: makefile.1.mak $(CONFIGFILE) @@ -82,12 +86,18 @@ cp_bin: $(BIN) setuid exec | mktree $(CP) -p $(BIN) $(BIN_PATH) endif -install: all cp_bin +ifdef LIB_PATH +cp_lib: $(LIB) | mktree + $(RM) -f $(LIB_PATH)/* + $(CP) -p $(LIB) $(LIB_PATH) +endif + +install: all cp_bin cp_lib clean: $(RM) -f $(TO_GENERATE) -PHONY: all clean +PHONY: all clean install cp_bin cp_lib diff --git a/sendpost b/sendpost deleted file mode 100755 index c1b36da..0000000 --- a/sendpost +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/perl - -use strict; -use Getopt::Long; diff --git a/sendpost.1.pl b/sendpost.1.pl index bcfdbea..e5cb857 100644 --- a/sendpost.1.pl +++ b/sendpost.1.pl @@ -1,4 +1,117 @@ ###RUN_PERL: +# Copyright (C) 2023 Balthasar Szczepański +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + use strict; +use utf8; use Getopt::Long; +use Encode qw(encode decode); + +###PERL_LIB: use lib '/botm/lib/post'; +use botm_common qw(readdatafile); + +###PERL_ENCODING_FILE_IN: use constant ENCODING_FILE_IN => 'UTF-8' +###PERL_ENCODING_FILE_OUT: use constant ENCODING_FILE_OUT => 'utf8' +###PERL_ENCODING_STD_IN: use constant ENCODING_STD_IN => 'UTF-8' +###PERL_ENCODING_STD_OUT: use constant ENCODING_STD_OUT => 'utf8' + +binmode STDIN, ':encoding('.ENCODING_STD_IN.')'; +# binmode ARGV, ':encoding('.ENCODING_STD_IN.')'; # not working +binmode STDOUT, ':encoding('.ENCODING_STD_OUT.')'; +binmode STDERR, ':encoding('.ENCODING_STD_OUT.')'; + +my $helptext = + "sendpost [options] [file(s)]\n". + " -s, --username=USERNAME\n". + " -p, --password=PASSWORD\n". + " -t, --subject=POST_SUBJECT\n". + " -c, --content=POST_CONTENT\n". + "\n". + " -d, --data-only\n". + "\n". + " -B, --bbcode\n". + " -b, --no-bbcode\n". + " -S, --smilies\n". + " -s, --no-smilies\n". + " -U, --urls\n". + " -u, --no-urls\n". + " -G, --signature\n". + " -g, --no-signature\n". + " -N, --notify\n". + " -n, --no-notify\n". + "\n". + " -M, --mirror\n". + " -m, --no-mirror\n". + "\n". + " -h, --help\n"; + +my %options = {}; + +foreach my $arg (@ARGV) { + $arg = decode(ENCODING_STD_IN, $arg); +} + +Getopt::Long::Configure('bundling'); +GetOptions ( + 'username|l=s' => \$options{'username'}, + 'password|p=s' => \$options{'password'}, + 'subject|t=s' => \$options{'subject'}, + 'content|c=s' => \$options{'content'}, + + 'data-only|d' => \$options{'data-only'}, + + 'bbcode|B' => \$options{ 'bbcode'}, + 'no-bbcode|b' => \$options{'no-bbcode'}, + 'smilies|S' => \$options{ 'smilies'}, + 'no-smilies|s' => \$options{'no-smilies'}, + 'urls|U' => \$options{ 'urls'}, + 'no-urls|u' => \$options{'no-urls'}, + 'signature|G' => \$options{ 'signature'}, + 'no-signature|g' => \$options{'no-signature'}, + 'notify|N' => \$options{ 'notify'}, + 'no-notify|n' => \$options{'no-notify'}, + + 'mirror|M' => \$options{ 'mirror'}, + 'no-mirror|m' => \$options{'no-mirror'}, + 'help|h' => \$options{ 'help'} +); + +if ($options{'help'} ne '') { + print $helptext; + exit 0; +} + +print "options\n"; +foreach my $ind (keys %options) { + print $ind.'='.$options{$ind}."\n"; +} +print; +foreach my $arg (@ARGV) { + print "\nFILE $arg\n"; + my %data = readdatafile($arg, 'utf8', 1); + foreach my $ind (keys %data) { + print $ind.'='.$data{$ind}."\n"; + } +} +if ((scalar @ARGV) == 0) { + print "\nSTDIN\n"; + my $fh; + my %data = readdatafile(\*STDIN, 'utf8', 1); + foreach my $ind (keys %data) { + print $ind.'='.$data{$ind}."\n"; + } +} + diff --git a/settings-debug.txt b/settings-debug.txt new file mode 100644 index 0000000..df2ea3d --- /dev/null +++ b/settings-debug.txt @@ -0,0 +1,22 @@ +# target name. settings file (THIS FILE) is "settings-TARGET_NAME.txt" +# where TARGET_NAME is the value +target: debug + +# The configuration tool +# http://bicyclesonthemoon.info/git/botm-config +# alternatively, copy configure.pl to source directory. +configure: /botm/bin/config/configure.pl + +bin_path: /botm/bin/test-post +lib_path: /botm/lib/test-post + +encoding_file_in: UTF-8 +encoding_file_out: utf8 +encoding_std_in: UTF-8 +encoding_std_out: utf8 + +cp: /usr/bin/cp +chmod: /usr/bin/chmod +mkdir: /usr/bin/mkdir +perl: /usr/bin/perl +rm: /usr/bin/rm diff --git a/settings-release.txt b/settings-release.txt index 4f8dcdd..f4c4783 100644 --- a/settings-release.txt +++ b/settings-release.txt @@ -8,6 +8,12 @@ target: release configure: /botm/bin/config/configure.pl bin_path: /botm/bin/post +lib_path: /botm/lib/post + +encoding_file_in: UTF-8 +encoding_file_out: utf8 +encoding_std_in: UTF-8 +encoding_std_out: utf8 cp: /usr/bin/cp chmod: /usr/bin/chmod diff --git a/settings.txt b/settings.txt index 0453656..7f34c9a 100644 --- a/settings.txt +++ b/settings.txt @@ -16,17 +16,27 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -MAKE_CONFIGURE = CONFIGURE = $configure -MAKE_TARGET = TARGET = $target - _SHEBANG: #!$0 +_PERL_USE_2: use $0 $1; +_PERL_CONSTANT: use constant $0 => $1; -RUN_PERL = @_SHEBANG($perl) +MAKE_CONFIGURE = CONFIGURE = $configure +MAKE_TARGET = TARGET = $target MAKE_CHMOD = CHMOD=$chmod MAKE_CP = CP =$cp MAKE_MKDIR = MKDIR=$mkdir MAKE_RM = RM =$rm +MAKE_BIN_PATH = BIN_PATH = $bin_path +MAKE_LIB_PATH = LIB_PATH = $lib_path + + +PERL_ENCODING_FILE_IN = @_PERL_CONSTANT(ENCODING_FILE_IN , '$encoding_file_in') +PERL_ENCODING_FILE_OUT = @_PERL_CONSTANT(ENCODING_FILE_OUT, '$encoding_file_out') +PERL_ENCODING_STD_IN = @_PERL_CONSTANT(ENCODING_STD_IN , '$encoding_std_in') +PERL_ENCODING_STD_OUT = @_PERL_CONSTANT(ENCODING_STD_OUT , '$encoding_std_out') +PERL_LIB = @_PERL_USE_2(lib, '$lib_path') + -MAKE_BIN_PATH = BIN_PATH = $bin_path \ No newline at end of file +RUN_PERL = @_SHEBANG($perl) \ No newline at end of file