aboutsummaryrefslogtreecommitdiffstats
path: root/argparse.c
blob: 75ca20dd5f0e185186b6cb33a6425156a0d81953 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "cputemp2maxfreq.h"
#include "debug.h"
#include "logger.h"

extern struct s_config config;

void printhelp()
{
 printf(
  "Usage: %s <options>\n"
  "\n"
  "Options:\n"
  "-c <file>      Write CSV log for later analysis to file. Append data if the\n"
  "               file already exists\n"
  "-C <file>      Same as -c but overwrite existing file\n"
  "-f <frequency> Fallback frequency in KHz, used when everything fails\n"
  "               Default: %ld\n"
  "-g <governor>  cpufreq governor to use, use \"keep\" to keep current governor\n"
  "               Default: %s\n"
  "-h             Display this help text\n"
  "-i <file>      Input for temperature readout, usually somewhere in sysfs\n"
  "               Default: %s\n"
  "-l <logger>    Logger to use, valid values: none, kmsg, stdout, syslog\n"
  "               Default: %s\n"
  "-m             Log measurements (CPU temperature and frequency)\n"
  "-p <time>      Poll interval in seconds\n"
  "               Default: %d\n"
  "-s <step>      Step size in Khz when increasing/decreasing CPU speed\n"
  "               Default: %ld\n"
  "-t <number>    Temperature limit\n"
  "               Default: %ld\n",
  config.name,config.fallback_freq,config.governor,config.temp_input,config.logger_name,
  config.interval,config.freq_step,config.max_temp/1000
 );
}

void argparse(int argc, char **argv)
{
 int opt;
 struct s_config userconfig;
 char *lastslash;
 DEBUG1_ARGPARSE("Started\n");

 lastslash=strrchr(argv[0],'/');
 if (lastslash==NULL)
 {
  lastslash=argv[0];
 } else {
  lastslash++;
 }
 strncpy(config.name,lastslash,sizeof(config.name));
 DEBUG2_ARGPARSE("Detected program name %s\n",config.name);

// Copy defaults
 memcpy(&userconfig,&config,sizeof(struct s_config));

// Parse options
 while((opt=getopt(argc,argv,"c:C:f:g:hi:l:mp:s:t:"))!=-1)
 {
  DEBUG1_ARGPARSE("Argument: %c Value: %s\n",opt,optarg);
  switch(opt)
  {
   case 'c':
    strncpy(userconfig.csvlog,optarg,sizeof(userconfig.csvlog));
    userconfig.csvoverwrite=0;
    break;
   case 'C':
    strncpy(userconfig.csvlog,optarg,sizeof(userconfig.csvlog));
    userconfig.csvoverwrite=1;
    break;
   case 'f':
    userconfig.fallback_freq=strtoll(optarg,NULL,10);
    break;
   case 'g':
    strncpy(userconfig.governor,optarg,sizeof(userconfig.governor));
    break;
   case 'h':
    printhelp();
    exit(1);
   case 'i':
    strncpy(userconfig.temp_input,optarg,sizeof(userconfig.temp_input));
    break;
   case 'l':
    strncpy(userconfig.logger_name,optarg,sizeof(userconfig.logger_name));
    break;
   case 'm':
    userconfig.log_data=1;
    break;
   case 'p':
    userconfig.interval=strtoll(optarg,NULL,10);
    break;
   case 's':
    userconfig.freq_step=strtoll(optarg,NULL,10);
    break;
   case 't':
    userconfig.max_temp=strtoll(optarg,NULL,10)*1000;
    break;
   default:
    printhelp();
    exit(1);
  }
 }

// Change the logger function pointer
 DEBUG1_ARGPARSE("Logger is set to %s\n",userconfig.logger_name);
 if (strcmp(userconfig.logger_name,"none")==0) userconfig.logger=&logger_none;
 else if (strcmp(userconfig.logger_name,"stdout")==0) userconfig.logger=&logger_stdout;
 else if (strcmp(userconfig.logger_name,"syslog")==0) userconfig.logger=&logger_syslog;
 else if (strcmp(userconfig.logger_name,"kmsg")==0) userconfig.logger=&logger_kmsg;
 else {
  config.logger("Invalid value for logger, exiting");
  exit(1);
 }
 DEBUG2_ARGPARSE("Logger pointer is %p\n",userconfig.logger);

// Copy modified config to global config
 memcpy(&config,&userconfig,sizeof(struct s_config));
}