| 1 | package rsyncopts |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | type poptOption struct { |
| 11 | longName string |
| 12 | shortName string |
| 13 | argInfo int |
| 14 | arg any // depends on argInfo |
| 15 | val int // 0 means don't return, just update arg |
| 16 | // descrip string |
| 17 | // argDescrip string |
| 18 | } |
| 19 | |
| 20 | func (o *poptOption) name() string { |
| 21 | if o.longName == "" { |
| 22 | return "-" + o.shortName |
| 23 | } |
| 24 | return "--" + o.longName |
| 25 | } |
| 26 | |
| 27 | // see popt(3) |
| 28 | const ( |
| 29 | POPT_ARG_NONE = iota // int; No argument expected |
| 30 | POPT_ARG_STRING // char*; No type checking to be performed |
| 31 | POPT_ARG_INT // int; An integer argument is expected |
| 32 | POPT_ARG_LONG // long; A long integer is expected |
| 33 | POPT_ARG_INCLUDE_TABLE // nest another option table |
| 34 | POPT_ARG_CALLBACK // call a function |
| 35 | POPT_ARG_INTL_DOMAIN // <not documented in popt(3)> |
| 36 | POPT_ARG_VAL // int; Integer value taken from val |
| 37 | POPT_ARG_FLOAT // float; A float argument is expected |
| 38 | POPT_ARG_DOUBLE // double; A double argument is expected |
| 39 | POPT_ARG_LONGLONG // long long; A long long integer is expected |
| 40 | POPT_ARG_MAINCALL = 16 + 11 |
| 41 | POPT_ARG_ARGV = 12 |
| 42 | POPT_ARG_SHORT = 13 |
| 43 | POPT_ARG_BITSET = 16 + 14 |
| 44 | ) |
| 45 | |
| 46 | const POPT_ARG_MASK = 0x000000FF |
| 47 | |
| 48 | const ( |
| 49 | POPT_ARGFLAG_OR = 0x08000000 |
| 50 | ) |
| 51 | |
| 52 | const ( |
| 53 | POPT_BIT_SET = POPT_ARG_VAL | POPT_ARGFLAG_OR |
| 54 | ) |
| 55 | |
| 56 | type PoptError struct { |
| 57 | Errno int32 |
| 58 | Err error |
| 59 | DaemonMode bool |
| 60 | } |
| 61 | |
| 62 | func (pe *PoptError) Unwrap() error { return pe.Err } |
| 63 | |
| 64 | func (pe *PoptError) Error() string { |
| 65 | if pe.DaemonMode { |
| 66 | return pe.Err.Error() + " (in daemon mode)" |
| 67 | } |
| 68 | return pe.Err.Error() |
| 69 | } |
| 70 | |
| 71 | // TODO(later): turn these into sentinel error values |
| 72 | // which stringify like poptStrerror() |
| 73 | const ( |
| 74 | POPT_ERROR_NOARG = -10 // missing argument |
| 75 | POPT_ERROR_BADOPT = -11 // unknown option |
| 76 | POPT_ERROR_UNWANTEDARG = -12 // option does not take an argument |
| 77 | POPT_ERROR_OPTSTOODEEP = -13 // aliases nested too deeply |
| 78 | POPT_ERROR_BADQUOTE = -15 // error in parameter quoting |
| 79 | POPT_ERROR_ERRNO = -16 // errno set, use strerror(errno) |
| 80 | POPT_ERROR_BADNUMBER = -17 // invalid numeric value |
| 81 | POPT_ERROR_OVERFLOW = -18 // number too large or too small |
| 82 | POPT_ERROR_BADOPERATION = -19 // mutually exclusive logical operations requested |
| 83 | POPT_ERROR_NULLARG = -20 // opt->arg should not be NULL |
| 84 | POPT_ERROR_MALLOC = -21 // memory allocation failed |
| 85 | POPT_ERROR_BADCONFIG = -22 // config file failed sanity test |
| 86 | ) |
| 87 | |
| 88 | type Context struct { |
| 89 | // state |
| 90 | table []poptOption |
| 91 | args []string |
| 92 | nextCharArg string |
| 93 | nextArg string |
| 94 | |
| 95 | // output |
| 96 | Options *Options |
| 97 | RemainingArgs []string |
| 98 | } |
| 99 | |
| 100 | func (pc *Context) findOption(longName, shortName string) *poptOption { |
| 101 | for idx, opt := range pc.table { |
| 102 | if longName != "" && opt.longName == longName { |
| 103 | return &pc.table[idx] |
| 104 | } |
| 105 | if shortName != "" && opt.shortName == shortName { |
| 106 | return &pc.table[idx] |
| 107 | } |
| 108 | } |
| 109 | return nil |
| 110 | } |
| 111 | |
| 112 | func (pc *Context) poptSaveInt(opt *poptOption, val int) bool { |
| 113 | intPtr := opt.arg.(*int) |
| 114 | if intPtr == nil { |
| 115 | return false |
| 116 | } |
| 117 | if opt.argInfo&POPT_ARGFLAG_OR != 0 { |
| 118 | *intPtr |= val |
| 119 | } else { |
| 120 | *intPtr = val |
| 121 | } |
| 122 | return true |
| 123 | } |
| 124 | |
| 125 | func (pc *Context) poptSaveArg(opt *poptOption, nextArg string) int32 { |
| 126 | argType := opt.argInfo & POPT_ARG_MASK |
| 127 | switch argType { |
| 128 | case POPT_ARG_INT: |
| 129 | i, err := strconv.ParseInt(nextArg, 0, 64) |
| 130 | if err != nil { |
| 131 | return POPT_ERROR_BADNUMBER |
| 132 | } |
| 133 | if i < math.MinInt32 || i > math.MaxInt32 { |
| 134 | return POPT_ERROR_OVERFLOW |
| 135 | } |
| 136 | pc.poptSaveInt(opt, int(i)) |
| 137 | return 0 |
| 138 | |
| 139 | case POPT_ARG_STRING: |
| 140 | stringPtr := opt.arg.(*string) |
| 141 | if stringPtr == nil { |
| 142 | return 0 |
| 143 | } |
| 144 | *stringPtr = nextArg |
| 145 | return 0 |
| 146 | } |
| 147 | |
| 148 | return POPT_ERROR_BADOPERATION |
| 149 | } |
| 150 | |
| 151 | func (pc *Context) poptGetNextOpt() (int32, error) { |
| 152 | var opt *poptOption |
| 153 | for { |
| 154 | var longArg string |
| 155 | if pc.nextCharArg == "" && len(pc.args) == 0 { |
| 156 | return -1, nil // done |
| 157 | } |
| 158 | if pc.nextCharArg == "" { |
| 159 | // process next long option |
| 160 | origOptString := pc.args[0] |
| 161 | pc.args = pc.args[1:] |
| 162 | if origOptString == "" { |
| 163 | return -1, &PoptError{ |
| 164 | Errno: POPT_ERROR_BADOPT, |
| 165 | Err: fmt.Errorf("unknown option: origOptString empty"), |
| 166 | } |
| 167 | } |
| 168 | if origOptString[0] != '-' || origOptString == "-" { |
| 169 | pc.RemainingArgs = append(pc.RemainingArgs, origOptString) |
| 170 | continue |
| 171 | } |
| 172 | before, after, found := strings.Cut(origOptString, "=") |
| 173 | if found { |
| 174 | longArg = after |
| 175 | } |
| 176 | // remove the one dash we ensured is present |
| 177 | before = strings.TrimPrefix(before, "-") |
| 178 | // a second dash is permitted |
| 179 | oneDash := false |
| 180 | if strings.HasPrefix(before, "-") { |
| 181 | before = strings.TrimPrefix(before, "-") |
| 182 | } else { |
| 183 | oneDash = true |
| 184 | } |
| 185 | opt = pc.findOption(before, "") |
| 186 | if opt == nil && !oneDash { |
| 187 | return -1, &PoptError{ |
| 188 | Errno: POPT_ERROR_BADOPT, |
| 189 | Err: fmt.Errorf("%s: unknown option", origOptString), |
| 190 | } |
| 191 | } |
| 192 | if opt == nil { |
| 193 | // try and parse it as a short option |
| 194 | pc.nextCharArg = origOptString[1:] |
| 195 | longArg = "" |
| 196 | } |
| 197 | } |
| 198 | if pc.nextCharArg != "" { |
| 199 | // process next short option |
| 200 | opt = pc.findOption("", pc.nextCharArg[:1]) |
| 201 | if opt == nil { |
| 202 | return -1, &PoptError{ |
| 203 | Errno: POPT_ERROR_BADOPT, |
| 204 | Err: fmt.Errorf("-%s: unknown option", pc.nextCharArg[:1]), |
| 205 | } |
| 206 | } |
| 207 | pc.nextCharArg = pc.nextCharArg[1:] |
| 208 | } |
| 209 | if opt == nil { |
| 210 | // neither long nor short? how can we end up here? |
| 211 | return -1, &PoptError{ |
| 212 | Errno: POPT_ERROR_BADOPT, |
| 213 | //lint:ignore ST1005 we need this punctuation for dramatic effect! |
| 214 | Err: fmt.Errorf("neither long nor short option found?!"), |
| 215 | } |
| 216 | } |
| 217 | argType := opt.argInfo & POPT_ARG_MASK |
| 218 | if argType == POPT_ARG_NONE || argType == POPT_ARG_VAL { |
| 219 | if longArg != "" || strings.HasPrefix(pc.nextCharArg, "=") { |
| 220 | return -1, &PoptError{ |
| 221 | Errno: POPT_ERROR_UNWANTEDARG, |
| 222 | Err: fmt.Errorf("option %s does not take an argument", opt.name()), |
| 223 | } |
| 224 | } |
| 225 | if opt.arg != nil { |
| 226 | val := 1 |
| 227 | if argType == POPT_ARG_VAL { |
| 228 | val = opt.val |
| 229 | } |
| 230 | if !pc.poptSaveInt(opt, val) { |
| 231 | return -1, &PoptError{ |
| 232 | Errno: POPT_ERROR_BADOPERATION, |
| 233 | Err: fmt.Errorf("poptSaveInt"), |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } else { |
| 238 | nextArg := longArg |
| 239 | if longArg != "" { |
| 240 | } else if pc.nextCharArg != "" { |
| 241 | nextArg = strings.TrimPrefix(pc.nextCharArg, "=") |
| 242 | pc.nextCharArg = "" |
| 243 | } else { |
| 244 | if len(pc.args) == 0 { |
| 245 | return -1, &PoptError{ |
| 246 | Errno: POPT_ERROR_NOARG, |
| 247 | Err: fmt.Errorf("missing argument for option %s", opt.name()), |
| 248 | } |
| 249 | } |
| 250 | nextArg = pc.args[0] |
| 251 | pc.args = pc.args[1:] |
| 252 | } |
| 253 | pc.nextArg = nextArg |
| 254 | if opt.arg != nil { |
| 255 | if errno := pc.poptSaveArg(opt, nextArg); errno != 0 { |
| 256 | return -1, &PoptError{ |
| 257 | Errno: errno, |
| 258 | Err: fmt.Errorf("poptSaveArg errno %d", errno), |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | if opt.val != 0 && argType != POPT_ARG_VAL { |
| 264 | return int32(opt.val), nil |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | func (pc *Context) poptGetOptArg() string { |
| 270 | ret := pc.nextArg |
| 271 | pc.nextArg = "" |
| 272 | return ret |
| 273 | } |