aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/apaste.c
blob: 1d2a73948afc938b4f1f3257c31ca743ed6c2a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ISC license. */

#include <stdint.h>
#include <string.h>

#include <skalibs/uint16.h>
#include <skalibs/uint32.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr.h>
#include <skalibs/exec.h>

#include <s6-networking/config.h>

#include <apaste/config.h>

#define USAGE "apaste [ -S | -s ] [ -C cadir ] [ -d server[:port] ] [ -r rtimeout ] [ -w wtimeout ] file..."
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char *const *argv)
{
  char const *cadir = APASTE_DEFAULT_CADIR ;
  char const *server = APASTE_DEFAULT_SERVER ;
  int dotls = APASTE_DEFAULT_TLS ;
  uint32_t rt = 0 ;
  uint32_t wt = 0 ;
  uint16_t port = 0 ;
  PROG = "apaste" ;

  {
    subgetopt l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, (char const *const *)argv, "SsC:d:r:w:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'S' : dotls = 0 ; break ;
        case 's' : dotls = 1 ; break ;
        case 'C' : cadir = l.arg ; break ;
        case 'd' :
        {
          char *colon = strchr(l.arg, ':') ;
          server = l.arg ;
          if (colon)
          {
            *colon = 0 ;
            if (!uint160_scan(colon + 1, &port)) dieusage() ;
          }
          break ;
        }
        case 'r' : if (!uint320_scan(l.arg, &rt)) dieusage() ; break ;
        case 'w' : if (!uint320_scan(l.arg, &wt)) dieusage() ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc) dieusage() ;
  if (!port) port = dotls ? APASTE_DEFAULT_TLSPORT : APASTE_DEFAULT_PORT ;
  {
    char const *newargv[12 + argc] ;
    unsigned int m = 0 ;
    char fmtr[UINT32_FMT] ;
    char fmtw[UINT32_FMT] ;
    char fmtp[UINT16_FMT] ;

    fmtp[uint16_fmt(fmtp, port)] = 0 ;

    newargv[m++] = dotls ? S6_NETWORKING_EXTBINPREFIX "s6-tlsclient" : S6_NETWORKING_EXTBINPREFIX "s6-tcpclient" ;
    newargv[m++] = "-N" ;
    newargv[m++] = "--" ;
    newargv[m++] = server ;
    newargv[m++] = fmtp ;
    newargv[m++] = APASTE_BINPREFIX "apastec" ;
    if (rt)
    {
      fmtr[uint32_fmt(fmtr, rt)] = 0 ;
      newargv[m++] = "-r" ;
      newargv[m++] = fmtr ;
    }
    if (rt)
    {
      fmtw[uint32_fmt(fmtw, wt)] = 0 ;
      newargv[m++] = "-w" ;
      newargv[m++] = fmtw ;
    }
    newargv[m++] = "--" ;
    while (argc--) newargv[m++] = *argv++ ;
    newargv[m++] = 0 ;

    if (dotls)
    {
      size_t len = strlen(cadir) ;
      char modif[7 + len] ;
      memcpy(modif, "CADIR=", 6) ;
      memcpy(modif + 6, cadir, len + 1) ;
      xmexec_n(newargv, modif, len + 7, 1) ;
    }
    else xexec(newargv) ;
  }
}