2d3e7189633768376e76748ba6c207df595d9e0c
[cascardo/declara.git] / lib / sistema.c
1 /*
2  *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "sistema.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include "cmd.h"
26
27 void sistema_free(struct declaracao *dec)
28 {
29         if (dec->sistema.so)
30                 free(dec->sistema.so);
31         if (dec->sistema.so_versao)
32                 free(dec->sistema.so_versao);
33         if (dec->sistema.jvm_versao)
34                 free(dec->sistema.jvm_versao);
35         if (dec->sistema.mac)
36                 free(dec->sistema.mac);
37 }
38
39 static int run_sistema(struct declaracao *dec, char **args, int argc)
40 {
41         struct rendimento *rendimento;
42         int r;
43         if (argc != 5)
44                 return -EINVAL;
45         dec->sistema.so = strdup(args[1]);
46         dec->sistema.so_versao = strdup(args[2]);
47         dec->sistema.jvm_versao = strdup(args[3]);
48         dec->sistema.mac = strdup(args[4]);
49         if (!dec->sistema.so || !dec->sistema.so_versao ||
50             !dec->sistema.jvm_versao || !dec->sistema.mac)
51                 goto out;
52         return 0;
53 out:
54         sistema_free(dec);
55         return -ENOMEM;
56 }
57
58 void sistema_salva(struct declaracao *dec, FILE *f)
59 {
60         fprintf(f, "sistema \"%s\" \"%s\" \"%s\" \"%s\"\n",
61                 dec->sistema.so, dec->sistema.so_versao,
62                 dec->sistema.jvm_versao, dec->sistema.mac);
63 }
64
65 static struct cmd cmd_sistema = {
66         .name = "sistema",
67         .run = run_sistema,
68 };
69
70 int sistema_cmd_init(void)
71 {
72         cmd_add(&cmd_sistema);
73         return 0;
74 }
75
76 #define SISTEMA_SO_DEFAULT "LINUX"
77 #define SISTEMA_SO_VERSAO_DEFAULT "3.16.0"
78 #define SISTEMA_JVM_VERSAO_DEFAULT "1.7.0"
79 #define SISTEMA_MAC_DEFAULT "000000000000"
80
81 char * sistema_get_so(struct declaracao *dec)
82 {
83         if (!dec->sistema.so)
84                 return SISTEMA_SO_DEFAULT;
85         return dec->sistema.so;
86 }
87
88 char * sistema_get_so_versao(struct declaracao *dec)
89 {
90         if (!dec->sistema.so_versao)
91                 return SISTEMA_SO_VERSAO_DEFAULT;
92         return dec->sistema.so_versao;
93 }
94
95 char * sistema_get_jvm_versao(struct declaracao *dec)
96 {
97         if (!dec->sistema.jvm_versao)
98                 return SISTEMA_JVM_VERSAO_DEFAULT;
99         return dec->sistema.jvm_versao;
100 }
101
102 char * sistema_get_mac(struct declaracao *dec)
103 {
104         if (!dec->sistema.mac)
105                 return SISTEMA_MAC_DEFAULT;
106         return dec->sistema.mac;
107 }