16216762177fdb16012e306824e524745b8eb879
[zilutils.git] / zilasm / main.c
1 /*
2  * main.c
3  *
4  * Copyright (C) 2015 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
5  * Copyright (C) 2015 Jason Self <j@jxself.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <getopt.h>
25 #include <time.h>
26 #include <ctype.h>
27
28 #include "config.h"
29
30 enum { ZVERSION = 11, ZORKID, ZSERIAL };
31
32 static struct option const long_options[] =
33 {
34     { "help",     no_argument,       NULL, 'h' },
35     { "version",  no_argument,       NULL, 'V' },
36     { "zversion", required_argument, NULL, ZVERSION },
37     { "zorkid",   required_argument, NULL, ZORKID   },
38     { "serial",   required_argument, NULL, ZSERIAL  },
39     { NULL, 0, NULL, 0 }
40 };
41
42 struct
43 {
44     int  zversion;     /* 0 - 8   */
45     int  zorkid;       /* 0 - 255 */
46     char zserial[7];   /* YYMMDD  */
47 } Config;
48
49 void wrong_arg()
50 {
51     fprintf(stderr, "Try `" PACKAGE_NAME " --help' for more information.\n");
52     exit(1);
53 }
54
55 void print_version()
56 {
57     printf(PACKAGE_STRING "\n"
58            "License AGPLv3+: GNU AGPL version 3 or later\n"
59            "<http://gnu.org/licenses/agpl.html>\n"
60            "This is free software: you are free to change and redistribute it.\n"
61            "There is NO WARRANTY, to the extent permitted by law.\n"
62           );
63
64     exit(0);
65 }
66
67 void print_usage(int failed)
68 {
69     printf("Usage: " PACKAGE_NAME " [OPTION...] [FILES...]\n"
70            "\n"
71            "--version  Display program version and exit\n"
72            "--help     Display this help\n"
73            "\n"
74            "--zversion (accepts numbers 1 - 8, defaults to 1 if not specified)\n"
75            "--zorkid   (accepts digits, defaults to 0 if not specified)\n"
76            "--serial   (six characters of ASCII, defaults to current date\n"
77            "            in the form YYMMDD if not specified)\n"
78           );
79
80     exit(failed);
81 }
82
83 void fill_zserial(void)
84 {
85     time_t t;
86     struct tm *timeinfo;
87     time(&t);
88     timeinfo = localtime(&t);
89     strftime(Config.zserial, sizeof(Config.zserial), "%y%m%d", timeinfo);
90 }
91
92 void fill_config(void)
93 {
94     Config.zversion = 1;
95     Config.zorkid   = 0;
96     fill_zserial();
97 }
98
99 void parse_intarg(int *dest, const char name[], int min, int max, int defval)
100 {
101     if (!optarg)
102     {
103         *dest = defval;
104         return;
105     }
106
107     int n = atoi(optarg);
108     if (n >= min && n <= max)
109     {
110         *dest = n;
111         return;
112     }
113
114     fprintf(stderr, "Wrong %s value %s, must be integer between %d and %d\n",
115             name, optarg, min, max);
116     wrong_arg();
117 }
118
119 void parse_zserial(void)
120 {
121     if (!optarg)
122     {
123         fill_zserial();
124         return;
125     }
126
127     size_t n = strlen(optarg);
128     if (n == sizeof(Config.zserial) - 1)
129     {
130         char *p = optarg;
131         while (*p && isalnum(*p))
132             p++;
133
134         if (!*p) /* ..optarg contains alphanumeric only? */
135         {
136             strncpy(Config.zserial, optarg, sizeof(Config.zserial));
137             return;
138         }
139     }
140
141     fprintf(stderr, "Wrong zserial value %s, must be 6 ASCII characters\n", optarg);
142     wrong_arg();
143 }
144
145 int main(int argc, char *argv[], char *envp[])
146 {
147     fill_config();
148
149     int opt = 0;
150     while ((opt = getopt_long(argc, argv, "hV", long_options, NULL)) != -1)
151     {
152         switch (opt)
153         {
154         case 'h'     :
155             print_usage(0);
156         case 'V'     :
157             print_version();
158         case ZVERSION:
159             parse_intarg(&Config.zversion, "zversion", 1, 8,   1);
160             break;
161         case ZORKID  :
162             parse_intarg(&Config.zorkid,   "zorkid",   0, 255, 0);
163             break;
164         case ZSERIAL :
165             parse_zserial();
166             break;
167         default      :
168             wrong_arg();
169         }
170     }
171
172     // TODO: Everything :)
173
174     printf("Config:\n"
175            "- ZVersion: %d\n"
176            "- ZorkID:   %d\n"
177            "- ZSerial:  %s\n",
178            Config.zversion, Config.zorkid, Config.zserial
179           );
180
181     return 0;
182 }