From: Sean Chiang <sean_chiang@openmoko.com>
Subject: [PATCH] improvement for find and read phonebooks in shell

This patch improves the functions to find and read phonebooks in shell.

Besides prr and pf, I add two new commands pRr and pRf to retrieve the
phonebook.

Signed-off-by: Jim Huang <jserv@openmoko.org>

Index: gsm/src/util/shell.c
===================================================================
--- gsm.orig/src/util/shell.c	2007-09-17 23:57:51.000000000 +0800
+++ gsm/src/util/shell.c	2007-09-17 23:59:04.000000000 +0800
@@ -34,8 +34,6 @@
 #include <gsmd/usock.h>
 #include <gsmd/ts0705.h>
 
-#include <common/linux_list.h>
-
 #ifndef __GSMD__
 #define __GSMD__
 #include <gsmd/talloc.h>
@@ -43,9 +41,8 @@
 #endif
 
 #define STDIN_BUF_SIZE	1024
-
-static LLIST_HEAD(storage_list);
-static LLIST_HEAD(phonebook_list);
+static int nFIND = 0;
+static int nREADRG = 0;
 
 /* this is the handler for receiving passthrough responses */
 static int pt_msghandler(struct lgsm_handle *lh, struct gsmd_msg_hdr *gmh)
@@ -62,46 +59,23 @@
 	struct gsmd_phonebook_storage *gpst;
 	char *payload;
 	char *fcomma, *lcomma, *ptr = NULL;
+	int *num;
 	char buf[128];
+	int i;
 
 	switch (gmh->msg_subtype) {
-#if 0
 	case GSMD_PHONEBOOK_FIND:		
+		num = (int *) ((char *)gmh + sizeof(*gmh));
+		printf("Records:%d\n", *num);
+
+		nFIND = *num;
+		break;
 	case GSMD_PHONEBOOK_READRG:
-		payload = (char *)gmh + sizeof(*gmh);
+		num = (int *) ((char *)gmh + sizeof(*gmh));
+		printf("Records:%d\n", *num);
 
-		if (!strncmp(payload, "+CPBR", 5) ||
-				!strncmp(payload, "+CPBF", 5)) {
-			gp = (struct gsmd_phonebook *) malloc(sizeof(struct gsmd_phonebook));
-			ptr = strchr(payload, ' ');
-	                gp->index = atoi(ptr+1);
-
-	                fcomma = strchr(payload, '"');
-	                lcomma = strchr(fcomma+1, '"');
-	                strncpy(gp->numb, fcomma + 1, (lcomma-fcomma-1));
-	                gp->numb[(lcomma - fcomma) - 1] = '\0';
-
-	                gp->type = atoi(lcomma + 2);
-
-	                ptr = strrchr(payload, ',');
-	                fcomma = ptr + 1;
-	                lcomma = strchr(fcomma + 1, '"');
-	                strncpy(gp->text, fcomma + 1, (lcomma - fcomma - 1));
-	                gp->text[(lcomma - fcomma) - 1] = '\0';
-
-			llist_add_tail(&gp->list, &phonebook_list);
-
-#if 0
-			llist_for_each_entry(gp, &phonebook_list, list) {
-				printf("%d, %s, %d, %s\n", gp->index, gp->numb, gp->type, gp->text);
-			}
-#endif
-			printf("%d, %s, %d, %s\n", gp->index, gp->numb, gp->type, gp->text);
-		}
-		else
-			printf("%s\n", payload);
+		nREADRG = *num;
 		break;
-#endif
 	case GSMD_PHONEBOOK_READ:
 		gp = (struct gsmd_phonebook *) ((char *)gmh + sizeof(*gmh));
 		if (gp->index)
@@ -115,48 +89,18 @@
 		gps = (struct gsmd_phonebook_support *) ((char *)gmh + sizeof(*gmh));
 		printf("(1-%d), %d, %d\n", gps->index, gps->nlength, gps->tlength);
 		break;
-#if 0
-	case GSMD_PHONEBOOK_LIST_STORAGE:
-		payload = (char *)gmh + sizeof(*gmh);
 
-		if (!strncmp(payload, "+CPBS", 5)) {
-			char* delim = "(,";
-			struct gsmd_phonebook_storage *cur, *cur2;
-
-			/* Remove previous record */
-			if (!llist_empty(&storage_list)) {
-				llist_for_each_entry_safe(cur, cur2,
-						&storage_list, list) {
-					llist_del(&cur->list);
-					talloc_free(cur);
-				}
-			}
-
-			ptr = strpbrk(payload, delim);
-
-			while ( ptr ) {
-				gpst = (struct gsmd_phonebook_storage *) malloc(sizeof(struct gsmd_phonebook_storage));
-				strncpy(gpst->storage, ptr+2, 2);
-				gpst->storage[2] = '\0';
-
-				ptr = strpbrk(ptr+2, delim);
-
-				llist_add_tail(&gpst->list, &storage_list);
-			}
+	case GSMD_PHONEBOOK_LIST_STORAGE:
+		gpst = (struct gsmd_phonebook_storage *)((char *)gmh + sizeof(*gmh));
 
-			if (llist_empty(&storage_list))
-				return 0;
+		for (i = 0; i < gpst->num; i++) {
+			printf("%s, ", gpst->mem[i].type);
+		}
 
-			llist_for_each_entry(cur, &storage_list, list) {
-				printf("\n%s",cur->storage);
-			}
+		printf("\n");
 
-			printf("\n");
-		}
-		else
-			printf("%s\n", payload);
 		break;
-#endif
+
 	case GSMD_PHONEBOOK_WRITE:
 	case GSMD_PHONEBOOK_DELETE:
 	case GSMD_PHONEBOOK_SET_STORAGE:
@@ -164,6 +108,26 @@
 		payload = (char *)gmh + sizeof(*gmh);
 		printf("%s\n", payload);
 		break;
+	case GSMD_PHONEBOOK_RETRIEVE_READRG:
+		gp = (struct gsmd_phonebook *) ((char *)gmh + sizeof(*gmh));
+
+		for (i=0; i<nREADRG; i++) {
+			printf("%d,%s,%d,%s\n", gp->index, gp->numb, gp->type, gp->text);
+			gp++;
+		}
+
+		nREADRG = 0;
+		break;
+	case GSMD_PHONEBOOK_RETRIEVE_FIND:
+		gp = (struct gsmd_phonebook *) ((char *)gmh + sizeof(*gmh));
+
+		for (i = 0; i < nFIND; i++) {
+			printf("%d,%s,%d,%s\n", gp->index, gp->numb, gp->type, gp->text);
+			gp++;
+		}
+
+		nFIND = 0;
+		break;
 	default:
 		return -EINVAL;
 	}	
