]> mj.ucw.cz Git - minsk.git/blobdiff - minsk.c
Fix compiler warnings
[minsk.git] / minsk.c
diff --git a/minsk.c b/minsk.c
index 6ffe02343b4827176d33af5ec50fd58cb8e4197d..51d91b206001853bf687c8ed9e13c11e2a197315 100644 (file)
--- a/minsk.c
+++ b/minsk.c
  */
 
 #define _GNU_SOURCE
+#define UNUSED __attribute__((unused))
+#define NORETURN __attribute__((noreturn))
+
+#undef ENABLE_DAEMON_MODE
 
 #include <stdio.h>
 #include <string.h>
@@ -23,6 +27,7 @@
 #include <inttypes.h>
 #include <assert.h>
 #include <math.h>
+#include <getopt.h>
 
 static int trace;
 static int cpu_quota = -1;
@@ -165,7 +170,7 @@ static void wr(int addr, word val)
 
 static int lino;
 
-static void parse_error(char *msg)
+NORETURN static void parse_error(char *msg)
 {
   if (error_hook)
     error_hook("Parse error");
@@ -185,18 +190,12 @@ static void parse_in(void)
       if (!eol)
        parse_error("Строка слишком долгая");
       *eol = 0;
+      if (eol > line && eol[-1] == '\r')
+       *--eol = 0;
 
       char *c = line;
       if (!c[0] || c[0] == ';')
-       {
-         if (!strncmp(c, ";daji_zor_i_litva=", 18))
-           {
-             trace = atoi(c+18);
-             if (error_hook)
-               error_hook("Secret tracing switch flipped");
-           }
-         continue;
-       }
+       continue;
 
       if (c[0] == '.')
        return;
@@ -250,7 +249,7 @@ static word r1, r2, current_ins;
 static int ip = 00050;                 // Standard program start location
 static int prev_ip;
 
-static void stop(char *reason, char *notice)
+NORETURN static void stop(char *reason, char *notice)
 {
   if (error_hook)
     error_hook(notice);
@@ -259,18 +258,18 @@ static void stop(char *reason, char *notice)
   exit(0);
 }
 
-static void over(void)
+NORETURN static void over(void)
 {
   stop("Аварийный останов", "Overflow");
 }
 
-static void notimp(void)
+NORETURN static void notimp(void)
 {
   acc = current_ins;
   stop("Устройство разбитое", "Not implemented");
 }
 
-static void noins(void)
+NORETURN static void noins(void)
 {
   acc = current_ins;
   stop("Эту команду не знаю", "Illegal instruction");
@@ -795,8 +794,21 @@ static void run(void)
     }
 }
 
+NORETURN static void die(char *msg)
+{
+  fprintf(stderr, "minsk: %s\n", msg);
+  exit(1);
+}
+
 /*** Daemon interface ***/
 
+#ifdef ENABLE_DAEMON_MODE
+
+/*
+ * The daemon mode was a quick hack for the Po drate contest.
+ * Most parameters are hard-wired.
+ */
+
 #include <unistd.h>
 #include <errno.h>
 #include <time.h>
@@ -826,14 +838,6 @@ static void run(void)
 #define UID 124
 #define GID 125
 
-static void die(char *msg)
-{
-  fprintf(stderr, "minsk: ");
-  fprintf(stderr, msg);
-  fputc('\n', stderr);
-  exit(1);
-}
-
 static char **spt_argv;
 static char *spt_start, *spt_end;
 
@@ -894,11 +898,11 @@ setproctitle(const char *msg, ...)
   va_end(args);
 }
 
-static void sigchld_handler(int sig __attribute__((unused)))
+static void sigchld_handler(int sig UNUSED)
 {
 }
 
-static void sigalrm_handler(int sig __attribute__((unused)))
+static void sigalrm_handler(int sig UNUSED)
 {
   const char err[] = "--- Timed out. Time machine disconnected. ---\n";
   write(1, err, sizeof(err));
@@ -1228,6 +1232,19 @@ static void run_as_daemon(int do_fork)
     }
 }
 
+#else
+
+static void run_as_daemon(int do_fork UNUSED)
+{
+  die("Daemon mode not supported in this version, need to recompile.");
+}
+
+static void setproctitle_init(int argc UNUSED, char **argv UNUSED)
+{
+}
+
+#endif
+
 static void init_memory(void)
 {
   // For the contest, we fill the whole memory with -00 00 0000 0000 (HALT),
@@ -1243,22 +1260,63 @@ static void init_memory(void)
   mem[pos++] = 0534051524017;
 }
 
+static const struct option longopts[] = {
+  { "cpu-quota",       1, NULL, 'q' },
+  { "daemon",          0, NULL, 'd' },
+  { "nofork",          0, NULL, 'n' },
+  { "print-quota",     1, NULL, 'p' },
+  { "trace",           1, NULL, 't' },
+  { NULL,              0, NULL, 0   },
+};
+
+static void usage(void)
+{
+  fprintf(stderr, "\
+Options:\n\n\
+--daemon               Run as daemon and listen for network connections\n\
+--nofork               When run with --daemon, avoid forking\n\
+--trace=<level>                Enable tracing of program execution\n\
+--cpu-quota=<n>                Set CPU quota to <n> instructions\n\
+--print-quota=<n>      Set printer quota to <n> lines\n\
+");
+  exit(1);
+}
+
 int main(int argc, char **argv)
 {
+  int opt;
+  int daemon_mode = 0;
+  int do_fork = 1;
+
+  while ((opt = getopt_long(argc, argv, "", longopts, NULL)) >= 0)
+    switch (opt)
+      {
+      case 'd':
+       daemon_mode = 1;
+       break;
+      case 'n':
+       do_fork = 0;
+       break;
+      case 'p':
+       print_quota = atoi(optarg);
+       break;
+      case 'q':
+       cpu_quota = atoi(optarg);
+       break;
+      case 't':
+       trace = atoi(optarg);
+       break;
+      default:
+       usage();
+      }
+  if (optind < argc)
+    usage();
+
+  setproctitle_init(argc, argv);
   init_memory();
 
-  if (argc > 1)
-    {
-      setproctitle_init(argc, argv);
-      if (!strcmp(argv[1], "--daemon"))
-       run_as_daemon(1);
-      else if (!strcmp(argv[1], "--net"))
-       run_as_daemon(0);
-      else if (!strncmp(argv[1], "--trace=", 8))
-       trace = atoi(argv[1] + 8);
-      else
-       die("Usage: minsk [--daemon | --net]");
-    }
+  if (daemon_mode)
+    run_as_daemon(do_fork);
 
   parse_in();
   run();