2023-10-27 09:20:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-07 03:34:17 +00:00
|
|
|
"flag"
|
2023-10-27 09:20:02 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-09-07 03:34:17 +00:00
|
|
|
//"strings"
|
2024-02-22 15:32:17 +00:00
|
|
|
"code.miflux.com/hyung-hwan/hcl/go"
|
2023-10-27 09:20:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
var src []uint16 = utf16.Encode(([]rune)(
|
|
|
|
`(printf ">>>>>>>>> [%d]\n" (+ 30 455))
|
|
|
|
|
|
|
|
(printf ">>>>>>>>> [%d]\n" (+ 11 455))
|
|
|
|
#include "a.hcl"
|
|
|
|
(printf ">>>>>>>>> [%d]\n" (+ 20 455))
|
|
|
|
`))
|
|
|
|
*/
|
2023-11-03 09:04:46 +00:00
|
|
|
|
2024-01-21 16:27:39 +00:00
|
|
|
/* to be set in build time */
|
|
|
|
var BINDIR = "."
|
|
|
|
var SBINDIR = "."
|
|
|
|
var LIBDIR = "."
|
|
|
|
var SYSCONFDIR = "."
|
|
|
|
|
2023-11-03 09:04:46 +00:00
|
|
|
type Param struct {
|
2023-11-05 14:33:22 +00:00
|
|
|
log_file string
|
2023-11-03 09:04:46 +00:00
|
|
|
input_file string
|
2024-09-07 03:34:17 +00:00
|
|
|
heapsize uint
|
|
|
|
modlibdirs string
|
|
|
|
fs_usage func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func empty_usage() {
|
|
|
|
|
2023-11-03 09:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handle_arguments(param *Param) error {
|
2024-09-07 03:34:17 +00:00
|
|
|
/*
|
2023-11-03 09:04:46 +00:00
|
|
|
var nargs int = len(os.Args)
|
|
|
|
var i int
|
|
|
|
|
|
|
|
for i = 1; i < nargs; i++ {
|
2023-11-05 14:33:22 +00:00
|
|
|
if strings.HasPrefix(os.Args[i], "--log=") {
|
2023-11-03 09:04:46 +00:00
|
|
|
param.log_file = os.Args[i][6:]
|
2023-11-05 14:33:22 +00:00
|
|
|
} else if os.Args[i] == "--log" {
|
2023-11-03 09:04:46 +00:00
|
|
|
i++
|
|
|
|
param.log_file = os.Args[i]
|
|
|
|
} else if strings.HasPrefix(os.Args[i], "--") || strings.HasPrefix(os.Args[i], "-") {
|
|
|
|
return fmt.Errorf("unknown option - %s", os.Args[i])
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= nargs {
|
|
|
|
return fmt.Errorf("no input file specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
param.input_file = os.Args[i]
|
|
|
|
return nil
|
2024-09-07 03:34:17 +00:00
|
|
|
*/
|
|
|
|
var fs *flag.FlagSet
|
|
|
|
var heapsize *uint
|
|
|
|
var modlibdirs *string
|
|
|
|
var log *string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
fs = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
|
|
|
|
|
|
|
heapsize = fs.Uint("heapsize", 0, "specify heap size")
|
|
|
|
modlibdirs = fs.String("modlibdirs", "", "specify module library directories")
|
|
|
|
log = fs.String("log", "", "specify log file")
|
|
|
|
|
|
|
|
param.fs_usage = fs.Usage
|
|
|
|
fs.Usage = empty_usage // i don't want fs.Parse() print the usage
|
|
|
|
err = fs.Parse(os.Args[1:])
|
|
|
|
fs.Usage = param.fs_usage // restore it
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("command line error - %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2024-09-07 03:54:16 +00:00
|
|
|
if fs.NArg() < 1 {
|
|
|
|
return fmt.Errorf("no input file specified")
|
|
|
|
} else if fs.NArg() > 1 {
|
|
|
|
return fmt.Errorf("too many input files specified")
|
2024-09-07 03:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
param.input_file = fs.Arg(0);
|
|
|
|
param.log_file = *log // TODO: parse the option part (e.g. --log /dev/stderr,debug+)
|
|
|
|
param.heapsize = *heapsize // TODO: set this to hcl
|
|
|
|
param.modlibdirs = *modlibdirs // TODO: set this to hcl
|
|
|
|
return nil;
|
2023-11-03 09:04:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 09:20:02 +00:00
|
|
|
func main() {
|
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
var x *hcl.HCL = nil
|
|
|
|
var err error = nil
|
2023-11-03 09:04:46 +00:00
|
|
|
var param Param
|
2023-10-27 09:20:02 +00:00
|
|
|
|
2023-11-05 13:31:33 +00:00
|
|
|
var rfh hcl.CciFileHandler
|
|
|
|
var sfh hcl.UdiFileHandler
|
|
|
|
var pfh hcl.UdoFileHandler
|
2023-10-27 09:20:02 +00:00
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
err = handle_arguments(¶m)
|
2023-11-03 09:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err.Error())
|
2024-09-07 03:34:17 +00:00
|
|
|
param.fs_usage()
|
2023-11-03 09:04:46 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
x, err = hcl.New()
|
2023-10-27 09:20:02 +00:00
|
|
|
if err != nil {
|
2023-11-01 07:06:28 +00:00
|
|
|
fmt.Printf("Error: failed to instantiate hcl - %s\n", err.Error())
|
2023-10-27 09:20:02 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-11-01 07:06:28 +00:00
|
|
|
|
2023-11-03 09:04:46 +00:00
|
|
|
if param.log_file != "" {
|
2023-11-05 14:33:22 +00:00
|
|
|
x.SetLogMask(^hcl.BitMask(0))
|
|
|
|
x.SetLogTarget("/dev/stderr")
|
2023-11-03 09:04:46 +00:00
|
|
|
}
|
2023-11-01 07:06:28 +00:00
|
|
|
|
2024-07-06 03:16:05 +00:00
|
|
|
x.SetTrait(x.GetTrait() | hcl.TRAIT_LANG_ENABLE_EOL)
|
2024-01-21 16:38:13 +00:00
|
|
|
|
2023-10-29 12:40:05 +00:00
|
|
|
err = x.Ignite(1000000)
|
2023-10-27 09:20:02 +00:00
|
|
|
if err != nil {
|
2023-11-01 07:06:28 +00:00
|
|
|
fmt.Printf("Error: failed to ignite - %s\n", err.Error())
|
2023-10-29 12:40:05 +00:00
|
|
|
goto oops
|
2023-10-27 09:20:02 +00:00
|
|
|
}
|
2023-10-29 12:40:05 +00:00
|
|
|
err = x.AddBuiltinPrims()
|
|
|
|
if err != nil {
|
2023-11-01 07:06:28 +00:00
|
|
|
fmt.Printf("Error: failed to add builtin primitives - %s\n", err.Error())
|
2023-10-29 12:40:05 +00:00
|
|
|
goto oops
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
err = x.AttachCCIO(&rfh, param.input_file)
|
2023-11-05 07:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err.Error())
|
|
|
|
goto oops
|
|
|
|
}
|
|
|
|
|
|
|
|
err = x.AttachUDIO(&sfh, &pfh)
|
2023-10-31 12:17:08 +00:00
|
|
|
if err != nil {
|
2023-11-01 07:06:28 +00:00
|
|
|
fmt.Printf("Error: %s\n", err.Error())
|
|
|
|
goto oops
|
2023-10-31 12:17:08 +00:00
|
|
|
}
|
2023-11-01 07:06:28 +00:00
|
|
|
|
2023-11-05 14:33:22 +00:00
|
|
|
err = x.BeginFeed()
|
2023-11-03 09:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err.Error())
|
|
|
|
goto oops
|
|
|
|
}
|
|
|
|
|
|
|
|
err = x.FeedFromFile(param.input_file)
|
|
|
|
//err = x.FeedString(`(printf ">>>>>>>>> [%d]\n" (+ 30 455))
|
|
|
|
// (printf ">>>>>>>>> [%d]\n" (+ 11 455))
|
|
|
|
// #include "a.hcl"
|
|
|
|
// (printf ">>>>>>>>> [%d]\n" (+ 20 455))`)
|
2023-11-01 07:06:28 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err.Error())
|
|
|
|
goto oops
|
|
|
|
}
|
|
|
|
|
|
|
|
err = x.EndFeed()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err.Error())
|
|
|
|
goto oops
|
|
|
|
}
|
|
|
|
|
|
|
|
x.Decode()
|
2023-11-05 14:33:22 +00:00
|
|
|
x.SetLogMask(0)
|
2023-10-29 12:40:05 +00:00
|
|
|
|
2023-11-01 07:06:28 +00:00
|
|
|
err = x.Execute()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err.Error())
|
|
|
|
goto oops
|
|
|
|
}
|
2023-10-29 12:40:05 +00:00
|
|
|
|
2023-10-27 09:20:02 +00:00
|
|
|
x.Close()
|
2023-10-29 12:40:05 +00:00
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
oops:
|
|
|
|
if x != nil {
|
|
|
|
x.Close()
|
|
|
|
x = nil
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2023-10-27 09:20:02 +00:00
|
|
|
}
|