const char * mfn_commas(MFUNARGS) { int v, i, count; char *ptr; char buf2[BUFFER_LEN]; char tmp[BUFFER_LEN]; if (argc == 3) ABORT_MPI("COMMAS","Takes 1, 2, or 4 arguments"); ptr = MesgParse(argv[0], argv[0]); CHECKRETURN(ptr,"COMMAS","arg 1"); count = countlitems(argv[0], "\r"); if (count == 0) return ""; if (argc > 1) { ptr = MesgParse(argv[1], argv[1]); CHECKRETURN(ptr,"COMMAS","arg 2"); } else { strcpy(argv[1], " and "); } if (argc > 2) { ptr = MesgParse(argv[2], buf2); CHECKRETURN(ptr,"COMMAS","arg 3"); v = new_mvar(ptr, tmp); if (v == 1) ABORT_MPI("COMMAS","Variable name too long"); if (v == 2) ABORT_MPI("COMMAS","Too many variables already defined"); } *buf = '\0'; for (i = 1; i <= count; i++) { ptr = getlitem(buf2, argv[0], "\r", i); if (argc > 2) { strcpy(tmp, ptr); ptr = MesgParse(argv[3], buf2); CHECKRETURN(ptr,"COMMAS","arg 3"); } strcat(buf, ptr); switch (count - i) { case 0: if (argc > 2) free_top_mvar(); return buf; break; case 1: strcat(buf, argv[1]); break; default: strcat(buf, ", "); break; } } if (argc > 2) free_top_mvar(); return buf; }
const char * mfn_with(MFUNARGS) { char vbuf[BUFFER_LEN]; char *ptr, *valptr; int v, cnt; ptr = MesgParse(argv[0], argv[0]); CHECKRETURN(ptr,"WITH","arg 1"); v = new_mvar(ptr, vbuf); if (v == 1) ABORT_MPI("WITH","Variable name too long"); if (v == 2) ABORT_MPI("WITH","Too many variables already defined"); valptr = MesgParse(argv[1], argv[1]); CHECKRETURN(valptr,"WITH","arg 2"); *buf = '\0'; strcpy(vbuf, valptr); for (cnt = 2; cnt < argc; cnt++) { ptr = MesgParse(argv[cnt],argv[cnt]); if (!ptr) { sprintf(buf, "%s %cWITH%c (%d)", get_mvar("how"), MFUN_LEADCHAR, MFUN_ARGEND, cnt); notify(player, buf); return NULL; } } free_top_mvar(); return ptr; }
const char * mfn_filter(MFUNARGS) { int iter_limit = MAX_MFUN_LIST_LEN; char buf2[BUFFER_LEN]; char tmp[BUFFER_LEN]; char *ptr, *ptr2, *dptr; char *sepin = argv[3]; char *sepbuf = argv[4]; int seplen, v; ptr = MesgParse(argv[0],argv[0]); CHECKRETURN(ptr,"FILTER","arg 1"); v = new_mvar(ptr, tmp); if (v == 1) ABORT_MPI("FILTER","Variable name too long."); if (v == 2) ABORT_MPI("FILTER","Too many variables already defined."); dptr = MesgParse(argv[1],argv[1]); CHECKRETURN(dptr,"FILTER","arg 2"); if (argc > 3) { ptr = MesgParse(sepin,sepin); CHECKRETURN(ptr,"FILTER","arg 4"); if (!*ptr) ABORT_MPI("FILTER","Can't use Null seperator string"); } else { strcpy(sepin, "\r"); } if (argc > 4) { ptr = MesgParse(sepbuf,sepbuf); CHECKRETURN(ptr,"FILTER","arg 5"); } else { strcpy(sepbuf, sepin); } seplen = strlen(sepin); *buf = '\0'; ptr = dptr; while (*ptr) { for (ptr2 = ptr; *ptr2 && strncmp(ptr2, sepin, seplen); ptr2++); if (*ptr2) { *ptr2 = '\0'; ptr2 += seplen; } strcpy(tmp, ptr); dptr = MesgParse(argv[2],buf2); CHECKRETURN(dptr,"FILTER","arg 3"); if (truestr(buf2)) { if (*buf) strcat(buf, sepbuf); strcat(buf, ptr); } ptr = ptr2; if (!(--iter_limit)) ABORT_MPI("FILTER","Iteration limit exceeded"); } free_top_mvar(); return buf; }
const char * mfn_for(MFUNARGS) { int iter_limit = MAX_MFUN_LIST_LEN; char tmp[BUFFER_LEN]; char *ptr, *dptr; int v, i, start, end, incr; ptr = MesgParse(argv[0],argv[0]); CHECKRETURN(ptr,"FOR","arg 1 (varname)"); v = new_mvar(ptr, tmp); if (v == 1) ABORT_MPI("FOR","Variable name too long"); if (v == 2) ABORT_MPI("FOR","Too many variables already defined"); dptr = MesgParse(argv[1],argv[1]); CHECKRETURN(dptr,"FOR","arg 2 (start num)"); start = atoi(dptr); dptr = MesgParse(argv[2],argv[2]); CHECKRETURN(dptr,"FOR","arg 3 (end num)"); end = atoi(dptr); dptr = MesgParse(argv[3],argv[3]); CHECKRETURN(dptr,"FOR","arg 4 (increment)"); incr = atoi(dptr); *buf = '\0'; for (i = start; ((incr>=0 && i<=end) || (incr<0 && i>=end)); i += incr) { sprintf(tmp, "%d", i); dptr = MesgParse(argv[4],buf); CHECKRETURN(dptr,"FOR","arg 5 (repeated command)"); if (!(--iter_limit)) ABORT_MPI("FOR","Iteration limit exceeded"); } free_top_mvar(); return buf; }
const char * mfn_lsort(MFUNARGS) { char *litem[MAX_MFUN_LIST_LEN]; char vbuf[BUFFER_LEN]; char vbuf2[BUFFER_LEN]; char *ptr, *ptr2, *tmp; int i, j, count; if (argc > 1 && argc < 4) ABORT_MPI("LSORT","Takes 1 or 4 arguments"); for (i = 0; i < MAX_MFUN_LIST_LEN; i++) litem[i] = NULL; ptr = MesgParse(argv[0],argv[0]); CHECKRETURN(ptr,"LSORT","arg 1"); if (argc > 1) { ptr2 = MesgParse(argv[1], argv[1]); CHECKRETURN(ptr2,"LSORT","arg 2"); j = new_mvar(ptr2, vbuf); if (j == 1) ABORT_MPI("LSORT","Variable name too long"); if (j == 2) ABORT_MPI("LSORT","Too many variables already defined"); ptr2 = MesgParse(argv[2],argv[2]); CHECKRETURN(ptr2,"LSORT","arg 3"); j = new_mvar(ptr2, vbuf2); if (j == 1) ABORT_MPI("LSORT","Variable name too long"); if (j == 2) ABORT_MPI("LSORT","Too many variables already defined"); } count = 0; while (*ptr) { for (ptr2 = ptr; *ptr2 && *ptr2 != '\r'; ptr2++); if (*ptr2 == '\r') *(ptr2++) = '\0'; litem[count++] = ptr; ptr = ptr2; if (count >= MAX_MFUN_LIST_LEN) ABORT_MPI("LSORT","Iteration limit exceeded"); } for (i = 0; i < count; i++) { for (j = i + 1; j < count; j++) { if (argc > 1) { strcpy(vbuf, litem[i]); strcpy(vbuf2, litem[j]); ptr = MesgParse(argv[3],buf); CHECKRETURN(ptr,"LSORT","arg 4"); if (truestr(buf)) { tmp = litem[i]; litem[i] = litem[j]; litem[j] = tmp; } } else { if (alphanum_compare(litem[i], litem[j]) > 0) { tmp = litem[i]; litem[i] = litem[j]; litem[j] = tmp; } } } } *buf = '\0'; for (i = 0; i < count; i++) { if (*buf) strcat(buf, "\r"); strcat(buf, litem[i]); } if (argc > 1) { free_top_mvar(); free_top_mvar(); } return buf; }