chiark / gitweb /
protogen: more consistent arg passing + fix login commands.
[disorder] / scripts / protocol
index 118fda33141cce448ea492ebd9c4670ec01e166d..0704942e04a54b84e37b5851b0a23c24508ae2d4 100755 (executable)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl -w
 #
 # This file is part of DisOrder.
-# Copyright (C) 2010 Richard Kettlewell
+# Copyright (C) 2010-11 Richard Kettlewell
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 #
 use strict;
 
+# This file contains the definition of the disorder protocol, plus
+# code to generates stubs for it in the various supported languages.
+#
+# At the time of writing it is a work in progress!
+
+#
+# Types:
+#
+#    string         A (Unicode) string.
+#    integer        An integer.  Decimal on the wire.
+#    boolean        True or false.  "yes" or "no" on the wire.
+#    list           In commands: a list of strings in the command.
+#                   In returns: a list of lines in the response.
+#    pair-list      In returns: a list of key-value pairs in a response body.
+#    body           In commands: a list of strings as a command body.
+#                   In returns: a list of strings as a response body.
+#    queue          In returns: a list of queue entries in a response body.
+#    queue-one      In returns: a queue entry in the response.
+#
+
 # Variables and utilities -----------------------------------------------------
 
 our @h = ();
 our @c = ();
 
