aboutsummaryrefslogtreecommitdiffstats
path: root/argparse.c
blob: 7246d31d8588a3f7b087346fecc4cf6af97b55b4 (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
#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: cputemp2maxfreq <options>\n"
  "\n"
  "Options:\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"
  "-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.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;

 DEBUG1_ARGPARSE("Started\n");

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

// Parse options
 while((opt=getopt(argc,argv,"f:g:hi:l:p:s:t:"))!=-1)
 {
  DEBUG1_ARGPARSE("Argument: %c Value: %s\n",opt,optarg);
  switch(opt)
  {
   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 '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
 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 {
  printf("Invalid value for logger, exiting.\n");
  exit(1);
 }

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