140
static void do_enum(const char *varname)
142
char out[SMALLBUF], temp[SMALLBUF], *val, *sel, *ptr;
144
snprintf(out, sizeof(out),
145
(upsname ? "ENUM %s@%s\n" : "ENUM %s\n"), varname, upsname);
147
if (upscli_sendline(&ups, out, strlen(out)) < 0) {
148
fprintf(stderr, "ENUM %s failed: %s\n", varname,
149
upscli_strerror(&ups));
150
printf("Unable to get ENUM list\n");
154
if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
155
fprintf(stderr, "Read ENUM %s failed: %s\n", varname,
156
upscli_strerror(&ups));
157
printf("Unable to get ENUM list\n");
161
if (strncmp(temp, "ENUM", 4) != 0) {
162
printf("Bogus reply from server for %s\n", varname);
166
printf("<SELECT NAME=\"UPSVAR_%s\">\n", varname);
168
/* 'OPTION "<val>" [SELECTED]' || 'END' */
169
if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
170
printf("<!-- Error reading OPTION list -->\n");
172
fprintf(stderr, "Can't read OPTION list: %s\n",
173
upscli_strerror(&ups));
174
printf("</SELECT>\n");
178
while (strcmp(temp, "END") != 0) {
180
/* split into value and selected */
181
val = strchr(temp, '"');
183
/* sanity check - no " means a protocol problem */
185
printf("<!-- Error: no quote detected in ENUM response -->\n");
186
printf("</SELECT>\n");
193
sel = strchr(val, ' ');
196
ptr = strchr(val, '"');
200
printf("<!-- Error: no quote detected in ENUM response -->\n");
201
printf("</SELECT>\n");
205
val [ptr - val] = '\0';
207
printf("<OPTION VALUE=\"%s\" ", val);
209
if ((sel != NULL) && (!strcmp(sel, "SELECTED")))
212
printf(">%s</OPTION>\n", val);
214
if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
215
printf("<!-- Error reading OPTION list -->\n");
217
fprintf(stderr, "Can't read OPTION list: %s\n",
218
upscli_strerror(&ups));
219
printf("</SELECT>\n");
224
printf("</SELECT>\n");
227
static void do_string(const char *varname, int typelen)
231
if (upscli_getvar(&ups, upsname, varname, temp, sizeof(temp)) == 0)
232
printf("<INPUT TYPE=\"TEXT\" NAME=\"UPSVAR_%s\" VALUE=\"%s\" "
234
varname, temp, typelen);
237
140
static void do_header(const char *title)
239
142
printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
434
static void old_showcmds(const char *desc)
436
char *ptr, cmds[SMALLBUF], *tmp;
438
if (upscli_getlist(&ups, upsname, UPSCLI_LIST_CMDS, cmds,
441
error_page("showcmds", "Protocol failure",
442
"Unable to get list of commands - %s\n",
443
upscli_strerror(&ups));
448
if (strlen(cmds) == 0)
449
error_page("showcmds", "No instant commands supported",
450
"This UPS doesn't support any instant commands.");
452
do_header("Instant commands");
453
printf("<FORM ACTION=\"upsset.cgi\" METHOD=\"POST\">\n");
456
/* include the description from checkhost() if present */
458
printf("<TR><TH BGCOLOR=\"#60B0B0\"COLSPAN=2>%s</TH></TR>\n",
461
printf("<TR BGCOLOR=\"#60B0B0\" ALIGN=\"CENTER\">\n");
462
printf("<TD>Instant commands</TD>\n");
465
printf("<SELECT NAME=\"upscommand\">\n");
467
/* provide a dummy do-nothing default choice */
468
printf("<OPTION VALUE=\"\" SELECTED></OPTION>\n");
473
char buf[SMALLBUF], *q, *desc;
476
ptr = strchr(tmp, ' ');
482
/* get a description for this command from the server */
483
snprintf(buf, sizeof(buf), "INSTCMDDESC %s\n", tmp);
485
/* if description retrieval fails, go with the cmd name */
486
if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
487
printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
491
if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
492
printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
496
q = strchr(buf, '"');
498
if (!q) { /* invalid/missing description, use cmd name */
499
printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
505
q = strchr(desc, '"');
506
if (!q) { /* missing second quote - don't use it */
507
printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, tmp);
512
printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", tmp, desc);
515
printf("</SELECT>\n");
516
printf("</TD></TR>\n");
518
printf("<TR BGCOLOR=\"#60B0B0\">\n");
519
printf("<TD COLSPAN=\"2\" ALIGN=\"CENTER\">\n");
521
printf("<INPUT TYPE=\"HIDDEN\" NAME=\"monups\" VALUE=\"%s\">\n", monups);
522
printf("<INPUT TYPE=\"SUBMIT\" VALUE=\"Issue command\">\n");
523
printf("<INPUT TYPE=\"RESET\" VALUE=\"Reset\">\n");
524
printf("</TD></TR>\n");
525
printf("</TABLE>\n");
528
printf("<TR><TD ALIGN=\"CENTER\">\n");
529
do_pickups("showcmds");
530
printf("</TD></TR>\n");
532
printf("</TABLE>\n");
533
printf("</BODY></HTML>\n");
535
upscli_disconnect(&ups);
539
341
static void print_cmd(const char *cmd)
344
unsigned int numq, numa;
543
346
const char *query[4];
669
475
printf("</BODY></HTML>\n");
671
477
upscli_disconnect(&ups);
675
/* generate a list of instant commands */
676
static void showcmds(void)
680
if (!checkhost(monups, &desc))
681
error_page("showsettings", "Access denied",
682
"Access to that host is not authorized");
694
static void old_docmd(const char *desc, const char *cmd)
698
/* send command, get response, show it */
700
snprintf(buf, sizeof(buf), "INSTCMD %s@%s\n", cmd, upsname);
702
snprintf(buf, sizeof(buf), "INSTCMD %s\n", cmd);
704
if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
705
do_header("Error while issuing command");
709
printf("<TR><TD>Error sending command: %s\n</TD></TR>",
710
upscli_strerror(&ups));
712
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
713
do_pickups("showcmds");
714
printf("</TD></TR>\n");
716
printf("</TABLE>\n");
718
printf("</TD></TR></TABLE>\n");
719
printf("</BODY></HTML>\n");
720
upscli_disconnect(&ups);
724
if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
725
do_header("Error while reading command response");
729
printf("<TR><TD>Error reading command response: %s\n</TD></TR>",
730
upscli_strerror(&ups));
732
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
733
do_pickups("showcmds");
734
printf("</TD></TR>\n");
736
printf("</TABLE>\n");
738
printf("</TD></TR></TABLE>\n");
739
printf("</BODY></HTML>\n");
740
upscli_disconnect(&ups);
744
do_header("Issuing commands");
747
printf("<TR><TD><PRE>\n");
748
printf("Sending command: %s\n", upscommand);
749
printf("Response: %s\n", buf);
750
printf("</PRE></TD></TR>\n");
752
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
753
do_pickups("showcmds");
754
printf("</TD></TR>\n");
756
printf("</TABLE>\n");
757
printf("</TD></TR></TABLE>\n");
758
printf("</BODY></HTML>\n");
760
upscli_disconnect(&ups);
765
static void new_docmd(const char *desc, const char *cmd)
769
snprintf(buf, sizeof(buf), "INSTCMD %s %s\n", upsname, cmd);
771
if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
772
do_header("Error while issuing command");
776
printf("<TR><TD>Error sending command: %s\n</TD></TR>",
777
upscli_strerror(&ups));
779
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
780
do_pickups("showcmds");
781
printf("</TD></TR>\n");
783
printf("</TABLE>\n");
785
printf("</TD></TR></TABLE>\n");
786
printf("</BODY></HTML>\n");
787
upscli_disconnect(&ups);
791
if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
792
do_header("Error while reading command response");
796
printf("<TR><TD>Error reading command response: %s\n</TD></TR>",
797
upscli_strerror(&ups));
799
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
800
do_pickups("showcmds");
801
printf("</TD></TR>\n");
803
printf("</TABLE>\n");
805
printf("</TD></TR></TABLE>\n");
806
printf("</BODY></HTML>\n");
807
upscli_disconnect(&ups);
811
do_header("Issuing command");
814
printf("<TR><TD><PRE>\n");
815
printf("Sending command: %s\n", upscommand);
816
printf("Response: %s\n", buf);
817
printf("</PRE></TD></TR>\n");
819
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
820
do_pickups("showcmds");
821
printf("</TD></TR>\n");
823
printf("</TABLE>\n");
824
printf("</TD></TR></TABLE>\n");
825
printf("</BODY></HTML>\n");
827
upscli_disconnect(&ups);
831
481
/* handle setting authentication data in the server */
832
482
static void send_auth(const char *next)
890
539
send_auth("showcmds");
893
old_docmd(desc, upscommand);
895
if (!strchr(upscommand, '.'))
896
old_docmd(desc, upscommand);
898
new_docmd(desc, upscommand);
903
/* dump a table of all read/write variables and their current values */
904
static void old_showsettings(const char *desc)
906
char vars[SMALLBUF], temp[SMALLBUF], out[SMALLBUF], *v,
910
if (upscli_getlist(&ups, upsname, UPSCLI_LIST_RW, vars,
913
error_page("showsettings", "Server protocol error",
914
"Unable to get variable list - %s\n",
915
upscli_strerror(&ups));
920
if (strlen(vars) == 0) {
921
error_page("showsettings", "No read/write variables",
922
"This UPS has no read/write variables.");
926
/* no spaces = seriously broken string */
927
if (!strchr(vars, ' ')) {
928
error_page("showsettings", "Server protocol error",
929
"Unable to get variable list - broken string");
933
do_header("Current settings");
934
printf("<FORM ACTION=\"upsset.cgi\" METHOD=\"POST\">\n");
541
snprintf(buf, sizeof(buf), "INSTCMD %s %s\n", upsname, upscommand);
543
if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
544
do_header("Error while issuing command");
548
printf("<TR><TD>Error sending command: %s\n</TD></TR>",
549
upscli_strerror(&ups));
551
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
552
do_pickups("showcmds");
553
printf("</TD></TR>\n");
555
printf("</TABLE>\n");
557
printf("</TD></TR></TABLE>\n");
558
printf("</BODY></HTML>\n");
559
upscli_disconnect(&ups);
563
if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
564
do_header("Error while reading command response");
568
printf("<TR><TD>Error reading command response: %s\n</TD></TR>",
569
upscli_strerror(&ups));
571
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
572
do_pickups("showcmds");
573
printf("</TD></TR>\n");
575
printf("</TABLE>\n");
577
printf("</TD></TR></TABLE>\n");
578
printf("</BODY></HTML>\n");
579
upscli_disconnect(&ups);
583
do_header("Issuing command");
937
/* include the description from checkhost() if present */
939
printf("<TR><TH BGCOLOR=\"#60B0B0\"COLSPAN=2>%s</TH></TR>\n",
942
printf("<TR BGCOLOR=\"#60B0B0\">\n");
943
printf("<TH>Setting</TH>\n");
944
printf("<TH>Value</TH></TR>\n");
949
vptr = strchr(v, ' ');
953
/* get description */
954
snprintf(out, sizeof(out), "VARDESC %s\n", v);
955
if (upscli_sendline(&ups, out, strlen(out)) < 0) {
956
printf("Can't get VARDESC\n");
958
fprintf(stderr, "Can't get VARDESC of %s: %s\n",
959
v, upscli_strerror(&ups));
963
if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
964
printf("Can't get VARDESC\n");
966
fprintf(stderr, "Can't get VARDESC of %s: %s\n",
967
v, upscli_strerror(&ups));
971
printf("<TR BGCOLOR=\"#60B0B0\" ALIGN=\"CENTER\">\n");
973
/* strip off leading/trailing quotes */
974
ptr = strchr(temp, '"');
979
ptr2 = strchr(ptr, '"');
984
printf("Unbalanced quotes from server\n");
989
printf("<TD>%s</TD>\n", ptr);
991
/* now get variable type */
992
snprintf(out, sizeof(out),
993
(upsname ? "VARTYPE %s@%s\n" : "VARTYPE %s\n"), v, upsname);
995
if (upscli_sendline(&ups, out, strlen(out)) < 0) {
996
printf("Can't get VARTYPE\n");
998
fprintf(stderr, "Can't get VARTYPE %s: %s\n",
999
v, upscli_strerror(&ups));
1003
if (upscli_readline(&ups, temp, sizeof(temp)) < 0) {
1004
printf("Can't get VARTYPE\n");
1006
fprintf(stderr, "Can't get VARTYPE %s: %s\n",
1007
v, upscli_strerror(&ups));
1014
ptr = strchr(temp, ' ');
1018
ptr = strchr(type, ' ');
1021
typelen = strtol(ptr, (char **) NULL, 10);
1028
if (!strcmp(type, "ENUM"))
1031
if (!strcmp(type, "STRING"))
1032
do_string(v, typelen);
1035
printf("Broken type received from server\n");
1037
printf("</TD></TR>\n");
1042
printf("<TR BGCOLOR=\"#60B0B0\">\n");
1043
printf("<TD COLSPAN=\"2\" ALIGN=\"CENTER\">\n");
1044
do_hidden("savesettings");
1045
printf("<INPUT TYPE=\"HIDDEN\" NAME=\"monups\" VALUE=\"%s\">\n", monups);
1046
printf("<INPUT TYPE=\"SUBMIT\" VALUE=\"Save changes\">\n");
1047
printf("<INPUT TYPE=\"RESET\" VALUE=\"Reset\">\n");
1048
printf("</TD></TR>\n");
1049
printf("</TABLE>\n");
1050
printf("</FORM>\n");
1052
printf("<TR><TD ALIGN=\"CENTER\">\n");
1053
do_pickups("showsettings");
1054
printf("</TD></TR>\n");
1056
printf("</TABLE>\n");
586
printf("<TR><TD><PRE>\n");
587
printf("Sending command: %s\n", upscommand);
588
printf("Response: %s\n", buf);
589
printf("</PRE></TD></TR>\n");
591
printf("<TR><TD ALIGN=\"CENTER\" COLSPAN=2>\n");
592
do_pickups("showcmds");
593
printf("</TD></TR>\n");
595
printf("</TABLE>\n");
596
printf("</TD></TR></TABLE>\n");
1057
597
printf("</BODY></HTML>\n");
1059
599
upscli_disconnect(&ups);
1063
603
static const char *get_data(const char *type, const char *varname)
1065
int ret, numq, numa;
606
unsigned int numq, numa;
1067
608
const char *query[4];
1339
870
printf("</BODY></HTML>\n");
1341
872
upscli_disconnect(&ups);
1345
static void old_setvar(const char *var, const char *val)
1349
upscli_getvar(&ups, upsname, var, buf, sizeof(buf));
1351
/* don't send a SET if it hasn't chnaged */
1352
if (strcmp(val, buf))
1355
printf("set %s to %s (was %s)\n", var, val, buf);
1358
snprintf(buf, sizeof(buf), "SET %s@%s %s\n", var, upsname, val);
1360
snprintf(buf, sizeof(buf), "SET %s %s\n", var, val);
1362
if (upscli_sendline(&ups, buf, strlen(buf)) < 0) {
1363
printf("Error: SET failed: %s\n", upscli_strerror(&ups));
1367
if (upscli_readline(&ups, buf, sizeof(buf)) < 0) {
1368
printf("Error: SET failed: %s\n", upscli_strerror(&ups));
1372
printf("response: %s\n", buf);
1375
static void new_setvar(const char *var, const char *val)
876
static int setvar(const char *var, const char *val)
1377
878
char buf[SMALLBUF], enc[SMALLBUF];
1378
879
const char *tmp;