+ regmatch_t matches[3];
+ // Door (hard coded)
+ if( strcmp(gsItemPattern, "door") == 0 )
+ {
+ // Connect, Authenticate, dispense and close
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return -1;
+ Authenticate(sock);
+ DispenseItem(sock, "door", 0);
+ close(sock);
+ return 0;
+ }
+ // Item id (<type>:<num>)
+ else if( RunRegex(&gUserItemIdentRegex, gsItemPattern, 3, matches, NULL) == 0 )
+ {
+ char *ident;
+ int id;
+
+ // Get and finish ident
+ ident = gsItemPattern + matches[1].rm_so;
+ gsItemPattern[matches[1].rm_eo] = '\0';
+ // Get ID
+ id = atoi( gsItemPattern + matches[2].rm_so );
+
+ // Connect, Authenticate, dispense and close
+ sock = OpenConnection(gsDispenseServer, giDispensePort);
+ if( sock < 0 ) return -1;
+
+ Dispense_ItemInfo(sock, ident, id);
+
+ Authenticate(sock);
+ DispenseItem(sock, ident, id);
+ close(sock);
+ return 0;
+ }
+ // Item number (6 = coke)
+ else if( strcmp(gsItemPattern, "0") == 0 || atoi(gsItemPattern) > 0 )
+ {
+ i = atoi(gsItemPattern);
+ }
+ // Item prefix
+ else
+ {
+ int j;
+ int best = -1;
+ for( i = 0; i < giNumItems; i ++ )
+ {
+ // Prefix match (with case-insensitive match)
+ for( j = 0; gsItemPattern[j]; j ++ )
+ {
+ if( gaItems[i].Desc[j] == gsItemPattern[j] )
+ continue;
+ if( tolower(gaItems[i].Desc[j]) == tolower(gsItemPattern[j]) )
+ continue;
+ break;
+ }
+ // Check if the prefix matched
+ if( gsItemPattern[j] != '\0' )
+ continue;
+
+ // Prefect match
+ if( gaItems[i].Desc[j] == '\0' ) {
+ best = i;
+ break;
+ }
+
+ // Only one match allowed
+ if( best == -1 ) {
+ best = i;
+ }
+ else {
+ // TODO: Allow ambiguous matches?
+ // or just print a wanrning
+ printf("Warning - Ambiguous pattern, stopping\n");
+ return 1;
+ }
+ }
+
+ // Was a match found?
+ if( best == -1 )
+ {
+ fprintf(stderr, "No item matches the passed string\n");
+ return 1;
+ }
+
+ i = best;
+ }