MS2 Fatal Error: Couldn't load 'ad'


M-Script II


A CGI programming language by
Mark Morley of Islandnet
Updated: December 02, 2003
Total Visits:
Last Access:
Copyright 1997, All rights reserved
"M-Script II" is a trademark of
Islandnet AMT Solutions Group Inc.

This document contains various bits and pieces of M-Script II code, along with explanations of how they work. Unless otherwise noted, all the examples are written by me (Mark Morley). If you have some sample code that you would like me to place on this page, either submit it to the mailing list or email me directly. Try to stick to the format I use, thanks.


 Generating GIF Images

This example creates a new GIF image in memory, draws some randomly sized rectangles in random colors on it, then outputs the image to the display. It sets the color BLACK to transparent, and interlaces the image too:

if gif = newgif( 64, 64 )
   i = 10
   while i > 0
      x = random( 48 )
      y = random( 48 )
      box( gif, x, y, x + random( 32 ), y + random( 32 ), random( 1, 30 ) )
      i = i - 1
   endwhile
   transparent( gif, BLACK )
   interlace( gif, 1 )
   writegif( gif )
   closegif( gif )
endif
Every time you load the URL http://www.islandnet.com/cgi-bin/ms2/mark/gifdemo the image will look different. Note that at no time does this "GIF" ever exist on disk.

This example also generates a GIF on the fly:

image = newgif( 101, 101 )
box( image, 0, 0, 100, 100, WHITE )
circle( image, 50, 50, 50, BLACK )
floodfill( image, 50, 50, YELLOW3 )
arc( image, 50, 60, 30, 20, 0, 180, BLACK )
circle( image, 35, 30, 7, BLACK )
floodfill( image, 35, 30, BLACK )
circle( image, 65, 30, 7, BLACK )
floodfill( image, 65, 30, BLACK )
interlace( image, TRUE )
transparent( image, WHITE )
writegif( image )
closegif( image )
And here's what it generates: Happy Face


 Sending E-Mail

This example sends an email message to me.

if m = mail( "mark@islandnet.com" )
   write( m, "From: joeschmo@somewhere.com\n" )
   write( m, "To: mark@islandnet.com\n" )
   write( m, "Subject: This is an M-Script II email!\n" )
   write( m, "\n" )
   write( m, "This is the body of the message.  After\n" )
   write( m, "the body I will append my signature file.\n" )
   include( ".Sig", m )
   close( m )
endif
Note that the address passed to the mail() function controls where the message will go, regardless of the content of the To: header. It is not strictly necessary to wite headers, but things may not quite look right to the recipient. Also note the newline characters ("\n") that are written at the end of each line - these are important. You must insert at least one blank line after any headers you create and before the first line of the body.


 Using Cookies

This simple example uses a cookie to "remember" if the current visitor has been to this page recently. It checks to see if a cookie named "TEST" is set. If it is, then the user has been here before. If it is not set, then we set it for the next time they come to visit.

if getcookie( "TEST" )
   print( "Welcome back!" )
else
   setcookie( "TEST", "X" )
   print( "This is your first visit." )
endif
In this example we don't care what the actual value of the cookie is, so we just set it to "X". Note how the setcookie() function is called before the print() function. This is very important. Once non-header data has been printed you cannot set cookies. Also note that we didn't set an expiry date in the setcookie() call. This means that the cookie will be deleted from the user's browser when they next shut it down, which makes this particular example not as useful.


 Adding to the Top of a File

In some cases (a guestbook perhaps) it is desirable to insert data at the top of a file instead of simply appending it to the bottom. To do this you need to open a temporary file, write your data to it, copy the data from the original file into the new one, then rename the new file over top of the old one. And just to keep our guestbook from growing indefinitely, we're going to allow no more than 25 lines to exist at any time:

lock( "guestbook.txt" )
if output = open( "guestbook.tmp", "w" )
   write( output, "This is the new line of text.\n" )
   if input = open( "guestbook.txt" )
      count = 1
      line = read( input )
      while not eof( input ) and count < 25
         write( output, line )
         count = count + 1
         line = read( input )
      endwhile
      close( input )
   endif
   close( output )
   move( "guestbook.tmp", "guestbook.txt" )
endif
unlock( "guestbook.txt" )
Note that the way this works, it doesn't matter if "guestbook.txt" already exists or not - the first time it is used the file will be created by the move() call anyway.


 Outputting Alternate Mime Types

Say you have a gif image of your company logo on your page. At Christmas time you like to replace it with a version that has some snow and Christmas lights on it for a week or two. You could go and change it every year, or you could write a CGI to do it for you. First of all, where you would normally have this line in your HTML:

<img src="logo.gif">
Put this instead:

<img src="/cgi-bin/ms2/LOGIN/logo">
(where LOGIN is your account name of course). Then place the following MS2 program (logo.ms2) in your account:

date = time( "%m%d" )
mimetype( "image/gif" )
if date >= "1201" and date <= "1225"
   include( "www/xmaslogo.gif" )
else
   include( "www/logo.gif" )
endif
That's it. The program is invoked automatically whenever the page is loaded. It grabs the current date using the format "%m%d", which will give a 2 digit month and a 2 digit day of the month. If the current date is greater than or equal to December 1st, and it's less than or equal to December 25th, it outputs "xmaslogo.gif", otherwise it sends the normal logo.


 Simple Hit Counter

This example automatically increments a hit counter every time the page that references it is called. It assumes that only one web page is referring to so it uses a hardwired file name to store the counter value in. The counter value is printed so that it will appear embedded within the web page:

lock( "counter.txt" )
if input = open( "counter.txt" )
   count = num( read( input ) ) + 1
   close( input )
else
   count = 1
endif
if output = open( "counter.txt", "w" )
   write( output, count, "\n" )
   close( output )
endif
unlock( "counter.txt" )
print( count, "\n" )
At the point in the web page where you want your counter to appear, insert this tag:

<!--#exec cgi="/cgi-bin/ms2/LOGIN/PROGRAM" -->
NOTE: Islandnet customers should use one of the supplied web gadgets whenever possible as they are written in pure C and run much faster, consuming less resources. This is especially important with things like counters that may be called hundreds of thousands of times per day on our servers.

 Checking a File's Modification Date

Author: Chris Webster (cwebster@compulinks.com)

This one-liner simply displays the date that a specified file was last modified:

print(time("%B %e, %Y", filetime(getenv("QUERY_STRING"))))
The time format used produces dates like "April 24, 1997" - but you can change that to anything you want. To embed the last modified date for any file into any web page, insert a line like this in your HTML:

<!--#exec cgi="/cgi-bin/ms2/LOGIN/PROGRAM?PATHNAME" -->