+static cmd_input_t *Cbuf_LinkGet(cmd_buf_t *cbuf, cmd_input_t *existing)
+{
+ cmd_input_t *ret = NULL;
+ if(existing && existing->pending)
+ ret = existing;
+ else if(!List_IsEmpty(&cbuf->free))
+ {
+ ret = List_Container(*cbuf->free.next, cmd_input_t, list);
+ ret->length = 0;
+ ret->pending = false;
+ }
+ return ret;
+}
+
+static cmd_input_t *Cmd_AllocInputNode(void)
+{
+ cmd_input_t *node = (cmd_input_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_input_t));
+ node->list.prev = node->list.next = &node->list;
+ node->size = node->length = node->pending = 0;
+ return node;
+}
+
+static size_t Cmd_ParseInput (cmd_input_t **output, char **input)
+{
+ size_t pos, cmdsize = 0, start = 0;
+ qbool command = false, lookahead = false;
+ qbool quotes = false, comment = false;
+ qbool escaped = false;
+
+ /*
+ * The Quake command-line is super basic. It can be entered in the console
+ * or in config files. A semicolon is used to terminate a command and chain
+ * them together. Otherwise, a newline delineates command input.
+ *
+ * In most engines, the Quake command-line is a simple linear text buffer that
+ * is parsed when it executes. In Darkplaces, we use a linked list of command
+ * input and parse the input on the spot.
+ *
+ * This was done because Darkplaces allows multiple command interpreters on the
+ * same thread. Previously, each interpreter maintained its own buffer and this
+ * caused problems related to execution order, and maintaining a single simple
+ * buffer for all interpreters makes it non-trivial to keep track of which
+ * command should execute on which interpreter.
+ */
+
+ // Run until command and lookahead are both true, or until we run out of input.
+ for (pos = 0; (*input)[pos]; pos++)
+ {
+ // Look for newlines and semicolons. Ignore semicolons in quotes.
+ switch((*input)[pos])
+ {
+ case '\r':
+ case '\n':
+ command = false;
+ comment = false;
+ break;
+ default:
+ if(!comment) // Not a newline so far. Still not a valid command yet.
+ {
+ if(!quotes && (*input)[pos] == ';') // Ignore semicolons in quotes.
+ command = false;
+ else if (ISCOMMENT((*input), pos)) // Comments
+ {
+ comment = true;
+ command = false;
+ }
+ else
+ {
+ command = true;
+ if(!lookahead)
+ {
+ if(!cmdsize)
+ start = pos;
+ cmdsize++;
+ }
+
+ switch((*input)[pos])
+ {
+ case '"':
+ if (!escaped)
+ quotes = !quotes;
+ else
+ escaped = false;
+ break;
+ case '\\':
+ if (!escaped && quotes)
+ escaped = true;
+ else if (escaped)
+ escaped = false;
+ break;
+ }
+ }
+ }
+ }
+ if(cmdsize && !command)
+ lookahead = true;
+
+ if(command && lookahead)
+ break;
+ }
+
+ if(cmdsize)
+ {
+ size_t offset = 0;
+
+ if(!*output)
+ *output = Cmd_AllocInputNode();
+
+ if((*output)->pending)
+ offset = (*output)->length;
+
+ (*output)->length += cmdsize;
+
+ if((*output)->size < (*output)->length)
+ {
+ (*output)->text = (char *)Mem_Realloc(cbuf_mempool, (*output)->text, (*output)->length + 1);
+ (*output)->size = (*output)->length;
+ }
+
+ strlcpy(&(*output)->text[offset], &(*input)[start], cmdsize + 1);
+ (*output)->pending = !lookahead;
+ }
+
+ // Set input to its new position. Can be NULL.
+ *input = &(*input)[pos];
+
+ return cmdsize;
+}
+
+// Cloudwalk: Not happy with this, but it works.
+static void Cbuf_LinkCreate(cmd_state_t *cmd, llist_t *head, cmd_input_t *existing, const char *text)
+{
+ char *in = (char *)&text[0];
+ cmd_buf_t *cbuf = cmd->cbuf;
+ size_t totalsize = 0, newsize = 0;
+ cmd_input_t *current = NULL;
+
+ // Slide the pointer down until we reach the end
+ while(*in)
+ {
+ current = Cbuf_LinkGet(cbuf, existing);
+ newsize = Cmd_ParseInput(¤t, &in);
+
+ // Valid command
+ if(newsize)
+ {
+ if(current != existing)
+ {
+ current->source = cmd;
+ List_Move_Tail(¤t->list, head);
+ }
+
+ totalsize += newsize;
+ }
+ else if (current == existing && !totalsize)
+ current->pending = false;
+ current = NULL;
+ }
+
+ cbuf->size += totalsize;
+}
+