#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "config.h"
#include "inifile.hh"
int main(int argc, char *argv[])
{
int t;
int num = 1;
char _variable[LINELEN] = "";
char *variable = 0;
char _section[LINELEN] = "";
char *section = 0;
char path[LINELEN] = "emc.ini";
const char *inistring;
int retval;
bool tildeexpand=false;
for (t = 1; t < argc; t++) {
if (!strcmp(argv[t], "-ini")) {
if (t == argc - 1) {
fprintf(stderr,
"%s: ini file not specified after -ini\n", argv[0]);
exit(1);
} else {
strncpy(path, argv[t + 1], LINELEN);
t++;
}
} else if (!strcmp(argv[t], "-var")) {
if (t == argc - 1) {
fprintf(stderr,
"%s: variable name not specified after -var\n", argv[0]);
exit(1);
} else {
strncpy(_variable, argv[t + 1], LINELEN);
variable = _variable;
t++;
}
} else if (!strcmp(argv[t], "-sec")) {
if (t == argc - 1) {
fprintf(stderr,
"%s: section name not specified after -sec\n", argv[0]);
exit(1);
} else {
strncpy(_section, argv[t + 1], LINELEN);
section = _section;
t++;
}
} else if (!strcmp(argv[t], "-num")) {
if (t == argc - 1) {
fprintf(stderr,
"%s: line not specified after -num\n", argv[0]);
exit(1);
} else {
if (sscanf(argv[t + 1], "%i", &num) != 1) {
fprintf(stderr,
"%s: invalid number after -num\n", argv[0]);
exit(1);
}
t++;
}
} else if (!strcmp(argv[t], "-tildeexpand")) {
tildeexpand = !tildeexpand;
} else{
fprintf(stderr,
"%s: -var <variable> {-sec <section>} {<-ini inifile>} [-num <nth item>]\n",
argv[0]);
exit(1);
}
}
if (0 == variable) {
fprintf(stderr, "%s: no variable supplied\n", argv[0]);
exit(1);
}
IniFile inifile;
inifile.Open(path);
if (inifile.IsOpen() == false) {
fprintf(stderr, "%s: can't open %s\n", argv[0], path);
exit(-1);
}
inistring = inifile.Find(variable, section, num);
if (inistring != NULL) {
if(tildeexpand)
{
char expanded[PATH_MAX];
inifile.TildeExpansion(inistring, expanded, sizeof(expanded));
printf("%s\n", expanded);
} else {
printf("%s\n", inistring);
}
retval = 0;
} else {
fprintf(stderr, "Can not find -sec %s -var %s -num %i \n", section, variable, num);
retval = 1;
}
exit(retval);
}