9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] decomp for rc script
@ 2021-12-16  1:59 有澤 健治
  2021-12-16  2:05 ` ori
  0 siblings, 1 reply; 6+ messages in thread
From: 有澤 健治 @ 2021-12-16  1:59 UTC (permalink / raw)
  To: 9fans

Hello 9fans.

Plan9 C has nice ARGBEGIN and ARGEND macros, which make easy to write 
code for flags and options.
For example composed flag style
     ls -pld dir
is OK. We are not insisted to write decomposed flag style:
     ls -p -l -d dir

In rc code, we write code such as

while(~ $1 -*){
     switch($1){
     case -a
         aopt=$2
         shift
     case -b
         bflag=1
     ...

assuming decomposed flags and options.

I sometimes make mistakes in executing commands forgetting they are 
coded in rc.
I want uniform handling for flags and options in command line.

A small helper tool named "decomp" may be useful for this purpose.
=== BEGIN ===
#!/bin/awk -f
# decomp is designed to make easy to parse
# flags and options in rc script.
# usage: echo flg:opt $* | decomp
# examples:
#    % echo abc:de -cadefg| decomp
#     -c -a -d efg
#    % echo abc:de -caxbcdefg| decomp
#     -c -a# error in x    (-caxbcdefg)
#    % echo abc:de -cabcdefg xyz| decomp
#     -c -a -b -c -d efg xyz
#    % echo abc:de -cabxcdefg -axy | decomp
#     -c -a -b# error in x    (-cabxcdefg)
#    % echo abc:de -cabcd efg -a xy | decomp
#     -c -a -b -c -d efg -a xy
#    % echo abc: -cabcdefg | decomp
#     -c -a -b -c# error in d    (-cabcdefg)
#    % echo :de -cabxcdefg | decomp
#    # error in c    (-cabxcdefg)
#    % echo abc: -a - foo | decomp
#     -a - foo
#    % echo abc: -a - 'f oo' | decomp
#     -a - f oo
#    BUG: decomp cannot handle spaces in arguments
#
# 2021/12/14 Kenar
# function definitions are first
function error(fmt,s){
     printf(fmt,s) > "/dev/cons"
     exit "usage"
}
function isplit(s,n){
     return substr(s,1,n-1) " " substr(s,n)
}
function decomp1(s,s0,b,c,t){
     # then s,s0,b,c,t are local
     if(cont){
         printf(" %s", s)
         cont = 0
         return
     }
     cont = 0
     s0 = s
     for(;;){
         #print "#1", s
         split(isplit(s,2),t," ")
         b=t[1]
         c=t[2]
         #print "#2", b, c
         if(b != "-"){
             printf(" %s", s)
             return
         }
         split(isplit(c,2),t," ")
         b=t[1]
         c=t[2]
         #print "#3", b, c
         if(index(opt,b) != 0){
             if(c == ""){
                 printf(" -%s", b)
                 cont = 1
             }
             else
                 printf(" -%s %s", b, c)
             return
         }
         if(index(flg,b) != 0){
             printf(" -%s", b)
             if(c == "")
                 return
             s = "-" c
             continue
         }
         error(fmt1, b "\t(" s0 ")")
         return
     }
}
BEGIN{ # global constants are here, not insisted by awk though
     fmt1="# error in %s\n"
     fmt2="# error in input line:\n#\t%s\n"\
     "# required flg:opt at the first argument\n"
}
{
     if((m = split($0,u," ")) < 2 || split(u[1],t,":") != 2)
         error(fmt2,$0)
     flg =t[1]
     opt =t[2]
     cont = 0
     #print "#", flg, opt
     for(n = 2; n <= m; n++){
         #print "#", u[n]
         decomp1(u[n])
     }

     # Why the code below doesn't work?
     # split("abc de",u," ")
     # for(a in u){
     #    print a        # a is ?
     # }

     # not required but maybe better in debugging
     printf("\n")
}
=== END ===

Then adding lines
     *=`{echo flg:opt $* | decomp}
     if(~ $status ?*) {do something}
before "while(~ $1 -*)" enables composed flags and options like C.

Maybe someone already have a tool of this sort,
or "decomp" may be improved to be better one,
or there are more bugs in it,
then information is welcome.

Kenji Arisawa





------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T0ceb5dd708ebfd4f-M9d1884360382f65d1acf8c9d
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] decomp for rc script
  2021-12-16  1:59 [9fans] decomp for rc script 有澤 健治
@ 2021-12-16  2:05 ` ori
  2021-12-16 23:58   ` 有澤 健治
  0 siblings, 1 reply; 6+ messages in thread
From: ori @ 2021-12-16  2:05 UTC (permalink / raw)
  To: 9fans

Quoth 有澤 健治 <karisawa@gmail.com>:
> Maybe someone already have a tool of this sort,
> or "decomp" may be improved to be better one,
> or there are more bugs in it,
> then information is welcome.

There's aux/getflags. For example:

        flagfmt='d:debug, b:branch branch'; args='remote [local]'
        eval `''{aux/getflags $*} || exec aux/usage
        if(~ $debug 1)
                debug=(-d)



------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T0ceb5dd708ebfd4f-Md99ba52a99672c1d143fefa3
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] decomp for rc script
  2021-12-16  2:05 ` ori
