int upscli_readline(UPSCONN *ups, char *buf, size_t buflen) { char tmp[LARGEBUF]; if (!ups) return -1; if ((!buf) || (buflen < 1) || (ups->fd == -1)) { ups->upserror = UPSCLI_ERR_INVALIDARG; return -1; } if (!upscli_checkmagic(ups)) { ups->upserror = UPSCLI_ERR_INVALIDARG; return -1; } if (upscli_read(ups, tmp, sizeof(tmp)) != 0) return -1; if (upscli_errcheck(ups, tmp) != 0) return -1; snprintf(buf, buflen, "%s", tmp); return 0; }
int upscli_list_start(UPSCONN_t *ups, unsigned int numq, const char **query) { char cmd[UPSCLI_NETBUF_LEN], tmp[UPSCLI_NETBUF_LEN]; if (!ups) { return -1; } if (numq < 1) { ups->upserror = UPSCLI_ERR_INVALIDARG; return -1; } /* create the string to send to upsd */ build_cmd(cmd, sizeof(cmd), "LIST", numq, query); if (upscli_sendline(ups, cmd, strlen(cmd)) != 0) { return -1; } if (upscli_readline(ups, tmp, sizeof(tmp)) != 0) { return -1; } if (upscli_errcheck(ups, tmp) != 0) { return -1; } if (!pconf_line(&ups->pc_ctx, tmp)) { ups->upserror = UPSCLI_ERR_PARSE; return -1; } if (ups->pc_ctx.numargs < 2) { ups->upserror = UPSCLI_ERR_PROTOCOL; return -1; } /* the response must start with BEGIN LIST */ if ((strcasecmp(ups->pc_ctx.arglist[0], "BEGIN") != 0) || (strcasecmp(ups->pc_ctx.arglist[1], "LIST") != 0)) { ups->upserror = UPSCLI_ERR_PROTOCOL; return -1; } /* q: [LIST] VAR <ups> * * a: [BEGIN LIST] VAR <ups> */ /* compare q[0]... to a[2]... */ if (!verify_resp(numq, query, &ups->pc_ctx.arglist[2])) { ups->upserror = UPSCLI_ERR_PROTOCOL; return -1; } return 0; }
int upscli_get(UPSCONN_t *ups, unsigned int numq, const char **query, unsigned int *numa, char ***answer) { char cmd[UPSCLI_NETBUF_LEN], tmp[UPSCLI_NETBUF_LEN]; if (!ups) { return -1; } if (numq < 1) { ups->upserror = UPSCLI_ERR_INVALIDARG; return -1; } /* create the string to send to upsd */ build_cmd(cmd, sizeof(cmd), "GET", numq, query); if (upscli_sendline(ups, cmd, strlen(cmd)) != 0) { return -1; } if (upscli_readline(ups, tmp, sizeof(tmp)) != 0) { return -1; } if (upscli_errcheck(ups, tmp) != 0) { return -1; } if (!pconf_line(&ups->pc_ctx, tmp)) { ups->upserror = UPSCLI_ERR_PARSE; return -1; } /* q: [GET] VAR <ups> <var> * * a: VAR <ups> <var> <val> */ if (ups->pc_ctx.numargs < numq) { ups->upserror = UPSCLI_ERR_PROTOCOL; return -1; } if (!verify_resp(numq, query, ups->pc_ctx.arglist)) { ups->upserror = UPSCLI_ERR_PROTOCOL; return -1; } *numa = ups->pc_ctx.numargs; *answer = ups->pc_ctx.arglist; return 0; }
int upscli_list_next(UPSCONN_t *ups, unsigned int numq, const char **query, unsigned int *numa, char ***answer) { char tmp[UPSCLI_NETBUF_LEN]; if (!ups) { return -1; } if (upscli_readline(ups, tmp, sizeof(tmp)) != 0) { return -1; } if (upscli_errcheck(ups, tmp) != 0) { return -1; } if (!pconf_line(&ups->pc_ctx, tmp)) { ups->upserror = UPSCLI_ERR_PARSE; return -1; } if (ups->pc_ctx.numargs < 1) { ups->upserror = UPSCLI_ERR_PROTOCOL; return -1; } *numa = ups->pc_ctx.numargs; *answer = ups->pc_ctx.arglist; /* see if this is the end */ if (ups->pc_ctx.numargs >= 2) { if ((!strcmp(ups->pc_ctx.arglist[0], "END")) && (!strcmp(ups->pc_ctx.arglist[1], "LIST"))) return 0; } /* q: VAR <ups> */ /* a: VAR <ups> <val> */ if (!verify_resp(numq, query, ups->pc_ctx.arglist)) { ups->upserror = UPSCLI_ERR_PROTOCOL; return -1; } /* just another part of the list */ return 1; }