@@ -381,11 +345,13 @@
 		"\tpd\tPB Delete (pb=index)\n"
 		"\tpr\tPB Read (pr=index)\n"
 		"\tprr\tPB Read Range (prr=index1,index2)\n"
-		"\tpf\tPB Find (pff=indtext)\n"
+		"\tpf\tPB Find (pf=indtext)\n"
 		"\tpw\tPB Write (pw=index,number,text)\n"
 		"\tps\tPB Support\n"
 		"\tpm\tPB Memory\n"
 		"\tpp\tPB Set Memory (pp=storage)\n"
+		"\tpRr\tRetrieve Readrg Records\n"
+		"\tpRf\tRetrieve Find Records\n"
 		"\tsd\tSMS Delete (sd=index,delflg)\n"
 		"\tsl\tSMS List (sl=stat)\n"
 		"\tsr\tSMS Read (sr=index)\n"
@@ -509,48 +475,21 @@
 				printf("Delete Phonebook Entry\n");				
 				ptr = strchr(buf, '=');
 				lgsm_pb_del_entry(lgsmh, atoi(ptr+1));
-#if 0
 			} else if ( !strncmp(buf, "prr", 3)) {	
 				printf("Read Phonebook Entries\n");
 				struct lgsm_phonebook_readrg pb_readrg;
-				struct gsmd_phonebook *gp_cur, *gp_cur2;
-
-				/* Remove records */
-				if (!llist_empty(&phonebook_list)) {
-					llist_for_each_entry_safe(gp_cur,
-							gp_cur2,
-							&phonebook_list,
-							list) {
-						llist_del(&gp_cur->list);
-						talloc_free(gp_cur);
-					}
-				}
 
 				ptr = strchr(buf, '=');
 				pb_readrg.index1 = atoi(ptr+1);				
 				ptr = strchr(buf, ',');
 				pb_readrg.index2 = atoi(ptr+1);
 				lgsm_pb_read_entries(lgsmh, &pb_readrg);
-#endif
 			} else if ( !strncmp(buf, "pr", 2)) {
 				ptr = strchr(buf, '=');
 				lgsm_pb_read_entry(lgsmh, atoi(ptr+1));
-#if 0
 			} else if ( !strncmp(buf, "pf", 2)) {
 				printf("Find Phonebook Entry\n");
 				struct lgsm_phonebook_find pb_find;
-				struct gsmd_phonebook *gp_cur, *gp_cur2;
-
-				/* Remove records */
-				if (!llist_empty(&phonebook_list)) {
-					llist_for_each_entry_safe(gp_cur,
-							gp_cur2,
-							&phonebook_list,
-							list) {
-						llist_del(&gp_cur->list);
-						talloc_free(gp_cur);
-					}
-				}
 
 				ptr = strchr(buf, '=');
 				strncpy(pb_find.findtext,
@@ -559,7 +498,6 @@
 				pb_find.findtext[strlen(ptr+1)] = '\0';	
 			
 				lgsm_pb_find_entry(lgsmh, &pb_find);
-#endif
 			} else if ( !strncmp(buf, "pw", 2)) {
 				printf("Write Phonebook Entry\n");
 				struct lgsm_phonebook pb;
@@ -591,6 +529,16 @@
 			} else if ( !strncmp(buf, "ps", 2)) {	
 				printf("Get Phonebook Support\n");
 				lgsm_pb_get_support(lgsmh);
+			} else if( !strncmp(buf, "pRr", 3) ) {
+				printf("Retrieve Readrg Records\n");
+
+				if ( nREADRG )
+					lgsm_pb_retrieve_readrg(lgsmh, nREADRG);
+			} else if( !strncmp(buf, "pRf", 3) ) {
+				printf("Retrieve Find Records\n");
+
+				if ( nFIND )
+					lgsm_pb_retrieve_find(lgsmh, nFIND);
 			} else if ( !strncmp(buf, "sd", 2)) {		
 				printf("Delete SMS\n");			
 				struct lgsm_sms_delete sms_del;