+# Write(PATH, LINES)
+#
+# Write array ref LINES to file PATH.
 sub Write {
     my $path = shift;
     my $lines = shift;
@@ -35,6 +58,10 @@ sub Write {
 
 # Command classes -------------------------------------------------------------
 
+# c_in_decl([TYPE, NAME])
+#
+# Return the C declaration for an input parameter of type TYPE with
+# name NAME.
 sub c_in_decl {
     my $arg = shift;
 
@@ -48,10 +75,14 @@ sub c_in_decl {
        return ("char **$name",
                "int n$name");
     } else {
-       die "$0: unknown type '$type'\n";
+       die "$0: c_in_decl: unknown type '$type'\n";
     }
 }
 
+# c_out_decl([TYPE, NAME])
+#
+# Return the C declaration for an output (reference) parameter of type
+# TYPE with name NAME.
 sub c_out_decl {
     my $arg = shift;
 
@@ -64,16 +95,23 @@ sub c_out_decl {
        return ("long *${name}p");
     } elsif($type eq 'boolean') {
        return ("int *${name}p");
-    } elsif($type eq 'list') {
+    } elsif($type eq 'list' or $type eq 'body') {
        return ("char ***${name}p",
                "int *n${name}p");
-    } elsif($type eq 'queue') {
+    } elsif($type eq 'pair-list') {
+       return ("struct kvp **${name}p");
+    } elsif($type eq 'queue' or $type eq 'queue-one') {
        return ("struct queue_entry **${name}p");
+    } elsif($type eq 'user') {
+       return ();
     } else {
-       die "$0: unknown type '$type'\n";
+       die "$0: c_out_decl: unknown type '$type'\n";
     }
 }
 
+# c_param_docs([TYPE, NAME})
+#
+# Return the doc string for a C input parameter.
 sub c_param_docs {
     my $args = shift;
     my @d = ();
@@ -89,6 +127,9 @@ sub c_param_docs {
     return @d;
 }
 
+# c_param_docs([TYPE, NAME})
+#
+# Return the doc string for a C output parameter.
 sub c_return_docs {
     my $return = shift;
     return () unless defined $return;
@@ -99,19 +140,27 @@ sub c_return_docs {
        or $type eq 'integer'
        or $type eq 'boolean') {
        return (" * \@param ${name}p $descr\n");
-    } elsif($type eq 'list') {
+    } elsif($type eq 'list' or $type eq 'body') {
        return (" * \@param ${name}p $descr\n",
                " * \@param n${name}p Number of elements in ${name}p\n");
-    } elsif($type eq 'queue') {
+    } elsif($type eq 'pair-list') {
+       return (" * \@param ${name}p $descr\n");
+    } elsif($type eq 'queue' or $type eq 'queue-one') {
        return (" * \@param ${name}p $descr\n");
+    } elsif($type eq 'user') {
+       return ();
     } else {
-       die "$0: unknown return type '$type'\n";
+       die "$0: c_return_docs: unknown type '$type'\n";
     }
 }
 
 # simple(CMD, SUMMARY, DETAIL,
 #        [[TYPE,NAME,DESCR], [TYPE,NAME,DESCR], ...],
 #        [RETURN-TYPE, RETURN-NAME, RETURN_DESCR])
+#
+# CMD is normally just the name of the command, but can
+# be [COMMAND,FUNCTION] if the function name should differ
+# from the protocol command.
 sub simple {
     my $cmd = shift;
     my $summary = shift;
@@ -119,9 +168,17 @@ sub simple {
     my $args = shift;
     my $return = shift;
 
-    my $cmdc = $cmd;
-    $cmdc =~ s/-/_/g;
+    my $cmdc;
+    if(ref $cmd eq 'ARRAY') {
+        $cmdc = $$cmd[1];
+        $cmd = $$cmd[0];
+    } else {
+        $cmdc = $cmd;
+        $cmdc =~ s/-/_/g;
+    }
+    print STDERR "Processing $cmd... ";
     # Synchronous C API
+    print STDERR "H ";
     push(@h, "/** \@brief $summary\n",
          " *\n",
          " * $detail\n",
@@ -136,60 +193,87 @@ sub simple {
                    map(c_in_decl($_), @$args),
                    c_out_decl($return)),
          ");\n\n");
+    print STDERR "C ";
     push(@c, "int disorder_$cmdc(",
         join(", ", "disorder_client *c",
                    map(c_in_decl($_), @$args),
                    c_out_decl($return)),
         ") {\n");
+    my @cargs = ();
+    for my $arg (@$args) {
+        if($arg->[0] eq 'body' or $arg->[0] eq 'list') {
+            push(@cargs, "disorder_$arg->[0]", $arg->[1], "n$arg->[1]");
+        } elsif($arg->[0] eq 'string') {
+            push(@cargs, $arg->[1]);
+        } elsif($arg->[0] eq 'integer') {
+            push(@cargs, "buf_$arg->[1]");
+            push(@c, "  char buf_$arg->[1]\[16];\n",
+                 "  byte_snprintf(buf_$arg->[1], sizeof buf_$arg->[1], \"%ld\", $arg->[1]);\n");
+        } else {
+            die "$0: unsupported arg type '$arg->[0]' for '$cmd'\n";
+        }
+    }
     if(!defined $return) {
-       my @cargs = ();
-       for my $arg (@$args) {
-           if($arg->[0] eq 'body' or $arg->[0] eq 'list') {
-               push(@cargs, "disorder_$arg->[0]", $arg->[1], "n$arg->[1]");
-           } elsif($arg->[0] eq 'string') {
-               push(@cargs, $arg->[1]);
-           } elsif($arg->[0] eq 'integer') {
-               push(@cargs, "buf_$arg->[1]");
-               push(@c, "  char buf_$arg->[1]\[16];\n",
-                    "  byte_snprintf(buf_$arg->[1], sizeof buf_$arg->[1], \"%ld\", $arg->[1]);\n");
-           } else {
-               die "$0: unsupported arg type '$arg->[0]' for '$cmd'\n";
-           }
-       }
        push(@c, "  return disorder_simple(",
             join(", ", "c", 0, "\"$cmd\"", @cargs, "(char *)0"),
             ");\n");
     } elsif($return->[0] eq 'string') {
-       push(@c, "  return dequote(disorder_simple(c, $return->[1]p, \"$cmd\"",
-            map(", $_->[1]", @$args),
-            ", (char *)0), $return->[1]p);\n");
+       push(@c, "  return dequote(disorder_simple(",
+             join(", ", "c", "$return->[1]p", "\"$cmd\"",
+                  @cargs,
+                  "(char *)0"),
+             "), $return->[1]p);\n");
     } elsif($return->[0] eq 'boolean') {
        push(@c, "  char *v;\n",
             "  int rc;\n",
-            "  if((rc = disorder_simple(c, &v, \"$cmd\"",
-            map(", $_->[1]", @$args),
-            ", (char *)0)))\n",
+            "  if((rc = disorder_simple(",
+             join(", ", "c", "&v", "\"$cmd\"",
+                  @cargs,
+                  "(char *)0"),
+             ")))\n",
             "    return rc;\n",
             "  return boolean(\"$cmd\", v, $return->[1]p);\n");
     } elsif($return->[0] eq 'integer') {
        push(@c, "  char *v;\n",
             "  int rc;\n",
             "\n",
-            "  if((rc = disorder_simple(c, &v, \"$cmd\"",
-            map(", $_->[1]", @$args),
-            ", (char *)0)))\n",
+            "  if((rc = disorder_simple(",
+             join(", ", "c", "&v", "\"$cmd\"",
+                  @cargs,
+                  "(char *)0"),
+             ")))\n",
             "    return rc;\n",
             "  *$return->[1]p = atol(v);\n",
             "  xfree(v);\n",
             "  return 0;\n");
-    } elsif($return->[0] eq 'list') {
-       push(@c, "  return disorder_simple_list(c, $return->[1]p, n$return->[1]p, \"$cmd\"",
-            map(", $_->[1]", @$args),
-            ", (char *)0);\n");
+    } elsif($return->[0] eq 'user') {
+       push(@c, "  char *u;\n",
+            "  int rc;\n",
+            "  if((rc = disorder_simple(",
+             join(", ", "c", "&u", "\"$cmd\"",
+                  @cargs, "(char *)0"),
+             ")))\n",
+            "    return rc;\n",
+            "  c->user = u;\n",
+            "  return 0;\n");
+    } elsif($return->[0] eq 'body') {
+       push(@c, "  return disorder_simple_list(",
+             join(", ", "c", "$return->[1]p", "n$return->[1]p", "\"$cmd\"",
+                  @cargs,
+                  "(char *)0"),
+            ");\n");
     } elsif($return->[0] eq 'queue') {
-       push(@c, "  return disorder_somequeue(c, \"$cmd\", $return->[1]p);\n");
+       push(@c, "  return somequeue(c, \"$cmd\", $return->[1]p);\n");
+    } elsif($return->[0] eq 'queue-one') {
+       push(@c, "  return onequeue(c, \"$cmd\", $return->[1]p);\n");
+    } elsif($return->[0] eq 'pair-list') {
+       push(@c, "  return pairlist(",
+             join(", ", "c", "$return->[1]p", "\"$cmd\"",
+                  @cargs,
+                  "(char *)0"),
+             ");\n");
     } else {
-       die "$0: unknown return type '$return->[0]' for '$cmd'\n";
+       die "$0: C API: unknown type '$return->[0]' for '$cmd'\n";
     }
     push(@c, "}\n\n");
 
@@ -201,64 +285,22 @@ sub simple {
 
     # Java API
     # TODO
-}
-
-# string_login(CMD, SUMMARY, DETAIL, [[TYPE,NAME,DESCR], [TYPE,NAME,DESCR], ...])
-#
-# Like string(), but the server returns a username, which we squirrel
-# away rather than returning to the caller.
-sub string_login {
-    my $cmd = shift;
-    my $summary = shift;
-    my $detail = shift;
-    my $args = shift;
-    my $return = shift;
-
-    my $cmdc = $cmd;
-    $cmdc =~ s/-/_/g;
-    # Synchronous C API
-    push(@h, "/** \@brief $summary\n",
-         " *\n",
-         " * $detail\n",
-         " *\n",
-        c_param_docs($args),
-         " * \@return 0 on success, non-0 on error\n",
-         " */\n",
-         "int disorder_$cmdc(",
-        join(", ", "disorder_client *c",
-                   map(c_in_decl($_), @$args)),
-         ");\n");
-    push(@c, "int disorder_$cmdc(",
-        join(", ", "disorder_client *c",
-                   map(c_in_decl($_), @$args)),
-        ") {\n",
-        "  char *u;\n",
-        "  int rc;\n",
-         "  if((rc = disorder_simple(c, &u, \"$cmd\"",
-         map(", $_->[1]", @$args),
-        "  )))\n",
-        "    return rc;\n",
-        "  c->user = u;\n",
-        "  return 0;\n",
-         "}\n\n");
-
-    # Asynchronous C API
-    # TODO
-
-    # Python API
-    # TODO
-
-    # Java API
-    # TODO
+    print STDERR "\n";
 }
 
 # TODO other command classes
 
 # Front matter ----------------------------------------------------------------
 
+our @generated = ("/*\n",
+                  " * Automatically generated file, see scripts/protocol\n",
+                  " *\n",
+                  " * DO NOT EDIT.\n",
+                  " */\n");
+
 our @gpl = ("/*\n",
             " * This file is part of DisOrder.\n",
-            " * Copyright (C) 2010 Richard Kettlewell\n",
+            " * Copyright (C) 2010-11 Richard Kettlewell\n",
             " *\n",
             " * This program is free software: you can redistribute it and/or modify\n",
             " * it under the terms of the GNU General Public License as published by\n",
@@ -275,12 +317,12 @@ our @gpl = ("/*\n",
             " */\n");
 
 
-push(@h, @gpl,
+push(@h, @generated, @gpl,
      "#ifndef CLIENT_STUBS_H\n",
      "#define CLIENT_STUBS_H\n",
      "\n");
 
-push(@c, @gpl,
+push(@c, @generated, @gpl,
      "\n");
 
 # The protocol ----------------------------------------------------------------
@@ -302,17 +344,19 @@ simple("allfiles",
        "See 'files' and 'dirs' for more specific lists.",
        [["string", "dir", "Directory to list (optional)"],
        ["string", "re", "Regexp that results must match (optional)"]],
-       ["list", "files", "List of matching files and directories"]);
+       ["body", "files", "List of matching files and directories"]);
 
-string_login("confirm",
-            "Confirm registration",
-            "The confirmation string must have been created with 'register'.  The username is returned so the caller knows who they are.",
-            [["string", "confirmation", "Confirmation string"]]);
+simple("confirm",
+       "Confirm registration",
+       "The confirmation string must have been created with 'register'.  The username is returned so the caller knows who they are.",
+       [["string", "confirmation", "Confirmation string"]],
+       ["user"]);
 
-string_login("cookie",
-            "Log in with a cookie",
-            "The cookie must have been created with 'make-cookie'.  The username is returned so the caller knows who they are.",
-            [["string", "cookie", "Cookie string"]]);
+simple("cookie",
+       "Log in with a cookie",
+       "The cookie must have been created with 'make-cookie'.  The username is returned so the caller knows who they are.",
+       [["string", "cookie", "Cookie string"]],
+       ["user"]);
 
 simple("deluser",
        "Delete user",
@@ -324,7 +368,7 @@ simple("dirs",
        "",
        [["string", "dir", "Directory to list (optional)"],
        ["string", "re", "Regexp that results must match (optional)"]],
-       ["list", "files", "List of matching directories"]);
+       ["body", "files", "List of matching directories"]);
 
 simple("disable",
        "Disable play",
@@ -360,7 +404,7 @@ simple("files",
        "",
        [["string", "dir", "Directory to list (optional)"],
        ["string", "re", "Regexp that results must match (optional)"]],
-       ["list", "files", "List of matching files"]);
+       ["body", "files", "List of matching files"]);
 
 simple("get",
        "Get a track preference",
@@ -401,7 +445,11 @@ simple("moveafter",
        [["string", "target", "Move after this track, or to head if \"\""],
        ["list", "ids", "List of tracks to move by ID"]]);
 
-# TODO new
+simple(["new", "new_tracks"],
+       "List recently added tracks",
+       "",
+       [["integer", "max", "Maximum tracks to fetch, or 0 for all available"]],
+       ["body", "tracks", "Recently added tracks"]);
 
 simple("nop",
        "Do nothing",
@@ -433,7 +481,11 @@ simple("playafter",
        [["string", "target", "Insert into queue after this track, or at head if \"\""],
        ["list", "tracks", "List of track names to play"]]);
 
-# TODO playing
+simple("playing",
+       "Retrieve the playing track",
+       "",
+       [],
+       ["queue-one", "playing", "Details of the playing track"]);
 
 simple("playlist-delete",
        "Delete a playlist",
@@ -444,7 +496,7 @@ simple("playlist-get",
        "List the contents of a playlist",
        "Requires the 'read' right and oermission to read the playlist.",
        [["string", "playlist", "Playlist name"]],
-       ["list", "tracks", "List of tracks in playlist"]);
+       ["body", "tracks", "List of tracks in playlist"]);
 
 simple("playlist-get-share",
        "Get a playlist's sharing status",
@@ -478,9 +530,13 @@ simple("playlists",
        "List playlists",
        "Requires the 'read' right.  Only playlists that you have permission to read are returned.",
        [],
-       ["list", "playlists", "Playlist names"]);
+       ["body", "playlists", "Playlist names"]);
 
-# TODO prefs
+simple("prefs",
+       "Get all the preferences for a track",
+       "",
+       [["string", "track", "Track name"]],
+       ["pair-list", "prefs", "Track preferences"]);
 
 simple("queue",
        "List the queue",
@@ -568,19 +624,23 @@ simple("schedule-del",
        "Users can always delete their own scheduled events; with the admin right you can delete any event.",
        [["string", "event", "ID of event to delete"]]);
 
-# TODO schedule-get
+simple("schedule-get",
+       "Get the details of scheduled event",
+       "",
+       [["string", "id", "Event ID"]],
+       ["pair-list", "actiondata", "Details of event"]);
 
 simple("schedule-list",
        "List scheduled events",
        "This just lists IDs.  Use 'schedule-get' to retrieve more detail",
        [],
-       ["list", "ids", "List of event IDs"]);
+       ["body", "ids", "List of event IDs"]);
 
 simple("search",
        "Search for tracks",
        "Terms are either keywords or tags formatted as 'tag:TAG-NAME'.",
        [["string", "terms", "List of search terms"]],
-       ["list", "tracks", "List of matching tracks"]);
+       ["body", "tracks", "List of matching tracks"]);
 
 simple("set",
        "Set a track preference",
@@ -604,13 +664,13 @@ simple("stats",
        "Get server statistics",
        "The details of what the server reports are not really defined.  The returned strings are intended to be printed out one to a line..",
        [],
-       ["list", "stats", "List of server information strings."]);
+       ["body", "stats", "List of server information strings."]);
 
 simple("tags",
        "Get a list of known tags",
        "Only tags which apply to at least one track are returned.",
        [],
-       ["list", "tags", "List of tags"]);
+       ["body", "tags", "List of tags"]);
 
 simple("unset",
        "Unset a track preference",
@@ -636,7 +696,7 @@ simple("users",
        "Get a list of users",
        "",
        [],
-       ["list", "users", "List of users"]);
+       ["body", "users", "List of users"]);
 
 simple("version",
        "Get the server version",