home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Printf.c
-
- Contains: A dcmd that supports the C concept of printf.
-
- Written by: JM3 = Jim Murphy
- DAL = Dave Lyons
- sad = Scott Douglass
-
- Copyright: ⌐ 1989, 1994-1996 by Apple Computer, Inc., All Rights Reserved.
-
- Change History (most recent first):
-
- <5> 25-Jan-96 JM3 Let's try that again. We supply Put.c as a library, so it
- doesn't need compiled.
- <4> 25-Jan-96 JM3 Fixed the warning in <3> again. The latest universal headers
- re-introduced this. Updated the sample build commands to be
- current.
- <3> 3/26/95 DAL Included <:Public:Strings.h> to avoid p2cstr warning (should fix
- header file names). Used new dcmdFillVersion and dcmdFillString
- macros.
- <2> 10-Dec-94 JM3 Updated for new format 3 dcmd requirements.
- <1> 2/16/89 sad written
-
- */
-
- /* This is mostly to show how dcmds can use the standard C library.
-
- The following MPW commands will build the dcmd and copy it to the "Debugger Prefs" file
- in the System folder. The dcmd's name in MacsBug will be the name of the file built by
- the Linker.
-
- C Printf.c
-
- Link -o Printf -sg Main=STDCLIB,STDIO,SANELIB dcmdGlue.a.o Put.c.o Printf.c.o ╢
- "{Libraries}Runtime.o" "{CLibraries}StdCLib.o" "{Libraries}Interface.o" ╖╖ dev:null
- BuildDcmd Printf 196 -format3
- Echo 'include "Printf";' | Rez -a -o "{SystemFolder}Debugger Prefs"
- */
-
- #include <Types.h>
-
- #include <stdio.h>
- #include <string.h>
- #include <TextUtils.h>
- #include <Memory.h>
-
- #include "dcmd.h"
- #include "put.h"
-
- pascal void CommandEntry(dcmdBlock* paramPtr)
- {
-
- static const char usageStr[] = "\p\"format\" arg...";
-
- switch (paramPtr->request)
- {
- case dcmdInit:
- break;
-
- case dcmdHelp:
- dcmdDrawLine("\pDisplays the arguments according to the format (no floating point).");
- break;
-
- case dcmdGetInfo:
- dcmdFillVersion(paramPtr, 0x03008000); // version 3.0
- dcmdFillString(paramPtr, usageStr, usageStr);
- break;
-
- case dcmdDoIt:
- {
- Str255 formatstring;
- unsigned char* formatp = formatstring;
- Str255 formattedstring;
-
- dcmdSwapWorlds(); // not really necessary because we don╒t access machine state
-
- (void)dcmdGetNextParameter(formatstring);
-
- p2cstr(formatstring);
-
- while (*formatp != '\0')
- {
- char aformatspec[256];
- size_t speclength;
- long arg;
- Boolean ok;
-
- // Find the next format spec (i.e. %d) and print out anything before it
-
- char* nextp = strchr(formatp, '%');
- if (nextp == nil) nextp = formatp + strlen(formatp);
-
- PutBytesTo(formatp, nextp - formatp, 0);
- formatp = nextp;
-
- // Find the length of the format spec
-
- speclength = strcspn(formatp + 1, "diouxXcspP%") + 2; // we don╒t do "feEgGn"
- memcpy(aformatspec, formatp, speclength);
- aformatspec[speclength] = '\0';
- (void)dcmdGetNextExpression(&arg, &ok);
- if (!ok) break;
-
- sprintf(formattedstring, aformatspec, arg);
- PutCStr(formattedstring);
- formatp = formatp + speclength;
- }
- PutLine();
-
- dcmdSwapWorlds();
- }
- break;
-
- // Version 3 and newer dcmds must quietly ignore requests we don't recognize.
-
- default:
- break;
- }
-
- } // CommandEntry
-
- /*
- the following are stubs to override the C library routines so that the dcmd isn╒t
- so big.
- */
-
- size_t fwrite (const void *, size_t, size_t, FILE *) { return 0; } // wont╒t actually be called by sprintf
- _flsbuf() {} // wont╒t actually be called by sprintf
-
- fcvt() {} // used only for floating point %f, etc.
- ecvt() {} // used only for floating point %e, etc.
-