@ 2021-12-16 23:58   ` 有澤 健治
  2021-12-18  0:09     ` Conor Williams
  2021-12-22  6:17     ` 有澤 健治
  0 siblings, 2 replies; 6+ messages in thread
From: 有澤 健治 @ 2021-12-16 23:58 UTC (permalink / raw)
  To: 9fans

Hello 9fans.

 >There's aux/getflags. For example:
Thanks for the info.

Now, I reconsider:
(a) decomp should always end with error in illegal syntax.
currently
     echo :d -d | decomp
does not end with error. this should be a BUG, which is easy to fix.
(b) AWK is inappropriate for decomp. I will rewrite decomp in C.
then the usage in rc will be:
     * = `{decomp flg:opt $*}

Kenji Arisawa


On 2021/12/16 11:05, ori@eigenstate.org wrote:
> Quoth 有澤 健治 <karisawa@gmail.com>:
>> Maybe someone already have a tool of this sort,
>> or "decomp" may be improved to be better one,
>> or there are more bugs in it,
>> then information is welcome.
> There's aux/getflags. For example:
> 
> flagfmt='d:debug, b:branch branch'; args='remote [local]'
> eval `''{aux/getflags $*} || exec aux/usage
> if(~ $debug 1)
>         debug=(-d)
> 

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T0ceb5dd708ebfd4f-M831bb95d8a3575e0a983ba73
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] decomp for rc script
  2021-12-16 23:58   ` 有澤 健治
@ 2021-12-18  0:09     ` Conor Williams
  2021-12-18  0:37       ` Conor Williams
  2021-12-22  6:17     ` 有澤 健治
  1 sibling, 1 reply; 6+ messages in thread
From: Conor Williams @ 2021-12-18  0:09 UTC (permalink / raw)
  To: 9fans


[-- Attachment #1.1: Type: text/plain, Size: 318 bytes --]

:-Q
On Thu, Dec 24, 2021 at 11:59 PM 健有澤治
<mrXmas@gmail.com> wrote:

> Hi
>

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T0ceb5dd708ebfd4f-M407976a359def05953d5a695
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #1.2: Type: text/html, Size: 1112 bytes --]

[-- Attachment #2: dollarzero.png --]
[-- Type: image/png, Size: 4341 bytes --]

[-- Attachment #3: dollarzero.tiff --]
[-- Type: image/tiff, Size: 4672 bytes --]

[-- Attachment #4: dollar1.png --]
[-- Type: image/png, Size: 5848 bytes --]

[-- Attachment #5: tree2.gif --]
[-- Type: image/gif, Size: 11043 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] decomp for rc script
  2021-12-18  0:09     ` Conor Williams
@ 2021-12-18  0:37       ` Conor Williams
  0 siblings, 0 replies; 6+ messages in thread
From: Conor Williams @ 2021-12-18  0:37 UTC (permalink / raw)
  To: 9fans


[-- Attachment #1.1: Type: text/plain, Size: 421 bytes --]

well

On Sat, Dec 18, 2021 at 12:09 AM Conor Williams <conor.williams@gmail.com>
wrote:

> :-Q
> On Thu, Dec 24, 2021 at 11:59 PM 健有澤治
> <mrXmas@gmail.com> wrote:
>
>> Hi
>>

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T0ceb5dd708ebfd4f-M5bc0f0c20f48d666874b179c
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

[-- Attachment #1.2: Type: text/html, Size: 1503 bytes --]

[-- Attachment #2: filecbp5.ogg --]
[-- Type: audio/ogg, Size: 53044 bytes --]

[-- Attachment #3: blah.mp4 --]
[-- Type: video/mp4, Size: 117335 bytes --]

[-- Attachment #4: blah2.mp4 --]
[-- Type: video/mp4, Size: 114817 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] decomp for rc script
  2021-12-16 23:58   ` 有澤 健治
  2021-12-18  0:09     ` Conor Williams
@ 2021-12-22  6:17     ` 有澤 健治
  1 sibling, 0 replies; 6+ messages in thread
From: 有澤 健治 @ 2021-12-22  6:17 UTC (permalink / raw)
  To: 9fans

Hello 9fans

Decomp is completed.

The pointer to the code and the manual is in
     http://p9.nyx.link/netlib/NEWS

Please try!

Kenji Arisawa


On 2021/12/17 8:58, 有澤 健治 wrote:
> Hello 9fans.
>
> >There's aux/getflags. For example:
> Thanks for the info.
>
> Now, I reconsider:
> (a) decomp should always end with error in illegal syntax.
> currently
>     echo :d -d | decomp
> does not end with error. this should be a BUG, which is easy to fix.
> (b) AWK is inappropriate for decomp. I will rewrite decomp in C.
> then the usage in rc will be:
>     * = `{decomp flg:opt $*}
>
> Kenji Arisawa
>
>
> On 2021/12/16 11:05, ori@eigenstate.org wrote:
>> Quoth 有澤 健治 <karisawa@gmail.com>:
>>> Maybe someone already have a tool of this sort,
>>> or "decomp" may be improved to be better one,
>>> or there are more bugs in it,
>>> then information is welcome.
>> There's aux/getflags. For example:
>> 
>>          flagfmt='d:debug, b:branch branch'; args='remote [local]'
>>          eval `''{aux/getflags $*} || exec aux/usage
>>          if(~ $debug 1)
>>                  debug=(-d)
>> 

------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T0ceb5dd708ebfd4f-M733dfefd7bdfb573fe8d038a
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-12-22  6:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-16  1:59 [9fans] decomp for rc script 有澤 健治
2021-12-16  2:05 ` ori
2021-12-16 23:58   ` 有澤 健治
2021-12-18  0:09     ` Conor Williams
2021-12-18  0:37       ` Conor Williams
2021-12-22  6:17     ` 有澤 健治

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).