Reformat everything
[nci.git] / nci-create-assignment-group.c
blob820ac922b89f5b61a758682b061fd18fa4582837
1 /*
2 * Copyright (c) 2016-2018 S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
7 * in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include <curl/curl.h>
26 #include <yajl_parse.h>
28 #include "macros.h"
29 #include "util.h"
31 int
32 main(int argc, char **argv)
34 int ret = EINVAL;
35 char *url_base = 0;
36 char *auth_token = 0;
37 char *course_id = 0;
38 char *name = 0;
39 char *weight_arg = 0;
40 double weight_d = 0;
41 char *weight = 0;
42 char *drop_lowest_arg = 0;
43 long long drop_lowest_ll = 0;
44 char *drop_lowest = 0;
45 char *drop_highest_arg = 0;
46 long long drop_highest_ll = 0;
47 char *drop_highest = 0;
48 size_t len = 0;
49 char *built_uri = 0;
50 struct curl_httppost *post = 0;
51 char *new_id = 0;
52 int opt = 0;
54 setlocale(LC_ALL, "");
56 while ((opt = getopt(argc, argv, "c:n:w:d:D:")) != -1) {
57 switch (opt) {
58 case 'c':
59 course_id = optarg;
60 break;
61 case 'n':
62 name = optarg;
63 break;
64 case 'w':
65 weight_arg = optarg;
66 break;
67 case 'd':
68 drop_lowest_arg = optarg;
69 break;
70 case 'D':
71 drop_highest_arg = optarg;
72 break;
73 default:
74 break;
78 if (!course_id) {
79 ret = EINVAL;
80 fprintf(stderr, "course-id is mandatory\n");
81 goto cleanup;
84 if (!name) {
85 ret = EINVAL;
86 fprintf(stderr, "name is mandatory\n");
87 goto cleanup;
90 if (weight_arg) {
91 weight_d = strtod(weight_arg, 0);
92 len = snprintf(0, 0, "%lf", weight_d);
94 if (len + 1 < len) {
95 ret = errno = EOVERFLOW;
96 perror(L(""));
97 goto cleanup;
100 if (!(weight = malloc(len + 1))) {
101 ret = errno;
102 perror(L("malloc"));
103 goto cleanup;
106 sprintf(weight, "%lf", weight_d);
109 if (drop_lowest_arg) {
110 drop_lowest_ll = strtoll(drop_lowest_arg, 0, 0);
111 len = snprintf(0, 0, "%lld", drop_lowest_ll);
113 if (len + 1 < len) {
114 ret = errno = EOVERFLOW;
115 perror(L(""));
116 goto cleanup;
119 if (!(drop_lowest = malloc(len + 1))) {
120 ret = errno;
121 perror(L("malloc"));
122 goto cleanup;
125 sprintf(drop_lowest, "%lld", drop_lowest_ll);
128 if (drop_highest_arg) {
129 drop_highest_ll = strtoll(drop_highest_arg, 0, 0);
130 len = snprintf(0, 0, "%lld", drop_highest_ll);
132 if (len + 1 < len) {
133 ret = errno = EOVERFLOW;
134 perror(L(""));
135 goto cleanup;
138 if (!(drop_highest = malloc(len + 1))) {
139 ret = errno;
140 perror(L("malloc"));
141 goto cleanup;
144 sprintf(drop_highest, "%lld", drop_highest_ll);
147 curl_global_init(CURL_GLOBAL_DEFAULT);
149 if (!(url_base = get_url_base())) {
150 ret = ENOENT;
152 /* Error should have already been printed */
153 goto cleanup;
156 if (!(auth_token = get_auth_token())) {
157 ret = ENOENT;
159 /* Error should have already been printed */
160 goto cleanup;
163 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignment_groups", url_base,
164 course_id);
166 if (len + 1 < len) {
167 ret = errno = EOVERFLOW;
168 perror(L(""));
169 goto cleanup;
172 if (!(built_uri = malloc(len + 1))) {
173 ret = errno;
174 perror(L("malloc"));
175 goto cleanup;
178 sprintf(built_uri, "%s/api/v1/courses/%s/assignment_groups", url_base,
179 course_id);
181 if (!(post = make_agroup_post(name, weight, drop_lowest,
182 drop_highest))) {
183 /* XXX: pass back proper error code from make_agroup_post() */
184 goto cleanup;
187 if ((ret = send_and_id_scan(built_uri, auth_token, post, "POST",
188 &new_id))) {
189 goto cleanup;
192 if (new_id) {
193 printf("%s\n", new_id);
196 ret = 0;
197 cleanup:
198 curl_formfree(post);
199 free(new_id);
200 free(built_uri);
201 free(drop_highest);
202 free(drop_lowest);
203 free(weight);
204 free(url_base);
205 free(auth_token);
206 curl_global_cleanup();
208 return ret;