How do I compile a script in C and get it on a web server?

Discussion in 'Mac Programming' started by FL Botanist, Feb 1, 2011.

  1. FL Botanist macrumors newbie

    Joined:
    Feb 1, 2011
    #1
    Hi everyone!

    I'm in the Environmental Horticulture at the University of Florida and I'm trying to build an online, multiple-entry key to help students identify the plant families. I have permission to modify and build on a similar script that was written at Colby College, but the computer people there don't have time (understandably) to help me with basic questions. So I'm hoping that someone here can help me out.

    Here is the Colby site: http://www.colby.edu/info.tech/BI211/PlantFamilyID.html
    Here is the Colby script: http://www.colby.edu/info.tech/BI211/plant_id.c

    I've already downloaded and installed the Xcode Developer Tools, so I should be good-to-go on compiling the CGI in Terminal. (Right?) I also set up a free account at http://free1.awardspace.com/ so I can run the CGI there.

    So what now?

    As I understand it, I copy the Colby script into TextEdit and save it as a plain text file. Do I also need to get those other things like "util.c" and "strings.h"? Do I compile them separately in Terminal? And what do I upload to the server?

    I feel like I'm getting so close to solving this! Thanks to anyone who can help walk me through this!
     
  2. notjustjay, Feb 1, 2011
    Last edited: Feb 1, 2011

    notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #2
    It looks like you will need to upload the C code onto your server, and compile it on that machine. The executable binary code will be what gets executed by the server (the CGI code).

    You will probably need to have ssh access to the web host. I'm not sure if yours will allow you to do so. If you can, then compiling it on their host will probably look and feel very similar to compiling it on your own machine in Terminal.

    Edit: yes, you will also need util.c and util.h. Upload all the files to your web host, and in an ssh shell, compile it thusly:

    gcc plant_id.c

    Your executable will be named "a.out" by default -- rename it to whatever you need. Note that I was able to compile it on my web host but without knowing how the program is used, I didn't get very far with it (it crashes with a segfault if you invoke it with nothing.)
     
  3. lee1210, Feb 1, 2011
    Last edited: Feb 1, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Hm. You are probably going to want to compile the code wherever it is to be run. You will need any libraries or additional source files the single file you have depends on. You will need a web server setup to run CGI programs, and you will probably need some supporting HTML files that utilize the program, and perhaps some templates used for generating results. Hopefully you don't need to host this elsewhere and your department already has some servers you can utilize. As for the actual setup of the web server, maybe you can buy someone that does IT for one of the more tech-oriented schools at your university a meal or five to help with this? It's probably for the best if you have no idea where to start.

    You're probably going to need more than the one source file from the folks at Colby where this originated. Unfortunately at this stage you may not even know what's missing. We may be able to give you some ideas, but it might be a lot to ask a collective of strangers to basically build out this environment based on one source file, then work through what's missing with the folks at Colby through you.

    I hope you can get this going, but it seems a bit out of your league (I respect your desire to help others with this, i don't mean that insultingly). We are always happy to help, but we can't really configure a server and provide consulting on a software project from afar. Hopefully you have some friends and colleagues locally that can spend a bit of time with you to get this going.

    -Lee

    Edit: it looks like notjustjay did some legwork to get this compiled. That's a start, but there are probably a number of other supporting files we don't know about. I looked at the free hosting and it looked like it offered php and cgi for perl, but no ssh (read: no compiling). It doesn't seem like that hosting is going to do it, but having attended and worked at a major university I am confident resources are available for you without you "slumming it" at a free provider.
     
  4. notjustjay, Feb 1, 2011
    Last edited: Feb 2, 2011

    notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #4
    I was able to get the script successfully compiled and running just like the original website that you linked above.

    You will need the three source files: plant_id.c, util.c, util.h. The only "trick" is that you will need to compile it on the web host (or on a machine using the exact same architecture as your webhost). Once you've got that done, though, you can install it as a CGI program however your webhost instructs you to do so, and then modify the HTML file to point to the CGI in its form post. (I had to mess with script permissions and .htaccess files to enable CGI on my hosting provider, Dreamhost).

    Note that, as I discovered above, if you submit the form with nothing checked off, you get a crash (Internal Server Error) -- the same thing happens with the original link you posted above. If you intend to modify the code to do your own thing, I recommend you add some error checking when you get a chance.

    As lee1210 states, normally it is not this easy to reverse-engineer a functioning web script, but you got lucky this time :)
     
  5. dasmb macrumors regular

    Joined:
    Jul 12, 2007
    #5
    Piece of advice? Nobody seriously uses CGI anymore, not if they want to get anything meaningful done with the application, anyway. Consider porting this to a more modern language and platform, you'll learn valuable skills, be able to access a wider pool of knowledge and overall ramp up much faster.

    Heck, these days something this basic would be done in straight up JavaScript, completely on the client.

    PS: if you compile it, it is not a script. It's just code.
     
  6. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #6
    True. There was nothing special about the algorithm (looks like just a big look up table) so this code could easily be done in many ways. Python for example, or PHP.
     
  7. FL Botanist thread starter macrumors newbie

    Joined:
    Feb 1, 2011
    #7
    Thanks very much for all the useful feedback! Thanks especially to notjustjay for the work he put into this! :)

    To answer your questions, lee1210, yes, I am way out of my league here. But it's fun, and it's a healthy diversion from my doctoral work in horticulture. And the reason I can't just grab a starving Computer Science student is that I'm not on the main UFL campus. I'm way out in the field at a research station on the Treasure Coast of Florida. Forums like this are my only lifeline for help.

    So... clearly CGI is not the right way to go. My options seem to be: javascript, php, python, and perl. (Any votes?) I didn't go with javascript originally because of the size of the database. It seemed like it might bog down an old browser on an old computer. But maybe that's not an issue?
     
  8. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #8
    The decision between Python, PHP, Perl, etc. will be partly based on your ability to write the code, but mostly on what your web host lets you do. Most web hosts let you do PHP. Python is, in my opinion, easier to learn, but PHP is rather C-like if that's what you're already comfortable with.
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    Well, I guess that kills my thought of leaning on folks around campus.

    The biggest problem with a naive conversion of this to JavaScript would be page load time. It seems like it could clock in at a megabyte or two. Other than that aspect that would probably be the most portable. If you did it with JavaScript you may be able to just have people download a page once, instead of loading from the server each time they want to use it, but that's not how people are used to using a browser.

    Next in terms of portability would probably be PHP. The free host you picked supports it. I know it is widely used, but I personally loathe it. It just doesn't quite feel right. Anyway, my feelings should not prevent you from exploring this, as it is probably your best option. The problem then shifts from you finding a place to host CGI to you learning enough PHP to port this. The good news is there is very little code. The bad news is that you don't know how to read it, so porting it to another language you don't know will definitely be a challenge. If you just kept the data in a source file rather than moving it to a database hopefully that piece wouldn't be too involved. As far as I can tell the main piece of data is a map of which species has which quality. Hopefully getting code to parse that, even starting from scratch, shouldn't be too bad.

    It's hard to pick a place to start, but PHP surely has a number of tutorials dedicated to it. That's probably as good a place as any to begin.

    Good luck. I hope you can get this going and it proves useful. What enhancements do you wish to make?

    -Lee
     
  10. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #10
    This code is in K&R syntax, which is ancient... I'm surprised you got this to compile so easily.
     
  11. FL Botanist thread starter macrumors newbie

    Joined:
    Feb 1, 2011
    #11
    Exactly. Each row of the database is for a plant characteristic and each column is for a plant family. If user is trying to identify an unknown plant, he/she enters in some information about the plant features and then the program returns a list of possible families.

    About the enhancements: I'm making this into a site where students can practice using botanical terminology to identify plants. I'm going to use some illustrations from Thome's Flora von Deutschland (copyright-free) and have students identify the plants to the correct family. I'm also going to narrow down the list of families to the more common ones in Europe and North America.
     
  12. FL Botanist thread starter macrumors newbie

    Joined:
    Feb 1, 2011
    #12
    Thanks very much for all the friendly, helpful advice on this thread! You convinced me that javascript was the way to go, so I wrote up a simple program to test this approach. I'm no programmer, but here's what I came up with:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Simpsons Family Identification</title>
    
    
    
    <script LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
    <!-- Hide script from old browsers
    
    var familydata = new Array(
     "Homer", 1, 0, 1, 0, 1,
     "Marge", 0, 1, 1, 0, 1,
      "Bart", 1, 0, 0, 1, 1,
      "Lisa", 0, 1, 0, 1, 1,
    "Maggie", 0, 1, 0, 1, 0
    );
    
    var fObj;
    
    function checkform() {
      fObj = document.getElementById("Features");
      var test;
    // first set up an array for all matching names
      matches = new Array();
    // then go through the database line by line
      for (var i=1; i<familydata.length; i+=6) { 
        test = 0;
    	// and for each line of the database, go through the form input by input
    	for (var j=0; j<fObj.length-2; j++) {
          if ((fObj.elements[j].checked == true) && (familydata[i+j] == "0")) {
    		 test = 1;
    	  }
    	} 
    	if (test == 0) {
    		matches = matches + familydata[i-1] + '<BR>';
    	}
      }
      document.getElementById("A1").innerHTML = matches;
    }
    
    function clearform(){
      document.getElementById("A1").innerHTML = " ";
    }
    
    //-->
    </script>
    
    </head>
    
    <body>
    <table width="800" border="1" align="center">
      <tr>
        <td width="600" align="center" bgcolor="#FFFFCC"><strong>FEATURES</strong></td>
        <td align="center" bgcolor="#FFFFCC"><strong>RESULTS</strong></td>
      </tr>
      <tr valign="top">
        <td width="600" bgcolor="#FFFFFF">Trying to identify a member of the Simpsons family?<br />
    Tell us, is this person:
    <form id="Features" name="Features">
      ... <input type="radio" name="gender" value="P02"> male or <input type="radio" name="gender" value="P03"> female? <br />
      ... <input type="radio" name="age" value="P04"> an adult or <input type="radio" name="age" value="P05"> a child? <br />
      ... <input type=checkbox name="T" value="P06">  able to speak?
      <p>
      <input TYPE="button" NAME="submit" value="Submit" onClick="javascript:checkform()"> 
      <input type="reset" value="Reset" onClick="javascript:clearform()">
    </form></td>
        <td bgcolor="#FFFFFF"><div id="A1"> </div></td>
      </tr>
    </table>
    
    </body>
    </html>
    
    If you see any problems with my script or with this approach, I'd be grateful to hear about it. Otherwise, I'm going to go ahead and try this with the big plant family database. Happy programming everybody!
     

Share This Page