Added dumb option detection to tomc
[tlb/tomd.git] / src / tomc / main.c
CommitLineData
06570394
TB
1/* Copyright (C) 2018 Thomas Balzer */
2
3/* This file is part of tomd. */
4
5/* tomd is free software: you can redistribute it and/or modify */
6/* it under the terms of the GNU General Public License as published by */
7/* the Free Software Foundation, either version 3 of the License, or */
8/* (at your option) any later version. */
9
10/* tomd is distributed in the hope that it will be useful, */
11/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13/* GNU General Public License for more details. */
14
15/* You should have received a copy of the GNU General Public License */
16/* along with tomd. If not, see <http://www.gnu.org/licenses/>. */
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <sys/un.h>
21#include <sys/socket.h>
22#include <unistd.h>
23
4dafa6fe
TB
24#include "../../include/macros.h"
25
06570394
TB
26static int sfd;
27
805d0ff4
TB
28static void header(void)
29{
30 tomc_p("Tom's Client, Copyright (C) 2018 Thomas Balzer");
31 tomc_p("GPL v3 or later license.");
32}
33
06570394
TB
34static void init(void)
35{
36 /* init socket connection */
37 sfd =
38 socket(PF_LOCAL,
39 SOCK_STREAM,
40 0);
41 if(sfd < 0){
42 perror("socket");
43 exit(EXIT_FAILURE);
44 }
45
46 struct sockaddr_un addr;
47 addr.sun_family = AF_LOCAL;
48 sprintf(addr.sun_path, "/run/user/1000/tomd/socket");
49
50 if(connect(sfd, (struct sockaddr *) &addr, SUN_LEN(&addr)) != 0){
51 perror("connect");
52 exit(EXIT_FAILURE);
53 }
54}
55
56#define HALLO "hallo there from dumb-client"
57
58void write_hallo(void)
59{
60 printf("writing hallo\n");
61 ssize_t wrote =
62 write(sfd, HALLO, sizeof HALLO);
63 printf("wrote %d bytes.\n", wrote);
64}
65
805d0ff4
TB
66static char *default_name = "no_name";
67static struct{
68 char status, stop, kill, start;
69 char *name;
70} options;
71
72static void extract_options(int argc, char **argv)
73{
74 /* -status <name> Get status - if we thought we ran it, current real status */
75 /* -stop <name> Run provided command to gently stop */
76 /* -kill <name> Aggressive killing of process (signal 15) */
77 /* -start <name> Run name */
78 if(argc == 1){
79 /* only tomc given */
80 tomc_p("no args given.");
81 exit(EXIT_SUCCESS);
82 }
83
84 options.name = NULL;
85
86 for(int i = 1;
87 i < argc;
88 i++){
89 char *arg = argv[i];
90#define X(op) {if(strcmp("-" #op, arg) == 0){options.op = 1; continue;}}
91 X(status);
92 X(stop);
93 X(kill);
94 X(start);
95#undef X
96 /* assume last non option was the name */
97 options.name = arg;
98 }
99
100 if(options.name == NULL){
101 options.name = default_name;
102 }
103}
104
105static void print_options()
06570394 106{
805d0ff4
TB
107 tomc_p("---------");
108 tomc_p(" name: %s", options.name);
109 tomc_p(" kill: %d", options.kill);
110 tomc_p("status: %d", options.status);
111 tomc_p(" stop: %d", options.stop);
112 tomc_p(" start: %d", options.start);
113 tomc_p("---------");
06570394
TB
114}
115
116int main(int argc, char **argv)
117{
118 header();
805d0ff4
TB
119 extract_options(argc, argv);
120 print_options();
06570394 121 init();
805d0ff4 122
06570394
TB
123
124 return EXIT_SUCCESS;
125}